]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
2018-05-24 Paolo Carlini <paolo.carlini@oracle.com>
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
47
48 \f
49 /* The lexer. */
50
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
53
54 static cp_token eof_token =
55 {
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57 };
58
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 };
117
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 };
129
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
153
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
175
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 };
185
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
188
189 class type_id_in_expr_sentinel
190 {
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
200 };
201
202 /* Prototypes. */
203
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249
250 static void cp_parser_initial_pragma
251 (cp_token *);
252
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
257
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
261
262 /* Variables. */
263
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
266
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
270
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
276
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
281 {
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
285
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
287
288 if (buffer == NULL)
289 return;
290
291 if (num == 0)
292 num = buffer->length ();
293
294 if (start_token == NULL)
295 start_token = buffer->address ();
296
297 if (start_token > buffer->address ())
298 {
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
301 }
302
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
306 {
307 if (token == start_token)
308 do_print = true;
309
310 if (!do_print)
311 continue;
312
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
316
317 cp_lexer_print_token (file, token);
318
319 if (token == curr_token)
320 fprintf (file, "]]");
321
322 switch (token->type)
323 {
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
330
331 default:
332 fputc (' ', file);
333 }
334 }
335
336 if (i == num && i < buffer->length ())
337 {
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
340 }
341
342 fprintf (file, "\n");
343 }
344
345
346 /* Dump all tokens in BUFFER to stderr. */
347
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
350 {
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
352 }
353
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
356 {
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
358 }
359
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
362 {
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
367 }
368
369
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
372
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
375 {
376 if (t)
377 {
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
380 }
381 }
382
383
384 /* Dump parser context C to FILE. */
385
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
388 {
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
393 }
394
395
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
397
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
400 {
401 unsigned i;
402 cp_parser_context *c;
403
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
406 {
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
409 }
410 }
411
412
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
414
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
417 {
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
420 }
421
422
423 /* Print an unparsed function entry UF to FILE. */
424
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
427 {
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
431
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
436 {
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
442 }
443
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
447 {
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
450 }
451 fprintf (file, "\n");
452
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
456 {
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
459 }
460 fprintf (file, "\n");
461 }
462
463
464 /* Print the stack of unparsed member functions S to FILE. */
465
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
469 {
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
472
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
475 {
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
478 }
479 }
480
481
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
484
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
487 {
488 cp_token *next_token, *first_token, *start_token;
489
490 if (file == NULL)
491 file = stderr;
492
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
500 }
501
502
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
505
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
508 {
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
512
513 if (file == NULL)
514 file = stderr;
515
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
587 }
588
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
591 {
592 cp_debug_parser (stderr, &ref);
593 }
594
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
597 {
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
602 }
603
604 /* Allocate memory for a new lexer object and return it. */
605
606 static cp_lexer *
607 cp_lexer_alloc (void)
608 {
609 cp_lexer *lexer;
610
611 c_common_no_more_pch ();
612
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
615
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
618
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623
624 return lexer;
625 }
626
627
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
630
631 static cp_lexer *
632 cp_lexer_new_main (void)
633 {
634 cp_lexer *lexer;
635 cp_token token;
636
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
641
642 lexer = cp_lexer_alloc ();
643
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
646
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
649 {
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
652 }
653
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
660
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
664
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
667 }
668
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
671
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 {
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
683
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
688
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
691 }
692
693 /* Frees all resources associated with LEXER. */
694
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
697 {
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
701 }
702
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
707
708 #define LEXER_DEBUGGING_ENABLED_P false
709
710 /* Returns nonzero if debugging information should be output. */
711
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
714 {
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
717
718 return lexer->debugging_p;
719 }
720
721
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 {
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
726
727 return lexer->next_token - previous_p;
728 }
729
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 {
733 return pos;
734 }
735
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 {
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
740 }
741
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
744 {
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
749 }
750
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
753 {
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
755
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
758 {
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
761 }
762
763 return cp_lexer_token_at (lexer, tp);
764 }
765
766 /* nonzero if we are presently saving tokens. */
767
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
770 {
771 return lexer->saved_tokens.length () != 0;
772 }
773
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
778
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781 {
782 static int is_extern_c = 0;
783
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
791
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
798
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
801 {
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
803 {
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
808 }
809 else
810 {
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814 {
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
820
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
824 }
825
826 token->keyword = RID_MAX;
827 }
828 }
829 else if (token->type == CPP_AT_NAME)
830 {
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
834 {
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
842 */
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
852 }
853 }
854 }
855
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
859 {
860 if (token->type != CPP_EOF)
861 {
862 input_location = token->location;
863 }
864 }
865
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
869 {
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
872 }
873
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
876
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
879 {
880 if (cp_lexer_debugging_p (lexer))
881 {
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
885 }
886 return lexer->next_token;
887 }
888
889 /* Return true if the next token has the indicated TYPE. */
890
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 {
894 return cp_lexer_peek_token (lexer)->type == type;
895 }
896
897 /* Return true if the next token does not have the indicated TYPE. */
898
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 {
902 return !cp_lexer_next_token_is (lexer, type);
903 }
904
905 /* Return true if the next token is the indicated KEYWORD. */
906
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 {
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
911 }
912
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 {
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
917 }
918
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 {
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923 }
924
925 /* Return true if the next token is not the indicated KEYWORD. */
926
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
929 {
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
931 }
932
933 /* Return true if KEYWORD can start a decl-specifier. */
934
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
937 {
938 switch (keyword)
939 {
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
977
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
984 }
985 }
986
987 /* Return true if the next token is a keyword for a decl-specifier. */
988
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
991 {
992 cp_token *token;
993
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
996 }
997
998 /* Returns TRUE iff the token T begins a decltype type. */
999
1000 static bool
1001 token_is_decltype (cp_token *t)
1002 {
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1005 }
1006
1007 /* Returns TRUE iff the next token begins a decltype type. */
1008
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1011 {
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1014 }
1015
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1018
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1021 {
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1027 {
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1033 }
1034 /* Return the stored value. */
1035 return check_value->value;
1036 }
1037
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1043
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1046 {
1047 cp_token *token;
1048
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1051
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1055
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1060 {
1061 ++token;
1062 if (token == lexer->last_token)
1063 {
1064 token = &eof_token;
1065 break;
1066 }
1067
1068 if (!token->purged_p)
1069 --n;
1070 }
1071
1072 if (cp_lexer_debugging_p (lexer))
1073 {
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1076 }
1077
1078 return token;
1079 }
1080
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1083
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1086 {
1087 cp_token *token = lexer->next_token;
1088
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091
1092 do
1093 {
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1096 {
1097 lexer->next_token = &eof_token;
1098 break;
1099 }
1100
1101 }
1102 while (lexer->next_token->purged_p);
1103
1104 cp_lexer_set_source_position_from_token (token);
1105
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1108 {
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1112 }
1113
1114 return token;
1115 }
1116
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1120
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1123 {
1124 cp_token *tok = lexer->next_token;
1125
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1131
1132 do
1133 {
1134 tok++;
1135 if (tok == lexer->last_token)
1136 {
1137 tok = &eof_token;
1138 break;
1139 }
1140 }
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1143 }
1144
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1148
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1151 {
1152 cp_token *peek = lexer->next_token;
1153
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1156
1157 gcc_assert (tok < peek);
1158
1159 for ( tok += 1; tok != peek; tok += 1)
1160 {
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1165 }
1166 }
1167
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1170
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1173 {
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1177
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1179 }
1180
1181 /* Commit to the portion of the token stream most recently saved. */
1182
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1185 {
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1189
1190 lexer->saved_tokens.pop ();
1191 }
1192
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1195
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1198 {
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1202
1203 lexer->next_token = lexer->saved_tokens.pop ();
1204 }
1205
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1210
1211 struct saved_token_sentinel
1212 {
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1217 {
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1220 }
1221 void rollback ()
1222 {
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1225 }
1226 ~saved_token_sentinel()
1227 {
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1231 }
1232 };
1233
1234 /* Print a representation of the TOKEN on the STREAM. */
1235
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1238 {
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1252 };
1253
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1256 {
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1266
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1274
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1278
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1287 }
1288 }
1289
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1292 {
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1295 }
1296
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1299 {
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1304 }
1305
1306
1307 /* Start emitting debugging information. */
1308
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1311 {
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1315
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1318 }
1319
1320 /* Stop emitting debugging information. */
1321
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1324 {
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1328
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1331 }
1332
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1334
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1337 {
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1342 }
1343
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1346
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1349 {
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1351 {
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1355 }
1356 }
1357
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1360
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1363 {
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1365 {
1366 if (fndecl == error_mark_node)
1367 {
1368 parser->omp_declare_simd = NULL;
1369 return;
1370 }
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1372 {
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1375 }
1376 }
1377 }
1378
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1381
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1384 {
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1386 {
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1391 }
1392 }
1393 \f
1394 /* Decl-specifiers. */
1395
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1397
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400 {
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1402 }
1403
1404 /* Declarators. */
1405
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1410
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1421
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1424
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1428
1429 /* Alloc BYTES from the declarator memory pool. */
1430
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1433 {
1434 return obstack_alloc (&declarator_obstack, bytes);
1435 }
1436
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1439
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1442 {
1443 cp_declarator *declarator;
1444
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1453
1454 return declarator;
1455 }
1456
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1462
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1466 {
1467 cp_declarator *declarator;
1468
1469 /* It is valid to write:
1470
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1474
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1481
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1485
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1490
1491 return declarator;
1492 }
1493
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1498
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1502 {
1503 cp_declarator *declarator;
1504
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1510 {
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1514 }
1515 else
1516 declarator->parameter_pack_p = false;
1517
1518 declarator->std_attributes = attributes;
1519
1520 return declarator;
1521 }
1522
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1526
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1530 {
1531 cp_declarator *declarator;
1532
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1538 {
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1542 }
1543 else
1544 declarator->parameter_pack_p = false;
1545
1546 declarator->std_attributes = attributes;
1547
1548 return declarator;
1549 }
1550
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1554
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1559 {
1560 cp_declarator *declarator;
1561
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1566
1567 if (pointee)
1568 {
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1571 }
1572 else
1573 declarator->parameter_pack_p = false;
1574
1575 declarator->std_attributes = attributes;
1576
1577 return declarator;
1578 }
1579
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1584
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1595 {
1596 cp_declarator *declarator;
1597
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1609 {
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1613 }
1614 else
1615 declarator->parameter_pack_p = false;
1616
1617 return declarator;
1618 }
1619
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1622
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1625 {
1626 cp_declarator *declarator;
1627
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1632 {
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1636 }
1637 else
1638 declarator->parameter_pack_p = false;
1639
1640 return declarator;
1641 }
1642
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1647 {
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1651
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1658 {
1659 switch ((int)declarator->kind)
1660 {
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1666
1667 case cdk_error:
1668 return true;
1669
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1673 }
1674 }
1675
1676 return !found;
1677 }
1678
1679 cp_parameter_declarator *no_parameters;
1680
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1683
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1690 {
1691 cp_parameter_declarator *parameter;
1692
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1704
1705 return parameter;
1706 }
1707
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1709
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1712 {
1713 while (declarator)
1714 {
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1723 }
1724 return false;
1725 }
1726
1727 /* The parser. */
1728
1729 /* Overview
1730 --------
1731
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1739
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1743
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1752
1753 Methodology
1754 -----------
1755
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1772
1773 Future Improvements
1774 -------------------
1775
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1781
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1784
1785 enum
1786 {
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1803 };
1804
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1808
1809 /* The different kinds of declarators we want to parse. */
1810
1811 enum cp_parser_declarator_kind
1812 {
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1819 };
1820
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1824
1825 enum cp_parser_prec
1826 {
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 };
1841
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1844
1845 struct cp_parser_binary_operations_map_node
1846 {
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1853 };
1854
1855 struct cp_parser_expression_stack_entry
1856 {
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1869 };
1870
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1876
1877 /* Prototypes. */
1878
1879 /* Constructors and destructors. */
1880
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1883
1884 /* Class variables. */
1885
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1891
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 };
1924
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929
1930 /* Constructors and destructors. */
1931
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1934
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1937 {
1938 cp_parser_context *context;
1939
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1942 {
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1947 }
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1950
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1956 {
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1962 }
1963
1964 return context;
1965 }
1966
1967 /* Managing the unparsed function queues. */
1968
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1977
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1980 {
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1983 }
1984
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1987 {
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1990 }
1991
1992 /* Prototypes. */
1993
1994 /* Constructors and destructors. */
1995
1996 static cp_parser *cp_parser_new
1997 (void);
1998
1999 /* Routines to parse various constructs.
2000
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2010
2011 /* Lexical conventions [gram.lex] */
2012
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2023
2024 /* Basic concepts [gram.basic] */
2025
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2028
2029 /* Expressions [gram.expr] */
2030
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2098
2099 /* Statements [gram.stmt.stmt] */
2100
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2135
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2140
2141 /* Declarations [gram.dcl.dcl] */
2142
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2202
2203 /* Declarators [gram.dcl.decl] */
2204
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2253
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2256
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2259
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2262
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268 (cp_parser *);
2269
2270 /* Classes [gram.class] */
2271
2272 static tree cp_parser_class_name
2273 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275 (cp_parser *);
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279 (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283 (cp_parser *);
2284 static void cp_parser_member_declaration
2285 (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287 (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289 (cp_parser *);
2290
2291 /* Derived classes [gram.class.derived] */
2292
2293 static tree cp_parser_base_clause
2294 (cp_parser *);
2295 static tree cp_parser_base_specifier
2296 (cp_parser *);
2297
2298 /* Special member functions [gram.special] */
2299
2300 static tree cp_parser_conversion_function_id
2301 (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303 (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305 (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307 (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313 (cp_parser *);
2314
2315 /* Overloading [gram.over] */
2316
2317 static cp_expr cp_parser_operator_function_id
2318 (cp_parser *);
2319 static cp_expr cp_parser_operator
2320 (cp_parser *);
2321
2322 /* Templates [gram.temp] */
2323
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327 (cp_parser *);
2328 static tree cp_parser_template_parameter
2329 (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337 (cp_parser *);
2338 static tree cp_parser_template_argument
2339 (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341 (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343 (cp_parser *);
2344
2345 /* Exception handling [gram.exception] */
2346
2347 static tree cp_parser_try_block
2348 (cp_parser *);
2349 static void cp_parser_function_try_block
2350 (cp_parser *);
2351 static void cp_parser_handler_seq
2352 (cp_parser *);
2353 static void cp_parser_handler
2354 (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356 (cp_parser *);
2357 static tree cp_parser_throw_expression
2358 (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_type_id_list
2362 (cp_parser *);
2363
2364 /* GNU Extensions */
2365
2366 static tree cp_parser_asm_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379 (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391 (cp_parser *);
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397 (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2404
2405 /* Concept Extensions */
2406
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2429
2430 /* Transactional Memory Extensions */
2431
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2440
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2447 };
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2450
2451 /* Objective-C++ Productions */
2452
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2489
2490 /* Utility Routines */
2491
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2649
2650 /* Concept-related syntactic transformations */
2651
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2654
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2657 //
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2660 {
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2663 }
2664
2665 cp_unevaluated::~cp_unevaluated ()
2666 {
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2669 }
2670
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2673
2674 /* Returns nonzero if we are parsing tentatively. */
2675
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2678 {
2679 return parser->context->next != NULL;
2680 }
2681
2682 /* Returns nonzero if TOKEN is a string literal. */
2683
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2686 {
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2692 }
2693
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2696
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2699 {
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2706 }
2707
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2709
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2712 {
2713 return token->keyword == keyword;
2714 }
2715
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2718
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2721 {
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2726 }
2727
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2738
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2742 {
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2752
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2757
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2764
2765 return true;
2766 }
2767
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 RT_CLOSE_PAREN. */
2770
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2773 {
2774 switch (token_desc)
2775 {
2776 default:
2777 gcc_unreachable ();
2778 return "";
2779 case RT_CLOSE_BRACE:
2780 return "{";
2781 case RT_CLOSE_PAREN:
2782 return "(";
2783 }
2784 }
2785
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2788
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2791 {
2792 switch (token_desc)
2793 {
2794 case RT_SEMICOLON:
2795 return CPP_SEMICOLON;
2796 case RT_OPEN_PAREN:
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_BRACE;
2800 case RT_OPEN_BRACE:
2801 return CPP_OPEN_BRACE;
2802 case RT_CLOSE_SQUARE:
2803 return CPP_CLOSE_SQUARE;
2804 case RT_OPEN_SQUARE:
2805 return CPP_OPEN_SQUARE;
2806 case RT_COMMA:
2807 return CPP_COMMA;
2808 case RT_COLON:
2809 return CPP_COLON;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2812
2813 default:
2814 /* Use CPP_EOF as a "no completions possible" code. */
2815 return CPP_EOF;
2816 }
2817 }
2818
2819
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2821
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2826 OTHER-TOKEN".
2827
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2830
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2833
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 location. */
2837
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840 required_token missing_token_desc,
2841 location_t matching_location)
2842 {
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token);
2847
2848 if (token->type == CPP_PRAGMA)
2849 {
2850 error_at (token->location,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser, token);
2853 return;
2854 }
2855
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token->type == CPP_LSHIFT
2858 || token->type == CPP_RSHIFT
2859 || token->type == CPP_EQ_EQ)
2860 {
2861 location_t loc;
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2863 {
2864 error_at (loc, "version control conflict marker in file");
2865 return;
2866 }
2867 }
2868
2869 gcc_rich_location richloc (input_location);
2870
2871 bool added_matching_location = false;
2872
2873 if (missing_token_desc != RT_NONE)
2874 {
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser->lexer)->location;
2881 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2882
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location != UNKNOWN_LOCATION)
2887 added_matching_location
2888 = richloc.add_location_if_nearby (matching_location);
2889 }
2890
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2895 identifiers. */
2896 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2897 token->u.value, token->flags, &richloc);
2898
2899 if (missing_token_desc != RT_NONE)
2900 {
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location != UNKNOWN_LOCATION
2904 && !added_matching_location)
2905 inform (matching_location, "to match this %qs",
2906 get_matching_symbol (missing_token_desc));
2907 }
2908 }
2909
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2914 OTHER-TOKEN". */
2915
2916 static void
2917 cp_parser_error (cp_parser* parser, const char* gmsgid)
2918 {
2919 if (!cp_parser_simulate_error (parser))
2920 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2921 }
2922
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2927
2928 static void
2929 cp_parser_name_lookup_error (cp_parser* parser,
2930 tree name,
2931 tree decl,
2932 name_lookup_error desired,
2933 location_t location)
2934 {
2935 /* If name lookup completely failed, tell the user that NAME was not
2936 declared. */
2937 if (decl == error_mark_node)
2938 {
2939 if (parser->scope && parser->scope != global_namespace)
2940 error_at (location, "%<%E::%E%> has not been declared",
2941 parser->scope, name);
2942 else if (parser->scope == global_namespace)
2943 error_at (location, "%<::%E%> has not been declared", name);
2944 else if (parser->object_scope
2945 && !CLASS_TYPE_P (parser->object_scope))
2946 error_at (location, "request for member %qE in non-class type %qT",
2947 name, parser->object_scope);
2948 else if (parser->object_scope)
2949 error_at (location, "%<%T::%E%> has not been declared",
2950 parser->object_scope, name);
2951 else
2952 error_at (location, "%qE has not been declared", name);
2953 }
2954 else if (parser->scope && parser->scope != global_namespace)
2955 {
2956 switch (desired)
2957 {
2958 case NLE_TYPE:
2959 error_at (location, "%<%E::%E%> is not a type",
2960 parser->scope, name);
2961 break;
2962 case NLE_CXX98:
2963 error_at (location, "%<%E::%E%> is not a class or namespace",
2964 parser->scope, name);
2965 break;
2966 case NLE_NOT_CXX98:
2967 error_at (location,
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser->scope, name);
2970 break;
2971 default:
2972 gcc_unreachable ();
2973
2974 }
2975 }
2976 else if (parser->scope == global_namespace)
2977 {
2978 switch (desired)
2979 {
2980 case NLE_TYPE:
2981 error_at (location, "%<::%E%> is not a type", name);
2982 break;
2983 case NLE_CXX98:
2984 error_at (location, "%<::%E%> is not a class or namespace", name);
2985 break;
2986 case NLE_NOT_CXX98:
2987 error_at (location,
2988 "%<::%E%> is not a class, namespace, or enumeration",
2989 name);
2990 break;
2991 default:
2992 gcc_unreachable ();
2993 }
2994 }
2995 else
2996 {
2997 switch (desired)
2998 {
2999 case NLE_TYPE:
3000 error_at (location, "%qE is not a type", name);
3001 break;
3002 case NLE_CXX98:
3003 error_at (location, "%qE is not a class or namespace", name);
3004 break;
3005 case NLE_NOT_CXX98:
3006 error_at (location,
3007 "%qE is not a class, namespace, or enumeration", name);
3008 break;
3009 default:
3010 gcc_unreachable ();
3011 }
3012 }
3013 }
3014
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3018
3019 static bool
3020 cp_parser_simulate_error (cp_parser* parser)
3021 {
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3023 {
3024 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3025 return true;
3026 }
3027 return false;
3028 }
3029
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3032 issued. */
3033
3034 static bool
3035 cp_parser_check_type_definition (cp_parser* parser)
3036 {
3037 /* If types are forbidden here, issue a message. */
3038 if (parser->type_definition_forbidden_message)
3039 {
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser->type_definition_forbidden_message);
3043 return false;
3044 }
3045 return true;
3046 }
3047
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3053
3054 static void
3055 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3056 tree type, location_t type_location)
3057 {
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3061 while (declarator
3062 && (declarator->kind == cdk_pointer
3063 || declarator->kind == cdk_reference
3064 || declarator->kind == cdk_ptrmem))
3065 declarator = declarator->declarator;
3066 if (declarator
3067 && declarator->kind == cdk_function)
3068 {
3069 error_at (type_location,
3070 "new types may not be defined in a return type");
3071 inform (type_location,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3073 type);
3074 }
3075 }
3076
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3082
3083 static void
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3085 tree type,
3086 enum tag_types tag_type,
3087 location_t location)
3088 {
3089 cp_token_position start = 0;
3090
3091 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3092 {
3093 if (TREE_CODE (type) == TYPE_DECL)
3094 type = TREE_TYPE (type);
3095 if (TYPE_P (type) && !template_placeholder_p (type))
3096 error_at (location, "%qT is not a template", type);
3097 else if (identifier_p (type))
3098 {
3099 if (tag_type != none_type)
3100 error_at (location, "%qE is not a class template", type);
3101 else
3102 error_at (location, "%qE is not a template", type);
3103 }
3104 else
3105 error_at (location, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3108 start = cp_lexer_token_position (parser->lexer, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser->lexer);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3115 if (start)
3116 cp_lexer_purge_tokens_after (parser->lexer, start);
3117 }
3118 }
3119
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3124
3125 static bool
3126 cp_parser_non_integral_constant_expression (cp_parser *parser,
3127 non_integral_constant thing)
3128 {
3129 parser->non_integral_constant_expression_p = true;
3130 if (parser->integral_constant_expression_p)
3131 {
3132 if (!parser->allow_non_integral_constant_expression_p)
3133 {
3134 const char *msg = NULL;
3135 switch (thing)
3136 {
3137 case NIC_FLOAT:
3138 pedwarn (input_location, OPT_Wpedantic,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3141 return true;
3142 case NIC_CAST:
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3146 return true;
3147 case NIC_TYPEID:
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3150 return true;
3151 case NIC_NCC:
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3154 return true;
3155 case NIC_FUNC_CALL:
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3158 return true;
3159 case NIC_INC:
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3162 return true;
3163 case NIC_DEC:
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_ARRAY_REF:
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_ADDR_LABEL:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_OVERLOADED:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_ASSIGNMENT:
3180 error ("an assignment cannot appear in a constant-expression");
3181 return true;
3182 case NIC_COMMA:
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_CONSTRUCTOR:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_TRANSACTION:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_THIS:
3195 msg = "this";
3196 break;
3197 case NIC_FUNC_NAME:
3198 msg = "__FUNCTION__";
3199 break;
3200 case NIC_PRETTY_FUNC:
3201 msg = "__PRETTY_FUNCTION__";
3202 break;
3203 case NIC_C99_FUNC:
3204 msg = "__func__";
3205 break;
3206 case NIC_VA_ARG:
3207 msg = "va_arg";
3208 break;
3209 case NIC_ARROW:
3210 msg = "->";
3211 break;
3212 case NIC_POINT:
3213 msg = ".";
3214 break;
3215 case NIC_STAR:
3216 msg = "*";
3217 break;
3218 case NIC_ADDR:
3219 msg = "&";
3220 break;
3221 case NIC_PREINCREMENT:
3222 msg = "++";
3223 break;
3224 case NIC_PREDECREMENT:
3225 msg = "--";
3226 break;
3227 case NIC_NEW:
3228 msg = "new";
3229 break;
3230 case NIC_DEL:
3231 msg = "delete";
3232 break;
3233 default:
3234 gcc_unreachable ();
3235 }
3236 if (msg)
3237 error ("%qs cannot appear in a constant-expression", msg);
3238 return true;
3239 }
3240 }
3241 return false;
3242 }
3243
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3248
3249 static void
3250 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3251 location_t location)
3252 {
3253 tree decl, ambiguous_decls;
3254 cp_parser_commit_to_tentative_parse (parser);
3255 /* Try to lookup the identifier. */
3256 decl = cp_parser_lookup_name (parser, id, none_type,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls, location);
3261 if (ambiguous_decls)
3262 /* If the lookup was ambiguous, an error will already have
3263 been issued. */
3264 return;
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl))
3268 {
3269 error_at (location,
3270 "invalid use of template-name %qE without an argument list",
3271 decl);
3272 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3273 inform (location, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3276 }
3277 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3278 error_at (location, "invalid use of destructor %qD as a type", id);
3279 else if (TREE_CODE (decl) == TYPE_DECL)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location, "invalid combination of multiple type-specifiers");
3282 else if (!parser->scope)
3283 {
3284 /* Issue an error message. */
3285 name_hint hint;
3286 if (TREE_CODE (id) == IDENTIFIER_NODE)
3287 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3288 if (hint)
3289 {
3290 gcc_rich_location richloc (location);
3291 richloc.add_fixit_replace (hint.suggestion ());
3292 error_at (&richloc,
3293 "%qE does not name a type; did you mean %qs?",
3294 id, hint.suggestion ());
3295 }
3296 else
3297 error_at (location, "%qE does not name a type", id);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3300
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3303
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3306 inform (location, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3309 inform (location, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect < cxx11
3312 && TREE_CODE (id) == IDENTIFIER_NODE
3313 && id_equal (id, "thread_local"))
3314 inform (location, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3317 inform (location, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl && current_class_type
3319 && TYPE_BINFO (current_class_type))
3320 {
3321 tree b;
3322
3323 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3324 b;
3325 b = TREE_CHAIN (b))
3326 {
3327 tree base_type = BINFO_TYPE (b);
3328 if (CLASS_TYPE_P (base_type)
3329 && dependent_type_p (base_type))
3330 {
3331 tree field;
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3336 for (field = TYPE_FIELDS (base_type);
3337 field;
3338 field = DECL_CHAIN (field))
3339 if (TREE_CODE (field) == TYPE_DECL
3340 && DECL_NAME (field) == id)
3341 {
3342 inform (location,
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b), id);
3345 break;
3346 }
3347 if (field)
3348 break;
3349 }
3350 }
3351 }
3352 }
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser->scope != error_mark_node)
3356 {
3357 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3358 {
3359 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3360 error_at (location_of (id),
3361 "%qE in namespace %qE does not name a template type",
3362 id, parser->scope);
3363 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3364 error_at (location_of (id),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id, 0), parser->scope);
3367 else
3368 error_at (location_of (id),
3369 "%qE in namespace %qE does not name a type",
3370 id, parser->scope);
3371 if (DECL_P (decl))
3372 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373 else if (decl == error_mark_node)
3374 suggest_alternative_in_explicit_scope (location, id,
3375 parser->scope);
3376 }
3377 else if (CLASS_TYPE_P (parser->scope)
3378 && constructor_name_p (id, parser->scope))
3379 {
3380 /* A<T>::A<T>() */
3381 error_at (location, "%<%T::%E%> names the constructor, not"
3382 " the type", parser->scope, id);
3383 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3384 error_at (location, "and %qT has no template constructors",
3385 parser->scope);
3386 }
3387 else if (TYPE_P (parser->scope)
3388 && dependent_scope_p (parser->scope))
3389 {
3390 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3391 error_at (location,
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser->scope),
3395 TYPENAME_TYPE_FULLNAME (parser->scope),
3396 id,
3397 TYPE_CONTEXT (parser->scope),
3398 TYPENAME_TYPE_FULLNAME (parser->scope));
3399 else
3400 error_at (location, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser->scope, id, parser->scope);
3403 }
3404 else if (TYPE_P (parser->scope))
3405 {
3406 if (!COMPLETE_TYPE_P (parser->scope))
3407 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3408 parser->scope);
3409 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410 error_at (location_of (id),
3411 "%qE in %q#T does not name a template type",
3412 id, parser->scope);
3413 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id, 0), parser->scope);
3417 else
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a type",
3420 id, parser->scope);
3421 if (DECL_P (decl))
3422 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3423 }
3424 else
3425 gcc_unreachable ();
3426 }
3427 }
3428
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3432
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3438
3439 static bool
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3441 {
3442 tree id;
3443 cp_token *token = cp_lexer_peek_token (parser->lexer);
3444
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3447 {
3448 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3449 if (next->type == CPP_NAME && next->error_reported)
3450 goto out;
3451 }
3452
3453 cp_parser_parse_tentatively (parser);
3454 id = cp_parser_id_expression (parser,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3463 || TREE_CODE (id) == TYPE_DECL)
3464 {
3465 cp_parser_abort_tentative_parse (parser);
3466 return false;
3467 }
3468 if (!cp_parser_parse_definitely (parser))
3469 return false;
3470
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3473 out:
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser->in_declarator_p)
3478 cp_parser_skip_to_end_of_block_or_statement (parser);
3479 return true;
3480 }
3481
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3486
3487 static int
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3489 bool recovering,
3490 cpp_ttype or_ttype,
3491 bool consume_paren)
3492 {
3493 unsigned paren_depth = 0;
3494 unsigned brace_depth = 0;
3495 unsigned square_depth = 0;
3496 unsigned condop_depth = 0;
3497
3498 if (recovering && or_ttype == CPP_EOF
3499 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3500 return 0;
3501
3502 while (true)
3503 {
3504 cp_token * token = cp_lexer_peek_token (parser->lexer);
3505
3506 /* Have we found what we're looking for before the closing paren? */
3507 if (token->type == or_ttype && or_ttype != CPP_EOF
3508 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3509 return -1;
3510
3511 switch (token->type)
3512 {
3513 case CPP_EOF:
3514 case CPP_PRAGMA_EOL:
3515 /* If we've run out of tokens, then there is no closing `)'. */
3516 return 0;
3517
3518 /* This is good for lambda expression capture-lists. */
3519 case CPP_OPEN_SQUARE:
3520 ++square_depth;
3521 break;
3522 case CPP_CLOSE_SQUARE:
3523 if (!square_depth--)
3524 return 0;
3525 break;
3526
3527 case CPP_SEMICOLON:
3528 /* This matches the processing in skip_to_end_of_statement. */
3529 if (!brace_depth)
3530 return 0;
3531 break;
3532
3533 case CPP_OPEN_BRACE:
3534 ++brace_depth;
3535 break;
3536 case CPP_CLOSE_BRACE:
3537 if (!brace_depth--)
3538 return 0;
3539 break;
3540
3541 case CPP_OPEN_PAREN:
3542 if (!brace_depth)
3543 ++paren_depth;
3544 break;
3545
3546 case CPP_CLOSE_PAREN:
3547 if (!brace_depth && !paren_depth--)
3548 {
3549 if (consume_paren)
3550 cp_lexer_consume_token (parser->lexer);
3551 return 1;
3552 }
3553 break;
3554
3555 case CPP_QUERY:
3556 if (!brace_depth && !paren_depth && !square_depth)
3557 ++condop_depth;
3558 break;
3559
3560 case CPP_COLON:
3561 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3562 condop_depth--;
3563 break;
3564
3565 default:
3566 break;
3567 }
3568
3569 /* Consume the token. */
3570 cp_lexer_consume_token (parser->lexer);
3571 }
3572 }
3573
3574 /* Consume tokens up to, and including, the next non-nested closing `)'.
3575 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3576 are doing error recovery. Returns -1 if OR_COMMA is true and we
3577 found an unnested token of that type. */
3578
3579 static int
3580 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3581 bool recovering,
3582 bool or_comma,
3583 bool consume_paren)
3584 {
3585 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3586 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3587 ttype, consume_paren);
3588 }
3589
3590 /* Consume tokens until we reach the end of the current statement.
3591 Normally, that will be just before consuming a `;'. However, if a
3592 non-nested `}' comes first, then we stop before consuming that. */
3593
3594 static void
3595 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3596 {
3597 unsigned nesting_depth = 0;
3598
3599 /* Unwind generic function template scope if necessary. */
3600 if (parser->fully_implicit_function_template_p)
3601 abort_fully_implicit_template (parser);
3602
3603 while (true)
3604 {
3605 cp_token *token = cp_lexer_peek_token (parser->lexer);
3606
3607 switch (token->type)
3608 {
3609 case CPP_EOF:
3610 case CPP_PRAGMA_EOL:
3611 /* If we've run out of tokens, stop. */
3612 return;
3613
3614 case CPP_SEMICOLON:
3615 /* If the next token is a `;', we have reached the end of the
3616 statement. */
3617 if (!nesting_depth)
3618 return;
3619 break;
3620
3621 case CPP_CLOSE_BRACE:
3622 /* If this is a non-nested '}', stop before consuming it.
3623 That way, when confronted with something like:
3624
3625 { 3 + }
3626
3627 we stop before consuming the closing '}', even though we
3628 have not yet reached a `;'. */
3629 if (nesting_depth == 0)
3630 return;
3631
3632 /* If it is the closing '}' for a block that we have
3633 scanned, stop -- but only after consuming the token.
3634 That way given:
3635
3636 void f g () { ... }
3637 typedef int I;
3638
3639 we will stop after the body of the erroneously declared
3640 function, but before consuming the following `typedef'
3641 declaration. */
3642 if (--nesting_depth == 0)
3643 {
3644 cp_lexer_consume_token (parser->lexer);
3645 return;
3646 }
3647 break;
3648
3649 case CPP_OPEN_BRACE:
3650 ++nesting_depth;
3651 break;
3652
3653 default:
3654 break;
3655 }
3656
3657 /* Consume the token. */
3658 cp_lexer_consume_token (parser->lexer);
3659 }
3660 }
3661
3662 /* This function is called at the end of a statement or declaration.
3663 If the next token is a semicolon, it is consumed; otherwise, error
3664 recovery is attempted. */
3665
3666 static void
3667 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3668 {
3669 /* Look for the trailing `;'. */
3670 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3671 {
3672 /* If there is additional (erroneous) input, skip to the end of
3673 the statement. */
3674 cp_parser_skip_to_end_of_statement (parser);
3675 /* If the next token is now a `;', consume it. */
3676 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3677 cp_lexer_consume_token (parser->lexer);
3678 }
3679 }
3680
3681 /* Skip tokens until we have consumed an entire block, or until we
3682 have consumed a non-nested `;'. */
3683
3684 static void
3685 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3686 {
3687 int nesting_depth = 0;
3688
3689 /* Unwind generic function template scope if necessary. */
3690 if (parser->fully_implicit_function_template_p)
3691 abort_fully_implicit_template (parser);
3692
3693 while (nesting_depth >= 0)
3694 {
3695 cp_token *token = cp_lexer_peek_token (parser->lexer);
3696
3697 switch (token->type)
3698 {
3699 case CPP_EOF:
3700 case CPP_PRAGMA_EOL:
3701 /* If we've run out of tokens, stop. */
3702 return;
3703
3704 case CPP_SEMICOLON:
3705 /* Stop if this is an unnested ';'. */
3706 if (!nesting_depth)
3707 nesting_depth = -1;
3708 break;
3709
3710 case CPP_CLOSE_BRACE:
3711 /* Stop if this is an unnested '}', or closes the outermost
3712 nesting level. */
3713 nesting_depth--;
3714 if (nesting_depth < 0)
3715 return;
3716 if (!nesting_depth)
3717 nesting_depth = -1;
3718 break;
3719
3720 case CPP_OPEN_BRACE:
3721 /* Nest. */
3722 nesting_depth++;
3723 break;
3724
3725 default:
3726 break;
3727 }
3728
3729 /* Consume the token. */
3730 cp_lexer_consume_token (parser->lexer);
3731 }
3732 }
3733
3734 /* Skip tokens until a non-nested closing curly brace is the next
3735 token, or there are no more tokens. Return true in the first case,
3736 false otherwise. */
3737
3738 static bool
3739 cp_parser_skip_to_closing_brace (cp_parser *parser)
3740 {
3741 unsigned nesting_depth = 0;
3742
3743 while (true)
3744 {
3745 cp_token *token = cp_lexer_peek_token (parser->lexer);
3746
3747 switch (token->type)
3748 {
3749 case CPP_EOF:
3750 case CPP_PRAGMA_EOL:
3751 /* If we've run out of tokens, stop. */
3752 return false;
3753
3754 case CPP_CLOSE_BRACE:
3755 /* If the next token is a non-nested `}', then we have reached
3756 the end of the current block. */
3757 if (nesting_depth-- == 0)
3758 return true;
3759 break;
3760
3761 case CPP_OPEN_BRACE:
3762 /* If it the next token is a `{', then we are entering a new
3763 block. Consume the entire block. */
3764 ++nesting_depth;
3765 break;
3766
3767 default:
3768 break;
3769 }
3770
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser->lexer);
3773 }
3774 }
3775
3776 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3777 parameter is the PRAGMA token, allowing us to purge the entire pragma
3778 sequence. */
3779
3780 static void
3781 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3782 {
3783 cp_token *token;
3784
3785 parser->lexer->in_pragma = false;
3786
3787 do
3788 token = cp_lexer_consume_token (parser->lexer);
3789 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3790
3791 /* Ensure that the pragma is not parsed again. */
3792 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3793 }
3794
3795 /* Require pragma end of line, resyncing with it as necessary. The
3796 arguments are as for cp_parser_skip_to_pragma_eol. */
3797
3798 static void
3799 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3800 {
3801 parser->lexer->in_pragma = false;
3802 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3803 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3804 }
3805
3806 /* This is a simple wrapper around make_typename_type. When the id is
3807 an unresolved identifier node, we can provide a superior diagnostic
3808 using cp_parser_diagnose_invalid_type_name. */
3809
3810 static tree
3811 cp_parser_make_typename_type (cp_parser *parser, tree id,
3812 location_t id_location)
3813 {
3814 tree result;
3815 if (identifier_p (id))
3816 {
3817 result = make_typename_type (parser->scope, id, typename_type,
3818 /*complain=*/tf_none);
3819 if (result == error_mark_node)
3820 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3821 return result;
3822 }
3823 return make_typename_type (parser->scope, id, typename_type, tf_error);
3824 }
3825
3826 /* This is a wrapper around the
3827 make_{pointer,ptrmem,reference}_declarator functions that decides
3828 which one to call based on the CODE and CLASS_TYPE arguments. The
3829 CODE argument should be one of the values returned by
3830 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3831 appertain to the pointer or reference. */
3832
3833 static cp_declarator *
3834 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3835 cp_cv_quals cv_qualifiers,
3836 cp_declarator *target,
3837 tree attributes)
3838 {
3839 if (code == ERROR_MARK || target == cp_error_declarator)
3840 return cp_error_declarator;
3841
3842 if (code == INDIRECT_REF)
3843 if (class_type == NULL_TREE)
3844 return make_pointer_declarator (cv_qualifiers, target, attributes);
3845 else
3846 return make_ptrmem_declarator (cv_qualifiers, class_type,
3847 target, attributes);
3848 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3849 return make_reference_declarator (cv_qualifiers, target,
3850 false, attributes);
3851 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3852 return make_reference_declarator (cv_qualifiers, target,
3853 true, attributes);
3854 gcc_unreachable ();
3855 }
3856
3857 /* Create a new C++ parser. */
3858
3859 static cp_parser *
3860 cp_parser_new (void)
3861 {
3862 cp_parser *parser;
3863 cp_lexer *lexer;
3864 unsigned i;
3865
3866 /* cp_lexer_new_main is called before doing GC allocation because
3867 cp_lexer_new_main might load a PCH file. */
3868 lexer = cp_lexer_new_main ();
3869
3870 /* Initialize the binops_by_token so that we can get the tree
3871 directly from the token. */
3872 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3873 binops_by_token[binops[i].token_type] = binops[i];
3874
3875 parser = ggc_cleared_alloc<cp_parser> ();
3876 parser->lexer = lexer;
3877 parser->context = cp_parser_context_new (NULL);
3878
3879 /* For now, we always accept GNU extensions. */
3880 parser->allow_gnu_extensions_p = 1;
3881
3882 /* The `>' token is a greater-than operator, not the end of a
3883 template-id. */
3884 parser->greater_than_is_operator_p = true;
3885
3886 parser->default_arg_ok_p = true;
3887
3888 /* We are not parsing a constant-expression. */
3889 parser->integral_constant_expression_p = false;
3890 parser->allow_non_integral_constant_expression_p = false;
3891 parser->non_integral_constant_expression_p = false;
3892
3893 /* Local variable names are not forbidden. */
3894 parser->local_variables_forbidden_p = false;
3895
3896 /* We are not processing an `extern "C"' declaration. */
3897 parser->in_unbraced_linkage_specification_p = false;
3898
3899 /* We are not processing a declarator. */
3900 parser->in_declarator_p = false;
3901
3902 /* We are not processing a template-argument-list. */
3903 parser->in_template_argument_list_p = false;
3904
3905 /* We are not in an iteration statement. */
3906 parser->in_statement = 0;
3907
3908 /* We are not in a switch statement. */
3909 parser->in_switch_statement_p = false;
3910
3911 /* We are not parsing a type-id inside an expression. */
3912 parser->in_type_id_in_expr_p = false;
3913
3914 /* Declarations aren't implicitly extern "C". */
3915 parser->implicit_extern_c = false;
3916
3917 /* String literals should be translated to the execution character set. */
3918 parser->translate_strings_p = true;
3919
3920 /* We are not parsing a function body. */
3921 parser->in_function_body = false;
3922
3923 /* We can correct until told otherwise. */
3924 parser->colon_corrects_to_scope_p = true;
3925
3926 /* The unparsed function queue is empty. */
3927 push_unparsed_function_queues (parser);
3928
3929 /* There are no classes being defined. */
3930 parser->num_classes_being_defined = 0;
3931
3932 /* No template parameters apply. */
3933 parser->num_template_parameter_lists = 0;
3934
3935 /* Special parsing data structures. */
3936 parser->omp_declare_simd = NULL;
3937 parser->oacc_routine = NULL;
3938
3939 /* Not declaring an implicit function template. */
3940 parser->auto_is_implicit_function_template_parm_p = false;
3941 parser->fully_implicit_function_template_p = false;
3942 parser->implicit_template_parms = 0;
3943 parser->implicit_template_scope = 0;
3944
3945 /* Allow constrained-type-specifiers. */
3946 parser->prevent_constrained_type_specifiers = 0;
3947
3948 /* We haven't yet seen an 'extern "C"'. */
3949 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3950
3951 return parser;
3952 }
3953
3954 /* Create a cp_lexer structure which will emit the tokens in CACHE
3955 and push it onto the parser's lexer stack. This is used for delayed
3956 parsing of in-class method bodies and default arguments, and should
3957 not be confused with tentative parsing. */
3958 static void
3959 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3960 {
3961 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3962 lexer->next = parser->lexer;
3963 parser->lexer = lexer;
3964
3965 /* Move the current source position to that of the first token in the
3966 new lexer. */
3967 cp_lexer_set_source_position_from_token (lexer->next_token);
3968 }
3969
3970 /* Pop the top lexer off the parser stack. This is never used for the
3971 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3972 static void
3973 cp_parser_pop_lexer (cp_parser *parser)
3974 {
3975 cp_lexer *lexer = parser->lexer;
3976 parser->lexer = lexer->next;
3977 cp_lexer_destroy (lexer);
3978
3979 /* Put the current source position back where it was before this
3980 lexer was pushed. */
3981 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3982 }
3983
3984 /* Lexical conventions [gram.lex] */
3985
3986 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3987 identifier. */
3988
3989 static cp_expr
3990 cp_parser_identifier (cp_parser* parser)
3991 {
3992 cp_token *token;
3993
3994 /* Look for the identifier. */
3995 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3996 /* Return the value. */
3997 if (token)
3998 return cp_expr (token->u.value, token->location);
3999 else
4000 return error_mark_node;
4001 }
4002
4003 /* Parse a sequence of adjacent string constants. Returns a
4004 TREE_STRING representing the combined, nul-terminated string
4005 constant. If TRANSLATE is true, translate the string to the
4006 execution character set. If WIDE_OK is true, a wide string is
4007 invalid here.
4008
4009 C++98 [lex.string] says that if a narrow string literal token is
4010 adjacent to a wide string literal token, the behavior is undefined.
4011 However, C99 6.4.5p4 says that this results in a wide string literal.
4012 We follow C99 here, for consistency with the C front end.
4013
4014 This code is largely lifted from lex_string() in c-lex.c.
4015
4016 FUTURE: ObjC++ will need to handle @-strings here. */
4017 static cp_expr
4018 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4019 bool lookup_udlit = true)
4020 {
4021 tree value;
4022 size_t count;
4023 struct obstack str_ob;
4024 cpp_string str, istr, *strs;
4025 cp_token *tok;
4026 enum cpp_ttype type, curr_type;
4027 int have_suffix_p = 0;
4028 tree string_tree;
4029 tree suffix_id = NULL_TREE;
4030 bool curr_tok_is_userdef_p = false;
4031
4032 tok = cp_lexer_peek_token (parser->lexer);
4033 if (!cp_parser_is_string_literal (tok))
4034 {
4035 cp_parser_error (parser, "expected string-literal");
4036 return error_mark_node;
4037 }
4038
4039 location_t loc = tok->location;
4040
4041 if (cpp_userdef_string_p (tok->type))
4042 {
4043 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4044 curr_type = cpp_userdef_string_remove_type (tok->type);
4045 curr_tok_is_userdef_p = true;
4046 }
4047 else
4048 {
4049 string_tree = tok->u.value;
4050 curr_type = tok->type;
4051 }
4052 type = curr_type;
4053
4054 /* Try to avoid the overhead of creating and destroying an obstack
4055 for the common case of just one string. */
4056 if (!cp_parser_is_string_literal
4057 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4058 {
4059 cp_lexer_consume_token (parser->lexer);
4060
4061 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4062 str.len = TREE_STRING_LENGTH (string_tree);
4063 count = 1;
4064
4065 if (curr_tok_is_userdef_p)
4066 {
4067 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4068 have_suffix_p = 1;
4069 curr_type = cpp_userdef_string_remove_type (tok->type);
4070 }
4071 else
4072 curr_type = tok->type;
4073
4074 strs = &str;
4075 }
4076 else
4077 {
4078 location_t last_tok_loc = tok->location;
4079 gcc_obstack_init (&str_ob);
4080 count = 0;
4081
4082 do
4083 {
4084 cp_lexer_consume_token (parser->lexer);
4085 count++;
4086 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4087 str.len = TREE_STRING_LENGTH (string_tree);
4088
4089 if (curr_tok_is_userdef_p)
4090 {
4091 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4092 if (have_suffix_p == 0)
4093 {
4094 suffix_id = curr_suffix_id;
4095 have_suffix_p = 1;
4096 }
4097 else if (have_suffix_p == 1
4098 && curr_suffix_id != suffix_id)
4099 {
4100 error ("inconsistent user-defined literal suffixes"
4101 " %qD and %qD in string literal",
4102 suffix_id, curr_suffix_id);
4103 have_suffix_p = -1;
4104 }
4105 curr_type = cpp_userdef_string_remove_type (tok->type);
4106 }
4107 else
4108 curr_type = tok->type;
4109
4110 if (type != curr_type)
4111 {
4112 if (type == CPP_STRING)
4113 type = curr_type;
4114 else if (curr_type != CPP_STRING)
4115 {
4116 rich_location rich_loc (line_table, tok->location);
4117 rich_loc.add_range (last_tok_loc, false);
4118 error_at (&rich_loc,
4119 "unsupported non-standard concatenation "
4120 "of string literals");
4121 }
4122 }
4123
4124 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4125
4126 last_tok_loc = tok->location;
4127
4128 tok = cp_lexer_peek_token (parser->lexer);
4129 if (cpp_userdef_string_p (tok->type))
4130 {
4131 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4132 curr_type = cpp_userdef_string_remove_type (tok->type);
4133 curr_tok_is_userdef_p = true;
4134 }
4135 else
4136 {
4137 string_tree = tok->u.value;
4138 curr_type = tok->type;
4139 curr_tok_is_userdef_p = false;
4140 }
4141 }
4142 while (cp_parser_is_string_literal (tok));
4143
4144 /* A string literal built by concatenation has its caret=start at
4145 the start of the initial string, and its finish at the finish of
4146 the final string literal. */
4147 loc = make_location (loc, loc, get_finish (last_tok_loc));
4148
4149 strs = (cpp_string *) obstack_finish (&str_ob);
4150 }
4151
4152 if (type != CPP_STRING && !wide_ok)
4153 {
4154 cp_parser_error (parser, "a wide string is invalid in this context");
4155 type = CPP_STRING;
4156 }
4157
4158 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4159 (parse_in, strs, count, &istr, type))
4160 {
4161 value = build_string (istr.len, (const char *)istr.text);
4162 free (CONST_CAST (unsigned char *, istr.text));
4163
4164 switch (type)
4165 {
4166 default:
4167 case CPP_STRING:
4168 case CPP_UTF8STRING:
4169 TREE_TYPE (value) = char_array_type_node;
4170 break;
4171 case CPP_STRING16:
4172 TREE_TYPE (value) = char16_array_type_node;
4173 break;
4174 case CPP_STRING32:
4175 TREE_TYPE (value) = char32_array_type_node;
4176 break;
4177 case CPP_WSTRING:
4178 TREE_TYPE (value) = wchar_array_type_node;
4179 break;
4180 }
4181
4182 value = fix_string_type (value);
4183
4184 if (have_suffix_p)
4185 {
4186 tree literal = build_userdef_literal (suffix_id, value,
4187 OT_NONE, NULL_TREE);
4188 if (lookup_udlit)
4189 value = cp_parser_userdef_string_literal (literal);
4190 else
4191 value = literal;
4192 }
4193 }
4194 else
4195 /* cpp_interpret_string has issued an error. */
4196 value = error_mark_node;
4197
4198 if (count > 1)
4199 obstack_free (&str_ob, 0);
4200
4201 return cp_expr (value, loc);
4202 }
4203
4204 /* Look up a literal operator with the name and the exact arguments. */
4205
4206 static tree
4207 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4208 {
4209 tree decl;
4210 decl = lookup_name (name);
4211 if (!decl || !is_overloaded_fn (decl))
4212 return error_mark_node;
4213
4214 for (lkp_iterator iter (decl); iter; ++iter)
4215 {
4216 unsigned int ix;
4217 bool found = true;
4218 tree fn = *iter;
4219 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4220 if (parmtypes != NULL_TREE)
4221 {
4222 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4223 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4224 {
4225 tree tparm = TREE_VALUE (parmtypes);
4226 tree targ = TREE_TYPE ((*args)[ix]);
4227 bool ptr = TYPE_PTR_P (tparm);
4228 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4229 if ((ptr || arr || !same_type_p (tparm, targ))
4230 && (!ptr || !arr
4231 || !same_type_p (TREE_TYPE (tparm),
4232 TREE_TYPE (targ))))
4233 found = false;
4234 }
4235 if (found
4236 && ix == vec_safe_length (args)
4237 /* May be this should be sufficient_parms_p instead,
4238 depending on how exactly should user-defined literals
4239 work in presence of default arguments on the literal
4240 operator parameters. */
4241 && parmtypes == void_list_node)
4242 return decl;
4243 }
4244 }
4245
4246 return error_mark_node;
4247 }
4248
4249 /* Parse a user-defined char constant. Returns a call to a user-defined
4250 literal operator taking the character as an argument. */
4251
4252 static cp_expr
4253 cp_parser_userdef_char_literal (cp_parser *parser)
4254 {
4255 cp_token *token = cp_lexer_consume_token (parser->lexer);
4256 tree literal = token->u.value;
4257 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4258 tree value = USERDEF_LITERAL_VALUE (literal);
4259 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4260 tree decl, result;
4261
4262 /* Build up a call to the user-defined operator */
4263 /* Lookup the name we got back from the id-expression. */
4264 vec<tree, va_gc> *args = make_tree_vector ();
4265 vec_safe_push (args, value);
4266 decl = lookup_literal_operator (name, args);
4267 if (!decl || decl == error_mark_node)
4268 {
4269 error ("unable to find character literal operator %qD with %qT argument",
4270 name, TREE_TYPE (value));
4271 release_tree_vector (args);
4272 return error_mark_node;
4273 }
4274 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4275 release_tree_vector (args);
4276 return result;
4277 }
4278
4279 /* A subroutine of cp_parser_userdef_numeric_literal to
4280 create a char... template parameter pack from a string node. */
4281
4282 static tree
4283 make_char_string_pack (tree value)
4284 {
4285 tree charvec;
4286 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4287 const char *str = TREE_STRING_POINTER (value);
4288 int i, len = TREE_STRING_LENGTH (value) - 1;
4289 tree argvec = make_tree_vec (1);
4290
4291 /* Fill in CHARVEC with all of the parameters. */
4292 charvec = make_tree_vec (len);
4293 for (i = 0; i < len; ++i)
4294 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4295
4296 /* Build the argument packs. */
4297 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4298
4299 TREE_VEC_ELT (argvec, 0) = argpack;
4300
4301 return argvec;
4302 }
4303
4304 /* A subroutine of cp_parser_userdef_numeric_literal to
4305 create a char... template parameter pack from a string node. */
4306
4307 static tree
4308 make_string_pack (tree value)
4309 {
4310 tree charvec;
4311 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4312 const unsigned char *str
4313 = (const unsigned char *) TREE_STRING_POINTER (value);
4314 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4315 int len = TREE_STRING_LENGTH (value) / sz - 1;
4316 tree argvec = make_tree_vec (2);
4317
4318 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4319 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4320
4321 /* First template parm is character type. */
4322 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4323
4324 /* Fill in CHARVEC with all of the parameters. */
4325 charvec = make_tree_vec (len);
4326 for (int i = 0; i < len; ++i)
4327 TREE_VEC_ELT (charvec, i)
4328 = double_int_to_tree (str_char_type_node,
4329 double_int::from_buffer (str + i * sz, sz));
4330
4331 /* Build the argument packs. */
4332 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4333
4334 TREE_VEC_ELT (argvec, 1) = argpack;
4335
4336 return argvec;
4337 }
4338
4339 /* Parse a user-defined numeric constant. returns a call to a user-defined
4340 literal operator. */
4341
4342 static cp_expr
4343 cp_parser_userdef_numeric_literal (cp_parser *parser)
4344 {
4345 cp_token *token = cp_lexer_consume_token (parser->lexer);
4346 tree literal = token->u.value;
4347 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4348 tree value = USERDEF_LITERAL_VALUE (literal);
4349 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4350 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4351 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4352 tree decl, result;
4353 vec<tree, va_gc> *args;
4354
4355 /* Look for a literal operator taking the exact type of numeric argument
4356 as the literal value. */
4357 args = make_tree_vector ();
4358 vec_safe_push (args, value);
4359 decl = lookup_literal_operator (name, args);
4360 if (decl && decl != error_mark_node)
4361 {
4362 result = finish_call_expr (decl, &args, false, true,
4363 tf_warning_or_error);
4364
4365 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4366 {
4367 warning_at (token->location, OPT_Woverflow,
4368 "integer literal exceeds range of %qT type",
4369 long_long_unsigned_type_node);
4370 }
4371 else
4372 {
4373 if (overflow > 0)
4374 warning_at (token->location, OPT_Woverflow,
4375 "floating literal exceeds range of %qT type",
4376 long_double_type_node);
4377 else if (overflow < 0)
4378 warning_at (token->location, OPT_Woverflow,
4379 "floating literal truncated to zero");
4380 }
4381
4382 release_tree_vector (args);
4383 return result;
4384 }
4385 release_tree_vector (args);
4386
4387 /* If the numeric argument didn't work, look for a raw literal
4388 operator taking a const char* argument consisting of the number
4389 in string format. */
4390 args = make_tree_vector ();
4391 vec_safe_push (args, num_string);
4392 decl = lookup_literal_operator (name, args);
4393 if (decl && decl != error_mark_node)
4394 {
4395 result = finish_call_expr (decl, &args, false, true,
4396 tf_warning_or_error);
4397 release_tree_vector (args);
4398 return result;
4399 }
4400 release_tree_vector (args);
4401
4402 /* If the raw literal didn't work, look for a non-type template
4403 function with parameter pack char.... Call the function with
4404 template parameter characters representing the number. */
4405 args = make_tree_vector ();
4406 decl = lookup_literal_operator (name, args);
4407 if (decl && decl != error_mark_node)
4408 {
4409 tree tmpl_args = make_char_string_pack (num_string);
4410 decl = lookup_template_function (decl, tmpl_args);
4411 result = finish_call_expr (decl, &args, false, true,
4412 tf_warning_or_error);
4413 release_tree_vector (args);
4414 return result;
4415 }
4416
4417 release_tree_vector (args);
4418
4419 /* In C++14 the standard library defines complex number suffixes that
4420 conflict with GNU extensions. Prefer them if <complex> is #included. */
4421 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4422 bool i14 = (cxx_dialect > cxx11
4423 && (id_equal (suffix_id, "i")
4424 || id_equal (suffix_id, "if")
4425 || id_equal (suffix_id, "il")));
4426 diagnostic_t kind = DK_ERROR;
4427 int opt = 0;
4428
4429 if (i14 && ext)
4430 {
4431 tree cxlit = lookup_qualified_name (std_node,
4432 get_identifier ("complex_literals"),
4433 0, false, false);
4434 if (cxlit == error_mark_node)
4435 {
4436 /* No <complex>, so pedwarn and use GNU semantics. */
4437 kind = DK_PEDWARN;
4438 opt = OPT_Wpedantic;
4439 }
4440 }
4441
4442 bool complained
4443 = emit_diagnostic (kind, input_location, opt,
4444 "unable to find numeric literal operator %qD", name);
4445
4446 if (!complained)
4447 /* Don't inform either. */;
4448 else if (i14)
4449 {
4450 inform (token->location, "add %<using namespace std::complex_literals%> "
4451 "(from <complex>) to enable the C++14 user-defined literal "
4452 "suffixes");
4453 if (ext)
4454 inform (token->location, "or use %<j%> instead of %<i%> for the "
4455 "GNU built-in suffix");
4456 }
4457 else if (!ext)
4458 inform (token->location, "use -fext-numeric-literals "
4459 "to enable more built-in suffixes");
4460
4461 if (kind == DK_ERROR)
4462 value = error_mark_node;
4463 else
4464 {
4465 /* Use the built-in semantics. */
4466 tree type;
4467 if (id_equal (suffix_id, "i"))
4468 {
4469 if (TREE_CODE (value) == INTEGER_CST)
4470 type = integer_type_node;
4471 else
4472 type = double_type_node;
4473 }
4474 else if (id_equal (suffix_id, "if"))
4475 type = float_type_node;
4476 else /* if (id_equal (suffix_id, "il")) */
4477 type = long_double_type_node;
4478
4479 value = build_complex (build_complex_type (type),
4480 fold_convert (type, integer_zero_node),
4481 fold_convert (type, value));
4482 }
4483
4484 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4485 /* Avoid repeated diagnostics. */
4486 token->u.value = value;
4487 return value;
4488 }
4489
4490 /* Parse a user-defined string constant. Returns a call to a user-defined
4491 literal operator taking a character pointer and the length of the string
4492 as arguments. */
4493
4494 static tree
4495 cp_parser_userdef_string_literal (tree literal)
4496 {
4497 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4498 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4499 tree value = USERDEF_LITERAL_VALUE (literal);
4500 int len = TREE_STRING_LENGTH (value)
4501 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4502 tree decl, result;
4503 vec<tree, va_gc> *args;
4504
4505 /* Build up a call to the user-defined operator. */
4506 /* Lookup the name we got back from the id-expression. */
4507 args = make_tree_vector ();
4508 vec_safe_push (args, value);
4509 vec_safe_push (args, build_int_cst (size_type_node, len));
4510 decl = lookup_literal_operator (name, args);
4511
4512 if (decl && decl != error_mark_node)
4513 {
4514 result = finish_call_expr (decl, &args, false, true,
4515 tf_warning_or_error);
4516 release_tree_vector (args);
4517 return result;
4518 }
4519 release_tree_vector (args);
4520
4521 /* Look for a template function with typename parameter CharT
4522 and parameter pack CharT... Call the function with
4523 template parameter characters representing the string. */
4524 args = make_tree_vector ();
4525 decl = lookup_literal_operator (name, args);
4526 if (decl && decl != error_mark_node)
4527 {
4528 tree tmpl_args = make_string_pack (value);
4529 decl = lookup_template_function (decl, tmpl_args);
4530 result = finish_call_expr (decl, &args, false, true,
4531 tf_warning_or_error);
4532 release_tree_vector (args);
4533 return result;
4534 }
4535 release_tree_vector (args);
4536
4537 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4538 name, TREE_TYPE (value), size_type_node);
4539 return error_mark_node;
4540 }
4541
4542
4543 /* Basic concepts [gram.basic] */
4544
4545 /* Parse a translation-unit.
4546
4547 translation-unit:
4548 declaration-seq [opt]
4549
4550 Returns TRUE if all went well. */
4551
4552 static bool
4553 cp_parser_translation_unit (cp_parser* parser)
4554 {
4555 /* The address of the first non-permanent object on the declarator
4556 obstack. */
4557 static void *declarator_obstack_base;
4558
4559 bool success;
4560
4561 /* Create the declarator obstack, if necessary. */
4562 if (!cp_error_declarator)
4563 {
4564 gcc_obstack_init (&declarator_obstack);
4565 /* Create the error declarator. */
4566 cp_error_declarator = make_declarator (cdk_error);
4567 /* Create the empty parameter list. */
4568 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4569 UNKNOWN_LOCATION);
4570 /* Remember where the base of the declarator obstack lies. */
4571 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4572 }
4573
4574 cp_parser_declaration_seq_opt (parser);
4575
4576 /* If there are no tokens left then all went well. */
4577 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4578 {
4579 /* Get rid of the token array; we don't need it any more. */
4580 cp_lexer_destroy (parser->lexer);
4581 parser->lexer = NULL;
4582
4583 /* This file might have been a context that's implicitly extern
4584 "C". If so, pop the lang context. (Only relevant for PCH.) */
4585 if (parser->implicit_extern_c)
4586 {
4587 pop_lang_context ();
4588 parser->implicit_extern_c = false;
4589 }
4590
4591 /* Finish up. */
4592 finish_translation_unit ();
4593
4594 success = true;
4595 }
4596 else
4597 {
4598 cp_parser_error (parser, "expected declaration");
4599 success = false;
4600 }
4601
4602 /* Make sure the declarator obstack was fully cleaned up. */
4603 gcc_assert (obstack_next_free (&declarator_obstack)
4604 == declarator_obstack_base);
4605
4606 /* All went well. */
4607 return success;
4608 }
4609
4610 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4611 decltype context. */
4612
4613 static inline tsubst_flags_t
4614 complain_flags (bool decltype_p)
4615 {
4616 tsubst_flags_t complain = tf_warning_or_error;
4617 if (decltype_p)
4618 complain |= tf_decltype;
4619 return complain;
4620 }
4621
4622 /* We're about to parse a collection of statements. If we're currently
4623 parsing tentatively, set up a firewall so that any nested
4624 cp_parser_commit_to_tentative_parse won't affect the current context. */
4625
4626 static cp_token_position
4627 cp_parser_start_tentative_firewall (cp_parser *parser)
4628 {
4629 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4630 return 0;
4631
4632 cp_parser_parse_tentatively (parser);
4633 cp_parser_commit_to_topmost_tentative_parse (parser);
4634 return cp_lexer_token_position (parser->lexer, false);
4635 }
4636
4637 /* We've finished parsing the collection of statements. Wrap up the
4638 firewall and replace the relevant tokens with the parsed form. */
4639
4640 static void
4641 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4642 tree expr)
4643 {
4644 if (!start)
4645 return;
4646
4647 /* Finish the firewall level. */
4648 cp_parser_parse_definitely (parser);
4649 /* And remember the result of the parse for when we try again. */
4650 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4651 token->type = CPP_PREPARSED_EXPR;
4652 token->u.value = expr;
4653 token->keyword = RID_MAX;
4654 cp_lexer_purge_tokens_after (parser->lexer, start);
4655 }
4656
4657 /* Like the above functions, but let the user modify the tokens. Used by
4658 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4659 later parses, so it makes sense to localize the effects of
4660 cp_parser_commit_to_tentative_parse. */
4661
4662 struct tentative_firewall
4663 {
4664 cp_parser *parser;
4665 bool set;
4666
4667 tentative_firewall (cp_parser *p): parser(p)
4668 {
4669 /* If we're currently parsing tentatively, start a committed level as a
4670 firewall and then an inner tentative parse. */
4671 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4672 {
4673 cp_parser_parse_tentatively (parser);
4674 cp_parser_commit_to_topmost_tentative_parse (parser);
4675 cp_parser_parse_tentatively (parser);
4676 }
4677 }
4678
4679 ~tentative_firewall()
4680 {
4681 if (set)
4682 {
4683 /* Finish the inner tentative parse and the firewall, propagating any
4684 uncommitted error state to the outer tentative parse. */
4685 bool err = cp_parser_error_occurred (parser);
4686 cp_parser_parse_definitely (parser);
4687 cp_parser_parse_definitely (parser);
4688 if (err)
4689 cp_parser_simulate_error (parser);
4690 }
4691 }
4692 };
4693
4694 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4695 This class is for tracking such a matching pair of symbols.
4696 In particular, it tracks the location of the first token,
4697 so that if the second token is missing, we can highlight the
4698 location of the first token when notifying the user about the
4699 problem. */
4700
4701 template <typename traits_t>
4702 class token_pair
4703 {
4704 public:
4705 /* token_pair's ctor. */
4706 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4707
4708 /* If the next token is the opening symbol for this pair, consume it and
4709 return true.
4710 Otherwise, issue an error and return false.
4711 In either case, record the location of the opening token. */
4712
4713 bool require_open (cp_parser *parser)
4714 {
4715 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4716 return cp_parser_require (parser, traits_t::open_token_type,
4717 traits_t::required_token_open);
4718 }
4719
4720 /* Consume the next token from PARSER, recording its location as
4721 that of the opening token within the pair. */
4722
4723 cp_token * consume_open (cp_parser *parser)
4724 {
4725 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4726 gcc_assert (tok->type == traits_t::open_token_type);
4727 m_open_loc = tok->location;
4728 return tok;
4729 }
4730
4731 /* If the next token is the closing symbol for this pair, consume it
4732 and return it.
4733 Otherwise, issue an error, highlighting the location of the
4734 corresponding opening token, and return NULL. */
4735
4736 cp_token *require_close (cp_parser *parser) const
4737 {
4738 return cp_parser_require (parser, traits_t::close_token_type,
4739 traits_t::required_token_close,
4740 m_open_loc);
4741 }
4742
4743 private:
4744 location_t m_open_loc;
4745 };
4746
4747 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4748
4749 struct matching_paren_traits
4750 {
4751 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4752 static const enum required_token required_token_open = RT_OPEN_PAREN;
4753 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4754 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4755 };
4756
4757 /* "matching_parens" is a token_pair<T> class for tracking matching
4758 pairs of parentheses. */
4759
4760 typedef token_pair<matching_paren_traits> matching_parens;
4761
4762 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4763
4764 struct matching_brace_traits
4765 {
4766 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4767 static const enum required_token required_token_open = RT_OPEN_BRACE;
4768 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4769 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4770 };
4771
4772 /* "matching_braces" is a token_pair<T> class for tracking matching
4773 pairs of braces. */
4774
4775 typedef token_pair<matching_brace_traits> matching_braces;
4776
4777
4778 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4779 enclosing parentheses. */
4780
4781 static cp_expr
4782 cp_parser_statement_expr (cp_parser *parser)
4783 {
4784 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4785
4786 /* Consume the '('. */
4787 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4788 matching_parens parens;
4789 parens.consume_open (parser);
4790 /* Start the statement-expression. */
4791 tree expr = begin_stmt_expr ();
4792 /* Parse the compound-statement. */
4793 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4794 /* Finish up. */
4795 expr = finish_stmt_expr (expr, false);
4796 /* Consume the ')'. */
4797 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4798 if (!parens.require_close (parser))
4799 cp_parser_skip_to_end_of_statement (parser);
4800
4801 cp_parser_end_tentative_firewall (parser, start, expr);
4802 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4803 return cp_expr (expr, combined_loc);
4804 }
4805
4806 /* Expressions [gram.expr] */
4807
4808 /* Parse a fold-operator.
4809
4810 fold-operator:
4811 - * / % ^ & | = < > << >>
4812 = -= *= /= %= ^= &= |= <<= >>=
4813 == != <= >= && || , .* ->*
4814
4815 This returns the tree code corresponding to the matched operator
4816 as an int. When the current token matches a compound assignment
4817 opertor, the resulting tree code is the negative value of the
4818 non-assignment operator. */
4819
4820 static int
4821 cp_parser_fold_operator (cp_token *token)
4822 {
4823 switch (token->type)
4824 {
4825 case CPP_PLUS: return PLUS_EXPR;
4826 case CPP_MINUS: return MINUS_EXPR;
4827 case CPP_MULT: return MULT_EXPR;
4828 case CPP_DIV: return TRUNC_DIV_EXPR;
4829 case CPP_MOD: return TRUNC_MOD_EXPR;
4830 case CPP_XOR: return BIT_XOR_EXPR;
4831 case CPP_AND: return BIT_AND_EXPR;
4832 case CPP_OR: return BIT_IOR_EXPR;
4833 case CPP_LSHIFT: return LSHIFT_EXPR;
4834 case CPP_RSHIFT: return RSHIFT_EXPR;
4835
4836 case CPP_EQ: return -NOP_EXPR;
4837 case CPP_PLUS_EQ: return -PLUS_EXPR;
4838 case CPP_MINUS_EQ: return -MINUS_EXPR;
4839 case CPP_MULT_EQ: return -MULT_EXPR;
4840 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4841 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4842 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4843 case CPP_AND_EQ: return -BIT_AND_EXPR;
4844 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4845 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4846 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4847
4848 case CPP_EQ_EQ: return EQ_EXPR;
4849 case CPP_NOT_EQ: return NE_EXPR;
4850 case CPP_LESS: return LT_EXPR;
4851 case CPP_GREATER: return GT_EXPR;
4852 case CPP_LESS_EQ: return LE_EXPR;
4853 case CPP_GREATER_EQ: return GE_EXPR;
4854
4855 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4856 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4857
4858 case CPP_COMMA: return COMPOUND_EXPR;
4859
4860 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4861 case CPP_DEREF_STAR: return MEMBER_REF;
4862
4863 default: return ERROR_MARK;
4864 }
4865 }
4866
4867 /* Returns true if CODE indicates a binary expression, which is not allowed in
4868 the LHS of a fold-expression. More codes will need to be added to use this
4869 function in other contexts. */
4870
4871 static bool
4872 is_binary_op (tree_code code)
4873 {
4874 switch (code)
4875 {
4876 case PLUS_EXPR:
4877 case POINTER_PLUS_EXPR:
4878 case MINUS_EXPR:
4879 case MULT_EXPR:
4880 case TRUNC_DIV_EXPR:
4881 case TRUNC_MOD_EXPR:
4882 case BIT_XOR_EXPR:
4883 case BIT_AND_EXPR:
4884 case BIT_IOR_EXPR:
4885 case LSHIFT_EXPR:
4886 case RSHIFT_EXPR:
4887
4888 case MODOP_EXPR:
4889
4890 case EQ_EXPR:
4891 case NE_EXPR:
4892 case LE_EXPR:
4893 case GE_EXPR:
4894 case LT_EXPR:
4895 case GT_EXPR:
4896
4897 case TRUTH_ANDIF_EXPR:
4898 case TRUTH_ORIF_EXPR:
4899
4900 case COMPOUND_EXPR:
4901
4902 case DOTSTAR_EXPR:
4903 case MEMBER_REF:
4904 return true;
4905
4906 default:
4907 return false;
4908 }
4909 }
4910
4911 /* If the next token is a suitable fold operator, consume it and return as
4912 the function above. */
4913
4914 static int
4915 cp_parser_fold_operator (cp_parser *parser)
4916 {
4917 cp_token* token = cp_lexer_peek_token (parser->lexer);
4918 int code = cp_parser_fold_operator (token);
4919 if (code != ERROR_MARK)
4920 cp_lexer_consume_token (parser->lexer);
4921 return code;
4922 }
4923
4924 /* Parse a fold-expression.
4925
4926 fold-expression:
4927 ( ... folding-operator cast-expression)
4928 ( cast-expression folding-operator ... )
4929 ( cast-expression folding operator ... folding-operator cast-expression)
4930
4931 Note that the '(' and ')' are matched in primary expression. */
4932
4933 static cp_expr
4934 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4935 {
4936 cp_id_kind pidk;
4937
4938 // Left fold.
4939 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4940 {
4941 cp_lexer_consume_token (parser->lexer);
4942 int op = cp_parser_fold_operator (parser);
4943 if (op == ERROR_MARK)
4944 {
4945 cp_parser_error (parser, "expected binary operator");
4946 return error_mark_node;
4947 }
4948
4949 tree expr = cp_parser_cast_expression (parser, false, false,
4950 false, &pidk);
4951 if (expr == error_mark_node)
4952 return error_mark_node;
4953 return finish_left_unary_fold_expr (expr, op);
4954 }
4955
4956 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4957 int op = cp_parser_fold_operator (parser);
4958 if (op == ERROR_MARK)
4959 {
4960 cp_parser_error (parser, "expected binary operator");
4961 return error_mark_node;
4962 }
4963
4964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4965 {
4966 cp_parser_error (parser, "expected ...");
4967 return error_mark_node;
4968 }
4969 cp_lexer_consume_token (parser->lexer);
4970
4971 /* The operands of a fold-expression are cast-expressions, so binary or
4972 conditional expressions are not allowed. We check this here to avoid
4973 tentative parsing. */
4974 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4975 /* OK, the expression was parenthesized. */;
4976 else if (is_binary_op (TREE_CODE (expr1)))
4977 error_at (location_of (expr1),
4978 "binary expression in operand of fold-expression");
4979 else if (TREE_CODE (expr1) == COND_EXPR
4980 || (REFERENCE_REF_P (expr1)
4981 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4982 error_at (location_of (expr1),
4983 "conditional expression in operand of fold-expression");
4984
4985 // Right fold.
4986 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4987 return finish_right_unary_fold_expr (expr1, op);
4988
4989 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4990 {
4991 cp_parser_error (parser, "mismatched operator in fold-expression");
4992 return error_mark_node;
4993 }
4994 cp_lexer_consume_token (parser->lexer);
4995
4996 // Binary left or right fold.
4997 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4998 if (expr2 == error_mark_node)
4999 return error_mark_node;
5000 return finish_binary_fold_expr (expr1, expr2, op);
5001 }
5002
5003 /* Parse a primary-expression.
5004
5005 primary-expression:
5006 literal
5007 this
5008 ( expression )
5009 id-expression
5010 lambda-expression (C++11)
5011
5012 GNU Extensions:
5013
5014 primary-expression:
5015 ( compound-statement )
5016 __builtin_va_arg ( assignment-expression , type-id )
5017 __builtin_offsetof ( type-id , offsetof-expression )
5018
5019 C++ Extensions:
5020 __has_nothrow_assign ( type-id )
5021 __has_nothrow_constructor ( type-id )
5022 __has_nothrow_copy ( type-id )
5023 __has_trivial_assign ( type-id )
5024 __has_trivial_constructor ( type-id )
5025 __has_trivial_copy ( type-id )
5026 __has_trivial_destructor ( type-id )
5027 __has_virtual_destructor ( type-id )
5028 __is_abstract ( type-id )
5029 __is_base_of ( type-id , type-id )
5030 __is_class ( type-id )
5031 __is_empty ( type-id )
5032 __is_enum ( type-id )
5033 __is_final ( type-id )
5034 __is_literal_type ( type-id )
5035 __is_pod ( type-id )
5036 __is_polymorphic ( type-id )
5037 __is_std_layout ( type-id )
5038 __is_trivial ( type-id )
5039 __is_union ( type-id )
5040
5041 Objective-C++ Extension:
5042
5043 primary-expression:
5044 objc-expression
5045
5046 literal:
5047 __null
5048
5049 ADDRESS_P is true iff this expression was immediately preceded by
5050 "&" and therefore might denote a pointer-to-member. CAST_P is true
5051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5052 true iff this expression is a template argument.
5053
5054 Returns a representation of the expression. Upon return, *IDK
5055 indicates what kind of id-expression (if any) was present. */
5056
5057 static cp_expr
5058 cp_parser_primary_expression (cp_parser *parser,
5059 bool address_p,
5060 bool cast_p,
5061 bool template_arg_p,
5062 bool decltype_p,
5063 cp_id_kind *idk)
5064 {
5065 cp_token *token = NULL;
5066
5067 /* Assume the primary expression is not an id-expression. */
5068 *idk = CP_ID_KIND_NONE;
5069
5070 /* Peek at the next token. */
5071 token = cp_lexer_peek_token (parser->lexer);
5072 switch ((int) token->type)
5073 {
5074 /* literal:
5075 integer-literal
5076 character-literal
5077 floating-literal
5078 string-literal
5079 boolean-literal
5080 pointer-literal
5081 user-defined-literal */
5082 case CPP_CHAR:
5083 case CPP_CHAR16:
5084 case CPP_CHAR32:
5085 case CPP_WCHAR:
5086 case CPP_UTF8CHAR:
5087 case CPP_NUMBER:
5088 case CPP_PREPARSED_EXPR:
5089 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5090 return cp_parser_userdef_numeric_literal (parser);
5091 token = cp_lexer_consume_token (parser->lexer);
5092 if (TREE_CODE (token->u.value) == FIXED_CST)
5093 {
5094 error_at (token->location,
5095 "fixed-point types not supported in C++");
5096 return error_mark_node;
5097 }
5098 /* Floating-point literals are only allowed in an integral
5099 constant expression if they are cast to an integral or
5100 enumeration type. */
5101 if (TREE_CODE (token->u.value) == REAL_CST
5102 && parser->integral_constant_expression_p
5103 && pedantic)
5104 {
5105 /* CAST_P will be set even in invalid code like "int(2.7 +
5106 ...)". Therefore, we have to check that the next token
5107 is sure to end the cast. */
5108 if (cast_p)
5109 {
5110 cp_token *next_token;
5111
5112 next_token = cp_lexer_peek_token (parser->lexer);
5113 if (/* The comma at the end of an
5114 enumerator-definition. */
5115 next_token->type != CPP_COMMA
5116 /* The curly brace at the end of an enum-specifier. */
5117 && next_token->type != CPP_CLOSE_BRACE
5118 /* The end of a statement. */
5119 && next_token->type != CPP_SEMICOLON
5120 /* The end of the cast-expression. */
5121 && next_token->type != CPP_CLOSE_PAREN
5122 /* The end of an array bound. */
5123 && next_token->type != CPP_CLOSE_SQUARE
5124 /* The closing ">" in a template-argument-list. */
5125 && (next_token->type != CPP_GREATER
5126 || parser->greater_than_is_operator_p)
5127 /* C++0x only: A ">>" treated like two ">" tokens,
5128 in a template-argument-list. */
5129 && (next_token->type != CPP_RSHIFT
5130 || (cxx_dialect == cxx98)
5131 || parser->greater_than_is_operator_p))
5132 cast_p = false;
5133 }
5134
5135 /* If we are within a cast, then the constraint that the
5136 cast is to an integral or enumeration type will be
5137 checked at that point. If we are not within a cast, then
5138 this code is invalid. */
5139 if (!cast_p)
5140 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5141 }
5142 return cp_expr (token->u.value, token->location);
5143
5144 case CPP_CHAR_USERDEF:
5145 case CPP_CHAR16_USERDEF:
5146 case CPP_CHAR32_USERDEF:
5147 case CPP_WCHAR_USERDEF:
5148 case CPP_UTF8CHAR_USERDEF:
5149 return cp_parser_userdef_char_literal (parser);
5150
5151 case CPP_STRING:
5152 case CPP_STRING16:
5153 case CPP_STRING32:
5154 case CPP_WSTRING:
5155 case CPP_UTF8STRING:
5156 case CPP_STRING_USERDEF:
5157 case CPP_STRING16_USERDEF:
5158 case CPP_STRING32_USERDEF:
5159 case CPP_WSTRING_USERDEF:
5160 case CPP_UTF8STRING_USERDEF:
5161 /* ??? Should wide strings be allowed when parser->translate_strings_p
5162 is false (i.e. in attributes)? If not, we can kill the third
5163 argument to cp_parser_string_literal. */
5164 return cp_parser_string_literal (parser,
5165 parser->translate_strings_p,
5166 true);
5167
5168 case CPP_OPEN_PAREN:
5169 /* If we see `( { ' then we are looking at the beginning of
5170 a GNU statement-expression. */
5171 if (cp_parser_allow_gnu_extensions_p (parser)
5172 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5173 {
5174 /* Statement-expressions are not allowed by the standard. */
5175 pedwarn (token->location, OPT_Wpedantic,
5176 "ISO C++ forbids braced-groups within expressions");
5177
5178 /* And they're not allowed outside of a function-body; you
5179 cannot, for example, write:
5180
5181 int i = ({ int j = 3; j + 1; });
5182
5183 at class or namespace scope. */
5184 if (!parser->in_function_body
5185 || parser->in_template_argument_list_p)
5186 {
5187 error_at (token->location,
5188 "statement-expressions are not allowed outside "
5189 "functions nor in template-argument lists");
5190 cp_parser_skip_to_end_of_block_or_statement (parser);
5191 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5192 cp_lexer_consume_token (parser->lexer);
5193 return error_mark_node;
5194 }
5195 else
5196 return cp_parser_statement_expr (parser);
5197 }
5198 /* Otherwise it's a normal parenthesized expression. */
5199 {
5200 cp_expr expr;
5201 bool saved_greater_than_is_operator_p;
5202
5203 location_t open_paren_loc = token->location;
5204
5205 /* Consume the `('. */
5206 matching_parens parens;
5207 parens.consume_open (parser);
5208 /* Within a parenthesized expression, a `>' token is always
5209 the greater-than operator. */
5210 saved_greater_than_is_operator_p
5211 = parser->greater_than_is_operator_p;
5212 parser->greater_than_is_operator_p = true;
5213
5214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5215 /* Left fold expression. */
5216 expr = NULL_TREE;
5217 else
5218 /* Parse the parenthesized expression. */
5219 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5220
5221 token = cp_lexer_peek_token (parser->lexer);
5222 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5223 {
5224 expr = cp_parser_fold_expression (parser, expr);
5225 if (expr != error_mark_node
5226 && cxx_dialect < cxx17
5227 && !in_system_header_at (input_location))
5228 pedwarn (input_location, 0, "fold-expressions only available "
5229 "with -std=c++17 or -std=gnu++17");
5230 }
5231 else
5232 /* Let the front end know that this expression was
5233 enclosed in parentheses. This matters in case, for
5234 example, the expression is of the form `A::B', since
5235 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5236 not. */
5237 expr = finish_parenthesized_expr (expr);
5238
5239 /* DR 705: Wrapping an unqualified name in parentheses
5240 suppresses arg-dependent lookup. We want to pass back
5241 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5242 (c++/37862), but none of the others. */
5243 if (*idk != CP_ID_KIND_QUALIFIED)
5244 *idk = CP_ID_KIND_NONE;
5245
5246 /* The `>' token might be the end of a template-id or
5247 template-parameter-list now. */
5248 parser->greater_than_is_operator_p
5249 = saved_greater_than_is_operator_p;
5250
5251 /* Consume the `)'. */
5252 token = cp_lexer_peek_token (parser->lexer);
5253 location_t close_paren_loc = token->location;
5254 expr.set_range (open_paren_loc, close_paren_loc);
5255 if (!parens.require_close (parser)
5256 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5257 cp_parser_skip_to_end_of_statement (parser);
5258
5259 return expr;
5260 }
5261
5262 case CPP_OPEN_SQUARE:
5263 {
5264 if (c_dialect_objc ())
5265 {
5266 /* We might have an Objective-C++ message. */
5267 cp_parser_parse_tentatively (parser);
5268 tree msg = cp_parser_objc_message_expression (parser);
5269 /* If that works out, we're done ... */
5270 if (cp_parser_parse_definitely (parser))
5271 return msg;
5272 /* ... else, fall though to see if it's a lambda. */
5273 }
5274 cp_expr lam = cp_parser_lambda_expression (parser);
5275 /* Don't warn about a failed tentative parse. */
5276 if (cp_parser_error_occurred (parser))
5277 return error_mark_node;
5278 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5279 return lam;
5280 }
5281
5282 case CPP_OBJC_STRING:
5283 if (c_dialect_objc ())
5284 /* We have an Objective-C++ string literal. */
5285 return cp_parser_objc_expression (parser);
5286 cp_parser_error (parser, "expected primary-expression");
5287 return error_mark_node;
5288
5289 case CPP_KEYWORD:
5290 switch (token->keyword)
5291 {
5292 /* These two are the boolean literals. */
5293 case RID_TRUE:
5294 cp_lexer_consume_token (parser->lexer);
5295 return cp_expr (boolean_true_node, token->location);
5296 case RID_FALSE:
5297 cp_lexer_consume_token (parser->lexer);
5298 return cp_expr (boolean_false_node, token->location);
5299
5300 /* The `__null' literal. */
5301 case RID_NULL:
5302 cp_lexer_consume_token (parser->lexer);
5303 return cp_expr (null_node, token->location);
5304
5305 /* The `nullptr' literal. */
5306 case RID_NULLPTR:
5307 cp_lexer_consume_token (parser->lexer);
5308 return cp_expr (nullptr_node, token->location);
5309
5310 /* Recognize the `this' keyword. */
5311 case RID_THIS:
5312 cp_lexer_consume_token (parser->lexer);
5313 if (parser->local_variables_forbidden_p)
5314 {
5315 error_at (token->location,
5316 "%<this%> may not be used in this context");
5317 return error_mark_node;
5318 }
5319 /* Pointers cannot appear in constant-expressions. */
5320 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5321 return error_mark_node;
5322 return cp_expr (finish_this_expr (), token->location);
5323
5324 /* The `operator' keyword can be the beginning of an
5325 id-expression. */
5326 case RID_OPERATOR:
5327 goto id_expression;
5328
5329 case RID_FUNCTION_NAME:
5330 case RID_PRETTY_FUNCTION_NAME:
5331 case RID_C99_FUNCTION_NAME:
5332 {
5333 non_integral_constant name;
5334
5335 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5336 __func__ are the names of variables -- but they are
5337 treated specially. Therefore, they are handled here,
5338 rather than relying on the generic id-expression logic
5339 below. Grammatically, these names are id-expressions.
5340
5341 Consume the token. */
5342 token = cp_lexer_consume_token (parser->lexer);
5343
5344 switch (token->keyword)
5345 {
5346 case RID_FUNCTION_NAME:
5347 name = NIC_FUNC_NAME;
5348 break;
5349 case RID_PRETTY_FUNCTION_NAME:
5350 name = NIC_PRETTY_FUNC;
5351 break;
5352 case RID_C99_FUNCTION_NAME:
5353 name = NIC_C99_FUNC;
5354 break;
5355 default:
5356 gcc_unreachable ();
5357 }
5358
5359 if (cp_parser_non_integral_constant_expression (parser, name))
5360 return error_mark_node;
5361
5362 /* Look up the name. */
5363 return finish_fname (token->u.value);
5364 }
5365
5366 case RID_VA_ARG:
5367 {
5368 tree expression;
5369 tree type;
5370 source_location type_location;
5371 location_t start_loc
5372 = cp_lexer_peek_token (parser->lexer)->location;
5373 /* The `__builtin_va_arg' construct is used to handle
5374 `va_arg'. Consume the `__builtin_va_arg' token. */
5375 cp_lexer_consume_token (parser->lexer);
5376 /* Look for the opening `('. */
5377 matching_parens parens;
5378 parens.require_open (parser);
5379 /* Now, parse the assignment-expression. */
5380 expression = cp_parser_assignment_expression (parser);
5381 /* Look for the `,'. */
5382 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5383 type_location = cp_lexer_peek_token (parser->lexer)->location;
5384 /* Parse the type-id. */
5385 {
5386 type_id_in_expr_sentinel s (parser);
5387 type = cp_parser_type_id (parser);
5388 }
5389 /* Look for the closing `)'. */
5390 location_t finish_loc
5391 = cp_lexer_peek_token (parser->lexer)->location;
5392 parens.require_close (parser);
5393 /* Using `va_arg' in a constant-expression is not
5394 allowed. */
5395 if (cp_parser_non_integral_constant_expression (parser,
5396 NIC_VA_ARG))
5397 return error_mark_node;
5398 /* Construct a location of the form:
5399 __builtin_va_arg (v, int)
5400 ~~~~~~~~~~~~~~~~~~~~~^~~~
5401 with the caret at the type, ranging from the start of the
5402 "__builtin_va_arg" token to the close paren. */
5403 location_t combined_loc
5404 = make_location (type_location, start_loc, finish_loc);
5405 return build_x_va_arg (combined_loc, expression, type);
5406 }
5407
5408 case RID_OFFSETOF:
5409 return cp_parser_builtin_offsetof (parser);
5410
5411 case RID_HAS_NOTHROW_ASSIGN:
5412 case RID_HAS_NOTHROW_CONSTRUCTOR:
5413 case RID_HAS_NOTHROW_COPY:
5414 case RID_HAS_TRIVIAL_ASSIGN:
5415 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5416 case RID_HAS_TRIVIAL_COPY:
5417 case RID_HAS_TRIVIAL_DESTRUCTOR:
5418 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5419 case RID_HAS_VIRTUAL_DESTRUCTOR:
5420 case RID_IS_ABSTRACT:
5421 case RID_IS_AGGREGATE:
5422 case RID_IS_BASE_OF:
5423 case RID_IS_CLASS:
5424 case RID_IS_EMPTY:
5425 case RID_IS_ENUM:
5426 case RID_IS_FINAL:
5427 case RID_IS_LITERAL_TYPE:
5428 case RID_IS_POD:
5429 case RID_IS_POLYMORPHIC:
5430 case RID_IS_SAME_AS:
5431 case RID_IS_STD_LAYOUT:
5432 case RID_IS_TRIVIAL:
5433 case RID_IS_TRIVIALLY_ASSIGNABLE:
5434 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5435 case RID_IS_TRIVIALLY_COPYABLE:
5436 case RID_IS_UNION:
5437 case RID_IS_ASSIGNABLE:
5438 case RID_IS_CONSTRUCTIBLE:
5439 return cp_parser_trait_expr (parser, token->keyword);
5440
5441 // C++ concepts
5442 case RID_REQUIRES:
5443 return cp_parser_requires_expression (parser);
5444
5445 /* Objective-C++ expressions. */
5446 case RID_AT_ENCODE:
5447 case RID_AT_PROTOCOL:
5448 case RID_AT_SELECTOR:
5449 return cp_parser_objc_expression (parser);
5450
5451 case RID_TEMPLATE:
5452 if (parser->in_function_body
5453 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5454 == CPP_LESS))
5455 {
5456 error_at (token->location,
5457 "a template declaration cannot appear at block scope");
5458 cp_parser_skip_to_end_of_block_or_statement (parser);
5459 return error_mark_node;
5460 }
5461 /* FALLTHRU */
5462 default:
5463 cp_parser_error (parser, "expected primary-expression");
5464 return error_mark_node;
5465 }
5466
5467 /* An id-expression can start with either an identifier, a
5468 `::' as the beginning of a qualified-id, or the "operator"
5469 keyword. */
5470 case CPP_NAME:
5471 case CPP_SCOPE:
5472 case CPP_TEMPLATE_ID:
5473 case CPP_NESTED_NAME_SPECIFIER:
5474 {
5475 id_expression:
5476 cp_expr id_expression;
5477 cp_expr decl;
5478 const char *error_msg;
5479 bool template_p;
5480 bool done;
5481 cp_token *id_expr_token;
5482
5483 /* Parse the id-expression. */
5484 id_expression
5485 = cp_parser_id_expression (parser,
5486 /*template_keyword_p=*/false,
5487 /*check_dependency_p=*/true,
5488 &template_p,
5489 /*declarator_p=*/false,
5490 /*optional_p=*/false);
5491 if (id_expression == error_mark_node)
5492 return error_mark_node;
5493 id_expr_token = token;
5494 token = cp_lexer_peek_token (parser->lexer);
5495 done = (token->type != CPP_OPEN_SQUARE
5496 && token->type != CPP_OPEN_PAREN
5497 && token->type != CPP_DOT
5498 && token->type != CPP_DEREF
5499 && token->type != CPP_PLUS_PLUS
5500 && token->type != CPP_MINUS_MINUS);
5501 /* If we have a template-id, then no further lookup is
5502 required. If the template-id was for a template-class, we
5503 will sometimes have a TYPE_DECL at this point. */
5504 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5505 || TREE_CODE (id_expression) == TYPE_DECL)
5506 decl = id_expression;
5507 /* Look up the name. */
5508 else
5509 {
5510 tree ambiguous_decls;
5511
5512 /* If we already know that this lookup is ambiguous, then
5513 we've already issued an error message; there's no reason
5514 to check again. */
5515 if (id_expr_token->type == CPP_NAME
5516 && id_expr_token->error_reported)
5517 {
5518 cp_parser_simulate_error (parser);
5519 return error_mark_node;
5520 }
5521
5522 decl = cp_parser_lookup_name (parser, id_expression,
5523 none_type,
5524 template_p,
5525 /*is_namespace=*/false,
5526 /*check_dependency=*/true,
5527 &ambiguous_decls,
5528 id_expr_token->location);
5529 /* If the lookup was ambiguous, an error will already have
5530 been issued. */
5531 if (ambiguous_decls)
5532 return error_mark_node;
5533
5534 /* In Objective-C++, we may have an Objective-C 2.0
5535 dot-syntax for classes here. */
5536 if (c_dialect_objc ()
5537 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5538 && TREE_CODE (decl) == TYPE_DECL
5539 && objc_is_class_name (decl))
5540 {
5541 tree component;
5542 cp_lexer_consume_token (parser->lexer);
5543 component = cp_parser_identifier (parser);
5544 if (component == error_mark_node)
5545 return error_mark_node;
5546
5547 tree result = objc_build_class_component_ref (id_expression,
5548 component);
5549 /* Build a location of the form:
5550 expr.component
5551 ~~~~~^~~~~~~~~
5552 with caret at the start of the component name (at
5553 input_location), ranging from the start of the id_expression
5554 to the end of the component name. */
5555 location_t combined_loc
5556 = make_location (input_location, id_expression.get_start (),
5557 get_finish (input_location));
5558 protected_set_expr_location (result, combined_loc);
5559 return result;
5560 }
5561
5562 /* In Objective-C++, an instance variable (ivar) may be preferred
5563 to whatever cp_parser_lookup_name() found.
5564 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5565 rest of c-family, we have to do a little extra work to preserve
5566 any location information in cp_expr "decl". Given that
5567 objc_lookup_ivar is implemented in "c-family" and "objc", we
5568 have a trip through the pure "tree" type, rather than cp_expr.
5569 Naively copying it back to "decl" would implicitly give the
5570 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5571 store an EXPR_LOCATION. Hence we only update "decl" (and
5572 hence its location_t) if we get back a different tree node. */
5573 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5574 id_expression);
5575 if (decl_tree != decl.get_value ())
5576 decl = cp_expr (decl_tree);
5577
5578 /* If name lookup gives us a SCOPE_REF, then the
5579 qualifying scope was dependent. */
5580 if (TREE_CODE (decl) == SCOPE_REF)
5581 {
5582 /* At this point, we do not know if DECL is a valid
5583 integral constant expression. We assume that it is
5584 in fact such an expression, so that code like:
5585
5586 template <int N> struct A {
5587 int a[B<N>::i];
5588 };
5589
5590 is accepted. At template-instantiation time, we
5591 will check that B<N>::i is actually a constant. */
5592 return decl;
5593 }
5594 /* Check to see if DECL is a local variable in a context
5595 where that is forbidden. */
5596 if (parser->local_variables_forbidden_p
5597 && local_variable_p (decl))
5598 {
5599 error_at (id_expr_token->location,
5600 "local variable %qD may not appear in this context",
5601 decl.get_value ());
5602 return error_mark_node;
5603 }
5604 }
5605
5606 decl = (finish_id_expression
5607 (id_expression, decl, parser->scope,
5608 idk,
5609 parser->integral_constant_expression_p,
5610 parser->allow_non_integral_constant_expression_p,
5611 &parser->non_integral_constant_expression_p,
5612 template_p, done, address_p,
5613 template_arg_p,
5614 &error_msg,
5615 id_expression.get_location ()));
5616 if (error_msg)
5617 cp_parser_error (parser, error_msg);
5618 decl.set_location (id_expr_token->location);
5619 return decl;
5620 }
5621
5622 /* Anything else is an error. */
5623 default:
5624 cp_parser_error (parser, "expected primary-expression");
5625 return error_mark_node;
5626 }
5627 }
5628
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser *parser,
5631 bool address_p,
5632 bool cast_p,
5633 bool template_arg_p,
5634 cp_id_kind *idk)
5635 {
5636 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5637 /*decltype*/false, idk);
5638 }
5639
5640 /* Parse an id-expression.
5641
5642 id-expression:
5643 unqualified-id
5644 qualified-id
5645
5646 qualified-id:
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5648 :: identifier
5649 :: operator-function-id
5650 :: template-id
5651
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5655
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5660
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5662 `template' keyword.
5663
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5666
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5670
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5673
5674 static cp_expr
5675 cp_parser_id_expression (cp_parser *parser,
5676 bool template_keyword_p,
5677 bool check_dependency_p,
5678 bool *template_p,
5679 bool declarator_p,
5680 bool optional_p)
5681 {
5682 bool global_scope_p;
5683 bool nested_name_specifier_p;
5684
5685 /* Assume the `template' keyword was not used. */
5686 if (template_p)
5687 *template_p = template_keyword_p;
5688
5689 /* Look for the optional `::' operator. */
5690 global_scope_p
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser,
5693 /*current_scope_valid_p=*/false)
5694 != NULL_TREE));
5695
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser,
5699 /*typename_keyword_p=*/false,
5700 check_dependency_p,
5701 /*type_p=*/false,
5702 declarator_p,
5703 template_keyword_p)
5704 != NULL_TREE);
5705
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p)
5709 {
5710 tree saved_scope;
5711 tree saved_object_scope;
5712 tree saved_qualifying_scope;
5713 cp_expr unqualified_id;
5714 bool is_template;
5715
5716 /* See if the next token is the `template' keyword. */
5717 if (!template_p)
5718 template_p = &is_template;
5719 *template_p = cp_parser_optional_template_keyword (parser);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope = parser->scope;
5723 saved_object_scope = parser->object_scope;
5724 saved_qualifying_scope = parser->qualifying_scope;
5725 /* Process the final unqualified-id. */
5726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5727 check_dependency_p,
5728 declarator_p,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser->scope = saved_scope;
5732 parser->object_scope = saved_object_scope;
5733 parser->qualifying_scope = saved_qualifying_scope;
5734
5735 return unqualified_id;
5736 }
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p)
5740 {
5741 cp_token *token;
5742 tree id;
5743
5744 /* Peek at the next token. */
5745 token = cp_lexer_peek_token (parser->lexer);
5746
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token->type == CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5752 (parser, 2))
5753 return cp_parser_identifier (parser);
5754
5755 cp_parser_parse_tentatively (parser);
5756 /* Try a template-id. */
5757 id = cp_parser_template_id (parser,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5760 none_type,
5761 declarator_p);
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser))
5764 return id;
5765
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token = cp_lexer_peek_token (parser->lexer);
5769
5770 switch (token->type)
5771 {
5772 case CPP_NAME:
5773 return cp_parser_identifier (parser);
5774
5775 case CPP_KEYWORD:
5776 if (token->keyword == RID_OPERATOR)
5777 return cp_parser_operator_function_id (parser);
5778 /* Fall through. */
5779
5780 default:
5781 cp_parser_error (parser, "expected id-expression");
5782 return error_mark_node;
5783 }
5784 }
5785 else
5786 return cp_parser_unqualified_id (parser, template_keyword_p,
5787 /*check_dependency_p=*/true,
5788 declarator_p,
5789 optional_p);
5790 }
5791
5792 /* Parse an unqualified-id.
5793
5794 unqualified-id:
5795 identifier
5796 operator-function-id
5797 conversion-function-id
5798 ~ class-name
5799 template-id
5800
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5803
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5813
5814 static cp_expr
5815 cp_parser_unqualified_id (cp_parser* parser,
5816 bool template_keyword_p,
5817 bool check_dependency_p,
5818 bool declarator_p,
5819 bool optional_p)
5820 {
5821 cp_token *token;
5822
5823 /* Peek at the next token. */
5824 token = cp_lexer_peek_token (parser->lexer);
5825
5826 switch ((int) token->type)
5827 {
5828 case CPP_NAME:
5829 {
5830 tree id;
5831
5832 /* We don't know yet whether or not this will be a
5833 template-id. */
5834 cp_parser_parse_tentatively (parser);
5835 /* Try a template-id. */
5836 id = cp_parser_template_id (parser, template_keyword_p,
5837 check_dependency_p,
5838 none_type,
5839 declarator_p);
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser))
5842 return id;
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser);
5845 }
5846
5847 case CPP_TEMPLATE_ID:
5848 return cp_parser_template_id (parser, template_keyword_p,
5849 check_dependency_p,
5850 none_type,
5851 declarator_p);
5852
5853 case CPP_COMPL:
5854 {
5855 tree type_decl;
5856 tree qualifying_scope;
5857 tree object_scope;
5858 tree scope;
5859 bool done;
5860
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser->lexer);
5863 /* Parse the class-name. The standard, as written, seems to
5864 say that:
5865
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5868
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5873
5874 template <typename T> void f(T t) {
5875 t.T::~T();
5876 }
5877
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5881
5882 Yet another issue is:
5883
5884 struct S {
5885 int S;
5886 ~S();
5887 };
5888
5889 S::~S() {}
5890
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5893 `S::S'. */
5894
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope = parser->scope;
5899 object_scope = parser->object_scope;
5900 qualifying_scope = parser->qualifying_scope;
5901
5902 /* Check for invalid scopes. */
5903 if (scope == error_mark_node)
5904 {
5905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5906 cp_lexer_consume_token (parser->lexer);
5907 return error_mark_node;
5908 }
5909 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5910 {
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5912 error_at (token->location,
5913 "scope %qT before %<~%> is not a class-name",
5914 scope);
5915 cp_parser_simulate_error (parser);
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5917 cp_lexer_consume_token (parser->lexer);
5918 return error_mark_node;
5919 }
5920 gcc_assert (!scope || TYPE_P (scope));
5921
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5923 typedef. */
5924 token = cp_lexer_peek_token (parser->lexer);
5925 if (scope
5926 && token->type == CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5928 != CPP_LESS)
5929 && (token->u.value == TYPE_IDENTIFIER (scope)
5930 || (CLASS_TYPE_P (scope)
5931 && constructor_name_p (token->u.value, scope))))
5932 {
5933 cp_lexer_consume_token (parser->lexer);
5934 return build_nt (BIT_NOT_EXPR, scope);
5935 }
5936
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token, RID_AUTO))
5939 {
5940 if (cxx_dialect < cxx14)
5941 pedwarn (input_location, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser->lexer);
5945 return build_nt (BIT_NOT_EXPR, make_auto ());
5946 }
5947
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5950
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5954 done = false;
5955 type_decl = NULL_TREE;
5956 if (scope)
5957 {
5958 cp_parser_parse_tentatively (parser);
5959 type_decl = cp_parser_class_name (parser,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5962 typename_type,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5965 declarator_p);
5966 if (cp_parser_parse_definitely (parser))
5967 done = true;
5968 }
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done && scope && qualifying_scope)
5971 {
5972 cp_parser_parse_tentatively (parser);
5973 parser->scope = qualifying_scope;
5974 parser->object_scope = NULL_TREE;
5975 parser->qualifying_scope = NULL_TREE;
5976 type_decl
5977 = cp_parser_class_name (parser,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5980 typename_type,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5983 declarator_p);
5984 if (cp_parser_parse_definitely (parser))
5985 done = true;
5986 }
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done && object_scope)
5989 {
5990 cp_parser_parse_tentatively (parser);
5991 parser->scope = object_scope;
5992 parser->object_scope = NULL_TREE;
5993 parser->qualifying_scope = NULL_TREE;
5994 type_decl
5995 = cp_parser_class_name (parser,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5998 typename_type,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6001 declarator_p);
6002 if (cp_parser_parse_definitely (parser))
6003 done = true;
6004 }
6005 /* Look in the surrounding context. */
6006 if (!done)
6007 {
6008 parser->scope = NULL_TREE;
6009 parser->object_scope = NULL_TREE;
6010 parser->qualifying_scope = NULL_TREE;
6011 if (processing_template_decl)
6012 cp_parser_parse_tentatively (parser);
6013 type_decl
6014 = cp_parser_class_name (parser,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6017 typename_type,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6020 declarator_p);
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser))
6023 {
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6027 {
6028 cp_parser_simulate_error (parser);
6029 return error_mark_node;
6030 }
6031 /* Otherwise, accept it and check for a match at instantiation
6032 time. */
6033 type_decl = cp_parser_identifier (parser);
6034 if (type_decl != error_mark_node)
6035 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6036 return type_decl;
6037 }
6038 }
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl == error_mark_node && scope)
6044 return build_nt (BIT_NOT_EXPR, scope);
6045 else if (type_decl == error_mark_node)
6046 return error_mark_node;
6047
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6050 {
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6052 error_at (token->location,
6053 "declaration of %<~%T%> as member of %qT",
6054 type_decl, scope);
6055 cp_parser_simulate_error (parser);
6056 return error_mark_node;
6057 }
6058
6059 /* [class.dtor]
6060
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6063 if (declarator_p
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6065 && !DECL_SELF_REFERENCE_P (type_decl)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6067 error_at (token->location,
6068 "typedef-name %qD used as destructor declarator",
6069 type_decl);
6070
6071 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6072 }
6073
6074 case CPP_KEYWORD:
6075 if (token->keyword == RID_OPERATOR)
6076 {
6077 cp_expr id;
6078
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser);
6081 /* Try a template-id. */
6082 id = cp_parser_template_id (parser, template_keyword_p,
6083 /*check_dependency_p=*/true,
6084 none_type,
6085 declarator_p);
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser))
6088 return id;
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser);
6092 /* Try an operator-function-id. */
6093 id = cp_parser_operator_function_id (parser);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser))
6096 id = cp_parser_conversion_function_id (parser);
6097
6098 return id;
6099 }
6100 /* Fall through. */
6101
6102 default:
6103 if (optional_p)
6104 return NULL_TREE;
6105 cp_parser_error (parser, "expected unqualified-id");
6106 return error_mark_node;
6107 }
6108 }
6109
6110 /* Parse an (optional) nested-name-specifier.
6111
6112 nested-name-specifier: [C++98]
6113 class-or-namespace-name :: nested-name-specifier [opt]
6114 class-or-namespace-name :: template nested-name-specifier [opt]
6115
6116 nested-name-specifier: [C++0x]
6117 type-name ::
6118 namespace-name ::
6119 nested-name-specifier identifier ::
6120 nested-name-specifier template [opt] simple-template-id ::
6121
6122 PARSER->SCOPE should be set appropriately before this function is
6123 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6124 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6125 in name lookups.
6126
6127 Sets PARSER->SCOPE to the class (TYPE) or namespace
6128 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6129 it unchanged if there is no nested-name-specifier. Returns the new
6130 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6131
6132 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6133 part of a declaration and/or decl-specifier. */
6134
6135 static tree
6136 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6137 bool typename_keyword_p,
6138 bool check_dependency_p,
6139 bool type_p,
6140 bool is_declaration,
6141 bool template_keyword_p /* = false */)
6142 {
6143 bool success = false;
6144 cp_token_position start = 0;
6145 cp_token *token;
6146
6147 /* Remember where the nested-name-specifier starts. */
6148 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6149 {
6150 start = cp_lexer_token_position (parser->lexer, false);
6151 push_deferring_access_checks (dk_deferred);
6152 }
6153
6154 while (true)
6155 {
6156 tree new_scope;
6157 tree old_scope;
6158 tree saved_qualifying_scope;
6159
6160 /* Spot cases that cannot be the beginning of a
6161 nested-name-specifier. */
6162 token = cp_lexer_peek_token (parser->lexer);
6163
6164 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6165 the already parsed nested-name-specifier. */
6166 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6167 {
6168 /* Grab the nested-name-specifier and continue the loop. */
6169 cp_parser_pre_parsed_nested_name_specifier (parser);
6170 /* If we originally encountered this nested-name-specifier
6171 with IS_DECLARATION set to false, we will not have
6172 resolved TYPENAME_TYPEs, so we must do so here. */
6173 if (is_declaration
6174 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6175 {
6176 new_scope = resolve_typename_type (parser->scope,
6177 /*only_current_p=*/false);
6178 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6179 parser->scope = new_scope;
6180 }
6181 success = true;
6182 continue;
6183 }
6184
6185 /* Spot cases that cannot be the beginning of a
6186 nested-name-specifier. On the second and subsequent times
6187 through the loop, we look for the `template' keyword. */
6188 if (success && token->keyword == RID_TEMPLATE)
6189 ;
6190 /* A template-id can start a nested-name-specifier. */
6191 else if (token->type == CPP_TEMPLATE_ID)
6192 ;
6193 /* DR 743: decltype can be used in a nested-name-specifier. */
6194 else if (token_is_decltype (token))
6195 ;
6196 else
6197 {
6198 /* If the next token is not an identifier, then it is
6199 definitely not a type-name or namespace-name. */
6200 if (token->type != CPP_NAME)
6201 break;
6202 /* If the following token is neither a `<' (to begin a
6203 template-id), nor a `::', then we are not looking at a
6204 nested-name-specifier. */
6205 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6206
6207 if (token->type == CPP_COLON
6208 && parser->colon_corrects_to_scope_p
6209 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6210 {
6211 gcc_rich_location richloc (token->location);
6212 richloc.add_fixit_replace ("::");
6213 error_at (&richloc,
6214 "found %<:%> in nested-name-specifier, "
6215 "expected %<::%>");
6216 token->type = CPP_SCOPE;
6217 }
6218
6219 if (token->type != CPP_SCOPE
6220 && !cp_parser_nth_token_starts_template_argument_list_p
6221 (parser, 2))
6222 break;
6223 }
6224
6225 /* The nested-name-specifier is optional, so we parse
6226 tentatively. */
6227 cp_parser_parse_tentatively (parser);
6228
6229 /* Look for the optional `template' keyword, if this isn't the
6230 first time through the loop. */
6231 if (success)
6232 template_keyword_p = cp_parser_optional_template_keyword (parser);
6233
6234 /* Save the old scope since the name lookup we are about to do
6235 might destroy it. */
6236 old_scope = parser->scope;
6237 saved_qualifying_scope = parser->qualifying_scope;
6238 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6239 look up names in "X<T>::I" in order to determine that "Y" is
6240 a template. So, if we have a typename at this point, we make
6241 an effort to look through it. */
6242 if (is_declaration
6243 && !typename_keyword_p
6244 && parser->scope
6245 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6246 parser->scope = resolve_typename_type (parser->scope,
6247 /*only_current_p=*/false);
6248 /* Parse the qualifying entity. */
6249 new_scope
6250 = cp_parser_qualifying_entity (parser,
6251 typename_keyword_p,
6252 template_keyword_p,
6253 check_dependency_p,
6254 type_p,
6255 is_declaration);
6256 /* Look for the `::' token. */
6257 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6258
6259 /* If we found what we wanted, we keep going; otherwise, we're
6260 done. */
6261 if (!cp_parser_parse_definitely (parser))
6262 {
6263 bool error_p = false;
6264
6265 /* Restore the OLD_SCOPE since it was valid before the
6266 failed attempt at finding the last
6267 class-or-namespace-name. */
6268 parser->scope = old_scope;
6269 parser->qualifying_scope = saved_qualifying_scope;
6270
6271 /* If the next token is a decltype, and the one after that is a
6272 `::', then the decltype has failed to resolve to a class or
6273 enumeration type. Give this error even when parsing
6274 tentatively since it can't possibly be valid--and we're going
6275 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6276 won't get another chance.*/
6277 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6278 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6279 == CPP_SCOPE))
6280 {
6281 token = cp_lexer_consume_token (parser->lexer);
6282 error_at (token->location, "decltype evaluates to %qT, "
6283 "which is not a class or enumeration type",
6284 token->u.tree_check_value->value);
6285 parser->scope = error_mark_node;
6286 error_p = true;
6287 /* As below. */
6288 success = true;
6289 cp_lexer_consume_token (parser->lexer);
6290 }
6291
6292 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6293 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6294 {
6295 /* If we have a non-type template-id followed by ::, it can't
6296 possibly be valid. */
6297 token = cp_lexer_peek_token (parser->lexer);
6298 tree tid = token->u.tree_check_value->value;
6299 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6300 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6301 {
6302 tree tmpl = NULL_TREE;
6303 if (is_overloaded_fn (tid))
6304 {
6305 tree fns = get_fns (tid);
6306 if (OVL_SINGLE_P (fns))
6307 tmpl = OVL_FIRST (fns);
6308 error_at (token->location, "function template-id %qD "
6309 "in nested-name-specifier", tid);
6310 }
6311 else
6312 {
6313 /* Variable template. */
6314 tmpl = TREE_OPERAND (tid, 0);
6315 gcc_assert (variable_template_p (tmpl));
6316 error_at (token->location, "variable template-id %qD "
6317 "in nested-name-specifier", tid);
6318 }
6319 if (tmpl)
6320 inform (DECL_SOURCE_LOCATION (tmpl),
6321 "%qD declared here", tmpl);
6322
6323 parser->scope = error_mark_node;
6324 error_p = true;
6325 /* As below. */
6326 success = true;
6327 cp_lexer_consume_token (parser->lexer);
6328 cp_lexer_consume_token (parser->lexer);
6329 }
6330 }
6331
6332 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6333 break;
6334 /* If the next token is an identifier, and the one after
6335 that is a `::', then any valid interpretation would have
6336 found a class-or-namespace-name. */
6337 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6338 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6339 == CPP_SCOPE)
6340 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6341 != CPP_COMPL))
6342 {
6343 token = cp_lexer_consume_token (parser->lexer);
6344 if (!error_p)
6345 {
6346 if (!token->error_reported)
6347 {
6348 tree decl;
6349 tree ambiguous_decls;
6350
6351 decl = cp_parser_lookup_name (parser, token->u.value,
6352 none_type,
6353 /*is_template=*/false,
6354 /*is_namespace=*/false,
6355 /*check_dependency=*/true,
6356 &ambiguous_decls,
6357 token->location);
6358 if (TREE_CODE (decl) == TEMPLATE_DECL)
6359 error_at (token->location,
6360 "%qD used without template arguments",
6361 decl);
6362 else if (ambiguous_decls)
6363 {
6364 // cp_parser_lookup_name has the same diagnostic,
6365 // thus make sure to emit it at most once.
6366 if (cp_parser_uncommitted_to_tentative_parse_p
6367 (parser))
6368 {
6369 error_at (token->location,
6370 "reference to %qD is ambiguous",
6371 token->u.value);
6372 print_candidates (ambiguous_decls);
6373 }
6374 decl = error_mark_node;
6375 }
6376 else
6377 {
6378 if (cxx_dialect != cxx98)
6379 cp_parser_name_lookup_error
6380 (parser, token->u.value, decl, NLE_NOT_CXX98,
6381 token->location);
6382 else
6383 cp_parser_name_lookup_error
6384 (parser, token->u.value, decl, NLE_CXX98,
6385 token->location);
6386 }
6387 }
6388 parser->scope = error_mark_node;
6389 error_p = true;
6390 /* Treat this as a successful nested-name-specifier
6391 due to:
6392
6393 [basic.lookup.qual]
6394
6395 If the name found is not a class-name (clause
6396 _class_) or namespace-name (_namespace.def_), the
6397 program is ill-formed. */
6398 success = true;
6399 }
6400 cp_lexer_consume_token (parser->lexer);
6401 }
6402 break;
6403 }
6404 /* We've found one valid nested-name-specifier. */
6405 success = true;
6406 /* Name lookup always gives us a DECL. */
6407 if (TREE_CODE (new_scope) == TYPE_DECL)
6408 new_scope = TREE_TYPE (new_scope);
6409 /* Uses of "template" must be followed by actual templates. */
6410 if (template_keyword_p
6411 && !(CLASS_TYPE_P (new_scope)
6412 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6414 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6415 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6416 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6417 == TEMPLATE_ID_EXPR)))
6418 permerror (input_location, TYPE_P (new_scope)
6419 ? G_("%qT is not a template")
6420 : G_("%qD is not a template"),
6421 new_scope);
6422 /* If it is a class scope, try to complete it; we are about to
6423 be looking up names inside the class. */
6424 if (TYPE_P (new_scope)
6425 /* Since checking types for dependency can be expensive,
6426 avoid doing it if the type is already complete. */
6427 && !COMPLETE_TYPE_P (new_scope)
6428 /* Do not try to complete dependent types. */
6429 && !dependent_type_p (new_scope))
6430 {
6431 new_scope = complete_type (new_scope);
6432 /* If it is a typedef to current class, use the current
6433 class instead, as the typedef won't have any names inside
6434 it yet. */
6435 if (!COMPLETE_TYPE_P (new_scope)
6436 && currently_open_class (new_scope))
6437 new_scope = TYPE_MAIN_VARIANT (new_scope);
6438 }
6439 /* Make sure we look in the right scope the next time through
6440 the loop. */
6441 parser->scope = new_scope;
6442 }
6443
6444 /* If parsing tentatively, replace the sequence of tokens that makes
6445 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6446 token. That way, should we re-parse the token stream, we will
6447 not have to repeat the effort required to do the parse, nor will
6448 we issue duplicate error messages. */
6449 if (success && start)
6450 {
6451 cp_token *token;
6452
6453 token = cp_lexer_token_at (parser->lexer, start);
6454 /* Reset the contents of the START token. */
6455 token->type = CPP_NESTED_NAME_SPECIFIER;
6456 /* Retrieve any deferred checks. Do not pop this access checks yet
6457 so the memory will not be reclaimed during token replacing below. */
6458 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6459 token->u.tree_check_value->value = parser->scope;
6460 token->u.tree_check_value->checks = get_deferred_access_checks ();
6461 token->u.tree_check_value->qualifying_scope =
6462 parser->qualifying_scope;
6463 token->keyword = RID_MAX;
6464
6465 /* Purge all subsequent tokens. */
6466 cp_lexer_purge_tokens_after (parser->lexer, start);
6467 }
6468
6469 if (start)
6470 pop_to_parent_deferring_access_checks ();
6471
6472 return success ? parser->scope : NULL_TREE;
6473 }
6474
6475 /* Parse a nested-name-specifier. See
6476 cp_parser_nested_name_specifier_opt for details. This function
6477 behaves identically, except that it will an issue an error if no
6478 nested-name-specifier is present. */
6479
6480 static tree
6481 cp_parser_nested_name_specifier (cp_parser *parser,
6482 bool typename_keyword_p,
6483 bool check_dependency_p,
6484 bool type_p,
6485 bool is_declaration)
6486 {
6487 tree scope;
6488
6489 /* Look for the nested-name-specifier. */
6490 scope = cp_parser_nested_name_specifier_opt (parser,
6491 typename_keyword_p,
6492 check_dependency_p,
6493 type_p,
6494 is_declaration);
6495 /* If it was not present, issue an error message. */
6496 if (!scope)
6497 {
6498 cp_parser_error (parser, "expected nested-name-specifier");
6499 parser->scope = NULL_TREE;
6500 }
6501
6502 return scope;
6503 }
6504
6505 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6506 this is either a class-name or a namespace-name (which corresponds
6507 to the class-or-namespace-name production in the grammar). For
6508 C++0x, it can also be a type-name that refers to an enumeration
6509 type or a simple-template-id.
6510
6511 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6512 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6513 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6514 TYPE_P is TRUE iff the next name should be taken as a class-name,
6515 even the same name is declared to be another entity in the same
6516 scope.
6517
6518 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6519 specified by the class-or-namespace-name. If neither is found the
6520 ERROR_MARK_NODE is returned. */
6521
6522 static tree
6523 cp_parser_qualifying_entity (cp_parser *parser,
6524 bool typename_keyword_p,
6525 bool template_keyword_p,
6526 bool check_dependency_p,
6527 bool type_p,
6528 bool is_declaration)
6529 {
6530 tree saved_scope;
6531 tree saved_qualifying_scope;
6532 tree saved_object_scope;
6533 tree scope;
6534 bool only_class_p;
6535 bool successful_parse_p;
6536
6537 /* DR 743: decltype can appear in a nested-name-specifier. */
6538 if (cp_lexer_next_token_is_decltype (parser->lexer))
6539 {
6540 scope = cp_parser_decltype (parser);
6541 if (TREE_CODE (scope) != ENUMERAL_TYPE
6542 && !MAYBE_CLASS_TYPE_P (scope))
6543 {
6544 cp_parser_simulate_error (parser);
6545 return error_mark_node;
6546 }
6547 if (TYPE_NAME (scope))
6548 scope = TYPE_NAME (scope);
6549 return scope;
6550 }
6551
6552 /* Before we try to parse the class-name, we must save away the
6553 current PARSER->SCOPE since cp_parser_class_name will destroy
6554 it. */
6555 saved_scope = parser->scope;
6556 saved_qualifying_scope = parser->qualifying_scope;
6557 saved_object_scope = parser->object_scope;
6558 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6559 there is no need to look for a namespace-name. */
6560 only_class_p = template_keyword_p
6561 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6562 if (!only_class_p)
6563 cp_parser_parse_tentatively (parser);
6564 scope = cp_parser_class_name (parser,
6565 typename_keyword_p,
6566 template_keyword_p,
6567 type_p ? class_type : none_type,
6568 check_dependency_p,
6569 /*class_head_p=*/false,
6570 is_declaration,
6571 /*enum_ok=*/cxx_dialect > cxx98);
6572 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6573 /* If that didn't work, try for a namespace-name. */
6574 if (!only_class_p && !successful_parse_p)
6575 {
6576 /* Restore the saved scope. */
6577 parser->scope = saved_scope;
6578 parser->qualifying_scope = saved_qualifying_scope;
6579 parser->object_scope = saved_object_scope;
6580 /* If we are not looking at an identifier followed by the scope
6581 resolution operator, then this is not part of a
6582 nested-name-specifier. (Note that this function is only used
6583 to parse the components of a nested-name-specifier.) */
6584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6585 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6586 return error_mark_node;
6587 scope = cp_parser_namespace_name (parser);
6588 }
6589
6590 return scope;
6591 }
6592
6593 /* Return true if we are looking at a compound-literal, false otherwise. */
6594
6595 static bool
6596 cp_parser_compound_literal_p (cp_parser *parser)
6597 {
6598 cp_lexer_save_tokens (parser->lexer);
6599
6600 /* Skip tokens until the next token is a closing parenthesis.
6601 If we find the closing `)', and the next token is a `{', then
6602 we are looking at a compound-literal. */
6603 bool compound_literal_p
6604 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6605 /*consume_paren=*/true)
6606 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6607
6608 /* Roll back the tokens we skipped. */
6609 cp_lexer_rollback_tokens (parser->lexer);
6610
6611 return compound_literal_p;
6612 }
6613
6614 /* Return true if EXPR is the integer constant zero or a complex constant
6615 of zero, without any folding, but ignoring location wrappers. */
6616
6617 static bool
6618 literal_integer_zerop (const_tree expr)
6619 {
6620 STRIP_ANY_LOCATION_WRAPPER (expr);
6621 return integer_zerop (expr);
6622 }
6623
6624 /* Parse a postfix-expression.
6625
6626 postfix-expression:
6627 primary-expression
6628 postfix-expression [ expression ]
6629 postfix-expression ( expression-list [opt] )
6630 simple-type-specifier ( expression-list [opt] )
6631 typename :: [opt] nested-name-specifier identifier
6632 ( expression-list [opt] )
6633 typename :: [opt] nested-name-specifier template [opt] template-id
6634 ( expression-list [opt] )
6635 postfix-expression . template [opt] id-expression
6636 postfix-expression -> template [opt] id-expression
6637 postfix-expression . pseudo-destructor-name
6638 postfix-expression -> pseudo-destructor-name
6639 postfix-expression ++
6640 postfix-expression --
6641 dynamic_cast < type-id > ( expression )
6642 static_cast < type-id > ( expression )
6643 reinterpret_cast < type-id > ( expression )
6644 const_cast < type-id > ( expression )
6645 typeid ( expression )
6646 typeid ( type-id )
6647
6648 GNU Extension:
6649
6650 postfix-expression:
6651 ( type-id ) { initializer-list , [opt] }
6652
6653 This extension is a GNU version of the C99 compound-literal
6654 construct. (The C99 grammar uses `type-name' instead of `type-id',
6655 but they are essentially the same concept.)
6656
6657 If ADDRESS_P is true, the postfix expression is the operand of the
6658 `&' operator. CAST_P is true if this expression is the target of a
6659 cast.
6660
6661 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6662 class member access expressions [expr.ref].
6663
6664 Returns a representation of the expression. */
6665
6666 static cp_expr
6667 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6668 bool member_access_only_p, bool decltype_p,
6669 cp_id_kind * pidk_return)
6670 {
6671 cp_token *token;
6672 location_t loc;
6673 enum rid keyword;
6674 cp_id_kind idk = CP_ID_KIND_NONE;
6675 cp_expr postfix_expression = NULL_TREE;
6676 bool is_member_access = false;
6677
6678 /* Peek at the next token. */
6679 token = cp_lexer_peek_token (parser->lexer);
6680 loc = token->location;
6681 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6682
6683 /* Some of the productions are determined by keywords. */
6684 keyword = token->keyword;
6685 switch (keyword)
6686 {
6687 case RID_DYNCAST:
6688 case RID_STATCAST:
6689 case RID_REINTCAST:
6690 case RID_CONSTCAST:
6691 {
6692 tree type;
6693 cp_expr expression;
6694 const char *saved_message;
6695 bool saved_in_type_id_in_expr_p;
6696
6697 /* All of these can be handled in the same way from the point
6698 of view of parsing. Begin by consuming the token
6699 identifying the cast. */
6700 cp_lexer_consume_token (parser->lexer);
6701
6702 /* New types cannot be defined in the cast. */
6703 saved_message = parser->type_definition_forbidden_message;
6704 parser->type_definition_forbidden_message
6705 = G_("types may not be defined in casts");
6706
6707 /* Look for the opening `<'. */
6708 cp_parser_require (parser, CPP_LESS, RT_LESS);
6709 /* Parse the type to which we are casting. */
6710 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6711 parser->in_type_id_in_expr_p = true;
6712 type = cp_parser_type_id (parser);
6713 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6714 /* Look for the closing `>'. */
6715 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6716 /* Restore the old message. */
6717 parser->type_definition_forbidden_message = saved_message;
6718
6719 bool saved_greater_than_is_operator_p
6720 = parser->greater_than_is_operator_p;
6721 parser->greater_than_is_operator_p = true;
6722
6723 /* And the expression which is being cast. */
6724 matching_parens parens;
6725 parens.require_open (parser);
6726 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6727 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6728 RT_CLOSE_PAREN);
6729 location_t end_loc = close_paren ?
6730 close_paren->location : UNKNOWN_LOCATION;
6731
6732 parser->greater_than_is_operator_p
6733 = saved_greater_than_is_operator_p;
6734
6735 /* Only type conversions to integral or enumeration types
6736 can be used in constant-expressions. */
6737 if (!cast_valid_in_integral_constant_expression_p (type)
6738 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6739 {
6740 postfix_expression = error_mark_node;
6741 break;
6742 }
6743
6744 switch (keyword)
6745 {
6746 case RID_DYNCAST:
6747 postfix_expression
6748 = build_dynamic_cast (type, expression, tf_warning_or_error);
6749 break;
6750 case RID_STATCAST:
6751 postfix_expression
6752 = build_static_cast (type, expression, tf_warning_or_error);
6753 break;
6754 case RID_REINTCAST:
6755 postfix_expression
6756 = build_reinterpret_cast (type, expression,
6757 tf_warning_or_error);
6758 break;
6759 case RID_CONSTCAST:
6760 postfix_expression
6761 = build_const_cast (type, expression, tf_warning_or_error);
6762 break;
6763 default:
6764 gcc_unreachable ();
6765 }
6766
6767 /* Construct a location e.g. :
6768 reinterpret_cast <int *> (expr)
6769 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6770 ranging from the start of the "*_cast" token to the final closing
6771 paren, with the caret at the start. */
6772 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6773 postfix_expression.set_location (cp_cast_loc);
6774 }
6775 break;
6776
6777 case RID_TYPEID:
6778 {
6779 tree type;
6780 const char *saved_message;
6781 bool saved_in_type_id_in_expr_p;
6782
6783 /* Consume the `typeid' token. */
6784 cp_lexer_consume_token (parser->lexer);
6785 /* Look for the `(' token. */
6786 matching_parens parens;
6787 parens.require_open (parser);
6788 /* Types cannot be defined in a `typeid' expression. */
6789 saved_message = parser->type_definition_forbidden_message;
6790 parser->type_definition_forbidden_message
6791 = G_("types may not be defined in a %<typeid%> expression");
6792 /* We can't be sure yet whether we're looking at a type-id or an
6793 expression. */
6794 cp_parser_parse_tentatively (parser);
6795 /* Try a type-id first. */
6796 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6797 parser->in_type_id_in_expr_p = true;
6798 type = cp_parser_type_id (parser);
6799 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6800 /* Look for the `)' token. Otherwise, we can't be sure that
6801 we're not looking at an expression: consider `typeid (int
6802 (3))', for example. */
6803 cp_token *close_paren = parens.require_close (parser);
6804 /* If all went well, simply lookup the type-id. */
6805 if (cp_parser_parse_definitely (parser))
6806 postfix_expression = get_typeid (type, tf_warning_or_error);
6807 /* Otherwise, fall back to the expression variant. */
6808 else
6809 {
6810 tree expression;
6811
6812 /* Look for an expression. */
6813 expression = cp_parser_expression (parser, & idk);
6814 /* Compute its typeid. */
6815 postfix_expression = build_typeid (expression, tf_warning_or_error);
6816 /* Look for the `)' token. */
6817 close_paren = parens.require_close (parser);
6818 }
6819 /* Restore the saved message. */
6820 parser->type_definition_forbidden_message = saved_message;
6821 /* `typeid' may not appear in an integral constant expression. */
6822 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6823 postfix_expression = error_mark_node;
6824
6825 /* Construct a location e.g. :
6826 typeid (expr)
6827 ^~~~~~~~~~~~~
6828 ranging from the start of the "typeid" token to the final closing
6829 paren, with the caret at the start. */
6830 if (close_paren)
6831 {
6832 location_t typeid_loc
6833 = make_location (start_loc, start_loc, close_paren->location);
6834 postfix_expression.set_location (typeid_loc);
6835 postfix_expression.maybe_add_location_wrapper ();
6836 }
6837 }
6838 break;
6839
6840 case RID_TYPENAME:
6841 {
6842 tree type;
6843 /* The syntax permitted here is the same permitted for an
6844 elaborated-type-specifier. */
6845 ++parser->prevent_constrained_type_specifiers;
6846 type = cp_parser_elaborated_type_specifier (parser,
6847 /*is_friend=*/false,
6848 /*is_declaration=*/false);
6849 --parser->prevent_constrained_type_specifiers;
6850 postfix_expression = cp_parser_functional_cast (parser, type);
6851 }
6852 break;
6853
6854 case RID_ADDRESSOF:
6855 case RID_BUILTIN_SHUFFLE:
6856 case RID_BUILTIN_LAUNDER:
6857 {
6858 vec<tree, va_gc> *vec;
6859 unsigned int i;
6860 tree p;
6861
6862 cp_lexer_consume_token (parser->lexer);
6863 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6864 /*cast_p=*/false, /*allow_expansion_p=*/true,
6865 /*non_constant_p=*/NULL);
6866 if (vec == NULL)
6867 {
6868 postfix_expression = error_mark_node;
6869 break;
6870 }
6871
6872 FOR_EACH_VEC_ELT (*vec, i, p)
6873 mark_exp_read (p);
6874
6875 switch (keyword)
6876 {
6877 case RID_ADDRESSOF:
6878 if (vec->length () == 1)
6879 postfix_expression
6880 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6881 else
6882 {
6883 error_at (loc, "wrong number of arguments to "
6884 "%<__builtin_addressof%>");
6885 postfix_expression = error_mark_node;
6886 }
6887 break;
6888
6889 case RID_BUILTIN_LAUNDER:
6890 if (vec->length () == 1)
6891 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6892 tf_warning_or_error);
6893 else
6894 {
6895 error_at (loc, "wrong number of arguments to "
6896 "%<__builtin_launder%>");
6897 postfix_expression = error_mark_node;
6898 }
6899 break;
6900
6901 case RID_BUILTIN_SHUFFLE:
6902 if (vec->length () == 2)
6903 postfix_expression
6904 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6905 (*vec)[1], tf_warning_or_error);
6906 else if (vec->length () == 3)
6907 postfix_expression
6908 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6909 (*vec)[2], tf_warning_or_error);
6910 else
6911 {
6912 error_at (loc, "wrong number of arguments to "
6913 "%<__builtin_shuffle%>");
6914 postfix_expression = error_mark_node;
6915 }
6916 break;
6917
6918 default:
6919 gcc_unreachable ();
6920 }
6921 break;
6922 }
6923
6924 default:
6925 {
6926 tree type;
6927
6928 /* If the next thing is a simple-type-specifier, we may be
6929 looking at a functional cast. We could also be looking at
6930 an id-expression. So, we try the functional cast, and if
6931 that doesn't work we fall back to the primary-expression. */
6932 cp_parser_parse_tentatively (parser);
6933 /* Look for the simple-type-specifier. */
6934 ++parser->prevent_constrained_type_specifiers;
6935 type = cp_parser_simple_type_specifier (parser,
6936 /*decl_specs=*/NULL,
6937 CP_PARSER_FLAGS_NONE);
6938 --parser->prevent_constrained_type_specifiers;
6939 /* Parse the cast itself. */
6940 if (!cp_parser_error_occurred (parser))
6941 postfix_expression
6942 = cp_parser_functional_cast (parser, type);
6943 /* If that worked, we're done. */
6944 if (cp_parser_parse_definitely (parser))
6945 break;
6946
6947 /* If the functional-cast didn't work out, try a
6948 compound-literal. */
6949 if (cp_parser_allow_gnu_extensions_p (parser)
6950 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6951 {
6952 cp_expr initializer = NULL_TREE;
6953
6954 cp_parser_parse_tentatively (parser);
6955
6956 matching_parens parens;
6957 parens.consume_open (parser);
6958
6959 /* Avoid calling cp_parser_type_id pointlessly, see comment
6960 in cp_parser_cast_expression about c++/29234. */
6961 if (!cp_parser_compound_literal_p (parser))
6962 cp_parser_simulate_error (parser);
6963 else
6964 {
6965 /* Parse the type. */
6966 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6967 parser->in_type_id_in_expr_p = true;
6968 type = cp_parser_type_id (parser);
6969 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6970 parens.require_close (parser);
6971 }
6972
6973 /* If things aren't going well, there's no need to
6974 keep going. */
6975 if (!cp_parser_error_occurred (parser))
6976 {
6977 bool non_constant_p;
6978 /* Parse the brace-enclosed initializer list. */
6979 initializer = cp_parser_braced_list (parser,
6980 &non_constant_p);
6981 }
6982 /* If that worked, we're definitely looking at a
6983 compound-literal expression. */
6984 if (cp_parser_parse_definitely (parser))
6985 {
6986 /* Warn the user that a compound literal is not
6987 allowed in standard C++. */
6988 pedwarn (input_location, OPT_Wpedantic,
6989 "ISO C++ forbids compound-literals");
6990 /* For simplicity, we disallow compound literals in
6991 constant-expressions. We could
6992 allow compound literals of integer type, whose
6993 initializer was a constant, in constant
6994 expressions. Permitting that usage, as a further
6995 extension, would not change the meaning of any
6996 currently accepted programs. (Of course, as
6997 compound literals are not part of ISO C++, the
6998 standard has nothing to say.) */
6999 if (cp_parser_non_integral_constant_expression (parser,
7000 NIC_NCC))
7001 {
7002 postfix_expression = error_mark_node;
7003 break;
7004 }
7005 /* Form the representation of the compound-literal. */
7006 postfix_expression
7007 = finish_compound_literal (type, initializer,
7008 tf_warning_or_error, fcl_c99);
7009 postfix_expression.set_location (initializer.get_location ());
7010 break;
7011 }
7012 }
7013
7014 /* It must be a primary-expression. */
7015 postfix_expression
7016 = cp_parser_primary_expression (parser, address_p, cast_p,
7017 /*template_arg_p=*/false,
7018 decltype_p,
7019 &idk);
7020 }
7021 break;
7022 }
7023
7024 /* Note that we don't need to worry about calling build_cplus_new on a
7025 class-valued CALL_EXPR in decltype when it isn't the end of the
7026 postfix-expression; unary_complex_lvalue will take care of that for
7027 all these cases. */
7028
7029 /* Keep looping until the postfix-expression is complete. */
7030 while (true)
7031 {
7032 if (idk == CP_ID_KIND_UNQUALIFIED
7033 && identifier_p (postfix_expression)
7034 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7035 /* It is not a Koenig lookup function call. */
7036 postfix_expression
7037 = unqualified_name_lookup_error (postfix_expression);
7038
7039 /* Peek at the next token. */
7040 token = cp_lexer_peek_token (parser->lexer);
7041
7042 switch (token->type)
7043 {
7044 case CPP_OPEN_SQUARE:
7045 if (cp_next_tokens_can_be_std_attribute_p (parser))
7046 {
7047 cp_parser_error (parser,
7048 "two consecutive %<[%> shall "
7049 "only introduce an attribute");
7050 return error_mark_node;
7051 }
7052 postfix_expression
7053 = cp_parser_postfix_open_square_expression (parser,
7054 postfix_expression,
7055 false,
7056 decltype_p);
7057 postfix_expression.set_range (start_loc,
7058 postfix_expression.get_location ());
7059
7060 idk = CP_ID_KIND_NONE;
7061 is_member_access = false;
7062 break;
7063
7064 case CPP_OPEN_PAREN:
7065 /* postfix-expression ( expression-list [opt] ) */
7066 {
7067 bool koenig_p;
7068 bool is_builtin_constant_p;
7069 bool saved_integral_constant_expression_p = false;
7070 bool saved_non_integral_constant_expression_p = false;
7071 tsubst_flags_t complain = complain_flags (decltype_p);
7072 vec<tree, va_gc> *args;
7073 location_t close_paren_loc = UNKNOWN_LOCATION;
7074
7075 is_member_access = false;
7076
7077 is_builtin_constant_p
7078 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7079 if (is_builtin_constant_p)
7080 {
7081 /* The whole point of __builtin_constant_p is to allow
7082 non-constant expressions to appear as arguments. */
7083 saved_integral_constant_expression_p
7084 = parser->integral_constant_expression_p;
7085 saved_non_integral_constant_expression_p
7086 = parser->non_integral_constant_expression_p;
7087 parser->integral_constant_expression_p = false;
7088 }
7089 args = (cp_parser_parenthesized_expression_list
7090 (parser, non_attr,
7091 /*cast_p=*/false, /*allow_expansion_p=*/true,
7092 /*non_constant_p=*/NULL,
7093 /*close_paren_loc=*/&close_paren_loc,
7094 /*wrap_locations_p=*/true));
7095 if (is_builtin_constant_p)
7096 {
7097 parser->integral_constant_expression_p
7098 = saved_integral_constant_expression_p;
7099 parser->non_integral_constant_expression_p
7100 = saved_non_integral_constant_expression_p;
7101 }
7102
7103 if (args == NULL)
7104 {
7105 postfix_expression = error_mark_node;
7106 break;
7107 }
7108
7109 /* Function calls are not permitted in
7110 constant-expressions. */
7111 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7112 && cp_parser_non_integral_constant_expression (parser,
7113 NIC_FUNC_CALL))
7114 {
7115 postfix_expression = error_mark_node;
7116 release_tree_vector (args);
7117 break;
7118 }
7119
7120 koenig_p = false;
7121 if (idk == CP_ID_KIND_UNQUALIFIED
7122 || idk == CP_ID_KIND_TEMPLATE_ID)
7123 {
7124 if (identifier_p (postfix_expression))
7125 {
7126 if (!args->is_empty ())
7127 {
7128 koenig_p = true;
7129 if (!any_type_dependent_arguments_p (args))
7130 postfix_expression
7131 = perform_koenig_lookup (postfix_expression, args,
7132 complain);
7133 }
7134 else
7135 postfix_expression
7136 = unqualified_fn_lookup_error (postfix_expression);
7137 }
7138 /* We do not perform argument-dependent lookup if
7139 normal lookup finds a non-function, in accordance
7140 with the expected resolution of DR 218. */
7141 else if (!args->is_empty ()
7142 && is_overloaded_fn (postfix_expression))
7143 {
7144 tree fn = get_first_fn (postfix_expression);
7145 fn = STRIP_TEMPLATE (fn);
7146
7147 /* Do not do argument dependent lookup if regular
7148 lookup finds a member function or a block-scope
7149 function declaration. [basic.lookup.argdep]/3 */
7150 if (!DECL_FUNCTION_MEMBER_P (fn)
7151 && !DECL_LOCAL_FUNCTION_P (fn))
7152 {
7153 koenig_p = true;
7154 if (!any_type_dependent_arguments_p (args))
7155 postfix_expression
7156 = perform_koenig_lookup (postfix_expression, args,
7157 complain);
7158 }
7159 }
7160 }
7161
7162 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7163 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7164 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7165 && vec_safe_length (args) == 3)
7166 {
7167 tree arg0 = (*args)[0];
7168 tree arg1 = (*args)[1];
7169 tree arg2 = (*args)[2];
7170 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7171 | (literal_integer_zerop (arg2) << 2));
7172 warn_for_memset (input_location, arg0, arg2, literal_mask);
7173 }
7174
7175 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7176 {
7177 tree instance = TREE_OPERAND (postfix_expression, 0);
7178 tree fn = TREE_OPERAND (postfix_expression, 1);
7179
7180 if (processing_template_decl
7181 && (type_dependent_object_expression_p (instance)
7182 || (!BASELINK_P (fn)
7183 && TREE_CODE (fn) != FIELD_DECL)
7184 || type_dependent_expression_p (fn)
7185 || any_type_dependent_arguments_p (args)))
7186 {
7187 maybe_generic_this_capture (instance, fn);
7188 postfix_expression
7189 = build_min_nt_call_vec (postfix_expression, args);
7190 release_tree_vector (args);
7191 break;
7192 }
7193
7194 if (BASELINK_P (fn))
7195 {
7196 postfix_expression
7197 = (build_new_method_call
7198 (instance, fn, &args, NULL_TREE,
7199 (idk == CP_ID_KIND_QUALIFIED
7200 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7201 : LOOKUP_NORMAL),
7202 /*fn_p=*/NULL,
7203 complain));
7204 }
7205 else
7206 postfix_expression
7207 = finish_call_expr (postfix_expression, &args,
7208 /*disallow_virtual=*/false,
7209 /*koenig_p=*/false,
7210 complain);
7211 }
7212 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7213 || TREE_CODE (postfix_expression) == MEMBER_REF
7214 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7215 postfix_expression = (build_offset_ref_call_from_tree
7216 (postfix_expression, &args,
7217 complain));
7218 else if (idk == CP_ID_KIND_QUALIFIED)
7219 /* A call to a static class member, or a namespace-scope
7220 function. */
7221 postfix_expression
7222 = finish_call_expr (postfix_expression, &args,
7223 /*disallow_virtual=*/true,
7224 koenig_p,
7225 complain);
7226 else
7227 /* All other function calls. */
7228 postfix_expression
7229 = finish_call_expr (postfix_expression, &args,
7230 /*disallow_virtual=*/false,
7231 koenig_p,
7232 complain);
7233
7234 if (close_paren_loc != UNKNOWN_LOCATION)
7235 {
7236 location_t combined_loc = make_location (token->location,
7237 start_loc,
7238 close_paren_loc);
7239 postfix_expression.set_location (combined_loc);
7240 }
7241
7242 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7243 idk = CP_ID_KIND_NONE;
7244
7245 release_tree_vector (args);
7246 }
7247 break;
7248
7249 case CPP_DOT:
7250 case CPP_DEREF:
7251 /* postfix-expression . template [opt] id-expression
7252 postfix-expression . pseudo-destructor-name
7253 postfix-expression -> template [opt] id-expression
7254 postfix-expression -> pseudo-destructor-name */
7255
7256 /* Consume the `.' or `->' operator. */
7257 cp_lexer_consume_token (parser->lexer);
7258
7259 postfix_expression
7260 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7261 postfix_expression,
7262 false, &idk, loc);
7263
7264 is_member_access = true;
7265 break;
7266
7267 case CPP_PLUS_PLUS:
7268 /* postfix-expression ++ */
7269 /* Consume the `++' token. */
7270 cp_lexer_consume_token (parser->lexer);
7271 /* Generate a representation for the complete expression. */
7272 postfix_expression
7273 = finish_increment_expr (postfix_expression,
7274 POSTINCREMENT_EXPR);
7275 /* Increments may not appear in constant-expressions. */
7276 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7277 postfix_expression = error_mark_node;
7278 idk = CP_ID_KIND_NONE;
7279 is_member_access = false;
7280 break;
7281
7282 case CPP_MINUS_MINUS:
7283 /* postfix-expression -- */
7284 /* Consume the `--' token. */
7285 cp_lexer_consume_token (parser->lexer);
7286 /* Generate a representation for the complete expression. */
7287 postfix_expression
7288 = finish_increment_expr (postfix_expression,
7289 POSTDECREMENT_EXPR);
7290 /* Decrements may not appear in constant-expressions. */
7291 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7292 postfix_expression = error_mark_node;
7293 idk = CP_ID_KIND_NONE;
7294 is_member_access = false;
7295 break;
7296
7297 default:
7298 if (pidk_return != NULL)
7299 * pidk_return = idk;
7300 if (member_access_only_p)
7301 return is_member_access
7302 ? postfix_expression
7303 : cp_expr (error_mark_node);
7304 else
7305 return postfix_expression;
7306 }
7307 }
7308
7309 /* We should never get here. */
7310 gcc_unreachable ();
7311 return error_mark_node;
7312 }
7313
7314 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7315 by cp_parser_builtin_offsetof. We're looking for
7316
7317 postfix-expression [ expression ]
7318 postfix-expression [ braced-init-list ] (C++11)
7319
7320 FOR_OFFSETOF is set if we're being called in that context, which
7321 changes how we deal with integer constant expressions. */
7322
7323 static tree
7324 cp_parser_postfix_open_square_expression (cp_parser *parser,
7325 tree postfix_expression,
7326 bool for_offsetof,
7327 bool decltype_p)
7328 {
7329 tree index = NULL_TREE;
7330 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7331 bool saved_greater_than_is_operator_p;
7332
7333 /* Consume the `[' token. */
7334 cp_lexer_consume_token (parser->lexer);
7335
7336 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7337 parser->greater_than_is_operator_p = true;
7338
7339 /* Parse the index expression. */
7340 /* ??? For offsetof, there is a question of what to allow here. If
7341 offsetof is not being used in an integral constant expression context,
7342 then we *could* get the right answer by computing the value at runtime.
7343 If we are in an integral constant expression context, then we might
7344 could accept any constant expression; hard to say without analysis.
7345 Rather than open the barn door too wide right away, allow only integer
7346 constant expressions here. */
7347 if (for_offsetof)
7348 index = cp_parser_constant_expression (parser);
7349 else
7350 {
7351 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7352 {
7353 bool expr_nonconst_p;
7354 cp_lexer_set_source_position (parser->lexer);
7355 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7356 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7357 }
7358 else
7359 index = cp_parser_expression (parser);
7360 }
7361
7362 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7363
7364 /* Look for the closing `]'. */
7365 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7366
7367 /* Build the ARRAY_REF. */
7368 postfix_expression = grok_array_decl (loc, postfix_expression,
7369 index, decltype_p);
7370
7371 /* When not doing offsetof, array references are not permitted in
7372 constant-expressions. */
7373 if (!for_offsetof
7374 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7375 postfix_expression = error_mark_node;
7376
7377 return postfix_expression;
7378 }
7379
7380 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7381 dereference of incomplete type, returns true if error_mark_node should
7382 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7383 and *DEPENDENT_P. */
7384
7385 bool
7386 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7387 bool *dependent_p)
7388 {
7389 /* In a template, be permissive by treating an object expression
7390 of incomplete type as dependent (after a pedwarn). */
7391 diagnostic_t kind = (processing_template_decl
7392 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7393
7394 switch (TREE_CODE (*postfix_expression))
7395 {
7396 case CAST_EXPR:
7397 case REINTERPRET_CAST_EXPR:
7398 case CONST_CAST_EXPR:
7399 case STATIC_CAST_EXPR:
7400 case DYNAMIC_CAST_EXPR:
7401 case IMPLICIT_CONV_EXPR:
7402 case VIEW_CONVERT_EXPR:
7403 case NON_LVALUE_EXPR:
7404 kind = DK_ERROR;
7405 break;
7406 case OVERLOAD:
7407 /* Don't emit any diagnostic for OVERLOADs. */
7408 kind = DK_IGNORED;
7409 break;
7410 default:
7411 /* Avoid clobbering e.g. DECLs. */
7412 if (!EXPR_P (*postfix_expression))
7413 kind = DK_ERROR;
7414 break;
7415 }
7416
7417 if (kind == DK_IGNORED)
7418 return false;
7419
7420 location_t exploc = location_of (*postfix_expression);
7421 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7422 if (!MAYBE_CLASS_TYPE_P (*scope))
7423 return true;
7424 if (kind == DK_ERROR)
7425 *scope = *postfix_expression = error_mark_node;
7426 else if (processing_template_decl)
7427 {
7428 *dependent_p = true;
7429 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7430 }
7431 return false;
7432 }
7433
7434 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7435 by cp_parser_builtin_offsetof. We're looking for
7436
7437 postfix-expression . template [opt] id-expression
7438 postfix-expression . pseudo-destructor-name
7439 postfix-expression -> template [opt] id-expression
7440 postfix-expression -> pseudo-destructor-name
7441
7442 FOR_OFFSETOF is set if we're being called in that context. That sorta
7443 limits what of the above we'll actually accept, but nevermind.
7444 TOKEN_TYPE is the "." or "->" token, which will already have been
7445 removed from the stream. */
7446
7447 static tree
7448 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7449 enum cpp_ttype token_type,
7450 cp_expr postfix_expression,
7451 bool for_offsetof, cp_id_kind *idk,
7452 location_t location)
7453 {
7454 tree name;
7455 bool dependent_p;
7456 bool pseudo_destructor_p;
7457 tree scope = NULL_TREE;
7458 location_t start_loc = postfix_expression.get_start ();
7459
7460 /* If this is a `->' operator, dereference the pointer. */
7461 if (token_type == CPP_DEREF)
7462 postfix_expression = build_x_arrow (location, postfix_expression,
7463 tf_warning_or_error);
7464 /* Check to see whether or not the expression is type-dependent and
7465 not the current instantiation. */
7466 dependent_p = type_dependent_object_expression_p (postfix_expression);
7467 /* The identifier following the `->' or `.' is not qualified. */
7468 parser->scope = NULL_TREE;
7469 parser->qualifying_scope = NULL_TREE;
7470 parser->object_scope = NULL_TREE;
7471 *idk = CP_ID_KIND_NONE;
7472
7473 /* Enter the scope corresponding to the type of the object
7474 given by the POSTFIX_EXPRESSION. */
7475 if (!dependent_p)
7476 {
7477 scope = TREE_TYPE (postfix_expression);
7478 /* According to the standard, no expression should ever have
7479 reference type. Unfortunately, we do not currently match
7480 the standard in this respect in that our internal representation
7481 of an expression may have reference type even when the standard
7482 says it does not. Therefore, we have to manually obtain the
7483 underlying type here. */
7484 scope = non_reference (scope);
7485 /* The type of the POSTFIX_EXPRESSION must be complete. */
7486 /* Unlike the object expression in other contexts, *this is not
7487 required to be of complete type for purposes of class member
7488 access (5.2.5) outside the member function body. */
7489 if (postfix_expression != current_class_ref
7490 && scope != error_mark_node
7491 && !(processing_template_decl
7492 && current_class_type
7493 && (same_type_ignoring_top_level_qualifiers_p
7494 (scope, current_class_type))))
7495 {
7496 scope = complete_type (scope);
7497 if (!COMPLETE_TYPE_P (scope)
7498 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7499 &dependent_p))
7500 return error_mark_node;
7501 }
7502
7503 if (!dependent_p)
7504 {
7505 /* Let the name lookup machinery know that we are processing a
7506 class member access expression. */
7507 parser->context->object_type = scope;
7508 /* If something went wrong, we want to be able to discern that case,
7509 as opposed to the case where there was no SCOPE due to the type
7510 of expression being dependent. */
7511 if (!scope)
7512 scope = error_mark_node;
7513 /* If the SCOPE was erroneous, make the various semantic analysis
7514 functions exit quickly -- and without issuing additional error
7515 messages. */
7516 if (scope == error_mark_node)
7517 postfix_expression = error_mark_node;
7518 }
7519 }
7520
7521 if (dependent_p)
7522 /* Tell cp_parser_lookup_name that there was an object, even though it's
7523 type-dependent. */
7524 parser->context->object_type = unknown_type_node;
7525
7526 /* Assume this expression is not a pseudo-destructor access. */
7527 pseudo_destructor_p = false;
7528
7529 /* If the SCOPE is a scalar type, then, if this is a valid program,
7530 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7531 is type dependent, it can be pseudo-destructor-name or something else.
7532 Try to parse it as pseudo-destructor-name first. */
7533 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7534 {
7535 tree s;
7536 tree type;
7537
7538 cp_parser_parse_tentatively (parser);
7539 /* Parse the pseudo-destructor-name. */
7540 s = NULL_TREE;
7541 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7542 &s, &type);
7543 if (dependent_p
7544 && (cp_parser_error_occurred (parser)
7545 || !SCALAR_TYPE_P (type)))
7546 cp_parser_abort_tentative_parse (parser);
7547 else if (cp_parser_parse_definitely (parser))
7548 {
7549 pseudo_destructor_p = true;
7550 postfix_expression
7551 = finish_pseudo_destructor_expr (postfix_expression,
7552 s, type, location);
7553 }
7554 }
7555
7556 if (!pseudo_destructor_p)
7557 {
7558 /* If the SCOPE is not a scalar type, we are looking at an
7559 ordinary class member access expression, rather than a
7560 pseudo-destructor-name. */
7561 bool template_p;
7562 cp_token *token = cp_lexer_peek_token (parser->lexer);
7563 /* Parse the id-expression. */
7564 name = (cp_parser_id_expression
7565 (parser,
7566 cp_parser_optional_template_keyword (parser),
7567 /*check_dependency_p=*/true,
7568 &template_p,
7569 /*declarator_p=*/false,
7570 /*optional_p=*/false));
7571 /* In general, build a SCOPE_REF if the member name is qualified.
7572 However, if the name was not dependent and has already been
7573 resolved; there is no need to build the SCOPE_REF. For example;
7574
7575 struct X { void f(); };
7576 template <typename T> void f(T* t) { t->X::f(); }
7577
7578 Even though "t" is dependent, "X::f" is not and has been resolved
7579 to a BASELINK; there is no need to include scope information. */
7580
7581 /* But we do need to remember that there was an explicit scope for
7582 virtual function calls. */
7583 if (parser->scope)
7584 *idk = CP_ID_KIND_QUALIFIED;
7585
7586 /* If the name is a template-id that names a type, we will get a
7587 TYPE_DECL here. That is invalid code. */
7588 if (TREE_CODE (name) == TYPE_DECL)
7589 {
7590 error_at (token->location, "invalid use of %qD", name);
7591 postfix_expression = error_mark_node;
7592 }
7593 else
7594 {
7595 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7596 {
7597 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7598 {
7599 error_at (token->location, "%<%D::%D%> is not a class member",
7600 parser->scope, name);
7601 postfix_expression = error_mark_node;
7602 }
7603 else
7604 name = build_qualified_name (/*type=*/NULL_TREE,
7605 parser->scope,
7606 name,
7607 template_p);
7608 parser->scope = NULL_TREE;
7609 parser->qualifying_scope = NULL_TREE;
7610 parser->object_scope = NULL_TREE;
7611 }
7612 if (parser->scope && name && BASELINK_P (name))
7613 adjust_result_of_qualified_name_lookup
7614 (name, parser->scope, scope);
7615 postfix_expression
7616 = finish_class_member_access_expr (postfix_expression, name,
7617 template_p,
7618 tf_warning_or_error);
7619 /* Build a location e.g.:
7620 ptr->access_expr
7621 ~~~^~~~~~~~~~~~~
7622 where the caret is at the deref token, ranging from
7623 the start of postfix_expression to the end of the access expr. */
7624 location_t end_loc
7625 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7626 location_t combined_loc
7627 = make_location (input_location, start_loc, end_loc);
7628 protected_set_expr_location (postfix_expression, combined_loc);
7629 }
7630 }
7631
7632 /* We no longer need to look up names in the scope of the object on
7633 the left-hand side of the `.' or `->' operator. */
7634 parser->context->object_type = NULL_TREE;
7635
7636 /* Outside of offsetof, these operators may not appear in
7637 constant-expressions. */
7638 if (!for_offsetof
7639 && (cp_parser_non_integral_constant_expression
7640 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7641 postfix_expression = error_mark_node;
7642
7643 return postfix_expression;
7644 }
7645
7646 /* Parse a parenthesized expression-list.
7647
7648 expression-list:
7649 assignment-expression
7650 expression-list, assignment-expression
7651
7652 attribute-list:
7653 expression-list
7654 identifier
7655 identifier, expression-list
7656
7657 CAST_P is true if this expression is the target of a cast.
7658
7659 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7660 argument pack.
7661
7662 WRAP_LOCATIONS_P is true if expressions within this list for which
7663 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7664 their source locations.
7665
7666 Returns a vector of trees. Each element is a representation of an
7667 assignment-expression. NULL is returned if the ( and or ) are
7668 missing. An empty, but allocated, vector is returned on no
7669 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7670 if we are parsing an attribute list for an attribute that wants a
7671 plain identifier argument, normal_attr for an attribute that wants
7672 an expression, or non_attr if we aren't parsing an attribute list. If
7673 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7674 not all of the expressions in the list were constant.
7675 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7676 will be written to with the location of the closing parenthesis. If
7677 an error occurs, it may or may not be written to. */
7678
7679 static vec<tree, va_gc> *
7680 cp_parser_parenthesized_expression_list (cp_parser* parser,
7681 int is_attribute_list,
7682 bool cast_p,
7683 bool allow_expansion_p,
7684 bool *non_constant_p,
7685 location_t *close_paren_loc,
7686 bool wrap_locations_p)
7687 {
7688 vec<tree, va_gc> *expression_list;
7689 bool fold_expr_p = is_attribute_list != non_attr;
7690 tree identifier = NULL_TREE;
7691 bool saved_greater_than_is_operator_p;
7692
7693 /* Assume all the expressions will be constant. */
7694 if (non_constant_p)
7695 *non_constant_p = false;
7696
7697 matching_parens parens;
7698 if (!parens.require_open (parser))
7699 return NULL;
7700
7701 expression_list = make_tree_vector ();
7702
7703 /* Within a parenthesized expression, a `>' token is always
7704 the greater-than operator. */
7705 saved_greater_than_is_operator_p
7706 = parser->greater_than_is_operator_p;
7707 parser->greater_than_is_operator_p = true;
7708
7709 cp_expr expr (NULL_TREE);
7710
7711 /* Consume expressions until there are no more. */
7712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7713 while (true)
7714 {
7715 /* At the beginning of attribute lists, check to see if the
7716 next token is an identifier. */
7717 if (is_attribute_list == id_attr
7718 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7719 {
7720 cp_token *token;
7721
7722 /* Consume the identifier. */
7723 token = cp_lexer_consume_token (parser->lexer);
7724 /* Save the identifier. */
7725 identifier = token->u.value;
7726 }
7727 else
7728 {
7729 bool expr_non_constant_p;
7730
7731 /* Parse the next assignment-expression. */
7732 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7733 {
7734 /* A braced-init-list. */
7735 cp_lexer_set_source_position (parser->lexer);
7736 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7737 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7738 if (non_constant_p && expr_non_constant_p)
7739 *non_constant_p = true;
7740 }
7741 else if (non_constant_p)
7742 {
7743 expr = (cp_parser_constant_expression
7744 (parser, /*allow_non_constant_p=*/true,
7745 &expr_non_constant_p));
7746 if (expr_non_constant_p)
7747 *non_constant_p = true;
7748 }
7749 else
7750 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7751 cast_p);
7752
7753 if (fold_expr_p)
7754 expr = instantiate_non_dependent_expr (expr);
7755
7756 /* If we have an ellipsis, then this is an expression
7757 expansion. */
7758 if (allow_expansion_p
7759 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7760 {
7761 /* Consume the `...'. */
7762 cp_lexer_consume_token (parser->lexer);
7763
7764 /* Build the argument pack. */
7765 expr = make_pack_expansion (expr);
7766 }
7767
7768 if (wrap_locations_p)
7769 expr.maybe_add_location_wrapper ();
7770
7771 /* Add it to the list. We add error_mark_node
7772 expressions to the list, so that we can still tell if
7773 the correct form for a parenthesized expression-list
7774 is found. That gives better errors. */
7775 vec_safe_push (expression_list, expr.get_value ());
7776
7777 if (expr == error_mark_node)
7778 goto skip_comma;
7779 }
7780
7781 /* After the first item, attribute lists look the same as
7782 expression lists. */
7783 is_attribute_list = non_attr;
7784
7785 get_comma:;
7786 /* If the next token isn't a `,', then we are done. */
7787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7788 break;
7789
7790 /* Otherwise, consume the `,' and keep going. */
7791 cp_lexer_consume_token (parser->lexer);
7792 }
7793
7794 if (close_paren_loc)
7795 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7796
7797 if (!parens.require_close (parser))
7798 {
7799 int ending;
7800
7801 skip_comma:;
7802 /* We try and resync to an unnested comma, as that will give the
7803 user better diagnostics. */
7804 ending = cp_parser_skip_to_closing_parenthesis (parser,
7805 /*recovering=*/true,
7806 /*or_comma=*/true,
7807 /*consume_paren=*/true);
7808 if (ending < 0)
7809 goto get_comma;
7810 if (!ending)
7811 {
7812 parser->greater_than_is_operator_p
7813 = saved_greater_than_is_operator_p;
7814 return NULL;
7815 }
7816 }
7817
7818 parser->greater_than_is_operator_p
7819 = saved_greater_than_is_operator_p;
7820
7821 if (identifier)
7822 vec_safe_insert (expression_list, 0, identifier);
7823
7824 return expression_list;
7825 }
7826
7827 /* Parse a pseudo-destructor-name.
7828
7829 pseudo-destructor-name:
7830 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7831 :: [opt] nested-name-specifier template template-id :: ~ type-name
7832 :: [opt] nested-name-specifier [opt] ~ type-name
7833
7834 If either of the first two productions is used, sets *SCOPE to the
7835 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7836 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7837 or ERROR_MARK_NODE if the parse fails. */
7838
7839 static void
7840 cp_parser_pseudo_destructor_name (cp_parser* parser,
7841 tree object,
7842 tree* scope,
7843 tree* type)
7844 {
7845 bool nested_name_specifier_p;
7846
7847 /* Handle ~auto. */
7848 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7849 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7850 && !type_dependent_expression_p (object))
7851 {
7852 if (cxx_dialect < cxx14)
7853 pedwarn (input_location, 0,
7854 "%<~auto%> only available with "
7855 "-std=c++14 or -std=gnu++14");
7856 cp_lexer_consume_token (parser->lexer);
7857 cp_lexer_consume_token (parser->lexer);
7858 *scope = NULL_TREE;
7859 *type = TREE_TYPE (object);
7860 return;
7861 }
7862
7863 /* Assume that things will not work out. */
7864 *type = error_mark_node;
7865
7866 /* Look for the optional `::' operator. */
7867 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7868 /* Look for the optional nested-name-specifier. */
7869 nested_name_specifier_p
7870 = (cp_parser_nested_name_specifier_opt (parser,
7871 /*typename_keyword_p=*/false,
7872 /*check_dependency_p=*/true,
7873 /*type_p=*/false,
7874 /*is_declaration=*/false)
7875 != NULL_TREE);
7876 /* Now, if we saw a nested-name-specifier, we might be doing the
7877 second production. */
7878 if (nested_name_specifier_p
7879 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7880 {
7881 /* Consume the `template' keyword. */
7882 cp_lexer_consume_token (parser->lexer);
7883 /* Parse the template-id. */
7884 cp_parser_template_id (parser,
7885 /*template_keyword_p=*/true,
7886 /*check_dependency_p=*/false,
7887 class_type,
7888 /*is_declaration=*/true);
7889 /* Look for the `::' token. */
7890 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7891 }
7892 /* If the next token is not a `~', then there might be some
7893 additional qualification. */
7894 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7895 {
7896 /* At this point, we're looking for "type-name :: ~". The type-name
7897 must not be a class-name, since this is a pseudo-destructor. So,
7898 it must be either an enum-name, or a typedef-name -- both of which
7899 are just identifiers. So, we peek ahead to check that the "::"
7900 and "~" tokens are present; if they are not, then we can avoid
7901 calling type_name. */
7902 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7903 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7904 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7905 {
7906 cp_parser_error (parser, "non-scalar type");
7907 return;
7908 }
7909
7910 /* Look for the type-name. */
7911 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7912 if (*scope == error_mark_node)
7913 return;
7914
7915 /* Look for the `::' token. */
7916 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7917 }
7918 else
7919 *scope = NULL_TREE;
7920
7921 /* Look for the `~'. */
7922 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7923
7924 /* Once we see the ~, this has to be a pseudo-destructor. */
7925 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7926 cp_parser_commit_to_topmost_tentative_parse (parser);
7927
7928 /* Look for the type-name again. We are not responsible for
7929 checking that it matches the first type-name. */
7930 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7931 }
7932
7933 /* Parse a unary-expression.
7934
7935 unary-expression:
7936 postfix-expression
7937 ++ cast-expression
7938 -- cast-expression
7939 unary-operator cast-expression
7940 sizeof unary-expression
7941 sizeof ( type-id )
7942 alignof ( type-id ) [C++0x]
7943 new-expression
7944 delete-expression
7945
7946 GNU Extensions:
7947
7948 unary-expression:
7949 __extension__ cast-expression
7950 __alignof__ unary-expression
7951 __alignof__ ( type-id )
7952 alignof unary-expression [C++0x]
7953 __real__ cast-expression
7954 __imag__ cast-expression
7955 && identifier
7956 sizeof ( type-id ) { initializer-list , [opt] }
7957 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7958 __alignof__ ( type-id ) { initializer-list , [opt] }
7959
7960 ADDRESS_P is true iff the unary-expression is appearing as the
7961 operand of the `&' operator. CAST_P is true if this expression is
7962 the target of a cast.
7963
7964 Returns a representation of the expression. */
7965
7966 static cp_expr
7967 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7968 bool address_p, bool cast_p, bool decltype_p)
7969 {
7970 cp_token *token;
7971 enum tree_code unary_operator;
7972
7973 /* Peek at the next token. */
7974 token = cp_lexer_peek_token (parser->lexer);
7975 /* Some keywords give away the kind of expression. */
7976 if (token->type == CPP_KEYWORD)
7977 {
7978 enum rid keyword = token->keyword;
7979
7980 switch (keyword)
7981 {
7982 case RID_ALIGNOF:
7983 case RID_SIZEOF:
7984 {
7985 tree operand, ret;
7986 enum tree_code op;
7987 location_t start_loc = token->location;
7988
7989 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7990 bool std_alignof = id_equal (token->u.value, "alignof");
7991
7992 /* Consume the token. */
7993 cp_lexer_consume_token (parser->lexer);
7994 /* Parse the operand. */
7995 operand = cp_parser_sizeof_operand (parser, keyword);
7996
7997 if (TYPE_P (operand))
7998 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
7999 true);
8000 else
8001 {
8002 /* ISO C++ defines alignof only with types, not with
8003 expressions. So pedwarn if alignof is used with a non-
8004 type expression. However, __alignof__ is ok. */
8005 if (std_alignof)
8006 pedwarn (token->location, OPT_Wpedantic,
8007 "ISO C++ does not allow %<alignof%> "
8008 "with a non-type");
8009
8010 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8011 }
8012 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8013 SIZEOF_EXPR with the original operand. */
8014 if (op == SIZEOF_EXPR && ret != error_mark_node)
8015 {
8016 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8017 {
8018 if (!processing_template_decl && TYPE_P (operand))
8019 {
8020 ret = build_min (SIZEOF_EXPR, size_type_node,
8021 build1 (NOP_EXPR, operand,
8022 error_mark_node));
8023 SIZEOF_EXPR_TYPE_P (ret) = 1;
8024 }
8025 else
8026 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8027 TREE_SIDE_EFFECTS (ret) = 0;
8028 TREE_READONLY (ret) = 1;
8029 }
8030 }
8031
8032 /* Construct a location e.g. :
8033 alignof (expr)
8034 ^~~~~~~~~~~~~~
8035 with start == caret at the start of the "alignof"/"sizeof"
8036 token, with the endpoint at the final closing paren. */
8037 location_t finish_loc
8038 = cp_lexer_previous_token (parser->lexer)->location;
8039 location_t compound_loc
8040 = make_location (start_loc, start_loc, finish_loc);
8041
8042 cp_expr ret_expr (ret);
8043 ret_expr.set_location (compound_loc);
8044 ret_expr = ret_expr.maybe_add_location_wrapper ();
8045 return ret_expr;
8046 }
8047
8048 case RID_NEW:
8049 return cp_parser_new_expression (parser);
8050
8051 case RID_DELETE:
8052 return cp_parser_delete_expression (parser);
8053
8054 case RID_EXTENSION:
8055 {
8056 /* The saved value of the PEDANTIC flag. */
8057 int saved_pedantic;
8058 tree expr;
8059
8060 /* Save away the PEDANTIC flag. */
8061 cp_parser_extension_opt (parser, &saved_pedantic);
8062 /* Parse the cast-expression. */
8063 expr = cp_parser_simple_cast_expression (parser);
8064 /* Restore the PEDANTIC flag. */
8065 pedantic = saved_pedantic;
8066
8067 return expr;
8068 }
8069
8070 case RID_REALPART:
8071 case RID_IMAGPART:
8072 {
8073 tree expression;
8074
8075 /* Consume the `__real__' or `__imag__' token. */
8076 cp_lexer_consume_token (parser->lexer);
8077 /* Parse the cast-expression. */
8078 expression = cp_parser_simple_cast_expression (parser);
8079 /* Create the complete representation. */
8080 return build_x_unary_op (token->location,
8081 (keyword == RID_REALPART
8082 ? REALPART_EXPR : IMAGPART_EXPR),
8083 expression,
8084 tf_warning_or_error);
8085 }
8086 break;
8087
8088 case RID_TRANSACTION_ATOMIC:
8089 case RID_TRANSACTION_RELAXED:
8090 return cp_parser_transaction_expression (parser, keyword);
8091
8092 case RID_NOEXCEPT:
8093 {
8094 tree expr;
8095 const char *saved_message;
8096 bool saved_integral_constant_expression_p;
8097 bool saved_non_integral_constant_expression_p;
8098 bool saved_greater_than_is_operator_p;
8099
8100 location_t start_loc = token->location;
8101
8102 cp_lexer_consume_token (parser->lexer);
8103 matching_parens parens;
8104 parens.require_open (parser);
8105
8106 saved_message = parser->type_definition_forbidden_message;
8107 parser->type_definition_forbidden_message
8108 = G_("types may not be defined in %<noexcept%> expressions");
8109
8110 saved_integral_constant_expression_p
8111 = parser->integral_constant_expression_p;
8112 saved_non_integral_constant_expression_p
8113 = parser->non_integral_constant_expression_p;
8114 parser->integral_constant_expression_p = false;
8115
8116 saved_greater_than_is_operator_p
8117 = parser->greater_than_is_operator_p;
8118 parser->greater_than_is_operator_p = true;
8119
8120 ++cp_unevaluated_operand;
8121 ++c_inhibit_evaluation_warnings;
8122 ++cp_noexcept_operand;
8123 expr = cp_parser_expression (parser);
8124 --cp_noexcept_operand;
8125 --c_inhibit_evaluation_warnings;
8126 --cp_unevaluated_operand;
8127
8128 parser->greater_than_is_operator_p
8129 = saved_greater_than_is_operator_p;
8130
8131 parser->integral_constant_expression_p
8132 = saved_integral_constant_expression_p;
8133 parser->non_integral_constant_expression_p
8134 = saved_non_integral_constant_expression_p;
8135
8136 parser->type_definition_forbidden_message = saved_message;
8137
8138 location_t finish_loc
8139 = cp_lexer_peek_token (parser->lexer)->location;
8140 parens.require_close (parser);
8141
8142 /* Construct a location of the form:
8143 noexcept (expr)
8144 ^~~~~~~~~~~~~~~
8145 with start == caret, finishing at the close-paren. */
8146 location_t noexcept_loc
8147 = make_location (start_loc, start_loc, finish_loc);
8148
8149 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8150 noexcept_loc);
8151 }
8152
8153 default:
8154 break;
8155 }
8156 }
8157
8158 /* Look for the `:: new' and `:: delete', which also signal the
8159 beginning of a new-expression, or delete-expression,
8160 respectively. If the next token is `::', then it might be one of
8161 these. */
8162 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8163 {
8164 enum rid keyword;
8165
8166 /* See if the token after the `::' is one of the keywords in
8167 which we're interested. */
8168 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8169 /* If it's `new', we have a new-expression. */
8170 if (keyword == RID_NEW)
8171 return cp_parser_new_expression (parser);
8172 /* Similarly, for `delete'. */
8173 else if (keyword == RID_DELETE)
8174 return cp_parser_delete_expression (parser);
8175 }
8176
8177 /* Look for a unary operator. */
8178 unary_operator = cp_parser_unary_operator (token);
8179 /* The `++' and `--' operators can be handled similarly, even though
8180 they are not technically unary-operators in the grammar. */
8181 if (unary_operator == ERROR_MARK)
8182 {
8183 if (token->type == CPP_PLUS_PLUS)
8184 unary_operator = PREINCREMENT_EXPR;
8185 else if (token->type == CPP_MINUS_MINUS)
8186 unary_operator = PREDECREMENT_EXPR;
8187 /* Handle the GNU address-of-label extension. */
8188 else if (cp_parser_allow_gnu_extensions_p (parser)
8189 && token->type == CPP_AND_AND)
8190 {
8191 tree identifier;
8192 tree expression;
8193 location_t start_loc = token->location;
8194
8195 /* Consume the '&&' token. */
8196 cp_lexer_consume_token (parser->lexer);
8197 /* Look for the identifier. */
8198 location_t finish_loc
8199 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8200 identifier = cp_parser_identifier (parser);
8201 /* Construct a location of the form:
8202 &&label
8203 ^~~~~~~
8204 with caret==start at the "&&", finish at the end of the label. */
8205 location_t combined_loc
8206 = make_location (start_loc, start_loc, finish_loc);
8207 /* Create an expression representing the address. */
8208 expression = finish_label_address_expr (identifier, combined_loc);
8209 if (cp_parser_non_integral_constant_expression (parser,
8210 NIC_ADDR_LABEL))
8211 expression = error_mark_node;
8212 return expression;
8213 }
8214 }
8215 if (unary_operator != ERROR_MARK)
8216 {
8217 cp_expr cast_expression;
8218 cp_expr expression = error_mark_node;
8219 non_integral_constant non_constant_p = NIC_NONE;
8220 location_t loc = token->location;
8221 tsubst_flags_t complain = complain_flags (decltype_p);
8222
8223 /* Consume the operator token. */
8224 token = cp_lexer_consume_token (parser->lexer);
8225 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8226
8227 /* Parse the cast-expression. */
8228 cast_expression
8229 = cp_parser_cast_expression (parser,
8230 unary_operator == ADDR_EXPR,
8231 /*cast_p=*/false,
8232 /*decltype*/false,
8233 pidk);
8234
8235 /* Make a location:
8236 OP_TOKEN CAST_EXPRESSION
8237 ^~~~~~~~~~~~~~~~~~~~~~~~~
8238 with start==caret at the operator token, and
8239 extending to the end of the cast_expression. */
8240 loc = make_location (loc, loc, cast_expression.get_finish ());
8241
8242 /* Now, build an appropriate representation. */
8243 switch (unary_operator)
8244 {
8245 case INDIRECT_REF:
8246 non_constant_p = NIC_STAR;
8247 expression = build_x_indirect_ref (loc, cast_expression,
8248 RO_UNARY_STAR,
8249 complain);
8250 /* TODO: build_x_indirect_ref does not always honor the
8251 location, so ensure it is set. */
8252 expression.set_location (loc);
8253 break;
8254
8255 case ADDR_EXPR:
8256 non_constant_p = NIC_ADDR;
8257 /* Fall through. */
8258 case BIT_NOT_EXPR:
8259 expression = build_x_unary_op (loc, unary_operator,
8260 cast_expression,
8261 complain);
8262 /* TODO: build_x_unary_op does not always honor the location,
8263 so ensure it is set. */
8264 expression.set_location (loc);
8265 break;
8266
8267 case PREINCREMENT_EXPR:
8268 case PREDECREMENT_EXPR:
8269 non_constant_p = unary_operator == PREINCREMENT_EXPR
8270 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8271 /* Fall through. */
8272 case NEGATE_EXPR:
8273 /* Immediately fold negation of a constant, unless the constant is 0
8274 (since -0 == 0) or it would overflow. */
8275 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8276 && CONSTANT_CLASS_P (cast_expression)
8277 && !integer_zerop (cast_expression)
8278 && !TREE_OVERFLOW (cast_expression))
8279 {
8280 tree folded = fold_build1 (unary_operator,
8281 TREE_TYPE (cast_expression),
8282 cast_expression);
8283 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8284 {
8285 expression = cp_expr (folded, loc);
8286 break;
8287 }
8288 }
8289 /* Fall through. */
8290 case UNARY_PLUS_EXPR:
8291 case TRUTH_NOT_EXPR:
8292 expression = finish_unary_op_expr (loc, unary_operator,
8293 cast_expression, complain);
8294 break;
8295
8296 default:
8297 gcc_unreachable ();
8298 }
8299
8300 if (non_constant_p != NIC_NONE
8301 && cp_parser_non_integral_constant_expression (parser,
8302 non_constant_p))
8303 expression = error_mark_node;
8304
8305 return expression;
8306 }
8307
8308 return cp_parser_postfix_expression (parser, address_p, cast_p,
8309 /*member_access_only_p=*/false,
8310 decltype_p,
8311 pidk);
8312 }
8313
8314 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8315 unary-operator, the corresponding tree code is returned. */
8316
8317 static enum tree_code
8318 cp_parser_unary_operator (cp_token* token)
8319 {
8320 switch (token->type)
8321 {
8322 case CPP_MULT:
8323 return INDIRECT_REF;
8324
8325 case CPP_AND:
8326 return ADDR_EXPR;
8327
8328 case CPP_PLUS:
8329 return UNARY_PLUS_EXPR;
8330
8331 case CPP_MINUS:
8332 return NEGATE_EXPR;
8333
8334 case CPP_NOT:
8335 return TRUTH_NOT_EXPR;
8336
8337 case CPP_COMPL:
8338 return BIT_NOT_EXPR;
8339
8340 default:
8341 return ERROR_MARK;
8342 }
8343 }
8344
8345 /* Parse a new-expression.
8346
8347 new-expression:
8348 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8349 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8350
8351 Returns a representation of the expression. */
8352
8353 static tree
8354 cp_parser_new_expression (cp_parser* parser)
8355 {
8356 bool global_scope_p;
8357 vec<tree, va_gc> *placement;
8358 tree type;
8359 vec<tree, va_gc> *initializer;
8360 tree nelts = NULL_TREE;
8361 tree ret;
8362
8363 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8364
8365 /* Look for the optional `::' operator. */
8366 global_scope_p
8367 = (cp_parser_global_scope_opt (parser,
8368 /*current_scope_valid_p=*/false)
8369 != NULL_TREE);
8370 /* Look for the `new' operator. */
8371 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8372 /* There's no easy way to tell a new-placement from the
8373 `( type-id )' construct. */
8374 cp_parser_parse_tentatively (parser);
8375 /* Look for a new-placement. */
8376 placement = cp_parser_new_placement (parser);
8377 /* If that didn't work out, there's no new-placement. */
8378 if (!cp_parser_parse_definitely (parser))
8379 {
8380 if (placement != NULL)
8381 release_tree_vector (placement);
8382 placement = NULL;
8383 }
8384
8385 /* If the next token is a `(', then we have a parenthesized
8386 type-id. */
8387 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8388 {
8389 cp_token *token;
8390 const char *saved_message = parser->type_definition_forbidden_message;
8391
8392 /* Consume the `('. */
8393 matching_parens parens;
8394 parens.consume_open (parser);
8395
8396 /* Parse the type-id. */
8397 parser->type_definition_forbidden_message
8398 = G_("types may not be defined in a new-expression");
8399 {
8400 type_id_in_expr_sentinel s (parser);
8401 type = cp_parser_type_id (parser);
8402 }
8403 parser->type_definition_forbidden_message = saved_message;
8404
8405 /* Look for the closing `)'. */
8406 parens.require_close (parser);
8407 token = cp_lexer_peek_token (parser->lexer);
8408 /* There should not be a direct-new-declarator in this production,
8409 but GCC used to allowed this, so we check and emit a sensible error
8410 message for this case. */
8411 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8412 {
8413 error_at (token->location,
8414 "array bound forbidden after parenthesized type-id");
8415 inform (token->location,
8416 "try removing the parentheses around the type-id");
8417 cp_parser_direct_new_declarator (parser);
8418 }
8419 }
8420 /* Otherwise, there must be a new-type-id. */
8421 else
8422 type = cp_parser_new_type_id (parser, &nelts);
8423
8424 /* If the next token is a `(' or '{', then we have a new-initializer. */
8425 cp_token *token = cp_lexer_peek_token (parser->lexer);
8426 if (token->type == CPP_OPEN_PAREN
8427 || token->type == CPP_OPEN_BRACE)
8428 initializer = cp_parser_new_initializer (parser);
8429 else
8430 initializer = NULL;
8431
8432 /* A new-expression may not appear in an integral constant
8433 expression. */
8434 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8435 ret = error_mark_node;
8436 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8437 of a new-type-id or type-id of a new-expression, the new-expression shall
8438 contain a new-initializer of the form ( assignment-expression )".
8439 Additionally, consistently with the spirit of DR 1467, we want to accept
8440 'new auto { 2 }' too. */
8441 else if ((ret = type_uses_auto (type))
8442 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8443 && (vec_safe_length (initializer) != 1
8444 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8445 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8446 {
8447 error_at (token->location,
8448 "initialization of new-expression for type %<auto%> "
8449 "requires exactly one element");
8450 ret = error_mark_node;
8451 }
8452 else
8453 {
8454 /* Construct a location e.g.:
8455 ptr = new int[100]
8456 ^~~~~~~~~~~~
8457 with caret == start at the start of the "new" token, and the end
8458 at the end of the final token we consumed. */
8459 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8460 location_t end_loc = get_finish (end_tok->location);
8461 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8462
8463 /* Create a representation of the new-expression. */
8464 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8465 tf_warning_or_error);
8466 protected_set_expr_location (ret, combined_loc);
8467 }
8468
8469 if (placement != NULL)
8470 release_tree_vector (placement);
8471 if (initializer != NULL)
8472 release_tree_vector (initializer);
8473
8474 return ret;
8475 }
8476
8477 /* Parse a new-placement.
8478
8479 new-placement:
8480 ( expression-list )
8481
8482 Returns the same representation as for an expression-list. */
8483
8484 static vec<tree, va_gc> *
8485 cp_parser_new_placement (cp_parser* parser)
8486 {
8487 vec<tree, va_gc> *expression_list;
8488
8489 /* Parse the expression-list. */
8490 expression_list = (cp_parser_parenthesized_expression_list
8491 (parser, non_attr, /*cast_p=*/false,
8492 /*allow_expansion_p=*/true,
8493 /*non_constant_p=*/NULL));
8494
8495 if (expression_list && expression_list->is_empty ())
8496 error ("expected expression-list or type-id");
8497
8498 return expression_list;
8499 }
8500
8501 /* Parse a new-type-id.
8502
8503 new-type-id:
8504 type-specifier-seq new-declarator [opt]
8505
8506 Returns the TYPE allocated. If the new-type-id indicates an array
8507 type, *NELTS is set to the number of elements in the last array
8508 bound; the TYPE will not include the last array bound. */
8509
8510 static tree
8511 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8512 {
8513 cp_decl_specifier_seq type_specifier_seq;
8514 cp_declarator *new_declarator;
8515 cp_declarator *declarator;
8516 cp_declarator *outer_declarator;
8517 const char *saved_message;
8518
8519 /* The type-specifier sequence must not contain type definitions.
8520 (It cannot contain declarations of new types either, but if they
8521 are not definitions we will catch that because they are not
8522 complete.) */
8523 saved_message = parser->type_definition_forbidden_message;
8524 parser->type_definition_forbidden_message
8525 = G_("types may not be defined in a new-type-id");
8526 /* Parse the type-specifier-seq. */
8527 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8528 /*is_trailing_return=*/false,
8529 &type_specifier_seq);
8530 /* Restore the old message. */
8531 parser->type_definition_forbidden_message = saved_message;
8532
8533 if (type_specifier_seq.type == error_mark_node)
8534 return error_mark_node;
8535
8536 /* Parse the new-declarator. */
8537 new_declarator = cp_parser_new_declarator_opt (parser);
8538
8539 /* Determine the number of elements in the last array dimension, if
8540 any. */
8541 *nelts = NULL_TREE;
8542 /* Skip down to the last array dimension. */
8543 declarator = new_declarator;
8544 outer_declarator = NULL;
8545 while (declarator && (declarator->kind == cdk_pointer
8546 || declarator->kind == cdk_ptrmem))
8547 {
8548 outer_declarator = declarator;
8549 declarator = declarator->declarator;
8550 }
8551 while (declarator
8552 && declarator->kind == cdk_array
8553 && declarator->declarator
8554 && declarator->declarator->kind == cdk_array)
8555 {
8556 outer_declarator = declarator;
8557 declarator = declarator->declarator;
8558 }
8559
8560 if (declarator && declarator->kind == cdk_array)
8561 {
8562 *nelts = declarator->u.array.bounds;
8563 if (*nelts == error_mark_node)
8564 *nelts = integer_one_node;
8565
8566 if (outer_declarator)
8567 outer_declarator->declarator = declarator->declarator;
8568 else
8569 new_declarator = NULL;
8570 }
8571
8572 return groktypename (&type_specifier_seq, new_declarator, false);
8573 }
8574
8575 /* Parse an (optional) new-declarator.
8576
8577 new-declarator:
8578 ptr-operator new-declarator [opt]
8579 direct-new-declarator
8580
8581 Returns the declarator. */
8582
8583 static cp_declarator *
8584 cp_parser_new_declarator_opt (cp_parser* parser)
8585 {
8586 enum tree_code code;
8587 tree type, std_attributes = NULL_TREE;
8588 cp_cv_quals cv_quals;
8589
8590 /* We don't know if there's a ptr-operator next, or not. */
8591 cp_parser_parse_tentatively (parser);
8592 /* Look for a ptr-operator. */
8593 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8594 /* If that worked, look for more new-declarators. */
8595 if (cp_parser_parse_definitely (parser))
8596 {
8597 cp_declarator *declarator;
8598
8599 /* Parse another optional declarator. */
8600 declarator = cp_parser_new_declarator_opt (parser);
8601
8602 declarator = cp_parser_make_indirect_declarator
8603 (code, type, cv_quals, declarator, std_attributes);
8604
8605 return declarator;
8606 }
8607
8608 /* If the next token is a `[', there is a direct-new-declarator. */
8609 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8610 return cp_parser_direct_new_declarator (parser);
8611
8612 return NULL;
8613 }
8614
8615 /* Parse a direct-new-declarator.
8616
8617 direct-new-declarator:
8618 [ expression ]
8619 direct-new-declarator [constant-expression]
8620
8621 */
8622
8623 static cp_declarator *
8624 cp_parser_direct_new_declarator (cp_parser* parser)
8625 {
8626 cp_declarator *declarator = NULL;
8627
8628 while (true)
8629 {
8630 tree expression;
8631 cp_token *token;
8632
8633 /* Look for the opening `['. */
8634 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8635
8636 token = cp_lexer_peek_token (parser->lexer);
8637 expression = cp_parser_expression (parser);
8638 /* The standard requires that the expression have integral
8639 type. DR 74 adds enumeration types. We believe that the
8640 real intent is that these expressions be handled like the
8641 expression in a `switch' condition, which also allows
8642 classes with a single conversion to integral or
8643 enumeration type. */
8644 if (!processing_template_decl)
8645 {
8646 expression
8647 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8648 expression,
8649 /*complain=*/true);
8650 if (!expression)
8651 {
8652 error_at (token->location,
8653 "expression in new-declarator must have integral "
8654 "or enumeration type");
8655 expression = error_mark_node;
8656 }
8657 }
8658
8659 /* Look for the closing `]'. */
8660 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8661
8662 /* Add this bound to the declarator. */
8663 declarator = make_array_declarator (declarator, expression);
8664
8665 /* If the next token is not a `[', then there are no more
8666 bounds. */
8667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8668 break;
8669 }
8670
8671 return declarator;
8672 }
8673
8674 /* Parse a new-initializer.
8675
8676 new-initializer:
8677 ( expression-list [opt] )
8678 braced-init-list
8679
8680 Returns a representation of the expression-list. */
8681
8682 static vec<tree, va_gc> *
8683 cp_parser_new_initializer (cp_parser* parser)
8684 {
8685 vec<tree, va_gc> *expression_list;
8686
8687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8688 {
8689 tree t;
8690 bool expr_non_constant_p;
8691 cp_lexer_set_source_position (parser->lexer);
8692 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8693 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8694 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8695 expression_list = make_tree_vector_single (t);
8696 }
8697 else
8698 expression_list = (cp_parser_parenthesized_expression_list
8699 (parser, non_attr, /*cast_p=*/false,
8700 /*allow_expansion_p=*/true,
8701 /*non_constant_p=*/NULL));
8702
8703 return expression_list;
8704 }
8705
8706 /* Parse a delete-expression.
8707
8708 delete-expression:
8709 :: [opt] delete cast-expression
8710 :: [opt] delete [ ] cast-expression
8711
8712 Returns a representation of the expression. */
8713
8714 static tree
8715 cp_parser_delete_expression (cp_parser* parser)
8716 {
8717 bool global_scope_p;
8718 bool array_p;
8719 tree expression;
8720
8721 /* Look for the optional `::' operator. */
8722 global_scope_p
8723 = (cp_parser_global_scope_opt (parser,
8724 /*current_scope_valid_p=*/false)
8725 != NULL_TREE);
8726 /* Look for the `delete' keyword. */
8727 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8728 /* See if the array syntax is in use. */
8729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8730 {
8731 /* Consume the `[' token. */
8732 cp_lexer_consume_token (parser->lexer);
8733 /* Look for the `]' token. */
8734 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8735 /* Remember that this is the `[]' construct. */
8736 array_p = true;
8737 }
8738 else
8739 array_p = false;
8740
8741 /* Parse the cast-expression. */
8742 expression = cp_parser_simple_cast_expression (parser);
8743
8744 /* A delete-expression may not appear in an integral constant
8745 expression. */
8746 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8747 return error_mark_node;
8748
8749 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8750 tf_warning_or_error);
8751 }
8752
8753 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8754 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8755 0 otherwise. */
8756
8757 static int
8758 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8759 {
8760 cp_token *token = cp_lexer_peek_token (parser->lexer);
8761 switch (token->type)
8762 {
8763 case CPP_COMMA:
8764 case CPP_SEMICOLON:
8765 case CPP_QUERY:
8766 case CPP_COLON:
8767 case CPP_CLOSE_SQUARE:
8768 case CPP_CLOSE_PAREN:
8769 case CPP_CLOSE_BRACE:
8770 case CPP_OPEN_BRACE:
8771 case CPP_DOT:
8772 case CPP_DOT_STAR:
8773 case CPP_DEREF:
8774 case CPP_DEREF_STAR:
8775 case CPP_DIV:
8776 case CPP_MOD:
8777 case CPP_LSHIFT:
8778 case CPP_RSHIFT:
8779 case CPP_LESS:
8780 case CPP_GREATER:
8781 case CPP_LESS_EQ:
8782 case CPP_GREATER_EQ:
8783 case CPP_EQ_EQ:
8784 case CPP_NOT_EQ:
8785 case CPP_EQ:
8786 case CPP_MULT_EQ:
8787 case CPP_DIV_EQ:
8788 case CPP_MOD_EQ:
8789 case CPP_PLUS_EQ:
8790 case CPP_MINUS_EQ:
8791 case CPP_RSHIFT_EQ:
8792 case CPP_LSHIFT_EQ:
8793 case CPP_AND_EQ:
8794 case CPP_XOR_EQ:
8795 case CPP_OR_EQ:
8796 case CPP_XOR:
8797 case CPP_OR:
8798 case CPP_OR_OR:
8799 case CPP_EOF:
8800 case CPP_ELLIPSIS:
8801 return 0;
8802
8803 case CPP_OPEN_PAREN:
8804 /* In ((type ()) () the last () isn't a valid cast-expression,
8805 so the whole must be parsed as postfix-expression. */
8806 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8807 != CPP_CLOSE_PAREN;
8808
8809 case CPP_OPEN_SQUARE:
8810 /* '[' may start a primary-expression in obj-c++ and in C++11,
8811 as a lambda-expression, eg, '(void)[]{}'. */
8812 if (cxx_dialect >= cxx11)
8813 return -1;
8814 return c_dialect_objc ();
8815
8816 case CPP_PLUS_PLUS:
8817 case CPP_MINUS_MINUS:
8818 /* '++' and '--' may or may not start a cast-expression:
8819
8820 struct T { void operator++(int); };
8821 void f() { (T())++; }
8822
8823 vs
8824
8825 int a;
8826 (int)++a; */
8827 return -1;
8828
8829 default:
8830 return 1;
8831 }
8832 }
8833
8834 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8835 in the order: const_cast, static_cast, reinterpret_cast.
8836
8837 Don't suggest dynamic_cast.
8838
8839 Return the first legal cast kind found, or NULL otherwise. */
8840
8841 static const char *
8842 get_cast_suggestion (tree dst_type, tree orig_expr)
8843 {
8844 tree trial;
8845
8846 /* Reuse the parser logic by attempting to build the various kinds of
8847 cast, with "complain" disabled.
8848 Identify the first such cast that is valid. */
8849
8850 /* Don't attempt to run such logic within template processing. */
8851 if (processing_template_decl)
8852 return NULL;
8853
8854 /* First try const_cast. */
8855 trial = build_const_cast (dst_type, orig_expr, tf_none);
8856 if (trial != error_mark_node)
8857 return "const_cast";
8858
8859 /* If that fails, try static_cast. */
8860 trial = build_static_cast (dst_type, orig_expr, tf_none);
8861 if (trial != error_mark_node)
8862 return "static_cast";
8863
8864 /* Finally, try reinterpret_cast. */
8865 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8866 if (trial != error_mark_node)
8867 return "reinterpret_cast";
8868
8869 /* No such cast possible. */
8870 return NULL;
8871 }
8872
8873 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8874 suggesting how to convert a C-style cast of the form:
8875
8876 (DST_TYPE)ORIG_EXPR
8877
8878 to a C++-style cast.
8879
8880 The primary range of RICHLOC is asssumed to be that of the original
8881 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8882 of the parens in the C-style cast. */
8883
8884 static void
8885 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8886 location_t close_paren_loc, tree orig_expr,
8887 tree dst_type)
8888 {
8889 /* This function is non-trivial, so bail out now if the warning isn't
8890 going to be emitted. */
8891 if (!warn_old_style_cast)
8892 return;
8893
8894 /* Try to find a legal C++ cast, trying them in order:
8895 const_cast, static_cast, reinterpret_cast. */
8896 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8897 if (!cast_suggestion)
8898 return;
8899
8900 /* Replace the open paren with "CAST_SUGGESTION<". */
8901 pretty_printer pp;
8902 pp_printf (&pp, "%s<", cast_suggestion);
8903 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8904
8905 /* Replace the close paren with "> (". */
8906 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8907
8908 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8909 rich_loc->add_fixit_insert_after (")");
8910 }
8911
8912
8913 /* Parse a cast-expression.
8914
8915 cast-expression:
8916 unary-expression
8917 ( type-id ) cast-expression
8918
8919 ADDRESS_P is true iff the unary-expression is appearing as the
8920 operand of the `&' operator. CAST_P is true if this expression is
8921 the target of a cast.
8922
8923 Returns a representation of the expression. */
8924
8925 static cp_expr
8926 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8927 bool decltype_p, cp_id_kind * pidk)
8928 {
8929 /* If it's a `(', then we might be looking at a cast. */
8930 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8931 {
8932 tree type = NULL_TREE;
8933 cp_expr expr (NULL_TREE);
8934 int cast_expression = 0;
8935 const char *saved_message;
8936
8937 /* There's no way to know yet whether or not this is a cast.
8938 For example, `(int (3))' is a unary-expression, while `(int)
8939 3' is a cast. So, we resort to parsing tentatively. */
8940 cp_parser_parse_tentatively (parser);
8941 /* Types may not be defined in a cast. */
8942 saved_message = parser->type_definition_forbidden_message;
8943 parser->type_definition_forbidden_message
8944 = G_("types may not be defined in casts");
8945 /* Consume the `('. */
8946 matching_parens parens;
8947 cp_token *open_paren = parens.consume_open (parser);
8948 location_t open_paren_loc = open_paren->location;
8949 location_t close_paren_loc = UNKNOWN_LOCATION;
8950
8951 /* A very tricky bit is that `(struct S) { 3 }' is a
8952 compound-literal (which we permit in C++ as an extension).
8953 But, that construct is not a cast-expression -- it is a
8954 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8955 is legal; if the compound-literal were a cast-expression,
8956 you'd need an extra set of parentheses.) But, if we parse
8957 the type-id, and it happens to be a class-specifier, then we
8958 will commit to the parse at that point, because we cannot
8959 undo the action that is done when creating a new class. So,
8960 then we cannot back up and do a postfix-expression.
8961
8962 Another tricky case is the following (c++/29234):
8963
8964 struct S { void operator () (); };
8965
8966 void foo ()
8967 {
8968 ( S()() );
8969 }
8970
8971 As a type-id we parse the parenthesized S()() as a function
8972 returning a function, groktypename complains and we cannot
8973 back up in this case either.
8974
8975 Therefore, we scan ahead to the closing `)', and check to see
8976 if the tokens after the `)' can start a cast-expression. Otherwise
8977 we are dealing with an unary-expression, a postfix-expression
8978 or something else.
8979
8980 Yet another tricky case, in C++11, is the following (c++/54891):
8981
8982 (void)[]{};
8983
8984 The issue is that usually, besides the case of lambda-expressions,
8985 the parenthesized type-id cannot be followed by '[', and, eg, we
8986 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8987 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8988 we don't commit, we try a cast-expression, then an unary-expression.
8989
8990 Save tokens so that we can put them back. */
8991 cp_lexer_save_tokens (parser->lexer);
8992
8993 /* We may be looking at a cast-expression. */
8994 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8995 /*consume_paren=*/true))
8996 cast_expression
8997 = cp_parser_tokens_start_cast_expression (parser);
8998
8999 /* Roll back the tokens we skipped. */
9000 cp_lexer_rollback_tokens (parser->lexer);
9001 /* If we aren't looking at a cast-expression, simulate an error so
9002 that the call to cp_parser_error_occurred below returns true. */
9003 if (!cast_expression)
9004 cp_parser_simulate_error (parser);
9005 else
9006 {
9007 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9008 parser->in_type_id_in_expr_p = true;
9009 /* Look for the type-id. */
9010 type = cp_parser_type_id (parser);
9011 /* Look for the closing `)'. */
9012 cp_token *close_paren = parens.require_close (parser);
9013 if (close_paren)
9014 close_paren_loc = close_paren->location;
9015 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9016 }
9017
9018 /* Restore the saved message. */
9019 parser->type_definition_forbidden_message = saved_message;
9020
9021 /* At this point this can only be either a cast or a
9022 parenthesized ctor such as `(T ())' that looks like a cast to
9023 function returning T. */
9024 if (!cp_parser_error_occurred (parser))
9025 {
9026 /* Only commit if the cast-expression doesn't start with
9027 '++', '--', or '[' in C++11. */
9028 if (cast_expression > 0)
9029 cp_parser_commit_to_topmost_tentative_parse (parser);
9030
9031 expr = cp_parser_cast_expression (parser,
9032 /*address_p=*/false,
9033 /*cast_p=*/true,
9034 /*decltype_p=*/false,
9035 pidk);
9036
9037 if (cp_parser_parse_definitely (parser))
9038 {
9039 /* Warn about old-style casts, if so requested. */
9040 if (warn_old_style_cast
9041 && !in_system_header_at (input_location)
9042 && !VOID_TYPE_P (type)
9043 && current_lang_name != lang_name_c)
9044 {
9045 gcc_rich_location rich_loc (input_location);
9046 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9047 expr, type);
9048 warning_at (&rich_loc, OPT_Wold_style_cast,
9049 "use of old-style cast to %q#T", type);
9050 }
9051
9052 /* Only type conversions to integral or enumeration types
9053 can be used in constant-expressions. */
9054 if (!cast_valid_in_integral_constant_expression_p (type)
9055 && cp_parser_non_integral_constant_expression (parser,
9056 NIC_CAST))
9057 return error_mark_node;
9058
9059 /* Perform the cast. */
9060 /* Make a location:
9061 (TYPE) EXPR
9062 ^~~~~~~~~~~
9063 with start==caret at the open paren, extending to the
9064 end of "expr". */
9065 location_t cast_loc = make_location (open_paren_loc,
9066 open_paren_loc,
9067 expr.get_finish ());
9068 expr = build_c_cast (cast_loc, type, expr);
9069 return expr;
9070 }
9071 }
9072 else
9073 cp_parser_abort_tentative_parse (parser);
9074 }
9075
9076 /* If we get here, then it's not a cast, so it must be a
9077 unary-expression. */
9078 return cp_parser_unary_expression (parser, pidk, address_p,
9079 cast_p, decltype_p);
9080 }
9081
9082 /* Parse a binary expression of the general form:
9083
9084 pm-expression:
9085 cast-expression
9086 pm-expression .* cast-expression
9087 pm-expression ->* cast-expression
9088
9089 multiplicative-expression:
9090 pm-expression
9091 multiplicative-expression * pm-expression
9092 multiplicative-expression / pm-expression
9093 multiplicative-expression % pm-expression
9094
9095 additive-expression:
9096 multiplicative-expression
9097 additive-expression + multiplicative-expression
9098 additive-expression - multiplicative-expression
9099
9100 shift-expression:
9101 additive-expression
9102 shift-expression << additive-expression
9103 shift-expression >> additive-expression
9104
9105 relational-expression:
9106 shift-expression
9107 relational-expression < shift-expression
9108 relational-expression > shift-expression
9109 relational-expression <= shift-expression
9110 relational-expression >= shift-expression
9111
9112 GNU Extension:
9113
9114 relational-expression:
9115 relational-expression <? shift-expression
9116 relational-expression >? shift-expression
9117
9118 equality-expression:
9119 relational-expression
9120 equality-expression == relational-expression
9121 equality-expression != relational-expression
9122
9123 and-expression:
9124 equality-expression
9125 and-expression & equality-expression
9126
9127 exclusive-or-expression:
9128 and-expression
9129 exclusive-or-expression ^ and-expression
9130
9131 inclusive-or-expression:
9132 exclusive-or-expression
9133 inclusive-or-expression | exclusive-or-expression
9134
9135 logical-and-expression:
9136 inclusive-or-expression
9137 logical-and-expression && inclusive-or-expression
9138
9139 logical-or-expression:
9140 logical-and-expression
9141 logical-or-expression || logical-and-expression
9142
9143 All these are implemented with a single function like:
9144
9145 binary-expression:
9146 simple-cast-expression
9147 binary-expression <token> binary-expression
9148
9149 CAST_P is true if this expression is the target of a cast.
9150
9151 The binops_by_token map is used to get the tree codes for each <token> type.
9152 binary-expressions are associated according to a precedence table. */
9153
9154 #define TOKEN_PRECEDENCE(token) \
9155 (((token->type == CPP_GREATER \
9156 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9157 && !parser->greater_than_is_operator_p) \
9158 ? PREC_NOT_OPERATOR \
9159 : binops_by_token[token->type].prec)
9160
9161 static cp_expr
9162 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9163 bool no_toplevel_fold_p,
9164 bool decltype_p,
9165 enum cp_parser_prec prec,
9166 cp_id_kind * pidk)
9167 {
9168 cp_parser_expression_stack stack;
9169 cp_parser_expression_stack_entry *sp = &stack[0];
9170 cp_parser_expression_stack_entry current;
9171 cp_expr rhs;
9172 cp_token *token;
9173 enum tree_code rhs_type;
9174 enum cp_parser_prec new_prec, lookahead_prec;
9175 tree overload;
9176
9177 /* Parse the first expression. */
9178 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9179 ? TRUTH_NOT_EXPR : ERROR_MARK);
9180 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9181 cast_p, decltype_p, pidk);
9182 current.prec = prec;
9183
9184 if (cp_parser_error_occurred (parser))
9185 return error_mark_node;
9186
9187 for (;;)
9188 {
9189 /* Get an operator token. */
9190 token = cp_lexer_peek_token (parser->lexer);
9191
9192 if (warn_cxx11_compat
9193 && token->type == CPP_RSHIFT
9194 && !parser->greater_than_is_operator_p)
9195 {
9196 if (warning_at (token->location, OPT_Wc__11_compat,
9197 "%<>>%> operator is treated"
9198 " as two right angle brackets in C++11"))
9199 inform (token->location,
9200 "suggest parentheses around %<>>%> expression");
9201 }
9202
9203 new_prec = TOKEN_PRECEDENCE (token);
9204 if (new_prec != PREC_NOT_OPERATOR
9205 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9206 /* This is a fold-expression; handle it later. */
9207 new_prec = PREC_NOT_OPERATOR;
9208
9209 /* Popping an entry off the stack means we completed a subexpression:
9210 - either we found a token which is not an operator (`>' where it is not
9211 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9212 will happen repeatedly;
9213 - or, we found an operator which has lower priority. This is the case
9214 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9215 parsing `3 * 4'. */
9216 if (new_prec <= current.prec)
9217 {
9218 if (sp == stack)
9219 break;
9220 else
9221 goto pop;
9222 }
9223
9224 get_rhs:
9225 current.tree_type = binops_by_token[token->type].tree_type;
9226 current.loc = token->location;
9227
9228 /* We used the operator token. */
9229 cp_lexer_consume_token (parser->lexer);
9230
9231 /* For "false && x" or "true || x", x will never be executed;
9232 disable warnings while evaluating it. */
9233 if (current.tree_type == TRUTH_ANDIF_EXPR)
9234 c_inhibit_evaluation_warnings +=
9235 cp_fully_fold (current.lhs) == truthvalue_false_node;
9236 else if (current.tree_type == TRUTH_ORIF_EXPR)
9237 c_inhibit_evaluation_warnings +=
9238 cp_fully_fold (current.lhs) == truthvalue_true_node;
9239
9240 /* Extract another operand. It may be the RHS of this expression
9241 or the LHS of a new, higher priority expression. */
9242 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9243 ? TRUTH_NOT_EXPR : ERROR_MARK);
9244 rhs = cp_parser_simple_cast_expression (parser);
9245
9246 /* Get another operator token. Look up its precedence to avoid
9247 building a useless (immediately popped) stack entry for common
9248 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9249 token = cp_lexer_peek_token (parser->lexer);
9250 lookahead_prec = TOKEN_PRECEDENCE (token);
9251 if (lookahead_prec != PREC_NOT_OPERATOR
9252 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9253 lookahead_prec = PREC_NOT_OPERATOR;
9254 if (lookahead_prec > new_prec)
9255 {
9256 /* ... and prepare to parse the RHS of the new, higher priority
9257 expression. Since precedence levels on the stack are
9258 monotonically increasing, we do not have to care about
9259 stack overflows. */
9260 *sp = current;
9261 ++sp;
9262 current.lhs = rhs;
9263 current.lhs_type = rhs_type;
9264 current.prec = new_prec;
9265 new_prec = lookahead_prec;
9266 goto get_rhs;
9267
9268 pop:
9269 lookahead_prec = new_prec;
9270 /* If the stack is not empty, we have parsed into LHS the right side
9271 (`4' in the example above) of an expression we had suspended.
9272 We can use the information on the stack to recover the LHS (`3')
9273 from the stack together with the tree code (`MULT_EXPR'), and
9274 the precedence of the higher level subexpression
9275 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9276 which will be used to actually build the additive expression. */
9277 rhs = current.lhs;
9278 rhs_type = current.lhs_type;
9279 --sp;
9280 current = *sp;
9281 }
9282
9283 /* Undo the disabling of warnings done above. */
9284 if (current.tree_type == TRUTH_ANDIF_EXPR)
9285 c_inhibit_evaluation_warnings -=
9286 cp_fully_fold (current.lhs) == truthvalue_false_node;
9287 else if (current.tree_type == TRUTH_ORIF_EXPR)
9288 c_inhibit_evaluation_warnings -=
9289 cp_fully_fold (current.lhs) == truthvalue_true_node;
9290
9291 if (warn_logical_not_paren
9292 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9293 && current.lhs_type == TRUTH_NOT_EXPR
9294 /* Avoid warning for !!x == y. */
9295 && (TREE_CODE (current.lhs) != NE_EXPR
9296 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9297 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9298 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9299 /* Avoid warning for !b == y where b is boolean. */
9300 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9301 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9302 != BOOLEAN_TYPE))))
9303 /* Avoid warning for !!b == y where b is boolean. */
9304 && (!DECL_P (current.lhs)
9305 || TREE_TYPE (current.lhs) == NULL_TREE
9306 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9307 warn_logical_not_parentheses (current.loc, current.tree_type,
9308 current.lhs, maybe_constant_value (rhs));
9309
9310 overload = NULL;
9311
9312 location_t combined_loc = make_location (current.loc,
9313 current.lhs.get_start (),
9314 rhs.get_finish ());
9315
9316 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9317 ERROR_MARK for everything that is not a binary expression.
9318 This makes warn_about_parentheses miss some warnings that
9319 involve unary operators. For unary expressions we should
9320 pass the correct tree_code unless the unary expression was
9321 surrounded by parentheses.
9322 */
9323 if (no_toplevel_fold_p
9324 && lookahead_prec <= current.prec
9325 && sp == stack)
9326 {
9327 if (current.lhs == error_mark_node || rhs == error_mark_node)
9328 current.lhs = error_mark_node;
9329 else
9330 {
9331 current.lhs
9332 = build_min (current.tree_type,
9333 TREE_CODE_CLASS (current.tree_type)
9334 == tcc_comparison
9335 ? boolean_type_node : TREE_TYPE (current.lhs),
9336 current.lhs.get_value (), rhs.get_value ());
9337 SET_EXPR_LOCATION (current.lhs, combined_loc);
9338 }
9339 }
9340 else
9341 {
9342 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9343 current.lhs, current.lhs_type,
9344 rhs, rhs_type, &overload,
9345 complain_flags (decltype_p));
9346 /* TODO: build_x_binary_op doesn't always honor the location. */
9347 current.lhs.set_location (combined_loc);
9348 }
9349 current.lhs_type = current.tree_type;
9350
9351 /* If the binary operator required the use of an overloaded operator,
9352 then this expression cannot be an integral constant-expression.
9353 An overloaded operator can be used even if both operands are
9354 otherwise permissible in an integral constant-expression if at
9355 least one of the operands is of enumeration type. */
9356
9357 if (overload
9358 && cp_parser_non_integral_constant_expression (parser,
9359 NIC_OVERLOADED))
9360 return error_mark_node;
9361 }
9362
9363 return current.lhs;
9364 }
9365
9366 static cp_expr
9367 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9368 bool no_toplevel_fold_p,
9369 enum cp_parser_prec prec,
9370 cp_id_kind * pidk)
9371 {
9372 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9373 /*decltype*/false, prec, pidk);
9374 }
9375
9376 /* Parse the `? expression : assignment-expression' part of a
9377 conditional-expression. The LOGICAL_OR_EXPR is the
9378 logical-or-expression that started the conditional-expression.
9379 Returns a representation of the entire conditional-expression.
9380
9381 This routine is used by cp_parser_assignment_expression.
9382
9383 ? expression : assignment-expression
9384
9385 GNU Extensions:
9386
9387 ? : assignment-expression */
9388
9389 static tree
9390 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9391 {
9392 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9393 cp_expr assignment_expr;
9394 struct cp_token *token;
9395 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9396
9397 /* Consume the `?' token. */
9398 cp_lexer_consume_token (parser->lexer);
9399 token = cp_lexer_peek_token (parser->lexer);
9400 if (cp_parser_allow_gnu_extensions_p (parser)
9401 && token->type == CPP_COLON)
9402 {
9403 pedwarn (token->location, OPT_Wpedantic,
9404 "ISO C++ does not allow ?: with omitted middle operand");
9405 /* Implicit true clause. */
9406 expr = NULL_TREE;
9407 c_inhibit_evaluation_warnings +=
9408 folded_logical_or_expr == truthvalue_true_node;
9409 warn_for_omitted_condop (token->location, logical_or_expr);
9410 }
9411 else
9412 {
9413 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9414 parser->colon_corrects_to_scope_p = false;
9415 /* Parse the expression. */
9416 c_inhibit_evaluation_warnings +=
9417 folded_logical_or_expr == truthvalue_false_node;
9418 expr = cp_parser_expression (parser);
9419 c_inhibit_evaluation_warnings +=
9420 ((folded_logical_or_expr == truthvalue_true_node)
9421 - (folded_logical_or_expr == truthvalue_false_node));
9422 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9423 }
9424
9425 /* The next token should be a `:'. */
9426 cp_parser_require (parser, CPP_COLON, RT_COLON);
9427 /* Parse the assignment-expression. */
9428 assignment_expr = cp_parser_assignment_expression (parser);
9429 c_inhibit_evaluation_warnings -=
9430 folded_logical_or_expr == truthvalue_true_node;
9431
9432 /* Make a location:
9433 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9434 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9435 with the caret at the "?", ranging from the start of
9436 the logical_or_expr to the end of the assignment_expr. */
9437 loc = make_location (loc,
9438 logical_or_expr.get_start (),
9439 assignment_expr.get_finish ());
9440
9441 /* Build the conditional-expression. */
9442 return build_x_conditional_expr (loc, logical_or_expr,
9443 expr,
9444 assignment_expr,
9445 tf_warning_or_error);
9446 }
9447
9448 /* Parse an assignment-expression.
9449
9450 assignment-expression:
9451 conditional-expression
9452 logical-or-expression assignment-operator assignment_expression
9453 throw-expression
9454
9455 CAST_P is true if this expression is the target of a cast.
9456 DECLTYPE_P is true if this expression is the operand of decltype.
9457
9458 Returns a representation for the expression. */
9459
9460 static cp_expr
9461 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9462 bool cast_p, bool decltype_p)
9463 {
9464 cp_expr expr;
9465
9466 /* If the next token is the `throw' keyword, then we're looking at
9467 a throw-expression. */
9468 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9469 expr = cp_parser_throw_expression (parser);
9470 /* Otherwise, it must be that we are looking at a
9471 logical-or-expression. */
9472 else
9473 {
9474 /* Parse the binary expressions (logical-or-expression). */
9475 expr = cp_parser_binary_expression (parser, cast_p, false,
9476 decltype_p,
9477 PREC_NOT_OPERATOR, pidk);
9478 /* If the next token is a `?' then we're actually looking at a
9479 conditional-expression. */
9480 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9481 return cp_parser_question_colon_clause (parser, expr);
9482 else
9483 {
9484 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9485
9486 /* If it's an assignment-operator, we're using the second
9487 production. */
9488 enum tree_code assignment_operator
9489 = cp_parser_assignment_operator_opt (parser);
9490 if (assignment_operator != ERROR_MARK)
9491 {
9492 bool non_constant_p;
9493
9494 /* Parse the right-hand side of the assignment. */
9495 cp_expr rhs = cp_parser_initializer_clause (parser,
9496 &non_constant_p);
9497
9498 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9499 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9500
9501 /* An assignment may not appear in a
9502 constant-expression. */
9503 if (cp_parser_non_integral_constant_expression (parser,
9504 NIC_ASSIGNMENT))
9505 return error_mark_node;
9506 /* Build the assignment expression. Its default
9507 location:
9508 LHS = RHS
9509 ~~~~^~~~~
9510 is the location of the '=' token as the
9511 caret, ranging from the start of the lhs to the
9512 end of the rhs. */
9513 loc = make_location (loc,
9514 expr.get_start (),
9515 rhs.get_finish ());
9516 expr = build_x_modify_expr (loc, expr,
9517 assignment_operator,
9518 rhs,
9519 complain_flags (decltype_p));
9520 /* TODO: build_x_modify_expr doesn't honor the location,
9521 so we must set it here. */
9522 expr.set_location (loc);
9523 }
9524 }
9525 }
9526
9527 return expr;
9528 }
9529
9530 /* Parse an (optional) assignment-operator.
9531
9532 assignment-operator: one of
9533 = *= /= %= += -= >>= <<= &= ^= |=
9534
9535 GNU Extension:
9536
9537 assignment-operator: one of
9538 <?= >?=
9539
9540 If the next token is an assignment operator, the corresponding tree
9541 code is returned, and the token is consumed. For example, for
9542 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9543 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9544 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9545 operator, ERROR_MARK is returned. */
9546
9547 static enum tree_code
9548 cp_parser_assignment_operator_opt (cp_parser* parser)
9549 {
9550 enum tree_code op;
9551 cp_token *token;
9552
9553 /* Peek at the next token. */
9554 token = cp_lexer_peek_token (parser->lexer);
9555
9556 switch (token->type)
9557 {
9558 case CPP_EQ:
9559 op = NOP_EXPR;
9560 break;
9561
9562 case CPP_MULT_EQ:
9563 op = MULT_EXPR;
9564 break;
9565
9566 case CPP_DIV_EQ:
9567 op = TRUNC_DIV_EXPR;
9568 break;
9569
9570 case CPP_MOD_EQ:
9571 op = TRUNC_MOD_EXPR;
9572 break;
9573
9574 case CPP_PLUS_EQ:
9575 op = PLUS_EXPR;
9576 break;
9577
9578 case CPP_MINUS_EQ:
9579 op = MINUS_EXPR;
9580 break;
9581
9582 case CPP_RSHIFT_EQ:
9583 op = RSHIFT_EXPR;
9584 break;
9585
9586 case CPP_LSHIFT_EQ:
9587 op = LSHIFT_EXPR;
9588 break;
9589
9590 case CPP_AND_EQ:
9591 op = BIT_AND_EXPR;
9592 break;
9593
9594 case CPP_XOR_EQ:
9595 op = BIT_XOR_EXPR;
9596 break;
9597
9598 case CPP_OR_EQ:
9599 op = BIT_IOR_EXPR;
9600 break;
9601
9602 default:
9603 /* Nothing else is an assignment operator. */
9604 op = ERROR_MARK;
9605 }
9606
9607 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9608 if (op != ERROR_MARK
9609 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9610 op = ERROR_MARK;
9611
9612 /* If it was an assignment operator, consume it. */
9613 if (op != ERROR_MARK)
9614 cp_lexer_consume_token (parser->lexer);
9615
9616 return op;
9617 }
9618
9619 /* Parse an expression.
9620
9621 expression:
9622 assignment-expression
9623 expression , assignment-expression
9624
9625 CAST_P is true if this expression is the target of a cast.
9626 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9627 except possibly parenthesized or on the RHS of a comma (N3276).
9628
9629 Returns a representation of the expression. */
9630
9631 static cp_expr
9632 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9633 bool cast_p, bool decltype_p)
9634 {
9635 cp_expr expression = NULL_TREE;
9636 location_t loc = UNKNOWN_LOCATION;
9637
9638 while (true)
9639 {
9640 cp_expr assignment_expression;
9641
9642 /* Parse the next assignment-expression. */
9643 assignment_expression
9644 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9645
9646 /* We don't create a temporary for a call that is the immediate operand
9647 of decltype or on the RHS of a comma. But when we see a comma, we
9648 need to create a temporary for a call on the LHS. */
9649 if (decltype_p && !processing_template_decl
9650 && TREE_CODE (assignment_expression) == CALL_EXPR
9651 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9652 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9653 assignment_expression
9654 = build_cplus_new (TREE_TYPE (assignment_expression),
9655 assignment_expression, tf_warning_or_error);
9656
9657 /* If this is the first assignment-expression, we can just
9658 save it away. */
9659 if (!expression)
9660 expression = assignment_expression;
9661 else
9662 {
9663 /* Create a location with caret at the comma, ranging
9664 from the start of the LHS to the end of the RHS. */
9665 loc = make_location (loc,
9666 expression.get_start (),
9667 assignment_expression.get_finish ());
9668 expression = build_x_compound_expr (loc, expression,
9669 assignment_expression,
9670 complain_flags (decltype_p));
9671 expression.set_location (loc);
9672 }
9673 /* If the next token is not a comma, or we're in a fold-expression, then
9674 we are done with the expression. */
9675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9676 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9677 break;
9678 /* Consume the `,'. */
9679 loc = cp_lexer_peek_token (parser->lexer)->location;
9680 cp_lexer_consume_token (parser->lexer);
9681 /* A comma operator cannot appear in a constant-expression. */
9682 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9683 expression = error_mark_node;
9684 }
9685
9686 return expression;
9687 }
9688
9689 /* Parse a constant-expression.
9690
9691 constant-expression:
9692 conditional-expression
9693
9694 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9695 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9696 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9697 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9698 only parse a conditional-expression, otherwise parse an
9699 assignment-expression. See below for rationale. */
9700
9701 static cp_expr
9702 cp_parser_constant_expression (cp_parser* parser,
9703 bool allow_non_constant_p,
9704 bool *non_constant_p,
9705 bool strict_p)
9706 {
9707 bool saved_integral_constant_expression_p;
9708 bool saved_allow_non_integral_constant_expression_p;
9709 bool saved_non_integral_constant_expression_p;
9710 cp_expr expression;
9711
9712 /* It might seem that we could simply parse the
9713 conditional-expression, and then check to see if it were
9714 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9715 one that the compiler can figure out is constant, possibly after
9716 doing some simplifications or optimizations. The standard has a
9717 precise definition of constant-expression, and we must honor
9718 that, even though it is somewhat more restrictive.
9719
9720 For example:
9721
9722 int i[(2, 3)];
9723
9724 is not a legal declaration, because `(2, 3)' is not a
9725 constant-expression. The `,' operator is forbidden in a
9726 constant-expression. However, GCC's constant-folding machinery
9727 will fold this operation to an INTEGER_CST for `3'. */
9728
9729 /* Save the old settings. */
9730 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9731 saved_allow_non_integral_constant_expression_p
9732 = parser->allow_non_integral_constant_expression_p;
9733 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9734 /* We are now parsing a constant-expression. */
9735 parser->integral_constant_expression_p = true;
9736 parser->allow_non_integral_constant_expression_p
9737 = (allow_non_constant_p || cxx_dialect >= cxx11);
9738 parser->non_integral_constant_expression_p = false;
9739 /* Although the grammar says "conditional-expression", when not STRICT_P,
9740 we parse an "assignment-expression", which also permits
9741 "throw-expression" and the use of assignment operators. In the case
9742 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9743 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9744 actually essential that we look for an assignment-expression.
9745 For example, cp_parser_initializer_clauses uses this function to
9746 determine whether a particular assignment-expression is in fact
9747 constant. */
9748 if (strict_p)
9749 {
9750 /* Parse the binary expressions (logical-or-expression). */
9751 expression = cp_parser_binary_expression (parser, false, false, false,
9752 PREC_NOT_OPERATOR, NULL);
9753 /* If the next token is a `?' then we're actually looking at
9754 a conditional-expression; otherwise we're done. */
9755 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9756 expression = cp_parser_question_colon_clause (parser, expression);
9757 }
9758 else
9759 expression = cp_parser_assignment_expression (parser);
9760 /* Restore the old settings. */
9761 parser->integral_constant_expression_p
9762 = saved_integral_constant_expression_p;
9763 parser->allow_non_integral_constant_expression_p
9764 = saved_allow_non_integral_constant_expression_p;
9765 if (cxx_dialect >= cxx11)
9766 {
9767 /* Require an rvalue constant expression here; that's what our
9768 callers expect. Reference constant expressions are handled
9769 separately in e.g. cp_parser_template_argument. */
9770 tree decay = expression;
9771 if (TREE_TYPE (expression)
9772 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9773 decay = build_address (expression);
9774 bool is_const = potential_rvalue_constant_expression (decay);
9775 parser->non_integral_constant_expression_p = !is_const;
9776 if (!is_const && !allow_non_constant_p)
9777 require_potential_rvalue_constant_expression (decay);
9778 }
9779 if (allow_non_constant_p)
9780 *non_constant_p = parser->non_integral_constant_expression_p;
9781 parser->non_integral_constant_expression_p
9782 = saved_non_integral_constant_expression_p;
9783
9784 return expression;
9785 }
9786
9787 /* Parse __builtin_offsetof.
9788
9789 offsetof-expression:
9790 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9791
9792 offsetof-member-designator:
9793 id-expression
9794 | offsetof-member-designator "." id-expression
9795 | offsetof-member-designator "[" expression "]"
9796 | offsetof-member-designator "->" id-expression */
9797
9798 static cp_expr
9799 cp_parser_builtin_offsetof (cp_parser *parser)
9800 {
9801 int save_ice_p, save_non_ice_p;
9802 tree type;
9803 cp_expr expr;
9804 cp_id_kind dummy;
9805 cp_token *token;
9806 location_t finish_loc;
9807
9808 /* We're about to accept non-integral-constant things, but will
9809 definitely yield an integral constant expression. Save and
9810 restore these values around our local parsing. */
9811 save_ice_p = parser->integral_constant_expression_p;
9812 save_non_ice_p = parser->non_integral_constant_expression_p;
9813
9814 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9815
9816 /* Consume the "__builtin_offsetof" token. */
9817 cp_lexer_consume_token (parser->lexer);
9818 /* Consume the opening `('. */
9819 matching_parens parens;
9820 parens.require_open (parser);
9821 /* Parse the type-id. */
9822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9823 {
9824 const char *saved_message = parser->type_definition_forbidden_message;
9825 parser->type_definition_forbidden_message
9826 = G_("types may not be defined within __builtin_offsetof");
9827 type = cp_parser_type_id (parser);
9828 parser->type_definition_forbidden_message = saved_message;
9829 }
9830 /* Look for the `,'. */
9831 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9832 token = cp_lexer_peek_token (parser->lexer);
9833
9834 /* Build the (type *)null that begins the traditional offsetof macro. */
9835 tree object_ptr
9836 = build_static_cast (build_pointer_type (type), null_pointer_node,
9837 tf_warning_or_error);
9838
9839 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9840 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9841 true, &dummy, token->location);
9842 while (true)
9843 {
9844 token = cp_lexer_peek_token (parser->lexer);
9845 switch (token->type)
9846 {
9847 case CPP_OPEN_SQUARE:
9848 /* offsetof-member-designator "[" expression "]" */
9849 expr = cp_parser_postfix_open_square_expression (parser, expr,
9850 true, false);
9851 break;
9852
9853 case CPP_DEREF:
9854 /* offsetof-member-designator "->" identifier */
9855 expr = grok_array_decl (token->location, expr,
9856 integer_zero_node, false);
9857 /* FALLTHRU */
9858
9859 case CPP_DOT:
9860 /* offsetof-member-designator "." identifier */
9861 cp_lexer_consume_token (parser->lexer);
9862 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9863 expr, true, &dummy,
9864 token->location);
9865 break;
9866
9867 case CPP_CLOSE_PAREN:
9868 /* Consume the ")" token. */
9869 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9870 cp_lexer_consume_token (parser->lexer);
9871 goto success;
9872
9873 default:
9874 /* Error. We know the following require will fail, but
9875 that gives the proper error message. */
9876 parens.require_close (parser);
9877 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9878 expr = error_mark_node;
9879 goto failure;
9880 }
9881 }
9882
9883 success:
9884 /* Make a location of the form:
9885 __builtin_offsetof (struct s, f)
9886 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9887 with caret at the type-id, ranging from the start of the
9888 "_builtin_offsetof" token to the close paren. */
9889 loc = make_location (loc, start_loc, finish_loc);
9890 /* The result will be an INTEGER_CST, so we need to explicitly
9891 preserve the location. */
9892 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9893
9894 failure:
9895 parser->integral_constant_expression_p = save_ice_p;
9896 parser->non_integral_constant_expression_p = save_non_ice_p;
9897
9898 expr = expr.maybe_add_location_wrapper ();
9899 return expr;
9900 }
9901
9902 /* Parse a trait expression.
9903
9904 Returns a representation of the expression, the underlying type
9905 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9906
9907 static cp_expr
9908 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9909 {
9910 cp_trait_kind kind;
9911 tree type1, type2 = NULL_TREE;
9912 bool binary = false;
9913 bool variadic = false;
9914
9915 switch (keyword)
9916 {
9917 case RID_HAS_NOTHROW_ASSIGN:
9918 kind = CPTK_HAS_NOTHROW_ASSIGN;
9919 break;
9920 case RID_HAS_NOTHROW_CONSTRUCTOR:
9921 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9922 break;
9923 case RID_HAS_NOTHROW_COPY:
9924 kind = CPTK_HAS_NOTHROW_COPY;
9925 break;
9926 case RID_HAS_TRIVIAL_ASSIGN:
9927 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9928 break;
9929 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9930 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9931 break;
9932 case RID_HAS_TRIVIAL_COPY:
9933 kind = CPTK_HAS_TRIVIAL_COPY;
9934 break;
9935 case RID_HAS_TRIVIAL_DESTRUCTOR:
9936 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9937 break;
9938 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9939 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9940 break;
9941 case RID_HAS_VIRTUAL_DESTRUCTOR:
9942 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9943 break;
9944 case RID_IS_ABSTRACT:
9945 kind = CPTK_IS_ABSTRACT;
9946 break;
9947 case RID_IS_AGGREGATE:
9948 kind = CPTK_IS_AGGREGATE;
9949 break;
9950 case RID_IS_BASE_OF:
9951 kind = CPTK_IS_BASE_OF;
9952 binary = true;
9953 break;
9954 case RID_IS_CLASS:
9955 kind = CPTK_IS_CLASS;
9956 break;
9957 case RID_IS_EMPTY:
9958 kind = CPTK_IS_EMPTY;
9959 break;
9960 case RID_IS_ENUM:
9961 kind = CPTK_IS_ENUM;
9962 break;
9963 case RID_IS_FINAL:
9964 kind = CPTK_IS_FINAL;
9965 break;
9966 case RID_IS_LITERAL_TYPE:
9967 kind = CPTK_IS_LITERAL_TYPE;
9968 break;
9969 case RID_IS_POD:
9970 kind = CPTK_IS_POD;
9971 break;
9972 case RID_IS_POLYMORPHIC:
9973 kind = CPTK_IS_POLYMORPHIC;
9974 break;
9975 case RID_IS_SAME_AS:
9976 kind = CPTK_IS_SAME_AS;
9977 binary = true;
9978 break;
9979 case RID_IS_STD_LAYOUT:
9980 kind = CPTK_IS_STD_LAYOUT;
9981 break;
9982 case RID_IS_TRIVIAL:
9983 kind = CPTK_IS_TRIVIAL;
9984 break;
9985 case RID_IS_TRIVIALLY_ASSIGNABLE:
9986 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9987 binary = true;
9988 break;
9989 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9990 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9991 variadic = true;
9992 break;
9993 case RID_IS_TRIVIALLY_COPYABLE:
9994 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9995 break;
9996 case RID_IS_UNION:
9997 kind = CPTK_IS_UNION;
9998 break;
9999 case RID_UNDERLYING_TYPE:
10000 kind = CPTK_UNDERLYING_TYPE;
10001 break;
10002 case RID_BASES:
10003 kind = CPTK_BASES;
10004 break;
10005 case RID_DIRECT_BASES:
10006 kind = CPTK_DIRECT_BASES;
10007 break;
10008 case RID_IS_ASSIGNABLE:
10009 kind = CPTK_IS_ASSIGNABLE;
10010 binary = true;
10011 break;
10012 case RID_IS_CONSTRUCTIBLE:
10013 kind = CPTK_IS_CONSTRUCTIBLE;
10014 variadic = true;
10015 break;
10016 default:
10017 gcc_unreachable ();
10018 }
10019
10020 /* Get location of initial token. */
10021 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10022
10023 /* Consume the token. */
10024 cp_lexer_consume_token (parser->lexer);
10025
10026 matching_parens parens;
10027 parens.require_open (parser);
10028
10029 {
10030 type_id_in_expr_sentinel s (parser);
10031 type1 = cp_parser_type_id (parser);
10032 }
10033
10034 if (type1 == error_mark_node)
10035 return error_mark_node;
10036
10037 if (binary)
10038 {
10039 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10040
10041 {
10042 type_id_in_expr_sentinel s (parser);
10043 type2 = cp_parser_type_id (parser);
10044 }
10045
10046 if (type2 == error_mark_node)
10047 return error_mark_node;
10048 }
10049 else if (variadic)
10050 {
10051 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10052 {
10053 cp_lexer_consume_token (parser->lexer);
10054 tree elt = cp_parser_type_id (parser);
10055 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10056 {
10057 cp_lexer_consume_token (parser->lexer);
10058 elt = make_pack_expansion (elt);
10059 }
10060 if (elt == error_mark_node)
10061 return error_mark_node;
10062 type2 = tree_cons (NULL_TREE, elt, type2);
10063 }
10064 }
10065
10066 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10067 parens.require_close (parser);
10068
10069 /* Construct a location of the form:
10070 __is_trivially_copyable(_Tp)
10071 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10072 with start == caret, finishing at the close-paren. */
10073 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10074
10075 /* Complete the trait expression, which may mean either processing
10076 the trait expr now or saving it for template instantiation. */
10077 switch (kind)
10078 {
10079 case CPTK_UNDERLYING_TYPE:
10080 return cp_expr (finish_underlying_type (type1), trait_loc);
10081 case CPTK_BASES:
10082 return cp_expr (finish_bases (type1, false), trait_loc);
10083 case CPTK_DIRECT_BASES:
10084 return cp_expr (finish_bases (type1, true), trait_loc);
10085 default:
10086 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10087 }
10088 }
10089
10090 /* Parse a lambda expression.
10091
10092 lambda-expression:
10093 lambda-introducer lambda-declarator [opt] compound-statement
10094
10095 Returns a representation of the expression. */
10096
10097 static cp_expr
10098 cp_parser_lambda_expression (cp_parser* parser)
10099 {
10100 tree lambda_expr = build_lambda_expr ();
10101 tree type;
10102 bool ok = true;
10103 cp_token *token = cp_lexer_peek_token (parser->lexer);
10104 cp_token_position start = 0;
10105
10106 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10107
10108 if (cp_unevaluated_operand)
10109 {
10110 if (!token->error_reported)
10111 {
10112 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10113 "lambda-expression in unevaluated context");
10114 token->error_reported = true;
10115 }
10116 ok = false;
10117 }
10118 else if (parser->in_template_argument_list_p)
10119 {
10120 if (!token->error_reported)
10121 {
10122 error_at (token->location, "lambda-expression in template-argument");
10123 token->error_reported = true;
10124 }
10125 ok = false;
10126 }
10127
10128 /* We may be in the middle of deferred access check. Disable
10129 it now. */
10130 push_deferring_access_checks (dk_no_deferred);
10131
10132 cp_parser_lambda_introducer (parser, lambda_expr);
10133
10134 type = begin_lambda_type (lambda_expr);
10135 if (type == error_mark_node)
10136 return error_mark_node;
10137
10138 record_lambda_scope (lambda_expr);
10139
10140 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10141 determine_visibility (TYPE_NAME (type));
10142
10143 /* Now that we've started the type, add the capture fields for any
10144 explicit captures. */
10145 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10146
10147 {
10148 /* Inside the class, surrounding template-parameter-lists do not apply. */
10149 unsigned int saved_num_template_parameter_lists
10150 = parser->num_template_parameter_lists;
10151 unsigned char in_statement = parser->in_statement;
10152 bool in_switch_statement_p = parser->in_switch_statement_p;
10153 bool fully_implicit_function_template_p
10154 = parser->fully_implicit_function_template_p;
10155 tree implicit_template_parms = parser->implicit_template_parms;
10156 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10157 bool auto_is_implicit_function_template_parm_p
10158 = parser->auto_is_implicit_function_template_parm_p;
10159
10160 parser->num_template_parameter_lists = 0;
10161 parser->in_statement = 0;
10162 parser->in_switch_statement_p = false;
10163 parser->fully_implicit_function_template_p = false;
10164 parser->implicit_template_parms = 0;
10165 parser->implicit_template_scope = 0;
10166 parser->auto_is_implicit_function_template_parm_p = false;
10167
10168 /* By virtue of defining a local class, a lambda expression has access to
10169 the private variables of enclosing classes. */
10170
10171 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10172
10173 if (ok && cp_parser_error_occurred (parser))
10174 ok = false;
10175
10176 if (ok)
10177 {
10178 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10179 && cp_parser_start_tentative_firewall (parser))
10180 start = token;
10181 cp_parser_lambda_body (parser, lambda_expr);
10182 }
10183 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10184 {
10185 if (cp_parser_skip_to_closing_brace (parser))
10186 cp_lexer_consume_token (parser->lexer);
10187 }
10188
10189 /* The capture list was built up in reverse order; fix that now. */
10190 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10191 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10192
10193 if (ok)
10194 maybe_add_lambda_conv_op (type);
10195
10196 type = finish_struct (type, /*attributes=*/NULL_TREE);
10197
10198 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10199 parser->in_statement = in_statement;
10200 parser->in_switch_statement_p = in_switch_statement_p;
10201 parser->fully_implicit_function_template_p
10202 = fully_implicit_function_template_p;
10203 parser->implicit_template_parms = implicit_template_parms;
10204 parser->implicit_template_scope = implicit_template_scope;
10205 parser->auto_is_implicit_function_template_parm_p
10206 = auto_is_implicit_function_template_parm_p;
10207 }
10208
10209 /* This field is only used during parsing of the lambda. */
10210 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10211
10212 /* This lambda shouldn't have any proxies left at this point. */
10213 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10214 /* And now that we're done, push proxies for an enclosing lambda. */
10215 insert_pending_capture_proxies ();
10216
10217 if (ok)
10218 lambda_expr = build_lambda_object (lambda_expr);
10219 else
10220 lambda_expr = error_mark_node;
10221
10222 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10223
10224 pop_deferring_access_checks ();
10225
10226 return lambda_expr;
10227 }
10228
10229 /* Parse the beginning of a lambda expression.
10230
10231 lambda-introducer:
10232 [ lambda-capture [opt] ]
10233
10234 LAMBDA_EXPR is the current representation of the lambda expression. */
10235
10236 static void
10237 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10238 {
10239 /* Need commas after the first capture. */
10240 bool first = true;
10241
10242 /* Eat the leading `['. */
10243 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10244
10245 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10246 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10247 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10248 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10249 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10250 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10251
10252 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10253 {
10254 cp_lexer_consume_token (parser->lexer);
10255 first = false;
10256 }
10257
10258 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10259 {
10260 cp_token* capture_token;
10261 tree capture_id;
10262 tree capture_init_expr;
10263 cp_id_kind idk = CP_ID_KIND_NONE;
10264 bool explicit_init_p = false;
10265
10266 enum capture_kind_type
10267 {
10268 BY_COPY,
10269 BY_REFERENCE
10270 };
10271 enum capture_kind_type capture_kind = BY_COPY;
10272
10273 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10274 {
10275 error ("expected end of capture-list");
10276 return;
10277 }
10278
10279 if (first)
10280 first = false;
10281 else
10282 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10283
10284 /* Possibly capture `this'. */
10285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10286 {
10287 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10288 if (cxx_dialect < cxx2a
10289 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10290 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10291 "with by-copy capture default");
10292 cp_lexer_consume_token (parser->lexer);
10293 add_capture (lambda_expr,
10294 /*id=*/this_identifier,
10295 /*initializer=*/finish_this_expr (),
10296 /*by_reference_p=*/true,
10297 explicit_init_p);
10298 continue;
10299 }
10300
10301 /* Possibly capture `*this'. */
10302 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10303 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10304 {
10305 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10306 if (cxx_dialect < cxx17)
10307 pedwarn (loc, 0, "%<*this%> capture only available with "
10308 "-std=c++17 or -std=gnu++17");
10309 cp_lexer_consume_token (parser->lexer);
10310 cp_lexer_consume_token (parser->lexer);
10311 add_capture (lambda_expr,
10312 /*id=*/this_identifier,
10313 /*initializer=*/finish_this_expr (),
10314 /*by_reference_p=*/false,
10315 explicit_init_p);
10316 continue;
10317 }
10318
10319 /* Remember whether we want to capture as a reference or not. */
10320 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10321 {
10322 capture_kind = BY_REFERENCE;
10323 cp_lexer_consume_token (parser->lexer);
10324 }
10325
10326 /* Get the identifier. */
10327 capture_token = cp_lexer_peek_token (parser->lexer);
10328 capture_id = cp_parser_identifier (parser);
10329
10330 if (capture_id == error_mark_node)
10331 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10332 delimiters, but I modified this to stop on unnested ']' as well. It
10333 was already changed to stop on unnested '}', so the
10334 "closing_parenthesis" name is no more misleading with my change. */
10335 {
10336 cp_parser_skip_to_closing_parenthesis (parser,
10337 /*recovering=*/true,
10338 /*or_comma=*/true,
10339 /*consume_paren=*/true);
10340 break;
10341 }
10342
10343 /* Find the initializer for this capture. */
10344 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10345 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10346 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10347 {
10348 bool direct, non_constant;
10349 /* An explicit initializer exists. */
10350 if (cxx_dialect < cxx14)
10351 pedwarn (input_location, 0,
10352 "lambda capture initializers "
10353 "only available with -std=c++14 or -std=gnu++14");
10354 capture_init_expr = cp_parser_initializer (parser, &direct,
10355 &non_constant, true);
10356 explicit_init_p = true;
10357 if (capture_init_expr == NULL_TREE)
10358 {
10359 error ("empty initializer for lambda init-capture");
10360 capture_init_expr = error_mark_node;
10361 }
10362 }
10363 else
10364 {
10365 const char* error_msg;
10366
10367 /* Turn the identifier into an id-expression. */
10368 capture_init_expr
10369 = cp_parser_lookup_name_simple (parser, capture_id,
10370 capture_token->location);
10371
10372 if (capture_init_expr == error_mark_node)
10373 {
10374 unqualified_name_lookup_error (capture_id);
10375 continue;
10376 }
10377 else if (!VAR_P (capture_init_expr)
10378 && TREE_CODE (capture_init_expr) != PARM_DECL)
10379 {
10380 error_at (capture_token->location,
10381 "capture of non-variable %qE",
10382 capture_init_expr);
10383 if (DECL_P (capture_init_expr))
10384 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10385 "%q#D declared here", capture_init_expr);
10386 continue;
10387 }
10388 if (VAR_P (capture_init_expr)
10389 && decl_storage_duration (capture_init_expr) != dk_auto)
10390 {
10391 if (pedwarn (capture_token->location, 0, "capture of variable "
10392 "%qD with non-automatic storage duration",
10393 capture_init_expr))
10394 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10395 "%q#D declared here", capture_init_expr);
10396 continue;
10397 }
10398
10399 capture_init_expr
10400 = finish_id_expression
10401 (capture_id,
10402 capture_init_expr,
10403 parser->scope,
10404 &idk,
10405 /*integral_constant_expression_p=*/false,
10406 /*allow_non_integral_constant_expression_p=*/false,
10407 /*non_integral_constant_expression_p=*/NULL,
10408 /*template_p=*/false,
10409 /*done=*/true,
10410 /*address_p=*/false,
10411 /*template_arg_p=*/false,
10412 &error_msg,
10413 capture_token->location);
10414
10415 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10416 {
10417 cp_lexer_consume_token (parser->lexer);
10418 capture_init_expr = make_pack_expansion (capture_init_expr);
10419 }
10420 }
10421
10422 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10423 && !explicit_init_p)
10424 {
10425 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10426 && capture_kind == BY_COPY)
10427 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10428 "of %qD redundant with by-copy capture default",
10429 capture_id);
10430 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10431 && capture_kind == BY_REFERENCE)
10432 pedwarn (capture_token->location, 0, "explicit by-reference "
10433 "capture of %qD redundant with by-reference capture "
10434 "default", capture_id);
10435 }
10436
10437 add_capture (lambda_expr,
10438 capture_id,
10439 capture_init_expr,
10440 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10441 explicit_init_p);
10442
10443 /* If there is any qualification still in effect, clear it
10444 now; we will be starting fresh with the next capture. */
10445 parser->scope = NULL_TREE;
10446 parser->qualifying_scope = NULL_TREE;
10447 parser->object_scope = NULL_TREE;
10448 }
10449
10450 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10451 }
10452
10453 /* Parse the (optional) middle of a lambda expression.
10454
10455 lambda-declarator:
10456 < template-parameter-list [opt] >
10457 ( parameter-declaration-clause [opt] )
10458 attribute-specifier [opt]
10459 decl-specifier-seq [opt]
10460 exception-specification [opt]
10461 lambda-return-type-clause [opt]
10462
10463 LAMBDA_EXPR is the current representation of the lambda expression. */
10464
10465 static bool
10466 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10467 {
10468 /* 5.1.1.4 of the standard says:
10469 If a lambda-expression does not include a lambda-declarator, it is as if
10470 the lambda-declarator were ().
10471 This means an empty parameter list, no attributes, and no exception
10472 specification. */
10473 tree param_list = void_list_node;
10474 tree attributes = NULL_TREE;
10475 tree exception_spec = NULL_TREE;
10476 tree template_param_list = NULL_TREE;
10477 tree tx_qual = NULL_TREE;
10478 tree return_type = NULL_TREE;
10479 cp_decl_specifier_seq lambda_specs;
10480 clear_decl_specs (&lambda_specs);
10481
10482 /* The template-parameter-list is optional, but must begin with
10483 an opening angle if present. */
10484 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10485 {
10486 if (cxx_dialect < cxx14)
10487 pedwarn (parser->lexer->next_token->location, 0,
10488 "lambda templates are only available with "
10489 "-std=c++14 or -std=gnu++14");
10490 else if (cxx_dialect < cxx2a)
10491 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10492 "lambda templates are only available with "
10493 "-std=c++2a or -std=gnu++2a");
10494
10495 cp_lexer_consume_token (parser->lexer);
10496
10497 template_param_list = cp_parser_template_parameter_list (parser);
10498
10499 cp_parser_skip_to_end_of_template_parameter_list (parser);
10500
10501 /* We just processed one more parameter list. */
10502 ++parser->num_template_parameter_lists;
10503 }
10504
10505 /* The parameter-declaration-clause is optional (unless
10506 template-parameter-list was given), but must begin with an
10507 opening parenthesis if present. */
10508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10509 {
10510 matching_parens parens;
10511 parens.consume_open (parser);
10512
10513 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10514
10515 /* Parse parameters. */
10516 param_list = cp_parser_parameter_declaration_clause (parser);
10517
10518 /* Default arguments shall not be specified in the
10519 parameter-declaration-clause of a lambda-declarator. */
10520 if (cxx_dialect < cxx14)
10521 for (tree t = param_list; t; t = TREE_CHAIN (t))
10522 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10523 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10524 "default argument specified for lambda parameter");
10525
10526 parens.require_close (parser);
10527
10528 attributes = cp_parser_attributes_opt (parser);
10529
10530 /* In the decl-specifier-seq of the lambda-declarator, each
10531 decl-specifier shall either be mutable or constexpr. */
10532 int declares_class_or_enum;
10533 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10534 cp_parser_decl_specifier_seq (parser,
10535 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10536 &lambda_specs, &declares_class_or_enum);
10537 if (lambda_specs.storage_class == sc_mutable)
10538 {
10539 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10540 if (lambda_specs.conflicting_specifiers_p)
10541 error_at (lambda_specs.locations[ds_storage_class],
10542 "duplicate %<mutable%>");
10543 }
10544
10545 tx_qual = cp_parser_tx_qualifier_opt (parser);
10546
10547 /* Parse optional exception specification. */
10548 exception_spec = cp_parser_exception_specification_opt (parser);
10549
10550 /* Parse optional trailing return type. */
10551 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10552 {
10553 cp_lexer_consume_token (parser->lexer);
10554 return_type = cp_parser_trailing_type_id (parser);
10555 }
10556
10557 /* The function parameters must be in scope all the way until after the
10558 trailing-return-type in case of decltype. */
10559 pop_bindings_and_leave_scope ();
10560 }
10561 else if (template_param_list != NULL_TREE) // generate diagnostic
10562 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10563
10564 /* Create the function call operator.
10565
10566 Messing with declarators like this is no uglier than building up the
10567 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10568 other code. */
10569 {
10570 cp_decl_specifier_seq return_type_specs;
10571 cp_declarator* declarator;
10572 tree fco;
10573 int quals;
10574 void *p;
10575
10576 clear_decl_specs (&return_type_specs);
10577 return_type_specs.type = make_auto ();
10578
10579 if (lambda_specs.locations[ds_constexpr])
10580 {
10581 if (cxx_dialect >= cxx17)
10582 return_type_specs.locations[ds_constexpr]
10583 = lambda_specs.locations[ds_constexpr];
10584 else
10585 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10586 "lambda only available with -std=c++17 or -std=gnu++17");
10587 }
10588
10589 p = obstack_alloc (&declarator_obstack, 0);
10590
10591 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10592
10593 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10594 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10595 declarator = make_call_declarator (declarator, param_list, quals,
10596 VIRT_SPEC_UNSPECIFIED,
10597 REF_QUAL_NONE,
10598 tx_qual,
10599 exception_spec,
10600 /*late_return_type=*/NULL_TREE,
10601 /*requires_clause*/NULL_TREE);
10602 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10603 if (return_type)
10604 declarator->u.function.late_return_type = return_type;
10605
10606 fco = grokmethod (&return_type_specs,
10607 declarator,
10608 attributes);
10609 if (fco != error_mark_node)
10610 {
10611 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10612 DECL_ARTIFICIAL (fco) = 1;
10613 /* Give the object parameter a different name. */
10614 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10615 }
10616 if (template_param_list)
10617 {
10618 fco = finish_member_template_decl (fco);
10619 finish_template_decl (template_param_list);
10620 --parser->num_template_parameter_lists;
10621 }
10622 else if (parser->fully_implicit_function_template_p)
10623 fco = finish_fully_implicit_template (parser, fco);
10624
10625 finish_member_declaration (fco);
10626
10627 obstack_free (&declarator_obstack, p);
10628
10629 return (fco != error_mark_node);
10630 }
10631 }
10632
10633 /* Parse the body of a lambda expression, which is simply
10634
10635 compound-statement
10636
10637 but which requires special handling.
10638 LAMBDA_EXPR is the current representation of the lambda expression. */
10639
10640 static void
10641 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10642 {
10643 bool nested = (current_function_decl != NULL_TREE);
10644 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10645 bool in_function_body = parser->in_function_body;
10646
10647 if (nested)
10648 push_function_context ();
10649 else
10650 /* Still increment function_depth so that we don't GC in the
10651 middle of an expression. */
10652 ++function_depth;
10653
10654 vec<tree> omp_privatization_save;
10655 save_omp_privatization_clauses (omp_privatization_save);
10656 /* Clear this in case we're in the middle of a default argument. */
10657 parser->local_variables_forbidden_p = false;
10658 parser->in_function_body = true;
10659
10660 {
10661 local_specialization_stack s (lss_copy);
10662 tree fco = lambda_function (lambda_expr);
10663 tree body = start_lambda_function (fco, lambda_expr);
10664 matching_braces braces;
10665
10666 if (braces.require_open (parser))
10667 {
10668 tree compound_stmt = begin_compound_stmt (0);
10669
10670 /* Originally C++11 required us to peek for 'return expr'; and
10671 process it specially here to deduce the return type. N3638
10672 removed the need for that. */
10673
10674 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10675 cp_parser_label_declaration (parser);
10676 cp_parser_statement_seq_opt (parser, NULL_TREE);
10677 braces.require_close (parser);
10678
10679 finish_compound_stmt (compound_stmt);
10680 }
10681
10682 finish_lambda_function (body);
10683 }
10684
10685 restore_omp_privatization_clauses (omp_privatization_save);
10686 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10687 parser->in_function_body = in_function_body;
10688 if (nested)
10689 pop_function_context();
10690 else
10691 --function_depth;
10692 }
10693
10694 /* Statements [gram.stmt.stmt] */
10695
10696 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10697
10698 static void
10699 add_debug_begin_stmt (location_t loc)
10700 {
10701 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10702 return;
10703 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10704 /* A concept is never expanded normally. */
10705 return;
10706
10707 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10708 SET_EXPR_LOCATION (stmt, loc);
10709 add_stmt (stmt);
10710 }
10711
10712 /* Parse a statement.
10713
10714 statement:
10715 labeled-statement
10716 expression-statement
10717 compound-statement
10718 selection-statement
10719 iteration-statement
10720 jump-statement
10721 declaration-statement
10722 try-block
10723
10724 C++11:
10725
10726 statement:
10727 labeled-statement
10728 attribute-specifier-seq (opt) expression-statement
10729 attribute-specifier-seq (opt) compound-statement
10730 attribute-specifier-seq (opt) selection-statement
10731 attribute-specifier-seq (opt) iteration-statement
10732 attribute-specifier-seq (opt) jump-statement
10733 declaration-statement
10734 attribute-specifier-seq (opt) try-block
10735
10736 init-statement:
10737 expression-statement
10738 simple-declaration
10739
10740 TM Extension:
10741
10742 statement:
10743 atomic-statement
10744
10745 IN_COMPOUND is true when the statement is nested inside a
10746 cp_parser_compound_statement; this matters for certain pragmas.
10747
10748 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10749 is a (possibly labeled) if statement which is not enclosed in braces
10750 and has an else clause. This is used to implement -Wparentheses.
10751
10752 CHAIN is a vector of if-else-if conditions. */
10753
10754 static void
10755 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10756 bool in_compound, bool *if_p, vec<tree> *chain,
10757 location_t *loc_after_labels)
10758 {
10759 tree statement, std_attrs = NULL_TREE;
10760 cp_token *token;
10761 location_t statement_location, attrs_location;
10762
10763 restart:
10764 if (if_p != NULL)
10765 *if_p = false;
10766 /* There is no statement yet. */
10767 statement = NULL_TREE;
10768
10769 saved_token_sentinel saved_tokens (parser->lexer);
10770 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10771 if (c_dialect_objc ())
10772 /* In obj-c++, seeing '[[' might be the either the beginning of
10773 c++11 attributes, or a nested objc-message-expression. So
10774 let's parse the c++11 attributes tentatively. */
10775 cp_parser_parse_tentatively (parser);
10776 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10777 if (c_dialect_objc ())
10778 {
10779 if (!cp_parser_parse_definitely (parser))
10780 std_attrs = NULL_TREE;
10781 }
10782
10783 /* Peek at the next token. */
10784 token = cp_lexer_peek_token (parser->lexer);
10785 /* Remember the location of the first token in the statement. */
10786 statement_location = token->location;
10787 add_debug_begin_stmt (statement_location);
10788 /* If this is a keyword, then that will often determine what kind of
10789 statement we have. */
10790 if (token->type == CPP_KEYWORD)
10791 {
10792 enum rid keyword = token->keyword;
10793
10794 switch (keyword)
10795 {
10796 case RID_CASE:
10797 case RID_DEFAULT:
10798 /* Looks like a labeled-statement with a case label.
10799 Parse the label, and then use tail recursion to parse
10800 the statement. */
10801 cp_parser_label_for_labeled_statement (parser, std_attrs);
10802 in_compound = false;
10803 goto restart;
10804
10805 case RID_IF:
10806 case RID_SWITCH:
10807 statement = cp_parser_selection_statement (parser, if_p, chain);
10808 break;
10809
10810 case RID_WHILE:
10811 case RID_DO:
10812 case RID_FOR:
10813 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10814 break;
10815
10816 case RID_BREAK:
10817 case RID_CONTINUE:
10818 case RID_RETURN:
10819 case RID_GOTO:
10820 statement = cp_parser_jump_statement (parser);
10821 break;
10822
10823 /* Objective-C++ exception-handling constructs. */
10824 case RID_AT_TRY:
10825 case RID_AT_CATCH:
10826 case RID_AT_FINALLY:
10827 case RID_AT_SYNCHRONIZED:
10828 case RID_AT_THROW:
10829 statement = cp_parser_objc_statement (parser);
10830 break;
10831
10832 case RID_TRY:
10833 statement = cp_parser_try_block (parser);
10834 break;
10835
10836 case RID_NAMESPACE:
10837 /* This must be a namespace alias definition. */
10838 cp_parser_declaration_statement (parser);
10839 return;
10840
10841 case RID_TRANSACTION_ATOMIC:
10842 case RID_TRANSACTION_RELAXED:
10843 case RID_SYNCHRONIZED:
10844 case RID_ATOMIC_NOEXCEPT:
10845 case RID_ATOMIC_CANCEL:
10846 statement = cp_parser_transaction (parser, token);
10847 break;
10848 case RID_TRANSACTION_CANCEL:
10849 statement = cp_parser_transaction_cancel (parser);
10850 break;
10851
10852 default:
10853 /* It might be a keyword like `int' that can start a
10854 declaration-statement. */
10855 break;
10856 }
10857 }
10858 else if (token->type == CPP_NAME)
10859 {
10860 /* If the next token is a `:', then we are looking at a
10861 labeled-statement. */
10862 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10863 if (token->type == CPP_COLON)
10864 {
10865 /* Looks like a labeled-statement with an ordinary label.
10866 Parse the label, and then use tail recursion to parse
10867 the statement. */
10868
10869 cp_parser_label_for_labeled_statement (parser, std_attrs);
10870 in_compound = false;
10871 goto restart;
10872 }
10873 }
10874 /* Anything that starts with a `{' must be a compound-statement. */
10875 else if (token->type == CPP_OPEN_BRACE)
10876 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10877 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10878 a statement all its own. */
10879 else if (token->type == CPP_PRAGMA)
10880 {
10881 /* Only certain OpenMP pragmas are attached to statements, and thus
10882 are considered statements themselves. All others are not. In
10883 the context of a compound, accept the pragma as a "statement" and
10884 return so that we can check for a close brace. Otherwise we
10885 require a real statement and must go back and read one. */
10886 if (in_compound)
10887 cp_parser_pragma (parser, pragma_compound, if_p);
10888 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10889 goto restart;
10890 return;
10891 }
10892 else if (token->type == CPP_EOF)
10893 {
10894 cp_parser_error (parser, "expected statement");
10895 return;
10896 }
10897
10898 /* Everything else must be a declaration-statement or an
10899 expression-statement. Try for the declaration-statement
10900 first, unless we are looking at a `;', in which case we know that
10901 we have an expression-statement. */
10902 if (!statement)
10903 {
10904 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10905 {
10906 if (std_attrs != NULL_TREE)
10907 {
10908 /* Attributes should be parsed as part of the the
10909 declaration, so let's un-parse them. */
10910 saved_tokens.rollback();
10911 std_attrs = NULL_TREE;
10912 }
10913
10914 cp_parser_parse_tentatively (parser);
10915 /* Try to parse the declaration-statement. */
10916 cp_parser_declaration_statement (parser);
10917 /* If that worked, we're done. */
10918 if (cp_parser_parse_definitely (parser))
10919 return;
10920 }
10921 /* All preceding labels have been parsed at this point. */
10922 if (loc_after_labels != NULL)
10923 *loc_after_labels = statement_location;
10924
10925 /* Look for an expression-statement instead. */
10926 statement = cp_parser_expression_statement (parser, in_statement_expr);
10927
10928 /* Handle [[fallthrough]];. */
10929 if (attribute_fallthrough_p (std_attrs))
10930 {
10931 /* The next token after the fallthrough attribute is ';'. */
10932 if (statement == NULL_TREE)
10933 {
10934 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10935 statement = build_call_expr_internal_loc (statement_location,
10936 IFN_FALLTHROUGH,
10937 void_type_node, 0);
10938 finish_expr_stmt (statement);
10939 }
10940 else
10941 warning_at (statement_location, OPT_Wattributes,
10942 "%<fallthrough%> attribute not followed by %<;%>");
10943 std_attrs = NULL_TREE;
10944 }
10945 }
10946
10947 /* Set the line number for the statement. */
10948 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10949 SET_EXPR_LOCATION (statement, statement_location);
10950
10951 /* Allow "[[fallthrough]];", but warn otherwise. */
10952 if (std_attrs != NULL_TREE)
10953 warning_at (attrs_location,
10954 OPT_Wattributes,
10955 "attributes at the beginning of statement are ignored");
10956 }
10957
10958 /* Append ATTR to attribute list ATTRS. */
10959
10960 static tree
10961 attr_chainon (tree attrs, tree attr)
10962 {
10963 if (attrs == error_mark_node)
10964 return error_mark_node;
10965 if (attr == error_mark_node)
10966 return error_mark_node;
10967 return chainon (attrs, attr);
10968 }
10969
10970 /* Parse the label for a labeled-statement, i.e.
10971
10972 identifier :
10973 case constant-expression :
10974 default :
10975
10976 GNU Extension:
10977 case constant-expression ... constant-expression : statement
10978
10979 When a label is parsed without errors, the label is added to the
10980 parse tree by the finish_* functions, so this function doesn't
10981 have to return the label. */
10982
10983 static void
10984 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10985 {
10986 cp_token *token;
10987 tree label = NULL_TREE;
10988 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10989
10990 /* The next token should be an identifier. */
10991 token = cp_lexer_peek_token (parser->lexer);
10992 if (token->type != CPP_NAME
10993 && token->type != CPP_KEYWORD)
10994 {
10995 cp_parser_error (parser, "expected labeled-statement");
10996 return;
10997 }
10998
10999 /* Remember whether this case or a user-defined label is allowed to fall
11000 through to. */
11001 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11002
11003 parser->colon_corrects_to_scope_p = false;
11004 switch (token->keyword)
11005 {
11006 case RID_CASE:
11007 {
11008 tree expr, expr_hi;
11009 cp_token *ellipsis;
11010
11011 /* Consume the `case' token. */
11012 cp_lexer_consume_token (parser->lexer);
11013 /* Parse the constant-expression. */
11014 expr = cp_parser_constant_expression (parser);
11015 if (check_for_bare_parameter_packs (expr))
11016 expr = error_mark_node;
11017
11018 ellipsis = cp_lexer_peek_token (parser->lexer);
11019 if (ellipsis->type == CPP_ELLIPSIS)
11020 {
11021 /* Consume the `...' token. */
11022 cp_lexer_consume_token (parser->lexer);
11023 expr_hi = cp_parser_constant_expression (parser);
11024 if (check_for_bare_parameter_packs (expr_hi))
11025 expr_hi = error_mark_node;
11026
11027 /* We don't need to emit warnings here, as the common code
11028 will do this for us. */
11029 }
11030 else
11031 expr_hi = NULL_TREE;
11032
11033 if (parser->in_switch_statement_p)
11034 {
11035 tree l = finish_case_label (token->location, expr, expr_hi);
11036 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11037 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11038 }
11039 else
11040 error_at (token->location,
11041 "case label %qE not within a switch statement",
11042 expr);
11043 }
11044 break;
11045
11046 case RID_DEFAULT:
11047 /* Consume the `default' token. */
11048 cp_lexer_consume_token (parser->lexer);
11049
11050 if (parser->in_switch_statement_p)
11051 {
11052 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11053 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11054 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11055 }
11056 else
11057 error_at (token->location, "case label not within a switch statement");
11058 break;
11059
11060 default:
11061 /* Anything else must be an ordinary label. */
11062 label = finish_label_stmt (cp_parser_identifier (parser));
11063 if (label && TREE_CODE (label) == LABEL_DECL)
11064 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11065 break;
11066 }
11067
11068 /* Require the `:' token. */
11069 cp_parser_require (parser, CPP_COLON, RT_COLON);
11070
11071 /* An ordinary label may optionally be followed by attributes.
11072 However, this is only permitted if the attributes are then
11073 followed by a semicolon. This is because, for backward
11074 compatibility, when parsing
11075 lab: __attribute__ ((unused)) int i;
11076 we want the attribute to attach to "i", not "lab". */
11077 if (label != NULL_TREE
11078 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11079 {
11080 tree attrs;
11081 cp_parser_parse_tentatively (parser);
11082 attrs = cp_parser_gnu_attributes_opt (parser);
11083 if (attrs == NULL_TREE
11084 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11085 cp_parser_abort_tentative_parse (parser);
11086 else if (!cp_parser_parse_definitely (parser))
11087 ;
11088 else
11089 attributes = attr_chainon (attributes, attrs);
11090 }
11091
11092 if (attributes != NULL_TREE)
11093 cplus_decl_attributes (&label, attributes, 0);
11094
11095 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11096 }
11097
11098 /* Parse an expression-statement.
11099
11100 expression-statement:
11101 expression [opt] ;
11102
11103 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11104 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11105 indicates whether this expression-statement is part of an
11106 expression statement. */
11107
11108 static tree
11109 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11110 {
11111 tree statement = NULL_TREE;
11112 cp_token *token = cp_lexer_peek_token (parser->lexer);
11113 location_t loc = token->location;
11114
11115 /* There might be attribute fallthrough. */
11116 tree attr = cp_parser_gnu_attributes_opt (parser);
11117
11118 /* If the next token is a ';', then there is no expression
11119 statement. */
11120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11121 {
11122 statement = cp_parser_expression (parser);
11123 if (statement == error_mark_node
11124 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11125 {
11126 cp_parser_skip_to_end_of_block_or_statement (parser);
11127 return error_mark_node;
11128 }
11129 }
11130
11131 /* Handle [[fallthrough]];. */
11132 if (attribute_fallthrough_p (attr))
11133 {
11134 /* The next token after the fallthrough attribute is ';'. */
11135 if (statement == NULL_TREE)
11136 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11137 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11138 void_type_node, 0);
11139 else
11140 warning_at (loc, OPT_Wattributes,
11141 "%<fallthrough%> attribute not followed by %<;%>");
11142 attr = NULL_TREE;
11143 }
11144
11145 /* Allow "[[fallthrough]];", but warn otherwise. */
11146 if (attr != NULL_TREE)
11147 warning_at (loc, OPT_Wattributes,
11148 "attributes at the beginning of statement are ignored");
11149
11150 /* Give a helpful message for "A<T>::type t;" and the like. */
11151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11152 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11153 {
11154 if (TREE_CODE (statement) == SCOPE_REF)
11155 error_at (token->location, "need %<typename%> before %qE because "
11156 "%qT is a dependent scope",
11157 statement, TREE_OPERAND (statement, 0));
11158 else if (is_overloaded_fn (statement)
11159 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11160 {
11161 /* A::A a; */
11162 tree fn = get_first_fn (statement);
11163 error_at (token->location,
11164 "%<%T::%D%> names the constructor, not the type",
11165 DECL_CONTEXT (fn), DECL_NAME (fn));
11166 }
11167 }
11168
11169 /* Consume the final `;'. */
11170 cp_parser_consume_semicolon_at_end_of_statement (parser);
11171
11172 if (in_statement_expr
11173 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11174 /* This is the final expression statement of a statement
11175 expression. */
11176 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11177 else if (statement)
11178 statement = finish_expr_stmt (statement);
11179
11180 return statement;
11181 }
11182
11183 /* Parse a compound-statement.
11184
11185 compound-statement:
11186 { statement-seq [opt] }
11187
11188 GNU extension:
11189
11190 compound-statement:
11191 { label-declaration-seq [opt] statement-seq [opt] }
11192
11193 label-declaration-seq:
11194 label-declaration
11195 label-declaration-seq label-declaration
11196
11197 Returns a tree representing the statement. */
11198
11199 static tree
11200 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11201 int bcs_flags, bool function_body)
11202 {
11203 tree compound_stmt;
11204 matching_braces braces;
11205
11206 /* Consume the `{'. */
11207 if (!braces.require_open (parser))
11208 return error_mark_node;
11209 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11210 && !function_body && cxx_dialect < cxx14)
11211 pedwarn (input_location, OPT_Wpedantic,
11212 "compound-statement in %<constexpr%> function");
11213 /* Begin the compound-statement. */
11214 compound_stmt = begin_compound_stmt (bcs_flags);
11215 /* If the next keyword is `__label__' we have a label declaration. */
11216 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11217 cp_parser_label_declaration (parser);
11218 /* Parse an (optional) statement-seq. */
11219 cp_parser_statement_seq_opt (parser, in_statement_expr);
11220 /* Finish the compound-statement. */
11221 finish_compound_stmt (compound_stmt);
11222 /* Consume the `}'. */
11223 braces.require_close (parser);
11224
11225 return compound_stmt;
11226 }
11227
11228 /* Parse an (optional) statement-seq.
11229
11230 statement-seq:
11231 statement
11232 statement-seq [opt] statement */
11233
11234 static void
11235 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11236 {
11237 /* Scan statements until there aren't any more. */
11238 while (true)
11239 {
11240 cp_token *token = cp_lexer_peek_token (parser->lexer);
11241
11242 /* If we are looking at a `}', then we have run out of
11243 statements; the same is true if we have reached the end
11244 of file, or have stumbled upon a stray '@end'. */
11245 if (token->type == CPP_CLOSE_BRACE
11246 || token->type == CPP_EOF
11247 || token->type == CPP_PRAGMA_EOL
11248 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11249 break;
11250
11251 /* If we are in a compound statement and find 'else' then
11252 something went wrong. */
11253 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11254 {
11255 if (parser->in_statement & IN_IF_STMT)
11256 break;
11257 else
11258 {
11259 token = cp_lexer_consume_token (parser->lexer);
11260 error_at (token->location, "%<else%> without a previous %<if%>");
11261 }
11262 }
11263
11264 /* Parse the statement. */
11265 cp_parser_statement (parser, in_statement_expr, true, NULL);
11266 }
11267 }
11268
11269 /* Return true if this is the C++20 version of range-based-for with
11270 init-statement. */
11271
11272 static bool
11273 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11274 {
11275 bool r = false;
11276
11277 /* Save tokens so that we can put them back. */
11278 cp_lexer_save_tokens (parser->lexer);
11279
11280 /* There has to be an unnested ; followed by an unnested :. */
11281 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11282 /*recovering=*/false,
11283 CPP_SEMICOLON,
11284 /*consume_paren=*/false) != -1)
11285 goto out;
11286
11287 /* We found the semicolon, eat it now. */
11288 cp_lexer_consume_token (parser->lexer);
11289
11290 /* Now look for ':' that is not nested in () or {}. */
11291 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11292 /*recovering=*/false,
11293 CPP_COLON,
11294 /*consume_paren=*/false) == -1);
11295
11296 out:
11297 /* Roll back the tokens we skipped. */
11298 cp_lexer_rollback_tokens (parser->lexer);
11299
11300 return r;
11301 }
11302
11303 /* Return true if we're looking at (init; cond), false otherwise. */
11304
11305 static bool
11306 cp_parser_init_statement_p (cp_parser *parser)
11307 {
11308 /* Save tokens so that we can put them back. */
11309 cp_lexer_save_tokens (parser->lexer);
11310
11311 /* Look for ';' that is not nested in () or {}. */
11312 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11313 /*recovering=*/false,
11314 CPP_SEMICOLON,
11315 /*consume_paren=*/false);
11316
11317 /* Roll back the tokens we skipped. */
11318 cp_lexer_rollback_tokens (parser->lexer);
11319
11320 return ret == -1;
11321 }
11322
11323 /* Parse a selection-statement.
11324
11325 selection-statement:
11326 if ( init-statement [opt] condition ) statement
11327 if ( init-statement [opt] condition ) statement else statement
11328 switch ( init-statement [opt] condition ) statement
11329
11330 Returns the new IF_STMT or SWITCH_STMT.
11331
11332 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11333 is a (possibly labeled) if statement which is not enclosed in
11334 braces and has an else clause. This is used to implement
11335 -Wparentheses.
11336
11337 CHAIN is a vector of if-else-if conditions. This is used to implement
11338 -Wduplicated-cond. */
11339
11340 static tree
11341 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11342 vec<tree> *chain)
11343 {
11344 cp_token *token;
11345 enum rid keyword;
11346 token_indent_info guard_tinfo;
11347
11348 if (if_p != NULL)
11349 *if_p = false;
11350
11351 /* Peek at the next token. */
11352 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11353 guard_tinfo = get_token_indent_info (token);
11354
11355 /* See what kind of keyword it is. */
11356 keyword = token->keyword;
11357 switch (keyword)
11358 {
11359 case RID_IF:
11360 case RID_SWITCH:
11361 {
11362 tree statement;
11363 tree condition;
11364
11365 bool cx = false;
11366 if (keyword == RID_IF
11367 && cp_lexer_next_token_is_keyword (parser->lexer,
11368 RID_CONSTEXPR))
11369 {
11370 cx = true;
11371 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11372 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11373 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11374 "with -std=c++17 or -std=gnu++17");
11375 }
11376
11377 /* Look for the `('. */
11378 matching_parens parens;
11379 if (!parens.require_open (parser))
11380 {
11381 cp_parser_skip_to_end_of_statement (parser);
11382 return error_mark_node;
11383 }
11384
11385 /* Begin the selection-statement. */
11386 if (keyword == RID_IF)
11387 {
11388 statement = begin_if_stmt ();
11389 IF_STMT_CONSTEXPR_P (statement) = cx;
11390 }
11391 else
11392 statement = begin_switch_stmt ();
11393
11394 /* Parse the optional init-statement. */
11395 if (cp_parser_init_statement_p (parser))
11396 {
11397 tree decl;
11398 if (cxx_dialect < cxx17)
11399 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11400 "init-statement in selection statements only available "
11401 "with -std=c++17 or -std=gnu++17");
11402 cp_parser_init_statement (parser, &decl);
11403 }
11404
11405 /* Parse the condition. */
11406 condition = cp_parser_condition (parser);
11407 /* Look for the `)'. */
11408 if (!parens.require_close (parser))
11409 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11410 /*consume_paren=*/true);
11411
11412 if (keyword == RID_IF)
11413 {
11414 bool nested_if;
11415 unsigned char in_statement;
11416
11417 /* Add the condition. */
11418 condition = finish_if_stmt_cond (condition, statement);
11419
11420 if (warn_duplicated_cond)
11421 warn_duplicated_cond_add_or_warn (token->location, condition,
11422 &chain);
11423
11424 /* Parse the then-clause. */
11425 in_statement = parser->in_statement;
11426 parser->in_statement |= IN_IF_STMT;
11427
11428 /* Outside a template, the non-selected branch of a constexpr
11429 if is a 'discarded statement', i.e. unevaluated. */
11430 bool was_discarded = in_discarded_stmt;
11431 bool discard_then = (cx && !processing_template_decl
11432 && integer_zerop (condition));
11433 if (discard_then)
11434 {
11435 in_discarded_stmt = true;
11436 ++c_inhibit_evaluation_warnings;
11437 }
11438
11439 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11440 guard_tinfo);
11441
11442 parser->in_statement = in_statement;
11443
11444 finish_then_clause (statement);
11445
11446 if (discard_then)
11447 {
11448 THEN_CLAUSE (statement) = NULL_TREE;
11449 in_discarded_stmt = was_discarded;
11450 --c_inhibit_evaluation_warnings;
11451 }
11452
11453 /* If the next token is `else', parse the else-clause. */
11454 if (cp_lexer_next_token_is_keyword (parser->lexer,
11455 RID_ELSE))
11456 {
11457 bool discard_else = (cx && !processing_template_decl
11458 && integer_nonzerop (condition));
11459 if (discard_else)
11460 {
11461 in_discarded_stmt = true;
11462 ++c_inhibit_evaluation_warnings;
11463 }
11464
11465 guard_tinfo
11466 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11467 /* Consume the `else' keyword. */
11468 cp_lexer_consume_token (parser->lexer);
11469 if (warn_duplicated_cond)
11470 {
11471 if (cp_lexer_next_token_is_keyword (parser->lexer,
11472 RID_IF)
11473 && chain == NULL)
11474 {
11475 /* We've got "if (COND) else if (COND2)". Start
11476 the condition chain and add COND as the first
11477 element. */
11478 chain = new vec<tree> ();
11479 if (!CONSTANT_CLASS_P (condition)
11480 && !TREE_SIDE_EFFECTS (condition))
11481 {
11482 /* Wrap it in a NOP_EXPR so that we can set the
11483 location of the condition. */
11484 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11485 condition);
11486 SET_EXPR_LOCATION (e, token->location);
11487 chain->safe_push (e);
11488 }
11489 }
11490 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11491 RID_IF))
11492 {
11493 /* This is if-else without subsequent if. Zap the
11494 condition chain; we would have already warned at
11495 this point. */
11496 delete chain;
11497 chain = NULL;
11498 }
11499 }
11500 begin_else_clause (statement);
11501 /* Parse the else-clause. */
11502 cp_parser_implicitly_scoped_statement (parser, NULL,
11503 guard_tinfo, chain);
11504
11505 finish_else_clause (statement);
11506
11507 /* If we are currently parsing a then-clause, then
11508 IF_P will not be NULL. We set it to true to
11509 indicate that this if statement has an else clause.
11510 This may trigger the Wparentheses warning below
11511 when we get back up to the parent if statement. */
11512 if (if_p != NULL)
11513 *if_p = true;
11514
11515 if (discard_else)
11516 {
11517 ELSE_CLAUSE (statement) = NULL_TREE;
11518 in_discarded_stmt = was_discarded;
11519 --c_inhibit_evaluation_warnings;
11520 }
11521 }
11522 else
11523 {
11524 /* This if statement does not have an else clause. If
11525 NESTED_IF is true, then the then-clause has an if
11526 statement which does have an else clause. We warn
11527 about the potential ambiguity. */
11528 if (nested_if)
11529 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11530 "suggest explicit braces to avoid ambiguous"
11531 " %<else%>");
11532 if (warn_duplicated_cond)
11533 {
11534 /* We don't need the condition chain anymore. */
11535 delete chain;
11536 chain = NULL;
11537 }
11538 }
11539
11540 /* Now we're all done with the if-statement. */
11541 finish_if_stmt (statement);
11542 }
11543 else
11544 {
11545 bool in_switch_statement_p;
11546 unsigned char in_statement;
11547
11548 /* Add the condition. */
11549 finish_switch_cond (condition, statement);
11550
11551 /* Parse the body of the switch-statement. */
11552 in_switch_statement_p = parser->in_switch_statement_p;
11553 in_statement = parser->in_statement;
11554 parser->in_switch_statement_p = true;
11555 parser->in_statement |= IN_SWITCH_STMT;
11556 cp_parser_implicitly_scoped_statement (parser, if_p,
11557 guard_tinfo);
11558 parser->in_switch_statement_p = in_switch_statement_p;
11559 parser->in_statement = in_statement;
11560
11561 /* Now we're all done with the switch-statement. */
11562 finish_switch_stmt (statement);
11563 }
11564
11565 return statement;
11566 }
11567 break;
11568
11569 default:
11570 cp_parser_error (parser, "expected selection-statement");
11571 return error_mark_node;
11572 }
11573 }
11574
11575 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11576 If we have seen at least one decl-specifier, and the next token
11577 is not a parenthesis, then we must be looking at a declaration.
11578 (After "int (" we might be looking at a functional cast.) */
11579
11580 static void
11581 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11582 bool any_specifiers_p)
11583 {
11584 if (any_specifiers_p
11585 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11586 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11587 && !cp_parser_error_occurred (parser))
11588 cp_parser_commit_to_tentative_parse (parser);
11589 }
11590
11591 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11592 The declarator shall not specify a function or an array. Returns
11593 TRUE if the declarator is valid, FALSE otherwise. */
11594
11595 static bool
11596 cp_parser_check_condition_declarator (cp_parser* parser,
11597 cp_declarator *declarator,
11598 location_t loc)
11599 {
11600 if (function_declarator_p (declarator)
11601 || declarator->kind == cdk_array)
11602 {
11603 if (declarator->kind == cdk_array)
11604 error_at (loc, "condition declares an array");
11605 else
11606 error_at (loc, "condition declares a function");
11607 if (parser->fully_implicit_function_template_p)
11608 abort_fully_implicit_template (parser);
11609 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11610 /*or_comma=*/false,
11611 /*consume_paren=*/false);
11612 return false;
11613 }
11614 else
11615 return true;
11616 }
11617
11618 /* Parse a condition.
11619
11620 condition:
11621 expression
11622 type-specifier-seq declarator = initializer-clause
11623 type-specifier-seq declarator braced-init-list
11624
11625 GNU Extension:
11626
11627 condition:
11628 type-specifier-seq declarator asm-specification [opt]
11629 attributes [opt] = assignment-expression
11630
11631 Returns the expression that should be tested. */
11632
11633 static tree
11634 cp_parser_condition (cp_parser* parser)
11635 {
11636 cp_decl_specifier_seq type_specifiers;
11637 const char *saved_message;
11638 int declares_class_or_enum;
11639
11640 /* Try the declaration first. */
11641 cp_parser_parse_tentatively (parser);
11642 /* New types are not allowed in the type-specifier-seq for a
11643 condition. */
11644 saved_message = parser->type_definition_forbidden_message;
11645 parser->type_definition_forbidden_message
11646 = G_("types may not be defined in conditions");
11647 /* Parse the type-specifier-seq. */
11648 cp_parser_decl_specifier_seq (parser,
11649 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11650 &type_specifiers,
11651 &declares_class_or_enum);
11652 /* Restore the saved message. */
11653 parser->type_definition_forbidden_message = saved_message;
11654
11655 cp_parser_maybe_commit_to_declaration (parser,
11656 type_specifiers.any_specifiers_p);
11657
11658 /* If all is well, we might be looking at a declaration. */
11659 if (!cp_parser_error_occurred (parser))
11660 {
11661 tree decl;
11662 tree asm_specification;
11663 tree attributes;
11664 cp_declarator *declarator;
11665 tree initializer = NULL_TREE;
11666 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11667
11668 /* Parse the declarator. */
11669 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11670 /*ctor_dtor_or_conv_p=*/NULL,
11671 /*parenthesized_p=*/NULL,
11672 /*member_p=*/false,
11673 /*friend_p=*/false);
11674 /* Parse the attributes. */
11675 attributes = cp_parser_attributes_opt (parser);
11676 /* Parse the asm-specification. */
11677 asm_specification = cp_parser_asm_specification_opt (parser);
11678 /* If the next token is not an `=' or '{', then we might still be
11679 looking at an expression. For example:
11680
11681 if (A(a).x)
11682
11683 looks like a decl-specifier-seq and a declarator -- but then
11684 there is no `=', so this is an expression. */
11685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11686 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11687 cp_parser_simulate_error (parser);
11688
11689 /* If we did see an `=' or '{', then we are looking at a declaration
11690 for sure. */
11691 if (cp_parser_parse_definitely (parser))
11692 {
11693 tree pushed_scope;
11694 bool non_constant_p;
11695 int flags = LOOKUP_ONLYCONVERTING;
11696
11697 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11698 return error_mark_node;
11699
11700 /* Create the declaration. */
11701 decl = start_decl (declarator, &type_specifiers,
11702 /*initialized_p=*/true,
11703 attributes, /*prefix_attributes=*/NULL_TREE,
11704 &pushed_scope);
11705
11706 /* Parse the initializer. */
11707 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11708 {
11709 initializer = cp_parser_braced_list (parser, &non_constant_p);
11710 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11711 flags = 0;
11712 }
11713 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11714 {
11715 /* Consume the `='. */
11716 cp_lexer_consume_token (parser->lexer);
11717 initializer = cp_parser_initializer_clause (parser,
11718 &non_constant_p);
11719 }
11720 else
11721 {
11722 cp_parser_error (parser, "expected initializer");
11723 initializer = error_mark_node;
11724 }
11725 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11726 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11727
11728 /* Process the initializer. */
11729 cp_finish_decl (decl,
11730 initializer, !non_constant_p,
11731 asm_specification,
11732 flags);
11733
11734 if (pushed_scope)
11735 pop_scope (pushed_scope);
11736
11737 return convert_from_reference (decl);
11738 }
11739 }
11740 /* If we didn't even get past the declarator successfully, we are
11741 definitely not looking at a declaration. */
11742 else
11743 cp_parser_abort_tentative_parse (parser);
11744
11745 /* Otherwise, we are looking at an expression. */
11746 return cp_parser_expression (parser);
11747 }
11748
11749 /* Parses a for-statement or range-for-statement until the closing ')',
11750 not included. */
11751
11752 static tree
11753 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11754 {
11755 tree init, scope, decl;
11756 bool is_range_for;
11757
11758 /* Begin the for-statement. */
11759 scope = begin_for_scope (&init);
11760
11761 /* Parse the initialization. */
11762 is_range_for = cp_parser_init_statement (parser, &decl);
11763
11764 if (is_range_for)
11765 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11766 else
11767 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11768 }
11769
11770 static tree
11771 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11772 unsigned short unroll)
11773 {
11774 /* Normal for loop */
11775 tree condition = NULL_TREE;
11776 tree expression = NULL_TREE;
11777 tree stmt;
11778
11779 stmt = begin_for_stmt (scope, init);
11780 /* The init-statement has already been parsed in
11781 cp_parser_init_statement, so no work is needed here. */
11782 finish_init_stmt (stmt);
11783
11784 /* If there's a condition, process it. */
11785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11786 condition = cp_parser_condition (parser);
11787 else if (ivdep)
11788 {
11789 cp_parser_error (parser, "missing loop condition in loop with "
11790 "%<GCC ivdep%> pragma");
11791 condition = error_mark_node;
11792 }
11793 else if (unroll)
11794 {
11795 cp_parser_error (parser, "missing loop condition in loop with "
11796 "%<GCC unroll%> pragma");
11797 condition = error_mark_node;
11798 }
11799 finish_for_cond (condition, stmt, ivdep, unroll);
11800 /* Look for the `;'. */
11801 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11802
11803 /* If there's an expression, process it. */
11804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11805 expression = cp_parser_expression (parser);
11806 finish_for_expr (expression, stmt);
11807
11808 return stmt;
11809 }
11810
11811 /* Tries to parse a range-based for-statement:
11812
11813 range-based-for:
11814 decl-specifier-seq declarator : expression
11815
11816 The decl-specifier-seq declarator and the `:' are already parsed by
11817 cp_parser_init_statement. If processing_template_decl it returns a
11818 newly created RANGE_FOR_STMT; if not, it is converted to a
11819 regular FOR_STMT. */
11820
11821 static tree
11822 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11823 bool ivdep, unsigned short unroll)
11824 {
11825 tree stmt, range_expr;
11826 auto_vec <cxx_binding *, 16> bindings;
11827 auto_vec <tree, 16> names;
11828 tree decomp_first_name = NULL_TREE;
11829 unsigned int decomp_cnt = 0;
11830
11831 /* Get the range declaration momentarily out of the way so that
11832 the range expression doesn't clash with it. */
11833 if (range_decl != error_mark_node)
11834 {
11835 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11836 {
11837 tree v = DECL_VALUE_EXPR (range_decl);
11838 /* For decomposition declaration get all of the corresponding
11839 declarations out of the way. */
11840 if (TREE_CODE (v) == ARRAY_REF
11841 && VAR_P (TREE_OPERAND (v, 0))
11842 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11843 {
11844 tree d = range_decl;
11845 range_decl = TREE_OPERAND (v, 0);
11846 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11847 decomp_first_name = d;
11848 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11849 {
11850 tree name = DECL_NAME (d);
11851 names.safe_push (name);
11852 bindings.safe_push (IDENTIFIER_BINDING (name));
11853 IDENTIFIER_BINDING (name)
11854 = IDENTIFIER_BINDING (name)->previous;
11855 }
11856 }
11857 }
11858 if (names.is_empty ())
11859 {
11860 tree name = DECL_NAME (range_decl);
11861 names.safe_push (name);
11862 bindings.safe_push (IDENTIFIER_BINDING (name));
11863 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11864 }
11865 }
11866
11867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11868 {
11869 bool expr_non_constant_p;
11870 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11871 }
11872 else
11873 range_expr = cp_parser_expression (parser);
11874
11875 /* Put the range declaration(s) back into scope. */
11876 for (unsigned int i = 0; i < names.length (); i++)
11877 {
11878 cxx_binding *binding = bindings[i];
11879 binding->previous = IDENTIFIER_BINDING (names[i]);
11880 IDENTIFIER_BINDING (names[i]) = binding;
11881 }
11882
11883 /* If in template, STMT is converted to a normal for-statement
11884 at instantiation. If not, it is done just ahead. */
11885 if (processing_template_decl)
11886 {
11887 if (check_for_bare_parameter_packs (range_expr))
11888 range_expr = error_mark_node;
11889 stmt = begin_range_for_stmt (scope, init);
11890 if (ivdep)
11891 RANGE_FOR_IVDEP (stmt) = 1;
11892 if (unroll)
11893 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11894 finish_range_for_decl (stmt, range_decl, range_expr);
11895 if (!type_dependent_expression_p (range_expr)
11896 /* do_auto_deduction doesn't mess with template init-lists. */
11897 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11898 do_range_for_auto_deduction (range_decl, range_expr);
11899 }
11900 else
11901 {
11902 stmt = begin_for_stmt (scope, init);
11903 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11904 decomp_first_name, decomp_cnt, ivdep,
11905 unroll);
11906 }
11907 return stmt;
11908 }
11909
11910 /* Subroutine of cp_convert_range_for: given the initializer expression,
11911 builds up the range temporary. */
11912
11913 static tree
11914 build_range_temp (tree range_expr)
11915 {
11916 tree range_type, range_temp;
11917
11918 /* Find out the type deduced by the declaration
11919 `auto &&__range = range_expr'. */
11920 range_type = cp_build_reference_type (make_auto (), true);
11921 range_type = do_auto_deduction (range_type, range_expr,
11922 type_uses_auto (range_type));
11923
11924 /* Create the __range variable. */
11925 range_temp = build_decl (input_location, VAR_DECL,
11926 get_identifier ("__for_range"), range_type);
11927 TREE_USED (range_temp) = 1;
11928 DECL_ARTIFICIAL (range_temp) = 1;
11929
11930 return range_temp;
11931 }
11932
11933 /* Used by cp_parser_range_for in template context: we aren't going to
11934 do a full conversion yet, but we still need to resolve auto in the
11935 type of the for-range-declaration if present. This is basically
11936 a shortcut version of cp_convert_range_for. */
11937
11938 static void
11939 do_range_for_auto_deduction (tree decl, tree range_expr)
11940 {
11941 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11942 if (auto_node)
11943 {
11944 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11945 range_temp = convert_from_reference (build_range_temp (range_expr));
11946 iter_type = (cp_parser_perform_range_for_lookup
11947 (range_temp, &begin_dummy, &end_dummy));
11948 if (iter_type)
11949 {
11950 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11951 iter_type);
11952 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11953 RO_UNARY_STAR,
11954 tf_warning_or_error);
11955 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11956 iter_decl, auto_node);
11957 }
11958 }
11959 }
11960
11961 /* Converts a range-based for-statement into a normal
11962 for-statement, as per the definition.
11963
11964 for (RANGE_DECL : RANGE_EXPR)
11965 BLOCK
11966
11967 should be equivalent to:
11968
11969 {
11970 auto &&__range = RANGE_EXPR;
11971 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11972 __begin != __end;
11973 ++__begin)
11974 {
11975 RANGE_DECL = *__begin;
11976 BLOCK
11977 }
11978 }
11979
11980 If RANGE_EXPR is an array:
11981 BEGIN_EXPR = __range
11982 END_EXPR = __range + ARRAY_SIZE(__range)
11983 Else if RANGE_EXPR has a member 'begin' or 'end':
11984 BEGIN_EXPR = __range.begin()
11985 END_EXPR = __range.end()
11986 Else:
11987 BEGIN_EXPR = begin(__range)
11988 END_EXPR = end(__range);
11989
11990 If __range has a member 'begin' but not 'end', or vice versa, we must
11991 still use the second alternative (it will surely fail, however).
11992 When calling begin()/end() in the third alternative we must use
11993 argument dependent lookup, but always considering 'std' as an associated
11994 namespace. */
11995
11996 tree
11997 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11998 tree decomp_first_name, unsigned int decomp_cnt,
11999 bool ivdep, unsigned short unroll)
12000 {
12001 tree begin, end;
12002 tree iter_type, begin_expr, end_expr;
12003 tree condition, expression;
12004
12005 range_expr = mark_lvalue_use (range_expr);
12006
12007 if (range_decl == error_mark_node || range_expr == error_mark_node)
12008 /* If an error happened previously do nothing or else a lot of
12009 unhelpful errors would be issued. */
12010 begin_expr = end_expr = iter_type = error_mark_node;
12011 else
12012 {
12013 tree range_temp;
12014
12015 if (VAR_P (range_expr)
12016 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12017 /* Can't bind a reference to an array of runtime bound. */
12018 range_temp = range_expr;
12019 else
12020 {
12021 range_temp = build_range_temp (range_expr);
12022 pushdecl (range_temp);
12023 cp_finish_decl (range_temp, range_expr,
12024 /*is_constant_init*/false, NULL_TREE,
12025 LOOKUP_ONLYCONVERTING);
12026 range_temp = convert_from_reference (range_temp);
12027 }
12028 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12029 &begin_expr, &end_expr);
12030 }
12031
12032 /* The new for initialization statement. */
12033 begin = build_decl (input_location, VAR_DECL,
12034 get_identifier ("__for_begin"), iter_type);
12035 TREE_USED (begin) = 1;
12036 DECL_ARTIFICIAL (begin) = 1;
12037 pushdecl (begin);
12038 cp_finish_decl (begin, begin_expr,
12039 /*is_constant_init*/false, NULL_TREE,
12040 LOOKUP_ONLYCONVERTING);
12041
12042 if (cxx_dialect >= cxx17)
12043 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12044 end = build_decl (input_location, VAR_DECL,
12045 get_identifier ("__for_end"), iter_type);
12046 TREE_USED (end) = 1;
12047 DECL_ARTIFICIAL (end) = 1;
12048 pushdecl (end);
12049 cp_finish_decl (end, end_expr,
12050 /*is_constant_init*/false, NULL_TREE,
12051 LOOKUP_ONLYCONVERTING);
12052
12053 finish_init_stmt (statement);
12054
12055 /* The new for condition. */
12056 condition = build_x_binary_op (input_location, NE_EXPR,
12057 begin, ERROR_MARK,
12058 end, ERROR_MARK,
12059 NULL, tf_warning_or_error);
12060 finish_for_cond (condition, statement, ivdep, unroll);
12061
12062 /* The new increment expression. */
12063 expression = finish_unary_op_expr (input_location,
12064 PREINCREMENT_EXPR, begin,
12065 tf_warning_or_error);
12066 finish_for_expr (expression, statement);
12067
12068 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12069 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12070
12071 /* The declaration is initialized with *__begin inside the loop body. */
12072 cp_finish_decl (range_decl,
12073 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12074 tf_warning_or_error),
12075 /*is_constant_init*/false, NULL_TREE,
12076 LOOKUP_ONLYCONVERTING);
12077 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12078 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12079
12080 return statement;
12081 }
12082
12083 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12084 We need to solve both at the same time because the method used
12085 depends on the existence of members begin or end.
12086 Returns the type deduced for the iterator expression. */
12087
12088 static tree
12089 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12090 {
12091 if (error_operand_p (range))
12092 {
12093 *begin = *end = error_mark_node;
12094 return error_mark_node;
12095 }
12096
12097 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12098 {
12099 error ("range-based %<for%> expression of type %qT "
12100 "has incomplete type", TREE_TYPE (range));
12101 *begin = *end = error_mark_node;
12102 return error_mark_node;
12103 }
12104 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12105 {
12106 /* If RANGE is an array, we will use pointer arithmetic. */
12107 *begin = decay_conversion (range, tf_warning_or_error);
12108 *end = build_binary_op (input_location, PLUS_EXPR,
12109 range,
12110 array_type_nelts_top (TREE_TYPE (range)),
12111 false);
12112 return TREE_TYPE (*begin);
12113 }
12114 else
12115 {
12116 /* If it is not an array, we must do a bit of magic. */
12117 tree id_begin, id_end;
12118 tree member_begin, member_end;
12119
12120 *begin = *end = error_mark_node;
12121
12122 id_begin = get_identifier ("begin");
12123 id_end = get_identifier ("end");
12124 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12125 /*protect=*/2, /*want_type=*/false,
12126 tf_warning_or_error);
12127 member_end = lookup_member (TREE_TYPE (range), id_end,
12128 /*protect=*/2, /*want_type=*/false,
12129 tf_warning_or_error);
12130
12131 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12132 {
12133 /* Use the member functions. */
12134 *begin = cp_parser_range_for_member_function (range, id_begin);
12135 *end = cp_parser_range_for_member_function (range, id_end);
12136 }
12137 else
12138 {
12139 /* Use global functions with ADL. */
12140 vec<tree, va_gc> *vec;
12141 vec = make_tree_vector ();
12142
12143 vec_safe_push (vec, range);
12144
12145 member_begin = perform_koenig_lookup (id_begin, vec,
12146 tf_warning_or_error);
12147 *begin = finish_call_expr (member_begin, &vec, false, true,
12148 tf_warning_or_error);
12149 member_end = perform_koenig_lookup (id_end, vec,
12150 tf_warning_or_error);
12151 *end = finish_call_expr (member_end, &vec, false, true,
12152 tf_warning_or_error);
12153
12154 release_tree_vector (vec);
12155 }
12156
12157 /* Last common checks. */
12158 if (*begin == error_mark_node || *end == error_mark_node)
12159 {
12160 /* If one of the expressions is an error do no more checks. */
12161 *begin = *end = error_mark_node;
12162 return error_mark_node;
12163 }
12164 else if (type_dependent_expression_p (*begin)
12165 || type_dependent_expression_p (*end))
12166 /* Can happen, when, eg, in a template context, Koenig lookup
12167 can't resolve begin/end (c++/58503). */
12168 return NULL_TREE;
12169 else
12170 {
12171 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12172 /* The unqualified type of the __begin and __end temporaries should
12173 be the same, as required by the multiple auto declaration. */
12174 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12175 {
12176 if (cxx_dialect >= cxx17
12177 && (build_x_binary_op (input_location, NE_EXPR,
12178 *begin, ERROR_MARK,
12179 *end, ERROR_MARK,
12180 NULL, tf_none)
12181 != error_mark_node))
12182 /* P0184R0 allows __begin and __end to have different types,
12183 but make sure they are comparable so we can give a better
12184 diagnostic. */;
12185 else
12186 error ("inconsistent begin/end types in range-based %<for%> "
12187 "statement: %qT and %qT",
12188 TREE_TYPE (*begin), TREE_TYPE (*end));
12189 }
12190 return iter_type;
12191 }
12192 }
12193 }
12194
12195 /* Helper function for cp_parser_perform_range_for_lookup.
12196 Builds a tree for RANGE.IDENTIFIER(). */
12197
12198 static tree
12199 cp_parser_range_for_member_function (tree range, tree identifier)
12200 {
12201 tree member, res;
12202 vec<tree, va_gc> *vec;
12203
12204 member = finish_class_member_access_expr (range, identifier,
12205 false, tf_warning_or_error);
12206 if (member == error_mark_node)
12207 return error_mark_node;
12208
12209 vec = make_tree_vector ();
12210 res = finish_call_expr (member, &vec,
12211 /*disallow_virtual=*/false,
12212 /*koenig_p=*/false,
12213 tf_warning_or_error);
12214 release_tree_vector (vec);
12215 return res;
12216 }
12217
12218 /* Parse an iteration-statement.
12219
12220 iteration-statement:
12221 while ( condition ) statement
12222 do statement while ( expression ) ;
12223 for ( init-statement condition [opt] ; expression [opt] )
12224 statement
12225
12226 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12227
12228 static tree
12229 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12230 unsigned short unroll)
12231 {
12232 cp_token *token;
12233 enum rid keyword;
12234 tree statement;
12235 unsigned char in_statement;
12236 token_indent_info guard_tinfo;
12237
12238 /* Peek at the next token. */
12239 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12240 if (!token)
12241 return error_mark_node;
12242
12243 guard_tinfo = get_token_indent_info (token);
12244
12245 /* Remember whether or not we are already within an iteration
12246 statement. */
12247 in_statement = parser->in_statement;
12248
12249 /* See what kind of keyword it is. */
12250 keyword = token->keyword;
12251 switch (keyword)
12252 {
12253 case RID_WHILE:
12254 {
12255 tree condition;
12256
12257 /* Begin the while-statement. */
12258 statement = begin_while_stmt ();
12259 /* Look for the `('. */
12260 matching_parens parens;
12261 parens.require_open (parser);
12262 /* Parse the condition. */
12263 condition = cp_parser_condition (parser);
12264 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12265 /* Look for the `)'. */
12266 parens.require_close (parser);
12267 /* Parse the dependent statement. */
12268 parser->in_statement = IN_ITERATION_STMT;
12269 bool prev = note_iteration_stmt_body_start ();
12270 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12271 note_iteration_stmt_body_end (prev);
12272 parser->in_statement = in_statement;
12273 /* We're done with the while-statement. */
12274 finish_while_stmt (statement);
12275 }
12276 break;
12277
12278 case RID_DO:
12279 {
12280 tree expression;
12281
12282 /* Begin the do-statement. */
12283 statement = begin_do_stmt ();
12284 /* Parse the body of the do-statement. */
12285 parser->in_statement = IN_ITERATION_STMT;
12286 bool prev = note_iteration_stmt_body_start ();
12287 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12288 note_iteration_stmt_body_end (prev);
12289 parser->in_statement = in_statement;
12290 finish_do_body (statement);
12291 /* Look for the `while' keyword. */
12292 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12293 /* Look for the `('. */
12294 matching_parens parens;
12295 parens.require_open (parser);
12296 /* Parse the expression. */
12297 expression = cp_parser_expression (parser);
12298 /* We're done with the do-statement. */
12299 finish_do_stmt (expression, statement, ivdep, unroll);
12300 /* Look for the `)'. */
12301 parens.require_close (parser);
12302 /* Look for the `;'. */
12303 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12304 }
12305 break;
12306
12307 case RID_FOR:
12308 {
12309 /* Look for the `('. */
12310 matching_parens parens;
12311 parens.require_open (parser);
12312
12313 statement = cp_parser_for (parser, ivdep, unroll);
12314
12315 /* Look for the `)'. */
12316 parens.require_close (parser);
12317
12318 /* Parse the body of the for-statement. */
12319 parser->in_statement = IN_ITERATION_STMT;
12320 bool prev = note_iteration_stmt_body_start ();
12321 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12322 note_iteration_stmt_body_end (prev);
12323 parser->in_statement = in_statement;
12324
12325 /* We're done with the for-statement. */
12326 finish_for_stmt (statement);
12327 }
12328 break;
12329
12330 default:
12331 cp_parser_error (parser, "expected iteration-statement");
12332 statement = error_mark_node;
12333 break;
12334 }
12335
12336 return statement;
12337 }
12338
12339 /* Parse a init-statement or the declarator of a range-based-for.
12340 Returns true if a range-based-for declaration is seen.
12341
12342 init-statement:
12343 expression-statement
12344 simple-declaration */
12345
12346 static bool
12347 cp_parser_init_statement (cp_parser *parser, tree *decl)
12348 {
12349 /* If the next token is a `;', then we have an empty
12350 expression-statement. Grammatically, this is also a
12351 simple-declaration, but an invalid one, because it does not
12352 declare anything. Therefore, if we did not handle this case
12353 specially, we would issue an error message about an invalid
12354 declaration. */
12355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12356 {
12357 bool is_range_for = false;
12358 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12359
12360 /* Try to parse the init-statement. */
12361 if (cp_parser_range_based_for_with_init_p (parser))
12362 {
12363 tree dummy;
12364 cp_parser_parse_tentatively (parser);
12365 /* Parse the declaration. */
12366 cp_parser_simple_declaration (parser,
12367 /*function_definition_allowed_p=*/false,
12368 &dummy);
12369 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12370 if (!cp_parser_parse_definitely (parser))
12371 /* That didn't work, try to parse it as an expression-statement. */
12372 cp_parser_expression_statement (parser, NULL_TREE);
12373
12374 if (cxx_dialect < cxx2a)
12375 {
12376 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12377 "range-based %<for%> loops with initializer only "
12378 "available with -std=c++2a or -std=gnu++2a");
12379 *decl = error_mark_node;
12380 }
12381 }
12382
12383 /* A colon is used in range-based for. */
12384 parser->colon_corrects_to_scope_p = false;
12385
12386 /* We're going to speculatively look for a declaration, falling back
12387 to an expression, if necessary. */
12388 cp_parser_parse_tentatively (parser);
12389 /* Parse the declaration. */
12390 cp_parser_simple_declaration (parser,
12391 /*function_definition_allowed_p=*/false,
12392 decl);
12393 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12394 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12395 {
12396 /* It is a range-for, consume the ':'. */
12397 cp_lexer_consume_token (parser->lexer);
12398 is_range_for = true;
12399 if (cxx_dialect < cxx11)
12400 {
12401 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12402 "range-based %<for%> loops only available with "
12403 "-std=c++11 or -std=gnu++11");
12404 *decl = error_mark_node;
12405 }
12406 }
12407 else
12408 /* The ';' is not consumed yet because we told
12409 cp_parser_simple_declaration not to. */
12410 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12411
12412 if (cp_parser_parse_definitely (parser))
12413 return is_range_for;
12414 /* If the tentative parse failed, then we shall need to look for an
12415 expression-statement. */
12416 }
12417 /* If we are here, it is an expression-statement. */
12418 cp_parser_expression_statement (parser, NULL_TREE);
12419 return false;
12420 }
12421
12422 /* Parse a jump-statement.
12423
12424 jump-statement:
12425 break ;
12426 continue ;
12427 return expression [opt] ;
12428 return braced-init-list ;
12429 goto identifier ;
12430
12431 GNU extension:
12432
12433 jump-statement:
12434 goto * expression ;
12435
12436 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12437
12438 static tree
12439 cp_parser_jump_statement (cp_parser* parser)
12440 {
12441 tree statement = error_mark_node;
12442 cp_token *token;
12443 enum rid keyword;
12444 unsigned char in_statement;
12445
12446 /* Peek at the next token. */
12447 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12448 if (!token)
12449 return error_mark_node;
12450
12451 /* See what kind of keyword it is. */
12452 keyword = token->keyword;
12453 switch (keyword)
12454 {
12455 case RID_BREAK:
12456 in_statement = parser->in_statement & ~IN_IF_STMT;
12457 switch (in_statement)
12458 {
12459 case 0:
12460 error_at (token->location, "break statement not within loop or switch");
12461 break;
12462 default:
12463 gcc_assert ((in_statement & IN_SWITCH_STMT)
12464 || in_statement == IN_ITERATION_STMT);
12465 statement = finish_break_stmt ();
12466 if (in_statement == IN_ITERATION_STMT)
12467 break_maybe_infinite_loop ();
12468 break;
12469 case IN_OMP_BLOCK:
12470 error_at (token->location, "invalid exit from OpenMP structured block");
12471 break;
12472 case IN_OMP_FOR:
12473 error_at (token->location, "break statement used with OpenMP for loop");
12474 break;
12475 }
12476 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12477 break;
12478
12479 case RID_CONTINUE:
12480 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12481 {
12482 case 0:
12483 error_at (token->location, "continue statement not within a loop");
12484 break;
12485 /* Fall through. */
12486 case IN_ITERATION_STMT:
12487 case IN_OMP_FOR:
12488 statement = finish_continue_stmt ();
12489 break;
12490 case IN_OMP_BLOCK:
12491 error_at (token->location, "invalid exit from OpenMP structured block");
12492 break;
12493 default:
12494 gcc_unreachable ();
12495 }
12496 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12497 break;
12498
12499 case RID_RETURN:
12500 {
12501 tree expr;
12502 bool expr_non_constant_p;
12503
12504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12505 {
12506 cp_lexer_set_source_position (parser->lexer);
12507 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12508 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12509 }
12510 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12511 expr = cp_parser_expression (parser);
12512 else
12513 /* If the next token is a `;', then there is no
12514 expression. */
12515 expr = NULL_TREE;
12516 /* Build the return-statement. */
12517 if (current_function_auto_return_pattern && in_discarded_stmt)
12518 /* Don't deduce from a discarded return statement. */;
12519 else
12520 statement = finish_return_stmt (expr);
12521 /* Look for the final `;'. */
12522 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12523 }
12524 break;
12525
12526 case RID_GOTO:
12527 if (parser->in_function_body
12528 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12529 {
12530 error ("%<goto%> in %<constexpr%> function");
12531 cp_function_chain->invalid_constexpr = true;
12532 }
12533
12534 /* Create the goto-statement. */
12535 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12536 {
12537 /* Issue a warning about this use of a GNU extension. */
12538 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12539 /* Consume the '*' token. */
12540 cp_lexer_consume_token (parser->lexer);
12541 /* Parse the dependent expression. */
12542 finish_goto_stmt (cp_parser_expression (parser));
12543 }
12544 else
12545 finish_goto_stmt (cp_parser_identifier (parser));
12546 /* Look for the final `;'. */
12547 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12548 break;
12549
12550 default:
12551 cp_parser_error (parser, "expected jump-statement");
12552 break;
12553 }
12554
12555 return statement;
12556 }
12557
12558 /* Parse a declaration-statement.
12559
12560 declaration-statement:
12561 block-declaration */
12562
12563 static void
12564 cp_parser_declaration_statement (cp_parser* parser)
12565 {
12566 void *p;
12567
12568 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12569 p = obstack_alloc (&declarator_obstack, 0);
12570
12571 /* Parse the block-declaration. */
12572 cp_parser_block_declaration (parser, /*statement_p=*/true);
12573
12574 /* Free any declarators allocated. */
12575 obstack_free (&declarator_obstack, p);
12576 }
12577
12578 /* Some dependent statements (like `if (cond) statement'), are
12579 implicitly in their own scope. In other words, if the statement is
12580 a single statement (as opposed to a compound-statement), it is
12581 none-the-less treated as if it were enclosed in braces. Any
12582 declarations appearing in the dependent statement are out of scope
12583 after control passes that point. This function parses a statement,
12584 but ensures that is in its own scope, even if it is not a
12585 compound-statement.
12586
12587 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12588 is a (possibly labeled) if statement which is not enclosed in
12589 braces and has an else clause. This is used to implement
12590 -Wparentheses.
12591
12592 CHAIN is a vector of if-else-if conditions. This is used to implement
12593 -Wduplicated-cond.
12594
12595 Returns the new statement. */
12596
12597 static tree
12598 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12599 const token_indent_info &guard_tinfo,
12600 vec<tree> *chain)
12601 {
12602 tree statement;
12603 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12604 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12605 token_indent_info body_tinfo
12606 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12607
12608 if (if_p != NULL)
12609 *if_p = false;
12610
12611 /* Mark if () ; with a special NOP_EXPR. */
12612 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12613 {
12614 cp_lexer_consume_token (parser->lexer);
12615 statement = add_stmt (build_empty_stmt (body_loc));
12616
12617 if (guard_tinfo.keyword == RID_IF
12618 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12619 warning_at (body_loc, OPT_Wempty_body,
12620 "suggest braces around empty body in an %<if%> statement");
12621 else if (guard_tinfo.keyword == RID_ELSE)
12622 warning_at (body_loc, OPT_Wempty_body,
12623 "suggest braces around empty body in an %<else%> statement");
12624 }
12625 /* if a compound is opened, we simply parse the statement directly. */
12626 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12627 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12628 /* If the token is not a `{', then we must take special action. */
12629 else
12630 {
12631 /* Create a compound-statement. */
12632 statement = begin_compound_stmt (0);
12633 /* Parse the dependent-statement. */
12634 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12635 &body_loc_after_labels);
12636 /* Finish the dummy compound-statement. */
12637 finish_compound_stmt (statement);
12638 }
12639
12640 token_indent_info next_tinfo
12641 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12642 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12643
12644 if (body_loc_after_labels != UNKNOWN_LOCATION
12645 && next_tinfo.type != CPP_SEMICOLON)
12646 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12647 guard_tinfo.location, guard_tinfo.keyword);
12648
12649 /* Return the statement. */
12650 return statement;
12651 }
12652
12653 /* For some dependent statements (like `while (cond) statement'), we
12654 have already created a scope. Therefore, even if the dependent
12655 statement is a compound-statement, we do not want to create another
12656 scope. */
12657
12658 static void
12659 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12660 const token_indent_info &guard_tinfo)
12661 {
12662 /* If the token is a `{', then we must take special action. */
12663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12664 {
12665 token_indent_info body_tinfo
12666 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12667 location_t loc_after_labels = UNKNOWN_LOCATION;
12668
12669 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12670 &loc_after_labels);
12671 token_indent_info next_tinfo
12672 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12673 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12674
12675 if (loc_after_labels != UNKNOWN_LOCATION
12676 && next_tinfo.type != CPP_SEMICOLON)
12677 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12678 guard_tinfo.location,
12679 guard_tinfo.keyword);
12680 }
12681 else
12682 {
12683 /* Avoid calling cp_parser_compound_statement, so that we
12684 don't create a new scope. Do everything else by hand. */
12685 matching_braces braces;
12686 braces.require_open (parser);
12687 /* If the next keyword is `__label__' we have a label declaration. */
12688 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12689 cp_parser_label_declaration (parser);
12690 /* Parse an (optional) statement-seq. */
12691 cp_parser_statement_seq_opt (parser, NULL_TREE);
12692 braces.require_close (parser);
12693 }
12694 }
12695
12696 /* Declarations [gram.dcl.dcl] */
12697
12698 /* Parse an optional declaration-sequence.
12699
12700 declaration-seq:
12701 declaration
12702 declaration-seq declaration */
12703
12704 static void
12705 cp_parser_declaration_seq_opt (cp_parser* parser)
12706 {
12707 while (true)
12708 {
12709 cp_token *token;
12710
12711 token = cp_lexer_peek_token (parser->lexer);
12712
12713 if (token->type == CPP_CLOSE_BRACE
12714 || token->type == CPP_EOF
12715 || token->type == CPP_PRAGMA_EOL)
12716 break;
12717
12718 if (token->type == CPP_SEMICOLON)
12719 {
12720 /* A declaration consisting of a single semicolon is
12721 invalid. Allow it unless we're being pedantic. */
12722 cp_lexer_consume_token (parser->lexer);
12723 if (!in_system_header_at (input_location))
12724 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12725 continue;
12726 }
12727
12728 /* If we're entering or exiting a region that's implicitly
12729 extern "C", modify the lang context appropriately. */
12730 if (!parser->implicit_extern_c && token->implicit_extern_c)
12731 {
12732 push_lang_context (lang_name_c);
12733 parser->implicit_extern_c = true;
12734 }
12735 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12736 {
12737 pop_lang_context ();
12738 parser->implicit_extern_c = false;
12739 }
12740
12741 if (token->type == CPP_PRAGMA)
12742 {
12743 /* A top-level declaration can consist solely of a #pragma.
12744 A nested declaration cannot, so this is done here and not
12745 in cp_parser_declaration. (A #pragma at block scope is
12746 handled in cp_parser_statement.) */
12747 cp_parser_pragma (parser, pragma_external, NULL);
12748 continue;
12749 }
12750
12751 /* Parse the declaration itself. */
12752 cp_parser_declaration (parser);
12753 }
12754 }
12755
12756 /* Parse a declaration.
12757
12758 declaration:
12759 block-declaration
12760 function-definition
12761 template-declaration
12762 explicit-instantiation
12763 explicit-specialization
12764 linkage-specification
12765 namespace-definition
12766
12767 C++17:
12768 deduction-guide
12769
12770 GNU extension:
12771
12772 declaration:
12773 __extension__ declaration */
12774
12775 static void
12776 cp_parser_declaration (cp_parser* parser)
12777 {
12778 cp_token token1;
12779 cp_token token2;
12780 int saved_pedantic;
12781 void *p;
12782 tree attributes = NULL_TREE;
12783
12784 /* Check for the `__extension__' keyword. */
12785 if (cp_parser_extension_opt (parser, &saved_pedantic))
12786 {
12787 /* Parse the qualified declaration. */
12788 cp_parser_declaration (parser);
12789 /* Restore the PEDANTIC flag. */
12790 pedantic = saved_pedantic;
12791
12792 return;
12793 }
12794
12795 /* Try to figure out what kind of declaration is present. */
12796 token1 = *cp_lexer_peek_token (parser->lexer);
12797
12798 if (token1.type != CPP_EOF)
12799 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12800 else
12801 {
12802 token2.type = CPP_EOF;
12803 token2.keyword = RID_MAX;
12804 }
12805
12806 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12807 p = obstack_alloc (&declarator_obstack, 0);
12808
12809 /* If the next token is `extern' and the following token is a string
12810 literal, then we have a linkage specification. */
12811 if (token1.keyword == RID_EXTERN
12812 && cp_parser_is_pure_string_literal (&token2))
12813 cp_parser_linkage_specification (parser);
12814 /* If the next token is `template', then we have either a template
12815 declaration, an explicit instantiation, or an explicit
12816 specialization. */
12817 else if (token1.keyword == RID_TEMPLATE)
12818 {
12819 /* `template <>' indicates a template specialization. */
12820 if (token2.type == CPP_LESS
12821 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12822 cp_parser_explicit_specialization (parser);
12823 /* `template <' indicates a template declaration. */
12824 else if (token2.type == CPP_LESS)
12825 cp_parser_template_declaration (parser, /*member_p=*/false);
12826 /* Anything else must be an explicit instantiation. */
12827 else
12828 cp_parser_explicit_instantiation (parser);
12829 }
12830 /* If the next token is `export', then we have a template
12831 declaration. */
12832 else if (token1.keyword == RID_EXPORT)
12833 cp_parser_template_declaration (parser, /*member_p=*/false);
12834 /* If the next token is `extern', 'static' or 'inline' and the one
12835 after that is `template', we have a GNU extended explicit
12836 instantiation directive. */
12837 else if (cp_parser_allow_gnu_extensions_p (parser)
12838 && (token1.keyword == RID_EXTERN
12839 || token1.keyword == RID_STATIC
12840 || token1.keyword == RID_INLINE)
12841 && token2.keyword == RID_TEMPLATE)
12842 cp_parser_explicit_instantiation (parser);
12843 /* If the next token is `namespace', check for a named or unnamed
12844 namespace definition. */
12845 else if (token1.keyword == RID_NAMESPACE
12846 && (/* A named namespace definition. */
12847 (token2.type == CPP_NAME
12848 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12849 != CPP_EQ))
12850 || (token2.type == CPP_OPEN_SQUARE
12851 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12852 == CPP_OPEN_SQUARE)
12853 /* An unnamed namespace definition. */
12854 || token2.type == CPP_OPEN_BRACE
12855 || token2.keyword == RID_ATTRIBUTE))
12856 cp_parser_namespace_definition (parser);
12857 /* An inline (associated) namespace definition. */
12858 else if (token1.keyword == RID_INLINE
12859 && token2.keyword == RID_NAMESPACE)
12860 cp_parser_namespace_definition (parser);
12861 /* Objective-C++ declaration/definition. */
12862 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12863 cp_parser_objc_declaration (parser, NULL_TREE);
12864 else if (c_dialect_objc ()
12865 && token1.keyword == RID_ATTRIBUTE
12866 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12867 cp_parser_objc_declaration (parser, attributes);
12868 /* At this point we may have a template declared by a concept
12869 introduction. */
12870 else if (flag_concepts
12871 && cp_parser_template_declaration_after_export (parser,
12872 /*member_p=*/false))
12873 /* We did. */;
12874 else
12875 /* Try to parse a block-declaration, or a function-definition. */
12876 cp_parser_block_declaration (parser, /*statement_p=*/false);
12877
12878 /* Free any declarators allocated. */
12879 obstack_free (&declarator_obstack, p);
12880 }
12881
12882 /* Parse a block-declaration.
12883
12884 block-declaration:
12885 simple-declaration
12886 asm-definition
12887 namespace-alias-definition
12888 using-declaration
12889 using-directive
12890
12891 GNU Extension:
12892
12893 block-declaration:
12894 __extension__ block-declaration
12895
12896 C++0x Extension:
12897
12898 block-declaration:
12899 static_assert-declaration
12900
12901 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12902 part of a declaration-statement. */
12903
12904 static void
12905 cp_parser_block_declaration (cp_parser *parser,
12906 bool statement_p)
12907 {
12908 cp_token *token1;
12909 int saved_pedantic;
12910
12911 /* Check for the `__extension__' keyword. */
12912 if (cp_parser_extension_opt (parser, &saved_pedantic))
12913 {
12914 /* Parse the qualified declaration. */
12915 cp_parser_block_declaration (parser, statement_p);
12916 /* Restore the PEDANTIC flag. */
12917 pedantic = saved_pedantic;
12918
12919 return;
12920 }
12921
12922 /* Peek at the next token to figure out which kind of declaration is
12923 present. */
12924 token1 = cp_lexer_peek_token (parser->lexer);
12925
12926 /* If the next keyword is `asm', we have an asm-definition. */
12927 if (token1->keyword == RID_ASM)
12928 {
12929 if (statement_p)
12930 cp_parser_commit_to_tentative_parse (parser);
12931 cp_parser_asm_definition (parser);
12932 }
12933 /* If the next keyword is `namespace', we have a
12934 namespace-alias-definition. */
12935 else if (token1->keyword == RID_NAMESPACE)
12936 cp_parser_namespace_alias_definition (parser);
12937 /* If the next keyword is `using', we have a
12938 using-declaration, a using-directive, or an alias-declaration. */
12939 else if (token1->keyword == RID_USING)
12940 {
12941 cp_token *token2;
12942
12943 if (statement_p)
12944 cp_parser_commit_to_tentative_parse (parser);
12945 /* If the token after `using' is `namespace', then we have a
12946 using-directive. */
12947 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12948 if (token2->keyword == RID_NAMESPACE)
12949 cp_parser_using_directive (parser);
12950 /* If the second token after 'using' is '=', then we have an
12951 alias-declaration. */
12952 else if (cxx_dialect >= cxx11
12953 && token2->type == CPP_NAME
12954 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12955 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12956 cp_parser_alias_declaration (parser);
12957 /* Otherwise, it's a using-declaration. */
12958 else
12959 cp_parser_using_declaration (parser,
12960 /*access_declaration_p=*/false);
12961 }
12962 /* If the next keyword is `__label__' we have a misplaced label
12963 declaration. */
12964 else if (token1->keyword == RID_LABEL)
12965 {
12966 cp_lexer_consume_token (parser->lexer);
12967 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12968 cp_parser_skip_to_end_of_statement (parser);
12969 /* If the next token is now a `;', consume it. */
12970 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12971 cp_lexer_consume_token (parser->lexer);
12972 }
12973 /* If the next token is `static_assert' we have a static assertion. */
12974 else if (token1->keyword == RID_STATIC_ASSERT)
12975 cp_parser_static_assert (parser, /*member_p=*/false);
12976 /* Anything else must be a simple-declaration. */
12977 else
12978 cp_parser_simple_declaration (parser, !statement_p,
12979 /*maybe_range_for_decl*/NULL);
12980 }
12981
12982 /* Parse a simple-declaration.
12983
12984 simple-declaration:
12985 decl-specifier-seq [opt] init-declarator-list [opt] ;
12986 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12987 brace-or-equal-initializer ;
12988
12989 init-declarator-list:
12990 init-declarator
12991 init-declarator-list , init-declarator
12992
12993 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12994 function-definition as a simple-declaration.
12995
12996 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12997 parsed declaration if it is an uninitialized single declarator not followed
12998 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12999 if present, will not be consumed. */
13000
13001 static void
13002 cp_parser_simple_declaration (cp_parser* parser,
13003 bool function_definition_allowed_p,
13004 tree *maybe_range_for_decl)
13005 {
13006 cp_decl_specifier_seq decl_specifiers;
13007 int declares_class_or_enum;
13008 bool saw_declarator;
13009 location_t comma_loc = UNKNOWN_LOCATION;
13010 location_t init_loc = UNKNOWN_LOCATION;
13011
13012 if (maybe_range_for_decl)
13013 *maybe_range_for_decl = NULL_TREE;
13014
13015 /* Defer access checks until we know what is being declared; the
13016 checks for names appearing in the decl-specifier-seq should be
13017 done as if we were in the scope of the thing being declared. */
13018 push_deferring_access_checks (dk_deferred);
13019
13020 /* Parse the decl-specifier-seq. We have to keep track of whether
13021 or not the decl-specifier-seq declares a named class or
13022 enumeration type, since that is the only case in which the
13023 init-declarator-list is allowed to be empty.
13024
13025 [dcl.dcl]
13026
13027 In a simple-declaration, the optional init-declarator-list can be
13028 omitted only when declaring a class or enumeration, that is when
13029 the decl-specifier-seq contains either a class-specifier, an
13030 elaborated-type-specifier, or an enum-specifier. */
13031 cp_parser_decl_specifier_seq (parser,
13032 CP_PARSER_FLAGS_OPTIONAL,
13033 &decl_specifiers,
13034 &declares_class_or_enum);
13035 /* We no longer need to defer access checks. */
13036 stop_deferring_access_checks ();
13037
13038 /* In a block scope, a valid declaration must always have a
13039 decl-specifier-seq. By not trying to parse declarators, we can
13040 resolve the declaration/expression ambiguity more quickly. */
13041 if (!function_definition_allowed_p
13042 && !decl_specifiers.any_specifiers_p)
13043 {
13044 cp_parser_error (parser, "expected declaration");
13045 goto done;
13046 }
13047
13048 /* If the next two tokens are both identifiers, the code is
13049 erroneous. The usual cause of this situation is code like:
13050
13051 T t;
13052
13053 where "T" should name a type -- but does not. */
13054 if (!decl_specifiers.any_type_specifiers_p
13055 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13056 {
13057 /* If parsing tentatively, we should commit; we really are
13058 looking at a declaration. */
13059 cp_parser_commit_to_tentative_parse (parser);
13060 /* Give up. */
13061 goto done;
13062 }
13063
13064 cp_parser_maybe_commit_to_declaration (parser,
13065 decl_specifiers.any_specifiers_p);
13066
13067 /* Look for C++17 decomposition declaration. */
13068 for (size_t n = 1; ; n++)
13069 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13070 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13071 continue;
13072 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13073 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13074 && decl_specifiers.any_specifiers_p)
13075 {
13076 tree decl
13077 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13078 maybe_range_for_decl,
13079 &init_loc);
13080
13081 /* The next token should be either a `,' or a `;'. */
13082 cp_token *token = cp_lexer_peek_token (parser->lexer);
13083 /* If it's a `;', we are done. */
13084 if (token->type == CPP_SEMICOLON)
13085 goto finish;
13086 else if (maybe_range_for_decl)
13087 {
13088 if (*maybe_range_for_decl == NULL_TREE)
13089 *maybe_range_for_decl = error_mark_node;
13090 goto finish;
13091 }
13092 /* Anything else is an error. */
13093 else
13094 {
13095 /* If we have already issued an error message we don't need
13096 to issue another one. */
13097 if ((decl != error_mark_node
13098 && DECL_INITIAL (decl) != error_mark_node)
13099 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13100 cp_parser_error (parser, "expected %<,%> or %<;%>");
13101 /* Skip tokens until we reach the end of the statement. */
13102 cp_parser_skip_to_end_of_statement (parser);
13103 /* If the next token is now a `;', consume it. */
13104 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13105 cp_lexer_consume_token (parser->lexer);
13106 goto done;
13107 }
13108 }
13109 else
13110 break;
13111
13112 tree last_type;
13113 bool auto_specifier_p;
13114 /* NULL_TREE if both variable and function declaration are allowed,
13115 error_mark_node if function declaration are not allowed and
13116 a FUNCTION_DECL that should be diagnosed if it is followed by
13117 variable declarations. */
13118 tree auto_function_declaration;
13119
13120 last_type = NULL_TREE;
13121 auto_specifier_p
13122 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13123 auto_function_declaration = NULL_TREE;
13124
13125 /* Keep going until we hit the `;' at the end of the simple
13126 declaration. */
13127 saw_declarator = false;
13128 while (cp_lexer_next_token_is_not (parser->lexer,
13129 CPP_SEMICOLON))
13130 {
13131 cp_token *token;
13132 bool function_definition_p;
13133 tree decl;
13134 tree auto_result = NULL_TREE;
13135
13136 if (saw_declarator)
13137 {
13138 /* If we are processing next declarator, comma is expected */
13139 token = cp_lexer_peek_token (parser->lexer);
13140 gcc_assert (token->type == CPP_COMMA);
13141 cp_lexer_consume_token (parser->lexer);
13142 if (maybe_range_for_decl)
13143 {
13144 *maybe_range_for_decl = error_mark_node;
13145 if (comma_loc == UNKNOWN_LOCATION)
13146 comma_loc = token->location;
13147 }
13148 }
13149 else
13150 saw_declarator = true;
13151
13152 /* Parse the init-declarator. */
13153 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13154 /*checks=*/NULL,
13155 function_definition_allowed_p,
13156 /*member_p=*/false,
13157 declares_class_or_enum,
13158 &function_definition_p,
13159 maybe_range_for_decl,
13160 &init_loc,
13161 &auto_result);
13162 /* If an error occurred while parsing tentatively, exit quickly.
13163 (That usually happens when in the body of a function; each
13164 statement is treated as a declaration-statement until proven
13165 otherwise.) */
13166 if (cp_parser_error_occurred (parser))
13167 goto done;
13168
13169 if (auto_specifier_p && cxx_dialect >= cxx14)
13170 {
13171 /* If the init-declarator-list contains more than one
13172 init-declarator, they shall all form declarations of
13173 variables. */
13174 if (auto_function_declaration == NULL_TREE)
13175 auto_function_declaration
13176 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13177 else if (TREE_CODE (decl) == FUNCTION_DECL
13178 || auto_function_declaration != error_mark_node)
13179 {
13180 error_at (decl_specifiers.locations[ds_type_spec],
13181 "non-variable %qD in declaration with more than one "
13182 "declarator with placeholder type",
13183 TREE_CODE (decl) == FUNCTION_DECL
13184 ? decl : auto_function_declaration);
13185 auto_function_declaration = error_mark_node;
13186 }
13187 }
13188
13189 if (auto_result
13190 && (!processing_template_decl || !type_uses_auto (auto_result)))
13191 {
13192 if (last_type
13193 && last_type != error_mark_node
13194 && !same_type_p (auto_result, last_type))
13195 {
13196 /* If the list of declarators contains more than one declarator,
13197 the type of each declared variable is determined as described
13198 above. If the type deduced for the template parameter U is not
13199 the same in each deduction, the program is ill-formed. */
13200 error_at (decl_specifiers.locations[ds_type_spec],
13201 "inconsistent deduction for %qT: %qT and then %qT",
13202 decl_specifiers.type, last_type, auto_result);
13203 last_type = error_mark_node;
13204 }
13205 else
13206 last_type = auto_result;
13207 }
13208
13209 /* Handle function definitions specially. */
13210 if (function_definition_p)
13211 {
13212 /* If the next token is a `,', then we are probably
13213 processing something like:
13214
13215 void f() {}, *p;
13216
13217 which is erroneous. */
13218 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13219 {
13220 cp_token *token = cp_lexer_peek_token (parser->lexer);
13221 error_at (token->location,
13222 "mixing"
13223 " declarations and function-definitions is forbidden");
13224 }
13225 /* Otherwise, we're done with the list of declarators. */
13226 else
13227 {
13228 pop_deferring_access_checks ();
13229 return;
13230 }
13231 }
13232 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13233 *maybe_range_for_decl = decl;
13234 /* The next token should be either a `,' or a `;'. */
13235 token = cp_lexer_peek_token (parser->lexer);
13236 /* If it's a `,', there are more declarators to come. */
13237 if (token->type == CPP_COMMA)
13238 /* will be consumed next time around */;
13239 /* If it's a `;', we are done. */
13240 else if (token->type == CPP_SEMICOLON)
13241 break;
13242 else if (maybe_range_for_decl)
13243 {
13244 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13245 permerror (decl_specifiers.locations[ds_type_spec],
13246 "types may not be defined in a for-range-declaration");
13247 break;
13248 }
13249 /* Anything else is an error. */
13250 else
13251 {
13252 /* If we have already issued an error message we don't need
13253 to issue another one. */
13254 if ((decl != error_mark_node
13255 && DECL_INITIAL (decl) != error_mark_node)
13256 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13257 cp_parser_error (parser, "expected %<,%> or %<;%>");
13258 /* Skip tokens until we reach the end of the statement. */
13259 cp_parser_skip_to_end_of_statement (parser);
13260 /* If the next token is now a `;', consume it. */
13261 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13262 cp_lexer_consume_token (parser->lexer);
13263 goto done;
13264 }
13265 /* After the first time around, a function-definition is not
13266 allowed -- even if it was OK at first. For example:
13267
13268 int i, f() {}
13269
13270 is not valid. */
13271 function_definition_allowed_p = false;
13272 }
13273
13274 /* Issue an error message if no declarators are present, and the
13275 decl-specifier-seq does not itself declare a class or
13276 enumeration: [dcl.dcl]/3. */
13277 if (!saw_declarator)
13278 {
13279 if (cp_parser_declares_only_class_p (parser))
13280 {
13281 if (!declares_class_or_enum
13282 && decl_specifiers.type
13283 && OVERLOAD_TYPE_P (decl_specifiers.type))
13284 /* Ensure an error is issued anyway when finish_decltype_type,
13285 called via cp_parser_decl_specifier_seq, returns a class or
13286 an enumeration (c++/51786). */
13287 decl_specifiers.type = NULL_TREE;
13288 shadow_tag (&decl_specifiers);
13289 }
13290 /* Perform any deferred access checks. */
13291 perform_deferred_access_checks (tf_warning_or_error);
13292 }
13293
13294 /* Consume the `;'. */
13295 finish:
13296 if (!maybe_range_for_decl)
13297 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13298 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13299 {
13300 if (init_loc != UNKNOWN_LOCATION)
13301 error_at (init_loc, "initializer in range-based %<for%> loop");
13302 if (comma_loc != UNKNOWN_LOCATION)
13303 error_at (comma_loc,
13304 "multiple declarations in range-based %<for%> loop");
13305 }
13306
13307 done:
13308 pop_deferring_access_checks ();
13309 }
13310
13311 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13312 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13313 initializer ; */
13314
13315 static tree
13316 cp_parser_decomposition_declaration (cp_parser *parser,
13317 cp_decl_specifier_seq *decl_specifiers,
13318 tree *maybe_range_for_decl,
13319 location_t *init_loc)
13320 {
13321 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13322 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13323 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13324
13325 /* Parse the identifier-list. */
13326 auto_vec<cp_expr, 10> v;
13327 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13328 while (true)
13329 {
13330 cp_expr e = cp_parser_identifier (parser);
13331 if (e.get_value () == error_mark_node)
13332 break;
13333 v.safe_push (e);
13334 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13335 break;
13336 cp_lexer_consume_token (parser->lexer);
13337 }
13338
13339 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13340 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13341 {
13342 end_loc = UNKNOWN_LOCATION;
13343 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13344 false);
13345 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13346 cp_lexer_consume_token (parser->lexer);
13347 else
13348 {
13349 cp_parser_skip_to_end_of_statement (parser);
13350 return error_mark_node;
13351 }
13352 }
13353
13354 if (cxx_dialect < cxx17)
13355 pedwarn (loc, 0, "structured bindings only available with "
13356 "-std=c++17 or -std=gnu++17");
13357
13358 tree pushed_scope;
13359 cp_declarator *declarator = make_declarator (cdk_decomp);
13360 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13361 declarator->id_loc = loc;
13362 if (ref_qual != REF_QUAL_NONE)
13363 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13364 ref_qual == REF_QUAL_RVALUE,
13365 NULL_TREE);
13366 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13367 NULL_TREE, decl_specifiers->attributes,
13368 &pushed_scope);
13369 tree orig_decl = decl;
13370
13371 unsigned int i;
13372 cp_expr e;
13373 cp_decl_specifier_seq decl_specs;
13374 clear_decl_specs (&decl_specs);
13375 decl_specs.type = make_auto ();
13376 tree prev = decl;
13377 FOR_EACH_VEC_ELT (v, i, e)
13378 {
13379 if (i == 0)
13380 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13381 else
13382 declarator->u.id.unqualified_name = e.get_value ();
13383 declarator->id_loc = e.get_location ();
13384 tree elt_pushed_scope;
13385 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13386 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13387 if (decl2 == error_mark_node)
13388 decl = error_mark_node;
13389 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13390 {
13391 /* Ensure we've diagnosed redeclaration if we aren't creating
13392 a new VAR_DECL. */
13393 gcc_assert (errorcount);
13394 decl = error_mark_node;
13395 }
13396 else
13397 prev = decl2;
13398 if (elt_pushed_scope)
13399 pop_scope (elt_pushed_scope);
13400 }
13401
13402 if (v.is_empty ())
13403 {
13404 error_at (loc, "empty structured binding declaration");
13405 decl = error_mark_node;
13406 }
13407
13408 if (maybe_range_for_decl == NULL
13409 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13410 {
13411 bool non_constant_p = false, is_direct_init = false;
13412 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13413 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13414 &non_constant_p);
13415 if (initializer == NULL_TREE
13416 || (TREE_CODE (initializer) == TREE_LIST
13417 && TREE_CHAIN (initializer))
13418 || (is_direct_init
13419 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13420 && CONSTRUCTOR_NELTS (initializer) != 1))
13421 {
13422 error_at (loc, "invalid initializer for structured binding "
13423 "declaration");
13424 initializer = error_mark_node;
13425 }
13426
13427 if (decl != error_mark_node)
13428 {
13429 cp_maybe_mangle_decomp (decl, prev, v.length ());
13430 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13431 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13432 cp_finish_decomp (decl, prev, v.length ());
13433 }
13434 }
13435 else if (decl != error_mark_node)
13436 {
13437 *maybe_range_for_decl = prev;
13438 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13439 the underlying DECL. */
13440 cp_finish_decomp (decl, prev, v.length ());
13441 }
13442
13443 if (pushed_scope)
13444 pop_scope (pushed_scope);
13445
13446 if (decl == error_mark_node && DECL_P (orig_decl))
13447 {
13448 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13449 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13450 }
13451
13452 return decl;
13453 }
13454
13455 /* Parse a decl-specifier-seq.
13456
13457 decl-specifier-seq:
13458 decl-specifier-seq [opt] decl-specifier
13459 decl-specifier attribute-specifier-seq [opt] (C++11)
13460
13461 decl-specifier:
13462 storage-class-specifier
13463 type-specifier
13464 function-specifier
13465 friend
13466 typedef
13467
13468 GNU Extension:
13469
13470 decl-specifier:
13471 attributes
13472
13473 Concepts Extension:
13474
13475 decl-specifier:
13476 concept
13477
13478 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13479
13480 The parser flags FLAGS is used to control type-specifier parsing.
13481
13482 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13483 flags:
13484
13485 1: one of the decl-specifiers is an elaborated-type-specifier
13486 (i.e., a type declaration)
13487 2: one of the decl-specifiers is an enum-specifier or a
13488 class-specifier (i.e., a type definition)
13489
13490 */
13491
13492 static void
13493 cp_parser_decl_specifier_seq (cp_parser* parser,
13494 cp_parser_flags flags,
13495 cp_decl_specifier_seq *decl_specs,
13496 int* declares_class_or_enum)
13497 {
13498 bool constructor_possible_p = !parser->in_declarator_p;
13499 bool found_decl_spec = false;
13500 cp_token *start_token = NULL;
13501 cp_decl_spec ds;
13502
13503 /* Clear DECL_SPECS. */
13504 clear_decl_specs (decl_specs);
13505
13506 /* Assume no class or enumeration type is declared. */
13507 *declares_class_or_enum = 0;
13508
13509 /* Keep reading specifiers until there are no more to read. */
13510 while (true)
13511 {
13512 bool constructor_p;
13513 cp_token *token;
13514 ds = ds_last;
13515
13516 /* Peek at the next token. */
13517 token = cp_lexer_peek_token (parser->lexer);
13518
13519 /* Save the first token of the decl spec list for error
13520 reporting. */
13521 if (!start_token)
13522 start_token = token;
13523 /* Handle attributes. */
13524 if (cp_next_tokens_can_be_attribute_p (parser))
13525 {
13526 /* Parse the attributes. */
13527 tree attrs = cp_parser_attributes_opt (parser);
13528
13529 /* In a sequence of declaration specifiers, c++11 attributes
13530 appertain to the type that precede them. In that case
13531 [dcl.spec]/1 says:
13532
13533 The attribute-specifier-seq affects the type only for
13534 the declaration it appears in, not other declarations
13535 involving the same type.
13536
13537 But for now let's force the user to position the
13538 attribute either at the beginning of the declaration or
13539 after the declarator-id, which would clearly mean that it
13540 applies to the declarator. */
13541 if (cxx11_attribute_p (attrs))
13542 {
13543 if (!found_decl_spec)
13544 /* The c++11 attribute is at the beginning of the
13545 declaration. It appertains to the entity being
13546 declared. */;
13547 else
13548 {
13549 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13550 {
13551 /* This is an attribute following a
13552 class-specifier. */
13553 if (decl_specs->type_definition_p)
13554 warn_misplaced_attr_for_class_type (token->location,
13555 decl_specs->type);
13556 attrs = NULL_TREE;
13557 }
13558 else
13559 {
13560 decl_specs->std_attributes
13561 = attr_chainon (decl_specs->std_attributes, attrs);
13562 if (decl_specs->locations[ds_std_attribute] == 0)
13563 decl_specs->locations[ds_std_attribute] = token->location;
13564 }
13565 continue;
13566 }
13567 }
13568
13569 decl_specs->attributes
13570 = attr_chainon (decl_specs->attributes, attrs);
13571 if (decl_specs->locations[ds_attribute] == 0)
13572 decl_specs->locations[ds_attribute] = token->location;
13573 continue;
13574 }
13575 /* Assume we will find a decl-specifier keyword. */
13576 found_decl_spec = true;
13577 /* If the next token is an appropriate keyword, we can simply
13578 add it to the list. */
13579 switch (token->keyword)
13580 {
13581 /* decl-specifier:
13582 friend
13583 constexpr */
13584 case RID_FRIEND:
13585 if (!at_class_scope_p ())
13586 {
13587 gcc_rich_location richloc (token->location);
13588 richloc.add_fixit_remove ();
13589 error_at (&richloc, "%<friend%> used outside of class");
13590 cp_lexer_purge_token (parser->lexer);
13591 }
13592 else
13593 {
13594 ds = ds_friend;
13595 /* Consume the token. */
13596 cp_lexer_consume_token (parser->lexer);
13597 }
13598 break;
13599
13600 case RID_CONSTEXPR:
13601 ds = ds_constexpr;
13602 cp_lexer_consume_token (parser->lexer);
13603 break;
13604
13605 case RID_CONCEPT:
13606 ds = ds_concept;
13607 cp_lexer_consume_token (parser->lexer);
13608 break;
13609
13610 /* function-specifier:
13611 inline
13612 virtual
13613 explicit */
13614 case RID_INLINE:
13615 case RID_VIRTUAL:
13616 case RID_EXPLICIT:
13617 cp_parser_function_specifier_opt (parser, decl_specs);
13618 break;
13619
13620 /* decl-specifier:
13621 typedef */
13622 case RID_TYPEDEF:
13623 ds = ds_typedef;
13624 /* Consume the token. */
13625 cp_lexer_consume_token (parser->lexer);
13626 /* A constructor declarator cannot appear in a typedef. */
13627 constructor_possible_p = false;
13628 /* The "typedef" keyword can only occur in a declaration; we
13629 may as well commit at this point. */
13630 cp_parser_commit_to_tentative_parse (parser);
13631
13632 if (decl_specs->storage_class != sc_none)
13633 decl_specs->conflicting_specifiers_p = true;
13634 break;
13635
13636 /* storage-class-specifier:
13637 auto
13638 register
13639 static
13640 extern
13641 mutable
13642
13643 GNU Extension:
13644 thread */
13645 case RID_AUTO:
13646 if (cxx_dialect == cxx98)
13647 {
13648 /* Consume the token. */
13649 cp_lexer_consume_token (parser->lexer);
13650
13651 /* Complain about `auto' as a storage specifier, if
13652 we're complaining about C++0x compatibility. */
13653 gcc_rich_location richloc (token->location);
13654 richloc.add_fixit_remove ();
13655 warning_at (&richloc, OPT_Wc__11_compat,
13656 "%<auto%> changes meaning in C++11; "
13657 "please remove it");
13658
13659 /* Set the storage class anyway. */
13660 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13661 token);
13662 }
13663 else
13664 /* C++0x auto type-specifier. */
13665 found_decl_spec = false;
13666 break;
13667
13668 case RID_REGISTER:
13669 case RID_STATIC:
13670 case RID_EXTERN:
13671 case RID_MUTABLE:
13672 /* Consume the token. */
13673 cp_lexer_consume_token (parser->lexer);
13674 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13675 token);
13676 break;
13677 case RID_THREAD:
13678 /* Consume the token. */
13679 ds = ds_thread;
13680 cp_lexer_consume_token (parser->lexer);
13681 break;
13682
13683 default:
13684 /* We did not yet find a decl-specifier yet. */
13685 found_decl_spec = false;
13686 break;
13687 }
13688
13689 if (found_decl_spec
13690 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13691 && token->keyword != RID_CONSTEXPR)
13692 error ("decl-specifier invalid in condition");
13693
13694 if (found_decl_spec
13695 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13696 && token->keyword != RID_MUTABLE
13697 && token->keyword != RID_CONSTEXPR)
13698 error_at (token->location, "%qD invalid in lambda",
13699 ridpointers[token->keyword]);
13700
13701 if (ds != ds_last)
13702 set_and_check_decl_spec_loc (decl_specs, ds, token);
13703
13704 /* Constructors are a special case. The `S' in `S()' is not a
13705 decl-specifier; it is the beginning of the declarator. */
13706 constructor_p
13707 = (!found_decl_spec
13708 && constructor_possible_p
13709 && (cp_parser_constructor_declarator_p
13710 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13711
13712 /* If we don't have a DECL_SPEC yet, then we must be looking at
13713 a type-specifier. */
13714 if (!found_decl_spec && !constructor_p)
13715 {
13716 int decl_spec_declares_class_or_enum;
13717 bool is_cv_qualifier;
13718 tree type_spec;
13719
13720 type_spec
13721 = cp_parser_type_specifier (parser, flags,
13722 decl_specs,
13723 /*is_declaration=*/true,
13724 &decl_spec_declares_class_or_enum,
13725 &is_cv_qualifier);
13726 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13727
13728 /* If this type-specifier referenced a user-defined type
13729 (a typedef, class-name, etc.), then we can't allow any
13730 more such type-specifiers henceforth.
13731
13732 [dcl.spec]
13733
13734 The longest sequence of decl-specifiers that could
13735 possibly be a type name is taken as the
13736 decl-specifier-seq of a declaration. The sequence shall
13737 be self-consistent as described below.
13738
13739 [dcl.type]
13740
13741 As a general rule, at most one type-specifier is allowed
13742 in the complete decl-specifier-seq of a declaration. The
13743 only exceptions are the following:
13744
13745 -- const or volatile can be combined with any other
13746 type-specifier.
13747
13748 -- signed or unsigned can be combined with char, long,
13749 short, or int.
13750
13751 -- ..
13752
13753 Example:
13754
13755 typedef char* Pc;
13756 void g (const int Pc);
13757
13758 Here, Pc is *not* part of the decl-specifier seq; it's
13759 the declarator. Therefore, once we see a type-specifier
13760 (other than a cv-qualifier), we forbid any additional
13761 user-defined types. We *do* still allow things like `int
13762 int' to be considered a decl-specifier-seq, and issue the
13763 error message later. */
13764 if (type_spec && !is_cv_qualifier)
13765 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13766 /* A constructor declarator cannot follow a type-specifier. */
13767 if (type_spec)
13768 {
13769 constructor_possible_p = false;
13770 found_decl_spec = true;
13771 if (!is_cv_qualifier)
13772 decl_specs->any_type_specifiers_p = true;
13773 }
13774 }
13775
13776 /* If we still do not have a DECL_SPEC, then there are no more
13777 decl-specifiers. */
13778 if (!found_decl_spec)
13779 break;
13780
13781 decl_specs->any_specifiers_p = true;
13782 /* After we see one decl-specifier, further decl-specifiers are
13783 always optional. */
13784 flags |= CP_PARSER_FLAGS_OPTIONAL;
13785 }
13786
13787 /* Don't allow a friend specifier with a class definition. */
13788 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13789 && (*declares_class_or_enum & 2))
13790 error_at (decl_specs->locations[ds_friend],
13791 "class definition may not be declared a friend");
13792 }
13793
13794 /* Parse an (optional) storage-class-specifier.
13795
13796 storage-class-specifier:
13797 auto
13798 register
13799 static
13800 extern
13801 mutable
13802
13803 GNU Extension:
13804
13805 storage-class-specifier:
13806 thread
13807
13808 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13809
13810 static tree
13811 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13812 {
13813 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13814 {
13815 case RID_AUTO:
13816 if (cxx_dialect != cxx98)
13817 return NULL_TREE;
13818 /* Fall through for C++98. */
13819 gcc_fallthrough ();
13820
13821 case RID_REGISTER:
13822 case RID_STATIC:
13823 case RID_EXTERN:
13824 case RID_MUTABLE:
13825 case RID_THREAD:
13826 /* Consume the token. */
13827 return cp_lexer_consume_token (parser->lexer)->u.value;
13828
13829 default:
13830 return NULL_TREE;
13831 }
13832 }
13833
13834 /* Parse an (optional) function-specifier.
13835
13836 function-specifier:
13837 inline
13838 virtual
13839 explicit
13840
13841 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13842 Updates DECL_SPECS, if it is non-NULL. */
13843
13844 static tree
13845 cp_parser_function_specifier_opt (cp_parser* parser,
13846 cp_decl_specifier_seq *decl_specs)
13847 {
13848 cp_token *token = cp_lexer_peek_token (parser->lexer);
13849 switch (token->keyword)
13850 {
13851 case RID_INLINE:
13852 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13853 break;
13854
13855 case RID_VIRTUAL:
13856 /* 14.5.2.3 [temp.mem]
13857
13858 A member function template shall not be virtual. */
13859 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13860 && current_class_type)
13861 error_at (token->location, "templates may not be %<virtual%>");
13862 else
13863 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13864 break;
13865
13866 case RID_EXPLICIT:
13867 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13868 break;
13869
13870 default:
13871 return NULL_TREE;
13872 }
13873
13874 /* Consume the token. */
13875 return cp_lexer_consume_token (parser->lexer)->u.value;
13876 }
13877
13878 /* Parse a linkage-specification.
13879
13880 linkage-specification:
13881 extern string-literal { declaration-seq [opt] }
13882 extern string-literal declaration */
13883
13884 static void
13885 cp_parser_linkage_specification (cp_parser* parser)
13886 {
13887 tree linkage;
13888
13889 /* Look for the `extern' keyword. */
13890 cp_token *extern_token
13891 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13892
13893 /* Look for the string-literal. */
13894 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13895 linkage = cp_parser_string_literal (parser, false, false);
13896
13897 /* Transform the literal into an identifier. If the literal is a
13898 wide-character string, or contains embedded NULs, then we can't
13899 handle it as the user wants. */
13900 if (strlen (TREE_STRING_POINTER (linkage))
13901 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13902 {
13903 cp_parser_error (parser, "invalid linkage-specification");
13904 /* Assume C++ linkage. */
13905 linkage = lang_name_cplusplus;
13906 }
13907 else
13908 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13909
13910 /* We're now using the new linkage. */
13911 push_lang_context (linkage);
13912
13913 /* Preserve the location of the the innermost linkage specification,
13914 tracking the locations of nested specifications via a local. */
13915 location_t saved_location
13916 = parser->innermost_linkage_specification_location;
13917 /* Construct a location ranging from the start of the "extern" to
13918 the end of the string-literal, with the caret at the start, e.g.:
13919 extern "C" {
13920 ^~~~~~~~~~
13921 */
13922 parser->innermost_linkage_specification_location
13923 = make_location (extern_token->location,
13924 extern_token->location,
13925 get_finish (string_token->location));
13926
13927 /* If the next token is a `{', then we're using the first
13928 production. */
13929 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13930 {
13931 cp_ensure_no_omp_declare_simd (parser);
13932 cp_ensure_no_oacc_routine (parser);
13933
13934 /* Consume the `{' token. */
13935 matching_braces braces;
13936 braces.consume_open (parser)->location;
13937 /* Parse the declarations. */
13938 cp_parser_declaration_seq_opt (parser);
13939 /* Look for the closing `}'. */
13940 braces.require_close (parser);
13941 }
13942 /* Otherwise, there's just one declaration. */
13943 else
13944 {
13945 bool saved_in_unbraced_linkage_specification_p;
13946
13947 saved_in_unbraced_linkage_specification_p
13948 = parser->in_unbraced_linkage_specification_p;
13949 parser->in_unbraced_linkage_specification_p = true;
13950 cp_parser_declaration (parser);
13951 parser->in_unbraced_linkage_specification_p
13952 = saved_in_unbraced_linkage_specification_p;
13953 }
13954
13955 /* We're done with the linkage-specification. */
13956 pop_lang_context ();
13957
13958 /* Restore location of parent linkage specification, if any. */
13959 parser->innermost_linkage_specification_location = saved_location;
13960 }
13961
13962 /* Parse a static_assert-declaration.
13963
13964 static_assert-declaration:
13965 static_assert ( constant-expression , string-literal ) ;
13966 static_assert ( constant-expression ) ; (C++17)
13967
13968 If MEMBER_P, this static_assert is a class member. */
13969
13970 static void
13971 cp_parser_static_assert(cp_parser *parser, bool member_p)
13972 {
13973 cp_expr condition;
13974 location_t token_loc;
13975 tree message;
13976 bool dummy;
13977
13978 /* Peek at the `static_assert' token so we can keep track of exactly
13979 where the static assertion started. */
13980 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13981
13982 /* Look for the `static_assert' keyword. */
13983 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13984 RT_STATIC_ASSERT))
13985 return;
13986
13987 /* We know we are in a static assertion; commit to any tentative
13988 parse. */
13989 if (cp_parser_parsing_tentatively (parser))
13990 cp_parser_commit_to_tentative_parse (parser);
13991
13992 /* Parse the `(' starting the static assertion condition. */
13993 matching_parens parens;
13994 parens.require_open (parser);
13995
13996 /* Parse the constant-expression. Allow a non-constant expression
13997 here in order to give better diagnostics in finish_static_assert. */
13998 condition =
13999 cp_parser_constant_expression (parser,
14000 /*allow_non_constant_p=*/true,
14001 /*non_constant_p=*/&dummy);
14002
14003 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14004 {
14005 if (cxx_dialect < cxx17)
14006 pedwarn (input_location, OPT_Wpedantic,
14007 "static_assert without a message "
14008 "only available with -std=c++17 or -std=gnu++17");
14009 /* Eat the ')' */
14010 cp_lexer_consume_token (parser->lexer);
14011 message = build_string (1, "");
14012 TREE_TYPE (message) = char_array_type_node;
14013 fix_string_type (message);
14014 }
14015 else
14016 {
14017 /* Parse the separating `,'. */
14018 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14019
14020 /* Parse the string-literal message. */
14021 message = cp_parser_string_literal (parser,
14022 /*translate=*/false,
14023 /*wide_ok=*/true);
14024
14025 /* A `)' completes the static assertion. */
14026 if (!parens.require_close (parser))
14027 cp_parser_skip_to_closing_parenthesis (parser,
14028 /*recovering=*/true,
14029 /*or_comma=*/false,
14030 /*consume_paren=*/true);
14031 }
14032
14033 /* A semicolon terminates the declaration. */
14034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14035
14036 /* Get the location for the static assertion. Use that of the
14037 condition if available, otherwise, use that of the "static_assert"
14038 token. */
14039 location_t assert_loc = condition.get_location ();
14040 if (assert_loc == UNKNOWN_LOCATION)
14041 assert_loc = token_loc;
14042
14043 /* Complete the static assertion, which may mean either processing
14044 the static assert now or saving it for template instantiation. */
14045 finish_static_assert (condition, message, assert_loc, member_p);
14046 }
14047
14048 /* Parse the expression in decltype ( expression ). */
14049
14050 static tree
14051 cp_parser_decltype_expr (cp_parser *parser,
14052 bool &id_expression_or_member_access_p)
14053 {
14054 cp_token *id_expr_start_token;
14055 tree expr;
14056
14057 /* Since we're going to preserve any side-effects from this parse, set up a
14058 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14059 in the expression. */
14060 tentative_firewall firewall (parser);
14061
14062 /* First, try parsing an id-expression. */
14063 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14064 cp_parser_parse_tentatively (parser);
14065 expr = cp_parser_id_expression (parser,
14066 /*template_keyword_p=*/false,
14067 /*check_dependency_p=*/true,
14068 /*template_p=*/NULL,
14069 /*declarator_p=*/false,
14070 /*optional_p=*/false);
14071
14072 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14073 {
14074 bool non_integral_constant_expression_p = false;
14075 tree id_expression = expr;
14076 cp_id_kind idk;
14077 const char *error_msg;
14078
14079 if (identifier_p (expr))
14080 /* Lookup the name we got back from the id-expression. */
14081 expr = cp_parser_lookup_name_simple (parser, expr,
14082 id_expr_start_token->location);
14083
14084 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14085 /* A template without args is not a complete id-expression. */
14086 expr = error_mark_node;
14087
14088 if (expr
14089 && expr != error_mark_node
14090 && TREE_CODE (expr) != TYPE_DECL
14091 && (TREE_CODE (expr) != BIT_NOT_EXPR
14092 || !TYPE_P (TREE_OPERAND (expr, 0)))
14093 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14094 {
14095 /* Complete lookup of the id-expression. */
14096 expr = (finish_id_expression
14097 (id_expression, expr, parser->scope, &idk,
14098 /*integral_constant_expression_p=*/false,
14099 /*allow_non_integral_constant_expression_p=*/true,
14100 &non_integral_constant_expression_p,
14101 /*template_p=*/false,
14102 /*done=*/true,
14103 /*address_p=*/false,
14104 /*template_arg_p=*/false,
14105 &error_msg,
14106 id_expr_start_token->location));
14107
14108 if (expr == error_mark_node)
14109 /* We found an id-expression, but it was something that we
14110 should not have found. This is an error, not something
14111 we can recover from, so note that we found an
14112 id-expression and we'll recover as gracefully as
14113 possible. */
14114 id_expression_or_member_access_p = true;
14115 }
14116
14117 if (expr
14118 && expr != error_mark_node
14119 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14120 /* We have an id-expression. */
14121 id_expression_or_member_access_p = true;
14122 }
14123
14124 if (!id_expression_or_member_access_p)
14125 {
14126 /* Abort the id-expression parse. */
14127 cp_parser_abort_tentative_parse (parser);
14128
14129 /* Parsing tentatively, again. */
14130 cp_parser_parse_tentatively (parser);
14131
14132 /* Parse a class member access. */
14133 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14134 /*cast_p=*/false, /*decltype*/true,
14135 /*member_access_only_p=*/true, NULL);
14136
14137 if (expr
14138 && expr != error_mark_node
14139 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14140 /* We have an id-expression. */
14141 id_expression_or_member_access_p = true;
14142 }
14143
14144 if (id_expression_or_member_access_p)
14145 /* We have parsed the complete id-expression or member access. */
14146 cp_parser_parse_definitely (parser);
14147 else
14148 {
14149 /* Abort our attempt to parse an id-expression or member access
14150 expression. */
14151 cp_parser_abort_tentative_parse (parser);
14152
14153 /* Commit to the tentative_firewall so we get syntax errors. */
14154 cp_parser_commit_to_tentative_parse (parser);
14155
14156 /* Parse a full expression. */
14157 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14158 /*decltype_p=*/true);
14159 }
14160
14161 return expr;
14162 }
14163
14164 /* Parse a `decltype' type. Returns the type.
14165
14166 simple-type-specifier:
14167 decltype ( expression )
14168 C++14 proposal:
14169 decltype ( auto ) */
14170
14171 static tree
14172 cp_parser_decltype (cp_parser *parser)
14173 {
14174 bool id_expression_or_member_access_p = false;
14175 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14176
14177 if (start_token->type == CPP_DECLTYPE)
14178 {
14179 /* Already parsed. */
14180 cp_lexer_consume_token (parser->lexer);
14181 return saved_checks_value (start_token->u.tree_check_value);
14182 }
14183
14184 /* Look for the `decltype' token. */
14185 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14186 return error_mark_node;
14187
14188 /* Parse the opening `('. */
14189 matching_parens parens;
14190 if (!parens.require_open (parser))
14191 return error_mark_node;
14192
14193 push_deferring_access_checks (dk_deferred);
14194
14195 tree expr = NULL_TREE;
14196
14197 if (cxx_dialect >= cxx14
14198 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14199 /* decltype (auto) */
14200 cp_lexer_consume_token (parser->lexer);
14201 else
14202 {
14203 /* decltype (expression) */
14204
14205 /* Types cannot be defined in a `decltype' expression. Save away the
14206 old message and set the new one. */
14207 const char *saved_message = parser->type_definition_forbidden_message;
14208 parser->type_definition_forbidden_message
14209 = G_("types may not be defined in %<decltype%> expressions");
14210
14211 /* The restrictions on constant-expressions do not apply inside
14212 decltype expressions. */
14213 bool saved_integral_constant_expression_p
14214 = parser->integral_constant_expression_p;
14215 bool saved_non_integral_constant_expression_p
14216 = parser->non_integral_constant_expression_p;
14217 parser->integral_constant_expression_p = false;
14218
14219 /* Within a parenthesized expression, a `>' token is always
14220 the greater-than operator. */
14221 bool saved_greater_than_is_operator_p
14222 = parser->greater_than_is_operator_p;
14223 parser->greater_than_is_operator_p = true;
14224
14225 /* Do not actually evaluate the expression. */
14226 ++cp_unevaluated_operand;
14227
14228 /* Do not warn about problems with the expression. */
14229 ++c_inhibit_evaluation_warnings;
14230
14231 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14232
14233 /* Go back to evaluating expressions. */
14234 --cp_unevaluated_operand;
14235 --c_inhibit_evaluation_warnings;
14236
14237 /* The `>' token might be the end of a template-id or
14238 template-parameter-list now. */
14239 parser->greater_than_is_operator_p
14240 = saved_greater_than_is_operator_p;
14241
14242 /* Restore the old message and the integral constant expression
14243 flags. */
14244 parser->type_definition_forbidden_message = saved_message;
14245 parser->integral_constant_expression_p
14246 = saved_integral_constant_expression_p;
14247 parser->non_integral_constant_expression_p
14248 = saved_non_integral_constant_expression_p;
14249 }
14250
14251 /* Parse to the closing `)'. */
14252 if (!parens.require_close (parser))
14253 {
14254 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14255 /*consume_paren=*/true);
14256 pop_deferring_access_checks ();
14257 return error_mark_node;
14258 }
14259
14260 if (!expr)
14261 {
14262 /* Build auto. */
14263 expr = make_decltype_auto ();
14264 AUTO_IS_DECLTYPE (expr) = true;
14265 }
14266 else
14267 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14268 tf_warning_or_error);
14269
14270 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14271 it again. */
14272 start_token->type = CPP_DECLTYPE;
14273 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14274 start_token->u.tree_check_value->value = expr;
14275 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14276 start_token->keyword = RID_MAX;
14277 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14278
14279 pop_to_parent_deferring_access_checks ();
14280
14281 return expr;
14282 }
14283
14284 /* Special member functions [gram.special] */
14285
14286 /* Parse a conversion-function-id.
14287
14288 conversion-function-id:
14289 operator conversion-type-id
14290
14291 Returns an IDENTIFIER_NODE representing the operator. */
14292
14293 static tree
14294 cp_parser_conversion_function_id (cp_parser* parser)
14295 {
14296 tree type;
14297 tree saved_scope;
14298 tree saved_qualifying_scope;
14299 tree saved_object_scope;
14300 tree pushed_scope = NULL_TREE;
14301
14302 /* Look for the `operator' token. */
14303 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14304 return error_mark_node;
14305 /* When we parse the conversion-type-id, the current scope will be
14306 reset. However, we need that information in able to look up the
14307 conversion function later, so we save it here. */
14308 saved_scope = parser->scope;
14309 saved_qualifying_scope = parser->qualifying_scope;
14310 saved_object_scope = parser->object_scope;
14311 /* We must enter the scope of the class so that the names of
14312 entities declared within the class are available in the
14313 conversion-type-id. For example, consider:
14314
14315 struct S {
14316 typedef int I;
14317 operator I();
14318 };
14319
14320 S::operator I() { ... }
14321
14322 In order to see that `I' is a type-name in the definition, we
14323 must be in the scope of `S'. */
14324 if (saved_scope)
14325 pushed_scope = push_scope (saved_scope);
14326 /* Parse the conversion-type-id. */
14327 type = cp_parser_conversion_type_id (parser);
14328 /* Leave the scope of the class, if any. */
14329 if (pushed_scope)
14330 pop_scope (pushed_scope);
14331 /* Restore the saved scope. */
14332 parser->scope = saved_scope;
14333 parser->qualifying_scope = saved_qualifying_scope;
14334 parser->object_scope = saved_object_scope;
14335 /* If the TYPE is invalid, indicate failure. */
14336 if (type == error_mark_node)
14337 return error_mark_node;
14338 return make_conv_op_name (type);
14339 }
14340
14341 /* Parse a conversion-type-id:
14342
14343 conversion-type-id:
14344 type-specifier-seq conversion-declarator [opt]
14345
14346 Returns the TYPE specified. */
14347
14348 static tree
14349 cp_parser_conversion_type_id (cp_parser* parser)
14350 {
14351 tree attributes;
14352 cp_decl_specifier_seq type_specifiers;
14353 cp_declarator *declarator;
14354 tree type_specified;
14355 const char *saved_message;
14356
14357 /* Parse the attributes. */
14358 attributes = cp_parser_attributes_opt (parser);
14359
14360 saved_message = parser->type_definition_forbidden_message;
14361 parser->type_definition_forbidden_message
14362 = G_("types may not be defined in a conversion-type-id");
14363
14364 /* Parse the type-specifiers. */
14365 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14366 /*is_trailing_return=*/false,
14367 &type_specifiers);
14368
14369 parser->type_definition_forbidden_message = saved_message;
14370
14371 /* If that didn't work, stop. */
14372 if (type_specifiers.type == error_mark_node)
14373 return error_mark_node;
14374 /* Parse the conversion-declarator. */
14375 declarator = cp_parser_conversion_declarator_opt (parser);
14376
14377 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14378 /*initialized=*/0, &attributes);
14379 if (attributes)
14380 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14381
14382 /* Don't give this error when parsing tentatively. This happens to
14383 work because we always parse this definitively once. */
14384 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14385 && type_uses_auto (type_specified))
14386 {
14387 if (cxx_dialect < cxx14)
14388 {
14389 error ("invalid use of %<auto%> in conversion operator");
14390 return error_mark_node;
14391 }
14392 else if (template_parm_scope_p ())
14393 warning (0, "use of %<auto%> in member template "
14394 "conversion operator can never be deduced");
14395 }
14396
14397 return type_specified;
14398 }
14399
14400 /* Parse an (optional) conversion-declarator.
14401
14402 conversion-declarator:
14403 ptr-operator conversion-declarator [opt]
14404
14405 */
14406
14407 static cp_declarator *
14408 cp_parser_conversion_declarator_opt (cp_parser* parser)
14409 {
14410 enum tree_code code;
14411 tree class_type, std_attributes = NULL_TREE;
14412 cp_cv_quals cv_quals;
14413
14414 /* We don't know if there's a ptr-operator next, or not. */
14415 cp_parser_parse_tentatively (parser);
14416 /* Try the ptr-operator. */
14417 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14418 &std_attributes);
14419 /* If it worked, look for more conversion-declarators. */
14420 if (cp_parser_parse_definitely (parser))
14421 {
14422 cp_declarator *declarator;
14423
14424 /* Parse another optional declarator. */
14425 declarator = cp_parser_conversion_declarator_opt (parser);
14426
14427 declarator = cp_parser_make_indirect_declarator
14428 (code, class_type, cv_quals, declarator, std_attributes);
14429
14430 return declarator;
14431 }
14432
14433 return NULL;
14434 }
14435
14436 /* Parse an (optional) ctor-initializer.
14437
14438 ctor-initializer:
14439 : mem-initializer-list */
14440
14441 static void
14442 cp_parser_ctor_initializer_opt (cp_parser* parser)
14443 {
14444 /* If the next token is not a `:', then there is no
14445 ctor-initializer. */
14446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14447 {
14448 /* Do default initialization of any bases and members. */
14449 if (DECL_CONSTRUCTOR_P (current_function_decl))
14450 finish_mem_initializers (NULL_TREE);
14451 return;
14452 }
14453
14454 /* Consume the `:' token. */
14455 cp_lexer_consume_token (parser->lexer);
14456 /* And the mem-initializer-list. */
14457 cp_parser_mem_initializer_list (parser);
14458 }
14459
14460 /* Parse a mem-initializer-list.
14461
14462 mem-initializer-list:
14463 mem-initializer ... [opt]
14464 mem-initializer ... [opt] , mem-initializer-list */
14465
14466 static void
14467 cp_parser_mem_initializer_list (cp_parser* parser)
14468 {
14469 tree mem_initializer_list = NULL_TREE;
14470 tree target_ctor = error_mark_node;
14471 cp_token *token = cp_lexer_peek_token (parser->lexer);
14472
14473 /* Let the semantic analysis code know that we are starting the
14474 mem-initializer-list. */
14475 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14476 error_at (token->location,
14477 "only constructors take member initializers");
14478
14479 /* Loop through the list. */
14480 while (true)
14481 {
14482 tree mem_initializer;
14483
14484 token = cp_lexer_peek_token (parser->lexer);
14485 /* Parse the mem-initializer. */
14486 mem_initializer = cp_parser_mem_initializer (parser);
14487 /* If the next token is a `...', we're expanding member initializers. */
14488 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14489 if (ellipsis
14490 || (mem_initializer != error_mark_node
14491 && check_for_bare_parameter_packs (TREE_PURPOSE
14492 (mem_initializer))))
14493 {
14494 /* Consume the `...'. */
14495 if (ellipsis)
14496 cp_lexer_consume_token (parser->lexer);
14497
14498 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14499 can be expanded but members cannot. */
14500 if (mem_initializer != error_mark_node
14501 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14502 {
14503 error_at (token->location,
14504 "cannot expand initializer for member %qD",
14505 TREE_PURPOSE (mem_initializer));
14506 mem_initializer = error_mark_node;
14507 }
14508
14509 /* Construct the pack expansion type. */
14510 if (mem_initializer != error_mark_node)
14511 mem_initializer = make_pack_expansion (mem_initializer);
14512 }
14513 if (target_ctor != error_mark_node
14514 && mem_initializer != error_mark_node)
14515 {
14516 error ("mem-initializer for %qD follows constructor delegation",
14517 TREE_PURPOSE (mem_initializer));
14518 mem_initializer = error_mark_node;
14519 }
14520 /* Look for a target constructor. */
14521 if (mem_initializer != error_mark_node
14522 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14523 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14524 {
14525 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14526 if (mem_initializer_list)
14527 {
14528 error ("constructor delegation follows mem-initializer for %qD",
14529 TREE_PURPOSE (mem_initializer_list));
14530 mem_initializer = error_mark_node;
14531 }
14532 target_ctor = mem_initializer;
14533 }
14534 /* Add it to the list, unless it was erroneous. */
14535 if (mem_initializer != error_mark_node)
14536 {
14537 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14538 mem_initializer_list = mem_initializer;
14539 }
14540 /* If the next token is not a `,', we're done. */
14541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14542 break;
14543 /* Consume the `,' token. */
14544 cp_lexer_consume_token (parser->lexer);
14545 }
14546
14547 /* Perform semantic analysis. */
14548 if (DECL_CONSTRUCTOR_P (current_function_decl))
14549 finish_mem_initializers (mem_initializer_list);
14550 }
14551
14552 /* Parse a mem-initializer.
14553
14554 mem-initializer:
14555 mem-initializer-id ( expression-list [opt] )
14556 mem-initializer-id braced-init-list
14557
14558 GNU extension:
14559
14560 mem-initializer:
14561 ( expression-list [opt] )
14562
14563 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14564 class) or FIELD_DECL (for a non-static data member) to initialize;
14565 the TREE_VALUE is the expression-list. An empty initialization
14566 list is represented by void_list_node. */
14567
14568 static tree
14569 cp_parser_mem_initializer (cp_parser* parser)
14570 {
14571 tree mem_initializer_id;
14572 tree expression_list;
14573 tree member;
14574 cp_token *token = cp_lexer_peek_token (parser->lexer);
14575
14576 /* Find out what is being initialized. */
14577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14578 {
14579 permerror (token->location,
14580 "anachronistic old-style base class initializer");
14581 mem_initializer_id = NULL_TREE;
14582 }
14583 else
14584 {
14585 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14586 if (mem_initializer_id == error_mark_node)
14587 return mem_initializer_id;
14588 }
14589 member = expand_member_init (mem_initializer_id);
14590 if (member && !DECL_P (member))
14591 in_base_initializer = 1;
14592
14593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14594 {
14595 bool expr_non_constant_p;
14596 cp_lexer_set_source_position (parser->lexer);
14597 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14598 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14599 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14600 expression_list = build_tree_list (NULL_TREE, expression_list);
14601 }
14602 else
14603 {
14604 vec<tree, va_gc> *vec;
14605 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14606 /*cast_p=*/false,
14607 /*allow_expansion_p=*/true,
14608 /*non_constant_p=*/NULL);
14609 if (vec == NULL)
14610 return error_mark_node;
14611 expression_list = build_tree_list_vec (vec);
14612 release_tree_vector (vec);
14613 }
14614
14615 if (expression_list == error_mark_node)
14616 return error_mark_node;
14617 if (!expression_list)
14618 expression_list = void_type_node;
14619
14620 in_base_initializer = 0;
14621
14622 return member ? build_tree_list (member, expression_list) : error_mark_node;
14623 }
14624
14625 /* Parse a mem-initializer-id.
14626
14627 mem-initializer-id:
14628 :: [opt] nested-name-specifier [opt] class-name
14629 decltype-specifier (C++11)
14630 identifier
14631
14632 Returns a TYPE indicating the class to be initialized for the first
14633 production (and the second in C++11). Returns an IDENTIFIER_NODE
14634 indicating the data member to be initialized for the last production. */
14635
14636 static tree
14637 cp_parser_mem_initializer_id (cp_parser* parser)
14638 {
14639 bool global_scope_p;
14640 bool nested_name_specifier_p;
14641 bool template_p = false;
14642 tree id;
14643
14644 cp_token *token = cp_lexer_peek_token (parser->lexer);
14645
14646 /* `typename' is not allowed in this context ([temp.res]). */
14647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14648 {
14649 error_at (token->location,
14650 "keyword %<typename%> not allowed in this context (a qualified "
14651 "member initializer is implicitly a type)");
14652 cp_lexer_consume_token (parser->lexer);
14653 }
14654 /* Look for the optional `::' operator. */
14655 global_scope_p
14656 = (cp_parser_global_scope_opt (parser,
14657 /*current_scope_valid_p=*/false)
14658 != NULL_TREE);
14659 /* Look for the optional nested-name-specifier. The simplest way to
14660 implement:
14661
14662 [temp.res]
14663
14664 The keyword `typename' is not permitted in a base-specifier or
14665 mem-initializer; in these contexts a qualified name that
14666 depends on a template-parameter is implicitly assumed to be a
14667 type name.
14668
14669 is to assume that we have seen the `typename' keyword at this
14670 point. */
14671 nested_name_specifier_p
14672 = (cp_parser_nested_name_specifier_opt (parser,
14673 /*typename_keyword_p=*/true,
14674 /*check_dependency_p=*/true,
14675 /*type_p=*/true,
14676 /*is_declaration=*/true)
14677 != NULL_TREE);
14678 if (nested_name_specifier_p)
14679 template_p = cp_parser_optional_template_keyword (parser);
14680 /* If there is a `::' operator or a nested-name-specifier, then we
14681 are definitely looking for a class-name. */
14682 if (global_scope_p || nested_name_specifier_p)
14683 return cp_parser_class_name (parser,
14684 /*typename_keyword_p=*/true,
14685 /*template_keyword_p=*/template_p,
14686 typename_type,
14687 /*check_dependency_p=*/true,
14688 /*class_head_p=*/false,
14689 /*is_declaration=*/true);
14690 /* Otherwise, we could also be looking for an ordinary identifier. */
14691 cp_parser_parse_tentatively (parser);
14692 if (cp_lexer_next_token_is_decltype (parser->lexer))
14693 /* Try a decltype-specifier. */
14694 id = cp_parser_decltype (parser);
14695 else
14696 /* Otherwise, try a class-name. */
14697 id = cp_parser_class_name (parser,
14698 /*typename_keyword_p=*/true,
14699 /*template_keyword_p=*/false,
14700 none_type,
14701 /*check_dependency_p=*/true,
14702 /*class_head_p=*/false,
14703 /*is_declaration=*/true);
14704 /* If we found one, we're done. */
14705 if (cp_parser_parse_definitely (parser))
14706 return id;
14707 /* Otherwise, look for an ordinary identifier. */
14708 return cp_parser_identifier (parser);
14709 }
14710
14711 /* Overloading [gram.over] */
14712
14713 /* Parse an operator-function-id.
14714
14715 operator-function-id:
14716 operator operator
14717
14718 Returns an IDENTIFIER_NODE for the operator which is a
14719 human-readable spelling of the identifier, e.g., `operator +'. */
14720
14721 static cp_expr
14722 cp_parser_operator_function_id (cp_parser* parser)
14723 {
14724 /* Look for the `operator' keyword. */
14725 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14726 return error_mark_node;
14727 /* And then the name of the operator itself. */
14728 return cp_parser_operator (parser);
14729 }
14730
14731 /* Return an identifier node for a user-defined literal operator.
14732 The suffix identifier is chained to the operator name identifier. */
14733
14734 tree
14735 cp_literal_operator_id (const char* name)
14736 {
14737 tree identifier;
14738 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14739 + strlen (name) + 10);
14740 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14741 identifier = get_identifier (buffer);
14742
14743 return identifier;
14744 }
14745
14746 /* Parse an operator.
14747
14748 operator:
14749 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14750 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14751 || ++ -- , ->* -> () []
14752
14753 GNU Extensions:
14754
14755 operator:
14756 <? >? <?= >?=
14757
14758 Returns an IDENTIFIER_NODE for the operator which is a
14759 human-readable spelling of the identifier, e.g., `operator +'. */
14760
14761 static cp_expr
14762 cp_parser_operator (cp_parser* parser)
14763 {
14764 tree id = NULL_TREE;
14765 cp_token *token;
14766 bool utf8 = false;
14767
14768 /* Peek at the next token. */
14769 token = cp_lexer_peek_token (parser->lexer);
14770
14771 location_t start_loc = token->location;
14772
14773 /* Figure out which operator we have. */
14774 enum tree_code op = ERROR_MARK;
14775 bool assop = false;
14776 bool consumed = false;
14777 switch (token->type)
14778 {
14779 case CPP_KEYWORD:
14780 {
14781 /* The keyword should be either `new' or `delete'. */
14782 if (token->keyword == RID_NEW)
14783 op = NEW_EXPR;
14784 else if (token->keyword == RID_DELETE)
14785 op = DELETE_EXPR;
14786 else
14787 break;
14788
14789 /* Consume the `new' or `delete' token. */
14790 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14791
14792 /* Peek at the next token. */
14793 token = cp_lexer_peek_token (parser->lexer);
14794 /* If it's a `[' token then this is the array variant of the
14795 operator. */
14796 if (token->type == CPP_OPEN_SQUARE)
14797 {
14798 /* Consume the `[' token. */
14799 cp_lexer_consume_token (parser->lexer);
14800 /* Look for the `]' token. */
14801 if (cp_token *close_token
14802 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14803 end_loc = close_token->location;
14804 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14805 }
14806 start_loc = make_location (start_loc, start_loc, end_loc);
14807 consumed = true;
14808 break;
14809 }
14810
14811 case CPP_PLUS:
14812 op = PLUS_EXPR;
14813 break;
14814
14815 case CPP_MINUS:
14816 op = MINUS_EXPR;
14817 break;
14818
14819 case CPP_MULT:
14820 op = MULT_EXPR;
14821 break;
14822
14823 case CPP_DIV:
14824 op = TRUNC_DIV_EXPR;
14825 break;
14826
14827 case CPP_MOD:
14828 op = TRUNC_MOD_EXPR;
14829 break;
14830
14831 case CPP_XOR:
14832 op = BIT_XOR_EXPR;
14833 break;
14834
14835 case CPP_AND:
14836 op = BIT_AND_EXPR;
14837 break;
14838
14839 case CPP_OR:
14840 op = BIT_IOR_EXPR;
14841 break;
14842
14843 case CPP_COMPL:
14844 op = BIT_NOT_EXPR;
14845 break;
14846
14847 case CPP_NOT:
14848 op = TRUTH_NOT_EXPR;
14849 break;
14850
14851 case CPP_EQ:
14852 assop = true;
14853 op = NOP_EXPR;
14854 break;
14855
14856 case CPP_LESS:
14857 op = LT_EXPR;
14858 break;
14859
14860 case CPP_GREATER:
14861 op = GT_EXPR;
14862 break;
14863
14864 case CPP_PLUS_EQ:
14865 assop = true;
14866 op = PLUS_EXPR;
14867 break;
14868
14869 case CPP_MINUS_EQ:
14870 assop = true;
14871 op = MINUS_EXPR;
14872 break;
14873
14874 case CPP_MULT_EQ:
14875 assop = true;
14876 op = MULT_EXPR;
14877 break;
14878
14879 case CPP_DIV_EQ:
14880 assop = true;
14881 op = TRUNC_DIV_EXPR;
14882 break;
14883
14884 case CPP_MOD_EQ:
14885 assop = true;
14886 op = TRUNC_MOD_EXPR;
14887 break;
14888
14889 case CPP_XOR_EQ:
14890 assop = true;
14891 op = BIT_XOR_EXPR;
14892 break;
14893
14894 case CPP_AND_EQ:
14895 assop = true;
14896 op = BIT_AND_EXPR;
14897 break;
14898
14899 case CPP_OR_EQ:
14900 assop = true;
14901 op = BIT_IOR_EXPR;
14902 break;
14903
14904 case CPP_LSHIFT:
14905 op = LSHIFT_EXPR;
14906 break;
14907
14908 case CPP_RSHIFT:
14909 op = RSHIFT_EXPR;
14910 break;
14911
14912 case CPP_LSHIFT_EQ:
14913 assop = true;
14914 op = LSHIFT_EXPR;
14915 break;
14916
14917 case CPP_RSHIFT_EQ:
14918 assop = true;
14919 op = RSHIFT_EXPR;
14920 break;
14921
14922 case CPP_EQ_EQ:
14923 op = EQ_EXPR;
14924 break;
14925
14926 case CPP_NOT_EQ:
14927 op = NE_EXPR;
14928 break;
14929
14930 case CPP_LESS_EQ:
14931 op = LE_EXPR;
14932 break;
14933
14934 case CPP_GREATER_EQ:
14935 op = GE_EXPR;
14936 break;
14937
14938 case CPP_AND_AND:
14939 op = TRUTH_ANDIF_EXPR;
14940 break;
14941
14942 case CPP_OR_OR:
14943 op = TRUTH_ORIF_EXPR;
14944 break;
14945
14946 case CPP_PLUS_PLUS:
14947 op = POSTINCREMENT_EXPR;
14948 break;
14949
14950 case CPP_MINUS_MINUS:
14951 op = PREDECREMENT_EXPR;
14952 break;
14953
14954 case CPP_COMMA:
14955 op = COMPOUND_EXPR;
14956 break;
14957
14958 case CPP_DEREF_STAR:
14959 op = MEMBER_REF;
14960 break;
14961
14962 case CPP_DEREF:
14963 op = COMPONENT_REF;
14964 break;
14965
14966 case CPP_OPEN_PAREN:
14967 {
14968 /* Consume the `('. */
14969 matching_parens parens;
14970 parens.consume_open (parser);
14971 /* Look for the matching `)'. */
14972 parens.require_close (parser);
14973 op = CALL_EXPR;
14974 consumed = true;
14975 break;
14976 }
14977
14978 case CPP_OPEN_SQUARE:
14979 /* Consume the `['. */
14980 cp_lexer_consume_token (parser->lexer);
14981 /* Look for the matching `]'. */
14982 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14983 op = ARRAY_REF;
14984 consumed = true;
14985 break;
14986
14987 case CPP_UTF8STRING:
14988 case CPP_UTF8STRING_USERDEF:
14989 utf8 = true;
14990 /* FALLTHRU */
14991 case CPP_STRING:
14992 case CPP_WSTRING:
14993 case CPP_STRING16:
14994 case CPP_STRING32:
14995 case CPP_STRING_USERDEF:
14996 case CPP_WSTRING_USERDEF:
14997 case CPP_STRING16_USERDEF:
14998 case CPP_STRING32_USERDEF:
14999 {
15000 tree str, string_tree;
15001 int sz, len;
15002
15003 if (cxx_dialect == cxx98)
15004 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15005
15006 /* Consume the string. */
15007 str = cp_parser_string_literal (parser, /*translate=*/true,
15008 /*wide_ok=*/true, /*lookup_udlit=*/false);
15009 if (str == error_mark_node)
15010 return error_mark_node;
15011 else if (TREE_CODE (str) == USERDEF_LITERAL)
15012 {
15013 string_tree = USERDEF_LITERAL_VALUE (str);
15014 id = USERDEF_LITERAL_SUFFIX_ID (str);
15015 }
15016 else
15017 {
15018 string_tree = str;
15019 /* Look for the suffix identifier. */
15020 token = cp_lexer_peek_token (parser->lexer);
15021 if (token->type == CPP_NAME)
15022 id = cp_parser_identifier (parser);
15023 else if (token->type == CPP_KEYWORD)
15024 {
15025 error ("unexpected keyword;"
15026 " remove space between quotes and suffix identifier");
15027 return error_mark_node;
15028 }
15029 else
15030 {
15031 error ("expected suffix identifier");
15032 return error_mark_node;
15033 }
15034 }
15035 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15036 (TREE_TYPE (TREE_TYPE (string_tree))));
15037 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15038 if (len != 0)
15039 {
15040 error ("expected empty string after %<operator%> keyword");
15041 return error_mark_node;
15042 }
15043 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15044 != char_type_node)
15045 {
15046 error ("invalid encoding prefix in literal operator");
15047 return error_mark_node;
15048 }
15049 if (id != error_mark_node)
15050 {
15051 const char *name = IDENTIFIER_POINTER (id);
15052 id = cp_literal_operator_id (name);
15053 }
15054 return id;
15055 }
15056
15057 default:
15058 /* Anything else is an error. */
15059 break;
15060 }
15061
15062 /* If we have selected an identifier, we need to consume the
15063 operator token. */
15064 if (op != ERROR_MARK)
15065 {
15066 id = ovl_op_identifier (assop, op);
15067 if (!consumed)
15068 cp_lexer_consume_token (parser->lexer);
15069 }
15070 /* Otherwise, no valid operator name was present. */
15071 else
15072 {
15073 cp_parser_error (parser, "expected operator");
15074 id = error_mark_node;
15075 }
15076
15077 return cp_expr (id, start_loc);
15078 }
15079
15080 /* Parse a template-declaration.
15081
15082 template-declaration:
15083 export [opt] template < template-parameter-list > declaration
15084
15085 If MEMBER_P is TRUE, this template-declaration occurs within a
15086 class-specifier.
15087
15088 The grammar rule given by the standard isn't correct. What
15089 is really meant is:
15090
15091 template-declaration:
15092 export [opt] template-parameter-list-seq
15093 decl-specifier-seq [opt] init-declarator [opt] ;
15094 export [opt] template-parameter-list-seq
15095 function-definition
15096
15097 template-parameter-list-seq:
15098 template-parameter-list-seq [opt]
15099 template < template-parameter-list >
15100
15101 Concept Extensions:
15102
15103 template-parameter-list-seq:
15104 template < template-parameter-list > requires-clause [opt]
15105
15106 requires-clause:
15107 requires logical-or-expression */
15108
15109 static void
15110 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15111 {
15112 /* Check for `export'. */
15113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15114 {
15115 /* Consume the `export' token. */
15116 cp_lexer_consume_token (parser->lexer);
15117 /* Warn that we do not support `export'. */
15118 warning (0, "keyword %<export%> not implemented, and will be ignored");
15119 }
15120
15121 cp_parser_template_declaration_after_export (parser, member_p);
15122 }
15123
15124 /* Parse a template-parameter-list.
15125
15126 template-parameter-list:
15127 template-parameter
15128 template-parameter-list , template-parameter
15129
15130 Returns a TREE_LIST. Each node represents a template parameter.
15131 The nodes are connected via their TREE_CHAINs. */
15132
15133 static tree
15134 cp_parser_template_parameter_list (cp_parser* parser)
15135 {
15136 tree parameter_list = NULL_TREE;
15137
15138 begin_template_parm_list ();
15139
15140 /* The loop below parses the template parms. We first need to know
15141 the total number of template parms to be able to compute proper
15142 canonical types of each dependent type. So after the loop, when
15143 we know the total number of template parms,
15144 end_template_parm_list computes the proper canonical types and
15145 fixes up the dependent types accordingly. */
15146 while (true)
15147 {
15148 tree parameter;
15149 bool is_non_type;
15150 bool is_parameter_pack;
15151 location_t parm_loc;
15152
15153 /* Parse the template-parameter. */
15154 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15155 parameter = cp_parser_template_parameter (parser,
15156 &is_non_type,
15157 &is_parameter_pack);
15158 /* Add it to the list. */
15159 if (parameter != error_mark_node)
15160 parameter_list = process_template_parm (parameter_list,
15161 parm_loc,
15162 parameter,
15163 is_non_type,
15164 is_parameter_pack);
15165 else
15166 {
15167 tree err_parm = build_tree_list (parameter, parameter);
15168 parameter_list = chainon (parameter_list, err_parm);
15169 }
15170
15171 /* If the next token is not a `,', we're done. */
15172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15173 break;
15174 /* Otherwise, consume the `,' token. */
15175 cp_lexer_consume_token (parser->lexer);
15176 }
15177
15178 return end_template_parm_list (parameter_list);
15179 }
15180
15181 /* Parse a introduction-list.
15182
15183 introduction-list:
15184 introduced-parameter
15185 introduction-list , introduced-parameter
15186
15187 introduced-parameter:
15188 ...[opt] identifier
15189
15190 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15191 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15192 WILDCARD_DECL will also have DECL_NAME set and token location in
15193 DECL_SOURCE_LOCATION. */
15194
15195 static tree
15196 cp_parser_introduction_list (cp_parser *parser)
15197 {
15198 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15199
15200 while (true)
15201 {
15202 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15203 if (is_pack)
15204 cp_lexer_consume_token (parser->lexer);
15205
15206 /* Build placeholder. */
15207 tree parm = build_nt (WILDCARD_DECL);
15208 DECL_SOURCE_LOCATION (parm)
15209 = cp_lexer_peek_token (parser->lexer)->location;
15210 DECL_NAME (parm) = cp_parser_identifier (parser);
15211 WILDCARD_PACK_P (parm) = is_pack;
15212 vec_safe_push (introduction_vec, parm);
15213
15214 /* If the next token is not a `,', we're done. */
15215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15216 break;
15217 /* Otherwise, consume the `,' token. */
15218 cp_lexer_consume_token (parser->lexer);
15219 }
15220
15221 /* Convert the vec into a TREE_VEC. */
15222 tree introduction_list = make_tree_vec (introduction_vec->length ());
15223 unsigned int n;
15224 tree parm;
15225 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15226 TREE_VEC_ELT (introduction_list, n) = parm;
15227
15228 release_tree_vector (introduction_vec);
15229 return introduction_list;
15230 }
15231
15232 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15233 is an abstract declarator. */
15234
15235 static inline cp_declarator*
15236 get_id_declarator (cp_declarator *declarator)
15237 {
15238 cp_declarator *d = declarator;
15239 while (d && d->kind != cdk_id)
15240 d = d->declarator;
15241 return d;
15242 }
15243
15244 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15245 is an abstract declarator. */
15246
15247 static inline tree
15248 get_unqualified_id (cp_declarator *declarator)
15249 {
15250 declarator = get_id_declarator (declarator);
15251 if (declarator)
15252 return declarator->u.id.unqualified_name;
15253 else
15254 return NULL_TREE;
15255 }
15256
15257 /* Returns true if DECL represents a constrained-parameter. */
15258
15259 static inline bool
15260 is_constrained_parameter (tree decl)
15261 {
15262 return (decl
15263 && TREE_CODE (decl) == TYPE_DECL
15264 && CONSTRAINED_PARM_CONCEPT (decl)
15265 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15266 }
15267
15268 /* Returns true if PARM declares a constrained-parameter. */
15269
15270 static inline bool
15271 is_constrained_parameter (cp_parameter_declarator *parm)
15272 {
15273 return is_constrained_parameter (parm->decl_specifiers.type);
15274 }
15275
15276 /* Check that the type parameter is only a declarator-id, and that its
15277 type is not cv-qualified. */
15278
15279 bool
15280 cp_parser_check_constrained_type_parm (cp_parser *parser,
15281 cp_parameter_declarator *parm)
15282 {
15283 if (!parm->declarator)
15284 return true;
15285
15286 if (parm->declarator->kind != cdk_id)
15287 {
15288 cp_parser_error (parser, "invalid constrained type parameter");
15289 return false;
15290 }
15291
15292 /* Don't allow cv-qualified type parameters. */
15293 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15294 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15295 {
15296 cp_parser_error (parser, "cv-qualified type parameter");
15297 return false;
15298 }
15299
15300 return true;
15301 }
15302
15303 /* Finish parsing/processing a template type parameter and checking
15304 various restrictions. */
15305
15306 static inline tree
15307 cp_parser_constrained_type_template_parm (cp_parser *parser,
15308 tree id,
15309 cp_parameter_declarator* parmdecl)
15310 {
15311 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15312 return finish_template_type_parm (class_type_node, id);
15313 else
15314 return error_mark_node;
15315 }
15316
15317 static tree
15318 finish_constrained_template_template_parm (tree proto, tree id)
15319 {
15320 /* FIXME: This should probably be copied, and we may need to adjust
15321 the template parameter depths. */
15322 tree saved_parms = current_template_parms;
15323 begin_template_parm_list ();
15324 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15325 end_template_parm_list ();
15326
15327 tree parm = finish_template_template_parm (class_type_node, id);
15328 current_template_parms = saved_parms;
15329
15330 return parm;
15331 }
15332
15333 /* Finish parsing/processing a template template parameter by borrowing
15334 the template parameter list from the prototype parameter. */
15335
15336 static tree
15337 cp_parser_constrained_template_template_parm (cp_parser *parser,
15338 tree proto,
15339 tree id,
15340 cp_parameter_declarator *parmdecl)
15341 {
15342 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15343 return error_mark_node;
15344 return finish_constrained_template_template_parm (proto, id);
15345 }
15346
15347 /* Create a new non-type template parameter from the given PARM
15348 declarator. */
15349
15350 static tree
15351 constrained_non_type_template_parm (bool *is_non_type,
15352 cp_parameter_declarator *parm)
15353 {
15354 *is_non_type = true;
15355 cp_declarator *decl = parm->declarator;
15356 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15357 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15358 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15359 }
15360
15361 /* Build a constrained template parameter based on the PARMDECL
15362 declarator. The type of PARMDECL is the constrained type, which
15363 refers to the prototype template parameter that ultimately
15364 specifies the type of the declared parameter. */
15365
15366 static tree
15367 finish_constrained_parameter (cp_parser *parser,
15368 cp_parameter_declarator *parmdecl,
15369 bool *is_non_type,
15370 bool *is_parameter_pack)
15371 {
15372 tree decl = parmdecl->decl_specifiers.type;
15373 tree id = get_unqualified_id (parmdecl->declarator);
15374 tree def = parmdecl->default_argument;
15375 tree proto = DECL_INITIAL (decl);
15376
15377 /* A template parameter constrained by a variadic concept shall also
15378 be declared as a template parameter pack. */
15379 bool is_variadic = template_parameter_pack_p (proto);
15380 if (is_variadic && !*is_parameter_pack)
15381 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15382
15383 /* Build the parameter. Return an error if the declarator was invalid. */
15384 tree parm;
15385 if (TREE_CODE (proto) == TYPE_DECL)
15386 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15387 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15388 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15389 parmdecl);
15390 else
15391 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15392 if (parm == error_mark_node)
15393 return error_mark_node;
15394
15395 /* Finish the parameter decl and create a node attaching the
15396 default argument and constraint. */
15397 parm = build_tree_list (def, parm);
15398 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15399
15400 return parm;
15401 }
15402
15403 /* Returns true if the parsed type actually represents the declaration
15404 of a type template-parameter. */
15405
15406 static inline bool
15407 declares_constrained_type_template_parameter (tree type)
15408 {
15409 return (is_constrained_parameter (type)
15410 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15411 }
15412
15413
15414 /* Returns true if the parsed type actually represents the declaration of
15415 a template template-parameter. */
15416
15417 static bool
15418 declares_constrained_template_template_parameter (tree type)
15419 {
15420 return (is_constrained_parameter (type)
15421 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15422 }
15423
15424 /* Parse a default argument for a type template-parameter.
15425 Note that diagnostics are handled in cp_parser_template_parameter. */
15426
15427 static tree
15428 cp_parser_default_type_template_argument (cp_parser *parser)
15429 {
15430 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15431
15432 /* Consume the `=' token. */
15433 cp_lexer_consume_token (parser->lexer);
15434
15435 cp_token *token = cp_lexer_peek_token (parser->lexer);
15436
15437 /* Parse the default-argument. */
15438 push_deferring_access_checks (dk_no_deferred);
15439 tree default_argument = cp_parser_type_id (parser);
15440 pop_deferring_access_checks ();
15441
15442 if (flag_concepts && type_uses_auto (default_argument))
15443 {
15444 error_at (token->location,
15445 "invalid use of %<auto%> in default template argument");
15446 return error_mark_node;
15447 }
15448
15449 return default_argument;
15450 }
15451
15452 /* Parse a default argument for a template template-parameter. */
15453
15454 static tree
15455 cp_parser_default_template_template_argument (cp_parser *parser)
15456 {
15457 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15458
15459 bool is_template;
15460
15461 /* Consume the `='. */
15462 cp_lexer_consume_token (parser->lexer);
15463 /* Parse the id-expression. */
15464 push_deferring_access_checks (dk_no_deferred);
15465 /* save token before parsing the id-expression, for error
15466 reporting */
15467 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15468 tree default_argument
15469 = cp_parser_id_expression (parser,
15470 /*template_keyword_p=*/false,
15471 /*check_dependency_p=*/true,
15472 /*template_p=*/&is_template,
15473 /*declarator_p=*/false,
15474 /*optional_p=*/false);
15475 if (TREE_CODE (default_argument) == TYPE_DECL)
15476 /* If the id-expression was a template-id that refers to
15477 a template-class, we already have the declaration here,
15478 so no further lookup is needed. */
15479 ;
15480 else
15481 /* Look up the name. */
15482 default_argument
15483 = cp_parser_lookup_name (parser, default_argument,
15484 none_type,
15485 /*is_template=*/is_template,
15486 /*is_namespace=*/false,
15487 /*check_dependency=*/true,
15488 /*ambiguous_decls=*/NULL,
15489 token->location);
15490 /* See if the default argument is valid. */
15491 default_argument = check_template_template_default_arg (default_argument);
15492 pop_deferring_access_checks ();
15493 return default_argument;
15494 }
15495
15496 /* Parse a template-parameter.
15497
15498 template-parameter:
15499 type-parameter
15500 parameter-declaration
15501
15502 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15503 the parameter. The TREE_PURPOSE is the default value, if any.
15504 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15505 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15506 set to true iff this parameter is a parameter pack. */
15507
15508 static tree
15509 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15510 bool *is_parameter_pack)
15511 {
15512 cp_token *token;
15513 cp_parameter_declarator *parameter_declarator;
15514 tree parm;
15515
15516 /* Assume it is a type parameter or a template parameter. */
15517 *is_non_type = false;
15518 /* Assume it not a parameter pack. */
15519 *is_parameter_pack = false;
15520 /* Peek at the next token. */
15521 token = cp_lexer_peek_token (parser->lexer);
15522 /* If it is `template', we have a type-parameter. */
15523 if (token->keyword == RID_TEMPLATE)
15524 return cp_parser_type_parameter (parser, is_parameter_pack);
15525 /* If it is `class' or `typename' we do not know yet whether it is a
15526 type parameter or a non-type parameter. Consider:
15527
15528 template <typename T, typename T::X X> ...
15529
15530 or:
15531
15532 template <class C, class D*> ...
15533
15534 Here, the first parameter is a type parameter, and the second is
15535 a non-type parameter. We can tell by looking at the token after
15536 the identifier -- if it is a `,', `=', or `>' then we have a type
15537 parameter. */
15538 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15539 {
15540 /* Peek at the token after `class' or `typename'. */
15541 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15542 /* If it's an ellipsis, we have a template type parameter
15543 pack. */
15544 if (token->type == CPP_ELLIPSIS)
15545 return cp_parser_type_parameter (parser, is_parameter_pack);
15546 /* If it's an identifier, skip it. */
15547 if (token->type == CPP_NAME)
15548 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15549 /* Now, see if the token looks like the end of a template
15550 parameter. */
15551 if (token->type == CPP_COMMA
15552 || token->type == CPP_EQ
15553 || token->type == CPP_GREATER)
15554 return cp_parser_type_parameter (parser, is_parameter_pack);
15555 }
15556
15557 /* Otherwise, it is a non-type parameter or a constrained parameter.
15558
15559 [temp.param]
15560
15561 When parsing a default template-argument for a non-type
15562 template-parameter, the first non-nested `>' is taken as the end
15563 of the template parameter-list rather than a greater-than
15564 operator. */
15565 parameter_declarator
15566 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15567 /*parenthesized_p=*/NULL);
15568
15569 if (!parameter_declarator)
15570 return error_mark_node;
15571
15572 /* If the parameter declaration is marked as a parameter pack, set
15573 *IS_PARAMETER_PACK to notify the caller. */
15574 if (parameter_declarator->template_parameter_pack_p)
15575 *is_parameter_pack = true;
15576
15577 if (parameter_declarator->default_argument)
15578 {
15579 /* Can happen in some cases of erroneous input (c++/34892). */
15580 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15581 /* Consume the `...' for better error recovery. */
15582 cp_lexer_consume_token (parser->lexer);
15583 }
15584
15585 // The parameter may have been constrained.
15586 if (is_constrained_parameter (parameter_declarator))
15587 return finish_constrained_parameter (parser,
15588 parameter_declarator,
15589 is_non_type,
15590 is_parameter_pack);
15591
15592 // Now we're sure that the parameter is a non-type parameter.
15593 *is_non_type = true;
15594
15595 parm = grokdeclarator (parameter_declarator->declarator,
15596 &parameter_declarator->decl_specifiers,
15597 TPARM, /*initialized=*/0,
15598 /*attrlist=*/NULL);
15599 if (parm == error_mark_node)
15600 return error_mark_node;
15601
15602 return build_tree_list (parameter_declarator->default_argument, parm);
15603 }
15604
15605 /* Parse a type-parameter.
15606
15607 type-parameter:
15608 class identifier [opt]
15609 class identifier [opt] = type-id
15610 typename identifier [opt]
15611 typename identifier [opt] = type-id
15612 template < template-parameter-list > class identifier [opt]
15613 template < template-parameter-list > class identifier [opt]
15614 = id-expression
15615
15616 GNU Extension (variadic templates):
15617
15618 type-parameter:
15619 class ... identifier [opt]
15620 typename ... identifier [opt]
15621
15622 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15623 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15624 the declaration of the parameter.
15625
15626 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15627
15628 static tree
15629 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15630 {
15631 cp_token *token;
15632 tree parameter;
15633
15634 /* Look for a keyword to tell us what kind of parameter this is. */
15635 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15636 if (!token)
15637 return error_mark_node;
15638
15639 switch (token->keyword)
15640 {
15641 case RID_CLASS:
15642 case RID_TYPENAME:
15643 {
15644 tree identifier;
15645 tree default_argument;
15646
15647 /* If the next token is an ellipsis, we have a template
15648 argument pack. */
15649 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15650 {
15651 /* Consume the `...' token. */
15652 cp_lexer_consume_token (parser->lexer);
15653 maybe_warn_variadic_templates ();
15654
15655 *is_parameter_pack = true;
15656 }
15657
15658 /* If the next token is an identifier, then it names the
15659 parameter. */
15660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15661 identifier = cp_parser_identifier (parser);
15662 else
15663 identifier = NULL_TREE;
15664
15665 /* Create the parameter. */
15666 parameter = finish_template_type_parm (class_type_node, identifier);
15667
15668 /* If the next token is an `=', we have a default argument. */
15669 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15670 {
15671 default_argument
15672 = cp_parser_default_type_template_argument (parser);
15673
15674 /* Template parameter packs cannot have default
15675 arguments. */
15676 if (*is_parameter_pack)
15677 {
15678 if (identifier)
15679 error_at (token->location,
15680 "template parameter pack %qD cannot have a "
15681 "default argument", identifier);
15682 else
15683 error_at (token->location,
15684 "template parameter packs cannot have "
15685 "default arguments");
15686 default_argument = NULL_TREE;
15687 }
15688 else if (check_for_bare_parameter_packs (default_argument))
15689 default_argument = error_mark_node;
15690 }
15691 else
15692 default_argument = NULL_TREE;
15693
15694 /* Create the combined representation of the parameter and the
15695 default argument. */
15696 parameter = build_tree_list (default_argument, parameter);
15697 }
15698 break;
15699
15700 case RID_TEMPLATE:
15701 {
15702 tree identifier;
15703 tree default_argument;
15704
15705 /* Look for the `<'. */
15706 cp_parser_require (parser, CPP_LESS, RT_LESS);
15707 /* Parse the template-parameter-list. */
15708 cp_parser_template_parameter_list (parser);
15709 /* Look for the `>'. */
15710 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15711
15712 // If template requirements are present, parse them.
15713 if (flag_concepts)
15714 {
15715 tree reqs = get_shorthand_constraints (current_template_parms);
15716 if (tree r = cp_parser_requires_clause_opt (parser))
15717 reqs = conjoin_constraints (reqs, normalize_expression (r));
15718 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15719 }
15720
15721 /* Look for the `class' or 'typename' keywords. */
15722 cp_parser_type_parameter_key (parser);
15723 /* If the next token is an ellipsis, we have a template
15724 argument pack. */
15725 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15726 {
15727 /* Consume the `...' token. */
15728 cp_lexer_consume_token (parser->lexer);
15729 maybe_warn_variadic_templates ();
15730
15731 *is_parameter_pack = true;
15732 }
15733 /* If the next token is an `=', then there is a
15734 default-argument. If the next token is a `>', we are at
15735 the end of the parameter-list. If the next token is a `,',
15736 then we are at the end of this parameter. */
15737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15738 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15739 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15740 {
15741 identifier = cp_parser_identifier (parser);
15742 /* Treat invalid names as if the parameter were nameless. */
15743 if (identifier == error_mark_node)
15744 identifier = NULL_TREE;
15745 }
15746 else
15747 identifier = NULL_TREE;
15748
15749 /* Create the template parameter. */
15750 parameter = finish_template_template_parm (class_type_node,
15751 identifier);
15752
15753 /* If the next token is an `=', then there is a
15754 default-argument. */
15755 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15756 {
15757 default_argument
15758 = cp_parser_default_template_template_argument (parser);
15759
15760 /* Template parameter packs cannot have default
15761 arguments. */
15762 if (*is_parameter_pack)
15763 {
15764 if (identifier)
15765 error_at (token->location,
15766 "template parameter pack %qD cannot "
15767 "have a default argument",
15768 identifier);
15769 else
15770 error_at (token->location, "template parameter packs cannot "
15771 "have default arguments");
15772 default_argument = NULL_TREE;
15773 }
15774 }
15775 else
15776 default_argument = NULL_TREE;
15777
15778 /* Create the combined representation of the parameter and the
15779 default argument. */
15780 parameter = build_tree_list (default_argument, parameter);
15781 }
15782 break;
15783
15784 default:
15785 gcc_unreachable ();
15786 break;
15787 }
15788
15789 return parameter;
15790 }
15791
15792 /* Parse a template-id.
15793
15794 template-id:
15795 template-name < template-argument-list [opt] >
15796
15797 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15798 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15799 returned. Otherwise, if the template-name names a function, or set
15800 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15801 names a class, returns a TYPE_DECL for the specialization.
15802
15803 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15804 uninstantiated templates. */
15805
15806 static tree
15807 cp_parser_template_id (cp_parser *parser,
15808 bool template_keyword_p,
15809 bool check_dependency_p,
15810 enum tag_types tag_type,
15811 bool is_declaration)
15812 {
15813 tree templ;
15814 tree arguments;
15815 tree template_id;
15816 cp_token_position start_of_id = 0;
15817 cp_token *next_token = NULL, *next_token_2 = NULL;
15818 bool is_identifier;
15819
15820 /* If the next token corresponds to a template-id, there is no need
15821 to reparse it. */
15822 cp_token *token = cp_lexer_peek_token (parser->lexer);
15823 if (token->type == CPP_TEMPLATE_ID)
15824 {
15825 cp_lexer_consume_token (parser->lexer);
15826 return saved_checks_value (token->u.tree_check_value);
15827 }
15828
15829 /* Avoid performing name lookup if there is no possibility of
15830 finding a template-id. */
15831 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15832 || (token->type == CPP_NAME
15833 && !cp_parser_nth_token_starts_template_argument_list_p
15834 (parser, 2)))
15835 {
15836 cp_parser_error (parser, "expected template-id");
15837 return error_mark_node;
15838 }
15839
15840 /* Remember where the template-id starts. */
15841 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15842 start_of_id = cp_lexer_token_position (parser->lexer, false);
15843
15844 push_deferring_access_checks (dk_deferred);
15845
15846 /* Parse the template-name. */
15847 is_identifier = false;
15848 templ = cp_parser_template_name (parser, template_keyword_p,
15849 check_dependency_p,
15850 is_declaration,
15851 tag_type,
15852 &is_identifier);
15853 if (templ == error_mark_node || is_identifier)
15854 {
15855 pop_deferring_access_checks ();
15856 return templ;
15857 }
15858
15859 /* Since we're going to preserve any side-effects from this parse, set up a
15860 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15861 in the template arguments. */
15862 tentative_firewall firewall (parser);
15863
15864 /* If we find the sequence `[:' after a template-name, it's probably
15865 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15866 parse correctly the argument list. */
15867 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15868 == CPP_OPEN_SQUARE)
15869 && next_token->flags & DIGRAPH
15870 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15871 == CPP_COLON)
15872 && !(next_token_2->flags & PREV_WHITE))
15873 {
15874 cp_parser_parse_tentatively (parser);
15875 /* Change `:' into `::'. */
15876 next_token_2->type = CPP_SCOPE;
15877 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15878 CPP_LESS. */
15879 cp_lexer_consume_token (parser->lexer);
15880
15881 /* Parse the arguments. */
15882 arguments = cp_parser_enclosed_template_argument_list (parser);
15883 if (!cp_parser_parse_definitely (parser))
15884 {
15885 /* If we couldn't parse an argument list, then we revert our changes
15886 and return simply an error. Maybe this is not a template-id
15887 after all. */
15888 next_token_2->type = CPP_COLON;
15889 cp_parser_error (parser, "expected %<<%>");
15890 pop_deferring_access_checks ();
15891 return error_mark_node;
15892 }
15893 /* Otherwise, emit an error about the invalid digraph, but continue
15894 parsing because we got our argument list. */
15895 if (permerror (next_token->location,
15896 "%<<::%> cannot begin a template-argument list"))
15897 {
15898 static bool hint = false;
15899 inform (next_token->location,
15900 "%<<:%> is an alternate spelling for %<[%>."
15901 " Insert whitespace between %<<%> and %<::%>");
15902 if (!hint && !flag_permissive)
15903 {
15904 inform (next_token->location, "(if you use %<-fpermissive%> "
15905 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15906 "accept your code)");
15907 hint = true;
15908 }
15909 }
15910 }
15911 else
15912 {
15913 /* Look for the `<' that starts the template-argument-list. */
15914 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15915 {
15916 pop_deferring_access_checks ();
15917 return error_mark_node;
15918 }
15919 /* Parse the arguments. */
15920 arguments = cp_parser_enclosed_template_argument_list (parser);
15921 }
15922
15923 /* Set the location to be of the form:
15924 template-name < template-argument-list [opt] >
15925 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15926 with caret == start at the start of the template-name,
15927 ranging until the closing '>'. */
15928 location_t finish_loc
15929 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15930 location_t combined_loc
15931 = make_location (token->location, token->location, finish_loc);
15932
15933 /* Check for concepts autos where they don't belong. We could
15934 identify types in some cases of idnetifier TEMPL, looking ahead
15935 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15936 types. We reject them in functions, but if what we have is an
15937 identifier, even with none_type we can't conclude it's NOT a
15938 type, we have to wait for template substitution. */
15939 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15940 template_id = error_mark_node;
15941 /* Build a representation of the specialization. */
15942 else if (identifier_p (templ))
15943 template_id = build_min_nt_loc (combined_loc,
15944 TEMPLATE_ID_EXPR,
15945 templ, arguments);
15946 else if (DECL_TYPE_TEMPLATE_P (templ)
15947 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15948 {
15949 bool entering_scope;
15950 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15951 template (rather than some instantiation thereof) only if
15952 is not nested within some other construct. For example, in
15953 "template <typename T> void f(T) { A<T>::", A<T> is just an
15954 instantiation of A. */
15955 entering_scope = (template_parm_scope_p ()
15956 && cp_lexer_next_token_is (parser->lexer,
15957 CPP_SCOPE));
15958 template_id
15959 = finish_template_type (templ, arguments, entering_scope);
15960 }
15961 /* A template-like identifier may be a partial concept id. */
15962 else if (flag_concepts
15963 && (template_id = (cp_parser_maybe_partial_concept_id
15964 (parser, templ, arguments))))
15965 return template_id;
15966 else if (variable_template_p (templ))
15967 {
15968 template_id = lookup_template_variable (templ, arguments);
15969 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15970 SET_EXPR_LOCATION (template_id, combined_loc);
15971 }
15972 else
15973 {
15974 /* If it's not a class-template or a template-template, it should be
15975 a function-template. */
15976 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15977 || TREE_CODE (templ) == OVERLOAD
15978 || BASELINK_P (templ)));
15979
15980 template_id = lookup_template_function (templ, arguments);
15981 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15982 SET_EXPR_LOCATION (template_id, combined_loc);
15983 }
15984
15985 /* If parsing tentatively, replace the sequence of tokens that makes
15986 up the template-id with a CPP_TEMPLATE_ID token. That way,
15987 should we re-parse the token stream, we will not have to repeat
15988 the effort required to do the parse, nor will we issue duplicate
15989 error messages about problems during instantiation of the
15990 template. */
15991 if (start_of_id
15992 /* Don't do this if we had a parse error in a declarator; re-parsing
15993 might succeed if a name changes meaning (60361). */
15994 && !(cp_parser_error_occurred (parser)
15995 && cp_parser_parsing_tentatively (parser)
15996 && parser->in_declarator_p))
15997 {
15998 /* Reset the contents of the START_OF_ID token. */
15999 token->type = CPP_TEMPLATE_ID;
16000 token->location = combined_loc;
16001
16002 /* We must mark the lookup as kept, so we don't throw it away on
16003 the first parse. */
16004 if (is_overloaded_fn (template_id))
16005 lookup_keep (get_fns (template_id), true);
16006
16007 /* Retrieve any deferred checks. Do not pop this access checks yet
16008 so the memory will not be reclaimed during token replacing below. */
16009 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16010 token->u.tree_check_value->value = template_id;
16011 token->u.tree_check_value->checks = get_deferred_access_checks ();
16012 token->keyword = RID_MAX;
16013
16014 /* Purge all subsequent tokens. */
16015 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16016
16017 /* ??? Can we actually assume that, if template_id ==
16018 error_mark_node, we will have issued a diagnostic to the
16019 user, as opposed to simply marking the tentative parse as
16020 failed? */
16021 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16022 error_at (token->location, "parse error in template argument list");
16023 }
16024
16025 pop_to_parent_deferring_access_checks ();
16026 return template_id;
16027 }
16028
16029 /* Parse a template-name.
16030
16031 template-name:
16032 identifier
16033
16034 The standard should actually say:
16035
16036 template-name:
16037 identifier
16038 operator-function-id
16039
16040 A defect report has been filed about this issue.
16041
16042 A conversion-function-id cannot be a template name because they cannot
16043 be part of a template-id. In fact, looking at this code:
16044
16045 a.operator K<int>()
16046
16047 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16048 It is impossible to call a templated conversion-function-id with an
16049 explicit argument list, since the only allowed template parameter is
16050 the type to which it is converting.
16051
16052 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16053 `template' keyword, in a construction like:
16054
16055 T::template f<3>()
16056
16057 In that case `f' is taken to be a template-name, even though there
16058 is no way of knowing for sure.
16059
16060 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16061 name refers to a set of overloaded functions, at least one of which
16062 is a template, or an IDENTIFIER_NODE with the name of the template,
16063 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16064 names are looked up inside uninstantiated templates. */
16065
16066 static tree
16067 cp_parser_template_name (cp_parser* parser,
16068 bool template_keyword_p,
16069 bool check_dependency_p,
16070 bool is_declaration,
16071 enum tag_types tag_type,
16072 bool *is_identifier)
16073 {
16074 tree identifier;
16075 tree decl;
16076 cp_token *token = cp_lexer_peek_token (parser->lexer);
16077
16078 /* If the next token is `operator', then we have either an
16079 operator-function-id or a conversion-function-id. */
16080 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16081 {
16082 /* We don't know whether we're looking at an
16083 operator-function-id or a conversion-function-id. */
16084 cp_parser_parse_tentatively (parser);
16085 /* Try an operator-function-id. */
16086 identifier = cp_parser_operator_function_id (parser);
16087 /* If that didn't work, try a conversion-function-id. */
16088 if (!cp_parser_parse_definitely (parser))
16089 {
16090 cp_parser_error (parser, "expected template-name");
16091 return error_mark_node;
16092 }
16093 }
16094 /* Look for the identifier. */
16095 else
16096 identifier = cp_parser_identifier (parser);
16097
16098 /* If we didn't find an identifier, we don't have a template-id. */
16099 if (identifier == error_mark_node)
16100 return error_mark_node;
16101
16102 /* If the name immediately followed the `template' keyword, then it
16103 is a template-name. However, if the next token is not `<', then
16104 we do not treat it as a template-name, since it is not being used
16105 as part of a template-id. This enables us to handle constructs
16106 like:
16107
16108 template <typename T> struct S { S(); };
16109 template <typename T> S<T>::S();
16110
16111 correctly. We would treat `S' as a template -- if it were `S<T>'
16112 -- but we do not if there is no `<'. */
16113
16114 if (processing_template_decl
16115 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16116 {
16117 /* In a declaration, in a dependent context, we pretend that the
16118 "template" keyword was present in order to improve error
16119 recovery. For example, given:
16120
16121 template <typename T> void f(T::X<int>);
16122
16123 we want to treat "X<int>" as a template-id. */
16124 if (is_declaration
16125 && !template_keyword_p
16126 && parser->scope && TYPE_P (parser->scope)
16127 && check_dependency_p
16128 && dependent_scope_p (parser->scope)
16129 /* Do not do this for dtors (or ctors), since they never
16130 need the template keyword before their name. */
16131 && !constructor_name_p (identifier, parser->scope))
16132 {
16133 cp_token_position start = 0;
16134
16135 /* Explain what went wrong. */
16136 error_at (token->location, "non-template %qD used as template",
16137 identifier);
16138 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16139 parser->scope, identifier);
16140 /* If parsing tentatively, find the location of the "<" token. */
16141 if (cp_parser_simulate_error (parser))
16142 start = cp_lexer_token_position (parser->lexer, true);
16143 /* Parse the template arguments so that we can issue error
16144 messages about them. */
16145 cp_lexer_consume_token (parser->lexer);
16146 cp_parser_enclosed_template_argument_list (parser);
16147 /* Skip tokens until we find a good place from which to
16148 continue parsing. */
16149 cp_parser_skip_to_closing_parenthesis (parser,
16150 /*recovering=*/true,
16151 /*or_comma=*/true,
16152 /*consume_paren=*/false);
16153 /* If parsing tentatively, permanently remove the
16154 template argument list. That will prevent duplicate
16155 error messages from being issued about the missing
16156 "template" keyword. */
16157 if (start)
16158 cp_lexer_purge_tokens_after (parser->lexer, start);
16159 if (is_identifier)
16160 *is_identifier = true;
16161 parser->context->object_type = NULL_TREE;
16162 return identifier;
16163 }
16164
16165 /* If the "template" keyword is present, then there is generally
16166 no point in doing name-lookup, so we just return IDENTIFIER.
16167 But, if the qualifying scope is non-dependent then we can
16168 (and must) do name-lookup normally. */
16169 if (template_keyword_p)
16170 {
16171 tree scope = (parser->scope ? parser->scope
16172 : parser->context->object_type);
16173 if (scope && TYPE_P (scope)
16174 && (!CLASS_TYPE_P (scope)
16175 || (check_dependency_p && dependent_type_p (scope))))
16176 {
16177 /* We're optimizing away the call to cp_parser_lookup_name, but
16178 we still need to do this. */
16179 parser->context->object_type = NULL_TREE;
16180 return identifier;
16181 }
16182 }
16183 }
16184
16185 /* Look up the name. */
16186 decl = cp_parser_lookup_name (parser, identifier,
16187 tag_type,
16188 /*is_template=*/true,
16189 /*is_namespace=*/false,
16190 check_dependency_p,
16191 /*ambiguous_decls=*/NULL,
16192 token->location);
16193
16194 decl = strip_using_decl (decl);
16195
16196 /* If DECL is a template, then the name was a template-name. */
16197 if (TREE_CODE (decl) == TEMPLATE_DECL)
16198 {
16199 if (TREE_DEPRECATED (decl)
16200 && deprecated_state != DEPRECATED_SUPPRESS)
16201 warn_deprecated_use (decl, NULL_TREE);
16202 }
16203 else
16204 {
16205 /* The standard does not explicitly indicate whether a name that
16206 names a set of overloaded declarations, some of which are
16207 templates, is a template-name. However, such a name should
16208 be a template-name; otherwise, there is no way to form a
16209 template-id for the overloaded templates. */
16210 bool found = false;
16211
16212 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16213 !found && iter; ++iter)
16214 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16215 found = true;
16216
16217 if (!found)
16218 {
16219 /* The name does not name a template. */
16220 cp_parser_error (parser, "expected template-name");
16221 return error_mark_node;
16222 }
16223 }
16224
16225 /* If DECL is dependent, and refers to a function, then just return
16226 its name; we will look it up again during template instantiation. */
16227 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16228 {
16229 tree scope = ovl_scope (decl);
16230 if (TYPE_P (scope) && dependent_type_p (scope))
16231 return identifier;
16232 }
16233
16234 return decl;
16235 }
16236
16237 /* Parse a template-argument-list.
16238
16239 template-argument-list:
16240 template-argument ... [opt]
16241 template-argument-list , template-argument ... [opt]
16242
16243 Returns a TREE_VEC containing the arguments. */
16244
16245 static tree
16246 cp_parser_template_argument_list (cp_parser* parser)
16247 {
16248 tree fixed_args[10];
16249 unsigned n_args = 0;
16250 unsigned alloced = 10;
16251 tree *arg_ary = fixed_args;
16252 tree vec;
16253 bool saved_in_template_argument_list_p;
16254 bool saved_ice_p;
16255 bool saved_non_ice_p;
16256
16257 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16258 parser->in_template_argument_list_p = true;
16259 /* Even if the template-id appears in an integral
16260 constant-expression, the contents of the argument list do
16261 not. */
16262 saved_ice_p = parser->integral_constant_expression_p;
16263 parser->integral_constant_expression_p = false;
16264 saved_non_ice_p = parser->non_integral_constant_expression_p;
16265 parser->non_integral_constant_expression_p = false;
16266
16267 /* Parse the arguments. */
16268 do
16269 {
16270 tree argument;
16271
16272 if (n_args)
16273 /* Consume the comma. */
16274 cp_lexer_consume_token (parser->lexer);
16275
16276 /* Parse the template-argument. */
16277 argument = cp_parser_template_argument (parser);
16278
16279 /* If the next token is an ellipsis, we're expanding a template
16280 argument pack. */
16281 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16282 {
16283 if (argument == error_mark_node)
16284 {
16285 cp_token *token = cp_lexer_peek_token (parser->lexer);
16286 error_at (token->location,
16287 "expected parameter pack before %<...%>");
16288 }
16289 /* Consume the `...' token. */
16290 cp_lexer_consume_token (parser->lexer);
16291
16292 /* Make the argument into a TYPE_PACK_EXPANSION or
16293 EXPR_PACK_EXPANSION. */
16294 argument = make_pack_expansion (argument);
16295 }
16296
16297 if (n_args == alloced)
16298 {
16299 alloced *= 2;
16300
16301 if (arg_ary == fixed_args)
16302 {
16303 arg_ary = XNEWVEC (tree, alloced);
16304 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16305 }
16306 else
16307 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16308 }
16309 arg_ary[n_args++] = argument;
16310 }
16311 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16312
16313 vec = make_tree_vec (n_args);
16314
16315 while (n_args--)
16316 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16317
16318 if (arg_ary != fixed_args)
16319 free (arg_ary);
16320 parser->non_integral_constant_expression_p = saved_non_ice_p;
16321 parser->integral_constant_expression_p = saved_ice_p;
16322 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16323 if (CHECKING_P)
16324 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16325 return vec;
16326 }
16327
16328 /* Parse a template-argument.
16329
16330 template-argument:
16331 assignment-expression
16332 type-id
16333 id-expression
16334
16335 The representation is that of an assignment-expression, type-id, or
16336 id-expression -- except that the qualified id-expression is
16337 evaluated, so that the value returned is either a DECL or an
16338 OVERLOAD.
16339
16340 Although the standard says "assignment-expression", it forbids
16341 throw-expressions or assignments in the template argument.
16342 Therefore, we use "conditional-expression" instead. */
16343
16344 static tree
16345 cp_parser_template_argument (cp_parser* parser)
16346 {
16347 tree argument;
16348 bool template_p;
16349 bool address_p;
16350 bool maybe_type_id = false;
16351 cp_token *token = NULL, *argument_start_token = NULL;
16352 location_t loc = 0;
16353 cp_id_kind idk;
16354
16355 /* There's really no way to know what we're looking at, so we just
16356 try each alternative in order.
16357
16358 [temp.arg]
16359
16360 In a template-argument, an ambiguity between a type-id and an
16361 expression is resolved to a type-id, regardless of the form of
16362 the corresponding template-parameter.
16363
16364 Therefore, we try a type-id first. */
16365 cp_parser_parse_tentatively (parser);
16366 argument = cp_parser_template_type_arg (parser);
16367 /* If there was no error parsing the type-id but the next token is a
16368 '>>', our behavior depends on which dialect of C++ we're
16369 parsing. In C++98, we probably found a typo for '> >'. But there
16370 are type-id which are also valid expressions. For instance:
16371
16372 struct X { int operator >> (int); };
16373 template <int V> struct Foo {};
16374 Foo<X () >> 5> r;
16375
16376 Here 'X()' is a valid type-id of a function type, but the user just
16377 wanted to write the expression "X() >> 5". Thus, we remember that we
16378 found a valid type-id, but we still try to parse the argument as an
16379 expression to see what happens.
16380
16381 In C++0x, the '>>' will be considered two separate '>'
16382 tokens. */
16383 if (!cp_parser_error_occurred (parser)
16384 && cxx_dialect == cxx98
16385 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16386 {
16387 maybe_type_id = true;
16388 cp_parser_abort_tentative_parse (parser);
16389 }
16390 else
16391 {
16392 /* If the next token isn't a `,' or a `>', then this argument wasn't
16393 really finished. This means that the argument is not a valid
16394 type-id. */
16395 if (!cp_parser_next_token_ends_template_argument_p (parser))
16396 cp_parser_error (parser, "expected template-argument");
16397 /* If that worked, we're done. */
16398 if (cp_parser_parse_definitely (parser))
16399 return argument;
16400 }
16401 /* We're still not sure what the argument will be. */
16402 cp_parser_parse_tentatively (parser);
16403 /* Try a template. */
16404 argument_start_token = cp_lexer_peek_token (parser->lexer);
16405 argument = cp_parser_id_expression (parser,
16406 /*template_keyword_p=*/false,
16407 /*check_dependency_p=*/true,
16408 &template_p,
16409 /*declarator_p=*/false,
16410 /*optional_p=*/false);
16411 /* If the next token isn't a `,' or a `>', then this argument wasn't
16412 really finished. */
16413 if (!cp_parser_next_token_ends_template_argument_p (parser))
16414 cp_parser_error (parser, "expected template-argument");
16415 if (!cp_parser_error_occurred (parser))
16416 {
16417 /* Figure out what is being referred to. If the id-expression
16418 was for a class template specialization, then we will have a
16419 TYPE_DECL at this point. There is no need to do name lookup
16420 at this point in that case. */
16421 if (TREE_CODE (argument) != TYPE_DECL)
16422 argument = cp_parser_lookup_name (parser, argument,
16423 none_type,
16424 /*is_template=*/template_p,
16425 /*is_namespace=*/false,
16426 /*check_dependency=*/true,
16427 /*ambiguous_decls=*/NULL,
16428 argument_start_token->location);
16429 /* Handle a constrained-type-specifier for a non-type template
16430 parameter. */
16431 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16432 argument = decl;
16433 else if (TREE_CODE (argument) != TEMPLATE_DECL
16434 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16435 cp_parser_error (parser, "expected template-name");
16436 }
16437 if (cp_parser_parse_definitely (parser))
16438 {
16439 if (TREE_DEPRECATED (argument))
16440 warn_deprecated_use (argument, NULL_TREE);
16441 return argument;
16442 }
16443 /* It must be a non-type argument. In C++17 any constant-expression is
16444 allowed. */
16445 if (cxx_dialect > cxx14)
16446 goto general_expr;
16447
16448 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16449
16450 -- an integral constant-expression of integral or enumeration
16451 type; or
16452
16453 -- the name of a non-type template-parameter; or
16454
16455 -- the name of an object or function with external linkage...
16456
16457 -- the address of an object or function with external linkage...
16458
16459 -- a pointer to member... */
16460 /* Look for a non-type template parameter. */
16461 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16462 {
16463 cp_parser_parse_tentatively (parser);
16464 argument = cp_parser_primary_expression (parser,
16465 /*address_p=*/false,
16466 /*cast_p=*/false,
16467 /*template_arg_p=*/true,
16468 &idk);
16469 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16470 || !cp_parser_next_token_ends_template_argument_p (parser))
16471 cp_parser_simulate_error (parser);
16472 if (cp_parser_parse_definitely (parser))
16473 return argument;
16474 }
16475
16476 /* If the next token is "&", the argument must be the address of an
16477 object or function with external linkage. */
16478 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16479 if (address_p)
16480 {
16481 loc = cp_lexer_peek_token (parser->lexer)->location;
16482 cp_lexer_consume_token (parser->lexer);
16483 }
16484 /* See if we might have an id-expression. */
16485 token = cp_lexer_peek_token (parser->lexer);
16486 if (token->type == CPP_NAME
16487 || token->keyword == RID_OPERATOR
16488 || token->type == CPP_SCOPE
16489 || token->type == CPP_TEMPLATE_ID
16490 || token->type == CPP_NESTED_NAME_SPECIFIER)
16491 {
16492 cp_parser_parse_tentatively (parser);
16493 argument = cp_parser_primary_expression (parser,
16494 address_p,
16495 /*cast_p=*/false,
16496 /*template_arg_p=*/true,
16497 &idk);
16498 if (cp_parser_error_occurred (parser)
16499 || !cp_parser_next_token_ends_template_argument_p (parser))
16500 cp_parser_abort_tentative_parse (parser);
16501 else
16502 {
16503 tree probe;
16504
16505 if (INDIRECT_REF_P (argument))
16506 {
16507 /* Strip the dereference temporarily. */
16508 gcc_assert (REFERENCE_REF_P (argument));
16509 argument = TREE_OPERAND (argument, 0);
16510 }
16511
16512 /* If we're in a template, we represent a qualified-id referring
16513 to a static data member as a SCOPE_REF even if the scope isn't
16514 dependent so that we can check access control later. */
16515 probe = argument;
16516 if (TREE_CODE (probe) == SCOPE_REF)
16517 probe = TREE_OPERAND (probe, 1);
16518 if (VAR_P (probe))
16519 {
16520 /* A variable without external linkage might still be a
16521 valid constant-expression, so no error is issued here
16522 if the external-linkage check fails. */
16523 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16524 cp_parser_simulate_error (parser);
16525 }
16526 else if (is_overloaded_fn (argument))
16527 /* All overloaded functions are allowed; if the external
16528 linkage test does not pass, an error will be issued
16529 later. */
16530 ;
16531 else if (address_p
16532 && (TREE_CODE (argument) == OFFSET_REF
16533 || TREE_CODE (argument) == SCOPE_REF))
16534 /* A pointer-to-member. */
16535 ;
16536 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16537 ;
16538 else
16539 cp_parser_simulate_error (parser);
16540
16541 if (cp_parser_parse_definitely (parser))
16542 {
16543 if (address_p)
16544 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16545 tf_warning_or_error);
16546 else
16547 argument = convert_from_reference (argument);
16548 return argument;
16549 }
16550 }
16551 }
16552 /* If the argument started with "&", there are no other valid
16553 alternatives at this point. */
16554 if (address_p)
16555 {
16556 cp_parser_error (parser, "invalid non-type template argument");
16557 return error_mark_node;
16558 }
16559
16560 general_expr:
16561 /* If the argument wasn't successfully parsed as a type-id followed
16562 by '>>', the argument can only be a constant expression now.
16563 Otherwise, we try parsing the constant-expression tentatively,
16564 because the argument could really be a type-id. */
16565 if (maybe_type_id)
16566 cp_parser_parse_tentatively (parser);
16567
16568 if (cxx_dialect <= cxx14)
16569 argument = cp_parser_constant_expression (parser);
16570 else
16571 {
16572 /* With C++17 generalized non-type template arguments we need to handle
16573 lvalue constant expressions, too. */
16574 argument = cp_parser_assignment_expression (parser);
16575 require_potential_constant_expression (argument);
16576 }
16577
16578 if (!maybe_type_id)
16579 return argument;
16580 if (!cp_parser_next_token_ends_template_argument_p (parser))
16581 cp_parser_error (parser, "expected template-argument");
16582 if (cp_parser_parse_definitely (parser))
16583 return argument;
16584 /* We did our best to parse the argument as a non type-id, but that
16585 was the only alternative that matched (albeit with a '>' after
16586 it). We can assume it's just a typo from the user, and a
16587 diagnostic will then be issued. */
16588 return cp_parser_template_type_arg (parser);
16589 }
16590
16591 /* Parse an explicit-instantiation.
16592
16593 explicit-instantiation:
16594 template declaration
16595
16596 Although the standard says `declaration', what it really means is:
16597
16598 explicit-instantiation:
16599 template decl-specifier-seq [opt] declarator [opt] ;
16600
16601 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16602 supposed to be allowed. A defect report has been filed about this
16603 issue.
16604
16605 GNU Extension:
16606
16607 explicit-instantiation:
16608 storage-class-specifier template
16609 decl-specifier-seq [opt] declarator [opt] ;
16610 function-specifier template
16611 decl-specifier-seq [opt] declarator [opt] ; */
16612
16613 static void
16614 cp_parser_explicit_instantiation (cp_parser* parser)
16615 {
16616 int declares_class_or_enum;
16617 cp_decl_specifier_seq decl_specifiers;
16618 tree extension_specifier = NULL_TREE;
16619
16620 timevar_push (TV_TEMPLATE_INST);
16621
16622 /* Look for an (optional) storage-class-specifier or
16623 function-specifier. */
16624 if (cp_parser_allow_gnu_extensions_p (parser))
16625 {
16626 extension_specifier
16627 = cp_parser_storage_class_specifier_opt (parser);
16628 if (!extension_specifier)
16629 extension_specifier
16630 = cp_parser_function_specifier_opt (parser,
16631 /*decl_specs=*/NULL);
16632 }
16633
16634 /* Look for the `template' keyword. */
16635 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16636 /* Let the front end know that we are processing an explicit
16637 instantiation. */
16638 begin_explicit_instantiation ();
16639 /* [temp.explicit] says that we are supposed to ignore access
16640 control while processing explicit instantiation directives. */
16641 push_deferring_access_checks (dk_no_check);
16642 /* Parse a decl-specifier-seq. */
16643 cp_parser_decl_specifier_seq (parser,
16644 CP_PARSER_FLAGS_OPTIONAL,
16645 &decl_specifiers,
16646 &declares_class_or_enum);
16647 /* If there was exactly one decl-specifier, and it declared a class,
16648 and there's no declarator, then we have an explicit type
16649 instantiation. */
16650 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16651 {
16652 tree type;
16653
16654 type = check_tag_decl (&decl_specifiers,
16655 /*explicit_type_instantiation_p=*/true);
16656 /* Turn access control back on for names used during
16657 template instantiation. */
16658 pop_deferring_access_checks ();
16659 if (type)
16660 do_type_instantiation (type, extension_specifier,
16661 /*complain=*/tf_error);
16662 }
16663 else
16664 {
16665 cp_declarator *declarator;
16666 tree decl;
16667
16668 /* Parse the declarator. */
16669 declarator
16670 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16671 /*ctor_dtor_or_conv_p=*/NULL,
16672 /*parenthesized_p=*/NULL,
16673 /*member_p=*/false,
16674 /*friend_p=*/false);
16675 if (declares_class_or_enum & 2)
16676 cp_parser_check_for_definition_in_return_type (declarator,
16677 decl_specifiers.type,
16678 decl_specifiers.locations[ds_type_spec]);
16679 if (declarator != cp_error_declarator)
16680 {
16681 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16682 permerror (decl_specifiers.locations[ds_inline],
16683 "explicit instantiation shall not use"
16684 " %<inline%> specifier");
16685 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16686 permerror (decl_specifiers.locations[ds_constexpr],
16687 "explicit instantiation shall not use"
16688 " %<constexpr%> specifier");
16689
16690 decl = grokdeclarator (declarator, &decl_specifiers,
16691 NORMAL, 0, &decl_specifiers.attributes);
16692 /* Turn access control back on for names used during
16693 template instantiation. */
16694 pop_deferring_access_checks ();
16695 /* Do the explicit instantiation. */
16696 do_decl_instantiation (decl, extension_specifier);
16697 }
16698 else
16699 {
16700 pop_deferring_access_checks ();
16701 /* Skip the body of the explicit instantiation. */
16702 cp_parser_skip_to_end_of_statement (parser);
16703 }
16704 }
16705 /* We're done with the instantiation. */
16706 end_explicit_instantiation ();
16707
16708 cp_parser_consume_semicolon_at_end_of_statement (parser);
16709
16710 timevar_pop (TV_TEMPLATE_INST);
16711 }
16712
16713 /* Parse an explicit-specialization.
16714
16715 explicit-specialization:
16716 template < > declaration
16717
16718 Although the standard says `declaration', what it really means is:
16719
16720 explicit-specialization:
16721 template <> decl-specifier [opt] init-declarator [opt] ;
16722 template <> function-definition
16723 template <> explicit-specialization
16724 template <> template-declaration */
16725
16726 static void
16727 cp_parser_explicit_specialization (cp_parser* parser)
16728 {
16729 bool need_lang_pop;
16730 cp_token *token = cp_lexer_peek_token (parser->lexer);
16731
16732 /* Look for the `template' keyword. */
16733 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16734 /* Look for the `<'. */
16735 cp_parser_require (parser, CPP_LESS, RT_LESS);
16736 /* Look for the `>'. */
16737 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16738 /* We have processed another parameter list. */
16739 ++parser->num_template_parameter_lists;
16740 /* [temp]
16741
16742 A template ... explicit specialization ... shall not have C
16743 linkage. */
16744 if (current_lang_name == lang_name_c)
16745 {
16746 error_at (token->location, "template specialization with C linkage");
16747 maybe_show_extern_c_location ();
16748 /* Give it C++ linkage to avoid confusing other parts of the
16749 front end. */
16750 push_lang_context (lang_name_cplusplus);
16751 need_lang_pop = true;
16752 }
16753 else
16754 need_lang_pop = false;
16755 /* Let the front end know that we are beginning a specialization. */
16756 if (!begin_specialization ())
16757 {
16758 end_specialization ();
16759 return;
16760 }
16761
16762 /* If the next keyword is `template', we need to figure out whether
16763 or not we're looking a template-declaration. */
16764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16765 {
16766 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16767 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16768 cp_parser_template_declaration_after_export (parser,
16769 /*member_p=*/false);
16770 else
16771 cp_parser_explicit_specialization (parser);
16772 }
16773 else
16774 /* Parse the dependent declaration. */
16775 cp_parser_single_declaration (parser,
16776 /*checks=*/NULL,
16777 /*member_p=*/false,
16778 /*explicit_specialization_p=*/true,
16779 /*friend_p=*/NULL);
16780 /* We're done with the specialization. */
16781 end_specialization ();
16782 /* For the erroneous case of a template with C linkage, we pushed an
16783 implicit C++ linkage scope; exit that scope now. */
16784 if (need_lang_pop)
16785 pop_lang_context ();
16786 /* We're done with this parameter list. */
16787 --parser->num_template_parameter_lists;
16788 }
16789
16790 /* Parse a type-specifier.
16791
16792 type-specifier:
16793 simple-type-specifier
16794 class-specifier
16795 enum-specifier
16796 elaborated-type-specifier
16797 cv-qualifier
16798
16799 GNU Extension:
16800
16801 type-specifier:
16802 __complex__
16803
16804 Returns a representation of the type-specifier. For a
16805 class-specifier, enum-specifier, or elaborated-type-specifier, a
16806 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16807
16808 The parser flags FLAGS is used to control type-specifier parsing.
16809
16810 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16811 in a decl-specifier-seq.
16812
16813 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16814 class-specifier, enum-specifier, or elaborated-type-specifier, then
16815 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16816 if a type is declared; 2 if it is defined. Otherwise, it is set to
16817 zero.
16818
16819 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16820 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16821 is set to FALSE. */
16822
16823 static tree
16824 cp_parser_type_specifier (cp_parser* parser,
16825 cp_parser_flags flags,
16826 cp_decl_specifier_seq *decl_specs,
16827 bool is_declaration,
16828 int* declares_class_or_enum,
16829 bool* is_cv_qualifier)
16830 {
16831 tree type_spec = NULL_TREE;
16832 cp_token *token;
16833 enum rid keyword;
16834 cp_decl_spec ds = ds_last;
16835
16836 /* Assume this type-specifier does not declare a new type. */
16837 if (declares_class_or_enum)
16838 *declares_class_or_enum = 0;
16839 /* And that it does not specify a cv-qualifier. */
16840 if (is_cv_qualifier)
16841 *is_cv_qualifier = false;
16842 /* Peek at the next token. */
16843 token = cp_lexer_peek_token (parser->lexer);
16844
16845 /* If we're looking at a keyword, we can use that to guide the
16846 production we choose. */
16847 keyword = token->keyword;
16848 switch (keyword)
16849 {
16850 case RID_ENUM:
16851 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16852 goto elaborated_type_specifier;
16853
16854 /* Look for the enum-specifier. */
16855 type_spec = cp_parser_enum_specifier (parser);
16856 /* If that worked, we're done. */
16857 if (type_spec)
16858 {
16859 if (declares_class_or_enum)
16860 *declares_class_or_enum = 2;
16861 if (decl_specs)
16862 cp_parser_set_decl_spec_type (decl_specs,
16863 type_spec,
16864 token,
16865 /*type_definition_p=*/true);
16866 return type_spec;
16867 }
16868 else
16869 goto elaborated_type_specifier;
16870
16871 /* Any of these indicate either a class-specifier, or an
16872 elaborated-type-specifier. */
16873 case RID_CLASS:
16874 case RID_STRUCT:
16875 case RID_UNION:
16876 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16877 goto elaborated_type_specifier;
16878
16879 /* Parse tentatively so that we can back up if we don't find a
16880 class-specifier. */
16881 cp_parser_parse_tentatively (parser);
16882 /* Look for the class-specifier. */
16883 type_spec = cp_parser_class_specifier (parser);
16884 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16885 /* If that worked, we're done. */
16886 if (cp_parser_parse_definitely (parser))
16887 {
16888 if (declares_class_or_enum)
16889 *declares_class_or_enum = 2;
16890 if (decl_specs)
16891 cp_parser_set_decl_spec_type (decl_specs,
16892 type_spec,
16893 token,
16894 /*type_definition_p=*/true);
16895 return type_spec;
16896 }
16897
16898 /* Fall through. */
16899 elaborated_type_specifier:
16900 /* We're declaring (not defining) a class or enum. */
16901 if (declares_class_or_enum)
16902 *declares_class_or_enum = 1;
16903
16904 /* Fall through. */
16905 case RID_TYPENAME:
16906 /* Look for an elaborated-type-specifier. */
16907 type_spec
16908 = (cp_parser_elaborated_type_specifier
16909 (parser,
16910 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16911 is_declaration));
16912 if (decl_specs)
16913 cp_parser_set_decl_spec_type (decl_specs,
16914 type_spec,
16915 token,
16916 /*type_definition_p=*/false);
16917 return type_spec;
16918
16919 case RID_CONST:
16920 ds = ds_const;
16921 if (is_cv_qualifier)
16922 *is_cv_qualifier = true;
16923 break;
16924
16925 case RID_VOLATILE:
16926 ds = ds_volatile;
16927 if (is_cv_qualifier)
16928 *is_cv_qualifier = true;
16929 break;
16930
16931 case RID_RESTRICT:
16932 ds = ds_restrict;
16933 if (is_cv_qualifier)
16934 *is_cv_qualifier = true;
16935 break;
16936
16937 case RID_COMPLEX:
16938 /* The `__complex__' keyword is a GNU extension. */
16939 ds = ds_complex;
16940 break;
16941
16942 default:
16943 break;
16944 }
16945
16946 /* Handle simple keywords. */
16947 if (ds != ds_last)
16948 {
16949 if (decl_specs)
16950 {
16951 set_and_check_decl_spec_loc (decl_specs, ds, token);
16952 decl_specs->any_specifiers_p = true;
16953 }
16954 return cp_lexer_consume_token (parser->lexer)->u.value;
16955 }
16956
16957 /* If we do not already have a type-specifier, assume we are looking
16958 at a simple-type-specifier. */
16959 type_spec = cp_parser_simple_type_specifier (parser,
16960 decl_specs,
16961 flags);
16962
16963 /* If we didn't find a type-specifier, and a type-specifier was not
16964 optional in this context, issue an error message. */
16965 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16966 {
16967 cp_parser_error (parser, "expected type specifier");
16968 return error_mark_node;
16969 }
16970
16971 return type_spec;
16972 }
16973
16974 /* Parse a simple-type-specifier.
16975
16976 simple-type-specifier:
16977 :: [opt] nested-name-specifier [opt] type-name
16978 :: [opt] nested-name-specifier template template-id
16979 char
16980 wchar_t
16981 bool
16982 short
16983 int
16984 long
16985 signed
16986 unsigned
16987 float
16988 double
16989 void
16990
16991 C++11 Extension:
16992
16993 simple-type-specifier:
16994 auto
16995 decltype ( expression )
16996 char16_t
16997 char32_t
16998 __underlying_type ( type-id )
16999
17000 C++17 extension:
17001
17002 nested-name-specifier(opt) template-name
17003
17004 GNU Extension:
17005
17006 simple-type-specifier:
17007 __int128
17008 __typeof__ unary-expression
17009 __typeof__ ( type-id )
17010 __typeof__ ( type-id ) { initializer-list , [opt] }
17011
17012 Concepts Extension:
17013
17014 simple-type-specifier:
17015 constrained-type-specifier
17016
17017 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17018 appropriately updated. */
17019
17020 static tree
17021 cp_parser_simple_type_specifier (cp_parser* parser,
17022 cp_decl_specifier_seq *decl_specs,
17023 cp_parser_flags flags)
17024 {
17025 tree type = NULL_TREE;
17026 cp_token *token;
17027 int idx;
17028
17029 /* Peek at the next token. */
17030 token = cp_lexer_peek_token (parser->lexer);
17031
17032 /* If we're looking at a keyword, things are easy. */
17033 switch (token->keyword)
17034 {
17035 case RID_CHAR:
17036 if (decl_specs)
17037 decl_specs->explicit_char_p = true;
17038 type = char_type_node;
17039 break;
17040 case RID_CHAR16:
17041 type = char16_type_node;
17042 break;
17043 case RID_CHAR32:
17044 type = char32_type_node;
17045 break;
17046 case RID_WCHAR:
17047 type = wchar_type_node;
17048 break;
17049 case RID_BOOL:
17050 type = boolean_type_node;
17051 break;
17052 case RID_SHORT:
17053 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17054 type = short_integer_type_node;
17055 break;
17056 case RID_INT:
17057 if (decl_specs)
17058 decl_specs->explicit_int_p = true;
17059 type = integer_type_node;
17060 break;
17061 case RID_INT_N_0:
17062 case RID_INT_N_1:
17063 case RID_INT_N_2:
17064 case RID_INT_N_3:
17065 idx = token->keyword - RID_INT_N_0;
17066 if (! int_n_enabled_p [idx])
17067 break;
17068 if (decl_specs)
17069 {
17070 decl_specs->explicit_intN_p = true;
17071 decl_specs->int_n_idx = idx;
17072 }
17073 type = int_n_trees [idx].signed_type;
17074 break;
17075 case RID_LONG:
17076 if (decl_specs)
17077 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17078 type = long_integer_type_node;
17079 break;
17080 case RID_SIGNED:
17081 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17082 type = integer_type_node;
17083 break;
17084 case RID_UNSIGNED:
17085 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17086 type = unsigned_type_node;
17087 break;
17088 case RID_FLOAT:
17089 type = float_type_node;
17090 break;
17091 case RID_DOUBLE:
17092 type = double_type_node;
17093 break;
17094 case RID_VOID:
17095 type = void_type_node;
17096 break;
17097
17098 case RID_AUTO:
17099 maybe_warn_cpp0x (CPP0X_AUTO);
17100 if (parser->auto_is_implicit_function_template_parm_p)
17101 {
17102 /* The 'auto' might be the placeholder return type for a function decl
17103 with trailing return type. */
17104 bool have_trailing_return_fn_decl = false;
17105
17106 cp_parser_parse_tentatively (parser);
17107 cp_lexer_consume_token (parser->lexer);
17108 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17109 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17110 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17111 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17112 {
17113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17114 {
17115 cp_lexer_consume_token (parser->lexer);
17116 cp_parser_skip_to_closing_parenthesis (parser,
17117 /*recovering*/false,
17118 /*or_comma*/false,
17119 /*consume_paren*/true);
17120 continue;
17121 }
17122
17123 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17124 {
17125 have_trailing_return_fn_decl = true;
17126 break;
17127 }
17128
17129 cp_lexer_consume_token (parser->lexer);
17130 }
17131 cp_parser_abort_tentative_parse (parser);
17132
17133 if (have_trailing_return_fn_decl)
17134 {
17135 type = make_auto ();
17136 break;
17137 }
17138
17139 if (cxx_dialect >= cxx14)
17140 {
17141 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17142 type = TREE_TYPE (type);
17143 }
17144 else
17145 type = error_mark_node;
17146
17147 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17148 {
17149 if (cxx_dialect < cxx14)
17150 error_at (token->location,
17151 "use of %<auto%> in lambda parameter declaration "
17152 "only available with "
17153 "-std=c++14 or -std=gnu++14");
17154 }
17155 else if (cxx_dialect < cxx14)
17156 error_at (token->location,
17157 "use of %<auto%> in parameter declaration "
17158 "only available with "
17159 "-std=c++14 or -std=gnu++14");
17160 else if (!flag_concepts)
17161 pedwarn (token->location, 0,
17162 "use of %<auto%> in parameter declaration "
17163 "only available with -fconcepts");
17164 }
17165 else
17166 type = make_auto ();
17167 break;
17168
17169 case RID_DECLTYPE:
17170 /* Since DR 743, decltype can either be a simple-type-specifier by
17171 itself or begin a nested-name-specifier. Parsing it will replace
17172 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17173 handling below decide what to do. */
17174 cp_parser_decltype (parser);
17175 cp_lexer_set_token_position (parser->lexer, token);
17176 break;
17177
17178 case RID_TYPEOF:
17179 /* Consume the `typeof' token. */
17180 cp_lexer_consume_token (parser->lexer);
17181 /* Parse the operand to `typeof'. */
17182 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17183 /* If it is not already a TYPE, take its type. */
17184 if (!TYPE_P (type))
17185 type = finish_typeof (type);
17186
17187 if (decl_specs)
17188 cp_parser_set_decl_spec_type (decl_specs, type,
17189 token,
17190 /*type_definition_p=*/false);
17191
17192 return type;
17193
17194 case RID_UNDERLYING_TYPE:
17195 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17196 if (decl_specs)
17197 cp_parser_set_decl_spec_type (decl_specs, type,
17198 token,
17199 /*type_definition_p=*/false);
17200
17201 return type;
17202
17203 case RID_BASES:
17204 case RID_DIRECT_BASES:
17205 type = cp_parser_trait_expr (parser, token->keyword);
17206 if (decl_specs)
17207 cp_parser_set_decl_spec_type (decl_specs, type,
17208 token,
17209 /*type_definition_p=*/false);
17210 return type;
17211 default:
17212 break;
17213 }
17214
17215 /* If token is an already-parsed decltype not followed by ::,
17216 it's a simple-type-specifier. */
17217 if (token->type == CPP_DECLTYPE
17218 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17219 {
17220 type = saved_checks_value (token->u.tree_check_value);
17221 if (decl_specs)
17222 {
17223 cp_parser_set_decl_spec_type (decl_specs, type,
17224 token,
17225 /*type_definition_p=*/false);
17226 /* Remember that we are handling a decltype in order to
17227 implement the resolution of DR 1510 when the argument
17228 isn't instantiation dependent. */
17229 decl_specs->decltype_p = true;
17230 }
17231 cp_lexer_consume_token (parser->lexer);
17232 return type;
17233 }
17234
17235 /* If the type-specifier was for a built-in type, we're done. */
17236 if (type)
17237 {
17238 /* Record the type. */
17239 if (decl_specs
17240 && (token->keyword != RID_SIGNED
17241 && token->keyword != RID_UNSIGNED
17242 && token->keyword != RID_SHORT
17243 && token->keyword != RID_LONG))
17244 cp_parser_set_decl_spec_type (decl_specs,
17245 type,
17246 token,
17247 /*type_definition_p=*/false);
17248 if (decl_specs)
17249 decl_specs->any_specifiers_p = true;
17250
17251 /* Consume the token. */
17252 cp_lexer_consume_token (parser->lexer);
17253
17254 if (type == error_mark_node)
17255 return error_mark_node;
17256
17257 /* There is no valid C++ program where a non-template type is
17258 followed by a "<". That usually indicates that the user thought
17259 that the type was a template. */
17260 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17261 token->location);
17262
17263 return TYPE_NAME (type);
17264 }
17265
17266 /* The type-specifier must be a user-defined type. */
17267 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17268 {
17269 bool qualified_p;
17270 bool global_p;
17271
17272 /* Don't gobble tokens or issue error messages if this is an
17273 optional type-specifier. */
17274 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17275 cp_parser_parse_tentatively (parser);
17276
17277 token = cp_lexer_peek_token (parser->lexer);
17278
17279 /* Look for the optional `::' operator. */
17280 global_p
17281 = (cp_parser_global_scope_opt (parser,
17282 /*current_scope_valid_p=*/false)
17283 != NULL_TREE);
17284 /* Look for the nested-name specifier. */
17285 qualified_p
17286 = (cp_parser_nested_name_specifier_opt (parser,
17287 /*typename_keyword_p=*/false,
17288 /*check_dependency_p=*/true,
17289 /*type_p=*/false,
17290 /*is_declaration=*/false)
17291 != NULL_TREE);
17292 /* If we have seen a nested-name-specifier, and the next token
17293 is `template', then we are using the template-id production. */
17294 if (parser->scope
17295 && cp_parser_optional_template_keyword (parser))
17296 {
17297 /* Look for the template-id. */
17298 type = cp_parser_template_id (parser,
17299 /*template_keyword_p=*/true,
17300 /*check_dependency_p=*/true,
17301 none_type,
17302 /*is_declaration=*/false);
17303 /* If the template-id did not name a type, we are out of
17304 luck. */
17305 if (TREE_CODE (type) != TYPE_DECL)
17306 {
17307 cp_parser_error (parser, "expected template-id for type");
17308 type = NULL_TREE;
17309 }
17310 }
17311 /* Otherwise, look for a type-name. */
17312 else
17313 type = cp_parser_type_name (parser);
17314 /* Keep track of all name-lookups performed in class scopes. */
17315 if (type
17316 && !global_p
17317 && !qualified_p
17318 && TREE_CODE (type) == TYPE_DECL
17319 && identifier_p (DECL_NAME (type)))
17320 maybe_note_name_used_in_class (DECL_NAME (type), type);
17321 /* If it didn't work out, we don't have a TYPE. */
17322 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17323 && !cp_parser_parse_definitely (parser))
17324 type = NULL_TREE;
17325 if (!type && cxx_dialect >= cxx17)
17326 {
17327 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17328 cp_parser_parse_tentatively (parser);
17329
17330 cp_parser_global_scope_opt (parser,
17331 /*current_scope_valid_p=*/false);
17332 cp_parser_nested_name_specifier_opt (parser,
17333 /*typename_keyword_p=*/false,
17334 /*check_dependency_p=*/true,
17335 /*type_p=*/false,
17336 /*is_declaration=*/false);
17337 tree name = cp_parser_identifier (parser);
17338 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17339 && parser->scope != error_mark_node)
17340 {
17341 tree tmpl = cp_parser_lookup_name (parser, name,
17342 none_type,
17343 /*is_template=*/false,
17344 /*is_namespace=*/false,
17345 /*check_dependency=*/true,
17346 /*ambiguous_decls=*/NULL,
17347 token->location);
17348 if (tmpl && tmpl != error_mark_node
17349 && (DECL_CLASS_TEMPLATE_P (tmpl)
17350 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17351 type = make_template_placeholder (tmpl);
17352 else
17353 {
17354 type = error_mark_node;
17355 if (!cp_parser_simulate_error (parser))
17356 cp_parser_name_lookup_error (parser, name, tmpl,
17357 NLE_TYPE, token->location);
17358 }
17359 }
17360 else
17361 type = error_mark_node;
17362
17363 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17364 && !cp_parser_parse_definitely (parser))
17365 type = NULL_TREE;
17366 }
17367 if (type && decl_specs)
17368 cp_parser_set_decl_spec_type (decl_specs, type,
17369 token,
17370 /*type_definition_p=*/false);
17371 }
17372
17373 /* If we didn't get a type-name, issue an error message. */
17374 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17375 {
17376 cp_parser_error (parser, "expected type-name");
17377 return error_mark_node;
17378 }
17379
17380 if (type && type != error_mark_node)
17381 {
17382 /* See if TYPE is an Objective-C type, and if so, parse and
17383 accept any protocol references following it. Do this before
17384 the cp_parser_check_for_invalid_template_id() call, because
17385 Objective-C types can be followed by '<...>' which would
17386 enclose protocol names rather than template arguments, and so
17387 everything is fine. */
17388 if (c_dialect_objc () && !parser->scope
17389 && (objc_is_id (type) || objc_is_class_name (type)))
17390 {
17391 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17392 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17393
17394 /* Clobber the "unqualified" type previously entered into
17395 DECL_SPECS with the new, improved protocol-qualified version. */
17396 if (decl_specs)
17397 decl_specs->type = qual_type;
17398
17399 return qual_type;
17400 }
17401
17402 /* There is no valid C++ program where a non-template type is
17403 followed by a "<". That usually indicates that the user
17404 thought that the type was a template. */
17405 cp_parser_check_for_invalid_template_id (parser, type,
17406 none_type,
17407 token->location);
17408 }
17409
17410 return type;
17411 }
17412
17413 /* Parse a type-name.
17414
17415 type-name:
17416 class-name
17417 enum-name
17418 typedef-name
17419 simple-template-id [in c++0x]
17420
17421 enum-name:
17422 identifier
17423
17424 typedef-name:
17425 identifier
17426
17427 Concepts:
17428
17429 type-name:
17430 concept-name
17431 partial-concept-id
17432
17433 concept-name:
17434 identifier
17435
17436 Returns a TYPE_DECL for the type. */
17437
17438 static tree
17439 cp_parser_type_name (cp_parser* parser)
17440 {
17441 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17442 }
17443
17444 /* See above. */
17445 static tree
17446 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17447 {
17448 tree type_decl;
17449
17450 /* We can't know yet whether it is a class-name or not. */
17451 cp_parser_parse_tentatively (parser);
17452 /* Try a class-name. */
17453 type_decl = cp_parser_class_name (parser,
17454 typename_keyword_p,
17455 /*template_keyword_p=*/false,
17456 none_type,
17457 /*check_dependency_p=*/true,
17458 /*class_head_p=*/false,
17459 /*is_declaration=*/false);
17460 /* If it's not a class-name, keep looking. */
17461 if (!cp_parser_parse_definitely (parser))
17462 {
17463 if (cxx_dialect < cxx11)
17464 /* It must be a typedef-name or an enum-name. */
17465 return cp_parser_nonclass_name (parser);
17466
17467 cp_parser_parse_tentatively (parser);
17468 /* It is either a simple-template-id representing an
17469 instantiation of an alias template... */
17470 type_decl = cp_parser_template_id (parser,
17471 /*template_keyword_p=*/false,
17472 /*check_dependency_p=*/true,
17473 none_type,
17474 /*is_declaration=*/false);
17475 /* Note that this must be an instantiation of an alias template
17476 because [temp.names]/6 says:
17477
17478 A template-id that names an alias template specialization
17479 is a type-name.
17480
17481 Whereas [temp.names]/7 says:
17482
17483 A simple-template-id that names a class template
17484 specialization is a class-name.
17485
17486 With concepts, this could also be a partial-concept-id that
17487 declares a non-type template parameter. */
17488 if (type_decl != NULL_TREE
17489 && TREE_CODE (type_decl) == TYPE_DECL
17490 && TYPE_DECL_ALIAS_P (type_decl))
17491 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17492 else if (is_constrained_parameter (type_decl))
17493 /* Don't do anything. */ ;
17494 else
17495 cp_parser_simulate_error (parser);
17496
17497 if (!cp_parser_parse_definitely (parser))
17498 /* ... Or a typedef-name or an enum-name. */
17499 return cp_parser_nonclass_name (parser);
17500 }
17501
17502 return type_decl;
17503 }
17504
17505 /* Check if DECL and ARGS can form a constrained-type-specifier.
17506 If ARGS is non-null, we try to form a concept check of the
17507 form DECL<?, ARGS> where ? is a wildcard that matches any
17508 kind of template argument. If ARGS is NULL, then we try to
17509 form a concept check of the form DECL<?>. */
17510
17511 static tree
17512 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17513 tree decl, tree args)
17514 {
17515 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17516
17517 /* If we a constrained-type-specifier cannot be deduced. */
17518 if (parser->prevent_constrained_type_specifiers)
17519 return NULL_TREE;
17520
17521 /* A constrained type specifier can only be found in an
17522 overload set or as a reference to a template declaration.
17523
17524 FIXME: This might be masking a bug. It's possible that
17525 that the deduction below is causing template specializations
17526 to be formed with the wildcard as an argument. */
17527 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17528 return NULL_TREE;
17529
17530 /* Try to build a call expression that evaluates the
17531 concept. This can fail if the overload set refers
17532 only to non-templates. */
17533 tree placeholder = build_nt (WILDCARD_DECL);
17534 tree check = build_concept_check (decl, placeholder, args);
17535 if (check == error_mark_node)
17536 return NULL_TREE;
17537
17538 /* Deduce the checked constraint and the prototype parameter.
17539
17540 FIXME: In certain cases, failure to deduce should be a
17541 diagnosable error. */
17542 tree conc;
17543 tree proto;
17544 if (!deduce_constrained_parameter (check, conc, proto))
17545 return NULL_TREE;
17546
17547 /* In template parameter scope, this results in a constrained
17548 parameter. Return a descriptor of that parm. */
17549 if (processing_template_parmlist)
17550 return build_constrained_parameter (conc, proto, args);
17551
17552 /* In a parameter-declaration-clause, constrained-type
17553 specifiers result in invented template parameters. */
17554 if (parser->auto_is_implicit_function_template_parm_p)
17555 {
17556 tree x = build_constrained_parameter (conc, proto, args);
17557 return synthesize_implicit_template_parm (parser, x);
17558 }
17559 else
17560 {
17561 /* Otherwise, we're in a context where the constrained
17562 type name is deduced and the constraint applies
17563 after deduction. */
17564 return make_constrained_auto (conc, args);
17565 }
17566
17567 return NULL_TREE;
17568 }
17569
17570 /* If DECL refers to a concept, return a TYPE_DECL representing
17571 the result of using the constrained type specifier in the
17572 current context. DECL refers to a concept if
17573
17574 - it is an overload set containing a function concept taking a single
17575 type argument, or
17576
17577 - it is a variable concept taking a single type argument. */
17578
17579 static tree
17580 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17581 {
17582 if (flag_concepts
17583 && (TREE_CODE (decl) == OVERLOAD
17584 || BASELINK_P (decl)
17585 || variable_concept_p (decl)))
17586 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17587 else
17588 return NULL_TREE;
17589 }
17590
17591 /* Check if DECL and ARGS form a partial-concept-id. If so,
17592 assign ID to the resulting constrained placeholder.
17593
17594 Returns true if the partial-concept-id designates a placeholder
17595 and false otherwise. Note that *id is set to NULL_TREE in
17596 this case. */
17597
17598 static tree
17599 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17600 {
17601 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17602 }
17603
17604 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17605 or a concept-name.
17606
17607 enum-name:
17608 identifier
17609
17610 typedef-name:
17611 identifier
17612
17613 concept-name:
17614 identifier
17615
17616 Returns a TYPE_DECL for the type. */
17617
17618 static tree
17619 cp_parser_nonclass_name (cp_parser* parser)
17620 {
17621 tree type_decl;
17622 tree identifier;
17623
17624 cp_token *token = cp_lexer_peek_token (parser->lexer);
17625 identifier = cp_parser_identifier (parser);
17626 if (identifier == error_mark_node)
17627 return error_mark_node;
17628
17629 /* Look up the type-name. */
17630 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17631
17632 type_decl = strip_using_decl (type_decl);
17633
17634 /* If we found an overload set, then it may refer to a concept-name. */
17635 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17636 type_decl = decl;
17637
17638 if (TREE_CODE (type_decl) != TYPE_DECL
17639 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17640 {
17641 /* See if this is an Objective-C type. */
17642 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17643 tree type = objc_get_protocol_qualified_type (identifier, protos);
17644 if (type)
17645 type_decl = TYPE_NAME (type);
17646 }
17647
17648 /* Issue an error if we did not find a type-name. */
17649 if (TREE_CODE (type_decl) != TYPE_DECL
17650 /* In Objective-C, we have the complication that class names are
17651 normally type names and start declarations (eg, the
17652 "NSObject" in "NSObject *object;"), but can be used in an
17653 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17654 is an expression. So, a classname followed by a dot is not a
17655 valid type-name. */
17656 || (objc_is_class_name (TREE_TYPE (type_decl))
17657 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17658 {
17659 if (!cp_parser_simulate_error (parser))
17660 cp_parser_name_lookup_error (parser, identifier, type_decl,
17661 NLE_TYPE, token->location);
17662 return error_mark_node;
17663 }
17664 /* Remember that the name was used in the definition of the
17665 current class so that we can check later to see if the
17666 meaning would have been different after the class was
17667 entirely defined. */
17668 else if (type_decl != error_mark_node
17669 && !parser->scope)
17670 maybe_note_name_used_in_class (identifier, type_decl);
17671
17672 return type_decl;
17673 }
17674
17675 /* Parse an elaborated-type-specifier. Note that the grammar given
17676 here incorporates the resolution to DR68.
17677
17678 elaborated-type-specifier:
17679 class-key :: [opt] nested-name-specifier [opt] identifier
17680 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17681 enum-key :: [opt] nested-name-specifier [opt] identifier
17682 typename :: [opt] nested-name-specifier identifier
17683 typename :: [opt] nested-name-specifier template [opt]
17684 template-id
17685
17686 GNU extension:
17687
17688 elaborated-type-specifier:
17689 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17690 class-key attributes :: [opt] nested-name-specifier [opt]
17691 template [opt] template-id
17692 enum attributes :: [opt] nested-name-specifier [opt] identifier
17693
17694 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17695 declared `friend'. If IS_DECLARATION is TRUE, then this
17696 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17697 something is being declared.
17698
17699 Returns the TYPE specified. */
17700
17701 static tree
17702 cp_parser_elaborated_type_specifier (cp_parser* parser,
17703 bool is_friend,
17704 bool is_declaration)
17705 {
17706 enum tag_types tag_type;
17707 tree identifier;
17708 tree type = NULL_TREE;
17709 tree attributes = NULL_TREE;
17710 tree globalscope;
17711 cp_token *token = NULL;
17712
17713 /* See if we're looking at the `enum' keyword. */
17714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17715 {
17716 /* Consume the `enum' token. */
17717 cp_lexer_consume_token (parser->lexer);
17718 /* Remember that it's an enumeration type. */
17719 tag_type = enum_type;
17720 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17721 enums) is used here. */
17722 cp_token *token = cp_lexer_peek_token (parser->lexer);
17723 if (cp_parser_is_keyword (token, RID_CLASS)
17724 || cp_parser_is_keyword (token, RID_STRUCT))
17725 {
17726 gcc_rich_location richloc (token->location);
17727 richloc.add_range (input_location, false);
17728 richloc.add_fixit_remove ();
17729 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17730 "a scoped enum must not use the %qD keyword",
17731 token->u.value);
17732 /* Consume the `struct' or `class' and parse it anyway. */
17733 cp_lexer_consume_token (parser->lexer);
17734 }
17735 /* Parse the attributes. */
17736 attributes = cp_parser_attributes_opt (parser);
17737 }
17738 /* Or, it might be `typename'. */
17739 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17740 RID_TYPENAME))
17741 {
17742 /* Consume the `typename' token. */
17743 cp_lexer_consume_token (parser->lexer);
17744 /* Remember that it's a `typename' type. */
17745 tag_type = typename_type;
17746 }
17747 /* Otherwise it must be a class-key. */
17748 else
17749 {
17750 tag_type = cp_parser_class_key (parser);
17751 if (tag_type == none_type)
17752 return error_mark_node;
17753 /* Parse the attributes. */
17754 attributes = cp_parser_attributes_opt (parser);
17755 }
17756
17757 /* Look for the `::' operator. */
17758 globalscope = cp_parser_global_scope_opt (parser,
17759 /*current_scope_valid_p=*/false);
17760 /* Look for the nested-name-specifier. */
17761 tree nested_name_specifier;
17762 if (tag_type == typename_type && !globalscope)
17763 {
17764 nested_name_specifier
17765 = cp_parser_nested_name_specifier (parser,
17766 /*typename_keyword_p=*/true,
17767 /*check_dependency_p=*/true,
17768 /*type_p=*/true,
17769 is_declaration);
17770 if (!nested_name_specifier)
17771 return error_mark_node;
17772 }
17773 else
17774 /* Even though `typename' is not present, the proposed resolution
17775 to Core Issue 180 says that in `class A<T>::B', `B' should be
17776 considered a type-name, even if `A<T>' is dependent. */
17777 nested_name_specifier
17778 = cp_parser_nested_name_specifier_opt (parser,
17779 /*typename_keyword_p=*/true,
17780 /*check_dependency_p=*/true,
17781 /*type_p=*/true,
17782 is_declaration);
17783 /* For everything but enumeration types, consider a template-id.
17784 For an enumeration type, consider only a plain identifier. */
17785 if (tag_type != enum_type)
17786 {
17787 bool template_p = false;
17788 tree decl;
17789
17790 /* Allow the `template' keyword. */
17791 template_p = cp_parser_optional_template_keyword (parser);
17792 /* If we didn't see `template', we don't know if there's a
17793 template-id or not. */
17794 if (!template_p)
17795 cp_parser_parse_tentatively (parser);
17796 /* Parse the template-id. */
17797 token = cp_lexer_peek_token (parser->lexer);
17798 decl = cp_parser_template_id (parser, template_p,
17799 /*check_dependency_p=*/true,
17800 tag_type,
17801 is_declaration);
17802 /* If we didn't find a template-id, look for an ordinary
17803 identifier. */
17804 if (!template_p && !cp_parser_parse_definitely (parser))
17805 ;
17806 /* We can get here when cp_parser_template_id, called by
17807 cp_parser_class_name with tag_type == none_type, succeeds
17808 and caches a BASELINK. Then, when called again here,
17809 instead of failing and returning an error_mark_node
17810 returns it (see template/typename17.C in C++11).
17811 ??? Could we diagnose this earlier? */
17812 else if (tag_type == typename_type && BASELINK_P (decl))
17813 {
17814 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17815 type = error_mark_node;
17816 }
17817 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17818 in effect, then we must assume that, upon instantiation, the
17819 template will correspond to a class. */
17820 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17821 && tag_type == typename_type)
17822 type = make_typename_type (parser->scope, decl,
17823 typename_type,
17824 /*complain=*/tf_error);
17825 /* If the `typename' keyword is in effect and DECL is not a type
17826 decl, then type is non existent. */
17827 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17828 ;
17829 else if (TREE_CODE (decl) == TYPE_DECL)
17830 {
17831 type = check_elaborated_type_specifier (tag_type, decl,
17832 /*allow_template_p=*/true);
17833
17834 /* If the next token is a semicolon, this must be a specialization,
17835 instantiation, or friend declaration. Check the scope while we
17836 still know whether or not we had a nested-name-specifier. */
17837 if (type != error_mark_node
17838 && !nested_name_specifier && !is_friend
17839 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17840 check_unqualified_spec_or_inst (type, token->location);
17841 }
17842 else if (decl == error_mark_node)
17843 type = error_mark_node;
17844 }
17845
17846 if (!type)
17847 {
17848 token = cp_lexer_peek_token (parser->lexer);
17849 identifier = cp_parser_identifier (parser);
17850
17851 if (identifier == error_mark_node)
17852 {
17853 parser->scope = NULL_TREE;
17854 return error_mark_node;
17855 }
17856
17857 /* For a `typename', we needn't call xref_tag. */
17858 if (tag_type == typename_type
17859 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17860 return cp_parser_make_typename_type (parser, identifier,
17861 token->location);
17862
17863 /* Template parameter lists apply only if we are not within a
17864 function parameter list. */
17865 bool template_parm_lists_apply
17866 = parser->num_template_parameter_lists;
17867 if (template_parm_lists_apply)
17868 for (cp_binding_level *s = current_binding_level;
17869 s && s->kind != sk_template_parms;
17870 s = s->level_chain)
17871 if (s->kind == sk_function_parms)
17872 template_parm_lists_apply = false;
17873
17874 /* Look up a qualified name in the usual way. */
17875 if (parser->scope)
17876 {
17877 tree decl;
17878 tree ambiguous_decls;
17879
17880 decl = cp_parser_lookup_name (parser, identifier,
17881 tag_type,
17882 /*is_template=*/false,
17883 /*is_namespace=*/false,
17884 /*check_dependency=*/true,
17885 &ambiguous_decls,
17886 token->location);
17887
17888 /* If the lookup was ambiguous, an error will already have been
17889 issued. */
17890 if (ambiguous_decls)
17891 return error_mark_node;
17892
17893 /* If we are parsing friend declaration, DECL may be a
17894 TEMPLATE_DECL tree node here. However, we need to check
17895 whether this TEMPLATE_DECL results in valid code. Consider
17896 the following example:
17897
17898 namespace N {
17899 template <class T> class C {};
17900 }
17901 class X {
17902 template <class T> friend class N::C; // #1, valid code
17903 };
17904 template <class T> class Y {
17905 friend class N::C; // #2, invalid code
17906 };
17907
17908 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17909 name lookup of `N::C'. We see that friend declaration must
17910 be template for the code to be valid. Note that
17911 processing_template_decl does not work here since it is
17912 always 1 for the above two cases. */
17913
17914 decl = (cp_parser_maybe_treat_template_as_class
17915 (decl, /*tag_name_p=*/is_friend
17916 && template_parm_lists_apply));
17917
17918 if (TREE_CODE (decl) != TYPE_DECL)
17919 {
17920 cp_parser_diagnose_invalid_type_name (parser,
17921 identifier,
17922 token->location);
17923 return error_mark_node;
17924 }
17925
17926 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17927 {
17928 bool allow_template = (template_parm_lists_apply
17929 || DECL_SELF_REFERENCE_P (decl));
17930 type = check_elaborated_type_specifier (tag_type, decl,
17931 allow_template);
17932
17933 if (type == error_mark_node)
17934 return error_mark_node;
17935 }
17936
17937 /* Forward declarations of nested types, such as
17938
17939 class C1::C2;
17940 class C1::C2::C3;
17941
17942 are invalid unless all components preceding the final '::'
17943 are complete. If all enclosing types are complete, these
17944 declarations become merely pointless.
17945
17946 Invalid forward declarations of nested types are errors
17947 caught elsewhere in parsing. Those that are pointless arrive
17948 here. */
17949
17950 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17951 && !is_friend && !processing_explicit_instantiation)
17952 warning (0, "declaration %qD does not declare anything", decl);
17953
17954 type = TREE_TYPE (decl);
17955 }
17956 else
17957 {
17958 /* An elaborated-type-specifier sometimes introduces a new type and
17959 sometimes names an existing type. Normally, the rule is that it
17960 introduces a new type only if there is not an existing type of
17961 the same name already in scope. For example, given:
17962
17963 struct S {};
17964 void f() { struct S s; }
17965
17966 the `struct S' in the body of `f' is the same `struct S' as in
17967 the global scope; the existing definition is used. However, if
17968 there were no global declaration, this would introduce a new
17969 local class named `S'.
17970
17971 An exception to this rule applies to the following code:
17972
17973 namespace N { struct S; }
17974
17975 Here, the elaborated-type-specifier names a new type
17976 unconditionally; even if there is already an `S' in the
17977 containing scope this declaration names a new type.
17978 This exception only applies if the elaborated-type-specifier
17979 forms the complete declaration:
17980
17981 [class.name]
17982
17983 A declaration consisting solely of `class-key identifier ;' is
17984 either a redeclaration of the name in the current scope or a
17985 forward declaration of the identifier as a class name. It
17986 introduces the name into the current scope.
17987
17988 We are in this situation precisely when the next token is a `;'.
17989
17990 An exception to the exception is that a `friend' declaration does
17991 *not* name a new type; i.e., given:
17992
17993 struct S { friend struct T; };
17994
17995 `T' is not a new type in the scope of `S'.
17996
17997 Also, `new struct S' or `sizeof (struct S)' never results in the
17998 definition of a new type; a new type can only be declared in a
17999 declaration context. */
18000
18001 tag_scope ts;
18002 bool template_p;
18003
18004 if (is_friend)
18005 /* Friends have special name lookup rules. */
18006 ts = ts_within_enclosing_non_class;
18007 else if (is_declaration
18008 && cp_lexer_next_token_is (parser->lexer,
18009 CPP_SEMICOLON))
18010 /* This is a `class-key identifier ;' */
18011 ts = ts_current;
18012 else
18013 ts = ts_global;
18014
18015 template_p =
18016 (template_parm_lists_apply
18017 && (cp_parser_next_token_starts_class_definition_p (parser)
18018 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18019 /* An unqualified name was used to reference this type, so
18020 there were no qualifying templates. */
18021 if (template_parm_lists_apply
18022 && !cp_parser_check_template_parameters (parser,
18023 /*num_templates=*/0,
18024 /*template_id*/false,
18025 token->location,
18026 /*declarator=*/NULL))
18027 return error_mark_node;
18028 type = xref_tag (tag_type, identifier, ts, template_p);
18029 }
18030 }
18031
18032 if (type == error_mark_node)
18033 return error_mark_node;
18034
18035 /* Allow attributes on forward declarations of classes. */
18036 if (attributes)
18037 {
18038 if (TREE_CODE (type) == TYPENAME_TYPE)
18039 warning (OPT_Wattributes,
18040 "attributes ignored on uninstantiated type");
18041 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18042 && ! processing_explicit_instantiation)
18043 warning (OPT_Wattributes,
18044 "attributes ignored on template instantiation");
18045 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18046 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18047 else
18048 warning (OPT_Wattributes,
18049 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18050 }
18051
18052 if (tag_type != enum_type)
18053 {
18054 /* Indicate whether this class was declared as a `class' or as a
18055 `struct'. */
18056 if (CLASS_TYPE_P (type))
18057 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18058 cp_parser_check_class_key (tag_type, type);
18059 }
18060
18061 /* A "<" cannot follow an elaborated type specifier. If that
18062 happens, the user was probably trying to form a template-id. */
18063 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18064 token->location);
18065
18066 return type;
18067 }
18068
18069 /* Parse an enum-specifier.
18070
18071 enum-specifier:
18072 enum-head { enumerator-list [opt] }
18073 enum-head { enumerator-list , } [C++0x]
18074
18075 enum-head:
18076 enum-key identifier [opt] enum-base [opt]
18077 enum-key nested-name-specifier identifier enum-base [opt]
18078
18079 enum-key:
18080 enum
18081 enum class [C++0x]
18082 enum struct [C++0x]
18083
18084 enum-base: [C++0x]
18085 : type-specifier-seq
18086
18087 opaque-enum-specifier:
18088 enum-key identifier enum-base [opt] ;
18089
18090 GNU Extensions:
18091 enum-key attributes[opt] identifier [opt] enum-base [opt]
18092 { enumerator-list [opt] }attributes[opt]
18093 enum-key attributes[opt] identifier [opt] enum-base [opt]
18094 { enumerator-list, }attributes[opt] [C++0x]
18095
18096 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18097 if the token stream isn't an enum-specifier after all. */
18098
18099 static tree
18100 cp_parser_enum_specifier (cp_parser* parser)
18101 {
18102 tree identifier;
18103 tree type = NULL_TREE;
18104 tree prev_scope;
18105 tree nested_name_specifier = NULL_TREE;
18106 tree attributes;
18107 bool scoped_enum_p = false;
18108 bool has_underlying_type = false;
18109 bool nested_being_defined = false;
18110 bool new_value_list = false;
18111 bool is_new_type = false;
18112 bool is_unnamed = false;
18113 tree underlying_type = NULL_TREE;
18114 cp_token *type_start_token = NULL;
18115 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18116
18117 parser->colon_corrects_to_scope_p = false;
18118
18119 /* Parse tentatively so that we can back up if we don't find a
18120 enum-specifier. */
18121 cp_parser_parse_tentatively (parser);
18122
18123 /* Caller guarantees that the current token is 'enum', an identifier
18124 possibly follows, and the token after that is an opening brace.
18125 If we don't have an identifier, fabricate an anonymous name for
18126 the enumeration being defined. */
18127 cp_lexer_consume_token (parser->lexer);
18128
18129 /* Parse the "class" or "struct", which indicates a scoped
18130 enumeration type in C++0x. */
18131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18132 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18133 {
18134 if (cxx_dialect < cxx11)
18135 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18136
18137 /* Consume the `struct' or `class' token. */
18138 cp_lexer_consume_token (parser->lexer);
18139
18140 scoped_enum_p = true;
18141 }
18142
18143 attributes = cp_parser_attributes_opt (parser);
18144
18145 /* Clear the qualification. */
18146 parser->scope = NULL_TREE;
18147 parser->qualifying_scope = NULL_TREE;
18148 parser->object_scope = NULL_TREE;
18149
18150 /* Figure out in what scope the declaration is being placed. */
18151 prev_scope = current_scope ();
18152
18153 type_start_token = cp_lexer_peek_token (parser->lexer);
18154
18155 push_deferring_access_checks (dk_no_check);
18156 nested_name_specifier
18157 = cp_parser_nested_name_specifier_opt (parser,
18158 /*typename_keyword_p=*/true,
18159 /*check_dependency_p=*/false,
18160 /*type_p=*/false,
18161 /*is_declaration=*/false);
18162
18163 if (nested_name_specifier)
18164 {
18165 tree name;
18166
18167 identifier = cp_parser_identifier (parser);
18168 name = cp_parser_lookup_name (parser, identifier,
18169 enum_type,
18170 /*is_template=*/false,
18171 /*is_namespace=*/false,
18172 /*check_dependency=*/true,
18173 /*ambiguous_decls=*/NULL,
18174 input_location);
18175 if (name && name != error_mark_node)
18176 {
18177 type = TREE_TYPE (name);
18178 if (TREE_CODE (type) == TYPENAME_TYPE)
18179 {
18180 /* Are template enums allowed in ISO? */
18181 if (template_parm_scope_p ())
18182 pedwarn (type_start_token->location, OPT_Wpedantic,
18183 "%qD is an enumeration template", name);
18184 /* ignore a typename reference, for it will be solved by name
18185 in start_enum. */
18186 type = NULL_TREE;
18187 }
18188 }
18189 else if (nested_name_specifier == error_mark_node)
18190 /* We already issued an error. */;
18191 else
18192 {
18193 error_at (type_start_token->location,
18194 "%qD does not name an enumeration in %qT",
18195 identifier, nested_name_specifier);
18196 nested_name_specifier = error_mark_node;
18197 }
18198 }
18199 else
18200 {
18201 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18202 identifier = cp_parser_identifier (parser);
18203 else
18204 {
18205 identifier = make_anon_name ();
18206 is_unnamed = true;
18207 if (scoped_enum_p)
18208 error_at (type_start_token->location,
18209 "unnamed scoped enum is not allowed");
18210 }
18211 }
18212 pop_deferring_access_checks ();
18213
18214 /* Check for the `:' that denotes a specified underlying type in C++0x.
18215 Note that a ':' could also indicate a bitfield width, however. */
18216 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18217 {
18218 cp_decl_specifier_seq type_specifiers;
18219
18220 /* Consume the `:'. */
18221 cp_lexer_consume_token (parser->lexer);
18222
18223 /* Parse the type-specifier-seq. */
18224 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18225 /*is_trailing_return=*/false,
18226 &type_specifiers);
18227
18228 /* At this point this is surely not elaborated type specifier. */
18229 if (!cp_parser_parse_definitely (parser))
18230 return NULL_TREE;
18231
18232 if (cxx_dialect < cxx11)
18233 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18234
18235 has_underlying_type = true;
18236
18237 /* If that didn't work, stop. */
18238 if (type_specifiers.type != error_mark_node)
18239 {
18240 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18241 /*initialized=*/0, NULL);
18242 if (underlying_type == error_mark_node
18243 || check_for_bare_parameter_packs (underlying_type))
18244 underlying_type = NULL_TREE;
18245 }
18246 }
18247
18248 /* Look for the `{' but don't consume it yet. */
18249 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18250 {
18251 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18252 {
18253 cp_parser_error (parser, "expected %<{%>");
18254 if (has_underlying_type)
18255 {
18256 type = NULL_TREE;
18257 goto out;
18258 }
18259 }
18260 /* An opaque-enum-specifier must have a ';' here. */
18261 if ((scoped_enum_p || underlying_type)
18262 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18263 {
18264 cp_parser_error (parser, "expected %<;%> or %<{%>");
18265 if (has_underlying_type)
18266 {
18267 type = NULL_TREE;
18268 goto out;
18269 }
18270 }
18271 }
18272
18273 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18274 return NULL_TREE;
18275
18276 if (nested_name_specifier)
18277 {
18278 if (CLASS_TYPE_P (nested_name_specifier))
18279 {
18280 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18281 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18282 push_scope (nested_name_specifier);
18283 }
18284 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18285 {
18286 push_nested_namespace (nested_name_specifier);
18287 }
18288 }
18289
18290 /* Issue an error message if type-definitions are forbidden here. */
18291 if (!cp_parser_check_type_definition (parser))
18292 type = error_mark_node;
18293 else
18294 /* Create the new type. We do this before consuming the opening
18295 brace so the enum will be recorded as being on the line of its
18296 tag (or the 'enum' keyword, if there is no tag). */
18297 type = start_enum (identifier, type, underlying_type,
18298 attributes, scoped_enum_p, &is_new_type);
18299
18300 /* If the next token is not '{' it is an opaque-enum-specifier or an
18301 elaborated-type-specifier. */
18302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18303 {
18304 timevar_push (TV_PARSE_ENUM);
18305 if (nested_name_specifier
18306 && nested_name_specifier != error_mark_node)
18307 {
18308 /* The following catches invalid code such as:
18309 enum class S<int>::E { A, B, C }; */
18310 if (!processing_specialization
18311 && CLASS_TYPE_P (nested_name_specifier)
18312 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18313 error_at (type_start_token->location, "cannot add an enumerator "
18314 "list to a template instantiation");
18315
18316 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18317 {
18318 error_at (type_start_token->location,
18319 "%<%T::%E%> has not been declared",
18320 TYPE_CONTEXT (nested_name_specifier),
18321 nested_name_specifier);
18322 type = error_mark_node;
18323 }
18324 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18325 && !CLASS_TYPE_P (nested_name_specifier))
18326 {
18327 error_at (type_start_token->location, "nested name specifier "
18328 "%qT for enum declaration does not name a class "
18329 "or namespace", nested_name_specifier);
18330 type = error_mark_node;
18331 }
18332 /* If that scope does not contain the scope in which the
18333 class was originally declared, the program is invalid. */
18334 else if (prev_scope && !is_ancestor (prev_scope,
18335 nested_name_specifier))
18336 {
18337 if (at_namespace_scope_p ())
18338 error_at (type_start_token->location,
18339 "declaration of %qD in namespace %qD which does not "
18340 "enclose %qD",
18341 type, prev_scope, nested_name_specifier);
18342 else
18343 error_at (type_start_token->location,
18344 "declaration of %qD in %qD which does not "
18345 "enclose %qD",
18346 type, prev_scope, nested_name_specifier);
18347 type = error_mark_node;
18348 }
18349 /* If that scope is the scope where the declaration is being placed
18350 the program is invalid. */
18351 else if (CLASS_TYPE_P (nested_name_specifier)
18352 && CLASS_TYPE_P (prev_scope)
18353 && same_type_p (nested_name_specifier, prev_scope))
18354 {
18355 permerror (type_start_token->location,
18356 "extra qualification not allowed");
18357 nested_name_specifier = NULL_TREE;
18358 }
18359 }
18360
18361 if (scoped_enum_p)
18362 begin_scope (sk_scoped_enum, type);
18363
18364 /* Consume the opening brace. */
18365 matching_braces braces;
18366 braces.consume_open (parser);
18367
18368 if (type == error_mark_node)
18369 ; /* Nothing to add */
18370 else if (OPAQUE_ENUM_P (type)
18371 || (cxx_dialect > cxx98 && processing_specialization))
18372 {
18373 new_value_list = true;
18374 SET_OPAQUE_ENUM_P (type, false);
18375 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18376 }
18377 else
18378 {
18379 error_at (type_start_token->location,
18380 "multiple definition of %q#T", type);
18381 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18382 "previous definition here");
18383 type = error_mark_node;
18384 }
18385
18386 if (type == error_mark_node)
18387 cp_parser_skip_to_end_of_block_or_statement (parser);
18388 /* If the next token is not '}', then there are some enumerators. */
18389 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18390 {
18391 if (is_unnamed && !scoped_enum_p)
18392 pedwarn (type_start_token->location, OPT_Wpedantic,
18393 "ISO C++ forbids empty unnamed enum");
18394 }
18395 else
18396 cp_parser_enumerator_list (parser, type);
18397
18398 /* Consume the final '}'. */
18399 braces.require_close (parser);
18400
18401 if (scoped_enum_p)
18402 finish_scope ();
18403 timevar_pop (TV_PARSE_ENUM);
18404 }
18405 else
18406 {
18407 /* If a ';' follows, then it is an opaque-enum-specifier
18408 and additional restrictions apply. */
18409 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18410 {
18411 if (is_unnamed)
18412 error_at (type_start_token->location,
18413 "opaque-enum-specifier without name");
18414 else if (nested_name_specifier)
18415 error_at (type_start_token->location,
18416 "opaque-enum-specifier must use a simple identifier");
18417 }
18418 }
18419
18420 /* Look for trailing attributes to apply to this enumeration, and
18421 apply them if appropriate. */
18422 if (cp_parser_allow_gnu_extensions_p (parser))
18423 {
18424 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18425 cplus_decl_attributes (&type,
18426 trailing_attr,
18427 (int) ATTR_FLAG_TYPE_IN_PLACE);
18428 }
18429
18430 /* Finish up the enumeration. */
18431 if (type != error_mark_node)
18432 {
18433 if (new_value_list)
18434 finish_enum_value_list (type);
18435 if (is_new_type)
18436 finish_enum (type);
18437 }
18438
18439 if (nested_name_specifier)
18440 {
18441 if (CLASS_TYPE_P (nested_name_specifier))
18442 {
18443 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18444 pop_scope (nested_name_specifier);
18445 }
18446 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18447 {
18448 pop_nested_namespace (nested_name_specifier);
18449 }
18450 }
18451 out:
18452 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18453 return type;
18454 }
18455
18456 /* Parse an enumerator-list. The enumerators all have the indicated
18457 TYPE.
18458
18459 enumerator-list:
18460 enumerator-definition
18461 enumerator-list , enumerator-definition */
18462
18463 static void
18464 cp_parser_enumerator_list (cp_parser* parser, tree type)
18465 {
18466 while (true)
18467 {
18468 /* Parse an enumerator-definition. */
18469 cp_parser_enumerator_definition (parser, type);
18470
18471 /* If the next token is not a ',', we've reached the end of
18472 the list. */
18473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18474 break;
18475 /* Otherwise, consume the `,' and keep going. */
18476 cp_lexer_consume_token (parser->lexer);
18477 /* If the next token is a `}', there is a trailing comma. */
18478 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18479 {
18480 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18481 pedwarn (input_location, OPT_Wpedantic,
18482 "comma at end of enumerator list");
18483 break;
18484 }
18485 }
18486 }
18487
18488 /* Parse an enumerator-definition. The enumerator has the indicated
18489 TYPE.
18490
18491 enumerator-definition:
18492 enumerator
18493 enumerator = constant-expression
18494
18495 enumerator:
18496 identifier
18497
18498 GNU Extensions:
18499
18500 enumerator-definition:
18501 enumerator attributes [opt]
18502 enumerator attributes [opt] = constant-expression */
18503
18504 static void
18505 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18506 {
18507 tree identifier;
18508 tree value;
18509 location_t loc;
18510
18511 /* Save the input location because we are interested in the location
18512 of the identifier and not the location of the explicit value. */
18513 loc = cp_lexer_peek_token (parser->lexer)->location;
18514
18515 /* Look for the identifier. */
18516 identifier = cp_parser_identifier (parser);
18517 if (identifier == error_mark_node)
18518 return;
18519
18520 /* Parse any specified attributes. */
18521 tree attrs = cp_parser_attributes_opt (parser);
18522
18523 /* If the next token is an '=', then there is an explicit value. */
18524 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18525 {
18526 /* Consume the `=' token. */
18527 cp_lexer_consume_token (parser->lexer);
18528 /* Parse the value. */
18529 value = cp_parser_constant_expression (parser);
18530 }
18531 else
18532 value = NULL_TREE;
18533
18534 /* If we are processing a template, make sure the initializer of the
18535 enumerator doesn't contain any bare template parameter pack. */
18536 if (check_for_bare_parameter_packs (value))
18537 value = error_mark_node;
18538
18539 /* Create the enumerator. */
18540 build_enumerator (identifier, value, type, attrs, loc);
18541 }
18542
18543 /* Parse a namespace-name.
18544
18545 namespace-name:
18546 original-namespace-name
18547 namespace-alias
18548
18549 Returns the NAMESPACE_DECL for the namespace. */
18550
18551 static tree
18552 cp_parser_namespace_name (cp_parser* parser)
18553 {
18554 tree identifier;
18555 tree namespace_decl;
18556
18557 cp_token *token = cp_lexer_peek_token (parser->lexer);
18558
18559 /* Get the name of the namespace. */
18560 identifier = cp_parser_identifier (parser);
18561 if (identifier == error_mark_node)
18562 return error_mark_node;
18563
18564 /* Look up the identifier in the currently active scope. Look only
18565 for namespaces, due to:
18566
18567 [basic.lookup.udir]
18568
18569 When looking up a namespace-name in a using-directive or alias
18570 definition, only namespace names are considered.
18571
18572 And:
18573
18574 [basic.lookup.qual]
18575
18576 During the lookup of a name preceding the :: scope resolution
18577 operator, object, function, and enumerator names are ignored.
18578
18579 (Note that cp_parser_qualifying_entity only calls this
18580 function if the token after the name is the scope resolution
18581 operator.) */
18582 namespace_decl = cp_parser_lookup_name (parser, identifier,
18583 none_type,
18584 /*is_template=*/false,
18585 /*is_namespace=*/true,
18586 /*check_dependency=*/true,
18587 /*ambiguous_decls=*/NULL,
18588 token->location);
18589 /* If it's not a namespace, issue an error. */
18590 if (namespace_decl == error_mark_node
18591 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18592 {
18593 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18594 {
18595 error_at (token->location, "%qD is not a namespace-name", identifier);
18596 if (namespace_decl == error_mark_node
18597 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18598 suggest_alternative_in_explicit_scope (token->location, identifier,
18599 parser->scope);
18600 }
18601 cp_parser_error (parser, "expected namespace-name");
18602 namespace_decl = error_mark_node;
18603 }
18604
18605 return namespace_decl;
18606 }
18607
18608 /* Parse a namespace-definition.
18609
18610 namespace-definition:
18611 named-namespace-definition
18612 unnamed-namespace-definition
18613
18614 named-namespace-definition:
18615 original-namespace-definition
18616 extension-namespace-definition
18617
18618 original-namespace-definition:
18619 namespace identifier { namespace-body }
18620
18621 extension-namespace-definition:
18622 namespace original-namespace-name { namespace-body }
18623
18624 unnamed-namespace-definition:
18625 namespace { namespace-body } */
18626
18627 static void
18628 cp_parser_namespace_definition (cp_parser* parser)
18629 {
18630 tree identifier;
18631 int nested_definition_count = 0;
18632
18633 cp_ensure_no_omp_declare_simd (parser);
18634 cp_ensure_no_oacc_routine (parser);
18635
18636 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18637
18638 if (is_inline)
18639 {
18640 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18641 cp_lexer_consume_token (parser->lexer);
18642 }
18643
18644 /* Look for the `namespace' keyword. */
18645 cp_token* token
18646 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18647
18648 /* Parse any specified attributes before the identifier. */
18649 tree attribs = cp_parser_attributes_opt (parser);
18650
18651 for (;;)
18652 {
18653 identifier = NULL_TREE;
18654
18655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18656 {
18657 identifier = cp_parser_identifier (parser);
18658
18659 /* Parse any attributes specified after the identifier. */
18660 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18661 }
18662
18663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18664 break;
18665
18666 if (!nested_definition_count && cxx_dialect < cxx17)
18667 pedwarn (input_location, OPT_Wpedantic,
18668 "nested namespace definitions only available with "
18669 "-std=c++17 or -std=gnu++17");
18670
18671 /* Nested namespace names can create new namespaces (unlike
18672 other qualified-ids). */
18673 if (int count = identifier ? push_namespace (identifier) : 0)
18674 nested_definition_count += count;
18675 else
18676 cp_parser_error (parser, "nested namespace name required");
18677 cp_lexer_consume_token (parser->lexer);
18678 }
18679
18680 if (nested_definition_count && !identifier)
18681 cp_parser_error (parser, "namespace name required");
18682
18683 if (nested_definition_count && attribs)
18684 error_at (token->location,
18685 "a nested namespace definition cannot have attributes");
18686 if (nested_definition_count && is_inline)
18687 error_at (token->location,
18688 "a nested namespace definition cannot be inline");
18689
18690 /* Start the namespace. */
18691 nested_definition_count += push_namespace (identifier, is_inline);
18692
18693 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18694
18695 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18696
18697 /* Look for the `{' to validate starting the namespace. */
18698 matching_braces braces;
18699 if (braces.require_open (parser))
18700 {
18701 /* Parse the body of the namespace. */
18702 cp_parser_namespace_body (parser);
18703
18704 /* Look for the final `}'. */
18705 braces.require_close (parser);
18706 }
18707
18708 if (has_visibility)
18709 pop_visibility (1);
18710
18711 /* Pop the nested namespace definitions. */
18712 while (nested_definition_count--)
18713 pop_namespace ();
18714 }
18715
18716 /* Parse a namespace-body.
18717
18718 namespace-body:
18719 declaration-seq [opt] */
18720
18721 static void
18722 cp_parser_namespace_body (cp_parser* parser)
18723 {
18724 cp_parser_declaration_seq_opt (parser);
18725 }
18726
18727 /* Parse a namespace-alias-definition.
18728
18729 namespace-alias-definition:
18730 namespace identifier = qualified-namespace-specifier ; */
18731
18732 static void
18733 cp_parser_namespace_alias_definition (cp_parser* parser)
18734 {
18735 tree identifier;
18736 tree namespace_specifier;
18737
18738 cp_token *token = cp_lexer_peek_token (parser->lexer);
18739
18740 /* Look for the `namespace' keyword. */
18741 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18742 /* Look for the identifier. */
18743 identifier = cp_parser_identifier (parser);
18744 if (identifier == error_mark_node)
18745 return;
18746 /* Look for the `=' token. */
18747 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18748 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18749 {
18750 error_at (token->location, "%<namespace%> definition is not allowed here");
18751 /* Skip the definition. */
18752 cp_lexer_consume_token (parser->lexer);
18753 if (cp_parser_skip_to_closing_brace (parser))
18754 cp_lexer_consume_token (parser->lexer);
18755 return;
18756 }
18757 cp_parser_require (parser, CPP_EQ, RT_EQ);
18758 /* Look for the qualified-namespace-specifier. */
18759 namespace_specifier
18760 = cp_parser_qualified_namespace_specifier (parser);
18761 /* Look for the `;' token. */
18762 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18763
18764 /* Register the alias in the symbol table. */
18765 do_namespace_alias (identifier, namespace_specifier);
18766 }
18767
18768 /* Parse a qualified-namespace-specifier.
18769
18770 qualified-namespace-specifier:
18771 :: [opt] nested-name-specifier [opt] namespace-name
18772
18773 Returns a NAMESPACE_DECL corresponding to the specified
18774 namespace. */
18775
18776 static tree
18777 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18778 {
18779 /* Look for the optional `::'. */
18780 cp_parser_global_scope_opt (parser,
18781 /*current_scope_valid_p=*/false);
18782
18783 /* Look for the optional nested-name-specifier. */
18784 cp_parser_nested_name_specifier_opt (parser,
18785 /*typename_keyword_p=*/false,
18786 /*check_dependency_p=*/true,
18787 /*type_p=*/false,
18788 /*is_declaration=*/true);
18789
18790 return cp_parser_namespace_name (parser);
18791 }
18792
18793 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18794 access declaration.
18795
18796 using-declaration:
18797 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18798 using :: unqualified-id ;
18799
18800 access-declaration:
18801 qualified-id ;
18802
18803 */
18804
18805 static bool
18806 cp_parser_using_declaration (cp_parser* parser,
18807 bool access_declaration_p)
18808 {
18809 cp_token *token;
18810 bool typename_p = false;
18811 bool global_scope_p;
18812 tree decl;
18813 tree identifier;
18814 tree qscope;
18815 int oldcount = errorcount;
18816 cp_token *diag_token = NULL;
18817
18818 if (access_declaration_p)
18819 {
18820 diag_token = cp_lexer_peek_token (parser->lexer);
18821 cp_parser_parse_tentatively (parser);
18822 }
18823 else
18824 {
18825 /* Look for the `using' keyword. */
18826 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18827
18828 again:
18829 /* Peek at the next token. */
18830 token = cp_lexer_peek_token (parser->lexer);
18831 /* See if it's `typename'. */
18832 if (token->keyword == RID_TYPENAME)
18833 {
18834 /* Remember that we've seen it. */
18835 typename_p = true;
18836 /* Consume the `typename' token. */
18837 cp_lexer_consume_token (parser->lexer);
18838 }
18839 }
18840
18841 /* Look for the optional global scope qualification. */
18842 global_scope_p
18843 = (cp_parser_global_scope_opt (parser,
18844 /*current_scope_valid_p=*/false)
18845 != NULL_TREE);
18846
18847 /* If we saw `typename', or didn't see `::', then there must be a
18848 nested-name-specifier present. */
18849 if (typename_p || !global_scope_p)
18850 {
18851 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18852 /*check_dependency_p=*/true,
18853 /*type_p=*/false,
18854 /*is_declaration=*/true);
18855 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18856 {
18857 cp_parser_skip_to_end_of_block_or_statement (parser);
18858 return false;
18859 }
18860 }
18861 /* Otherwise, we could be in either of the two productions. In that
18862 case, treat the nested-name-specifier as optional. */
18863 else
18864 qscope = cp_parser_nested_name_specifier_opt (parser,
18865 /*typename_keyword_p=*/false,
18866 /*check_dependency_p=*/true,
18867 /*type_p=*/false,
18868 /*is_declaration=*/true);
18869 if (!qscope)
18870 qscope = global_namespace;
18871 else if (UNSCOPED_ENUM_P (qscope))
18872 qscope = CP_TYPE_CONTEXT (qscope);
18873
18874 if (access_declaration_p && cp_parser_error_occurred (parser))
18875 /* Something has already gone wrong; there's no need to parse
18876 further. Since an error has occurred, the return value of
18877 cp_parser_parse_definitely will be false, as required. */
18878 return cp_parser_parse_definitely (parser);
18879
18880 token = cp_lexer_peek_token (parser->lexer);
18881 /* Parse the unqualified-id. */
18882 identifier = cp_parser_unqualified_id (parser,
18883 /*template_keyword_p=*/false,
18884 /*check_dependency_p=*/true,
18885 /*declarator_p=*/true,
18886 /*optional_p=*/false);
18887
18888 if (access_declaration_p)
18889 {
18890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18891 cp_parser_simulate_error (parser);
18892 if (!cp_parser_parse_definitely (parser))
18893 return false;
18894 }
18895 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18896 {
18897 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18898 if (cxx_dialect < cxx17
18899 && !in_system_header_at (ell->location))
18900 pedwarn (ell->location, 0,
18901 "pack expansion in using-declaration only available "
18902 "with -std=c++17 or -std=gnu++17");
18903 qscope = make_pack_expansion (qscope);
18904 }
18905
18906 /* The function we call to handle a using-declaration is different
18907 depending on what scope we are in. */
18908 if (qscope == error_mark_node || identifier == error_mark_node)
18909 ;
18910 else if (!identifier_p (identifier)
18911 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18912 /* [namespace.udecl]
18913
18914 A using declaration shall not name a template-id. */
18915 error_at (token->location,
18916 "a template-id may not appear in a using-declaration");
18917 else
18918 {
18919 if (at_class_scope_p ())
18920 {
18921 /* Create the USING_DECL. */
18922 decl = do_class_using_decl (qscope, identifier);
18923
18924 if (decl && typename_p)
18925 USING_DECL_TYPENAME_P (decl) = 1;
18926
18927 if (check_for_bare_parameter_packs (decl))
18928 {
18929 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18930 return false;
18931 }
18932 else
18933 /* Add it to the list of members in this class. */
18934 finish_member_declaration (decl);
18935 }
18936 else
18937 {
18938 decl = cp_parser_lookup_name_simple (parser,
18939 identifier,
18940 token->location);
18941 if (decl == error_mark_node)
18942 cp_parser_name_lookup_error (parser, identifier,
18943 decl, NLE_NULL,
18944 token->location);
18945 else if (check_for_bare_parameter_packs (decl))
18946 {
18947 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18948 return false;
18949 }
18950 else if (!at_namespace_scope_p ())
18951 finish_local_using_decl (decl, qscope, identifier);
18952 else
18953 finish_namespace_using_decl (decl, qscope, identifier);
18954 }
18955 }
18956
18957 if (!access_declaration_p
18958 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18959 {
18960 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18961 if (cxx_dialect < cxx17)
18962 pedwarn (comma->location, 0,
18963 "comma-separated list in using-declaration only available "
18964 "with -std=c++17 or -std=gnu++17");
18965 goto again;
18966 }
18967
18968 /* Look for the final `;'. */
18969 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18970
18971 if (access_declaration_p && errorcount == oldcount)
18972 warning_at (diag_token->location, OPT_Wdeprecated,
18973 "access declarations are deprecated "
18974 "in favour of using-declarations; "
18975 "suggestion: add the %<using%> keyword");
18976
18977 return true;
18978 }
18979
18980 /* Parse an alias-declaration.
18981
18982 alias-declaration:
18983 using identifier attribute-specifier-seq [opt] = type-id */
18984
18985 static tree
18986 cp_parser_alias_declaration (cp_parser* parser)
18987 {
18988 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18989 location_t id_location;
18990 cp_declarator *declarator;
18991 cp_decl_specifier_seq decl_specs;
18992 bool member_p;
18993 const char *saved_message = NULL;
18994
18995 /* Look for the `using' keyword. */
18996 cp_token *using_token
18997 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18998 if (using_token == NULL)
18999 return error_mark_node;
19000
19001 id_location = cp_lexer_peek_token (parser->lexer)->location;
19002 id = cp_parser_identifier (parser);
19003 if (id == error_mark_node)
19004 return error_mark_node;
19005
19006 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19007 attributes = cp_parser_attributes_opt (parser);
19008 if (attributes == error_mark_node)
19009 return error_mark_node;
19010
19011 cp_parser_require (parser, CPP_EQ, RT_EQ);
19012
19013 if (cp_parser_error_occurred (parser))
19014 return error_mark_node;
19015
19016 cp_parser_commit_to_tentative_parse (parser);
19017
19018 /* Now we are going to parse the type-id of the declaration. */
19019
19020 /*
19021 [dcl.type]/3 says:
19022
19023 "A type-specifier-seq shall not define a class or enumeration
19024 unless it appears in the type-id of an alias-declaration (7.1.3) that
19025 is not the declaration of a template-declaration."
19026
19027 In other words, if we currently are in an alias template, the
19028 type-id should not define a type.
19029
19030 So let's set parser->type_definition_forbidden_message in that
19031 case; cp_parser_check_type_definition (called by
19032 cp_parser_class_specifier) will then emit an error if a type is
19033 defined in the type-id. */
19034 if (parser->num_template_parameter_lists)
19035 {
19036 saved_message = parser->type_definition_forbidden_message;
19037 parser->type_definition_forbidden_message =
19038 G_("types may not be defined in alias template declarations");
19039 }
19040
19041 type = cp_parser_type_id (parser);
19042
19043 /* Restore the error message if need be. */
19044 if (parser->num_template_parameter_lists)
19045 parser->type_definition_forbidden_message = saved_message;
19046
19047 if (type == error_mark_node
19048 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19049 {
19050 cp_parser_skip_to_end_of_block_or_statement (parser);
19051 return error_mark_node;
19052 }
19053
19054 /* A typedef-name can also be introduced by an alias-declaration. The
19055 identifier following the using keyword becomes a typedef-name. It has
19056 the same semantics as if it were introduced by the typedef
19057 specifier. In particular, it does not define a new type and it shall
19058 not appear in the type-id. */
19059
19060 clear_decl_specs (&decl_specs);
19061 decl_specs.type = type;
19062 if (attributes != NULL_TREE)
19063 {
19064 decl_specs.attributes = attributes;
19065 set_and_check_decl_spec_loc (&decl_specs,
19066 ds_attribute,
19067 attrs_token);
19068 }
19069 set_and_check_decl_spec_loc (&decl_specs,
19070 ds_typedef,
19071 using_token);
19072 set_and_check_decl_spec_loc (&decl_specs,
19073 ds_alias,
19074 using_token);
19075
19076 if (parser->num_template_parameter_lists
19077 && !cp_parser_check_template_parameters (parser,
19078 /*num_templates=*/0,
19079 /*template_id*/false,
19080 id_location,
19081 /*declarator=*/NULL))
19082 return error_mark_node;
19083
19084 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19085 declarator->id_loc = id_location;
19086
19087 member_p = at_class_scope_p ();
19088 if (member_p)
19089 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19090 NULL_TREE, attributes);
19091 else
19092 decl = start_decl (declarator, &decl_specs, 0,
19093 attributes, NULL_TREE, &pushed_scope);
19094 if (decl == error_mark_node)
19095 return decl;
19096
19097 // Attach constraints to the alias declaration.
19098 if (flag_concepts && current_template_parms)
19099 {
19100 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19101 tree constr = build_constraints (reqs, NULL_TREE);
19102 set_constraints (decl, constr);
19103 }
19104
19105 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19106
19107 if (pushed_scope)
19108 pop_scope (pushed_scope);
19109
19110 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19111 added into the symbol table; otherwise, return the TYPE_DECL. */
19112 if (DECL_LANG_SPECIFIC (decl)
19113 && DECL_TEMPLATE_INFO (decl)
19114 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19115 {
19116 decl = DECL_TI_TEMPLATE (decl);
19117 if (member_p)
19118 check_member_template (decl);
19119 }
19120
19121 return decl;
19122 }
19123
19124 /* Parse a using-directive.
19125
19126 using-directive:
19127 using namespace :: [opt] nested-name-specifier [opt]
19128 namespace-name ; */
19129
19130 static void
19131 cp_parser_using_directive (cp_parser* parser)
19132 {
19133 tree namespace_decl;
19134 tree attribs;
19135
19136 /* Look for the `using' keyword. */
19137 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19138 /* And the `namespace' keyword. */
19139 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19140 /* Look for the optional `::' operator. */
19141 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19142 /* And the optional nested-name-specifier. */
19143 cp_parser_nested_name_specifier_opt (parser,
19144 /*typename_keyword_p=*/false,
19145 /*check_dependency_p=*/true,
19146 /*type_p=*/false,
19147 /*is_declaration=*/true);
19148 /* Get the namespace being used. */
19149 namespace_decl = cp_parser_namespace_name (parser);
19150 /* And any specified attributes. */
19151 attribs = cp_parser_attributes_opt (parser);
19152
19153 /* Update the symbol table. */
19154 if (namespace_bindings_p ())
19155 finish_namespace_using_directive (namespace_decl, attribs);
19156 else
19157 finish_local_using_directive (namespace_decl, attribs);
19158
19159 /* Look for the final `;'. */
19160 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19161 }
19162
19163 /* Parse an asm-definition.
19164
19165 asm-definition:
19166 asm ( string-literal ) ;
19167
19168 GNU Extension:
19169
19170 asm-definition:
19171 asm volatile [opt] ( string-literal ) ;
19172 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19173 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19174 : asm-operand-list [opt] ) ;
19175 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19176 : asm-operand-list [opt]
19177 : asm-clobber-list [opt] ) ;
19178 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19179 : asm-clobber-list [opt]
19180 : asm-goto-list ) ; */
19181
19182 static void
19183 cp_parser_asm_definition (cp_parser* parser)
19184 {
19185 tree string;
19186 tree outputs = NULL_TREE;
19187 tree inputs = NULL_TREE;
19188 tree clobbers = NULL_TREE;
19189 tree labels = NULL_TREE;
19190 tree asm_stmt;
19191 bool volatile_p = false;
19192 bool extended_p = false;
19193 bool invalid_inputs_p = false;
19194 bool invalid_outputs_p = false;
19195 bool goto_p = false;
19196 required_token missing = RT_NONE;
19197
19198 /* Look for the `asm' keyword. */
19199 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19200
19201 if (parser->in_function_body
19202 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19203 {
19204 error ("%<asm%> in %<constexpr%> function");
19205 cp_function_chain->invalid_constexpr = true;
19206 }
19207
19208 /* See if the next token is `volatile'. */
19209 if (cp_parser_allow_gnu_extensions_p (parser)
19210 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19211 {
19212 /* Remember that we saw the `volatile' keyword. */
19213 volatile_p = true;
19214 /* Consume the token. */
19215 cp_lexer_consume_token (parser->lexer);
19216 }
19217 if (cp_parser_allow_gnu_extensions_p (parser)
19218 && parser->in_function_body
19219 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19220 {
19221 /* Remember that we saw the `goto' keyword. */
19222 goto_p = true;
19223 /* Consume the token. */
19224 cp_lexer_consume_token (parser->lexer);
19225 }
19226 /* Look for the opening `('. */
19227 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19228 return;
19229 /* Look for the string. */
19230 string = cp_parser_string_literal (parser, false, false);
19231 if (string == error_mark_node)
19232 {
19233 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19234 /*consume_paren=*/true);
19235 return;
19236 }
19237
19238 /* If we're allowing GNU extensions, check for the extended assembly
19239 syntax. Unfortunately, the `:' tokens need not be separated by
19240 a space in C, and so, for compatibility, we tolerate that here
19241 too. Doing that means that we have to treat the `::' operator as
19242 two `:' tokens. */
19243 if (cp_parser_allow_gnu_extensions_p (parser)
19244 && parser->in_function_body
19245 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19246 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19247 {
19248 bool inputs_p = false;
19249 bool clobbers_p = false;
19250 bool labels_p = false;
19251
19252 /* The extended syntax was used. */
19253 extended_p = true;
19254
19255 /* Look for outputs. */
19256 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19257 {
19258 /* Consume the `:'. */
19259 cp_lexer_consume_token (parser->lexer);
19260 /* Parse the output-operands. */
19261 if (cp_lexer_next_token_is_not (parser->lexer,
19262 CPP_COLON)
19263 && cp_lexer_next_token_is_not (parser->lexer,
19264 CPP_SCOPE)
19265 && cp_lexer_next_token_is_not (parser->lexer,
19266 CPP_CLOSE_PAREN)
19267 && !goto_p)
19268 {
19269 outputs = cp_parser_asm_operand_list (parser);
19270 if (outputs == error_mark_node)
19271 invalid_outputs_p = true;
19272 }
19273 }
19274 /* If the next token is `::', there are no outputs, and the
19275 next token is the beginning of the inputs. */
19276 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19277 /* The inputs are coming next. */
19278 inputs_p = true;
19279
19280 /* Look for inputs. */
19281 if (inputs_p
19282 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19283 {
19284 /* Consume the `:' or `::'. */
19285 cp_lexer_consume_token (parser->lexer);
19286 /* Parse the output-operands. */
19287 if (cp_lexer_next_token_is_not (parser->lexer,
19288 CPP_COLON)
19289 && cp_lexer_next_token_is_not (parser->lexer,
19290 CPP_SCOPE)
19291 && cp_lexer_next_token_is_not (parser->lexer,
19292 CPP_CLOSE_PAREN))
19293 {
19294 inputs = cp_parser_asm_operand_list (parser);
19295 if (inputs == error_mark_node)
19296 invalid_inputs_p = true;
19297 }
19298 }
19299 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19300 /* The clobbers are coming next. */
19301 clobbers_p = true;
19302
19303 /* Look for clobbers. */
19304 if (clobbers_p
19305 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19306 {
19307 clobbers_p = true;
19308 /* Consume the `:' or `::'. */
19309 cp_lexer_consume_token (parser->lexer);
19310 /* Parse the clobbers. */
19311 if (cp_lexer_next_token_is_not (parser->lexer,
19312 CPP_COLON)
19313 && cp_lexer_next_token_is_not (parser->lexer,
19314 CPP_CLOSE_PAREN))
19315 clobbers = cp_parser_asm_clobber_list (parser);
19316 }
19317 else if (goto_p
19318 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19319 /* The labels are coming next. */
19320 labels_p = true;
19321
19322 /* Look for labels. */
19323 if (labels_p
19324 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19325 {
19326 labels_p = true;
19327 /* Consume the `:' or `::'. */
19328 cp_lexer_consume_token (parser->lexer);
19329 /* Parse the labels. */
19330 labels = cp_parser_asm_label_list (parser);
19331 }
19332
19333 if (goto_p && !labels_p)
19334 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19335 }
19336 else if (goto_p)
19337 missing = RT_COLON_SCOPE;
19338
19339 /* Look for the closing `)'. */
19340 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19341 missing ? missing : RT_CLOSE_PAREN))
19342 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19343 /*consume_paren=*/true);
19344 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19345
19346 if (!invalid_inputs_p && !invalid_outputs_p)
19347 {
19348 /* Create the ASM_EXPR. */
19349 if (parser->in_function_body)
19350 {
19351 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19352 inputs, clobbers, labels);
19353 /* If the extended syntax was not used, mark the ASM_EXPR. */
19354 if (!extended_p)
19355 {
19356 tree temp = asm_stmt;
19357 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19358 temp = TREE_OPERAND (temp, 0);
19359
19360 ASM_INPUT_P (temp) = 1;
19361 }
19362 }
19363 else
19364 symtab->finalize_toplevel_asm (string);
19365 }
19366 }
19367
19368 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19369 type that comes from the decl-specifier-seq. */
19370
19371 static tree
19372 strip_declarator_types (tree type, cp_declarator *declarator)
19373 {
19374 for (cp_declarator *d = declarator; d;)
19375 switch (d->kind)
19376 {
19377 case cdk_id:
19378 case cdk_decomp:
19379 case cdk_error:
19380 d = NULL;
19381 break;
19382
19383 default:
19384 if (TYPE_PTRMEMFUNC_P (type))
19385 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19386 type = TREE_TYPE (type);
19387 d = d->declarator;
19388 break;
19389 }
19390
19391 return type;
19392 }
19393
19394 /* Declarators [gram.dcl.decl] */
19395
19396 /* Parse an init-declarator.
19397
19398 init-declarator:
19399 declarator initializer [opt]
19400
19401 GNU Extension:
19402
19403 init-declarator:
19404 declarator asm-specification [opt] attributes [opt] initializer [opt]
19405
19406 function-definition:
19407 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19408 function-body
19409 decl-specifier-seq [opt] declarator function-try-block
19410
19411 GNU Extension:
19412
19413 function-definition:
19414 __extension__ function-definition
19415
19416 TM Extension:
19417
19418 function-definition:
19419 decl-specifier-seq [opt] declarator function-transaction-block
19420
19421 The DECL_SPECIFIERS apply to this declarator. Returns a
19422 representation of the entity declared. If MEMBER_P is TRUE, then
19423 this declarator appears in a class scope. The new DECL created by
19424 this declarator is returned.
19425
19426 The CHECKS are access checks that should be performed once we know
19427 what entity is being declared (and, therefore, what classes have
19428 befriended it).
19429
19430 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19431 for a function-definition here as well. If the declarator is a
19432 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19433 be TRUE upon return. By that point, the function-definition will
19434 have been completely parsed.
19435
19436 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19437 is FALSE.
19438
19439 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19440 parsed declaration if it is an uninitialized single declarator not followed
19441 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19442 if present, will not be consumed. If returned, this declarator will be
19443 created with SD_INITIALIZED but will not call cp_finish_decl.
19444
19445 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19446 and there is an initializer, the pointed location_t is set to the
19447 location of the '=' or `(', or '{' in C++11 token introducing the
19448 initializer. */
19449
19450 static tree
19451 cp_parser_init_declarator (cp_parser* parser,
19452 cp_decl_specifier_seq *decl_specifiers,
19453 vec<deferred_access_check, va_gc> *checks,
19454 bool function_definition_allowed_p,
19455 bool member_p,
19456 int declares_class_or_enum,
19457 bool* function_definition_p,
19458 tree* maybe_range_for_decl,
19459 location_t* init_loc,
19460 tree* auto_result)
19461 {
19462 cp_token *token = NULL, *asm_spec_start_token = NULL,
19463 *attributes_start_token = NULL;
19464 cp_declarator *declarator;
19465 tree prefix_attributes;
19466 tree attributes = NULL;
19467 tree asm_specification;
19468 tree initializer;
19469 tree decl = NULL_TREE;
19470 tree scope;
19471 int is_initialized;
19472 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19473 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19474 "(...)". */
19475 enum cpp_ttype initialization_kind;
19476 bool is_direct_init = false;
19477 bool is_non_constant_init;
19478 int ctor_dtor_or_conv_p;
19479 bool friend_p = cp_parser_friend_p (decl_specifiers);
19480 tree pushed_scope = NULL_TREE;
19481 bool range_for_decl_p = false;
19482 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19483 location_t tmp_init_loc = UNKNOWN_LOCATION;
19484
19485 /* Gather the attributes that were provided with the
19486 decl-specifiers. */
19487 prefix_attributes = decl_specifiers->attributes;
19488
19489 /* Assume that this is not the declarator for a function
19490 definition. */
19491 if (function_definition_p)
19492 *function_definition_p = false;
19493
19494 /* Default arguments are only permitted for function parameters. */
19495 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19496 parser->default_arg_ok_p = false;
19497
19498 /* Defer access checks while parsing the declarator; we cannot know
19499 what names are accessible until we know what is being
19500 declared. */
19501 resume_deferring_access_checks ();
19502
19503 token = cp_lexer_peek_token (parser->lexer);
19504
19505 /* Parse the declarator. */
19506 declarator
19507 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19508 &ctor_dtor_or_conv_p,
19509 /*parenthesized_p=*/NULL,
19510 member_p, friend_p);
19511 /* Gather up the deferred checks. */
19512 stop_deferring_access_checks ();
19513
19514 parser->default_arg_ok_p = saved_default_arg_ok_p;
19515
19516 /* If the DECLARATOR was erroneous, there's no need to go
19517 further. */
19518 if (declarator == cp_error_declarator)
19519 return error_mark_node;
19520
19521 /* Check that the number of template-parameter-lists is OK. */
19522 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19523 token->location))
19524 return error_mark_node;
19525
19526 if (declares_class_or_enum & 2)
19527 cp_parser_check_for_definition_in_return_type (declarator,
19528 decl_specifiers->type,
19529 decl_specifiers->locations[ds_type_spec]);
19530
19531 /* Figure out what scope the entity declared by the DECLARATOR is
19532 located in. `grokdeclarator' sometimes changes the scope, so
19533 we compute it now. */
19534 scope = get_scope_of_declarator (declarator);
19535
19536 /* Perform any lookups in the declared type which were thought to be
19537 dependent, but are not in the scope of the declarator. */
19538 decl_specifiers->type
19539 = maybe_update_decl_type (decl_specifiers->type, scope);
19540
19541 /* If we're allowing GNU extensions, look for an
19542 asm-specification. */
19543 if (cp_parser_allow_gnu_extensions_p (parser))
19544 {
19545 /* Look for an asm-specification. */
19546 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19547 asm_specification = cp_parser_asm_specification_opt (parser);
19548 }
19549 else
19550 asm_specification = NULL_TREE;
19551
19552 /* Look for attributes. */
19553 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19554 attributes = cp_parser_attributes_opt (parser);
19555
19556 /* Peek at the next token. */
19557 token = cp_lexer_peek_token (parser->lexer);
19558
19559 bool bogus_implicit_tmpl = false;
19560
19561 if (function_declarator_p (declarator))
19562 {
19563 /* Handle C++17 deduction guides. */
19564 if (!decl_specifiers->type
19565 && ctor_dtor_or_conv_p <= 0
19566 && cxx_dialect >= cxx17)
19567 {
19568 cp_declarator *id = get_id_declarator (declarator);
19569 tree name = id->u.id.unqualified_name;
19570 parser->scope = id->u.id.qualifying_scope;
19571 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19572 if (tmpl
19573 && (DECL_CLASS_TEMPLATE_P (tmpl)
19574 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19575 {
19576 id->u.id.unqualified_name = dguide_name (tmpl);
19577 id->u.id.sfk = sfk_deduction_guide;
19578 ctor_dtor_or_conv_p = 1;
19579 }
19580 }
19581
19582 /* Check to see if the token indicates the start of a
19583 function-definition. */
19584 if (cp_parser_token_starts_function_definition_p (token))
19585 {
19586 if (!function_definition_allowed_p)
19587 {
19588 /* If a function-definition should not appear here, issue an
19589 error message. */
19590 cp_parser_error (parser,
19591 "a function-definition is not allowed here");
19592 return error_mark_node;
19593 }
19594
19595 location_t func_brace_location
19596 = cp_lexer_peek_token (parser->lexer)->location;
19597
19598 /* Neither attributes nor an asm-specification are allowed
19599 on a function-definition. */
19600 if (asm_specification)
19601 error_at (asm_spec_start_token->location,
19602 "an asm-specification is not allowed "
19603 "on a function-definition");
19604 if (attributes)
19605 error_at (attributes_start_token->location,
19606 "attributes are not allowed "
19607 "on a function-definition");
19608 /* This is a function-definition. */
19609 *function_definition_p = true;
19610
19611 /* Parse the function definition. */
19612 if (member_p)
19613 decl = cp_parser_save_member_function_body (parser,
19614 decl_specifiers,
19615 declarator,
19616 prefix_attributes);
19617 else
19618 decl =
19619 (cp_parser_function_definition_from_specifiers_and_declarator
19620 (parser, decl_specifiers, prefix_attributes, declarator));
19621
19622 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19623 {
19624 /* This is where the prologue starts... */
19625 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19626 = func_brace_location;
19627 }
19628
19629 return decl;
19630 }
19631 }
19632 else if (parser->fully_implicit_function_template_p)
19633 {
19634 /* A non-template declaration involving a function parameter list
19635 containing an implicit template parameter will be made into a
19636 template. If the resulting declaration is not going to be an
19637 actual function then finish the template scope here to prevent it.
19638 An error message will be issued once we have a decl to talk about.
19639
19640 FIXME probably we should do type deduction rather than create an
19641 implicit template, but the standard currently doesn't allow it. */
19642 bogus_implicit_tmpl = true;
19643 finish_fully_implicit_template (parser, NULL_TREE);
19644 }
19645
19646 /* [dcl.dcl]
19647
19648 Only in function declarations for constructors, destructors, type
19649 conversions, and deduction guides can the decl-specifier-seq be omitted.
19650
19651 We explicitly postpone this check past the point where we handle
19652 function-definitions because we tolerate function-definitions
19653 that are missing their return types in some modes. */
19654 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19655 {
19656 cp_parser_error (parser,
19657 "expected constructor, destructor, or type conversion");
19658 return error_mark_node;
19659 }
19660
19661 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19662 if (token->type == CPP_EQ
19663 || token->type == CPP_OPEN_PAREN
19664 || token->type == CPP_OPEN_BRACE)
19665 {
19666 is_initialized = SD_INITIALIZED;
19667 initialization_kind = token->type;
19668 if (maybe_range_for_decl)
19669 *maybe_range_for_decl = error_mark_node;
19670 tmp_init_loc = token->location;
19671 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19672 *init_loc = tmp_init_loc;
19673
19674 if (token->type == CPP_EQ
19675 && function_declarator_p (declarator))
19676 {
19677 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19678 if (t2->keyword == RID_DEFAULT)
19679 is_initialized = SD_DEFAULTED;
19680 else if (t2->keyword == RID_DELETE)
19681 is_initialized = SD_DELETED;
19682 }
19683 }
19684 else
19685 {
19686 /* If the init-declarator isn't initialized and isn't followed by a
19687 `,' or `;', it's not a valid init-declarator. */
19688 if (token->type != CPP_COMMA
19689 && token->type != CPP_SEMICOLON)
19690 {
19691 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19692 range_for_decl_p = true;
19693 else
19694 {
19695 if (!maybe_range_for_decl)
19696 cp_parser_error (parser, "expected initializer");
19697 return error_mark_node;
19698 }
19699 }
19700 is_initialized = SD_UNINITIALIZED;
19701 initialization_kind = CPP_EOF;
19702 }
19703
19704 /* Because start_decl has side-effects, we should only call it if we
19705 know we're going ahead. By this point, we know that we cannot
19706 possibly be looking at any other construct. */
19707 cp_parser_commit_to_tentative_parse (parser);
19708
19709 /* Enter the newly declared entry in the symbol table. If we're
19710 processing a declaration in a class-specifier, we wait until
19711 after processing the initializer. */
19712 if (!member_p)
19713 {
19714 if (parser->in_unbraced_linkage_specification_p)
19715 decl_specifiers->storage_class = sc_extern;
19716 decl = start_decl (declarator, decl_specifiers,
19717 range_for_decl_p? SD_INITIALIZED : is_initialized,
19718 attributes, prefix_attributes, &pushed_scope);
19719 cp_finalize_omp_declare_simd (parser, decl);
19720 cp_finalize_oacc_routine (parser, decl, false);
19721 /* Adjust location of decl if declarator->id_loc is more appropriate:
19722 set, and decl wasn't merged with another decl, in which case its
19723 location would be different from input_location, and more accurate. */
19724 if (DECL_P (decl)
19725 && declarator->id_loc != UNKNOWN_LOCATION
19726 && DECL_SOURCE_LOCATION (decl) == input_location)
19727 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19728 }
19729 else if (scope)
19730 /* Enter the SCOPE. That way unqualified names appearing in the
19731 initializer will be looked up in SCOPE. */
19732 pushed_scope = push_scope (scope);
19733
19734 /* Perform deferred access control checks, now that we know in which
19735 SCOPE the declared entity resides. */
19736 if (!member_p && decl)
19737 {
19738 tree saved_current_function_decl = NULL_TREE;
19739
19740 /* If the entity being declared is a function, pretend that we
19741 are in its scope. If it is a `friend', it may have access to
19742 things that would not otherwise be accessible. */
19743 if (TREE_CODE (decl) == FUNCTION_DECL)
19744 {
19745 saved_current_function_decl = current_function_decl;
19746 current_function_decl = decl;
19747 }
19748
19749 /* Perform access checks for template parameters. */
19750 cp_parser_perform_template_parameter_access_checks (checks);
19751
19752 /* Perform the access control checks for the declarator and the
19753 decl-specifiers. */
19754 perform_deferred_access_checks (tf_warning_or_error);
19755
19756 /* Restore the saved value. */
19757 if (TREE_CODE (decl) == FUNCTION_DECL)
19758 current_function_decl = saved_current_function_decl;
19759 }
19760
19761 /* Parse the initializer. */
19762 initializer = NULL_TREE;
19763 is_direct_init = false;
19764 is_non_constant_init = true;
19765 if (is_initialized)
19766 {
19767 if (function_declarator_p (declarator))
19768 {
19769 if (initialization_kind == CPP_EQ)
19770 initializer = cp_parser_pure_specifier (parser);
19771 else
19772 {
19773 /* If the declaration was erroneous, we don't really
19774 know what the user intended, so just silently
19775 consume the initializer. */
19776 if (decl != error_mark_node)
19777 error_at (tmp_init_loc, "initializer provided for function");
19778 cp_parser_skip_to_closing_parenthesis (parser,
19779 /*recovering=*/true,
19780 /*or_comma=*/false,
19781 /*consume_paren=*/true);
19782 }
19783 }
19784 else
19785 {
19786 /* We want to record the extra mangling scope for in-class
19787 initializers of class members and initializers of static data
19788 member templates. The former involves deferring
19789 parsing of the initializer until end of class as with default
19790 arguments. So right here we only handle the latter. */
19791 if (!member_p && processing_template_decl && decl != error_mark_node)
19792 start_lambda_scope (decl);
19793 initializer = cp_parser_initializer (parser,
19794 &is_direct_init,
19795 &is_non_constant_init);
19796 if (!member_p && processing_template_decl && decl != error_mark_node)
19797 finish_lambda_scope ();
19798 if (initializer == error_mark_node)
19799 cp_parser_skip_to_end_of_statement (parser);
19800 }
19801 }
19802
19803 /* The old parser allows attributes to appear after a parenthesized
19804 initializer. Mark Mitchell proposed removing this functionality
19805 on the GCC mailing lists on 2002-08-13. This parser accepts the
19806 attributes -- but ignores them. Made a permerror in GCC 8. */
19807 if (cp_parser_allow_gnu_extensions_p (parser)
19808 && initialization_kind == CPP_OPEN_PAREN
19809 && cp_parser_attributes_opt (parser)
19810 && permerror (input_location,
19811 "attributes after parenthesized initializer ignored"))
19812 {
19813 static bool hint;
19814 if (flag_permissive && !hint)
19815 {
19816 hint = true;
19817 inform (input_location,
19818 "this flexibility is deprecated and will be removed");
19819 }
19820 }
19821
19822 /* And now complain about a non-function implicit template. */
19823 if (bogus_implicit_tmpl && decl != error_mark_node)
19824 error_at (DECL_SOURCE_LOCATION (decl),
19825 "non-function %qD declared as implicit template", decl);
19826
19827 /* For an in-class declaration, use `grokfield' to create the
19828 declaration. */
19829 if (member_p)
19830 {
19831 if (pushed_scope)
19832 {
19833 pop_scope (pushed_scope);
19834 pushed_scope = NULL_TREE;
19835 }
19836 decl = grokfield (declarator, decl_specifiers,
19837 initializer, !is_non_constant_init,
19838 /*asmspec=*/NULL_TREE,
19839 attr_chainon (attributes, prefix_attributes));
19840 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19841 cp_parser_save_default_args (parser, decl);
19842 cp_finalize_omp_declare_simd (parser, decl);
19843 cp_finalize_oacc_routine (parser, decl, false);
19844 }
19845
19846 /* Finish processing the declaration. But, skip member
19847 declarations. */
19848 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19849 {
19850 cp_finish_decl (decl,
19851 initializer, !is_non_constant_init,
19852 asm_specification,
19853 /* If the initializer is in parentheses, then this is
19854 a direct-initialization, which means that an
19855 `explicit' constructor is OK. Otherwise, an
19856 `explicit' constructor cannot be used. */
19857 ((is_direct_init || !is_initialized)
19858 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19859 }
19860 else if ((cxx_dialect != cxx98) && friend_p
19861 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19862 /* Core issue #226 (C++0x only): A default template-argument
19863 shall not be specified in a friend class template
19864 declaration. */
19865 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19866 /*is_partial=*/false, /*is_friend_decl=*/1);
19867
19868 if (!friend_p && pushed_scope)
19869 pop_scope (pushed_scope);
19870
19871 if (function_declarator_p (declarator)
19872 && parser->fully_implicit_function_template_p)
19873 {
19874 if (member_p)
19875 decl = finish_fully_implicit_template (parser, decl);
19876 else
19877 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19878 }
19879
19880 if (auto_result && is_initialized && decl_specifiers->type
19881 && type_uses_auto (decl_specifiers->type))
19882 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19883
19884 return decl;
19885 }
19886
19887 /* Parse a declarator.
19888
19889 declarator:
19890 direct-declarator
19891 ptr-operator declarator
19892
19893 abstract-declarator:
19894 ptr-operator abstract-declarator [opt]
19895 direct-abstract-declarator
19896
19897 GNU Extensions:
19898
19899 declarator:
19900 attributes [opt] direct-declarator
19901 attributes [opt] ptr-operator declarator
19902
19903 abstract-declarator:
19904 attributes [opt] ptr-operator abstract-declarator [opt]
19905 attributes [opt] direct-abstract-declarator
19906
19907 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19908 detect constructors, destructors, deduction guides, or conversion operators.
19909 It is set to -1 if the declarator is a name, and +1 if it is a
19910 function. Otherwise it is set to zero. Usually you just want to
19911 test for >0, but internally the negative value is used.
19912
19913 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19914 a decl-specifier-seq unless it declares a constructor, destructor,
19915 or conversion. It might seem that we could check this condition in
19916 semantic analysis, rather than parsing, but that makes it difficult
19917 to handle something like `f()'. We want to notice that there are
19918 no decl-specifiers, and therefore realize that this is an
19919 expression, not a declaration.)
19920
19921 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19922 the declarator is a direct-declarator of the form "(...)".
19923
19924 MEMBER_P is true iff this declarator is a member-declarator.
19925
19926 FRIEND_P is true iff this declarator is a friend. */
19927
19928 static cp_declarator *
19929 cp_parser_declarator (cp_parser* parser,
19930 cp_parser_declarator_kind dcl_kind,
19931 int* ctor_dtor_or_conv_p,
19932 bool* parenthesized_p,
19933 bool member_p, bool friend_p)
19934 {
19935 cp_declarator *declarator;
19936 enum tree_code code;
19937 cp_cv_quals cv_quals;
19938 tree class_type;
19939 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19940
19941 /* Assume this is not a constructor, destructor, or type-conversion
19942 operator. */
19943 if (ctor_dtor_or_conv_p)
19944 *ctor_dtor_or_conv_p = 0;
19945
19946 if (cp_parser_allow_gnu_extensions_p (parser))
19947 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19948
19949 /* Check for the ptr-operator production. */
19950 cp_parser_parse_tentatively (parser);
19951 /* Parse the ptr-operator. */
19952 code = cp_parser_ptr_operator (parser,
19953 &class_type,
19954 &cv_quals,
19955 &std_attributes);
19956
19957 /* If that worked, then we have a ptr-operator. */
19958 if (cp_parser_parse_definitely (parser))
19959 {
19960 /* If a ptr-operator was found, then this declarator was not
19961 parenthesized. */
19962 if (parenthesized_p)
19963 *parenthesized_p = true;
19964 /* The dependent declarator is optional if we are parsing an
19965 abstract-declarator. */
19966 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19967 cp_parser_parse_tentatively (parser);
19968
19969 /* Parse the dependent declarator. */
19970 declarator = cp_parser_declarator (parser, dcl_kind,
19971 /*ctor_dtor_or_conv_p=*/NULL,
19972 /*parenthesized_p=*/NULL,
19973 /*member_p=*/false,
19974 friend_p);
19975
19976 /* If we are parsing an abstract-declarator, we must handle the
19977 case where the dependent declarator is absent. */
19978 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19979 && !cp_parser_parse_definitely (parser))
19980 declarator = NULL;
19981
19982 declarator = cp_parser_make_indirect_declarator
19983 (code, class_type, cv_quals, declarator, std_attributes);
19984 }
19985 /* Everything else is a direct-declarator. */
19986 else
19987 {
19988 if (parenthesized_p)
19989 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19990 CPP_OPEN_PAREN);
19991 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19992 ctor_dtor_or_conv_p,
19993 member_p, friend_p);
19994 }
19995
19996 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19997 declarator->attributes = gnu_attributes;
19998 return declarator;
19999 }
20000
20001 /* Parse a direct-declarator or direct-abstract-declarator.
20002
20003 direct-declarator:
20004 declarator-id
20005 direct-declarator ( parameter-declaration-clause )
20006 cv-qualifier-seq [opt]
20007 ref-qualifier [opt]
20008 exception-specification [opt]
20009 direct-declarator [ constant-expression [opt] ]
20010 ( declarator )
20011
20012 direct-abstract-declarator:
20013 direct-abstract-declarator [opt]
20014 ( parameter-declaration-clause )
20015 cv-qualifier-seq [opt]
20016 ref-qualifier [opt]
20017 exception-specification [opt]
20018 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20019 ( abstract-declarator )
20020
20021 Returns a representation of the declarator. DCL_KIND is
20022 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20023 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20024 we are parsing a direct-declarator. It is
20025 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20026 of ambiguity we prefer an abstract declarator, as per
20027 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20028 as for cp_parser_declarator. */
20029
20030 static cp_declarator *
20031 cp_parser_direct_declarator (cp_parser* parser,
20032 cp_parser_declarator_kind dcl_kind,
20033 int* ctor_dtor_or_conv_p,
20034 bool member_p, bool friend_p)
20035 {
20036 cp_token *token;
20037 cp_declarator *declarator = NULL;
20038 tree scope = NULL_TREE;
20039 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20040 bool saved_in_declarator_p = parser->in_declarator_p;
20041 bool first = true;
20042 tree pushed_scope = NULL_TREE;
20043 cp_token *open_paren = NULL, *close_paren = NULL;
20044
20045 while (true)
20046 {
20047 /* Peek at the next token. */
20048 token = cp_lexer_peek_token (parser->lexer);
20049 if (token->type == CPP_OPEN_PAREN)
20050 {
20051 /* This is either a parameter-declaration-clause, or a
20052 parenthesized declarator. When we know we are parsing a
20053 named declarator, it must be a parenthesized declarator
20054 if FIRST is true. For instance, `(int)' is a
20055 parameter-declaration-clause, with an omitted
20056 direct-abstract-declarator. But `((*))', is a
20057 parenthesized abstract declarator. Finally, when T is a
20058 template parameter `(T)' is a
20059 parameter-declaration-clause, and not a parenthesized
20060 named declarator.
20061
20062 We first try and parse a parameter-declaration-clause,
20063 and then try a nested declarator (if FIRST is true).
20064
20065 It is not an error for it not to be a
20066 parameter-declaration-clause, even when FIRST is
20067 false. Consider,
20068
20069 int i (int);
20070 int i (3);
20071
20072 The first is the declaration of a function while the
20073 second is the definition of a variable, including its
20074 initializer.
20075
20076 Having seen only the parenthesis, we cannot know which of
20077 these two alternatives should be selected. Even more
20078 complex are examples like:
20079
20080 int i (int (a));
20081 int i (int (3));
20082
20083 The former is a function-declaration; the latter is a
20084 variable initialization.
20085
20086 Thus again, we try a parameter-declaration-clause, and if
20087 that fails, we back out and return. */
20088
20089 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20090 {
20091 tree params;
20092 bool is_declarator = false;
20093
20094 open_paren = NULL;
20095
20096 /* In a member-declarator, the only valid interpretation
20097 of a parenthesis is the start of a
20098 parameter-declaration-clause. (It is invalid to
20099 initialize a static data member with a parenthesized
20100 initializer; only the "=" form of initialization is
20101 permitted.) */
20102 if (!member_p)
20103 cp_parser_parse_tentatively (parser);
20104
20105 /* Consume the `('. */
20106 matching_parens parens;
20107 parens.consume_open (parser);
20108 if (first)
20109 {
20110 /* If this is going to be an abstract declarator, we're
20111 in a declarator and we can't have default args. */
20112 parser->default_arg_ok_p = false;
20113 parser->in_declarator_p = true;
20114 }
20115
20116 begin_scope (sk_function_parms, NULL_TREE);
20117
20118 /* Parse the parameter-declaration-clause. */
20119 params = cp_parser_parameter_declaration_clause (parser);
20120
20121 /* Consume the `)'. */
20122 parens.require_close (parser);
20123
20124 /* If all went well, parse the cv-qualifier-seq,
20125 ref-qualifier and the exception-specification. */
20126 if (member_p || cp_parser_parse_definitely (parser))
20127 {
20128 cp_cv_quals cv_quals;
20129 cp_virt_specifiers virt_specifiers;
20130 cp_ref_qualifier ref_qual;
20131 tree exception_specification;
20132 tree late_return;
20133 tree attrs;
20134 bool memfn = (member_p || (pushed_scope
20135 && CLASS_TYPE_P (pushed_scope)));
20136
20137 is_declarator = true;
20138
20139 if (ctor_dtor_or_conv_p)
20140 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20141 first = false;
20142
20143 /* Parse the cv-qualifier-seq. */
20144 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20145 /* Parse the ref-qualifier. */
20146 ref_qual = cp_parser_ref_qualifier_opt (parser);
20147 /* Parse the tx-qualifier. */
20148 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20149 /* And the exception-specification. */
20150 exception_specification
20151 = cp_parser_exception_specification_opt (parser);
20152
20153 attrs = cp_parser_std_attribute_spec_seq (parser);
20154
20155 /* In here, we handle cases where attribute is used after
20156 the function declaration. For example:
20157 void func (int x) __attribute__((vector(..))); */
20158 tree gnu_attrs = NULL_TREE;
20159 tree requires_clause = NULL_TREE;
20160 late_return = (cp_parser_late_return_type_opt
20161 (parser, declarator, requires_clause,
20162 memfn ? cv_quals : -1));
20163
20164 /* Parse the virt-specifier-seq. */
20165 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20166
20167 /* Create the function-declarator. */
20168 declarator = make_call_declarator (declarator,
20169 params,
20170 cv_quals,
20171 virt_specifiers,
20172 ref_qual,
20173 tx_qual,
20174 exception_specification,
20175 late_return,
20176 requires_clause);
20177 declarator->std_attributes = attrs;
20178 declarator->attributes = gnu_attrs;
20179 /* Any subsequent parameter lists are to do with
20180 return type, so are not those of the declared
20181 function. */
20182 parser->default_arg_ok_p = false;
20183 }
20184
20185 /* Remove the function parms from scope. */
20186 pop_bindings_and_leave_scope ();
20187
20188 if (is_declarator)
20189 /* Repeat the main loop. */
20190 continue;
20191 }
20192
20193 /* If this is the first, we can try a parenthesized
20194 declarator. */
20195 if (first)
20196 {
20197 bool saved_in_type_id_in_expr_p;
20198
20199 parser->default_arg_ok_p = saved_default_arg_ok_p;
20200 parser->in_declarator_p = saved_in_declarator_p;
20201
20202 open_paren = token;
20203 /* Consume the `('. */
20204 matching_parens parens;
20205 parens.consume_open (parser);
20206 /* Parse the nested declarator. */
20207 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20208 parser->in_type_id_in_expr_p = true;
20209 declarator
20210 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20211 /*parenthesized_p=*/NULL,
20212 member_p, friend_p);
20213 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20214 first = false;
20215 /* Expect a `)'. */
20216 close_paren = cp_lexer_peek_token (parser->lexer);
20217 if (!parens.require_close (parser))
20218 declarator = cp_error_declarator;
20219 if (declarator == cp_error_declarator)
20220 break;
20221
20222 goto handle_declarator;
20223 }
20224 /* Otherwise, we must be done. */
20225 else
20226 break;
20227 }
20228 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20229 && token->type == CPP_OPEN_SQUARE
20230 && !cp_next_tokens_can_be_attribute_p (parser))
20231 {
20232 /* Parse an array-declarator. */
20233 tree bounds, attrs;
20234
20235 if (ctor_dtor_or_conv_p)
20236 *ctor_dtor_or_conv_p = 0;
20237
20238 open_paren = NULL;
20239 first = false;
20240 parser->default_arg_ok_p = false;
20241 parser->in_declarator_p = true;
20242 /* Consume the `['. */
20243 cp_lexer_consume_token (parser->lexer);
20244 /* Peek at the next token. */
20245 token = cp_lexer_peek_token (parser->lexer);
20246 /* If the next token is `]', then there is no
20247 constant-expression. */
20248 if (token->type != CPP_CLOSE_SQUARE)
20249 {
20250 bool non_constant_p;
20251 bounds
20252 = cp_parser_constant_expression (parser,
20253 /*allow_non_constant=*/true,
20254 &non_constant_p);
20255 if (!non_constant_p)
20256 /* OK */;
20257 else if (error_operand_p (bounds))
20258 /* Already gave an error. */;
20259 else if (!parser->in_function_body
20260 || current_binding_level->kind == sk_function_parms)
20261 {
20262 /* Normally, the array bound must be an integral constant
20263 expression. However, as an extension, we allow VLAs
20264 in function scopes as long as they aren't part of a
20265 parameter declaration. */
20266 cp_parser_error (parser,
20267 "array bound is not an integer constant");
20268 bounds = error_mark_node;
20269 }
20270 else if (processing_template_decl
20271 && !type_dependent_expression_p (bounds))
20272 {
20273 /* Remember this wasn't a constant-expression. */
20274 bounds = build_nop (TREE_TYPE (bounds), bounds);
20275 TREE_SIDE_EFFECTS (bounds) = 1;
20276 }
20277 }
20278 else
20279 bounds = NULL_TREE;
20280 /* Look for the closing `]'. */
20281 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20282 {
20283 declarator = cp_error_declarator;
20284 break;
20285 }
20286
20287 attrs = cp_parser_std_attribute_spec_seq (parser);
20288 declarator = make_array_declarator (declarator, bounds);
20289 declarator->std_attributes = attrs;
20290 }
20291 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20292 {
20293 {
20294 tree qualifying_scope;
20295 tree unqualified_name;
20296 tree attrs;
20297 special_function_kind sfk;
20298 bool abstract_ok;
20299 bool pack_expansion_p = false;
20300 cp_token *declarator_id_start_token;
20301
20302 /* Parse a declarator-id */
20303 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20304 if (abstract_ok)
20305 {
20306 cp_parser_parse_tentatively (parser);
20307
20308 /* If we see an ellipsis, we should be looking at a
20309 parameter pack. */
20310 if (token->type == CPP_ELLIPSIS)
20311 {
20312 /* Consume the `...' */
20313 cp_lexer_consume_token (parser->lexer);
20314
20315 pack_expansion_p = true;
20316 }
20317 }
20318
20319 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20320 unqualified_name
20321 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20322 qualifying_scope = parser->scope;
20323 if (abstract_ok)
20324 {
20325 bool okay = false;
20326
20327 if (!unqualified_name && pack_expansion_p)
20328 {
20329 /* Check whether an error occurred. */
20330 okay = !cp_parser_error_occurred (parser);
20331
20332 /* We already consumed the ellipsis to mark a
20333 parameter pack, but we have no way to report it,
20334 so abort the tentative parse. We will be exiting
20335 immediately anyway. */
20336 cp_parser_abort_tentative_parse (parser);
20337 }
20338 else
20339 okay = cp_parser_parse_definitely (parser);
20340
20341 if (!okay)
20342 unqualified_name = error_mark_node;
20343 else if (unqualified_name
20344 && (qualifying_scope
20345 || (!identifier_p (unqualified_name))))
20346 {
20347 cp_parser_error (parser, "expected unqualified-id");
20348 unqualified_name = error_mark_node;
20349 }
20350 }
20351
20352 if (!unqualified_name)
20353 return NULL;
20354 if (unqualified_name == error_mark_node)
20355 {
20356 declarator = cp_error_declarator;
20357 pack_expansion_p = false;
20358 declarator->parameter_pack_p = false;
20359 break;
20360 }
20361
20362 attrs = cp_parser_std_attribute_spec_seq (parser);
20363
20364 if (qualifying_scope && at_namespace_scope_p ()
20365 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20366 {
20367 /* In the declaration of a member of a template class
20368 outside of the class itself, the SCOPE will sometimes
20369 be a TYPENAME_TYPE. For example, given:
20370
20371 template <typename T>
20372 int S<T>::R::i = 3;
20373
20374 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20375 this context, we must resolve S<T>::R to an ordinary
20376 type, rather than a typename type.
20377
20378 The reason we normally avoid resolving TYPENAME_TYPEs
20379 is that a specialization of `S' might render
20380 `S<T>::R' not a type. However, if `S' is
20381 specialized, then this `i' will not be used, so there
20382 is no harm in resolving the types here. */
20383 tree type;
20384
20385 /* Resolve the TYPENAME_TYPE. */
20386 type = resolve_typename_type (qualifying_scope,
20387 /*only_current_p=*/false);
20388 /* If that failed, the declarator is invalid. */
20389 if (TREE_CODE (type) == TYPENAME_TYPE)
20390 {
20391 if (typedef_variant_p (type))
20392 error_at (declarator_id_start_token->location,
20393 "cannot define member of dependent typedef "
20394 "%qT", type);
20395 else
20396 error_at (declarator_id_start_token->location,
20397 "%<%T::%E%> is not a type",
20398 TYPE_CONTEXT (qualifying_scope),
20399 TYPE_IDENTIFIER (qualifying_scope));
20400 }
20401 qualifying_scope = type;
20402 }
20403
20404 sfk = sfk_none;
20405
20406 if (unqualified_name)
20407 {
20408 tree class_type;
20409
20410 if (qualifying_scope
20411 && CLASS_TYPE_P (qualifying_scope))
20412 class_type = qualifying_scope;
20413 else
20414 class_type = current_class_type;
20415
20416 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20417 {
20418 tree name_type = TREE_TYPE (unqualified_name);
20419
20420 if (!class_type || !same_type_p (name_type, class_type))
20421 {
20422 /* We do not attempt to print the declarator
20423 here because we do not have enough
20424 information about its original syntactic
20425 form. */
20426 cp_parser_error (parser, "invalid declarator");
20427 declarator = cp_error_declarator;
20428 break;
20429 }
20430 else if (qualifying_scope
20431 && CLASSTYPE_USE_TEMPLATE (name_type))
20432 {
20433 error_at (declarator_id_start_token->location,
20434 "invalid use of constructor as a template");
20435 inform (declarator_id_start_token->location,
20436 "use %<%T::%D%> instead of %<%T::%D%> to "
20437 "name the constructor in a qualified name",
20438 class_type,
20439 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20440 class_type, name_type);
20441 declarator = cp_error_declarator;
20442 break;
20443 }
20444 unqualified_name = constructor_name (class_type);
20445 }
20446
20447 if (class_type)
20448 {
20449 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20450 sfk = sfk_destructor;
20451 else if (identifier_p (unqualified_name)
20452 && IDENTIFIER_CONV_OP_P (unqualified_name))
20453 sfk = sfk_conversion;
20454 else if (/* There's no way to declare a constructor
20455 for an unnamed type, even if the type
20456 got a name for linkage purposes. */
20457 !TYPE_WAS_UNNAMED (class_type)
20458 /* Handle correctly (c++/19200):
20459
20460 struct S {
20461 struct T{};
20462 friend void S(T);
20463 };
20464
20465 and also:
20466
20467 namespace N {
20468 void S();
20469 }
20470
20471 struct S {
20472 friend void N::S();
20473 }; */
20474 && (!friend_p || class_type == qualifying_scope)
20475 && constructor_name_p (unqualified_name,
20476 class_type))
20477 sfk = sfk_constructor;
20478 else if (is_overloaded_fn (unqualified_name)
20479 && DECL_CONSTRUCTOR_P (get_first_fn
20480 (unqualified_name)))
20481 sfk = sfk_constructor;
20482
20483 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20484 *ctor_dtor_or_conv_p = -1;
20485 }
20486 }
20487 declarator = make_id_declarator (qualifying_scope,
20488 unqualified_name,
20489 sfk);
20490 declarator->std_attributes = attrs;
20491 declarator->id_loc = token->location;
20492 declarator->parameter_pack_p = pack_expansion_p;
20493
20494 if (pack_expansion_p)
20495 maybe_warn_variadic_templates ();
20496 }
20497
20498 handle_declarator:;
20499 scope = get_scope_of_declarator (declarator);
20500 if (scope)
20501 {
20502 /* Any names that appear after the declarator-id for a
20503 member are looked up in the containing scope. */
20504 if (at_function_scope_p ())
20505 {
20506 /* But declarations with qualified-ids can't appear in a
20507 function. */
20508 cp_parser_error (parser, "qualified-id in declaration");
20509 declarator = cp_error_declarator;
20510 break;
20511 }
20512 pushed_scope = push_scope (scope);
20513 }
20514 parser->in_declarator_p = true;
20515 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20516 || (declarator && declarator->kind == cdk_id))
20517 /* Default args are only allowed on function
20518 declarations. */
20519 parser->default_arg_ok_p = saved_default_arg_ok_p;
20520 else
20521 parser->default_arg_ok_p = false;
20522
20523 first = false;
20524 }
20525 /* We're done. */
20526 else
20527 break;
20528 }
20529
20530 /* For an abstract declarator, we might wind up with nothing at this
20531 point. That's an error; the declarator is not optional. */
20532 if (!declarator)
20533 cp_parser_error (parser, "expected declarator");
20534 else if (open_paren)
20535 {
20536 /* Record overly parenthesized declarator so we can give a
20537 diagnostic about confusing decl/expr disambiguation. */
20538 if (declarator->kind == cdk_array)
20539 {
20540 /* If the open and close parens are on different lines, this
20541 is probably a formatting thing, so ignore. */
20542 expanded_location open = expand_location (open_paren->location);
20543 expanded_location close = expand_location (close_paren->location);
20544 if (open.line != close.line || open.file != close.file)
20545 open_paren = NULL;
20546 }
20547 if (open_paren)
20548 declarator->parenthesized = open_paren->location;
20549 }
20550
20551 /* If we entered a scope, we must exit it now. */
20552 if (pushed_scope)
20553 pop_scope (pushed_scope);
20554
20555 parser->default_arg_ok_p = saved_default_arg_ok_p;
20556 parser->in_declarator_p = saved_in_declarator_p;
20557
20558 return declarator;
20559 }
20560
20561 /* Parse a ptr-operator.
20562
20563 ptr-operator:
20564 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20565 * cv-qualifier-seq [opt]
20566 &
20567 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20568 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20569
20570 GNU Extension:
20571
20572 ptr-operator:
20573 & cv-qualifier-seq [opt]
20574
20575 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20576 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20577 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20578 filled in with the TYPE containing the member. *CV_QUALS is
20579 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20580 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20581 Note that the tree codes returned by this function have nothing
20582 to do with the types of trees that will be eventually be created
20583 to represent the pointer or reference type being parsed. They are
20584 just constants with suggestive names. */
20585 static enum tree_code
20586 cp_parser_ptr_operator (cp_parser* parser,
20587 tree* type,
20588 cp_cv_quals *cv_quals,
20589 tree *attributes)
20590 {
20591 enum tree_code code = ERROR_MARK;
20592 cp_token *token;
20593 tree attrs = NULL_TREE;
20594
20595 /* Assume that it's not a pointer-to-member. */
20596 *type = NULL_TREE;
20597 /* And that there are no cv-qualifiers. */
20598 *cv_quals = TYPE_UNQUALIFIED;
20599
20600 /* Peek at the next token. */
20601 token = cp_lexer_peek_token (parser->lexer);
20602
20603 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20604 if (token->type == CPP_MULT)
20605 code = INDIRECT_REF;
20606 else if (token->type == CPP_AND)
20607 code = ADDR_EXPR;
20608 else if ((cxx_dialect != cxx98) &&
20609 token->type == CPP_AND_AND) /* C++0x only */
20610 code = NON_LVALUE_EXPR;
20611
20612 if (code != ERROR_MARK)
20613 {
20614 /* Consume the `*', `&' or `&&'. */
20615 cp_lexer_consume_token (parser->lexer);
20616
20617 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20618 `&', if we are allowing GNU extensions. (The only qualifier
20619 that can legally appear after `&' is `restrict', but that is
20620 enforced during semantic analysis. */
20621 if (code == INDIRECT_REF
20622 || cp_parser_allow_gnu_extensions_p (parser))
20623 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20624
20625 attrs = cp_parser_std_attribute_spec_seq (parser);
20626 if (attributes != NULL)
20627 *attributes = attrs;
20628 }
20629 else
20630 {
20631 /* Try the pointer-to-member case. */
20632 cp_parser_parse_tentatively (parser);
20633 /* Look for the optional `::' operator. */
20634 cp_parser_global_scope_opt (parser,
20635 /*current_scope_valid_p=*/false);
20636 /* Look for the nested-name specifier. */
20637 token = cp_lexer_peek_token (parser->lexer);
20638 cp_parser_nested_name_specifier (parser,
20639 /*typename_keyword_p=*/false,
20640 /*check_dependency_p=*/true,
20641 /*type_p=*/false,
20642 /*is_declaration=*/false);
20643 /* If we found it, and the next token is a `*', then we are
20644 indeed looking at a pointer-to-member operator. */
20645 if (!cp_parser_error_occurred (parser)
20646 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20647 {
20648 /* Indicate that the `*' operator was used. */
20649 code = INDIRECT_REF;
20650
20651 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20652 error_at (token->location, "%qD is a namespace", parser->scope);
20653 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20654 error_at (token->location, "cannot form pointer to member of "
20655 "non-class %q#T", parser->scope);
20656 else
20657 {
20658 /* The type of which the member is a member is given by the
20659 current SCOPE. */
20660 *type = parser->scope;
20661 /* The next name will not be qualified. */
20662 parser->scope = NULL_TREE;
20663 parser->qualifying_scope = NULL_TREE;
20664 parser->object_scope = NULL_TREE;
20665 /* Look for optional c++11 attributes. */
20666 attrs = cp_parser_std_attribute_spec_seq (parser);
20667 if (attributes != NULL)
20668 *attributes = attrs;
20669 /* Look for the optional cv-qualifier-seq. */
20670 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20671 }
20672 }
20673 /* If that didn't work we don't have a ptr-operator. */
20674 if (!cp_parser_parse_definitely (parser))
20675 cp_parser_error (parser, "expected ptr-operator");
20676 }
20677
20678 return code;
20679 }
20680
20681 /* Parse an (optional) cv-qualifier-seq.
20682
20683 cv-qualifier-seq:
20684 cv-qualifier cv-qualifier-seq [opt]
20685
20686 cv-qualifier:
20687 const
20688 volatile
20689
20690 GNU Extension:
20691
20692 cv-qualifier:
20693 __restrict__
20694
20695 Returns a bitmask representing the cv-qualifiers. */
20696
20697 static cp_cv_quals
20698 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20699 {
20700 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20701
20702 while (true)
20703 {
20704 cp_token *token;
20705 cp_cv_quals cv_qualifier;
20706
20707 /* Peek at the next token. */
20708 token = cp_lexer_peek_token (parser->lexer);
20709 /* See if it's a cv-qualifier. */
20710 switch (token->keyword)
20711 {
20712 case RID_CONST:
20713 cv_qualifier = TYPE_QUAL_CONST;
20714 break;
20715
20716 case RID_VOLATILE:
20717 cv_qualifier = TYPE_QUAL_VOLATILE;
20718 break;
20719
20720 case RID_RESTRICT:
20721 cv_qualifier = TYPE_QUAL_RESTRICT;
20722 break;
20723
20724 default:
20725 cv_qualifier = TYPE_UNQUALIFIED;
20726 break;
20727 }
20728
20729 if (!cv_qualifier)
20730 break;
20731
20732 if (cv_quals & cv_qualifier)
20733 {
20734 gcc_rich_location richloc (token->location);
20735 richloc.add_fixit_remove ();
20736 error_at (&richloc, "duplicate cv-qualifier");
20737 cp_lexer_purge_token (parser->lexer);
20738 }
20739 else
20740 {
20741 cp_lexer_consume_token (parser->lexer);
20742 cv_quals |= cv_qualifier;
20743 }
20744 }
20745
20746 return cv_quals;
20747 }
20748
20749 /* Parse an (optional) ref-qualifier
20750
20751 ref-qualifier:
20752 &
20753 &&
20754
20755 Returns cp_ref_qualifier representing ref-qualifier. */
20756
20757 static cp_ref_qualifier
20758 cp_parser_ref_qualifier_opt (cp_parser* parser)
20759 {
20760 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20761
20762 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20763 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20764 return ref_qual;
20765
20766 while (true)
20767 {
20768 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20769 cp_token *token = cp_lexer_peek_token (parser->lexer);
20770
20771 switch (token->type)
20772 {
20773 case CPP_AND:
20774 curr_ref_qual = REF_QUAL_LVALUE;
20775 break;
20776
20777 case CPP_AND_AND:
20778 curr_ref_qual = REF_QUAL_RVALUE;
20779 break;
20780
20781 default:
20782 curr_ref_qual = REF_QUAL_NONE;
20783 break;
20784 }
20785
20786 if (!curr_ref_qual)
20787 break;
20788 else if (ref_qual)
20789 {
20790 error_at (token->location, "multiple ref-qualifiers");
20791 cp_lexer_purge_token (parser->lexer);
20792 }
20793 else
20794 {
20795 ref_qual = curr_ref_qual;
20796 cp_lexer_consume_token (parser->lexer);
20797 }
20798 }
20799
20800 return ref_qual;
20801 }
20802
20803 /* Parse an optional tx-qualifier.
20804
20805 tx-qualifier:
20806 transaction_safe
20807 transaction_safe_dynamic */
20808
20809 static tree
20810 cp_parser_tx_qualifier_opt (cp_parser *parser)
20811 {
20812 cp_token *token = cp_lexer_peek_token (parser->lexer);
20813 if (token->type == CPP_NAME)
20814 {
20815 tree name = token->u.value;
20816 const char *p = IDENTIFIER_POINTER (name);
20817 const int len = strlen ("transaction_safe");
20818 if (!strncmp (p, "transaction_safe", len))
20819 {
20820 p += len;
20821 if (*p == '\0'
20822 || !strcmp (p, "_dynamic"))
20823 {
20824 cp_lexer_consume_token (parser->lexer);
20825 if (!flag_tm)
20826 {
20827 error ("%qE requires %<-fgnu-tm%>", name);
20828 return NULL_TREE;
20829 }
20830 else
20831 return name;
20832 }
20833 }
20834 }
20835 return NULL_TREE;
20836 }
20837
20838 /* Parse an (optional) virt-specifier-seq.
20839
20840 virt-specifier-seq:
20841 virt-specifier virt-specifier-seq [opt]
20842
20843 virt-specifier:
20844 override
20845 final
20846
20847 Returns a bitmask representing the virt-specifiers. */
20848
20849 static cp_virt_specifiers
20850 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20851 {
20852 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20853
20854 while (true)
20855 {
20856 cp_token *token;
20857 cp_virt_specifiers virt_specifier;
20858
20859 /* Peek at the next token. */
20860 token = cp_lexer_peek_token (parser->lexer);
20861 /* See if it's a virt-specifier-qualifier. */
20862 if (token->type != CPP_NAME)
20863 break;
20864 if (id_equal (token->u.value, "override"))
20865 {
20866 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20867 virt_specifier = VIRT_SPEC_OVERRIDE;
20868 }
20869 else if (id_equal (token->u.value, "final"))
20870 {
20871 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20872 virt_specifier = VIRT_SPEC_FINAL;
20873 }
20874 else if (id_equal (token->u.value, "__final"))
20875 {
20876 virt_specifier = VIRT_SPEC_FINAL;
20877 }
20878 else
20879 break;
20880
20881 if (virt_specifiers & virt_specifier)
20882 {
20883 gcc_rich_location richloc (token->location);
20884 richloc.add_fixit_remove ();
20885 error_at (&richloc, "duplicate virt-specifier");
20886 cp_lexer_purge_token (parser->lexer);
20887 }
20888 else
20889 {
20890 cp_lexer_consume_token (parser->lexer);
20891 virt_specifiers |= virt_specifier;
20892 }
20893 }
20894 return virt_specifiers;
20895 }
20896
20897 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20898 is in scope even though it isn't real. */
20899
20900 void
20901 inject_this_parameter (tree ctype, cp_cv_quals quals)
20902 {
20903 tree this_parm;
20904
20905 if (current_class_ptr)
20906 {
20907 /* We don't clear this between NSDMIs. Is it already what we want? */
20908 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20909 if (DECL_P (current_class_ptr)
20910 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20911 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20912 && cp_type_quals (type) == quals)
20913 return;
20914 }
20915
20916 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20917 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20918 current_class_ptr = NULL_TREE;
20919 current_class_ref
20920 = cp_build_fold_indirect_ref (this_parm);
20921 current_class_ptr = this_parm;
20922 }
20923
20924 /* Return true iff our current scope is a non-static data member
20925 initializer. */
20926
20927 bool
20928 parsing_nsdmi (void)
20929 {
20930 /* We recognize NSDMI context by the context-less 'this' pointer set up
20931 by the function above. */
20932 if (current_class_ptr
20933 && TREE_CODE (current_class_ptr) == PARM_DECL
20934 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20935 return true;
20936 return false;
20937 }
20938
20939 /* Parse a late-specified return type, if any. This is not a separate
20940 non-terminal, but part of a function declarator, which looks like
20941
20942 -> trailing-type-specifier-seq abstract-declarator(opt)
20943
20944 Returns the type indicated by the type-id.
20945
20946 In addition to this, parse any queued up #pragma omp declare simd
20947 clauses, and #pragma acc routine clauses.
20948
20949 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20950 function. */
20951
20952 static tree
20953 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20954 tree& requires_clause, cp_cv_quals quals)
20955 {
20956 cp_token *token;
20957 tree type = NULL_TREE;
20958 bool declare_simd_p = (parser->omp_declare_simd
20959 && declarator
20960 && declarator->kind == cdk_id);
20961
20962 bool oacc_routine_p = (parser->oacc_routine
20963 && declarator
20964 && declarator->kind == cdk_id);
20965
20966 /* Peek at the next token. */
20967 token = cp_lexer_peek_token (parser->lexer);
20968 /* A late-specified return type is indicated by an initial '->'. */
20969 if (token->type != CPP_DEREF
20970 && token->keyword != RID_REQUIRES
20971 && !(token->type == CPP_NAME
20972 && token->u.value == ridpointers[RID_REQUIRES])
20973 && !(declare_simd_p || oacc_routine_p))
20974 return NULL_TREE;
20975
20976 tree save_ccp = current_class_ptr;
20977 tree save_ccr = current_class_ref;
20978 if (quals >= 0)
20979 {
20980 /* DR 1207: 'this' is in scope in the trailing return type. */
20981 inject_this_parameter (current_class_type, quals);
20982 }
20983
20984 if (token->type == CPP_DEREF)
20985 {
20986 /* Consume the ->. */
20987 cp_lexer_consume_token (parser->lexer);
20988
20989 type = cp_parser_trailing_type_id (parser);
20990 }
20991
20992 /* Function declarations may be followed by a trailing
20993 requires-clause. */
20994 requires_clause = cp_parser_requires_clause_opt (parser);
20995
20996 if (declare_simd_p)
20997 declarator->attributes
20998 = cp_parser_late_parsing_omp_declare_simd (parser,
20999 declarator->attributes);
21000 if (oacc_routine_p)
21001 declarator->attributes
21002 = cp_parser_late_parsing_oacc_routine (parser,
21003 declarator->attributes);
21004
21005 if (quals >= 0)
21006 {
21007 current_class_ptr = save_ccp;
21008 current_class_ref = save_ccr;
21009 }
21010
21011 return type;
21012 }
21013
21014 /* Parse a declarator-id.
21015
21016 declarator-id:
21017 id-expression
21018 :: [opt] nested-name-specifier [opt] type-name
21019
21020 In the `id-expression' case, the value returned is as for
21021 cp_parser_id_expression if the id-expression was an unqualified-id.
21022 If the id-expression was a qualified-id, then a SCOPE_REF is
21023 returned. The first operand is the scope (either a NAMESPACE_DECL
21024 or TREE_TYPE), but the second is still just a representation of an
21025 unqualified-id. */
21026
21027 static tree
21028 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21029 {
21030 tree id;
21031 /* The expression must be an id-expression. Assume that qualified
21032 names are the names of types so that:
21033
21034 template <class T>
21035 int S<T>::R::i = 3;
21036
21037 will work; we must treat `S<T>::R' as the name of a type.
21038 Similarly, assume that qualified names are templates, where
21039 required, so that:
21040
21041 template <class T>
21042 int S<T>::R<T>::i = 3;
21043
21044 will work, too. */
21045 id = cp_parser_id_expression (parser,
21046 /*template_keyword_p=*/false,
21047 /*check_dependency_p=*/false,
21048 /*template_p=*/NULL,
21049 /*declarator_p=*/true,
21050 optional_p);
21051 if (id && BASELINK_P (id))
21052 id = BASELINK_FUNCTIONS (id);
21053 return id;
21054 }
21055
21056 /* Parse a type-id.
21057
21058 type-id:
21059 type-specifier-seq abstract-declarator [opt]
21060
21061 Returns the TYPE specified. */
21062
21063 static tree
21064 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21065 bool is_trailing_return)
21066 {
21067 cp_decl_specifier_seq type_specifier_seq;
21068 cp_declarator *abstract_declarator;
21069
21070 /* Parse the type-specifier-seq. */
21071 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21072 is_trailing_return,
21073 &type_specifier_seq);
21074 if (is_template_arg && type_specifier_seq.type
21075 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21076 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21077 /* A bare template name as a template argument is a template template
21078 argument, not a placeholder, so fail parsing it as a type argument. */
21079 {
21080 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21081 cp_parser_simulate_error (parser);
21082 return error_mark_node;
21083 }
21084 if (type_specifier_seq.type == error_mark_node)
21085 return error_mark_node;
21086
21087 /* There might or might not be an abstract declarator. */
21088 cp_parser_parse_tentatively (parser);
21089 /* Look for the declarator. */
21090 abstract_declarator
21091 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21092 /*parenthesized_p=*/NULL,
21093 /*member_p=*/false,
21094 /*friend_p=*/false);
21095 /* Check to see if there really was a declarator. */
21096 if (!cp_parser_parse_definitely (parser))
21097 abstract_declarator = NULL;
21098
21099 if (type_specifier_seq.type
21100 /* The concepts TS allows 'auto' as a type-id. */
21101 && (!flag_concepts || parser->in_type_id_in_expr_p)
21102 /* None of the valid uses of 'auto' in C++14 involve the type-id
21103 nonterminal, but it is valid in a trailing-return-type. */
21104 && !(cxx_dialect >= cxx14 && is_trailing_return))
21105 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21106 {
21107 /* A type-id with type 'auto' is only ok if the abstract declarator
21108 is a function declarator with a late-specified return type.
21109
21110 A type-id with 'auto' is also valid in a trailing-return-type
21111 in a compound-requirement. */
21112 if (abstract_declarator
21113 && abstract_declarator->kind == cdk_function
21114 && abstract_declarator->u.function.late_return_type)
21115 /* OK */;
21116 else if (parser->in_result_type_constraint_p)
21117 /* OK */;
21118 else
21119 {
21120 location_t loc = type_specifier_seq.locations[ds_type_spec];
21121 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21122 {
21123 error_at (loc, "missing template arguments after %qT",
21124 auto_node);
21125 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21126 tmpl);
21127 }
21128 else
21129 error_at (loc, "invalid use of %qT", auto_node);
21130 return error_mark_node;
21131 }
21132 }
21133
21134 return groktypename (&type_specifier_seq, abstract_declarator,
21135 is_template_arg);
21136 }
21137
21138 static tree
21139 cp_parser_type_id (cp_parser *parser)
21140 {
21141 return cp_parser_type_id_1 (parser, false, false);
21142 }
21143
21144 static tree
21145 cp_parser_template_type_arg (cp_parser *parser)
21146 {
21147 tree r;
21148 const char *saved_message = parser->type_definition_forbidden_message;
21149 parser->type_definition_forbidden_message
21150 = G_("types may not be defined in template arguments");
21151 r = cp_parser_type_id_1 (parser, true, false);
21152 parser->type_definition_forbidden_message = saved_message;
21153 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21154 {
21155 error ("invalid use of %<auto%> in template argument");
21156 r = error_mark_node;
21157 }
21158 return r;
21159 }
21160
21161 static tree
21162 cp_parser_trailing_type_id (cp_parser *parser)
21163 {
21164 return cp_parser_type_id_1 (parser, false, true);
21165 }
21166
21167 /* Parse a type-specifier-seq.
21168
21169 type-specifier-seq:
21170 type-specifier type-specifier-seq [opt]
21171
21172 GNU extension:
21173
21174 type-specifier-seq:
21175 attributes type-specifier-seq [opt]
21176
21177 If IS_DECLARATION is true, we are at the start of a "condition" or
21178 exception-declaration, so we might be followed by a declarator-id.
21179
21180 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21181 i.e. we've just seen "->".
21182
21183 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21184
21185 static void
21186 cp_parser_type_specifier_seq (cp_parser* parser,
21187 bool is_declaration,
21188 bool is_trailing_return,
21189 cp_decl_specifier_seq *type_specifier_seq)
21190 {
21191 bool seen_type_specifier = false;
21192 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21193 cp_token *start_token = NULL;
21194
21195 /* Clear the TYPE_SPECIFIER_SEQ. */
21196 clear_decl_specs (type_specifier_seq);
21197
21198 /* In the context of a trailing return type, enum E { } is an
21199 elaborated-type-specifier followed by a function-body, not an
21200 enum-specifier. */
21201 if (is_trailing_return)
21202 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21203
21204 /* Parse the type-specifiers and attributes. */
21205 while (true)
21206 {
21207 tree type_specifier;
21208 bool is_cv_qualifier;
21209
21210 /* Check for attributes first. */
21211 if (cp_next_tokens_can_be_attribute_p (parser))
21212 {
21213 type_specifier_seq->attributes
21214 = attr_chainon (type_specifier_seq->attributes,
21215 cp_parser_attributes_opt (parser));
21216 continue;
21217 }
21218
21219 /* record the token of the beginning of the type specifier seq,
21220 for error reporting purposes*/
21221 if (!start_token)
21222 start_token = cp_lexer_peek_token (parser->lexer);
21223
21224 /* Look for the type-specifier. */
21225 type_specifier = cp_parser_type_specifier (parser,
21226 flags,
21227 type_specifier_seq,
21228 /*is_declaration=*/false,
21229 NULL,
21230 &is_cv_qualifier);
21231 if (!type_specifier)
21232 {
21233 /* If the first type-specifier could not be found, this is not a
21234 type-specifier-seq at all. */
21235 if (!seen_type_specifier)
21236 {
21237 /* Set in_declarator_p to avoid skipping to the semicolon. */
21238 int in_decl = parser->in_declarator_p;
21239 parser->in_declarator_p = true;
21240
21241 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21242 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21243 cp_parser_error (parser, "expected type-specifier");
21244
21245 parser->in_declarator_p = in_decl;
21246
21247 type_specifier_seq->type = error_mark_node;
21248 return;
21249 }
21250 /* If subsequent type-specifiers could not be found, the
21251 type-specifier-seq is complete. */
21252 break;
21253 }
21254
21255 seen_type_specifier = true;
21256 /* The standard says that a condition can be:
21257
21258 type-specifier-seq declarator = assignment-expression
21259
21260 However, given:
21261
21262 struct S {};
21263 if (int S = ...)
21264
21265 we should treat the "S" as a declarator, not as a
21266 type-specifier. The standard doesn't say that explicitly for
21267 type-specifier-seq, but it does say that for
21268 decl-specifier-seq in an ordinary declaration. Perhaps it
21269 would be clearer just to allow a decl-specifier-seq here, and
21270 then add a semantic restriction that if any decl-specifiers
21271 that are not type-specifiers appear, the program is invalid. */
21272 if (is_declaration && !is_cv_qualifier)
21273 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21274 }
21275 }
21276
21277 /* Return whether the function currently being declared has an associated
21278 template parameter list. */
21279
21280 static bool
21281 function_being_declared_is_template_p (cp_parser* parser)
21282 {
21283 if (!current_template_parms || processing_template_parmlist)
21284 return false;
21285
21286 if (parser->implicit_template_scope)
21287 return true;
21288
21289 if (at_class_scope_p ()
21290 && TYPE_BEING_DEFINED (current_class_type))
21291 return parser->num_template_parameter_lists != 0;
21292
21293 return ((int) parser->num_template_parameter_lists > template_class_depth
21294 (current_class_type));
21295 }
21296
21297 /* Parse a parameter-declaration-clause.
21298
21299 parameter-declaration-clause:
21300 parameter-declaration-list [opt] ... [opt]
21301 parameter-declaration-list , ...
21302
21303 Returns a representation for the parameter declarations. A return
21304 value of NULL indicates a parameter-declaration-clause consisting
21305 only of an ellipsis. */
21306
21307 static tree
21308 cp_parser_parameter_declaration_clause (cp_parser* parser)
21309 {
21310 tree parameters;
21311 cp_token *token;
21312 bool ellipsis_p;
21313
21314 temp_override<bool> cleanup
21315 (parser->auto_is_implicit_function_template_parm_p);
21316
21317 if (!processing_specialization
21318 && !processing_template_parmlist
21319 && !processing_explicit_instantiation
21320 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21321 actual function or a random abstract declarator. */
21322 && parser->default_arg_ok_p)
21323 if (!current_function_decl
21324 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21325 parser->auto_is_implicit_function_template_parm_p = true;
21326
21327 /* Peek at the next token. */
21328 token = cp_lexer_peek_token (parser->lexer);
21329 /* Check for trivial parameter-declaration-clauses. */
21330 if (token->type == CPP_ELLIPSIS)
21331 {
21332 /* Consume the `...' token. */
21333 cp_lexer_consume_token (parser->lexer);
21334 return NULL_TREE;
21335 }
21336 else if (token->type == CPP_CLOSE_PAREN)
21337 /* There are no parameters. */
21338 {
21339 #ifndef NO_IMPLICIT_EXTERN_C
21340 if (in_system_header_at (input_location)
21341 && current_class_type == NULL
21342 && current_lang_name == lang_name_c)
21343 return NULL_TREE;
21344 else
21345 #endif
21346 return void_list_node;
21347 }
21348 /* Check for `(void)', too, which is a special case. */
21349 else if (token->keyword == RID_VOID
21350 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21351 == CPP_CLOSE_PAREN))
21352 {
21353 /* Consume the `void' token. */
21354 cp_lexer_consume_token (parser->lexer);
21355 /* There are no parameters. */
21356 return void_list_node;
21357 }
21358
21359 /* Parse the parameter-declaration-list. */
21360 parameters = cp_parser_parameter_declaration_list (parser);
21361 /* If a parse error occurred while parsing the
21362 parameter-declaration-list, then the entire
21363 parameter-declaration-clause is erroneous. */
21364 if (parameters == error_mark_node)
21365 return NULL_TREE;
21366
21367 /* Peek at the next token. */
21368 token = cp_lexer_peek_token (parser->lexer);
21369 /* If it's a `,', the clause should terminate with an ellipsis. */
21370 if (token->type == CPP_COMMA)
21371 {
21372 /* Consume the `,'. */
21373 cp_lexer_consume_token (parser->lexer);
21374 /* Expect an ellipsis. */
21375 ellipsis_p
21376 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21377 }
21378 /* It might also be `...' if the optional trailing `,' was
21379 omitted. */
21380 else if (token->type == CPP_ELLIPSIS)
21381 {
21382 /* Consume the `...' token. */
21383 cp_lexer_consume_token (parser->lexer);
21384 /* And remember that we saw it. */
21385 ellipsis_p = true;
21386 }
21387 else
21388 ellipsis_p = false;
21389
21390 /* Finish the parameter list. */
21391 if (!ellipsis_p)
21392 parameters = chainon (parameters, void_list_node);
21393
21394 return parameters;
21395 }
21396
21397 /* Parse a parameter-declaration-list.
21398
21399 parameter-declaration-list:
21400 parameter-declaration
21401 parameter-declaration-list , parameter-declaration
21402
21403 Returns a representation of the parameter-declaration-list, as for
21404 cp_parser_parameter_declaration_clause. However, the
21405 `void_list_node' is never appended to the list. */
21406
21407 static tree
21408 cp_parser_parameter_declaration_list (cp_parser* parser)
21409 {
21410 tree parameters = NULL_TREE;
21411 tree *tail = &parameters;
21412 bool saved_in_unbraced_linkage_specification_p;
21413 int index = 0;
21414
21415 /* The special considerations that apply to a function within an
21416 unbraced linkage specifications do not apply to the parameters
21417 to the function. */
21418 saved_in_unbraced_linkage_specification_p
21419 = parser->in_unbraced_linkage_specification_p;
21420 parser->in_unbraced_linkage_specification_p = false;
21421
21422 /* Look for more parameters. */
21423 while (true)
21424 {
21425 cp_parameter_declarator *parameter;
21426 tree decl = error_mark_node;
21427 bool parenthesized_p = false;
21428
21429 /* Parse the parameter. */
21430 parameter
21431 = cp_parser_parameter_declaration (parser,
21432 /*template_parm_p=*/false,
21433 &parenthesized_p);
21434
21435 /* We don't know yet if the enclosing context is deprecated, so wait
21436 and warn in grokparms if appropriate. */
21437 deprecated_state = DEPRECATED_SUPPRESS;
21438
21439 if (parameter)
21440 {
21441 decl = grokdeclarator (parameter->declarator,
21442 &parameter->decl_specifiers,
21443 PARM,
21444 parameter->default_argument != NULL_TREE,
21445 &parameter->decl_specifiers.attributes);
21446 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21447 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21448 }
21449
21450 deprecated_state = DEPRECATED_NORMAL;
21451
21452 /* If a parse error occurred parsing the parameter declaration,
21453 then the entire parameter-declaration-list is erroneous. */
21454 if (decl == error_mark_node)
21455 {
21456 parameters = error_mark_node;
21457 break;
21458 }
21459
21460 if (parameter->decl_specifiers.attributes)
21461 cplus_decl_attributes (&decl,
21462 parameter->decl_specifiers.attributes,
21463 0);
21464 if (DECL_NAME (decl))
21465 decl = pushdecl (decl);
21466
21467 if (decl != error_mark_node)
21468 {
21469 retrofit_lang_decl (decl);
21470 DECL_PARM_INDEX (decl) = ++index;
21471 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21472 }
21473
21474 /* Add the new parameter to the list. */
21475 *tail = build_tree_list (parameter->default_argument, decl);
21476 tail = &TREE_CHAIN (*tail);
21477
21478 /* Peek at the next token. */
21479 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21480 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21481 /* These are for Objective-C++ */
21482 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21483 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21484 /* The parameter-declaration-list is complete. */
21485 break;
21486 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21487 {
21488 cp_token *token;
21489
21490 /* Peek at the next token. */
21491 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21492 /* If it's an ellipsis, then the list is complete. */
21493 if (token->type == CPP_ELLIPSIS)
21494 break;
21495 /* Otherwise, there must be more parameters. Consume the
21496 `,'. */
21497 cp_lexer_consume_token (parser->lexer);
21498 /* When parsing something like:
21499
21500 int i(float f, double d)
21501
21502 we can tell after seeing the declaration for "f" that we
21503 are not looking at an initialization of a variable "i",
21504 but rather at the declaration of a function "i".
21505
21506 Due to the fact that the parsing of template arguments
21507 (as specified to a template-id) requires backtracking we
21508 cannot use this technique when inside a template argument
21509 list. */
21510 if (!parser->in_template_argument_list_p
21511 && !parser->in_type_id_in_expr_p
21512 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21513 /* However, a parameter-declaration of the form
21514 "float(f)" (which is a valid declaration of a
21515 parameter "f") can also be interpreted as an
21516 expression (the conversion of "f" to "float"). */
21517 && !parenthesized_p)
21518 cp_parser_commit_to_tentative_parse (parser);
21519 }
21520 else
21521 {
21522 cp_parser_error (parser, "expected %<,%> or %<...%>");
21523 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21524 cp_parser_skip_to_closing_parenthesis (parser,
21525 /*recovering=*/true,
21526 /*or_comma=*/false,
21527 /*consume_paren=*/false);
21528 break;
21529 }
21530 }
21531
21532 parser->in_unbraced_linkage_specification_p
21533 = saved_in_unbraced_linkage_specification_p;
21534
21535 /* Reset implicit_template_scope if we are about to leave the function
21536 parameter list that introduced it. Note that for out-of-line member
21537 definitions, there will be one or more class scopes before we get to
21538 the template parameter scope. */
21539
21540 if (cp_binding_level *its = parser->implicit_template_scope)
21541 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21542 {
21543 while (maybe_its->kind == sk_class)
21544 maybe_its = maybe_its->level_chain;
21545 if (maybe_its == its)
21546 {
21547 parser->implicit_template_parms = 0;
21548 parser->implicit_template_scope = 0;
21549 }
21550 }
21551
21552 return parameters;
21553 }
21554
21555 /* Parse a parameter declaration.
21556
21557 parameter-declaration:
21558 decl-specifier-seq ... [opt] declarator
21559 decl-specifier-seq declarator = assignment-expression
21560 decl-specifier-seq ... [opt] abstract-declarator [opt]
21561 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21562
21563 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21564 declares a template parameter. (In that case, a non-nested `>'
21565 token encountered during the parsing of the assignment-expression
21566 is not interpreted as a greater-than operator.)
21567
21568 Returns a representation of the parameter, or NULL if an error
21569 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21570 true iff the declarator is of the form "(p)". */
21571
21572 static cp_parameter_declarator *
21573 cp_parser_parameter_declaration (cp_parser *parser,
21574 bool template_parm_p,
21575 bool *parenthesized_p)
21576 {
21577 int declares_class_or_enum;
21578 cp_decl_specifier_seq decl_specifiers;
21579 cp_declarator *declarator;
21580 tree default_argument;
21581 cp_token *token = NULL, *declarator_token_start = NULL;
21582 const char *saved_message;
21583 bool template_parameter_pack_p = false;
21584
21585 /* In a template parameter, `>' is not an operator.
21586
21587 [temp.param]
21588
21589 When parsing a default template-argument for a non-type
21590 template-parameter, the first non-nested `>' is taken as the end
21591 of the template parameter-list rather than a greater-than
21592 operator. */
21593
21594 /* Type definitions may not appear in parameter types. */
21595 saved_message = parser->type_definition_forbidden_message;
21596 parser->type_definition_forbidden_message
21597 = G_("types may not be defined in parameter types");
21598
21599 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21600 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21601 (current_template_parms)) : 0);
21602
21603 /* Parse the declaration-specifiers. */
21604 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21605 cp_parser_decl_specifier_seq (parser,
21606 CP_PARSER_FLAGS_NONE,
21607 &decl_specifiers,
21608 &declares_class_or_enum);
21609
21610 /* Complain about missing 'typename' or other invalid type names. */
21611 if (!decl_specifiers.any_type_specifiers_p
21612 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21613 decl_specifiers.type = error_mark_node;
21614
21615 /* If an error occurred, there's no reason to attempt to parse the
21616 rest of the declaration. */
21617 if (cp_parser_error_occurred (parser))
21618 {
21619 parser->type_definition_forbidden_message = saved_message;
21620 return NULL;
21621 }
21622
21623 /* Peek at the next token. */
21624 token = cp_lexer_peek_token (parser->lexer);
21625
21626 /* If the next token is a `)', `,', `=', `>', or `...', then there
21627 is no declarator. However, when variadic templates are enabled,
21628 there may be a declarator following `...'. */
21629 if (token->type == CPP_CLOSE_PAREN
21630 || token->type == CPP_COMMA
21631 || token->type == CPP_EQ
21632 || token->type == CPP_GREATER)
21633 {
21634 declarator = NULL;
21635 if (parenthesized_p)
21636 *parenthesized_p = false;
21637 }
21638 /* Otherwise, there should be a declarator. */
21639 else
21640 {
21641 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21642 parser->default_arg_ok_p = false;
21643
21644 /* After seeing a decl-specifier-seq, if the next token is not a
21645 "(", there is no possibility that the code is a valid
21646 expression. Therefore, if parsing tentatively, we commit at
21647 this point. */
21648 if (!parser->in_template_argument_list_p
21649 /* In an expression context, having seen:
21650
21651 (int((char ...
21652
21653 we cannot be sure whether we are looking at a
21654 function-type (taking a "char" as a parameter) or a cast
21655 of some object of type "char" to "int". */
21656 && !parser->in_type_id_in_expr_p
21657 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21658 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21659 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21660 cp_parser_commit_to_tentative_parse (parser);
21661 /* Parse the declarator. */
21662 declarator_token_start = token;
21663 declarator = cp_parser_declarator (parser,
21664 CP_PARSER_DECLARATOR_EITHER,
21665 /*ctor_dtor_or_conv_p=*/NULL,
21666 parenthesized_p,
21667 /*member_p=*/false,
21668 /*friend_p=*/false);
21669 parser->default_arg_ok_p = saved_default_arg_ok_p;
21670 /* After the declarator, allow more attributes. */
21671 decl_specifiers.attributes
21672 = attr_chainon (decl_specifiers.attributes,
21673 cp_parser_attributes_opt (parser));
21674
21675 /* If the declarator is a template parameter pack, remember that and
21676 clear the flag in the declarator itself so we don't get errors
21677 from grokdeclarator. */
21678 if (template_parm_p && declarator && declarator->parameter_pack_p)
21679 {
21680 declarator->parameter_pack_p = false;
21681 template_parameter_pack_p = true;
21682 }
21683 }
21684
21685 /* If the next token is an ellipsis, and we have not seen a declarator
21686 name, and if either the type of the declarator contains parameter
21687 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21688 for, eg, abbreviated integral type names), then we actually have a
21689 parameter pack expansion expression. Otherwise, leave the ellipsis
21690 for a C-style variadic function. */
21691 token = cp_lexer_peek_token (parser->lexer);
21692
21693 /* If a function parameter pack was specified and an implicit template
21694 parameter was introduced during cp_parser_parameter_declaration,
21695 change any implicit parameters introduced into packs. */
21696 if (parser->implicit_template_parms
21697 && (token->type == CPP_ELLIPSIS
21698 || (declarator && declarator->parameter_pack_p)))
21699 {
21700 int latest_template_parm_idx = TREE_VEC_LENGTH
21701 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21702
21703 if (latest_template_parm_idx != template_parm_idx)
21704 decl_specifiers.type = convert_generic_types_to_packs
21705 (decl_specifiers.type,
21706 template_parm_idx, latest_template_parm_idx);
21707 }
21708
21709 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21710 {
21711 tree type = decl_specifiers.type;
21712
21713 if (type && DECL_P (type))
21714 type = TREE_TYPE (type);
21715
21716 if (((type
21717 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21718 && (template_parm_p || uses_parameter_packs (type)))
21719 || (!type && template_parm_p))
21720 && declarator_can_be_parameter_pack (declarator))
21721 {
21722 /* Consume the `...'. */
21723 cp_lexer_consume_token (parser->lexer);
21724 maybe_warn_variadic_templates ();
21725
21726 /* Build a pack expansion type */
21727 if (template_parm_p)
21728 template_parameter_pack_p = true;
21729 else if (declarator)
21730 declarator->parameter_pack_p = true;
21731 else
21732 decl_specifiers.type = make_pack_expansion (type);
21733 }
21734 }
21735
21736 /* The restriction on defining new types applies only to the type
21737 of the parameter, not to the default argument. */
21738 parser->type_definition_forbidden_message = saved_message;
21739
21740 /* If the next token is `=', then process a default argument. */
21741 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21742 {
21743 tree type = decl_specifiers.type;
21744 token = cp_lexer_peek_token (parser->lexer);
21745 /* If we are defining a class, then the tokens that make up the
21746 default argument must be saved and processed later. */
21747 if (!template_parm_p && at_class_scope_p ()
21748 && TYPE_BEING_DEFINED (current_class_type)
21749 && !LAMBDA_TYPE_P (current_class_type))
21750 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21751
21752 // A constrained-type-specifier may declare a type template-parameter.
21753 else if (declares_constrained_type_template_parameter (type))
21754 default_argument
21755 = cp_parser_default_type_template_argument (parser);
21756
21757 // A constrained-type-specifier may declare a template-template-parameter.
21758 else if (declares_constrained_template_template_parameter (type))
21759 default_argument
21760 = cp_parser_default_template_template_argument (parser);
21761
21762 /* Outside of a class definition, we can just parse the
21763 assignment-expression. */
21764 else
21765 default_argument
21766 = cp_parser_default_argument (parser, template_parm_p);
21767
21768 if (!parser->default_arg_ok_p)
21769 {
21770 permerror (token->location,
21771 "default arguments are only "
21772 "permitted for function parameters");
21773 }
21774 else if ((declarator && declarator->parameter_pack_p)
21775 || template_parameter_pack_p
21776 || (decl_specifiers.type
21777 && PACK_EXPANSION_P (decl_specifiers.type)))
21778 {
21779 /* Find the name of the parameter pack. */
21780 cp_declarator *id_declarator = declarator;
21781 while (id_declarator && id_declarator->kind != cdk_id)
21782 id_declarator = id_declarator->declarator;
21783
21784 if (id_declarator && id_declarator->kind == cdk_id)
21785 error_at (declarator_token_start->location,
21786 template_parm_p
21787 ? G_("template parameter pack %qD "
21788 "cannot have a default argument")
21789 : G_("parameter pack %qD cannot have "
21790 "a default argument"),
21791 id_declarator->u.id.unqualified_name);
21792 else
21793 error_at (declarator_token_start->location,
21794 template_parm_p
21795 ? G_("template parameter pack cannot have "
21796 "a default argument")
21797 : G_("parameter pack cannot have a "
21798 "default argument"));
21799
21800 default_argument = NULL_TREE;
21801 }
21802 }
21803 else
21804 default_argument = NULL_TREE;
21805
21806 /* Generate a location for the parameter, ranging from the start of the
21807 initial token to the end of the final token (using input_location for
21808 the latter, set up by cp_lexer_set_source_position_from_token when
21809 consuming tokens).
21810
21811 If we have a identifier, then use it for the caret location, e.g.
21812
21813 extern int callee (int one, int (*two)(int, int), float three);
21814 ~~~~~~^~~~~~~~~~~~~~
21815
21816 otherwise, reuse the start location for the caret location e.g.:
21817
21818 extern int callee (int one, int (*)(int, int), float three);
21819 ^~~~~~~~~~~~~~~~~
21820
21821 */
21822 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21823 ? declarator->id_loc
21824 : decl_spec_token_start->location);
21825 location_t param_loc = make_location (caret_loc,
21826 decl_spec_token_start->location,
21827 input_location);
21828
21829 return make_parameter_declarator (&decl_specifiers,
21830 declarator,
21831 default_argument,
21832 param_loc,
21833 template_parameter_pack_p);
21834 }
21835
21836 /* Parse a default argument and return it.
21837
21838 TEMPLATE_PARM_P is true if this is a default argument for a
21839 non-type template parameter. */
21840 static tree
21841 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21842 {
21843 tree default_argument = NULL_TREE;
21844 bool saved_greater_than_is_operator_p;
21845 bool saved_local_variables_forbidden_p;
21846 bool non_constant_p, is_direct_init;
21847
21848 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21849 set correctly. */
21850 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21851 parser->greater_than_is_operator_p = !template_parm_p;
21852 /* Local variable names (and the `this' keyword) may not
21853 appear in a default argument. */
21854 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21855 parser->local_variables_forbidden_p = true;
21856 /* Parse the assignment-expression. */
21857 if (template_parm_p)
21858 push_deferring_access_checks (dk_no_deferred);
21859 tree saved_class_ptr = NULL_TREE;
21860 tree saved_class_ref = NULL_TREE;
21861 /* The "this" pointer is not valid in a default argument. */
21862 if (cfun)
21863 {
21864 saved_class_ptr = current_class_ptr;
21865 cp_function_chain->x_current_class_ptr = NULL_TREE;
21866 saved_class_ref = current_class_ref;
21867 cp_function_chain->x_current_class_ref = NULL_TREE;
21868 }
21869 default_argument
21870 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21871 /* Restore the "this" pointer. */
21872 if (cfun)
21873 {
21874 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21875 cp_function_chain->x_current_class_ref = saved_class_ref;
21876 }
21877 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21879 if (template_parm_p)
21880 pop_deferring_access_checks ();
21881 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21882 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21883
21884 return default_argument;
21885 }
21886
21887 /* Parse a function-body.
21888
21889 function-body:
21890 compound_statement */
21891
21892 static void
21893 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21894 {
21895 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21896 ? BCS_TRY_BLOCK : BCS_NORMAL),
21897 true);
21898 }
21899
21900 /* Parse a ctor-initializer-opt followed by a function-body. Return
21901 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21902 is true we are parsing a function-try-block. */
21903
21904 static void
21905 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21906 bool in_function_try_block)
21907 {
21908 tree body, list;
21909 const bool check_body_p =
21910 DECL_CONSTRUCTOR_P (current_function_decl)
21911 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21912 tree last = NULL;
21913
21914 /* Begin the function body. */
21915 body = begin_function_body ();
21916 /* Parse the optional ctor-initializer. */
21917 cp_parser_ctor_initializer_opt (parser);
21918
21919 /* If we're parsing a constexpr constructor definition, we need
21920 to check that the constructor body is indeed empty. However,
21921 before we get to cp_parser_function_body lot of junk has been
21922 generated, so we can't just check that we have an empty block.
21923 Rather we take a snapshot of the outermost block, and check whether
21924 cp_parser_function_body changed its state. */
21925 if (check_body_p)
21926 {
21927 list = cur_stmt_list;
21928 if (STATEMENT_LIST_TAIL (list))
21929 last = STATEMENT_LIST_TAIL (list)->stmt;
21930 }
21931 /* Parse the function-body. */
21932 cp_parser_function_body (parser, in_function_try_block);
21933 if (check_body_p)
21934 check_constexpr_ctor_body (last, list, /*complain=*/true);
21935 /* Finish the function body. */
21936 finish_function_body (body);
21937 }
21938
21939 /* Parse an initializer.
21940
21941 initializer:
21942 = initializer-clause
21943 ( expression-list )
21944
21945 Returns an expression representing the initializer. If no
21946 initializer is present, NULL_TREE is returned.
21947
21948 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21949 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21950 set to TRUE if there is no initializer present. If there is an
21951 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21952 is set to true; otherwise it is set to false. */
21953
21954 static tree
21955 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21956 bool* non_constant_p, bool subexpression_p)
21957 {
21958 cp_token *token;
21959 tree init;
21960
21961 /* Peek at the next token. */
21962 token = cp_lexer_peek_token (parser->lexer);
21963
21964 /* Let our caller know whether or not this initializer was
21965 parenthesized. */
21966 *is_direct_init = (token->type != CPP_EQ);
21967 /* Assume that the initializer is constant. */
21968 *non_constant_p = false;
21969
21970 if (token->type == CPP_EQ)
21971 {
21972 /* Consume the `='. */
21973 cp_lexer_consume_token (parser->lexer);
21974 /* Parse the initializer-clause. */
21975 init = cp_parser_initializer_clause (parser, non_constant_p);
21976 }
21977 else if (token->type == CPP_OPEN_PAREN)
21978 {
21979 vec<tree, va_gc> *vec;
21980 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21981 /*cast_p=*/false,
21982 /*allow_expansion_p=*/true,
21983 non_constant_p);
21984 if (vec == NULL)
21985 return error_mark_node;
21986 init = build_tree_list_vec (vec);
21987 release_tree_vector (vec);
21988 }
21989 else if (token->type == CPP_OPEN_BRACE)
21990 {
21991 cp_lexer_set_source_position (parser->lexer);
21992 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21993 init = cp_parser_braced_list (parser, non_constant_p);
21994 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21995 }
21996 else
21997 {
21998 /* Anything else is an error. */
21999 cp_parser_error (parser, "expected initializer");
22000 init = error_mark_node;
22001 }
22002
22003 if (!subexpression_p && check_for_bare_parameter_packs (init))
22004 init = error_mark_node;
22005
22006 return init;
22007 }
22008
22009 /* Parse an initializer-clause.
22010
22011 initializer-clause:
22012 assignment-expression
22013 braced-init-list
22014
22015 Returns an expression representing the initializer.
22016
22017 If the `assignment-expression' production is used the value
22018 returned is simply a representation for the expression.
22019
22020 Otherwise, calls cp_parser_braced_list. */
22021
22022 static cp_expr
22023 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22024 {
22025 cp_expr initializer;
22026
22027 /* Assume the expression is constant. */
22028 *non_constant_p = false;
22029
22030 /* If it is not a `{', then we are looking at an
22031 assignment-expression. */
22032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22033 {
22034 initializer
22035 = cp_parser_constant_expression (parser,
22036 /*allow_non_constant_p=*/true,
22037 non_constant_p);
22038 }
22039 else
22040 initializer = cp_parser_braced_list (parser, non_constant_p);
22041
22042 return initializer;
22043 }
22044
22045 /* Parse a brace-enclosed initializer list.
22046
22047 braced-init-list:
22048 { initializer-list , [opt] }
22049 { designated-initializer-list , [opt] }
22050 { }
22051
22052 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22053 the elements of the initializer-list (or NULL, if the last
22054 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22055 NULL_TREE. There is no way to detect whether or not the optional
22056 trailing `,' was provided. NON_CONSTANT_P is as for
22057 cp_parser_initializer. */
22058
22059 static cp_expr
22060 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22061 {
22062 tree initializer;
22063 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22064
22065 /* Consume the `{' token. */
22066 matching_braces braces;
22067 braces.require_open (parser);
22068 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22069 initializer = make_node (CONSTRUCTOR);
22070 /* If it's not a `}', then there is a non-trivial initializer. */
22071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22072 {
22073 /* Parse the initializer list. */
22074 CONSTRUCTOR_ELTS (initializer)
22075 = cp_parser_initializer_list (parser, non_constant_p);
22076 /* A trailing `,' token is allowed. */
22077 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22078 cp_lexer_consume_token (parser->lexer);
22079 }
22080 else
22081 *non_constant_p = false;
22082 /* Now, there should be a trailing `}'. */
22083 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22084 braces.require_close (parser);
22085 TREE_TYPE (initializer) = init_list_type_node;
22086
22087 cp_expr result (initializer);
22088 /* Build a location of the form:
22089 { ... }
22090 ^~~~~~~
22091 with caret==start at the open brace, finish at the close brace. */
22092 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22093 result.set_location (combined_loc);
22094 return result;
22095 }
22096
22097 /* Consume tokens up to, and including, the next non-nested closing `]'.
22098 Returns true iff we found a closing `]'. */
22099
22100 static bool
22101 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22102 {
22103 unsigned square_depth = 0;
22104
22105 while (true)
22106 {
22107 cp_token * token = cp_lexer_peek_token (parser->lexer);
22108
22109 switch (token->type)
22110 {
22111 case CPP_EOF:
22112 case CPP_PRAGMA_EOL:
22113 /* If we've run out of tokens, then there is no closing `]'. */
22114 return false;
22115
22116 case CPP_OPEN_SQUARE:
22117 ++square_depth;
22118 break;
22119
22120 case CPP_CLOSE_SQUARE:
22121 if (!square_depth--)
22122 {
22123 cp_lexer_consume_token (parser->lexer);
22124 return true;
22125 }
22126 break;
22127
22128 default:
22129 break;
22130 }
22131
22132 /* Consume the token. */
22133 cp_lexer_consume_token (parser->lexer);
22134 }
22135 }
22136
22137 /* Return true if we are looking at an array-designator, false otherwise. */
22138
22139 static bool
22140 cp_parser_array_designator_p (cp_parser *parser)
22141 {
22142 /* Consume the `['. */
22143 cp_lexer_consume_token (parser->lexer);
22144
22145 cp_lexer_save_tokens (parser->lexer);
22146
22147 /* Skip tokens until the next token is a closing square bracket.
22148 If we find the closing `]', and the next token is a `=', then
22149 we are looking at an array designator. */
22150 bool array_designator_p
22151 = (cp_parser_skip_to_closing_square_bracket (parser)
22152 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22153
22154 /* Roll back the tokens we skipped. */
22155 cp_lexer_rollback_tokens (parser->lexer);
22156
22157 return array_designator_p;
22158 }
22159
22160 /* Parse an initializer-list.
22161
22162 initializer-list:
22163 initializer-clause ... [opt]
22164 initializer-list , initializer-clause ... [opt]
22165
22166 C++2A Extension:
22167
22168 designated-initializer-list:
22169 designated-initializer-clause
22170 designated-initializer-list , designated-initializer-clause
22171
22172 designated-initializer-clause:
22173 designator brace-or-equal-initializer
22174
22175 designator:
22176 . identifier
22177
22178 GNU Extension:
22179
22180 initializer-list:
22181 designation initializer-clause ...[opt]
22182 initializer-list , designation initializer-clause ...[opt]
22183
22184 designation:
22185 . identifier =
22186 identifier :
22187 [ constant-expression ] =
22188
22189 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22190 for the initializer. If the INDEX of the elt is non-NULL, it is the
22191 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22192 as for cp_parser_initializer. */
22193
22194 static vec<constructor_elt, va_gc> *
22195 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22196 {
22197 vec<constructor_elt, va_gc> *v = NULL;
22198 bool first_p = true;
22199 tree first_designator = NULL_TREE;
22200
22201 /* Assume all of the expressions are constant. */
22202 *non_constant_p = false;
22203
22204 /* Parse the rest of the list. */
22205 while (true)
22206 {
22207 cp_token *token;
22208 tree designator;
22209 tree initializer;
22210 bool clause_non_constant_p;
22211 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22212
22213 /* Handle the C++2A syntax, '. id ='. */
22214 if ((cxx_dialect >= cxx2a
22215 || cp_parser_allow_gnu_extensions_p (parser))
22216 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22217 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22218 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22219 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22220 == CPP_OPEN_BRACE)))
22221 {
22222 if (cxx_dialect < cxx2a)
22223 pedwarn (loc, OPT_Wpedantic,
22224 "C++ designated initializers only available with "
22225 "-std=c++2a or -std=gnu++2a");
22226 /* Consume the `.'. */
22227 cp_lexer_consume_token (parser->lexer);
22228 /* Consume the identifier. */
22229 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22230 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22231 /* Consume the `='. */
22232 cp_lexer_consume_token (parser->lexer);
22233 }
22234 /* Also, if the next token is an identifier and the following one is a
22235 colon, we are looking at the GNU designated-initializer
22236 syntax. */
22237 else if (cp_parser_allow_gnu_extensions_p (parser)
22238 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22239 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22240 == CPP_COLON))
22241 {
22242 /* Warn the user that they are using an extension. */
22243 pedwarn (loc, OPT_Wpedantic,
22244 "ISO C++ does not allow GNU designated initializers");
22245 /* Consume the identifier. */
22246 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22247 /* Consume the `:'. */
22248 cp_lexer_consume_token (parser->lexer);
22249 }
22250 /* Also handle C99 array designators, '[ const ] ='. */
22251 else if (cp_parser_allow_gnu_extensions_p (parser)
22252 && !c_dialect_objc ()
22253 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22254 {
22255 /* In C++11, [ could start a lambda-introducer. */
22256 bool non_const = false;
22257
22258 cp_parser_parse_tentatively (parser);
22259
22260 if (!cp_parser_array_designator_p (parser))
22261 {
22262 cp_parser_simulate_error (parser);
22263 designator = NULL_TREE;
22264 }
22265 else
22266 {
22267 designator = cp_parser_constant_expression (parser, true,
22268 &non_const);
22269 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22270 cp_parser_require (parser, CPP_EQ, RT_EQ);
22271 }
22272
22273 if (!cp_parser_parse_definitely (parser))
22274 designator = NULL_TREE;
22275 else if (non_const
22276 && (!require_potential_rvalue_constant_expression
22277 (designator)))
22278 designator = NULL_TREE;
22279 if (designator)
22280 /* Warn the user that they are using an extension. */
22281 pedwarn (loc, OPT_Wpedantic,
22282 "ISO C++ does not allow C99 designated initializers");
22283 }
22284 else
22285 designator = NULL_TREE;
22286
22287 if (first_p)
22288 {
22289 first_designator = designator;
22290 first_p = false;
22291 }
22292 else if (cxx_dialect >= cxx2a
22293 && first_designator != error_mark_node
22294 && (!first_designator != !designator))
22295 {
22296 error_at (loc, "either all initializer clauses should be designated "
22297 "or none of them should be");
22298 first_designator = error_mark_node;
22299 }
22300 else if (cxx_dialect < cxx2a && !first_designator)
22301 first_designator = designator;
22302
22303 /* Parse the initializer. */
22304 initializer = cp_parser_initializer_clause (parser,
22305 &clause_non_constant_p);
22306 /* If any clause is non-constant, so is the entire initializer. */
22307 if (clause_non_constant_p)
22308 *non_constant_p = true;
22309
22310 /* If we have an ellipsis, this is an initializer pack
22311 expansion. */
22312 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22313 {
22314 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22315
22316 /* Consume the `...'. */
22317 cp_lexer_consume_token (parser->lexer);
22318
22319 if (designator && cxx_dialect >= cxx2a)
22320 error_at (loc,
22321 "%<...%> not allowed in designated initializer list");
22322
22323 /* Turn the initializer into an initializer expansion. */
22324 initializer = make_pack_expansion (initializer);
22325 }
22326
22327 /* Add it to the vector. */
22328 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22329
22330 /* If the next token is not a comma, we have reached the end of
22331 the list. */
22332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22333 break;
22334
22335 /* Peek at the next token. */
22336 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22337 /* If the next token is a `}', then we're still done. An
22338 initializer-clause can have a trailing `,' after the
22339 initializer-list and before the closing `}'. */
22340 if (token->type == CPP_CLOSE_BRACE)
22341 break;
22342
22343 /* Consume the `,' token. */
22344 cp_lexer_consume_token (parser->lexer);
22345 }
22346
22347 /* The same identifier shall not appear in multiple designators
22348 of a designated-initializer-list. */
22349 if (first_designator)
22350 {
22351 unsigned int i;
22352 tree designator, val;
22353 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22354 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22355 {
22356 if (IDENTIFIER_MARKED (designator))
22357 {
22358 error_at (EXPR_LOC_OR_LOC (val, input_location),
22359 "%<.%s%> designator used multiple times in "
22360 "the same initializer list",
22361 IDENTIFIER_POINTER (designator));
22362 (*v)[i].index = NULL_TREE;
22363 }
22364 else
22365 IDENTIFIER_MARKED (designator) = 1;
22366 }
22367 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22368 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22369 IDENTIFIER_MARKED (designator) = 0;
22370 }
22371
22372 return v;
22373 }
22374
22375 /* Classes [gram.class] */
22376
22377 /* Parse a class-name.
22378
22379 class-name:
22380 identifier
22381 template-id
22382
22383 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22384 to indicate that names looked up in dependent types should be
22385 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22386 keyword has been used to indicate that the name that appears next
22387 is a template. TAG_TYPE indicates the explicit tag given before
22388 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22389 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22390 is the class being defined in a class-head. If ENUM_OK is TRUE,
22391 enum-names are also accepted.
22392
22393 Returns the TYPE_DECL representing the class. */
22394
22395 static tree
22396 cp_parser_class_name (cp_parser *parser,
22397 bool typename_keyword_p,
22398 bool template_keyword_p,
22399 enum tag_types tag_type,
22400 bool check_dependency_p,
22401 bool class_head_p,
22402 bool is_declaration,
22403 bool enum_ok)
22404 {
22405 tree decl;
22406 tree scope;
22407 bool typename_p;
22408 cp_token *token;
22409 tree identifier = NULL_TREE;
22410
22411 /* All class-names start with an identifier. */
22412 token = cp_lexer_peek_token (parser->lexer);
22413 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22414 {
22415 cp_parser_error (parser, "expected class-name");
22416 return error_mark_node;
22417 }
22418
22419 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22420 to a template-id, so we save it here. */
22421 scope = parser->scope;
22422 if (scope == error_mark_node)
22423 return error_mark_node;
22424
22425 /* Any name names a type if we're following the `typename' keyword
22426 in a qualified name where the enclosing scope is type-dependent. */
22427 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22428 && dependent_type_p (scope));
22429 /* Handle the common case (an identifier, but not a template-id)
22430 efficiently. */
22431 if (token->type == CPP_NAME
22432 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22433 {
22434 cp_token *identifier_token;
22435 bool ambiguous_p;
22436
22437 /* Look for the identifier. */
22438 identifier_token = cp_lexer_peek_token (parser->lexer);
22439 ambiguous_p = identifier_token->error_reported;
22440 identifier = cp_parser_identifier (parser);
22441 /* If the next token isn't an identifier, we are certainly not
22442 looking at a class-name. */
22443 if (identifier == error_mark_node)
22444 decl = error_mark_node;
22445 /* If we know this is a type-name, there's no need to look it
22446 up. */
22447 else if (typename_p)
22448 decl = identifier;
22449 else
22450 {
22451 tree ambiguous_decls;
22452 /* If we already know that this lookup is ambiguous, then
22453 we've already issued an error message; there's no reason
22454 to check again. */
22455 if (ambiguous_p)
22456 {
22457 cp_parser_simulate_error (parser);
22458 return error_mark_node;
22459 }
22460 /* If the next token is a `::', then the name must be a type
22461 name.
22462
22463 [basic.lookup.qual]
22464
22465 During the lookup for a name preceding the :: scope
22466 resolution operator, object, function, and enumerator
22467 names are ignored. */
22468 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22469 tag_type = scope_type;
22470 /* Look up the name. */
22471 decl = cp_parser_lookup_name (parser, identifier,
22472 tag_type,
22473 /*is_template=*/false,
22474 /*is_namespace=*/false,
22475 check_dependency_p,
22476 &ambiguous_decls,
22477 identifier_token->location);
22478 if (ambiguous_decls)
22479 {
22480 if (cp_parser_parsing_tentatively (parser))
22481 cp_parser_simulate_error (parser);
22482 return error_mark_node;
22483 }
22484 }
22485 }
22486 else
22487 {
22488 /* Try a template-id. */
22489 decl = cp_parser_template_id (parser, template_keyword_p,
22490 check_dependency_p,
22491 tag_type,
22492 is_declaration);
22493 if (decl == error_mark_node)
22494 return error_mark_node;
22495 }
22496
22497 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22498
22499 /* If this is a typename, create a TYPENAME_TYPE. */
22500 if (typename_p && decl != error_mark_node)
22501 {
22502 decl = make_typename_type (scope, decl, typename_type,
22503 /*complain=*/tf_error);
22504 if (decl != error_mark_node)
22505 decl = TYPE_NAME (decl);
22506 }
22507
22508 decl = strip_using_decl (decl);
22509
22510 /* Check to see that it is really the name of a class. */
22511 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22512 && identifier_p (TREE_OPERAND (decl, 0))
22513 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22514 /* Situations like this:
22515
22516 template <typename T> struct A {
22517 typename T::template X<int>::I i;
22518 };
22519
22520 are problematic. Is `T::template X<int>' a class-name? The
22521 standard does not seem to be definitive, but there is no other
22522 valid interpretation of the following `::'. Therefore, those
22523 names are considered class-names. */
22524 {
22525 decl = make_typename_type (scope, decl, tag_type, tf_error);
22526 if (decl != error_mark_node)
22527 decl = TYPE_NAME (decl);
22528 }
22529 else if (TREE_CODE (decl) != TYPE_DECL
22530 || TREE_TYPE (decl) == error_mark_node
22531 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22532 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22533 /* In Objective-C 2.0, a classname followed by '.' starts a
22534 dot-syntax expression, and it's not a type-name. */
22535 || (c_dialect_objc ()
22536 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22537 && objc_is_class_name (decl)))
22538 decl = error_mark_node;
22539
22540 if (decl == error_mark_node)
22541 cp_parser_error (parser, "expected class-name");
22542 else if (identifier && !parser->scope)
22543 maybe_note_name_used_in_class (identifier, decl);
22544
22545 return decl;
22546 }
22547
22548 /* Parse a class-specifier.
22549
22550 class-specifier:
22551 class-head { member-specification [opt] }
22552
22553 Returns the TREE_TYPE representing the class. */
22554
22555 static tree
22556 cp_parser_class_specifier_1 (cp_parser* parser)
22557 {
22558 tree type;
22559 tree attributes = NULL_TREE;
22560 bool nested_name_specifier_p;
22561 unsigned saved_num_template_parameter_lists;
22562 bool saved_in_function_body;
22563 unsigned char in_statement;
22564 bool in_switch_statement_p;
22565 bool saved_in_unbraced_linkage_specification_p;
22566 tree old_scope = NULL_TREE;
22567 tree scope = NULL_TREE;
22568 cp_token *closing_brace;
22569
22570 push_deferring_access_checks (dk_no_deferred);
22571
22572 /* Parse the class-head. */
22573 type = cp_parser_class_head (parser,
22574 &nested_name_specifier_p);
22575 /* If the class-head was a semantic disaster, skip the entire body
22576 of the class. */
22577 if (!type)
22578 {
22579 cp_parser_skip_to_end_of_block_or_statement (parser);
22580 pop_deferring_access_checks ();
22581 return error_mark_node;
22582 }
22583
22584 /* Look for the `{'. */
22585 matching_braces braces;
22586 if (!braces.require_open (parser))
22587 {
22588 pop_deferring_access_checks ();
22589 return error_mark_node;
22590 }
22591
22592 cp_ensure_no_omp_declare_simd (parser);
22593 cp_ensure_no_oacc_routine (parser);
22594
22595 /* Issue an error message if type-definitions are forbidden here. */
22596 cp_parser_check_type_definition (parser);
22597 /* Remember that we are defining one more class. */
22598 ++parser->num_classes_being_defined;
22599 /* Inside the class, surrounding template-parameter-lists do not
22600 apply. */
22601 saved_num_template_parameter_lists
22602 = parser->num_template_parameter_lists;
22603 parser->num_template_parameter_lists = 0;
22604 /* We are not in a function body. */
22605 saved_in_function_body = parser->in_function_body;
22606 parser->in_function_body = false;
22607 /* Or in a loop. */
22608 in_statement = parser->in_statement;
22609 parser->in_statement = 0;
22610 /* Or in a switch. */
22611 in_switch_statement_p = parser->in_switch_statement_p;
22612 parser->in_switch_statement_p = false;
22613 /* We are not immediately inside an extern "lang" block. */
22614 saved_in_unbraced_linkage_specification_p
22615 = parser->in_unbraced_linkage_specification_p;
22616 parser->in_unbraced_linkage_specification_p = false;
22617
22618 // Associate constraints with the type.
22619 if (flag_concepts)
22620 type = associate_classtype_constraints (type);
22621
22622 /* Start the class. */
22623 if (nested_name_specifier_p)
22624 {
22625 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22626 old_scope = push_inner_scope (scope);
22627 }
22628 type = begin_class_definition (type);
22629
22630 if (type == error_mark_node)
22631 /* If the type is erroneous, skip the entire body of the class. */
22632 cp_parser_skip_to_closing_brace (parser);
22633 else
22634 /* Parse the member-specification. */
22635 cp_parser_member_specification_opt (parser);
22636
22637 /* Look for the trailing `}'. */
22638 closing_brace = braces.require_close (parser);
22639 /* Look for trailing attributes to apply to this class. */
22640 if (cp_parser_allow_gnu_extensions_p (parser))
22641 attributes = cp_parser_gnu_attributes_opt (parser);
22642 if (type != error_mark_node)
22643 type = finish_struct (type, attributes);
22644 if (nested_name_specifier_p)
22645 pop_inner_scope (old_scope, scope);
22646
22647 /* We've finished a type definition. Check for the common syntax
22648 error of forgetting a semicolon after the definition. We need to
22649 be careful, as we can't just check for not-a-semicolon and be done
22650 with it; the user might have typed:
22651
22652 class X { } c = ...;
22653 class X { } *p = ...;
22654
22655 and so forth. Instead, enumerate all the possible tokens that
22656 might follow this production; if we don't see one of them, then
22657 complain and silently insert the semicolon. */
22658 {
22659 cp_token *token = cp_lexer_peek_token (parser->lexer);
22660 bool want_semicolon = true;
22661
22662 if (cp_next_tokens_can_be_std_attribute_p (parser))
22663 /* Don't try to parse c++11 attributes here. As per the
22664 grammar, that should be a task for
22665 cp_parser_decl_specifier_seq. */
22666 want_semicolon = false;
22667
22668 switch (token->type)
22669 {
22670 case CPP_NAME:
22671 case CPP_SEMICOLON:
22672 case CPP_MULT:
22673 case CPP_AND:
22674 case CPP_OPEN_PAREN:
22675 case CPP_CLOSE_PAREN:
22676 case CPP_COMMA:
22677 want_semicolon = false;
22678 break;
22679
22680 /* While it's legal for type qualifiers and storage class
22681 specifiers to follow type definitions in the grammar, only
22682 compiler testsuites contain code like that. Assume that if
22683 we see such code, then what we're really seeing is a case
22684 like:
22685
22686 class X { }
22687 const <type> var = ...;
22688
22689 or
22690
22691 class Y { }
22692 static <type> func (...) ...
22693
22694 i.e. the qualifier or specifier applies to the next
22695 declaration. To do so, however, we need to look ahead one
22696 more token to see if *that* token is a type specifier.
22697
22698 This code could be improved to handle:
22699
22700 class Z { }
22701 static const <type> var = ...; */
22702 case CPP_KEYWORD:
22703 if (keyword_is_decl_specifier (token->keyword))
22704 {
22705 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22706
22707 /* Handling user-defined types here would be nice, but very
22708 tricky. */
22709 want_semicolon
22710 = (lookahead->type == CPP_KEYWORD
22711 && keyword_begins_type_specifier (lookahead->keyword));
22712 }
22713 break;
22714 default:
22715 break;
22716 }
22717
22718 /* If we don't have a type, then something is very wrong and we
22719 shouldn't try to do anything clever. Likewise for not seeing the
22720 closing brace. */
22721 if (closing_brace && TYPE_P (type) && want_semicolon)
22722 {
22723 /* Locate the closing brace. */
22724 cp_token_position prev
22725 = cp_lexer_previous_token_position (parser->lexer);
22726 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22727 location_t loc = prev_token->location;
22728
22729 /* We want to suggest insertion of a ';' immediately *after* the
22730 closing brace, so, if we can, offset the location by 1 column. */
22731 location_t next_loc = loc;
22732 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22733 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22734
22735 rich_location richloc (line_table, next_loc);
22736
22737 /* If we successfully offset the location, suggest the fix-it. */
22738 if (next_loc != loc)
22739 richloc.add_fixit_insert_before (next_loc, ";");
22740
22741 if (CLASSTYPE_DECLARED_CLASS (type))
22742 error_at (&richloc,
22743 "expected %<;%> after class definition");
22744 else if (TREE_CODE (type) == RECORD_TYPE)
22745 error_at (&richloc,
22746 "expected %<;%> after struct definition");
22747 else if (TREE_CODE (type) == UNION_TYPE)
22748 error_at (&richloc,
22749 "expected %<;%> after union definition");
22750 else
22751 gcc_unreachable ();
22752
22753 /* Unget one token and smash it to look as though we encountered
22754 a semicolon in the input stream. */
22755 cp_lexer_set_token_position (parser->lexer, prev);
22756 token = cp_lexer_peek_token (parser->lexer);
22757 token->type = CPP_SEMICOLON;
22758 token->keyword = RID_MAX;
22759 }
22760 }
22761
22762 /* If this class is not itself within the scope of another class,
22763 then we need to parse the bodies of all of the queued function
22764 definitions. Note that the queued functions defined in a class
22765 are not always processed immediately following the
22766 class-specifier for that class. Consider:
22767
22768 struct A {
22769 struct B { void f() { sizeof (A); } };
22770 };
22771
22772 If `f' were processed before the processing of `A' were
22773 completed, there would be no way to compute the size of `A'.
22774 Note that the nesting we are interested in here is lexical --
22775 not the semantic nesting given by TYPE_CONTEXT. In particular,
22776 for:
22777
22778 struct A { struct B; };
22779 struct A::B { void f() { } };
22780
22781 there is no need to delay the parsing of `A::B::f'. */
22782 if (--parser->num_classes_being_defined == 0)
22783 {
22784 tree decl;
22785 tree class_type = NULL_TREE;
22786 tree pushed_scope = NULL_TREE;
22787 unsigned ix;
22788 cp_default_arg_entry *e;
22789 tree save_ccp, save_ccr;
22790
22791 if (any_erroneous_template_args_p (type))
22792 {
22793 /* Skip default arguments, NSDMIs, etc, in order to improve
22794 error recovery (c++/71169, c++/71832). */
22795 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22796 vec_safe_truncate (unparsed_nsdmis, 0);
22797 vec_safe_truncate (unparsed_classes, 0);
22798 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22799 }
22800
22801 /* In a first pass, parse default arguments to the functions.
22802 Then, in a second pass, parse the bodies of the functions.
22803 This two-phased approach handles cases like:
22804
22805 struct S {
22806 void f() { g(); }
22807 void g(int i = 3);
22808 };
22809
22810 */
22811 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22812 {
22813 decl = e->decl;
22814 /* If there are default arguments that have not yet been processed,
22815 take care of them now. */
22816 if (class_type != e->class_type)
22817 {
22818 if (pushed_scope)
22819 pop_scope (pushed_scope);
22820 class_type = e->class_type;
22821 pushed_scope = push_scope (class_type);
22822 }
22823 /* Make sure that any template parameters are in scope. */
22824 maybe_begin_member_template_processing (decl);
22825 /* Parse the default argument expressions. */
22826 cp_parser_late_parsing_default_args (parser, decl);
22827 /* Remove any template parameters from the symbol table. */
22828 maybe_end_member_template_processing ();
22829 }
22830 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22831 /* Now parse any NSDMIs. */
22832 save_ccp = current_class_ptr;
22833 save_ccr = current_class_ref;
22834 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22835 {
22836 if (class_type != DECL_CONTEXT (decl))
22837 {
22838 if (pushed_scope)
22839 pop_scope (pushed_scope);
22840 class_type = DECL_CONTEXT (decl);
22841 pushed_scope = push_scope (class_type);
22842 }
22843 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22844 cp_parser_late_parsing_nsdmi (parser, decl);
22845 }
22846 vec_safe_truncate (unparsed_nsdmis, 0);
22847 current_class_ptr = save_ccp;
22848 current_class_ref = save_ccr;
22849 if (pushed_scope)
22850 pop_scope (pushed_scope);
22851
22852 /* Now do some post-NSDMI bookkeeping. */
22853 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22854 after_nsdmi_defaulted_late_checks (class_type);
22855 vec_safe_truncate (unparsed_classes, 0);
22856 after_nsdmi_defaulted_late_checks (type);
22857
22858 /* Now parse the body of the functions. */
22859 if (flag_openmp)
22860 {
22861 /* OpenMP UDRs need to be parsed before all other functions. */
22862 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22863 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22864 cp_parser_late_parsing_for_member (parser, decl);
22865 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22866 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22867 cp_parser_late_parsing_for_member (parser, decl);
22868 }
22869 else
22870 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22871 cp_parser_late_parsing_for_member (parser, decl);
22872 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22873 }
22874 else
22875 vec_safe_push (unparsed_classes, type);
22876
22877 /* Put back any saved access checks. */
22878 pop_deferring_access_checks ();
22879
22880 /* Restore saved state. */
22881 parser->in_switch_statement_p = in_switch_statement_p;
22882 parser->in_statement = in_statement;
22883 parser->in_function_body = saved_in_function_body;
22884 parser->num_template_parameter_lists
22885 = saved_num_template_parameter_lists;
22886 parser->in_unbraced_linkage_specification_p
22887 = saved_in_unbraced_linkage_specification_p;
22888
22889 return type;
22890 }
22891
22892 static tree
22893 cp_parser_class_specifier (cp_parser* parser)
22894 {
22895 tree ret;
22896 timevar_push (TV_PARSE_STRUCT);
22897 ret = cp_parser_class_specifier_1 (parser);
22898 timevar_pop (TV_PARSE_STRUCT);
22899 return ret;
22900 }
22901
22902 /* Parse a class-head.
22903
22904 class-head:
22905 class-key identifier [opt] base-clause [opt]
22906 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22907 class-key nested-name-specifier [opt] template-id
22908 base-clause [opt]
22909
22910 class-virt-specifier:
22911 final
22912
22913 GNU Extensions:
22914 class-key attributes identifier [opt] base-clause [opt]
22915 class-key attributes nested-name-specifier identifier base-clause [opt]
22916 class-key attributes nested-name-specifier [opt] template-id
22917 base-clause [opt]
22918
22919 Upon return BASES is initialized to the list of base classes (or
22920 NULL, if there are none) in the same form returned by
22921 cp_parser_base_clause.
22922
22923 Returns the TYPE of the indicated class. Sets
22924 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22925 involving a nested-name-specifier was used, and FALSE otherwise.
22926
22927 Returns error_mark_node if this is not a class-head.
22928
22929 Returns NULL_TREE if the class-head is syntactically valid, but
22930 semantically invalid in a way that means we should skip the entire
22931 body of the class. */
22932
22933 static tree
22934 cp_parser_class_head (cp_parser* parser,
22935 bool* nested_name_specifier_p)
22936 {
22937 tree nested_name_specifier;
22938 enum tag_types class_key;
22939 tree id = NULL_TREE;
22940 tree type = NULL_TREE;
22941 tree attributes;
22942 tree bases;
22943 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22944 bool template_id_p = false;
22945 bool qualified_p = false;
22946 bool invalid_nested_name_p = false;
22947 bool invalid_explicit_specialization_p = false;
22948 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22949 tree pushed_scope = NULL_TREE;
22950 unsigned num_templates;
22951 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22952 /* Assume no nested-name-specifier will be present. */
22953 *nested_name_specifier_p = false;
22954 /* Assume no template parameter lists will be used in defining the
22955 type. */
22956 num_templates = 0;
22957 parser->colon_corrects_to_scope_p = false;
22958
22959 /* Look for the class-key. */
22960 class_key = cp_parser_class_key (parser);
22961 if (class_key == none_type)
22962 return error_mark_node;
22963
22964 location_t class_head_start_location = input_location;
22965
22966 /* Parse the attributes. */
22967 attributes = cp_parser_attributes_opt (parser);
22968
22969 /* If the next token is `::', that is invalid -- but sometimes
22970 people do try to write:
22971
22972 struct ::S {};
22973
22974 Handle this gracefully by accepting the extra qualifier, and then
22975 issuing an error about it later if this really is a
22976 class-head. If it turns out just to be an elaborated type
22977 specifier, remain silent. */
22978 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22979 qualified_p = true;
22980
22981 push_deferring_access_checks (dk_no_check);
22982
22983 /* Determine the name of the class. Begin by looking for an
22984 optional nested-name-specifier. */
22985 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22986 nested_name_specifier
22987 = cp_parser_nested_name_specifier_opt (parser,
22988 /*typename_keyword_p=*/false,
22989 /*check_dependency_p=*/false,
22990 /*type_p=*/true,
22991 /*is_declaration=*/false);
22992 /* If there was a nested-name-specifier, then there *must* be an
22993 identifier. */
22994
22995 cp_token *bad_template_keyword = NULL;
22996
22997 if (nested_name_specifier)
22998 {
22999 type_start_token = cp_lexer_peek_token (parser->lexer);
23000 /* Although the grammar says `identifier', it really means
23001 `class-name' or `template-name'. You are only allowed to
23002 define a class that has already been declared with this
23003 syntax.
23004
23005 The proposed resolution for Core Issue 180 says that wherever
23006 you see `class T::X' you should treat `X' as a type-name.
23007
23008 It is OK to define an inaccessible class; for example:
23009
23010 class A { class B; };
23011 class A::B {};
23012
23013 We do not know if we will see a class-name, or a
23014 template-name. We look for a class-name first, in case the
23015 class-name is a template-id; if we looked for the
23016 template-name first we would stop after the template-name. */
23017 cp_parser_parse_tentatively (parser);
23018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23019 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23020 type = cp_parser_class_name (parser,
23021 /*typename_keyword_p=*/false,
23022 /*template_keyword_p=*/false,
23023 class_type,
23024 /*check_dependency_p=*/false,
23025 /*class_head_p=*/true,
23026 /*is_declaration=*/false);
23027 /* If that didn't work, ignore the nested-name-specifier. */
23028 if (!cp_parser_parse_definitely (parser))
23029 {
23030 invalid_nested_name_p = true;
23031 type_start_token = cp_lexer_peek_token (parser->lexer);
23032 id = cp_parser_identifier (parser);
23033 if (id == error_mark_node)
23034 id = NULL_TREE;
23035 }
23036 /* If we could not find a corresponding TYPE, treat this
23037 declaration like an unqualified declaration. */
23038 if (type == error_mark_node)
23039 nested_name_specifier = NULL_TREE;
23040 /* Otherwise, count the number of templates used in TYPE and its
23041 containing scopes. */
23042 else
23043 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23044 }
23045 /* Otherwise, the identifier is optional. */
23046 else
23047 {
23048 /* We don't know whether what comes next is a template-id,
23049 an identifier, or nothing at all. */
23050 cp_parser_parse_tentatively (parser);
23051 /* Check for a template-id. */
23052 type_start_token = cp_lexer_peek_token (parser->lexer);
23053 id = cp_parser_template_id (parser,
23054 /*template_keyword_p=*/false,
23055 /*check_dependency_p=*/true,
23056 class_key,
23057 /*is_declaration=*/true);
23058 /* If that didn't work, it could still be an identifier. */
23059 if (!cp_parser_parse_definitely (parser))
23060 {
23061 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23062 {
23063 type_start_token = cp_lexer_peek_token (parser->lexer);
23064 id = cp_parser_identifier (parser);
23065 }
23066 else
23067 id = NULL_TREE;
23068 }
23069 else
23070 {
23071 template_id_p = true;
23072 ++num_templates;
23073 }
23074 }
23075
23076 pop_deferring_access_checks ();
23077
23078 if (id)
23079 {
23080 cp_parser_check_for_invalid_template_id (parser, id,
23081 class_key,
23082 type_start_token->location);
23083 }
23084 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23085
23086 /* If it's not a `:' or a `{' then we can't really be looking at a
23087 class-head, since a class-head only appears as part of a
23088 class-specifier. We have to detect this situation before calling
23089 xref_tag, since that has irreversible side-effects. */
23090 if (!cp_parser_next_token_starts_class_definition_p (parser))
23091 {
23092 cp_parser_error (parser, "expected %<{%> or %<:%>");
23093 type = error_mark_node;
23094 goto out;
23095 }
23096
23097 /* At this point, we're going ahead with the class-specifier, even
23098 if some other problem occurs. */
23099 cp_parser_commit_to_tentative_parse (parser);
23100 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23101 {
23102 cp_parser_error (parser,
23103 "cannot specify %<override%> for a class");
23104 type = error_mark_node;
23105 goto out;
23106 }
23107 /* Issue the error about the overly-qualified name now. */
23108 if (qualified_p)
23109 {
23110 cp_parser_error (parser,
23111 "global qualification of class name is invalid");
23112 type = error_mark_node;
23113 goto out;
23114 }
23115 else if (invalid_nested_name_p)
23116 {
23117 cp_parser_error (parser,
23118 "qualified name does not name a class");
23119 type = error_mark_node;
23120 goto out;
23121 }
23122 else if (nested_name_specifier)
23123 {
23124 tree scope;
23125
23126 if (bad_template_keyword)
23127 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23128 keyword template shall not appear at the top level. */
23129 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23130 "keyword %<template%> not allowed in class-head-name");
23131
23132 /* Reject typedef-names in class heads. */
23133 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23134 {
23135 error_at (type_start_token->location,
23136 "invalid class name in declaration of %qD",
23137 type);
23138 type = NULL_TREE;
23139 goto done;
23140 }
23141
23142 /* Figure out in what scope the declaration is being placed. */
23143 scope = current_scope ();
23144 /* If that scope does not contain the scope in which the
23145 class was originally declared, the program is invalid. */
23146 if (scope && !is_ancestor (scope, nested_name_specifier))
23147 {
23148 if (at_namespace_scope_p ())
23149 error_at (type_start_token->location,
23150 "declaration of %qD in namespace %qD which does not "
23151 "enclose %qD",
23152 type, scope, nested_name_specifier);
23153 else
23154 error_at (type_start_token->location,
23155 "declaration of %qD in %qD which does not enclose %qD",
23156 type, scope, nested_name_specifier);
23157 type = NULL_TREE;
23158 goto done;
23159 }
23160 /* [dcl.meaning]
23161
23162 A declarator-id shall not be qualified except for the
23163 definition of a ... nested class outside of its class
23164 ... [or] the definition or explicit instantiation of a
23165 class member of a namespace outside of its namespace. */
23166 if (scope == nested_name_specifier)
23167 {
23168 permerror (nested_name_specifier_token_start->location,
23169 "extra qualification not allowed");
23170 nested_name_specifier = NULL_TREE;
23171 num_templates = 0;
23172 }
23173 }
23174 /* An explicit-specialization must be preceded by "template <>". If
23175 it is not, try to recover gracefully. */
23176 if (at_namespace_scope_p ()
23177 && parser->num_template_parameter_lists == 0
23178 && !processing_template_parmlist
23179 && template_id_p)
23180 {
23181 /* Build a location of this form:
23182 struct typename <ARGS>
23183 ^~~~~~~~~~~~~~~~~~~~~~
23184 with caret==start at the start token, and
23185 finishing at the end of the type. */
23186 location_t reported_loc
23187 = make_location (class_head_start_location,
23188 class_head_start_location,
23189 get_finish (type_start_token->location));
23190 rich_location richloc (line_table, reported_loc);
23191 richloc.add_fixit_insert_before (class_head_start_location,
23192 "template <> ");
23193 error_at (&richloc,
23194 "an explicit specialization must be preceded by"
23195 " %<template <>%>");
23196 invalid_explicit_specialization_p = true;
23197 /* Take the same action that would have been taken by
23198 cp_parser_explicit_specialization. */
23199 ++parser->num_template_parameter_lists;
23200 begin_specialization ();
23201 }
23202 /* There must be no "return" statements between this point and the
23203 end of this function; set "type "to the correct return value and
23204 use "goto done;" to return. */
23205 /* Make sure that the right number of template parameters were
23206 present. */
23207 if (!cp_parser_check_template_parameters (parser, num_templates,
23208 template_id_p,
23209 type_start_token->location,
23210 /*declarator=*/NULL))
23211 {
23212 /* If something went wrong, there is no point in even trying to
23213 process the class-definition. */
23214 type = NULL_TREE;
23215 goto done;
23216 }
23217
23218 /* Look up the type. */
23219 if (template_id_p)
23220 {
23221 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23222 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23223 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23224 {
23225 error_at (type_start_token->location,
23226 "function template %qD redeclared as a class template", id);
23227 type = error_mark_node;
23228 }
23229 else
23230 {
23231 type = TREE_TYPE (id);
23232 type = maybe_process_partial_specialization (type);
23233
23234 /* Check the scope while we still know whether or not we had a
23235 nested-name-specifier. */
23236 if (type != error_mark_node)
23237 check_unqualified_spec_or_inst (type, type_start_token->location);
23238 }
23239 if (nested_name_specifier)
23240 pushed_scope = push_scope (nested_name_specifier);
23241 }
23242 else if (nested_name_specifier)
23243 {
23244 tree class_type;
23245
23246 /* Given:
23247
23248 template <typename T> struct S { struct T };
23249 template <typename T> struct S<T>::T { };
23250
23251 we will get a TYPENAME_TYPE when processing the definition of
23252 `S::T'. We need to resolve it to the actual type before we
23253 try to define it. */
23254 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23255 {
23256 class_type = resolve_typename_type (TREE_TYPE (type),
23257 /*only_current_p=*/false);
23258 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23259 type = TYPE_NAME (class_type);
23260 else
23261 {
23262 cp_parser_error (parser, "could not resolve typename type");
23263 type = error_mark_node;
23264 }
23265 }
23266
23267 if (maybe_process_partial_specialization (TREE_TYPE (type))
23268 == error_mark_node)
23269 {
23270 type = NULL_TREE;
23271 goto done;
23272 }
23273
23274 class_type = current_class_type;
23275 /* Enter the scope indicated by the nested-name-specifier. */
23276 pushed_scope = push_scope (nested_name_specifier);
23277 /* Get the canonical version of this type. */
23278 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23279 /* Call push_template_decl if it seems like we should be defining a
23280 template either from the template headers or the type we're
23281 defining, so that we diagnose both extra and missing headers. */
23282 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23283 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23284 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23285 {
23286 type = push_template_decl (type);
23287 if (type == error_mark_node)
23288 {
23289 type = NULL_TREE;
23290 goto done;
23291 }
23292 }
23293
23294 type = TREE_TYPE (type);
23295 *nested_name_specifier_p = true;
23296 }
23297 else /* The name is not a nested name. */
23298 {
23299 /* If the class was unnamed, create a dummy name. */
23300 if (!id)
23301 id = make_anon_name ();
23302 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23303 ? ts_within_enclosing_non_class
23304 : ts_current);
23305 type = xref_tag (class_key, id, tag_scope,
23306 parser->num_template_parameter_lists);
23307 }
23308
23309 /* Indicate whether this class was declared as a `class' or as a
23310 `struct'. */
23311 if (TREE_CODE (type) == RECORD_TYPE)
23312 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23313 cp_parser_check_class_key (class_key, type);
23314
23315 /* If this type was already complete, and we see another definition,
23316 that's an error. */
23317 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23318 {
23319 error_at (type_start_token->location, "redefinition of %q#T",
23320 type);
23321 inform (location_of (type), "previous definition of %q#T",
23322 type);
23323 type = NULL_TREE;
23324 goto done;
23325 }
23326 else if (type == error_mark_node)
23327 type = NULL_TREE;
23328
23329 if (type)
23330 {
23331 /* Apply attributes now, before any use of the class as a template
23332 argument in its base list. */
23333 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23334 fixup_attribute_variants (type);
23335 }
23336
23337 /* We will have entered the scope containing the class; the names of
23338 base classes should be looked up in that context. For example:
23339
23340 struct A { struct B {}; struct C; };
23341 struct A::C : B {};
23342
23343 is valid. */
23344
23345 /* Get the list of base-classes, if there is one. */
23346 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23347 {
23348 /* PR59482: enter the class scope so that base-specifiers are looked
23349 up correctly. */
23350 if (type)
23351 pushclass (type);
23352 bases = cp_parser_base_clause (parser);
23353 /* PR59482: get out of the previously pushed class scope so that the
23354 subsequent pops pop the right thing. */
23355 if (type)
23356 popclass ();
23357 }
23358 else
23359 bases = NULL_TREE;
23360
23361 /* If we're really defining a class, process the base classes.
23362 If they're invalid, fail. */
23363 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23364 xref_basetypes (type, bases);
23365
23366 done:
23367 /* Leave the scope given by the nested-name-specifier. We will
23368 enter the class scope itself while processing the members. */
23369 if (pushed_scope)
23370 pop_scope (pushed_scope);
23371
23372 if (invalid_explicit_specialization_p)
23373 {
23374 end_specialization ();
23375 --parser->num_template_parameter_lists;
23376 }
23377
23378 if (type)
23379 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23380 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23381 CLASSTYPE_FINAL (type) = 1;
23382 out:
23383 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23384 return type;
23385 }
23386
23387 /* Parse a class-key.
23388
23389 class-key:
23390 class
23391 struct
23392 union
23393
23394 Returns the kind of class-key specified, or none_type to indicate
23395 error. */
23396
23397 static enum tag_types
23398 cp_parser_class_key (cp_parser* parser)
23399 {
23400 cp_token *token;
23401 enum tag_types tag_type;
23402
23403 /* Look for the class-key. */
23404 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23405 if (!token)
23406 return none_type;
23407
23408 /* Check to see if the TOKEN is a class-key. */
23409 tag_type = cp_parser_token_is_class_key (token);
23410 if (!tag_type)
23411 cp_parser_error (parser, "expected class-key");
23412 return tag_type;
23413 }
23414
23415 /* Parse a type-parameter-key.
23416
23417 type-parameter-key:
23418 class
23419 typename
23420 */
23421
23422 static void
23423 cp_parser_type_parameter_key (cp_parser* parser)
23424 {
23425 /* Look for the type-parameter-key. */
23426 enum tag_types tag_type = none_type;
23427 cp_token *token = cp_lexer_peek_token (parser->lexer);
23428 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23429 {
23430 cp_lexer_consume_token (parser->lexer);
23431 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23432 /* typename is not allowed in a template template parameter
23433 by the standard until C++17. */
23434 pedwarn (token->location, OPT_Wpedantic,
23435 "ISO C++ forbids typename key in template template parameter;"
23436 " use -std=c++17 or -std=gnu++17");
23437 }
23438 else
23439 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23440
23441 return;
23442 }
23443
23444 /* Parse an (optional) member-specification.
23445
23446 member-specification:
23447 member-declaration member-specification [opt]
23448 access-specifier : member-specification [opt] */
23449
23450 static void
23451 cp_parser_member_specification_opt (cp_parser* parser)
23452 {
23453 while (true)
23454 {
23455 cp_token *token;
23456 enum rid keyword;
23457
23458 /* Peek at the next token. */
23459 token = cp_lexer_peek_token (parser->lexer);
23460 /* If it's a `}', or EOF then we've seen all the members. */
23461 if (token->type == CPP_CLOSE_BRACE
23462 || token->type == CPP_EOF
23463 || token->type == CPP_PRAGMA_EOL)
23464 break;
23465
23466 /* See if this token is a keyword. */
23467 keyword = token->keyword;
23468 switch (keyword)
23469 {
23470 case RID_PUBLIC:
23471 case RID_PROTECTED:
23472 case RID_PRIVATE:
23473 /* Consume the access-specifier. */
23474 cp_lexer_consume_token (parser->lexer);
23475 /* Remember which access-specifier is active. */
23476 current_access_specifier = token->u.value;
23477 /* Look for the `:'. */
23478 cp_parser_require (parser, CPP_COLON, RT_COLON);
23479 break;
23480
23481 default:
23482 /* Accept #pragmas at class scope. */
23483 if (token->type == CPP_PRAGMA)
23484 {
23485 cp_parser_pragma (parser, pragma_member, NULL);
23486 break;
23487 }
23488
23489 /* Otherwise, the next construction must be a
23490 member-declaration. */
23491 cp_parser_member_declaration (parser);
23492 }
23493 }
23494 }
23495
23496 /* Parse a member-declaration.
23497
23498 member-declaration:
23499 decl-specifier-seq [opt] member-declarator-list [opt] ;
23500 function-definition ; [opt]
23501 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23502 using-declaration
23503 template-declaration
23504 alias-declaration
23505
23506 member-declarator-list:
23507 member-declarator
23508 member-declarator-list , member-declarator
23509
23510 member-declarator:
23511 declarator pure-specifier [opt]
23512 declarator constant-initializer [opt]
23513 identifier [opt] : constant-expression
23514
23515 GNU Extensions:
23516
23517 member-declaration:
23518 __extension__ member-declaration
23519
23520 member-declarator:
23521 declarator attributes [opt] pure-specifier [opt]
23522 declarator attributes [opt] constant-initializer [opt]
23523 identifier [opt] attributes [opt] : constant-expression
23524
23525 C++0x Extensions:
23526
23527 member-declaration:
23528 static_assert-declaration */
23529
23530 static void
23531 cp_parser_member_declaration (cp_parser* parser)
23532 {
23533 cp_decl_specifier_seq decl_specifiers;
23534 tree prefix_attributes;
23535 tree decl;
23536 int declares_class_or_enum;
23537 bool friend_p;
23538 cp_token *token = NULL;
23539 cp_token *decl_spec_token_start = NULL;
23540 cp_token *initializer_token_start = NULL;
23541 int saved_pedantic;
23542 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23543
23544 /* Check for the `__extension__' keyword. */
23545 if (cp_parser_extension_opt (parser, &saved_pedantic))
23546 {
23547 /* Recurse. */
23548 cp_parser_member_declaration (parser);
23549 /* Restore the old value of the PEDANTIC flag. */
23550 pedantic = saved_pedantic;
23551
23552 return;
23553 }
23554
23555 /* Check for a template-declaration. */
23556 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23557 {
23558 /* An explicit specialization here is an error condition, and we
23559 expect the specialization handler to detect and report this. */
23560 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23561 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23562 cp_parser_explicit_specialization (parser);
23563 else
23564 cp_parser_template_declaration (parser, /*member_p=*/true);
23565
23566 return;
23567 }
23568 /* Check for a template introduction. */
23569 else if (cp_parser_template_declaration_after_export (parser, true))
23570 return;
23571
23572 /* Check for a using-declaration. */
23573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23574 {
23575 if (cxx_dialect < cxx11)
23576 {
23577 /* Parse the using-declaration. */
23578 cp_parser_using_declaration (parser,
23579 /*access_declaration_p=*/false);
23580 return;
23581 }
23582 else
23583 {
23584 tree decl;
23585 bool alias_decl_expected;
23586 cp_parser_parse_tentatively (parser);
23587 decl = cp_parser_alias_declaration (parser);
23588 /* Note that if we actually see the '=' token after the
23589 identifier, cp_parser_alias_declaration commits the
23590 tentative parse. In that case, we really expect an
23591 alias-declaration. Otherwise, we expect a using
23592 declaration. */
23593 alias_decl_expected =
23594 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23595 cp_parser_parse_definitely (parser);
23596
23597 if (alias_decl_expected)
23598 finish_member_declaration (decl);
23599 else
23600 cp_parser_using_declaration (parser,
23601 /*access_declaration_p=*/false);
23602 return;
23603 }
23604 }
23605
23606 /* Check for @defs. */
23607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23608 {
23609 tree ivar, member;
23610 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23611 ivar = ivar_chains;
23612 while (ivar)
23613 {
23614 member = ivar;
23615 ivar = TREE_CHAIN (member);
23616 TREE_CHAIN (member) = NULL_TREE;
23617 finish_member_declaration (member);
23618 }
23619 return;
23620 }
23621
23622 /* If the next token is `static_assert' we have a static assertion. */
23623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23624 {
23625 cp_parser_static_assert (parser, /*member_p=*/true);
23626 return;
23627 }
23628
23629 parser->colon_corrects_to_scope_p = false;
23630
23631 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23632 goto out;
23633
23634 /* Parse the decl-specifier-seq. */
23635 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23636 cp_parser_decl_specifier_seq (parser,
23637 CP_PARSER_FLAGS_OPTIONAL,
23638 &decl_specifiers,
23639 &declares_class_or_enum);
23640 /* Check for an invalid type-name. */
23641 if (!decl_specifiers.any_type_specifiers_p
23642 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23643 goto out;
23644 /* If there is no declarator, then the decl-specifier-seq should
23645 specify a type. */
23646 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23647 {
23648 /* If there was no decl-specifier-seq, and the next token is a
23649 `;', then we have something like:
23650
23651 struct S { ; };
23652
23653 [class.mem]
23654
23655 Each member-declaration shall declare at least one member
23656 name of the class. */
23657 if (!decl_specifiers.any_specifiers_p)
23658 {
23659 cp_token *token = cp_lexer_peek_token (parser->lexer);
23660 if (!in_system_header_at (token->location))
23661 {
23662 gcc_rich_location richloc (token->location);
23663 richloc.add_fixit_remove ();
23664 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23665 }
23666 }
23667 else
23668 {
23669 tree type;
23670
23671 /* See if this declaration is a friend. */
23672 friend_p = cp_parser_friend_p (&decl_specifiers);
23673 /* If there were decl-specifiers, check to see if there was
23674 a class-declaration. */
23675 type = check_tag_decl (&decl_specifiers,
23676 /*explicit_type_instantiation_p=*/false);
23677 /* Nested classes have already been added to the class, but
23678 a `friend' needs to be explicitly registered. */
23679 if (friend_p)
23680 {
23681 /* If the `friend' keyword was present, the friend must
23682 be introduced with a class-key. */
23683 if (!declares_class_or_enum && cxx_dialect < cxx11)
23684 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23685 "in C++03 a class-key must be used "
23686 "when declaring a friend");
23687 /* In this case:
23688
23689 template <typename T> struct A {
23690 friend struct A<T>::B;
23691 };
23692
23693 A<T>::B will be represented by a TYPENAME_TYPE, and
23694 therefore not recognized by check_tag_decl. */
23695 if (!type)
23696 {
23697 type = decl_specifiers.type;
23698 if (type && TREE_CODE (type) == TYPE_DECL)
23699 type = TREE_TYPE (type);
23700 }
23701 if (!type || !TYPE_P (type))
23702 error_at (decl_spec_token_start->location,
23703 "friend declaration does not name a class or "
23704 "function");
23705 else
23706 make_friend_class (current_class_type, type,
23707 /*complain=*/true);
23708 }
23709 /* If there is no TYPE, an error message will already have
23710 been issued. */
23711 else if (!type || type == error_mark_node)
23712 ;
23713 /* An anonymous aggregate has to be handled specially; such
23714 a declaration really declares a data member (with a
23715 particular type), as opposed to a nested class. */
23716 else if (ANON_AGGR_TYPE_P (type))
23717 {
23718 /* C++11 9.5/6. */
23719 if (decl_specifiers.storage_class != sc_none)
23720 error_at (decl_spec_token_start->location,
23721 "a storage class on an anonymous aggregate "
23722 "in class scope is not allowed");
23723
23724 /* Remove constructors and such from TYPE, now that we
23725 know it is an anonymous aggregate. */
23726 fixup_anonymous_aggr (type);
23727 /* And make the corresponding data member. */
23728 decl = build_decl (decl_spec_token_start->location,
23729 FIELD_DECL, NULL_TREE, type);
23730 /* Add it to the class. */
23731 finish_member_declaration (decl);
23732 }
23733 else
23734 cp_parser_check_access_in_redeclaration
23735 (TYPE_NAME (type),
23736 decl_spec_token_start->location);
23737 }
23738 }
23739 else
23740 {
23741 bool assume_semicolon = false;
23742
23743 /* Clear attributes from the decl_specifiers but keep them
23744 around as prefix attributes that apply them to the entity
23745 being declared. */
23746 prefix_attributes = decl_specifiers.attributes;
23747 decl_specifiers.attributes = NULL_TREE;
23748
23749 /* See if these declarations will be friends. */
23750 friend_p = cp_parser_friend_p (&decl_specifiers);
23751
23752 /* Keep going until we hit the `;' at the end of the
23753 declaration. */
23754 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23755 {
23756 tree attributes = NULL_TREE;
23757 tree first_attribute;
23758 tree initializer;
23759 bool named_bitfld = false;
23760
23761 /* Peek at the next token. */
23762 token = cp_lexer_peek_token (parser->lexer);
23763
23764 /* The following code wants to know early if it is a bit-field
23765 or some other declaration. Attributes can appear before
23766 the `:' token. Skip over them without consuming any tokens
23767 to peek if they are followed by `:'. */
23768 if (cp_next_tokens_can_be_attribute_p (parser)
23769 || (token->type == CPP_NAME
23770 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23771 && (named_bitfld = true)))
23772 {
23773 size_t n
23774 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23775 token = cp_lexer_peek_nth_token (parser->lexer, n);
23776 }
23777
23778 /* Check for a bitfield declaration. */
23779 if (token->type == CPP_COLON
23780 || (token->type == CPP_NAME
23781 && token == cp_lexer_peek_token (parser->lexer)
23782 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23783 && (named_bitfld = true)))
23784 {
23785 tree identifier;
23786 tree width;
23787 tree late_attributes = NULL_TREE;
23788
23789 if (named_bitfld)
23790 identifier = cp_parser_identifier (parser);
23791 else
23792 identifier = NULL_TREE;
23793
23794 /* Look for attributes that apply to the bitfield. */
23795 attributes = cp_parser_attributes_opt (parser);
23796
23797 /* Consume the `:' token. */
23798 cp_lexer_consume_token (parser->lexer);
23799
23800 /* Get the width of the bitfield. */
23801 width = cp_parser_constant_expression (parser, false, NULL,
23802 cxx_dialect >= cxx11);
23803
23804 /* In C++2A and as extension for C++11 and above we allow
23805 default member initializers for bit-fields. */
23806 initializer = NULL_TREE;
23807 if (cxx_dialect >= cxx11
23808 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23809 || cp_lexer_next_token_is (parser->lexer,
23810 CPP_OPEN_BRACE)))
23811 {
23812 location_t loc
23813 = cp_lexer_peek_token (parser->lexer)->location;
23814 if (cxx_dialect < cxx2a
23815 && !in_system_header_at (loc)
23816 && identifier != NULL_TREE)
23817 pedwarn (loc, 0,
23818 "default member initializers for bit-fields "
23819 "only available with -std=c++2a or "
23820 "-std=gnu++2a");
23821
23822 initializer = cp_parser_save_nsdmi (parser);
23823 if (identifier == NULL_TREE)
23824 {
23825 error_at (loc, "default member initializer for "
23826 "unnamed bit-field");
23827 initializer = NULL_TREE;
23828 }
23829 }
23830 else
23831 {
23832 /* Look for attributes that apply to the bitfield after
23833 the `:' token and width. This is where GCC used to
23834 parse attributes in the past, pedwarn if there is
23835 a std attribute. */
23836 if (cp_next_tokens_can_be_std_attribute_p (parser))
23837 pedwarn (input_location, OPT_Wpedantic,
23838 "ISO C++ allows bit-field attributes only "
23839 "before the %<:%> token");
23840
23841 late_attributes = cp_parser_attributes_opt (parser);
23842 }
23843
23844 attributes = attr_chainon (attributes, late_attributes);
23845
23846 /* Remember which attributes are prefix attributes and
23847 which are not. */
23848 first_attribute = attributes;
23849 /* Combine the attributes. */
23850 attributes = attr_chainon (prefix_attributes, attributes);
23851
23852 /* Create the bitfield declaration. */
23853 decl = grokbitfield (identifier
23854 ? make_id_declarator (NULL_TREE,
23855 identifier,
23856 sfk_none)
23857 : NULL,
23858 &decl_specifiers,
23859 width, initializer,
23860 attributes);
23861 }
23862 else
23863 {
23864 cp_declarator *declarator;
23865 tree asm_specification;
23866 int ctor_dtor_or_conv_p;
23867
23868 /* Parse the declarator. */
23869 declarator
23870 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23871 &ctor_dtor_or_conv_p,
23872 /*parenthesized_p=*/NULL,
23873 /*member_p=*/true,
23874 friend_p);
23875
23876 /* If something went wrong parsing the declarator, make sure
23877 that we at least consume some tokens. */
23878 if (declarator == cp_error_declarator)
23879 {
23880 /* Skip to the end of the statement. */
23881 cp_parser_skip_to_end_of_statement (parser);
23882 /* If the next token is not a semicolon, that is
23883 probably because we just skipped over the body of
23884 a function. So, we consume a semicolon if
23885 present, but do not issue an error message if it
23886 is not present. */
23887 if (cp_lexer_next_token_is (parser->lexer,
23888 CPP_SEMICOLON))
23889 cp_lexer_consume_token (parser->lexer);
23890 goto out;
23891 }
23892
23893 if (declares_class_or_enum & 2)
23894 cp_parser_check_for_definition_in_return_type
23895 (declarator, decl_specifiers.type,
23896 decl_specifiers.locations[ds_type_spec]);
23897
23898 /* Look for an asm-specification. */
23899 asm_specification = cp_parser_asm_specification_opt (parser);
23900 /* Look for attributes that apply to the declaration. */
23901 attributes = cp_parser_attributes_opt (parser);
23902 /* Remember which attributes are prefix attributes and
23903 which are not. */
23904 first_attribute = attributes;
23905 /* Combine the attributes. */
23906 attributes = attr_chainon (prefix_attributes, attributes);
23907
23908 /* If it's an `=', then we have a constant-initializer or a
23909 pure-specifier. It is not correct to parse the
23910 initializer before registering the member declaration
23911 since the member declaration should be in scope while
23912 its initializer is processed. However, the rest of the
23913 front end does not yet provide an interface that allows
23914 us to handle this correctly. */
23915 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23916 {
23917 /* In [class.mem]:
23918
23919 A pure-specifier shall be used only in the declaration of
23920 a virtual function.
23921
23922 A member-declarator can contain a constant-initializer
23923 only if it declares a static member of integral or
23924 enumeration type.
23925
23926 Therefore, if the DECLARATOR is for a function, we look
23927 for a pure-specifier; otherwise, we look for a
23928 constant-initializer. When we call `grokfield', it will
23929 perform more stringent semantics checks. */
23930 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23931 if (function_declarator_p (declarator)
23932 || (decl_specifiers.type
23933 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23934 && declarator->kind == cdk_id
23935 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23936 == FUNCTION_TYPE)))
23937 initializer = cp_parser_pure_specifier (parser);
23938 else if (decl_specifiers.storage_class != sc_static)
23939 initializer = cp_parser_save_nsdmi (parser);
23940 else if (cxx_dialect >= cxx11)
23941 {
23942 bool nonconst;
23943 /* Don't require a constant rvalue in C++11, since we
23944 might want a reference constant. We'll enforce
23945 constancy later. */
23946 cp_lexer_consume_token (parser->lexer);
23947 /* Parse the initializer. */
23948 initializer = cp_parser_initializer_clause (parser,
23949 &nonconst);
23950 }
23951 else
23952 /* Parse the initializer. */
23953 initializer = cp_parser_constant_initializer (parser);
23954 }
23955 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23956 && !function_declarator_p (declarator))
23957 {
23958 bool x;
23959 if (decl_specifiers.storage_class != sc_static)
23960 initializer = cp_parser_save_nsdmi (parser);
23961 else
23962 initializer = cp_parser_initializer (parser, &x, &x);
23963 }
23964 /* Otherwise, there is no initializer. */
23965 else
23966 initializer = NULL_TREE;
23967
23968 /* See if we are probably looking at a function
23969 definition. We are certainly not looking at a
23970 member-declarator. Calling `grokfield' has
23971 side-effects, so we must not do it unless we are sure
23972 that we are looking at a member-declarator. */
23973 if (cp_parser_token_starts_function_definition_p
23974 (cp_lexer_peek_token (parser->lexer)))
23975 {
23976 /* The grammar does not allow a pure-specifier to be
23977 used when a member function is defined. (It is
23978 possible that this fact is an oversight in the
23979 standard, since a pure function may be defined
23980 outside of the class-specifier. */
23981 if (initializer && initializer_token_start)
23982 error_at (initializer_token_start->location,
23983 "pure-specifier on function-definition");
23984 decl = cp_parser_save_member_function_body (parser,
23985 &decl_specifiers,
23986 declarator,
23987 attributes);
23988 if (parser->fully_implicit_function_template_p)
23989 decl = finish_fully_implicit_template (parser, decl);
23990 /* If the member was not a friend, declare it here. */
23991 if (!friend_p)
23992 finish_member_declaration (decl);
23993 /* Peek at the next token. */
23994 token = cp_lexer_peek_token (parser->lexer);
23995 /* If the next token is a semicolon, consume it. */
23996 if (token->type == CPP_SEMICOLON)
23997 {
23998 location_t semicolon_loc
23999 = cp_lexer_consume_token (parser->lexer)->location;
24000 gcc_rich_location richloc (semicolon_loc);
24001 richloc.add_fixit_remove ();
24002 warning_at (&richloc, OPT_Wextra_semi,
24003 "extra %<;%> after in-class "
24004 "function definition");
24005 }
24006 goto out;
24007 }
24008 else
24009 if (declarator->kind == cdk_function)
24010 declarator->id_loc = token->location;
24011 /* Create the declaration. */
24012 decl = grokfield (declarator, &decl_specifiers,
24013 initializer, /*init_const_expr_p=*/true,
24014 asm_specification, attributes);
24015 if (parser->fully_implicit_function_template_p)
24016 {
24017 if (friend_p)
24018 finish_fully_implicit_template (parser, 0);
24019 else
24020 decl = finish_fully_implicit_template (parser, decl);
24021 }
24022 }
24023
24024 cp_finalize_omp_declare_simd (parser, decl);
24025 cp_finalize_oacc_routine (parser, decl, false);
24026
24027 /* Reset PREFIX_ATTRIBUTES. */
24028 if (attributes != error_mark_node)
24029 {
24030 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24031 attributes = TREE_CHAIN (attributes);
24032 if (attributes)
24033 TREE_CHAIN (attributes) = NULL_TREE;
24034 }
24035
24036 /* If there is any qualification still in effect, clear it
24037 now; we will be starting fresh with the next declarator. */
24038 parser->scope = NULL_TREE;
24039 parser->qualifying_scope = NULL_TREE;
24040 parser->object_scope = NULL_TREE;
24041 /* If it's a `,', then there are more declarators. */
24042 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24043 {
24044 cp_lexer_consume_token (parser->lexer);
24045 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24046 {
24047 cp_token *token = cp_lexer_previous_token (parser->lexer);
24048 gcc_rich_location richloc (token->location);
24049 richloc.add_fixit_remove ();
24050 error_at (&richloc, "stray %<,%> at end of "
24051 "member declaration");
24052 }
24053 }
24054 /* If the next token isn't a `;', then we have a parse error. */
24055 else if (cp_lexer_next_token_is_not (parser->lexer,
24056 CPP_SEMICOLON))
24057 {
24058 /* The next token might be a ways away from where the
24059 actual semicolon is missing. Find the previous token
24060 and use that for our error position. */
24061 cp_token *token = cp_lexer_previous_token (parser->lexer);
24062 gcc_rich_location richloc (token->location);
24063 richloc.add_fixit_insert_after (";");
24064 error_at (&richloc, "expected %<;%> at end of "
24065 "member declaration");
24066
24067 /* Assume that the user meant to provide a semicolon. If
24068 we were to cp_parser_skip_to_end_of_statement, we might
24069 skip to a semicolon inside a member function definition
24070 and issue nonsensical error messages. */
24071 assume_semicolon = true;
24072 }
24073
24074 if (decl)
24075 {
24076 /* Add DECL to the list of members. */
24077 if (!friend_p
24078 /* Explicitly include, eg, NSDMIs, for better error
24079 recovery (c++/58650). */
24080 || !DECL_DECLARES_FUNCTION_P (decl))
24081 finish_member_declaration (decl);
24082
24083 if (TREE_CODE (decl) == FUNCTION_DECL)
24084 cp_parser_save_default_args (parser, decl);
24085 else if (TREE_CODE (decl) == FIELD_DECL
24086 && DECL_INITIAL (decl))
24087 /* Add DECL to the queue of NSDMI to be parsed later. */
24088 vec_safe_push (unparsed_nsdmis, decl);
24089 }
24090
24091 if (assume_semicolon)
24092 goto out;
24093 }
24094 }
24095
24096 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24097 out:
24098 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24099 }
24100
24101 /* Parse a pure-specifier.
24102
24103 pure-specifier:
24104 = 0
24105
24106 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24107 Otherwise, ERROR_MARK_NODE is returned. */
24108
24109 static tree
24110 cp_parser_pure_specifier (cp_parser* parser)
24111 {
24112 cp_token *token;
24113
24114 /* Look for the `=' token. */
24115 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24116 return error_mark_node;
24117 /* Look for the `0' token. */
24118 token = cp_lexer_peek_token (parser->lexer);
24119
24120 if (token->type == CPP_EOF
24121 || token->type == CPP_PRAGMA_EOL)
24122 return error_mark_node;
24123
24124 cp_lexer_consume_token (parser->lexer);
24125
24126 /* Accept = default or = delete in c++0x mode. */
24127 if (token->keyword == RID_DEFAULT
24128 || token->keyword == RID_DELETE)
24129 {
24130 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24131 return token->u.value;
24132 }
24133
24134 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24135 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24136 {
24137 cp_parser_error (parser,
24138 "invalid pure specifier (only %<= 0%> is allowed)");
24139 cp_parser_skip_to_end_of_statement (parser);
24140 return error_mark_node;
24141 }
24142 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24143 {
24144 error_at (token->location, "templates may not be %<virtual%>");
24145 return error_mark_node;
24146 }
24147
24148 return integer_zero_node;
24149 }
24150
24151 /* Parse a constant-initializer.
24152
24153 constant-initializer:
24154 = constant-expression
24155
24156 Returns a representation of the constant-expression. */
24157
24158 static tree
24159 cp_parser_constant_initializer (cp_parser* parser)
24160 {
24161 /* Look for the `=' token. */
24162 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24163 return error_mark_node;
24164
24165 /* It is invalid to write:
24166
24167 struct S { static const int i = { 7 }; };
24168
24169 */
24170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24171 {
24172 cp_parser_error (parser,
24173 "a brace-enclosed initializer is not allowed here");
24174 /* Consume the opening brace. */
24175 matching_braces braces;
24176 braces.consume_open (parser);
24177 /* Skip the initializer. */
24178 cp_parser_skip_to_closing_brace (parser);
24179 /* Look for the trailing `}'. */
24180 braces.require_close (parser);
24181
24182 return error_mark_node;
24183 }
24184
24185 return cp_parser_constant_expression (parser);
24186 }
24187
24188 /* Derived classes [gram.class.derived] */
24189
24190 /* Parse a base-clause.
24191
24192 base-clause:
24193 : base-specifier-list
24194
24195 base-specifier-list:
24196 base-specifier ... [opt]
24197 base-specifier-list , base-specifier ... [opt]
24198
24199 Returns a TREE_LIST representing the base-classes, in the order in
24200 which they were declared. The representation of each node is as
24201 described by cp_parser_base_specifier.
24202
24203 In the case that no bases are specified, this function will return
24204 NULL_TREE, not ERROR_MARK_NODE. */
24205
24206 static tree
24207 cp_parser_base_clause (cp_parser* parser)
24208 {
24209 tree bases = NULL_TREE;
24210
24211 /* Look for the `:' that begins the list. */
24212 cp_parser_require (parser, CPP_COLON, RT_COLON);
24213
24214 /* Scan the base-specifier-list. */
24215 while (true)
24216 {
24217 cp_token *token;
24218 tree base;
24219 bool pack_expansion_p = false;
24220
24221 /* Look for the base-specifier. */
24222 base = cp_parser_base_specifier (parser);
24223 /* Look for the (optional) ellipsis. */
24224 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24225 {
24226 /* Consume the `...'. */
24227 cp_lexer_consume_token (parser->lexer);
24228
24229 pack_expansion_p = true;
24230 }
24231
24232 /* Add BASE to the front of the list. */
24233 if (base && base != error_mark_node)
24234 {
24235 if (pack_expansion_p)
24236 /* Make this a pack expansion type. */
24237 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24238
24239 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24240 {
24241 TREE_CHAIN (base) = bases;
24242 bases = base;
24243 }
24244 }
24245 /* Peek at the next token. */
24246 token = cp_lexer_peek_token (parser->lexer);
24247 /* If it's not a comma, then the list is complete. */
24248 if (token->type != CPP_COMMA)
24249 break;
24250 /* Consume the `,'. */
24251 cp_lexer_consume_token (parser->lexer);
24252 }
24253
24254 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24255 base class had a qualified name. However, the next name that
24256 appears is certainly not qualified. */
24257 parser->scope = NULL_TREE;
24258 parser->qualifying_scope = NULL_TREE;
24259 parser->object_scope = NULL_TREE;
24260
24261 return nreverse (bases);
24262 }
24263
24264 /* Parse a base-specifier.
24265
24266 base-specifier:
24267 :: [opt] nested-name-specifier [opt] class-name
24268 virtual access-specifier [opt] :: [opt] nested-name-specifier
24269 [opt] class-name
24270 access-specifier virtual [opt] :: [opt] nested-name-specifier
24271 [opt] class-name
24272
24273 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24274 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24275 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24276 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24277
24278 static tree
24279 cp_parser_base_specifier (cp_parser* parser)
24280 {
24281 cp_token *token;
24282 bool done = false;
24283 bool virtual_p = false;
24284 bool duplicate_virtual_error_issued_p = false;
24285 bool duplicate_access_error_issued_p = false;
24286 bool class_scope_p, template_p;
24287 tree access = access_default_node;
24288 tree type;
24289
24290 /* Process the optional `virtual' and `access-specifier'. */
24291 while (!done)
24292 {
24293 /* Peek at the next token. */
24294 token = cp_lexer_peek_token (parser->lexer);
24295 /* Process `virtual'. */
24296 switch (token->keyword)
24297 {
24298 case RID_VIRTUAL:
24299 /* If `virtual' appears more than once, issue an error. */
24300 if (virtual_p && !duplicate_virtual_error_issued_p)
24301 {
24302 cp_parser_error (parser,
24303 "%<virtual%> specified more than once in base-specifier");
24304 duplicate_virtual_error_issued_p = true;
24305 }
24306
24307 virtual_p = true;
24308
24309 /* Consume the `virtual' token. */
24310 cp_lexer_consume_token (parser->lexer);
24311
24312 break;
24313
24314 case RID_PUBLIC:
24315 case RID_PROTECTED:
24316 case RID_PRIVATE:
24317 /* If more than one access specifier appears, issue an
24318 error. */
24319 if (access != access_default_node
24320 && !duplicate_access_error_issued_p)
24321 {
24322 cp_parser_error (parser,
24323 "more than one access specifier in base-specifier");
24324 duplicate_access_error_issued_p = true;
24325 }
24326
24327 access = ridpointers[(int) token->keyword];
24328
24329 /* Consume the access-specifier. */
24330 cp_lexer_consume_token (parser->lexer);
24331
24332 break;
24333
24334 default:
24335 done = true;
24336 break;
24337 }
24338 }
24339 /* It is not uncommon to see programs mechanically, erroneously, use
24340 the 'typename' keyword to denote (dependent) qualified types
24341 as base classes. */
24342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24343 {
24344 token = cp_lexer_peek_token (parser->lexer);
24345 if (!processing_template_decl)
24346 error_at (token->location,
24347 "keyword %<typename%> not allowed outside of templates");
24348 else
24349 error_at (token->location,
24350 "keyword %<typename%> not allowed in this context "
24351 "(the base class is implicitly a type)");
24352 cp_lexer_consume_token (parser->lexer);
24353 }
24354
24355 /* Look for the optional `::' operator. */
24356 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24357 /* Look for the nested-name-specifier. The simplest way to
24358 implement:
24359
24360 [temp.res]
24361
24362 The keyword `typename' is not permitted in a base-specifier or
24363 mem-initializer; in these contexts a qualified name that
24364 depends on a template-parameter is implicitly assumed to be a
24365 type name.
24366
24367 is to pretend that we have seen the `typename' keyword at this
24368 point. */
24369 cp_parser_nested_name_specifier_opt (parser,
24370 /*typename_keyword_p=*/true,
24371 /*check_dependency_p=*/true,
24372 /*type_p=*/true,
24373 /*is_declaration=*/true);
24374 /* If the base class is given by a qualified name, assume that names
24375 we see are type names or templates, as appropriate. */
24376 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24377 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24378
24379 if (!parser->scope
24380 && cp_lexer_next_token_is_decltype (parser->lexer))
24381 /* DR 950 allows decltype as a base-specifier. */
24382 type = cp_parser_decltype (parser);
24383 else
24384 {
24385 /* Otherwise, look for the class-name. */
24386 type = cp_parser_class_name (parser,
24387 class_scope_p,
24388 template_p,
24389 typename_type,
24390 /*check_dependency_p=*/true,
24391 /*class_head_p=*/false,
24392 /*is_declaration=*/true);
24393 type = TREE_TYPE (type);
24394 }
24395
24396 if (type == error_mark_node)
24397 return error_mark_node;
24398
24399 return finish_base_specifier (type, access, virtual_p);
24400 }
24401
24402 /* Exception handling [gram.exception] */
24403
24404 /* Parse an (optional) noexcept-specification.
24405
24406 noexcept-specification:
24407 noexcept ( constant-expression ) [opt]
24408
24409 If no noexcept-specification is present, returns NULL_TREE.
24410 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24411 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24412 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24413 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24414 in which case a boolean condition is returned instead. */
24415
24416 static tree
24417 cp_parser_noexcept_specification_opt (cp_parser* parser,
24418 bool require_constexpr,
24419 bool* consumed_expr,
24420 bool return_cond)
24421 {
24422 cp_token *token;
24423 const char *saved_message;
24424
24425 /* Peek at the next token. */
24426 token = cp_lexer_peek_token (parser->lexer);
24427
24428 /* Is it a noexcept-specification? */
24429 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24430 {
24431 tree expr;
24432 cp_lexer_consume_token (parser->lexer);
24433
24434 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24435 {
24436 matching_parens parens;
24437 parens.consume_open (parser);
24438
24439 if (require_constexpr)
24440 {
24441 /* Types may not be defined in an exception-specification. */
24442 saved_message = parser->type_definition_forbidden_message;
24443 parser->type_definition_forbidden_message
24444 = G_("types may not be defined in an exception-specification");
24445
24446 expr = cp_parser_constant_expression (parser);
24447
24448 /* Restore the saved message. */
24449 parser->type_definition_forbidden_message = saved_message;
24450 }
24451 else
24452 {
24453 expr = cp_parser_expression (parser);
24454 *consumed_expr = true;
24455 }
24456
24457 parens.require_close (parser);
24458 }
24459 else
24460 {
24461 expr = boolean_true_node;
24462 if (!require_constexpr)
24463 *consumed_expr = false;
24464 }
24465
24466 /* We cannot build a noexcept-spec right away because this will check
24467 that expr is a constexpr. */
24468 if (!return_cond)
24469 return build_noexcept_spec (expr, tf_warning_or_error);
24470 else
24471 return expr;
24472 }
24473 else
24474 return NULL_TREE;
24475 }
24476
24477 /* Parse an (optional) exception-specification.
24478
24479 exception-specification:
24480 throw ( type-id-list [opt] )
24481
24482 Returns a TREE_LIST representing the exception-specification. The
24483 TREE_VALUE of each node is a type. */
24484
24485 static tree
24486 cp_parser_exception_specification_opt (cp_parser* parser)
24487 {
24488 cp_token *token;
24489 tree type_id_list;
24490 const char *saved_message;
24491
24492 /* Peek at the next token. */
24493 token = cp_lexer_peek_token (parser->lexer);
24494
24495 /* Is it a noexcept-specification? */
24496 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24497 false);
24498 if (type_id_list != NULL_TREE)
24499 return type_id_list;
24500
24501 /* If it's not `throw', then there's no exception-specification. */
24502 if (!cp_parser_is_keyword (token, RID_THROW))
24503 return NULL_TREE;
24504
24505 location_t loc = token->location;
24506
24507 /* Consume the `throw'. */
24508 cp_lexer_consume_token (parser->lexer);
24509
24510 /* Look for the `('. */
24511 matching_parens parens;
24512 parens.require_open (parser);
24513
24514 /* Peek at the next token. */
24515 token = cp_lexer_peek_token (parser->lexer);
24516 /* If it's not a `)', then there is a type-id-list. */
24517 if (token->type != CPP_CLOSE_PAREN)
24518 {
24519 /* Types may not be defined in an exception-specification. */
24520 saved_message = parser->type_definition_forbidden_message;
24521 parser->type_definition_forbidden_message
24522 = G_("types may not be defined in an exception-specification");
24523 /* Parse the type-id-list. */
24524 type_id_list = cp_parser_type_id_list (parser);
24525 /* Restore the saved message. */
24526 parser->type_definition_forbidden_message = saved_message;
24527
24528 if (cxx_dialect >= cxx17)
24529 {
24530 error_at (loc, "ISO C++17 does not allow dynamic exception "
24531 "specifications");
24532 type_id_list = NULL_TREE;
24533 }
24534 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24535 warning_at (loc, OPT_Wdeprecated,
24536 "dynamic exception specifications are deprecated in "
24537 "C++11");
24538 }
24539 /* In C++17, throw() is equivalent to noexcept (true). throw()
24540 is deprecated in C++11 and above as well, but is still widely used,
24541 so don't warn about it yet. */
24542 else if (cxx_dialect >= cxx17)
24543 type_id_list = noexcept_true_spec;
24544 else
24545 type_id_list = empty_except_spec;
24546
24547 /* Look for the `)'. */
24548 parens.require_close (parser);
24549
24550 return type_id_list;
24551 }
24552
24553 /* Parse an (optional) type-id-list.
24554
24555 type-id-list:
24556 type-id ... [opt]
24557 type-id-list , type-id ... [opt]
24558
24559 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24560 in the order that the types were presented. */
24561
24562 static tree
24563 cp_parser_type_id_list (cp_parser* parser)
24564 {
24565 tree types = NULL_TREE;
24566
24567 while (true)
24568 {
24569 cp_token *token;
24570 tree type;
24571
24572 token = cp_lexer_peek_token (parser->lexer);
24573
24574 /* Get the next type-id. */
24575 type = cp_parser_type_id (parser);
24576 /* Check for invalid 'auto'. */
24577 if (flag_concepts && type_uses_auto (type))
24578 {
24579 error_at (token->location,
24580 "invalid use of %<auto%> in exception-specification");
24581 type = error_mark_node;
24582 }
24583 /* Parse the optional ellipsis. */
24584 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24585 {
24586 /* Consume the `...'. */
24587 cp_lexer_consume_token (parser->lexer);
24588
24589 /* Turn the type into a pack expansion expression. */
24590 type = make_pack_expansion (type);
24591 }
24592 /* Add it to the list. */
24593 types = add_exception_specifier (types, type, /*complain=*/1);
24594 /* Peek at the next token. */
24595 token = cp_lexer_peek_token (parser->lexer);
24596 /* If it is not a `,', we are done. */
24597 if (token->type != CPP_COMMA)
24598 break;
24599 /* Consume the `,'. */
24600 cp_lexer_consume_token (parser->lexer);
24601 }
24602
24603 return nreverse (types);
24604 }
24605
24606 /* Parse a try-block.
24607
24608 try-block:
24609 try compound-statement handler-seq */
24610
24611 static tree
24612 cp_parser_try_block (cp_parser* parser)
24613 {
24614 tree try_block;
24615
24616 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24617 if (parser->in_function_body
24618 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24619 error ("%<try%> in %<constexpr%> function");
24620
24621 try_block = begin_try_block ();
24622 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24623 finish_try_block (try_block);
24624 cp_parser_handler_seq (parser);
24625 finish_handler_sequence (try_block);
24626
24627 return try_block;
24628 }
24629
24630 /* Parse a function-try-block.
24631
24632 function-try-block:
24633 try ctor-initializer [opt] function-body handler-seq */
24634
24635 static void
24636 cp_parser_function_try_block (cp_parser* parser)
24637 {
24638 tree compound_stmt;
24639 tree try_block;
24640
24641 /* Look for the `try' keyword. */
24642 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24643 return;
24644 /* Let the rest of the front end know where we are. */
24645 try_block = begin_function_try_block (&compound_stmt);
24646 /* Parse the function-body. */
24647 cp_parser_ctor_initializer_opt_and_function_body
24648 (parser, /*in_function_try_block=*/true);
24649 /* We're done with the `try' part. */
24650 finish_function_try_block (try_block);
24651 /* Parse the handlers. */
24652 cp_parser_handler_seq (parser);
24653 /* We're done with the handlers. */
24654 finish_function_handler_sequence (try_block, compound_stmt);
24655 }
24656
24657 /* Parse a handler-seq.
24658
24659 handler-seq:
24660 handler handler-seq [opt] */
24661
24662 static void
24663 cp_parser_handler_seq (cp_parser* parser)
24664 {
24665 while (true)
24666 {
24667 cp_token *token;
24668
24669 /* Parse the handler. */
24670 cp_parser_handler (parser);
24671 /* Peek at the next token. */
24672 token = cp_lexer_peek_token (parser->lexer);
24673 /* If it's not `catch' then there are no more handlers. */
24674 if (!cp_parser_is_keyword (token, RID_CATCH))
24675 break;
24676 }
24677 }
24678
24679 /* Parse a handler.
24680
24681 handler:
24682 catch ( exception-declaration ) compound-statement */
24683
24684 static void
24685 cp_parser_handler (cp_parser* parser)
24686 {
24687 tree handler;
24688 tree declaration;
24689
24690 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24691 handler = begin_handler ();
24692 matching_parens parens;
24693 parens.require_open (parser);
24694 declaration = cp_parser_exception_declaration (parser);
24695 finish_handler_parms (declaration, handler);
24696 parens.require_close (parser);
24697 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24698 finish_handler (handler);
24699 }
24700
24701 /* Parse an exception-declaration.
24702
24703 exception-declaration:
24704 type-specifier-seq declarator
24705 type-specifier-seq abstract-declarator
24706 type-specifier-seq
24707 ...
24708
24709 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24710 ellipsis variant is used. */
24711
24712 static tree
24713 cp_parser_exception_declaration (cp_parser* parser)
24714 {
24715 cp_decl_specifier_seq type_specifiers;
24716 cp_declarator *declarator;
24717 const char *saved_message;
24718
24719 /* If it's an ellipsis, it's easy to handle. */
24720 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24721 {
24722 /* Consume the `...' token. */
24723 cp_lexer_consume_token (parser->lexer);
24724 return NULL_TREE;
24725 }
24726
24727 /* Types may not be defined in exception-declarations. */
24728 saved_message = parser->type_definition_forbidden_message;
24729 parser->type_definition_forbidden_message
24730 = G_("types may not be defined in exception-declarations");
24731
24732 /* Parse the type-specifier-seq. */
24733 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24734 /*is_trailing_return=*/false,
24735 &type_specifiers);
24736 /* If it's a `)', then there is no declarator. */
24737 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24738 declarator = NULL;
24739 else
24740 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24741 /*ctor_dtor_or_conv_p=*/NULL,
24742 /*parenthesized_p=*/NULL,
24743 /*member_p=*/false,
24744 /*friend_p=*/false);
24745
24746 /* Restore the saved message. */
24747 parser->type_definition_forbidden_message = saved_message;
24748
24749 if (!type_specifiers.any_specifiers_p)
24750 return error_mark_node;
24751
24752 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24753 }
24754
24755 /* Parse a throw-expression.
24756
24757 throw-expression:
24758 throw assignment-expression [opt]
24759
24760 Returns a THROW_EXPR representing the throw-expression. */
24761
24762 static tree
24763 cp_parser_throw_expression (cp_parser* parser)
24764 {
24765 tree expression;
24766 cp_token* token;
24767
24768 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24769 token = cp_lexer_peek_token (parser->lexer);
24770 /* Figure out whether or not there is an assignment-expression
24771 following the "throw" keyword. */
24772 if (token->type == CPP_COMMA
24773 || token->type == CPP_SEMICOLON
24774 || token->type == CPP_CLOSE_PAREN
24775 || token->type == CPP_CLOSE_SQUARE
24776 || token->type == CPP_CLOSE_BRACE
24777 || token->type == CPP_COLON)
24778 expression = NULL_TREE;
24779 else
24780 expression = cp_parser_assignment_expression (parser);
24781
24782 return build_throw (expression);
24783 }
24784
24785 /* GNU Extensions */
24786
24787 /* Parse an (optional) asm-specification.
24788
24789 asm-specification:
24790 asm ( string-literal )
24791
24792 If the asm-specification is present, returns a STRING_CST
24793 corresponding to the string-literal. Otherwise, returns
24794 NULL_TREE. */
24795
24796 static tree
24797 cp_parser_asm_specification_opt (cp_parser* parser)
24798 {
24799 cp_token *token;
24800 tree asm_specification;
24801
24802 /* Peek at the next token. */
24803 token = cp_lexer_peek_token (parser->lexer);
24804 /* If the next token isn't the `asm' keyword, then there's no
24805 asm-specification. */
24806 if (!cp_parser_is_keyword (token, RID_ASM))
24807 return NULL_TREE;
24808
24809 /* Consume the `asm' token. */
24810 cp_lexer_consume_token (parser->lexer);
24811 /* Look for the `('. */
24812 matching_parens parens;
24813 parens.require_open (parser);
24814
24815 /* Look for the string-literal. */
24816 asm_specification = cp_parser_string_literal (parser, false, false);
24817
24818 /* Look for the `)'. */
24819 parens.require_close (parser);
24820
24821 return asm_specification;
24822 }
24823
24824 /* Parse an asm-operand-list.
24825
24826 asm-operand-list:
24827 asm-operand
24828 asm-operand-list , asm-operand
24829
24830 asm-operand:
24831 string-literal ( expression )
24832 [ string-literal ] string-literal ( expression )
24833
24834 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24835 each node is the expression. The TREE_PURPOSE is itself a
24836 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24837 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24838 is a STRING_CST for the string literal before the parenthesis. Returns
24839 ERROR_MARK_NODE if any of the operands are invalid. */
24840
24841 static tree
24842 cp_parser_asm_operand_list (cp_parser* parser)
24843 {
24844 tree asm_operands = NULL_TREE;
24845 bool invalid_operands = false;
24846
24847 while (true)
24848 {
24849 tree string_literal;
24850 tree expression;
24851 tree name;
24852
24853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24854 {
24855 /* Consume the `[' token. */
24856 cp_lexer_consume_token (parser->lexer);
24857 /* Read the operand name. */
24858 name = cp_parser_identifier (parser);
24859 if (name != error_mark_node)
24860 name = build_string (IDENTIFIER_LENGTH (name),
24861 IDENTIFIER_POINTER (name));
24862 /* Look for the closing `]'. */
24863 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24864 }
24865 else
24866 name = NULL_TREE;
24867 /* Look for the string-literal. */
24868 string_literal = cp_parser_string_literal (parser, false, false);
24869
24870 /* Look for the `('. */
24871 matching_parens parens;
24872 parens.require_open (parser);
24873 /* Parse the expression. */
24874 expression = cp_parser_expression (parser);
24875 /* Look for the `)'. */
24876 parens.require_close (parser);
24877
24878 if (name == error_mark_node
24879 || string_literal == error_mark_node
24880 || expression == error_mark_node)
24881 invalid_operands = true;
24882
24883 /* Add this operand to the list. */
24884 asm_operands = tree_cons (build_tree_list (name, string_literal),
24885 expression,
24886 asm_operands);
24887 /* If the next token is not a `,', there are no more
24888 operands. */
24889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24890 break;
24891 /* Consume the `,'. */
24892 cp_lexer_consume_token (parser->lexer);
24893 }
24894
24895 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24896 }
24897
24898 /* Parse an asm-clobber-list.
24899
24900 asm-clobber-list:
24901 string-literal
24902 asm-clobber-list , string-literal
24903
24904 Returns a TREE_LIST, indicating the clobbers in the order that they
24905 appeared. The TREE_VALUE of each node is a STRING_CST. */
24906
24907 static tree
24908 cp_parser_asm_clobber_list (cp_parser* parser)
24909 {
24910 tree clobbers = NULL_TREE;
24911
24912 while (true)
24913 {
24914 tree string_literal;
24915
24916 /* Look for the string literal. */
24917 string_literal = cp_parser_string_literal (parser, false, false);
24918 /* Add it to the list. */
24919 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24920 /* If the next token is not a `,', then the list is
24921 complete. */
24922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24923 break;
24924 /* Consume the `,' token. */
24925 cp_lexer_consume_token (parser->lexer);
24926 }
24927
24928 return clobbers;
24929 }
24930
24931 /* Parse an asm-label-list.
24932
24933 asm-label-list:
24934 identifier
24935 asm-label-list , identifier
24936
24937 Returns a TREE_LIST, indicating the labels in the order that they
24938 appeared. The TREE_VALUE of each node is a label. */
24939
24940 static tree
24941 cp_parser_asm_label_list (cp_parser* parser)
24942 {
24943 tree labels = NULL_TREE;
24944
24945 while (true)
24946 {
24947 tree identifier, label, name;
24948
24949 /* Look for the identifier. */
24950 identifier = cp_parser_identifier (parser);
24951 if (!error_operand_p (identifier))
24952 {
24953 label = lookup_label (identifier);
24954 if (TREE_CODE (label) == LABEL_DECL)
24955 {
24956 TREE_USED (label) = 1;
24957 check_goto (label);
24958 name = build_string (IDENTIFIER_LENGTH (identifier),
24959 IDENTIFIER_POINTER (identifier));
24960 labels = tree_cons (name, label, labels);
24961 }
24962 }
24963 /* If the next token is not a `,', then the list is
24964 complete. */
24965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24966 break;
24967 /* Consume the `,' token. */
24968 cp_lexer_consume_token (parser->lexer);
24969 }
24970
24971 return nreverse (labels);
24972 }
24973
24974 /* Return TRUE iff the next tokens in the stream are possibly the
24975 beginning of a GNU extension attribute. */
24976
24977 static bool
24978 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24979 {
24980 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24981 }
24982
24983 /* Return TRUE iff the next tokens in the stream are possibly the
24984 beginning of a standard C++-11 attribute specifier. */
24985
24986 static bool
24987 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24988 {
24989 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24990 }
24991
24992 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24993 beginning of a standard C++-11 attribute specifier. */
24994
24995 static bool
24996 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24997 {
24998 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24999
25000 return (cxx_dialect >= cxx11
25001 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25002 || (token->type == CPP_OPEN_SQUARE
25003 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25004 && token->type == CPP_OPEN_SQUARE)));
25005 }
25006
25007 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25008 beginning of a GNU extension attribute. */
25009
25010 static bool
25011 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25012 {
25013 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25014
25015 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25016 }
25017
25018 /* Return true iff the next tokens can be the beginning of either a
25019 GNU attribute list, or a standard C++11 attribute sequence. */
25020
25021 static bool
25022 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25023 {
25024 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25025 || cp_next_tokens_can_be_std_attribute_p (parser));
25026 }
25027
25028 /* Return true iff the next Nth tokens can be the beginning of either
25029 a GNU attribute list, or a standard C++11 attribute sequence. */
25030
25031 static bool
25032 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25033 {
25034 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25035 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25036 }
25037
25038 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25039 of GNU attributes, or return NULL. */
25040
25041 static tree
25042 cp_parser_attributes_opt (cp_parser *parser)
25043 {
25044 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25045 return cp_parser_gnu_attributes_opt (parser);
25046 return cp_parser_std_attribute_spec_seq (parser);
25047 }
25048
25049 /* Parse an (optional) series of attributes.
25050
25051 attributes:
25052 attributes attribute
25053
25054 attribute:
25055 __attribute__ (( attribute-list [opt] ))
25056
25057 The return value is as for cp_parser_gnu_attribute_list. */
25058
25059 static tree
25060 cp_parser_gnu_attributes_opt (cp_parser* parser)
25061 {
25062 tree attributes = NULL_TREE;
25063
25064 temp_override<bool> cleanup
25065 (parser->auto_is_implicit_function_template_parm_p, false);
25066
25067 while (true)
25068 {
25069 cp_token *token;
25070 tree attribute_list;
25071 bool ok = true;
25072
25073 /* Peek at the next token. */
25074 token = cp_lexer_peek_token (parser->lexer);
25075 /* If it's not `__attribute__', then we're done. */
25076 if (token->keyword != RID_ATTRIBUTE)
25077 break;
25078
25079 /* Consume the `__attribute__' keyword. */
25080 cp_lexer_consume_token (parser->lexer);
25081 /* Look for the two `(' tokens. */
25082 matching_parens outer_parens;
25083 outer_parens.require_open (parser);
25084 matching_parens inner_parens;
25085 inner_parens.require_open (parser);
25086
25087 /* Peek at the next token. */
25088 token = cp_lexer_peek_token (parser->lexer);
25089 if (token->type != CPP_CLOSE_PAREN)
25090 /* Parse the attribute-list. */
25091 attribute_list = cp_parser_gnu_attribute_list (parser);
25092 else
25093 /* If the next token is a `)', then there is no attribute
25094 list. */
25095 attribute_list = NULL;
25096
25097 /* Look for the two `)' tokens. */
25098 if (!inner_parens.require_close (parser))
25099 ok = false;
25100 if (!outer_parens.require_close (parser))
25101 ok = false;
25102 if (!ok)
25103 cp_parser_skip_to_end_of_statement (parser);
25104
25105 /* Add these new attributes to the list. */
25106 attributes = attr_chainon (attributes, attribute_list);
25107 }
25108
25109 return attributes;
25110 }
25111
25112 /* Parse a GNU attribute-list.
25113
25114 attribute-list:
25115 attribute
25116 attribute-list , attribute
25117
25118 attribute:
25119 identifier
25120 identifier ( identifier )
25121 identifier ( identifier , expression-list )
25122 identifier ( expression-list )
25123
25124 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25125 to an attribute. The TREE_PURPOSE of each node is the identifier
25126 indicating which attribute is in use. The TREE_VALUE represents
25127 the arguments, if any. */
25128
25129 static tree
25130 cp_parser_gnu_attribute_list (cp_parser* parser)
25131 {
25132 tree attribute_list = NULL_TREE;
25133 bool save_translate_strings_p = parser->translate_strings_p;
25134
25135 parser->translate_strings_p = false;
25136 while (true)
25137 {
25138 cp_token *token;
25139 tree identifier;
25140 tree attribute;
25141
25142 /* Look for the identifier. We also allow keywords here; for
25143 example `__attribute__ ((const))' is legal. */
25144 token = cp_lexer_peek_token (parser->lexer);
25145 if (token->type == CPP_NAME
25146 || token->type == CPP_KEYWORD)
25147 {
25148 tree arguments = NULL_TREE;
25149
25150 /* Consume the token, but save it since we need it for the
25151 SIMD enabled function parsing. */
25152 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25153
25154 /* Save away the identifier that indicates which attribute
25155 this is. */
25156 identifier = (token->type == CPP_KEYWORD)
25157 /* For keywords, use the canonical spelling, not the
25158 parsed identifier. */
25159 ? ridpointers[(int) token->keyword]
25160 : id_token->u.value;
25161
25162 identifier = canonicalize_attr_name (identifier);
25163 attribute = build_tree_list (identifier, NULL_TREE);
25164
25165 /* Peek at the next token. */
25166 token = cp_lexer_peek_token (parser->lexer);
25167 /* If it's an `(', then parse the attribute arguments. */
25168 if (token->type == CPP_OPEN_PAREN)
25169 {
25170 vec<tree, va_gc> *vec;
25171 int attr_flag = (attribute_takes_identifier_p (identifier)
25172 ? id_attr : normal_attr);
25173 vec = cp_parser_parenthesized_expression_list
25174 (parser, attr_flag, /*cast_p=*/false,
25175 /*allow_expansion_p=*/false,
25176 /*non_constant_p=*/NULL);
25177 if (vec == NULL)
25178 arguments = error_mark_node;
25179 else
25180 {
25181 arguments = build_tree_list_vec (vec);
25182 release_tree_vector (vec);
25183 }
25184 /* Save the arguments away. */
25185 TREE_VALUE (attribute) = arguments;
25186 }
25187
25188 if (arguments != error_mark_node)
25189 {
25190 /* Add this attribute to the list. */
25191 TREE_CHAIN (attribute) = attribute_list;
25192 attribute_list = attribute;
25193 }
25194
25195 token = cp_lexer_peek_token (parser->lexer);
25196 }
25197 /* Now, look for more attributes. If the next token isn't a
25198 `,', we're done. */
25199 if (token->type != CPP_COMMA)
25200 break;
25201
25202 /* Consume the comma and keep going. */
25203 cp_lexer_consume_token (parser->lexer);
25204 }
25205 parser->translate_strings_p = save_translate_strings_p;
25206
25207 /* We built up the list in reverse order. */
25208 return nreverse (attribute_list);
25209 }
25210
25211 /* Parse a standard C++11 attribute.
25212
25213 The returned representation is a TREE_LIST which TREE_PURPOSE is
25214 the scoped name of the attribute, and the TREE_VALUE is its
25215 arguments list.
25216
25217 Note that the scoped name of the attribute is itself a TREE_LIST
25218 which TREE_PURPOSE is the namespace of the attribute, and
25219 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25220 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25221 and which TREE_PURPOSE is directly the attribute name.
25222
25223 Clients of the attribute code should use get_attribute_namespace
25224 and get_attribute_name to get the actual namespace and name of
25225 attributes, regardless of their being GNU or C++11 attributes.
25226
25227 attribute:
25228 attribute-token attribute-argument-clause [opt]
25229
25230 attribute-token:
25231 identifier
25232 attribute-scoped-token
25233
25234 attribute-scoped-token:
25235 attribute-namespace :: identifier
25236
25237 attribute-namespace:
25238 identifier
25239
25240 attribute-argument-clause:
25241 ( balanced-token-seq )
25242
25243 balanced-token-seq:
25244 balanced-token [opt]
25245 balanced-token-seq balanced-token
25246
25247 balanced-token:
25248 ( balanced-token-seq )
25249 [ balanced-token-seq ]
25250 { balanced-token-seq }. */
25251
25252 static tree
25253 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25254 {
25255 tree attribute, attr_id = NULL_TREE, arguments;
25256 cp_token *token;
25257
25258 temp_override<bool> cleanup
25259 (parser->auto_is_implicit_function_template_parm_p, false);
25260
25261 /* First, parse name of the attribute, a.k.a attribute-token. */
25262
25263 token = cp_lexer_peek_token (parser->lexer);
25264 if (token->type == CPP_NAME)
25265 attr_id = token->u.value;
25266 else if (token->type == CPP_KEYWORD)
25267 attr_id = ridpointers[(int) token->keyword];
25268 else if (token->flags & NAMED_OP)
25269 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25270
25271 if (attr_id == NULL_TREE)
25272 return NULL_TREE;
25273
25274 cp_lexer_consume_token (parser->lexer);
25275
25276 token = cp_lexer_peek_token (parser->lexer);
25277 if (token->type == CPP_SCOPE)
25278 {
25279 /* We are seeing a scoped attribute token. */
25280
25281 cp_lexer_consume_token (parser->lexer);
25282 if (attr_ns)
25283 error_at (token->location, "attribute using prefix used together "
25284 "with scoped attribute token");
25285 attr_ns = attr_id;
25286
25287 token = cp_lexer_consume_token (parser->lexer);
25288 if (token->type == CPP_NAME)
25289 attr_id = token->u.value;
25290 else if (token->type == CPP_KEYWORD)
25291 attr_id = ridpointers[(int) token->keyword];
25292 else if (token->flags & NAMED_OP)
25293 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25294 else
25295 {
25296 error_at (token->location,
25297 "expected an identifier for the attribute name");
25298 return error_mark_node;
25299 }
25300
25301 attr_id = canonicalize_attr_name (attr_id);
25302 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25303 NULL_TREE);
25304 token = cp_lexer_peek_token (parser->lexer);
25305 }
25306 else if (attr_ns)
25307 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25308 NULL_TREE);
25309 else
25310 {
25311 attr_id = canonicalize_attr_name (attr_id);
25312 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25313 NULL_TREE);
25314 /* C++11 noreturn attribute is equivalent to GNU's. */
25315 if (is_attribute_p ("noreturn", attr_id))
25316 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25317 /* C++14 deprecated attribute is equivalent to GNU's. */
25318 else if (is_attribute_p ("deprecated", attr_id))
25319 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25320 /* C++17 fallthrough attribute is equivalent to GNU's. */
25321 else if (is_attribute_p ("fallthrough", attr_id))
25322 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25323 /* Transactional Memory TS optimize_for_synchronized attribute is
25324 equivalent to GNU transaction_callable. */
25325 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25326 TREE_PURPOSE (attribute)
25327 = get_identifier ("transaction_callable");
25328 /* Transactional Memory attributes are GNU attributes. */
25329 else if (tm_attr_to_mask (attr_id))
25330 TREE_PURPOSE (attribute) = attr_id;
25331 }
25332
25333 /* Now parse the optional argument clause of the attribute. */
25334
25335 if (token->type != CPP_OPEN_PAREN)
25336 return attribute;
25337
25338 {
25339 vec<tree, va_gc> *vec;
25340 int attr_flag = normal_attr;
25341
25342 if (attr_ns == get_identifier ("gnu")
25343 && attribute_takes_identifier_p (attr_id))
25344 /* A GNU attribute that takes an identifier in parameter. */
25345 attr_flag = id_attr;
25346
25347 vec = cp_parser_parenthesized_expression_list
25348 (parser, attr_flag, /*cast_p=*/false,
25349 /*allow_expansion_p=*/true,
25350 /*non_constant_p=*/NULL);
25351 if (vec == NULL)
25352 arguments = error_mark_node;
25353 else
25354 {
25355 arguments = build_tree_list_vec (vec);
25356 release_tree_vector (vec);
25357 }
25358
25359 if (arguments == error_mark_node)
25360 attribute = error_mark_node;
25361 else
25362 TREE_VALUE (attribute) = arguments;
25363 }
25364
25365 return attribute;
25366 }
25367
25368 /* Check that the attribute ATTRIBUTE appears at most once in the
25369 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25370 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25371 isn't implemented yet in GCC. */
25372
25373 static void
25374 cp_parser_check_std_attribute (tree attributes, tree attribute)
25375 {
25376 if (attributes)
25377 {
25378 tree name = get_attribute_name (attribute);
25379 if (is_attribute_p ("noreturn", name)
25380 && lookup_attribute ("noreturn", attributes))
25381 error ("attribute %<noreturn%> can appear at most once "
25382 "in an attribute-list");
25383 else if (is_attribute_p ("deprecated", name)
25384 && lookup_attribute ("deprecated", attributes))
25385 error ("attribute %<deprecated%> can appear at most once "
25386 "in an attribute-list");
25387 }
25388 }
25389
25390 /* Parse a list of standard C++-11 attributes.
25391
25392 attribute-list:
25393 attribute [opt]
25394 attribute-list , attribute[opt]
25395 attribute ...
25396 attribute-list , attribute ...
25397 */
25398
25399 static tree
25400 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25401 {
25402 tree attributes = NULL_TREE, attribute = NULL_TREE;
25403 cp_token *token = NULL;
25404
25405 while (true)
25406 {
25407 attribute = cp_parser_std_attribute (parser, attr_ns);
25408 if (attribute == error_mark_node)
25409 break;
25410 if (attribute != NULL_TREE)
25411 {
25412 cp_parser_check_std_attribute (attributes, attribute);
25413 TREE_CHAIN (attribute) = attributes;
25414 attributes = attribute;
25415 }
25416 token = cp_lexer_peek_token (parser->lexer);
25417 if (token->type == CPP_ELLIPSIS)
25418 {
25419 cp_lexer_consume_token (parser->lexer);
25420 if (attribute == NULL_TREE)
25421 error_at (token->location,
25422 "expected attribute before %<...%>");
25423 else
25424 {
25425 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25426 if (pack == error_mark_node)
25427 return error_mark_node;
25428 TREE_VALUE (attribute) = pack;
25429 }
25430 token = cp_lexer_peek_token (parser->lexer);
25431 }
25432 if (token->type != CPP_COMMA)
25433 break;
25434 cp_lexer_consume_token (parser->lexer);
25435 }
25436 attributes = nreverse (attributes);
25437 return attributes;
25438 }
25439
25440 /* Parse a standard C++-11 attribute specifier.
25441
25442 attribute-specifier:
25443 [ [ attribute-using-prefix [opt] attribute-list ] ]
25444 alignment-specifier
25445
25446 attribute-using-prefix:
25447 using attribute-namespace :
25448
25449 alignment-specifier:
25450 alignas ( type-id ... [opt] )
25451 alignas ( alignment-expression ... [opt] ). */
25452
25453 static tree
25454 cp_parser_std_attribute_spec (cp_parser *parser)
25455 {
25456 tree attributes = NULL_TREE;
25457 cp_token *token = cp_lexer_peek_token (parser->lexer);
25458
25459 if (token->type == CPP_OPEN_SQUARE
25460 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25461 {
25462 tree attr_ns = NULL_TREE;
25463
25464 cp_lexer_consume_token (parser->lexer);
25465 cp_lexer_consume_token (parser->lexer);
25466
25467 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25468 {
25469 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25470 if (token->type == CPP_NAME)
25471 attr_ns = token->u.value;
25472 else if (token->type == CPP_KEYWORD)
25473 attr_ns = ridpointers[(int) token->keyword];
25474 else if (token->flags & NAMED_OP)
25475 attr_ns = get_identifier (cpp_type2name (token->type,
25476 token->flags));
25477 if (attr_ns
25478 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25479 {
25480 if (cxx_dialect < cxx17
25481 && !in_system_header_at (input_location))
25482 pedwarn (input_location, 0,
25483 "attribute using prefix only available "
25484 "with -std=c++17 or -std=gnu++17");
25485
25486 cp_lexer_consume_token (parser->lexer);
25487 cp_lexer_consume_token (parser->lexer);
25488 cp_lexer_consume_token (parser->lexer);
25489 }
25490 else
25491 attr_ns = NULL_TREE;
25492 }
25493
25494 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25495
25496 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25497 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25498 cp_parser_skip_to_end_of_statement (parser);
25499 else
25500 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25501 when we are sure that we have actually parsed them. */
25502 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25503 }
25504 else
25505 {
25506 tree alignas_expr;
25507
25508 /* Look for an alignment-specifier. */
25509
25510 token = cp_lexer_peek_token (parser->lexer);
25511
25512 if (token->type != CPP_KEYWORD
25513 || token->keyword != RID_ALIGNAS)
25514 return NULL_TREE;
25515
25516 cp_lexer_consume_token (parser->lexer);
25517 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25518
25519 matching_parens parens;
25520 if (!parens.require_open (parser))
25521 return error_mark_node;
25522
25523 cp_parser_parse_tentatively (parser);
25524 alignas_expr = cp_parser_type_id (parser);
25525
25526 if (!cp_parser_parse_definitely (parser))
25527 {
25528 alignas_expr = cp_parser_assignment_expression (parser);
25529 if (alignas_expr == error_mark_node)
25530 cp_parser_skip_to_end_of_statement (parser);
25531 if (alignas_expr == NULL_TREE
25532 || alignas_expr == error_mark_node)
25533 return alignas_expr;
25534 }
25535
25536 alignas_expr = cxx_alignas_expr (alignas_expr);
25537 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25538
25539 /* Handle alignas (pack...). */
25540 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25541 {
25542 cp_lexer_consume_token (parser->lexer);
25543 alignas_expr = make_pack_expansion (alignas_expr);
25544 }
25545
25546 /* Something went wrong, so don't build the attribute. */
25547 if (alignas_expr == error_mark_node)
25548 return error_mark_node;
25549
25550 if (!parens.require_close (parser))
25551 return error_mark_node;
25552
25553 /* Build the C++-11 representation of an 'aligned'
25554 attribute. */
25555 attributes =
25556 build_tree_list (build_tree_list (get_identifier ("gnu"),
25557 get_identifier ("aligned")),
25558 alignas_expr);
25559 }
25560
25561 return attributes;
25562 }
25563
25564 /* Parse a standard C++-11 attribute-specifier-seq.
25565
25566 attribute-specifier-seq:
25567 attribute-specifier-seq [opt] attribute-specifier
25568 */
25569
25570 static tree
25571 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25572 {
25573 tree attr_specs = NULL_TREE;
25574 tree attr_last = NULL_TREE;
25575
25576 while (true)
25577 {
25578 tree attr_spec = cp_parser_std_attribute_spec (parser);
25579 if (attr_spec == NULL_TREE)
25580 break;
25581 if (attr_spec == error_mark_node)
25582 return error_mark_node;
25583
25584 if (attr_last)
25585 TREE_CHAIN (attr_last) = attr_spec;
25586 else
25587 attr_specs = attr_last = attr_spec;
25588 attr_last = tree_last (attr_last);
25589 }
25590
25591 return attr_specs;
25592 }
25593
25594 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25595 return index of the first token after balanced-token, or N on failure. */
25596
25597 static size_t
25598 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25599 {
25600 size_t orig_n = n;
25601 int nparens = 0, nbraces = 0, nsquares = 0;
25602 do
25603 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25604 {
25605 case CPP_EOF:
25606 case CPP_PRAGMA_EOL:
25607 /* Ran out of tokens. */
25608 return orig_n;
25609 case CPP_OPEN_PAREN:
25610 ++nparens;
25611 break;
25612 case CPP_OPEN_BRACE:
25613 ++nbraces;
25614 break;
25615 case CPP_OPEN_SQUARE:
25616 ++nsquares;
25617 break;
25618 case CPP_CLOSE_PAREN:
25619 --nparens;
25620 break;
25621 case CPP_CLOSE_BRACE:
25622 --nbraces;
25623 break;
25624 case CPP_CLOSE_SQUARE:
25625 --nsquares;
25626 break;
25627 default:
25628 break;
25629 }
25630 while (nparens || nbraces || nsquares);
25631 return n;
25632 }
25633
25634 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25635 return index of the first token after the GNU attribute tokens, or N on
25636 failure. */
25637
25638 static size_t
25639 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25640 {
25641 while (true)
25642 {
25643 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25644 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25645 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25646 break;
25647
25648 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25649 if (n2 == n + 2)
25650 break;
25651 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25652 break;
25653 n = n2 + 1;
25654 }
25655 return n;
25656 }
25657
25658 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25659 next token), return index of the first token after the standard C++11
25660 attribute tokens, or N on failure. */
25661
25662 static size_t
25663 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25664 {
25665 while (true)
25666 {
25667 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25668 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25669 {
25670 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25671 if (n2 == n + 1)
25672 break;
25673 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25674 break;
25675 n = n2 + 1;
25676 }
25677 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25678 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25679 {
25680 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25681 if (n2 == n + 1)
25682 break;
25683 n = n2;
25684 }
25685 else
25686 break;
25687 }
25688 return n;
25689 }
25690
25691 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25692 as the next token), return index of the first token after the attribute
25693 tokens, or N on failure. */
25694
25695 static size_t
25696 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25697 {
25698 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25699 return cp_parser_skip_gnu_attributes_opt (parser, n);
25700 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25701 }
25702
25703 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25704 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25705 current value of the PEDANTIC flag, regardless of whether or not
25706 the `__extension__' keyword is present. The caller is responsible
25707 for restoring the value of the PEDANTIC flag. */
25708
25709 static bool
25710 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25711 {
25712 /* Save the old value of the PEDANTIC flag. */
25713 *saved_pedantic = pedantic;
25714
25715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25716 {
25717 /* Consume the `__extension__' token. */
25718 cp_lexer_consume_token (parser->lexer);
25719 /* We're not being pedantic while the `__extension__' keyword is
25720 in effect. */
25721 pedantic = 0;
25722
25723 return true;
25724 }
25725
25726 return false;
25727 }
25728
25729 /* Parse a label declaration.
25730
25731 label-declaration:
25732 __label__ label-declarator-seq ;
25733
25734 label-declarator-seq:
25735 identifier , label-declarator-seq
25736 identifier */
25737
25738 static void
25739 cp_parser_label_declaration (cp_parser* parser)
25740 {
25741 /* Look for the `__label__' keyword. */
25742 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25743
25744 while (true)
25745 {
25746 tree identifier;
25747
25748 /* Look for an identifier. */
25749 identifier = cp_parser_identifier (parser);
25750 /* If we failed, stop. */
25751 if (identifier == error_mark_node)
25752 break;
25753 /* Declare it as a label. */
25754 finish_label_decl (identifier);
25755 /* If the next token is a `;', stop. */
25756 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25757 break;
25758 /* Look for the `,' separating the label declarations. */
25759 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25760 }
25761
25762 /* Look for the final `;'. */
25763 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25764 }
25765
25766 // -------------------------------------------------------------------------- //
25767 // Requires Clause
25768
25769 // Parse a requires clause.
25770 //
25771 // requires-clause:
25772 // 'requires' logical-or-expression
25773 //
25774 // The required logical-or-expression must be a constant expression. Note
25775 // that we don't check that the expression is constepxr here. We defer until
25776 // we analyze constraints and then, we only check atomic constraints.
25777 static tree
25778 cp_parser_requires_clause (cp_parser *parser)
25779 {
25780 // Parse the requires clause so that it is not automatically folded.
25781 ++processing_template_decl;
25782 tree expr = cp_parser_binary_expression (parser, false, false,
25783 PREC_NOT_OPERATOR, NULL);
25784 if (check_for_bare_parameter_packs (expr))
25785 expr = error_mark_node;
25786 --processing_template_decl;
25787 return expr;
25788 }
25789
25790 // Optionally parse a requires clause:
25791 static tree
25792 cp_parser_requires_clause_opt (cp_parser *parser)
25793 {
25794 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25795 if (tok->keyword != RID_REQUIRES)
25796 {
25797 if (!flag_concepts && tok->type == CPP_NAME
25798 && tok->u.value == ridpointers[RID_REQUIRES])
25799 {
25800 error_at (cp_lexer_peek_token (parser->lexer)->location,
25801 "%<requires%> only available with -fconcepts");
25802 /* Parse and discard the requires-clause. */
25803 cp_lexer_consume_token (parser->lexer);
25804 cp_parser_requires_clause (parser);
25805 }
25806 return NULL_TREE;
25807 }
25808 cp_lexer_consume_token (parser->lexer);
25809 return cp_parser_requires_clause (parser);
25810 }
25811
25812
25813 /*---------------------------------------------------------------------------
25814 Requires expressions
25815 ---------------------------------------------------------------------------*/
25816
25817 /* Parse a requires expression
25818
25819 requirement-expression:
25820 'requires' requirement-parameter-list [opt] requirement-body */
25821 static tree
25822 cp_parser_requires_expression (cp_parser *parser)
25823 {
25824 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25825 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25826
25827 /* A requires-expression shall appear only within a concept
25828 definition or a requires-clause.
25829
25830 TODO: Implement this diagnostic correctly. */
25831 if (!processing_template_decl)
25832 {
25833 error_at (loc, "a requires expression cannot appear outside a template");
25834 cp_parser_skip_to_end_of_statement (parser);
25835 return error_mark_node;
25836 }
25837
25838 tree parms, reqs;
25839 {
25840 /* Local parameters are delared as variables within the scope
25841 of the expression. They are not visible past the end of
25842 the expression. Expressions within the requires-expression
25843 are unevaluated. */
25844 struct scope_sentinel
25845 {
25846 scope_sentinel ()
25847 {
25848 ++cp_unevaluated_operand;
25849 begin_scope (sk_block, NULL_TREE);
25850 }
25851
25852 ~scope_sentinel ()
25853 {
25854 pop_bindings_and_leave_scope ();
25855 --cp_unevaluated_operand;
25856 }
25857 } s;
25858
25859 /* Parse the optional parameter list. */
25860 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25861 {
25862 parms = cp_parser_requirement_parameter_list (parser);
25863 if (parms == error_mark_node)
25864 return error_mark_node;
25865 }
25866 else
25867 parms = NULL_TREE;
25868
25869 /* Parse the requirement body. */
25870 reqs = cp_parser_requirement_body (parser);
25871 if (reqs == error_mark_node)
25872 return error_mark_node;
25873 }
25874
25875 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25876 the parm chain. */
25877 grokparms (parms, &parms);
25878 return finish_requires_expr (parms, reqs);
25879 }
25880
25881 /* Parse a parameterized requirement.
25882
25883 requirement-parameter-list:
25884 '(' parameter-declaration-clause ')' */
25885 static tree
25886 cp_parser_requirement_parameter_list (cp_parser *parser)
25887 {
25888 matching_parens parens;
25889 if (!parens.require_open (parser))
25890 return error_mark_node;
25891
25892 tree parms = cp_parser_parameter_declaration_clause (parser);
25893
25894 if (!parens.require_close (parser))
25895 return error_mark_node;
25896
25897 return parms;
25898 }
25899
25900 /* Parse the body of a requirement.
25901
25902 requirement-body:
25903 '{' requirement-list '}' */
25904 static tree
25905 cp_parser_requirement_body (cp_parser *parser)
25906 {
25907 matching_braces braces;
25908 if (!braces.require_open (parser))
25909 return error_mark_node;
25910
25911 tree reqs = cp_parser_requirement_list (parser);
25912
25913 if (!braces.require_close (parser))
25914 return error_mark_node;
25915
25916 return reqs;
25917 }
25918
25919 /* Parse a list of requirements.
25920
25921 requirement-list:
25922 requirement
25923 requirement-list ';' requirement[opt] */
25924 static tree
25925 cp_parser_requirement_list (cp_parser *parser)
25926 {
25927 tree result = NULL_TREE;
25928 while (true)
25929 {
25930 tree req = cp_parser_requirement (parser);
25931 if (req == error_mark_node)
25932 return error_mark_node;
25933
25934 result = tree_cons (NULL_TREE, req, result);
25935
25936 /* If we see a semi-colon, consume it. */
25937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25938 cp_lexer_consume_token (parser->lexer);
25939
25940 /* Stop processing at the end of the list. */
25941 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25942 break;
25943 }
25944
25945 /* Reverse the order of requirements so they are analyzed in
25946 declaration order. */
25947 return nreverse (result);
25948 }
25949
25950 /* Parse a syntactic requirement or type requirement.
25951
25952 requirement:
25953 simple-requirement
25954 compound-requirement
25955 type-requirement
25956 nested-requirement */
25957 static tree
25958 cp_parser_requirement (cp_parser *parser)
25959 {
25960 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25961 return cp_parser_compound_requirement (parser);
25962 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25963 return cp_parser_type_requirement (parser);
25964 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25965 return cp_parser_nested_requirement (parser);
25966 else
25967 return cp_parser_simple_requirement (parser);
25968 }
25969
25970 /* Parse a simple requirement.
25971
25972 simple-requirement:
25973 expression ';' */
25974 static tree
25975 cp_parser_simple_requirement (cp_parser *parser)
25976 {
25977 tree expr = cp_parser_expression (parser, NULL, false, false);
25978 if (!expr || expr == error_mark_node)
25979 return error_mark_node;
25980
25981 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25982 return error_mark_node;
25983
25984 return finish_simple_requirement (expr);
25985 }
25986
25987 /* Parse a type requirement
25988
25989 type-requirement
25990 nested-name-specifier [opt] required-type-name ';'
25991
25992 required-type-name:
25993 type-name
25994 'template' [opt] simple-template-id */
25995 static tree
25996 cp_parser_type_requirement (cp_parser *parser)
25997 {
25998 cp_lexer_consume_token (parser->lexer);
25999
26000 // Save the scope before parsing name specifiers.
26001 tree saved_scope = parser->scope;
26002 tree saved_object_scope = parser->object_scope;
26003 tree saved_qualifying_scope = parser->qualifying_scope;
26004 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26005 cp_parser_nested_name_specifier_opt (parser,
26006 /*typename_keyword_p=*/true,
26007 /*check_dependency_p=*/false,
26008 /*type_p=*/true,
26009 /*is_declaration=*/false);
26010
26011 tree type;
26012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26013 {
26014 cp_lexer_consume_token (parser->lexer);
26015 type = cp_parser_template_id (parser,
26016 /*template_keyword_p=*/true,
26017 /*check_dependency=*/false,
26018 /*tag_type=*/none_type,
26019 /*is_declaration=*/false);
26020 type = make_typename_type (parser->scope, type, typename_type,
26021 /*complain=*/tf_error);
26022 }
26023 else
26024 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26025
26026 if (TREE_CODE (type) == TYPE_DECL)
26027 type = TREE_TYPE (type);
26028
26029 parser->scope = saved_scope;
26030 parser->object_scope = saved_object_scope;
26031 parser->qualifying_scope = saved_qualifying_scope;
26032
26033 if (type == error_mark_node)
26034 cp_parser_skip_to_end_of_statement (parser);
26035
26036 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26037 return error_mark_node;
26038 if (type == error_mark_node)
26039 return error_mark_node;
26040
26041 return finish_type_requirement (type);
26042 }
26043
26044 /* Parse a compound requirement
26045
26046 compound-requirement:
26047 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26048 static tree
26049 cp_parser_compound_requirement (cp_parser *parser)
26050 {
26051 /* Parse an expression enclosed in '{ }'s. */
26052 matching_braces braces;
26053 if (!braces.require_open (parser))
26054 return error_mark_node;
26055
26056 tree expr = cp_parser_expression (parser, NULL, false, false);
26057 if (!expr || expr == error_mark_node)
26058 return error_mark_node;
26059
26060 if (!braces.require_close (parser))
26061 return error_mark_node;
26062
26063 /* Parse the optional noexcept. */
26064 bool noexcept_p = false;
26065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26066 {
26067 cp_lexer_consume_token (parser->lexer);
26068 noexcept_p = true;
26069 }
26070
26071 /* Parse the optional trailing return type. */
26072 tree type = NULL_TREE;
26073 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26074 {
26075 cp_lexer_consume_token (parser->lexer);
26076 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26077 parser->in_result_type_constraint_p = true;
26078 type = cp_parser_trailing_type_id (parser);
26079 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26080 if (type == error_mark_node)
26081 return error_mark_node;
26082 }
26083
26084 return finish_compound_requirement (expr, type, noexcept_p);
26085 }
26086
26087 /* Parse a nested requirement. This is the same as a requires clause.
26088
26089 nested-requirement:
26090 requires-clause */
26091 static tree
26092 cp_parser_nested_requirement (cp_parser *parser)
26093 {
26094 cp_lexer_consume_token (parser->lexer);
26095 tree req = cp_parser_requires_clause (parser);
26096 if (req == error_mark_node)
26097 return error_mark_node;
26098 return finish_nested_requirement (req);
26099 }
26100
26101 /* Support Functions */
26102
26103 /* Return the appropriate prefer_type argument for lookup_name_real based on
26104 tag_type and template_mem_access. */
26105
26106 static inline int
26107 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26108 {
26109 /* DR 141: When looking in the current enclosing context for a template-name
26110 after -> or ., only consider class templates. */
26111 if (template_mem_access)
26112 return 2;
26113 switch (tag_type)
26114 {
26115 case none_type: return 0; // No preference.
26116 case scope_type: return 1; // Type or namespace.
26117 default: return 2; // Type only.
26118 }
26119 }
26120
26121 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26122 NAME should have one of the representations used for an
26123 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26124 is returned. If PARSER->SCOPE is a dependent type, then a
26125 SCOPE_REF is returned.
26126
26127 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26128 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26129 was formed. Abstractly, such entities should not be passed to this
26130 function, because they do not need to be looked up, but it is
26131 simpler to check for this special case here, rather than at the
26132 call-sites.
26133
26134 In cases not explicitly covered above, this function returns a
26135 DECL, OVERLOAD, or baselink representing the result of the lookup.
26136 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26137 is returned.
26138
26139 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26140 (e.g., "struct") that was used. In that case bindings that do not
26141 refer to types are ignored.
26142
26143 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26144 ignored.
26145
26146 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26147 are ignored.
26148
26149 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26150 types.
26151
26152 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26153 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26154 NULL_TREE otherwise. */
26155
26156 static cp_expr
26157 cp_parser_lookup_name (cp_parser *parser, tree name,
26158 enum tag_types tag_type,
26159 bool is_template,
26160 bool is_namespace,
26161 bool check_dependency,
26162 tree *ambiguous_decls,
26163 location_t name_location)
26164 {
26165 tree decl;
26166 tree object_type = parser->context->object_type;
26167
26168 /* Assume that the lookup will be unambiguous. */
26169 if (ambiguous_decls)
26170 *ambiguous_decls = NULL_TREE;
26171
26172 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26173 no longer valid. Note that if we are parsing tentatively, and
26174 the parse fails, OBJECT_TYPE will be automatically restored. */
26175 parser->context->object_type = NULL_TREE;
26176
26177 if (name == error_mark_node)
26178 return error_mark_node;
26179
26180 /* A template-id has already been resolved; there is no lookup to
26181 do. */
26182 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26183 return name;
26184 if (BASELINK_P (name))
26185 {
26186 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26187 == TEMPLATE_ID_EXPR);
26188 return name;
26189 }
26190
26191 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26192 it should already have been checked to make sure that the name
26193 used matches the type being destroyed. */
26194 if (TREE_CODE (name) == BIT_NOT_EXPR)
26195 {
26196 tree type;
26197
26198 /* Figure out to which type this destructor applies. */
26199 if (parser->scope)
26200 type = parser->scope;
26201 else if (object_type)
26202 type = object_type;
26203 else
26204 type = current_class_type;
26205 /* If that's not a class type, there is no destructor. */
26206 if (!type || !CLASS_TYPE_P (type))
26207 return error_mark_node;
26208
26209 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26210 lazily_declare_fn (sfk_destructor, type);
26211
26212 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26213 return dtor;
26214
26215 return error_mark_node;
26216 }
26217
26218 /* By this point, the NAME should be an ordinary identifier. If
26219 the id-expression was a qualified name, the qualifying scope is
26220 stored in PARSER->SCOPE at this point. */
26221 gcc_assert (identifier_p (name));
26222
26223 /* Perform the lookup. */
26224 if (parser->scope)
26225 {
26226 bool dependent_p;
26227
26228 if (parser->scope == error_mark_node)
26229 return error_mark_node;
26230
26231 /* If the SCOPE is dependent, the lookup must be deferred until
26232 the template is instantiated -- unless we are explicitly
26233 looking up names in uninstantiated templates. Even then, we
26234 cannot look up the name if the scope is not a class type; it
26235 might, for example, be a template type parameter. */
26236 dependent_p = (TYPE_P (parser->scope)
26237 && dependent_scope_p (parser->scope));
26238 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26239 && dependent_p)
26240 /* Defer lookup. */
26241 decl = error_mark_node;
26242 else
26243 {
26244 tree pushed_scope = NULL_TREE;
26245
26246 /* If PARSER->SCOPE is a dependent type, then it must be a
26247 class type, and we must not be checking dependencies;
26248 otherwise, we would have processed this lookup above. So
26249 that PARSER->SCOPE is not considered a dependent base by
26250 lookup_member, we must enter the scope here. */
26251 if (dependent_p)
26252 pushed_scope = push_scope (parser->scope);
26253
26254 /* If the PARSER->SCOPE is a template specialization, it
26255 may be instantiated during name lookup. In that case,
26256 errors may be issued. Even if we rollback the current
26257 tentative parse, those errors are valid. */
26258 decl = lookup_qualified_name (parser->scope, name,
26259 prefer_type_arg (tag_type),
26260 /*complain=*/true);
26261
26262 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26263 lookup result and the nested-name-specifier nominates a class C:
26264 * if the name specified after the nested-name-specifier, when
26265 looked up in C, is the injected-class-name of C (Clause 9), or
26266 * if the name specified after the nested-name-specifier is the
26267 same as the identifier or the simple-template-id's template-
26268 name in the last component of the nested-name-specifier,
26269 the name is instead considered to name the constructor of
26270 class C. [ Note: for example, the constructor is not an
26271 acceptable lookup result in an elaborated-type-specifier so
26272 the constructor would not be used in place of the
26273 injected-class-name. --end note ] Such a constructor name
26274 shall be used only in the declarator-id of a declaration that
26275 names a constructor or in a using-declaration. */
26276 if (tag_type == none_type
26277 && DECL_SELF_REFERENCE_P (decl)
26278 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26279 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26280 prefer_type_arg (tag_type),
26281 /*complain=*/true);
26282
26283 /* If we have a single function from a using decl, pull it out. */
26284 if (TREE_CODE (decl) == OVERLOAD
26285 && !really_overloaded_fn (decl))
26286 decl = OVL_FUNCTION (decl);
26287
26288 if (pushed_scope)
26289 pop_scope (pushed_scope);
26290 }
26291
26292 /* If the scope is a dependent type and either we deferred lookup or
26293 we did lookup but didn't find the name, rememeber the name. */
26294 if (decl == error_mark_node && TYPE_P (parser->scope)
26295 && dependent_type_p (parser->scope))
26296 {
26297 if (tag_type)
26298 {
26299 tree type;
26300
26301 /* The resolution to Core Issue 180 says that `struct
26302 A::B' should be considered a type-name, even if `A'
26303 is dependent. */
26304 type = make_typename_type (parser->scope, name, tag_type,
26305 /*complain=*/tf_error);
26306 if (type != error_mark_node)
26307 decl = TYPE_NAME (type);
26308 }
26309 else if (is_template
26310 && (cp_parser_next_token_ends_template_argument_p (parser)
26311 || cp_lexer_next_token_is (parser->lexer,
26312 CPP_CLOSE_PAREN)))
26313 decl = make_unbound_class_template (parser->scope,
26314 name, NULL_TREE,
26315 /*complain=*/tf_error);
26316 else
26317 decl = build_qualified_name (/*type=*/NULL_TREE,
26318 parser->scope, name,
26319 is_template);
26320 }
26321 parser->qualifying_scope = parser->scope;
26322 parser->object_scope = NULL_TREE;
26323 }
26324 else if (object_type)
26325 {
26326 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26327 OBJECT_TYPE is not a class. */
26328 if (CLASS_TYPE_P (object_type))
26329 /* If the OBJECT_TYPE is a template specialization, it may
26330 be instantiated during name lookup. In that case, errors
26331 may be issued. Even if we rollback the current tentative
26332 parse, those errors are valid. */
26333 decl = lookup_member (object_type,
26334 name,
26335 /*protect=*/0,
26336 prefer_type_arg (tag_type),
26337 tf_warning_or_error);
26338 else
26339 decl = NULL_TREE;
26340
26341 if (!decl)
26342 /* Look it up in the enclosing context. DR 141: When looking for a
26343 template-name after -> or ., only consider class templates. */
26344 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26345 /*nonclass=*/0,
26346 /*block_p=*/true, is_namespace, 0);
26347 if (object_type == unknown_type_node)
26348 /* The object is type-dependent, so we can't look anything up; we used
26349 this to get the DR 141 behavior. */
26350 object_type = NULL_TREE;
26351 parser->object_scope = object_type;
26352 parser->qualifying_scope = NULL_TREE;
26353 }
26354 else
26355 {
26356 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26357 /*nonclass=*/0,
26358 /*block_p=*/true, is_namespace, 0);
26359 parser->qualifying_scope = NULL_TREE;
26360 parser->object_scope = NULL_TREE;
26361 }
26362
26363 /* If the lookup failed, let our caller know. */
26364 if (!decl || decl == error_mark_node)
26365 return error_mark_node;
26366
26367 /* Pull out the template from an injected-class-name (or multiple). */
26368 if (is_template)
26369 decl = maybe_get_template_decl_from_type_decl (decl);
26370
26371 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26372 if (TREE_CODE (decl) == TREE_LIST)
26373 {
26374 if (ambiguous_decls)
26375 *ambiguous_decls = decl;
26376 /* The error message we have to print is too complicated for
26377 cp_parser_error, so we incorporate its actions directly. */
26378 if (!cp_parser_simulate_error (parser))
26379 {
26380 error_at (name_location, "reference to %qD is ambiguous",
26381 name);
26382 print_candidates (decl);
26383 }
26384 return error_mark_node;
26385 }
26386
26387 gcc_assert (DECL_P (decl)
26388 || TREE_CODE (decl) == OVERLOAD
26389 || TREE_CODE (decl) == SCOPE_REF
26390 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26391 || BASELINK_P (decl));
26392
26393 /* If we have resolved the name of a member declaration, check to
26394 see if the declaration is accessible. When the name resolves to
26395 set of overloaded functions, accessibility is checked when
26396 overload resolution is done.
26397
26398 During an explicit instantiation, access is not checked at all,
26399 as per [temp.explicit]. */
26400 if (DECL_P (decl))
26401 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26402
26403 maybe_record_typedef_use (decl);
26404
26405 return cp_expr (decl, name_location);
26406 }
26407
26408 /* Like cp_parser_lookup_name, but for use in the typical case where
26409 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26410 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26411
26412 static tree
26413 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26414 {
26415 return cp_parser_lookup_name (parser, name,
26416 none_type,
26417 /*is_template=*/false,
26418 /*is_namespace=*/false,
26419 /*check_dependency=*/true,
26420 /*ambiguous_decls=*/NULL,
26421 location);
26422 }
26423
26424 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26425 the current context, return the TYPE_DECL. If TAG_NAME_P is
26426 true, the DECL indicates the class being defined in a class-head,
26427 or declared in an elaborated-type-specifier.
26428
26429 Otherwise, return DECL. */
26430
26431 static tree
26432 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26433 {
26434 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26435 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26436
26437 struct A {
26438 template <typename T> struct B;
26439 };
26440
26441 template <typename T> struct A::B {};
26442
26443 Similarly, in an elaborated-type-specifier:
26444
26445 namespace N { struct X{}; }
26446
26447 struct A {
26448 template <typename T> friend struct N::X;
26449 };
26450
26451 However, if the DECL refers to a class type, and we are in
26452 the scope of the class, then the name lookup automatically
26453 finds the TYPE_DECL created by build_self_reference rather
26454 than a TEMPLATE_DECL. For example, in:
26455
26456 template <class T> struct S {
26457 S s;
26458 };
26459
26460 there is no need to handle such case. */
26461
26462 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26463 return DECL_TEMPLATE_RESULT (decl);
26464
26465 return decl;
26466 }
26467
26468 /* If too many, or too few, template-parameter lists apply to the
26469 declarator, issue an error message. Returns TRUE if all went well,
26470 and FALSE otherwise. */
26471
26472 static bool
26473 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26474 cp_declarator *declarator,
26475 location_t declarator_location)
26476 {
26477 switch (declarator->kind)
26478 {
26479 case cdk_id:
26480 {
26481 unsigned num_templates = 0;
26482 tree scope = declarator->u.id.qualifying_scope;
26483 bool template_id_p = false;
26484
26485 if (scope)
26486 num_templates = num_template_headers_for_class (scope);
26487 else if (TREE_CODE (declarator->u.id.unqualified_name)
26488 == TEMPLATE_ID_EXPR)
26489 {
26490 /* If the DECLARATOR has the form `X<y>' then it uses one
26491 additional level of template parameters. */
26492 ++num_templates;
26493 template_id_p = true;
26494 }
26495
26496 return cp_parser_check_template_parameters
26497 (parser, num_templates, template_id_p, declarator_location,
26498 declarator);
26499 }
26500
26501 case cdk_function:
26502 case cdk_array:
26503 case cdk_pointer:
26504 case cdk_reference:
26505 case cdk_ptrmem:
26506 return (cp_parser_check_declarator_template_parameters
26507 (parser, declarator->declarator, declarator_location));
26508
26509 case cdk_decomp:
26510 case cdk_error:
26511 return true;
26512
26513 default:
26514 gcc_unreachable ();
26515 }
26516 return false;
26517 }
26518
26519 /* NUM_TEMPLATES were used in the current declaration. If that is
26520 invalid, return FALSE and issue an error messages. Otherwise,
26521 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26522 declarator and we can print more accurate diagnostics. */
26523
26524 static bool
26525 cp_parser_check_template_parameters (cp_parser* parser,
26526 unsigned num_templates,
26527 bool template_id_p,
26528 location_t location,
26529 cp_declarator *declarator)
26530 {
26531 /* If there are the same number of template classes and parameter
26532 lists, that's OK. */
26533 if (parser->num_template_parameter_lists == num_templates)
26534 return true;
26535 /* If there are more, but only one more, and the name ends in an identifier,
26536 then we are declaring a primary template. That's OK too. */
26537 if (!template_id_p
26538 && parser->num_template_parameter_lists == num_templates + 1)
26539 return true;
26540 /* If there are more template classes than parameter lists, we have
26541 something like:
26542
26543 template <class T> void S<T>::R<T>::f (); */
26544 if (parser->num_template_parameter_lists < num_templates)
26545 {
26546 if (declarator && !current_function_decl)
26547 error_at (location, "specializing member %<%T::%E%> "
26548 "requires %<template<>%> syntax",
26549 declarator->u.id.qualifying_scope,
26550 declarator->u.id.unqualified_name);
26551 else if (declarator)
26552 error_at (location, "invalid declaration of %<%T::%E%>",
26553 declarator->u.id.qualifying_scope,
26554 declarator->u.id.unqualified_name);
26555 else
26556 error_at (location, "too few template-parameter-lists");
26557 return false;
26558 }
26559 /* Otherwise, there are too many template parameter lists. We have
26560 something like:
26561
26562 template <class T> template <class U> void S::f(); */
26563 error_at (location, "too many template-parameter-lists");
26564 return false;
26565 }
26566
26567 /* Parse an optional `::' token indicating that the following name is
26568 from the global namespace. If so, PARSER->SCOPE is set to the
26569 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26570 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26571 Returns the new value of PARSER->SCOPE, if the `::' token is
26572 present, and NULL_TREE otherwise. */
26573
26574 static tree
26575 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26576 {
26577 cp_token *token;
26578
26579 /* Peek at the next token. */
26580 token = cp_lexer_peek_token (parser->lexer);
26581 /* If we're looking at a `::' token then we're starting from the
26582 global namespace, not our current location. */
26583 if (token->type == CPP_SCOPE)
26584 {
26585 /* Consume the `::' token. */
26586 cp_lexer_consume_token (parser->lexer);
26587 /* Set the SCOPE so that we know where to start the lookup. */
26588 parser->scope = global_namespace;
26589 parser->qualifying_scope = global_namespace;
26590 parser->object_scope = NULL_TREE;
26591
26592 return parser->scope;
26593 }
26594 else if (!current_scope_valid_p)
26595 {
26596 parser->scope = NULL_TREE;
26597 parser->qualifying_scope = NULL_TREE;
26598 parser->object_scope = NULL_TREE;
26599 }
26600
26601 return NULL_TREE;
26602 }
26603
26604 /* Returns TRUE if the upcoming token sequence is the start of a
26605 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26606 declarator is preceded by the `friend' specifier. */
26607
26608 static bool
26609 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26610 {
26611 bool constructor_p;
26612 bool outside_class_specifier_p;
26613 tree nested_name_specifier;
26614 cp_token *next_token;
26615
26616 /* The common case is that this is not a constructor declarator, so
26617 try to avoid doing lots of work if at all possible. It's not
26618 valid declare a constructor at function scope. */
26619 if (parser->in_function_body)
26620 return false;
26621 /* And only certain tokens can begin a constructor declarator. */
26622 next_token = cp_lexer_peek_token (parser->lexer);
26623 if (next_token->type != CPP_NAME
26624 && next_token->type != CPP_SCOPE
26625 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26626 && next_token->type != CPP_TEMPLATE_ID)
26627 return false;
26628
26629 /* Parse tentatively; we are going to roll back all of the tokens
26630 consumed here. */
26631 cp_parser_parse_tentatively (parser);
26632 /* Assume that we are looking at a constructor declarator. */
26633 constructor_p = true;
26634
26635 /* Look for the optional `::' operator. */
26636 cp_parser_global_scope_opt (parser,
26637 /*current_scope_valid_p=*/false);
26638 /* Look for the nested-name-specifier. */
26639 nested_name_specifier
26640 = (cp_parser_nested_name_specifier_opt (parser,
26641 /*typename_keyword_p=*/false,
26642 /*check_dependency_p=*/false,
26643 /*type_p=*/false,
26644 /*is_declaration=*/false));
26645
26646 outside_class_specifier_p = (!at_class_scope_p ()
26647 || !TYPE_BEING_DEFINED (current_class_type)
26648 || friend_p);
26649
26650 /* Outside of a class-specifier, there must be a
26651 nested-name-specifier. Except in C++17 mode, where we
26652 might be declaring a guiding declaration. */
26653 if (!nested_name_specifier && outside_class_specifier_p
26654 && cxx_dialect < cxx17)
26655 constructor_p = false;
26656 else if (nested_name_specifier == error_mark_node)
26657 constructor_p = false;
26658
26659 /* If we have a class scope, this is easy; DR 147 says that S::S always
26660 names the constructor, and no other qualified name could. */
26661 if (constructor_p && nested_name_specifier
26662 && CLASS_TYPE_P (nested_name_specifier))
26663 {
26664 tree id = cp_parser_unqualified_id (parser,
26665 /*template_keyword_p=*/false,
26666 /*check_dependency_p=*/false,
26667 /*declarator_p=*/true,
26668 /*optional_p=*/false);
26669 if (is_overloaded_fn (id))
26670 id = DECL_NAME (get_first_fn (id));
26671 if (!constructor_name_p (id, nested_name_specifier))
26672 constructor_p = false;
26673 }
26674 /* If we still think that this might be a constructor-declarator,
26675 look for a class-name. */
26676 else if (constructor_p)
26677 {
26678 /* If we have:
26679
26680 template <typename T> struct S {
26681 S();
26682 };
26683
26684 we must recognize that the nested `S' names a class. */
26685 if (cxx_dialect >= cxx17)
26686 cp_parser_parse_tentatively (parser);
26687
26688 tree type_decl;
26689 type_decl = cp_parser_class_name (parser,
26690 /*typename_keyword_p=*/false,
26691 /*template_keyword_p=*/false,
26692 none_type,
26693 /*check_dependency_p=*/false,
26694 /*class_head_p=*/false,
26695 /*is_declaration=*/false);
26696
26697 if (cxx_dialect >= cxx17
26698 && !cp_parser_parse_definitely (parser))
26699 {
26700 type_decl = NULL_TREE;
26701 tree tmpl = cp_parser_template_name (parser,
26702 /*template_keyword*/false,
26703 /*check_dependency_p*/false,
26704 /*is_declaration*/false,
26705 none_type,
26706 /*is_identifier*/NULL);
26707 if (DECL_CLASS_TEMPLATE_P (tmpl)
26708 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26709 /* It's a deduction guide, return true. */;
26710 else
26711 cp_parser_simulate_error (parser);
26712 }
26713
26714 /* If there was no class-name, then this is not a constructor.
26715 Otherwise, if we are in a class-specifier and we aren't
26716 handling a friend declaration, check that its type matches
26717 current_class_type (c++/38313). Note: error_mark_node
26718 is left alone for error recovery purposes. */
26719 constructor_p = (!cp_parser_error_occurred (parser)
26720 && (outside_class_specifier_p
26721 || type_decl == NULL_TREE
26722 || type_decl == error_mark_node
26723 || same_type_p (current_class_type,
26724 TREE_TYPE (type_decl))));
26725
26726 /* If we're still considering a constructor, we have to see a `(',
26727 to begin the parameter-declaration-clause, followed by either a
26728 `)', an `...', or a decl-specifier. We need to check for a
26729 type-specifier to avoid being fooled into thinking that:
26730
26731 S (f) (int);
26732
26733 is a constructor. (It is actually a function named `f' that
26734 takes one parameter (of type `int') and returns a value of type
26735 `S'. */
26736 if (constructor_p
26737 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26738 constructor_p = false;
26739
26740 if (constructor_p
26741 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26742 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26743 /* A parameter declaration begins with a decl-specifier,
26744 which is either the "attribute" keyword, a storage class
26745 specifier, or (usually) a type-specifier. */
26746 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26747 {
26748 tree type;
26749 tree pushed_scope = NULL_TREE;
26750 unsigned saved_num_template_parameter_lists;
26751
26752 /* Names appearing in the type-specifier should be looked up
26753 in the scope of the class. */
26754 if (current_class_type)
26755 type = NULL_TREE;
26756 else if (type_decl)
26757 {
26758 type = TREE_TYPE (type_decl);
26759 if (TREE_CODE (type) == TYPENAME_TYPE)
26760 {
26761 type = resolve_typename_type (type,
26762 /*only_current_p=*/false);
26763 if (TREE_CODE (type) == TYPENAME_TYPE)
26764 {
26765 cp_parser_abort_tentative_parse (parser);
26766 return false;
26767 }
26768 }
26769 pushed_scope = push_scope (type);
26770 }
26771
26772 /* Inside the constructor parameter list, surrounding
26773 template-parameter-lists do not apply. */
26774 saved_num_template_parameter_lists
26775 = parser->num_template_parameter_lists;
26776 parser->num_template_parameter_lists = 0;
26777
26778 /* Look for the type-specifier. */
26779 cp_parser_type_specifier (parser,
26780 CP_PARSER_FLAGS_NONE,
26781 /*decl_specs=*/NULL,
26782 /*is_declarator=*/true,
26783 /*declares_class_or_enum=*/NULL,
26784 /*is_cv_qualifier=*/NULL);
26785
26786 parser->num_template_parameter_lists
26787 = saved_num_template_parameter_lists;
26788
26789 /* Leave the scope of the class. */
26790 if (pushed_scope)
26791 pop_scope (pushed_scope);
26792
26793 constructor_p = !cp_parser_error_occurred (parser);
26794 }
26795 }
26796
26797 /* We did not really want to consume any tokens. */
26798 cp_parser_abort_tentative_parse (parser);
26799
26800 return constructor_p;
26801 }
26802
26803 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26804 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26805 they must be performed once we are in the scope of the function.
26806
26807 Returns the function defined. */
26808
26809 static tree
26810 cp_parser_function_definition_from_specifiers_and_declarator
26811 (cp_parser* parser,
26812 cp_decl_specifier_seq *decl_specifiers,
26813 tree attributes,
26814 const cp_declarator *declarator)
26815 {
26816 tree fn;
26817 bool success_p;
26818
26819 /* Begin the function-definition. */
26820 success_p = start_function (decl_specifiers, declarator, attributes);
26821
26822 /* The things we're about to see are not directly qualified by any
26823 template headers we've seen thus far. */
26824 reset_specialization ();
26825
26826 /* If there were names looked up in the decl-specifier-seq that we
26827 did not check, check them now. We must wait until we are in the
26828 scope of the function to perform the checks, since the function
26829 might be a friend. */
26830 perform_deferred_access_checks (tf_warning_or_error);
26831
26832 if (success_p)
26833 {
26834 cp_finalize_omp_declare_simd (parser, current_function_decl);
26835 parser->omp_declare_simd = NULL;
26836 cp_finalize_oacc_routine (parser, current_function_decl, true);
26837 parser->oacc_routine = NULL;
26838 }
26839
26840 if (!success_p)
26841 {
26842 /* Skip the entire function. */
26843 cp_parser_skip_to_end_of_block_or_statement (parser);
26844 fn = error_mark_node;
26845 }
26846 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26847 {
26848 /* Seen already, skip it. An error message has already been output. */
26849 cp_parser_skip_to_end_of_block_or_statement (parser);
26850 fn = current_function_decl;
26851 current_function_decl = NULL_TREE;
26852 /* If this is a function from a class, pop the nested class. */
26853 if (current_class_name)
26854 pop_nested_class ();
26855 }
26856 else
26857 {
26858 timevar_id_t tv;
26859 if (DECL_DECLARED_INLINE_P (current_function_decl))
26860 tv = TV_PARSE_INLINE;
26861 else
26862 tv = TV_PARSE_FUNC;
26863 timevar_push (tv);
26864 fn = cp_parser_function_definition_after_declarator (parser,
26865 /*inline_p=*/false);
26866 timevar_pop (tv);
26867 }
26868
26869 return fn;
26870 }
26871
26872 /* Parse the part of a function-definition that follows the
26873 declarator. INLINE_P is TRUE iff this function is an inline
26874 function defined within a class-specifier.
26875
26876 Returns the function defined. */
26877
26878 static tree
26879 cp_parser_function_definition_after_declarator (cp_parser* parser,
26880 bool inline_p)
26881 {
26882 tree fn;
26883 bool saved_in_unbraced_linkage_specification_p;
26884 bool saved_in_function_body;
26885 unsigned saved_num_template_parameter_lists;
26886 cp_token *token;
26887 bool fully_implicit_function_template_p
26888 = parser->fully_implicit_function_template_p;
26889 parser->fully_implicit_function_template_p = false;
26890 tree implicit_template_parms
26891 = parser->implicit_template_parms;
26892 parser->implicit_template_parms = 0;
26893 cp_binding_level* implicit_template_scope
26894 = parser->implicit_template_scope;
26895 parser->implicit_template_scope = 0;
26896
26897 saved_in_function_body = parser->in_function_body;
26898 parser->in_function_body = true;
26899 /* If the next token is `return', then the code may be trying to
26900 make use of the "named return value" extension that G++ used to
26901 support. */
26902 token = cp_lexer_peek_token (parser->lexer);
26903 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26904 {
26905 /* Consume the `return' keyword. */
26906 cp_lexer_consume_token (parser->lexer);
26907 /* Look for the identifier that indicates what value is to be
26908 returned. */
26909 cp_parser_identifier (parser);
26910 /* Issue an error message. */
26911 error_at (token->location,
26912 "named return values are no longer supported");
26913 /* Skip tokens until we reach the start of the function body. */
26914 while (true)
26915 {
26916 cp_token *token = cp_lexer_peek_token (parser->lexer);
26917 if (token->type == CPP_OPEN_BRACE
26918 || token->type == CPP_EOF
26919 || token->type == CPP_PRAGMA_EOL)
26920 break;
26921 cp_lexer_consume_token (parser->lexer);
26922 }
26923 }
26924 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26925 anything declared inside `f'. */
26926 saved_in_unbraced_linkage_specification_p
26927 = parser->in_unbraced_linkage_specification_p;
26928 parser->in_unbraced_linkage_specification_p = false;
26929 /* Inside the function, surrounding template-parameter-lists do not
26930 apply. */
26931 saved_num_template_parameter_lists
26932 = parser->num_template_parameter_lists;
26933 parser->num_template_parameter_lists = 0;
26934
26935 /* If the next token is `try', `__transaction_atomic', or
26936 `__transaction_relaxed`, then we are looking at either function-try-block
26937 or function-transaction-block. Note that all of these include the
26938 function-body. */
26939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26940 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26941 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26942 RID_TRANSACTION_RELAXED))
26943 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26944 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26945 cp_parser_function_try_block (parser);
26946 else
26947 cp_parser_ctor_initializer_opt_and_function_body
26948 (parser, /*in_function_try_block=*/false);
26949
26950 /* Finish the function. */
26951 fn = finish_function (inline_p);
26952 /* Generate code for it, if necessary. */
26953 expand_or_defer_fn (fn);
26954 /* Restore the saved values. */
26955 parser->in_unbraced_linkage_specification_p
26956 = saved_in_unbraced_linkage_specification_p;
26957 parser->num_template_parameter_lists
26958 = saved_num_template_parameter_lists;
26959 parser->in_function_body = saved_in_function_body;
26960
26961 parser->fully_implicit_function_template_p
26962 = fully_implicit_function_template_p;
26963 parser->implicit_template_parms
26964 = implicit_template_parms;
26965 parser->implicit_template_scope
26966 = implicit_template_scope;
26967
26968 if (parser->fully_implicit_function_template_p)
26969 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26970
26971 return fn;
26972 }
26973
26974 /* Parse a template-declaration body (following argument list). */
26975
26976 static void
26977 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26978 tree parameter_list,
26979 bool member_p)
26980 {
26981 tree decl = NULL_TREE;
26982 bool friend_p = false;
26983
26984 /* We just processed one more parameter list. */
26985 ++parser->num_template_parameter_lists;
26986
26987 /* Get the deferred access checks from the parameter list. These
26988 will be checked once we know what is being declared, as for a
26989 member template the checks must be performed in the scope of the
26990 class containing the member. */
26991 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26992
26993 /* Tentatively parse for a new template parameter list, which can either be
26994 the template keyword or a template introduction. */
26995 if (cp_parser_template_declaration_after_export (parser, member_p))
26996 /* OK */;
26997 else if (cxx_dialect >= cxx11
26998 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26999 decl = cp_parser_alias_declaration (parser);
27000 else
27001 {
27002 /* There are no access checks when parsing a template, as we do not
27003 know if a specialization will be a friend. */
27004 push_deferring_access_checks (dk_no_check);
27005 cp_token *token = cp_lexer_peek_token (parser->lexer);
27006 decl = cp_parser_single_declaration (parser,
27007 checks,
27008 member_p,
27009 /*explicit_specialization_p=*/false,
27010 &friend_p);
27011 pop_deferring_access_checks ();
27012
27013 /* If this is a member template declaration, let the front
27014 end know. */
27015 if (member_p && !friend_p && decl)
27016 {
27017 if (TREE_CODE (decl) == TYPE_DECL)
27018 cp_parser_check_access_in_redeclaration (decl, token->location);
27019
27020 decl = finish_member_template_decl (decl);
27021 }
27022 else if (friend_p && decl
27023 && DECL_DECLARES_TYPE_P (decl))
27024 make_friend_class (current_class_type, TREE_TYPE (decl),
27025 /*complain=*/true);
27026 }
27027 /* We are done with the current parameter list. */
27028 --parser->num_template_parameter_lists;
27029
27030 pop_deferring_access_checks ();
27031
27032 /* Finish up. */
27033 finish_template_decl (parameter_list);
27034
27035 /* Check the template arguments for a literal operator template. */
27036 if (decl
27037 && DECL_DECLARES_FUNCTION_P (decl)
27038 && UDLIT_OPER_P (DECL_NAME (decl)))
27039 {
27040 bool ok = true;
27041 if (parameter_list == NULL_TREE)
27042 ok = false;
27043 else
27044 {
27045 int num_parms = TREE_VEC_LENGTH (parameter_list);
27046 if (num_parms == 1)
27047 {
27048 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27049 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27050 if (TREE_TYPE (parm) != char_type_node
27051 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27052 ok = false;
27053 }
27054 else if (num_parms == 2 && cxx_dialect >= cxx14)
27055 {
27056 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27057 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27058 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27059 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27060 if (parm == error_mark_node
27061 || TREE_TYPE (parm) != TREE_TYPE (type)
27062 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27063 ok = false;
27064 }
27065 else
27066 ok = false;
27067 }
27068 if (!ok)
27069 {
27070 if (cxx_dialect >= cxx14)
27071 error ("literal operator template %qD has invalid parameter list."
27072 " Expected non-type template argument pack <char...>"
27073 " or <typename CharT, CharT...>",
27074 decl);
27075 else
27076 error ("literal operator template %qD has invalid parameter list."
27077 " Expected non-type template argument pack <char...>",
27078 decl);
27079 }
27080 }
27081
27082 /* Register member declarations. */
27083 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27084 finish_member_declaration (decl);
27085 /* If DECL is a function template, we must return to parse it later.
27086 (Even though there is no definition, there might be default
27087 arguments that need handling.) */
27088 if (member_p && decl
27089 && DECL_DECLARES_FUNCTION_P (decl))
27090 vec_safe_push (unparsed_funs_with_definitions, decl);
27091 }
27092
27093 /* Parse a template introduction header for a template-declaration. Returns
27094 false if tentative parse fails. */
27095
27096 static bool
27097 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27098 {
27099 cp_parser_parse_tentatively (parser);
27100
27101 tree saved_scope = parser->scope;
27102 tree saved_object_scope = parser->object_scope;
27103 tree saved_qualifying_scope = parser->qualifying_scope;
27104
27105 /* Look for the optional `::' operator. */
27106 cp_parser_global_scope_opt (parser,
27107 /*current_scope_valid_p=*/false);
27108 /* Look for the nested-name-specifier. */
27109 cp_parser_nested_name_specifier_opt (parser,
27110 /*typename_keyword_p=*/false,
27111 /*check_dependency_p=*/true,
27112 /*type_p=*/false,
27113 /*is_declaration=*/false);
27114
27115 cp_token *token = cp_lexer_peek_token (parser->lexer);
27116 tree concept_name = cp_parser_identifier (parser);
27117
27118 /* Look up the concept for which we will be matching
27119 template parameters. */
27120 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27121 token->location);
27122 parser->scope = saved_scope;
27123 parser->object_scope = saved_object_scope;
27124 parser->qualifying_scope = saved_qualifying_scope;
27125
27126 if (concept_name == error_mark_node)
27127 cp_parser_simulate_error (parser);
27128
27129 /* Look for opening brace for introduction. */
27130 matching_braces braces;
27131 braces.require_open (parser);
27132
27133 if (!cp_parser_parse_definitely (parser))
27134 return false;
27135
27136 push_deferring_access_checks (dk_deferred);
27137
27138 /* Build vector of placeholder parameters and grab
27139 matching identifiers. */
27140 tree introduction_list = cp_parser_introduction_list (parser);
27141
27142 /* The introduction-list shall not be empty. */
27143 int nargs = TREE_VEC_LENGTH (introduction_list);
27144 if (nargs == 0)
27145 {
27146 error ("empty introduction-list");
27147 return true;
27148 }
27149
27150 /* Look for closing brace for introduction. */
27151 if (!braces.require_close (parser))
27152 return true;
27153
27154 if (tmpl_decl == error_mark_node)
27155 {
27156 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27157 token->location);
27158 return true;
27159 }
27160
27161 /* Build and associate the constraint. */
27162 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27163 if (parms && parms != error_mark_node)
27164 {
27165 cp_parser_template_declaration_after_parameters (parser, parms,
27166 member_p);
27167 return true;
27168 }
27169
27170 error_at (token->location, "no matching concept for template-introduction");
27171 return true;
27172 }
27173
27174 /* Parse a normal template-declaration following the template keyword. */
27175
27176 static void
27177 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27178 {
27179 tree parameter_list;
27180 bool need_lang_pop;
27181 location_t location = input_location;
27182
27183 /* Look for the `<' token. */
27184 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27185 return;
27186 if (at_class_scope_p () && current_function_decl)
27187 {
27188 /* 14.5.2.2 [temp.mem]
27189
27190 A local class shall not have member templates. */
27191 error_at (location,
27192 "invalid declaration of member template in local class");
27193 cp_parser_skip_to_end_of_block_or_statement (parser);
27194 return;
27195 }
27196 /* [temp]
27197
27198 A template ... shall not have C linkage. */
27199 if (current_lang_name == lang_name_c)
27200 {
27201 error_at (location, "template with C linkage");
27202 maybe_show_extern_c_location ();
27203 /* Give it C++ linkage to avoid confusing other parts of the
27204 front end. */
27205 push_lang_context (lang_name_cplusplus);
27206 need_lang_pop = true;
27207 }
27208 else
27209 need_lang_pop = false;
27210
27211 /* We cannot perform access checks on the template parameter
27212 declarations until we know what is being declared, just as we
27213 cannot check the decl-specifier list. */
27214 push_deferring_access_checks (dk_deferred);
27215
27216 /* If the next token is `>', then we have an invalid
27217 specialization. Rather than complain about an invalid template
27218 parameter, issue an error message here. */
27219 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27220 {
27221 cp_parser_error (parser, "invalid explicit specialization");
27222 begin_specialization ();
27223 parameter_list = NULL_TREE;
27224 }
27225 else
27226 {
27227 /* Parse the template parameters. */
27228 parameter_list = cp_parser_template_parameter_list (parser);
27229 }
27230
27231 /* Look for the `>'. */
27232 cp_parser_skip_to_end_of_template_parameter_list (parser);
27233
27234 /* Manage template requirements */
27235 if (flag_concepts)
27236 {
27237 tree reqs = get_shorthand_constraints (current_template_parms);
27238 if (tree r = cp_parser_requires_clause_opt (parser))
27239 reqs = conjoin_constraints (reqs, normalize_expression (r));
27240 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27241 }
27242
27243 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27244 member_p);
27245
27246 /* For the erroneous case of a template with C linkage, we pushed an
27247 implicit C++ linkage scope; exit that scope now. */
27248 if (need_lang_pop)
27249 pop_lang_context ();
27250 }
27251
27252 /* Parse a template-declaration, assuming that the `export' (and
27253 `extern') keywords, if present, has already been scanned. MEMBER_P
27254 is as for cp_parser_template_declaration. */
27255
27256 static bool
27257 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27258 {
27259 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27260 {
27261 cp_lexer_consume_token (parser->lexer);
27262 cp_parser_explicit_template_declaration (parser, member_p);
27263 return true;
27264 }
27265 else if (flag_concepts)
27266 return cp_parser_template_introduction (parser, member_p);
27267
27268 return false;
27269 }
27270
27271 /* Perform the deferred access checks from a template-parameter-list.
27272 CHECKS is a TREE_LIST of access checks, as returned by
27273 get_deferred_access_checks. */
27274
27275 static void
27276 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27277 {
27278 ++processing_template_parmlist;
27279 perform_access_checks (checks, tf_warning_or_error);
27280 --processing_template_parmlist;
27281 }
27282
27283 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27284 `function-definition' sequence that follows a template header.
27285 If MEMBER_P is true, this declaration appears in a class scope.
27286
27287 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27288 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27289
27290 static tree
27291 cp_parser_single_declaration (cp_parser* parser,
27292 vec<deferred_access_check, va_gc> *checks,
27293 bool member_p,
27294 bool explicit_specialization_p,
27295 bool* friend_p)
27296 {
27297 int declares_class_or_enum;
27298 tree decl = NULL_TREE;
27299 cp_decl_specifier_seq decl_specifiers;
27300 bool function_definition_p = false;
27301 cp_token *decl_spec_token_start;
27302
27303 /* This function is only used when processing a template
27304 declaration. */
27305 gcc_assert (innermost_scope_kind () == sk_template_parms
27306 || innermost_scope_kind () == sk_template_spec);
27307
27308 /* Defer access checks until we know what is being declared. */
27309 push_deferring_access_checks (dk_deferred);
27310
27311 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27312 alternative. */
27313 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27314 cp_parser_decl_specifier_seq (parser,
27315 CP_PARSER_FLAGS_OPTIONAL,
27316 &decl_specifiers,
27317 &declares_class_or_enum);
27318 if (friend_p)
27319 *friend_p = cp_parser_friend_p (&decl_specifiers);
27320
27321 /* There are no template typedefs. */
27322 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27323 {
27324 error_at (decl_spec_token_start->location,
27325 "template declaration of %<typedef%>");
27326 decl = error_mark_node;
27327 }
27328
27329 /* Gather up the access checks that occurred the
27330 decl-specifier-seq. */
27331 stop_deferring_access_checks ();
27332
27333 /* Check for the declaration of a template class. */
27334 if (declares_class_or_enum)
27335 {
27336 if (cp_parser_declares_only_class_p (parser)
27337 || (declares_class_or_enum & 2))
27338 {
27339 // If this is a declaration, but not a definition, associate
27340 // any constraints with the type declaration. Constraints
27341 // are associated with definitions in cp_parser_class_specifier.
27342 if (declares_class_or_enum == 1)
27343 associate_classtype_constraints (decl_specifiers.type);
27344
27345 decl = shadow_tag (&decl_specifiers);
27346
27347 /* In this case:
27348
27349 struct C {
27350 friend template <typename T> struct A<T>::B;
27351 };
27352
27353 A<T>::B will be represented by a TYPENAME_TYPE, and
27354 therefore not recognized by shadow_tag. */
27355 if (friend_p && *friend_p
27356 && !decl
27357 && decl_specifiers.type
27358 && TYPE_P (decl_specifiers.type))
27359 decl = decl_specifiers.type;
27360
27361 if (decl && decl != error_mark_node)
27362 decl = TYPE_NAME (decl);
27363 else
27364 decl = error_mark_node;
27365
27366 /* Perform access checks for template parameters. */
27367 cp_parser_perform_template_parameter_access_checks (checks);
27368
27369 /* Give a helpful diagnostic for
27370 template <class T> struct A { } a;
27371 if we aren't already recovering from an error. */
27372 if (!cp_parser_declares_only_class_p (parser)
27373 && !seen_error ())
27374 {
27375 error_at (cp_lexer_peek_token (parser->lexer)->location,
27376 "a class template declaration must not declare "
27377 "anything else");
27378 cp_parser_skip_to_end_of_block_or_statement (parser);
27379 goto out;
27380 }
27381 }
27382 }
27383
27384 /* Complain about missing 'typename' or other invalid type names. */
27385 if (!decl_specifiers.any_type_specifiers_p
27386 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27387 {
27388 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27389 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27390 the rest of this declaration. */
27391 decl = error_mark_node;
27392 goto out;
27393 }
27394
27395 /* If it's not a template class, try for a template function. If
27396 the next token is a `;', then this declaration does not declare
27397 anything. But, if there were errors in the decl-specifiers, then
27398 the error might well have come from an attempted class-specifier.
27399 In that case, there's no need to warn about a missing declarator. */
27400 if (!decl
27401 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27402 || decl_specifiers.type != error_mark_node))
27403 {
27404 decl = cp_parser_init_declarator (parser,
27405 &decl_specifiers,
27406 checks,
27407 /*function_definition_allowed_p=*/true,
27408 member_p,
27409 declares_class_or_enum,
27410 &function_definition_p,
27411 NULL, NULL, NULL);
27412
27413 /* 7.1.1-1 [dcl.stc]
27414
27415 A storage-class-specifier shall not be specified in an explicit
27416 specialization... */
27417 if (decl
27418 && explicit_specialization_p
27419 && decl_specifiers.storage_class != sc_none)
27420 {
27421 error_at (decl_spec_token_start->location,
27422 "explicit template specialization cannot have a storage class");
27423 decl = error_mark_node;
27424 }
27425
27426 if (decl && VAR_P (decl))
27427 check_template_variable (decl);
27428 }
27429
27430 /* Look for a trailing `;' after the declaration. */
27431 if (!function_definition_p
27432 && (decl == error_mark_node
27433 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27434 cp_parser_skip_to_end_of_block_or_statement (parser);
27435
27436 out:
27437 pop_deferring_access_checks ();
27438
27439 /* Clear any current qualification; whatever comes next is the start
27440 of something new. */
27441 parser->scope = NULL_TREE;
27442 parser->qualifying_scope = NULL_TREE;
27443 parser->object_scope = NULL_TREE;
27444
27445 return decl;
27446 }
27447
27448 /* Parse a cast-expression that is not the operand of a unary "&". */
27449
27450 static cp_expr
27451 cp_parser_simple_cast_expression (cp_parser *parser)
27452 {
27453 return cp_parser_cast_expression (parser, /*address_p=*/false,
27454 /*cast_p=*/false, /*decltype*/false, NULL);
27455 }
27456
27457 /* Parse a functional cast to TYPE. Returns an expression
27458 representing the cast. */
27459
27460 static cp_expr
27461 cp_parser_functional_cast (cp_parser* parser, tree type)
27462 {
27463 vec<tree, va_gc> *vec;
27464 tree expression_list;
27465 cp_expr cast;
27466 bool nonconst_p;
27467
27468 location_t start_loc = input_location;
27469
27470 if (!type)
27471 type = error_mark_node;
27472
27473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27474 {
27475 cp_lexer_set_source_position (parser->lexer);
27476 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27477 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27478 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27479 if (TREE_CODE (type) == TYPE_DECL)
27480 type = TREE_TYPE (type);
27481
27482 cast = finish_compound_literal (type, expression_list,
27483 tf_warning_or_error, fcl_functional);
27484 /* Create a location of the form:
27485 type_name{i, f}
27486 ^~~~~~~~~~~~~~~
27487 with caret == start at the start of the type name,
27488 finishing at the closing brace. */
27489 location_t finish_loc
27490 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27491 location_t combined_loc = make_location (start_loc, start_loc,
27492 finish_loc);
27493 cast.set_location (combined_loc);
27494 return cast;
27495 }
27496
27497
27498 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27499 /*cast_p=*/true,
27500 /*allow_expansion_p=*/true,
27501 /*non_constant_p=*/NULL);
27502 if (vec == NULL)
27503 expression_list = error_mark_node;
27504 else
27505 {
27506 expression_list = build_tree_list_vec (vec);
27507 release_tree_vector (vec);
27508 }
27509
27510 cast = build_functional_cast (type, expression_list,
27511 tf_warning_or_error);
27512 /* [expr.const]/1: In an integral constant expression "only type
27513 conversions to integral or enumeration type can be used". */
27514 if (TREE_CODE (type) == TYPE_DECL)
27515 type = TREE_TYPE (type);
27516 if (cast != error_mark_node
27517 && !cast_valid_in_integral_constant_expression_p (type)
27518 && cp_parser_non_integral_constant_expression (parser,
27519 NIC_CONSTRUCTOR))
27520 return error_mark_node;
27521
27522 /* Create a location of the form:
27523 float(i)
27524 ^~~~~~~~
27525 with caret == start at the start of the type name,
27526 finishing at the closing paren. */
27527 location_t finish_loc
27528 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27529 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27530 cast.set_location (combined_loc);
27531 return cast;
27532 }
27533
27534 /* Save the tokens that make up the body of a member function defined
27535 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27536 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27537 specifiers applied to the declaration. Returns the FUNCTION_DECL
27538 for the member function. */
27539
27540 static tree
27541 cp_parser_save_member_function_body (cp_parser* parser,
27542 cp_decl_specifier_seq *decl_specifiers,
27543 cp_declarator *declarator,
27544 tree attributes)
27545 {
27546 cp_token *first;
27547 cp_token *last;
27548 tree fn;
27549 bool function_try_block = false;
27550
27551 /* Create the FUNCTION_DECL. */
27552 fn = grokmethod (decl_specifiers, declarator, attributes);
27553 cp_finalize_omp_declare_simd (parser, fn);
27554 cp_finalize_oacc_routine (parser, fn, true);
27555 /* If something went badly wrong, bail out now. */
27556 if (fn == error_mark_node)
27557 {
27558 /* If there's a function-body, skip it. */
27559 if (cp_parser_token_starts_function_definition_p
27560 (cp_lexer_peek_token (parser->lexer)))
27561 cp_parser_skip_to_end_of_block_or_statement (parser);
27562 return error_mark_node;
27563 }
27564
27565 /* Remember it, if there default args to post process. */
27566 cp_parser_save_default_args (parser, fn);
27567
27568 /* Save away the tokens that make up the body of the
27569 function. */
27570 first = parser->lexer->next_token;
27571
27572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27573 cp_lexer_consume_token (parser->lexer);
27574 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27575 RID_TRANSACTION_ATOMIC))
27576 {
27577 cp_lexer_consume_token (parser->lexer);
27578 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27580 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27581 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27582 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27583 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27584 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27585 {
27586 cp_lexer_consume_token (parser->lexer);
27587 cp_lexer_consume_token (parser->lexer);
27588 cp_lexer_consume_token (parser->lexer);
27589 cp_lexer_consume_token (parser->lexer);
27590 cp_lexer_consume_token (parser->lexer);
27591 }
27592 else
27593 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27594 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27595 {
27596 cp_lexer_consume_token (parser->lexer);
27597 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27598 break;
27599 }
27600 }
27601
27602 /* Handle function try blocks. */
27603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27604 {
27605 cp_lexer_consume_token (parser->lexer);
27606 function_try_block = true;
27607 }
27608 /* We can have braced-init-list mem-initializers before the fn body. */
27609 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27610 {
27611 cp_lexer_consume_token (parser->lexer);
27612 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27613 {
27614 /* cache_group will stop after an un-nested { } pair, too. */
27615 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27616 break;
27617
27618 /* variadic mem-inits have ... after the ')'. */
27619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27620 cp_lexer_consume_token (parser->lexer);
27621 }
27622 }
27623 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27624 /* Handle function try blocks. */
27625 if (function_try_block)
27626 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27627 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27628 last = parser->lexer->next_token;
27629
27630 /* Save away the inline definition; we will process it when the
27631 class is complete. */
27632 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27633 DECL_PENDING_INLINE_P (fn) = 1;
27634
27635 /* We need to know that this was defined in the class, so that
27636 friend templates are handled correctly. */
27637 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27638
27639 /* Add FN to the queue of functions to be parsed later. */
27640 vec_safe_push (unparsed_funs_with_definitions, fn);
27641
27642 return fn;
27643 }
27644
27645 /* Save the tokens that make up the in-class initializer for a non-static
27646 data member. Returns a DEFAULT_ARG. */
27647
27648 static tree
27649 cp_parser_save_nsdmi (cp_parser* parser)
27650 {
27651 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27652 }
27653
27654 /* Parse a template-argument-list, as well as the trailing ">" (but
27655 not the opening "<"). See cp_parser_template_argument_list for the
27656 return value. */
27657
27658 static tree
27659 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27660 {
27661 tree arguments;
27662 tree saved_scope;
27663 tree saved_qualifying_scope;
27664 tree saved_object_scope;
27665 bool saved_greater_than_is_operator_p;
27666 int saved_unevaluated_operand;
27667 int saved_inhibit_evaluation_warnings;
27668
27669 /* [temp.names]
27670
27671 When parsing a template-id, the first non-nested `>' is taken as
27672 the end of the template-argument-list rather than a greater-than
27673 operator. */
27674 saved_greater_than_is_operator_p
27675 = parser->greater_than_is_operator_p;
27676 parser->greater_than_is_operator_p = false;
27677 /* Parsing the argument list may modify SCOPE, so we save it
27678 here. */
27679 saved_scope = parser->scope;
27680 saved_qualifying_scope = parser->qualifying_scope;
27681 saved_object_scope = parser->object_scope;
27682 /* We need to evaluate the template arguments, even though this
27683 template-id may be nested within a "sizeof". */
27684 saved_unevaluated_operand = cp_unevaluated_operand;
27685 cp_unevaluated_operand = 0;
27686 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27687 c_inhibit_evaluation_warnings = 0;
27688 /* Parse the template-argument-list itself. */
27689 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27690 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27691 arguments = NULL_TREE;
27692 else
27693 arguments = cp_parser_template_argument_list (parser);
27694 /* Look for the `>' that ends the template-argument-list. If we find
27695 a '>>' instead, it's probably just a typo. */
27696 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27697 {
27698 if (cxx_dialect != cxx98)
27699 {
27700 /* In C++0x, a `>>' in a template argument list or cast
27701 expression is considered to be two separate `>'
27702 tokens. So, change the current token to a `>', but don't
27703 consume it: it will be consumed later when the outer
27704 template argument list (or cast expression) is parsed.
27705 Note that this replacement of `>' for `>>' is necessary
27706 even if we are parsing tentatively: in the tentative
27707 case, after calling
27708 cp_parser_enclosed_template_argument_list we will always
27709 throw away all of the template arguments and the first
27710 closing `>', either because the template argument list
27711 was erroneous or because we are replacing those tokens
27712 with a CPP_TEMPLATE_ID token. The second `>' (which will
27713 not have been thrown away) is needed either to close an
27714 outer template argument list or to complete a new-style
27715 cast. */
27716 cp_token *token = cp_lexer_peek_token (parser->lexer);
27717 token->type = CPP_GREATER;
27718 }
27719 else if (!saved_greater_than_is_operator_p)
27720 {
27721 /* If we're in a nested template argument list, the '>>' has
27722 to be a typo for '> >'. We emit the error message, but we
27723 continue parsing and we push a '>' as next token, so that
27724 the argument list will be parsed correctly. Note that the
27725 global source location is still on the token before the
27726 '>>', so we need to say explicitly where we want it. */
27727 cp_token *token = cp_lexer_peek_token (parser->lexer);
27728 gcc_rich_location richloc (token->location);
27729 richloc.add_fixit_replace ("> >");
27730 error_at (&richloc, "%<>>%> should be %<> >%> "
27731 "within a nested template argument list");
27732
27733 token->type = CPP_GREATER;
27734 }
27735 else
27736 {
27737 /* If this is not a nested template argument list, the '>>'
27738 is a typo for '>'. Emit an error message and continue.
27739 Same deal about the token location, but here we can get it
27740 right by consuming the '>>' before issuing the diagnostic. */
27741 cp_token *token = cp_lexer_consume_token (parser->lexer);
27742 error_at (token->location,
27743 "spurious %<>>%>, use %<>%> to terminate "
27744 "a template argument list");
27745 }
27746 }
27747 else
27748 cp_parser_skip_to_end_of_template_parameter_list (parser);
27749 /* The `>' token might be a greater-than operator again now. */
27750 parser->greater_than_is_operator_p
27751 = saved_greater_than_is_operator_p;
27752 /* Restore the SAVED_SCOPE. */
27753 parser->scope = saved_scope;
27754 parser->qualifying_scope = saved_qualifying_scope;
27755 parser->object_scope = saved_object_scope;
27756 cp_unevaluated_operand = saved_unevaluated_operand;
27757 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27758
27759 return arguments;
27760 }
27761
27762 /* MEMBER_FUNCTION is a member function, or a friend. If default
27763 arguments, or the body of the function have not yet been parsed,
27764 parse them now. */
27765
27766 static void
27767 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27768 {
27769 timevar_push (TV_PARSE_INMETH);
27770 /* If this member is a template, get the underlying
27771 FUNCTION_DECL. */
27772 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27773 member_function = DECL_TEMPLATE_RESULT (member_function);
27774
27775 /* There should not be any class definitions in progress at this
27776 point; the bodies of members are only parsed outside of all class
27777 definitions. */
27778 gcc_assert (parser->num_classes_being_defined == 0);
27779 /* While we're parsing the member functions we might encounter more
27780 classes. We want to handle them right away, but we don't want
27781 them getting mixed up with functions that are currently in the
27782 queue. */
27783 push_unparsed_function_queues (parser);
27784
27785 /* Make sure that any template parameters are in scope. */
27786 maybe_begin_member_template_processing (member_function);
27787
27788 /* If the body of the function has not yet been parsed, parse it
27789 now. */
27790 if (DECL_PENDING_INLINE_P (member_function))
27791 {
27792 tree function_scope;
27793 cp_token_cache *tokens;
27794
27795 /* The function is no longer pending; we are processing it. */
27796 tokens = DECL_PENDING_INLINE_INFO (member_function);
27797 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27798 DECL_PENDING_INLINE_P (member_function) = 0;
27799
27800 /* If this is a local class, enter the scope of the containing
27801 function. */
27802 function_scope = current_function_decl;
27803 if (function_scope)
27804 push_function_context ();
27805
27806 /* Push the body of the function onto the lexer stack. */
27807 cp_parser_push_lexer_for_tokens (parser, tokens);
27808
27809 /* Let the front end know that we going to be defining this
27810 function. */
27811 start_preparsed_function (member_function, NULL_TREE,
27812 SF_PRE_PARSED | SF_INCLASS_INLINE);
27813
27814 /* Don't do access checking if it is a templated function. */
27815 if (processing_template_decl)
27816 push_deferring_access_checks (dk_no_check);
27817
27818 /* #pragma omp declare reduction needs special parsing. */
27819 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27820 {
27821 parser->lexer->in_pragma = true;
27822 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27823 finish_function (/*inline_p=*/true);
27824 cp_check_omp_declare_reduction (member_function);
27825 }
27826 else
27827 /* Now, parse the body of the function. */
27828 cp_parser_function_definition_after_declarator (parser,
27829 /*inline_p=*/true);
27830
27831 if (processing_template_decl)
27832 pop_deferring_access_checks ();
27833
27834 /* Leave the scope of the containing function. */
27835 if (function_scope)
27836 pop_function_context ();
27837 cp_parser_pop_lexer (parser);
27838 }
27839
27840 /* Remove any template parameters from the symbol table. */
27841 maybe_end_member_template_processing ();
27842
27843 /* Restore the queue. */
27844 pop_unparsed_function_queues (parser);
27845 timevar_pop (TV_PARSE_INMETH);
27846 }
27847
27848 /* If DECL contains any default args, remember it on the unparsed
27849 functions queue. */
27850
27851 static void
27852 cp_parser_save_default_args (cp_parser* parser, tree decl)
27853 {
27854 tree probe;
27855
27856 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27857 probe;
27858 probe = TREE_CHAIN (probe))
27859 if (TREE_PURPOSE (probe))
27860 {
27861 cp_default_arg_entry entry = {current_class_type, decl};
27862 vec_safe_push (unparsed_funs_with_default_args, entry);
27863 break;
27864 }
27865 }
27866
27867 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27868 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27869 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27870 from the parameter-type-list. */
27871
27872 static tree
27873 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27874 tree default_arg, tree parmtype)
27875 {
27876 cp_token_cache *tokens;
27877 tree parsed_arg;
27878 bool dummy;
27879
27880 if (default_arg == error_mark_node)
27881 return error_mark_node;
27882
27883 /* Push the saved tokens for the default argument onto the parser's
27884 lexer stack. */
27885 tokens = DEFARG_TOKENS (default_arg);
27886 cp_parser_push_lexer_for_tokens (parser, tokens);
27887
27888 start_lambda_scope (decl);
27889
27890 /* Parse the default argument. */
27891 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27892 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27893 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27894
27895 finish_lambda_scope ();
27896
27897 if (parsed_arg == error_mark_node)
27898 cp_parser_skip_to_end_of_statement (parser);
27899
27900 if (!processing_template_decl)
27901 {
27902 /* In a non-template class, check conversions now. In a template,
27903 we'll wait and instantiate these as needed. */
27904 if (TREE_CODE (decl) == PARM_DECL)
27905 parsed_arg = check_default_argument (parmtype, parsed_arg,
27906 tf_warning_or_error);
27907 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27908 parsed_arg = error_mark_node;
27909 else
27910 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27911 }
27912
27913 /* If the token stream has not been completely used up, then
27914 there was extra junk after the end of the default
27915 argument. */
27916 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27917 {
27918 if (TREE_CODE (decl) == PARM_DECL)
27919 cp_parser_error (parser, "expected %<,%>");
27920 else
27921 cp_parser_error (parser, "expected %<;%>");
27922 }
27923
27924 /* Revert to the main lexer. */
27925 cp_parser_pop_lexer (parser);
27926
27927 return parsed_arg;
27928 }
27929
27930 /* FIELD is a non-static data member with an initializer which we saved for
27931 later; parse it now. */
27932
27933 static void
27934 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27935 {
27936 tree def;
27937
27938 maybe_begin_member_template_processing (field);
27939
27940 push_unparsed_function_queues (parser);
27941 def = cp_parser_late_parse_one_default_arg (parser, field,
27942 DECL_INITIAL (field),
27943 NULL_TREE);
27944 pop_unparsed_function_queues (parser);
27945
27946 maybe_end_member_template_processing ();
27947
27948 DECL_INITIAL (field) = def;
27949 }
27950
27951 /* FN is a FUNCTION_DECL which may contains a parameter with an
27952 unparsed DEFAULT_ARG. Parse the default args now. This function
27953 assumes that the current scope is the scope in which the default
27954 argument should be processed. */
27955
27956 static void
27957 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27958 {
27959 bool saved_local_variables_forbidden_p;
27960 tree parm, parmdecl;
27961
27962 /* While we're parsing the default args, we might (due to the
27963 statement expression extension) encounter more classes. We want
27964 to handle them right away, but we don't want them getting mixed
27965 up with default args that are currently in the queue. */
27966 push_unparsed_function_queues (parser);
27967
27968 /* Local variable names (and the `this' keyword) may not appear
27969 in a default argument. */
27970 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27971 parser->local_variables_forbidden_p = true;
27972
27973 push_defarg_context (fn);
27974
27975 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27976 parmdecl = DECL_ARGUMENTS (fn);
27977 parm && parm != void_list_node;
27978 parm = TREE_CHAIN (parm),
27979 parmdecl = DECL_CHAIN (parmdecl))
27980 {
27981 tree default_arg = TREE_PURPOSE (parm);
27982 tree parsed_arg;
27983 vec<tree, va_gc> *insts;
27984 tree copy;
27985 unsigned ix;
27986
27987 if (!default_arg)
27988 continue;
27989
27990 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27991 /* This can happen for a friend declaration for a function
27992 already declared with default arguments. */
27993 continue;
27994
27995 parsed_arg
27996 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27997 default_arg,
27998 TREE_VALUE (parm));
27999 TREE_PURPOSE (parm) = parsed_arg;
28000
28001 /* Update any instantiations we've already created. */
28002 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28003 vec_safe_iterate (insts, ix, &copy); ix++)
28004 TREE_PURPOSE (copy) = parsed_arg;
28005 }
28006
28007 pop_defarg_context ();
28008
28009 /* Make sure no default arg is missing. */
28010 check_default_args (fn);
28011
28012 /* Restore the state of local_variables_forbidden_p. */
28013 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28014
28015 /* Restore the queue. */
28016 pop_unparsed_function_queues (parser);
28017 }
28018
28019 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28020
28021 sizeof ... ( identifier )
28022
28023 where the 'sizeof' token has already been consumed. */
28024
28025 static tree
28026 cp_parser_sizeof_pack (cp_parser *parser)
28027 {
28028 /* Consume the `...'. */
28029 cp_lexer_consume_token (parser->lexer);
28030 maybe_warn_variadic_templates ();
28031
28032 matching_parens parens;
28033 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28034 if (paren)
28035 parens.consume_open (parser);
28036 else
28037 permerror (cp_lexer_peek_token (parser->lexer)->location,
28038 "%<sizeof...%> argument must be surrounded by parentheses");
28039
28040 cp_token *token = cp_lexer_peek_token (parser->lexer);
28041 tree name = cp_parser_identifier (parser);
28042 if (name == error_mark_node)
28043 return error_mark_node;
28044 /* The name is not qualified. */
28045 parser->scope = NULL_TREE;
28046 parser->qualifying_scope = NULL_TREE;
28047 parser->object_scope = NULL_TREE;
28048 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28049 if (expr == error_mark_node)
28050 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28051 token->location);
28052 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28053 expr = TREE_TYPE (expr);
28054 else if (TREE_CODE (expr) == CONST_DECL)
28055 expr = DECL_INITIAL (expr);
28056 expr = make_pack_expansion (expr);
28057 PACK_EXPANSION_SIZEOF_P (expr) = true;
28058
28059 if (paren)
28060 parens.require_close (parser);
28061
28062 return expr;
28063 }
28064
28065 /* Parse the operand of `sizeof' (or a similar operator). Returns
28066 either a TYPE or an expression, depending on the form of the
28067 input. The KEYWORD indicates which kind of expression we have
28068 encountered. */
28069
28070 static tree
28071 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28072 {
28073 tree expr = NULL_TREE;
28074 const char *saved_message;
28075 char *tmp;
28076 bool saved_integral_constant_expression_p;
28077 bool saved_non_integral_constant_expression_p;
28078
28079 /* If it's a `...', then we are computing the length of a parameter
28080 pack. */
28081 if (keyword == RID_SIZEOF
28082 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28083 return cp_parser_sizeof_pack (parser);
28084
28085 /* Types cannot be defined in a `sizeof' expression. Save away the
28086 old message. */
28087 saved_message = parser->type_definition_forbidden_message;
28088 /* And create the new one. */
28089 tmp = concat ("types may not be defined in %<",
28090 IDENTIFIER_POINTER (ridpointers[keyword]),
28091 "%> expressions", NULL);
28092 parser->type_definition_forbidden_message = tmp;
28093
28094 /* The restrictions on constant-expressions do not apply inside
28095 sizeof expressions. */
28096 saved_integral_constant_expression_p
28097 = parser->integral_constant_expression_p;
28098 saved_non_integral_constant_expression_p
28099 = parser->non_integral_constant_expression_p;
28100 parser->integral_constant_expression_p = false;
28101
28102 /* Do not actually evaluate the expression. */
28103 ++cp_unevaluated_operand;
28104 ++c_inhibit_evaluation_warnings;
28105 /* If it's a `(', then we might be looking at the type-id
28106 construction. */
28107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28108 {
28109 tree type = NULL_TREE;
28110
28111 /* We can't be sure yet whether we're looking at a type-id or an
28112 expression. */
28113 cp_parser_parse_tentatively (parser);
28114
28115 matching_parens parens;
28116 parens.consume_open (parser);
28117
28118 /* Note: as a GNU Extension, compound literals are considered
28119 postfix-expressions as they are in C99, so they are valid
28120 arguments to sizeof. See comment in cp_parser_cast_expression
28121 for details. */
28122 if (cp_parser_compound_literal_p (parser))
28123 cp_parser_simulate_error (parser);
28124 else
28125 {
28126 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28127 parser->in_type_id_in_expr_p = true;
28128 /* Look for the type-id. */
28129 type = cp_parser_type_id (parser);
28130 /* Look for the closing `)'. */
28131 parens.require_close (parser);
28132 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28133 }
28134
28135 /* If all went well, then we're done. */
28136 if (cp_parser_parse_definitely (parser))
28137 {
28138 cp_decl_specifier_seq decl_specs;
28139
28140 /* Build a trivial decl-specifier-seq. */
28141 clear_decl_specs (&decl_specs);
28142 decl_specs.type = type;
28143
28144 /* Call grokdeclarator to figure out what type this is. */
28145 expr = grokdeclarator (NULL,
28146 &decl_specs,
28147 TYPENAME,
28148 /*initialized=*/0,
28149 /*attrlist=*/NULL);
28150 }
28151 }
28152
28153 /* If the type-id production did not work out, then we must be
28154 looking at the unary-expression production. */
28155 if (!expr)
28156 expr = cp_parser_unary_expression (parser);
28157
28158 /* Go back to evaluating expressions. */
28159 --cp_unevaluated_operand;
28160 --c_inhibit_evaluation_warnings;
28161
28162 /* Free the message we created. */
28163 free (tmp);
28164 /* And restore the old one. */
28165 parser->type_definition_forbidden_message = saved_message;
28166 parser->integral_constant_expression_p
28167 = saved_integral_constant_expression_p;
28168 parser->non_integral_constant_expression_p
28169 = saved_non_integral_constant_expression_p;
28170
28171 return expr;
28172 }
28173
28174 /* If the current declaration has no declarator, return true. */
28175
28176 static bool
28177 cp_parser_declares_only_class_p (cp_parser *parser)
28178 {
28179 /* If the next token is a `;' or a `,' then there is no
28180 declarator. */
28181 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28182 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28183 }
28184
28185 /* Update the DECL_SPECS to reflect the storage class indicated by
28186 KEYWORD. */
28187
28188 static void
28189 cp_parser_set_storage_class (cp_parser *parser,
28190 cp_decl_specifier_seq *decl_specs,
28191 enum rid keyword,
28192 cp_token *token)
28193 {
28194 cp_storage_class storage_class;
28195
28196 if (parser->in_unbraced_linkage_specification_p)
28197 {
28198 error_at (token->location, "invalid use of %qD in linkage specification",
28199 ridpointers[keyword]);
28200 return;
28201 }
28202 else if (decl_specs->storage_class != sc_none)
28203 {
28204 decl_specs->conflicting_specifiers_p = true;
28205 return;
28206 }
28207
28208 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28209 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28210 && decl_specs->gnu_thread_keyword_p)
28211 {
28212 pedwarn (decl_specs->locations[ds_thread], 0,
28213 "%<__thread%> before %qD", ridpointers[keyword]);
28214 }
28215
28216 switch (keyword)
28217 {
28218 case RID_AUTO:
28219 storage_class = sc_auto;
28220 break;
28221 case RID_REGISTER:
28222 storage_class = sc_register;
28223 break;
28224 case RID_STATIC:
28225 storage_class = sc_static;
28226 break;
28227 case RID_EXTERN:
28228 storage_class = sc_extern;
28229 break;
28230 case RID_MUTABLE:
28231 storage_class = sc_mutable;
28232 break;
28233 default:
28234 gcc_unreachable ();
28235 }
28236 decl_specs->storage_class = storage_class;
28237 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28238
28239 /* A storage class specifier cannot be applied alongside a typedef
28240 specifier. If there is a typedef specifier present then set
28241 conflicting_specifiers_p which will trigger an error later
28242 on in grokdeclarator. */
28243 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28244 decl_specs->conflicting_specifiers_p = true;
28245 }
28246
28247 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28248 is true, the type is a class or enum definition. */
28249
28250 static void
28251 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28252 tree type_spec,
28253 cp_token *token,
28254 bool type_definition_p)
28255 {
28256 decl_specs->any_specifiers_p = true;
28257
28258 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28259 (with, for example, in "typedef int wchar_t;") we remember that
28260 this is what happened. In system headers, we ignore these
28261 declarations so that G++ can work with system headers that are not
28262 C++-safe. */
28263 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28264 && !type_definition_p
28265 && (type_spec == boolean_type_node
28266 || type_spec == char16_type_node
28267 || type_spec == char32_type_node
28268 || type_spec == wchar_type_node)
28269 && (decl_specs->type
28270 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28271 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28272 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28273 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28274 {
28275 decl_specs->redefined_builtin_type = type_spec;
28276 set_and_check_decl_spec_loc (decl_specs,
28277 ds_redefined_builtin_type_spec,
28278 token);
28279 if (!decl_specs->type)
28280 {
28281 decl_specs->type = type_spec;
28282 decl_specs->type_definition_p = false;
28283 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28284 }
28285 }
28286 else if (decl_specs->type)
28287 decl_specs->multiple_types_p = true;
28288 else
28289 {
28290 decl_specs->type = type_spec;
28291 decl_specs->type_definition_p = type_definition_p;
28292 decl_specs->redefined_builtin_type = NULL_TREE;
28293 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28294 }
28295 }
28296
28297 /* True iff TOKEN is the GNU keyword __thread. */
28298
28299 static bool
28300 token_is__thread (cp_token *token)
28301 {
28302 gcc_assert (token->keyword == RID_THREAD);
28303 return id_equal (token->u.value, "__thread");
28304 }
28305
28306 /* Set the location for a declarator specifier and check if it is
28307 duplicated.
28308
28309 DECL_SPECS is the sequence of declarator specifiers onto which to
28310 set the location.
28311
28312 DS is the single declarator specifier to set which location is to
28313 be set onto the existing sequence of declarators.
28314
28315 LOCATION is the location for the declarator specifier to
28316 consider. */
28317
28318 static void
28319 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28320 cp_decl_spec ds, cp_token *token)
28321 {
28322 gcc_assert (ds < ds_last);
28323
28324 if (decl_specs == NULL)
28325 return;
28326
28327 source_location location = token->location;
28328
28329 if (decl_specs->locations[ds] == 0)
28330 {
28331 decl_specs->locations[ds] = location;
28332 if (ds == ds_thread)
28333 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28334 }
28335 else
28336 {
28337 if (ds == ds_long)
28338 {
28339 if (decl_specs->locations[ds_long_long] != 0)
28340 error_at (location,
28341 "%<long long long%> is too long for GCC");
28342 else
28343 {
28344 decl_specs->locations[ds_long_long] = location;
28345 pedwarn_cxx98 (location,
28346 OPT_Wlong_long,
28347 "ISO C++ 1998 does not support %<long long%>");
28348 }
28349 }
28350 else if (ds == ds_thread)
28351 {
28352 bool gnu = token_is__thread (token);
28353 if (gnu != decl_specs->gnu_thread_keyword_p)
28354 error_at (location,
28355 "both %<__thread%> and %<thread_local%> specified");
28356 else
28357 {
28358 gcc_rich_location richloc (location);
28359 richloc.add_fixit_remove ();
28360 error_at (&richloc, "duplicate %qD", token->u.value);
28361 }
28362 }
28363 else
28364 {
28365 static const char *const decl_spec_names[] = {
28366 "signed",
28367 "unsigned",
28368 "short",
28369 "long",
28370 "const",
28371 "volatile",
28372 "restrict",
28373 "inline",
28374 "virtual",
28375 "explicit",
28376 "friend",
28377 "typedef",
28378 "using",
28379 "constexpr",
28380 "__complex"
28381 };
28382 gcc_rich_location richloc (location);
28383 richloc.add_fixit_remove ();
28384 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28385 }
28386 }
28387 }
28388
28389 /* Return true iff the declarator specifier DS is present in the
28390 sequence of declarator specifiers DECL_SPECS. */
28391
28392 bool
28393 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28394 cp_decl_spec ds)
28395 {
28396 gcc_assert (ds < ds_last);
28397
28398 if (decl_specs == NULL)
28399 return false;
28400
28401 return decl_specs->locations[ds] != 0;
28402 }
28403
28404 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28405 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28406
28407 static bool
28408 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28409 {
28410 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28411 }
28412
28413 /* Issue an error message indicating that TOKEN_DESC was expected.
28414 If KEYWORD is true, it indicated this function is called by
28415 cp_parser_require_keword and the required token can only be
28416 a indicated keyword.
28417
28418 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28419 within any error as the location of an "opening" token matching
28420 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28421 RT_CLOSE_PAREN). */
28422
28423 static void
28424 cp_parser_required_error (cp_parser *parser,
28425 required_token token_desc,
28426 bool keyword,
28427 location_t matching_location)
28428 {
28429 if (cp_parser_simulate_error (parser))
28430 return;
28431
28432 const char *gmsgid = NULL;
28433 switch (token_desc)
28434 {
28435 case RT_NEW:
28436 gmsgid = G_("expected %<new%>");
28437 break;
28438 case RT_DELETE:
28439 gmsgid = G_("expected %<delete%>");
28440 break;
28441 case RT_RETURN:
28442 gmsgid = G_("expected %<return%>");
28443 break;
28444 case RT_WHILE:
28445 gmsgid = G_("expected %<while%>");
28446 break;
28447 case RT_EXTERN:
28448 gmsgid = G_("expected %<extern%>");
28449 break;
28450 case RT_STATIC_ASSERT:
28451 gmsgid = G_("expected %<static_assert%>");
28452 break;
28453 case RT_DECLTYPE:
28454 gmsgid = G_("expected %<decltype%>");
28455 break;
28456 case RT_OPERATOR:
28457 gmsgid = G_("expected %<operator%>");
28458 break;
28459 case RT_CLASS:
28460 gmsgid = G_("expected %<class%>");
28461 break;
28462 case RT_TEMPLATE:
28463 gmsgid = G_("expected %<template%>");
28464 break;
28465 case RT_NAMESPACE:
28466 gmsgid = G_("expected %<namespace%>");
28467 break;
28468 case RT_USING:
28469 gmsgid = G_("expected %<using%>");
28470 break;
28471 case RT_ASM:
28472 gmsgid = G_("expected %<asm%>");
28473 break;
28474 case RT_TRY:
28475 gmsgid = G_("expected %<try%>");
28476 break;
28477 case RT_CATCH:
28478 gmsgid = G_("expected %<catch%>");
28479 break;
28480 case RT_THROW:
28481 gmsgid = G_("expected %<throw%>");
28482 break;
28483 case RT_LABEL:
28484 gmsgid = G_("expected %<__label__%>");
28485 break;
28486 case RT_AT_TRY:
28487 gmsgid = G_("expected %<@try%>");
28488 break;
28489 case RT_AT_SYNCHRONIZED:
28490 gmsgid = G_("expected %<@synchronized%>");
28491 break;
28492 case RT_AT_THROW:
28493 gmsgid = G_("expected %<@throw%>");
28494 break;
28495 case RT_TRANSACTION_ATOMIC:
28496 gmsgid = G_("expected %<__transaction_atomic%>");
28497 break;
28498 case RT_TRANSACTION_RELAXED:
28499 gmsgid = G_("expected %<__transaction_relaxed%>");
28500 break;
28501 default:
28502 break;
28503 }
28504
28505 if (!gmsgid && !keyword)
28506 {
28507 switch (token_desc)
28508 {
28509 case RT_SEMICOLON:
28510 gmsgid = G_("expected %<;%>");
28511 break;
28512 case RT_OPEN_PAREN:
28513 gmsgid = G_("expected %<(%>");
28514 break;
28515 case RT_CLOSE_BRACE:
28516 gmsgid = G_("expected %<}%>");
28517 break;
28518 case RT_OPEN_BRACE:
28519 gmsgid = G_("expected %<{%>");
28520 break;
28521 case RT_CLOSE_SQUARE:
28522 gmsgid = G_("expected %<]%>");
28523 break;
28524 case RT_OPEN_SQUARE:
28525 gmsgid = G_("expected %<[%>");
28526 break;
28527 case RT_COMMA:
28528 gmsgid = G_("expected %<,%>");
28529 break;
28530 case RT_SCOPE:
28531 gmsgid = G_("expected %<::%>");
28532 break;
28533 case RT_LESS:
28534 gmsgid = G_("expected %<<%>");
28535 break;
28536 case RT_GREATER:
28537 gmsgid = G_("expected %<>%>");
28538 break;
28539 case RT_EQ:
28540 gmsgid = G_("expected %<=%>");
28541 break;
28542 case RT_ELLIPSIS:
28543 gmsgid = G_("expected %<...%>");
28544 break;
28545 case RT_MULT:
28546 gmsgid = G_("expected %<*%>");
28547 break;
28548 case RT_COMPL:
28549 gmsgid = G_("expected %<~%>");
28550 break;
28551 case RT_COLON:
28552 gmsgid = G_("expected %<:%>");
28553 break;
28554 case RT_COLON_SCOPE:
28555 gmsgid = G_("expected %<:%> or %<::%>");
28556 break;
28557 case RT_CLOSE_PAREN:
28558 gmsgid = G_("expected %<)%>");
28559 break;
28560 case RT_COMMA_CLOSE_PAREN:
28561 gmsgid = G_("expected %<,%> or %<)%>");
28562 break;
28563 case RT_PRAGMA_EOL:
28564 gmsgid = G_("expected end of line");
28565 break;
28566 case RT_NAME:
28567 gmsgid = G_("expected identifier");
28568 break;
28569 case RT_SELECT:
28570 gmsgid = G_("expected selection-statement");
28571 break;
28572 case RT_ITERATION:
28573 gmsgid = G_("expected iteration-statement");
28574 break;
28575 case RT_JUMP:
28576 gmsgid = G_("expected jump-statement");
28577 break;
28578 case RT_CLASS_KEY:
28579 gmsgid = G_("expected class-key");
28580 break;
28581 case RT_CLASS_TYPENAME_TEMPLATE:
28582 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28583 break;
28584 default:
28585 gcc_unreachable ();
28586 }
28587 }
28588
28589 if (gmsgid)
28590 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28591 }
28592
28593
28594 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28595 issue an error message indicating that TOKEN_DESC was expected.
28596
28597 Returns the token consumed, if the token had the appropriate type.
28598 Otherwise, returns NULL.
28599
28600 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28601 within any error as the location of an "opening" token matching
28602 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28603 RT_CLOSE_PAREN). */
28604
28605 static cp_token *
28606 cp_parser_require (cp_parser* parser,
28607 enum cpp_ttype type,
28608 required_token token_desc,
28609 location_t matching_location)
28610 {
28611 if (cp_lexer_next_token_is (parser->lexer, type))
28612 return cp_lexer_consume_token (parser->lexer);
28613 else
28614 {
28615 /* Output the MESSAGE -- unless we're parsing tentatively. */
28616 if (!cp_parser_simulate_error (parser))
28617 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28618 matching_location);
28619 return NULL;
28620 }
28621 }
28622
28623 /* An error message is produced if the next token is not '>'.
28624 All further tokens are skipped until the desired token is
28625 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28626
28627 static void
28628 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28629 {
28630 /* Current level of '< ... >'. */
28631 unsigned level = 0;
28632 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28633 unsigned nesting_depth = 0;
28634
28635 /* Are we ready, yet? If not, issue error message. */
28636 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28637 return;
28638
28639 /* Skip tokens until the desired token is found. */
28640 while (true)
28641 {
28642 /* Peek at the next token. */
28643 switch (cp_lexer_peek_token (parser->lexer)->type)
28644 {
28645 case CPP_LESS:
28646 if (!nesting_depth)
28647 ++level;
28648 break;
28649
28650 case CPP_RSHIFT:
28651 if (cxx_dialect == cxx98)
28652 /* C++0x views the `>>' operator as two `>' tokens, but
28653 C++98 does not. */
28654 break;
28655 else if (!nesting_depth && level-- == 0)
28656 {
28657 /* We've hit a `>>' where the first `>' closes the
28658 template argument list, and the second `>' is
28659 spurious. Just consume the `>>' and stop; we've
28660 already produced at least one error. */
28661 cp_lexer_consume_token (parser->lexer);
28662 return;
28663 }
28664 /* Fall through for C++0x, so we handle the second `>' in
28665 the `>>'. */
28666 gcc_fallthrough ();
28667
28668 case CPP_GREATER:
28669 if (!nesting_depth && level-- == 0)
28670 {
28671 /* We've reached the token we want, consume it and stop. */
28672 cp_lexer_consume_token (parser->lexer);
28673 return;
28674 }
28675 break;
28676
28677 case CPP_OPEN_PAREN:
28678 case CPP_OPEN_SQUARE:
28679 ++nesting_depth;
28680 break;
28681
28682 case CPP_CLOSE_PAREN:
28683 case CPP_CLOSE_SQUARE:
28684 if (nesting_depth-- == 0)
28685 return;
28686 break;
28687
28688 case CPP_EOF:
28689 case CPP_PRAGMA_EOL:
28690 case CPP_SEMICOLON:
28691 case CPP_OPEN_BRACE:
28692 case CPP_CLOSE_BRACE:
28693 /* The '>' was probably forgotten, don't look further. */
28694 return;
28695
28696 default:
28697 break;
28698 }
28699
28700 /* Consume this token. */
28701 cp_lexer_consume_token (parser->lexer);
28702 }
28703 }
28704
28705 /* If the next token is the indicated keyword, consume it. Otherwise,
28706 issue an error message indicating that TOKEN_DESC was expected.
28707
28708 Returns the token consumed, if the token had the appropriate type.
28709 Otherwise, returns NULL. */
28710
28711 static cp_token *
28712 cp_parser_require_keyword (cp_parser* parser,
28713 enum rid keyword,
28714 required_token token_desc)
28715 {
28716 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28717
28718 if (token && token->keyword != keyword)
28719 {
28720 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28721 UNKNOWN_LOCATION);
28722 return NULL;
28723 }
28724
28725 return token;
28726 }
28727
28728 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28729 function-definition. */
28730
28731 static bool
28732 cp_parser_token_starts_function_definition_p (cp_token* token)
28733 {
28734 return (/* An ordinary function-body begins with an `{'. */
28735 token->type == CPP_OPEN_BRACE
28736 /* A ctor-initializer begins with a `:'. */
28737 || token->type == CPP_COLON
28738 /* A function-try-block begins with `try'. */
28739 || token->keyword == RID_TRY
28740 /* A function-transaction-block begins with `__transaction_atomic'
28741 or `__transaction_relaxed'. */
28742 || token->keyword == RID_TRANSACTION_ATOMIC
28743 || token->keyword == RID_TRANSACTION_RELAXED
28744 /* The named return value extension begins with `return'. */
28745 || token->keyword == RID_RETURN);
28746 }
28747
28748 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28749 definition. */
28750
28751 static bool
28752 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28753 {
28754 cp_token *token;
28755
28756 token = cp_lexer_peek_token (parser->lexer);
28757 return (token->type == CPP_OPEN_BRACE
28758 || (token->type == CPP_COLON
28759 && !parser->colon_doesnt_start_class_def_p));
28760 }
28761
28762 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28763 C++0x) ending a template-argument. */
28764
28765 static bool
28766 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28767 {
28768 cp_token *token;
28769
28770 token = cp_lexer_peek_token (parser->lexer);
28771 return (token->type == CPP_COMMA
28772 || token->type == CPP_GREATER
28773 || token->type == CPP_ELLIPSIS
28774 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28775 }
28776
28777 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28778 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28779
28780 static bool
28781 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28782 size_t n)
28783 {
28784 cp_token *token;
28785
28786 token = cp_lexer_peek_nth_token (parser->lexer, n);
28787 if (token->type == CPP_LESS)
28788 return true;
28789 /* Check for the sequence `<::' in the original code. It would be lexed as
28790 `[:', where `[' is a digraph, and there is no whitespace before
28791 `:'. */
28792 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28793 {
28794 cp_token *token2;
28795 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28796 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28797 return true;
28798 }
28799 return false;
28800 }
28801
28802 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28803 or none_type otherwise. */
28804
28805 static enum tag_types
28806 cp_parser_token_is_class_key (cp_token* token)
28807 {
28808 switch (token->keyword)
28809 {
28810 case RID_CLASS:
28811 return class_type;
28812 case RID_STRUCT:
28813 return record_type;
28814 case RID_UNION:
28815 return union_type;
28816
28817 default:
28818 return none_type;
28819 }
28820 }
28821
28822 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28823 or none_type otherwise or if the token is null. */
28824
28825 static enum tag_types
28826 cp_parser_token_is_type_parameter_key (cp_token* token)
28827 {
28828 if (!token)
28829 return none_type;
28830
28831 switch (token->keyword)
28832 {
28833 case RID_CLASS:
28834 return class_type;
28835 case RID_TYPENAME:
28836 return typename_type;
28837
28838 default:
28839 return none_type;
28840 }
28841 }
28842
28843 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28844
28845 static void
28846 cp_parser_check_class_key (enum tag_types class_key, tree type)
28847 {
28848 if (type == error_mark_node)
28849 return;
28850 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28851 {
28852 if (permerror (input_location, "%qs tag used in naming %q#T",
28853 class_key == union_type ? "union"
28854 : class_key == record_type ? "struct" : "class",
28855 type))
28856 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28857 "%q#T was previously declared here", type);
28858 }
28859 }
28860
28861 /* Issue an error message if DECL is redeclared with different
28862 access than its original declaration [class.access.spec/3].
28863 This applies to nested classes, nested class templates and
28864 enumerations [class.mem/1]. */
28865
28866 static void
28867 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28868 {
28869 if (!decl
28870 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28871 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28872 return;
28873
28874 if ((TREE_PRIVATE (decl)
28875 != (current_access_specifier == access_private_node))
28876 || (TREE_PROTECTED (decl)
28877 != (current_access_specifier == access_protected_node)))
28878 error_at (location, "%qD redeclared with different access", decl);
28879 }
28880
28881 /* Look for the `template' keyword, as a syntactic disambiguator.
28882 Return TRUE iff it is present, in which case it will be
28883 consumed. */
28884
28885 static bool
28886 cp_parser_optional_template_keyword (cp_parser *parser)
28887 {
28888 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28889 {
28890 /* In C++98 the `template' keyword can only be used within templates;
28891 outside templates the parser can always figure out what is a
28892 template and what is not. In C++11, per the resolution of DR 468,
28893 `template' is allowed in cases where it is not strictly necessary. */
28894 if (!processing_template_decl
28895 && pedantic && cxx_dialect == cxx98)
28896 {
28897 cp_token *token = cp_lexer_peek_token (parser->lexer);
28898 pedwarn (token->location, OPT_Wpedantic,
28899 "in C++98 %<template%> (as a disambiguator) is only "
28900 "allowed within templates");
28901 /* If this part of the token stream is rescanned, the same
28902 error message would be generated. So, we purge the token
28903 from the stream. */
28904 cp_lexer_purge_token (parser->lexer);
28905 return false;
28906 }
28907 else
28908 {
28909 /* Consume the `template' keyword. */
28910 cp_lexer_consume_token (parser->lexer);
28911 return true;
28912 }
28913 }
28914 return false;
28915 }
28916
28917 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28918 set PARSER->SCOPE, and perform other related actions. */
28919
28920 static void
28921 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28922 {
28923 struct tree_check *check_value;
28924
28925 /* Get the stored value. */
28926 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28927 /* Set the scope from the stored value. */
28928 parser->scope = saved_checks_value (check_value);
28929 parser->qualifying_scope = check_value->qualifying_scope;
28930 parser->object_scope = NULL_TREE;
28931 }
28932
28933 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28934 encounter the end of a block before what we were looking for. */
28935
28936 static bool
28937 cp_parser_cache_group (cp_parser *parser,
28938 enum cpp_ttype end,
28939 unsigned depth)
28940 {
28941 while (true)
28942 {
28943 cp_token *token = cp_lexer_peek_token (parser->lexer);
28944
28945 /* Abort a parenthesized expression if we encounter a semicolon. */
28946 if ((end == CPP_CLOSE_PAREN || depth == 0)
28947 && token->type == CPP_SEMICOLON)
28948 return true;
28949 /* If we've reached the end of the file, stop. */
28950 if (token->type == CPP_EOF
28951 || (end != CPP_PRAGMA_EOL
28952 && token->type == CPP_PRAGMA_EOL))
28953 return true;
28954 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28955 /* We've hit the end of an enclosing block, so there's been some
28956 kind of syntax error. */
28957 return true;
28958
28959 /* Consume the token. */
28960 cp_lexer_consume_token (parser->lexer);
28961 /* See if it starts a new group. */
28962 if (token->type == CPP_OPEN_BRACE)
28963 {
28964 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28965 /* In theory this should probably check end == '}', but
28966 cp_parser_save_member_function_body needs it to exit
28967 after either '}' or ')' when called with ')'. */
28968 if (depth == 0)
28969 return false;
28970 }
28971 else if (token->type == CPP_OPEN_PAREN)
28972 {
28973 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28974 if (depth == 0 && end == CPP_CLOSE_PAREN)
28975 return false;
28976 }
28977 else if (token->type == CPP_PRAGMA)
28978 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28979 else if (token->type == end)
28980 return false;
28981 }
28982 }
28983
28984 /* Like above, for caching a default argument or NSDMI. Both of these are
28985 terminated by a non-nested comma, but it can be unclear whether or not a
28986 comma is nested in a template argument list unless we do more parsing.
28987 In order to handle this ambiguity, when we encounter a ',' after a '<'
28988 we try to parse what follows as a parameter-declaration-list (in the
28989 case of a default argument) or a member-declarator (in the case of an
28990 NSDMI). If that succeeds, then we stop caching. */
28991
28992 static tree
28993 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28994 {
28995 unsigned depth = 0;
28996 int maybe_template_id = 0;
28997 cp_token *first_token;
28998 cp_token *token;
28999 tree default_argument;
29000
29001 /* Add tokens until we have processed the entire default
29002 argument. We add the range [first_token, token). */
29003 first_token = cp_lexer_peek_token (parser->lexer);
29004 if (first_token->type == CPP_OPEN_BRACE)
29005 {
29006 /* For list-initialization, this is straightforward. */
29007 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29008 token = cp_lexer_peek_token (parser->lexer);
29009 }
29010 else while (true)
29011 {
29012 bool done = false;
29013
29014 /* Peek at the next token. */
29015 token = cp_lexer_peek_token (parser->lexer);
29016 /* What we do depends on what token we have. */
29017 switch (token->type)
29018 {
29019 /* In valid code, a default argument must be
29020 immediately followed by a `,' `)', or `...'. */
29021 case CPP_COMMA:
29022 if (depth == 0 && maybe_template_id)
29023 {
29024 /* If we've seen a '<', we might be in a
29025 template-argument-list. Until Core issue 325 is
29026 resolved, we don't know how this situation ought
29027 to be handled, so try to DTRT. We check whether
29028 what comes after the comma is a valid parameter
29029 declaration list. If it is, then the comma ends
29030 the default argument; otherwise the default
29031 argument continues. */
29032 bool error = false;
29033 cp_token *peek;
29034
29035 /* Set ITALP so cp_parser_parameter_declaration_list
29036 doesn't decide to commit to this parse. */
29037 bool saved_italp = parser->in_template_argument_list_p;
29038 parser->in_template_argument_list_p = true;
29039
29040 cp_parser_parse_tentatively (parser);
29041
29042 if (nsdmi)
29043 {
29044 /* Parse declarators until we reach a non-comma or
29045 somthing that cannot be an initializer.
29046 Just checking whether we're looking at a single
29047 declarator is insufficient. Consider:
29048 int var = tuple<T,U>::x;
29049 The template parameter 'U' looks exactly like a
29050 declarator. */
29051 do
29052 {
29053 int ctor_dtor_or_conv_p;
29054 cp_lexer_consume_token (parser->lexer);
29055 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29056 &ctor_dtor_or_conv_p,
29057 /*parenthesized_p=*/NULL,
29058 /*member_p=*/true,
29059 /*friend_p=*/false);
29060 peek = cp_lexer_peek_token (parser->lexer);
29061 if (cp_parser_error_occurred (parser))
29062 break;
29063 }
29064 while (peek->type == CPP_COMMA);
29065 /* If we met an '=' or ';' then the original comma
29066 was the end of the NSDMI. Otherwise assume
29067 we're still in the NSDMI. */
29068 error = (peek->type != CPP_EQ
29069 && peek->type != CPP_SEMICOLON);
29070 }
29071 else
29072 {
29073 cp_lexer_consume_token (parser->lexer);
29074 begin_scope (sk_function_parms, NULL_TREE);
29075 if (cp_parser_parameter_declaration_list (parser)
29076 == error_mark_node)
29077 error = true;
29078 pop_bindings_and_leave_scope ();
29079 }
29080 if (!cp_parser_error_occurred (parser) && !error)
29081 done = true;
29082 cp_parser_abort_tentative_parse (parser);
29083
29084 parser->in_template_argument_list_p = saved_italp;
29085 break;
29086 }
29087 /* FALLTHRU */
29088 case CPP_CLOSE_PAREN:
29089 case CPP_ELLIPSIS:
29090 /* If we run into a non-nested `;', `}', or `]',
29091 then the code is invalid -- but the default
29092 argument is certainly over. */
29093 case CPP_SEMICOLON:
29094 case CPP_CLOSE_BRACE:
29095 case CPP_CLOSE_SQUARE:
29096 if (depth == 0
29097 /* Handle correctly int n = sizeof ... ( p ); */
29098 && token->type != CPP_ELLIPSIS)
29099 done = true;
29100 /* Update DEPTH, if necessary. */
29101 else if (token->type == CPP_CLOSE_PAREN
29102 || token->type == CPP_CLOSE_BRACE
29103 || token->type == CPP_CLOSE_SQUARE)
29104 --depth;
29105 break;
29106
29107 case CPP_OPEN_PAREN:
29108 case CPP_OPEN_SQUARE:
29109 case CPP_OPEN_BRACE:
29110 ++depth;
29111 break;
29112
29113 case CPP_LESS:
29114 if (depth == 0)
29115 /* This might be the comparison operator, or it might
29116 start a template argument list. */
29117 ++maybe_template_id;
29118 break;
29119
29120 case CPP_RSHIFT:
29121 if (cxx_dialect == cxx98)
29122 break;
29123 /* Fall through for C++0x, which treats the `>>'
29124 operator like two `>' tokens in certain
29125 cases. */
29126 gcc_fallthrough ();
29127
29128 case CPP_GREATER:
29129 if (depth == 0)
29130 {
29131 /* This might be an operator, or it might close a
29132 template argument list. But if a previous '<'
29133 started a template argument list, this will have
29134 closed it, so we can't be in one anymore. */
29135 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29136 if (maybe_template_id < 0)
29137 maybe_template_id = 0;
29138 }
29139 break;
29140
29141 /* If we run out of tokens, issue an error message. */
29142 case CPP_EOF:
29143 case CPP_PRAGMA_EOL:
29144 error_at (token->location, "file ends in default argument");
29145 return error_mark_node;
29146
29147 case CPP_NAME:
29148 case CPP_SCOPE:
29149 /* In these cases, we should look for template-ids.
29150 For example, if the default argument is
29151 `X<int, double>()', we need to do name lookup to
29152 figure out whether or not `X' is a template; if
29153 so, the `,' does not end the default argument.
29154
29155 That is not yet done. */
29156 break;
29157
29158 default:
29159 break;
29160 }
29161
29162 /* If we've reached the end, stop. */
29163 if (done)
29164 break;
29165
29166 /* Add the token to the token block. */
29167 token = cp_lexer_consume_token (parser->lexer);
29168 }
29169
29170 /* Create a DEFAULT_ARG to represent the unparsed default
29171 argument. */
29172 default_argument = make_node (DEFAULT_ARG);
29173 DEFARG_TOKENS (default_argument)
29174 = cp_token_cache_new (first_token, token);
29175 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29176
29177 return default_argument;
29178 }
29179
29180 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29181
29182 location_t
29183 defarg_location (tree default_argument)
29184 {
29185 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29186 location_t start = tokens->first->location;
29187 location_t end = tokens->last->location;
29188 return make_location (start, start, end);
29189 }
29190
29191 /* Begin parsing tentatively. We always save tokens while parsing
29192 tentatively so that if the tentative parsing fails we can restore the
29193 tokens. */
29194
29195 static void
29196 cp_parser_parse_tentatively (cp_parser* parser)
29197 {
29198 /* Enter a new parsing context. */
29199 parser->context = cp_parser_context_new (parser->context);
29200 /* Begin saving tokens. */
29201 cp_lexer_save_tokens (parser->lexer);
29202 /* In order to avoid repetitive access control error messages,
29203 access checks are queued up until we are no longer parsing
29204 tentatively. */
29205 push_deferring_access_checks (dk_deferred);
29206 }
29207
29208 /* Commit to the currently active tentative parse. */
29209
29210 static void
29211 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29212 {
29213 cp_parser_context *context;
29214 cp_lexer *lexer;
29215
29216 /* Mark all of the levels as committed. */
29217 lexer = parser->lexer;
29218 for (context = parser->context; context->next; context = context->next)
29219 {
29220 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29221 break;
29222 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29223 while (!cp_lexer_saving_tokens (lexer))
29224 lexer = lexer->next;
29225 cp_lexer_commit_tokens (lexer);
29226 }
29227 }
29228
29229 /* Commit to the topmost currently active tentative parse.
29230
29231 Note that this function shouldn't be called when there are
29232 irreversible side-effects while in a tentative state. For
29233 example, we shouldn't create a permanent entry in the symbol
29234 table, or issue an error message that might not apply if the
29235 tentative parse is aborted. */
29236
29237 static void
29238 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29239 {
29240 cp_parser_context *context = parser->context;
29241 cp_lexer *lexer = parser->lexer;
29242
29243 if (context)
29244 {
29245 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29246 return;
29247 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29248
29249 while (!cp_lexer_saving_tokens (lexer))
29250 lexer = lexer->next;
29251 cp_lexer_commit_tokens (lexer);
29252 }
29253 }
29254
29255 /* Abort the currently active tentative parse. All consumed tokens
29256 will be rolled back, and no diagnostics will be issued. */
29257
29258 static void
29259 cp_parser_abort_tentative_parse (cp_parser* parser)
29260 {
29261 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29262 || errorcount > 0);
29263 cp_parser_simulate_error (parser);
29264 /* Now, pretend that we want to see if the construct was
29265 successfully parsed. */
29266 cp_parser_parse_definitely (parser);
29267 }
29268
29269 /* Stop parsing tentatively. If a parse error has occurred, restore the
29270 token stream. Otherwise, commit to the tokens we have consumed.
29271 Returns true if no error occurred; false otherwise. */
29272
29273 static bool
29274 cp_parser_parse_definitely (cp_parser* parser)
29275 {
29276 bool error_occurred;
29277 cp_parser_context *context;
29278
29279 /* Remember whether or not an error occurred, since we are about to
29280 destroy that information. */
29281 error_occurred = cp_parser_error_occurred (parser);
29282 /* Remove the topmost context from the stack. */
29283 context = parser->context;
29284 parser->context = context->next;
29285 /* If no parse errors occurred, commit to the tentative parse. */
29286 if (!error_occurred)
29287 {
29288 /* Commit to the tokens read tentatively, unless that was
29289 already done. */
29290 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29291 cp_lexer_commit_tokens (parser->lexer);
29292
29293 pop_to_parent_deferring_access_checks ();
29294 }
29295 /* Otherwise, if errors occurred, roll back our state so that things
29296 are just as they were before we began the tentative parse. */
29297 else
29298 {
29299 cp_lexer_rollback_tokens (parser->lexer);
29300 pop_deferring_access_checks ();
29301 }
29302 /* Add the context to the front of the free list. */
29303 context->next = cp_parser_context_free_list;
29304 cp_parser_context_free_list = context;
29305
29306 return !error_occurred;
29307 }
29308
29309 /* Returns true if we are parsing tentatively and are not committed to
29310 this tentative parse. */
29311
29312 static bool
29313 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29314 {
29315 return (cp_parser_parsing_tentatively (parser)
29316 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29317 }
29318
29319 /* Returns nonzero iff an error has occurred during the most recent
29320 tentative parse. */
29321
29322 static bool
29323 cp_parser_error_occurred (cp_parser* parser)
29324 {
29325 return (cp_parser_parsing_tentatively (parser)
29326 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29327 }
29328
29329 /* Returns nonzero if GNU extensions are allowed. */
29330
29331 static bool
29332 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29333 {
29334 return parser->allow_gnu_extensions_p;
29335 }
29336 \f
29337 /* Objective-C++ Productions */
29338
29339
29340 /* Parse an Objective-C expression, which feeds into a primary-expression
29341 above.
29342
29343 objc-expression:
29344 objc-message-expression
29345 objc-string-literal
29346 objc-encode-expression
29347 objc-protocol-expression
29348 objc-selector-expression
29349
29350 Returns a tree representation of the expression. */
29351
29352 static cp_expr
29353 cp_parser_objc_expression (cp_parser* parser)
29354 {
29355 /* Try to figure out what kind of declaration is present. */
29356 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29357
29358 switch (kwd->type)
29359 {
29360 case CPP_OPEN_SQUARE:
29361 return cp_parser_objc_message_expression (parser);
29362
29363 case CPP_OBJC_STRING:
29364 kwd = cp_lexer_consume_token (parser->lexer);
29365 return objc_build_string_object (kwd->u.value);
29366
29367 case CPP_KEYWORD:
29368 switch (kwd->keyword)
29369 {
29370 case RID_AT_ENCODE:
29371 return cp_parser_objc_encode_expression (parser);
29372
29373 case RID_AT_PROTOCOL:
29374 return cp_parser_objc_protocol_expression (parser);
29375
29376 case RID_AT_SELECTOR:
29377 return cp_parser_objc_selector_expression (parser);
29378
29379 default:
29380 break;
29381 }
29382 /* FALLTHRU */
29383 default:
29384 error_at (kwd->location,
29385 "misplaced %<@%D%> Objective-C++ construct",
29386 kwd->u.value);
29387 cp_parser_skip_to_end_of_block_or_statement (parser);
29388 }
29389
29390 return error_mark_node;
29391 }
29392
29393 /* Parse an Objective-C message expression.
29394
29395 objc-message-expression:
29396 [ objc-message-receiver objc-message-args ]
29397
29398 Returns a representation of an Objective-C message. */
29399
29400 static tree
29401 cp_parser_objc_message_expression (cp_parser* parser)
29402 {
29403 tree receiver, messageargs;
29404
29405 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29406 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29407 receiver = cp_parser_objc_message_receiver (parser);
29408 messageargs = cp_parser_objc_message_args (parser);
29409 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29410 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29411
29412 tree result = objc_build_message_expr (receiver, messageargs);
29413
29414 /* Construct a location e.g.
29415 [self func1:5]
29416 ^~~~~~~~~~~~~~
29417 ranging from the '[' to the ']', with the caret at the start. */
29418 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29419 protected_set_expr_location (result, combined_loc);
29420
29421 return result;
29422 }
29423
29424 /* Parse an objc-message-receiver.
29425
29426 objc-message-receiver:
29427 expression
29428 simple-type-specifier
29429
29430 Returns a representation of the type or expression. */
29431
29432 static tree
29433 cp_parser_objc_message_receiver (cp_parser* parser)
29434 {
29435 tree rcv;
29436
29437 /* An Objective-C message receiver may be either (1) a type
29438 or (2) an expression. */
29439 cp_parser_parse_tentatively (parser);
29440 rcv = cp_parser_expression (parser);
29441
29442 /* If that worked out, fine. */
29443 if (cp_parser_parse_definitely (parser))
29444 return rcv;
29445
29446 cp_parser_parse_tentatively (parser);
29447 rcv = cp_parser_simple_type_specifier (parser,
29448 /*decl_specs=*/NULL,
29449 CP_PARSER_FLAGS_NONE);
29450
29451 if (cp_parser_parse_definitely (parser))
29452 return objc_get_class_reference (rcv);
29453
29454 cp_parser_error (parser, "objective-c++ message receiver expected");
29455 return error_mark_node;
29456 }
29457
29458 /* Parse the arguments and selectors comprising an Objective-C message.
29459
29460 objc-message-args:
29461 objc-selector
29462 objc-selector-args
29463 objc-selector-args , objc-comma-args
29464
29465 objc-selector-args:
29466 objc-selector [opt] : assignment-expression
29467 objc-selector-args objc-selector [opt] : assignment-expression
29468
29469 objc-comma-args:
29470 assignment-expression
29471 objc-comma-args , assignment-expression
29472
29473 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29474 selector arguments and TREE_VALUE containing a list of comma
29475 arguments. */
29476
29477 static tree
29478 cp_parser_objc_message_args (cp_parser* parser)
29479 {
29480 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29481 bool maybe_unary_selector_p = true;
29482 cp_token *token = cp_lexer_peek_token (parser->lexer);
29483
29484 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29485 {
29486 tree selector = NULL_TREE, arg;
29487
29488 if (token->type != CPP_COLON)
29489 selector = cp_parser_objc_selector (parser);
29490
29491 /* Detect if we have a unary selector. */
29492 if (maybe_unary_selector_p
29493 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29494 return build_tree_list (selector, NULL_TREE);
29495
29496 maybe_unary_selector_p = false;
29497 cp_parser_require (parser, CPP_COLON, RT_COLON);
29498 arg = cp_parser_assignment_expression (parser);
29499
29500 sel_args
29501 = chainon (sel_args,
29502 build_tree_list (selector, arg));
29503
29504 token = cp_lexer_peek_token (parser->lexer);
29505 }
29506
29507 /* Handle non-selector arguments, if any. */
29508 while (token->type == CPP_COMMA)
29509 {
29510 tree arg;
29511
29512 cp_lexer_consume_token (parser->lexer);
29513 arg = cp_parser_assignment_expression (parser);
29514
29515 addl_args
29516 = chainon (addl_args,
29517 build_tree_list (NULL_TREE, arg));
29518
29519 token = cp_lexer_peek_token (parser->lexer);
29520 }
29521
29522 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29523 {
29524 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29525 return build_tree_list (error_mark_node, error_mark_node);
29526 }
29527
29528 return build_tree_list (sel_args, addl_args);
29529 }
29530
29531 /* Parse an Objective-C encode expression.
29532
29533 objc-encode-expression:
29534 @encode objc-typename
29535
29536 Returns an encoded representation of the type argument. */
29537
29538 static cp_expr
29539 cp_parser_objc_encode_expression (cp_parser* parser)
29540 {
29541 tree type;
29542 cp_token *token;
29543 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29544
29545 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29546 matching_parens parens;
29547 parens.require_open (parser);
29548 token = cp_lexer_peek_token (parser->lexer);
29549 type = complete_type (cp_parser_type_id (parser));
29550 parens.require_close (parser);
29551
29552 if (!type)
29553 {
29554 error_at (token->location,
29555 "%<@encode%> must specify a type as an argument");
29556 return error_mark_node;
29557 }
29558
29559 /* This happens if we find @encode(T) (where T is a template
29560 typename or something dependent on a template typename) when
29561 parsing a template. In that case, we can't compile it
29562 immediately, but we rather create an AT_ENCODE_EXPR which will
29563 need to be instantiated when the template is used.
29564 */
29565 if (dependent_type_p (type))
29566 {
29567 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29568 TREE_READONLY (value) = 1;
29569 return value;
29570 }
29571
29572
29573 /* Build a location of the form:
29574 @encode(int)
29575 ^~~~~~~~~~~~
29576 with caret==start at the @ token, finishing at the close paren. */
29577 location_t combined_loc
29578 = make_location (start_loc, start_loc,
29579 cp_lexer_previous_token (parser->lexer)->location);
29580
29581 return cp_expr (objc_build_encode_expr (type), combined_loc);
29582 }
29583
29584 /* Parse an Objective-C @defs expression. */
29585
29586 static tree
29587 cp_parser_objc_defs_expression (cp_parser *parser)
29588 {
29589 tree name;
29590
29591 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29592 matching_parens parens;
29593 parens.require_open (parser);
29594 name = cp_parser_identifier (parser);
29595 parens.require_close (parser);
29596
29597 return objc_get_class_ivars (name);
29598 }
29599
29600 /* Parse an Objective-C protocol expression.
29601
29602 objc-protocol-expression:
29603 @protocol ( identifier )
29604
29605 Returns a representation of the protocol expression. */
29606
29607 static tree
29608 cp_parser_objc_protocol_expression (cp_parser* parser)
29609 {
29610 tree proto;
29611 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29612
29613 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29614 matching_parens parens;
29615 parens.require_open (parser);
29616 proto = cp_parser_identifier (parser);
29617 parens.require_close (parser);
29618
29619 /* Build a location of the form:
29620 @protocol(prot)
29621 ^~~~~~~~~~~~~~~
29622 with caret==start at the @ token, finishing at the close paren. */
29623 location_t combined_loc
29624 = make_location (start_loc, start_loc,
29625 cp_lexer_previous_token (parser->lexer)->location);
29626 tree result = objc_build_protocol_expr (proto);
29627 protected_set_expr_location (result, combined_loc);
29628 return result;
29629 }
29630
29631 /* Parse an Objective-C selector expression.
29632
29633 objc-selector-expression:
29634 @selector ( objc-method-signature )
29635
29636 objc-method-signature:
29637 objc-selector
29638 objc-selector-seq
29639
29640 objc-selector-seq:
29641 objc-selector :
29642 objc-selector-seq objc-selector :
29643
29644 Returns a representation of the method selector. */
29645
29646 static tree
29647 cp_parser_objc_selector_expression (cp_parser* parser)
29648 {
29649 tree sel_seq = NULL_TREE;
29650 bool maybe_unary_selector_p = true;
29651 cp_token *token;
29652 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29653
29654 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29655 matching_parens parens;
29656 parens.require_open (parser);
29657 token = cp_lexer_peek_token (parser->lexer);
29658
29659 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29660 || token->type == CPP_SCOPE)
29661 {
29662 tree selector = NULL_TREE;
29663
29664 if (token->type != CPP_COLON
29665 || token->type == CPP_SCOPE)
29666 selector = cp_parser_objc_selector (parser);
29667
29668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29669 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29670 {
29671 /* Detect if we have a unary selector. */
29672 if (maybe_unary_selector_p)
29673 {
29674 sel_seq = selector;
29675 goto finish_selector;
29676 }
29677 else
29678 {
29679 cp_parser_error (parser, "expected %<:%>");
29680 }
29681 }
29682 maybe_unary_selector_p = false;
29683 token = cp_lexer_consume_token (parser->lexer);
29684
29685 if (token->type == CPP_SCOPE)
29686 {
29687 sel_seq
29688 = chainon (sel_seq,
29689 build_tree_list (selector, NULL_TREE));
29690 sel_seq
29691 = chainon (sel_seq,
29692 build_tree_list (NULL_TREE, NULL_TREE));
29693 }
29694 else
29695 sel_seq
29696 = chainon (sel_seq,
29697 build_tree_list (selector, NULL_TREE));
29698
29699 token = cp_lexer_peek_token (parser->lexer);
29700 }
29701
29702 finish_selector:
29703 parens.require_close (parser);
29704
29705
29706 /* Build a location of the form:
29707 @selector(func)
29708 ^~~~~~~~~~~~~~~
29709 with caret==start at the @ token, finishing at the close paren. */
29710 location_t combined_loc
29711 = make_location (loc, loc,
29712 cp_lexer_previous_token (parser->lexer)->location);
29713 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29714 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29715 protected_set_expr_location (result, combined_loc);
29716 return result;
29717 }
29718
29719 /* Parse a list of identifiers.
29720
29721 objc-identifier-list:
29722 identifier
29723 objc-identifier-list , identifier
29724
29725 Returns a TREE_LIST of identifier nodes. */
29726
29727 static tree
29728 cp_parser_objc_identifier_list (cp_parser* parser)
29729 {
29730 tree identifier;
29731 tree list;
29732 cp_token *sep;
29733
29734 identifier = cp_parser_identifier (parser);
29735 if (identifier == error_mark_node)
29736 return error_mark_node;
29737
29738 list = build_tree_list (NULL_TREE, identifier);
29739 sep = cp_lexer_peek_token (parser->lexer);
29740
29741 while (sep->type == CPP_COMMA)
29742 {
29743 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29744 identifier = cp_parser_identifier (parser);
29745 if (identifier == error_mark_node)
29746 return list;
29747
29748 list = chainon (list, build_tree_list (NULL_TREE,
29749 identifier));
29750 sep = cp_lexer_peek_token (parser->lexer);
29751 }
29752
29753 return list;
29754 }
29755
29756 /* Parse an Objective-C alias declaration.
29757
29758 objc-alias-declaration:
29759 @compatibility_alias identifier identifier ;
29760
29761 This function registers the alias mapping with the Objective-C front end.
29762 It returns nothing. */
29763
29764 static void
29765 cp_parser_objc_alias_declaration (cp_parser* parser)
29766 {
29767 tree alias, orig;
29768
29769 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29770 alias = cp_parser_identifier (parser);
29771 orig = cp_parser_identifier (parser);
29772 objc_declare_alias (alias, orig);
29773 cp_parser_consume_semicolon_at_end_of_statement (parser);
29774 }
29775
29776 /* Parse an Objective-C class forward-declaration.
29777
29778 objc-class-declaration:
29779 @class objc-identifier-list ;
29780
29781 The function registers the forward declarations with the Objective-C
29782 front end. It returns nothing. */
29783
29784 static void
29785 cp_parser_objc_class_declaration (cp_parser* parser)
29786 {
29787 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29788 while (true)
29789 {
29790 tree id;
29791
29792 id = cp_parser_identifier (parser);
29793 if (id == error_mark_node)
29794 break;
29795
29796 objc_declare_class (id);
29797
29798 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29799 cp_lexer_consume_token (parser->lexer);
29800 else
29801 break;
29802 }
29803 cp_parser_consume_semicolon_at_end_of_statement (parser);
29804 }
29805
29806 /* Parse a list of Objective-C protocol references.
29807
29808 objc-protocol-refs-opt:
29809 objc-protocol-refs [opt]
29810
29811 objc-protocol-refs:
29812 < objc-identifier-list >
29813
29814 Returns a TREE_LIST of identifiers, if any. */
29815
29816 static tree
29817 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29818 {
29819 tree protorefs = NULL_TREE;
29820
29821 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29822 {
29823 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29824 protorefs = cp_parser_objc_identifier_list (parser);
29825 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29826 }
29827
29828 return protorefs;
29829 }
29830
29831 /* Parse a Objective-C visibility specification. */
29832
29833 static void
29834 cp_parser_objc_visibility_spec (cp_parser* parser)
29835 {
29836 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29837
29838 switch (vis->keyword)
29839 {
29840 case RID_AT_PRIVATE:
29841 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29842 break;
29843 case RID_AT_PROTECTED:
29844 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29845 break;
29846 case RID_AT_PUBLIC:
29847 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29848 break;
29849 case RID_AT_PACKAGE:
29850 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29851 break;
29852 default:
29853 return;
29854 }
29855
29856 /* Eat '@private'/'@protected'/'@public'. */
29857 cp_lexer_consume_token (parser->lexer);
29858 }
29859
29860 /* Parse an Objective-C method type. Return 'true' if it is a class
29861 (+) method, and 'false' if it is an instance (-) method. */
29862
29863 static inline bool
29864 cp_parser_objc_method_type (cp_parser* parser)
29865 {
29866 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29867 return true;
29868 else
29869 return false;
29870 }
29871
29872 /* Parse an Objective-C protocol qualifier. */
29873
29874 static tree
29875 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29876 {
29877 tree quals = NULL_TREE, node;
29878 cp_token *token = cp_lexer_peek_token (parser->lexer);
29879
29880 node = token->u.value;
29881
29882 while (node && identifier_p (node)
29883 && (node == ridpointers [(int) RID_IN]
29884 || node == ridpointers [(int) RID_OUT]
29885 || node == ridpointers [(int) RID_INOUT]
29886 || node == ridpointers [(int) RID_BYCOPY]
29887 || node == ridpointers [(int) RID_BYREF]
29888 || node == ridpointers [(int) RID_ONEWAY]))
29889 {
29890 quals = tree_cons (NULL_TREE, node, quals);
29891 cp_lexer_consume_token (parser->lexer);
29892 token = cp_lexer_peek_token (parser->lexer);
29893 node = token->u.value;
29894 }
29895
29896 return quals;
29897 }
29898
29899 /* Parse an Objective-C typename. */
29900
29901 static tree
29902 cp_parser_objc_typename (cp_parser* parser)
29903 {
29904 tree type_name = NULL_TREE;
29905
29906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29907 {
29908 tree proto_quals, cp_type = NULL_TREE;
29909
29910 matching_parens parens;
29911 parens.consume_open (parser); /* Eat '('. */
29912 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29913
29914 /* An ObjC type name may consist of just protocol qualifiers, in which
29915 case the type shall default to 'id'. */
29916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29917 {
29918 cp_type = cp_parser_type_id (parser);
29919
29920 /* If the type could not be parsed, an error has already
29921 been produced. For error recovery, behave as if it had
29922 not been specified, which will use the default type
29923 'id'. */
29924 if (cp_type == error_mark_node)
29925 {
29926 cp_type = NULL_TREE;
29927 /* We need to skip to the closing parenthesis as
29928 cp_parser_type_id() does not seem to do it for
29929 us. */
29930 cp_parser_skip_to_closing_parenthesis (parser,
29931 /*recovering=*/true,
29932 /*or_comma=*/false,
29933 /*consume_paren=*/false);
29934 }
29935 }
29936
29937 parens.require_close (parser);
29938 type_name = build_tree_list (proto_quals, cp_type);
29939 }
29940
29941 return type_name;
29942 }
29943
29944 /* Check to see if TYPE refers to an Objective-C selector name. */
29945
29946 static bool
29947 cp_parser_objc_selector_p (enum cpp_ttype type)
29948 {
29949 return (type == CPP_NAME || type == CPP_KEYWORD
29950 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29951 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29952 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29953 || type == CPP_XOR || type == CPP_XOR_EQ);
29954 }
29955
29956 /* Parse an Objective-C selector. */
29957
29958 static tree
29959 cp_parser_objc_selector (cp_parser* parser)
29960 {
29961 cp_token *token = cp_lexer_consume_token (parser->lexer);
29962
29963 if (!cp_parser_objc_selector_p (token->type))
29964 {
29965 error_at (token->location, "invalid Objective-C++ selector name");
29966 return error_mark_node;
29967 }
29968
29969 /* C++ operator names are allowed to appear in ObjC selectors. */
29970 switch (token->type)
29971 {
29972 case CPP_AND_AND: return get_identifier ("and");
29973 case CPP_AND_EQ: return get_identifier ("and_eq");
29974 case CPP_AND: return get_identifier ("bitand");
29975 case CPP_OR: return get_identifier ("bitor");
29976 case CPP_COMPL: return get_identifier ("compl");
29977 case CPP_NOT: return get_identifier ("not");
29978 case CPP_NOT_EQ: return get_identifier ("not_eq");
29979 case CPP_OR_OR: return get_identifier ("or");
29980 case CPP_OR_EQ: return get_identifier ("or_eq");
29981 case CPP_XOR: return get_identifier ("xor");
29982 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29983 default: return token->u.value;
29984 }
29985 }
29986
29987 /* Parse an Objective-C params list. */
29988
29989 static tree
29990 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29991 {
29992 tree params = NULL_TREE;
29993 bool maybe_unary_selector_p = true;
29994 cp_token *token = cp_lexer_peek_token (parser->lexer);
29995
29996 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29997 {
29998 tree selector = NULL_TREE, type_name, identifier;
29999 tree parm_attr = NULL_TREE;
30000
30001 if (token->keyword == RID_ATTRIBUTE)
30002 break;
30003
30004 if (token->type != CPP_COLON)
30005 selector = cp_parser_objc_selector (parser);
30006
30007 /* Detect if we have a unary selector. */
30008 if (maybe_unary_selector_p
30009 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30010 {
30011 params = selector; /* Might be followed by attributes. */
30012 break;
30013 }
30014
30015 maybe_unary_selector_p = false;
30016 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30017 {
30018 /* Something went quite wrong. There should be a colon
30019 here, but there is not. Stop parsing parameters. */
30020 break;
30021 }
30022 type_name = cp_parser_objc_typename (parser);
30023 /* New ObjC allows attributes on parameters too. */
30024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30025 parm_attr = cp_parser_attributes_opt (parser);
30026 identifier = cp_parser_identifier (parser);
30027
30028 params
30029 = chainon (params,
30030 objc_build_keyword_decl (selector,
30031 type_name,
30032 identifier,
30033 parm_attr));
30034
30035 token = cp_lexer_peek_token (parser->lexer);
30036 }
30037
30038 if (params == NULL_TREE)
30039 {
30040 cp_parser_error (parser, "objective-c++ method declaration is expected");
30041 return error_mark_node;
30042 }
30043
30044 /* We allow tail attributes for the method. */
30045 if (token->keyword == RID_ATTRIBUTE)
30046 {
30047 *attributes = cp_parser_attributes_opt (parser);
30048 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30049 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30050 return params;
30051 cp_parser_error (parser,
30052 "method attributes must be specified at the end");
30053 return error_mark_node;
30054 }
30055
30056 if (params == NULL_TREE)
30057 {
30058 cp_parser_error (parser, "objective-c++ method declaration is expected");
30059 return error_mark_node;
30060 }
30061 return params;
30062 }
30063
30064 /* Parse the non-keyword Objective-C params. */
30065
30066 static tree
30067 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30068 tree* attributes)
30069 {
30070 tree params = make_node (TREE_LIST);
30071 cp_token *token = cp_lexer_peek_token (parser->lexer);
30072 *ellipsisp = false; /* Initially, assume no ellipsis. */
30073
30074 while (token->type == CPP_COMMA)
30075 {
30076 cp_parameter_declarator *parmdecl;
30077 tree parm;
30078
30079 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30080 token = cp_lexer_peek_token (parser->lexer);
30081
30082 if (token->type == CPP_ELLIPSIS)
30083 {
30084 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30085 *ellipsisp = true;
30086 token = cp_lexer_peek_token (parser->lexer);
30087 break;
30088 }
30089
30090 /* TODO: parse attributes for tail parameters. */
30091 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30092 parm = grokdeclarator (parmdecl->declarator,
30093 &parmdecl->decl_specifiers,
30094 PARM, /*initialized=*/0,
30095 /*attrlist=*/NULL);
30096
30097 chainon (params, build_tree_list (NULL_TREE, parm));
30098 token = cp_lexer_peek_token (parser->lexer);
30099 }
30100
30101 /* We allow tail attributes for the method. */
30102 if (token->keyword == RID_ATTRIBUTE)
30103 {
30104 if (*attributes == NULL_TREE)
30105 {
30106 *attributes = cp_parser_attributes_opt (parser);
30107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30108 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30109 return params;
30110 }
30111 else
30112 /* We have an error, but parse the attributes, so that we can
30113 carry on. */
30114 *attributes = cp_parser_attributes_opt (parser);
30115
30116 cp_parser_error (parser,
30117 "method attributes must be specified at the end");
30118 return error_mark_node;
30119 }
30120
30121 return params;
30122 }
30123
30124 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30125
30126 static void
30127 cp_parser_objc_interstitial_code (cp_parser* parser)
30128 {
30129 cp_token *token = cp_lexer_peek_token (parser->lexer);
30130
30131 /* If the next token is `extern' and the following token is a string
30132 literal, then we have a linkage specification. */
30133 if (token->keyword == RID_EXTERN
30134 && cp_parser_is_pure_string_literal
30135 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30136 cp_parser_linkage_specification (parser);
30137 /* Handle #pragma, if any. */
30138 else if (token->type == CPP_PRAGMA)
30139 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30140 /* Allow stray semicolons. */
30141 else if (token->type == CPP_SEMICOLON)
30142 cp_lexer_consume_token (parser->lexer);
30143 /* Mark methods as optional or required, when building protocols. */
30144 else if (token->keyword == RID_AT_OPTIONAL)
30145 {
30146 cp_lexer_consume_token (parser->lexer);
30147 objc_set_method_opt (true);
30148 }
30149 else if (token->keyword == RID_AT_REQUIRED)
30150 {
30151 cp_lexer_consume_token (parser->lexer);
30152 objc_set_method_opt (false);
30153 }
30154 else if (token->keyword == RID_NAMESPACE)
30155 cp_parser_namespace_definition (parser);
30156 /* Other stray characters must generate errors. */
30157 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30158 {
30159 cp_lexer_consume_token (parser->lexer);
30160 error ("stray %qs between Objective-C++ methods",
30161 token->type == CPP_OPEN_BRACE ? "{" : "}");
30162 }
30163 /* Finally, try to parse a block-declaration, or a function-definition. */
30164 else
30165 cp_parser_block_declaration (parser, /*statement_p=*/false);
30166 }
30167
30168 /* Parse a method signature. */
30169
30170 static tree
30171 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30172 {
30173 tree rettype, kwdparms, optparms;
30174 bool ellipsis = false;
30175 bool is_class_method;
30176
30177 is_class_method = cp_parser_objc_method_type (parser);
30178 rettype = cp_parser_objc_typename (parser);
30179 *attributes = NULL_TREE;
30180 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30181 if (kwdparms == error_mark_node)
30182 return error_mark_node;
30183 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30184 if (optparms == error_mark_node)
30185 return error_mark_node;
30186
30187 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30188 }
30189
30190 static bool
30191 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30192 {
30193 tree tattr;
30194 cp_lexer_save_tokens (parser->lexer);
30195 tattr = cp_parser_attributes_opt (parser);
30196 gcc_assert (tattr) ;
30197
30198 /* If the attributes are followed by a method introducer, this is not allowed.
30199 Dump the attributes and flag the situation. */
30200 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30201 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30202 return true;
30203
30204 /* Otherwise, the attributes introduce some interstitial code, possibly so
30205 rewind to allow that check. */
30206 cp_lexer_rollback_tokens (parser->lexer);
30207 return false;
30208 }
30209
30210 /* Parse an Objective-C method prototype list. */
30211
30212 static void
30213 cp_parser_objc_method_prototype_list (cp_parser* parser)
30214 {
30215 cp_token *token = cp_lexer_peek_token (parser->lexer);
30216
30217 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30218 {
30219 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30220 {
30221 tree attributes, sig;
30222 bool is_class_method;
30223 if (token->type == CPP_PLUS)
30224 is_class_method = true;
30225 else
30226 is_class_method = false;
30227 sig = cp_parser_objc_method_signature (parser, &attributes);
30228 if (sig == error_mark_node)
30229 {
30230 cp_parser_skip_to_end_of_block_or_statement (parser);
30231 token = cp_lexer_peek_token (parser->lexer);
30232 continue;
30233 }
30234 objc_add_method_declaration (is_class_method, sig, attributes);
30235 cp_parser_consume_semicolon_at_end_of_statement (parser);
30236 }
30237 else if (token->keyword == RID_AT_PROPERTY)
30238 cp_parser_objc_at_property_declaration (parser);
30239 else if (token->keyword == RID_ATTRIBUTE
30240 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30241 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30242 OPT_Wattributes,
30243 "prefix attributes are ignored for methods");
30244 else
30245 /* Allow for interspersed non-ObjC++ code. */
30246 cp_parser_objc_interstitial_code (parser);
30247
30248 token = cp_lexer_peek_token (parser->lexer);
30249 }
30250
30251 if (token->type != CPP_EOF)
30252 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30253 else
30254 cp_parser_error (parser, "expected %<@end%>");
30255
30256 objc_finish_interface ();
30257 }
30258
30259 /* Parse an Objective-C method definition list. */
30260
30261 static void
30262 cp_parser_objc_method_definition_list (cp_parser* parser)
30263 {
30264 cp_token *token = cp_lexer_peek_token (parser->lexer);
30265
30266 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30267 {
30268 tree meth;
30269
30270 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30271 {
30272 cp_token *ptk;
30273 tree sig, attribute;
30274 bool is_class_method;
30275 if (token->type == CPP_PLUS)
30276 is_class_method = true;
30277 else
30278 is_class_method = false;
30279 push_deferring_access_checks (dk_deferred);
30280 sig = cp_parser_objc_method_signature (parser, &attribute);
30281 if (sig == error_mark_node)
30282 {
30283 cp_parser_skip_to_end_of_block_or_statement (parser);
30284 token = cp_lexer_peek_token (parser->lexer);
30285 continue;
30286 }
30287 objc_start_method_definition (is_class_method, sig, attribute,
30288 NULL_TREE);
30289
30290 /* For historical reasons, we accept an optional semicolon. */
30291 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30292 cp_lexer_consume_token (parser->lexer);
30293
30294 ptk = cp_lexer_peek_token (parser->lexer);
30295 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30296 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30297 {
30298 perform_deferred_access_checks (tf_warning_or_error);
30299 stop_deferring_access_checks ();
30300 meth = cp_parser_function_definition_after_declarator (parser,
30301 false);
30302 pop_deferring_access_checks ();
30303 objc_finish_method_definition (meth);
30304 }
30305 }
30306 /* The following case will be removed once @synthesize is
30307 completely implemented. */
30308 else if (token->keyword == RID_AT_PROPERTY)
30309 cp_parser_objc_at_property_declaration (parser);
30310 else if (token->keyword == RID_AT_SYNTHESIZE)
30311 cp_parser_objc_at_synthesize_declaration (parser);
30312 else if (token->keyword == RID_AT_DYNAMIC)
30313 cp_parser_objc_at_dynamic_declaration (parser);
30314 else if (token->keyword == RID_ATTRIBUTE
30315 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30316 warning_at (token->location, OPT_Wattributes,
30317 "prefix attributes are ignored for methods");
30318 else
30319 /* Allow for interspersed non-ObjC++ code. */
30320 cp_parser_objc_interstitial_code (parser);
30321
30322 token = cp_lexer_peek_token (parser->lexer);
30323 }
30324
30325 if (token->type != CPP_EOF)
30326 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30327 else
30328 cp_parser_error (parser, "expected %<@end%>");
30329
30330 objc_finish_implementation ();
30331 }
30332
30333 /* Parse Objective-C ivars. */
30334
30335 static void
30336 cp_parser_objc_class_ivars (cp_parser* parser)
30337 {
30338 cp_token *token = cp_lexer_peek_token (parser->lexer);
30339
30340 if (token->type != CPP_OPEN_BRACE)
30341 return; /* No ivars specified. */
30342
30343 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30344 token = cp_lexer_peek_token (parser->lexer);
30345
30346 while (token->type != CPP_CLOSE_BRACE
30347 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30348 {
30349 cp_decl_specifier_seq declspecs;
30350 int decl_class_or_enum_p;
30351 tree prefix_attributes;
30352
30353 cp_parser_objc_visibility_spec (parser);
30354
30355 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30356 break;
30357
30358 cp_parser_decl_specifier_seq (parser,
30359 CP_PARSER_FLAGS_OPTIONAL,
30360 &declspecs,
30361 &decl_class_or_enum_p);
30362
30363 /* auto, register, static, extern, mutable. */
30364 if (declspecs.storage_class != sc_none)
30365 {
30366 cp_parser_error (parser, "invalid type for instance variable");
30367 declspecs.storage_class = sc_none;
30368 }
30369
30370 /* thread_local. */
30371 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30372 {
30373 cp_parser_error (parser, "invalid type for instance variable");
30374 declspecs.locations[ds_thread] = 0;
30375 }
30376
30377 /* typedef. */
30378 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30379 {
30380 cp_parser_error (parser, "invalid type for instance variable");
30381 declspecs.locations[ds_typedef] = 0;
30382 }
30383
30384 prefix_attributes = declspecs.attributes;
30385 declspecs.attributes = NULL_TREE;
30386
30387 /* Keep going until we hit the `;' at the end of the
30388 declaration. */
30389 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30390 {
30391 tree width = NULL_TREE, attributes, first_attribute, decl;
30392 cp_declarator *declarator = NULL;
30393 int ctor_dtor_or_conv_p;
30394
30395 /* Check for a (possibly unnamed) bitfield declaration. */
30396 token = cp_lexer_peek_token (parser->lexer);
30397 if (token->type == CPP_COLON)
30398 goto eat_colon;
30399
30400 if (token->type == CPP_NAME
30401 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30402 == CPP_COLON))
30403 {
30404 /* Get the name of the bitfield. */
30405 declarator = make_id_declarator (NULL_TREE,
30406 cp_parser_identifier (parser),
30407 sfk_none);
30408
30409 eat_colon:
30410 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30411 /* Get the width of the bitfield. */
30412 width
30413 = cp_parser_constant_expression (parser);
30414 }
30415 else
30416 {
30417 /* Parse the declarator. */
30418 declarator
30419 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30420 &ctor_dtor_or_conv_p,
30421 /*parenthesized_p=*/NULL,
30422 /*member_p=*/false,
30423 /*friend_p=*/false);
30424 }
30425
30426 /* Look for attributes that apply to the ivar. */
30427 attributes = cp_parser_attributes_opt (parser);
30428 /* Remember which attributes are prefix attributes and
30429 which are not. */
30430 first_attribute = attributes;
30431 /* Combine the attributes. */
30432 attributes = attr_chainon (prefix_attributes, attributes);
30433
30434 if (width)
30435 /* Create the bitfield declaration. */
30436 decl = grokbitfield (declarator, &declspecs,
30437 width, NULL_TREE, attributes);
30438 else
30439 decl = grokfield (declarator, &declspecs,
30440 NULL_TREE, /*init_const_expr_p=*/false,
30441 NULL_TREE, attributes);
30442
30443 /* Add the instance variable. */
30444 if (decl != error_mark_node && decl != NULL_TREE)
30445 objc_add_instance_variable (decl);
30446
30447 /* Reset PREFIX_ATTRIBUTES. */
30448 if (attributes != error_mark_node)
30449 {
30450 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30451 attributes = TREE_CHAIN (attributes);
30452 if (attributes)
30453 TREE_CHAIN (attributes) = NULL_TREE;
30454 }
30455
30456 token = cp_lexer_peek_token (parser->lexer);
30457
30458 if (token->type == CPP_COMMA)
30459 {
30460 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30461 continue;
30462 }
30463 break;
30464 }
30465
30466 cp_parser_consume_semicolon_at_end_of_statement (parser);
30467 token = cp_lexer_peek_token (parser->lexer);
30468 }
30469
30470 if (token->keyword == RID_AT_END)
30471 cp_parser_error (parser, "expected %<}%>");
30472
30473 /* Do not consume the RID_AT_END, so it will be read again as terminating
30474 the @interface of @implementation. */
30475 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30476 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30477
30478 /* For historical reasons, we accept an optional semicolon. */
30479 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30480 cp_lexer_consume_token (parser->lexer);
30481 }
30482
30483 /* Parse an Objective-C protocol declaration. */
30484
30485 static void
30486 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30487 {
30488 tree proto, protorefs;
30489 cp_token *tok;
30490
30491 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30493 {
30494 tok = cp_lexer_peek_token (parser->lexer);
30495 error_at (tok->location, "identifier expected after %<@protocol%>");
30496 cp_parser_consume_semicolon_at_end_of_statement (parser);
30497 return;
30498 }
30499
30500 /* See if we have a forward declaration or a definition. */
30501 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30502
30503 /* Try a forward declaration first. */
30504 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30505 {
30506 while (true)
30507 {
30508 tree id;
30509
30510 id = cp_parser_identifier (parser);
30511 if (id == error_mark_node)
30512 break;
30513
30514 objc_declare_protocol (id, attributes);
30515
30516 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30517 cp_lexer_consume_token (parser->lexer);
30518 else
30519 break;
30520 }
30521 cp_parser_consume_semicolon_at_end_of_statement (parser);
30522 }
30523
30524 /* Ok, we got a full-fledged definition (or at least should). */
30525 else
30526 {
30527 proto = cp_parser_identifier (parser);
30528 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30529 objc_start_protocol (proto, protorefs, attributes);
30530 cp_parser_objc_method_prototype_list (parser);
30531 }
30532 }
30533
30534 /* Parse an Objective-C superclass or category. */
30535
30536 static void
30537 cp_parser_objc_superclass_or_category (cp_parser *parser,
30538 bool iface_p,
30539 tree *super,
30540 tree *categ, bool *is_class_extension)
30541 {
30542 cp_token *next = cp_lexer_peek_token (parser->lexer);
30543
30544 *super = *categ = NULL_TREE;
30545 *is_class_extension = false;
30546 if (next->type == CPP_COLON)
30547 {
30548 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30549 *super = cp_parser_identifier (parser);
30550 }
30551 else if (next->type == CPP_OPEN_PAREN)
30552 {
30553 matching_parens parens;
30554 parens.consume_open (parser); /* Eat '('. */
30555
30556 /* If there is no category name, and this is an @interface, we
30557 have a class extension. */
30558 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30559 {
30560 *categ = NULL_TREE;
30561 *is_class_extension = true;
30562 }
30563 else
30564 *categ = cp_parser_identifier (parser);
30565
30566 parens.require_close (parser);
30567 }
30568 }
30569
30570 /* Parse an Objective-C class interface. */
30571
30572 static void
30573 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30574 {
30575 tree name, super, categ, protos;
30576 bool is_class_extension;
30577
30578 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30579 name = cp_parser_identifier (parser);
30580 if (name == error_mark_node)
30581 {
30582 /* It's hard to recover because even if valid @interface stuff
30583 is to follow, we can't compile it (or validate it) if we
30584 don't even know which class it refers to. Let's assume this
30585 was a stray '@interface' token in the stream and skip it.
30586 */
30587 return;
30588 }
30589 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30590 &is_class_extension);
30591 protos = cp_parser_objc_protocol_refs_opt (parser);
30592
30593 /* We have either a class or a category on our hands. */
30594 if (categ || is_class_extension)
30595 objc_start_category_interface (name, categ, protos, attributes);
30596 else
30597 {
30598 objc_start_class_interface (name, super, protos, attributes);
30599 /* Handle instance variable declarations, if any. */
30600 cp_parser_objc_class_ivars (parser);
30601 objc_continue_interface ();
30602 }
30603
30604 cp_parser_objc_method_prototype_list (parser);
30605 }
30606
30607 /* Parse an Objective-C class implementation. */
30608
30609 static void
30610 cp_parser_objc_class_implementation (cp_parser* parser)
30611 {
30612 tree name, super, categ;
30613 bool is_class_extension;
30614
30615 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30616 name = cp_parser_identifier (parser);
30617 if (name == error_mark_node)
30618 {
30619 /* It's hard to recover because even if valid @implementation
30620 stuff is to follow, we can't compile it (or validate it) if
30621 we don't even know which class it refers to. Let's assume
30622 this was a stray '@implementation' token in the stream and
30623 skip it.
30624 */
30625 return;
30626 }
30627 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30628 &is_class_extension);
30629
30630 /* We have either a class or a category on our hands. */
30631 if (categ)
30632 objc_start_category_implementation (name, categ);
30633 else
30634 {
30635 objc_start_class_implementation (name, super);
30636 /* Handle instance variable declarations, if any. */
30637 cp_parser_objc_class_ivars (parser);
30638 objc_continue_implementation ();
30639 }
30640
30641 cp_parser_objc_method_definition_list (parser);
30642 }
30643
30644 /* Consume the @end token and finish off the implementation. */
30645
30646 static void
30647 cp_parser_objc_end_implementation (cp_parser* parser)
30648 {
30649 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30650 objc_finish_implementation ();
30651 }
30652
30653 /* Parse an Objective-C declaration. */
30654
30655 static void
30656 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30657 {
30658 /* Try to figure out what kind of declaration is present. */
30659 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30660
30661 if (attributes)
30662 switch (kwd->keyword)
30663 {
30664 case RID_AT_ALIAS:
30665 case RID_AT_CLASS:
30666 case RID_AT_END:
30667 error_at (kwd->location, "attributes may not be specified before"
30668 " the %<@%D%> Objective-C++ keyword",
30669 kwd->u.value);
30670 attributes = NULL;
30671 break;
30672 case RID_AT_IMPLEMENTATION:
30673 warning_at (kwd->location, OPT_Wattributes,
30674 "prefix attributes are ignored before %<@%D%>",
30675 kwd->u.value);
30676 attributes = NULL;
30677 default:
30678 break;
30679 }
30680
30681 switch (kwd->keyword)
30682 {
30683 case RID_AT_ALIAS:
30684 cp_parser_objc_alias_declaration (parser);
30685 break;
30686 case RID_AT_CLASS:
30687 cp_parser_objc_class_declaration (parser);
30688 break;
30689 case RID_AT_PROTOCOL:
30690 cp_parser_objc_protocol_declaration (parser, attributes);
30691 break;
30692 case RID_AT_INTERFACE:
30693 cp_parser_objc_class_interface (parser, attributes);
30694 break;
30695 case RID_AT_IMPLEMENTATION:
30696 cp_parser_objc_class_implementation (parser);
30697 break;
30698 case RID_AT_END:
30699 cp_parser_objc_end_implementation (parser);
30700 break;
30701 default:
30702 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30703 kwd->u.value);
30704 cp_parser_skip_to_end_of_block_or_statement (parser);
30705 }
30706 }
30707
30708 /* Parse an Objective-C try-catch-finally statement.
30709
30710 objc-try-catch-finally-stmt:
30711 @try compound-statement objc-catch-clause-seq [opt]
30712 objc-finally-clause [opt]
30713
30714 objc-catch-clause-seq:
30715 objc-catch-clause objc-catch-clause-seq [opt]
30716
30717 objc-catch-clause:
30718 @catch ( objc-exception-declaration ) compound-statement
30719
30720 objc-finally-clause:
30721 @finally compound-statement
30722
30723 objc-exception-declaration:
30724 parameter-declaration
30725 '...'
30726
30727 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30728
30729 Returns NULL_TREE.
30730
30731 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30732 for C. Keep them in sync. */
30733
30734 static tree
30735 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30736 {
30737 location_t location;
30738 tree stmt;
30739
30740 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30741 location = cp_lexer_peek_token (parser->lexer)->location;
30742 objc_maybe_warn_exceptions (location);
30743 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30744 node, lest it get absorbed into the surrounding block. */
30745 stmt = push_stmt_list ();
30746 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30747 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30748
30749 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30750 {
30751 cp_parameter_declarator *parm;
30752 tree parameter_declaration = error_mark_node;
30753 bool seen_open_paren = false;
30754 matching_parens parens;
30755
30756 cp_lexer_consume_token (parser->lexer);
30757 if (parens.require_open (parser))
30758 seen_open_paren = true;
30759 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30760 {
30761 /* We have "@catch (...)" (where the '...' are literally
30762 what is in the code). Skip the '...'.
30763 parameter_declaration is set to NULL_TREE, and
30764 objc_being_catch_clauses() knows that that means
30765 '...'. */
30766 cp_lexer_consume_token (parser->lexer);
30767 parameter_declaration = NULL_TREE;
30768 }
30769 else
30770 {
30771 /* We have "@catch (NSException *exception)" or something
30772 like that. Parse the parameter declaration. */
30773 parm = cp_parser_parameter_declaration (parser, false, NULL);
30774 if (parm == NULL)
30775 parameter_declaration = error_mark_node;
30776 else
30777 parameter_declaration = grokdeclarator (parm->declarator,
30778 &parm->decl_specifiers,
30779 PARM, /*initialized=*/0,
30780 /*attrlist=*/NULL);
30781 }
30782 if (seen_open_paren)
30783 parens.require_close (parser);
30784 else
30785 {
30786 /* If there was no open parenthesis, we are recovering from
30787 an error, and we are trying to figure out what mistake
30788 the user has made. */
30789
30790 /* If there is an immediate closing parenthesis, the user
30791 probably forgot the opening one (ie, they typed "@catch
30792 NSException *e)". Parse the closing parenthesis and keep
30793 going. */
30794 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30795 cp_lexer_consume_token (parser->lexer);
30796
30797 /* If these is no immediate closing parenthesis, the user
30798 probably doesn't know that parenthesis are required at
30799 all (ie, they typed "@catch NSException *e"). So, just
30800 forget about the closing parenthesis and keep going. */
30801 }
30802 objc_begin_catch_clause (parameter_declaration);
30803 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30804 objc_finish_catch_clause ();
30805 }
30806 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30807 {
30808 cp_lexer_consume_token (parser->lexer);
30809 location = cp_lexer_peek_token (parser->lexer)->location;
30810 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30811 node, lest it get absorbed into the surrounding block. */
30812 stmt = push_stmt_list ();
30813 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30814 objc_build_finally_clause (location, pop_stmt_list (stmt));
30815 }
30816
30817 return objc_finish_try_stmt ();
30818 }
30819
30820 /* Parse an Objective-C synchronized statement.
30821
30822 objc-synchronized-stmt:
30823 @synchronized ( expression ) compound-statement
30824
30825 Returns NULL_TREE. */
30826
30827 static tree
30828 cp_parser_objc_synchronized_statement (cp_parser *parser)
30829 {
30830 location_t location;
30831 tree lock, stmt;
30832
30833 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30834
30835 location = cp_lexer_peek_token (parser->lexer)->location;
30836 objc_maybe_warn_exceptions (location);
30837 matching_parens parens;
30838 parens.require_open (parser);
30839 lock = cp_parser_expression (parser);
30840 parens.require_close (parser);
30841
30842 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30843 node, lest it get absorbed into the surrounding block. */
30844 stmt = push_stmt_list ();
30845 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30846
30847 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30848 }
30849
30850 /* Parse an Objective-C throw statement.
30851
30852 objc-throw-stmt:
30853 @throw assignment-expression [opt] ;
30854
30855 Returns a constructed '@throw' statement. */
30856
30857 static tree
30858 cp_parser_objc_throw_statement (cp_parser *parser)
30859 {
30860 tree expr = NULL_TREE;
30861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30862
30863 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30864
30865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30866 expr = cp_parser_expression (parser);
30867
30868 cp_parser_consume_semicolon_at_end_of_statement (parser);
30869
30870 return objc_build_throw_stmt (loc, expr);
30871 }
30872
30873 /* Parse an Objective-C statement. */
30874
30875 static tree
30876 cp_parser_objc_statement (cp_parser * parser)
30877 {
30878 /* Try to figure out what kind of declaration is present. */
30879 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30880
30881 switch (kwd->keyword)
30882 {
30883 case RID_AT_TRY:
30884 return cp_parser_objc_try_catch_finally_statement (parser);
30885 case RID_AT_SYNCHRONIZED:
30886 return cp_parser_objc_synchronized_statement (parser);
30887 case RID_AT_THROW:
30888 return cp_parser_objc_throw_statement (parser);
30889 default:
30890 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30891 kwd->u.value);
30892 cp_parser_skip_to_end_of_block_or_statement (parser);
30893 }
30894
30895 return error_mark_node;
30896 }
30897
30898 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30899 look ahead to see if an objc keyword follows the attributes. This
30900 is to detect the use of prefix attributes on ObjC @interface and
30901 @protocol. */
30902
30903 static bool
30904 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30905 {
30906 cp_lexer_save_tokens (parser->lexer);
30907 *attrib = cp_parser_attributes_opt (parser);
30908 gcc_assert (*attrib);
30909 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30910 {
30911 cp_lexer_commit_tokens (parser->lexer);
30912 return true;
30913 }
30914 cp_lexer_rollback_tokens (parser->lexer);
30915 return false;
30916 }
30917
30918 /* This routine is a minimal replacement for
30919 c_parser_struct_declaration () used when parsing the list of
30920 types/names or ObjC++ properties. For example, when parsing the
30921 code
30922
30923 @property (readonly) int a, b, c;
30924
30925 this function is responsible for parsing "int a, int b, int c" and
30926 returning the declarations as CHAIN of DECLs.
30927
30928 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30929 similar parsing. */
30930 static tree
30931 cp_parser_objc_struct_declaration (cp_parser *parser)
30932 {
30933 tree decls = NULL_TREE;
30934 cp_decl_specifier_seq declspecs;
30935 int decl_class_or_enum_p;
30936 tree prefix_attributes;
30937
30938 cp_parser_decl_specifier_seq (parser,
30939 CP_PARSER_FLAGS_NONE,
30940 &declspecs,
30941 &decl_class_or_enum_p);
30942
30943 if (declspecs.type == error_mark_node)
30944 return error_mark_node;
30945
30946 /* auto, register, static, extern, mutable. */
30947 if (declspecs.storage_class != sc_none)
30948 {
30949 cp_parser_error (parser, "invalid type for property");
30950 declspecs.storage_class = sc_none;
30951 }
30952
30953 /* thread_local. */
30954 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30955 {
30956 cp_parser_error (parser, "invalid type for property");
30957 declspecs.locations[ds_thread] = 0;
30958 }
30959
30960 /* typedef. */
30961 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30962 {
30963 cp_parser_error (parser, "invalid type for property");
30964 declspecs.locations[ds_typedef] = 0;
30965 }
30966
30967 prefix_attributes = declspecs.attributes;
30968 declspecs.attributes = NULL_TREE;
30969
30970 /* Keep going until we hit the `;' at the end of the declaration. */
30971 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30972 {
30973 tree attributes, first_attribute, decl;
30974 cp_declarator *declarator;
30975 cp_token *token;
30976
30977 /* Parse the declarator. */
30978 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30979 NULL, NULL, false, false);
30980
30981 /* Look for attributes that apply to the ivar. */
30982 attributes = cp_parser_attributes_opt (parser);
30983 /* Remember which attributes are prefix attributes and
30984 which are not. */
30985 first_attribute = attributes;
30986 /* Combine the attributes. */
30987 attributes = attr_chainon (prefix_attributes, attributes);
30988
30989 decl = grokfield (declarator, &declspecs,
30990 NULL_TREE, /*init_const_expr_p=*/false,
30991 NULL_TREE, attributes);
30992
30993 if (decl == error_mark_node || decl == NULL_TREE)
30994 return error_mark_node;
30995
30996 /* Reset PREFIX_ATTRIBUTES. */
30997 if (attributes != error_mark_node)
30998 {
30999 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31000 attributes = TREE_CHAIN (attributes);
31001 if (attributes)
31002 TREE_CHAIN (attributes) = NULL_TREE;
31003 }
31004
31005 DECL_CHAIN (decl) = decls;
31006 decls = decl;
31007
31008 token = cp_lexer_peek_token (parser->lexer);
31009 if (token->type == CPP_COMMA)
31010 {
31011 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31012 continue;
31013 }
31014 else
31015 break;
31016 }
31017 return decls;
31018 }
31019
31020 /* Parse an Objective-C @property declaration. The syntax is:
31021
31022 objc-property-declaration:
31023 '@property' objc-property-attributes[opt] struct-declaration ;
31024
31025 objc-property-attributes:
31026 '(' objc-property-attribute-list ')'
31027
31028 objc-property-attribute-list:
31029 objc-property-attribute
31030 objc-property-attribute-list, objc-property-attribute
31031
31032 objc-property-attribute
31033 'getter' = identifier
31034 'setter' = identifier
31035 'readonly'
31036 'readwrite'
31037 'assign'
31038 'retain'
31039 'copy'
31040 'nonatomic'
31041
31042 For example:
31043 @property NSString *name;
31044 @property (readonly) id object;
31045 @property (retain, nonatomic, getter=getTheName) id name;
31046 @property int a, b, c;
31047
31048 PS: This function is identical to
31049 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31050 static void
31051 cp_parser_objc_at_property_declaration (cp_parser *parser)
31052 {
31053 /* The following variables hold the attributes of the properties as
31054 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31055 seen. When we see an attribute, we set them to 'true' (if they
31056 are boolean properties) or to the identifier (if they have an
31057 argument, ie, for getter and setter). Note that here we only
31058 parse the list of attributes, check the syntax and accumulate the
31059 attributes that we find. objc_add_property_declaration() will
31060 then process the information. */
31061 bool property_assign = false;
31062 bool property_copy = false;
31063 tree property_getter_ident = NULL_TREE;
31064 bool property_nonatomic = false;
31065 bool property_readonly = false;
31066 bool property_readwrite = false;
31067 bool property_retain = false;
31068 tree property_setter_ident = NULL_TREE;
31069
31070 /* 'properties' is the list of properties that we read. Usually a
31071 single one, but maybe more (eg, in "@property int a, b, c;" there
31072 are three). */
31073 tree properties;
31074 location_t loc;
31075
31076 loc = cp_lexer_peek_token (parser->lexer)->location;
31077
31078 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31079
31080 /* Parse the optional attribute list... */
31081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31082 {
31083 /* Eat the '('. */
31084 matching_parens parens;
31085 parens.consume_open (parser);
31086
31087 while (true)
31088 {
31089 bool syntax_error = false;
31090 cp_token *token = cp_lexer_peek_token (parser->lexer);
31091 enum rid keyword;
31092
31093 if (token->type != CPP_NAME)
31094 {
31095 cp_parser_error (parser, "expected identifier");
31096 break;
31097 }
31098 keyword = C_RID_CODE (token->u.value);
31099 cp_lexer_consume_token (parser->lexer);
31100 switch (keyword)
31101 {
31102 case RID_ASSIGN: property_assign = true; break;
31103 case RID_COPY: property_copy = true; break;
31104 case RID_NONATOMIC: property_nonatomic = true; break;
31105 case RID_READONLY: property_readonly = true; break;
31106 case RID_READWRITE: property_readwrite = true; break;
31107 case RID_RETAIN: property_retain = true; break;
31108
31109 case RID_GETTER:
31110 case RID_SETTER:
31111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31112 {
31113 if (keyword == RID_GETTER)
31114 cp_parser_error (parser,
31115 "missing %<=%> (after %<getter%> attribute)");
31116 else
31117 cp_parser_error (parser,
31118 "missing %<=%> (after %<setter%> attribute)");
31119 syntax_error = true;
31120 break;
31121 }
31122 cp_lexer_consume_token (parser->lexer); /* eat the = */
31123 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31124 {
31125 cp_parser_error (parser, "expected identifier");
31126 syntax_error = true;
31127 break;
31128 }
31129 if (keyword == RID_SETTER)
31130 {
31131 if (property_setter_ident != NULL_TREE)
31132 {
31133 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31134 cp_lexer_consume_token (parser->lexer);
31135 }
31136 else
31137 property_setter_ident = cp_parser_objc_selector (parser);
31138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31139 cp_parser_error (parser, "setter name must terminate with %<:%>");
31140 else
31141 cp_lexer_consume_token (parser->lexer);
31142 }
31143 else
31144 {
31145 if (property_getter_ident != NULL_TREE)
31146 {
31147 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31148 cp_lexer_consume_token (parser->lexer);
31149 }
31150 else
31151 property_getter_ident = cp_parser_objc_selector (parser);
31152 }
31153 break;
31154 default:
31155 cp_parser_error (parser, "unknown property attribute");
31156 syntax_error = true;
31157 break;
31158 }
31159
31160 if (syntax_error)
31161 break;
31162
31163 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31164 cp_lexer_consume_token (parser->lexer);
31165 else
31166 break;
31167 }
31168
31169 /* FIXME: "@property (setter, assign);" will generate a spurious
31170 "error: expected ‘)’ before ‘,’ token". This is because
31171 cp_parser_require, unlike the C counterpart, will produce an
31172 error even if we are in error recovery. */
31173 if (!parens.require_close (parser))
31174 {
31175 cp_parser_skip_to_closing_parenthesis (parser,
31176 /*recovering=*/true,
31177 /*or_comma=*/false,
31178 /*consume_paren=*/true);
31179 }
31180 }
31181
31182 /* ... and the property declaration(s). */
31183 properties = cp_parser_objc_struct_declaration (parser);
31184
31185 if (properties == error_mark_node)
31186 {
31187 cp_parser_skip_to_end_of_statement (parser);
31188 /* If the next token is now a `;', consume it. */
31189 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31190 cp_lexer_consume_token (parser->lexer);
31191 return;
31192 }
31193
31194 if (properties == NULL_TREE)
31195 cp_parser_error (parser, "expected identifier");
31196 else
31197 {
31198 /* Comma-separated properties are chained together in
31199 reverse order; add them one by one. */
31200 properties = nreverse (properties);
31201
31202 for (; properties; properties = TREE_CHAIN (properties))
31203 objc_add_property_declaration (loc, copy_node (properties),
31204 property_readonly, property_readwrite,
31205 property_assign, property_retain,
31206 property_copy, property_nonatomic,
31207 property_getter_ident, property_setter_ident);
31208 }
31209
31210 cp_parser_consume_semicolon_at_end_of_statement (parser);
31211 }
31212
31213 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31214
31215 objc-synthesize-declaration:
31216 @synthesize objc-synthesize-identifier-list ;
31217
31218 objc-synthesize-identifier-list:
31219 objc-synthesize-identifier
31220 objc-synthesize-identifier-list, objc-synthesize-identifier
31221
31222 objc-synthesize-identifier
31223 identifier
31224 identifier = identifier
31225
31226 For example:
31227 @synthesize MyProperty;
31228 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31229
31230 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31231 for C. Keep them in sync.
31232 */
31233 static void
31234 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31235 {
31236 tree list = NULL_TREE;
31237 location_t loc;
31238 loc = cp_lexer_peek_token (parser->lexer)->location;
31239
31240 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31241 while (true)
31242 {
31243 tree property, ivar;
31244 property = cp_parser_identifier (parser);
31245 if (property == error_mark_node)
31246 {
31247 cp_parser_consume_semicolon_at_end_of_statement (parser);
31248 return;
31249 }
31250 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31251 {
31252 cp_lexer_consume_token (parser->lexer);
31253 ivar = cp_parser_identifier (parser);
31254 if (ivar == error_mark_node)
31255 {
31256 cp_parser_consume_semicolon_at_end_of_statement (parser);
31257 return;
31258 }
31259 }
31260 else
31261 ivar = NULL_TREE;
31262 list = chainon (list, build_tree_list (ivar, property));
31263 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31264 cp_lexer_consume_token (parser->lexer);
31265 else
31266 break;
31267 }
31268 cp_parser_consume_semicolon_at_end_of_statement (parser);
31269 objc_add_synthesize_declaration (loc, list);
31270 }
31271
31272 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31273
31274 objc-dynamic-declaration:
31275 @dynamic identifier-list ;
31276
31277 For example:
31278 @dynamic MyProperty;
31279 @dynamic MyProperty, AnotherProperty;
31280
31281 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31282 for C. Keep them in sync.
31283 */
31284 static void
31285 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31286 {
31287 tree list = NULL_TREE;
31288 location_t loc;
31289 loc = cp_lexer_peek_token (parser->lexer)->location;
31290
31291 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31292 while (true)
31293 {
31294 tree property;
31295 property = cp_parser_identifier (parser);
31296 if (property == error_mark_node)
31297 {
31298 cp_parser_consume_semicolon_at_end_of_statement (parser);
31299 return;
31300 }
31301 list = chainon (list, build_tree_list (NULL, property));
31302 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31303 cp_lexer_consume_token (parser->lexer);
31304 else
31305 break;
31306 }
31307 cp_parser_consume_semicolon_at_end_of_statement (parser);
31308 objc_add_dynamic_declaration (loc, list);
31309 }
31310
31311 \f
31312 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31313
31314 /* Returns name of the next clause.
31315 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31316 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31317 returned and the token is consumed. */
31318
31319 static pragma_omp_clause
31320 cp_parser_omp_clause_name (cp_parser *parser)
31321 {
31322 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31323
31324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31325 result = PRAGMA_OACC_CLAUSE_AUTO;
31326 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31327 result = PRAGMA_OMP_CLAUSE_IF;
31328 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31329 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31330 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31331 result = PRAGMA_OACC_CLAUSE_DELETE;
31332 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31333 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31334 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31335 result = PRAGMA_OMP_CLAUSE_FOR;
31336 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31337 {
31338 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31339 const char *p = IDENTIFIER_POINTER (id);
31340
31341 switch (p[0])
31342 {
31343 case 'a':
31344 if (!strcmp ("aligned", p))
31345 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31346 else if (!strcmp ("async", p))
31347 result = PRAGMA_OACC_CLAUSE_ASYNC;
31348 break;
31349 case 'c':
31350 if (!strcmp ("collapse", p))
31351 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31352 else if (!strcmp ("copy", p))
31353 result = PRAGMA_OACC_CLAUSE_COPY;
31354 else if (!strcmp ("copyin", p))
31355 result = PRAGMA_OMP_CLAUSE_COPYIN;
31356 else if (!strcmp ("copyout", p))
31357 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31358 else if (!strcmp ("copyprivate", p))
31359 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31360 else if (!strcmp ("create", p))
31361 result = PRAGMA_OACC_CLAUSE_CREATE;
31362 break;
31363 case 'd':
31364 if (!strcmp ("defaultmap", p))
31365 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31366 else if (!strcmp ("depend", p))
31367 result = PRAGMA_OMP_CLAUSE_DEPEND;
31368 else if (!strcmp ("device", p))
31369 result = PRAGMA_OMP_CLAUSE_DEVICE;
31370 else if (!strcmp ("deviceptr", p))
31371 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31372 else if (!strcmp ("device_resident", p))
31373 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31374 else if (!strcmp ("dist_schedule", p))
31375 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31376 break;
31377 case 'f':
31378 if (!strcmp ("final", p))
31379 result = PRAGMA_OMP_CLAUSE_FINAL;
31380 else if (!strcmp ("firstprivate", p))
31381 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31382 else if (!strcmp ("from", p))
31383 result = PRAGMA_OMP_CLAUSE_FROM;
31384 break;
31385 case 'g':
31386 if (!strcmp ("gang", p))
31387 result = PRAGMA_OACC_CLAUSE_GANG;
31388 else if (!strcmp ("grainsize", p))
31389 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31390 break;
31391 case 'h':
31392 if (!strcmp ("hint", p))
31393 result = PRAGMA_OMP_CLAUSE_HINT;
31394 else if (!strcmp ("host", p))
31395 result = PRAGMA_OACC_CLAUSE_HOST;
31396 break;
31397 case 'i':
31398 if (!strcmp ("inbranch", p))
31399 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31400 else if (!strcmp ("independent", p))
31401 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31402 else if (!strcmp ("is_device_ptr", p))
31403 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31404 break;
31405 case 'l':
31406 if (!strcmp ("lastprivate", p))
31407 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31408 else if (!strcmp ("linear", p))
31409 result = PRAGMA_OMP_CLAUSE_LINEAR;
31410 else if (!strcmp ("link", p))
31411 result = PRAGMA_OMP_CLAUSE_LINK;
31412 break;
31413 case 'm':
31414 if (!strcmp ("map", p))
31415 result = PRAGMA_OMP_CLAUSE_MAP;
31416 else if (!strcmp ("mergeable", p))
31417 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31418 break;
31419 case 'n':
31420 if (!strcmp ("nogroup", p))
31421 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31422 else if (!strcmp ("notinbranch", p))
31423 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31424 else if (!strcmp ("nowait", p))
31425 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31426 else if (!strcmp ("num_gangs", p))
31427 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31428 else if (!strcmp ("num_tasks", p))
31429 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31430 else if (!strcmp ("num_teams", p))
31431 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31432 else if (!strcmp ("num_threads", p))
31433 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31434 else if (!strcmp ("num_workers", p))
31435 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31436 break;
31437 case 'o':
31438 if (!strcmp ("ordered", p))
31439 result = PRAGMA_OMP_CLAUSE_ORDERED;
31440 break;
31441 case 'p':
31442 if (!strcmp ("parallel", p))
31443 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31444 else if (!strcmp ("present", p))
31445 result = PRAGMA_OACC_CLAUSE_PRESENT;
31446 else if (!strcmp ("present_or_copy", p)
31447 || !strcmp ("pcopy", p))
31448 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31449 else if (!strcmp ("present_or_copyin", p)
31450 || !strcmp ("pcopyin", p))
31451 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31452 else if (!strcmp ("present_or_copyout", p)
31453 || !strcmp ("pcopyout", p))
31454 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31455 else if (!strcmp ("present_or_create", p)
31456 || !strcmp ("pcreate", p))
31457 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31458 else if (!strcmp ("priority", p))
31459 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31460 else if (!strcmp ("proc_bind", p))
31461 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31462 break;
31463 case 'r':
31464 if (!strcmp ("reduction", p))
31465 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31466 break;
31467 case 's':
31468 if (!strcmp ("safelen", p))
31469 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31470 else if (!strcmp ("schedule", p))
31471 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31472 else if (!strcmp ("sections", p))
31473 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31474 else if (!strcmp ("self", p))
31475 result = PRAGMA_OACC_CLAUSE_SELF;
31476 else if (!strcmp ("seq", p))
31477 result = PRAGMA_OACC_CLAUSE_SEQ;
31478 else if (!strcmp ("shared", p))
31479 result = PRAGMA_OMP_CLAUSE_SHARED;
31480 else if (!strcmp ("simd", p))
31481 result = PRAGMA_OMP_CLAUSE_SIMD;
31482 else if (!strcmp ("simdlen", p))
31483 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31484 break;
31485 case 't':
31486 if (!strcmp ("taskgroup", p))
31487 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31488 else if (!strcmp ("thread_limit", p))
31489 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31490 else if (!strcmp ("threads", p))
31491 result = PRAGMA_OMP_CLAUSE_THREADS;
31492 else if (!strcmp ("tile", p))
31493 result = PRAGMA_OACC_CLAUSE_TILE;
31494 else if (!strcmp ("to", p))
31495 result = PRAGMA_OMP_CLAUSE_TO;
31496 break;
31497 case 'u':
31498 if (!strcmp ("uniform", p))
31499 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31500 else if (!strcmp ("untied", p))
31501 result = PRAGMA_OMP_CLAUSE_UNTIED;
31502 else if (!strcmp ("use_device", p))
31503 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31504 else if (!strcmp ("use_device_ptr", p))
31505 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31506 break;
31507 case 'v':
31508 if (!strcmp ("vector", p))
31509 result = PRAGMA_OACC_CLAUSE_VECTOR;
31510 else if (!strcmp ("vector_length", p))
31511 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31512 break;
31513 case 'w':
31514 if (!strcmp ("wait", p))
31515 result = PRAGMA_OACC_CLAUSE_WAIT;
31516 else if (!strcmp ("worker", p))
31517 result = PRAGMA_OACC_CLAUSE_WORKER;
31518 break;
31519 }
31520 }
31521
31522 if (result != PRAGMA_OMP_CLAUSE_NONE)
31523 cp_lexer_consume_token (parser->lexer);
31524
31525 return result;
31526 }
31527
31528 /* Validate that a clause of the given type does not already exist. */
31529
31530 static void
31531 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31532 const char *name, location_t location)
31533 {
31534 tree c;
31535
31536 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31537 if (OMP_CLAUSE_CODE (c) == code)
31538 {
31539 error_at (location, "too many %qs clauses", name);
31540 break;
31541 }
31542 }
31543
31544 /* OpenMP 2.5:
31545 variable-list:
31546 identifier
31547 variable-list , identifier
31548
31549 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31550 colon). An opening parenthesis will have been consumed by the caller.
31551
31552 If KIND is nonzero, create the appropriate node and install the decl
31553 in OMP_CLAUSE_DECL and add the node to the head of the list.
31554
31555 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31556 return the list created.
31557
31558 COLON can be NULL if only closing parenthesis should end the list,
31559 or pointer to bool which will receive false if the list is terminated
31560 by closing parenthesis or true if the list is terminated by colon. */
31561
31562 static tree
31563 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31564 tree list, bool *colon)
31565 {
31566 cp_token *token;
31567 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31568 if (colon)
31569 {
31570 parser->colon_corrects_to_scope_p = false;
31571 *colon = false;
31572 }
31573 while (1)
31574 {
31575 tree name, decl;
31576
31577 token = cp_lexer_peek_token (parser->lexer);
31578 if (kind != 0
31579 && current_class_ptr
31580 && cp_parser_is_keyword (token, RID_THIS))
31581 {
31582 decl = finish_this_expr ();
31583 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31584 || CONVERT_EXPR_P (decl))
31585 decl = TREE_OPERAND (decl, 0);
31586 cp_lexer_consume_token (parser->lexer);
31587 }
31588 else
31589 {
31590 name = cp_parser_id_expression (parser, /*template_p=*/false,
31591 /*check_dependency_p=*/true,
31592 /*template_p=*/NULL,
31593 /*declarator_p=*/false,
31594 /*optional_p=*/false);
31595 if (name == error_mark_node)
31596 goto skip_comma;
31597
31598 if (identifier_p (name))
31599 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31600 else
31601 decl = name;
31602 if (decl == error_mark_node)
31603 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31604 token->location);
31605 }
31606 if (decl == error_mark_node)
31607 ;
31608 else if (kind != 0)
31609 {
31610 switch (kind)
31611 {
31612 case OMP_CLAUSE__CACHE_:
31613 /* The OpenACC cache directive explicitly only allows "array
31614 elements or subarrays". */
31615 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31616 {
31617 error_at (token->location, "expected %<[%>");
31618 decl = error_mark_node;
31619 break;
31620 }
31621 /* FALLTHROUGH. */
31622 case OMP_CLAUSE_MAP:
31623 case OMP_CLAUSE_FROM:
31624 case OMP_CLAUSE_TO:
31625 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31626 {
31627 location_t loc
31628 = cp_lexer_peek_token (parser->lexer)->location;
31629 cp_id_kind idk = CP_ID_KIND_NONE;
31630 cp_lexer_consume_token (parser->lexer);
31631 decl = convert_from_reference (decl);
31632 decl
31633 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31634 decl, false,
31635 &idk, loc);
31636 }
31637 /* FALLTHROUGH. */
31638 case OMP_CLAUSE_DEPEND:
31639 case OMP_CLAUSE_REDUCTION:
31640 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31641 {
31642 tree low_bound = NULL_TREE, length = NULL_TREE;
31643
31644 parser->colon_corrects_to_scope_p = false;
31645 cp_lexer_consume_token (parser->lexer);
31646 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31647 low_bound = cp_parser_expression (parser);
31648 if (!colon)
31649 parser->colon_corrects_to_scope_p
31650 = saved_colon_corrects_to_scope_p;
31651 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31652 length = integer_one_node;
31653 else
31654 {
31655 /* Look for `:'. */
31656 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31657 goto skip_comma;
31658 if (!cp_lexer_next_token_is (parser->lexer,
31659 CPP_CLOSE_SQUARE))
31660 length = cp_parser_expression (parser);
31661 }
31662 /* Look for the closing `]'. */
31663 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31664 RT_CLOSE_SQUARE))
31665 goto skip_comma;
31666
31667 decl = tree_cons (low_bound, length, decl);
31668 }
31669 break;
31670 default:
31671 break;
31672 }
31673
31674 tree u = build_omp_clause (token->location, kind);
31675 OMP_CLAUSE_DECL (u) = decl;
31676 OMP_CLAUSE_CHAIN (u) = list;
31677 list = u;
31678 }
31679 else
31680 list = tree_cons (decl, NULL_TREE, list);
31681
31682 get_comma:
31683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31684 break;
31685 cp_lexer_consume_token (parser->lexer);
31686 }
31687
31688 if (colon)
31689 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31690
31691 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31692 {
31693 *colon = true;
31694 cp_parser_require (parser, CPP_COLON, RT_COLON);
31695 return list;
31696 }
31697
31698 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31699 {
31700 int ending;
31701
31702 /* Try to resync to an unnested comma. Copied from
31703 cp_parser_parenthesized_expression_list. */
31704 skip_comma:
31705 if (colon)
31706 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31707 ending = cp_parser_skip_to_closing_parenthesis (parser,
31708 /*recovering=*/true,
31709 /*or_comma=*/true,
31710 /*consume_paren=*/true);
31711 if (ending < 0)
31712 goto get_comma;
31713 }
31714
31715 return list;
31716 }
31717
31718 /* Similarly, but expect leading and trailing parenthesis. This is a very
31719 common case for omp clauses. */
31720
31721 static tree
31722 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31723 {
31724 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31725 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31726 return list;
31727 }
31728
31729 /* OpenACC 2.0:
31730 copy ( variable-list )
31731 copyin ( variable-list )
31732 copyout ( variable-list )
31733 create ( variable-list )
31734 delete ( variable-list )
31735 present ( variable-list )
31736 present_or_copy ( variable-list )
31737 pcopy ( variable-list )
31738 present_or_copyin ( variable-list )
31739 pcopyin ( variable-list )
31740 present_or_copyout ( variable-list )
31741 pcopyout ( variable-list )
31742 present_or_create ( variable-list )
31743 pcreate ( variable-list ) */
31744
31745 static tree
31746 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31747 tree list)
31748 {
31749 enum gomp_map_kind kind;
31750 switch (c_kind)
31751 {
31752 case PRAGMA_OACC_CLAUSE_COPY:
31753 kind = GOMP_MAP_FORCE_TOFROM;
31754 break;
31755 case PRAGMA_OACC_CLAUSE_COPYIN:
31756 kind = GOMP_MAP_FORCE_TO;
31757 break;
31758 case PRAGMA_OACC_CLAUSE_COPYOUT:
31759 kind = GOMP_MAP_FORCE_FROM;
31760 break;
31761 case PRAGMA_OACC_CLAUSE_CREATE:
31762 kind = GOMP_MAP_FORCE_ALLOC;
31763 break;
31764 case PRAGMA_OACC_CLAUSE_DELETE:
31765 kind = GOMP_MAP_DELETE;
31766 break;
31767 case PRAGMA_OACC_CLAUSE_DEVICE:
31768 kind = GOMP_MAP_FORCE_TO;
31769 break;
31770 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31771 kind = GOMP_MAP_DEVICE_RESIDENT;
31772 break;
31773 case PRAGMA_OACC_CLAUSE_HOST:
31774 case PRAGMA_OACC_CLAUSE_SELF:
31775 kind = GOMP_MAP_FORCE_FROM;
31776 break;
31777 case PRAGMA_OACC_CLAUSE_LINK:
31778 kind = GOMP_MAP_LINK;
31779 break;
31780 case PRAGMA_OACC_CLAUSE_PRESENT:
31781 kind = GOMP_MAP_FORCE_PRESENT;
31782 break;
31783 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31784 kind = GOMP_MAP_TOFROM;
31785 break;
31786 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31787 kind = GOMP_MAP_TO;
31788 break;
31789 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31790 kind = GOMP_MAP_FROM;
31791 break;
31792 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31793 kind = GOMP_MAP_ALLOC;
31794 break;
31795 default:
31796 gcc_unreachable ();
31797 }
31798 tree nl, c;
31799 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31800
31801 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31802 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31803
31804 return nl;
31805 }
31806
31807 /* OpenACC 2.0:
31808 deviceptr ( variable-list ) */
31809
31810 static tree
31811 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31812 {
31813 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31814 tree vars, t;
31815
31816 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31817 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31818 variable-list must only allow for pointer variables. */
31819 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31820 for (t = vars; t; t = TREE_CHAIN (t))
31821 {
31822 tree v = TREE_PURPOSE (t);
31823 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31824 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31825 OMP_CLAUSE_DECL (u) = v;
31826 OMP_CLAUSE_CHAIN (u) = list;
31827 list = u;
31828 }
31829
31830 return list;
31831 }
31832
31833 /* OpenACC 2.0:
31834 auto
31835 independent
31836 nohost
31837 seq */
31838
31839 static tree
31840 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31841 enum omp_clause_code code,
31842 tree list, location_t location)
31843 {
31844 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31845 tree c = build_omp_clause (location, code);
31846 OMP_CLAUSE_CHAIN (c) = list;
31847 return c;
31848 }
31849
31850 /* OpenACC:
31851 num_gangs ( expression )
31852 num_workers ( expression )
31853 vector_length ( expression ) */
31854
31855 static tree
31856 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31857 const char *str, tree list)
31858 {
31859 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31860
31861 matching_parens parens;
31862 if (!parens.require_open (parser))
31863 return list;
31864
31865 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31866
31867 if (t == error_mark_node
31868 || !parens.require_close (parser))
31869 {
31870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31871 /*or_comma=*/false,
31872 /*consume_paren=*/true);
31873 return list;
31874 }
31875
31876 check_no_duplicate_clause (list, code, str, loc);
31877
31878 tree c = build_omp_clause (loc, code);
31879 OMP_CLAUSE_OPERAND (c, 0) = t;
31880 OMP_CLAUSE_CHAIN (c) = list;
31881 return c;
31882 }
31883
31884 /* OpenACC:
31885
31886 gang [( gang-arg-list )]
31887 worker [( [num:] int-expr )]
31888 vector [( [length:] int-expr )]
31889
31890 where gang-arg is one of:
31891
31892 [num:] int-expr
31893 static: size-expr
31894
31895 and size-expr may be:
31896
31897 *
31898 int-expr
31899 */
31900
31901 static tree
31902 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31903 const char *str, tree list)
31904 {
31905 const char *id = "num";
31906 cp_lexer *lexer = parser->lexer;
31907 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31908 location_t loc = cp_lexer_peek_token (lexer)->location;
31909
31910 if (kind == OMP_CLAUSE_VECTOR)
31911 id = "length";
31912
31913 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31914 {
31915 matching_parens parens;
31916 parens.consume_open (parser);
31917
31918 do
31919 {
31920 cp_token *next = cp_lexer_peek_token (lexer);
31921 int idx = 0;
31922
31923 /* Gang static argument. */
31924 if (kind == OMP_CLAUSE_GANG
31925 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31926 {
31927 cp_lexer_consume_token (lexer);
31928
31929 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31930 goto cleanup_error;
31931
31932 idx = 1;
31933 if (ops[idx] != NULL)
31934 {
31935 cp_parser_error (parser, "too many %<static%> arguments");
31936 goto cleanup_error;
31937 }
31938
31939 /* Check for the '*' argument. */
31940 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31941 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31942 || cp_lexer_nth_token_is (parser->lexer, 2,
31943 CPP_CLOSE_PAREN)))
31944 {
31945 cp_lexer_consume_token (lexer);
31946 ops[idx] = integer_minus_one_node;
31947
31948 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31949 {
31950 cp_lexer_consume_token (lexer);
31951 continue;
31952 }
31953 else break;
31954 }
31955 }
31956 /* Worker num: argument and vector length: arguments. */
31957 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31958 && id_equal (next->u.value, id)
31959 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31960 {
31961 cp_lexer_consume_token (lexer); /* id */
31962 cp_lexer_consume_token (lexer); /* ':' */
31963 }
31964
31965 /* Now collect the actual argument. */
31966 if (ops[idx] != NULL_TREE)
31967 {
31968 cp_parser_error (parser, "unexpected argument");
31969 goto cleanup_error;
31970 }
31971
31972 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31973 false);
31974 if (expr == error_mark_node)
31975 goto cleanup_error;
31976
31977 mark_exp_read (expr);
31978 ops[idx] = expr;
31979
31980 if (kind == OMP_CLAUSE_GANG
31981 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31982 {
31983 cp_lexer_consume_token (lexer);
31984 continue;
31985 }
31986 break;
31987 }
31988 while (1);
31989
31990 if (!parens.require_close (parser))
31991 goto cleanup_error;
31992 }
31993
31994 check_no_duplicate_clause (list, kind, str, loc);
31995
31996 c = build_omp_clause (loc, kind);
31997
31998 if (ops[1])
31999 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32000
32001 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32002 OMP_CLAUSE_CHAIN (c) = list;
32003
32004 return c;
32005
32006 cleanup_error:
32007 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32008 return list;
32009 }
32010
32011 /* OpenACC 2.0:
32012 tile ( size-expr-list ) */
32013
32014 static tree
32015 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32016 {
32017 tree c, expr = error_mark_node;
32018 tree tile = NULL_TREE;
32019
32020 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32021 so, but the spec authors never considered such a case and have
32022 differing opinions on what it might mean, including 'not
32023 allowed'.) */
32024 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32025 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32026 clause_loc);
32027
32028 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32029 return list;
32030
32031 do
32032 {
32033 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32034 return list;
32035
32036 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32037 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32038 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32039 {
32040 cp_lexer_consume_token (parser->lexer);
32041 expr = integer_zero_node;
32042 }
32043 else
32044 expr = cp_parser_constant_expression (parser);
32045
32046 tile = tree_cons (NULL_TREE, expr, tile);
32047 }
32048 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32049
32050 /* Consume the trailing ')'. */
32051 cp_lexer_consume_token (parser->lexer);
32052
32053 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32054 tile = nreverse (tile);
32055 OMP_CLAUSE_TILE_LIST (c) = tile;
32056 OMP_CLAUSE_CHAIN (c) = list;
32057 return c;
32058 }
32059
32060 /* OpenACC 2.0
32061 Parse wait clause or directive parameters. */
32062
32063 static tree
32064 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32065 {
32066 vec<tree, va_gc> *args;
32067 tree t, args_tree;
32068
32069 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32070 /*cast_p=*/false,
32071 /*allow_expansion_p=*/true,
32072 /*non_constant_p=*/NULL);
32073
32074 if (args == NULL || args->length () == 0)
32075 {
32076 cp_parser_error (parser, "expected integer expression before ')'");
32077 if (args != NULL)
32078 release_tree_vector (args);
32079 return list;
32080 }
32081
32082 args_tree = build_tree_list_vec (args);
32083
32084 release_tree_vector (args);
32085
32086 for (t = args_tree; t; t = TREE_CHAIN (t))
32087 {
32088 tree targ = TREE_VALUE (t);
32089
32090 if (targ != error_mark_node)
32091 {
32092 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32093 error ("%<wait%> expression must be integral");
32094 else
32095 {
32096 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32097
32098 targ = mark_rvalue_use (targ);
32099 OMP_CLAUSE_DECL (c) = targ;
32100 OMP_CLAUSE_CHAIN (c) = list;
32101 list = c;
32102 }
32103 }
32104 }
32105
32106 return list;
32107 }
32108
32109 /* OpenACC:
32110 wait ( int-expr-list ) */
32111
32112 static tree
32113 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32114 {
32115 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32116
32117 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32118 return list;
32119
32120 list = cp_parser_oacc_wait_list (parser, location, list);
32121
32122 return list;
32123 }
32124
32125 /* OpenMP 3.0:
32126 collapse ( constant-expression ) */
32127
32128 static tree
32129 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32130 {
32131 tree c, num;
32132 location_t loc;
32133 HOST_WIDE_INT n;
32134
32135 loc = cp_lexer_peek_token (parser->lexer)->location;
32136 matching_parens parens;
32137 if (!parens.require_open (parser))
32138 return list;
32139
32140 num = cp_parser_constant_expression (parser);
32141
32142 if (!parens.require_close (parser))
32143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32144 /*or_comma=*/false,
32145 /*consume_paren=*/true);
32146
32147 if (num == error_mark_node)
32148 return list;
32149 num = fold_non_dependent_expr (num);
32150 if (!tree_fits_shwi_p (num)
32151 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32152 || (n = tree_to_shwi (num)) <= 0
32153 || (int) n != n)
32154 {
32155 error_at (loc, "collapse argument needs positive constant integer expression");
32156 return list;
32157 }
32158
32159 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32160 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32161 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32162 OMP_CLAUSE_CHAIN (c) = list;
32163 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32164
32165 return c;
32166 }
32167
32168 /* OpenMP 2.5:
32169 default ( none | shared )
32170
32171 OpenACC:
32172 default ( none | present ) */
32173
32174 static tree
32175 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32176 location_t location, bool is_oacc)
32177 {
32178 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32179 tree c;
32180
32181 matching_parens parens;
32182 if (!parens.require_open (parser))
32183 return list;
32184 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32185 {
32186 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32187 const char *p = IDENTIFIER_POINTER (id);
32188
32189 switch (p[0])
32190 {
32191 case 'n':
32192 if (strcmp ("none", p) != 0)
32193 goto invalid_kind;
32194 kind = OMP_CLAUSE_DEFAULT_NONE;
32195 break;
32196
32197 case 'p':
32198 if (strcmp ("present", p) != 0 || !is_oacc)
32199 goto invalid_kind;
32200 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32201 break;
32202
32203 case 's':
32204 if (strcmp ("shared", p) != 0 || is_oacc)
32205 goto invalid_kind;
32206 kind = OMP_CLAUSE_DEFAULT_SHARED;
32207 break;
32208
32209 default:
32210 goto invalid_kind;
32211 }
32212
32213 cp_lexer_consume_token (parser->lexer);
32214 }
32215 else
32216 {
32217 invalid_kind:
32218 if (is_oacc)
32219 cp_parser_error (parser, "expected %<none%> or %<present%>");
32220 else
32221 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32222 }
32223
32224 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32225 || !parens.require_close (parser))
32226 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32227 /*or_comma=*/false,
32228 /*consume_paren=*/true);
32229
32230 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32231 return list;
32232
32233 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32234 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32235 OMP_CLAUSE_CHAIN (c) = list;
32236 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32237
32238 return c;
32239 }
32240
32241 /* OpenMP 3.1:
32242 final ( expression ) */
32243
32244 static tree
32245 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32246 {
32247 tree t, c;
32248
32249 matching_parens parens;
32250 if (!parens.require_open (parser))
32251 return list;
32252
32253 t = cp_parser_condition (parser);
32254
32255 if (t == error_mark_node
32256 || !parens.require_close (parser))
32257 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32258 /*or_comma=*/false,
32259 /*consume_paren=*/true);
32260
32261 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32262
32263 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32264 OMP_CLAUSE_FINAL_EXPR (c) = t;
32265 OMP_CLAUSE_CHAIN (c) = list;
32266
32267 return c;
32268 }
32269
32270 /* OpenMP 2.5:
32271 if ( expression )
32272
32273 OpenMP 4.5:
32274 if ( directive-name-modifier : expression )
32275
32276 directive-name-modifier:
32277 parallel | task | taskloop | target data | target | target update
32278 | target enter data | target exit data */
32279
32280 static tree
32281 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32282 bool is_omp)
32283 {
32284 tree t, c;
32285 enum tree_code if_modifier = ERROR_MARK;
32286
32287 matching_parens parens;
32288 if (!parens.require_open (parser))
32289 return list;
32290
32291 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32292 {
32293 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32294 const char *p = IDENTIFIER_POINTER (id);
32295 int n = 2;
32296
32297 if (strcmp ("parallel", p) == 0)
32298 if_modifier = OMP_PARALLEL;
32299 else if (strcmp ("task", p) == 0)
32300 if_modifier = OMP_TASK;
32301 else if (strcmp ("taskloop", p) == 0)
32302 if_modifier = OMP_TASKLOOP;
32303 else if (strcmp ("target", p) == 0)
32304 {
32305 if_modifier = OMP_TARGET;
32306 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32307 {
32308 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32309 p = IDENTIFIER_POINTER (id);
32310 if (strcmp ("data", p) == 0)
32311 if_modifier = OMP_TARGET_DATA;
32312 else if (strcmp ("update", p) == 0)
32313 if_modifier = OMP_TARGET_UPDATE;
32314 else if (strcmp ("enter", p) == 0)
32315 if_modifier = OMP_TARGET_ENTER_DATA;
32316 else if (strcmp ("exit", p) == 0)
32317 if_modifier = OMP_TARGET_EXIT_DATA;
32318 if (if_modifier != OMP_TARGET)
32319 n = 3;
32320 else
32321 {
32322 location_t loc
32323 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32324 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32325 "or %<exit%>");
32326 if_modifier = ERROR_MARK;
32327 }
32328 if (if_modifier == OMP_TARGET_ENTER_DATA
32329 || if_modifier == OMP_TARGET_EXIT_DATA)
32330 {
32331 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32332 {
32333 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32334 p = IDENTIFIER_POINTER (id);
32335 if (strcmp ("data", p) == 0)
32336 n = 4;
32337 }
32338 if (n != 4)
32339 {
32340 location_t loc
32341 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32342 error_at (loc, "expected %<data%>");
32343 if_modifier = ERROR_MARK;
32344 }
32345 }
32346 }
32347 }
32348 if (if_modifier != ERROR_MARK)
32349 {
32350 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32351 {
32352 while (n-- > 0)
32353 cp_lexer_consume_token (parser->lexer);
32354 }
32355 else
32356 {
32357 if (n > 2)
32358 {
32359 location_t loc
32360 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32361 error_at (loc, "expected %<:%>");
32362 }
32363 if_modifier = ERROR_MARK;
32364 }
32365 }
32366 }
32367
32368 t = cp_parser_condition (parser);
32369
32370 if (t == error_mark_node
32371 || !parens.require_close (parser))
32372 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32373 /*or_comma=*/false,
32374 /*consume_paren=*/true);
32375
32376 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32377 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32378 {
32379 if (if_modifier != ERROR_MARK
32380 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32381 {
32382 const char *p = NULL;
32383 switch (if_modifier)
32384 {
32385 case OMP_PARALLEL: p = "parallel"; break;
32386 case OMP_TASK: p = "task"; break;
32387 case OMP_TASKLOOP: p = "taskloop"; break;
32388 case OMP_TARGET_DATA: p = "target data"; break;
32389 case OMP_TARGET: p = "target"; break;
32390 case OMP_TARGET_UPDATE: p = "target update"; break;
32391 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32392 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32393 default: gcc_unreachable ();
32394 }
32395 error_at (location, "too many %<if%> clauses with %qs modifier",
32396 p);
32397 return list;
32398 }
32399 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32400 {
32401 if (!is_omp)
32402 error_at (location, "too many %<if%> clauses");
32403 else
32404 error_at (location, "too many %<if%> clauses without modifier");
32405 return list;
32406 }
32407 else if (if_modifier == ERROR_MARK
32408 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32409 {
32410 error_at (location, "if any %<if%> clause has modifier, then all "
32411 "%<if%> clauses have to use modifier");
32412 return list;
32413 }
32414 }
32415
32416 c = build_omp_clause (location, OMP_CLAUSE_IF);
32417 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32418 OMP_CLAUSE_IF_EXPR (c) = t;
32419 OMP_CLAUSE_CHAIN (c) = list;
32420
32421 return c;
32422 }
32423
32424 /* OpenMP 3.1:
32425 mergeable */
32426
32427 static tree
32428 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32429 tree list, location_t location)
32430 {
32431 tree c;
32432
32433 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32434 location);
32435
32436 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32437 OMP_CLAUSE_CHAIN (c) = list;
32438 return c;
32439 }
32440
32441 /* OpenMP 2.5:
32442 nowait */
32443
32444 static tree
32445 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32446 tree list, location_t location)
32447 {
32448 tree c;
32449
32450 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32451
32452 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32453 OMP_CLAUSE_CHAIN (c) = list;
32454 return c;
32455 }
32456
32457 /* OpenMP 2.5:
32458 num_threads ( expression ) */
32459
32460 static tree
32461 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32462 location_t location)
32463 {
32464 tree t, c;
32465
32466 matching_parens parens;
32467 if (!parens.require_open (parser))
32468 return list;
32469
32470 t = cp_parser_expression (parser);
32471
32472 if (t == error_mark_node
32473 || !parens.require_close (parser))
32474 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32475 /*or_comma=*/false,
32476 /*consume_paren=*/true);
32477
32478 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32479 "num_threads", location);
32480
32481 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32482 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32483 OMP_CLAUSE_CHAIN (c) = list;
32484
32485 return c;
32486 }
32487
32488 /* OpenMP 4.5:
32489 num_tasks ( expression ) */
32490
32491 static tree
32492 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32493 location_t location)
32494 {
32495 tree t, c;
32496
32497 matching_parens parens;
32498 if (!parens.require_open (parser))
32499 return list;
32500
32501 t = cp_parser_expression (parser);
32502
32503 if (t == error_mark_node
32504 || !parens.require_close (parser))
32505 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32506 /*or_comma=*/false,
32507 /*consume_paren=*/true);
32508
32509 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32510 "num_tasks", location);
32511
32512 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32513 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32514 OMP_CLAUSE_CHAIN (c) = list;
32515
32516 return c;
32517 }
32518
32519 /* OpenMP 4.5:
32520 grainsize ( expression ) */
32521
32522 static tree
32523 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32524 location_t location)
32525 {
32526 tree t, c;
32527
32528 matching_parens parens;
32529 if (!parens.require_open (parser))
32530 return list;
32531
32532 t = cp_parser_expression (parser);
32533
32534 if (t == error_mark_node
32535 || !parens.require_close (parser))
32536 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32537 /*or_comma=*/false,
32538 /*consume_paren=*/true);
32539
32540 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32541 "grainsize", location);
32542
32543 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32544 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32545 OMP_CLAUSE_CHAIN (c) = list;
32546
32547 return c;
32548 }
32549
32550 /* OpenMP 4.5:
32551 priority ( expression ) */
32552
32553 static tree
32554 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32555 location_t location)
32556 {
32557 tree t, c;
32558
32559 matching_parens parens;
32560 if (!parens.require_open (parser))
32561 return list;
32562
32563 t = cp_parser_expression (parser);
32564
32565 if (t == error_mark_node
32566 || !parens.require_close (parser))
32567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32568 /*or_comma=*/false,
32569 /*consume_paren=*/true);
32570
32571 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32572 "priority", location);
32573
32574 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32575 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32576 OMP_CLAUSE_CHAIN (c) = list;
32577
32578 return c;
32579 }
32580
32581 /* OpenMP 4.5:
32582 hint ( expression ) */
32583
32584 static tree
32585 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32586 location_t location)
32587 {
32588 tree t, c;
32589
32590 matching_parens parens;
32591 if (!parens.require_open (parser))
32592 return list;
32593
32594 t = cp_parser_expression (parser);
32595
32596 if (t == error_mark_node
32597 || !parens.require_close (parser))
32598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32599 /*or_comma=*/false,
32600 /*consume_paren=*/true);
32601
32602 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32603
32604 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32605 OMP_CLAUSE_HINT_EXPR (c) = t;
32606 OMP_CLAUSE_CHAIN (c) = list;
32607
32608 return c;
32609 }
32610
32611 /* OpenMP 4.5:
32612 defaultmap ( tofrom : scalar ) */
32613
32614 static tree
32615 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32616 location_t location)
32617 {
32618 tree c, id;
32619 const char *p;
32620
32621 matching_parens parens;
32622 if (!parens.require_open (parser))
32623 return list;
32624
32625 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32626 {
32627 cp_parser_error (parser, "expected %<tofrom%>");
32628 goto out_err;
32629 }
32630 id = cp_lexer_peek_token (parser->lexer)->u.value;
32631 p = IDENTIFIER_POINTER (id);
32632 if (strcmp (p, "tofrom") != 0)
32633 {
32634 cp_parser_error (parser, "expected %<tofrom%>");
32635 goto out_err;
32636 }
32637 cp_lexer_consume_token (parser->lexer);
32638 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32639 goto out_err;
32640
32641 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32642 {
32643 cp_parser_error (parser, "expected %<scalar%>");
32644 goto out_err;
32645 }
32646 id = cp_lexer_peek_token (parser->lexer)->u.value;
32647 p = IDENTIFIER_POINTER (id);
32648 if (strcmp (p, "scalar") != 0)
32649 {
32650 cp_parser_error (parser, "expected %<scalar%>");
32651 goto out_err;
32652 }
32653 cp_lexer_consume_token (parser->lexer);
32654 if (!parens.require_close (parser))
32655 goto out_err;
32656
32657 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32658 location);
32659
32660 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32661 OMP_CLAUSE_CHAIN (c) = list;
32662 return c;
32663
32664 out_err:
32665 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32666 /*or_comma=*/false,
32667 /*consume_paren=*/true);
32668 return list;
32669 }
32670
32671 /* OpenMP 2.5:
32672 ordered
32673
32674 OpenMP 4.5:
32675 ordered ( constant-expression ) */
32676
32677 static tree
32678 cp_parser_omp_clause_ordered (cp_parser *parser,
32679 tree list, location_t location)
32680 {
32681 tree c, num = NULL_TREE;
32682 HOST_WIDE_INT n;
32683
32684 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32685 "ordered", location);
32686
32687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32688 {
32689 matching_parens parens;
32690 parens.consume_open (parser);
32691
32692 num = cp_parser_constant_expression (parser);
32693
32694 if (!parens.require_close (parser))
32695 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32696 /*or_comma=*/false,
32697 /*consume_paren=*/true);
32698
32699 if (num == error_mark_node)
32700 return list;
32701 num = fold_non_dependent_expr (num);
32702 if (!tree_fits_shwi_p (num)
32703 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32704 || (n = tree_to_shwi (num)) <= 0
32705 || (int) n != n)
32706 {
32707 error_at (location,
32708 "ordered argument needs positive constant integer "
32709 "expression");
32710 return list;
32711 }
32712 }
32713
32714 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32715 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32716 OMP_CLAUSE_CHAIN (c) = list;
32717 return c;
32718 }
32719
32720 /* OpenMP 2.5:
32721 reduction ( reduction-operator : variable-list )
32722
32723 reduction-operator:
32724 One of: + * - & ^ | && ||
32725
32726 OpenMP 3.1:
32727
32728 reduction-operator:
32729 One of: + * - & ^ | && || min max
32730
32731 OpenMP 4.0:
32732
32733 reduction-operator:
32734 One of: + * - & ^ | && ||
32735 id-expression */
32736
32737 static tree
32738 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32739 {
32740 enum tree_code code = ERROR_MARK;
32741 tree nlist, c, id = NULL_TREE;
32742
32743 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32744 return list;
32745
32746 switch (cp_lexer_peek_token (parser->lexer)->type)
32747 {
32748 case CPP_PLUS: code = PLUS_EXPR; break;
32749 case CPP_MULT: code = MULT_EXPR; break;
32750 case CPP_MINUS: code = MINUS_EXPR; break;
32751 case CPP_AND: code = BIT_AND_EXPR; break;
32752 case CPP_XOR: code = BIT_XOR_EXPR; break;
32753 case CPP_OR: code = BIT_IOR_EXPR; break;
32754 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32755 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32756 default: break;
32757 }
32758
32759 if (code != ERROR_MARK)
32760 cp_lexer_consume_token (parser->lexer);
32761 else
32762 {
32763 bool saved_colon_corrects_to_scope_p;
32764 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32765 parser->colon_corrects_to_scope_p = false;
32766 id = cp_parser_id_expression (parser, /*template_p=*/false,
32767 /*check_dependency_p=*/true,
32768 /*template_p=*/NULL,
32769 /*declarator_p=*/false,
32770 /*optional_p=*/false);
32771 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32772 if (identifier_p (id))
32773 {
32774 const char *p = IDENTIFIER_POINTER (id);
32775
32776 if (strcmp (p, "min") == 0)
32777 code = MIN_EXPR;
32778 else if (strcmp (p, "max") == 0)
32779 code = MAX_EXPR;
32780 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32781 code = PLUS_EXPR;
32782 else if (id == ovl_op_identifier (false, MULT_EXPR))
32783 code = MULT_EXPR;
32784 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32785 code = MINUS_EXPR;
32786 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32787 code = BIT_AND_EXPR;
32788 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32789 code = BIT_IOR_EXPR;
32790 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32791 code = BIT_XOR_EXPR;
32792 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32793 code = TRUTH_ANDIF_EXPR;
32794 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32795 code = TRUTH_ORIF_EXPR;
32796 id = omp_reduction_id (code, id, NULL_TREE);
32797 tree scope = parser->scope;
32798 if (scope)
32799 id = build_qualified_name (NULL_TREE, scope, id, false);
32800 parser->scope = NULL_TREE;
32801 parser->qualifying_scope = NULL_TREE;
32802 parser->object_scope = NULL_TREE;
32803 }
32804 else
32805 {
32806 error ("invalid reduction-identifier");
32807 resync_fail:
32808 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32809 /*or_comma=*/false,
32810 /*consume_paren=*/true);
32811 return list;
32812 }
32813 }
32814
32815 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32816 goto resync_fail;
32817
32818 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32819 NULL);
32820 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32821 {
32822 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32823 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32824 }
32825
32826 return nlist;
32827 }
32828
32829 /* OpenMP 2.5:
32830 schedule ( schedule-kind )
32831 schedule ( schedule-kind , expression )
32832
32833 schedule-kind:
32834 static | dynamic | guided | runtime | auto
32835
32836 OpenMP 4.5:
32837 schedule ( schedule-modifier : schedule-kind )
32838 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32839
32840 schedule-modifier:
32841 simd
32842 monotonic
32843 nonmonotonic */
32844
32845 static tree
32846 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32847 {
32848 tree c, t;
32849 int modifiers = 0, nmodifiers = 0;
32850
32851 matching_parens parens;
32852 if (!parens.require_open (parser))
32853 return list;
32854
32855 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32856
32857 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32858 {
32859 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32860 const char *p = IDENTIFIER_POINTER (id);
32861 if (strcmp ("simd", p) == 0)
32862 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32863 else if (strcmp ("monotonic", p) == 0)
32864 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32865 else if (strcmp ("nonmonotonic", p) == 0)
32866 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32867 else
32868 break;
32869 cp_lexer_consume_token (parser->lexer);
32870 if (nmodifiers++ == 0
32871 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32872 cp_lexer_consume_token (parser->lexer);
32873 else
32874 {
32875 cp_parser_require (parser, CPP_COLON, RT_COLON);
32876 break;
32877 }
32878 }
32879
32880 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32881 {
32882 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32883 const char *p = IDENTIFIER_POINTER (id);
32884
32885 switch (p[0])
32886 {
32887 case 'd':
32888 if (strcmp ("dynamic", p) != 0)
32889 goto invalid_kind;
32890 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32891 break;
32892
32893 case 'g':
32894 if (strcmp ("guided", p) != 0)
32895 goto invalid_kind;
32896 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32897 break;
32898
32899 case 'r':
32900 if (strcmp ("runtime", p) != 0)
32901 goto invalid_kind;
32902 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32903 break;
32904
32905 default:
32906 goto invalid_kind;
32907 }
32908 }
32909 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32910 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32911 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32912 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32913 else
32914 goto invalid_kind;
32915 cp_lexer_consume_token (parser->lexer);
32916
32917 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32918 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32919 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32920 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32921 {
32922 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32923 "specified");
32924 modifiers = 0;
32925 }
32926
32927 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32928 {
32929 cp_token *token;
32930 cp_lexer_consume_token (parser->lexer);
32931
32932 token = cp_lexer_peek_token (parser->lexer);
32933 t = cp_parser_assignment_expression (parser);
32934
32935 if (t == error_mark_node)
32936 goto resync_fail;
32937 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32938 error_at (token->location, "schedule %<runtime%> does not take "
32939 "a %<chunk_size%> parameter");
32940 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32941 error_at (token->location, "schedule %<auto%> does not take "
32942 "a %<chunk_size%> parameter");
32943 else
32944 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32945
32946 if (!parens.require_close (parser))
32947 goto resync_fail;
32948 }
32949 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32950 goto resync_fail;
32951
32952 OMP_CLAUSE_SCHEDULE_KIND (c)
32953 = (enum omp_clause_schedule_kind)
32954 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32955
32956 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32957 OMP_CLAUSE_CHAIN (c) = list;
32958 return c;
32959
32960 invalid_kind:
32961 cp_parser_error (parser, "invalid schedule kind");
32962 resync_fail:
32963 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32964 /*or_comma=*/false,
32965 /*consume_paren=*/true);
32966 return list;
32967 }
32968
32969 /* OpenMP 3.0:
32970 untied */
32971
32972 static tree
32973 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32974 tree list, location_t location)
32975 {
32976 tree c;
32977
32978 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32979
32980 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32981 OMP_CLAUSE_CHAIN (c) = list;
32982 return c;
32983 }
32984
32985 /* OpenMP 4.0:
32986 inbranch
32987 notinbranch */
32988
32989 static tree
32990 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32991 tree list, location_t location)
32992 {
32993 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32994 tree c = build_omp_clause (location, code);
32995 OMP_CLAUSE_CHAIN (c) = list;
32996 return c;
32997 }
32998
32999 /* OpenMP 4.0:
33000 parallel
33001 for
33002 sections
33003 taskgroup */
33004
33005 static tree
33006 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33007 enum omp_clause_code code,
33008 tree list, location_t location)
33009 {
33010 tree c = build_omp_clause (location, code);
33011 OMP_CLAUSE_CHAIN (c) = list;
33012 return c;
33013 }
33014
33015 /* OpenMP 4.5:
33016 nogroup */
33017
33018 static tree
33019 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33020 tree list, location_t location)
33021 {
33022 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33023 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33024 OMP_CLAUSE_CHAIN (c) = list;
33025 return c;
33026 }
33027
33028 /* OpenMP 4.5:
33029 simd
33030 threads */
33031
33032 static tree
33033 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33034 enum omp_clause_code code,
33035 tree list, location_t location)
33036 {
33037 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33038 tree c = build_omp_clause (location, code);
33039 OMP_CLAUSE_CHAIN (c) = list;
33040 return c;
33041 }
33042
33043 /* OpenMP 4.0:
33044 num_teams ( expression ) */
33045
33046 static tree
33047 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33048 location_t location)
33049 {
33050 tree t, c;
33051
33052 matching_parens parens;
33053 if (!parens.require_open (parser))
33054 return list;
33055
33056 t = cp_parser_expression (parser);
33057
33058 if (t == error_mark_node
33059 || !parens.require_close (parser))
33060 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33061 /*or_comma=*/false,
33062 /*consume_paren=*/true);
33063
33064 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33065 "num_teams", location);
33066
33067 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33068 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33069 OMP_CLAUSE_CHAIN (c) = list;
33070
33071 return c;
33072 }
33073
33074 /* OpenMP 4.0:
33075 thread_limit ( expression ) */
33076
33077 static tree
33078 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33079 location_t location)
33080 {
33081 tree t, c;
33082
33083 matching_parens parens;
33084 if (!parens.require_open (parser))
33085 return list;
33086
33087 t = cp_parser_expression (parser);
33088
33089 if (t == error_mark_node
33090 || !parens.require_close (parser))
33091 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33092 /*or_comma=*/false,
33093 /*consume_paren=*/true);
33094
33095 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33096 "thread_limit", location);
33097
33098 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33099 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33100 OMP_CLAUSE_CHAIN (c) = list;
33101
33102 return c;
33103 }
33104
33105 /* OpenMP 4.0:
33106 aligned ( variable-list )
33107 aligned ( variable-list : constant-expression ) */
33108
33109 static tree
33110 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33111 {
33112 tree nlist, c, alignment = NULL_TREE;
33113 bool colon;
33114
33115 matching_parens parens;
33116 if (!parens.require_open (parser))
33117 return list;
33118
33119 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33120 &colon);
33121
33122 if (colon)
33123 {
33124 alignment = cp_parser_constant_expression (parser);
33125
33126 if (!parens.require_close (parser))
33127 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33128 /*or_comma=*/false,
33129 /*consume_paren=*/true);
33130
33131 if (alignment == error_mark_node)
33132 alignment = NULL_TREE;
33133 }
33134
33135 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33136 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33137
33138 return nlist;
33139 }
33140
33141 /* OpenMP 4.0:
33142 linear ( variable-list )
33143 linear ( variable-list : expression )
33144
33145 OpenMP 4.5:
33146 linear ( modifier ( variable-list ) )
33147 linear ( modifier ( variable-list ) : expression ) */
33148
33149 static tree
33150 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33151 bool declare_simd)
33152 {
33153 tree nlist, c, step = integer_one_node;
33154 bool colon;
33155 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33156
33157 matching_parens parens;
33158 if (!parens.require_open (parser))
33159 return list;
33160
33161 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33162 {
33163 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33164 const char *p = IDENTIFIER_POINTER (id);
33165
33166 if (strcmp ("ref", p) == 0)
33167 kind = OMP_CLAUSE_LINEAR_REF;
33168 else if (strcmp ("val", p) == 0)
33169 kind = OMP_CLAUSE_LINEAR_VAL;
33170 else if (strcmp ("uval", p) == 0)
33171 kind = OMP_CLAUSE_LINEAR_UVAL;
33172 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33173 cp_lexer_consume_token (parser->lexer);
33174 else
33175 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33176 }
33177
33178 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33179 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33180 &colon);
33181 else
33182 {
33183 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33184 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33185 if (colon)
33186 cp_parser_require (parser, CPP_COLON, RT_COLON);
33187 else if (!parens.require_close (parser))
33188 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33189 /*or_comma=*/false,
33190 /*consume_paren=*/true);
33191 }
33192
33193 if (colon)
33194 {
33195 step = NULL_TREE;
33196 if (declare_simd
33197 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33198 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33199 {
33200 cp_token *token = cp_lexer_peek_token (parser->lexer);
33201 cp_parser_parse_tentatively (parser);
33202 step = cp_parser_id_expression (parser, /*template_p=*/false,
33203 /*check_dependency_p=*/true,
33204 /*template_p=*/NULL,
33205 /*declarator_p=*/false,
33206 /*optional_p=*/false);
33207 if (step != error_mark_node)
33208 step = cp_parser_lookup_name_simple (parser, step, token->location);
33209 if (step == error_mark_node)
33210 {
33211 step = NULL_TREE;
33212 cp_parser_abort_tentative_parse (parser);
33213 }
33214 else if (!cp_parser_parse_definitely (parser))
33215 step = NULL_TREE;
33216 }
33217 if (!step)
33218 step = cp_parser_expression (parser);
33219
33220 if (!parens.require_close (parser))
33221 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33222 /*or_comma=*/false,
33223 /*consume_paren=*/true);
33224
33225 if (step == error_mark_node)
33226 return list;
33227 }
33228
33229 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33230 {
33231 OMP_CLAUSE_LINEAR_STEP (c) = step;
33232 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33233 }
33234
33235 return nlist;
33236 }
33237
33238 /* OpenMP 4.0:
33239 safelen ( constant-expression ) */
33240
33241 static tree
33242 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33243 location_t location)
33244 {
33245 tree t, c;
33246
33247 matching_parens parens;
33248 if (!parens.require_open (parser))
33249 return list;
33250
33251 t = cp_parser_constant_expression (parser);
33252
33253 if (t == error_mark_node
33254 || !parens.require_close (parser))
33255 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33256 /*or_comma=*/false,
33257 /*consume_paren=*/true);
33258
33259 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33260
33261 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33262 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33263 OMP_CLAUSE_CHAIN (c) = list;
33264
33265 return c;
33266 }
33267
33268 /* OpenMP 4.0:
33269 simdlen ( constant-expression ) */
33270
33271 static tree
33272 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33273 location_t location)
33274 {
33275 tree t, c;
33276
33277 matching_parens parens;
33278 if (!parens.require_open (parser))
33279 return list;
33280
33281 t = cp_parser_constant_expression (parser);
33282
33283 if (t == error_mark_node
33284 || !parens.require_close (parser))
33285 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33286 /*or_comma=*/false,
33287 /*consume_paren=*/true);
33288
33289 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33290
33291 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33292 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33293 OMP_CLAUSE_CHAIN (c) = list;
33294
33295 return c;
33296 }
33297
33298 /* OpenMP 4.5:
33299 vec:
33300 identifier [+/- integer]
33301 vec , identifier [+/- integer]
33302 */
33303
33304 static tree
33305 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33306 tree list)
33307 {
33308 tree vec = NULL;
33309
33310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33311 {
33312 cp_parser_error (parser, "expected identifier");
33313 return list;
33314 }
33315
33316 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33317 {
33318 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33319 tree t, identifier = cp_parser_identifier (parser);
33320 tree addend = NULL;
33321
33322 if (identifier == error_mark_node)
33323 t = error_mark_node;
33324 else
33325 {
33326 t = cp_parser_lookup_name_simple
33327 (parser, identifier,
33328 cp_lexer_peek_token (parser->lexer)->location);
33329 if (t == error_mark_node)
33330 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33331 id_loc);
33332 }
33333
33334 bool neg = false;
33335 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33336 neg = true;
33337 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33338 {
33339 addend = integer_zero_node;
33340 goto add_to_vector;
33341 }
33342 cp_lexer_consume_token (parser->lexer);
33343
33344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33345 {
33346 cp_parser_error (parser, "expected integer");
33347 return list;
33348 }
33349
33350 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33351 if (TREE_CODE (addend) != INTEGER_CST)
33352 {
33353 cp_parser_error (parser, "expected integer");
33354 return list;
33355 }
33356 cp_lexer_consume_token (parser->lexer);
33357
33358 add_to_vector:
33359 if (t != error_mark_node)
33360 {
33361 vec = tree_cons (addend, t, vec);
33362 if (neg)
33363 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33364 }
33365
33366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33367 break;
33368
33369 cp_lexer_consume_token (parser->lexer);
33370 }
33371
33372 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33373 {
33374 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33375 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33376 OMP_CLAUSE_DECL (u) = nreverse (vec);
33377 OMP_CLAUSE_CHAIN (u) = list;
33378 return u;
33379 }
33380 return list;
33381 }
33382
33383 /* OpenMP 4.0:
33384 depend ( depend-kind : variable-list )
33385
33386 depend-kind:
33387 in | out | inout
33388
33389 OpenMP 4.5:
33390 depend ( source )
33391
33392 depend ( sink : vec ) */
33393
33394 static tree
33395 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33396 {
33397 tree nlist, c;
33398 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33399
33400 matching_parens parens;
33401 if (!parens.require_open (parser))
33402 return list;
33403
33404 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33405 {
33406 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33407 const char *p = IDENTIFIER_POINTER (id);
33408
33409 if (strcmp ("in", p) == 0)
33410 kind = OMP_CLAUSE_DEPEND_IN;
33411 else if (strcmp ("inout", p) == 0)
33412 kind = OMP_CLAUSE_DEPEND_INOUT;
33413 else if (strcmp ("out", p) == 0)
33414 kind = OMP_CLAUSE_DEPEND_OUT;
33415 else if (strcmp ("source", p) == 0)
33416 kind = OMP_CLAUSE_DEPEND_SOURCE;
33417 else if (strcmp ("sink", p) == 0)
33418 kind = OMP_CLAUSE_DEPEND_SINK;
33419 else
33420 goto invalid_kind;
33421 }
33422 else
33423 goto invalid_kind;
33424
33425 cp_lexer_consume_token (parser->lexer);
33426
33427 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33428 {
33429 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33430 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33431 OMP_CLAUSE_DECL (c) = NULL_TREE;
33432 OMP_CLAUSE_CHAIN (c) = list;
33433 if (!parens.require_close (parser))
33434 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33435 /*or_comma=*/false,
33436 /*consume_paren=*/true);
33437 return c;
33438 }
33439
33440 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33441 goto resync_fail;
33442
33443 if (kind == OMP_CLAUSE_DEPEND_SINK)
33444 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33445 else
33446 {
33447 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33448 list, NULL);
33449
33450 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33451 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33452 }
33453 return nlist;
33454
33455 invalid_kind:
33456 cp_parser_error (parser, "invalid depend kind");
33457 resync_fail:
33458 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33459 /*or_comma=*/false,
33460 /*consume_paren=*/true);
33461 return list;
33462 }
33463
33464 /* OpenMP 4.0:
33465 map ( map-kind : variable-list )
33466 map ( variable-list )
33467
33468 map-kind:
33469 alloc | to | from | tofrom
33470
33471 OpenMP 4.5:
33472 map-kind:
33473 alloc | to | from | tofrom | release | delete
33474
33475 map ( always [,] map-kind: variable-list ) */
33476
33477 static tree
33478 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33479 {
33480 tree nlist, c;
33481 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33482 bool always = false;
33483
33484 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33485 return list;
33486
33487 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33488 {
33489 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33490 const char *p = IDENTIFIER_POINTER (id);
33491
33492 if (strcmp ("always", p) == 0)
33493 {
33494 int nth = 2;
33495 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33496 nth++;
33497 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33498 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33499 == RID_DELETE))
33500 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33501 == CPP_COLON))
33502 {
33503 always = true;
33504 cp_lexer_consume_token (parser->lexer);
33505 if (nth == 3)
33506 cp_lexer_consume_token (parser->lexer);
33507 }
33508 }
33509 }
33510
33511 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33512 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33513 {
33514 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33515 const char *p = IDENTIFIER_POINTER (id);
33516
33517 if (strcmp ("alloc", p) == 0)
33518 kind = GOMP_MAP_ALLOC;
33519 else if (strcmp ("to", p) == 0)
33520 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33521 else if (strcmp ("from", p) == 0)
33522 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33523 else if (strcmp ("tofrom", p) == 0)
33524 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33525 else if (strcmp ("release", p) == 0)
33526 kind = GOMP_MAP_RELEASE;
33527 else
33528 {
33529 cp_parser_error (parser, "invalid map kind");
33530 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33531 /*or_comma=*/false,
33532 /*consume_paren=*/true);
33533 return list;
33534 }
33535 cp_lexer_consume_token (parser->lexer);
33536 cp_lexer_consume_token (parser->lexer);
33537 }
33538 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33539 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33540 {
33541 kind = GOMP_MAP_DELETE;
33542 cp_lexer_consume_token (parser->lexer);
33543 cp_lexer_consume_token (parser->lexer);
33544 }
33545
33546 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33547 NULL);
33548
33549 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33550 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33551
33552 return nlist;
33553 }
33554
33555 /* OpenMP 4.0:
33556 device ( expression ) */
33557
33558 static tree
33559 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33560 location_t location)
33561 {
33562 tree t, c;
33563
33564 matching_parens parens;
33565 if (!parens.require_open (parser))
33566 return list;
33567
33568 t = cp_parser_expression (parser);
33569
33570 if (t == error_mark_node
33571 || !parens.require_close (parser))
33572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33573 /*or_comma=*/false,
33574 /*consume_paren=*/true);
33575
33576 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33577 "device", location);
33578
33579 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33580 OMP_CLAUSE_DEVICE_ID (c) = t;
33581 OMP_CLAUSE_CHAIN (c) = list;
33582
33583 return c;
33584 }
33585
33586 /* OpenMP 4.0:
33587 dist_schedule ( static )
33588 dist_schedule ( static , expression ) */
33589
33590 static tree
33591 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33592 location_t location)
33593 {
33594 tree c, t;
33595
33596 matching_parens parens;
33597 if (!parens.require_open (parser))
33598 return list;
33599
33600 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33601
33602 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33603 goto invalid_kind;
33604 cp_lexer_consume_token (parser->lexer);
33605
33606 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33607 {
33608 cp_lexer_consume_token (parser->lexer);
33609
33610 t = cp_parser_assignment_expression (parser);
33611
33612 if (t == error_mark_node)
33613 goto resync_fail;
33614 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33615
33616 if (!parens.require_close (parser))
33617 goto resync_fail;
33618 }
33619 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33620 goto resync_fail;
33621
33622 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33623 location);
33624 OMP_CLAUSE_CHAIN (c) = list;
33625 return c;
33626
33627 invalid_kind:
33628 cp_parser_error (parser, "invalid dist_schedule kind");
33629 resync_fail:
33630 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33631 /*or_comma=*/false,
33632 /*consume_paren=*/true);
33633 return list;
33634 }
33635
33636 /* OpenMP 4.0:
33637 proc_bind ( proc-bind-kind )
33638
33639 proc-bind-kind:
33640 master | close | spread */
33641
33642 static tree
33643 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33644 location_t location)
33645 {
33646 tree c;
33647 enum omp_clause_proc_bind_kind kind;
33648
33649 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33650 return list;
33651
33652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33653 {
33654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33655 const char *p = IDENTIFIER_POINTER (id);
33656
33657 if (strcmp ("master", p) == 0)
33658 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33659 else if (strcmp ("close", p) == 0)
33660 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33661 else if (strcmp ("spread", p) == 0)
33662 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33663 else
33664 goto invalid_kind;
33665 }
33666 else
33667 goto invalid_kind;
33668
33669 cp_lexer_consume_token (parser->lexer);
33670 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33671 goto resync_fail;
33672
33673 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33674 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33675 location);
33676 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33677 OMP_CLAUSE_CHAIN (c) = list;
33678 return c;
33679
33680 invalid_kind:
33681 cp_parser_error (parser, "invalid depend kind");
33682 resync_fail:
33683 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33684 /*or_comma=*/false,
33685 /*consume_paren=*/true);
33686 return list;
33687 }
33688
33689 /* OpenACC:
33690 async [( int-expr )] */
33691
33692 static tree
33693 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33694 {
33695 tree c, t;
33696 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33697
33698 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33699
33700 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33701 {
33702 matching_parens parens;
33703 parens.consume_open (parser);
33704
33705 t = cp_parser_expression (parser);
33706 if (t == error_mark_node
33707 || !parens.require_close (parser))
33708 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33709 /*or_comma=*/false,
33710 /*consume_paren=*/true);
33711 }
33712
33713 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33714
33715 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33716 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33717 OMP_CLAUSE_CHAIN (c) = list;
33718 list = c;
33719
33720 return list;
33721 }
33722
33723 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33724 is a bitmask in MASK. Return the list of clauses found. */
33725
33726 static tree
33727 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33728 const char *where, cp_token *pragma_tok,
33729 bool finish_p = true)
33730 {
33731 tree clauses = NULL;
33732 bool first = true;
33733
33734 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33735 {
33736 location_t here;
33737 pragma_omp_clause c_kind;
33738 omp_clause_code code;
33739 const char *c_name;
33740 tree prev = clauses;
33741
33742 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33743 cp_lexer_consume_token (parser->lexer);
33744
33745 here = cp_lexer_peek_token (parser->lexer)->location;
33746 c_kind = cp_parser_omp_clause_name (parser);
33747
33748 switch (c_kind)
33749 {
33750 case PRAGMA_OACC_CLAUSE_ASYNC:
33751 clauses = cp_parser_oacc_clause_async (parser, clauses);
33752 c_name = "async";
33753 break;
33754 case PRAGMA_OACC_CLAUSE_AUTO:
33755 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33756 clauses, here);
33757 c_name = "auto";
33758 break;
33759 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33760 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33761 c_name = "collapse";
33762 break;
33763 case PRAGMA_OACC_CLAUSE_COPY:
33764 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33765 c_name = "copy";
33766 break;
33767 case PRAGMA_OACC_CLAUSE_COPYIN:
33768 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33769 c_name = "copyin";
33770 break;
33771 case PRAGMA_OACC_CLAUSE_COPYOUT:
33772 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33773 c_name = "copyout";
33774 break;
33775 case PRAGMA_OACC_CLAUSE_CREATE:
33776 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33777 c_name = "create";
33778 break;
33779 case PRAGMA_OACC_CLAUSE_DELETE:
33780 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33781 c_name = "delete";
33782 break;
33783 case PRAGMA_OMP_CLAUSE_DEFAULT:
33784 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33785 c_name = "default";
33786 break;
33787 case PRAGMA_OACC_CLAUSE_DEVICE:
33788 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33789 c_name = "device";
33790 break;
33791 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33792 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33793 c_name = "deviceptr";
33794 break;
33795 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33796 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33797 c_name = "device_resident";
33798 break;
33799 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33800 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33801 clauses);
33802 c_name = "firstprivate";
33803 break;
33804 case PRAGMA_OACC_CLAUSE_GANG:
33805 c_name = "gang";
33806 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33807 c_name, clauses);
33808 break;
33809 case PRAGMA_OACC_CLAUSE_HOST:
33810 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33811 c_name = "host";
33812 break;
33813 case PRAGMA_OACC_CLAUSE_IF:
33814 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33815 c_name = "if";
33816 break;
33817 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33818 clauses = cp_parser_oacc_simple_clause (parser,
33819 OMP_CLAUSE_INDEPENDENT,
33820 clauses, here);
33821 c_name = "independent";
33822 break;
33823 case PRAGMA_OACC_CLAUSE_LINK:
33824 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33825 c_name = "link";
33826 break;
33827 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33828 code = OMP_CLAUSE_NUM_GANGS;
33829 c_name = "num_gangs";
33830 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33831 clauses);
33832 break;
33833 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33834 c_name = "num_workers";
33835 code = OMP_CLAUSE_NUM_WORKERS;
33836 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33837 clauses);
33838 break;
33839 case PRAGMA_OACC_CLAUSE_PRESENT:
33840 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33841 c_name = "present";
33842 break;
33843 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33844 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33845 c_name = "present_or_copy";
33846 break;
33847 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33848 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33849 c_name = "present_or_copyin";
33850 break;
33851 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33852 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33853 c_name = "present_or_copyout";
33854 break;
33855 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33856 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33857 c_name = "present_or_create";
33858 break;
33859 case PRAGMA_OACC_CLAUSE_PRIVATE:
33860 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33861 clauses);
33862 c_name = "private";
33863 break;
33864 case PRAGMA_OACC_CLAUSE_REDUCTION:
33865 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33866 c_name = "reduction";
33867 break;
33868 case PRAGMA_OACC_CLAUSE_SELF:
33869 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33870 c_name = "self";
33871 break;
33872 case PRAGMA_OACC_CLAUSE_SEQ:
33873 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33874 clauses, here);
33875 c_name = "seq";
33876 break;
33877 case PRAGMA_OACC_CLAUSE_TILE:
33878 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33879 c_name = "tile";
33880 break;
33881 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33882 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33883 clauses);
33884 c_name = "use_device";
33885 break;
33886 case PRAGMA_OACC_CLAUSE_VECTOR:
33887 c_name = "vector";
33888 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33889 c_name, clauses);
33890 break;
33891 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33892 c_name = "vector_length";
33893 code = OMP_CLAUSE_VECTOR_LENGTH;
33894 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33895 clauses);
33896 break;
33897 case PRAGMA_OACC_CLAUSE_WAIT:
33898 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33899 c_name = "wait";
33900 break;
33901 case PRAGMA_OACC_CLAUSE_WORKER:
33902 c_name = "worker";
33903 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33904 c_name, clauses);
33905 break;
33906 default:
33907 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33908 goto saw_error;
33909 }
33910
33911 first = false;
33912
33913 if (((mask >> c_kind) & 1) == 0)
33914 {
33915 /* Remove the invalid clause(s) from the list to avoid
33916 confusing the rest of the compiler. */
33917 clauses = prev;
33918 error_at (here, "%qs is not valid for %qs", c_name, where);
33919 }
33920 }
33921
33922 saw_error:
33923 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33924
33925 if (finish_p)
33926 return finish_omp_clauses (clauses, C_ORT_ACC);
33927
33928 return clauses;
33929 }
33930
33931 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33932 is a bitmask in MASK. Return the list of clauses found; the result
33933 of clause default goes in *pdefault. */
33934
33935 static tree
33936 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33937 const char *where, cp_token *pragma_tok,
33938 bool finish_p = true)
33939 {
33940 tree clauses = NULL;
33941 bool first = true;
33942 cp_token *token = NULL;
33943
33944 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33945 {
33946 pragma_omp_clause c_kind;
33947 const char *c_name;
33948 tree prev = clauses;
33949
33950 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33951 cp_lexer_consume_token (parser->lexer);
33952
33953 token = cp_lexer_peek_token (parser->lexer);
33954 c_kind = cp_parser_omp_clause_name (parser);
33955
33956 switch (c_kind)
33957 {
33958 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33959 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33960 token->location);
33961 c_name = "collapse";
33962 break;
33963 case PRAGMA_OMP_CLAUSE_COPYIN:
33964 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33965 c_name = "copyin";
33966 break;
33967 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33968 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33969 clauses);
33970 c_name = "copyprivate";
33971 break;
33972 case PRAGMA_OMP_CLAUSE_DEFAULT:
33973 clauses = cp_parser_omp_clause_default (parser, clauses,
33974 token->location, false);
33975 c_name = "default";
33976 break;
33977 case PRAGMA_OMP_CLAUSE_FINAL:
33978 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33979 c_name = "final";
33980 break;
33981 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33982 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33983 clauses);
33984 c_name = "firstprivate";
33985 break;
33986 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33987 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33988 token->location);
33989 c_name = "grainsize";
33990 break;
33991 case PRAGMA_OMP_CLAUSE_HINT:
33992 clauses = cp_parser_omp_clause_hint (parser, clauses,
33993 token->location);
33994 c_name = "hint";
33995 break;
33996 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33997 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33998 token->location);
33999 c_name = "defaultmap";
34000 break;
34001 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34002 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34003 clauses);
34004 c_name = "use_device_ptr";
34005 break;
34006 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34007 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34008 clauses);
34009 c_name = "is_device_ptr";
34010 break;
34011 case PRAGMA_OMP_CLAUSE_IF:
34012 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34013 true);
34014 c_name = "if";
34015 break;
34016 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34017 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34018 clauses);
34019 c_name = "lastprivate";
34020 break;
34021 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34022 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34023 token->location);
34024 c_name = "mergeable";
34025 break;
34026 case PRAGMA_OMP_CLAUSE_NOWAIT:
34027 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34028 c_name = "nowait";
34029 break;
34030 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34031 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34032 token->location);
34033 c_name = "num_tasks";
34034 break;
34035 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34036 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34037 token->location);
34038 c_name = "num_threads";
34039 break;
34040 case PRAGMA_OMP_CLAUSE_ORDERED:
34041 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34042 token->location);
34043 c_name = "ordered";
34044 break;
34045 case PRAGMA_OMP_CLAUSE_PRIORITY:
34046 clauses = cp_parser_omp_clause_priority (parser, clauses,
34047 token->location);
34048 c_name = "priority";
34049 break;
34050 case PRAGMA_OMP_CLAUSE_PRIVATE:
34051 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34052 clauses);
34053 c_name = "private";
34054 break;
34055 case PRAGMA_OMP_CLAUSE_REDUCTION:
34056 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34057 c_name = "reduction";
34058 break;
34059 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34060 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34061 token->location);
34062 c_name = "schedule";
34063 break;
34064 case PRAGMA_OMP_CLAUSE_SHARED:
34065 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34066 clauses);
34067 c_name = "shared";
34068 break;
34069 case PRAGMA_OMP_CLAUSE_UNTIED:
34070 clauses = cp_parser_omp_clause_untied (parser, clauses,
34071 token->location);
34072 c_name = "untied";
34073 break;
34074 case PRAGMA_OMP_CLAUSE_INBRANCH:
34075 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34076 clauses, token->location);
34077 c_name = "inbranch";
34078 break;
34079 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34080 clauses = cp_parser_omp_clause_branch (parser,
34081 OMP_CLAUSE_NOTINBRANCH,
34082 clauses, token->location);
34083 c_name = "notinbranch";
34084 break;
34085 case PRAGMA_OMP_CLAUSE_PARALLEL:
34086 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34087 clauses, token->location);
34088 c_name = "parallel";
34089 if (!first)
34090 {
34091 clause_not_first:
34092 error_at (token->location, "%qs must be the first clause of %qs",
34093 c_name, where);
34094 clauses = prev;
34095 }
34096 break;
34097 case PRAGMA_OMP_CLAUSE_FOR:
34098 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34099 clauses, token->location);
34100 c_name = "for";
34101 if (!first)
34102 goto clause_not_first;
34103 break;
34104 case PRAGMA_OMP_CLAUSE_SECTIONS:
34105 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34106 clauses, token->location);
34107 c_name = "sections";
34108 if (!first)
34109 goto clause_not_first;
34110 break;
34111 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34112 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34113 clauses, token->location);
34114 c_name = "taskgroup";
34115 if (!first)
34116 goto clause_not_first;
34117 break;
34118 case PRAGMA_OMP_CLAUSE_LINK:
34119 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34120 c_name = "to";
34121 break;
34122 case PRAGMA_OMP_CLAUSE_TO:
34123 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34124 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34125 clauses);
34126 else
34127 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34128 c_name = "to";
34129 break;
34130 case PRAGMA_OMP_CLAUSE_FROM:
34131 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34132 c_name = "from";
34133 break;
34134 case PRAGMA_OMP_CLAUSE_UNIFORM:
34135 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34136 clauses);
34137 c_name = "uniform";
34138 break;
34139 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34140 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34141 token->location);
34142 c_name = "num_teams";
34143 break;
34144 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34145 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34146 token->location);
34147 c_name = "thread_limit";
34148 break;
34149 case PRAGMA_OMP_CLAUSE_ALIGNED:
34150 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34151 c_name = "aligned";
34152 break;
34153 case PRAGMA_OMP_CLAUSE_LINEAR:
34154 {
34155 bool declare_simd = false;
34156 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34157 declare_simd = true;
34158 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34159 }
34160 c_name = "linear";
34161 break;
34162 case PRAGMA_OMP_CLAUSE_DEPEND:
34163 clauses = cp_parser_omp_clause_depend (parser, clauses,
34164 token->location);
34165 c_name = "depend";
34166 break;
34167 case PRAGMA_OMP_CLAUSE_MAP:
34168 clauses = cp_parser_omp_clause_map (parser, clauses);
34169 c_name = "map";
34170 break;
34171 case PRAGMA_OMP_CLAUSE_DEVICE:
34172 clauses = cp_parser_omp_clause_device (parser, clauses,
34173 token->location);
34174 c_name = "device";
34175 break;
34176 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34177 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34178 token->location);
34179 c_name = "dist_schedule";
34180 break;
34181 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34182 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34183 token->location);
34184 c_name = "proc_bind";
34185 break;
34186 case PRAGMA_OMP_CLAUSE_SAFELEN:
34187 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34188 token->location);
34189 c_name = "safelen";
34190 break;
34191 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34192 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34193 token->location);
34194 c_name = "simdlen";
34195 break;
34196 case PRAGMA_OMP_CLAUSE_NOGROUP:
34197 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34198 token->location);
34199 c_name = "nogroup";
34200 break;
34201 case PRAGMA_OMP_CLAUSE_THREADS:
34202 clauses
34203 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34204 clauses, token->location);
34205 c_name = "threads";
34206 break;
34207 case PRAGMA_OMP_CLAUSE_SIMD:
34208 clauses
34209 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34210 clauses, token->location);
34211 c_name = "simd";
34212 break;
34213 default:
34214 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34215 goto saw_error;
34216 }
34217
34218 first = false;
34219
34220 if (((mask >> c_kind) & 1) == 0)
34221 {
34222 /* Remove the invalid clause(s) from the list to avoid
34223 confusing the rest of the compiler. */
34224 clauses = prev;
34225 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34226 }
34227 }
34228 saw_error:
34229 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34230 if (finish_p)
34231 {
34232 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34233 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34234 else
34235 return finish_omp_clauses (clauses, C_ORT_OMP);
34236 }
34237 return clauses;
34238 }
34239
34240 /* OpenMP 2.5:
34241 structured-block:
34242 statement
34243
34244 In practice, we're also interested in adding the statement to an
34245 outer node. So it is convenient if we work around the fact that
34246 cp_parser_statement calls add_stmt. */
34247
34248 static unsigned
34249 cp_parser_begin_omp_structured_block (cp_parser *parser)
34250 {
34251 unsigned save = parser->in_statement;
34252
34253 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34254 This preserves the "not within loop or switch" style error messages
34255 for nonsense cases like
34256 void foo() {
34257 #pragma omp single
34258 break;
34259 }
34260 */
34261 if (parser->in_statement)
34262 parser->in_statement = IN_OMP_BLOCK;
34263
34264 return save;
34265 }
34266
34267 static void
34268 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34269 {
34270 parser->in_statement = save;
34271 }
34272
34273 static tree
34274 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34275 {
34276 tree stmt = begin_omp_structured_block ();
34277 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34278
34279 cp_parser_statement (parser, NULL_TREE, false, if_p);
34280
34281 cp_parser_end_omp_structured_block (parser, save);
34282 return finish_omp_structured_block (stmt);
34283 }
34284
34285 /* OpenMP 2.5:
34286 # pragma omp atomic new-line
34287 expression-stmt
34288
34289 expression-stmt:
34290 x binop= expr | x++ | ++x | x-- | --x
34291 binop:
34292 +, *, -, /, &, ^, |, <<, >>
34293
34294 where x is an lvalue expression with scalar type.
34295
34296 OpenMP 3.1:
34297 # pragma omp atomic new-line
34298 update-stmt
34299
34300 # pragma omp atomic read new-line
34301 read-stmt
34302
34303 # pragma omp atomic write new-line
34304 write-stmt
34305
34306 # pragma omp atomic update new-line
34307 update-stmt
34308
34309 # pragma omp atomic capture new-line
34310 capture-stmt
34311
34312 # pragma omp atomic capture new-line
34313 capture-block
34314
34315 read-stmt:
34316 v = x
34317 write-stmt:
34318 x = expr
34319 update-stmt:
34320 expression-stmt | x = x binop expr
34321 capture-stmt:
34322 v = expression-stmt
34323 capture-block:
34324 { v = x; update-stmt; } | { update-stmt; v = x; }
34325
34326 OpenMP 4.0:
34327 update-stmt:
34328 expression-stmt | x = x binop expr | x = expr binop x
34329 capture-stmt:
34330 v = update-stmt
34331 capture-block:
34332 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34333
34334 where x and v are lvalue expressions with scalar type. */
34335
34336 static void
34337 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34338 {
34339 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34340 tree rhs1 = NULL_TREE, orig_lhs;
34341 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34342 bool structured_block = false;
34343 bool seq_cst = false;
34344
34345 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34346 {
34347 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34348 const char *p = IDENTIFIER_POINTER (id);
34349
34350 if (!strcmp (p, "seq_cst"))
34351 {
34352 seq_cst = true;
34353 cp_lexer_consume_token (parser->lexer);
34354 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34355 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34356 cp_lexer_consume_token (parser->lexer);
34357 }
34358 }
34359 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34360 {
34361 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34362 const char *p = IDENTIFIER_POINTER (id);
34363
34364 if (!strcmp (p, "read"))
34365 code = OMP_ATOMIC_READ;
34366 else if (!strcmp (p, "write"))
34367 code = NOP_EXPR;
34368 else if (!strcmp (p, "update"))
34369 code = OMP_ATOMIC;
34370 else if (!strcmp (p, "capture"))
34371 code = OMP_ATOMIC_CAPTURE_NEW;
34372 else
34373 p = NULL;
34374 if (p)
34375 cp_lexer_consume_token (parser->lexer);
34376 }
34377 if (!seq_cst)
34378 {
34379 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34380 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34381 cp_lexer_consume_token (parser->lexer);
34382
34383 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34384 {
34385 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34386 const char *p = IDENTIFIER_POINTER (id);
34387
34388 if (!strcmp (p, "seq_cst"))
34389 {
34390 seq_cst = true;
34391 cp_lexer_consume_token (parser->lexer);
34392 }
34393 }
34394 }
34395 cp_parser_require_pragma_eol (parser, pragma_tok);
34396
34397 switch (code)
34398 {
34399 case OMP_ATOMIC_READ:
34400 case NOP_EXPR: /* atomic write */
34401 v = cp_parser_unary_expression (parser);
34402 if (v == error_mark_node)
34403 goto saw_error;
34404 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34405 goto saw_error;
34406 if (code == NOP_EXPR)
34407 lhs = cp_parser_expression (parser);
34408 else
34409 lhs = cp_parser_unary_expression (parser);
34410 if (lhs == error_mark_node)
34411 goto saw_error;
34412 if (code == NOP_EXPR)
34413 {
34414 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34415 opcode. */
34416 code = OMP_ATOMIC;
34417 rhs = lhs;
34418 lhs = v;
34419 v = NULL_TREE;
34420 }
34421 goto done;
34422 case OMP_ATOMIC_CAPTURE_NEW:
34423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34424 {
34425 cp_lexer_consume_token (parser->lexer);
34426 structured_block = true;
34427 }
34428 else
34429 {
34430 v = cp_parser_unary_expression (parser);
34431 if (v == error_mark_node)
34432 goto saw_error;
34433 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34434 goto saw_error;
34435 }
34436 default:
34437 break;
34438 }
34439
34440 restart:
34441 lhs = cp_parser_unary_expression (parser);
34442 orig_lhs = lhs;
34443 switch (TREE_CODE (lhs))
34444 {
34445 case ERROR_MARK:
34446 goto saw_error;
34447
34448 case POSTINCREMENT_EXPR:
34449 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34450 code = OMP_ATOMIC_CAPTURE_OLD;
34451 /* FALLTHROUGH */
34452 case PREINCREMENT_EXPR:
34453 lhs = TREE_OPERAND (lhs, 0);
34454 opcode = PLUS_EXPR;
34455 rhs = integer_one_node;
34456 break;
34457
34458 case POSTDECREMENT_EXPR:
34459 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34460 code = OMP_ATOMIC_CAPTURE_OLD;
34461 /* FALLTHROUGH */
34462 case PREDECREMENT_EXPR:
34463 lhs = TREE_OPERAND (lhs, 0);
34464 opcode = MINUS_EXPR;
34465 rhs = integer_one_node;
34466 break;
34467
34468 case COMPOUND_EXPR:
34469 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34470 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34471 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34472 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34473 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34474 (TREE_OPERAND (lhs, 1), 0), 0)))
34475 == BOOLEAN_TYPE)
34476 /* Undo effects of boolean_increment for post {in,de}crement. */
34477 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34478 /* FALLTHRU */
34479 case MODIFY_EXPR:
34480 if (TREE_CODE (lhs) == MODIFY_EXPR
34481 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34482 {
34483 /* Undo effects of boolean_increment. */
34484 if (integer_onep (TREE_OPERAND (lhs, 1)))
34485 {
34486 /* This is pre or post increment. */
34487 rhs = TREE_OPERAND (lhs, 1);
34488 lhs = TREE_OPERAND (lhs, 0);
34489 opcode = NOP_EXPR;
34490 if (code == OMP_ATOMIC_CAPTURE_NEW
34491 && !structured_block
34492 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34493 code = OMP_ATOMIC_CAPTURE_OLD;
34494 break;
34495 }
34496 }
34497 /* FALLTHRU */
34498 default:
34499 switch (cp_lexer_peek_token (parser->lexer)->type)
34500 {
34501 case CPP_MULT_EQ:
34502 opcode = MULT_EXPR;
34503 break;
34504 case CPP_DIV_EQ:
34505 opcode = TRUNC_DIV_EXPR;
34506 break;
34507 case CPP_PLUS_EQ:
34508 opcode = PLUS_EXPR;
34509 break;
34510 case CPP_MINUS_EQ:
34511 opcode = MINUS_EXPR;
34512 break;
34513 case CPP_LSHIFT_EQ:
34514 opcode = LSHIFT_EXPR;
34515 break;
34516 case CPP_RSHIFT_EQ:
34517 opcode = RSHIFT_EXPR;
34518 break;
34519 case CPP_AND_EQ:
34520 opcode = BIT_AND_EXPR;
34521 break;
34522 case CPP_OR_EQ:
34523 opcode = BIT_IOR_EXPR;
34524 break;
34525 case CPP_XOR_EQ:
34526 opcode = BIT_XOR_EXPR;
34527 break;
34528 case CPP_EQ:
34529 enum cp_parser_prec oprec;
34530 cp_token *token;
34531 cp_lexer_consume_token (parser->lexer);
34532 cp_parser_parse_tentatively (parser);
34533 rhs1 = cp_parser_simple_cast_expression (parser);
34534 if (rhs1 == error_mark_node)
34535 {
34536 cp_parser_abort_tentative_parse (parser);
34537 cp_parser_simple_cast_expression (parser);
34538 goto saw_error;
34539 }
34540 token = cp_lexer_peek_token (parser->lexer);
34541 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34542 {
34543 cp_parser_abort_tentative_parse (parser);
34544 cp_parser_parse_tentatively (parser);
34545 rhs = cp_parser_binary_expression (parser, false, true,
34546 PREC_NOT_OPERATOR, NULL);
34547 if (rhs == error_mark_node)
34548 {
34549 cp_parser_abort_tentative_parse (parser);
34550 cp_parser_binary_expression (parser, false, true,
34551 PREC_NOT_OPERATOR, NULL);
34552 goto saw_error;
34553 }
34554 switch (TREE_CODE (rhs))
34555 {
34556 case MULT_EXPR:
34557 case TRUNC_DIV_EXPR:
34558 case RDIV_EXPR:
34559 case PLUS_EXPR:
34560 case MINUS_EXPR:
34561 case LSHIFT_EXPR:
34562 case RSHIFT_EXPR:
34563 case BIT_AND_EXPR:
34564 case BIT_IOR_EXPR:
34565 case BIT_XOR_EXPR:
34566 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34567 {
34568 if (cp_parser_parse_definitely (parser))
34569 {
34570 opcode = TREE_CODE (rhs);
34571 rhs1 = TREE_OPERAND (rhs, 0);
34572 rhs = TREE_OPERAND (rhs, 1);
34573 goto stmt_done;
34574 }
34575 else
34576 goto saw_error;
34577 }
34578 break;
34579 default:
34580 break;
34581 }
34582 cp_parser_abort_tentative_parse (parser);
34583 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34584 {
34585 rhs = cp_parser_expression (parser);
34586 if (rhs == error_mark_node)
34587 goto saw_error;
34588 opcode = NOP_EXPR;
34589 rhs1 = NULL_TREE;
34590 goto stmt_done;
34591 }
34592 cp_parser_error (parser,
34593 "invalid form of %<#pragma omp atomic%>");
34594 goto saw_error;
34595 }
34596 if (!cp_parser_parse_definitely (parser))
34597 goto saw_error;
34598 switch (token->type)
34599 {
34600 case CPP_SEMICOLON:
34601 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34602 {
34603 code = OMP_ATOMIC_CAPTURE_OLD;
34604 v = lhs;
34605 lhs = NULL_TREE;
34606 lhs1 = rhs1;
34607 rhs1 = NULL_TREE;
34608 cp_lexer_consume_token (parser->lexer);
34609 goto restart;
34610 }
34611 else if (structured_block)
34612 {
34613 opcode = NOP_EXPR;
34614 rhs = rhs1;
34615 rhs1 = NULL_TREE;
34616 goto stmt_done;
34617 }
34618 cp_parser_error (parser,
34619 "invalid form of %<#pragma omp atomic%>");
34620 goto saw_error;
34621 case CPP_MULT:
34622 opcode = MULT_EXPR;
34623 break;
34624 case CPP_DIV:
34625 opcode = TRUNC_DIV_EXPR;
34626 break;
34627 case CPP_PLUS:
34628 opcode = PLUS_EXPR;
34629 break;
34630 case CPP_MINUS:
34631 opcode = MINUS_EXPR;
34632 break;
34633 case CPP_LSHIFT:
34634 opcode = LSHIFT_EXPR;
34635 break;
34636 case CPP_RSHIFT:
34637 opcode = RSHIFT_EXPR;
34638 break;
34639 case CPP_AND:
34640 opcode = BIT_AND_EXPR;
34641 break;
34642 case CPP_OR:
34643 opcode = BIT_IOR_EXPR;
34644 break;
34645 case CPP_XOR:
34646 opcode = BIT_XOR_EXPR;
34647 break;
34648 default:
34649 cp_parser_error (parser,
34650 "invalid operator for %<#pragma omp atomic%>");
34651 goto saw_error;
34652 }
34653 oprec = TOKEN_PRECEDENCE (token);
34654 gcc_assert (oprec != PREC_NOT_OPERATOR);
34655 if (commutative_tree_code (opcode))
34656 oprec = (enum cp_parser_prec) (oprec - 1);
34657 cp_lexer_consume_token (parser->lexer);
34658 rhs = cp_parser_binary_expression (parser, false, false,
34659 oprec, NULL);
34660 if (rhs == error_mark_node)
34661 goto saw_error;
34662 goto stmt_done;
34663 /* FALLTHROUGH */
34664 default:
34665 cp_parser_error (parser,
34666 "invalid operator for %<#pragma omp atomic%>");
34667 goto saw_error;
34668 }
34669 cp_lexer_consume_token (parser->lexer);
34670
34671 rhs = cp_parser_expression (parser);
34672 if (rhs == error_mark_node)
34673 goto saw_error;
34674 break;
34675 }
34676 stmt_done:
34677 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34678 {
34679 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34680 goto saw_error;
34681 v = cp_parser_unary_expression (parser);
34682 if (v == error_mark_node)
34683 goto saw_error;
34684 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34685 goto saw_error;
34686 lhs1 = cp_parser_unary_expression (parser);
34687 if (lhs1 == error_mark_node)
34688 goto saw_error;
34689 }
34690 if (structured_block)
34691 {
34692 cp_parser_consume_semicolon_at_end_of_statement (parser);
34693 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34694 }
34695 done:
34696 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34697 if (!structured_block)
34698 cp_parser_consume_semicolon_at_end_of_statement (parser);
34699 return;
34700
34701 saw_error:
34702 cp_parser_skip_to_end_of_block_or_statement (parser);
34703 if (structured_block)
34704 {
34705 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34706 cp_lexer_consume_token (parser->lexer);
34707 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34708 {
34709 cp_parser_skip_to_end_of_block_or_statement (parser);
34710 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34711 cp_lexer_consume_token (parser->lexer);
34712 }
34713 }
34714 }
34715
34716
34717 /* OpenMP 2.5:
34718 # pragma omp barrier new-line */
34719
34720 static void
34721 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34722 {
34723 cp_parser_require_pragma_eol (parser, pragma_tok);
34724 finish_omp_barrier ();
34725 }
34726
34727 /* OpenMP 2.5:
34728 # pragma omp critical [(name)] new-line
34729 structured-block
34730
34731 OpenMP 4.5:
34732 # pragma omp critical [(name) [hint(expression)]] new-line
34733 structured-block */
34734
34735 #define OMP_CRITICAL_CLAUSE_MASK \
34736 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34737
34738 static tree
34739 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34740 {
34741 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34742
34743 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34744 {
34745 matching_parens parens;
34746 parens.consume_open (parser);
34747
34748 name = cp_parser_identifier (parser);
34749
34750 if (name == error_mark_node
34751 || !parens.require_close (parser))
34752 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34753 /*or_comma=*/false,
34754 /*consume_paren=*/true);
34755 if (name == error_mark_node)
34756 name = NULL;
34757
34758 clauses = cp_parser_omp_all_clauses (parser,
34759 OMP_CRITICAL_CLAUSE_MASK,
34760 "#pragma omp critical", pragma_tok);
34761 }
34762 else
34763 cp_parser_require_pragma_eol (parser, pragma_tok);
34764
34765 stmt = cp_parser_omp_structured_block (parser, if_p);
34766 return c_finish_omp_critical (input_location, stmt, name, clauses);
34767 }
34768
34769 /* OpenMP 2.5:
34770 # pragma omp flush flush-vars[opt] new-line
34771
34772 flush-vars:
34773 ( variable-list ) */
34774
34775 static void
34776 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34777 {
34778 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34779 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34780 cp_parser_require_pragma_eol (parser, pragma_tok);
34781
34782 finish_omp_flush ();
34783 }
34784
34785 /* Helper function, to parse omp for increment expression. */
34786
34787 static tree
34788 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34789 {
34790 tree cond = cp_parser_binary_expression (parser, false, true,
34791 PREC_NOT_OPERATOR, NULL);
34792 if (cond == error_mark_node
34793 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34794 {
34795 cp_parser_skip_to_end_of_statement (parser);
34796 return error_mark_node;
34797 }
34798
34799 switch (TREE_CODE (cond))
34800 {
34801 case GT_EXPR:
34802 case GE_EXPR:
34803 case LT_EXPR:
34804 case LE_EXPR:
34805 break;
34806 case NE_EXPR:
34807 /* Fall through: OpenMP disallows NE_EXPR. */
34808 gcc_fallthrough ();
34809 default:
34810 return error_mark_node;
34811 }
34812
34813 /* If decl is an iterator, preserve LHS and RHS of the relational
34814 expr until finish_omp_for. */
34815 if (decl
34816 && (type_dependent_expression_p (decl)
34817 || CLASS_TYPE_P (TREE_TYPE (decl))))
34818 return cond;
34819
34820 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34821 TREE_CODE (cond),
34822 TREE_OPERAND (cond, 0), ERROR_MARK,
34823 TREE_OPERAND (cond, 1), ERROR_MARK,
34824 /*overload=*/NULL, tf_warning_or_error);
34825 }
34826
34827 /* Helper function, to parse omp for increment expression. */
34828
34829 static tree
34830 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34831 {
34832 cp_token *token = cp_lexer_peek_token (parser->lexer);
34833 enum tree_code op;
34834 tree lhs, rhs;
34835 cp_id_kind idk;
34836 bool decl_first;
34837
34838 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34839 {
34840 op = (token->type == CPP_PLUS_PLUS
34841 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34842 cp_lexer_consume_token (parser->lexer);
34843 lhs = cp_parser_simple_cast_expression (parser);
34844 if (lhs != decl
34845 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34846 return error_mark_node;
34847 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34848 }
34849
34850 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34851 if (lhs != decl
34852 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34853 return error_mark_node;
34854
34855 token = cp_lexer_peek_token (parser->lexer);
34856 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34857 {
34858 op = (token->type == CPP_PLUS_PLUS
34859 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34860 cp_lexer_consume_token (parser->lexer);
34861 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34862 }
34863
34864 op = cp_parser_assignment_operator_opt (parser);
34865 if (op == ERROR_MARK)
34866 return error_mark_node;
34867
34868 if (op != NOP_EXPR)
34869 {
34870 rhs = cp_parser_assignment_expression (parser);
34871 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34872 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34873 }
34874
34875 lhs = cp_parser_binary_expression (parser, false, false,
34876 PREC_ADDITIVE_EXPRESSION, NULL);
34877 token = cp_lexer_peek_token (parser->lexer);
34878 decl_first = (lhs == decl
34879 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34880 if (decl_first)
34881 lhs = NULL_TREE;
34882 if (token->type != CPP_PLUS
34883 && token->type != CPP_MINUS)
34884 return error_mark_node;
34885
34886 do
34887 {
34888 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34889 cp_lexer_consume_token (parser->lexer);
34890 rhs = cp_parser_binary_expression (parser, false, false,
34891 PREC_ADDITIVE_EXPRESSION, NULL);
34892 token = cp_lexer_peek_token (parser->lexer);
34893 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34894 {
34895 if (lhs == NULL_TREE)
34896 {
34897 if (op == PLUS_EXPR)
34898 lhs = rhs;
34899 else
34900 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34901 tf_warning_or_error);
34902 }
34903 else
34904 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34905 ERROR_MARK, NULL, tf_warning_or_error);
34906 }
34907 }
34908 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34909
34910 if (!decl_first)
34911 {
34912 if ((rhs != decl
34913 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34914 || op == MINUS_EXPR)
34915 return error_mark_node;
34916 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34917 }
34918 else
34919 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34920
34921 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34922 }
34923
34924 /* Parse the initialization statement of an OpenMP for loop.
34925
34926 Return true if the resulting construct should have an
34927 OMP_CLAUSE_PRIVATE added to it. */
34928
34929 static tree
34930 cp_parser_omp_for_loop_init (cp_parser *parser,
34931 tree &this_pre_body,
34932 vec<tree, va_gc> *for_block,
34933 tree &init,
34934 tree &orig_init,
34935 tree &decl,
34936 tree &real_decl)
34937 {
34938 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34939 return NULL_TREE;
34940
34941 tree add_private_clause = NULL_TREE;
34942
34943 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34944
34945 init-expr:
34946 var = lb
34947 integer-type var = lb
34948 random-access-iterator-type var = lb
34949 pointer-type var = lb
34950 */
34951 cp_decl_specifier_seq type_specifiers;
34952
34953 /* First, try to parse as an initialized declaration. See
34954 cp_parser_condition, from whence the bulk of this is copied. */
34955
34956 cp_parser_parse_tentatively (parser);
34957 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34958 /*is_trailing_return=*/false,
34959 &type_specifiers);
34960 if (cp_parser_parse_definitely (parser))
34961 {
34962 /* If parsing a type specifier seq succeeded, then this
34963 MUST be a initialized declaration. */
34964 tree asm_specification, attributes;
34965 cp_declarator *declarator;
34966
34967 declarator = cp_parser_declarator (parser,
34968 CP_PARSER_DECLARATOR_NAMED,
34969 /*ctor_dtor_or_conv_p=*/NULL,
34970 /*parenthesized_p=*/NULL,
34971 /*member_p=*/false,
34972 /*friend_p=*/false);
34973 attributes = cp_parser_attributes_opt (parser);
34974 asm_specification = cp_parser_asm_specification_opt (parser);
34975
34976 if (declarator == cp_error_declarator)
34977 cp_parser_skip_to_end_of_statement (parser);
34978
34979 else
34980 {
34981 tree pushed_scope, auto_node;
34982
34983 decl = start_decl (declarator, &type_specifiers,
34984 SD_INITIALIZED, attributes,
34985 /*prefix_attributes=*/NULL_TREE,
34986 &pushed_scope);
34987
34988 auto_node = type_uses_auto (TREE_TYPE (decl));
34989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34990 {
34991 if (cp_lexer_next_token_is (parser->lexer,
34992 CPP_OPEN_PAREN))
34993 error ("parenthesized initialization is not allowed in "
34994 "OpenMP %<for%> loop");
34995 else
34996 /* Trigger an error. */
34997 cp_parser_require (parser, CPP_EQ, RT_EQ);
34998
34999 init = error_mark_node;
35000 cp_parser_skip_to_end_of_statement (parser);
35001 }
35002 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35003 || type_dependent_expression_p (decl)
35004 || auto_node)
35005 {
35006 bool is_direct_init, is_non_constant_init;
35007
35008 init = cp_parser_initializer (parser,
35009 &is_direct_init,
35010 &is_non_constant_init);
35011
35012 if (auto_node)
35013 {
35014 TREE_TYPE (decl)
35015 = do_auto_deduction (TREE_TYPE (decl), init,
35016 auto_node);
35017
35018 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35019 && !type_dependent_expression_p (decl))
35020 goto non_class;
35021 }
35022
35023 cp_finish_decl (decl, init, !is_non_constant_init,
35024 asm_specification,
35025 LOOKUP_ONLYCONVERTING);
35026 orig_init = init;
35027 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35028 {
35029 vec_safe_push (for_block, this_pre_body);
35030 init = NULL_TREE;
35031 }
35032 else
35033 {
35034 init = pop_stmt_list (this_pre_body);
35035 if (init && TREE_CODE (init) == STATEMENT_LIST)
35036 {
35037 tree_stmt_iterator i = tsi_start (init);
35038 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35039 while (!tsi_end_p (i))
35040 {
35041 tree t = tsi_stmt (i);
35042 if (TREE_CODE (t) == DECL_EXPR
35043 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35044 {
35045 tsi_delink (&i);
35046 vec_safe_push (for_block, t);
35047 continue;
35048 }
35049 break;
35050 }
35051 if (tsi_one_before_end_p (i))
35052 {
35053 tree t = tsi_stmt (i);
35054 tsi_delink (&i);
35055 free_stmt_list (init);
35056 init = t;
35057 }
35058 }
35059 }
35060 this_pre_body = NULL_TREE;
35061 }
35062 else
35063 {
35064 /* Consume '='. */
35065 cp_lexer_consume_token (parser->lexer);
35066 init = cp_parser_assignment_expression (parser);
35067
35068 non_class:
35069 if (TYPE_REF_P (TREE_TYPE (decl)))
35070 init = error_mark_node;
35071 else
35072 cp_finish_decl (decl, NULL_TREE,
35073 /*init_const_expr_p=*/false,
35074 asm_specification,
35075 LOOKUP_ONLYCONVERTING);
35076 }
35077
35078 if (pushed_scope)
35079 pop_scope (pushed_scope);
35080 }
35081 }
35082 else
35083 {
35084 cp_id_kind idk;
35085 /* If parsing a type specifier sequence failed, then
35086 this MUST be a simple expression. */
35087 cp_parser_parse_tentatively (parser);
35088 decl = cp_parser_primary_expression (parser, false, false,
35089 false, &idk);
35090 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35091 if (!cp_parser_error_occurred (parser)
35092 && decl
35093 && (TREE_CODE (decl) == COMPONENT_REF
35094 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35095 {
35096 cp_parser_abort_tentative_parse (parser);
35097 cp_parser_parse_tentatively (parser);
35098 cp_token *token = cp_lexer_peek_token (parser->lexer);
35099 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35100 /*check_dependency_p=*/true,
35101 /*template_p=*/NULL,
35102 /*declarator_p=*/false,
35103 /*optional_p=*/false);
35104 if (name != error_mark_node
35105 && last_tok == cp_lexer_peek_token (parser->lexer))
35106 {
35107 decl = cp_parser_lookup_name_simple (parser, name,
35108 token->location);
35109 if (TREE_CODE (decl) == FIELD_DECL)
35110 add_private_clause = omp_privatize_field (decl, false);
35111 }
35112 cp_parser_abort_tentative_parse (parser);
35113 cp_parser_parse_tentatively (parser);
35114 decl = cp_parser_primary_expression (parser, false, false,
35115 false, &idk);
35116 }
35117 if (!cp_parser_error_occurred (parser)
35118 && decl
35119 && DECL_P (decl)
35120 && CLASS_TYPE_P (TREE_TYPE (decl)))
35121 {
35122 tree rhs;
35123
35124 cp_parser_parse_definitely (parser);
35125 cp_parser_require (parser, CPP_EQ, RT_EQ);
35126 rhs = cp_parser_assignment_expression (parser);
35127 orig_init = rhs;
35128 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35129 decl, NOP_EXPR,
35130 rhs,
35131 tf_warning_or_error));
35132 if (!add_private_clause)
35133 add_private_clause = decl;
35134 }
35135 else
35136 {
35137 decl = NULL;
35138 cp_parser_abort_tentative_parse (parser);
35139 init = cp_parser_expression (parser);
35140 if (init)
35141 {
35142 if (TREE_CODE (init) == MODIFY_EXPR
35143 || TREE_CODE (init) == MODOP_EXPR)
35144 real_decl = TREE_OPERAND (init, 0);
35145 }
35146 }
35147 }
35148 return add_private_clause;
35149 }
35150
35151 /* Parse the restricted form of the for statement allowed by OpenMP. */
35152
35153 static tree
35154 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35155 tree *cclauses, bool *if_p)
35156 {
35157 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35158 tree real_decl, initv, condv, incrv, declv;
35159 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35160 location_t loc_first;
35161 bool collapse_err = false;
35162 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35163 vec<tree, va_gc> *for_block = make_tree_vector ();
35164 auto_vec<tree, 4> orig_inits;
35165 bool tiling = false;
35166
35167 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35168 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35169 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35170 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35171 {
35172 tiling = true;
35173 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35174 }
35175 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35176 && OMP_CLAUSE_ORDERED_EXPR (cl))
35177 {
35178 ordered_cl = cl;
35179 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35180 }
35181
35182 if (ordered && ordered < collapse)
35183 {
35184 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35185 "%<ordered%> clause parameter is less than %<collapse%>");
35186 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35187 = build_int_cst (NULL_TREE, collapse);
35188 ordered = collapse;
35189 }
35190 if (ordered)
35191 {
35192 for (tree *pc = &clauses; *pc; )
35193 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35194 {
35195 error_at (OMP_CLAUSE_LOCATION (*pc),
35196 "%<linear%> clause may not be specified together "
35197 "with %<ordered%> clause with a parameter");
35198 *pc = OMP_CLAUSE_CHAIN (*pc);
35199 }
35200 else
35201 pc = &OMP_CLAUSE_CHAIN (*pc);
35202 }
35203
35204 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35205 count = ordered ? ordered : collapse;
35206
35207 declv = make_tree_vec (count);
35208 initv = make_tree_vec (count);
35209 condv = make_tree_vec (count);
35210 incrv = make_tree_vec (count);
35211
35212 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35213
35214 for (i = 0; i < count; i++)
35215 {
35216 int bracecount = 0;
35217 tree add_private_clause = NULL_TREE;
35218 location_t loc;
35219
35220 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35221 {
35222 if (!collapse_err)
35223 cp_parser_error (parser, "for statement expected");
35224 return NULL;
35225 }
35226 loc = cp_lexer_consume_token (parser->lexer)->location;
35227
35228 matching_parens parens;
35229 if (!parens.require_open (parser))
35230 return NULL;
35231
35232 init = orig_init = decl = real_decl = NULL;
35233 this_pre_body = push_stmt_list ();
35234
35235 add_private_clause
35236 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35237 init, orig_init, decl, real_decl);
35238
35239 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35240 if (this_pre_body)
35241 {
35242 this_pre_body = pop_stmt_list (this_pre_body);
35243 if (pre_body)
35244 {
35245 tree t = pre_body;
35246 pre_body = push_stmt_list ();
35247 add_stmt (t);
35248 add_stmt (this_pre_body);
35249 pre_body = pop_stmt_list (pre_body);
35250 }
35251 else
35252 pre_body = this_pre_body;
35253 }
35254
35255 if (decl)
35256 real_decl = decl;
35257 if (cclauses != NULL
35258 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35259 && real_decl != NULL_TREE)
35260 {
35261 tree *c;
35262 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35263 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35264 && OMP_CLAUSE_DECL (*c) == real_decl)
35265 {
35266 error_at (loc, "iteration variable %qD"
35267 " should not be firstprivate", real_decl);
35268 *c = OMP_CLAUSE_CHAIN (*c);
35269 }
35270 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35271 && OMP_CLAUSE_DECL (*c) == real_decl)
35272 {
35273 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35274 tree l = *c;
35275 *c = OMP_CLAUSE_CHAIN (*c);
35276 if (code == OMP_SIMD)
35277 {
35278 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35279 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35280 }
35281 else
35282 {
35283 OMP_CLAUSE_CHAIN (l) = clauses;
35284 clauses = l;
35285 }
35286 add_private_clause = NULL_TREE;
35287 }
35288 else
35289 {
35290 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35291 && OMP_CLAUSE_DECL (*c) == real_decl)
35292 add_private_clause = NULL_TREE;
35293 c = &OMP_CLAUSE_CHAIN (*c);
35294 }
35295 }
35296
35297 if (add_private_clause)
35298 {
35299 tree c;
35300 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35301 {
35302 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35303 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35304 && OMP_CLAUSE_DECL (c) == decl)
35305 break;
35306 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35307 && OMP_CLAUSE_DECL (c) == decl)
35308 error_at (loc, "iteration variable %qD "
35309 "should not be firstprivate",
35310 decl);
35311 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35312 && OMP_CLAUSE_DECL (c) == decl)
35313 error_at (loc, "iteration variable %qD should not be reduction",
35314 decl);
35315 }
35316 if (c == NULL)
35317 {
35318 if (code != OMP_SIMD)
35319 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35320 else if (collapse == 1)
35321 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35322 else
35323 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35324 OMP_CLAUSE_DECL (c) = add_private_clause;
35325 c = finish_omp_clauses (c, C_ORT_OMP);
35326 if (c)
35327 {
35328 OMP_CLAUSE_CHAIN (c) = clauses;
35329 clauses = c;
35330 /* For linear, signal that we need to fill up
35331 the so far unknown linear step. */
35332 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35333 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35334 }
35335 }
35336 }
35337
35338 cond = NULL;
35339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35340 cond = cp_parser_omp_for_cond (parser, decl);
35341 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35342
35343 incr = NULL;
35344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35345 {
35346 /* If decl is an iterator, preserve the operator on decl
35347 until finish_omp_for. */
35348 if (real_decl
35349 && ((processing_template_decl
35350 && (TREE_TYPE (real_decl) == NULL_TREE
35351 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35352 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35353 incr = cp_parser_omp_for_incr (parser, real_decl);
35354 else
35355 incr = cp_parser_expression (parser);
35356 if (!EXPR_HAS_LOCATION (incr))
35357 protected_set_expr_location (incr, input_location);
35358 }
35359
35360 if (!parens.require_close (parser))
35361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35362 /*or_comma=*/false,
35363 /*consume_paren=*/true);
35364
35365 TREE_VEC_ELT (declv, i) = decl;
35366 TREE_VEC_ELT (initv, i) = init;
35367 TREE_VEC_ELT (condv, i) = cond;
35368 TREE_VEC_ELT (incrv, i) = incr;
35369 if (orig_init)
35370 {
35371 orig_inits.safe_grow_cleared (i + 1);
35372 orig_inits[i] = orig_init;
35373 }
35374
35375 if (i == count - 1)
35376 break;
35377
35378 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35379 in between the collapsed for loops to be still considered perfectly
35380 nested. Hopefully the final version clarifies this.
35381 For now handle (multiple) {'s and empty statements. */
35382 cp_parser_parse_tentatively (parser);
35383 for (;;)
35384 {
35385 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35386 break;
35387 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35388 {
35389 cp_lexer_consume_token (parser->lexer);
35390 bracecount++;
35391 }
35392 else if (bracecount
35393 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35394 cp_lexer_consume_token (parser->lexer);
35395 else
35396 {
35397 loc = cp_lexer_peek_token (parser->lexer)->location;
35398 error_at (loc, "not enough for loops to collapse");
35399 collapse_err = true;
35400 cp_parser_abort_tentative_parse (parser);
35401 declv = NULL_TREE;
35402 break;
35403 }
35404 }
35405
35406 if (declv)
35407 {
35408 cp_parser_parse_definitely (parser);
35409 nbraces += bracecount;
35410 }
35411 }
35412
35413 if (nbraces)
35414 if_p = NULL;
35415
35416 /* Note that we saved the original contents of this flag when we entered
35417 the structured block, and so we don't need to re-save it here. */
35418 parser->in_statement = IN_OMP_FOR;
35419
35420 /* Note that the grammar doesn't call for a structured block here,
35421 though the loop as a whole is a structured block. */
35422 body = push_stmt_list ();
35423 cp_parser_statement (parser, NULL_TREE, false, if_p);
35424 body = pop_stmt_list (body);
35425
35426 if (declv == NULL_TREE)
35427 ret = NULL_TREE;
35428 else
35429 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35430 body, pre_body, &orig_inits, clauses);
35431
35432 while (nbraces)
35433 {
35434 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35435 {
35436 cp_lexer_consume_token (parser->lexer);
35437 nbraces--;
35438 }
35439 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35440 cp_lexer_consume_token (parser->lexer);
35441 else
35442 {
35443 if (!collapse_err)
35444 {
35445 error_at (cp_lexer_peek_token (parser->lexer)->location,
35446 "collapsed loops not perfectly nested");
35447 }
35448 collapse_err = true;
35449 cp_parser_statement_seq_opt (parser, NULL);
35450 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35451 break;
35452 }
35453 }
35454
35455 while (!for_block->is_empty ())
35456 {
35457 tree t = for_block->pop ();
35458 if (TREE_CODE (t) == STATEMENT_LIST)
35459 add_stmt (pop_stmt_list (t));
35460 else
35461 add_stmt (t);
35462 }
35463 release_tree_vector (for_block);
35464
35465 return ret;
35466 }
35467
35468 /* Helper function for OpenMP parsing, split clauses and call
35469 finish_omp_clauses on each of the set of clauses afterwards. */
35470
35471 static void
35472 cp_omp_split_clauses (location_t loc, enum tree_code code,
35473 omp_clause_mask mask, tree clauses, tree *cclauses)
35474 {
35475 int i;
35476 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35477 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35478 if (cclauses[i])
35479 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35480 }
35481
35482 /* OpenMP 4.0:
35483 #pragma omp simd simd-clause[optseq] new-line
35484 for-loop */
35485
35486 #define OMP_SIMD_CLAUSE_MASK \
35487 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35495
35496 static tree
35497 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35498 char *p_name, omp_clause_mask mask, tree *cclauses,
35499 bool *if_p)
35500 {
35501 tree clauses, sb, ret;
35502 unsigned int save;
35503 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35504
35505 strcat (p_name, " simd");
35506 mask |= OMP_SIMD_CLAUSE_MASK;
35507
35508 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35509 cclauses == NULL);
35510 if (cclauses)
35511 {
35512 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35513 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35514 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35515 OMP_CLAUSE_ORDERED);
35516 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35517 {
35518 error_at (OMP_CLAUSE_LOCATION (c),
35519 "%<ordered%> clause with parameter may not be specified "
35520 "on %qs construct", p_name);
35521 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35522 }
35523 }
35524
35525 sb = begin_omp_structured_block ();
35526 save = cp_parser_begin_omp_structured_block (parser);
35527
35528 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35529
35530 cp_parser_end_omp_structured_block (parser, save);
35531 add_stmt (finish_omp_structured_block (sb));
35532
35533 return ret;
35534 }
35535
35536 /* OpenMP 2.5:
35537 #pragma omp for for-clause[optseq] new-line
35538 for-loop
35539
35540 OpenMP 4.0:
35541 #pragma omp for simd for-simd-clause[optseq] new-line
35542 for-loop */
35543
35544 #define OMP_FOR_CLAUSE_MASK \
35545 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35554
35555 static tree
35556 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35557 char *p_name, omp_clause_mask mask, tree *cclauses,
35558 bool *if_p)
35559 {
35560 tree clauses, sb, ret;
35561 unsigned int save;
35562 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35563
35564 strcat (p_name, " for");
35565 mask |= OMP_FOR_CLAUSE_MASK;
35566 /* parallel for{, simd} disallows nowait clause, but for
35567 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35568 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35569 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35570 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35571 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35572 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35573
35574 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35575 {
35576 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35577 const char *p = IDENTIFIER_POINTER (id);
35578
35579 if (strcmp (p, "simd") == 0)
35580 {
35581 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35582 if (cclauses == NULL)
35583 cclauses = cclauses_buf;
35584
35585 cp_lexer_consume_token (parser->lexer);
35586 if (!flag_openmp) /* flag_openmp_simd */
35587 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35588 cclauses, if_p);
35589 sb = begin_omp_structured_block ();
35590 save = cp_parser_begin_omp_structured_block (parser);
35591 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35592 cclauses, if_p);
35593 cp_parser_end_omp_structured_block (parser, save);
35594 tree body = finish_omp_structured_block (sb);
35595 if (ret == NULL)
35596 return ret;
35597 ret = make_node (OMP_FOR);
35598 TREE_TYPE (ret) = void_type_node;
35599 OMP_FOR_BODY (ret) = body;
35600 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35601 SET_EXPR_LOCATION (ret, loc);
35602 add_stmt (ret);
35603 return ret;
35604 }
35605 }
35606 if (!flag_openmp) /* flag_openmp_simd */
35607 {
35608 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35609 return NULL_TREE;
35610 }
35611
35612 /* Composite distribute parallel for disallows linear clause. */
35613 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35614 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35615
35616 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35617 cclauses == NULL);
35618 if (cclauses)
35619 {
35620 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35621 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35622 }
35623
35624 sb = begin_omp_structured_block ();
35625 save = cp_parser_begin_omp_structured_block (parser);
35626
35627 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35628
35629 cp_parser_end_omp_structured_block (parser, save);
35630 add_stmt (finish_omp_structured_block (sb));
35631
35632 return ret;
35633 }
35634
35635 /* OpenMP 2.5:
35636 # pragma omp master new-line
35637 structured-block */
35638
35639 static tree
35640 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35641 {
35642 cp_parser_require_pragma_eol (parser, pragma_tok);
35643 return c_finish_omp_master (input_location,
35644 cp_parser_omp_structured_block (parser, if_p));
35645 }
35646
35647 /* OpenMP 2.5:
35648 # pragma omp ordered new-line
35649 structured-block
35650
35651 OpenMP 4.5:
35652 # pragma omp ordered ordered-clauses new-line
35653 structured-block */
35654
35655 #define OMP_ORDERED_CLAUSE_MASK \
35656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35658
35659 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35660 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35661
35662 static bool
35663 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35664 enum pragma_context context, bool *if_p)
35665 {
35666 location_t loc = pragma_tok->location;
35667
35668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35669 {
35670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35671 const char *p = IDENTIFIER_POINTER (id);
35672
35673 if (strcmp (p, "depend") == 0)
35674 {
35675 if (!flag_openmp) /* flag_openmp_simd */
35676 {
35677 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35678 return false;
35679 }
35680 if (context == pragma_stmt)
35681 {
35682 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35683 "%<depend%> clause may only be used in compound "
35684 "statements");
35685 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35686 return false;
35687 }
35688 tree clauses
35689 = cp_parser_omp_all_clauses (parser,
35690 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35691 "#pragma omp ordered", pragma_tok);
35692 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35693 return false;
35694 }
35695 }
35696
35697 tree clauses
35698 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35699 "#pragma omp ordered", pragma_tok);
35700
35701 if (!flag_openmp /* flag_openmp_simd */
35702 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35703 return false;
35704
35705 c_finish_omp_ordered (loc, clauses,
35706 cp_parser_omp_structured_block (parser, if_p));
35707 return true;
35708 }
35709
35710 /* OpenMP 2.5:
35711
35712 section-scope:
35713 { section-sequence }
35714
35715 section-sequence:
35716 section-directive[opt] structured-block
35717 section-sequence section-directive structured-block */
35718
35719 static tree
35720 cp_parser_omp_sections_scope (cp_parser *parser)
35721 {
35722 tree stmt, substmt;
35723 bool error_suppress = false;
35724 cp_token *tok;
35725
35726 matching_braces braces;
35727 if (!braces.require_open (parser))
35728 return NULL_TREE;
35729
35730 stmt = push_stmt_list ();
35731
35732 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35733 != PRAGMA_OMP_SECTION)
35734 {
35735 substmt = cp_parser_omp_structured_block (parser, NULL);
35736 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35737 add_stmt (substmt);
35738 }
35739
35740 while (1)
35741 {
35742 tok = cp_lexer_peek_token (parser->lexer);
35743 if (tok->type == CPP_CLOSE_BRACE)
35744 break;
35745 if (tok->type == CPP_EOF)
35746 break;
35747
35748 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35749 {
35750 cp_lexer_consume_token (parser->lexer);
35751 cp_parser_require_pragma_eol (parser, tok);
35752 error_suppress = false;
35753 }
35754 else if (!error_suppress)
35755 {
35756 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35757 error_suppress = true;
35758 }
35759
35760 substmt = cp_parser_omp_structured_block (parser, NULL);
35761 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35762 add_stmt (substmt);
35763 }
35764 braces.require_close (parser);
35765
35766 substmt = pop_stmt_list (stmt);
35767
35768 stmt = make_node (OMP_SECTIONS);
35769 TREE_TYPE (stmt) = void_type_node;
35770 OMP_SECTIONS_BODY (stmt) = substmt;
35771
35772 add_stmt (stmt);
35773 return stmt;
35774 }
35775
35776 /* OpenMP 2.5:
35777 # pragma omp sections sections-clause[optseq] newline
35778 sections-scope */
35779
35780 #define OMP_SECTIONS_CLAUSE_MASK \
35781 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35786
35787 static tree
35788 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35789 char *p_name, omp_clause_mask mask, tree *cclauses)
35790 {
35791 tree clauses, ret;
35792 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35793
35794 strcat (p_name, " sections");
35795 mask |= OMP_SECTIONS_CLAUSE_MASK;
35796 if (cclauses)
35797 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35798
35799 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35800 cclauses == NULL);
35801 if (cclauses)
35802 {
35803 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35804 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35805 }
35806
35807 ret = cp_parser_omp_sections_scope (parser);
35808 if (ret)
35809 OMP_SECTIONS_CLAUSES (ret) = clauses;
35810
35811 return ret;
35812 }
35813
35814 /* OpenMP 2.5:
35815 # pragma omp parallel parallel-clause[optseq] new-line
35816 structured-block
35817 # pragma omp parallel for parallel-for-clause[optseq] new-line
35818 structured-block
35819 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35820 structured-block
35821
35822 OpenMP 4.0:
35823 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35824 structured-block */
35825
35826 #define OMP_PARALLEL_CLAUSE_MASK \
35827 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35836
35837 static tree
35838 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35839 char *p_name, omp_clause_mask mask, tree *cclauses,
35840 bool *if_p)
35841 {
35842 tree stmt, clauses, block;
35843 unsigned int save;
35844 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35845
35846 strcat (p_name, " parallel");
35847 mask |= OMP_PARALLEL_CLAUSE_MASK;
35848 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35849 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35850 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35851 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35852
35853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35854 {
35855 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35856 if (cclauses == NULL)
35857 cclauses = cclauses_buf;
35858
35859 cp_lexer_consume_token (parser->lexer);
35860 if (!flag_openmp) /* flag_openmp_simd */
35861 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35862 if_p);
35863 block = begin_omp_parallel ();
35864 save = cp_parser_begin_omp_structured_block (parser);
35865 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35866 if_p);
35867 cp_parser_end_omp_structured_block (parser, save);
35868 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35869 block);
35870 if (ret == NULL_TREE)
35871 return ret;
35872 OMP_PARALLEL_COMBINED (stmt) = 1;
35873 return stmt;
35874 }
35875 /* When combined with distribute, parallel has to be followed by for.
35876 #pragma omp target parallel is allowed though. */
35877 else if (cclauses
35878 && (mask & (OMP_CLAUSE_MASK_1
35879 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35880 {
35881 error_at (loc, "expected %<for%> after %qs", p_name);
35882 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35883 return NULL_TREE;
35884 }
35885 else if (!flag_openmp) /* flag_openmp_simd */
35886 {
35887 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35888 return NULL_TREE;
35889 }
35890 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35891 {
35892 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35893 const char *p = IDENTIFIER_POINTER (id);
35894 if (strcmp (p, "sections") == 0)
35895 {
35896 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35897 cclauses = cclauses_buf;
35898
35899 cp_lexer_consume_token (parser->lexer);
35900 block = begin_omp_parallel ();
35901 save = cp_parser_begin_omp_structured_block (parser);
35902 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35903 cp_parser_end_omp_structured_block (parser, save);
35904 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35905 block);
35906 OMP_PARALLEL_COMBINED (stmt) = 1;
35907 return stmt;
35908 }
35909 }
35910
35911 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35912 cclauses == NULL);
35913 if (cclauses)
35914 {
35915 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35916 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35917 }
35918
35919 block = begin_omp_parallel ();
35920 save = cp_parser_begin_omp_structured_block (parser);
35921 cp_parser_statement (parser, NULL_TREE, false, if_p);
35922 cp_parser_end_omp_structured_block (parser, save);
35923 stmt = finish_omp_parallel (clauses, block);
35924 return stmt;
35925 }
35926
35927 /* OpenMP 2.5:
35928 # pragma omp single single-clause[optseq] new-line
35929 structured-block */
35930
35931 #define OMP_SINGLE_CLAUSE_MASK \
35932 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35936
35937 static tree
35938 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35939 {
35940 tree stmt = make_node (OMP_SINGLE);
35941 TREE_TYPE (stmt) = void_type_node;
35942
35943 OMP_SINGLE_CLAUSES (stmt)
35944 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35945 "#pragma omp single", pragma_tok);
35946 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35947
35948 return add_stmt (stmt);
35949 }
35950
35951 /* OpenMP 3.0:
35952 # pragma omp task task-clause[optseq] new-line
35953 structured-block */
35954
35955 #define OMP_TASK_CLAUSE_MASK \
35956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35966
35967 static tree
35968 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35969 {
35970 tree clauses, block;
35971 unsigned int save;
35972
35973 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35974 "#pragma omp task", pragma_tok);
35975 block = begin_omp_task ();
35976 save = cp_parser_begin_omp_structured_block (parser);
35977 cp_parser_statement (parser, NULL_TREE, false, if_p);
35978 cp_parser_end_omp_structured_block (parser, save);
35979 return finish_omp_task (clauses, block);
35980 }
35981
35982 /* OpenMP 3.0:
35983 # pragma omp taskwait new-line */
35984
35985 static void
35986 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35987 {
35988 cp_parser_require_pragma_eol (parser, pragma_tok);
35989 finish_omp_taskwait ();
35990 }
35991
35992 /* OpenMP 3.1:
35993 # pragma omp taskyield new-line */
35994
35995 static void
35996 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35997 {
35998 cp_parser_require_pragma_eol (parser, pragma_tok);
35999 finish_omp_taskyield ();
36000 }
36001
36002 /* OpenMP 4.0:
36003 # pragma omp taskgroup new-line
36004 structured-block */
36005
36006 static tree
36007 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36008 {
36009 cp_parser_require_pragma_eol (parser, pragma_tok);
36010 return c_finish_omp_taskgroup (input_location,
36011 cp_parser_omp_structured_block (parser,
36012 if_p));
36013 }
36014
36015
36016 /* OpenMP 2.5:
36017 # pragma omp threadprivate (variable-list) */
36018
36019 static void
36020 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36021 {
36022 tree vars;
36023
36024 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36025 cp_parser_require_pragma_eol (parser, pragma_tok);
36026
36027 finish_omp_threadprivate (vars);
36028 }
36029
36030 /* OpenMP 4.0:
36031 # pragma omp cancel cancel-clause[optseq] new-line */
36032
36033 #define OMP_CANCEL_CLAUSE_MASK \
36034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36039
36040 static void
36041 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36042 {
36043 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36044 "#pragma omp cancel", pragma_tok);
36045 finish_omp_cancel (clauses);
36046 }
36047
36048 /* OpenMP 4.0:
36049 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36050
36051 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36052 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36056
36057 static void
36058 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36059 enum pragma_context context)
36060 {
36061 tree clauses;
36062 bool point_seen = false;
36063
36064 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36065 {
36066 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36067 const char *p = IDENTIFIER_POINTER (id);
36068
36069 if (strcmp (p, "point") == 0)
36070 {
36071 cp_lexer_consume_token (parser->lexer);
36072 point_seen = true;
36073 }
36074 }
36075 if (!point_seen)
36076 {
36077 cp_parser_error (parser, "expected %<point%>");
36078 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36079 return;
36080 }
36081
36082 if (context != pragma_compound)
36083 {
36084 if (context == pragma_stmt)
36085 error_at (pragma_tok->location,
36086 "%<#pragma %s%> may only be used in compound statements",
36087 "omp cancellation point");
36088 else
36089 cp_parser_error (parser, "expected declaration specifiers");
36090 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36091 return;
36092 }
36093
36094 clauses = cp_parser_omp_all_clauses (parser,
36095 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36096 "#pragma omp cancellation point",
36097 pragma_tok);
36098 finish_omp_cancellation_point (clauses);
36099 }
36100
36101 /* OpenMP 4.0:
36102 #pragma omp distribute distribute-clause[optseq] new-line
36103 for-loop */
36104
36105 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36106 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36111
36112 static tree
36113 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36114 char *p_name, omp_clause_mask mask, tree *cclauses,
36115 bool *if_p)
36116 {
36117 tree clauses, sb, ret;
36118 unsigned int save;
36119 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36120
36121 strcat (p_name, " distribute");
36122 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36123
36124 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36125 {
36126 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36127 const char *p = IDENTIFIER_POINTER (id);
36128 bool simd = false;
36129 bool parallel = false;
36130
36131 if (strcmp (p, "simd") == 0)
36132 simd = true;
36133 else
36134 parallel = strcmp (p, "parallel") == 0;
36135 if (parallel || simd)
36136 {
36137 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36138 if (cclauses == NULL)
36139 cclauses = cclauses_buf;
36140 cp_lexer_consume_token (parser->lexer);
36141 if (!flag_openmp) /* flag_openmp_simd */
36142 {
36143 if (simd)
36144 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36145 cclauses, if_p);
36146 else
36147 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36148 cclauses, if_p);
36149 }
36150 sb = begin_omp_structured_block ();
36151 save = cp_parser_begin_omp_structured_block (parser);
36152 if (simd)
36153 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36154 cclauses, if_p);
36155 else
36156 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36157 cclauses, if_p);
36158 cp_parser_end_omp_structured_block (parser, save);
36159 tree body = finish_omp_structured_block (sb);
36160 if (ret == NULL)
36161 return ret;
36162 ret = make_node (OMP_DISTRIBUTE);
36163 TREE_TYPE (ret) = void_type_node;
36164 OMP_FOR_BODY (ret) = body;
36165 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36166 SET_EXPR_LOCATION (ret, loc);
36167 add_stmt (ret);
36168 return ret;
36169 }
36170 }
36171 if (!flag_openmp) /* flag_openmp_simd */
36172 {
36173 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36174 return NULL_TREE;
36175 }
36176
36177 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36178 cclauses == NULL);
36179 if (cclauses)
36180 {
36181 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36182 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36183 }
36184
36185 sb = begin_omp_structured_block ();
36186 save = cp_parser_begin_omp_structured_block (parser);
36187
36188 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36189
36190 cp_parser_end_omp_structured_block (parser, save);
36191 add_stmt (finish_omp_structured_block (sb));
36192
36193 return ret;
36194 }
36195
36196 /* OpenMP 4.0:
36197 # pragma omp teams teams-clause[optseq] new-line
36198 structured-block */
36199
36200 #define OMP_TEAMS_CLAUSE_MASK \
36201 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36208
36209 static tree
36210 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36211 char *p_name, omp_clause_mask mask, tree *cclauses,
36212 bool *if_p)
36213 {
36214 tree clauses, sb, ret;
36215 unsigned int save;
36216 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36217
36218 strcat (p_name, " teams");
36219 mask |= OMP_TEAMS_CLAUSE_MASK;
36220
36221 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36222 {
36223 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36224 const char *p = IDENTIFIER_POINTER (id);
36225 if (strcmp (p, "distribute") == 0)
36226 {
36227 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36228 if (cclauses == NULL)
36229 cclauses = cclauses_buf;
36230
36231 cp_lexer_consume_token (parser->lexer);
36232 if (!flag_openmp) /* flag_openmp_simd */
36233 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36234 cclauses, if_p);
36235 sb = begin_omp_structured_block ();
36236 save = cp_parser_begin_omp_structured_block (parser);
36237 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36238 cclauses, if_p);
36239 cp_parser_end_omp_structured_block (parser, save);
36240 tree body = finish_omp_structured_block (sb);
36241 if (ret == NULL)
36242 return ret;
36243 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36244 ret = make_node (OMP_TEAMS);
36245 TREE_TYPE (ret) = void_type_node;
36246 OMP_TEAMS_CLAUSES (ret) = clauses;
36247 OMP_TEAMS_BODY (ret) = body;
36248 OMP_TEAMS_COMBINED (ret) = 1;
36249 SET_EXPR_LOCATION (ret, loc);
36250 return add_stmt (ret);
36251 }
36252 }
36253 if (!flag_openmp) /* flag_openmp_simd */
36254 {
36255 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36256 return NULL_TREE;
36257 }
36258
36259 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36260 cclauses == NULL);
36261 if (cclauses)
36262 {
36263 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36264 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36265 }
36266
36267 tree stmt = make_node (OMP_TEAMS);
36268 TREE_TYPE (stmt) = void_type_node;
36269 OMP_TEAMS_CLAUSES (stmt) = clauses;
36270 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36271 SET_EXPR_LOCATION (stmt, loc);
36272
36273 return add_stmt (stmt);
36274 }
36275
36276 /* OpenMP 4.0:
36277 # pragma omp target data target-data-clause[optseq] new-line
36278 structured-block */
36279
36280 #define OMP_TARGET_DATA_CLAUSE_MASK \
36281 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36285
36286 static tree
36287 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36288 {
36289 tree clauses
36290 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36291 "#pragma omp target data", pragma_tok);
36292 int map_seen = 0;
36293 for (tree *pc = &clauses; *pc;)
36294 {
36295 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36296 switch (OMP_CLAUSE_MAP_KIND (*pc))
36297 {
36298 case GOMP_MAP_TO:
36299 case GOMP_MAP_ALWAYS_TO:
36300 case GOMP_MAP_FROM:
36301 case GOMP_MAP_ALWAYS_FROM:
36302 case GOMP_MAP_TOFROM:
36303 case GOMP_MAP_ALWAYS_TOFROM:
36304 case GOMP_MAP_ALLOC:
36305 map_seen = 3;
36306 break;
36307 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36308 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36309 case GOMP_MAP_ALWAYS_POINTER:
36310 break;
36311 default:
36312 map_seen |= 1;
36313 error_at (OMP_CLAUSE_LOCATION (*pc),
36314 "%<#pragma omp target data%> with map-type other "
36315 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36316 "on %<map%> clause");
36317 *pc = OMP_CLAUSE_CHAIN (*pc);
36318 continue;
36319 }
36320 pc = &OMP_CLAUSE_CHAIN (*pc);
36321 }
36322
36323 if (map_seen != 3)
36324 {
36325 if (map_seen == 0)
36326 error_at (pragma_tok->location,
36327 "%<#pragma omp target data%> must contain at least "
36328 "one %<map%> clause");
36329 return NULL_TREE;
36330 }
36331
36332 tree stmt = make_node (OMP_TARGET_DATA);
36333 TREE_TYPE (stmt) = void_type_node;
36334 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36335
36336 keep_next_level (true);
36337 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36338
36339 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36340 return add_stmt (stmt);
36341 }
36342
36343 /* OpenMP 4.5:
36344 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36345 structured-block */
36346
36347 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36348 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36353
36354 static tree
36355 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36356 enum pragma_context context)
36357 {
36358 bool data_seen = false;
36359 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36360 {
36361 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36362 const char *p = IDENTIFIER_POINTER (id);
36363
36364 if (strcmp (p, "data") == 0)
36365 {
36366 cp_lexer_consume_token (parser->lexer);
36367 data_seen = true;
36368 }
36369 }
36370 if (!data_seen)
36371 {
36372 cp_parser_error (parser, "expected %<data%>");
36373 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36374 return NULL_TREE;
36375 }
36376
36377 if (context == pragma_stmt)
36378 {
36379 error_at (pragma_tok->location,
36380 "%<#pragma %s%> may only be used in compound statements",
36381 "omp target enter data");
36382 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36383 return NULL_TREE;
36384 }
36385
36386 tree clauses
36387 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36388 "#pragma omp target enter data", pragma_tok);
36389 int map_seen = 0;
36390 for (tree *pc = &clauses; *pc;)
36391 {
36392 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36393 switch (OMP_CLAUSE_MAP_KIND (*pc))
36394 {
36395 case GOMP_MAP_TO:
36396 case GOMP_MAP_ALWAYS_TO:
36397 case GOMP_MAP_ALLOC:
36398 map_seen = 3;
36399 break;
36400 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36401 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36402 case GOMP_MAP_ALWAYS_POINTER:
36403 break;
36404 default:
36405 map_seen |= 1;
36406 error_at (OMP_CLAUSE_LOCATION (*pc),
36407 "%<#pragma omp target enter data%> with map-type other "
36408 "than %<to%> or %<alloc%> on %<map%> clause");
36409 *pc = OMP_CLAUSE_CHAIN (*pc);
36410 continue;
36411 }
36412 pc = &OMP_CLAUSE_CHAIN (*pc);
36413 }
36414
36415 if (map_seen != 3)
36416 {
36417 if (map_seen == 0)
36418 error_at (pragma_tok->location,
36419 "%<#pragma omp target enter data%> must contain at least "
36420 "one %<map%> clause");
36421 return NULL_TREE;
36422 }
36423
36424 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36425 TREE_TYPE (stmt) = void_type_node;
36426 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36427 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36428 return add_stmt (stmt);
36429 }
36430
36431 /* OpenMP 4.5:
36432 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36433 structured-block */
36434
36435 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36436 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36441
36442 static tree
36443 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36444 enum pragma_context context)
36445 {
36446 bool data_seen = false;
36447 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36448 {
36449 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36450 const char *p = IDENTIFIER_POINTER (id);
36451
36452 if (strcmp (p, "data") == 0)
36453 {
36454 cp_lexer_consume_token (parser->lexer);
36455 data_seen = true;
36456 }
36457 }
36458 if (!data_seen)
36459 {
36460 cp_parser_error (parser, "expected %<data%>");
36461 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36462 return NULL_TREE;
36463 }
36464
36465 if (context == pragma_stmt)
36466 {
36467 error_at (pragma_tok->location,
36468 "%<#pragma %s%> may only be used in compound statements",
36469 "omp target exit data");
36470 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36471 return NULL_TREE;
36472 }
36473
36474 tree clauses
36475 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36476 "#pragma omp target exit data", pragma_tok);
36477 int map_seen = 0;
36478 for (tree *pc = &clauses; *pc;)
36479 {
36480 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36481 switch (OMP_CLAUSE_MAP_KIND (*pc))
36482 {
36483 case GOMP_MAP_FROM:
36484 case GOMP_MAP_ALWAYS_FROM:
36485 case GOMP_MAP_RELEASE:
36486 case GOMP_MAP_DELETE:
36487 map_seen = 3;
36488 break;
36489 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36490 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36491 case GOMP_MAP_ALWAYS_POINTER:
36492 break;
36493 default:
36494 map_seen |= 1;
36495 error_at (OMP_CLAUSE_LOCATION (*pc),
36496 "%<#pragma omp target exit data%> with map-type other "
36497 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36498 " clause");
36499 *pc = OMP_CLAUSE_CHAIN (*pc);
36500 continue;
36501 }
36502 pc = &OMP_CLAUSE_CHAIN (*pc);
36503 }
36504
36505 if (map_seen != 3)
36506 {
36507 if (map_seen == 0)
36508 error_at (pragma_tok->location,
36509 "%<#pragma omp target exit data%> must contain at least "
36510 "one %<map%> clause");
36511 return NULL_TREE;
36512 }
36513
36514 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36515 TREE_TYPE (stmt) = void_type_node;
36516 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36517 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36518 return add_stmt (stmt);
36519 }
36520
36521 /* OpenMP 4.0:
36522 # pragma omp target update target-update-clause[optseq] new-line */
36523
36524 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36525 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36531
36532 static bool
36533 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36534 enum pragma_context context)
36535 {
36536 if (context == pragma_stmt)
36537 {
36538 error_at (pragma_tok->location,
36539 "%<#pragma %s%> may only be used in compound statements",
36540 "omp target update");
36541 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36542 return false;
36543 }
36544
36545 tree clauses
36546 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36547 "#pragma omp target update", pragma_tok);
36548 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36549 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36550 {
36551 error_at (pragma_tok->location,
36552 "%<#pragma omp target update%> must contain at least one "
36553 "%<from%> or %<to%> clauses");
36554 return false;
36555 }
36556
36557 tree stmt = make_node (OMP_TARGET_UPDATE);
36558 TREE_TYPE (stmt) = void_type_node;
36559 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36560 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36561 add_stmt (stmt);
36562 return false;
36563 }
36564
36565 /* OpenMP 4.0:
36566 # pragma omp target target-clause[optseq] new-line
36567 structured-block */
36568
36569 #define OMP_TARGET_CLAUSE_MASK \
36570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36579
36580 static bool
36581 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36582 enum pragma_context context, bool *if_p)
36583 {
36584 tree *pc = NULL, stmt;
36585
36586 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36587 {
36588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36589 const char *p = IDENTIFIER_POINTER (id);
36590 enum tree_code ccode = ERROR_MARK;
36591
36592 if (strcmp (p, "teams") == 0)
36593 ccode = OMP_TEAMS;
36594 else if (strcmp (p, "parallel") == 0)
36595 ccode = OMP_PARALLEL;
36596 else if (strcmp (p, "simd") == 0)
36597 ccode = OMP_SIMD;
36598 if (ccode != ERROR_MARK)
36599 {
36600 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36601 char p_name[sizeof ("#pragma omp target teams distribute "
36602 "parallel for simd")];
36603
36604 cp_lexer_consume_token (parser->lexer);
36605 strcpy (p_name, "#pragma omp target");
36606 if (!flag_openmp) /* flag_openmp_simd */
36607 {
36608 tree stmt;
36609 switch (ccode)
36610 {
36611 case OMP_TEAMS:
36612 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36613 OMP_TARGET_CLAUSE_MASK,
36614 cclauses, if_p);
36615 break;
36616 case OMP_PARALLEL:
36617 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36618 OMP_TARGET_CLAUSE_MASK,
36619 cclauses, if_p);
36620 break;
36621 case OMP_SIMD:
36622 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36623 OMP_TARGET_CLAUSE_MASK,
36624 cclauses, if_p);
36625 break;
36626 default:
36627 gcc_unreachable ();
36628 }
36629 return stmt != NULL_TREE;
36630 }
36631 keep_next_level (true);
36632 tree sb = begin_omp_structured_block (), ret;
36633 unsigned save = cp_parser_begin_omp_structured_block (parser);
36634 switch (ccode)
36635 {
36636 case OMP_TEAMS:
36637 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36638 OMP_TARGET_CLAUSE_MASK, cclauses,
36639 if_p);
36640 break;
36641 case OMP_PARALLEL:
36642 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36643 OMP_TARGET_CLAUSE_MASK, cclauses,
36644 if_p);
36645 break;
36646 case OMP_SIMD:
36647 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36648 OMP_TARGET_CLAUSE_MASK, cclauses,
36649 if_p);
36650 break;
36651 default:
36652 gcc_unreachable ();
36653 }
36654 cp_parser_end_omp_structured_block (parser, save);
36655 tree body = finish_omp_structured_block (sb);
36656 if (ret == NULL_TREE)
36657 return false;
36658 if (ccode == OMP_TEAMS && !processing_template_decl)
36659 {
36660 /* For combined target teams, ensure the num_teams and
36661 thread_limit clause expressions are evaluated on the host,
36662 before entering the target construct. */
36663 tree c;
36664 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36665 c; c = OMP_CLAUSE_CHAIN (c))
36666 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36667 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36668 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36669 {
36670 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36671 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36672 if (expr == error_mark_node)
36673 continue;
36674 tree tmp = TARGET_EXPR_SLOT (expr);
36675 add_stmt (expr);
36676 OMP_CLAUSE_OPERAND (c, 0) = expr;
36677 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36678 OMP_CLAUSE_FIRSTPRIVATE);
36679 OMP_CLAUSE_DECL (tc) = tmp;
36680 OMP_CLAUSE_CHAIN (tc)
36681 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36682 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36683 }
36684 }
36685 tree stmt = make_node (OMP_TARGET);
36686 TREE_TYPE (stmt) = void_type_node;
36687 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36688 OMP_TARGET_BODY (stmt) = body;
36689 OMP_TARGET_COMBINED (stmt) = 1;
36690 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36691 add_stmt (stmt);
36692 pc = &OMP_TARGET_CLAUSES (stmt);
36693 goto check_clauses;
36694 }
36695 else if (!flag_openmp) /* flag_openmp_simd */
36696 {
36697 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36698 return false;
36699 }
36700 else if (strcmp (p, "data") == 0)
36701 {
36702 cp_lexer_consume_token (parser->lexer);
36703 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36704 return true;
36705 }
36706 else if (strcmp (p, "enter") == 0)
36707 {
36708 cp_lexer_consume_token (parser->lexer);
36709 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36710 return false;
36711 }
36712 else if (strcmp (p, "exit") == 0)
36713 {
36714 cp_lexer_consume_token (parser->lexer);
36715 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36716 return false;
36717 }
36718 else if (strcmp (p, "update") == 0)
36719 {
36720 cp_lexer_consume_token (parser->lexer);
36721 return cp_parser_omp_target_update (parser, pragma_tok, context);
36722 }
36723 }
36724 if (!flag_openmp) /* flag_openmp_simd */
36725 {
36726 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36727 return false;
36728 }
36729
36730 stmt = make_node (OMP_TARGET);
36731 TREE_TYPE (stmt) = void_type_node;
36732
36733 OMP_TARGET_CLAUSES (stmt)
36734 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36735 "#pragma omp target", pragma_tok);
36736 pc = &OMP_TARGET_CLAUSES (stmt);
36737 keep_next_level (true);
36738 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36739
36740 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36741 add_stmt (stmt);
36742
36743 check_clauses:
36744 while (*pc)
36745 {
36746 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36747 switch (OMP_CLAUSE_MAP_KIND (*pc))
36748 {
36749 case GOMP_MAP_TO:
36750 case GOMP_MAP_ALWAYS_TO:
36751 case GOMP_MAP_FROM:
36752 case GOMP_MAP_ALWAYS_FROM:
36753 case GOMP_MAP_TOFROM:
36754 case GOMP_MAP_ALWAYS_TOFROM:
36755 case GOMP_MAP_ALLOC:
36756 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36757 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36758 case GOMP_MAP_ALWAYS_POINTER:
36759 break;
36760 default:
36761 error_at (OMP_CLAUSE_LOCATION (*pc),
36762 "%<#pragma omp target%> with map-type other "
36763 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36764 "on %<map%> clause");
36765 *pc = OMP_CLAUSE_CHAIN (*pc);
36766 continue;
36767 }
36768 pc = &OMP_CLAUSE_CHAIN (*pc);
36769 }
36770 return true;
36771 }
36772
36773 /* OpenACC 2.0:
36774 # pragma acc cache (variable-list) new-line
36775 */
36776
36777 static tree
36778 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36779 {
36780 tree stmt, clauses;
36781
36782 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36783 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36784
36785 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36786
36787 stmt = make_node (OACC_CACHE);
36788 TREE_TYPE (stmt) = void_type_node;
36789 OACC_CACHE_CLAUSES (stmt) = clauses;
36790 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36791 add_stmt (stmt);
36792
36793 return stmt;
36794 }
36795
36796 /* OpenACC 2.0:
36797 # pragma acc data oacc-data-clause[optseq] new-line
36798 structured-block */
36799
36800 #define OACC_DATA_CLAUSE_MASK \
36801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36812
36813 static tree
36814 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36815 {
36816 tree stmt, clauses, block;
36817 unsigned int save;
36818
36819 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36820 "#pragma acc data", pragma_tok);
36821
36822 block = begin_omp_parallel ();
36823 save = cp_parser_begin_omp_structured_block (parser);
36824 cp_parser_statement (parser, NULL_TREE, false, if_p);
36825 cp_parser_end_omp_structured_block (parser, save);
36826 stmt = finish_oacc_data (clauses, block);
36827 return stmt;
36828 }
36829
36830 /* OpenACC 2.0:
36831 # pragma acc host_data <clauses> new-line
36832 structured-block */
36833
36834 #define OACC_HOST_DATA_CLAUSE_MASK \
36835 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36836
36837 static tree
36838 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36839 {
36840 tree stmt, clauses, block;
36841 unsigned int save;
36842
36843 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36844 "#pragma acc host_data", pragma_tok);
36845
36846 block = begin_omp_parallel ();
36847 save = cp_parser_begin_omp_structured_block (parser);
36848 cp_parser_statement (parser, NULL_TREE, false, if_p);
36849 cp_parser_end_omp_structured_block (parser, save);
36850 stmt = finish_oacc_host_data (clauses, block);
36851 return stmt;
36852 }
36853
36854 /* OpenACC 2.0:
36855 # pragma acc declare oacc-data-clause[optseq] new-line
36856 */
36857
36858 #define OACC_DECLARE_CLAUSE_MASK \
36859 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36871
36872 static tree
36873 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36874 {
36875 tree clauses, stmt;
36876 bool error = false;
36877
36878 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36879 "#pragma acc declare", pragma_tok, true);
36880
36881
36882 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36883 {
36884 error_at (pragma_tok->location,
36885 "no valid clauses specified in %<#pragma acc declare%>");
36886 return NULL_TREE;
36887 }
36888
36889 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36890 {
36891 location_t loc = OMP_CLAUSE_LOCATION (t);
36892 tree decl = OMP_CLAUSE_DECL (t);
36893 if (!DECL_P (decl))
36894 {
36895 error_at (loc, "array section in %<#pragma acc declare%>");
36896 error = true;
36897 continue;
36898 }
36899 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36900 switch (OMP_CLAUSE_MAP_KIND (t))
36901 {
36902 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36903 case GOMP_MAP_FORCE_ALLOC:
36904 case GOMP_MAP_FORCE_TO:
36905 case GOMP_MAP_FORCE_DEVICEPTR:
36906 case GOMP_MAP_DEVICE_RESIDENT:
36907 break;
36908
36909 case GOMP_MAP_LINK:
36910 if (!global_bindings_p ()
36911 && (TREE_STATIC (decl)
36912 || !DECL_EXTERNAL (decl)))
36913 {
36914 error_at (loc,
36915 "%qD must be a global variable in "
36916 "%<#pragma acc declare link%>",
36917 decl);
36918 error = true;
36919 continue;
36920 }
36921 break;
36922
36923 default:
36924 if (global_bindings_p ())
36925 {
36926 error_at (loc, "invalid OpenACC clause at file scope");
36927 error = true;
36928 continue;
36929 }
36930 if (DECL_EXTERNAL (decl))
36931 {
36932 error_at (loc,
36933 "invalid use of %<extern%> variable %qD "
36934 "in %<#pragma acc declare%>", decl);
36935 error = true;
36936 continue;
36937 }
36938 else if (TREE_PUBLIC (decl))
36939 {
36940 error_at (loc,
36941 "invalid use of %<global%> variable %qD "
36942 "in %<#pragma acc declare%>", decl);
36943 error = true;
36944 continue;
36945 }
36946 break;
36947 }
36948
36949 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36950 || lookup_attribute ("omp declare target link",
36951 DECL_ATTRIBUTES (decl)))
36952 {
36953 error_at (loc, "variable %qD used more than once with "
36954 "%<#pragma acc declare%>", decl);
36955 error = true;
36956 continue;
36957 }
36958
36959 if (!error)
36960 {
36961 tree id;
36962
36963 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36964 id = get_identifier ("omp declare target link");
36965 else
36966 id = get_identifier ("omp declare target");
36967
36968 DECL_ATTRIBUTES (decl)
36969 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36970 if (global_bindings_p ())
36971 {
36972 symtab_node *node = symtab_node::get (decl);
36973 if (node != NULL)
36974 {
36975 node->offloadable = 1;
36976 if (ENABLE_OFFLOADING)
36977 {
36978 g->have_offload = true;
36979 if (is_a <varpool_node *> (node))
36980 vec_safe_push (offload_vars, decl);
36981 }
36982 }
36983 }
36984 }
36985 }
36986
36987 if (error || global_bindings_p ())
36988 return NULL_TREE;
36989
36990 stmt = make_node (OACC_DECLARE);
36991 TREE_TYPE (stmt) = void_type_node;
36992 OACC_DECLARE_CLAUSES (stmt) = clauses;
36993 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36994
36995 add_stmt (stmt);
36996
36997 return NULL_TREE;
36998 }
36999
37000 /* OpenACC 2.0:
37001 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37002
37003 or
37004
37005 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37006
37007 LOC is the location of the #pragma token.
37008 */
37009
37010 #define OACC_ENTER_DATA_CLAUSE_MASK \
37011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37018
37019 #define OACC_EXIT_DATA_CLAUSE_MASK \
37020 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37025
37026 static tree
37027 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37028 bool enter)
37029 {
37030 location_t loc = pragma_tok->location;
37031 tree stmt, clauses;
37032 const char *p = "";
37033
37034 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37035 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37036
37037 if (strcmp (p, "data") != 0)
37038 {
37039 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37040 enter ? "enter" : "exit");
37041 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37042 return NULL_TREE;
37043 }
37044
37045 cp_lexer_consume_token (parser->lexer);
37046
37047 if (enter)
37048 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37049 "#pragma acc enter data", pragma_tok);
37050 else
37051 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37052 "#pragma acc exit data", pragma_tok);
37053
37054 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37055 {
37056 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37057 enter ? "enter" : "exit");
37058 return NULL_TREE;
37059 }
37060
37061 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37062 TREE_TYPE (stmt) = void_type_node;
37063 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37064 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37065 add_stmt (stmt);
37066 return stmt;
37067 }
37068
37069 /* OpenACC 2.0:
37070 # pragma acc loop oacc-loop-clause[optseq] new-line
37071 structured-block */
37072
37073 #define OACC_LOOP_CLAUSE_MASK \
37074 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37084
37085 static tree
37086 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37087 omp_clause_mask mask, tree *cclauses, bool *if_p)
37088 {
37089 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37090
37091 strcat (p_name, " loop");
37092 mask |= OACC_LOOP_CLAUSE_MASK;
37093
37094 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37095 cclauses == NULL);
37096 if (cclauses)
37097 {
37098 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37099 if (*cclauses)
37100 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37101 if (clauses)
37102 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37103 }
37104
37105 tree block = begin_omp_structured_block ();
37106 int save = cp_parser_begin_omp_structured_block (parser);
37107 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37108 cp_parser_end_omp_structured_block (parser, save);
37109 add_stmt (finish_omp_structured_block (block));
37110
37111 return stmt;
37112 }
37113
37114 /* OpenACC 2.0:
37115 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37116 structured-block
37117
37118 or
37119
37120 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37121 structured-block
37122 */
37123
37124 #define OACC_KERNELS_CLAUSE_MASK \
37125 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37142
37143 #define OACC_PARALLEL_CLAUSE_MASK \
37144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37164
37165 static tree
37166 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37167 char *p_name, bool *if_p)
37168 {
37169 omp_clause_mask mask;
37170 enum tree_code code;
37171 switch (cp_parser_pragma_kind (pragma_tok))
37172 {
37173 case PRAGMA_OACC_KERNELS:
37174 strcat (p_name, " kernels");
37175 mask = OACC_KERNELS_CLAUSE_MASK;
37176 code = OACC_KERNELS;
37177 break;
37178 case PRAGMA_OACC_PARALLEL:
37179 strcat (p_name, " parallel");
37180 mask = OACC_PARALLEL_CLAUSE_MASK;
37181 code = OACC_PARALLEL;
37182 break;
37183 default:
37184 gcc_unreachable ();
37185 }
37186
37187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37188 {
37189 const char *p
37190 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37191 if (strcmp (p, "loop") == 0)
37192 {
37193 cp_lexer_consume_token (parser->lexer);
37194 tree block = begin_omp_parallel ();
37195 tree clauses;
37196 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37197 if_p);
37198 return finish_omp_construct (code, block, clauses);
37199 }
37200 }
37201
37202 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37203
37204 tree block = begin_omp_parallel ();
37205 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37206 cp_parser_statement (parser, NULL_TREE, false, if_p);
37207 cp_parser_end_omp_structured_block (parser, save);
37208 return finish_omp_construct (code, block, clauses);
37209 }
37210
37211 /* OpenACC 2.0:
37212 # pragma acc update oacc-update-clause[optseq] new-line
37213 */
37214
37215 #define OACC_UPDATE_CLAUSE_MASK \
37216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37222
37223 static tree
37224 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37225 {
37226 tree stmt, clauses;
37227
37228 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37229 "#pragma acc update", pragma_tok);
37230
37231 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37232 {
37233 error_at (pragma_tok->location,
37234 "%<#pragma acc update%> must contain at least one "
37235 "%<device%> or %<host%> or %<self%> clause");
37236 return NULL_TREE;
37237 }
37238
37239 stmt = make_node (OACC_UPDATE);
37240 TREE_TYPE (stmt) = void_type_node;
37241 OACC_UPDATE_CLAUSES (stmt) = clauses;
37242 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37243 add_stmt (stmt);
37244 return stmt;
37245 }
37246
37247 /* OpenACC 2.0:
37248 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37249
37250 LOC is the location of the #pragma token.
37251 */
37252
37253 #define OACC_WAIT_CLAUSE_MASK \
37254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37255
37256 static tree
37257 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37258 {
37259 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37260 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37261
37262 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37263 list = cp_parser_oacc_wait_list (parser, loc, list);
37264
37265 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37266 "#pragma acc wait", pragma_tok);
37267
37268 stmt = c_finish_oacc_wait (loc, list, clauses);
37269 stmt = finish_expr_stmt (stmt);
37270
37271 return stmt;
37272 }
37273
37274 /* OpenMP 4.0:
37275 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37276
37277 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37284
37285 static void
37286 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37287 enum pragma_context context)
37288 {
37289 bool first_p = parser->omp_declare_simd == NULL;
37290 cp_omp_declare_simd_data data;
37291 if (first_p)
37292 {
37293 data.error_seen = false;
37294 data.fndecl_seen = false;
37295 data.tokens = vNULL;
37296 data.clauses = NULL_TREE;
37297 /* It is safe to take the address of a local variable; it will only be
37298 used while this scope is live. */
37299 parser->omp_declare_simd = &data;
37300 }
37301
37302 /* Store away all pragma tokens. */
37303 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37304 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37305 cp_lexer_consume_token (parser->lexer);
37306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37307 parser->omp_declare_simd->error_seen = true;
37308 cp_parser_require_pragma_eol (parser, pragma_tok);
37309 struct cp_token_cache *cp
37310 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37311 parser->omp_declare_simd->tokens.safe_push (cp);
37312
37313 if (first_p)
37314 {
37315 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37316 cp_parser_pragma (parser, context, NULL);
37317 switch (context)
37318 {
37319 case pragma_external:
37320 cp_parser_declaration (parser);
37321 break;
37322 case pragma_member:
37323 cp_parser_member_declaration (parser);
37324 break;
37325 case pragma_objc_icode:
37326 cp_parser_block_declaration (parser, /*statement_p=*/false);
37327 break;
37328 default:
37329 cp_parser_declaration_statement (parser);
37330 break;
37331 }
37332 if (parser->omp_declare_simd
37333 && !parser->omp_declare_simd->error_seen
37334 && !parser->omp_declare_simd->fndecl_seen)
37335 error_at (pragma_tok->location,
37336 "%<#pragma omp declare simd%> not immediately followed by "
37337 "function declaration or definition");
37338 data.tokens.release ();
37339 parser->omp_declare_simd = NULL;
37340 }
37341 }
37342
37343 /* Finalize #pragma omp declare simd clauses after direct declarator has
37344 been parsed, and put that into "omp declare simd" attribute. */
37345
37346 static tree
37347 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37348 {
37349 struct cp_token_cache *ce;
37350 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37351 int i;
37352
37353 if (!data->error_seen && data->fndecl_seen)
37354 {
37355 error ("%<#pragma omp declare simd%> not immediately followed by "
37356 "a single function declaration or definition");
37357 data->error_seen = true;
37358 }
37359 if (data->error_seen)
37360 return attrs;
37361
37362 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37363 {
37364 tree c, cl;
37365
37366 cp_parser_push_lexer_for_tokens (parser, ce);
37367 parser->lexer->in_pragma = true;
37368 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37369 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37370 cp_lexer_consume_token (parser->lexer);
37371 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37372 "#pragma omp declare simd", pragma_tok);
37373 cp_parser_pop_lexer (parser);
37374 if (cl)
37375 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37376 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37377 TREE_CHAIN (c) = attrs;
37378 if (processing_template_decl)
37379 ATTR_IS_DEPENDENT (c) = 1;
37380 attrs = c;
37381 }
37382
37383 data->fndecl_seen = true;
37384 return attrs;
37385 }
37386
37387
37388 /* OpenMP 4.0:
37389 # pragma omp declare target new-line
37390 declarations and definitions
37391 # pragma omp end declare target new-line
37392
37393 OpenMP 4.5:
37394 # pragma omp declare target ( extended-list ) new-line
37395
37396 # pragma omp declare target declare-target-clauses[seq] new-line */
37397
37398 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37401
37402 static void
37403 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37404 {
37405 tree clauses = NULL_TREE;
37406 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37407 clauses
37408 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37409 "#pragma omp declare target", pragma_tok);
37410 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37411 {
37412 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37413 clauses);
37414 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37415 cp_parser_require_pragma_eol (parser, pragma_tok);
37416 }
37417 else
37418 {
37419 cp_parser_require_pragma_eol (parser, pragma_tok);
37420 scope_chain->omp_declare_target_attribute++;
37421 return;
37422 }
37423 if (scope_chain->omp_declare_target_attribute)
37424 error_at (pragma_tok->location,
37425 "%<#pragma omp declare target%> with clauses in between "
37426 "%<#pragma omp declare target%> without clauses and "
37427 "%<#pragma omp end declare target%>");
37428 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37429 {
37430 tree t = OMP_CLAUSE_DECL (c), id;
37431 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37432 tree at2 = lookup_attribute ("omp declare target link",
37433 DECL_ATTRIBUTES (t));
37434 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37435 {
37436 id = get_identifier ("omp declare target link");
37437 std::swap (at1, at2);
37438 }
37439 else
37440 id = get_identifier ("omp declare target");
37441 if (at2)
37442 {
37443 error_at (OMP_CLAUSE_LOCATION (c),
37444 "%qD specified both in declare target %<link%> and %<to%>"
37445 " clauses", t);
37446 continue;
37447 }
37448 if (!at1)
37449 {
37450 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37451 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37452 continue;
37453
37454 symtab_node *node = symtab_node::get (t);
37455 if (node != NULL)
37456 {
37457 node->offloadable = 1;
37458 if (ENABLE_OFFLOADING)
37459 {
37460 g->have_offload = true;
37461 if (is_a <varpool_node *> (node))
37462 vec_safe_push (offload_vars, t);
37463 }
37464 }
37465 }
37466 }
37467 }
37468
37469 static void
37470 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37471 {
37472 const char *p = "";
37473 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37474 {
37475 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37476 p = IDENTIFIER_POINTER (id);
37477 }
37478 if (strcmp (p, "declare") == 0)
37479 {
37480 cp_lexer_consume_token (parser->lexer);
37481 p = "";
37482 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37483 {
37484 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37485 p = IDENTIFIER_POINTER (id);
37486 }
37487 if (strcmp (p, "target") == 0)
37488 cp_lexer_consume_token (parser->lexer);
37489 else
37490 {
37491 cp_parser_error (parser, "expected %<target%>");
37492 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37493 return;
37494 }
37495 }
37496 else
37497 {
37498 cp_parser_error (parser, "expected %<declare%>");
37499 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37500 return;
37501 }
37502 cp_parser_require_pragma_eol (parser, pragma_tok);
37503 if (!scope_chain->omp_declare_target_attribute)
37504 error_at (pragma_tok->location,
37505 "%<#pragma omp end declare target%> without corresponding "
37506 "%<#pragma omp declare target%>");
37507 else
37508 scope_chain->omp_declare_target_attribute--;
37509 }
37510
37511 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37512 expression and optional initializer clause of
37513 #pragma omp declare reduction. We store the expression(s) as
37514 either 3, 6 or 7 special statements inside of the artificial function's
37515 body. The first two statements are DECL_EXPRs for the artificial
37516 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37517 expression that uses those variables.
37518 If there was any INITIALIZER clause, this is followed by further statements,
37519 the fourth and fifth statements are DECL_EXPRs for the artificial
37520 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37521 constructor variant (first token after open paren is not omp_priv),
37522 then the sixth statement is a statement with the function call expression
37523 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37524 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37525 to initialize the OMP_PRIV artificial variable and there is seventh
37526 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37527
37528 static bool
37529 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37530 {
37531 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37532 gcc_assert (TYPE_REF_P (type));
37533 type = TREE_TYPE (type);
37534 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37535 DECL_ARTIFICIAL (omp_out) = 1;
37536 pushdecl (omp_out);
37537 add_decl_expr (omp_out);
37538 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37539 DECL_ARTIFICIAL (omp_in) = 1;
37540 pushdecl (omp_in);
37541 add_decl_expr (omp_in);
37542 tree combiner;
37543 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37544
37545 keep_next_level (true);
37546 tree block = begin_omp_structured_block ();
37547 combiner = cp_parser_expression (parser);
37548 finish_expr_stmt (combiner);
37549 block = finish_omp_structured_block (block);
37550 add_stmt (block);
37551
37552 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37553 return false;
37554
37555 const char *p = "";
37556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37557 {
37558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37559 p = IDENTIFIER_POINTER (id);
37560 }
37561
37562 if (strcmp (p, "initializer") == 0)
37563 {
37564 cp_lexer_consume_token (parser->lexer);
37565 matching_parens parens;
37566 if (!parens.require_open (parser))
37567 return false;
37568
37569 p = "";
37570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37571 {
37572 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37573 p = IDENTIFIER_POINTER (id);
37574 }
37575
37576 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37577 DECL_ARTIFICIAL (omp_priv) = 1;
37578 pushdecl (omp_priv);
37579 add_decl_expr (omp_priv);
37580 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37581 DECL_ARTIFICIAL (omp_orig) = 1;
37582 pushdecl (omp_orig);
37583 add_decl_expr (omp_orig);
37584
37585 keep_next_level (true);
37586 block = begin_omp_structured_block ();
37587
37588 bool ctor = false;
37589 if (strcmp (p, "omp_priv") == 0)
37590 {
37591 bool is_direct_init, is_non_constant_init;
37592 ctor = true;
37593 cp_lexer_consume_token (parser->lexer);
37594 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37595 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37596 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37597 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37598 == CPP_CLOSE_PAREN
37599 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37600 == CPP_CLOSE_PAREN))
37601 {
37602 finish_omp_structured_block (block);
37603 error ("invalid initializer clause");
37604 return false;
37605 }
37606 initializer = cp_parser_initializer (parser, &is_direct_init,
37607 &is_non_constant_init);
37608 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37609 NULL_TREE, LOOKUP_ONLYCONVERTING);
37610 }
37611 else
37612 {
37613 cp_parser_parse_tentatively (parser);
37614 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37615 /*check_dependency_p=*/true,
37616 /*template_p=*/NULL,
37617 /*declarator_p=*/false,
37618 /*optional_p=*/false);
37619 vec<tree, va_gc> *args;
37620 if (fn_name == error_mark_node
37621 || cp_parser_error_occurred (parser)
37622 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37623 || ((args = cp_parser_parenthesized_expression_list
37624 (parser, non_attr, /*cast_p=*/false,
37625 /*allow_expansion_p=*/true,
37626 /*non_constant_p=*/NULL)),
37627 cp_parser_error_occurred (parser)))
37628 {
37629 finish_omp_structured_block (block);
37630 cp_parser_abort_tentative_parse (parser);
37631 cp_parser_error (parser, "expected id-expression (arguments)");
37632 return false;
37633 }
37634 unsigned int i;
37635 tree arg;
37636 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37637 if (arg == omp_priv
37638 || (TREE_CODE (arg) == ADDR_EXPR
37639 && TREE_OPERAND (arg, 0) == omp_priv))
37640 break;
37641 cp_parser_abort_tentative_parse (parser);
37642 if (arg == NULL_TREE)
37643 error ("one of the initializer call arguments should be %<omp_priv%>"
37644 " or %<&omp_priv%>");
37645 initializer = cp_parser_postfix_expression (parser, false, false, false,
37646 false, NULL);
37647 finish_expr_stmt (initializer);
37648 }
37649
37650 block = finish_omp_structured_block (block);
37651 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37652 add_stmt (block);
37653
37654 if (ctor)
37655 add_decl_expr (omp_orig);
37656
37657 if (!parens.require_close (parser))
37658 return false;
37659 }
37660
37661 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37662 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37663 UNKNOWN_LOCATION);
37664
37665 return true;
37666 }
37667
37668 /* OpenMP 4.0
37669 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37670 initializer-clause[opt] new-line
37671
37672 initializer-clause:
37673 initializer (omp_priv initializer)
37674 initializer (function-name (argument-list)) */
37675
37676 static void
37677 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37678 enum pragma_context)
37679 {
37680 auto_vec<tree> types;
37681 enum tree_code reduc_code = ERROR_MARK;
37682 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37683 unsigned int i;
37684 cp_token *first_token;
37685 cp_token_cache *cp;
37686 int errs;
37687 void *p;
37688
37689 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37690 p = obstack_alloc (&declarator_obstack, 0);
37691
37692 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37693 goto fail;
37694
37695 switch (cp_lexer_peek_token (parser->lexer)->type)
37696 {
37697 case CPP_PLUS:
37698 reduc_code = PLUS_EXPR;
37699 break;
37700 case CPP_MULT:
37701 reduc_code = MULT_EXPR;
37702 break;
37703 case CPP_MINUS:
37704 reduc_code = MINUS_EXPR;
37705 break;
37706 case CPP_AND:
37707 reduc_code = BIT_AND_EXPR;
37708 break;
37709 case CPP_XOR:
37710 reduc_code = BIT_XOR_EXPR;
37711 break;
37712 case CPP_OR:
37713 reduc_code = BIT_IOR_EXPR;
37714 break;
37715 case CPP_AND_AND:
37716 reduc_code = TRUTH_ANDIF_EXPR;
37717 break;
37718 case CPP_OR_OR:
37719 reduc_code = TRUTH_ORIF_EXPR;
37720 break;
37721 case CPP_NAME:
37722 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37723 break;
37724 default:
37725 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37726 "%<|%>, %<&&%>, %<||%> or identifier");
37727 goto fail;
37728 }
37729
37730 if (reduc_code != ERROR_MARK)
37731 cp_lexer_consume_token (parser->lexer);
37732
37733 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37734 if (reduc_id == error_mark_node)
37735 goto fail;
37736
37737 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37738 goto fail;
37739
37740 /* Types may not be defined in declare reduction type list. */
37741 const char *saved_message;
37742 saved_message = parser->type_definition_forbidden_message;
37743 parser->type_definition_forbidden_message
37744 = G_("types may not be defined in declare reduction type list");
37745 bool saved_colon_corrects_to_scope_p;
37746 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37747 parser->colon_corrects_to_scope_p = false;
37748 bool saved_colon_doesnt_start_class_def_p;
37749 saved_colon_doesnt_start_class_def_p
37750 = parser->colon_doesnt_start_class_def_p;
37751 parser->colon_doesnt_start_class_def_p = true;
37752
37753 while (true)
37754 {
37755 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37756 type = cp_parser_type_id (parser);
37757 if (type == error_mark_node)
37758 ;
37759 else if (ARITHMETIC_TYPE_P (type)
37760 && (orig_reduc_id == NULL_TREE
37761 || (TREE_CODE (type) != COMPLEX_TYPE
37762 && (id_equal (orig_reduc_id, "min")
37763 || id_equal (orig_reduc_id, "max")))))
37764 error_at (loc, "predeclared arithmetic type %qT in "
37765 "%<#pragma omp declare reduction%>", type);
37766 else if (TREE_CODE (type) == FUNCTION_TYPE
37767 || TREE_CODE (type) == METHOD_TYPE
37768 || TREE_CODE (type) == ARRAY_TYPE)
37769 error_at (loc, "function or array type %qT in "
37770 "%<#pragma omp declare reduction%>", type);
37771 else if (TYPE_REF_P (type))
37772 error_at (loc, "reference type %qT in "
37773 "%<#pragma omp declare reduction%>", type);
37774 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37775 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37776 "%<#pragma omp declare reduction%>", type);
37777 else
37778 types.safe_push (type);
37779
37780 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37781 cp_lexer_consume_token (parser->lexer);
37782 else
37783 break;
37784 }
37785
37786 /* Restore the saved message. */
37787 parser->type_definition_forbidden_message = saved_message;
37788 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37789 parser->colon_doesnt_start_class_def_p
37790 = saved_colon_doesnt_start_class_def_p;
37791
37792 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37793 || types.is_empty ())
37794 {
37795 fail:
37796 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37797 goto done;
37798 }
37799
37800 first_token = cp_lexer_peek_token (parser->lexer);
37801 cp = NULL;
37802 errs = errorcount;
37803 FOR_EACH_VEC_ELT (types, i, type)
37804 {
37805 tree fntype
37806 = build_function_type_list (void_type_node,
37807 cp_build_reference_type (type, false),
37808 NULL_TREE);
37809 tree this_reduc_id = reduc_id;
37810 if (!dependent_type_p (type))
37811 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37812 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37813 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37814 DECL_ARTIFICIAL (fndecl) = 1;
37815 DECL_EXTERNAL (fndecl) = 1;
37816 DECL_DECLARED_INLINE_P (fndecl) = 1;
37817 DECL_IGNORED_P (fndecl) = 1;
37818 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37819 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37820 DECL_ATTRIBUTES (fndecl)
37821 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37822 DECL_ATTRIBUTES (fndecl));
37823 if (processing_template_decl)
37824 fndecl = push_template_decl (fndecl);
37825 bool block_scope = false;
37826 tree block = NULL_TREE;
37827 if (current_function_decl)
37828 {
37829 block_scope = true;
37830 DECL_CONTEXT (fndecl) = global_namespace;
37831 if (!processing_template_decl)
37832 pushdecl (fndecl);
37833 }
37834 else if (current_class_type)
37835 {
37836 if (cp == NULL)
37837 {
37838 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37839 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37840 cp_lexer_consume_token (parser->lexer);
37841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37842 goto fail;
37843 cp = cp_token_cache_new (first_token,
37844 cp_lexer_peek_nth_token (parser->lexer,
37845 2));
37846 }
37847 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37848 finish_member_declaration (fndecl);
37849 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37850 DECL_PENDING_INLINE_P (fndecl) = 1;
37851 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37852 continue;
37853 }
37854 else
37855 {
37856 DECL_CONTEXT (fndecl) = current_namespace;
37857 pushdecl (fndecl);
37858 }
37859 if (!block_scope)
37860 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37861 else
37862 block = begin_omp_structured_block ();
37863 if (cp)
37864 {
37865 cp_parser_push_lexer_for_tokens (parser, cp);
37866 parser->lexer->in_pragma = true;
37867 }
37868 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37869 {
37870 if (!block_scope)
37871 finish_function (/*inline_p=*/false);
37872 else
37873 DECL_CONTEXT (fndecl) = current_function_decl;
37874 if (cp)
37875 cp_parser_pop_lexer (parser);
37876 goto fail;
37877 }
37878 if (cp)
37879 cp_parser_pop_lexer (parser);
37880 if (!block_scope)
37881 finish_function (/*inline_p=*/false);
37882 else
37883 {
37884 DECL_CONTEXT (fndecl) = current_function_decl;
37885 block = finish_omp_structured_block (block);
37886 if (TREE_CODE (block) == BIND_EXPR)
37887 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37888 else if (TREE_CODE (block) == STATEMENT_LIST)
37889 DECL_SAVED_TREE (fndecl) = block;
37890 if (processing_template_decl)
37891 add_decl_expr (fndecl);
37892 }
37893 cp_check_omp_declare_reduction (fndecl);
37894 if (cp == NULL && types.length () > 1)
37895 cp = cp_token_cache_new (first_token,
37896 cp_lexer_peek_nth_token (parser->lexer, 2));
37897 if (errs != errorcount)
37898 break;
37899 }
37900
37901 cp_parser_require_pragma_eol (parser, pragma_tok);
37902
37903 done:
37904 /* Free any declarators allocated. */
37905 obstack_free (&declarator_obstack, p);
37906 }
37907
37908 /* OpenMP 4.0
37909 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37910 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37911 initializer-clause[opt] new-line
37912 #pragma omp declare target new-line */
37913
37914 static bool
37915 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37916 enum pragma_context context)
37917 {
37918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37919 {
37920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37921 const char *p = IDENTIFIER_POINTER (id);
37922
37923 if (strcmp (p, "simd") == 0)
37924 {
37925 cp_lexer_consume_token (parser->lexer);
37926 cp_parser_omp_declare_simd (parser, pragma_tok,
37927 context);
37928 return true;
37929 }
37930 cp_ensure_no_omp_declare_simd (parser);
37931 if (strcmp (p, "reduction") == 0)
37932 {
37933 cp_lexer_consume_token (parser->lexer);
37934 cp_parser_omp_declare_reduction (parser, pragma_tok,
37935 context);
37936 return false;
37937 }
37938 if (!flag_openmp) /* flag_openmp_simd */
37939 {
37940 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37941 return false;
37942 }
37943 if (strcmp (p, "target") == 0)
37944 {
37945 cp_lexer_consume_token (parser->lexer);
37946 cp_parser_omp_declare_target (parser, pragma_tok);
37947 return false;
37948 }
37949 }
37950 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37951 "or %<target%>");
37952 cp_parser_require_pragma_eol (parser, pragma_tok);
37953 return false;
37954 }
37955
37956 /* OpenMP 4.5:
37957 #pragma omp taskloop taskloop-clause[optseq] new-line
37958 for-loop
37959
37960 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37961 for-loop */
37962
37963 #define OMP_TASKLOOP_CLAUSE_MASK \
37964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37978
37979 static tree
37980 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37981 char *p_name, omp_clause_mask mask, tree *cclauses,
37982 bool *if_p)
37983 {
37984 tree clauses, sb, ret;
37985 unsigned int save;
37986 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37987
37988 strcat (p_name, " taskloop");
37989 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37990
37991 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37992 {
37993 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37994 const char *p = IDENTIFIER_POINTER (id);
37995
37996 if (strcmp (p, "simd") == 0)
37997 {
37998 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37999 if (cclauses == NULL)
38000 cclauses = cclauses_buf;
38001
38002 cp_lexer_consume_token (parser->lexer);
38003 if (!flag_openmp) /* flag_openmp_simd */
38004 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38005 cclauses, if_p);
38006 sb = begin_omp_structured_block ();
38007 save = cp_parser_begin_omp_structured_block (parser);
38008 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38009 cclauses, if_p);
38010 cp_parser_end_omp_structured_block (parser, save);
38011 tree body = finish_omp_structured_block (sb);
38012 if (ret == NULL)
38013 return ret;
38014 ret = make_node (OMP_TASKLOOP);
38015 TREE_TYPE (ret) = void_type_node;
38016 OMP_FOR_BODY (ret) = body;
38017 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38018 SET_EXPR_LOCATION (ret, loc);
38019 add_stmt (ret);
38020 return ret;
38021 }
38022 }
38023 if (!flag_openmp) /* flag_openmp_simd */
38024 {
38025 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38026 return NULL_TREE;
38027 }
38028
38029 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38030 cclauses == NULL);
38031 if (cclauses)
38032 {
38033 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38034 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38035 }
38036
38037 sb = begin_omp_structured_block ();
38038 save = cp_parser_begin_omp_structured_block (parser);
38039
38040 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38041 if_p);
38042
38043 cp_parser_end_omp_structured_block (parser, save);
38044 add_stmt (finish_omp_structured_block (sb));
38045
38046 return ret;
38047 }
38048
38049
38050 /* OpenACC 2.0:
38051 # pragma acc routine oacc-routine-clause[optseq] new-line
38052 function-definition
38053
38054 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38055 */
38056
38057 #define OACC_ROUTINE_CLAUSE_MASK \
38058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38062
38063
38064 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38065 component, which must resolve to a declared namespace-scope
38066 function. The clauses are either processed directly (for a named
38067 function), or defered until the immediatley following declaration
38068 is parsed. */
38069
38070 static void
38071 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38072 enum pragma_context context)
38073 {
38074 gcc_checking_assert (context == pragma_external);
38075 /* The checking for "another pragma following this one" in the "no optional
38076 '( name )'" case makes sure that we dont re-enter. */
38077 gcc_checking_assert (parser->oacc_routine == NULL);
38078
38079 cp_oacc_routine_data data;
38080 data.error_seen = false;
38081 data.fndecl_seen = false;
38082 data.tokens = vNULL;
38083 data.clauses = NULL_TREE;
38084 data.loc = pragma_tok->location;
38085 /* It is safe to take the address of a local variable; it will only be
38086 used while this scope is live. */
38087 parser->oacc_routine = &data;
38088
38089 /* Look for optional '( name )'. */
38090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38091 {
38092 matching_parens parens;
38093 parens.consume_open (parser); /* '(' */
38094
38095 /* We parse the name as an id-expression. If it resolves to
38096 anything other than a non-overloaded function at namespace
38097 scope, it's an error. */
38098 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38099 tree name = cp_parser_id_expression (parser,
38100 /*template_keyword_p=*/false,
38101 /*check_dependency_p=*/false,
38102 /*template_p=*/NULL,
38103 /*declarator_p=*/false,
38104 /*optional_p=*/false);
38105 tree decl = (identifier_p (name)
38106 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38107 : name);
38108 if (name != error_mark_node && decl == error_mark_node)
38109 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38110
38111 if (decl == error_mark_node
38112 || !parens.require_close (parser))
38113 {
38114 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38115 parser->oacc_routine = NULL;
38116 return;
38117 }
38118
38119 data.clauses
38120 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38121 "#pragma acc routine",
38122 cp_lexer_peek_token (parser->lexer));
38123
38124 if (decl && is_overloaded_fn (decl)
38125 && (TREE_CODE (decl) != FUNCTION_DECL
38126 || DECL_FUNCTION_TEMPLATE_P (decl)))
38127 {
38128 error_at (name_loc,
38129 "%<#pragma acc routine%> names a set of overloads");
38130 parser->oacc_routine = NULL;
38131 return;
38132 }
38133
38134 /* Perhaps we should use the same rule as declarations in different
38135 namespaces? */
38136 if (!DECL_NAMESPACE_SCOPE_P (decl))
38137 {
38138 error_at (name_loc,
38139 "%qD does not refer to a namespace scope function", decl);
38140 parser->oacc_routine = NULL;
38141 return;
38142 }
38143
38144 if (TREE_CODE (decl) != FUNCTION_DECL)
38145 {
38146 error_at (name_loc, "%qD does not refer to a function", decl);
38147 parser->oacc_routine = NULL;
38148 return;
38149 }
38150
38151 cp_finalize_oacc_routine (parser, decl, false);
38152 parser->oacc_routine = NULL;
38153 }
38154 else /* No optional '( name )'. */
38155 {
38156 /* Store away all pragma tokens. */
38157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38158 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38159 cp_lexer_consume_token (parser->lexer);
38160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38161 parser->oacc_routine->error_seen = true;
38162 cp_parser_require_pragma_eol (parser, pragma_tok);
38163 struct cp_token_cache *cp
38164 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38165 parser->oacc_routine->tokens.safe_push (cp);
38166
38167 /* Emit a helpful diagnostic if there's another pragma following this
38168 one. */
38169 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38170 {
38171 cp_ensure_no_oacc_routine (parser);
38172 data.tokens.release ();
38173 /* ..., and then just keep going. */
38174 return;
38175 }
38176
38177 /* We only have to consider the pragma_external case here. */
38178 cp_parser_declaration (parser);
38179 if (parser->oacc_routine
38180 && !parser->oacc_routine->fndecl_seen)
38181 cp_ensure_no_oacc_routine (parser);
38182 else
38183 parser->oacc_routine = NULL;
38184 data.tokens.release ();
38185 }
38186 }
38187
38188 /* Finalize #pragma acc routine clauses after direct declarator has
38189 been parsed. */
38190
38191 static tree
38192 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38193 {
38194 struct cp_token_cache *ce;
38195 cp_oacc_routine_data *data = parser->oacc_routine;
38196
38197 if (!data->error_seen && data->fndecl_seen)
38198 {
38199 error_at (data->loc,
38200 "%<#pragma acc routine%> not immediately followed by "
38201 "a single function declaration or definition");
38202 data->error_seen = true;
38203 }
38204 if (data->error_seen)
38205 return attrs;
38206
38207 gcc_checking_assert (data->tokens.length () == 1);
38208 ce = data->tokens[0];
38209
38210 cp_parser_push_lexer_for_tokens (parser, ce);
38211 parser->lexer->in_pragma = true;
38212 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38213
38214 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38215 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38216 parser->oacc_routine->clauses
38217 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38218 "#pragma acc routine", pragma_tok);
38219 cp_parser_pop_lexer (parser);
38220 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38221 fndecl_seen. */
38222
38223 return attrs;
38224 }
38225
38226 /* Apply any saved OpenACC routine clauses to a just-parsed
38227 declaration. */
38228
38229 static void
38230 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38231 {
38232 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38233 {
38234 /* Keep going if we're in error reporting mode. */
38235 if (parser->oacc_routine->error_seen
38236 || fndecl == error_mark_node)
38237 return;
38238
38239 if (parser->oacc_routine->fndecl_seen)
38240 {
38241 error_at (parser->oacc_routine->loc,
38242 "%<#pragma acc routine%> not immediately followed by"
38243 " a single function declaration or definition");
38244 parser->oacc_routine = NULL;
38245 return;
38246 }
38247 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38248 {
38249 cp_ensure_no_oacc_routine (parser);
38250 return;
38251 }
38252
38253 if (oacc_get_fn_attrib (fndecl))
38254 {
38255 error_at (parser->oacc_routine->loc,
38256 "%<#pragma acc routine%> already applied to %qD", fndecl);
38257 parser->oacc_routine = NULL;
38258 return;
38259 }
38260
38261 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38262 {
38263 error_at (parser->oacc_routine->loc,
38264 TREE_USED (fndecl)
38265 ? G_("%<#pragma acc routine%> must be applied before use")
38266 : G_("%<#pragma acc routine%> must be applied before "
38267 "definition"));
38268 parser->oacc_routine = NULL;
38269 return;
38270 }
38271
38272 /* Process the routine's dimension clauses. */
38273 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38274 oacc_replace_fn_attrib (fndecl, dims);
38275
38276 /* Add an "omp declare target" attribute. */
38277 DECL_ATTRIBUTES (fndecl)
38278 = tree_cons (get_identifier ("omp declare target"),
38279 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38280
38281 /* Don't unset parser->oacc_routine here: we may still need it to
38282 diagnose wrong usage. But, remember that we've used this "#pragma acc
38283 routine". */
38284 parser->oacc_routine->fndecl_seen = true;
38285 }
38286 }
38287
38288 /* Main entry point to OpenMP statement pragmas. */
38289
38290 static void
38291 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38292 {
38293 tree stmt;
38294 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38295 omp_clause_mask mask (0);
38296
38297 switch (cp_parser_pragma_kind (pragma_tok))
38298 {
38299 case PRAGMA_OACC_ATOMIC:
38300 cp_parser_omp_atomic (parser, pragma_tok);
38301 return;
38302 case PRAGMA_OACC_CACHE:
38303 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38304 break;
38305 case PRAGMA_OACC_DATA:
38306 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38307 break;
38308 case PRAGMA_OACC_ENTER_DATA:
38309 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38310 break;
38311 case PRAGMA_OACC_EXIT_DATA:
38312 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38313 break;
38314 case PRAGMA_OACC_HOST_DATA:
38315 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38316 break;
38317 case PRAGMA_OACC_KERNELS:
38318 case PRAGMA_OACC_PARALLEL:
38319 strcpy (p_name, "#pragma acc");
38320 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38321 if_p);
38322 break;
38323 case PRAGMA_OACC_LOOP:
38324 strcpy (p_name, "#pragma acc");
38325 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38326 if_p);
38327 break;
38328 case PRAGMA_OACC_UPDATE:
38329 stmt = cp_parser_oacc_update (parser, pragma_tok);
38330 break;
38331 case PRAGMA_OACC_WAIT:
38332 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38333 break;
38334 case PRAGMA_OMP_ATOMIC:
38335 cp_parser_omp_atomic (parser, pragma_tok);
38336 return;
38337 case PRAGMA_OMP_CRITICAL:
38338 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38339 break;
38340 case PRAGMA_OMP_DISTRIBUTE:
38341 strcpy (p_name, "#pragma omp");
38342 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38343 if_p);
38344 break;
38345 case PRAGMA_OMP_FOR:
38346 strcpy (p_name, "#pragma omp");
38347 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38348 if_p);
38349 break;
38350 case PRAGMA_OMP_MASTER:
38351 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38352 break;
38353 case PRAGMA_OMP_PARALLEL:
38354 strcpy (p_name, "#pragma omp");
38355 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38356 if_p);
38357 break;
38358 case PRAGMA_OMP_SECTIONS:
38359 strcpy (p_name, "#pragma omp");
38360 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38361 break;
38362 case PRAGMA_OMP_SIMD:
38363 strcpy (p_name, "#pragma omp");
38364 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38365 if_p);
38366 break;
38367 case PRAGMA_OMP_SINGLE:
38368 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38369 break;
38370 case PRAGMA_OMP_TASK:
38371 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38372 break;
38373 case PRAGMA_OMP_TASKGROUP:
38374 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38375 break;
38376 case PRAGMA_OMP_TASKLOOP:
38377 strcpy (p_name, "#pragma omp");
38378 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38379 if_p);
38380 break;
38381 case PRAGMA_OMP_TEAMS:
38382 strcpy (p_name, "#pragma omp");
38383 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38384 if_p);
38385 break;
38386 default:
38387 gcc_unreachable ();
38388 }
38389
38390 protected_set_expr_location (stmt, pragma_tok->location);
38391 }
38392 \f
38393 /* Transactional Memory parsing routines. */
38394
38395 /* Parse a transaction attribute.
38396
38397 txn-attribute:
38398 attribute
38399 [ [ identifier ] ]
38400
38401 We use this instead of cp_parser_attributes_opt for transactions to avoid
38402 the pedwarn in C++98 mode. */
38403
38404 static tree
38405 cp_parser_txn_attribute_opt (cp_parser *parser)
38406 {
38407 cp_token *token;
38408 tree attr_name, attr = NULL;
38409
38410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38411 return cp_parser_attributes_opt (parser);
38412
38413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38414 return NULL_TREE;
38415 cp_lexer_consume_token (parser->lexer);
38416 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38417 goto error1;
38418
38419 token = cp_lexer_peek_token (parser->lexer);
38420 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38421 {
38422 token = cp_lexer_consume_token (parser->lexer);
38423
38424 attr_name = (token->type == CPP_KEYWORD
38425 /* For keywords, use the canonical spelling,
38426 not the parsed identifier. */
38427 ? ridpointers[(int) token->keyword]
38428 : token->u.value);
38429 attr = build_tree_list (attr_name, NULL_TREE);
38430 }
38431 else
38432 cp_parser_error (parser, "expected identifier");
38433
38434 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38435 error1:
38436 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38437 return attr;
38438 }
38439
38440 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38441
38442 transaction-statement:
38443 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38444 compound-statement
38445 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38446 */
38447
38448 static tree
38449 cp_parser_transaction (cp_parser *parser, cp_token *token)
38450 {
38451 unsigned char old_in = parser->in_transaction;
38452 unsigned char this_in = 1, new_in;
38453 enum rid keyword = token->keyword;
38454 tree stmt, attrs, noex;
38455
38456 cp_lexer_consume_token (parser->lexer);
38457
38458 if (keyword == RID_TRANSACTION_RELAXED
38459 || keyword == RID_SYNCHRONIZED)
38460 this_in |= TM_STMT_ATTR_RELAXED;
38461 else
38462 {
38463 attrs = cp_parser_txn_attribute_opt (parser);
38464 if (attrs)
38465 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38466 }
38467
38468 /* Parse a noexcept specification. */
38469 if (keyword == RID_ATOMIC_NOEXCEPT)
38470 noex = boolean_true_node;
38471 else if (keyword == RID_ATOMIC_CANCEL)
38472 {
38473 /* cancel-and-throw is unimplemented. */
38474 sorry ("atomic_cancel");
38475 noex = NULL_TREE;
38476 }
38477 else
38478 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38479
38480 /* Keep track if we're in the lexical scope of an outer transaction. */
38481 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38482
38483 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38484
38485 parser->in_transaction = new_in;
38486 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38487 parser->in_transaction = old_in;
38488
38489 finish_transaction_stmt (stmt, NULL, this_in, noex);
38490
38491 return stmt;
38492 }
38493
38494 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38495
38496 transaction-expression:
38497 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38498 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38499 */
38500
38501 static tree
38502 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38503 {
38504 unsigned char old_in = parser->in_transaction;
38505 unsigned char this_in = 1;
38506 cp_token *token;
38507 tree expr, noex;
38508 bool noex_expr;
38509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38510
38511 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38512 || keyword == RID_TRANSACTION_RELAXED);
38513
38514 if (!flag_tm)
38515 error_at (loc,
38516 keyword == RID_TRANSACTION_RELAXED
38517 ? G_("%<__transaction_relaxed%> without transactional memory "
38518 "support enabled")
38519 : G_("%<__transaction_atomic%> without transactional memory "
38520 "support enabled"));
38521
38522 token = cp_parser_require_keyword (parser, keyword,
38523 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38524 : RT_TRANSACTION_RELAXED));
38525 gcc_assert (token != NULL);
38526
38527 if (keyword == RID_TRANSACTION_RELAXED)
38528 this_in |= TM_STMT_ATTR_RELAXED;
38529
38530 /* Set this early. This might mean that we allow transaction_cancel in
38531 an expression that we find out later actually has to be a constexpr.
38532 However, we expect that cxx_constant_value will be able to deal with
38533 this; also, if the noexcept has no constexpr, then what we parse next
38534 really is a transaction's body. */
38535 parser->in_transaction = this_in;
38536
38537 /* Parse a noexcept specification. */
38538 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38539 true);
38540
38541 if (!noex || !noex_expr
38542 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38543 {
38544 matching_parens parens;
38545 parens.require_open (parser);
38546
38547 expr = cp_parser_expression (parser);
38548 expr = finish_parenthesized_expr (expr);
38549
38550 parens.require_close (parser);
38551 }
38552 else
38553 {
38554 /* The only expression that is available got parsed for the noexcept
38555 already. noexcept is true then. */
38556 expr = noex;
38557 noex = boolean_true_node;
38558 }
38559
38560 expr = build_transaction_expr (token->location, expr, this_in, noex);
38561 parser->in_transaction = old_in;
38562
38563 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38564 return error_mark_node;
38565
38566 return (flag_tm ? expr : error_mark_node);
38567 }
38568
38569 /* Parse a function-transaction-block.
38570
38571 function-transaction-block:
38572 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38573 function-body
38574 __transaction_atomic txn-attribute[opt] function-try-block
38575 __transaction_relaxed ctor-initializer[opt] function-body
38576 __transaction_relaxed function-try-block
38577 */
38578
38579 static void
38580 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38581 {
38582 unsigned char old_in = parser->in_transaction;
38583 unsigned char new_in = 1;
38584 tree compound_stmt, stmt, attrs;
38585 cp_token *token;
38586
38587 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38588 || keyword == RID_TRANSACTION_RELAXED);
38589 token = cp_parser_require_keyword (parser, keyword,
38590 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38591 : RT_TRANSACTION_RELAXED));
38592 gcc_assert (token != NULL);
38593
38594 if (keyword == RID_TRANSACTION_RELAXED)
38595 new_in |= TM_STMT_ATTR_RELAXED;
38596 else
38597 {
38598 attrs = cp_parser_txn_attribute_opt (parser);
38599 if (attrs)
38600 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38601 }
38602
38603 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38604
38605 parser->in_transaction = new_in;
38606
38607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38608 cp_parser_function_try_block (parser);
38609 else
38610 cp_parser_ctor_initializer_opt_and_function_body
38611 (parser, /*in_function_try_block=*/false);
38612
38613 parser->in_transaction = old_in;
38614
38615 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38616 }
38617
38618 /* Parse a __transaction_cancel statement.
38619
38620 cancel-statement:
38621 __transaction_cancel txn-attribute[opt] ;
38622 __transaction_cancel txn-attribute[opt] throw-expression ;
38623
38624 ??? Cancel and throw is not yet implemented. */
38625
38626 static tree
38627 cp_parser_transaction_cancel (cp_parser *parser)
38628 {
38629 cp_token *token;
38630 bool is_outer = false;
38631 tree stmt, attrs;
38632
38633 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38634 RT_TRANSACTION_CANCEL);
38635 gcc_assert (token != NULL);
38636
38637 attrs = cp_parser_txn_attribute_opt (parser);
38638 if (attrs)
38639 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38640
38641 /* ??? Parse cancel-and-throw here. */
38642
38643 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38644
38645 if (!flag_tm)
38646 {
38647 error_at (token->location, "%<__transaction_cancel%> without "
38648 "transactional memory support enabled");
38649 return error_mark_node;
38650 }
38651 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38652 {
38653 error_at (token->location, "%<__transaction_cancel%> within a "
38654 "%<__transaction_relaxed%>");
38655 return error_mark_node;
38656 }
38657 else if (is_outer)
38658 {
38659 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38660 && !is_tm_may_cancel_outer (current_function_decl))
38661 {
38662 error_at (token->location, "outer %<__transaction_cancel%> not "
38663 "within outer %<__transaction_atomic%>");
38664 error_at (token->location,
38665 " or a %<transaction_may_cancel_outer%> function");
38666 return error_mark_node;
38667 }
38668 }
38669 else if (parser->in_transaction == 0)
38670 {
38671 error_at (token->location, "%<__transaction_cancel%> not within "
38672 "%<__transaction_atomic%>");
38673 return error_mark_node;
38674 }
38675
38676 stmt = build_tm_abort_call (token->location, is_outer);
38677 add_stmt (stmt);
38678
38679 return stmt;
38680 }
38681 \f
38682 /* The parser. */
38683
38684 static GTY (()) cp_parser *the_parser;
38685
38686 \f
38687 /* Special handling for the first token or line in the file. The first
38688 thing in the file might be #pragma GCC pch_preprocess, which loads a
38689 PCH file, which is a GC collection point. So we need to handle this
38690 first pragma without benefit of an existing lexer structure.
38691
38692 Always returns one token to the caller in *FIRST_TOKEN. This is
38693 either the true first token of the file, or the first token after
38694 the initial pragma. */
38695
38696 static void
38697 cp_parser_initial_pragma (cp_token *first_token)
38698 {
38699 tree name = NULL;
38700
38701 cp_lexer_get_preprocessor_token (NULL, first_token);
38702 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38703 return;
38704
38705 cp_lexer_get_preprocessor_token (NULL, first_token);
38706 if (first_token->type == CPP_STRING)
38707 {
38708 name = first_token->u.value;
38709
38710 cp_lexer_get_preprocessor_token (NULL, first_token);
38711 if (first_token->type != CPP_PRAGMA_EOL)
38712 error_at (first_token->location,
38713 "junk at end of %<#pragma GCC pch_preprocess%>");
38714 }
38715 else
38716 error_at (first_token->location, "expected string literal");
38717
38718 /* Skip to the end of the pragma. */
38719 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38720 cp_lexer_get_preprocessor_token (NULL, first_token);
38721
38722 /* Now actually load the PCH file. */
38723 if (name)
38724 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38725
38726 /* Read one more token to return to our caller. We have to do this
38727 after reading the PCH file in, since its pointers have to be
38728 live. */
38729 cp_lexer_get_preprocessor_token (NULL, first_token);
38730 }
38731
38732 /* Parse a pragma GCC ivdep. */
38733
38734 static bool
38735 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38736 {
38737 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38738 return true;
38739 }
38740
38741 /* Parse a pragma GCC unroll. */
38742
38743 static unsigned short
38744 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38745 {
38746 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38747 tree expr = cp_parser_constant_expression (parser);
38748 unsigned short unroll;
38749 expr = maybe_constant_value (expr);
38750 HOST_WIDE_INT lunroll = 0;
38751 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38752 || TREE_CODE (expr) != INTEGER_CST
38753 || (lunroll = tree_to_shwi (expr)) < 0
38754 || lunroll >= USHRT_MAX)
38755 {
38756 error_at (location, "%<#pragma GCC unroll%> requires an"
38757 " assignment-expression that evaluates to a non-negative"
38758 " integral constant less than %u", USHRT_MAX);
38759 unroll = 0;
38760 }
38761 else
38762 {
38763 unroll = (unsigned short)lunroll;
38764 if (unroll == 0)
38765 unroll = 1;
38766 }
38767 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38768 return unroll;
38769 }
38770
38771 /* Normal parsing of a pragma token. Here we can (and must) use the
38772 regular lexer. */
38773
38774 static bool
38775 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38776 {
38777 cp_token *pragma_tok;
38778 unsigned int id;
38779 tree stmt;
38780 bool ret;
38781
38782 pragma_tok = cp_lexer_consume_token (parser->lexer);
38783 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38784 parser->lexer->in_pragma = true;
38785
38786 id = cp_parser_pragma_kind (pragma_tok);
38787 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38788 cp_ensure_no_omp_declare_simd (parser);
38789 switch (id)
38790 {
38791 case PRAGMA_GCC_PCH_PREPROCESS:
38792 error_at (pragma_tok->location,
38793 "%<#pragma GCC pch_preprocess%> must be first");
38794 break;
38795
38796 case PRAGMA_OMP_BARRIER:
38797 switch (context)
38798 {
38799 case pragma_compound:
38800 cp_parser_omp_barrier (parser, pragma_tok);
38801 return false;
38802 case pragma_stmt:
38803 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38804 "used in compound statements", "omp barrier");
38805 break;
38806 default:
38807 goto bad_stmt;
38808 }
38809 break;
38810
38811 case PRAGMA_OMP_FLUSH:
38812 switch (context)
38813 {
38814 case pragma_compound:
38815 cp_parser_omp_flush (parser, pragma_tok);
38816 return false;
38817 case pragma_stmt:
38818 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38819 "used in compound statements", "omp flush");
38820 break;
38821 default:
38822 goto bad_stmt;
38823 }
38824 break;
38825
38826 case PRAGMA_OMP_TASKWAIT:
38827 switch (context)
38828 {
38829 case pragma_compound:
38830 cp_parser_omp_taskwait (parser, pragma_tok);
38831 return false;
38832 case pragma_stmt:
38833 error_at (pragma_tok->location,
38834 "%<#pragma %s%> may only be used in compound statements",
38835 "omp taskwait");
38836 break;
38837 default:
38838 goto bad_stmt;
38839 }
38840 break;
38841
38842 case PRAGMA_OMP_TASKYIELD:
38843 switch (context)
38844 {
38845 case pragma_compound:
38846 cp_parser_omp_taskyield (parser, pragma_tok);
38847 return false;
38848 case pragma_stmt:
38849 error_at (pragma_tok->location,
38850 "%<#pragma %s%> may only be used in compound statements",
38851 "omp taskyield");
38852 break;
38853 default:
38854 goto bad_stmt;
38855 }
38856 break;
38857
38858 case PRAGMA_OMP_CANCEL:
38859 switch (context)
38860 {
38861 case pragma_compound:
38862 cp_parser_omp_cancel (parser, pragma_tok);
38863 return false;
38864 case pragma_stmt:
38865 error_at (pragma_tok->location,
38866 "%<#pragma %s%> may only be used in compound statements",
38867 "omp cancel");
38868 break;
38869 default:
38870 goto bad_stmt;
38871 }
38872 break;
38873
38874 case PRAGMA_OMP_CANCELLATION_POINT:
38875 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38876 return false;
38877
38878 case PRAGMA_OMP_THREADPRIVATE:
38879 cp_parser_omp_threadprivate (parser, pragma_tok);
38880 return false;
38881
38882 case PRAGMA_OMP_DECLARE:
38883 return cp_parser_omp_declare (parser, pragma_tok, context);
38884
38885 case PRAGMA_OACC_DECLARE:
38886 cp_parser_oacc_declare (parser, pragma_tok);
38887 return false;
38888
38889 case PRAGMA_OACC_ENTER_DATA:
38890 if (context == pragma_stmt)
38891 {
38892 error_at (pragma_tok->location,
38893 "%<#pragma %s%> may only be used in compound statements",
38894 "acc enter data");
38895 break;
38896 }
38897 else if (context != pragma_compound)
38898 goto bad_stmt;
38899 cp_parser_omp_construct (parser, pragma_tok, if_p);
38900 return true;
38901
38902 case PRAGMA_OACC_EXIT_DATA:
38903 if (context == pragma_stmt)
38904 {
38905 error_at (pragma_tok->location,
38906 "%<#pragma %s%> may only be used in compound statements",
38907 "acc exit data");
38908 break;
38909 }
38910 else if (context != pragma_compound)
38911 goto bad_stmt;
38912 cp_parser_omp_construct (parser, pragma_tok, if_p);
38913 return true;
38914
38915 case PRAGMA_OACC_ROUTINE:
38916 if (context != pragma_external)
38917 {
38918 error_at (pragma_tok->location,
38919 "%<#pragma acc routine%> must be at file scope");
38920 break;
38921 }
38922 cp_parser_oacc_routine (parser, pragma_tok, context);
38923 return false;
38924
38925 case PRAGMA_OACC_UPDATE:
38926 if (context == pragma_stmt)
38927 {
38928 error_at (pragma_tok->location,
38929 "%<#pragma %s%> may only be used in compound statements",
38930 "acc update");
38931 break;
38932 }
38933 else if (context != pragma_compound)
38934 goto bad_stmt;
38935 cp_parser_omp_construct (parser, pragma_tok, if_p);
38936 return true;
38937
38938 case PRAGMA_OACC_WAIT:
38939 if (context == pragma_stmt)
38940 {
38941 error_at (pragma_tok->location,
38942 "%<#pragma %s%> may only be used in compound statements",
38943 "acc wait");
38944 break;
38945 }
38946 else if (context != pragma_compound)
38947 goto bad_stmt;
38948 cp_parser_omp_construct (parser, pragma_tok, if_p);
38949 return true;
38950
38951 case PRAGMA_OACC_ATOMIC:
38952 case PRAGMA_OACC_CACHE:
38953 case PRAGMA_OACC_DATA:
38954 case PRAGMA_OACC_HOST_DATA:
38955 case PRAGMA_OACC_KERNELS:
38956 case PRAGMA_OACC_PARALLEL:
38957 case PRAGMA_OACC_LOOP:
38958 case PRAGMA_OMP_ATOMIC:
38959 case PRAGMA_OMP_CRITICAL:
38960 case PRAGMA_OMP_DISTRIBUTE:
38961 case PRAGMA_OMP_FOR:
38962 case PRAGMA_OMP_MASTER:
38963 case PRAGMA_OMP_PARALLEL:
38964 case PRAGMA_OMP_SECTIONS:
38965 case PRAGMA_OMP_SIMD:
38966 case PRAGMA_OMP_SINGLE:
38967 case PRAGMA_OMP_TASK:
38968 case PRAGMA_OMP_TASKGROUP:
38969 case PRAGMA_OMP_TASKLOOP:
38970 case PRAGMA_OMP_TEAMS:
38971 if (context != pragma_stmt && context != pragma_compound)
38972 goto bad_stmt;
38973 stmt = push_omp_privatization_clauses (false);
38974 cp_parser_omp_construct (parser, pragma_tok, if_p);
38975 pop_omp_privatization_clauses (stmt);
38976 return true;
38977
38978 case PRAGMA_OMP_ORDERED:
38979 if (context != pragma_stmt && context != pragma_compound)
38980 goto bad_stmt;
38981 stmt = push_omp_privatization_clauses (false);
38982 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38983 pop_omp_privatization_clauses (stmt);
38984 return ret;
38985
38986 case PRAGMA_OMP_TARGET:
38987 if (context != pragma_stmt && context != pragma_compound)
38988 goto bad_stmt;
38989 stmt = push_omp_privatization_clauses (false);
38990 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38991 pop_omp_privatization_clauses (stmt);
38992 return ret;
38993
38994 case PRAGMA_OMP_END_DECLARE_TARGET:
38995 cp_parser_omp_end_declare_target (parser, pragma_tok);
38996 return false;
38997
38998 case PRAGMA_OMP_SECTION:
38999 error_at (pragma_tok->location,
39000 "%<#pragma omp section%> may only be used in "
39001 "%<#pragma omp sections%> construct");
39002 break;
39003
39004 case PRAGMA_IVDEP:
39005 {
39006 if (context == pragma_external)
39007 {
39008 error_at (pragma_tok->location,
39009 "%<#pragma GCC ivdep%> must be inside a function");
39010 break;
39011 }
39012 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39013 unsigned short unroll;
39014 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39015 if (tok->type == CPP_PRAGMA
39016 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39017 {
39018 tok = cp_lexer_consume_token (parser->lexer);
39019 unroll = cp_parser_pragma_unroll (parser, tok);
39020 tok = cp_lexer_peek_token (the_parser->lexer);
39021 }
39022 else
39023 unroll = 0;
39024 if (tok->type != CPP_KEYWORD
39025 || (tok->keyword != RID_FOR
39026 && tok->keyword != RID_WHILE
39027 && tok->keyword != RID_DO))
39028 {
39029 cp_parser_error (parser, "for, while or do statement expected");
39030 return false;
39031 }
39032 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39033 return true;
39034 }
39035
39036 case PRAGMA_UNROLL:
39037 {
39038 if (context == pragma_external)
39039 {
39040 error_at (pragma_tok->location,
39041 "%<#pragma GCC unroll%> must be inside a function");
39042 break;
39043 }
39044 const unsigned short unroll
39045 = cp_parser_pragma_unroll (parser, pragma_tok);
39046 bool ivdep;
39047 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39048 if (tok->type == CPP_PRAGMA
39049 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39050 {
39051 tok = cp_lexer_consume_token (parser->lexer);
39052 ivdep = cp_parser_pragma_ivdep (parser, tok);
39053 tok = cp_lexer_peek_token (the_parser->lexer);
39054 }
39055 else
39056 ivdep = false;
39057 if (tok->type != CPP_KEYWORD
39058 || (tok->keyword != RID_FOR
39059 && tok->keyword != RID_WHILE
39060 && tok->keyword != RID_DO))
39061 {
39062 cp_parser_error (parser, "for, while or do statement expected");
39063 return false;
39064 }
39065 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39066 return true;
39067 }
39068
39069 default:
39070 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39071 c_invoke_pragma_handler (id);
39072 break;
39073
39074 bad_stmt:
39075 cp_parser_error (parser, "expected declaration specifiers");
39076 break;
39077 }
39078
39079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39080 return false;
39081 }
39082
39083 /* The interface the pragma parsers have to the lexer. */
39084
39085 enum cpp_ttype
39086 pragma_lex (tree *value, location_t *loc)
39087 {
39088 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39089 enum cpp_ttype ret = tok->type;
39090
39091 *value = tok->u.value;
39092 if (loc)
39093 *loc = tok->location;
39094
39095 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39096 ret = CPP_EOF;
39097 else if (ret == CPP_STRING)
39098 *value = cp_parser_string_literal (the_parser, false, false);
39099 else
39100 {
39101 if (ret == CPP_KEYWORD)
39102 ret = CPP_NAME;
39103 cp_lexer_consume_token (the_parser->lexer);
39104 }
39105
39106 return ret;
39107 }
39108
39109 \f
39110 /* External interface. */
39111
39112 /* Parse one entire translation unit. */
39113
39114 void
39115 c_parse_file (void)
39116 {
39117 static bool already_called = false;
39118
39119 if (already_called)
39120 fatal_error (input_location,
39121 "inter-module optimizations not implemented for C++");
39122 already_called = true;
39123
39124 the_parser = cp_parser_new ();
39125 push_deferring_access_checks (flag_access_control
39126 ? dk_no_deferred : dk_no_check);
39127 cp_parser_translation_unit (the_parser);
39128 the_parser = NULL;
39129 }
39130
39131 /* Create an identifier for a generic parameter type (a synthesized
39132 template parameter implied by `auto' or a concept identifier). */
39133
39134 static GTY(()) int generic_parm_count;
39135 static tree
39136 make_generic_type_name ()
39137 {
39138 char buf[32];
39139 sprintf (buf, "auto:%d", ++generic_parm_count);
39140 return get_identifier (buf);
39141 }
39142
39143 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39144 (creating a new template parameter list if necessary). Returns the newly
39145 created template type parm. */
39146
39147 static tree
39148 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39149 {
39150 gcc_assert (current_binding_level->kind == sk_function_parms);
39151
39152 /* Before committing to modifying any scope, if we're in an
39153 implicit template scope, and we're trying to synthesize a
39154 constrained parameter, try to find a previous parameter with
39155 the same name. This is the same-type rule for abbreviated
39156 function templates.
39157
39158 NOTE: We can generate implicit parameters when tentatively
39159 parsing a nested name specifier, only to reject that parse
39160 later. However, matching the same template-id as part of a
39161 direct-declarator should generate an identical template
39162 parameter, so this rule will merge them. */
39163 if (parser->implicit_template_scope && constr)
39164 {
39165 tree t = parser->implicit_template_parms;
39166 while (t)
39167 {
39168 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39169 {
39170 tree d = TREE_VALUE (t);
39171 if (TREE_CODE (d) == PARM_DECL)
39172 /* Return the TEMPLATE_PARM_INDEX. */
39173 d = DECL_INITIAL (d);
39174 return d;
39175 }
39176 t = TREE_CHAIN (t);
39177 }
39178 }
39179
39180 /* We are either continuing a function template that already contains implicit
39181 template parameters, creating a new fully-implicit function template, or
39182 extending an existing explicit function template with implicit template
39183 parameters. */
39184
39185 cp_binding_level *const entry_scope = current_binding_level;
39186
39187 bool become_template = false;
39188 cp_binding_level *parent_scope = 0;
39189
39190 if (parser->implicit_template_scope)
39191 {
39192 gcc_assert (parser->implicit_template_parms);
39193
39194 current_binding_level = parser->implicit_template_scope;
39195 }
39196 else
39197 {
39198 /* Roll back to the existing template parameter scope (in the case of
39199 extending an explicit function template) or introduce a new template
39200 parameter scope ahead of the function parameter scope (or class scope
39201 in the case of out-of-line member definitions). The function scope is
39202 added back after template parameter synthesis below. */
39203
39204 cp_binding_level *scope = entry_scope;
39205
39206 while (scope->kind == sk_function_parms)
39207 {
39208 parent_scope = scope;
39209 scope = scope->level_chain;
39210 }
39211 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39212 {
39213 /* If not defining a class, then any class scope is a scope level in
39214 an out-of-line member definition. In this case simply wind back
39215 beyond the first such scope to inject the template parameter list.
39216 Otherwise wind back to the class being defined. The latter can
39217 occur in class member friend declarations such as:
39218
39219 class A {
39220 void foo (auto);
39221 };
39222 class B {
39223 friend void A::foo (auto);
39224 };
39225
39226 The template parameter list synthesized for the friend declaration
39227 must be injected in the scope of 'B'. This can also occur in
39228 erroneous cases such as:
39229
39230 struct A {
39231 struct B {
39232 void foo (auto);
39233 };
39234 void B::foo (auto) {}
39235 };
39236
39237 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39238 but, nevertheless, the template parameter list synthesized for the
39239 declarator should be injected into the scope of 'A' as if the
39240 ill-formed template was specified explicitly. */
39241
39242 while (scope->kind == sk_class && !scope->defining_class_p)
39243 {
39244 parent_scope = scope;
39245 scope = scope->level_chain;
39246 }
39247 }
39248
39249 current_binding_level = scope;
39250
39251 if (scope->kind != sk_template_parms
39252 || !function_being_declared_is_template_p (parser))
39253 {
39254 /* Introduce a new template parameter list for implicit template
39255 parameters. */
39256
39257 become_template = true;
39258
39259 parser->implicit_template_scope
39260 = begin_scope (sk_template_parms, NULL);
39261
39262 ++processing_template_decl;
39263
39264 parser->fully_implicit_function_template_p = true;
39265 ++parser->num_template_parameter_lists;
39266 }
39267 else
39268 {
39269 /* Synthesize implicit template parameters at the end of the explicit
39270 template parameter list. */
39271
39272 gcc_assert (current_template_parms);
39273
39274 parser->implicit_template_scope = scope;
39275
39276 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39277 parser->implicit_template_parms
39278 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39279 }
39280 }
39281
39282 /* Synthesize a new template parameter and track the current template
39283 parameter chain with implicit_template_parms. */
39284
39285 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39286 tree synth_id = make_generic_type_name ();
39287 tree synth_tmpl_parm;
39288 bool non_type = false;
39289
39290 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39291 synth_tmpl_parm
39292 = finish_template_type_parm (class_type_node, synth_id);
39293 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39294 synth_tmpl_parm
39295 = finish_constrained_template_template_parm (proto, synth_id);
39296 else
39297 {
39298 synth_tmpl_parm = copy_decl (proto);
39299 DECL_NAME (synth_tmpl_parm) = synth_id;
39300 non_type = true;
39301 }
39302
39303 // Attach the constraint to the parm before processing.
39304 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39305 TREE_TYPE (node) = constr;
39306 tree new_parm
39307 = process_template_parm (parser->implicit_template_parms,
39308 input_location,
39309 node,
39310 /*non_type=*/non_type,
39311 /*param_pack=*/false);
39312
39313 // Chain the new parameter to the list of implicit parameters.
39314 if (parser->implicit_template_parms)
39315 parser->implicit_template_parms
39316 = TREE_CHAIN (parser->implicit_template_parms);
39317 else
39318 parser->implicit_template_parms = new_parm;
39319
39320 tree new_decl = get_local_decls ();
39321 if (non_type)
39322 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39323 new_decl = DECL_INITIAL (new_decl);
39324
39325 /* If creating a fully implicit function template, start the new implicit
39326 template parameter list with this synthesized type, otherwise grow the
39327 current template parameter list. */
39328
39329 if (become_template)
39330 {
39331 parent_scope->level_chain = current_binding_level;
39332
39333 tree new_parms = make_tree_vec (1);
39334 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39335 current_template_parms = tree_cons (size_int (processing_template_decl),
39336 new_parms, current_template_parms);
39337 }
39338 else
39339 {
39340 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39341 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39342 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39343 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39344 }
39345
39346 // If the new parameter was constrained, we need to add that to the
39347 // constraints in the template parameter list.
39348 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39349 {
39350 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39351 reqs = conjoin_constraints (reqs, req);
39352 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39353 }
39354
39355 current_binding_level = entry_scope;
39356
39357 return new_decl;
39358 }
39359
39360 /* Finish the declaration of a fully implicit function template. Such a
39361 template has no explicit template parameter list so has not been through the
39362 normal template head and tail processing. synthesize_implicit_template_parm
39363 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39364 provided if the declaration is a class member such that its template
39365 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39366 form is returned. Otherwise NULL_TREE is returned. */
39367
39368 static tree
39369 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39370 {
39371 gcc_assert (parser->fully_implicit_function_template_p);
39372
39373 if (member_decl_opt && member_decl_opt != error_mark_node
39374 && DECL_VIRTUAL_P (member_decl_opt))
39375 {
39376 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39377 "implicit templates may not be %<virtual%>");
39378 DECL_VIRTUAL_P (member_decl_opt) = false;
39379 }
39380
39381 if (member_decl_opt)
39382 member_decl_opt = finish_member_template_decl (member_decl_opt);
39383 end_template_decl ();
39384
39385 parser->fully_implicit_function_template_p = false;
39386 parser->implicit_template_parms = 0;
39387 parser->implicit_template_scope = 0;
39388 --parser->num_template_parameter_lists;
39389
39390 return member_decl_opt;
39391 }
39392
39393 /* Like finish_fully_implicit_template, but to be used in error
39394 recovery, rearranging scopes so that we restore the state we had
39395 before synthesize_implicit_template_parm inserted the implement
39396 template parms scope. */
39397
39398 static void
39399 abort_fully_implicit_template (cp_parser *parser)
39400 {
39401 cp_binding_level *return_to_scope = current_binding_level;
39402
39403 if (parser->implicit_template_scope
39404 && return_to_scope != parser->implicit_template_scope)
39405 {
39406 cp_binding_level *child = return_to_scope;
39407 for (cp_binding_level *scope = child->level_chain;
39408 scope != parser->implicit_template_scope;
39409 scope = child->level_chain)
39410 child = scope;
39411 child->level_chain = parser->implicit_template_scope->level_chain;
39412 parser->implicit_template_scope->level_chain = return_to_scope;
39413 current_binding_level = parser->implicit_template_scope;
39414 }
39415 else
39416 return_to_scope = return_to_scope->level_chain;
39417
39418 finish_fully_implicit_template (parser, NULL);
39419
39420 gcc_assert (current_binding_level == return_to_scope);
39421 }
39422
39423 /* Helper function for diagnostics that have complained about things
39424 being used with 'extern "C"' linkage.
39425
39426 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39427
39428 void
39429 maybe_show_extern_c_location (void)
39430 {
39431 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39432 inform (the_parser->innermost_linkage_specification_location,
39433 "%<extern \"C\"%> linkage started here");
39434 }
39435
39436 #include "gt-cp-parser.h"