359460cd4d8d633c9cb84e17c23c72e6a6d7054f
[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 *, bool *);
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 *);
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
2268 /* Classes [gram.class] */
2269
2270 static tree cp_parser_class_name
2271 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2272 static tree cp_parser_class_specifier
2273 (cp_parser *);
2274 static tree cp_parser_class_head
2275 (cp_parser *, bool *);
2276 static enum tag_types cp_parser_class_key
2277 (cp_parser *);
2278 static void cp_parser_type_parameter_key
2279 (cp_parser* parser);
2280 static void cp_parser_member_specification_opt
2281 (cp_parser *);
2282 static void cp_parser_member_declaration
2283 (cp_parser *);
2284 static tree cp_parser_pure_specifier
2285 (cp_parser *);
2286 static tree cp_parser_constant_initializer
2287 (cp_parser *);
2288
2289 /* Derived classes [gram.class.derived] */
2290
2291 static tree cp_parser_base_clause
2292 (cp_parser *);
2293 static tree cp_parser_base_specifier
2294 (cp_parser *);
2295
2296 /* Special member functions [gram.special] */
2297
2298 static tree cp_parser_conversion_function_id
2299 (cp_parser *);
2300 static tree cp_parser_conversion_type_id
2301 (cp_parser *);
2302 static cp_declarator *cp_parser_conversion_declarator_opt
2303 (cp_parser *);
2304 static void cp_parser_ctor_initializer_opt
2305 (cp_parser *);
2306 static void cp_parser_mem_initializer_list
2307 (cp_parser *);
2308 static tree cp_parser_mem_initializer
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer_id
2311 (cp_parser *);
2312
2313 /* Overloading [gram.over] */
2314
2315 static cp_expr cp_parser_operator_function_id
2316 (cp_parser *);
2317 static cp_expr cp_parser_operator
2318 (cp_parser *);
2319
2320 /* Templates [gram.temp] */
2321
2322 static void cp_parser_template_declaration
2323 (cp_parser *, bool);
2324 static tree cp_parser_template_parameter_list
2325 (cp_parser *);
2326 static tree cp_parser_template_parameter
2327 (cp_parser *, bool *, bool *);
2328 static tree cp_parser_type_parameter
2329 (cp_parser *, bool *);
2330 static tree cp_parser_template_id
2331 (cp_parser *, bool, bool, enum tag_types, bool);
2332 static tree cp_parser_template_name
2333 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2334 static tree cp_parser_template_argument_list
2335 (cp_parser *);
2336 static tree cp_parser_template_argument
2337 (cp_parser *);
2338 static void cp_parser_explicit_instantiation
2339 (cp_parser *);
2340 static void cp_parser_explicit_specialization
2341 (cp_parser *);
2342
2343 /* Exception handling [gram.exception] */
2344
2345 static tree cp_parser_try_block
2346 (cp_parser *);
2347 static void cp_parser_function_try_block
2348 (cp_parser *);
2349 static void cp_parser_handler_seq
2350 (cp_parser *);
2351 static void cp_parser_handler
2352 (cp_parser *);
2353 static tree cp_parser_exception_declaration
2354 (cp_parser *);
2355 static tree cp_parser_throw_expression
2356 (cp_parser *);
2357 static tree cp_parser_exception_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_type_id_list
2360 (cp_parser *);
2361
2362 /* GNU Extensions */
2363
2364 static tree cp_parser_asm_specification_opt
2365 (cp_parser *);
2366 static tree cp_parser_asm_operand_list
2367 (cp_parser *);
2368 static tree cp_parser_asm_clobber_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_label_list
2371 (cp_parser *);
2372 static bool cp_next_tokens_can_be_attribute_p
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_gnu_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_std_attribute_p
2377 (cp_parser *);
2378 static bool cp_nth_tokens_can_be_std_attribute_p
2379 (cp_parser *, size_t);
2380 static bool cp_nth_tokens_can_be_gnu_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_attribute_p
2383 (cp_parser *, size_t);
2384 static tree cp_parser_attributes_opt
2385 (cp_parser *);
2386 static tree cp_parser_gnu_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attribute_list
2389 (cp_parser *);
2390 static tree cp_parser_std_attribute
2391 (cp_parser *, tree);
2392 static tree cp_parser_std_attribute_spec
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute_spec_seq
2395 (cp_parser *);
2396 static size_t cp_parser_skip_attributes_opt
2397 (cp_parser *, size_t);
2398 static bool cp_parser_extension_opt
2399 (cp_parser *, int *);
2400 static void cp_parser_label_declaration
2401 (cp_parser *);
2402
2403 /* Concept Extensions */
2404
2405 static tree cp_parser_requires_clause
2406 (cp_parser *);
2407 static tree cp_parser_requires_clause_opt
2408 (cp_parser *);
2409 static tree cp_parser_requires_expression
2410 (cp_parser *);
2411 static tree cp_parser_requirement_parameter_list
2412 (cp_parser *);
2413 static tree cp_parser_requirement_body
2414 (cp_parser *);
2415 static tree cp_parser_requirement_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement
2418 (cp_parser *);
2419 static tree cp_parser_simple_requirement
2420 (cp_parser *);
2421 static tree cp_parser_compound_requirement
2422 (cp_parser *);
2423 static tree cp_parser_type_requirement
2424 (cp_parser *);
2425 static tree cp_parser_nested_requirement
2426 (cp_parser *);
2427
2428 /* Transactional Memory Extensions */
2429
2430 static tree cp_parser_transaction
2431 (cp_parser *, cp_token *);
2432 static tree cp_parser_transaction_expression
2433 (cp_parser *, enum rid);
2434 static void cp_parser_function_transaction
2435 (cp_parser *, enum rid);
2436 static tree cp_parser_transaction_cancel
2437 (cp_parser *);
2438
2439 enum pragma_context {
2440 pragma_external,
2441 pragma_member,
2442 pragma_objc_icode,
2443 pragma_stmt,
2444 pragma_compound
2445 };
2446 static bool cp_parser_pragma
2447 (cp_parser *, enum pragma_context, bool *);
2448
2449 /* Objective-C++ Productions */
2450
2451 static tree cp_parser_objc_message_receiver
2452 (cp_parser *);
2453 static tree cp_parser_objc_message_args
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_expression
2456 (cp_parser *);
2457 static cp_expr cp_parser_objc_encode_expression
2458 (cp_parser *);
2459 static tree cp_parser_objc_defs_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_protocol_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_selector_expression
2464 (cp_parser *);
2465 static cp_expr cp_parser_objc_expression
2466 (cp_parser *);
2467 static bool cp_parser_objc_selector_p
2468 (enum cpp_ttype);
2469 static tree cp_parser_objc_selector
2470 (cp_parser *);
2471 static tree cp_parser_objc_protocol_refs_opt
2472 (cp_parser *);
2473 static void cp_parser_objc_declaration
2474 (cp_parser *, tree);
2475 static tree cp_parser_objc_statement
2476 (cp_parser *);
2477 static bool cp_parser_objc_valid_prefix_attributes
2478 (cp_parser *, tree *);
2479 static void cp_parser_objc_at_property_declaration
2480 (cp_parser *) ;
2481 static void cp_parser_objc_at_synthesize_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_dynamic_declaration
2484 (cp_parser *) ;
2485 static tree cp_parser_objc_struct_declaration
2486 (cp_parser *) ;
2487
2488 /* Utility Routines */
2489
2490 static cp_expr cp_parser_lookup_name
2491 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2492 static tree cp_parser_lookup_name_simple
2493 (cp_parser *, tree, location_t);
2494 static tree cp_parser_maybe_treat_template_as_class
2495 (tree, bool);
2496 static bool cp_parser_check_declarator_template_parameters
2497 (cp_parser *, cp_declarator *, location_t);
2498 static bool cp_parser_check_template_parameters
2499 (cp_parser *, unsigned, location_t, cp_declarator *);
2500 static cp_expr cp_parser_simple_cast_expression
2501 (cp_parser *);
2502 static tree cp_parser_global_scope_opt
2503 (cp_parser *, bool);
2504 static bool cp_parser_constructor_declarator_p
2505 (cp_parser *, bool);
2506 static tree cp_parser_function_definition_from_specifiers_and_declarator
2507 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2508 static tree cp_parser_function_definition_after_declarator
2509 (cp_parser *, bool);
2510 static bool cp_parser_template_declaration_after_export
2511 (cp_parser *, bool);
2512 static void cp_parser_perform_template_parameter_access_checks
2513 (vec<deferred_access_check, va_gc> *);
2514 static tree cp_parser_single_declaration
2515 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2516 static cp_expr cp_parser_functional_cast
2517 (cp_parser *, tree);
2518 static tree cp_parser_save_member_function_body
2519 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2520 static tree cp_parser_save_nsdmi
2521 (cp_parser *);
2522 static tree cp_parser_enclosed_template_argument_list
2523 (cp_parser *);
2524 static void cp_parser_save_default_args
2525 (cp_parser *, tree);
2526 static void cp_parser_late_parsing_for_member
2527 (cp_parser *, tree);
2528 static tree cp_parser_late_parse_one_default_arg
2529 (cp_parser *, tree, tree, tree);
2530 static void cp_parser_late_parsing_nsdmi
2531 (cp_parser *, tree);
2532 static void cp_parser_late_parsing_default_args
2533 (cp_parser *, tree);
2534 static tree cp_parser_sizeof_operand
2535 (cp_parser *, enum rid);
2536 static cp_expr cp_parser_trait_expr
2537 (cp_parser *, enum rid);
2538 static bool cp_parser_declares_only_class_p
2539 (cp_parser *);
2540 static void cp_parser_set_storage_class
2541 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2542 static void cp_parser_set_decl_spec_type
2543 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2544 static void set_and_check_decl_spec_loc
2545 (cp_decl_specifier_seq *decl_specs,
2546 cp_decl_spec ds, cp_token *);
2547 static bool cp_parser_friend_p
2548 (const cp_decl_specifier_seq *);
2549 static void cp_parser_required_error
2550 (cp_parser *, required_token, bool, location_t);
2551 static cp_token *cp_parser_require
2552 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2553 static cp_token *cp_parser_require_keyword
2554 (cp_parser *, enum rid, required_token);
2555 static bool cp_parser_token_starts_function_definition_p
2556 (cp_token *);
2557 static bool cp_parser_next_token_starts_class_definition_p
2558 (cp_parser *);
2559 static bool cp_parser_next_token_ends_template_argument_p
2560 (cp_parser *);
2561 static bool cp_parser_nth_token_starts_template_argument_list_p
2562 (cp_parser *, size_t);
2563 static enum tag_types cp_parser_token_is_class_key
2564 (cp_token *);
2565 static enum tag_types cp_parser_token_is_type_parameter_key
2566 (cp_token *);
2567 static void cp_parser_check_class_key
2568 (enum tag_types, tree type);
2569 static void cp_parser_check_access_in_redeclaration
2570 (tree type, location_t location);
2571 static bool cp_parser_optional_template_keyword
2572 (cp_parser *);
2573 static void cp_parser_pre_parsed_nested_name_specifier
2574 (cp_parser *);
2575 static bool cp_parser_cache_group
2576 (cp_parser *, enum cpp_ttype, unsigned);
2577 static tree cp_parser_cache_defarg
2578 (cp_parser *parser, bool nsdmi);
2579 static void cp_parser_parse_tentatively
2580 (cp_parser *);
2581 static void cp_parser_commit_to_tentative_parse
2582 (cp_parser *);
2583 static void cp_parser_commit_to_topmost_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_abort_tentative_parse
2586 (cp_parser *);
2587 static bool cp_parser_parse_definitely
2588 (cp_parser *);
2589 static inline bool cp_parser_parsing_tentatively
2590 (cp_parser *);
2591 static bool cp_parser_uncommitted_to_tentative_parse_p
2592 (cp_parser *);
2593 static void cp_parser_error
2594 (cp_parser *, const char *);
2595 static void cp_parser_name_lookup_error
2596 (cp_parser *, tree, tree, name_lookup_error, location_t);
2597 static bool cp_parser_simulate_error
2598 (cp_parser *);
2599 static bool cp_parser_check_type_definition
2600 (cp_parser *);
2601 static void cp_parser_check_for_definition_in_return_type
2602 (cp_declarator *, tree, location_t type_location);
2603 static void cp_parser_check_for_invalid_template_id
2604 (cp_parser *, tree, enum tag_types, location_t location);
2605 static bool cp_parser_non_integral_constant_expression
2606 (cp_parser *, non_integral_constant);
2607 static void cp_parser_diagnose_invalid_type_name
2608 (cp_parser *, tree, location_t);
2609 static bool cp_parser_parse_and_diagnose_invalid_type_name
2610 (cp_parser *);
2611 static int cp_parser_skip_to_closing_parenthesis
2612 (cp_parser *, bool, bool, bool);
2613 static void cp_parser_skip_to_end_of_statement
2614 (cp_parser *);
2615 static void cp_parser_consume_semicolon_at_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_skip_to_end_of_block_or_statement
2618 (cp_parser *);
2619 static bool cp_parser_skip_to_closing_brace
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_template_parameter_list
2622 (cp_parser *);
2623 static void cp_parser_skip_to_pragma_eol
2624 (cp_parser*, cp_token *);
2625 static bool cp_parser_error_occurred
2626 (cp_parser *);
2627 static bool cp_parser_allow_gnu_extensions_p
2628 (cp_parser *);
2629 static bool cp_parser_is_pure_string_literal
2630 (cp_token *);
2631 static bool cp_parser_is_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_keyword
2634 (cp_token *, enum rid);
2635 static tree cp_parser_make_typename_type
2636 (cp_parser *, tree, location_t location);
2637 static cp_declarator * cp_parser_make_indirect_declarator
2638 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2639 static bool cp_parser_compound_literal_p
2640 (cp_parser *);
2641 static bool cp_parser_array_designator_p
2642 (cp_parser *);
2643 static bool cp_parser_init_statement_p
2644 (cp_parser *);
2645 static bool cp_parser_skip_to_closing_square_bracket
2646 (cp_parser *);
2647
2648 /* Concept-related syntactic transformations */
2649
2650 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2651 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2652
2653 // -------------------------------------------------------------------------- //
2654 // Unevaluated Operand Guard
2655 //
2656 // Implementation of an RAII helper for unevaluated operand parsing.
2657 cp_unevaluated::cp_unevaluated ()
2658 {
2659 ++cp_unevaluated_operand;
2660 ++c_inhibit_evaluation_warnings;
2661 }
2662
2663 cp_unevaluated::~cp_unevaluated ()
2664 {
2665 --c_inhibit_evaluation_warnings;
2666 --cp_unevaluated_operand;
2667 }
2668
2669 // -------------------------------------------------------------------------- //
2670 // Tentative Parsing
2671
2672 /* Returns nonzero if we are parsing tentatively. */
2673
2674 static inline bool
2675 cp_parser_parsing_tentatively (cp_parser* parser)
2676 {
2677 return parser->context->next != NULL;
2678 }
2679
2680 /* Returns nonzero if TOKEN is a string literal. */
2681
2682 static bool
2683 cp_parser_is_pure_string_literal (cp_token* token)
2684 {
2685 return (token->type == CPP_STRING ||
2686 token->type == CPP_STRING16 ||
2687 token->type == CPP_STRING32 ||
2688 token->type == CPP_WSTRING ||
2689 token->type == CPP_UTF8STRING);
2690 }
2691
2692 /* Returns nonzero if TOKEN is a string literal
2693 of a user-defined string literal. */
2694
2695 static bool
2696 cp_parser_is_string_literal (cp_token* token)
2697 {
2698 return (cp_parser_is_pure_string_literal (token) ||
2699 token->type == CPP_STRING_USERDEF ||
2700 token->type == CPP_STRING16_USERDEF ||
2701 token->type == CPP_STRING32_USERDEF ||
2702 token->type == CPP_WSTRING_USERDEF ||
2703 token->type == CPP_UTF8STRING_USERDEF);
2704 }
2705
2706 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2707
2708 static bool
2709 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2710 {
2711 return token->keyword == keyword;
2712 }
2713
2714 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2715 PRAGMA_NONE. */
2716
2717 static enum pragma_kind
2718 cp_parser_pragma_kind (cp_token *token)
2719 {
2720 if (token->type != CPP_PRAGMA)
2721 return PRAGMA_NONE;
2722 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2723 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2724 }
2725
2726 /* Helper function for cp_parser_error.
2727 Having peeked a token of kind TOK1_KIND that might signify
2728 a conflict marker, peek successor tokens to determine
2729 if we actually do have a conflict marker.
2730 Specifically, we consider a run of 7 '<', '=' or '>' characters
2731 at the start of a line as a conflict marker.
2732 These come through the lexer as three pairs and a single,
2733 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2734 If it returns true, *OUT_LOC is written to with the location/range
2735 of the marker. */
2736
2737 static bool
2738 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2739 location_t *out_loc)
2740 {
2741 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2742 if (token2->type != tok1_kind)
2743 return false;
2744 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2745 if (token3->type != tok1_kind)
2746 return false;
2747 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2748 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2749 return false;
2750
2751 /* It must be at the start of the line. */
2752 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2753 if (LOCATION_COLUMN (start_loc) != 1)
2754 return false;
2755
2756 /* We have a conflict marker. Construct a location of the form:
2757 <<<<<<<
2758 ^~~~~~~
2759 with start == caret, finishing at the end of the marker. */
2760 location_t finish_loc = get_finish (token4->location);
2761 *out_loc = make_location (start_loc, start_loc, finish_loc);
2762
2763 return true;
2764 }
2765
2766 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2767 RT_CLOSE_PAREN. */
2768
2769 static const char *
2770 get_matching_symbol (required_token token_desc)
2771 {
2772 switch (token_desc)
2773 {
2774 default:
2775 gcc_unreachable ();
2776 return "";
2777 case RT_CLOSE_BRACE:
2778 return "{";
2779 case RT_CLOSE_PAREN:
2780 return "(";
2781 }
2782 }
2783
2784 /* Attempt to convert TOKEN_DESC from a required_token to an
2785 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2786
2787 static enum cpp_ttype
2788 get_required_cpp_ttype (required_token token_desc)
2789 {
2790 switch (token_desc)
2791 {
2792 case RT_SEMICOLON:
2793 return CPP_SEMICOLON;
2794 case RT_OPEN_PAREN:
2795 return CPP_OPEN_PAREN;
2796 case RT_CLOSE_BRACE:
2797 return CPP_CLOSE_BRACE;
2798 case RT_OPEN_BRACE:
2799 return CPP_OPEN_BRACE;
2800 case RT_CLOSE_SQUARE:
2801 return CPP_CLOSE_SQUARE;
2802 case RT_OPEN_SQUARE:
2803 return CPP_OPEN_SQUARE;
2804 case RT_COMMA:
2805 return CPP_COMMA;
2806 case RT_COLON:
2807 return CPP_COLON;
2808 case RT_CLOSE_PAREN:
2809 return CPP_CLOSE_PAREN;
2810
2811 default:
2812 /* Use CPP_EOF as a "no completions possible" code. */
2813 return CPP_EOF;
2814 }
2815 }
2816
2817
2818 /* Subroutine of cp_parser_error and cp_parser_required_error.
2819
2820 Issue a diagnostic of the form
2821 FILE:LINE: MESSAGE before TOKEN
2822 where TOKEN is the next token in the input stream. MESSAGE
2823 (specified by the caller) is usually of the form "expected
2824 OTHER-TOKEN".
2825
2826 This bypasses the check for tentative passing, and potentially
2827 adds material needed by cp_parser_required_error.
2828
2829 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2830 suggesting insertion of the missing token.
2831
2832 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2833 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2834 location. */
2835
2836 static void
2837 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2838 required_token missing_token_desc,
2839 location_t matching_location)
2840 {
2841 cp_token *token = cp_lexer_peek_token (parser->lexer);
2842 /* This diagnostic makes more sense if it is tagged to the line
2843 of the token we just peeked at. */
2844 cp_lexer_set_source_position_from_token (token);
2845
2846 if (token->type == CPP_PRAGMA)
2847 {
2848 error_at (token->location,
2849 "%<#pragma%> is not allowed here");
2850 cp_parser_skip_to_pragma_eol (parser, token);
2851 return;
2852 }
2853
2854 /* If this is actually a conflict marker, report it as such. */
2855 if (token->type == CPP_LSHIFT
2856 || token->type == CPP_RSHIFT
2857 || token->type == CPP_EQ_EQ)
2858 {
2859 location_t loc;
2860 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2861 {
2862 error_at (loc, "version control conflict marker in file");
2863 return;
2864 }
2865 }
2866
2867 gcc_rich_location richloc (input_location);
2868
2869 bool added_matching_location = false;
2870
2871 if (missing_token_desc != RT_NONE)
2872 {
2873 /* Potentially supply a fix-it hint, suggesting to add the
2874 missing token immediately after the *previous* token.
2875 This may move the primary location within richloc. */
2876 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2877 location_t prev_token_loc
2878 = cp_lexer_previous_token (parser->lexer)->location;
2879 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2880
2881 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2882 Attempt to consolidate diagnostics by printing it as a
2883 secondary range within the main diagnostic. */
2884 if (matching_location != UNKNOWN_LOCATION)
2885 added_matching_location
2886 = richloc.add_location_if_nearby (matching_location);
2887 }
2888
2889 /* Actually emit the error. */
2890 c_parse_error (gmsgid,
2891 /* Because c_parser_error does not understand
2892 CPP_KEYWORD, keywords are treated like
2893 identifiers. */
2894 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2895 token->u.value, token->flags, &richloc);
2896
2897 if (missing_token_desc != RT_NONE)
2898 {
2899 /* If we weren't able to consolidate matching_location, then
2900 print it as a secondary diagnostic. */
2901 if (matching_location != UNKNOWN_LOCATION
2902 && !added_matching_location)
2903 inform (matching_location, "to match this %qs",
2904 get_matching_symbol (missing_token_desc));
2905 }
2906 }
2907
2908 /* If not parsing tentatively, issue a diagnostic of the form
2909 FILE:LINE: MESSAGE before TOKEN
2910 where TOKEN is the next token in the input stream. MESSAGE
2911 (specified by the caller) is usually of the form "expected
2912 OTHER-TOKEN". */
2913
2914 static void
2915 cp_parser_error (cp_parser* parser, const char* gmsgid)
2916 {
2917 if (!cp_parser_simulate_error (parser))
2918 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2919 }
2920
2921 /* Issue an error about name-lookup failing. NAME is the
2922 IDENTIFIER_NODE DECL is the result of
2923 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2924 the thing that we hoped to find. */
2925
2926 static void
2927 cp_parser_name_lookup_error (cp_parser* parser,
2928 tree name,
2929 tree decl,
2930 name_lookup_error desired,
2931 location_t location)
2932 {
2933 /* If name lookup completely failed, tell the user that NAME was not
2934 declared. */
2935 if (decl == error_mark_node)
2936 {
2937 if (parser->scope && parser->scope != global_namespace)
2938 error_at (location, "%<%E::%E%> has not been declared",
2939 parser->scope, name);
2940 else if (parser->scope == global_namespace)
2941 error_at (location, "%<::%E%> has not been declared", name);
2942 else if (parser->object_scope
2943 && !CLASS_TYPE_P (parser->object_scope))
2944 error_at (location, "request for member %qE in non-class type %qT",
2945 name, parser->object_scope);
2946 else if (parser->object_scope)
2947 error_at (location, "%<%T::%E%> has not been declared",
2948 parser->object_scope, name);
2949 else
2950 error_at (location, "%qE has not been declared", name);
2951 }
2952 else if (parser->scope && parser->scope != global_namespace)
2953 {
2954 switch (desired)
2955 {
2956 case NLE_TYPE:
2957 error_at (location, "%<%E::%E%> is not a type",
2958 parser->scope, name);
2959 break;
2960 case NLE_CXX98:
2961 error_at (location, "%<%E::%E%> is not a class or namespace",
2962 parser->scope, name);
2963 break;
2964 case NLE_NOT_CXX98:
2965 error_at (location,
2966 "%<%E::%E%> is not a class, namespace, or enumeration",
2967 parser->scope, name);
2968 break;
2969 default:
2970 gcc_unreachable ();
2971
2972 }
2973 }
2974 else if (parser->scope == global_namespace)
2975 {
2976 switch (desired)
2977 {
2978 case NLE_TYPE:
2979 error_at (location, "%<::%E%> is not a type", name);
2980 break;
2981 case NLE_CXX98:
2982 error_at (location, "%<::%E%> is not a class or namespace", name);
2983 break;
2984 case NLE_NOT_CXX98:
2985 error_at (location,
2986 "%<::%E%> is not a class, namespace, or enumeration",
2987 name);
2988 break;
2989 default:
2990 gcc_unreachable ();
2991 }
2992 }
2993 else
2994 {
2995 switch (desired)
2996 {
2997 case NLE_TYPE:
2998 error_at (location, "%qE is not a type", name);
2999 break;
3000 case NLE_CXX98:
3001 error_at (location, "%qE is not a class or namespace", name);
3002 break;
3003 case NLE_NOT_CXX98:
3004 error_at (location,
3005 "%qE is not a class, namespace, or enumeration", name);
3006 break;
3007 default:
3008 gcc_unreachable ();
3009 }
3010 }
3011 }
3012
3013 /* If we are parsing tentatively, remember that an error has occurred
3014 during this tentative parse. Returns true if the error was
3015 simulated; false if a message should be issued by the caller. */
3016
3017 static bool
3018 cp_parser_simulate_error (cp_parser* parser)
3019 {
3020 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3021 {
3022 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3023 return true;
3024 }
3025 return false;
3026 }
3027
3028 /* This function is called when a type is defined. If type
3029 definitions are forbidden at this point, an error message is
3030 issued. */
3031
3032 static bool
3033 cp_parser_check_type_definition (cp_parser* parser)
3034 {
3035 /* If types are forbidden here, issue a message. */
3036 if (parser->type_definition_forbidden_message)
3037 {
3038 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3039 in the message need to be interpreted. */
3040 error (parser->type_definition_forbidden_message);
3041 return false;
3042 }
3043 return true;
3044 }
3045
3046 /* This function is called when the DECLARATOR is processed. The TYPE
3047 was a type defined in the decl-specifiers. If it is invalid to
3048 define a type in the decl-specifiers for DECLARATOR, an error is
3049 issued. TYPE_LOCATION is the location of TYPE and is used
3050 for error reporting. */
3051
3052 static void
3053 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3054 tree type, location_t type_location)
3055 {
3056 /* [dcl.fct] forbids type definitions in return types.
3057 Unfortunately, it's not easy to know whether or not we are
3058 processing a return type until after the fact. */
3059 while (declarator
3060 && (declarator->kind == cdk_pointer
3061 || declarator->kind == cdk_reference
3062 || declarator->kind == cdk_ptrmem))
3063 declarator = declarator->declarator;
3064 if (declarator
3065 && declarator->kind == cdk_function)
3066 {
3067 error_at (type_location,
3068 "new types may not be defined in a return type");
3069 inform (type_location,
3070 "(perhaps a semicolon is missing after the definition of %qT)",
3071 type);
3072 }
3073 }
3074
3075 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3076 "<" in any valid C++ program. If the next token is indeed "<",
3077 issue a message warning the user about what appears to be an
3078 invalid attempt to form a template-id. LOCATION is the location
3079 of the type-specifier (TYPE) */
3080
3081 static void
3082 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3083 tree type,
3084 enum tag_types tag_type,
3085 location_t location)
3086 {
3087 cp_token_position start = 0;
3088
3089 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3090 {
3091 if (TREE_CODE (type) == TYPE_DECL)
3092 type = TREE_TYPE (type);
3093 if (TYPE_P (type) && !template_placeholder_p (type))
3094 error_at (location, "%qT is not a template", type);
3095 else if (identifier_p (type))
3096 {
3097 if (tag_type != none_type)
3098 error_at (location, "%qE is not a class template", type);
3099 else
3100 error_at (location, "%qE is not a template", type);
3101 }
3102 else
3103 error_at (location, "invalid template-id");
3104 /* Remember the location of the invalid "<". */
3105 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3106 start = cp_lexer_token_position (parser->lexer, true);
3107 /* Consume the "<". */
3108 cp_lexer_consume_token (parser->lexer);
3109 /* Parse the template arguments. */
3110 cp_parser_enclosed_template_argument_list (parser);
3111 /* Permanently remove the invalid template arguments so that
3112 this error message is not issued again. */
3113 if (start)
3114 cp_lexer_purge_tokens_after (parser->lexer, start);
3115 }
3116 }
3117
3118 /* If parsing an integral constant-expression, issue an error message
3119 about the fact that THING appeared and return true. Otherwise,
3120 return false. In either case, set
3121 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3122
3123 static bool
3124 cp_parser_non_integral_constant_expression (cp_parser *parser,
3125 non_integral_constant thing)
3126 {
3127 parser->non_integral_constant_expression_p = true;
3128 if (parser->integral_constant_expression_p)
3129 {
3130 if (!parser->allow_non_integral_constant_expression_p)
3131 {
3132 const char *msg = NULL;
3133 switch (thing)
3134 {
3135 case NIC_FLOAT:
3136 pedwarn (input_location, OPT_Wpedantic,
3137 "ISO C++ forbids using a floating-point literal "
3138 "in a constant-expression");
3139 return true;
3140 case NIC_CAST:
3141 error ("a cast to a type other than an integral or "
3142 "enumeration type cannot appear in a "
3143 "constant-expression");
3144 return true;
3145 case NIC_TYPEID:
3146 error ("%<typeid%> operator "
3147 "cannot appear in a constant-expression");
3148 return true;
3149 case NIC_NCC:
3150 error ("non-constant compound literals "
3151 "cannot appear in a constant-expression");
3152 return true;
3153 case NIC_FUNC_CALL:
3154 error ("a function call "
3155 "cannot appear in a constant-expression");
3156 return true;
3157 case NIC_INC:
3158 error ("an increment "
3159 "cannot appear in a constant-expression");
3160 return true;
3161 case NIC_DEC:
3162 error ("an decrement "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_ARRAY_REF:
3166 error ("an array reference "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_ADDR_LABEL:
3170 error ("the address of a label "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_OVERLOADED:
3174 error ("calls to overloaded operators "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_ASSIGNMENT:
3178 error ("an assignment cannot appear in a constant-expression");
3179 return true;
3180 case NIC_COMMA:
3181 error ("a comma operator "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_CONSTRUCTOR:
3185 error ("a call to a constructor "
3186 "cannot appear in a constant-expression");
3187 return true;
3188 case NIC_TRANSACTION:
3189 error ("a transaction expression "
3190 "cannot appear in a constant-expression");
3191 return true;
3192 case NIC_THIS:
3193 msg = "this";
3194 break;
3195 case NIC_FUNC_NAME:
3196 msg = "__FUNCTION__";
3197 break;
3198 case NIC_PRETTY_FUNC:
3199 msg = "__PRETTY_FUNCTION__";
3200 break;
3201 case NIC_C99_FUNC:
3202 msg = "__func__";
3203 break;
3204 case NIC_VA_ARG:
3205 msg = "va_arg";
3206 break;
3207 case NIC_ARROW:
3208 msg = "->";
3209 break;
3210 case NIC_POINT:
3211 msg = ".";
3212 break;
3213 case NIC_STAR:
3214 msg = "*";
3215 break;
3216 case NIC_ADDR:
3217 msg = "&";
3218 break;
3219 case NIC_PREINCREMENT:
3220 msg = "++";
3221 break;
3222 case NIC_PREDECREMENT:
3223 msg = "--";
3224 break;
3225 case NIC_NEW:
3226 msg = "new";
3227 break;
3228 case NIC_DEL:
3229 msg = "delete";
3230 break;
3231 default:
3232 gcc_unreachable ();
3233 }
3234 if (msg)
3235 error ("%qs cannot appear in a constant-expression", msg);
3236 return true;
3237 }
3238 }
3239 return false;
3240 }
3241
3242 /* Emit a diagnostic for an invalid type name. This function commits
3243 to the current active tentative parse, if any. (Otherwise, the
3244 problematic construct might be encountered again later, resulting
3245 in duplicate error messages.) LOCATION is the location of ID. */
3246
3247 static void
3248 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3249 location_t location)
3250 {
3251 tree decl, ambiguous_decls;
3252 cp_parser_commit_to_tentative_parse (parser);
3253 /* Try to lookup the identifier. */
3254 decl = cp_parser_lookup_name (parser, id, none_type,
3255 /*is_template=*/false,
3256 /*is_namespace=*/false,
3257 /*check_dependency=*/true,
3258 &ambiguous_decls, location);
3259 if (ambiguous_decls)
3260 /* If the lookup was ambiguous, an error will already have
3261 been issued. */
3262 return;
3263 /* If the lookup found a template-name, it means that the user forgot
3264 to specify an argument list. Emit a useful error message. */
3265 if (DECL_TYPE_TEMPLATE_P (decl))
3266 {
3267 error_at (location,
3268 "invalid use of template-name %qE without an argument list",
3269 decl);
3270 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3271 inform (location, "class template argument deduction is only available "
3272 "with -std=c++17 or -std=gnu++17");
3273 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3274 }
3275 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3276 error_at (location, "invalid use of destructor %qD as a type", id);
3277 else if (TREE_CODE (decl) == TYPE_DECL)
3278 /* Something like 'unsigned A a;' */
3279 error_at (location, "invalid combination of multiple type-specifiers");
3280 else if (!parser->scope)
3281 {
3282 /* Issue an error message. */
3283 name_hint hint;
3284 if (TREE_CODE (id) == IDENTIFIER_NODE)
3285 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3286 if (hint)
3287 {
3288 gcc_rich_location richloc (location);
3289 richloc.add_fixit_replace (hint.suggestion ());
3290 error_at (&richloc,
3291 "%qE does not name a type; did you mean %qs?",
3292 id, hint.suggestion ());
3293 }
3294 else
3295 error_at (location, "%qE does not name a type", id);
3296 /* If we're in a template class, it's possible that the user was
3297 referring to a type from a base class. For example:
3298
3299 template <typename T> struct A { typedef T X; };
3300 template <typename T> struct B : public A<T> { X x; };
3301
3302 The user should have said "typename A<T>::X". */
3303 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3304 inform (location, "C++11 %<constexpr%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3307 inform (location, "C++11 %<noexcept%> only available with "
3308 "-std=c++11 or -std=gnu++11");
3309 else if (cxx_dialect < cxx11
3310 && TREE_CODE (id) == IDENTIFIER_NODE
3311 && id_equal (id, "thread_local"))
3312 inform (location, "C++11 %<thread_local%> only available with "
3313 "-std=c++11 or -std=gnu++11");
3314 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3315 inform (location, "%<concept%> only available with -fconcepts");
3316 else if (processing_template_decl && current_class_type
3317 && TYPE_BINFO (current_class_type))
3318 {
3319 tree b;
3320
3321 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3322 b;
3323 b = TREE_CHAIN (b))
3324 {
3325 tree base_type = BINFO_TYPE (b);
3326 if (CLASS_TYPE_P (base_type)
3327 && dependent_type_p (base_type))
3328 {
3329 tree field;
3330 /* Go from a particular instantiation of the
3331 template (which will have an empty TYPE_FIELDs),
3332 to the main version. */
3333 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3334 for (field = TYPE_FIELDS (base_type);
3335 field;
3336 field = DECL_CHAIN (field))
3337 if (TREE_CODE (field) == TYPE_DECL
3338 && DECL_NAME (field) == id)
3339 {
3340 inform (location,
3341 "(perhaps %<typename %T::%E%> was intended)",
3342 BINFO_TYPE (b), id);
3343 break;
3344 }
3345 if (field)
3346 break;
3347 }
3348 }
3349 }
3350 }
3351 /* Here we diagnose qualified-ids where the scope is actually correct,
3352 but the identifier does not resolve to a valid type name. */
3353 else if (parser->scope != error_mark_node)
3354 {
3355 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3356 {
3357 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3358 error_at (location_of (id),
3359 "%qE in namespace %qE does not name a template type",
3360 id, parser->scope);
3361 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3362 error_at (location_of (id),
3363 "%qE in namespace %qE does not name a template type",
3364 TREE_OPERAND (id, 0), parser->scope);
3365 else
3366 error_at (location_of (id),
3367 "%qE in namespace %qE does not name a type",
3368 id, parser->scope);
3369 if (DECL_P (decl))
3370 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3371 else if (decl == error_mark_node)
3372 suggest_alternative_in_explicit_scope (location, id,
3373 parser->scope);
3374 }
3375 else if (CLASS_TYPE_P (parser->scope)
3376 && constructor_name_p (id, parser->scope))
3377 {
3378 /* A<T>::A<T>() */
3379 error_at (location, "%<%T::%E%> names the constructor, not"
3380 " the type", parser->scope, id);
3381 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3382 error_at (location, "and %qT has no template constructors",
3383 parser->scope);
3384 }
3385 else if (TYPE_P (parser->scope)
3386 && dependent_scope_p (parser->scope))
3387 {
3388 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3389 error_at (location,
3390 "need %<typename%> before %<%T::%D::%E%> because "
3391 "%<%T::%D%> is a dependent scope",
3392 TYPE_CONTEXT (parser->scope),
3393 TYPENAME_TYPE_FULLNAME (parser->scope),
3394 id,
3395 TYPE_CONTEXT (parser->scope),
3396 TYPENAME_TYPE_FULLNAME (parser->scope));
3397 else
3398 error_at (location, "need %<typename%> before %<%T::%E%> because "
3399 "%qT is a dependent scope",
3400 parser->scope, id, parser->scope);
3401 }
3402 else if (TYPE_P (parser->scope))
3403 {
3404 if (!COMPLETE_TYPE_P (parser->scope))
3405 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3406 parser->scope);
3407 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3408 error_at (location_of (id),
3409 "%qE in %q#T does not name a template type",
3410 id, parser->scope);
3411 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3412 error_at (location_of (id),
3413 "%qE in %q#T does not name a template type",
3414 TREE_OPERAND (id, 0), parser->scope);
3415 else
3416 error_at (location_of (id),
3417 "%qE in %q#T does not name a type",
3418 id, parser->scope);
3419 if (DECL_P (decl))
3420 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3421 }
3422 else
3423 gcc_unreachable ();
3424 }
3425 }
3426
3427 /* Check for a common situation where a type-name should be present,
3428 but is not, and issue a sensible error message. Returns true if an
3429 invalid type-name was detected.
3430
3431 The situation handled by this function are variable declarations of the
3432 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3433 Usually, `ID' should name a type, but if we got here it means that it
3434 does not. We try to emit the best possible error message depending on
3435 how exactly the id-expression looks like. */
3436
3437 static bool
3438 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3439 {
3440 tree id;
3441 cp_token *token = cp_lexer_peek_token (parser->lexer);
3442
3443 /* Avoid duplicate error about ambiguous lookup. */
3444 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3445 {
3446 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3447 if (next->type == CPP_NAME && next->error_reported)
3448 goto out;
3449 }
3450
3451 cp_parser_parse_tentatively (parser);
3452 id = cp_parser_id_expression (parser,
3453 /*template_keyword_p=*/false,
3454 /*check_dependency_p=*/true,
3455 /*template_p=*/NULL,
3456 /*declarator_p=*/true,
3457 /*optional_p=*/false);
3458 /* If the next token is a (, this is a function with no explicit return
3459 type, i.e. constructor, destructor or conversion op. */
3460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3461 || TREE_CODE (id) == TYPE_DECL)
3462 {
3463 cp_parser_abort_tentative_parse (parser);
3464 return false;
3465 }
3466 if (!cp_parser_parse_definitely (parser))
3467 return false;
3468
3469 /* Emit a diagnostic for the invalid type. */
3470 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3471 out:
3472 /* If we aren't in the middle of a declarator (i.e. in a
3473 parameter-declaration-clause), skip to the end of the declaration;
3474 there's no point in trying to process it. */
3475 if (!parser->in_declarator_p)
3476 cp_parser_skip_to_end_of_block_or_statement (parser);
3477 return true;
3478 }
3479
3480 /* Consume tokens up to, and including, the next non-nested closing `)'.
3481 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3482 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3483 found an unnested token of that type. */
3484
3485 static int
3486 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3487 bool recovering,
3488 cpp_ttype or_ttype,
3489 bool consume_paren)
3490 {
3491 unsigned paren_depth = 0;
3492 unsigned brace_depth = 0;
3493 unsigned square_depth = 0;
3494
3495 if (recovering && or_ttype == CPP_EOF
3496 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3497 return 0;
3498
3499 while (true)
3500 {
3501 cp_token * token = cp_lexer_peek_token (parser->lexer);
3502
3503 /* Have we found what we're looking for before the closing paren? */
3504 if (token->type == or_ttype && or_ttype != CPP_EOF
3505 && !brace_depth && !paren_depth && !square_depth)
3506 return -1;
3507
3508 switch (token->type)
3509 {
3510 case CPP_EOF:
3511 case CPP_PRAGMA_EOL:
3512 /* If we've run out of tokens, then there is no closing `)'. */
3513 return 0;
3514
3515 /* This is good for lambda expression capture-lists. */
3516 case CPP_OPEN_SQUARE:
3517 ++square_depth;
3518 break;
3519 case CPP_CLOSE_SQUARE:
3520 if (!square_depth--)
3521 return 0;
3522 break;
3523
3524 case CPP_SEMICOLON:
3525 /* This matches the processing in skip_to_end_of_statement. */
3526 if (!brace_depth)
3527 return 0;
3528 break;
3529
3530 case CPP_OPEN_BRACE:
3531 ++brace_depth;
3532 break;
3533 case CPP_CLOSE_BRACE:
3534 if (!brace_depth--)
3535 return 0;
3536 break;
3537
3538 case CPP_OPEN_PAREN:
3539 if (!brace_depth)
3540 ++paren_depth;
3541 break;
3542
3543 case CPP_CLOSE_PAREN:
3544 if (!brace_depth && !paren_depth--)
3545 {
3546 if (consume_paren)
3547 cp_lexer_consume_token (parser->lexer);
3548 return 1;
3549 }
3550 break;
3551
3552 default:
3553 break;
3554 }
3555
3556 /* Consume the token. */
3557 cp_lexer_consume_token (parser->lexer);
3558 }
3559 }
3560
3561 /* Consume tokens up to, and including, the next non-nested closing `)'.
3562 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3563 are doing error recovery. Returns -1 if OR_COMMA is true and we
3564 found an unnested token of that type. */
3565
3566 static int
3567 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3568 bool recovering,
3569 bool or_comma,
3570 bool consume_paren)
3571 {
3572 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3573 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3574 ttype, consume_paren);
3575 }
3576
3577 /* Consume tokens until we reach the end of the current statement.
3578 Normally, that will be just before consuming a `;'. However, if a
3579 non-nested `}' comes first, then we stop before consuming that. */
3580
3581 static void
3582 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3583 {
3584 unsigned nesting_depth = 0;
3585
3586 /* Unwind generic function template scope if necessary. */
3587 if (parser->fully_implicit_function_template_p)
3588 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3589
3590 while (true)
3591 {
3592 cp_token *token = cp_lexer_peek_token (parser->lexer);
3593
3594 switch (token->type)
3595 {
3596 case CPP_EOF:
3597 case CPP_PRAGMA_EOL:
3598 /* If we've run out of tokens, stop. */
3599 return;
3600
3601 case CPP_SEMICOLON:
3602 /* If the next token is a `;', we have reached the end of the
3603 statement. */
3604 if (!nesting_depth)
3605 return;
3606 break;
3607
3608 case CPP_CLOSE_BRACE:
3609 /* If this is a non-nested '}', stop before consuming it.
3610 That way, when confronted with something like:
3611
3612 { 3 + }
3613
3614 we stop before consuming the closing '}', even though we
3615 have not yet reached a `;'. */
3616 if (nesting_depth == 0)
3617 return;
3618
3619 /* If it is the closing '}' for a block that we have
3620 scanned, stop -- but only after consuming the token.
3621 That way given:
3622
3623 void f g () { ... }
3624 typedef int I;
3625
3626 we will stop after the body of the erroneously declared
3627 function, but before consuming the following `typedef'
3628 declaration. */
3629 if (--nesting_depth == 0)
3630 {
3631 cp_lexer_consume_token (parser->lexer);
3632 return;
3633 }
3634 break;
3635
3636 case CPP_OPEN_BRACE:
3637 ++nesting_depth;
3638 break;
3639
3640 default:
3641 break;
3642 }
3643
3644 /* Consume the token. */
3645 cp_lexer_consume_token (parser->lexer);
3646 }
3647 }
3648
3649 /* This function is called at the end of a statement or declaration.
3650 If the next token is a semicolon, it is consumed; otherwise, error
3651 recovery is attempted. */
3652
3653 static void
3654 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3655 {
3656 /* Look for the trailing `;'. */
3657 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3658 {
3659 /* If there is additional (erroneous) input, skip to the end of
3660 the statement. */
3661 cp_parser_skip_to_end_of_statement (parser);
3662 /* If the next token is now a `;', consume it. */
3663 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3664 cp_lexer_consume_token (parser->lexer);
3665 }
3666 }
3667
3668 /* Skip tokens until we have consumed an entire block, or until we
3669 have consumed a non-nested `;'. */
3670
3671 static void
3672 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3673 {
3674 int nesting_depth = 0;
3675
3676 /* Unwind generic function template scope if necessary. */
3677 if (parser->fully_implicit_function_template_p)
3678 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3679
3680 while (nesting_depth >= 0)
3681 {
3682 cp_token *token = cp_lexer_peek_token (parser->lexer);
3683
3684 switch (token->type)
3685 {
3686 case CPP_EOF:
3687 case CPP_PRAGMA_EOL:
3688 /* If we've run out of tokens, stop. */
3689 return;
3690
3691 case CPP_SEMICOLON:
3692 /* Stop if this is an unnested ';'. */
3693 if (!nesting_depth)
3694 nesting_depth = -1;
3695 break;
3696
3697 case CPP_CLOSE_BRACE:
3698 /* Stop if this is an unnested '}', or closes the outermost
3699 nesting level. */
3700 nesting_depth--;
3701 if (nesting_depth < 0)
3702 return;
3703 if (!nesting_depth)
3704 nesting_depth = -1;
3705 break;
3706
3707 case CPP_OPEN_BRACE:
3708 /* Nest. */
3709 nesting_depth++;
3710 break;
3711
3712 default:
3713 break;
3714 }
3715
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser->lexer);
3718 }
3719 }
3720
3721 /* Skip tokens until a non-nested closing curly brace is the next
3722 token, or there are no more tokens. Return true in the first case,
3723 false otherwise. */
3724
3725 static bool
3726 cp_parser_skip_to_closing_brace (cp_parser *parser)
3727 {
3728 unsigned nesting_depth = 0;
3729
3730 while (true)
3731 {
3732 cp_token *token = cp_lexer_peek_token (parser->lexer);
3733
3734 switch (token->type)
3735 {
3736 case CPP_EOF:
3737 case CPP_PRAGMA_EOL:
3738 /* If we've run out of tokens, stop. */
3739 return false;
3740
3741 case CPP_CLOSE_BRACE:
3742 /* If the next token is a non-nested `}', then we have reached
3743 the end of the current block. */
3744 if (nesting_depth-- == 0)
3745 return true;
3746 break;
3747
3748 case CPP_OPEN_BRACE:
3749 /* If it the next token is a `{', then we are entering a new
3750 block. Consume the entire block. */
3751 ++nesting_depth;
3752 break;
3753
3754 default:
3755 break;
3756 }
3757
3758 /* Consume the token. */
3759 cp_lexer_consume_token (parser->lexer);
3760 }
3761 }
3762
3763 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3764 parameter is the PRAGMA token, allowing us to purge the entire pragma
3765 sequence. */
3766
3767 static void
3768 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3769 {
3770 cp_token *token;
3771
3772 parser->lexer->in_pragma = false;
3773
3774 do
3775 token = cp_lexer_consume_token (parser->lexer);
3776 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3777
3778 /* Ensure that the pragma is not parsed again. */
3779 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3780 }
3781
3782 /* Require pragma end of line, resyncing with it as necessary. The
3783 arguments are as for cp_parser_skip_to_pragma_eol. */
3784
3785 static void
3786 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3787 {
3788 parser->lexer->in_pragma = false;
3789 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3790 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3791 }
3792
3793 /* This is a simple wrapper around make_typename_type. When the id is
3794 an unresolved identifier node, we can provide a superior diagnostic
3795 using cp_parser_diagnose_invalid_type_name. */
3796
3797 static tree
3798 cp_parser_make_typename_type (cp_parser *parser, tree id,
3799 location_t id_location)
3800 {
3801 tree result;
3802 if (identifier_p (id))
3803 {
3804 result = make_typename_type (parser->scope, id, typename_type,
3805 /*complain=*/tf_none);
3806 if (result == error_mark_node)
3807 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3808 return result;
3809 }
3810 return make_typename_type (parser->scope, id, typename_type, tf_error);
3811 }
3812
3813 /* This is a wrapper around the
3814 make_{pointer,ptrmem,reference}_declarator functions that decides
3815 which one to call based on the CODE and CLASS_TYPE arguments. The
3816 CODE argument should be one of the values returned by
3817 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3818 appertain to the pointer or reference. */
3819
3820 static cp_declarator *
3821 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3822 cp_cv_quals cv_qualifiers,
3823 cp_declarator *target,
3824 tree attributes)
3825 {
3826 if (code == ERROR_MARK)
3827 return cp_error_declarator;
3828
3829 if (code == INDIRECT_REF)
3830 if (class_type == NULL_TREE)
3831 return make_pointer_declarator (cv_qualifiers, target, attributes);
3832 else
3833 return make_ptrmem_declarator (cv_qualifiers, class_type,
3834 target, attributes);
3835 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3836 return make_reference_declarator (cv_qualifiers, target,
3837 false, attributes);
3838 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3839 return make_reference_declarator (cv_qualifiers, target,
3840 true, attributes);
3841 gcc_unreachable ();
3842 }
3843
3844 /* Create a new C++ parser. */
3845
3846 static cp_parser *
3847 cp_parser_new (void)
3848 {
3849 cp_parser *parser;
3850 cp_lexer *lexer;
3851 unsigned i;
3852
3853 /* cp_lexer_new_main is called before doing GC allocation because
3854 cp_lexer_new_main might load a PCH file. */
3855 lexer = cp_lexer_new_main ();
3856
3857 /* Initialize the binops_by_token so that we can get the tree
3858 directly from the token. */
3859 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3860 binops_by_token[binops[i].token_type] = binops[i];
3861
3862 parser = ggc_cleared_alloc<cp_parser> ();
3863 parser->lexer = lexer;
3864 parser->context = cp_parser_context_new (NULL);
3865
3866 /* For now, we always accept GNU extensions. */
3867 parser->allow_gnu_extensions_p = 1;
3868
3869 /* The `>' token is a greater-than operator, not the end of a
3870 template-id. */
3871 parser->greater_than_is_operator_p = true;
3872
3873 parser->default_arg_ok_p = true;
3874
3875 /* We are not parsing a constant-expression. */
3876 parser->integral_constant_expression_p = false;
3877 parser->allow_non_integral_constant_expression_p = false;
3878 parser->non_integral_constant_expression_p = false;
3879
3880 /* Local variable names are not forbidden. */
3881 parser->local_variables_forbidden_p = false;
3882
3883 /* We are not processing an `extern "C"' declaration. */
3884 parser->in_unbraced_linkage_specification_p = false;
3885
3886 /* We are not processing a declarator. */
3887 parser->in_declarator_p = false;
3888
3889 /* We are not processing a template-argument-list. */
3890 parser->in_template_argument_list_p = false;
3891
3892 /* We are not in an iteration statement. */
3893 parser->in_statement = 0;
3894
3895 /* We are not in a switch statement. */
3896 parser->in_switch_statement_p = false;
3897
3898 /* We are not parsing a type-id inside an expression. */
3899 parser->in_type_id_in_expr_p = false;
3900
3901 /* Declarations aren't implicitly extern "C". */
3902 parser->implicit_extern_c = false;
3903
3904 /* String literals should be translated to the execution character set. */
3905 parser->translate_strings_p = true;
3906
3907 /* We are not parsing a function body. */
3908 parser->in_function_body = false;
3909
3910 /* We can correct until told otherwise. */
3911 parser->colon_corrects_to_scope_p = true;
3912
3913 /* The unparsed function queue is empty. */
3914 push_unparsed_function_queues (parser);
3915
3916 /* There are no classes being defined. */
3917 parser->num_classes_being_defined = 0;
3918
3919 /* No template parameters apply. */
3920 parser->num_template_parameter_lists = 0;
3921
3922 /* Special parsing data structures. */
3923 parser->omp_declare_simd = NULL;
3924 parser->oacc_routine = NULL;
3925
3926 /* Not declaring an implicit function template. */
3927 parser->auto_is_implicit_function_template_parm_p = false;
3928 parser->fully_implicit_function_template_p = false;
3929 parser->implicit_template_parms = 0;
3930 parser->implicit_template_scope = 0;
3931
3932 /* Allow constrained-type-specifiers. */
3933 parser->prevent_constrained_type_specifiers = 0;
3934
3935 /* We haven't yet seen an 'extern "C"'. */
3936 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3937
3938 return parser;
3939 }
3940
3941 /* Create a cp_lexer structure which will emit the tokens in CACHE
3942 and push it onto the parser's lexer stack. This is used for delayed
3943 parsing of in-class method bodies and default arguments, and should
3944 not be confused with tentative parsing. */
3945 static void
3946 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3947 {
3948 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3949 lexer->next = parser->lexer;
3950 parser->lexer = lexer;
3951
3952 /* Move the current source position to that of the first token in the
3953 new lexer. */
3954 cp_lexer_set_source_position_from_token (lexer->next_token);
3955 }
3956
3957 /* Pop the top lexer off the parser stack. This is never used for the
3958 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3959 static void
3960 cp_parser_pop_lexer (cp_parser *parser)
3961 {
3962 cp_lexer *lexer = parser->lexer;
3963 parser->lexer = lexer->next;
3964 cp_lexer_destroy (lexer);
3965
3966 /* Put the current source position back where it was before this
3967 lexer was pushed. */
3968 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3969 }
3970
3971 /* Lexical conventions [gram.lex] */
3972
3973 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3974 identifier. */
3975
3976 static cp_expr
3977 cp_parser_identifier (cp_parser* parser)
3978 {
3979 cp_token *token;
3980
3981 /* Look for the identifier. */
3982 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3983 /* Return the value. */
3984 if (token)
3985 return cp_expr (token->u.value, token->location);
3986 else
3987 return error_mark_node;
3988 }
3989
3990 /* Parse a sequence of adjacent string constants. Returns a
3991 TREE_STRING representing the combined, nul-terminated string
3992 constant. If TRANSLATE is true, translate the string to the
3993 execution character set. If WIDE_OK is true, a wide string is
3994 invalid here.
3995
3996 C++98 [lex.string] says that if a narrow string literal token is
3997 adjacent to a wide string literal token, the behavior is undefined.
3998 However, C99 6.4.5p4 says that this results in a wide string literal.
3999 We follow C99 here, for consistency with the C front end.
4000
4001 This code is largely lifted from lex_string() in c-lex.c.
4002
4003 FUTURE: ObjC++ will need to handle @-strings here. */
4004 static cp_expr
4005 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4006 bool lookup_udlit = true)
4007 {
4008 tree value;
4009 size_t count;
4010 struct obstack str_ob;
4011 cpp_string str, istr, *strs;
4012 cp_token *tok;
4013 enum cpp_ttype type, curr_type;
4014 int have_suffix_p = 0;
4015 tree string_tree;
4016 tree suffix_id = NULL_TREE;
4017 bool curr_tok_is_userdef_p = false;
4018
4019 tok = cp_lexer_peek_token (parser->lexer);
4020 if (!cp_parser_is_string_literal (tok))
4021 {
4022 cp_parser_error (parser, "expected string-literal");
4023 return error_mark_node;
4024 }
4025
4026 location_t loc = tok->location;
4027
4028 if (cpp_userdef_string_p (tok->type))
4029 {
4030 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4031 curr_type = cpp_userdef_string_remove_type (tok->type);
4032 curr_tok_is_userdef_p = true;
4033 }
4034 else
4035 {
4036 string_tree = tok->u.value;
4037 curr_type = tok->type;
4038 }
4039 type = curr_type;
4040
4041 /* Try to avoid the overhead of creating and destroying an obstack
4042 for the common case of just one string. */
4043 if (!cp_parser_is_string_literal
4044 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4045 {
4046 cp_lexer_consume_token (parser->lexer);
4047
4048 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4049 str.len = TREE_STRING_LENGTH (string_tree);
4050 count = 1;
4051
4052 if (curr_tok_is_userdef_p)
4053 {
4054 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4055 have_suffix_p = 1;
4056 curr_type = cpp_userdef_string_remove_type (tok->type);
4057 }
4058 else
4059 curr_type = tok->type;
4060
4061 strs = &str;
4062 }
4063 else
4064 {
4065 location_t last_tok_loc = tok->location;
4066 gcc_obstack_init (&str_ob);
4067 count = 0;
4068
4069 do
4070 {
4071 cp_lexer_consume_token (parser->lexer);
4072 count++;
4073 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4074 str.len = TREE_STRING_LENGTH (string_tree);
4075
4076 if (curr_tok_is_userdef_p)
4077 {
4078 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4079 if (have_suffix_p == 0)
4080 {
4081 suffix_id = curr_suffix_id;
4082 have_suffix_p = 1;
4083 }
4084 else if (have_suffix_p == 1
4085 && curr_suffix_id != suffix_id)
4086 {
4087 error ("inconsistent user-defined literal suffixes"
4088 " %qD and %qD in string literal",
4089 suffix_id, curr_suffix_id);
4090 have_suffix_p = -1;
4091 }
4092 curr_type = cpp_userdef_string_remove_type (tok->type);
4093 }
4094 else
4095 curr_type = tok->type;
4096
4097 if (type != curr_type)
4098 {
4099 if (type == CPP_STRING)
4100 type = curr_type;
4101 else if (curr_type != CPP_STRING)
4102 {
4103 rich_location rich_loc (line_table, tok->location);
4104 rich_loc.add_range (last_tok_loc, false);
4105 error_at (&rich_loc,
4106 "unsupported non-standard concatenation "
4107 "of string literals");
4108 }
4109 }
4110
4111 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4112
4113 last_tok_loc = tok->location;
4114
4115 tok = cp_lexer_peek_token (parser->lexer);
4116 if (cpp_userdef_string_p (tok->type))
4117 {
4118 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4119 curr_type = cpp_userdef_string_remove_type (tok->type);
4120 curr_tok_is_userdef_p = true;
4121 }
4122 else
4123 {
4124 string_tree = tok->u.value;
4125 curr_type = tok->type;
4126 curr_tok_is_userdef_p = false;
4127 }
4128 }
4129 while (cp_parser_is_string_literal (tok));
4130
4131 /* A string literal built by concatenation has its caret=start at
4132 the start of the initial string, and its finish at the finish of
4133 the final string literal. */
4134 loc = make_location (loc, loc, get_finish (last_tok_loc));
4135
4136 strs = (cpp_string *) obstack_finish (&str_ob);
4137 }
4138
4139 if (type != CPP_STRING && !wide_ok)
4140 {
4141 cp_parser_error (parser, "a wide string is invalid in this context");
4142 type = CPP_STRING;
4143 }
4144
4145 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4146 (parse_in, strs, count, &istr, type))
4147 {
4148 value = build_string (istr.len, (const char *)istr.text);
4149 free (CONST_CAST (unsigned char *, istr.text));
4150
4151 switch (type)
4152 {
4153 default:
4154 case CPP_STRING:
4155 case CPP_UTF8STRING:
4156 TREE_TYPE (value) = char_array_type_node;
4157 break;
4158 case CPP_STRING16:
4159 TREE_TYPE (value) = char16_array_type_node;
4160 break;
4161 case CPP_STRING32:
4162 TREE_TYPE (value) = char32_array_type_node;
4163 break;
4164 case CPP_WSTRING:
4165 TREE_TYPE (value) = wchar_array_type_node;
4166 break;
4167 }
4168
4169 value = fix_string_type (value);
4170
4171 if (have_suffix_p)
4172 {
4173 tree literal = build_userdef_literal (suffix_id, value,
4174 OT_NONE, NULL_TREE);
4175 if (lookup_udlit)
4176 value = cp_parser_userdef_string_literal (literal);
4177 else
4178 value = literal;
4179 }
4180 }
4181 else
4182 /* cpp_interpret_string has issued an error. */
4183 value = error_mark_node;
4184
4185 if (count > 1)
4186 obstack_free (&str_ob, 0);
4187
4188 return cp_expr (value, loc);
4189 }
4190
4191 /* Look up a literal operator with the name and the exact arguments. */
4192
4193 static tree
4194 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4195 {
4196 tree decl;
4197 decl = lookup_name (name);
4198 if (!decl || !is_overloaded_fn (decl))
4199 return error_mark_node;
4200
4201 for (lkp_iterator iter (decl); iter; ++iter)
4202 {
4203 unsigned int ix;
4204 bool found = true;
4205 tree fn = *iter;
4206 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4207 if (parmtypes != NULL_TREE)
4208 {
4209 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4210 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4211 {
4212 tree tparm = TREE_VALUE (parmtypes);
4213 tree targ = TREE_TYPE ((*args)[ix]);
4214 bool ptr = TYPE_PTR_P (tparm);
4215 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4216 if ((ptr || arr || !same_type_p (tparm, targ))
4217 && (!ptr || !arr
4218 || !same_type_p (TREE_TYPE (tparm),
4219 TREE_TYPE (targ))))
4220 found = false;
4221 }
4222 if (found
4223 && ix == vec_safe_length (args)
4224 /* May be this should be sufficient_parms_p instead,
4225 depending on how exactly should user-defined literals
4226 work in presence of default arguments on the literal
4227 operator parameters. */
4228 && parmtypes == void_list_node)
4229 return decl;
4230 }
4231 }
4232
4233 return error_mark_node;
4234 }
4235
4236 /* Parse a user-defined char constant. Returns a call to a user-defined
4237 literal operator taking the character as an argument. */
4238
4239 static cp_expr
4240 cp_parser_userdef_char_literal (cp_parser *parser)
4241 {
4242 cp_token *token = cp_lexer_consume_token (parser->lexer);
4243 tree literal = token->u.value;
4244 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4245 tree value = USERDEF_LITERAL_VALUE (literal);
4246 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4247 tree decl, result;
4248
4249 /* Build up a call to the user-defined operator */
4250 /* Lookup the name we got back from the id-expression. */
4251 vec<tree, va_gc> *args = make_tree_vector ();
4252 vec_safe_push (args, value);
4253 decl = lookup_literal_operator (name, args);
4254 if (!decl || decl == error_mark_node)
4255 {
4256 error ("unable to find character literal operator %qD with %qT argument",
4257 name, TREE_TYPE (value));
4258 release_tree_vector (args);
4259 return error_mark_node;
4260 }
4261 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4262 release_tree_vector (args);
4263 return result;
4264 }
4265
4266 /* A subroutine of cp_parser_userdef_numeric_literal to
4267 create a char... template parameter pack from a string node. */
4268
4269 static tree
4270 make_char_string_pack (tree value)
4271 {
4272 tree charvec;
4273 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4274 const char *str = TREE_STRING_POINTER (value);
4275 int i, len = TREE_STRING_LENGTH (value) - 1;
4276 tree argvec = make_tree_vec (1);
4277
4278 /* Fill in CHARVEC with all of the parameters. */
4279 charvec = make_tree_vec (len);
4280 for (i = 0; i < len; ++i)
4281 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4282
4283 /* Build the argument packs. */
4284 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4285
4286 TREE_VEC_ELT (argvec, 0) = argpack;
4287
4288 return argvec;
4289 }
4290
4291 /* A subroutine of cp_parser_userdef_numeric_literal to
4292 create a char... template parameter pack from a string node. */
4293
4294 static tree
4295 make_string_pack (tree value)
4296 {
4297 tree charvec;
4298 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4299 const unsigned char *str
4300 = (const unsigned char *) TREE_STRING_POINTER (value);
4301 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4302 int len = TREE_STRING_LENGTH (value) / sz - 1;
4303 tree argvec = make_tree_vec (2);
4304
4305 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4306 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4307
4308 /* First template parm is character type. */
4309 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4310
4311 /* Fill in CHARVEC with all of the parameters. */
4312 charvec = make_tree_vec (len);
4313 for (int i = 0; i < len; ++i)
4314 TREE_VEC_ELT (charvec, i)
4315 = double_int_to_tree (str_char_type_node,
4316 double_int::from_buffer (str + i * sz, sz));
4317
4318 /* Build the argument packs. */
4319 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4320
4321 TREE_VEC_ELT (argvec, 1) = argpack;
4322
4323 return argvec;
4324 }
4325
4326 /* Parse a user-defined numeric constant. returns a call to a user-defined
4327 literal operator. */
4328
4329 static cp_expr
4330 cp_parser_userdef_numeric_literal (cp_parser *parser)
4331 {
4332 cp_token *token = cp_lexer_consume_token (parser->lexer);
4333 tree literal = token->u.value;
4334 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4335 tree value = USERDEF_LITERAL_VALUE (literal);
4336 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4337 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4338 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4339 tree decl, result;
4340 vec<tree, va_gc> *args;
4341
4342 /* Look for a literal operator taking the exact type of numeric argument
4343 as the literal value. */
4344 args = make_tree_vector ();
4345 vec_safe_push (args, value);
4346 decl = lookup_literal_operator (name, args);
4347 if (decl && decl != error_mark_node)
4348 {
4349 result = finish_call_expr (decl, &args, false, true,
4350 tf_warning_or_error);
4351
4352 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4353 {
4354 warning_at (token->location, OPT_Woverflow,
4355 "integer literal exceeds range of %qT type",
4356 long_long_unsigned_type_node);
4357 }
4358 else
4359 {
4360 if (overflow > 0)
4361 warning_at (token->location, OPT_Woverflow,
4362 "floating literal exceeds range of %qT type",
4363 long_double_type_node);
4364 else if (overflow < 0)
4365 warning_at (token->location, OPT_Woverflow,
4366 "floating literal truncated to zero");
4367 }
4368
4369 release_tree_vector (args);
4370 return result;
4371 }
4372 release_tree_vector (args);
4373
4374 /* If the numeric argument didn't work, look for a raw literal
4375 operator taking a const char* argument consisting of the number
4376 in string format. */
4377 args = make_tree_vector ();
4378 vec_safe_push (args, num_string);
4379 decl = lookup_literal_operator (name, args);
4380 if (decl && decl != error_mark_node)
4381 {
4382 result = finish_call_expr (decl, &args, false, true,
4383 tf_warning_or_error);
4384 release_tree_vector (args);
4385 return result;
4386 }
4387 release_tree_vector (args);
4388
4389 /* If the raw literal didn't work, look for a non-type template
4390 function with parameter pack char.... Call the function with
4391 template parameter characters representing the number. */
4392 args = make_tree_vector ();
4393 decl = lookup_literal_operator (name, args);
4394 if (decl && decl != error_mark_node)
4395 {
4396 tree tmpl_args = make_char_string_pack (num_string);
4397 decl = lookup_template_function (decl, tmpl_args);
4398 result = finish_call_expr (decl, &args, false, true,
4399 tf_warning_or_error);
4400 release_tree_vector (args);
4401 return result;
4402 }
4403
4404 release_tree_vector (args);
4405
4406 /* In C++14 the standard library defines complex number suffixes that
4407 conflict with GNU extensions. Prefer them if <complex> is #included. */
4408 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4409 bool i14 = (cxx_dialect > cxx11
4410 && (id_equal (suffix_id, "i")
4411 || id_equal (suffix_id, "if")
4412 || id_equal (suffix_id, "il")));
4413 diagnostic_t kind = DK_ERROR;
4414 int opt = 0;
4415
4416 if (i14 && ext)
4417 {
4418 tree cxlit = lookup_qualified_name (std_node,
4419 get_identifier ("complex_literals"),
4420 0, false, false);
4421 if (cxlit == error_mark_node)
4422 {
4423 /* No <complex>, so pedwarn and use GNU semantics. */
4424 kind = DK_PEDWARN;
4425 opt = OPT_Wpedantic;
4426 }
4427 }
4428
4429 bool complained
4430 = emit_diagnostic (kind, input_location, opt,
4431 "unable to find numeric literal operator %qD", name);
4432
4433 if (!complained)
4434 /* Don't inform either. */;
4435 else if (i14)
4436 {
4437 inform (token->location, "add %<using namespace std::complex_literals%> "
4438 "(from <complex>) to enable the C++14 user-defined literal "
4439 "suffixes");
4440 if (ext)
4441 inform (token->location, "or use %<j%> instead of %<i%> for the "
4442 "GNU built-in suffix");
4443 }
4444 else if (!ext)
4445 inform (token->location, "use -fext-numeric-literals "
4446 "to enable more built-in suffixes");
4447
4448 if (kind == DK_ERROR)
4449 value = error_mark_node;
4450 else
4451 {
4452 /* Use the built-in semantics. */
4453 tree type;
4454 if (id_equal (suffix_id, "i"))
4455 {
4456 if (TREE_CODE (value) == INTEGER_CST)
4457 type = integer_type_node;
4458 else
4459 type = double_type_node;
4460 }
4461 else if (id_equal (suffix_id, "if"))
4462 type = float_type_node;
4463 else /* if (id_equal (suffix_id, "il")) */
4464 type = long_double_type_node;
4465
4466 value = build_complex (build_complex_type (type),
4467 fold_convert (type, integer_zero_node),
4468 fold_convert (type, value));
4469 }
4470
4471 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4472 /* Avoid repeated diagnostics. */
4473 token->u.value = value;
4474 return value;
4475 }
4476
4477 /* Parse a user-defined string constant. Returns a call to a user-defined
4478 literal operator taking a character pointer and the length of the string
4479 as arguments. */
4480
4481 static tree
4482 cp_parser_userdef_string_literal (tree literal)
4483 {
4484 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4485 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4486 tree value = USERDEF_LITERAL_VALUE (literal);
4487 int len = TREE_STRING_LENGTH (value)
4488 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4489 tree decl, result;
4490 vec<tree, va_gc> *args;
4491
4492 /* Build up a call to the user-defined operator. */
4493 /* Lookup the name we got back from the id-expression. */
4494 args = make_tree_vector ();
4495 vec_safe_push (args, value);
4496 vec_safe_push (args, build_int_cst (size_type_node, len));
4497 decl = lookup_literal_operator (name, args);
4498
4499 if (decl && decl != error_mark_node)
4500 {
4501 result = finish_call_expr (decl, &args, false, true,
4502 tf_warning_or_error);
4503 release_tree_vector (args);
4504 return result;
4505 }
4506 release_tree_vector (args);
4507
4508 /* Look for a template function with typename parameter CharT
4509 and parameter pack CharT... Call the function with
4510 template parameter characters representing the string. */
4511 args = make_tree_vector ();
4512 decl = lookup_literal_operator (name, args);
4513 if (decl && decl != error_mark_node)
4514 {
4515 tree tmpl_args = make_string_pack (value);
4516 decl = lookup_template_function (decl, tmpl_args);
4517 result = finish_call_expr (decl, &args, false, true,
4518 tf_warning_or_error);
4519 release_tree_vector (args);
4520 return result;
4521 }
4522 release_tree_vector (args);
4523
4524 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4525 name, TREE_TYPE (value), size_type_node);
4526 return error_mark_node;
4527 }
4528
4529
4530 /* Basic concepts [gram.basic] */
4531
4532 /* Parse a translation-unit.
4533
4534 translation-unit:
4535 declaration-seq [opt]
4536
4537 Returns TRUE if all went well. */
4538
4539 static bool
4540 cp_parser_translation_unit (cp_parser* parser)
4541 {
4542 /* The address of the first non-permanent object on the declarator
4543 obstack. */
4544 static void *declarator_obstack_base;
4545
4546 bool success;
4547
4548 /* Create the declarator obstack, if necessary. */
4549 if (!cp_error_declarator)
4550 {
4551 gcc_obstack_init (&declarator_obstack);
4552 /* Create the error declarator. */
4553 cp_error_declarator = make_declarator (cdk_error);
4554 /* Create the empty parameter list. */
4555 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4556 UNKNOWN_LOCATION);
4557 /* Remember where the base of the declarator obstack lies. */
4558 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4559 }
4560
4561 cp_parser_declaration_seq_opt (parser);
4562
4563 /* If there are no tokens left then all went well. */
4564 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4565 {
4566 /* Get rid of the token array; we don't need it any more. */
4567 cp_lexer_destroy (parser->lexer);
4568 parser->lexer = NULL;
4569
4570 /* This file might have been a context that's implicitly extern
4571 "C". If so, pop the lang context. (Only relevant for PCH.) */
4572 if (parser->implicit_extern_c)
4573 {
4574 pop_lang_context ();
4575 parser->implicit_extern_c = false;
4576 }
4577
4578 /* Finish up. */
4579 finish_translation_unit ();
4580
4581 success = true;
4582 }
4583 else
4584 {
4585 cp_parser_error (parser, "expected declaration");
4586 success = false;
4587 }
4588
4589 /* Make sure the declarator obstack was fully cleaned up. */
4590 gcc_assert (obstack_next_free (&declarator_obstack)
4591 == declarator_obstack_base);
4592
4593 /* All went well. */
4594 return success;
4595 }
4596
4597 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4598 decltype context. */
4599
4600 static inline tsubst_flags_t
4601 complain_flags (bool decltype_p)
4602 {
4603 tsubst_flags_t complain = tf_warning_or_error;
4604 if (decltype_p)
4605 complain |= tf_decltype;
4606 return complain;
4607 }
4608
4609 /* We're about to parse a collection of statements. If we're currently
4610 parsing tentatively, set up a firewall so that any nested
4611 cp_parser_commit_to_tentative_parse won't affect the current context. */
4612
4613 static cp_token_position
4614 cp_parser_start_tentative_firewall (cp_parser *parser)
4615 {
4616 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4617 return 0;
4618
4619 cp_parser_parse_tentatively (parser);
4620 cp_parser_commit_to_topmost_tentative_parse (parser);
4621 return cp_lexer_token_position (parser->lexer, false);
4622 }
4623
4624 /* We've finished parsing the collection of statements. Wrap up the
4625 firewall and replace the relevant tokens with the parsed form. */
4626
4627 static void
4628 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4629 tree expr)
4630 {
4631 if (!start)
4632 return;
4633
4634 /* Finish the firewall level. */
4635 cp_parser_parse_definitely (parser);
4636 /* And remember the result of the parse for when we try again. */
4637 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4638 token->type = CPP_PREPARSED_EXPR;
4639 token->u.value = expr;
4640 token->keyword = RID_MAX;
4641 cp_lexer_purge_tokens_after (parser->lexer, start);
4642 }
4643
4644 /* Like the above functions, but let the user modify the tokens. Used by
4645 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4646 later parses, so it makes sense to localize the effects of
4647 cp_parser_commit_to_tentative_parse. */
4648
4649 struct tentative_firewall
4650 {
4651 cp_parser *parser;
4652 bool set;
4653
4654 tentative_firewall (cp_parser *p): parser(p)
4655 {
4656 /* If we're currently parsing tentatively, start a committed level as a
4657 firewall and then an inner tentative parse. */
4658 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4659 {
4660 cp_parser_parse_tentatively (parser);
4661 cp_parser_commit_to_topmost_tentative_parse (parser);
4662 cp_parser_parse_tentatively (parser);
4663 }
4664 }
4665
4666 ~tentative_firewall()
4667 {
4668 if (set)
4669 {
4670 /* Finish the inner tentative parse and the firewall, propagating any
4671 uncommitted error state to the outer tentative parse. */
4672 bool err = cp_parser_error_occurred (parser);
4673 cp_parser_parse_definitely (parser);
4674 cp_parser_parse_definitely (parser);
4675 if (err)
4676 cp_parser_simulate_error (parser);
4677 }
4678 }
4679 };
4680
4681 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4682 This class is for tracking such a matching pair of symbols.
4683 In particular, it tracks the location of the first token,
4684 so that if the second token is missing, we can highlight the
4685 location of the first token when notifying the user about the
4686 problem. */
4687
4688 template <typename traits_t>
4689 class token_pair
4690 {
4691 public:
4692 /* token_pair's ctor. */
4693 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4694
4695 /* If the next token is the opening symbol for this pair, consume it and
4696 return true.
4697 Otherwise, issue an error and return false.
4698 In either case, record the location of the opening token. */
4699
4700 bool require_open (cp_parser *parser)
4701 {
4702 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4703 return cp_parser_require (parser, traits_t::open_token_type,
4704 traits_t::required_token_open);
4705 }
4706
4707 /* Consume the next token from PARSER, recording its location as
4708 that of the opening token within the pair. */
4709
4710 cp_token * consume_open (cp_parser *parser)
4711 {
4712 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4713 gcc_assert (tok->type == traits_t::open_token_type);
4714 m_open_loc = tok->location;
4715 return tok;
4716 }
4717
4718 /* If the next token is the closing symbol for this pair, consume it
4719 and return it.
4720 Otherwise, issue an error, highlighting the location of the
4721 corresponding opening token, and return NULL. */
4722
4723 cp_token *require_close (cp_parser *parser) const
4724 {
4725 return cp_parser_require (parser, traits_t::close_token_type,
4726 traits_t::required_token_close,
4727 m_open_loc);
4728 }
4729
4730 private:
4731 location_t m_open_loc;
4732 };
4733
4734 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4735
4736 struct matching_paren_traits
4737 {
4738 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4739 static const enum required_token required_token_open = RT_OPEN_PAREN;
4740 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4741 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4742 };
4743
4744 /* "matching_parens" is a token_pair<T> class for tracking matching
4745 pairs of parentheses. */
4746
4747 typedef token_pair<matching_paren_traits> matching_parens;
4748
4749 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4750
4751 struct matching_brace_traits
4752 {
4753 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4754 static const enum required_token required_token_open = RT_OPEN_BRACE;
4755 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4756 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4757 };
4758
4759 /* "matching_braces" is a token_pair<T> class for tracking matching
4760 pairs of braces. */
4761
4762 typedef token_pair<matching_brace_traits> matching_braces;
4763
4764
4765 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4766 enclosing parentheses. */
4767
4768 static cp_expr
4769 cp_parser_statement_expr (cp_parser *parser)
4770 {
4771 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4772
4773 /* Consume the '('. */
4774 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4775 matching_parens parens;
4776 parens.consume_open (parser);
4777 /* Start the statement-expression. */
4778 tree expr = begin_stmt_expr ();
4779 /* Parse the compound-statement. */
4780 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4781 /* Finish up. */
4782 expr = finish_stmt_expr (expr, false);
4783 /* Consume the ')'. */
4784 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4785 if (!parens.require_close (parser))
4786 cp_parser_skip_to_end_of_statement (parser);
4787
4788 cp_parser_end_tentative_firewall (parser, start, expr);
4789 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4790 return cp_expr (expr, combined_loc);
4791 }
4792
4793 /* Expressions [gram.expr] */
4794
4795 /* Parse a fold-operator.
4796
4797 fold-operator:
4798 - * / % ^ & | = < > << >>
4799 = -= *= /= %= ^= &= |= <<= >>=
4800 == != <= >= && || , .* ->*
4801
4802 This returns the tree code corresponding to the matched operator
4803 as an int. When the current token matches a compound assignment
4804 opertor, the resulting tree code is the negative value of the
4805 non-assignment operator. */
4806
4807 static int
4808 cp_parser_fold_operator (cp_token *token)
4809 {
4810 switch (token->type)
4811 {
4812 case CPP_PLUS: return PLUS_EXPR;
4813 case CPP_MINUS: return MINUS_EXPR;
4814 case CPP_MULT: return MULT_EXPR;
4815 case CPP_DIV: return TRUNC_DIV_EXPR;
4816 case CPP_MOD: return TRUNC_MOD_EXPR;
4817 case CPP_XOR: return BIT_XOR_EXPR;
4818 case CPP_AND: return BIT_AND_EXPR;
4819 case CPP_OR: return BIT_IOR_EXPR;
4820 case CPP_LSHIFT: return LSHIFT_EXPR;
4821 case CPP_RSHIFT: return RSHIFT_EXPR;
4822
4823 case CPP_EQ: return -NOP_EXPR;
4824 case CPP_PLUS_EQ: return -PLUS_EXPR;
4825 case CPP_MINUS_EQ: return -MINUS_EXPR;
4826 case CPP_MULT_EQ: return -MULT_EXPR;
4827 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4828 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4829 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4830 case CPP_AND_EQ: return -BIT_AND_EXPR;
4831 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4832 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4833 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4834
4835 case CPP_EQ_EQ: return EQ_EXPR;
4836 case CPP_NOT_EQ: return NE_EXPR;
4837 case CPP_LESS: return LT_EXPR;
4838 case CPP_GREATER: return GT_EXPR;
4839 case CPP_LESS_EQ: return LE_EXPR;
4840 case CPP_GREATER_EQ: return GE_EXPR;
4841
4842 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4843 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4844
4845 case CPP_COMMA: return COMPOUND_EXPR;
4846
4847 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4848 case CPP_DEREF_STAR: return MEMBER_REF;
4849
4850 default: return ERROR_MARK;
4851 }
4852 }
4853
4854 /* Returns true if CODE indicates a binary expression, which is not allowed in
4855 the LHS of a fold-expression. More codes will need to be added to use this
4856 function in other contexts. */
4857
4858 static bool
4859 is_binary_op (tree_code code)
4860 {
4861 switch (code)
4862 {
4863 case PLUS_EXPR:
4864 case POINTER_PLUS_EXPR:
4865 case MINUS_EXPR:
4866 case MULT_EXPR:
4867 case TRUNC_DIV_EXPR:
4868 case TRUNC_MOD_EXPR:
4869 case BIT_XOR_EXPR:
4870 case BIT_AND_EXPR:
4871 case BIT_IOR_EXPR:
4872 case LSHIFT_EXPR:
4873 case RSHIFT_EXPR:
4874
4875 case MODOP_EXPR:
4876
4877 case EQ_EXPR:
4878 case NE_EXPR:
4879 case LE_EXPR:
4880 case GE_EXPR:
4881 case LT_EXPR:
4882 case GT_EXPR:
4883
4884 case TRUTH_ANDIF_EXPR:
4885 case TRUTH_ORIF_EXPR:
4886
4887 case COMPOUND_EXPR:
4888
4889 case DOTSTAR_EXPR:
4890 case MEMBER_REF:
4891 return true;
4892
4893 default:
4894 return false;
4895 }
4896 }
4897
4898 /* If the next token is a suitable fold operator, consume it and return as
4899 the function above. */
4900
4901 static int
4902 cp_parser_fold_operator (cp_parser *parser)
4903 {
4904 cp_token* token = cp_lexer_peek_token (parser->lexer);
4905 int code = cp_parser_fold_operator (token);
4906 if (code != ERROR_MARK)
4907 cp_lexer_consume_token (parser->lexer);
4908 return code;
4909 }
4910
4911 /* Parse a fold-expression.
4912
4913 fold-expression:
4914 ( ... folding-operator cast-expression)
4915 ( cast-expression folding-operator ... )
4916 ( cast-expression folding operator ... folding-operator cast-expression)
4917
4918 Note that the '(' and ')' are matched in primary expression. */
4919
4920 static cp_expr
4921 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4922 {
4923 cp_id_kind pidk;
4924
4925 // Left fold.
4926 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4927 {
4928 cp_lexer_consume_token (parser->lexer);
4929 int op = cp_parser_fold_operator (parser);
4930 if (op == ERROR_MARK)
4931 {
4932 cp_parser_error (parser, "expected binary operator");
4933 return error_mark_node;
4934 }
4935
4936 tree expr = cp_parser_cast_expression (parser, false, false,
4937 false, &pidk);
4938 if (expr == error_mark_node)
4939 return error_mark_node;
4940 return finish_left_unary_fold_expr (expr, op);
4941 }
4942
4943 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4944 int op = cp_parser_fold_operator (parser);
4945 if (op == ERROR_MARK)
4946 {
4947 cp_parser_error (parser, "expected binary operator");
4948 return error_mark_node;
4949 }
4950
4951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4952 {
4953 cp_parser_error (parser, "expected ...");
4954 return error_mark_node;
4955 }
4956 cp_lexer_consume_token (parser->lexer);
4957
4958 /* The operands of a fold-expression are cast-expressions, so binary or
4959 conditional expressions are not allowed. We check this here to avoid
4960 tentative parsing. */
4961 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4962 /* OK, the expression was parenthesized. */;
4963 else if (is_binary_op (TREE_CODE (expr1)))
4964 error_at (location_of (expr1),
4965 "binary expression in operand of fold-expression");
4966 else if (TREE_CODE (expr1) == COND_EXPR)
4967 error_at (location_of (expr1),
4968 "conditional expression in operand of fold-expression");
4969
4970 // Right fold.
4971 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4972 return finish_right_unary_fold_expr (expr1, op);
4973
4974 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4975 {
4976 cp_parser_error (parser, "mismatched operator in fold-expression");
4977 return error_mark_node;
4978 }
4979 cp_lexer_consume_token (parser->lexer);
4980
4981 // Binary left or right fold.
4982 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4983 if (expr2 == error_mark_node)
4984 return error_mark_node;
4985 return finish_binary_fold_expr (expr1, expr2, op);
4986 }
4987
4988 /* Parse a primary-expression.
4989
4990 primary-expression:
4991 literal
4992 this
4993 ( expression )
4994 id-expression
4995 lambda-expression (C++11)
4996
4997 GNU Extensions:
4998
4999 primary-expression:
5000 ( compound-statement )
5001 __builtin_va_arg ( assignment-expression , type-id )
5002 __builtin_offsetof ( type-id , offsetof-expression )
5003
5004 C++ Extensions:
5005 __has_nothrow_assign ( type-id )
5006 __has_nothrow_constructor ( type-id )
5007 __has_nothrow_copy ( type-id )
5008 __has_trivial_assign ( type-id )
5009 __has_trivial_constructor ( type-id )
5010 __has_trivial_copy ( type-id )
5011 __has_trivial_destructor ( type-id )
5012 __has_virtual_destructor ( type-id )
5013 __is_abstract ( type-id )
5014 __is_base_of ( type-id , type-id )
5015 __is_class ( type-id )
5016 __is_empty ( type-id )
5017 __is_enum ( type-id )
5018 __is_final ( type-id )
5019 __is_literal_type ( type-id )
5020 __is_pod ( type-id )
5021 __is_polymorphic ( type-id )
5022 __is_std_layout ( type-id )
5023 __is_trivial ( type-id )
5024 __is_union ( type-id )
5025
5026 Objective-C++ Extension:
5027
5028 primary-expression:
5029 objc-expression
5030
5031 literal:
5032 __null
5033
5034 ADDRESS_P is true iff this expression was immediately preceded by
5035 "&" and therefore might denote a pointer-to-member. CAST_P is true
5036 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5037 true iff this expression is a template argument.
5038
5039 Returns a representation of the expression. Upon return, *IDK
5040 indicates what kind of id-expression (if any) was present. */
5041
5042 static cp_expr
5043 cp_parser_primary_expression (cp_parser *parser,
5044 bool address_p,
5045 bool cast_p,
5046 bool template_arg_p,
5047 bool decltype_p,
5048 cp_id_kind *idk)
5049 {
5050 cp_token *token = NULL;
5051
5052 /* Assume the primary expression is not an id-expression. */
5053 *idk = CP_ID_KIND_NONE;
5054
5055 /* Peek at the next token. */
5056 token = cp_lexer_peek_token (parser->lexer);
5057 switch ((int) token->type)
5058 {
5059 /* literal:
5060 integer-literal
5061 character-literal
5062 floating-literal
5063 string-literal
5064 boolean-literal
5065 pointer-literal
5066 user-defined-literal */
5067 case CPP_CHAR:
5068 case CPP_CHAR16:
5069 case CPP_CHAR32:
5070 case CPP_WCHAR:
5071 case CPP_UTF8CHAR:
5072 case CPP_NUMBER:
5073 case CPP_PREPARSED_EXPR:
5074 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5075 return cp_parser_userdef_numeric_literal (parser);
5076 token = cp_lexer_consume_token (parser->lexer);
5077 if (TREE_CODE (token->u.value) == FIXED_CST)
5078 {
5079 error_at (token->location,
5080 "fixed-point types not supported in C++");
5081 return error_mark_node;
5082 }
5083 /* Floating-point literals are only allowed in an integral
5084 constant expression if they are cast to an integral or
5085 enumeration type. */
5086 if (TREE_CODE (token->u.value) == REAL_CST
5087 && parser->integral_constant_expression_p
5088 && pedantic)
5089 {
5090 /* CAST_P will be set even in invalid code like "int(2.7 +
5091 ...)". Therefore, we have to check that the next token
5092 is sure to end the cast. */
5093 if (cast_p)
5094 {
5095 cp_token *next_token;
5096
5097 next_token = cp_lexer_peek_token (parser->lexer);
5098 if (/* The comma at the end of an
5099 enumerator-definition. */
5100 next_token->type != CPP_COMMA
5101 /* The curly brace at the end of an enum-specifier. */
5102 && next_token->type != CPP_CLOSE_BRACE
5103 /* The end of a statement. */
5104 && next_token->type != CPP_SEMICOLON
5105 /* The end of the cast-expression. */
5106 && next_token->type != CPP_CLOSE_PAREN
5107 /* The end of an array bound. */
5108 && next_token->type != CPP_CLOSE_SQUARE
5109 /* The closing ">" in a template-argument-list. */
5110 && (next_token->type != CPP_GREATER
5111 || parser->greater_than_is_operator_p)
5112 /* C++0x only: A ">>" treated like two ">" tokens,
5113 in a template-argument-list. */
5114 && (next_token->type != CPP_RSHIFT
5115 || (cxx_dialect == cxx98)
5116 || parser->greater_than_is_operator_p))
5117 cast_p = false;
5118 }
5119
5120 /* If we are within a cast, then the constraint that the
5121 cast is to an integral or enumeration type will be
5122 checked at that point. If we are not within a cast, then
5123 this code is invalid. */
5124 if (!cast_p)
5125 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5126 }
5127 return cp_expr (token->u.value, token->location);
5128
5129 case CPP_CHAR_USERDEF:
5130 case CPP_CHAR16_USERDEF:
5131 case CPP_CHAR32_USERDEF:
5132 case CPP_WCHAR_USERDEF:
5133 case CPP_UTF8CHAR_USERDEF:
5134 return cp_parser_userdef_char_literal (parser);
5135
5136 case CPP_STRING:
5137 case CPP_STRING16:
5138 case CPP_STRING32:
5139 case CPP_WSTRING:
5140 case CPP_UTF8STRING:
5141 case CPP_STRING_USERDEF:
5142 case CPP_STRING16_USERDEF:
5143 case CPP_STRING32_USERDEF:
5144 case CPP_WSTRING_USERDEF:
5145 case CPP_UTF8STRING_USERDEF:
5146 /* ??? Should wide strings be allowed when parser->translate_strings_p
5147 is false (i.e. in attributes)? If not, we can kill the third
5148 argument to cp_parser_string_literal. */
5149 return cp_parser_string_literal (parser,
5150 parser->translate_strings_p,
5151 true);
5152
5153 case CPP_OPEN_PAREN:
5154 /* If we see `( { ' then we are looking at the beginning of
5155 a GNU statement-expression. */
5156 if (cp_parser_allow_gnu_extensions_p (parser)
5157 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5158 {
5159 /* Statement-expressions are not allowed by the standard. */
5160 pedwarn (token->location, OPT_Wpedantic,
5161 "ISO C++ forbids braced-groups within expressions");
5162
5163 /* And they're not allowed outside of a function-body; you
5164 cannot, for example, write:
5165
5166 int i = ({ int j = 3; j + 1; });
5167
5168 at class or namespace scope. */
5169 if (!parser->in_function_body
5170 || parser->in_template_argument_list_p)
5171 {
5172 error_at (token->location,
5173 "statement-expressions are not allowed outside "
5174 "functions nor in template-argument lists");
5175 cp_parser_skip_to_end_of_block_or_statement (parser);
5176 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5177 cp_lexer_consume_token (parser->lexer);
5178 return error_mark_node;
5179 }
5180 else
5181 return cp_parser_statement_expr (parser);
5182 }
5183 /* Otherwise it's a normal parenthesized expression. */
5184 {
5185 cp_expr expr;
5186 bool saved_greater_than_is_operator_p;
5187
5188 location_t open_paren_loc = token->location;
5189
5190 /* Consume the `('. */
5191 matching_parens parens;
5192 parens.consume_open (parser);
5193 /* Within a parenthesized expression, a `>' token is always
5194 the greater-than operator. */
5195 saved_greater_than_is_operator_p
5196 = parser->greater_than_is_operator_p;
5197 parser->greater_than_is_operator_p = true;
5198
5199 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5200 /* Left fold expression. */
5201 expr = NULL_TREE;
5202 else
5203 /* Parse the parenthesized expression. */
5204 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5205
5206 token = cp_lexer_peek_token (parser->lexer);
5207 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5208 {
5209 expr = cp_parser_fold_expression (parser, expr);
5210 if (expr != error_mark_node
5211 && cxx_dialect < cxx17
5212 && !in_system_header_at (input_location))
5213 pedwarn (input_location, 0, "fold-expressions only available "
5214 "with -std=c++17 or -std=gnu++17");
5215 }
5216 else
5217 /* Let the front end know that this expression was
5218 enclosed in parentheses. This matters in case, for
5219 example, the expression is of the form `A::B', since
5220 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5221 not. */
5222 expr = finish_parenthesized_expr (expr);
5223
5224 /* DR 705: Wrapping an unqualified name in parentheses
5225 suppresses arg-dependent lookup. We want to pass back
5226 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5227 (c++/37862), but none of the others. */
5228 if (*idk != CP_ID_KIND_QUALIFIED)
5229 *idk = CP_ID_KIND_NONE;
5230
5231 /* The `>' token might be the end of a template-id or
5232 template-parameter-list now. */
5233 parser->greater_than_is_operator_p
5234 = saved_greater_than_is_operator_p;
5235
5236 /* Consume the `)'. */
5237 token = cp_lexer_peek_token (parser->lexer);
5238 location_t close_paren_loc = token->location;
5239 expr.set_range (open_paren_loc, close_paren_loc);
5240 if (!parens.require_close (parser)
5241 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5242 cp_parser_skip_to_end_of_statement (parser);
5243
5244 return expr;
5245 }
5246
5247 case CPP_OPEN_SQUARE:
5248 {
5249 if (c_dialect_objc ())
5250 {
5251 /* We might have an Objective-C++ message. */
5252 cp_parser_parse_tentatively (parser);
5253 tree msg = cp_parser_objc_message_expression (parser);
5254 /* If that works out, we're done ... */
5255 if (cp_parser_parse_definitely (parser))
5256 return msg;
5257 /* ... else, fall though to see if it's a lambda. */
5258 }
5259 cp_expr lam = cp_parser_lambda_expression (parser);
5260 /* Don't warn about a failed tentative parse. */
5261 if (cp_parser_error_occurred (parser))
5262 return error_mark_node;
5263 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5264 return lam;
5265 }
5266
5267 case CPP_OBJC_STRING:
5268 if (c_dialect_objc ())
5269 /* We have an Objective-C++ string literal. */
5270 return cp_parser_objc_expression (parser);
5271 cp_parser_error (parser, "expected primary-expression");
5272 return error_mark_node;
5273
5274 case CPP_KEYWORD:
5275 switch (token->keyword)
5276 {
5277 /* These two are the boolean literals. */
5278 case RID_TRUE:
5279 cp_lexer_consume_token (parser->lexer);
5280 return cp_expr (boolean_true_node, token->location);
5281 case RID_FALSE:
5282 cp_lexer_consume_token (parser->lexer);
5283 return cp_expr (boolean_false_node, token->location);
5284
5285 /* The `__null' literal. */
5286 case RID_NULL:
5287 cp_lexer_consume_token (parser->lexer);
5288 return cp_expr (null_node, token->location);
5289
5290 /* The `nullptr' literal. */
5291 case RID_NULLPTR:
5292 cp_lexer_consume_token (parser->lexer);
5293 return cp_expr (nullptr_node, token->location);
5294
5295 /* Recognize the `this' keyword. */
5296 case RID_THIS:
5297 cp_lexer_consume_token (parser->lexer);
5298 if (parser->local_variables_forbidden_p)
5299 {
5300 error_at (token->location,
5301 "%<this%> may not be used in this context");
5302 return error_mark_node;
5303 }
5304 /* Pointers cannot appear in constant-expressions. */
5305 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5306 return error_mark_node;
5307 return cp_expr (finish_this_expr (), token->location);
5308
5309 /* The `operator' keyword can be the beginning of an
5310 id-expression. */
5311 case RID_OPERATOR:
5312 goto id_expression;
5313
5314 case RID_FUNCTION_NAME:
5315 case RID_PRETTY_FUNCTION_NAME:
5316 case RID_C99_FUNCTION_NAME:
5317 {
5318 non_integral_constant name;
5319
5320 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5321 __func__ are the names of variables -- but they are
5322 treated specially. Therefore, they are handled here,
5323 rather than relying on the generic id-expression logic
5324 below. Grammatically, these names are id-expressions.
5325
5326 Consume the token. */
5327 token = cp_lexer_consume_token (parser->lexer);
5328
5329 switch (token->keyword)
5330 {
5331 case RID_FUNCTION_NAME:
5332 name = NIC_FUNC_NAME;
5333 break;
5334 case RID_PRETTY_FUNCTION_NAME:
5335 name = NIC_PRETTY_FUNC;
5336 break;
5337 case RID_C99_FUNCTION_NAME:
5338 name = NIC_C99_FUNC;
5339 break;
5340 default:
5341 gcc_unreachable ();
5342 }
5343
5344 if (cp_parser_non_integral_constant_expression (parser, name))
5345 return error_mark_node;
5346
5347 /* Look up the name. */
5348 return finish_fname (token->u.value);
5349 }
5350
5351 case RID_VA_ARG:
5352 {
5353 tree expression;
5354 tree type;
5355 source_location type_location;
5356 location_t start_loc
5357 = cp_lexer_peek_token (parser->lexer)->location;
5358 /* The `__builtin_va_arg' construct is used to handle
5359 `va_arg'. Consume the `__builtin_va_arg' token. */
5360 cp_lexer_consume_token (parser->lexer);
5361 /* Look for the opening `('. */
5362 matching_parens parens;
5363 parens.require_open (parser);
5364 /* Now, parse the assignment-expression. */
5365 expression = cp_parser_assignment_expression (parser);
5366 /* Look for the `,'. */
5367 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5368 type_location = cp_lexer_peek_token (parser->lexer)->location;
5369 /* Parse the type-id. */
5370 {
5371 type_id_in_expr_sentinel s (parser);
5372 type = cp_parser_type_id (parser);
5373 }
5374 /* Look for the closing `)'. */
5375 location_t finish_loc
5376 = cp_lexer_peek_token (parser->lexer)->location;
5377 parens.require_close (parser);
5378 /* Using `va_arg' in a constant-expression is not
5379 allowed. */
5380 if (cp_parser_non_integral_constant_expression (parser,
5381 NIC_VA_ARG))
5382 return error_mark_node;
5383 /* Construct a location of the form:
5384 __builtin_va_arg (v, int)
5385 ~~~~~~~~~~~~~~~~~~~~~^~~~
5386 with the caret at the type, ranging from the start of the
5387 "__builtin_va_arg" token to the close paren. */
5388 location_t combined_loc
5389 = make_location (type_location, start_loc, finish_loc);
5390 return build_x_va_arg (combined_loc, expression, type);
5391 }
5392
5393 case RID_OFFSETOF:
5394 return cp_parser_builtin_offsetof (parser);
5395
5396 case RID_HAS_NOTHROW_ASSIGN:
5397 case RID_HAS_NOTHROW_CONSTRUCTOR:
5398 case RID_HAS_NOTHROW_COPY:
5399 case RID_HAS_TRIVIAL_ASSIGN:
5400 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5401 case RID_HAS_TRIVIAL_COPY:
5402 case RID_HAS_TRIVIAL_DESTRUCTOR:
5403 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5404 case RID_HAS_VIRTUAL_DESTRUCTOR:
5405 case RID_IS_ABSTRACT:
5406 case RID_IS_AGGREGATE:
5407 case RID_IS_BASE_OF:
5408 case RID_IS_CLASS:
5409 case RID_IS_EMPTY:
5410 case RID_IS_ENUM:
5411 case RID_IS_FINAL:
5412 case RID_IS_LITERAL_TYPE:
5413 case RID_IS_POD:
5414 case RID_IS_POLYMORPHIC:
5415 case RID_IS_SAME_AS:
5416 case RID_IS_STD_LAYOUT:
5417 case RID_IS_TRIVIAL:
5418 case RID_IS_TRIVIALLY_ASSIGNABLE:
5419 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5420 case RID_IS_TRIVIALLY_COPYABLE:
5421 case RID_IS_UNION:
5422 case RID_IS_ASSIGNABLE:
5423 case RID_IS_CONSTRUCTIBLE:
5424 return cp_parser_trait_expr (parser, token->keyword);
5425
5426 // C++ concepts
5427 case RID_REQUIRES:
5428 return cp_parser_requires_expression (parser);
5429
5430 /* Objective-C++ expressions. */
5431 case RID_AT_ENCODE:
5432 case RID_AT_PROTOCOL:
5433 case RID_AT_SELECTOR:
5434 return cp_parser_objc_expression (parser);
5435
5436 case RID_TEMPLATE:
5437 if (parser->in_function_body
5438 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5439 == CPP_LESS))
5440 {
5441 error_at (token->location,
5442 "a template declaration cannot appear at block scope");
5443 cp_parser_skip_to_end_of_block_or_statement (parser);
5444 return error_mark_node;
5445 }
5446 /* FALLTHRU */
5447 default:
5448 cp_parser_error (parser, "expected primary-expression");
5449 return error_mark_node;
5450 }
5451
5452 /* An id-expression can start with either an identifier, a
5453 `::' as the beginning of a qualified-id, or the "operator"
5454 keyword. */
5455 case CPP_NAME:
5456 case CPP_SCOPE:
5457 case CPP_TEMPLATE_ID:
5458 case CPP_NESTED_NAME_SPECIFIER:
5459 {
5460 id_expression:
5461 cp_expr id_expression;
5462 cp_expr decl;
5463 const char *error_msg;
5464 bool template_p;
5465 bool done;
5466 cp_token *id_expr_token;
5467
5468 /* Parse the id-expression. */
5469 id_expression
5470 = cp_parser_id_expression (parser,
5471 /*template_keyword_p=*/false,
5472 /*check_dependency_p=*/true,
5473 &template_p,
5474 /*declarator_p=*/false,
5475 /*optional_p=*/false);
5476 if (id_expression == error_mark_node)
5477 return error_mark_node;
5478 id_expr_token = token;
5479 token = cp_lexer_peek_token (parser->lexer);
5480 done = (token->type != CPP_OPEN_SQUARE
5481 && token->type != CPP_OPEN_PAREN
5482 && token->type != CPP_DOT
5483 && token->type != CPP_DEREF
5484 && token->type != CPP_PLUS_PLUS
5485 && token->type != CPP_MINUS_MINUS);
5486 /* If we have a template-id, then no further lookup is
5487 required. If the template-id was for a template-class, we
5488 will sometimes have a TYPE_DECL at this point. */
5489 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5490 || TREE_CODE (id_expression) == TYPE_DECL)
5491 decl = id_expression;
5492 /* Look up the name. */
5493 else
5494 {
5495 tree ambiguous_decls;
5496
5497 /* If we already know that this lookup is ambiguous, then
5498 we've already issued an error message; there's no reason
5499 to check again. */
5500 if (id_expr_token->type == CPP_NAME
5501 && id_expr_token->error_reported)
5502 {
5503 cp_parser_simulate_error (parser);
5504 return error_mark_node;
5505 }
5506
5507 decl = cp_parser_lookup_name (parser, id_expression,
5508 none_type,
5509 template_p,
5510 /*is_namespace=*/false,
5511 /*check_dependency=*/true,
5512 &ambiguous_decls,
5513 id_expr_token->location);
5514 /* If the lookup was ambiguous, an error will already have
5515 been issued. */
5516 if (ambiguous_decls)
5517 return error_mark_node;
5518
5519 /* In Objective-C++, we may have an Objective-C 2.0
5520 dot-syntax for classes here. */
5521 if (c_dialect_objc ()
5522 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5523 && TREE_CODE (decl) == TYPE_DECL
5524 && objc_is_class_name (decl))
5525 {
5526 tree component;
5527 cp_lexer_consume_token (parser->lexer);
5528 component = cp_parser_identifier (parser);
5529 if (component == error_mark_node)
5530 return error_mark_node;
5531
5532 tree result = objc_build_class_component_ref (id_expression,
5533 component);
5534 /* Build a location of the form:
5535 expr.component
5536 ~~~~~^~~~~~~~~
5537 with caret at the start of the component name (at
5538 input_location), ranging from the start of the id_expression
5539 to the end of the component name. */
5540 location_t combined_loc
5541 = make_location (input_location, id_expression.get_start (),
5542 get_finish (input_location));
5543 protected_set_expr_location (result, combined_loc);
5544 return result;
5545 }
5546
5547 /* In Objective-C++, an instance variable (ivar) may be preferred
5548 to whatever cp_parser_lookup_name() found.
5549 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5550 rest of c-family, we have to do a little extra work to preserve
5551 any location information in cp_expr "decl". Given that
5552 objc_lookup_ivar is implemented in "c-family" and "objc", we
5553 have a trip through the pure "tree" type, rather than cp_expr.
5554 Naively copying it back to "decl" would implicitly give the
5555 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5556 store an EXPR_LOCATION. Hence we only update "decl" (and
5557 hence its location_t) if we get back a different tree node. */
5558 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5559 id_expression);
5560 if (decl_tree != decl.get_value ())
5561 decl = cp_expr (decl_tree);
5562
5563 /* If name lookup gives us a SCOPE_REF, then the
5564 qualifying scope was dependent. */
5565 if (TREE_CODE (decl) == SCOPE_REF)
5566 {
5567 /* At this point, we do not know if DECL is a valid
5568 integral constant expression. We assume that it is
5569 in fact such an expression, so that code like:
5570
5571 template <int N> struct A {
5572 int a[B<N>::i];
5573 };
5574
5575 is accepted. At template-instantiation time, we
5576 will check that B<N>::i is actually a constant. */
5577 return decl;
5578 }
5579 /* Check to see if DECL is a local variable in a context
5580 where that is forbidden. */
5581 if (parser->local_variables_forbidden_p
5582 && local_variable_p (decl))
5583 {
5584 /* It might be that we only found DECL because we are
5585 trying to be generous with pre-ISO scoping rules.
5586 For example, consider:
5587
5588 int i;
5589 void g() {
5590 for (int i = 0; i < 10; ++i) {}
5591 extern void f(int j = i);
5592 }
5593
5594 Here, name look up will originally find the out
5595 of scope `i'. We need to issue a warning message,
5596 but then use the global `i'. */
5597 decl = check_for_out_of_scope_variable (decl);
5598 if (local_variable_p (decl))
5599 {
5600 error_at (id_expr_token->location,
5601 "local variable %qD may not appear in this context",
5602 decl.get_value ());
5603 return error_mark_node;
5604 }
5605 }
5606 }
5607
5608 decl = (finish_id_expression
5609 (id_expression, decl, parser->scope,
5610 idk,
5611 parser->integral_constant_expression_p,
5612 parser->allow_non_integral_constant_expression_p,
5613 &parser->non_integral_constant_expression_p,
5614 template_p, done, address_p,
5615 template_arg_p,
5616 &error_msg,
5617 id_expression.get_location ()));
5618 if (error_msg)
5619 cp_parser_error (parser, error_msg);
5620 decl.set_location (id_expr_token->location);
5621 return decl;
5622 }
5623
5624 /* Anything else is an error. */
5625 default:
5626 cp_parser_error (parser, "expected primary-expression");
5627 return error_mark_node;
5628 }
5629 }
5630
5631 static inline cp_expr
5632 cp_parser_primary_expression (cp_parser *parser,
5633 bool address_p,
5634 bool cast_p,
5635 bool template_arg_p,
5636 cp_id_kind *idk)
5637 {
5638 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5639 /*decltype*/false, idk);
5640 }
5641
5642 /* Parse an id-expression.
5643
5644 id-expression:
5645 unqualified-id
5646 qualified-id
5647
5648 qualified-id:
5649 :: [opt] nested-name-specifier template [opt] unqualified-id
5650 :: identifier
5651 :: operator-function-id
5652 :: template-id
5653
5654 Return a representation of the unqualified portion of the
5655 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5656 a `::' or nested-name-specifier.
5657
5658 Often, if the id-expression was a qualified-id, the caller will
5659 want to make a SCOPE_REF to represent the qualified-id. This
5660 function does not do this in order to avoid wastefully creating
5661 SCOPE_REFs when they are not required.
5662
5663 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5664 `template' keyword.
5665
5666 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5667 uninstantiated templates.
5668
5669 If *TEMPLATE_P is non-NULL, it is set to true iff the
5670 `template' keyword is used to explicitly indicate that the entity
5671 named is a template.
5672
5673 If DECLARATOR_P is true, the id-expression is appearing as part of
5674 a declarator, rather than as part of an expression. */
5675
5676 static cp_expr
5677 cp_parser_id_expression (cp_parser *parser,
5678 bool template_keyword_p,
5679 bool check_dependency_p,
5680 bool *template_p,
5681 bool declarator_p,
5682 bool optional_p)
5683 {
5684 bool global_scope_p;
5685 bool nested_name_specifier_p;
5686
5687 /* Assume the `template' keyword was not used. */
5688 if (template_p)
5689 *template_p = template_keyword_p;
5690
5691 /* Look for the optional `::' operator. */
5692 global_scope_p
5693 = (!template_keyword_p
5694 && (cp_parser_global_scope_opt (parser,
5695 /*current_scope_valid_p=*/false)
5696 != NULL_TREE));
5697
5698 /* Look for the optional nested-name-specifier. */
5699 nested_name_specifier_p
5700 = (cp_parser_nested_name_specifier_opt (parser,
5701 /*typename_keyword_p=*/false,
5702 check_dependency_p,
5703 /*type_p=*/false,
5704 declarator_p,
5705 template_keyword_p)
5706 != NULL_TREE);
5707
5708 /* If there is a nested-name-specifier, then we are looking at
5709 the first qualified-id production. */
5710 if (nested_name_specifier_p)
5711 {
5712 tree saved_scope;
5713 tree saved_object_scope;
5714 tree saved_qualifying_scope;
5715 cp_expr unqualified_id;
5716 bool is_template;
5717
5718 /* See if the next token is the `template' keyword. */
5719 if (!template_p)
5720 template_p = &is_template;
5721 *template_p = cp_parser_optional_template_keyword (parser);
5722 /* Name lookup we do during the processing of the
5723 unqualified-id might obliterate SCOPE. */
5724 saved_scope = parser->scope;
5725 saved_object_scope = parser->object_scope;
5726 saved_qualifying_scope = parser->qualifying_scope;
5727 /* Process the final unqualified-id. */
5728 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5729 check_dependency_p,
5730 declarator_p,
5731 /*optional_p=*/false);
5732 /* Restore the SAVED_SCOPE for our caller. */
5733 parser->scope = saved_scope;
5734 parser->object_scope = saved_object_scope;
5735 parser->qualifying_scope = saved_qualifying_scope;
5736
5737 return unqualified_id;
5738 }
5739 /* Otherwise, if we are in global scope, then we are looking at one
5740 of the other qualified-id productions. */
5741 else if (global_scope_p)
5742 {
5743 cp_token *token;
5744 tree id;
5745
5746 /* Peek at the next token. */
5747 token = cp_lexer_peek_token (parser->lexer);
5748
5749 /* If it's an identifier, and the next token is not a "<", then
5750 we can avoid the template-id case. This is an optimization
5751 for this common case. */
5752 if (token->type == CPP_NAME
5753 && !cp_parser_nth_token_starts_template_argument_list_p
5754 (parser, 2))
5755 return cp_parser_identifier (parser);
5756
5757 cp_parser_parse_tentatively (parser);
5758 /* Try a template-id. */
5759 id = cp_parser_template_id (parser,
5760 /*template_keyword_p=*/false,
5761 /*check_dependency_p=*/true,
5762 none_type,
5763 declarator_p);
5764 /* If that worked, we're done. */
5765 if (cp_parser_parse_definitely (parser))
5766 return id;
5767
5768 /* Peek at the next token. (Changes in the token buffer may
5769 have invalidated the pointer obtained above.) */
5770 token = cp_lexer_peek_token (parser->lexer);
5771
5772 switch (token->type)
5773 {
5774 case CPP_NAME:
5775 return cp_parser_identifier (parser);
5776
5777 case CPP_KEYWORD:
5778 if (token->keyword == RID_OPERATOR)
5779 return cp_parser_operator_function_id (parser);
5780 /* Fall through. */
5781
5782 default:
5783 cp_parser_error (parser, "expected id-expression");
5784 return error_mark_node;
5785 }
5786 }
5787 else
5788 return cp_parser_unqualified_id (parser, template_keyword_p,
5789 /*check_dependency_p=*/true,
5790 declarator_p,
5791 optional_p);
5792 }
5793
5794 /* Parse an unqualified-id.
5795
5796 unqualified-id:
5797 identifier
5798 operator-function-id
5799 conversion-function-id
5800 ~ class-name
5801 template-id
5802
5803 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5804 keyword, in a construct like `A::template ...'.
5805
5806 Returns a representation of unqualified-id. For the `identifier'
5807 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5808 production a BIT_NOT_EXPR is returned; the operand of the
5809 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5810 other productions, see the documentation accompanying the
5811 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5812 names are looked up in uninstantiated templates. If DECLARATOR_P
5813 is true, the unqualified-id is appearing as part of a declarator,
5814 rather than as part of an expression. */
5815
5816 static cp_expr
5817 cp_parser_unqualified_id (cp_parser* parser,
5818 bool template_keyword_p,
5819 bool check_dependency_p,
5820 bool declarator_p,
5821 bool optional_p)
5822 {
5823 cp_token *token;
5824
5825 /* Peek at the next token. */
5826 token = cp_lexer_peek_token (parser->lexer);
5827
5828 switch ((int) token->type)
5829 {
5830 case CPP_NAME:
5831 {
5832 tree id;
5833
5834 /* We don't know yet whether or not this will be a
5835 template-id. */
5836 cp_parser_parse_tentatively (parser);
5837 /* Try a template-id. */
5838 id = cp_parser_template_id (parser, template_keyword_p,
5839 check_dependency_p,
5840 none_type,
5841 declarator_p);
5842 /* If it worked, we're done. */
5843 if (cp_parser_parse_definitely (parser))
5844 return id;
5845 /* Otherwise, it's an ordinary identifier. */
5846 return cp_parser_identifier (parser);
5847 }
5848
5849 case CPP_TEMPLATE_ID:
5850 return cp_parser_template_id (parser, template_keyword_p,
5851 check_dependency_p,
5852 none_type,
5853 declarator_p);
5854
5855 case CPP_COMPL:
5856 {
5857 tree type_decl;
5858 tree qualifying_scope;
5859 tree object_scope;
5860 tree scope;
5861 bool done;
5862
5863 /* Consume the `~' token. */
5864 cp_lexer_consume_token (parser->lexer);
5865 /* Parse the class-name. The standard, as written, seems to
5866 say that:
5867
5868 template <typename T> struct S { ~S (); };
5869 template <typename T> S<T>::~S() {}
5870
5871 is invalid, since `~' must be followed by a class-name, but
5872 `S<T>' is dependent, and so not known to be a class.
5873 That's not right; we need to look in uninstantiated
5874 templates. A further complication arises from:
5875
5876 template <typename T> void f(T t) {
5877 t.T::~T();
5878 }
5879
5880 Here, it is not possible to look up `T' in the scope of `T'
5881 itself. We must look in both the current scope, and the
5882 scope of the containing complete expression.
5883
5884 Yet another issue is:
5885
5886 struct S {
5887 int S;
5888 ~S();
5889 };
5890
5891 S::~S() {}
5892
5893 The standard does not seem to say that the `S' in `~S'
5894 should refer to the type `S' and not the data member
5895 `S::S'. */
5896
5897 /* DR 244 says that we look up the name after the "~" in the
5898 same scope as we looked up the qualifying name. That idea
5899 isn't fully worked out; it's more complicated than that. */
5900 scope = parser->scope;
5901 object_scope = parser->object_scope;
5902 qualifying_scope = parser->qualifying_scope;
5903
5904 /* Check for invalid scopes. */
5905 if (scope == error_mark_node)
5906 {
5907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5908 cp_lexer_consume_token (parser->lexer);
5909 return error_mark_node;
5910 }
5911 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5912 {
5913 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5914 error_at (token->location,
5915 "scope %qT before %<~%> is not a class-name",
5916 scope);
5917 cp_parser_simulate_error (parser);
5918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5919 cp_lexer_consume_token (parser->lexer);
5920 return error_mark_node;
5921 }
5922 gcc_assert (!scope || TYPE_P (scope));
5923
5924 /* If the name is of the form "X::~X" it's OK even if X is a
5925 typedef. */
5926 token = cp_lexer_peek_token (parser->lexer);
5927 if (scope
5928 && token->type == CPP_NAME
5929 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5930 != CPP_LESS)
5931 && (token->u.value == TYPE_IDENTIFIER (scope)
5932 || (CLASS_TYPE_P (scope)
5933 && constructor_name_p (token->u.value, scope))))
5934 {
5935 cp_lexer_consume_token (parser->lexer);
5936 return build_nt (BIT_NOT_EXPR, scope);
5937 }
5938
5939 /* ~auto means the destructor of whatever the object is. */
5940 if (cp_parser_is_keyword (token, RID_AUTO))
5941 {
5942 if (cxx_dialect < cxx14)
5943 pedwarn (input_location, 0,
5944 "%<~auto%> only available with "
5945 "-std=c++14 or -std=gnu++14");
5946 cp_lexer_consume_token (parser->lexer);
5947 return build_nt (BIT_NOT_EXPR, make_auto ());
5948 }
5949
5950 /* If there was an explicit qualification (S::~T), first look
5951 in the scope given by the qualification (i.e., S).
5952
5953 Note: in the calls to cp_parser_class_name below we pass
5954 typename_type so that lookup finds the injected-class-name
5955 rather than the constructor. */
5956 done = false;
5957 type_decl = NULL_TREE;
5958 if (scope)
5959 {
5960 cp_parser_parse_tentatively (parser);
5961 type_decl = cp_parser_class_name (parser,
5962 /*typename_keyword_p=*/false,
5963 /*template_keyword_p=*/false,
5964 typename_type,
5965 /*check_dependency=*/false,
5966 /*class_head_p=*/false,
5967 declarator_p);
5968 if (cp_parser_parse_definitely (parser))
5969 done = true;
5970 }
5971 /* In "N::S::~S", look in "N" as well. */
5972 if (!done && scope && qualifying_scope)
5973 {
5974 cp_parser_parse_tentatively (parser);
5975 parser->scope = qualifying_scope;
5976 parser->object_scope = NULL_TREE;
5977 parser->qualifying_scope = NULL_TREE;
5978 type_decl
5979 = cp_parser_class_name (parser,
5980 /*typename_keyword_p=*/false,
5981 /*template_keyword_p=*/false,
5982 typename_type,
5983 /*check_dependency=*/false,
5984 /*class_head_p=*/false,
5985 declarator_p);
5986 if (cp_parser_parse_definitely (parser))
5987 done = true;
5988 }
5989 /* In "p->S::~T", look in the scope given by "*p" as well. */
5990 else if (!done && object_scope)
5991 {
5992 cp_parser_parse_tentatively (parser);
5993 parser->scope = object_scope;
5994 parser->object_scope = NULL_TREE;
5995 parser->qualifying_scope = NULL_TREE;
5996 type_decl
5997 = cp_parser_class_name (parser,
5998 /*typename_keyword_p=*/false,
5999 /*template_keyword_p=*/false,
6000 typename_type,
6001 /*check_dependency=*/false,
6002 /*class_head_p=*/false,
6003 declarator_p);
6004 if (cp_parser_parse_definitely (parser))
6005 done = true;
6006 }
6007 /* Look in the surrounding context. */
6008 if (!done)
6009 {
6010 parser->scope = NULL_TREE;
6011 parser->object_scope = NULL_TREE;
6012 parser->qualifying_scope = NULL_TREE;
6013 if (processing_template_decl)
6014 cp_parser_parse_tentatively (parser);
6015 type_decl
6016 = cp_parser_class_name (parser,
6017 /*typename_keyword_p=*/false,
6018 /*template_keyword_p=*/false,
6019 typename_type,
6020 /*check_dependency=*/false,
6021 /*class_head_p=*/false,
6022 declarator_p);
6023 if (processing_template_decl
6024 && ! cp_parser_parse_definitely (parser))
6025 {
6026 /* We couldn't find a type with this name. If we're parsing
6027 tentatively, fail and try something else. */
6028 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6029 {
6030 cp_parser_simulate_error (parser);
6031 return error_mark_node;
6032 }
6033 /* Otherwise, accept it and check for a match at instantiation
6034 time. */
6035 type_decl = cp_parser_identifier (parser);
6036 if (type_decl != error_mark_node)
6037 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6038 return type_decl;
6039 }
6040 }
6041 /* If an error occurred, assume that the name of the
6042 destructor is the same as the name of the qualifying
6043 class. That allows us to keep parsing after running
6044 into ill-formed destructor names. */
6045 if (type_decl == error_mark_node && scope)
6046 return build_nt (BIT_NOT_EXPR, scope);
6047 else if (type_decl == error_mark_node)
6048 return error_mark_node;
6049
6050 /* Check that destructor name and scope match. */
6051 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6052 {
6053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6054 error_at (token->location,
6055 "declaration of %<~%T%> as member of %qT",
6056 type_decl, scope);
6057 cp_parser_simulate_error (parser);
6058 return error_mark_node;
6059 }
6060
6061 /* [class.dtor]
6062
6063 A typedef-name that names a class shall not be used as the
6064 identifier in the declarator for a destructor declaration. */
6065 if (declarator_p
6066 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6067 && !DECL_SELF_REFERENCE_P (type_decl)
6068 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6069 error_at (token->location,
6070 "typedef-name %qD used as destructor declarator",
6071 type_decl);
6072
6073 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6074 }
6075
6076 case CPP_KEYWORD:
6077 if (token->keyword == RID_OPERATOR)
6078 {
6079 cp_expr id;
6080
6081 /* This could be a template-id, so we try that first. */
6082 cp_parser_parse_tentatively (parser);
6083 /* Try a template-id. */
6084 id = cp_parser_template_id (parser, template_keyword_p,
6085 /*check_dependency_p=*/true,
6086 none_type,
6087 declarator_p);
6088 /* If that worked, we're done. */
6089 if (cp_parser_parse_definitely (parser))
6090 return id;
6091 /* We still don't know whether we're looking at an
6092 operator-function-id or a conversion-function-id. */
6093 cp_parser_parse_tentatively (parser);
6094 /* Try an operator-function-id. */
6095 id = cp_parser_operator_function_id (parser);
6096 /* If that didn't work, try a conversion-function-id. */
6097 if (!cp_parser_parse_definitely (parser))
6098 id = cp_parser_conversion_function_id (parser);
6099 else if (UDLIT_OPER_P (id))
6100 {
6101 /* 17.6.3.3.5 */
6102 const char *name = UDLIT_OP_SUFFIX (id);
6103 if (name[0] != '_' && !in_system_header_at (input_location)
6104 && declarator_p)
6105 warning (OPT_Wliteral_suffix,
6106 "literal operator suffixes not preceded by %<_%>"
6107 " are reserved for future standardization");
6108 }
6109
6110 return id;
6111 }
6112 /* Fall through. */
6113
6114 default:
6115 if (optional_p)
6116 return NULL_TREE;
6117 cp_parser_error (parser, "expected unqualified-id");
6118 return error_mark_node;
6119 }
6120 }
6121
6122 /* Parse an (optional) nested-name-specifier.
6123
6124 nested-name-specifier: [C++98]
6125 class-or-namespace-name :: nested-name-specifier [opt]
6126 class-or-namespace-name :: template nested-name-specifier [opt]
6127
6128 nested-name-specifier: [C++0x]
6129 type-name ::
6130 namespace-name ::
6131 nested-name-specifier identifier ::
6132 nested-name-specifier template [opt] simple-template-id ::
6133
6134 PARSER->SCOPE should be set appropriately before this function is
6135 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6136 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6137 in name lookups.
6138
6139 Sets PARSER->SCOPE to the class (TYPE) or namespace
6140 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6141 it unchanged if there is no nested-name-specifier. Returns the new
6142 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6143
6144 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6145 part of a declaration and/or decl-specifier. */
6146
6147 static tree
6148 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6149 bool typename_keyword_p,
6150 bool check_dependency_p,
6151 bool type_p,
6152 bool is_declaration,
6153 bool template_keyword_p /* = false */)
6154 {
6155 bool success = false;
6156 cp_token_position start = 0;
6157 cp_token *token;
6158
6159 /* Remember where the nested-name-specifier starts. */
6160 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6161 {
6162 start = cp_lexer_token_position (parser->lexer, false);
6163 push_deferring_access_checks (dk_deferred);
6164 }
6165
6166 while (true)
6167 {
6168 tree new_scope;
6169 tree old_scope;
6170 tree saved_qualifying_scope;
6171
6172 /* Spot cases that cannot be the beginning of a
6173 nested-name-specifier. */
6174 token = cp_lexer_peek_token (parser->lexer);
6175
6176 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6177 the already parsed nested-name-specifier. */
6178 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6179 {
6180 /* Grab the nested-name-specifier and continue the loop. */
6181 cp_parser_pre_parsed_nested_name_specifier (parser);
6182 /* If we originally encountered this nested-name-specifier
6183 with IS_DECLARATION set to false, we will not have
6184 resolved TYPENAME_TYPEs, so we must do so here. */
6185 if (is_declaration
6186 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6187 {
6188 new_scope = resolve_typename_type (parser->scope,
6189 /*only_current_p=*/false);
6190 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6191 parser->scope = new_scope;
6192 }
6193 success = true;
6194 continue;
6195 }
6196
6197 /* Spot cases that cannot be the beginning of a
6198 nested-name-specifier. On the second and subsequent times
6199 through the loop, we look for the `template' keyword. */
6200 if (success && token->keyword == RID_TEMPLATE)
6201 ;
6202 /* A template-id can start a nested-name-specifier. */
6203 else if (token->type == CPP_TEMPLATE_ID)
6204 ;
6205 /* DR 743: decltype can be used in a nested-name-specifier. */
6206 else if (token_is_decltype (token))
6207 ;
6208 else
6209 {
6210 /* If the next token is not an identifier, then it is
6211 definitely not a type-name or namespace-name. */
6212 if (token->type != CPP_NAME)
6213 break;
6214 /* If the following token is neither a `<' (to begin a
6215 template-id), nor a `::', then we are not looking at a
6216 nested-name-specifier. */
6217 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6218
6219 if (token->type == CPP_COLON
6220 && parser->colon_corrects_to_scope_p
6221 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6222 {
6223 gcc_rich_location richloc (token->location);
6224 richloc.add_fixit_replace ("::");
6225 error_at (&richloc,
6226 "found %<:%> in nested-name-specifier, "
6227 "expected %<::%>");
6228 token->type = CPP_SCOPE;
6229 }
6230
6231 if (token->type != CPP_SCOPE
6232 && !cp_parser_nth_token_starts_template_argument_list_p
6233 (parser, 2))
6234 break;
6235 }
6236
6237 /* The nested-name-specifier is optional, so we parse
6238 tentatively. */
6239 cp_parser_parse_tentatively (parser);
6240
6241 /* Look for the optional `template' keyword, if this isn't the
6242 first time through the loop. */
6243 if (success)
6244 template_keyword_p = cp_parser_optional_template_keyword (parser);
6245
6246 /* Save the old scope since the name lookup we are about to do
6247 might destroy it. */
6248 old_scope = parser->scope;
6249 saved_qualifying_scope = parser->qualifying_scope;
6250 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6251 look up names in "X<T>::I" in order to determine that "Y" is
6252 a template. So, if we have a typename at this point, we make
6253 an effort to look through it. */
6254 if (is_declaration
6255 && !typename_keyword_p
6256 && parser->scope
6257 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6258 parser->scope = resolve_typename_type (parser->scope,
6259 /*only_current_p=*/false);
6260 /* Parse the qualifying entity. */
6261 new_scope
6262 = cp_parser_qualifying_entity (parser,
6263 typename_keyword_p,
6264 template_keyword_p,
6265 check_dependency_p,
6266 type_p,
6267 is_declaration);
6268 /* Look for the `::' token. */
6269 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6270
6271 /* If we found what we wanted, we keep going; otherwise, we're
6272 done. */
6273 if (!cp_parser_parse_definitely (parser))
6274 {
6275 bool error_p = false;
6276
6277 /* Restore the OLD_SCOPE since it was valid before the
6278 failed attempt at finding the last
6279 class-or-namespace-name. */
6280 parser->scope = old_scope;
6281 parser->qualifying_scope = saved_qualifying_scope;
6282
6283 /* If the next token is a decltype, and the one after that is a
6284 `::', then the decltype has failed to resolve to a class or
6285 enumeration type. Give this error even when parsing
6286 tentatively since it can't possibly be valid--and we're going
6287 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6288 won't get another chance.*/
6289 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6290 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6291 == CPP_SCOPE))
6292 {
6293 token = cp_lexer_consume_token (parser->lexer);
6294 error_at (token->location, "decltype evaluates to %qT, "
6295 "which is not a class or enumeration type",
6296 token->u.tree_check_value->value);
6297 parser->scope = error_mark_node;
6298 error_p = true;
6299 /* As below. */
6300 success = true;
6301 cp_lexer_consume_token (parser->lexer);
6302 }
6303
6304 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6305 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6306 {
6307 /* If we have a non-type template-id followed by ::, it can't
6308 possibly be valid. */
6309 token = cp_lexer_peek_token (parser->lexer);
6310 tree tid = token->u.tree_check_value->value;
6311 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6312 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6313 {
6314 tree tmpl = NULL_TREE;
6315 if (is_overloaded_fn (tid))
6316 {
6317 tree fns = get_fns (tid);
6318 if (OVL_SINGLE_P (fns))
6319 tmpl = OVL_FIRST (fns);
6320 error_at (token->location, "function template-id %qD "
6321 "in nested-name-specifier", tid);
6322 }
6323 else
6324 {
6325 /* Variable template. */
6326 tmpl = TREE_OPERAND (tid, 0);
6327 gcc_assert (variable_template_p (tmpl));
6328 error_at (token->location, "variable template-id %qD "
6329 "in nested-name-specifier", tid);
6330 }
6331 if (tmpl)
6332 inform (DECL_SOURCE_LOCATION (tmpl),
6333 "%qD declared here", tmpl);
6334
6335 parser->scope = error_mark_node;
6336 error_p = true;
6337 /* As below. */
6338 success = true;
6339 cp_lexer_consume_token (parser->lexer);
6340 cp_lexer_consume_token (parser->lexer);
6341 }
6342 }
6343
6344 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6345 break;
6346 /* If the next token is an identifier, and the one after
6347 that is a `::', then any valid interpretation would have
6348 found a class-or-namespace-name. */
6349 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6350 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6351 == CPP_SCOPE)
6352 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6353 != CPP_COMPL))
6354 {
6355 token = cp_lexer_consume_token (parser->lexer);
6356 if (!error_p)
6357 {
6358 if (!token->error_reported)
6359 {
6360 tree decl;
6361 tree ambiguous_decls;
6362
6363 decl = cp_parser_lookup_name (parser, token->u.value,
6364 none_type,
6365 /*is_template=*/false,
6366 /*is_namespace=*/false,
6367 /*check_dependency=*/true,
6368 &ambiguous_decls,
6369 token->location);
6370 if (TREE_CODE (decl) == TEMPLATE_DECL)
6371 error_at (token->location,
6372 "%qD used without template parameters",
6373 decl);
6374 else if (ambiguous_decls)
6375 {
6376 // cp_parser_lookup_name has the same diagnostic,
6377 // thus make sure to emit it at most once.
6378 if (cp_parser_uncommitted_to_tentative_parse_p
6379 (parser))
6380 {
6381 error_at (token->location,
6382 "reference to %qD is ambiguous",
6383 token->u.value);
6384 print_candidates (ambiguous_decls);
6385 }
6386 decl = error_mark_node;
6387 }
6388 else
6389 {
6390 if (cxx_dialect != cxx98)
6391 cp_parser_name_lookup_error
6392 (parser, token->u.value, decl, NLE_NOT_CXX98,
6393 token->location);
6394 else
6395 cp_parser_name_lookup_error
6396 (parser, token->u.value, decl, NLE_CXX98,
6397 token->location);
6398 }
6399 }
6400 parser->scope = error_mark_node;
6401 error_p = true;
6402 /* Treat this as a successful nested-name-specifier
6403 due to:
6404
6405 [basic.lookup.qual]
6406
6407 If the name found is not a class-name (clause
6408 _class_) or namespace-name (_namespace.def_), the
6409 program is ill-formed. */
6410 success = true;
6411 }
6412 cp_lexer_consume_token (parser->lexer);
6413 }
6414 break;
6415 }
6416 /* We've found one valid nested-name-specifier. */
6417 success = true;
6418 /* Name lookup always gives us a DECL. */
6419 if (TREE_CODE (new_scope) == TYPE_DECL)
6420 new_scope = TREE_TYPE (new_scope);
6421 /* Uses of "template" must be followed by actual templates. */
6422 if (template_keyword_p
6423 && !(CLASS_TYPE_P (new_scope)
6424 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6425 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6426 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6427 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6428 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6429 == TEMPLATE_ID_EXPR)))
6430 permerror (input_location, TYPE_P (new_scope)
6431 ? G_("%qT is not a template")
6432 : G_("%qD is not a template"),
6433 new_scope);
6434 /* If it is a class scope, try to complete it; we are about to
6435 be looking up names inside the class. */
6436 if (TYPE_P (new_scope)
6437 /* Since checking types for dependency can be expensive,
6438 avoid doing it if the type is already complete. */
6439 && !COMPLETE_TYPE_P (new_scope)
6440 /* Do not try to complete dependent types. */
6441 && !dependent_type_p (new_scope))
6442 {
6443 new_scope = complete_type (new_scope);
6444 /* If it is a typedef to current class, use the current
6445 class instead, as the typedef won't have any names inside
6446 it yet. */
6447 if (!COMPLETE_TYPE_P (new_scope)
6448 && currently_open_class (new_scope))
6449 new_scope = TYPE_MAIN_VARIANT (new_scope);
6450 }
6451 /* Make sure we look in the right scope the next time through
6452 the loop. */
6453 parser->scope = new_scope;
6454 }
6455
6456 /* If parsing tentatively, replace the sequence of tokens that makes
6457 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6458 token. That way, should we re-parse the token stream, we will
6459 not have to repeat the effort required to do the parse, nor will
6460 we issue duplicate error messages. */
6461 if (success && start)
6462 {
6463 cp_token *token;
6464
6465 token = cp_lexer_token_at (parser->lexer, start);
6466 /* Reset the contents of the START token. */
6467 token->type = CPP_NESTED_NAME_SPECIFIER;
6468 /* Retrieve any deferred checks. Do not pop this access checks yet
6469 so the memory will not be reclaimed during token replacing below. */
6470 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6471 token->u.tree_check_value->value = parser->scope;
6472 token->u.tree_check_value->checks = get_deferred_access_checks ();
6473 token->u.tree_check_value->qualifying_scope =
6474 parser->qualifying_scope;
6475 token->keyword = RID_MAX;
6476
6477 /* Purge all subsequent tokens. */
6478 cp_lexer_purge_tokens_after (parser->lexer, start);
6479 }
6480
6481 if (start)
6482 pop_to_parent_deferring_access_checks ();
6483
6484 return success ? parser->scope : NULL_TREE;
6485 }
6486
6487 /* Parse a nested-name-specifier. See
6488 cp_parser_nested_name_specifier_opt for details. This function
6489 behaves identically, except that it will an issue an error if no
6490 nested-name-specifier is present. */
6491
6492 static tree
6493 cp_parser_nested_name_specifier (cp_parser *parser,
6494 bool typename_keyword_p,
6495 bool check_dependency_p,
6496 bool type_p,
6497 bool is_declaration)
6498 {
6499 tree scope;
6500
6501 /* Look for the nested-name-specifier. */
6502 scope = cp_parser_nested_name_specifier_opt (parser,
6503 typename_keyword_p,
6504 check_dependency_p,
6505 type_p,
6506 is_declaration);
6507 /* If it was not present, issue an error message. */
6508 if (!scope)
6509 {
6510 cp_parser_error (parser, "expected nested-name-specifier");
6511 parser->scope = NULL_TREE;
6512 }
6513
6514 return scope;
6515 }
6516
6517 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6518 this is either a class-name or a namespace-name (which corresponds
6519 to the class-or-namespace-name production in the grammar). For
6520 C++0x, it can also be a type-name that refers to an enumeration
6521 type or a simple-template-id.
6522
6523 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6524 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6525 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6526 TYPE_P is TRUE iff the next name should be taken as a class-name,
6527 even the same name is declared to be another entity in the same
6528 scope.
6529
6530 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6531 specified by the class-or-namespace-name. If neither is found the
6532 ERROR_MARK_NODE is returned. */
6533
6534 static tree
6535 cp_parser_qualifying_entity (cp_parser *parser,
6536 bool typename_keyword_p,
6537 bool template_keyword_p,
6538 bool check_dependency_p,
6539 bool type_p,
6540 bool is_declaration)
6541 {
6542 tree saved_scope;
6543 tree saved_qualifying_scope;
6544 tree saved_object_scope;
6545 tree scope;
6546 bool only_class_p;
6547 bool successful_parse_p;
6548
6549 /* DR 743: decltype can appear in a nested-name-specifier. */
6550 if (cp_lexer_next_token_is_decltype (parser->lexer))
6551 {
6552 scope = cp_parser_decltype (parser);
6553 if (TREE_CODE (scope) != ENUMERAL_TYPE
6554 && !MAYBE_CLASS_TYPE_P (scope))
6555 {
6556 cp_parser_simulate_error (parser);
6557 return error_mark_node;
6558 }
6559 if (TYPE_NAME (scope))
6560 scope = TYPE_NAME (scope);
6561 return scope;
6562 }
6563
6564 /* Before we try to parse the class-name, we must save away the
6565 current PARSER->SCOPE since cp_parser_class_name will destroy
6566 it. */
6567 saved_scope = parser->scope;
6568 saved_qualifying_scope = parser->qualifying_scope;
6569 saved_object_scope = parser->object_scope;
6570 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6571 there is no need to look for a namespace-name. */
6572 only_class_p = template_keyword_p
6573 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6574 if (!only_class_p)
6575 cp_parser_parse_tentatively (parser);
6576 scope = cp_parser_class_name (parser,
6577 typename_keyword_p,
6578 template_keyword_p,
6579 type_p ? class_type : none_type,
6580 check_dependency_p,
6581 /*class_head_p=*/false,
6582 is_declaration,
6583 /*enum_ok=*/cxx_dialect > cxx98);
6584 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6585 /* If that didn't work, try for a namespace-name. */
6586 if (!only_class_p && !successful_parse_p)
6587 {
6588 /* Restore the saved scope. */
6589 parser->scope = saved_scope;
6590 parser->qualifying_scope = saved_qualifying_scope;
6591 parser->object_scope = saved_object_scope;
6592 /* If we are not looking at an identifier followed by the scope
6593 resolution operator, then this is not part of a
6594 nested-name-specifier. (Note that this function is only used
6595 to parse the components of a nested-name-specifier.) */
6596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6597 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6598 return error_mark_node;
6599 scope = cp_parser_namespace_name (parser);
6600 }
6601
6602 return scope;
6603 }
6604
6605 /* Return true if we are looking at a compound-literal, false otherwise. */
6606
6607 static bool
6608 cp_parser_compound_literal_p (cp_parser *parser)
6609 {
6610 cp_lexer_save_tokens (parser->lexer);
6611
6612 /* Skip tokens until the next token is a closing parenthesis.
6613 If we find the closing `)', and the next token is a `{', then
6614 we are looking at a compound-literal. */
6615 bool compound_literal_p
6616 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6617 /*consume_paren=*/true)
6618 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6619
6620 /* Roll back the tokens we skipped. */
6621 cp_lexer_rollback_tokens (parser->lexer);
6622
6623 return compound_literal_p;
6624 }
6625
6626 /* Return true if EXPR is the integer constant zero or a complex constant
6627 of zero, without any folding, but ignoring location wrappers. */
6628
6629 static bool
6630 literal_integer_zerop (const_tree expr)
6631 {
6632 STRIP_ANY_LOCATION_WRAPPER (expr);
6633 return integer_zerop (expr);
6634 }
6635
6636 /* Parse a postfix-expression.
6637
6638 postfix-expression:
6639 primary-expression
6640 postfix-expression [ expression ]
6641 postfix-expression ( expression-list [opt] )
6642 simple-type-specifier ( expression-list [opt] )
6643 typename :: [opt] nested-name-specifier identifier
6644 ( expression-list [opt] )
6645 typename :: [opt] nested-name-specifier template [opt] template-id
6646 ( expression-list [opt] )
6647 postfix-expression . template [opt] id-expression
6648 postfix-expression -> template [opt] id-expression
6649 postfix-expression . pseudo-destructor-name
6650 postfix-expression -> pseudo-destructor-name
6651 postfix-expression ++
6652 postfix-expression --
6653 dynamic_cast < type-id > ( expression )
6654 static_cast < type-id > ( expression )
6655 reinterpret_cast < type-id > ( expression )
6656 const_cast < type-id > ( expression )
6657 typeid ( expression )
6658 typeid ( type-id )
6659
6660 GNU Extension:
6661
6662 postfix-expression:
6663 ( type-id ) { initializer-list , [opt] }
6664
6665 This extension is a GNU version of the C99 compound-literal
6666 construct. (The C99 grammar uses `type-name' instead of `type-id',
6667 but they are essentially the same concept.)
6668
6669 If ADDRESS_P is true, the postfix expression is the operand of the
6670 `&' operator. CAST_P is true if this expression is the target of a
6671 cast.
6672
6673 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6674 class member access expressions [expr.ref].
6675
6676 Returns a representation of the expression. */
6677
6678 static cp_expr
6679 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6680 bool member_access_only_p, bool decltype_p,
6681 cp_id_kind * pidk_return)
6682 {
6683 cp_token *token;
6684 location_t loc;
6685 enum rid keyword;
6686 cp_id_kind idk = CP_ID_KIND_NONE;
6687 cp_expr postfix_expression = NULL_TREE;
6688 bool is_member_access = false;
6689
6690 /* Peek at the next token. */
6691 token = cp_lexer_peek_token (parser->lexer);
6692 loc = token->location;
6693 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6694
6695 /* Some of the productions are determined by keywords. */
6696 keyword = token->keyword;
6697 switch (keyword)
6698 {
6699 case RID_DYNCAST:
6700 case RID_STATCAST:
6701 case RID_REINTCAST:
6702 case RID_CONSTCAST:
6703 {
6704 tree type;
6705 cp_expr expression;
6706 const char *saved_message;
6707 bool saved_in_type_id_in_expr_p;
6708
6709 /* All of these can be handled in the same way from the point
6710 of view of parsing. Begin by consuming the token
6711 identifying the cast. */
6712 cp_lexer_consume_token (parser->lexer);
6713
6714 /* New types cannot be defined in the cast. */
6715 saved_message = parser->type_definition_forbidden_message;
6716 parser->type_definition_forbidden_message
6717 = G_("types may not be defined in casts");
6718
6719 /* Look for the opening `<'. */
6720 cp_parser_require (parser, CPP_LESS, RT_LESS);
6721 /* Parse the type to which we are casting. */
6722 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6723 parser->in_type_id_in_expr_p = true;
6724 type = cp_parser_type_id (parser);
6725 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6726 /* Look for the closing `>'. */
6727 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6728 /* Restore the old message. */
6729 parser->type_definition_forbidden_message = saved_message;
6730
6731 bool saved_greater_than_is_operator_p
6732 = parser->greater_than_is_operator_p;
6733 parser->greater_than_is_operator_p = true;
6734
6735 /* And the expression which is being cast. */
6736 matching_parens parens;
6737 parens.require_open (parser);
6738 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6739 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6740 RT_CLOSE_PAREN);
6741 location_t end_loc = close_paren ?
6742 close_paren->location : UNKNOWN_LOCATION;
6743
6744 parser->greater_than_is_operator_p
6745 = saved_greater_than_is_operator_p;
6746
6747 /* Only type conversions to integral or enumeration types
6748 can be used in constant-expressions. */
6749 if (!cast_valid_in_integral_constant_expression_p (type)
6750 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6751 {
6752 postfix_expression = error_mark_node;
6753 break;
6754 }
6755
6756 switch (keyword)
6757 {
6758 case RID_DYNCAST:
6759 postfix_expression
6760 = build_dynamic_cast (type, expression, tf_warning_or_error);
6761 break;
6762 case RID_STATCAST:
6763 postfix_expression
6764 = build_static_cast (type, expression, tf_warning_or_error);
6765 break;
6766 case RID_REINTCAST:
6767 postfix_expression
6768 = build_reinterpret_cast (type, expression,
6769 tf_warning_or_error);
6770 break;
6771 case RID_CONSTCAST:
6772 postfix_expression
6773 = build_const_cast (type, expression, tf_warning_or_error);
6774 break;
6775 default:
6776 gcc_unreachable ();
6777 }
6778
6779 /* Construct a location e.g. :
6780 reinterpret_cast <int *> (expr)
6781 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6782 ranging from the start of the "*_cast" token to the final closing
6783 paren, with the caret at the start. */
6784 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6785 postfix_expression.set_location (cp_cast_loc);
6786 }
6787 break;
6788
6789 case RID_TYPEID:
6790 {
6791 tree type;
6792 const char *saved_message;
6793 bool saved_in_type_id_in_expr_p;
6794
6795 /* Consume the `typeid' token. */
6796 cp_lexer_consume_token (parser->lexer);
6797 /* Look for the `(' token. */
6798 matching_parens parens;
6799 parens.require_open (parser);
6800 /* Types cannot be defined in a `typeid' expression. */
6801 saved_message = parser->type_definition_forbidden_message;
6802 parser->type_definition_forbidden_message
6803 = G_("types may not be defined in a %<typeid%> expression");
6804 /* We can't be sure yet whether we're looking at a type-id or an
6805 expression. */
6806 cp_parser_parse_tentatively (parser);
6807 /* Try a type-id first. */
6808 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6809 parser->in_type_id_in_expr_p = true;
6810 type = cp_parser_type_id (parser);
6811 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6812 /* Look for the `)' token. Otherwise, we can't be sure that
6813 we're not looking at an expression: consider `typeid (int
6814 (3))', for example. */
6815 cp_token *close_paren = parens.require_close (parser);
6816 /* If all went well, simply lookup the type-id. */
6817 if (cp_parser_parse_definitely (parser))
6818 postfix_expression = get_typeid (type, tf_warning_or_error);
6819 /* Otherwise, fall back to the expression variant. */
6820 else
6821 {
6822 tree expression;
6823
6824 /* Look for an expression. */
6825 expression = cp_parser_expression (parser, & idk);
6826 /* Compute its typeid. */
6827 postfix_expression = build_typeid (expression, tf_warning_or_error);
6828 /* Look for the `)' token. */
6829 close_paren = parens.require_close (parser);
6830 }
6831 /* Restore the saved message. */
6832 parser->type_definition_forbidden_message = saved_message;
6833 /* `typeid' may not appear in an integral constant expression. */
6834 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6835 postfix_expression = error_mark_node;
6836
6837 /* Construct a location e.g. :
6838 typeid (expr)
6839 ^~~~~~~~~~~~~
6840 ranging from the start of the "typeid" token to the final closing
6841 paren, with the caret at the start. */
6842 if (close_paren)
6843 {
6844 location_t typeid_loc
6845 = make_location (start_loc, start_loc, close_paren->location);
6846 postfix_expression.set_location (typeid_loc);
6847 postfix_expression.maybe_add_location_wrapper ();
6848 }
6849 }
6850 break;
6851
6852 case RID_TYPENAME:
6853 {
6854 tree type;
6855 /* The syntax permitted here is the same permitted for an
6856 elaborated-type-specifier. */
6857 ++parser->prevent_constrained_type_specifiers;
6858 type = cp_parser_elaborated_type_specifier (parser,
6859 /*is_friend=*/false,
6860 /*is_declaration=*/false);
6861 --parser->prevent_constrained_type_specifiers;
6862 postfix_expression = cp_parser_functional_cast (parser, type);
6863 }
6864 break;
6865
6866 case RID_ADDRESSOF:
6867 case RID_BUILTIN_SHUFFLE:
6868 case RID_BUILTIN_LAUNDER:
6869 {
6870 vec<tree, va_gc> *vec;
6871 unsigned int i;
6872 tree p;
6873
6874 cp_lexer_consume_token (parser->lexer);
6875 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6876 /*cast_p=*/false, /*allow_expansion_p=*/true,
6877 /*non_constant_p=*/NULL);
6878 if (vec == NULL)
6879 {
6880 postfix_expression = error_mark_node;
6881 break;
6882 }
6883
6884 FOR_EACH_VEC_ELT (*vec, i, p)
6885 mark_exp_read (p);
6886
6887 switch (keyword)
6888 {
6889 case RID_ADDRESSOF:
6890 if (vec->length () == 1)
6891 postfix_expression
6892 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6893 else
6894 {
6895 error_at (loc, "wrong number of arguments to "
6896 "%<__builtin_addressof%>");
6897 postfix_expression = error_mark_node;
6898 }
6899 break;
6900
6901 case RID_BUILTIN_LAUNDER:
6902 if (vec->length () == 1)
6903 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6904 tf_warning_or_error);
6905 else
6906 {
6907 error_at (loc, "wrong number of arguments to "
6908 "%<__builtin_launder%>");
6909 postfix_expression = error_mark_node;
6910 }
6911 break;
6912
6913 case RID_BUILTIN_SHUFFLE:
6914 if (vec->length () == 2)
6915 postfix_expression
6916 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6917 (*vec)[1], tf_warning_or_error);
6918 else if (vec->length () == 3)
6919 postfix_expression
6920 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6921 (*vec)[2], tf_warning_or_error);
6922 else
6923 {
6924 error_at (loc, "wrong number of arguments to "
6925 "%<__builtin_shuffle%>");
6926 postfix_expression = error_mark_node;
6927 }
6928 break;
6929
6930 default:
6931 gcc_unreachable ();
6932 }
6933 break;
6934 }
6935
6936 default:
6937 {
6938 tree type;
6939
6940 /* If the next thing is a simple-type-specifier, we may be
6941 looking at a functional cast. We could also be looking at
6942 an id-expression. So, we try the functional cast, and if
6943 that doesn't work we fall back to the primary-expression. */
6944 cp_parser_parse_tentatively (parser);
6945 /* Look for the simple-type-specifier. */
6946 ++parser->prevent_constrained_type_specifiers;
6947 type = cp_parser_simple_type_specifier (parser,
6948 /*decl_specs=*/NULL,
6949 CP_PARSER_FLAGS_NONE);
6950 --parser->prevent_constrained_type_specifiers;
6951 /* Parse the cast itself. */
6952 if (!cp_parser_error_occurred (parser))
6953 postfix_expression
6954 = cp_parser_functional_cast (parser, type);
6955 /* If that worked, we're done. */
6956 if (cp_parser_parse_definitely (parser))
6957 break;
6958
6959 /* If the functional-cast didn't work out, try a
6960 compound-literal. */
6961 if (cp_parser_allow_gnu_extensions_p (parser)
6962 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6963 {
6964 cp_expr initializer = NULL_TREE;
6965
6966 cp_parser_parse_tentatively (parser);
6967
6968 matching_parens parens;
6969 parens.consume_open (parser);
6970
6971 /* Avoid calling cp_parser_type_id pointlessly, see comment
6972 in cp_parser_cast_expression about c++/29234. */
6973 if (!cp_parser_compound_literal_p (parser))
6974 cp_parser_simulate_error (parser);
6975 else
6976 {
6977 /* Parse the type. */
6978 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6979 parser->in_type_id_in_expr_p = true;
6980 type = cp_parser_type_id (parser);
6981 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6982 parens.require_close (parser);
6983 }
6984
6985 /* If things aren't going well, there's no need to
6986 keep going. */
6987 if (!cp_parser_error_occurred (parser))
6988 {
6989 bool non_constant_p;
6990 /* Parse the brace-enclosed initializer list. */
6991 initializer = cp_parser_braced_list (parser,
6992 &non_constant_p);
6993 }
6994 /* If that worked, we're definitely looking at a
6995 compound-literal expression. */
6996 if (cp_parser_parse_definitely (parser))
6997 {
6998 /* Warn the user that a compound literal is not
6999 allowed in standard C++. */
7000 pedwarn (input_location, OPT_Wpedantic,
7001 "ISO C++ forbids compound-literals");
7002 /* For simplicity, we disallow compound literals in
7003 constant-expressions. We could
7004 allow compound literals of integer type, whose
7005 initializer was a constant, in constant
7006 expressions. Permitting that usage, as a further
7007 extension, would not change the meaning of any
7008 currently accepted programs. (Of course, as
7009 compound literals are not part of ISO C++, the
7010 standard has nothing to say.) */
7011 if (cp_parser_non_integral_constant_expression (parser,
7012 NIC_NCC))
7013 {
7014 postfix_expression = error_mark_node;
7015 break;
7016 }
7017 /* Form the representation of the compound-literal. */
7018 postfix_expression
7019 = finish_compound_literal (type, initializer,
7020 tf_warning_or_error, fcl_c99);
7021 postfix_expression.set_location (initializer.get_location ());
7022 break;
7023 }
7024 }
7025
7026 /* It must be a primary-expression. */
7027 postfix_expression
7028 = cp_parser_primary_expression (parser, address_p, cast_p,
7029 /*template_arg_p=*/false,
7030 decltype_p,
7031 &idk);
7032 }
7033 break;
7034 }
7035
7036 /* Note that we don't need to worry about calling build_cplus_new on a
7037 class-valued CALL_EXPR in decltype when it isn't the end of the
7038 postfix-expression; unary_complex_lvalue will take care of that for
7039 all these cases. */
7040
7041 /* Keep looping until the postfix-expression is complete. */
7042 while (true)
7043 {
7044 if (idk == CP_ID_KIND_UNQUALIFIED
7045 && identifier_p (postfix_expression)
7046 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7047 /* It is not a Koenig lookup function call. */
7048 postfix_expression
7049 = unqualified_name_lookup_error (postfix_expression);
7050
7051 /* Peek at the next token. */
7052 token = cp_lexer_peek_token (parser->lexer);
7053
7054 switch (token->type)
7055 {
7056 case CPP_OPEN_SQUARE:
7057 if (cp_next_tokens_can_be_std_attribute_p (parser))
7058 {
7059 cp_parser_error (parser,
7060 "two consecutive %<[%> shall "
7061 "only introduce an attribute");
7062 return error_mark_node;
7063 }
7064 postfix_expression
7065 = cp_parser_postfix_open_square_expression (parser,
7066 postfix_expression,
7067 false,
7068 decltype_p);
7069 postfix_expression.set_range (start_loc,
7070 postfix_expression.get_location ());
7071
7072 idk = CP_ID_KIND_NONE;
7073 is_member_access = false;
7074 break;
7075
7076 case CPP_OPEN_PAREN:
7077 /* postfix-expression ( expression-list [opt] ) */
7078 {
7079 bool koenig_p;
7080 bool is_builtin_constant_p;
7081 bool saved_integral_constant_expression_p = false;
7082 bool saved_non_integral_constant_expression_p = false;
7083 tsubst_flags_t complain = complain_flags (decltype_p);
7084 vec<tree, va_gc> *args;
7085 location_t close_paren_loc = UNKNOWN_LOCATION;
7086
7087 is_member_access = false;
7088
7089 is_builtin_constant_p
7090 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7091 if (is_builtin_constant_p)
7092 {
7093 /* The whole point of __builtin_constant_p is to allow
7094 non-constant expressions to appear as arguments. */
7095 saved_integral_constant_expression_p
7096 = parser->integral_constant_expression_p;
7097 saved_non_integral_constant_expression_p
7098 = parser->non_integral_constant_expression_p;
7099 parser->integral_constant_expression_p = false;
7100 }
7101 args = (cp_parser_parenthesized_expression_list
7102 (parser, non_attr,
7103 /*cast_p=*/false, /*allow_expansion_p=*/true,
7104 /*non_constant_p=*/NULL,
7105 /*close_paren_loc=*/&close_paren_loc,
7106 /*wrap_locations_p=*/true));
7107 if (is_builtin_constant_p)
7108 {
7109 parser->integral_constant_expression_p
7110 = saved_integral_constant_expression_p;
7111 parser->non_integral_constant_expression_p
7112 = saved_non_integral_constant_expression_p;
7113 }
7114
7115 if (args == NULL)
7116 {
7117 postfix_expression = error_mark_node;
7118 break;
7119 }
7120
7121 /* Function calls are not permitted in
7122 constant-expressions. */
7123 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7124 && cp_parser_non_integral_constant_expression (parser,
7125 NIC_FUNC_CALL))
7126 {
7127 postfix_expression = error_mark_node;
7128 release_tree_vector (args);
7129 break;
7130 }
7131
7132 koenig_p = false;
7133 if (idk == CP_ID_KIND_UNQUALIFIED
7134 || idk == CP_ID_KIND_TEMPLATE_ID)
7135 {
7136 if (identifier_p (postfix_expression))
7137 {
7138 if (!args->is_empty ())
7139 {
7140 koenig_p = true;
7141 if (!any_type_dependent_arguments_p (args))
7142 postfix_expression
7143 = perform_koenig_lookup (postfix_expression, args,
7144 complain);
7145 }
7146 else
7147 postfix_expression
7148 = unqualified_fn_lookup_error (postfix_expression);
7149 }
7150 /* We do not perform argument-dependent lookup if
7151 normal lookup finds a non-function, in accordance
7152 with the expected resolution of DR 218. */
7153 else if (!args->is_empty ()
7154 && is_overloaded_fn (postfix_expression))
7155 {
7156 tree fn = get_first_fn (postfix_expression);
7157 fn = STRIP_TEMPLATE (fn);
7158
7159 /* Do not do argument dependent lookup if regular
7160 lookup finds a member function or a block-scope
7161 function declaration. [basic.lookup.argdep]/3 */
7162 if (!DECL_FUNCTION_MEMBER_P (fn)
7163 && !DECL_LOCAL_FUNCTION_P (fn))
7164 {
7165 koenig_p = true;
7166 if (!any_type_dependent_arguments_p (args))
7167 postfix_expression
7168 = perform_koenig_lookup (postfix_expression, args,
7169 complain);
7170 }
7171 }
7172 }
7173
7174 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7175 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7176 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7177 && vec_safe_length (args) == 3)
7178 {
7179 tree arg0 = (*args)[0];
7180 tree arg1 = (*args)[1];
7181 tree arg2 = (*args)[2];
7182 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7183 | (literal_integer_zerop (arg2) << 2));
7184 warn_for_memset (input_location, arg0, arg2, literal_mask);
7185 }
7186
7187 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7188 {
7189 tree instance = TREE_OPERAND (postfix_expression, 0);
7190 tree fn = TREE_OPERAND (postfix_expression, 1);
7191
7192 if (processing_template_decl
7193 && (type_dependent_object_expression_p (instance)
7194 || (!BASELINK_P (fn)
7195 && TREE_CODE (fn) != FIELD_DECL)
7196 || type_dependent_expression_p (fn)
7197 || any_type_dependent_arguments_p (args)))
7198 {
7199 maybe_generic_this_capture (instance, fn);
7200 postfix_expression
7201 = build_min_nt_call_vec (postfix_expression, args);
7202 release_tree_vector (args);
7203 break;
7204 }
7205
7206 if (BASELINK_P (fn))
7207 {
7208 postfix_expression
7209 = (build_new_method_call
7210 (instance, fn, &args, NULL_TREE,
7211 (idk == CP_ID_KIND_QUALIFIED
7212 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7213 : LOOKUP_NORMAL),
7214 /*fn_p=*/NULL,
7215 complain));
7216 }
7217 else
7218 postfix_expression
7219 = finish_call_expr (postfix_expression, &args,
7220 /*disallow_virtual=*/false,
7221 /*koenig_p=*/false,
7222 complain);
7223 }
7224 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7225 || TREE_CODE (postfix_expression) == MEMBER_REF
7226 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7227 postfix_expression = (build_offset_ref_call_from_tree
7228 (postfix_expression, &args,
7229 complain));
7230 else if (idk == CP_ID_KIND_QUALIFIED)
7231 /* A call to a static class member, or a namespace-scope
7232 function. */
7233 postfix_expression
7234 = finish_call_expr (postfix_expression, &args,
7235 /*disallow_virtual=*/true,
7236 koenig_p,
7237 complain);
7238 else
7239 /* All other function calls. */
7240 postfix_expression
7241 = finish_call_expr (postfix_expression, &args,
7242 /*disallow_virtual=*/false,
7243 koenig_p,
7244 complain);
7245
7246 if (close_paren_loc != UNKNOWN_LOCATION)
7247 {
7248 location_t combined_loc = make_location (token->location,
7249 start_loc,
7250 close_paren_loc);
7251 postfix_expression.set_location (combined_loc);
7252 }
7253
7254 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7255 idk = CP_ID_KIND_NONE;
7256
7257 release_tree_vector (args);
7258 }
7259 break;
7260
7261 case CPP_DOT:
7262 case CPP_DEREF:
7263 /* postfix-expression . template [opt] id-expression
7264 postfix-expression . pseudo-destructor-name
7265 postfix-expression -> template [opt] id-expression
7266 postfix-expression -> pseudo-destructor-name */
7267
7268 /* Consume the `.' or `->' operator. */
7269 cp_lexer_consume_token (parser->lexer);
7270
7271 postfix_expression
7272 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7273 postfix_expression,
7274 false, &idk, loc);
7275
7276 is_member_access = true;
7277 break;
7278
7279 case CPP_PLUS_PLUS:
7280 /* postfix-expression ++ */
7281 /* Consume the `++' token. */
7282 cp_lexer_consume_token (parser->lexer);
7283 /* Generate a representation for the complete expression. */
7284 postfix_expression
7285 = finish_increment_expr (postfix_expression,
7286 POSTINCREMENT_EXPR);
7287 /* Increments may not appear in constant-expressions. */
7288 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7289 postfix_expression = error_mark_node;
7290 idk = CP_ID_KIND_NONE;
7291 is_member_access = false;
7292 break;
7293
7294 case CPP_MINUS_MINUS:
7295 /* postfix-expression -- */
7296 /* Consume the `--' token. */
7297 cp_lexer_consume_token (parser->lexer);
7298 /* Generate a representation for the complete expression. */
7299 postfix_expression
7300 = finish_increment_expr (postfix_expression,
7301 POSTDECREMENT_EXPR);
7302 /* Decrements may not appear in constant-expressions. */
7303 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7304 postfix_expression = error_mark_node;
7305 idk = CP_ID_KIND_NONE;
7306 is_member_access = false;
7307 break;
7308
7309 default:
7310 if (pidk_return != NULL)
7311 * pidk_return = idk;
7312 if (member_access_only_p)
7313 return is_member_access
7314 ? postfix_expression
7315 : cp_expr (error_mark_node);
7316 else
7317 return postfix_expression;
7318 }
7319 }
7320
7321 /* We should never get here. */
7322 gcc_unreachable ();
7323 return error_mark_node;
7324 }
7325
7326 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7327 by cp_parser_builtin_offsetof. We're looking for
7328
7329 postfix-expression [ expression ]
7330 postfix-expression [ braced-init-list ] (C++11)
7331
7332 FOR_OFFSETOF is set if we're being called in that context, which
7333 changes how we deal with integer constant expressions. */
7334
7335 static tree
7336 cp_parser_postfix_open_square_expression (cp_parser *parser,
7337 tree postfix_expression,
7338 bool for_offsetof,
7339 bool decltype_p)
7340 {
7341 tree index = NULL_TREE;
7342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7343 bool saved_greater_than_is_operator_p;
7344
7345 /* Consume the `[' token. */
7346 cp_lexer_consume_token (parser->lexer);
7347
7348 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7349 parser->greater_than_is_operator_p = true;
7350
7351 /* Parse the index expression. */
7352 /* ??? For offsetof, there is a question of what to allow here. If
7353 offsetof is not being used in an integral constant expression context,
7354 then we *could* get the right answer by computing the value at runtime.
7355 If we are in an integral constant expression context, then we might
7356 could accept any constant expression; hard to say without analysis.
7357 Rather than open the barn door too wide right away, allow only integer
7358 constant expressions here. */
7359 if (for_offsetof)
7360 index = cp_parser_constant_expression (parser);
7361 else
7362 {
7363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7364 {
7365 bool expr_nonconst_p;
7366 cp_lexer_set_source_position (parser->lexer);
7367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7368 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7369 }
7370 else
7371 index = cp_parser_expression (parser);
7372 }
7373
7374 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7375
7376 /* Look for the closing `]'. */
7377 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7378
7379 /* Build the ARRAY_REF. */
7380 postfix_expression = grok_array_decl (loc, postfix_expression,
7381 index, decltype_p);
7382
7383 /* When not doing offsetof, array references are not permitted in
7384 constant-expressions. */
7385 if (!for_offsetof
7386 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7387 postfix_expression = error_mark_node;
7388
7389 return postfix_expression;
7390 }
7391
7392 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7393 dereference of incomplete type, returns true if error_mark_node should
7394 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7395 and *DEPENDENT_P. */
7396
7397 bool
7398 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7399 bool *dependent_p)
7400 {
7401 /* In a template, be permissive by treating an object expression
7402 of incomplete type as dependent (after a pedwarn). */
7403 diagnostic_t kind = (processing_template_decl
7404 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7405
7406 switch (TREE_CODE (*postfix_expression))
7407 {
7408 case CAST_EXPR:
7409 case REINTERPRET_CAST_EXPR:
7410 case CONST_CAST_EXPR:
7411 case STATIC_CAST_EXPR:
7412 case DYNAMIC_CAST_EXPR:
7413 case IMPLICIT_CONV_EXPR:
7414 case VIEW_CONVERT_EXPR:
7415 case NON_LVALUE_EXPR:
7416 kind = DK_ERROR;
7417 break;
7418 case OVERLOAD:
7419 /* Don't emit any diagnostic for OVERLOADs. */
7420 kind = DK_IGNORED;
7421 break;
7422 default:
7423 /* Avoid clobbering e.g. DECLs. */
7424 if (!EXPR_P (*postfix_expression))
7425 kind = DK_ERROR;
7426 break;
7427 }
7428
7429 if (kind == DK_IGNORED)
7430 return false;
7431
7432 location_t exploc = location_of (*postfix_expression);
7433 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7434 if (!MAYBE_CLASS_TYPE_P (*scope))
7435 return true;
7436 if (kind == DK_ERROR)
7437 *scope = *postfix_expression = error_mark_node;
7438 else if (processing_template_decl)
7439 {
7440 *dependent_p = true;
7441 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7442 }
7443 return false;
7444 }
7445
7446 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7447 by cp_parser_builtin_offsetof. We're looking for
7448
7449 postfix-expression . template [opt] id-expression
7450 postfix-expression . pseudo-destructor-name
7451 postfix-expression -> template [opt] id-expression
7452 postfix-expression -> pseudo-destructor-name
7453
7454 FOR_OFFSETOF is set if we're being called in that context. That sorta
7455 limits what of the above we'll actually accept, but nevermind.
7456 TOKEN_TYPE is the "." or "->" token, which will already have been
7457 removed from the stream. */
7458
7459 static tree
7460 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7461 enum cpp_ttype token_type,
7462 cp_expr postfix_expression,
7463 bool for_offsetof, cp_id_kind *idk,
7464 location_t location)
7465 {
7466 tree name;
7467 bool dependent_p;
7468 bool pseudo_destructor_p;
7469 tree scope = NULL_TREE;
7470 location_t start_loc = postfix_expression.get_start ();
7471
7472 /* If this is a `->' operator, dereference the pointer. */
7473 if (token_type == CPP_DEREF)
7474 postfix_expression = build_x_arrow (location, postfix_expression,
7475 tf_warning_or_error);
7476 /* Check to see whether or not the expression is type-dependent and
7477 not the current instantiation. */
7478 dependent_p = type_dependent_object_expression_p (postfix_expression);
7479 /* The identifier following the `->' or `.' is not qualified. */
7480 parser->scope = NULL_TREE;
7481 parser->qualifying_scope = NULL_TREE;
7482 parser->object_scope = NULL_TREE;
7483 *idk = CP_ID_KIND_NONE;
7484
7485 /* Enter the scope corresponding to the type of the object
7486 given by the POSTFIX_EXPRESSION. */
7487 if (!dependent_p)
7488 {
7489 scope = TREE_TYPE (postfix_expression);
7490 /* According to the standard, no expression should ever have
7491 reference type. Unfortunately, we do not currently match
7492 the standard in this respect in that our internal representation
7493 of an expression may have reference type even when the standard
7494 says it does not. Therefore, we have to manually obtain the
7495 underlying type here. */
7496 scope = non_reference (scope);
7497 /* The type of the POSTFIX_EXPRESSION must be complete. */
7498 /* Unlike the object expression in other contexts, *this is not
7499 required to be of complete type for purposes of class member
7500 access (5.2.5) outside the member function body. */
7501 if (postfix_expression != current_class_ref
7502 && scope != error_mark_node
7503 && !(processing_template_decl
7504 && current_class_type
7505 && (same_type_ignoring_top_level_qualifiers_p
7506 (scope, current_class_type))))
7507 {
7508 scope = complete_type (scope);
7509 if (!COMPLETE_TYPE_P (scope)
7510 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7511 &dependent_p))
7512 return error_mark_node;
7513 }
7514
7515 if (!dependent_p)
7516 {
7517 /* Let the name lookup machinery know that we are processing a
7518 class member access expression. */
7519 parser->context->object_type = scope;
7520 /* If something went wrong, we want to be able to discern that case,
7521 as opposed to the case where there was no SCOPE due to the type
7522 of expression being dependent. */
7523 if (!scope)
7524 scope = error_mark_node;
7525 /* If the SCOPE was erroneous, make the various semantic analysis
7526 functions exit quickly -- and without issuing additional error
7527 messages. */
7528 if (scope == error_mark_node)
7529 postfix_expression = error_mark_node;
7530 }
7531 }
7532
7533 if (dependent_p)
7534 /* Tell cp_parser_lookup_name that there was an object, even though it's
7535 type-dependent. */
7536 parser->context->object_type = unknown_type_node;
7537
7538 /* Assume this expression is not a pseudo-destructor access. */
7539 pseudo_destructor_p = false;
7540
7541 /* If the SCOPE is a scalar type, then, if this is a valid program,
7542 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7543 is type dependent, it can be pseudo-destructor-name or something else.
7544 Try to parse it as pseudo-destructor-name first. */
7545 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7546 {
7547 tree s;
7548 tree type;
7549
7550 cp_parser_parse_tentatively (parser);
7551 /* Parse the pseudo-destructor-name. */
7552 s = NULL_TREE;
7553 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7554 &s, &type);
7555 if (dependent_p
7556 && (cp_parser_error_occurred (parser)
7557 || !SCALAR_TYPE_P (type)))
7558 cp_parser_abort_tentative_parse (parser);
7559 else if (cp_parser_parse_definitely (parser))
7560 {
7561 pseudo_destructor_p = true;
7562 postfix_expression
7563 = finish_pseudo_destructor_expr (postfix_expression,
7564 s, type, location);
7565 }
7566 }
7567
7568 if (!pseudo_destructor_p)
7569 {
7570 /* If the SCOPE is not a scalar type, we are looking at an
7571 ordinary class member access expression, rather than a
7572 pseudo-destructor-name. */
7573 bool template_p;
7574 cp_token *token = cp_lexer_peek_token (parser->lexer);
7575 /* Parse the id-expression. */
7576 name = (cp_parser_id_expression
7577 (parser,
7578 cp_parser_optional_template_keyword (parser),
7579 /*check_dependency_p=*/true,
7580 &template_p,
7581 /*declarator_p=*/false,
7582 /*optional_p=*/false));
7583 /* In general, build a SCOPE_REF if the member name is qualified.
7584 However, if the name was not dependent and has already been
7585 resolved; there is no need to build the SCOPE_REF. For example;
7586
7587 struct X { void f(); };
7588 template <typename T> void f(T* t) { t->X::f(); }
7589
7590 Even though "t" is dependent, "X::f" is not and has been resolved
7591 to a BASELINK; there is no need to include scope information. */
7592
7593 /* But we do need to remember that there was an explicit scope for
7594 virtual function calls. */
7595 if (parser->scope)
7596 *idk = CP_ID_KIND_QUALIFIED;
7597
7598 /* If the name is a template-id that names a type, we will get a
7599 TYPE_DECL here. That is invalid code. */
7600 if (TREE_CODE (name) == TYPE_DECL)
7601 {
7602 error_at (token->location, "invalid use of %qD", name);
7603 postfix_expression = error_mark_node;
7604 }
7605 else
7606 {
7607 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7608 {
7609 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7610 {
7611 error_at (token->location, "%<%D::%D%> is not a class member",
7612 parser->scope, name);
7613 postfix_expression = error_mark_node;
7614 }
7615 else
7616 name = build_qualified_name (/*type=*/NULL_TREE,
7617 parser->scope,
7618 name,
7619 template_p);
7620 parser->scope = NULL_TREE;
7621 parser->qualifying_scope = NULL_TREE;
7622 parser->object_scope = NULL_TREE;
7623 }
7624 if (parser->scope && name && BASELINK_P (name))
7625 adjust_result_of_qualified_name_lookup
7626 (name, parser->scope, scope);
7627 postfix_expression
7628 = finish_class_member_access_expr (postfix_expression, name,
7629 template_p,
7630 tf_warning_or_error);
7631 /* Build a location e.g.:
7632 ptr->access_expr
7633 ~~~^~~~~~~~~~~~~
7634 where the caret is at the deref token, ranging from
7635 the start of postfix_expression to the end of the access expr. */
7636 location_t end_loc
7637 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7638 location_t combined_loc
7639 = make_location (input_location, start_loc, end_loc);
7640 protected_set_expr_location (postfix_expression, combined_loc);
7641 }
7642 }
7643
7644 /* We no longer need to look up names in the scope of the object on
7645 the left-hand side of the `.' or `->' operator. */
7646 parser->context->object_type = NULL_TREE;
7647
7648 /* Outside of offsetof, these operators may not appear in
7649 constant-expressions. */
7650 if (!for_offsetof
7651 && (cp_parser_non_integral_constant_expression
7652 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7653 postfix_expression = error_mark_node;
7654
7655 return postfix_expression;
7656 }
7657
7658 /* Parse a parenthesized expression-list.
7659
7660 expression-list:
7661 assignment-expression
7662 expression-list, assignment-expression
7663
7664 attribute-list:
7665 expression-list
7666 identifier
7667 identifier, expression-list
7668
7669 CAST_P is true if this expression is the target of a cast.
7670
7671 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7672 argument pack.
7673
7674 WRAP_LOCATIONS_P is true if expressions within this list for which
7675 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7676 their source locations.
7677
7678 Returns a vector of trees. Each element is a representation of an
7679 assignment-expression. NULL is returned if the ( and or ) are
7680 missing. An empty, but allocated, vector is returned on no
7681 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7682 if we are parsing an attribute list for an attribute that wants a
7683 plain identifier argument, normal_attr for an attribute that wants
7684 an expression, or non_attr if we aren't parsing an attribute list. If
7685 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7686 not all of the expressions in the list were constant.
7687 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7688 will be written to with the location of the closing parenthesis. If
7689 an error occurs, it may or may not be written to. */
7690
7691 static vec<tree, va_gc> *
7692 cp_parser_parenthesized_expression_list (cp_parser* parser,
7693 int is_attribute_list,
7694 bool cast_p,
7695 bool allow_expansion_p,
7696 bool *non_constant_p,
7697 location_t *close_paren_loc,
7698 bool wrap_locations_p)
7699 {
7700 vec<tree, va_gc> *expression_list;
7701 bool fold_expr_p = is_attribute_list != non_attr;
7702 tree identifier = NULL_TREE;
7703 bool saved_greater_than_is_operator_p;
7704
7705 /* Assume all the expressions will be constant. */
7706 if (non_constant_p)
7707 *non_constant_p = false;
7708
7709 matching_parens parens;
7710 if (!parens.require_open (parser))
7711 return NULL;
7712
7713 expression_list = make_tree_vector ();
7714
7715 /* Within a parenthesized expression, a `>' token is always
7716 the greater-than operator. */
7717 saved_greater_than_is_operator_p
7718 = parser->greater_than_is_operator_p;
7719 parser->greater_than_is_operator_p = true;
7720
7721 cp_expr expr (NULL_TREE);
7722
7723 /* Consume expressions until there are no more. */
7724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7725 while (true)
7726 {
7727 /* At the beginning of attribute lists, check to see if the
7728 next token is an identifier. */
7729 if (is_attribute_list == id_attr
7730 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7731 {
7732 cp_token *token;
7733
7734 /* Consume the identifier. */
7735 token = cp_lexer_consume_token (parser->lexer);
7736 /* Save the identifier. */
7737 identifier = token->u.value;
7738 }
7739 else
7740 {
7741 bool expr_non_constant_p;
7742
7743 /* Parse the next assignment-expression. */
7744 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7745 {
7746 /* A braced-init-list. */
7747 cp_lexer_set_source_position (parser->lexer);
7748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7749 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7750 if (non_constant_p && expr_non_constant_p)
7751 *non_constant_p = true;
7752 }
7753 else if (non_constant_p)
7754 {
7755 expr = (cp_parser_constant_expression
7756 (parser, /*allow_non_constant_p=*/true,
7757 &expr_non_constant_p));
7758 if (expr_non_constant_p)
7759 *non_constant_p = true;
7760 }
7761 else
7762 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7763 cast_p);
7764
7765 if (fold_expr_p)
7766 expr = instantiate_non_dependent_expr (expr);
7767
7768 /* If we have an ellipsis, then this is an expression
7769 expansion. */
7770 if (allow_expansion_p
7771 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7772 {
7773 /* Consume the `...'. */
7774 cp_lexer_consume_token (parser->lexer);
7775
7776 /* Build the argument pack. */
7777 expr = make_pack_expansion (expr);
7778 }
7779
7780 if (wrap_locations_p)
7781 expr.maybe_add_location_wrapper ();
7782
7783 /* Add it to the list. We add error_mark_node
7784 expressions to the list, so that we can still tell if
7785 the correct form for a parenthesized expression-list
7786 is found. That gives better errors. */
7787 vec_safe_push (expression_list, expr.get_value ());
7788
7789 if (expr == error_mark_node)
7790 goto skip_comma;
7791 }
7792
7793 /* After the first item, attribute lists look the same as
7794 expression lists. */
7795 is_attribute_list = non_attr;
7796
7797 get_comma:;
7798 /* If the next token isn't a `,', then we are done. */
7799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7800 break;
7801
7802 /* Otherwise, consume the `,' and keep going. */
7803 cp_lexer_consume_token (parser->lexer);
7804 }
7805
7806 if (close_paren_loc)
7807 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7808
7809 if (!parens.require_close (parser))
7810 {
7811 int ending;
7812
7813 skip_comma:;
7814 /* We try and resync to an unnested comma, as that will give the
7815 user better diagnostics. */
7816 ending = cp_parser_skip_to_closing_parenthesis (parser,
7817 /*recovering=*/true,
7818 /*or_comma=*/true,
7819 /*consume_paren=*/true);
7820 if (ending < 0)
7821 goto get_comma;
7822 if (!ending)
7823 {
7824 parser->greater_than_is_operator_p
7825 = saved_greater_than_is_operator_p;
7826 return NULL;
7827 }
7828 }
7829
7830 parser->greater_than_is_operator_p
7831 = saved_greater_than_is_operator_p;
7832
7833 if (identifier)
7834 vec_safe_insert (expression_list, 0, identifier);
7835
7836 return expression_list;
7837 }
7838
7839 /* Parse a pseudo-destructor-name.
7840
7841 pseudo-destructor-name:
7842 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7843 :: [opt] nested-name-specifier template template-id :: ~ type-name
7844 :: [opt] nested-name-specifier [opt] ~ type-name
7845
7846 If either of the first two productions is used, sets *SCOPE to the
7847 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7848 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7849 or ERROR_MARK_NODE if the parse fails. */
7850
7851 static void
7852 cp_parser_pseudo_destructor_name (cp_parser* parser,
7853 tree object,
7854 tree* scope,
7855 tree* type)
7856 {
7857 bool nested_name_specifier_p;
7858
7859 /* Handle ~auto. */
7860 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7861 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7862 && !type_dependent_expression_p (object))
7863 {
7864 if (cxx_dialect < cxx14)
7865 pedwarn (input_location, 0,
7866 "%<~auto%> only available with "
7867 "-std=c++14 or -std=gnu++14");
7868 cp_lexer_consume_token (parser->lexer);
7869 cp_lexer_consume_token (parser->lexer);
7870 *scope = NULL_TREE;
7871 *type = TREE_TYPE (object);
7872 return;
7873 }
7874
7875 /* Assume that things will not work out. */
7876 *type = error_mark_node;
7877
7878 /* Look for the optional `::' operator. */
7879 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7880 /* Look for the optional nested-name-specifier. */
7881 nested_name_specifier_p
7882 = (cp_parser_nested_name_specifier_opt (parser,
7883 /*typename_keyword_p=*/false,
7884 /*check_dependency_p=*/true,
7885 /*type_p=*/false,
7886 /*is_declaration=*/false)
7887 != NULL_TREE);
7888 /* Now, if we saw a nested-name-specifier, we might be doing the
7889 second production. */
7890 if (nested_name_specifier_p
7891 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7892 {
7893 /* Consume the `template' keyword. */
7894 cp_lexer_consume_token (parser->lexer);
7895 /* Parse the template-id. */
7896 cp_parser_template_id (parser,
7897 /*template_keyword_p=*/true,
7898 /*check_dependency_p=*/false,
7899 class_type,
7900 /*is_declaration=*/true);
7901 /* Look for the `::' token. */
7902 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7903 }
7904 /* If the next token is not a `~', then there might be some
7905 additional qualification. */
7906 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7907 {
7908 /* At this point, we're looking for "type-name :: ~". The type-name
7909 must not be a class-name, since this is a pseudo-destructor. So,
7910 it must be either an enum-name, or a typedef-name -- both of which
7911 are just identifiers. So, we peek ahead to check that the "::"
7912 and "~" tokens are present; if they are not, then we can avoid
7913 calling type_name. */
7914 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7915 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7916 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7917 {
7918 cp_parser_error (parser, "non-scalar type");
7919 return;
7920 }
7921
7922 /* Look for the type-name. */
7923 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7924 if (*scope == error_mark_node)
7925 return;
7926
7927 /* Look for the `::' token. */
7928 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7929 }
7930 else
7931 *scope = NULL_TREE;
7932
7933 /* Look for the `~'. */
7934 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7935
7936 /* Once we see the ~, this has to be a pseudo-destructor. */
7937 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7938 cp_parser_commit_to_topmost_tentative_parse (parser);
7939
7940 /* Look for the type-name again. We are not responsible for
7941 checking that it matches the first type-name. */
7942 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7943 }
7944
7945 /* Parse a unary-expression.
7946
7947 unary-expression:
7948 postfix-expression
7949 ++ cast-expression
7950 -- cast-expression
7951 unary-operator cast-expression
7952 sizeof unary-expression
7953 sizeof ( type-id )
7954 alignof ( type-id ) [C++0x]
7955 new-expression
7956 delete-expression
7957
7958 GNU Extensions:
7959
7960 unary-expression:
7961 __extension__ cast-expression
7962 __alignof__ unary-expression
7963 __alignof__ ( type-id )
7964 alignof unary-expression [C++0x]
7965 __real__ cast-expression
7966 __imag__ cast-expression
7967 && identifier
7968 sizeof ( type-id ) { initializer-list , [opt] }
7969 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7970 __alignof__ ( type-id ) { initializer-list , [opt] }
7971
7972 ADDRESS_P is true iff the unary-expression is appearing as the
7973 operand of the `&' operator. CAST_P is true if this expression is
7974 the target of a cast.
7975
7976 Returns a representation of the expression. */
7977
7978 static cp_expr
7979 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7980 bool address_p, bool cast_p, bool decltype_p)
7981 {
7982 cp_token *token;
7983 enum tree_code unary_operator;
7984
7985 /* Peek at the next token. */
7986 token = cp_lexer_peek_token (parser->lexer);
7987 /* Some keywords give away the kind of expression. */
7988 if (token->type == CPP_KEYWORD)
7989 {
7990 enum rid keyword = token->keyword;
7991
7992 switch (keyword)
7993 {
7994 case RID_ALIGNOF:
7995 case RID_SIZEOF:
7996 {
7997 tree operand, ret;
7998 enum tree_code op;
7999 location_t start_loc = token->location;
8000
8001 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8002 /* Consume the token. */
8003 cp_lexer_consume_token (parser->lexer);
8004 /* Parse the operand. */
8005 operand = cp_parser_sizeof_operand (parser, keyword);
8006
8007 if (TYPE_P (operand))
8008 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8009 else
8010 {
8011 /* ISO C++ defines alignof only with types, not with
8012 expressions. So pedwarn if alignof is used with a non-
8013 type expression. However, __alignof__ is ok. */
8014 if (id_equal (token->u.value, "alignof"))
8015 pedwarn (token->location, OPT_Wpedantic,
8016 "ISO C++ does not allow %<alignof%> "
8017 "with a non-type");
8018
8019 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8020 }
8021 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8022 SIZEOF_EXPR with the original operand. */
8023 if (op == SIZEOF_EXPR && ret != error_mark_node)
8024 {
8025 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8026 {
8027 if (!processing_template_decl && TYPE_P (operand))
8028 {
8029 ret = build_min (SIZEOF_EXPR, size_type_node,
8030 build1 (NOP_EXPR, operand,
8031 error_mark_node));
8032 SIZEOF_EXPR_TYPE_P (ret) = 1;
8033 }
8034 else
8035 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8036 TREE_SIDE_EFFECTS (ret) = 0;
8037 TREE_READONLY (ret) = 1;
8038 }
8039 }
8040
8041 /* Construct a location e.g. :
8042 alignof (expr)
8043 ^~~~~~~~~~~~~~
8044 with start == caret at the start of the "alignof"/"sizeof"
8045 token, with the endpoint at the final closing paren. */
8046 location_t finish_loc
8047 = cp_lexer_previous_token (parser->lexer)->location;
8048 location_t compound_loc
8049 = make_location (start_loc, start_loc, finish_loc);
8050
8051 cp_expr ret_expr (ret);
8052 ret_expr.set_location (compound_loc);
8053 ret_expr = ret_expr.maybe_add_location_wrapper ();
8054 return ret_expr;
8055 }
8056
8057 case RID_NEW:
8058 return cp_parser_new_expression (parser);
8059
8060 case RID_DELETE:
8061 return cp_parser_delete_expression (parser);
8062
8063 case RID_EXTENSION:
8064 {
8065 /* The saved value of the PEDANTIC flag. */
8066 int saved_pedantic;
8067 tree expr;
8068
8069 /* Save away the PEDANTIC flag. */
8070 cp_parser_extension_opt (parser, &saved_pedantic);
8071 /* Parse the cast-expression. */
8072 expr = cp_parser_simple_cast_expression (parser);
8073 /* Restore the PEDANTIC flag. */
8074 pedantic = saved_pedantic;
8075
8076 return expr;
8077 }
8078
8079 case RID_REALPART:
8080 case RID_IMAGPART:
8081 {
8082 tree expression;
8083
8084 /* Consume the `__real__' or `__imag__' token. */
8085 cp_lexer_consume_token (parser->lexer);
8086 /* Parse the cast-expression. */
8087 expression = cp_parser_simple_cast_expression (parser);
8088 /* Create the complete representation. */
8089 return build_x_unary_op (token->location,
8090 (keyword == RID_REALPART
8091 ? REALPART_EXPR : IMAGPART_EXPR),
8092 expression,
8093 tf_warning_or_error);
8094 }
8095 break;
8096
8097 case RID_TRANSACTION_ATOMIC:
8098 case RID_TRANSACTION_RELAXED:
8099 return cp_parser_transaction_expression (parser, keyword);
8100
8101 case RID_NOEXCEPT:
8102 {
8103 tree expr;
8104 const char *saved_message;
8105 bool saved_integral_constant_expression_p;
8106 bool saved_non_integral_constant_expression_p;
8107 bool saved_greater_than_is_operator_p;
8108
8109 location_t start_loc = token->location;
8110
8111 cp_lexer_consume_token (parser->lexer);
8112 matching_parens parens;
8113 parens.require_open (parser);
8114
8115 saved_message = parser->type_definition_forbidden_message;
8116 parser->type_definition_forbidden_message
8117 = G_("types may not be defined in %<noexcept%> expressions");
8118
8119 saved_integral_constant_expression_p
8120 = parser->integral_constant_expression_p;
8121 saved_non_integral_constant_expression_p
8122 = parser->non_integral_constant_expression_p;
8123 parser->integral_constant_expression_p = false;
8124
8125 saved_greater_than_is_operator_p
8126 = parser->greater_than_is_operator_p;
8127 parser->greater_than_is_operator_p = true;
8128
8129 ++cp_unevaluated_operand;
8130 ++c_inhibit_evaluation_warnings;
8131 ++cp_noexcept_operand;
8132 expr = cp_parser_expression (parser);
8133 --cp_noexcept_operand;
8134 --c_inhibit_evaluation_warnings;
8135 --cp_unevaluated_operand;
8136
8137 parser->greater_than_is_operator_p
8138 = saved_greater_than_is_operator_p;
8139
8140 parser->integral_constant_expression_p
8141 = saved_integral_constant_expression_p;
8142 parser->non_integral_constant_expression_p
8143 = saved_non_integral_constant_expression_p;
8144
8145 parser->type_definition_forbidden_message = saved_message;
8146
8147 location_t finish_loc
8148 = cp_lexer_peek_token (parser->lexer)->location;
8149 parens.require_close (parser);
8150
8151 /* Construct a location of the form:
8152 noexcept (expr)
8153 ^~~~~~~~~~~~~~~
8154 with start == caret, finishing at the close-paren. */
8155 location_t noexcept_loc
8156 = make_location (start_loc, start_loc, finish_loc);
8157
8158 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8159 noexcept_loc);
8160 }
8161
8162 default:
8163 break;
8164 }
8165 }
8166
8167 /* Look for the `:: new' and `:: delete', which also signal the
8168 beginning of a new-expression, or delete-expression,
8169 respectively. If the next token is `::', then it might be one of
8170 these. */
8171 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8172 {
8173 enum rid keyword;
8174
8175 /* See if the token after the `::' is one of the keywords in
8176 which we're interested. */
8177 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8178 /* If it's `new', we have a new-expression. */
8179 if (keyword == RID_NEW)
8180 return cp_parser_new_expression (parser);
8181 /* Similarly, for `delete'. */
8182 else if (keyword == RID_DELETE)
8183 return cp_parser_delete_expression (parser);
8184 }
8185
8186 /* Look for a unary operator. */
8187 unary_operator = cp_parser_unary_operator (token);
8188 /* The `++' and `--' operators can be handled similarly, even though
8189 they are not technically unary-operators in the grammar. */
8190 if (unary_operator == ERROR_MARK)
8191 {
8192 if (token->type == CPP_PLUS_PLUS)
8193 unary_operator = PREINCREMENT_EXPR;
8194 else if (token->type == CPP_MINUS_MINUS)
8195 unary_operator = PREDECREMENT_EXPR;
8196 /* Handle the GNU address-of-label extension. */
8197 else if (cp_parser_allow_gnu_extensions_p (parser)
8198 && token->type == CPP_AND_AND)
8199 {
8200 tree identifier;
8201 tree expression;
8202 location_t start_loc = token->location;
8203
8204 /* Consume the '&&' token. */
8205 cp_lexer_consume_token (parser->lexer);
8206 /* Look for the identifier. */
8207 location_t finish_loc
8208 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8209 identifier = cp_parser_identifier (parser);
8210 /* Construct a location of the form:
8211 &&label
8212 ^~~~~~~
8213 with caret==start at the "&&", finish at the end of the label. */
8214 location_t combined_loc
8215 = make_location (start_loc, start_loc, finish_loc);
8216 /* Create an expression representing the address. */
8217 expression = finish_label_address_expr (identifier, combined_loc);
8218 if (cp_parser_non_integral_constant_expression (parser,
8219 NIC_ADDR_LABEL))
8220 expression = error_mark_node;
8221 return expression;
8222 }
8223 }
8224 if (unary_operator != ERROR_MARK)
8225 {
8226 cp_expr cast_expression;
8227 cp_expr expression = error_mark_node;
8228 non_integral_constant non_constant_p = NIC_NONE;
8229 location_t loc = token->location;
8230 tsubst_flags_t complain = complain_flags (decltype_p);
8231
8232 /* Consume the operator token. */
8233 token = cp_lexer_consume_token (parser->lexer);
8234 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8235
8236 /* Parse the cast-expression. */
8237 cast_expression
8238 = cp_parser_cast_expression (parser,
8239 unary_operator == ADDR_EXPR,
8240 /*cast_p=*/false,
8241 /*decltype*/false,
8242 pidk);
8243
8244 /* Make a location:
8245 OP_TOKEN CAST_EXPRESSION
8246 ^~~~~~~~~~~~~~~~~~~~~~~~~
8247 with start==caret at the operator token, and
8248 extending to the end of the cast_expression. */
8249 loc = make_location (loc, loc, cast_expression.get_finish ());
8250
8251 /* Now, build an appropriate representation. */
8252 switch (unary_operator)
8253 {
8254 case INDIRECT_REF:
8255 non_constant_p = NIC_STAR;
8256 expression = build_x_indirect_ref (loc, cast_expression,
8257 RO_UNARY_STAR,
8258 complain);
8259 /* TODO: build_x_indirect_ref does not always honor the
8260 location, so ensure it is set. */
8261 expression.set_location (loc);
8262 break;
8263
8264 case ADDR_EXPR:
8265 non_constant_p = NIC_ADDR;
8266 /* Fall through. */
8267 case BIT_NOT_EXPR:
8268 expression = build_x_unary_op (loc, unary_operator,
8269 cast_expression,
8270 complain);
8271 /* TODO: build_x_unary_op does not always honor the location,
8272 so ensure it is set. */
8273 expression.set_location (loc);
8274 break;
8275
8276 case PREINCREMENT_EXPR:
8277 case PREDECREMENT_EXPR:
8278 non_constant_p = unary_operator == PREINCREMENT_EXPR
8279 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8280 /* Fall through. */
8281 case NEGATE_EXPR:
8282 /* Immediately fold negation of a constant, unless the constant is 0
8283 (since -0 == 0) or it would overflow. */
8284 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8285 && CONSTANT_CLASS_P (cast_expression)
8286 && !integer_zerop (cast_expression)
8287 && !TREE_OVERFLOW (cast_expression))
8288 {
8289 tree folded = fold_build1 (unary_operator,
8290 TREE_TYPE (cast_expression),
8291 cast_expression);
8292 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8293 {
8294 expression = cp_expr (folded, loc);
8295 break;
8296 }
8297 }
8298 /* Fall through. */
8299 case UNARY_PLUS_EXPR:
8300 case TRUTH_NOT_EXPR:
8301 expression = finish_unary_op_expr (loc, unary_operator,
8302 cast_expression, complain);
8303 break;
8304
8305 default:
8306 gcc_unreachable ();
8307 }
8308
8309 if (non_constant_p != NIC_NONE
8310 && cp_parser_non_integral_constant_expression (parser,
8311 non_constant_p))
8312 expression = error_mark_node;
8313
8314 return expression;
8315 }
8316
8317 return cp_parser_postfix_expression (parser, address_p, cast_p,
8318 /*member_access_only_p=*/false,
8319 decltype_p,
8320 pidk);
8321 }
8322
8323 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8324 unary-operator, the corresponding tree code is returned. */
8325
8326 static enum tree_code
8327 cp_parser_unary_operator (cp_token* token)
8328 {
8329 switch (token->type)
8330 {
8331 case CPP_MULT:
8332 return INDIRECT_REF;
8333
8334 case CPP_AND:
8335 return ADDR_EXPR;
8336
8337 case CPP_PLUS:
8338 return UNARY_PLUS_EXPR;
8339
8340 case CPP_MINUS:
8341 return NEGATE_EXPR;
8342
8343 case CPP_NOT:
8344 return TRUTH_NOT_EXPR;
8345
8346 case CPP_COMPL:
8347 return BIT_NOT_EXPR;
8348
8349 default:
8350 return ERROR_MARK;
8351 }
8352 }
8353
8354 /* Parse a new-expression.
8355
8356 new-expression:
8357 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8358 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8359
8360 Returns a representation of the expression. */
8361
8362 static tree
8363 cp_parser_new_expression (cp_parser* parser)
8364 {
8365 bool global_scope_p;
8366 vec<tree, va_gc> *placement;
8367 tree type;
8368 vec<tree, va_gc> *initializer;
8369 tree nelts = NULL_TREE;
8370 tree ret;
8371
8372 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8373
8374 /* Look for the optional `::' operator. */
8375 global_scope_p
8376 = (cp_parser_global_scope_opt (parser,
8377 /*current_scope_valid_p=*/false)
8378 != NULL_TREE);
8379 /* Look for the `new' operator. */
8380 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8381 /* There's no easy way to tell a new-placement from the
8382 `( type-id )' construct. */
8383 cp_parser_parse_tentatively (parser);
8384 /* Look for a new-placement. */
8385 placement = cp_parser_new_placement (parser);
8386 /* If that didn't work out, there's no new-placement. */
8387 if (!cp_parser_parse_definitely (parser))
8388 {
8389 if (placement != NULL)
8390 release_tree_vector (placement);
8391 placement = NULL;
8392 }
8393
8394 /* If the next token is a `(', then we have a parenthesized
8395 type-id. */
8396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8397 {
8398 cp_token *token;
8399 const char *saved_message = parser->type_definition_forbidden_message;
8400
8401 /* Consume the `('. */
8402 matching_parens parens;
8403 parens.consume_open (parser);
8404
8405 /* Parse the type-id. */
8406 parser->type_definition_forbidden_message
8407 = G_("types may not be defined in a new-expression");
8408 {
8409 type_id_in_expr_sentinel s (parser);
8410 type = cp_parser_type_id (parser);
8411 }
8412 parser->type_definition_forbidden_message = saved_message;
8413
8414 /* Look for the closing `)'. */
8415 parens.require_close (parser);
8416 token = cp_lexer_peek_token (parser->lexer);
8417 /* There should not be a direct-new-declarator in this production,
8418 but GCC used to allowed this, so we check and emit a sensible error
8419 message for this case. */
8420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8421 {
8422 error_at (token->location,
8423 "array bound forbidden after parenthesized type-id");
8424 inform (token->location,
8425 "try removing the parentheses around the type-id");
8426 cp_parser_direct_new_declarator (parser);
8427 }
8428 }
8429 /* Otherwise, there must be a new-type-id. */
8430 else
8431 type = cp_parser_new_type_id (parser, &nelts);
8432
8433 /* If the next token is a `(' or '{', then we have a new-initializer. */
8434 cp_token *token = cp_lexer_peek_token (parser->lexer);
8435 if (token->type == CPP_OPEN_PAREN
8436 || token->type == CPP_OPEN_BRACE)
8437 initializer = cp_parser_new_initializer (parser);
8438 else
8439 initializer = NULL;
8440
8441 /* A new-expression may not appear in an integral constant
8442 expression. */
8443 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8444 ret = error_mark_node;
8445 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8446 of a new-type-id or type-id of a new-expression, the new-expression shall
8447 contain a new-initializer of the form ( assignment-expression )".
8448 Additionally, consistently with the spirit of DR 1467, we want to accept
8449 'new auto { 2 }' too. */
8450 else if ((ret = type_uses_auto (type))
8451 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8452 && (vec_safe_length (initializer) != 1
8453 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8454 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8455 {
8456 error_at (token->location,
8457 "initialization of new-expression for type %<auto%> "
8458 "requires exactly one element");
8459 ret = error_mark_node;
8460 }
8461 else
8462 {
8463 /* Construct a location e.g.:
8464 ptr = new int[100]
8465 ^~~~~~~~~~~~
8466 with caret == start at the start of the "new" token, and the end
8467 at the end of the final token we consumed. */
8468 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8469 location_t end_loc = get_finish (end_tok->location);
8470 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8471
8472 /* Create a representation of the new-expression. */
8473 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8474 tf_warning_or_error);
8475 protected_set_expr_location (ret, combined_loc);
8476 }
8477
8478 if (placement != NULL)
8479 release_tree_vector (placement);
8480 if (initializer != NULL)
8481 release_tree_vector (initializer);
8482
8483 return ret;
8484 }
8485
8486 /* Parse a new-placement.
8487
8488 new-placement:
8489 ( expression-list )
8490
8491 Returns the same representation as for an expression-list. */
8492
8493 static vec<tree, va_gc> *
8494 cp_parser_new_placement (cp_parser* parser)
8495 {
8496 vec<tree, va_gc> *expression_list;
8497
8498 /* Parse the expression-list. */
8499 expression_list = (cp_parser_parenthesized_expression_list
8500 (parser, non_attr, /*cast_p=*/false,
8501 /*allow_expansion_p=*/true,
8502 /*non_constant_p=*/NULL));
8503
8504 if (expression_list && expression_list->is_empty ())
8505 error ("expected expression-list or type-id");
8506
8507 return expression_list;
8508 }
8509
8510 /* Parse a new-type-id.
8511
8512 new-type-id:
8513 type-specifier-seq new-declarator [opt]
8514
8515 Returns the TYPE allocated. If the new-type-id indicates an array
8516 type, *NELTS is set to the number of elements in the last array
8517 bound; the TYPE will not include the last array bound. */
8518
8519 static tree
8520 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8521 {
8522 cp_decl_specifier_seq type_specifier_seq;
8523 cp_declarator *new_declarator;
8524 cp_declarator *declarator;
8525 cp_declarator *outer_declarator;
8526 const char *saved_message;
8527
8528 /* The type-specifier sequence must not contain type definitions.
8529 (It cannot contain declarations of new types either, but if they
8530 are not definitions we will catch that because they are not
8531 complete.) */
8532 saved_message = parser->type_definition_forbidden_message;
8533 parser->type_definition_forbidden_message
8534 = G_("types may not be defined in a new-type-id");
8535 /* Parse the type-specifier-seq. */
8536 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8537 /*is_trailing_return=*/false,
8538 &type_specifier_seq);
8539 /* Restore the old message. */
8540 parser->type_definition_forbidden_message = saved_message;
8541
8542 if (type_specifier_seq.type == error_mark_node)
8543 return error_mark_node;
8544
8545 /* Parse the new-declarator. */
8546 new_declarator = cp_parser_new_declarator_opt (parser);
8547
8548 /* Determine the number of elements in the last array dimension, if
8549 any. */
8550 *nelts = NULL_TREE;
8551 /* Skip down to the last array dimension. */
8552 declarator = new_declarator;
8553 outer_declarator = NULL;
8554 while (declarator && (declarator->kind == cdk_pointer
8555 || declarator->kind == cdk_ptrmem))
8556 {
8557 outer_declarator = declarator;
8558 declarator = declarator->declarator;
8559 }
8560 while (declarator
8561 && declarator->kind == cdk_array
8562 && declarator->declarator
8563 && declarator->declarator->kind == cdk_array)
8564 {
8565 outer_declarator = declarator;
8566 declarator = declarator->declarator;
8567 }
8568
8569 if (declarator && declarator->kind == cdk_array)
8570 {
8571 *nelts = declarator->u.array.bounds;
8572 if (*nelts == error_mark_node)
8573 *nelts = integer_one_node;
8574
8575 if (outer_declarator)
8576 outer_declarator->declarator = declarator->declarator;
8577 else
8578 new_declarator = NULL;
8579 }
8580
8581 return groktypename (&type_specifier_seq, new_declarator, false);
8582 }
8583
8584 /* Parse an (optional) new-declarator.
8585
8586 new-declarator:
8587 ptr-operator new-declarator [opt]
8588 direct-new-declarator
8589
8590 Returns the declarator. */
8591
8592 static cp_declarator *
8593 cp_parser_new_declarator_opt (cp_parser* parser)
8594 {
8595 enum tree_code code;
8596 tree type, std_attributes = NULL_TREE;
8597 cp_cv_quals cv_quals;
8598
8599 /* We don't know if there's a ptr-operator next, or not. */
8600 cp_parser_parse_tentatively (parser);
8601 /* Look for a ptr-operator. */
8602 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8603 /* If that worked, look for more new-declarators. */
8604 if (cp_parser_parse_definitely (parser))
8605 {
8606 cp_declarator *declarator;
8607
8608 /* Parse another optional declarator. */
8609 declarator = cp_parser_new_declarator_opt (parser);
8610
8611 declarator = cp_parser_make_indirect_declarator
8612 (code, type, cv_quals, declarator, std_attributes);
8613
8614 return declarator;
8615 }
8616
8617 /* If the next token is a `[', there is a direct-new-declarator. */
8618 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8619 return cp_parser_direct_new_declarator (parser);
8620
8621 return NULL;
8622 }
8623
8624 /* Parse a direct-new-declarator.
8625
8626 direct-new-declarator:
8627 [ expression ]
8628 direct-new-declarator [constant-expression]
8629
8630 */
8631
8632 static cp_declarator *
8633 cp_parser_direct_new_declarator (cp_parser* parser)
8634 {
8635 cp_declarator *declarator = NULL;
8636
8637 while (true)
8638 {
8639 tree expression;
8640 cp_token *token;
8641
8642 /* Look for the opening `['. */
8643 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8644
8645 token = cp_lexer_peek_token (parser->lexer);
8646 expression = cp_parser_expression (parser);
8647 /* The standard requires that the expression have integral
8648 type. DR 74 adds enumeration types. We believe that the
8649 real intent is that these expressions be handled like the
8650 expression in a `switch' condition, which also allows
8651 classes with a single conversion to integral or
8652 enumeration type. */
8653 if (!processing_template_decl)
8654 {
8655 expression
8656 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8657 expression,
8658 /*complain=*/true);
8659 if (!expression)
8660 {
8661 error_at (token->location,
8662 "expression in new-declarator must have integral "
8663 "or enumeration type");
8664 expression = error_mark_node;
8665 }
8666 }
8667
8668 /* Look for the closing `]'. */
8669 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8670
8671 /* Add this bound to the declarator. */
8672 declarator = make_array_declarator (declarator, expression);
8673
8674 /* If the next token is not a `[', then there are no more
8675 bounds. */
8676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8677 break;
8678 }
8679
8680 return declarator;
8681 }
8682
8683 /* Parse a new-initializer.
8684
8685 new-initializer:
8686 ( expression-list [opt] )
8687 braced-init-list
8688
8689 Returns a representation of the expression-list. */
8690
8691 static vec<tree, va_gc> *
8692 cp_parser_new_initializer (cp_parser* parser)
8693 {
8694 vec<tree, va_gc> *expression_list;
8695
8696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8697 {
8698 tree t;
8699 bool expr_non_constant_p;
8700 cp_lexer_set_source_position (parser->lexer);
8701 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8702 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8703 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8704 expression_list = make_tree_vector_single (t);
8705 }
8706 else
8707 expression_list = (cp_parser_parenthesized_expression_list
8708 (parser, non_attr, /*cast_p=*/false,
8709 /*allow_expansion_p=*/true,
8710 /*non_constant_p=*/NULL));
8711
8712 return expression_list;
8713 }
8714
8715 /* Parse a delete-expression.
8716
8717 delete-expression:
8718 :: [opt] delete cast-expression
8719 :: [opt] delete [ ] cast-expression
8720
8721 Returns a representation of the expression. */
8722
8723 static tree
8724 cp_parser_delete_expression (cp_parser* parser)
8725 {
8726 bool global_scope_p;
8727 bool array_p;
8728 tree expression;
8729
8730 /* Look for the optional `::' operator. */
8731 global_scope_p
8732 = (cp_parser_global_scope_opt (parser,
8733 /*current_scope_valid_p=*/false)
8734 != NULL_TREE);
8735 /* Look for the `delete' keyword. */
8736 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8737 /* See if the array syntax is in use. */
8738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8739 {
8740 /* Consume the `[' token. */
8741 cp_lexer_consume_token (parser->lexer);
8742 /* Look for the `]' token. */
8743 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8744 /* Remember that this is the `[]' construct. */
8745 array_p = true;
8746 }
8747 else
8748 array_p = false;
8749
8750 /* Parse the cast-expression. */
8751 expression = cp_parser_simple_cast_expression (parser);
8752
8753 /* A delete-expression may not appear in an integral constant
8754 expression. */
8755 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8756 return error_mark_node;
8757
8758 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8759 tf_warning_or_error);
8760 }
8761
8762 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8763 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8764 0 otherwise. */
8765
8766 static int
8767 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8768 {
8769 cp_token *token = cp_lexer_peek_token (parser->lexer);
8770 switch (token->type)
8771 {
8772 case CPP_COMMA:
8773 case CPP_SEMICOLON:
8774 case CPP_QUERY:
8775 case CPP_COLON:
8776 case CPP_CLOSE_SQUARE:
8777 case CPP_CLOSE_PAREN:
8778 case CPP_CLOSE_BRACE:
8779 case CPP_OPEN_BRACE:
8780 case CPP_DOT:
8781 case CPP_DOT_STAR:
8782 case CPP_DEREF:
8783 case CPP_DEREF_STAR:
8784 case CPP_DIV:
8785 case CPP_MOD:
8786 case CPP_LSHIFT:
8787 case CPP_RSHIFT:
8788 case CPP_LESS:
8789 case CPP_GREATER:
8790 case CPP_LESS_EQ:
8791 case CPP_GREATER_EQ:
8792 case CPP_EQ_EQ:
8793 case CPP_NOT_EQ:
8794 case CPP_EQ:
8795 case CPP_MULT_EQ:
8796 case CPP_DIV_EQ:
8797 case CPP_MOD_EQ:
8798 case CPP_PLUS_EQ:
8799 case CPP_MINUS_EQ:
8800 case CPP_RSHIFT_EQ:
8801 case CPP_LSHIFT_EQ:
8802 case CPP_AND_EQ:
8803 case CPP_XOR_EQ:
8804 case CPP_OR_EQ:
8805 case CPP_XOR:
8806 case CPP_OR:
8807 case CPP_OR_OR:
8808 case CPP_EOF:
8809 case CPP_ELLIPSIS:
8810 return 0;
8811
8812 case CPP_OPEN_PAREN:
8813 /* In ((type ()) () the last () isn't a valid cast-expression,
8814 so the whole must be parsed as postfix-expression. */
8815 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8816 != CPP_CLOSE_PAREN;
8817
8818 case CPP_OPEN_SQUARE:
8819 /* '[' may start a primary-expression in obj-c++ and in C++11,
8820 as a lambda-expression, eg, '(void)[]{}'. */
8821 if (cxx_dialect >= cxx11)
8822 return -1;
8823 return c_dialect_objc ();
8824
8825 case CPP_PLUS_PLUS:
8826 case CPP_MINUS_MINUS:
8827 /* '++' and '--' may or may not start a cast-expression:
8828
8829 struct T { void operator++(int); };
8830 void f() { (T())++; }
8831
8832 vs
8833
8834 int a;
8835 (int)++a; */
8836 return -1;
8837
8838 default:
8839 return 1;
8840 }
8841 }
8842
8843 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8844 in the order: const_cast, static_cast, reinterpret_cast.
8845
8846 Don't suggest dynamic_cast.
8847
8848 Return the first legal cast kind found, or NULL otherwise. */
8849
8850 static const char *
8851 get_cast_suggestion (tree dst_type, tree orig_expr)
8852 {
8853 tree trial;
8854
8855 /* Reuse the parser logic by attempting to build the various kinds of
8856 cast, with "complain" disabled.
8857 Identify the first such cast that is valid. */
8858
8859 /* Don't attempt to run such logic within template processing. */
8860 if (processing_template_decl)
8861 return NULL;
8862
8863 /* First try const_cast. */
8864 trial = build_const_cast (dst_type, orig_expr, tf_none);
8865 if (trial != error_mark_node)
8866 return "const_cast";
8867
8868 /* If that fails, try static_cast. */
8869 trial = build_static_cast (dst_type, orig_expr, tf_none);
8870 if (trial != error_mark_node)
8871 return "static_cast";
8872
8873 /* Finally, try reinterpret_cast. */
8874 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8875 if (trial != error_mark_node)
8876 return "reinterpret_cast";
8877
8878 /* No such cast possible. */
8879 return NULL;
8880 }
8881
8882 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8883 suggesting how to convert a C-style cast of the form:
8884
8885 (DST_TYPE)ORIG_EXPR
8886
8887 to a C++-style cast.
8888
8889 The primary range of RICHLOC is asssumed to be that of the original
8890 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8891 of the parens in the C-style cast. */
8892
8893 static void
8894 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8895 location_t close_paren_loc, tree orig_expr,
8896 tree dst_type)
8897 {
8898 /* This function is non-trivial, so bail out now if the warning isn't
8899 going to be emitted. */
8900 if (!warn_old_style_cast)
8901 return;
8902
8903 /* Try to find a legal C++ cast, trying them in order:
8904 const_cast, static_cast, reinterpret_cast. */
8905 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8906 if (!cast_suggestion)
8907 return;
8908
8909 /* Replace the open paren with "CAST_SUGGESTION<". */
8910 pretty_printer pp;
8911 pp_printf (&pp, "%s<", cast_suggestion);
8912 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8913
8914 /* Replace the close paren with "> (". */
8915 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8916
8917 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8918 rich_loc->add_fixit_insert_after (")");
8919 }
8920
8921
8922 /* Parse a cast-expression.
8923
8924 cast-expression:
8925 unary-expression
8926 ( type-id ) cast-expression
8927
8928 ADDRESS_P is true iff the unary-expression is appearing as the
8929 operand of the `&' operator. CAST_P is true if this expression is
8930 the target of a cast.
8931
8932 Returns a representation of the expression. */
8933
8934 static cp_expr
8935 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8936 bool decltype_p, cp_id_kind * pidk)
8937 {
8938 /* If it's a `(', then we might be looking at a cast. */
8939 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8940 {
8941 tree type = NULL_TREE;
8942 cp_expr expr (NULL_TREE);
8943 int cast_expression = 0;
8944 const char *saved_message;
8945
8946 /* There's no way to know yet whether or not this is a cast.
8947 For example, `(int (3))' is a unary-expression, while `(int)
8948 3' is a cast. So, we resort to parsing tentatively. */
8949 cp_parser_parse_tentatively (parser);
8950 /* Types may not be defined in a cast. */
8951 saved_message = parser->type_definition_forbidden_message;
8952 parser->type_definition_forbidden_message
8953 = G_("types may not be defined in casts");
8954 /* Consume the `('. */
8955 matching_parens parens;
8956 cp_token *open_paren = parens.consume_open (parser);
8957 location_t open_paren_loc = open_paren->location;
8958 location_t close_paren_loc = UNKNOWN_LOCATION;
8959
8960 /* A very tricky bit is that `(struct S) { 3 }' is a
8961 compound-literal (which we permit in C++ as an extension).
8962 But, that construct is not a cast-expression -- it is a
8963 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8964 is legal; if the compound-literal were a cast-expression,
8965 you'd need an extra set of parentheses.) But, if we parse
8966 the type-id, and it happens to be a class-specifier, then we
8967 will commit to the parse at that point, because we cannot
8968 undo the action that is done when creating a new class. So,
8969 then we cannot back up and do a postfix-expression.
8970
8971 Another tricky case is the following (c++/29234):
8972
8973 struct S { void operator () (); };
8974
8975 void foo ()
8976 {
8977 ( S()() );
8978 }
8979
8980 As a type-id we parse the parenthesized S()() as a function
8981 returning a function, groktypename complains and we cannot
8982 back up in this case either.
8983
8984 Therefore, we scan ahead to the closing `)', and check to see
8985 if the tokens after the `)' can start a cast-expression. Otherwise
8986 we are dealing with an unary-expression, a postfix-expression
8987 or something else.
8988
8989 Yet another tricky case, in C++11, is the following (c++/54891):
8990
8991 (void)[]{};
8992
8993 The issue is that usually, besides the case of lambda-expressions,
8994 the parenthesized type-id cannot be followed by '[', and, eg, we
8995 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8996 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8997 we don't commit, we try a cast-expression, then an unary-expression.
8998
8999 Save tokens so that we can put them back. */
9000 cp_lexer_save_tokens (parser->lexer);
9001
9002 /* We may be looking at a cast-expression. */
9003 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9004 /*consume_paren=*/true))
9005 cast_expression
9006 = cp_parser_tokens_start_cast_expression (parser);
9007
9008 /* Roll back the tokens we skipped. */
9009 cp_lexer_rollback_tokens (parser->lexer);
9010 /* If we aren't looking at a cast-expression, simulate an error so
9011 that the call to cp_parser_error_occurred below returns true. */
9012 if (!cast_expression)
9013 cp_parser_simulate_error (parser);
9014 else
9015 {
9016 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9017 parser->in_type_id_in_expr_p = true;
9018 /* Look for the type-id. */
9019 type = cp_parser_type_id (parser);
9020 /* Look for the closing `)'. */
9021 cp_token *close_paren = parens.require_close (parser);
9022 if (close_paren)
9023 close_paren_loc = close_paren->location;
9024 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9025 }
9026
9027 /* Restore the saved message. */
9028 parser->type_definition_forbidden_message = saved_message;
9029
9030 /* At this point this can only be either a cast or a
9031 parenthesized ctor such as `(T ())' that looks like a cast to
9032 function returning T. */
9033 if (!cp_parser_error_occurred (parser))
9034 {
9035 /* Only commit if the cast-expression doesn't start with
9036 '++', '--', or '[' in C++11. */
9037 if (cast_expression > 0)
9038 cp_parser_commit_to_topmost_tentative_parse (parser);
9039
9040 expr = cp_parser_cast_expression (parser,
9041 /*address_p=*/false,
9042 /*cast_p=*/true,
9043 /*decltype_p=*/false,
9044 pidk);
9045
9046 if (cp_parser_parse_definitely (parser))
9047 {
9048 /* Warn about old-style casts, if so requested. */
9049 if (warn_old_style_cast
9050 && !in_system_header_at (input_location)
9051 && !VOID_TYPE_P (type)
9052 && current_lang_name != lang_name_c)
9053 {
9054 gcc_rich_location rich_loc (input_location);
9055 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9056 expr, type);
9057 warning_at (&rich_loc, OPT_Wold_style_cast,
9058 "use of old-style cast to %q#T", type);
9059 }
9060
9061 /* Only type conversions to integral or enumeration types
9062 can be used in constant-expressions. */
9063 if (!cast_valid_in_integral_constant_expression_p (type)
9064 && cp_parser_non_integral_constant_expression (parser,
9065 NIC_CAST))
9066 return error_mark_node;
9067
9068 /* Perform the cast. */
9069 /* Make a location:
9070 (TYPE) EXPR
9071 ^~~~~~~~~~~
9072 with start==caret at the open paren, extending to the
9073 end of "expr". */
9074 location_t cast_loc = make_location (open_paren_loc,
9075 open_paren_loc,
9076 expr.get_finish ());
9077 expr = build_c_cast (cast_loc, type, expr);
9078 return expr;
9079 }
9080 }
9081 else
9082 cp_parser_abort_tentative_parse (parser);
9083 }
9084
9085 /* If we get here, then it's not a cast, so it must be a
9086 unary-expression. */
9087 return cp_parser_unary_expression (parser, pidk, address_p,
9088 cast_p, decltype_p);
9089 }
9090
9091 /* Parse a binary expression of the general form:
9092
9093 pm-expression:
9094 cast-expression
9095 pm-expression .* cast-expression
9096 pm-expression ->* cast-expression
9097
9098 multiplicative-expression:
9099 pm-expression
9100 multiplicative-expression * pm-expression
9101 multiplicative-expression / pm-expression
9102 multiplicative-expression % pm-expression
9103
9104 additive-expression:
9105 multiplicative-expression
9106 additive-expression + multiplicative-expression
9107 additive-expression - multiplicative-expression
9108
9109 shift-expression:
9110 additive-expression
9111 shift-expression << additive-expression
9112 shift-expression >> additive-expression
9113
9114 relational-expression:
9115 shift-expression
9116 relational-expression < shift-expression
9117 relational-expression > shift-expression
9118 relational-expression <= shift-expression
9119 relational-expression >= shift-expression
9120
9121 GNU Extension:
9122
9123 relational-expression:
9124 relational-expression <? shift-expression
9125 relational-expression >? shift-expression
9126
9127 equality-expression:
9128 relational-expression
9129 equality-expression == relational-expression
9130 equality-expression != relational-expression
9131
9132 and-expression:
9133 equality-expression
9134 and-expression & equality-expression
9135
9136 exclusive-or-expression:
9137 and-expression
9138 exclusive-or-expression ^ and-expression
9139
9140 inclusive-or-expression:
9141 exclusive-or-expression
9142 inclusive-or-expression | exclusive-or-expression
9143
9144 logical-and-expression:
9145 inclusive-or-expression
9146 logical-and-expression && inclusive-or-expression
9147
9148 logical-or-expression:
9149 logical-and-expression
9150 logical-or-expression || logical-and-expression
9151
9152 All these are implemented with a single function like:
9153
9154 binary-expression:
9155 simple-cast-expression
9156 binary-expression <token> binary-expression
9157
9158 CAST_P is true if this expression is the target of a cast.
9159
9160 The binops_by_token map is used to get the tree codes for each <token> type.
9161 binary-expressions are associated according to a precedence table. */
9162
9163 #define TOKEN_PRECEDENCE(token) \
9164 (((token->type == CPP_GREATER \
9165 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9166 && !parser->greater_than_is_operator_p) \
9167 ? PREC_NOT_OPERATOR \
9168 : binops_by_token[token->type].prec)
9169
9170 static cp_expr
9171 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9172 bool no_toplevel_fold_p,
9173 bool decltype_p,
9174 enum cp_parser_prec prec,
9175 cp_id_kind * pidk)
9176 {
9177 cp_parser_expression_stack stack;
9178 cp_parser_expression_stack_entry *sp = &stack[0];
9179 cp_parser_expression_stack_entry current;
9180 cp_expr rhs;
9181 cp_token *token;
9182 enum tree_code rhs_type;
9183 enum cp_parser_prec new_prec, lookahead_prec;
9184 tree overload;
9185
9186 /* Parse the first expression. */
9187 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9188 ? TRUTH_NOT_EXPR : ERROR_MARK);
9189 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9190 cast_p, decltype_p, pidk);
9191 current.prec = prec;
9192
9193 if (cp_parser_error_occurred (parser))
9194 return error_mark_node;
9195
9196 for (;;)
9197 {
9198 /* Get an operator token. */
9199 token = cp_lexer_peek_token (parser->lexer);
9200
9201 if (warn_cxx11_compat
9202 && token->type == CPP_RSHIFT
9203 && !parser->greater_than_is_operator_p)
9204 {
9205 if (warning_at (token->location, OPT_Wc__11_compat,
9206 "%<>>%> operator is treated"
9207 " as two right angle brackets in C++11"))
9208 inform (token->location,
9209 "suggest parentheses around %<>>%> expression");
9210 }
9211
9212 new_prec = TOKEN_PRECEDENCE (token);
9213 if (new_prec != PREC_NOT_OPERATOR
9214 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9215 /* This is a fold-expression; handle it later. */
9216 new_prec = PREC_NOT_OPERATOR;
9217
9218 /* Popping an entry off the stack means we completed a subexpression:
9219 - either we found a token which is not an operator (`>' where it is not
9220 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9221 will happen repeatedly;
9222 - or, we found an operator which has lower priority. This is the case
9223 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9224 parsing `3 * 4'. */
9225 if (new_prec <= current.prec)
9226 {
9227 if (sp == stack)
9228 break;
9229 else
9230 goto pop;
9231 }
9232
9233 get_rhs:
9234 current.tree_type = binops_by_token[token->type].tree_type;
9235 current.loc = token->location;
9236
9237 /* We used the operator token. */
9238 cp_lexer_consume_token (parser->lexer);
9239
9240 /* For "false && x" or "true || x", x will never be executed;
9241 disable warnings while evaluating it. */
9242 if (current.tree_type == TRUTH_ANDIF_EXPR)
9243 c_inhibit_evaluation_warnings +=
9244 cp_fully_fold (current.lhs) == truthvalue_false_node;
9245 else if (current.tree_type == TRUTH_ORIF_EXPR)
9246 c_inhibit_evaluation_warnings +=
9247 cp_fully_fold (current.lhs) == truthvalue_true_node;
9248
9249 /* Extract another operand. It may be the RHS of this expression
9250 or the LHS of a new, higher priority expression. */
9251 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9252 ? TRUTH_NOT_EXPR : ERROR_MARK);
9253 rhs = cp_parser_simple_cast_expression (parser);
9254
9255 /* Get another operator token. Look up its precedence to avoid
9256 building a useless (immediately popped) stack entry for common
9257 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9258 token = cp_lexer_peek_token (parser->lexer);
9259 lookahead_prec = TOKEN_PRECEDENCE (token);
9260 if (lookahead_prec != PREC_NOT_OPERATOR
9261 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9262 lookahead_prec = PREC_NOT_OPERATOR;
9263 if (lookahead_prec > new_prec)
9264 {
9265 /* ... and prepare to parse the RHS of the new, higher priority
9266 expression. Since precedence levels on the stack are
9267 monotonically increasing, we do not have to care about
9268 stack overflows. */
9269 *sp = current;
9270 ++sp;
9271 current.lhs = rhs;
9272 current.lhs_type = rhs_type;
9273 current.prec = new_prec;
9274 new_prec = lookahead_prec;
9275 goto get_rhs;
9276
9277 pop:
9278 lookahead_prec = new_prec;
9279 /* If the stack is not empty, we have parsed into LHS the right side
9280 (`4' in the example above) of an expression we had suspended.
9281 We can use the information on the stack to recover the LHS (`3')
9282 from the stack together with the tree code (`MULT_EXPR'), and
9283 the precedence of the higher level subexpression
9284 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9285 which will be used to actually build the additive expression. */
9286 rhs = current.lhs;
9287 rhs_type = current.lhs_type;
9288 --sp;
9289 current = *sp;
9290 }
9291
9292 /* Undo the disabling of warnings done above. */
9293 if (current.tree_type == TRUTH_ANDIF_EXPR)
9294 c_inhibit_evaluation_warnings -=
9295 cp_fully_fold (current.lhs) == truthvalue_false_node;
9296 else if (current.tree_type == TRUTH_ORIF_EXPR)
9297 c_inhibit_evaluation_warnings -=
9298 cp_fully_fold (current.lhs) == truthvalue_true_node;
9299
9300 if (warn_logical_not_paren
9301 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9302 && current.lhs_type == TRUTH_NOT_EXPR
9303 /* Avoid warning for !!x == y. */
9304 && (TREE_CODE (current.lhs) != NE_EXPR
9305 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9306 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9307 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9308 /* Avoid warning for !b == y where b is boolean. */
9309 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9310 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9311 != BOOLEAN_TYPE))))
9312 /* Avoid warning for !!b == y where b is boolean. */
9313 && (!DECL_P (current.lhs)
9314 || TREE_TYPE (current.lhs) == NULL_TREE
9315 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9316 warn_logical_not_parentheses (current.loc, current.tree_type,
9317 current.lhs, maybe_constant_value (rhs));
9318
9319 overload = NULL;
9320
9321 location_t combined_loc = make_location (current.loc,
9322 current.lhs.get_start (),
9323 rhs.get_finish ());
9324
9325 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9326 ERROR_MARK for everything that is not a binary expression.
9327 This makes warn_about_parentheses miss some warnings that
9328 involve unary operators. For unary expressions we should
9329 pass the correct tree_code unless the unary expression was
9330 surrounded by parentheses.
9331 */
9332 if (no_toplevel_fold_p
9333 && lookahead_prec <= current.prec
9334 && sp == stack)
9335 {
9336 if (current.lhs == error_mark_node || rhs == error_mark_node)
9337 current.lhs = error_mark_node;
9338 else
9339 {
9340 current.lhs
9341 = build_min (current.tree_type,
9342 TREE_CODE_CLASS (current.tree_type)
9343 == tcc_comparison
9344 ? boolean_type_node : TREE_TYPE (current.lhs),
9345 current.lhs.get_value (), rhs.get_value ());
9346 SET_EXPR_LOCATION (current.lhs, combined_loc);
9347 }
9348 }
9349 else
9350 {
9351 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9352 current.lhs, current.lhs_type,
9353 rhs, rhs_type, &overload,
9354 complain_flags (decltype_p));
9355 /* TODO: build_x_binary_op doesn't always honor the location. */
9356 current.lhs.set_location (combined_loc);
9357 }
9358 current.lhs_type = current.tree_type;
9359
9360 /* If the binary operator required the use of an overloaded operator,
9361 then this expression cannot be an integral constant-expression.
9362 An overloaded operator can be used even if both operands are
9363 otherwise permissible in an integral constant-expression if at
9364 least one of the operands is of enumeration type. */
9365
9366 if (overload
9367 && cp_parser_non_integral_constant_expression (parser,
9368 NIC_OVERLOADED))
9369 return error_mark_node;
9370 }
9371
9372 return current.lhs;
9373 }
9374
9375 static cp_expr
9376 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9377 bool no_toplevel_fold_p,
9378 enum cp_parser_prec prec,
9379 cp_id_kind * pidk)
9380 {
9381 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9382 /*decltype*/false, prec, pidk);
9383 }
9384
9385 /* Parse the `? expression : assignment-expression' part of a
9386 conditional-expression. The LOGICAL_OR_EXPR is the
9387 logical-or-expression that started the conditional-expression.
9388 Returns a representation of the entire conditional-expression.
9389
9390 This routine is used by cp_parser_assignment_expression.
9391
9392 ? expression : assignment-expression
9393
9394 GNU Extensions:
9395
9396 ? : assignment-expression */
9397
9398 static tree
9399 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9400 {
9401 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9402 cp_expr assignment_expr;
9403 struct cp_token *token;
9404 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9405
9406 /* Consume the `?' token. */
9407 cp_lexer_consume_token (parser->lexer);
9408 token = cp_lexer_peek_token (parser->lexer);
9409 if (cp_parser_allow_gnu_extensions_p (parser)
9410 && token->type == CPP_COLON)
9411 {
9412 pedwarn (token->location, OPT_Wpedantic,
9413 "ISO C++ does not allow ?: with omitted middle operand");
9414 /* Implicit true clause. */
9415 expr = NULL_TREE;
9416 c_inhibit_evaluation_warnings +=
9417 folded_logical_or_expr == truthvalue_true_node;
9418 warn_for_omitted_condop (token->location, logical_or_expr);
9419 }
9420 else
9421 {
9422 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9423 parser->colon_corrects_to_scope_p = false;
9424 /* Parse the expression. */
9425 c_inhibit_evaluation_warnings +=
9426 folded_logical_or_expr == truthvalue_false_node;
9427 expr = cp_parser_expression (parser);
9428 c_inhibit_evaluation_warnings +=
9429 ((folded_logical_or_expr == truthvalue_true_node)
9430 - (folded_logical_or_expr == truthvalue_false_node));
9431 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9432 }
9433
9434 /* The next token should be a `:'. */
9435 cp_parser_require (parser, CPP_COLON, RT_COLON);
9436 /* Parse the assignment-expression. */
9437 assignment_expr = cp_parser_assignment_expression (parser);
9438 c_inhibit_evaluation_warnings -=
9439 folded_logical_or_expr == truthvalue_true_node;
9440
9441 /* Make a location:
9442 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9443 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9444 with the caret at the "?", ranging from the start of
9445 the logical_or_expr to the end of the assignment_expr. */
9446 loc = make_location (loc,
9447 logical_or_expr.get_start (),
9448 assignment_expr.get_finish ());
9449
9450 /* Build the conditional-expression. */
9451 return build_x_conditional_expr (loc, logical_or_expr,
9452 expr,
9453 assignment_expr,
9454 tf_warning_or_error);
9455 }
9456
9457 /* Parse an assignment-expression.
9458
9459 assignment-expression:
9460 conditional-expression
9461 logical-or-expression assignment-operator assignment_expression
9462 throw-expression
9463
9464 CAST_P is true if this expression is the target of a cast.
9465 DECLTYPE_P is true if this expression is the operand of decltype.
9466
9467 Returns a representation for the expression. */
9468
9469 static cp_expr
9470 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9471 bool cast_p, bool decltype_p)
9472 {
9473 cp_expr expr;
9474
9475 /* If the next token is the `throw' keyword, then we're looking at
9476 a throw-expression. */
9477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9478 expr = cp_parser_throw_expression (parser);
9479 /* Otherwise, it must be that we are looking at a
9480 logical-or-expression. */
9481 else
9482 {
9483 /* Parse the binary expressions (logical-or-expression). */
9484 expr = cp_parser_binary_expression (parser, cast_p, false,
9485 decltype_p,
9486 PREC_NOT_OPERATOR, pidk);
9487 /* If the next token is a `?' then we're actually looking at a
9488 conditional-expression. */
9489 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9490 return cp_parser_question_colon_clause (parser, expr);
9491 else
9492 {
9493 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9494
9495 /* If it's an assignment-operator, we're using the second
9496 production. */
9497 enum tree_code assignment_operator
9498 = cp_parser_assignment_operator_opt (parser);
9499 if (assignment_operator != ERROR_MARK)
9500 {
9501 bool non_constant_p;
9502
9503 /* Parse the right-hand side of the assignment. */
9504 cp_expr rhs = cp_parser_initializer_clause (parser,
9505 &non_constant_p);
9506
9507 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9508 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9509
9510 /* An assignment may not appear in a
9511 constant-expression. */
9512 if (cp_parser_non_integral_constant_expression (parser,
9513 NIC_ASSIGNMENT))
9514 return error_mark_node;
9515 /* Build the assignment expression. Its default
9516 location:
9517 LHS = RHS
9518 ~~~~^~~~~
9519 is the location of the '=' token as the
9520 caret, ranging from the start of the lhs to the
9521 end of the rhs. */
9522 loc = make_location (loc,
9523 expr.get_start (),
9524 rhs.get_finish ());
9525 expr = build_x_modify_expr (loc, expr,
9526 assignment_operator,
9527 rhs,
9528 complain_flags (decltype_p));
9529 /* TODO: build_x_modify_expr doesn't honor the location,
9530 so we must set it here. */
9531 expr.set_location (loc);
9532 }
9533 }
9534 }
9535
9536 return expr;
9537 }
9538
9539 /* Parse an (optional) assignment-operator.
9540
9541 assignment-operator: one of
9542 = *= /= %= += -= >>= <<= &= ^= |=
9543
9544 GNU Extension:
9545
9546 assignment-operator: one of
9547 <?= >?=
9548
9549 If the next token is an assignment operator, the corresponding tree
9550 code is returned, and the token is consumed. For example, for
9551 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9552 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9553 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9554 operator, ERROR_MARK is returned. */
9555
9556 static enum tree_code
9557 cp_parser_assignment_operator_opt (cp_parser* parser)
9558 {
9559 enum tree_code op;
9560 cp_token *token;
9561
9562 /* Peek at the next token. */
9563 token = cp_lexer_peek_token (parser->lexer);
9564
9565 switch (token->type)
9566 {
9567 case CPP_EQ:
9568 op = NOP_EXPR;
9569 break;
9570
9571 case CPP_MULT_EQ:
9572 op = MULT_EXPR;
9573 break;
9574
9575 case CPP_DIV_EQ:
9576 op = TRUNC_DIV_EXPR;
9577 break;
9578
9579 case CPP_MOD_EQ:
9580 op = TRUNC_MOD_EXPR;
9581 break;
9582
9583 case CPP_PLUS_EQ:
9584 op = PLUS_EXPR;
9585 break;
9586
9587 case CPP_MINUS_EQ:
9588 op = MINUS_EXPR;
9589 break;
9590
9591 case CPP_RSHIFT_EQ:
9592 op = RSHIFT_EXPR;
9593 break;
9594
9595 case CPP_LSHIFT_EQ:
9596 op = LSHIFT_EXPR;
9597 break;
9598
9599 case CPP_AND_EQ:
9600 op = BIT_AND_EXPR;
9601 break;
9602
9603 case CPP_XOR_EQ:
9604 op = BIT_XOR_EXPR;
9605 break;
9606
9607 case CPP_OR_EQ:
9608 op = BIT_IOR_EXPR;
9609 break;
9610
9611 default:
9612 /* Nothing else is an assignment operator. */
9613 op = ERROR_MARK;
9614 }
9615
9616 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9617 if (op != ERROR_MARK
9618 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9619 op = ERROR_MARK;
9620
9621 /* If it was an assignment operator, consume it. */
9622 if (op != ERROR_MARK)
9623 cp_lexer_consume_token (parser->lexer);
9624
9625 return op;
9626 }
9627
9628 /* Parse an expression.
9629
9630 expression:
9631 assignment-expression
9632 expression , assignment-expression
9633
9634 CAST_P is true if this expression is the target of a cast.
9635 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9636 except possibly parenthesized or on the RHS of a comma (N3276).
9637
9638 Returns a representation of the expression. */
9639
9640 static cp_expr
9641 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9642 bool cast_p, bool decltype_p)
9643 {
9644 cp_expr expression = NULL_TREE;
9645 location_t loc = UNKNOWN_LOCATION;
9646
9647 while (true)
9648 {
9649 cp_expr assignment_expression;
9650
9651 /* Parse the next assignment-expression. */
9652 assignment_expression
9653 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9654
9655 /* We don't create a temporary for a call that is the immediate operand
9656 of decltype or on the RHS of a comma. But when we see a comma, we
9657 need to create a temporary for a call on the LHS. */
9658 if (decltype_p && !processing_template_decl
9659 && TREE_CODE (assignment_expression) == CALL_EXPR
9660 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9661 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9662 assignment_expression
9663 = build_cplus_new (TREE_TYPE (assignment_expression),
9664 assignment_expression, tf_warning_or_error);
9665
9666 /* If this is the first assignment-expression, we can just
9667 save it away. */
9668 if (!expression)
9669 expression = assignment_expression;
9670 else
9671 {
9672 /* Create a location with caret at the comma, ranging
9673 from the start of the LHS to the end of the RHS. */
9674 loc = make_location (loc,
9675 expression.get_start (),
9676 assignment_expression.get_finish ());
9677 expression = build_x_compound_expr (loc, expression,
9678 assignment_expression,
9679 complain_flags (decltype_p));
9680 expression.set_location (loc);
9681 }
9682 /* If the next token is not a comma, or we're in a fold-expression, then
9683 we are done with the expression. */
9684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9685 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9686 break;
9687 /* Consume the `,'. */
9688 loc = cp_lexer_peek_token (parser->lexer)->location;
9689 cp_lexer_consume_token (parser->lexer);
9690 /* A comma operator cannot appear in a constant-expression. */
9691 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9692 expression = error_mark_node;
9693 }
9694
9695 return expression;
9696 }
9697
9698 /* Parse a constant-expression.
9699
9700 constant-expression:
9701 conditional-expression
9702
9703 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9704 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9705 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9706 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9707 only parse a conditional-expression, otherwise parse an
9708 assignment-expression. See below for rationale. */
9709
9710 static cp_expr
9711 cp_parser_constant_expression (cp_parser* parser,
9712 bool allow_non_constant_p,
9713 bool *non_constant_p,
9714 bool strict_p)
9715 {
9716 bool saved_integral_constant_expression_p;
9717 bool saved_allow_non_integral_constant_expression_p;
9718 bool saved_non_integral_constant_expression_p;
9719 cp_expr expression;
9720
9721 /* It might seem that we could simply parse the
9722 conditional-expression, and then check to see if it were
9723 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9724 one that the compiler can figure out is constant, possibly after
9725 doing some simplifications or optimizations. The standard has a
9726 precise definition of constant-expression, and we must honor
9727 that, even though it is somewhat more restrictive.
9728
9729 For example:
9730
9731 int i[(2, 3)];
9732
9733 is not a legal declaration, because `(2, 3)' is not a
9734 constant-expression. The `,' operator is forbidden in a
9735 constant-expression. However, GCC's constant-folding machinery
9736 will fold this operation to an INTEGER_CST for `3'. */
9737
9738 /* Save the old settings. */
9739 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9740 saved_allow_non_integral_constant_expression_p
9741 = parser->allow_non_integral_constant_expression_p;
9742 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9743 /* We are now parsing a constant-expression. */
9744 parser->integral_constant_expression_p = true;
9745 parser->allow_non_integral_constant_expression_p
9746 = (allow_non_constant_p || cxx_dialect >= cxx11);
9747 parser->non_integral_constant_expression_p = false;
9748 /* Although the grammar says "conditional-expression", when not STRICT_P,
9749 we parse an "assignment-expression", which also permits
9750 "throw-expression" and the use of assignment operators. In the case
9751 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9752 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9753 actually essential that we look for an assignment-expression.
9754 For example, cp_parser_initializer_clauses uses this function to
9755 determine whether a particular assignment-expression is in fact
9756 constant. */
9757 if (strict_p)
9758 {
9759 /* Parse the binary expressions (logical-or-expression). */
9760 expression = cp_parser_binary_expression (parser, false, false, false,
9761 PREC_NOT_OPERATOR, NULL);
9762 /* If the next token is a `?' then we're actually looking at
9763 a conditional-expression; otherwise we're done. */
9764 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9765 expression = cp_parser_question_colon_clause (parser, expression);
9766 }
9767 else
9768 expression = cp_parser_assignment_expression (parser);
9769 /* Restore the old settings. */
9770 parser->integral_constant_expression_p
9771 = saved_integral_constant_expression_p;
9772 parser->allow_non_integral_constant_expression_p
9773 = saved_allow_non_integral_constant_expression_p;
9774 if (cxx_dialect >= cxx11)
9775 {
9776 /* Require an rvalue constant expression here; that's what our
9777 callers expect. Reference constant expressions are handled
9778 separately in e.g. cp_parser_template_argument. */
9779 tree decay = expression;
9780 if (TREE_TYPE (expression)
9781 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9782 decay = build_address (expression);
9783 bool is_const = potential_rvalue_constant_expression (decay);
9784 parser->non_integral_constant_expression_p = !is_const;
9785 if (!is_const && !allow_non_constant_p)
9786 require_potential_rvalue_constant_expression (decay);
9787 }
9788 if (allow_non_constant_p)
9789 *non_constant_p = parser->non_integral_constant_expression_p;
9790 parser->non_integral_constant_expression_p
9791 = saved_non_integral_constant_expression_p;
9792
9793 return expression;
9794 }
9795
9796 /* Parse __builtin_offsetof.
9797
9798 offsetof-expression:
9799 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9800
9801 offsetof-member-designator:
9802 id-expression
9803 | offsetof-member-designator "." id-expression
9804 | offsetof-member-designator "[" expression "]"
9805 | offsetof-member-designator "->" id-expression */
9806
9807 static cp_expr
9808 cp_parser_builtin_offsetof (cp_parser *parser)
9809 {
9810 int save_ice_p, save_non_ice_p;
9811 tree type;
9812 cp_expr expr;
9813 cp_id_kind dummy;
9814 cp_token *token;
9815 location_t finish_loc;
9816
9817 /* We're about to accept non-integral-constant things, but will
9818 definitely yield an integral constant expression. Save and
9819 restore these values around our local parsing. */
9820 save_ice_p = parser->integral_constant_expression_p;
9821 save_non_ice_p = parser->non_integral_constant_expression_p;
9822
9823 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9824
9825 /* Consume the "__builtin_offsetof" token. */
9826 cp_lexer_consume_token (parser->lexer);
9827 /* Consume the opening `('. */
9828 matching_parens parens;
9829 parens.require_open (parser);
9830 /* Parse the type-id. */
9831 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9832 type = cp_parser_type_id (parser);
9833 /* Look for the `,'. */
9834 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9835 token = cp_lexer_peek_token (parser->lexer);
9836
9837 /* Build the (type *)null that begins the traditional offsetof macro. */
9838 tree object_ptr
9839 = build_static_cast (build_pointer_type (type), null_pointer_node,
9840 tf_warning_or_error);
9841
9842 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9843 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9844 true, &dummy, token->location);
9845 while (true)
9846 {
9847 token = cp_lexer_peek_token (parser->lexer);
9848 switch (token->type)
9849 {
9850 case CPP_OPEN_SQUARE:
9851 /* offsetof-member-designator "[" expression "]" */
9852 expr = cp_parser_postfix_open_square_expression (parser, expr,
9853 true, false);
9854 break;
9855
9856 case CPP_DEREF:
9857 /* offsetof-member-designator "->" identifier */
9858 expr = grok_array_decl (token->location, expr,
9859 integer_zero_node, false);
9860 /* FALLTHRU */
9861
9862 case CPP_DOT:
9863 /* offsetof-member-designator "." identifier */
9864 cp_lexer_consume_token (parser->lexer);
9865 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9866 expr, true, &dummy,
9867 token->location);
9868 break;
9869
9870 case CPP_CLOSE_PAREN:
9871 /* Consume the ")" token. */
9872 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9873 cp_lexer_consume_token (parser->lexer);
9874 goto success;
9875
9876 default:
9877 /* Error. We know the following require will fail, but
9878 that gives the proper error message. */
9879 parens.require_close (parser);
9880 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9881 expr = error_mark_node;
9882 goto failure;
9883 }
9884 }
9885
9886 success:
9887 /* Make a location of the form:
9888 __builtin_offsetof (struct s, f)
9889 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9890 with caret at the type-id, ranging from the start of the
9891 "_builtin_offsetof" token to the close paren. */
9892 loc = make_location (loc, start_loc, finish_loc);
9893 /* The result will be an INTEGER_CST, so we need to explicitly
9894 preserve the location. */
9895 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9896
9897 failure:
9898 parser->integral_constant_expression_p = save_ice_p;
9899 parser->non_integral_constant_expression_p = save_non_ice_p;
9900
9901 expr = expr.maybe_add_location_wrapper ();
9902 return expr;
9903 }
9904
9905 /* Parse a trait expression.
9906
9907 Returns a representation of the expression, the underlying type
9908 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9909
9910 static cp_expr
9911 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9912 {
9913 cp_trait_kind kind;
9914 tree type1, type2 = NULL_TREE;
9915 bool binary = false;
9916 bool variadic = false;
9917
9918 switch (keyword)
9919 {
9920 case RID_HAS_NOTHROW_ASSIGN:
9921 kind = CPTK_HAS_NOTHROW_ASSIGN;
9922 break;
9923 case RID_HAS_NOTHROW_CONSTRUCTOR:
9924 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9925 break;
9926 case RID_HAS_NOTHROW_COPY:
9927 kind = CPTK_HAS_NOTHROW_COPY;
9928 break;
9929 case RID_HAS_TRIVIAL_ASSIGN:
9930 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9931 break;
9932 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9933 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9934 break;
9935 case RID_HAS_TRIVIAL_COPY:
9936 kind = CPTK_HAS_TRIVIAL_COPY;
9937 break;
9938 case RID_HAS_TRIVIAL_DESTRUCTOR:
9939 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9940 break;
9941 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9942 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9943 break;
9944 case RID_HAS_VIRTUAL_DESTRUCTOR:
9945 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9946 break;
9947 case RID_IS_ABSTRACT:
9948 kind = CPTK_IS_ABSTRACT;
9949 break;
9950 case RID_IS_AGGREGATE:
9951 kind = CPTK_IS_AGGREGATE;
9952 break;
9953 case RID_IS_BASE_OF:
9954 kind = CPTK_IS_BASE_OF;
9955 binary = true;
9956 break;
9957 case RID_IS_CLASS:
9958 kind = CPTK_IS_CLASS;
9959 break;
9960 case RID_IS_EMPTY:
9961 kind = CPTK_IS_EMPTY;
9962 break;
9963 case RID_IS_ENUM:
9964 kind = CPTK_IS_ENUM;
9965 break;
9966 case RID_IS_FINAL:
9967 kind = CPTK_IS_FINAL;
9968 break;
9969 case RID_IS_LITERAL_TYPE:
9970 kind = CPTK_IS_LITERAL_TYPE;
9971 break;
9972 case RID_IS_POD:
9973 kind = CPTK_IS_POD;
9974 break;
9975 case RID_IS_POLYMORPHIC:
9976 kind = CPTK_IS_POLYMORPHIC;
9977 break;
9978 case RID_IS_SAME_AS:
9979 kind = CPTK_IS_SAME_AS;
9980 binary = true;
9981 break;
9982 case RID_IS_STD_LAYOUT:
9983 kind = CPTK_IS_STD_LAYOUT;
9984 break;
9985 case RID_IS_TRIVIAL:
9986 kind = CPTK_IS_TRIVIAL;
9987 break;
9988 case RID_IS_TRIVIALLY_ASSIGNABLE:
9989 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9990 binary = true;
9991 break;
9992 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9993 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9994 variadic = true;
9995 break;
9996 case RID_IS_TRIVIALLY_COPYABLE:
9997 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9998 break;
9999 case RID_IS_UNION:
10000 kind = CPTK_IS_UNION;
10001 break;
10002 case RID_UNDERLYING_TYPE:
10003 kind = CPTK_UNDERLYING_TYPE;
10004 break;
10005 case RID_BASES:
10006 kind = CPTK_BASES;
10007 break;
10008 case RID_DIRECT_BASES:
10009 kind = CPTK_DIRECT_BASES;
10010 break;
10011 case RID_IS_ASSIGNABLE:
10012 kind = CPTK_IS_ASSIGNABLE;
10013 binary = true;
10014 break;
10015 case RID_IS_CONSTRUCTIBLE:
10016 kind = CPTK_IS_CONSTRUCTIBLE;
10017 variadic = true;
10018 break;
10019 default:
10020 gcc_unreachable ();
10021 }
10022
10023 /* Get location of initial token. */
10024 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10025
10026 /* Consume the token. */
10027 cp_lexer_consume_token (parser->lexer);
10028
10029 matching_parens parens;
10030 parens.require_open (parser);
10031
10032 {
10033 type_id_in_expr_sentinel s (parser);
10034 type1 = cp_parser_type_id (parser);
10035 }
10036
10037 if (type1 == error_mark_node)
10038 return error_mark_node;
10039
10040 if (binary)
10041 {
10042 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10043
10044 {
10045 type_id_in_expr_sentinel s (parser);
10046 type2 = cp_parser_type_id (parser);
10047 }
10048
10049 if (type2 == error_mark_node)
10050 return error_mark_node;
10051 }
10052 else if (variadic)
10053 {
10054 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10055 {
10056 cp_lexer_consume_token (parser->lexer);
10057 tree elt = cp_parser_type_id (parser);
10058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10059 {
10060 cp_lexer_consume_token (parser->lexer);
10061 elt = make_pack_expansion (elt);
10062 }
10063 if (elt == error_mark_node)
10064 return error_mark_node;
10065 type2 = tree_cons (NULL_TREE, elt, type2);
10066 }
10067 }
10068
10069 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10070 parens.require_close (parser);
10071
10072 /* Construct a location of the form:
10073 __is_trivially_copyable(_Tp)
10074 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10075 with start == caret, finishing at the close-paren. */
10076 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10077
10078 /* Complete the trait expression, which may mean either processing
10079 the trait expr now or saving it for template instantiation. */
10080 switch (kind)
10081 {
10082 case CPTK_UNDERLYING_TYPE:
10083 return cp_expr (finish_underlying_type (type1), trait_loc);
10084 case CPTK_BASES:
10085 return cp_expr (finish_bases (type1, false), trait_loc);
10086 case CPTK_DIRECT_BASES:
10087 return cp_expr (finish_bases (type1, true), trait_loc);
10088 default:
10089 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10090 }
10091 }
10092
10093 /* Parse a lambda expression.
10094
10095 lambda-expression:
10096 lambda-introducer lambda-declarator [opt] compound-statement
10097
10098 Returns a representation of the expression. */
10099
10100 static cp_expr
10101 cp_parser_lambda_expression (cp_parser* parser)
10102 {
10103 tree lambda_expr = build_lambda_expr ();
10104 tree type;
10105 bool ok = true;
10106 cp_token *token = cp_lexer_peek_token (parser->lexer);
10107 cp_token_position start = 0;
10108
10109 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10110
10111 if (cp_unevaluated_operand)
10112 {
10113 if (!token->error_reported)
10114 {
10115 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10116 "lambda-expression in unevaluated context");
10117 token->error_reported = true;
10118 }
10119 ok = false;
10120 }
10121 else if (parser->in_template_argument_list_p)
10122 {
10123 if (!token->error_reported)
10124 {
10125 error_at (token->location, "lambda-expression in template-argument");
10126 token->error_reported = true;
10127 }
10128 ok = false;
10129 }
10130
10131 /* We may be in the middle of deferred access check. Disable
10132 it now. */
10133 push_deferring_access_checks (dk_no_deferred);
10134
10135 cp_parser_lambda_introducer (parser, lambda_expr);
10136
10137 type = begin_lambda_type (lambda_expr);
10138 if (type == error_mark_node)
10139 return error_mark_node;
10140
10141 record_lambda_scope (lambda_expr);
10142
10143 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10144 determine_visibility (TYPE_NAME (type));
10145
10146 /* Now that we've started the type, add the capture fields for any
10147 explicit captures. */
10148 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10149
10150 {
10151 /* Inside the class, surrounding template-parameter-lists do not apply. */
10152 unsigned int saved_num_template_parameter_lists
10153 = parser->num_template_parameter_lists;
10154 unsigned char in_statement = parser->in_statement;
10155 bool in_switch_statement_p = parser->in_switch_statement_p;
10156 bool fully_implicit_function_template_p
10157 = parser->fully_implicit_function_template_p;
10158 tree implicit_template_parms = parser->implicit_template_parms;
10159 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10160 bool auto_is_implicit_function_template_parm_p
10161 = parser->auto_is_implicit_function_template_parm_p;
10162
10163 parser->num_template_parameter_lists = 0;
10164 parser->in_statement = 0;
10165 parser->in_switch_statement_p = false;
10166 parser->fully_implicit_function_template_p = false;
10167 parser->implicit_template_parms = 0;
10168 parser->implicit_template_scope = 0;
10169 parser->auto_is_implicit_function_template_parm_p = false;
10170
10171 /* By virtue of defining a local class, a lambda expression has access to
10172 the private variables of enclosing classes. */
10173
10174 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10175
10176 if (ok && cp_parser_error_occurred (parser))
10177 ok = false;
10178
10179 if (ok)
10180 {
10181 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10182 && cp_parser_start_tentative_firewall (parser))
10183 start = token;
10184 cp_parser_lambda_body (parser, lambda_expr);
10185 }
10186 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10187 {
10188 if (cp_parser_skip_to_closing_brace (parser))
10189 cp_lexer_consume_token (parser->lexer);
10190 }
10191
10192 /* The capture list was built up in reverse order; fix that now. */
10193 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10194 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10195
10196 if (ok)
10197 maybe_add_lambda_conv_op (type);
10198
10199 type = finish_struct (type, /*attributes=*/NULL_TREE);
10200
10201 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10202 parser->in_statement = in_statement;
10203 parser->in_switch_statement_p = in_switch_statement_p;
10204 parser->fully_implicit_function_template_p
10205 = fully_implicit_function_template_p;
10206 parser->implicit_template_parms = implicit_template_parms;
10207 parser->implicit_template_scope = implicit_template_scope;
10208 parser->auto_is_implicit_function_template_parm_p
10209 = auto_is_implicit_function_template_parm_p;
10210 }
10211
10212 /* This field is only used during parsing of the lambda. */
10213 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10214
10215 /* This lambda shouldn't have any proxies left at this point. */
10216 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10217 /* And now that we're done, push proxies for an enclosing lambda. */
10218 insert_pending_capture_proxies ();
10219
10220 if (ok)
10221 lambda_expr = build_lambda_object (lambda_expr);
10222 else
10223 lambda_expr = error_mark_node;
10224
10225 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10226
10227 pop_deferring_access_checks ();
10228
10229 return lambda_expr;
10230 }
10231
10232 /* Parse the beginning of a lambda expression.
10233
10234 lambda-introducer:
10235 [ lambda-capture [opt] ]
10236
10237 LAMBDA_EXPR is the current representation of the lambda expression. */
10238
10239 static void
10240 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10241 {
10242 /* Need commas after the first capture. */
10243 bool first = true;
10244
10245 /* Eat the leading `['. */
10246 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10247
10248 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10249 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10250 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10251 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10252 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10254
10255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10256 {
10257 cp_lexer_consume_token (parser->lexer);
10258 first = false;
10259 }
10260
10261 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10262 {
10263 cp_token* capture_token;
10264 tree capture_id;
10265 tree capture_init_expr;
10266 cp_id_kind idk = CP_ID_KIND_NONE;
10267 bool explicit_init_p = false;
10268
10269 enum capture_kind_type
10270 {
10271 BY_COPY,
10272 BY_REFERENCE
10273 };
10274 enum capture_kind_type capture_kind = BY_COPY;
10275
10276 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10277 {
10278 error ("expected end of capture-list");
10279 return;
10280 }
10281
10282 if (first)
10283 first = false;
10284 else
10285 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10286
10287 /* Possibly capture `this'. */
10288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10289 {
10290 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10291 if (cxx_dialect < cxx2a
10292 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10293 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10294 "with by-copy capture default");
10295 cp_lexer_consume_token (parser->lexer);
10296 add_capture (lambda_expr,
10297 /*id=*/this_identifier,
10298 /*initializer=*/finish_this_expr (),
10299 /*by_reference_p=*/true,
10300 explicit_init_p);
10301 continue;
10302 }
10303
10304 /* Possibly capture `*this'. */
10305 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10306 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10307 {
10308 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10309 if (cxx_dialect < cxx17)
10310 pedwarn (loc, 0, "%<*this%> capture only available with "
10311 "-std=c++17 or -std=gnu++17");
10312 cp_lexer_consume_token (parser->lexer);
10313 cp_lexer_consume_token (parser->lexer);
10314 add_capture (lambda_expr,
10315 /*id=*/this_identifier,
10316 /*initializer=*/finish_this_expr (),
10317 /*by_reference_p=*/false,
10318 explicit_init_p);
10319 continue;
10320 }
10321
10322 /* Remember whether we want to capture as a reference or not. */
10323 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10324 {
10325 capture_kind = BY_REFERENCE;
10326 cp_lexer_consume_token (parser->lexer);
10327 }
10328
10329 /* Get the identifier. */
10330 capture_token = cp_lexer_peek_token (parser->lexer);
10331 capture_id = cp_parser_identifier (parser);
10332
10333 if (capture_id == error_mark_node)
10334 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10335 delimiters, but I modified this to stop on unnested ']' as well. It
10336 was already changed to stop on unnested '}', so the
10337 "closing_parenthesis" name is no more misleading with my change. */
10338 {
10339 cp_parser_skip_to_closing_parenthesis (parser,
10340 /*recovering=*/true,
10341 /*or_comma=*/true,
10342 /*consume_paren=*/true);
10343 break;
10344 }
10345
10346 /* Find the initializer for this capture. */
10347 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10348 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10349 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10350 {
10351 bool direct, non_constant;
10352 /* An explicit initializer exists. */
10353 if (cxx_dialect < cxx14)
10354 pedwarn (input_location, 0,
10355 "lambda capture initializers "
10356 "only available with -std=c++14 or -std=gnu++14");
10357 capture_init_expr = cp_parser_initializer (parser, &direct,
10358 &non_constant);
10359 explicit_init_p = true;
10360 if (capture_init_expr == NULL_TREE)
10361 {
10362 error ("empty initializer for lambda init-capture");
10363 capture_init_expr = error_mark_node;
10364 }
10365 }
10366 else
10367 {
10368 const char* error_msg;
10369
10370 /* Turn the identifier into an id-expression. */
10371 capture_init_expr
10372 = cp_parser_lookup_name_simple (parser, capture_id,
10373 capture_token->location);
10374
10375 if (capture_init_expr == error_mark_node)
10376 {
10377 unqualified_name_lookup_error (capture_id);
10378 continue;
10379 }
10380 else if (DECL_P (capture_init_expr)
10381 && (!VAR_P (capture_init_expr)
10382 && TREE_CODE (capture_init_expr) != PARM_DECL))
10383 {
10384 error_at (capture_token->location,
10385 "capture of non-variable %qD ",
10386 capture_init_expr);
10387 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10388 "%q#D declared here", capture_init_expr);
10389 continue;
10390 }
10391 if (VAR_P (capture_init_expr)
10392 && decl_storage_duration (capture_init_expr) != dk_auto)
10393 {
10394 if (pedwarn (capture_token->location, 0, "capture of variable "
10395 "%qD with non-automatic storage duration",
10396 capture_init_expr))
10397 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10398 "%q#D declared here", capture_init_expr);
10399 continue;
10400 }
10401
10402 capture_init_expr
10403 = finish_id_expression
10404 (capture_id,
10405 capture_init_expr,
10406 parser->scope,
10407 &idk,
10408 /*integral_constant_expression_p=*/false,
10409 /*allow_non_integral_constant_expression_p=*/false,
10410 /*non_integral_constant_expression_p=*/NULL,
10411 /*template_p=*/false,
10412 /*done=*/true,
10413 /*address_p=*/false,
10414 /*template_arg_p=*/false,
10415 &error_msg,
10416 capture_token->location);
10417
10418 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10419 {
10420 cp_lexer_consume_token (parser->lexer);
10421 capture_init_expr = make_pack_expansion (capture_init_expr);
10422 }
10423 }
10424
10425 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10426 && !explicit_init_p)
10427 {
10428 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10429 && capture_kind == BY_COPY)
10430 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10431 "of %qD redundant with by-copy capture default",
10432 capture_id);
10433 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10434 && capture_kind == BY_REFERENCE)
10435 pedwarn (capture_token->location, 0, "explicit by-reference "
10436 "capture of %qD redundant with by-reference capture "
10437 "default", capture_id);
10438 }
10439
10440 add_capture (lambda_expr,
10441 capture_id,
10442 capture_init_expr,
10443 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10444 explicit_init_p);
10445
10446 /* If there is any qualification still in effect, clear it
10447 now; we will be starting fresh with the next capture. */
10448 parser->scope = NULL_TREE;
10449 parser->qualifying_scope = NULL_TREE;
10450 parser->object_scope = NULL_TREE;
10451 }
10452
10453 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10454 }
10455
10456 /* Parse the (optional) middle of a lambda expression.
10457
10458 lambda-declarator:
10459 < template-parameter-list [opt] >
10460 ( parameter-declaration-clause [opt] )
10461 attribute-specifier [opt]
10462 decl-specifier-seq [opt]
10463 exception-specification [opt]
10464 lambda-return-type-clause [opt]
10465
10466 LAMBDA_EXPR is the current representation of the lambda expression. */
10467
10468 static bool
10469 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10470 {
10471 /* 5.1.1.4 of the standard says:
10472 If a lambda-expression does not include a lambda-declarator, it is as if
10473 the lambda-declarator were ().
10474 This means an empty parameter list, no attributes, and no exception
10475 specification. */
10476 tree param_list = void_list_node;
10477 tree attributes = NULL_TREE;
10478 tree exception_spec = NULL_TREE;
10479 tree template_param_list = NULL_TREE;
10480 tree tx_qual = NULL_TREE;
10481 tree return_type = NULL_TREE;
10482 cp_decl_specifier_seq lambda_specs;
10483 clear_decl_specs (&lambda_specs);
10484
10485 /* The template-parameter-list is optional, but must begin with
10486 an opening angle if present. */
10487 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10488 {
10489 if (cxx_dialect < cxx14)
10490 pedwarn (parser->lexer->next_token->location, 0,
10491 "lambda templates are only available with "
10492 "-std=c++14 or -std=gnu++14");
10493 else if (cxx_dialect < cxx2a)
10494 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10495 "lambda templates are only available with "
10496 "-std=c++2a or -std=gnu++2a");
10497
10498 cp_lexer_consume_token (parser->lexer);
10499
10500 template_param_list = cp_parser_template_parameter_list (parser);
10501
10502 cp_parser_skip_to_end_of_template_parameter_list (parser);
10503
10504 /* We just processed one more parameter list. */
10505 ++parser->num_template_parameter_lists;
10506 }
10507
10508 /* The parameter-declaration-clause is optional (unless
10509 template-parameter-list was given), but must begin with an
10510 opening parenthesis if present. */
10511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10512 {
10513 matching_parens parens;
10514 parens.consume_open (parser);
10515
10516 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10517
10518 /* Parse parameters. */
10519 param_list = cp_parser_parameter_declaration_clause (parser);
10520
10521 /* Default arguments shall not be specified in the
10522 parameter-declaration-clause of a lambda-declarator. */
10523 if (cxx_dialect < cxx14)
10524 for (tree t = param_list; t; t = TREE_CHAIN (t))
10525 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10526 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10527 "default argument specified for lambda parameter");
10528
10529 parens.require_close (parser);
10530
10531 attributes = cp_parser_attributes_opt (parser);
10532
10533 /* In the decl-specifier-seq of the lambda-declarator, each
10534 decl-specifier shall either be mutable or constexpr. */
10535 int declares_class_or_enum;
10536 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10537 cp_parser_decl_specifier_seq (parser,
10538 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10539 &lambda_specs, &declares_class_or_enum);
10540 if (lambda_specs.storage_class == sc_mutable)
10541 {
10542 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10543 if (lambda_specs.conflicting_specifiers_p)
10544 error_at (lambda_specs.locations[ds_storage_class],
10545 "duplicate %<mutable%>");
10546 }
10547
10548 tx_qual = cp_parser_tx_qualifier_opt (parser);
10549
10550 /* Parse optional exception specification. */
10551 exception_spec = cp_parser_exception_specification_opt (parser);
10552
10553 /* Parse optional trailing return type. */
10554 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10555 {
10556 cp_lexer_consume_token (parser->lexer);
10557 return_type = cp_parser_trailing_type_id (parser);
10558 }
10559
10560 /* The function parameters must be in scope all the way until after the
10561 trailing-return-type in case of decltype. */
10562 pop_bindings_and_leave_scope ();
10563 }
10564 else if (template_param_list != NULL_TREE) // generate diagnostic
10565 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10566
10567 /* Create the function call operator.
10568
10569 Messing with declarators like this is no uglier than building up the
10570 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10571 other code. */
10572 {
10573 cp_decl_specifier_seq return_type_specs;
10574 cp_declarator* declarator;
10575 tree fco;
10576 int quals;
10577 void *p;
10578
10579 clear_decl_specs (&return_type_specs);
10580 if (return_type)
10581 return_type_specs.type = return_type;
10582 else
10583 /* Maybe we will deduce the return type later. */
10584 return_type_specs.type = make_auto ();
10585
10586 if (lambda_specs.locations[ds_constexpr])
10587 {
10588 if (cxx_dialect >= cxx17)
10589 return_type_specs.locations[ds_constexpr]
10590 = lambda_specs.locations[ds_constexpr];
10591 else
10592 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10593 "lambda only available with -std=c++17 or -std=gnu++17");
10594 }
10595
10596 p = obstack_alloc (&declarator_obstack, 0);
10597
10598 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10599
10600 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10601 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10602 declarator = make_call_declarator (declarator, param_list, quals,
10603 VIRT_SPEC_UNSPECIFIED,
10604 REF_QUAL_NONE,
10605 tx_qual,
10606 exception_spec,
10607 /*late_return_type=*/NULL_TREE,
10608 /*requires_clause*/NULL_TREE);
10609 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10610
10611 fco = grokmethod (&return_type_specs,
10612 declarator,
10613 attributes);
10614 if (fco != error_mark_node)
10615 {
10616 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10617 DECL_ARTIFICIAL (fco) = 1;
10618 /* Give the object parameter a different name. */
10619 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10620 if (return_type)
10621 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10622 }
10623 if (template_param_list)
10624 {
10625 fco = finish_member_template_decl (fco);
10626 finish_template_decl (template_param_list);
10627 --parser->num_template_parameter_lists;
10628 }
10629 else if (parser->fully_implicit_function_template_p)
10630 fco = finish_fully_implicit_template (parser, fco);
10631
10632 finish_member_declaration (fco);
10633
10634 obstack_free (&declarator_obstack, p);
10635
10636 return (fco != error_mark_node);
10637 }
10638 }
10639
10640 /* Parse the body of a lambda expression, which is simply
10641
10642 compound-statement
10643
10644 but which requires special handling.
10645 LAMBDA_EXPR is the current representation of the lambda expression. */
10646
10647 static void
10648 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10649 {
10650 bool nested = (current_function_decl != NULL_TREE);
10651 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10652 bool in_function_body = parser->in_function_body;
10653
10654 if (nested)
10655 push_function_context ();
10656 else
10657 /* Still increment function_depth so that we don't GC in the
10658 middle of an expression. */
10659 ++function_depth;
10660
10661 vec<tree> omp_privatization_save;
10662 save_omp_privatization_clauses (omp_privatization_save);
10663 /* Clear this in case we're in the middle of a default argument. */
10664 parser->local_variables_forbidden_p = false;
10665 parser->in_function_body = true;
10666
10667 {
10668 local_specialization_stack s (lss_copy);
10669 tree fco = lambda_function (lambda_expr);
10670 tree body = start_lambda_function (fco, lambda_expr);
10671 matching_braces braces;
10672
10673 if (braces.require_open (parser))
10674 {
10675 tree compound_stmt = begin_compound_stmt (0);
10676
10677 /* Originally C++11 required us to peek for 'return expr'; and
10678 process it specially here to deduce the return type. N3638
10679 removed the need for that. */
10680
10681 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10682 cp_parser_label_declaration (parser);
10683 cp_parser_statement_seq_opt (parser, NULL_TREE);
10684 braces.require_close (parser);
10685
10686 finish_compound_stmt (compound_stmt);
10687 }
10688
10689 finish_lambda_function (body);
10690 }
10691
10692 restore_omp_privatization_clauses (omp_privatization_save);
10693 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10694 parser->in_function_body = in_function_body;
10695 if (nested)
10696 pop_function_context();
10697 else
10698 --function_depth;
10699 }
10700
10701 /* Statements [gram.stmt.stmt] */
10702
10703 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10704
10705 static void
10706 add_debug_begin_stmt (location_t loc)
10707 {
10708 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10709 return;
10710 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10711 /* A concept is never expanded normally. */
10712 return;
10713
10714 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10715 SET_EXPR_LOCATION (stmt, loc);
10716 add_stmt (stmt);
10717 }
10718
10719 /* Parse a statement.
10720
10721 statement:
10722 labeled-statement
10723 expression-statement
10724 compound-statement
10725 selection-statement
10726 iteration-statement
10727 jump-statement
10728 declaration-statement
10729 try-block
10730
10731 C++11:
10732
10733 statement:
10734 labeled-statement
10735 attribute-specifier-seq (opt) expression-statement
10736 attribute-specifier-seq (opt) compound-statement
10737 attribute-specifier-seq (opt) selection-statement
10738 attribute-specifier-seq (opt) iteration-statement
10739 attribute-specifier-seq (opt) jump-statement
10740 declaration-statement
10741 attribute-specifier-seq (opt) try-block
10742
10743 init-statement:
10744 expression-statement
10745 simple-declaration
10746
10747 TM Extension:
10748
10749 statement:
10750 atomic-statement
10751
10752 IN_COMPOUND is true when the statement is nested inside a
10753 cp_parser_compound_statement; this matters for certain pragmas.
10754
10755 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10756 is a (possibly labeled) if statement which is not enclosed in braces
10757 and has an else clause. This is used to implement -Wparentheses.
10758
10759 CHAIN is a vector of if-else-if conditions. */
10760
10761 static void
10762 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10763 bool in_compound, bool *if_p, vec<tree> *chain,
10764 location_t *loc_after_labels)
10765 {
10766 tree statement, std_attrs = NULL_TREE;
10767 cp_token *token;
10768 location_t statement_location, attrs_location;
10769
10770 restart:
10771 if (if_p != NULL)
10772 *if_p = false;
10773 /* There is no statement yet. */
10774 statement = NULL_TREE;
10775
10776 saved_token_sentinel saved_tokens (parser->lexer);
10777 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10778 if (c_dialect_objc ())
10779 /* In obj-c++, seeing '[[' might be the either the beginning of
10780 c++11 attributes, or a nested objc-message-expression. So
10781 let's parse the c++11 attributes tentatively. */
10782 cp_parser_parse_tentatively (parser);
10783 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10784 if (c_dialect_objc ())
10785 {
10786 if (!cp_parser_parse_definitely (parser))
10787 std_attrs = NULL_TREE;
10788 }
10789
10790 /* Peek at the next token. */
10791 token = cp_lexer_peek_token (parser->lexer);
10792 /* Remember the location of the first token in the statement. */
10793 statement_location = token->location;
10794 add_debug_begin_stmt (statement_location);
10795 /* If this is a keyword, then that will often determine what kind of
10796 statement we have. */
10797 if (token->type == CPP_KEYWORD)
10798 {
10799 enum rid keyword = token->keyword;
10800
10801 switch (keyword)
10802 {
10803 case RID_CASE:
10804 case RID_DEFAULT:
10805 /* Looks like a labeled-statement with a case label.
10806 Parse the label, and then use tail recursion to parse
10807 the statement. */
10808 cp_parser_label_for_labeled_statement (parser, std_attrs);
10809 in_compound = false;
10810 goto restart;
10811
10812 case RID_IF:
10813 case RID_SWITCH:
10814 statement = cp_parser_selection_statement (parser, if_p, chain);
10815 break;
10816
10817 case RID_WHILE:
10818 case RID_DO:
10819 case RID_FOR:
10820 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10821 break;
10822
10823 case RID_BREAK:
10824 case RID_CONTINUE:
10825 case RID_RETURN:
10826 case RID_GOTO:
10827 statement = cp_parser_jump_statement (parser);
10828 break;
10829
10830 /* Objective-C++ exception-handling constructs. */
10831 case RID_AT_TRY:
10832 case RID_AT_CATCH:
10833 case RID_AT_FINALLY:
10834 case RID_AT_SYNCHRONIZED:
10835 case RID_AT_THROW:
10836 statement = cp_parser_objc_statement (parser);
10837 break;
10838
10839 case RID_TRY:
10840 statement = cp_parser_try_block (parser);
10841 break;
10842
10843 case RID_NAMESPACE:
10844 /* This must be a namespace alias definition. */
10845 cp_parser_declaration_statement (parser);
10846 return;
10847
10848 case RID_TRANSACTION_ATOMIC:
10849 case RID_TRANSACTION_RELAXED:
10850 case RID_SYNCHRONIZED:
10851 case RID_ATOMIC_NOEXCEPT:
10852 case RID_ATOMIC_CANCEL:
10853 statement = cp_parser_transaction (parser, token);
10854 break;
10855 case RID_TRANSACTION_CANCEL:
10856 statement = cp_parser_transaction_cancel (parser);
10857 break;
10858
10859 default:
10860 /* It might be a keyword like `int' that can start a
10861 declaration-statement. */
10862 break;
10863 }
10864 }
10865 else if (token->type == CPP_NAME)
10866 {
10867 /* If the next token is a `:', then we are looking at a
10868 labeled-statement. */
10869 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10870 if (token->type == CPP_COLON)
10871 {
10872 /* Looks like a labeled-statement with an ordinary label.
10873 Parse the label, and then use tail recursion to parse
10874 the statement. */
10875
10876 cp_parser_label_for_labeled_statement (parser, std_attrs);
10877 in_compound = false;
10878 goto restart;
10879 }
10880 }
10881 /* Anything that starts with a `{' must be a compound-statement. */
10882 else if (token->type == CPP_OPEN_BRACE)
10883 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10884 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10885 a statement all its own. */
10886 else if (token->type == CPP_PRAGMA)
10887 {
10888 /* Only certain OpenMP pragmas are attached to statements, and thus
10889 are considered statements themselves. All others are not. In
10890 the context of a compound, accept the pragma as a "statement" and
10891 return so that we can check for a close brace. Otherwise we
10892 require a real statement and must go back and read one. */
10893 if (in_compound)
10894 cp_parser_pragma (parser, pragma_compound, if_p);
10895 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10896 goto restart;
10897 return;
10898 }
10899 else if (token->type == CPP_EOF)
10900 {
10901 cp_parser_error (parser, "expected statement");
10902 return;
10903 }
10904
10905 /* Everything else must be a declaration-statement or an
10906 expression-statement. Try for the declaration-statement
10907 first, unless we are looking at a `;', in which case we know that
10908 we have an expression-statement. */
10909 if (!statement)
10910 {
10911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10912 {
10913 if (std_attrs != NULL_TREE)
10914 {
10915 /* Attributes should be parsed as part of the the
10916 declaration, so let's un-parse them. */
10917 saved_tokens.rollback();
10918 std_attrs = NULL_TREE;
10919 }
10920
10921 cp_parser_parse_tentatively (parser);
10922 /* Try to parse the declaration-statement. */
10923 cp_parser_declaration_statement (parser);
10924 /* If that worked, we're done. */
10925 if (cp_parser_parse_definitely (parser))
10926 return;
10927 }
10928 /* All preceding labels have been parsed at this point. */
10929 if (loc_after_labels != NULL)
10930 *loc_after_labels = statement_location;
10931
10932 /* Look for an expression-statement instead. */
10933 statement = cp_parser_expression_statement (parser, in_statement_expr);
10934
10935 /* Handle [[fallthrough]];. */
10936 if (attribute_fallthrough_p (std_attrs))
10937 {
10938 /* The next token after the fallthrough attribute is ';'. */
10939 if (statement == NULL_TREE)
10940 {
10941 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10942 statement = build_call_expr_internal_loc (statement_location,
10943 IFN_FALLTHROUGH,
10944 void_type_node, 0);
10945 finish_expr_stmt (statement);
10946 }
10947 else
10948 warning_at (statement_location, OPT_Wattributes,
10949 "%<fallthrough%> attribute not followed by %<;%>");
10950 std_attrs = NULL_TREE;
10951 }
10952 }
10953
10954 /* Set the line number for the statement. */
10955 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10956 SET_EXPR_LOCATION (statement, statement_location);
10957
10958 /* Allow "[[fallthrough]];", but warn otherwise. */
10959 if (std_attrs != NULL_TREE)
10960 warning_at (attrs_location,
10961 OPT_Wattributes,
10962 "attributes at the beginning of statement are ignored");
10963 }
10964
10965 /* Append ATTR to attribute list ATTRS. */
10966
10967 static tree
10968 attr_chainon (tree attrs, tree attr)
10969 {
10970 if (attrs == error_mark_node)
10971 return error_mark_node;
10972 if (attr == error_mark_node)
10973 return error_mark_node;
10974 return chainon (attrs, attr);
10975 }
10976
10977 /* Parse the label for a labeled-statement, i.e.
10978
10979 identifier :
10980 case constant-expression :
10981 default :
10982
10983 GNU Extension:
10984 case constant-expression ... constant-expression : statement
10985
10986 When a label is parsed without errors, the label is added to the
10987 parse tree by the finish_* functions, so this function doesn't
10988 have to return the label. */
10989
10990 static void
10991 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10992 {
10993 cp_token *token;
10994 tree label = NULL_TREE;
10995 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10996
10997 /* The next token should be an identifier. */
10998 token = cp_lexer_peek_token (parser->lexer);
10999 if (token->type != CPP_NAME
11000 && token->type != CPP_KEYWORD)
11001 {
11002 cp_parser_error (parser, "expected labeled-statement");
11003 return;
11004 }
11005
11006 /* Remember whether this case or a user-defined label is allowed to fall
11007 through to. */
11008 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11009
11010 parser->colon_corrects_to_scope_p = false;
11011 switch (token->keyword)
11012 {
11013 case RID_CASE:
11014 {
11015 tree expr, expr_hi;
11016 cp_token *ellipsis;
11017
11018 /* Consume the `case' token. */
11019 cp_lexer_consume_token (parser->lexer);
11020 /* Parse the constant-expression. */
11021 expr = cp_parser_constant_expression (parser);
11022 if (check_for_bare_parameter_packs (expr))
11023 expr = error_mark_node;
11024
11025 ellipsis = cp_lexer_peek_token (parser->lexer);
11026 if (ellipsis->type == CPP_ELLIPSIS)
11027 {
11028 /* Consume the `...' token. */
11029 cp_lexer_consume_token (parser->lexer);
11030 expr_hi = cp_parser_constant_expression (parser);
11031 if (check_for_bare_parameter_packs (expr_hi))
11032 expr_hi = error_mark_node;
11033
11034 /* We don't need to emit warnings here, as the common code
11035 will do this for us. */
11036 }
11037 else
11038 expr_hi = NULL_TREE;
11039
11040 if (parser->in_switch_statement_p)
11041 {
11042 tree l = finish_case_label (token->location, expr, expr_hi);
11043 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11044 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11045 }
11046 else
11047 error_at (token->location,
11048 "case label %qE not within a switch statement",
11049 expr);
11050 }
11051 break;
11052
11053 case RID_DEFAULT:
11054 /* Consume the `default' token. */
11055 cp_lexer_consume_token (parser->lexer);
11056
11057 if (parser->in_switch_statement_p)
11058 {
11059 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11060 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11061 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11062 }
11063 else
11064 error_at (token->location, "case label not within a switch statement");
11065 break;
11066
11067 default:
11068 /* Anything else must be an ordinary label. */
11069 label = finish_label_stmt (cp_parser_identifier (parser));
11070 if (label && TREE_CODE (label) == LABEL_DECL)
11071 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11072 break;
11073 }
11074
11075 /* Require the `:' token. */
11076 cp_parser_require (parser, CPP_COLON, RT_COLON);
11077
11078 /* An ordinary label may optionally be followed by attributes.
11079 However, this is only permitted if the attributes are then
11080 followed by a semicolon. This is because, for backward
11081 compatibility, when parsing
11082 lab: __attribute__ ((unused)) int i;
11083 we want the attribute to attach to "i", not "lab". */
11084 if (label != NULL_TREE
11085 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11086 {
11087 tree attrs;
11088 cp_parser_parse_tentatively (parser);
11089 attrs = cp_parser_gnu_attributes_opt (parser);
11090 if (attrs == NULL_TREE
11091 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11092 cp_parser_abort_tentative_parse (parser);
11093 else if (!cp_parser_parse_definitely (parser))
11094 ;
11095 else
11096 attributes = attr_chainon (attributes, attrs);
11097 }
11098
11099 if (attributes != NULL_TREE)
11100 cplus_decl_attributes (&label, attributes, 0);
11101
11102 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11103 }
11104
11105 /* Parse an expression-statement.
11106
11107 expression-statement:
11108 expression [opt] ;
11109
11110 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11111 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11112 indicates whether this expression-statement is part of an
11113 expression statement. */
11114
11115 static tree
11116 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11117 {
11118 tree statement = NULL_TREE;
11119 cp_token *token = cp_lexer_peek_token (parser->lexer);
11120 location_t loc = token->location;
11121
11122 /* There might be attribute fallthrough. */
11123 tree attr = cp_parser_gnu_attributes_opt (parser);
11124
11125 /* If the next token is a ';', then there is no expression
11126 statement. */
11127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11128 {
11129 statement = cp_parser_expression (parser);
11130 if (statement == error_mark_node
11131 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11132 {
11133 cp_parser_skip_to_end_of_block_or_statement (parser);
11134 return error_mark_node;
11135 }
11136 }
11137
11138 /* Handle [[fallthrough]];. */
11139 if (attribute_fallthrough_p (attr))
11140 {
11141 /* The next token after the fallthrough attribute is ';'. */
11142 if (statement == NULL_TREE)
11143 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11144 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11145 void_type_node, 0);
11146 else
11147 warning_at (loc, OPT_Wattributes,
11148 "%<fallthrough%> attribute not followed by %<;%>");
11149 attr = NULL_TREE;
11150 }
11151
11152 /* Allow "[[fallthrough]];", but warn otherwise. */
11153 if (attr != NULL_TREE)
11154 warning_at (loc, OPT_Wattributes,
11155 "attributes at the beginning of statement are ignored");
11156
11157 /* Give a helpful message for "A<T>::type t;" and the like. */
11158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11159 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11160 {
11161 if (TREE_CODE (statement) == SCOPE_REF)
11162 error_at (token->location, "need %<typename%> before %qE because "
11163 "%qT is a dependent scope",
11164 statement, TREE_OPERAND (statement, 0));
11165 else if (is_overloaded_fn (statement)
11166 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11167 {
11168 /* A::A a; */
11169 tree fn = get_first_fn (statement);
11170 error_at (token->location,
11171 "%<%T::%D%> names the constructor, not the type",
11172 DECL_CONTEXT (fn), DECL_NAME (fn));
11173 }
11174 }
11175
11176 /* Consume the final `;'. */
11177 cp_parser_consume_semicolon_at_end_of_statement (parser);
11178
11179 if (in_statement_expr
11180 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11181 /* This is the final expression statement of a statement
11182 expression. */
11183 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11184 else if (statement)
11185 statement = finish_expr_stmt (statement);
11186
11187 return statement;
11188 }
11189
11190 /* Parse a compound-statement.
11191
11192 compound-statement:
11193 { statement-seq [opt] }
11194
11195 GNU extension:
11196
11197 compound-statement:
11198 { label-declaration-seq [opt] statement-seq [opt] }
11199
11200 label-declaration-seq:
11201 label-declaration
11202 label-declaration-seq label-declaration
11203
11204 Returns a tree representing the statement. */
11205
11206 static tree
11207 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11208 int bcs_flags, bool function_body)
11209 {
11210 tree compound_stmt;
11211 matching_braces braces;
11212
11213 /* Consume the `{'. */
11214 if (!braces.require_open (parser))
11215 return error_mark_node;
11216 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11217 && !function_body && cxx_dialect < cxx14)
11218 pedwarn (input_location, OPT_Wpedantic,
11219 "compound-statement in %<constexpr%> function");
11220 /* Begin the compound-statement. */
11221 compound_stmt = begin_compound_stmt (bcs_flags);
11222 /* If the next keyword is `__label__' we have a label declaration. */
11223 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11224 cp_parser_label_declaration (parser);
11225 /* Parse an (optional) statement-seq. */
11226 cp_parser_statement_seq_opt (parser, in_statement_expr);
11227 /* Finish the compound-statement. */
11228 finish_compound_stmt (compound_stmt);
11229 /* Consume the `}'. */
11230 braces.require_close (parser);
11231
11232 return compound_stmt;
11233 }
11234
11235 /* Parse an (optional) statement-seq.
11236
11237 statement-seq:
11238 statement
11239 statement-seq [opt] statement */
11240
11241 static void
11242 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11243 {
11244 /* Scan statements until there aren't any more. */
11245 while (true)
11246 {
11247 cp_token *token = cp_lexer_peek_token (parser->lexer);
11248
11249 /* If we are looking at a `}', then we have run out of
11250 statements; the same is true if we have reached the end
11251 of file, or have stumbled upon a stray '@end'. */
11252 if (token->type == CPP_CLOSE_BRACE
11253 || token->type == CPP_EOF
11254 || token->type == CPP_PRAGMA_EOL
11255 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11256 break;
11257
11258 /* If we are in a compound statement and find 'else' then
11259 something went wrong. */
11260 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11261 {
11262 if (parser->in_statement & IN_IF_STMT)
11263 break;
11264 else
11265 {
11266 token = cp_lexer_consume_token (parser->lexer);
11267 error_at (token->location, "%<else%> without a previous %<if%>");
11268 }
11269 }
11270
11271 /* Parse the statement. */
11272 cp_parser_statement (parser, in_statement_expr, true, NULL);
11273 }
11274 }
11275
11276 /* Return true if we're looking at (init; cond), false otherwise. */
11277
11278 static bool
11279 cp_parser_init_statement_p (cp_parser *parser)
11280 {
11281 /* Save tokens so that we can put them back. */
11282 cp_lexer_save_tokens (parser->lexer);
11283
11284 /* Look for ';' that is not nested in () or {}. */
11285 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11286 /*recovering=*/false,
11287 CPP_SEMICOLON,
11288 /*consume_paren=*/false);
11289
11290 /* Roll back the tokens we skipped. */
11291 cp_lexer_rollback_tokens (parser->lexer);
11292
11293 return ret == -1;
11294 }
11295
11296 /* Parse a selection-statement.
11297
11298 selection-statement:
11299 if ( init-statement [opt] condition ) statement
11300 if ( init-statement [opt] condition ) statement else statement
11301 switch ( init-statement [opt] condition ) statement
11302
11303 Returns the new IF_STMT or SWITCH_STMT.
11304
11305 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11306 is a (possibly labeled) if statement which is not enclosed in
11307 braces and has an else clause. This is used to implement
11308 -Wparentheses.
11309
11310 CHAIN is a vector of if-else-if conditions. This is used to implement
11311 -Wduplicated-cond. */
11312
11313 static tree
11314 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11315 vec<tree> *chain)
11316 {
11317 cp_token *token;
11318 enum rid keyword;
11319 token_indent_info guard_tinfo;
11320
11321 if (if_p != NULL)
11322 *if_p = false;
11323
11324 /* Peek at the next token. */
11325 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11326 guard_tinfo = get_token_indent_info (token);
11327
11328 /* See what kind of keyword it is. */
11329 keyword = token->keyword;
11330 switch (keyword)
11331 {
11332 case RID_IF:
11333 case RID_SWITCH:
11334 {
11335 tree statement;
11336 tree condition;
11337
11338 bool cx = false;
11339 if (keyword == RID_IF
11340 && cp_lexer_next_token_is_keyword (parser->lexer,
11341 RID_CONSTEXPR))
11342 {
11343 cx = true;
11344 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11345 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11346 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11347 "with -std=c++17 or -std=gnu++17");
11348 }
11349
11350 /* Look for the `('. */
11351 matching_parens parens;
11352 if (!parens.require_open (parser))
11353 {
11354 cp_parser_skip_to_end_of_statement (parser);
11355 return error_mark_node;
11356 }
11357
11358 /* Begin the selection-statement. */
11359 if (keyword == RID_IF)
11360 {
11361 statement = begin_if_stmt ();
11362 IF_STMT_CONSTEXPR_P (statement) = cx;
11363 }
11364 else
11365 statement = begin_switch_stmt ();
11366
11367 /* Parse the optional init-statement. */
11368 if (cp_parser_init_statement_p (parser))
11369 {
11370 tree decl;
11371 if (cxx_dialect < cxx17)
11372 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11373 "init-statement in selection statements only available "
11374 "with -std=c++17 or -std=gnu++17");
11375 cp_parser_init_statement (parser, &decl);
11376 }
11377
11378 /* Parse the condition. */
11379 condition = cp_parser_condition (parser);
11380 /* Look for the `)'. */
11381 if (!parens.require_close (parser))
11382 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11383 /*consume_paren=*/true);
11384
11385 if (keyword == RID_IF)
11386 {
11387 bool nested_if;
11388 unsigned char in_statement;
11389
11390 /* Add the condition. */
11391 condition = finish_if_stmt_cond (condition, statement);
11392
11393 if (warn_duplicated_cond)
11394 warn_duplicated_cond_add_or_warn (token->location, condition,
11395 &chain);
11396
11397 /* Parse the then-clause. */
11398 in_statement = parser->in_statement;
11399 parser->in_statement |= IN_IF_STMT;
11400
11401 /* Outside a template, the non-selected branch of a constexpr
11402 if is a 'discarded statement', i.e. unevaluated. */
11403 bool was_discarded = in_discarded_stmt;
11404 bool discard_then = (cx && !processing_template_decl
11405 && integer_zerop (condition));
11406 if (discard_then)
11407 {
11408 in_discarded_stmt = true;
11409 ++c_inhibit_evaluation_warnings;
11410 }
11411
11412 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11413 guard_tinfo);
11414
11415 parser->in_statement = in_statement;
11416
11417 finish_then_clause (statement);
11418
11419 if (discard_then)
11420 {
11421 THEN_CLAUSE (statement) = NULL_TREE;
11422 in_discarded_stmt = was_discarded;
11423 --c_inhibit_evaluation_warnings;
11424 }
11425
11426 /* If the next token is `else', parse the else-clause. */
11427 if (cp_lexer_next_token_is_keyword (parser->lexer,
11428 RID_ELSE))
11429 {
11430 bool discard_else = (cx && !processing_template_decl
11431 && integer_nonzerop (condition));
11432 if (discard_else)
11433 {
11434 in_discarded_stmt = true;
11435 ++c_inhibit_evaluation_warnings;
11436 }
11437
11438 guard_tinfo
11439 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11440 /* Consume the `else' keyword. */
11441 cp_lexer_consume_token (parser->lexer);
11442 if (warn_duplicated_cond)
11443 {
11444 if (cp_lexer_next_token_is_keyword (parser->lexer,
11445 RID_IF)
11446 && chain == NULL)
11447 {
11448 /* We've got "if (COND) else if (COND2)". Start
11449 the condition chain and add COND as the first
11450 element. */
11451 chain = new vec<tree> ();
11452 if (!CONSTANT_CLASS_P (condition)
11453 && !TREE_SIDE_EFFECTS (condition))
11454 {
11455 /* Wrap it in a NOP_EXPR so that we can set the
11456 location of the condition. */
11457 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11458 condition);
11459 SET_EXPR_LOCATION (e, token->location);
11460 chain->safe_push (e);
11461 }
11462 }
11463 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11464 RID_IF))
11465 {
11466 /* This is if-else without subsequent if. Zap the
11467 condition chain; we would have already warned at
11468 this point. */
11469 delete chain;
11470 chain = NULL;
11471 }
11472 }
11473 begin_else_clause (statement);
11474 /* Parse the else-clause. */
11475 cp_parser_implicitly_scoped_statement (parser, NULL,
11476 guard_tinfo, chain);
11477
11478 finish_else_clause (statement);
11479
11480 /* If we are currently parsing a then-clause, then
11481 IF_P will not be NULL. We set it to true to
11482 indicate that this if statement has an else clause.
11483 This may trigger the Wparentheses warning below
11484 when we get back up to the parent if statement. */
11485 if (if_p != NULL)
11486 *if_p = true;
11487
11488 if (discard_else)
11489 {
11490 ELSE_CLAUSE (statement) = NULL_TREE;
11491 in_discarded_stmt = was_discarded;
11492 --c_inhibit_evaluation_warnings;
11493 }
11494 }
11495 else
11496 {
11497 /* This if statement does not have an else clause. If
11498 NESTED_IF is true, then the then-clause has an if
11499 statement which does have an else clause. We warn
11500 about the potential ambiguity. */
11501 if (nested_if)
11502 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11503 "suggest explicit braces to avoid ambiguous"
11504 " %<else%>");
11505 if (warn_duplicated_cond)
11506 {
11507 /* We don't need the condition chain anymore. */
11508 delete chain;
11509 chain = NULL;
11510 }
11511 }
11512
11513 /* Now we're all done with the if-statement. */
11514 finish_if_stmt (statement);
11515 }
11516 else
11517 {
11518 bool in_switch_statement_p;
11519 unsigned char in_statement;
11520
11521 /* Add the condition. */
11522 finish_switch_cond (condition, statement);
11523
11524 /* Parse the body of the switch-statement. */
11525 in_switch_statement_p = parser->in_switch_statement_p;
11526 in_statement = parser->in_statement;
11527 parser->in_switch_statement_p = true;
11528 parser->in_statement |= IN_SWITCH_STMT;
11529 cp_parser_implicitly_scoped_statement (parser, if_p,
11530 guard_tinfo);
11531 parser->in_switch_statement_p = in_switch_statement_p;
11532 parser->in_statement = in_statement;
11533
11534 /* Now we're all done with the switch-statement. */
11535 finish_switch_stmt (statement);
11536 }
11537
11538 return statement;
11539 }
11540 break;
11541
11542 default:
11543 cp_parser_error (parser, "expected selection-statement");
11544 return error_mark_node;
11545 }
11546 }
11547
11548 /* Parse a condition.
11549
11550 condition:
11551 expression
11552 type-specifier-seq declarator = initializer-clause
11553 type-specifier-seq declarator braced-init-list
11554
11555 GNU Extension:
11556
11557 condition:
11558 type-specifier-seq declarator asm-specification [opt]
11559 attributes [opt] = assignment-expression
11560
11561 Returns the expression that should be tested. */
11562
11563 static tree
11564 cp_parser_condition (cp_parser* parser)
11565 {
11566 cp_decl_specifier_seq type_specifiers;
11567 const char *saved_message;
11568 int declares_class_or_enum;
11569
11570 /* Try the declaration first. */
11571 cp_parser_parse_tentatively (parser);
11572 /* New types are not allowed in the type-specifier-seq for a
11573 condition. */
11574 saved_message = parser->type_definition_forbidden_message;
11575 parser->type_definition_forbidden_message
11576 = G_("types may not be defined in conditions");
11577 /* Parse the type-specifier-seq. */
11578 cp_parser_decl_specifier_seq (parser,
11579 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11580 &type_specifiers,
11581 &declares_class_or_enum);
11582 /* Restore the saved message. */
11583 parser->type_definition_forbidden_message = saved_message;
11584 /* If all is well, we might be looking at a declaration. */
11585 if (!cp_parser_error_occurred (parser))
11586 {
11587 tree decl;
11588 tree asm_specification;
11589 tree attributes;
11590 cp_declarator *declarator;
11591 tree initializer = NULL_TREE;
11592
11593 /* Parse the declarator. */
11594 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11595 /*ctor_dtor_or_conv_p=*/NULL,
11596 /*parenthesized_p=*/NULL,
11597 /*member_p=*/false,
11598 /*friend_p=*/false);
11599 /* Parse the attributes. */
11600 attributes = cp_parser_attributes_opt (parser);
11601 /* Parse the asm-specification. */
11602 asm_specification = cp_parser_asm_specification_opt (parser);
11603 /* If the next token is not an `=' or '{', then we might still be
11604 looking at an expression. For example:
11605
11606 if (A(a).x)
11607
11608 looks like a decl-specifier-seq and a declarator -- but then
11609 there is no `=', so this is an expression. */
11610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11611 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11612 cp_parser_simulate_error (parser);
11613
11614 /* If we did see an `=' or '{', then we are looking at a declaration
11615 for sure. */
11616 if (cp_parser_parse_definitely (parser))
11617 {
11618 tree pushed_scope;
11619 bool non_constant_p;
11620 int flags = LOOKUP_ONLYCONVERTING;
11621
11622 /* Create the declaration. */
11623 decl = start_decl (declarator, &type_specifiers,
11624 /*initialized_p=*/true,
11625 attributes, /*prefix_attributes=*/NULL_TREE,
11626 &pushed_scope);
11627
11628 /* Parse the initializer. */
11629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11630 {
11631 initializer = cp_parser_braced_list (parser, &non_constant_p);
11632 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11633 flags = 0;
11634 }
11635 else
11636 {
11637 /* Consume the `='. */
11638 cp_parser_require (parser, CPP_EQ, RT_EQ);
11639 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11640 }
11641 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11643
11644 /* Process the initializer. */
11645 cp_finish_decl (decl,
11646 initializer, !non_constant_p,
11647 asm_specification,
11648 flags);
11649
11650 if (pushed_scope)
11651 pop_scope (pushed_scope);
11652
11653 return convert_from_reference (decl);
11654 }
11655 }
11656 /* If we didn't even get past the declarator successfully, we are
11657 definitely not looking at a declaration. */
11658 else
11659 cp_parser_abort_tentative_parse (parser);
11660
11661 /* Otherwise, we are looking at an expression. */
11662 return cp_parser_expression (parser);
11663 }
11664
11665 /* Parses a for-statement or range-for-statement until the closing ')',
11666 not included. */
11667
11668 static tree
11669 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11670 {
11671 tree init, scope, decl;
11672 bool is_range_for;
11673
11674 /* Begin the for-statement. */
11675 scope = begin_for_scope (&init);
11676
11677 /* Parse the initialization. */
11678 is_range_for = cp_parser_init_statement (parser, &decl);
11679
11680 if (is_range_for)
11681 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11682 else
11683 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11684 }
11685
11686 static tree
11687 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11688 unsigned short unroll)
11689 {
11690 /* Normal for loop */
11691 tree condition = NULL_TREE;
11692 tree expression = NULL_TREE;
11693 tree stmt;
11694
11695 stmt = begin_for_stmt (scope, init);
11696 /* The init-statement has already been parsed in
11697 cp_parser_init_statement, so no work is needed here. */
11698 finish_init_stmt (stmt);
11699
11700 /* If there's a condition, process it. */
11701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11702 condition = cp_parser_condition (parser);
11703 else if (ivdep)
11704 {
11705 cp_parser_error (parser, "missing loop condition in loop with "
11706 "%<GCC ivdep%> pragma");
11707 condition = error_mark_node;
11708 }
11709 else if (unroll)
11710 {
11711 cp_parser_error (parser, "missing loop condition in loop with "
11712 "%<GCC unroll%> pragma");
11713 condition = error_mark_node;
11714 }
11715 finish_for_cond (condition, stmt, ivdep, unroll);
11716 /* Look for the `;'. */
11717 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11718
11719 /* If there's an expression, process it. */
11720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11721 expression = cp_parser_expression (parser);
11722 finish_for_expr (expression, stmt);
11723
11724 return stmt;
11725 }
11726
11727 /* Tries to parse a range-based for-statement:
11728
11729 range-based-for:
11730 decl-specifier-seq declarator : expression
11731
11732 The decl-specifier-seq declarator and the `:' are already parsed by
11733 cp_parser_init_statement. If processing_template_decl it returns a
11734 newly created RANGE_FOR_STMT; if not, it is converted to a
11735 regular FOR_STMT. */
11736
11737 static tree
11738 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11739 bool ivdep, unsigned short unroll)
11740 {
11741 tree stmt, range_expr;
11742 auto_vec <cxx_binding *, 16> bindings;
11743 auto_vec <tree, 16> names;
11744 tree decomp_first_name = NULL_TREE;
11745 unsigned int decomp_cnt = 0;
11746
11747 /* Get the range declaration momentarily out of the way so that
11748 the range expression doesn't clash with it. */
11749 if (range_decl != error_mark_node)
11750 {
11751 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11752 {
11753 tree v = DECL_VALUE_EXPR (range_decl);
11754 /* For decomposition declaration get all of the corresponding
11755 declarations out of the way. */
11756 if (TREE_CODE (v) == ARRAY_REF
11757 && VAR_P (TREE_OPERAND (v, 0))
11758 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11759 {
11760 tree d = range_decl;
11761 range_decl = TREE_OPERAND (v, 0);
11762 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11763 decomp_first_name = d;
11764 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11765 {
11766 tree name = DECL_NAME (d);
11767 names.safe_push (name);
11768 bindings.safe_push (IDENTIFIER_BINDING (name));
11769 IDENTIFIER_BINDING (name)
11770 = IDENTIFIER_BINDING (name)->previous;
11771 }
11772 }
11773 }
11774 if (names.is_empty ())
11775 {
11776 tree name = DECL_NAME (range_decl);
11777 names.safe_push (name);
11778 bindings.safe_push (IDENTIFIER_BINDING (name));
11779 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11780 }
11781 }
11782
11783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11784 {
11785 bool expr_non_constant_p;
11786 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11787 }
11788 else
11789 range_expr = cp_parser_expression (parser);
11790
11791 /* Put the range declaration(s) back into scope. */
11792 for (unsigned int i = 0; i < names.length (); i++)
11793 {
11794 cxx_binding *binding = bindings[i];
11795 binding->previous = IDENTIFIER_BINDING (names[i]);
11796 IDENTIFIER_BINDING (names[i]) = binding;
11797 }
11798
11799 /* If in template, STMT is converted to a normal for-statement
11800 at instantiation. If not, it is done just ahead. */
11801 if (processing_template_decl)
11802 {
11803 if (check_for_bare_parameter_packs (range_expr))
11804 range_expr = error_mark_node;
11805 stmt = begin_range_for_stmt (scope, init);
11806 if (ivdep)
11807 RANGE_FOR_IVDEP (stmt) = 1;
11808 if (unroll)
11809 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11810 finish_range_for_decl (stmt, range_decl, range_expr);
11811 if (!type_dependent_expression_p (range_expr)
11812 /* do_auto_deduction doesn't mess with template init-lists. */
11813 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11814 do_range_for_auto_deduction (range_decl, range_expr);
11815 }
11816 else
11817 {
11818 stmt = begin_for_stmt (scope, init);
11819 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11820 decomp_first_name, decomp_cnt, ivdep,
11821 unroll);
11822 }
11823 return stmt;
11824 }
11825
11826 /* Subroutine of cp_convert_range_for: given the initializer expression,
11827 builds up the range temporary. */
11828
11829 static tree
11830 build_range_temp (tree range_expr)
11831 {
11832 tree range_type, range_temp;
11833
11834 /* Find out the type deduced by the declaration
11835 `auto &&__range = range_expr'. */
11836 range_type = cp_build_reference_type (make_auto (), true);
11837 range_type = do_auto_deduction (range_type, range_expr,
11838 type_uses_auto (range_type));
11839
11840 /* Create the __range variable. */
11841 range_temp = build_decl (input_location, VAR_DECL,
11842 get_identifier ("__for_range"), range_type);
11843 TREE_USED (range_temp) = 1;
11844 DECL_ARTIFICIAL (range_temp) = 1;
11845
11846 return range_temp;
11847 }
11848
11849 /* Used by cp_parser_range_for in template context: we aren't going to
11850 do a full conversion yet, but we still need to resolve auto in the
11851 type of the for-range-declaration if present. This is basically
11852 a shortcut version of cp_convert_range_for. */
11853
11854 static void
11855 do_range_for_auto_deduction (tree decl, tree range_expr)
11856 {
11857 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11858 if (auto_node)
11859 {
11860 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11861 range_temp = convert_from_reference (build_range_temp (range_expr));
11862 iter_type = (cp_parser_perform_range_for_lookup
11863 (range_temp, &begin_dummy, &end_dummy));
11864 if (iter_type)
11865 {
11866 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11867 iter_type);
11868 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11869 RO_UNARY_STAR,
11870 tf_warning_or_error);
11871 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11872 iter_decl, auto_node);
11873 }
11874 }
11875 }
11876
11877 /* Converts a range-based for-statement into a normal
11878 for-statement, as per the definition.
11879
11880 for (RANGE_DECL : RANGE_EXPR)
11881 BLOCK
11882
11883 should be equivalent to:
11884
11885 {
11886 auto &&__range = RANGE_EXPR;
11887 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11888 __begin != __end;
11889 ++__begin)
11890 {
11891 RANGE_DECL = *__begin;
11892 BLOCK
11893 }
11894 }
11895
11896 If RANGE_EXPR is an array:
11897 BEGIN_EXPR = __range
11898 END_EXPR = __range + ARRAY_SIZE(__range)
11899 Else if RANGE_EXPR has a member 'begin' or 'end':
11900 BEGIN_EXPR = __range.begin()
11901 END_EXPR = __range.end()
11902 Else:
11903 BEGIN_EXPR = begin(__range)
11904 END_EXPR = end(__range);
11905
11906 If __range has a member 'begin' but not 'end', or vice versa, we must
11907 still use the second alternative (it will surely fail, however).
11908 When calling begin()/end() in the third alternative we must use
11909 argument dependent lookup, but always considering 'std' as an associated
11910 namespace. */
11911
11912 tree
11913 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11914 tree decomp_first_name, unsigned int decomp_cnt,
11915 bool ivdep, unsigned short unroll)
11916 {
11917 tree begin, end;
11918 tree iter_type, begin_expr, end_expr;
11919 tree condition, expression;
11920
11921 range_expr = mark_lvalue_use (range_expr);
11922
11923 if (range_decl == error_mark_node || range_expr == error_mark_node)
11924 /* If an error happened previously do nothing or else a lot of
11925 unhelpful errors would be issued. */
11926 begin_expr = end_expr = iter_type = error_mark_node;
11927 else
11928 {
11929 tree range_temp;
11930
11931 if (VAR_P (range_expr)
11932 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11933 /* Can't bind a reference to an array of runtime bound. */
11934 range_temp = range_expr;
11935 else
11936 {
11937 range_temp = build_range_temp (range_expr);
11938 pushdecl (range_temp);
11939 cp_finish_decl (range_temp, range_expr,
11940 /*is_constant_init*/false, NULL_TREE,
11941 LOOKUP_ONLYCONVERTING);
11942 range_temp = convert_from_reference (range_temp);
11943 }
11944 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11945 &begin_expr, &end_expr);
11946 }
11947
11948 /* The new for initialization statement. */
11949 begin = build_decl (input_location, VAR_DECL,
11950 get_identifier ("__for_begin"), iter_type);
11951 TREE_USED (begin) = 1;
11952 DECL_ARTIFICIAL (begin) = 1;
11953 pushdecl (begin);
11954 cp_finish_decl (begin, begin_expr,
11955 /*is_constant_init*/false, NULL_TREE,
11956 LOOKUP_ONLYCONVERTING);
11957
11958 if (cxx_dialect >= cxx17)
11959 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11960 end = build_decl (input_location, VAR_DECL,
11961 get_identifier ("__for_end"), iter_type);
11962 TREE_USED (end) = 1;
11963 DECL_ARTIFICIAL (end) = 1;
11964 pushdecl (end);
11965 cp_finish_decl (end, end_expr,
11966 /*is_constant_init*/false, NULL_TREE,
11967 LOOKUP_ONLYCONVERTING);
11968
11969 finish_init_stmt (statement);
11970
11971 /* The new for condition. */
11972 condition = build_x_binary_op (input_location, NE_EXPR,
11973 begin, ERROR_MARK,
11974 end, ERROR_MARK,
11975 NULL, tf_warning_or_error);
11976 finish_for_cond (condition, statement, ivdep, unroll);
11977
11978 /* The new increment expression. */
11979 expression = finish_unary_op_expr (input_location,
11980 PREINCREMENT_EXPR, begin,
11981 tf_warning_or_error);
11982 finish_for_expr (expression, statement);
11983
11984 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11985 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11986
11987 /* The declaration is initialized with *__begin inside the loop body. */
11988 cp_finish_decl (range_decl,
11989 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11990 tf_warning_or_error),
11991 /*is_constant_init*/false, NULL_TREE,
11992 LOOKUP_ONLYCONVERTING);
11993 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11994 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11995
11996 return statement;
11997 }
11998
11999 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12000 We need to solve both at the same time because the method used
12001 depends on the existence of members begin or end.
12002 Returns the type deduced for the iterator expression. */
12003
12004 static tree
12005 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12006 {
12007 if (error_operand_p (range))
12008 {
12009 *begin = *end = error_mark_node;
12010 return error_mark_node;
12011 }
12012
12013 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12014 {
12015 error ("range-based %<for%> expression of type %qT "
12016 "has incomplete type", TREE_TYPE (range));
12017 *begin = *end = error_mark_node;
12018 return error_mark_node;
12019 }
12020 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12021 {
12022 /* If RANGE is an array, we will use pointer arithmetic. */
12023 *begin = decay_conversion (range, tf_warning_or_error);
12024 *end = build_binary_op (input_location, PLUS_EXPR,
12025 range,
12026 array_type_nelts_top (TREE_TYPE (range)),
12027 false);
12028 return TREE_TYPE (*begin);
12029 }
12030 else
12031 {
12032 /* If it is not an array, we must do a bit of magic. */
12033 tree id_begin, id_end;
12034 tree member_begin, member_end;
12035
12036 *begin = *end = error_mark_node;
12037
12038 id_begin = get_identifier ("begin");
12039 id_end = get_identifier ("end");
12040 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12041 /*protect=*/2, /*want_type=*/false,
12042 tf_warning_or_error);
12043 member_end = lookup_member (TREE_TYPE (range), id_end,
12044 /*protect=*/2, /*want_type=*/false,
12045 tf_warning_or_error);
12046
12047 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12048 {
12049 /* Use the member functions. */
12050 if (member_begin != NULL_TREE)
12051 *begin = cp_parser_range_for_member_function (range, id_begin);
12052 else
12053 error ("range-based %<for%> expression of type %qT has an "
12054 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12055
12056 if (member_end != NULL_TREE)
12057 *end = cp_parser_range_for_member_function (range, id_end);
12058 else
12059 error ("range-based %<for%> expression of type %qT has a "
12060 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12061 }
12062 else
12063 {
12064 /* Use global functions with ADL. */
12065 vec<tree, va_gc> *vec;
12066 vec = make_tree_vector ();
12067
12068 vec_safe_push (vec, range);
12069
12070 member_begin = perform_koenig_lookup (id_begin, vec,
12071 tf_warning_or_error);
12072 *begin = finish_call_expr (member_begin, &vec, false, true,
12073 tf_warning_or_error);
12074 member_end = perform_koenig_lookup (id_end, vec,
12075 tf_warning_or_error);
12076 *end = finish_call_expr (member_end, &vec, false, true,
12077 tf_warning_or_error);
12078
12079 release_tree_vector (vec);
12080 }
12081
12082 /* Last common checks. */
12083 if (*begin == error_mark_node || *end == error_mark_node)
12084 {
12085 /* If one of the expressions is an error do no more checks. */
12086 *begin = *end = error_mark_node;
12087 return error_mark_node;
12088 }
12089 else if (type_dependent_expression_p (*begin)
12090 || type_dependent_expression_p (*end))
12091 /* Can happen, when, eg, in a template context, Koenig lookup
12092 can't resolve begin/end (c++/58503). */
12093 return NULL_TREE;
12094 else
12095 {
12096 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12097 /* The unqualified type of the __begin and __end temporaries should
12098 be the same, as required by the multiple auto declaration. */
12099 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12100 {
12101 if (cxx_dialect >= cxx17
12102 && (build_x_binary_op (input_location, NE_EXPR,
12103 *begin, ERROR_MARK,
12104 *end, ERROR_MARK,
12105 NULL, tf_none)
12106 != error_mark_node))
12107 /* P0184R0 allows __begin and __end to have different types,
12108 but make sure they are comparable so we can give a better
12109 diagnostic. */;
12110 else
12111 error ("inconsistent begin/end types in range-based %<for%> "
12112 "statement: %qT and %qT",
12113 TREE_TYPE (*begin), TREE_TYPE (*end));
12114 }
12115 return iter_type;
12116 }
12117 }
12118 }
12119
12120 /* Helper function for cp_parser_perform_range_for_lookup.
12121 Builds a tree for RANGE.IDENTIFIER(). */
12122
12123 static tree
12124 cp_parser_range_for_member_function (tree range, tree identifier)
12125 {
12126 tree member, res;
12127 vec<tree, va_gc> *vec;
12128
12129 member = finish_class_member_access_expr (range, identifier,
12130 false, tf_warning_or_error);
12131 if (member == error_mark_node)
12132 return error_mark_node;
12133
12134 vec = make_tree_vector ();
12135 res = finish_call_expr (member, &vec,
12136 /*disallow_virtual=*/false,
12137 /*koenig_p=*/false,
12138 tf_warning_or_error);
12139 release_tree_vector (vec);
12140 return res;
12141 }
12142
12143 /* Parse an iteration-statement.
12144
12145 iteration-statement:
12146 while ( condition ) statement
12147 do statement while ( expression ) ;
12148 for ( init-statement condition [opt] ; expression [opt] )
12149 statement
12150
12151 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12152
12153 static tree
12154 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12155 unsigned short unroll)
12156 {
12157 cp_token *token;
12158 enum rid keyword;
12159 tree statement;
12160 unsigned char in_statement;
12161 token_indent_info guard_tinfo;
12162
12163 /* Peek at the next token. */
12164 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12165 if (!token)
12166 return error_mark_node;
12167
12168 guard_tinfo = get_token_indent_info (token);
12169
12170 /* Remember whether or not we are already within an iteration
12171 statement. */
12172 in_statement = parser->in_statement;
12173
12174 /* See what kind of keyword it is. */
12175 keyword = token->keyword;
12176 switch (keyword)
12177 {
12178 case RID_WHILE:
12179 {
12180 tree condition;
12181
12182 /* Begin the while-statement. */
12183 statement = begin_while_stmt ();
12184 /* Look for the `('. */
12185 matching_parens parens;
12186 parens.require_open (parser);
12187 /* Parse the condition. */
12188 condition = cp_parser_condition (parser);
12189 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12190 /* Look for the `)'. */
12191 parens.require_close (parser);
12192 /* Parse the dependent statement. */
12193 parser->in_statement = IN_ITERATION_STMT;
12194 bool prev = note_iteration_stmt_body_start ();
12195 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12196 note_iteration_stmt_body_end (prev);
12197 parser->in_statement = in_statement;
12198 /* We're done with the while-statement. */
12199 finish_while_stmt (statement);
12200 }
12201 break;
12202
12203 case RID_DO:
12204 {
12205 tree expression;
12206
12207 /* Begin the do-statement. */
12208 statement = begin_do_stmt ();
12209 /* Parse the body of the do-statement. */
12210 parser->in_statement = IN_ITERATION_STMT;
12211 bool prev = note_iteration_stmt_body_start ();
12212 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12213 note_iteration_stmt_body_end (prev);
12214 parser->in_statement = in_statement;
12215 finish_do_body (statement);
12216 /* Look for the `while' keyword. */
12217 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12218 /* Look for the `('. */
12219 matching_parens parens;
12220 parens.require_open (parser);
12221 /* Parse the expression. */
12222 expression = cp_parser_expression (parser);
12223 /* We're done with the do-statement. */
12224 finish_do_stmt (expression, statement, ivdep, unroll);
12225 /* Look for the `)'. */
12226 parens.require_close (parser);
12227 /* Look for the `;'. */
12228 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12229 }
12230 break;
12231
12232 case RID_FOR:
12233 {
12234 /* Look for the `('. */
12235 matching_parens parens;
12236 parens.require_open (parser);
12237
12238 statement = cp_parser_for (parser, ivdep, unroll);
12239
12240 /* Look for the `)'. */
12241 parens.require_close (parser);
12242
12243 /* Parse the body of the for-statement. */
12244 parser->in_statement = IN_ITERATION_STMT;
12245 bool prev = note_iteration_stmt_body_start ();
12246 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12247 note_iteration_stmt_body_end (prev);
12248 parser->in_statement = in_statement;
12249
12250 /* We're done with the for-statement. */
12251 finish_for_stmt (statement);
12252 }
12253 break;
12254
12255 default:
12256 cp_parser_error (parser, "expected iteration-statement");
12257 statement = error_mark_node;
12258 break;
12259 }
12260
12261 return statement;
12262 }
12263
12264 /* Parse a init-statement or the declarator of a range-based-for.
12265 Returns true if a range-based-for declaration is seen.
12266
12267 init-statement:
12268 expression-statement
12269 simple-declaration */
12270
12271 static bool
12272 cp_parser_init_statement (cp_parser* parser, tree *decl)
12273 {
12274 /* If the next token is a `;', then we have an empty
12275 expression-statement. Grammatically, this is also a
12276 simple-declaration, but an invalid one, because it does not
12277 declare anything. Therefore, if we did not handle this case
12278 specially, we would issue an error message about an invalid
12279 declaration. */
12280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12281 {
12282 bool is_range_for = false;
12283 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12284
12285 /* A colon is used in range-based for. */
12286 parser->colon_corrects_to_scope_p = false;
12287
12288 /* We're going to speculatively look for a declaration, falling back
12289 to an expression, if necessary. */
12290 cp_parser_parse_tentatively (parser);
12291 /* Parse the declaration. */
12292 cp_parser_simple_declaration (parser,
12293 /*function_definition_allowed_p=*/false,
12294 decl);
12295 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12296 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12297 {
12298 /* It is a range-for, consume the ':' */
12299 cp_lexer_consume_token (parser->lexer);
12300 is_range_for = true;
12301 if (cxx_dialect < cxx11)
12302 {
12303 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12304 "range-based %<for%> loops only available with "
12305 "-std=c++11 or -std=gnu++11");
12306 *decl = error_mark_node;
12307 }
12308 }
12309 else
12310 /* The ';' is not consumed yet because we told
12311 cp_parser_simple_declaration not to. */
12312 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12313
12314 if (cp_parser_parse_definitely (parser))
12315 return is_range_for;
12316 /* If the tentative parse failed, then we shall need to look for an
12317 expression-statement. */
12318 }
12319 /* If we are here, it is an expression-statement. */
12320 cp_parser_expression_statement (parser, NULL_TREE);
12321 return false;
12322 }
12323
12324 /* Parse a jump-statement.
12325
12326 jump-statement:
12327 break ;
12328 continue ;
12329 return expression [opt] ;
12330 return braced-init-list ;
12331 goto identifier ;
12332
12333 GNU extension:
12334
12335 jump-statement:
12336 goto * expression ;
12337
12338 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12339
12340 static tree
12341 cp_parser_jump_statement (cp_parser* parser)
12342 {
12343 tree statement = error_mark_node;
12344 cp_token *token;
12345 enum rid keyword;
12346 unsigned char in_statement;
12347
12348 /* Peek at the next token. */
12349 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12350 if (!token)
12351 return error_mark_node;
12352
12353 /* See what kind of keyword it is. */
12354 keyword = token->keyword;
12355 switch (keyword)
12356 {
12357 case RID_BREAK:
12358 in_statement = parser->in_statement & ~IN_IF_STMT;
12359 switch (in_statement)
12360 {
12361 case 0:
12362 error_at (token->location, "break statement not within loop or switch");
12363 break;
12364 default:
12365 gcc_assert ((in_statement & IN_SWITCH_STMT)
12366 || in_statement == IN_ITERATION_STMT);
12367 statement = finish_break_stmt ();
12368 if (in_statement == IN_ITERATION_STMT)
12369 break_maybe_infinite_loop ();
12370 break;
12371 case IN_OMP_BLOCK:
12372 error_at (token->location, "invalid exit from OpenMP structured block");
12373 break;
12374 case IN_OMP_FOR:
12375 error_at (token->location, "break statement used with OpenMP for loop");
12376 break;
12377 }
12378 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12379 break;
12380
12381 case RID_CONTINUE:
12382 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12383 {
12384 case 0:
12385 error_at (token->location, "continue statement not within a loop");
12386 break;
12387 /* Fall through. */
12388 case IN_ITERATION_STMT:
12389 case IN_OMP_FOR:
12390 statement = finish_continue_stmt ();
12391 break;
12392 case IN_OMP_BLOCK:
12393 error_at (token->location, "invalid exit from OpenMP structured block");
12394 break;
12395 default:
12396 gcc_unreachable ();
12397 }
12398 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12399 break;
12400
12401 case RID_RETURN:
12402 {
12403 tree expr;
12404 bool expr_non_constant_p;
12405
12406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12407 {
12408 cp_lexer_set_source_position (parser->lexer);
12409 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12410 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12411 }
12412 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12413 expr = cp_parser_expression (parser);
12414 else
12415 /* If the next token is a `;', then there is no
12416 expression. */
12417 expr = NULL_TREE;
12418 /* Build the return-statement. */
12419 if (current_function_auto_return_pattern && in_discarded_stmt)
12420 /* Don't deduce from a discarded return statement. */;
12421 else
12422 statement = finish_return_stmt (expr);
12423 /* Look for the final `;'. */
12424 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12425 }
12426 break;
12427
12428 case RID_GOTO:
12429 if (parser->in_function_body
12430 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12431 {
12432 error ("%<goto%> in %<constexpr%> function");
12433 cp_function_chain->invalid_constexpr = true;
12434 }
12435
12436 /* Create the goto-statement. */
12437 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12438 {
12439 /* Issue a warning about this use of a GNU extension. */
12440 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12441 /* Consume the '*' token. */
12442 cp_lexer_consume_token (parser->lexer);
12443 /* Parse the dependent expression. */
12444 finish_goto_stmt (cp_parser_expression (parser));
12445 }
12446 else
12447 finish_goto_stmt (cp_parser_identifier (parser));
12448 /* Look for the final `;'. */
12449 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12450 break;
12451
12452 default:
12453 cp_parser_error (parser, "expected jump-statement");
12454 break;
12455 }
12456
12457 return statement;
12458 }
12459
12460 /* Parse a declaration-statement.
12461
12462 declaration-statement:
12463 block-declaration */
12464
12465 static void
12466 cp_parser_declaration_statement (cp_parser* parser)
12467 {
12468 void *p;
12469
12470 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12471 p = obstack_alloc (&declarator_obstack, 0);
12472
12473 /* Parse the block-declaration. */
12474 cp_parser_block_declaration (parser, /*statement_p=*/true);
12475
12476 /* Free any declarators allocated. */
12477 obstack_free (&declarator_obstack, p);
12478 }
12479
12480 /* Some dependent statements (like `if (cond) statement'), are
12481 implicitly in their own scope. In other words, if the statement is
12482 a single statement (as opposed to a compound-statement), it is
12483 none-the-less treated as if it were enclosed in braces. Any
12484 declarations appearing in the dependent statement are out of scope
12485 after control passes that point. This function parses a statement,
12486 but ensures that is in its own scope, even if it is not a
12487 compound-statement.
12488
12489 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12490 is a (possibly labeled) if statement which is not enclosed in
12491 braces and has an else clause. This is used to implement
12492 -Wparentheses.
12493
12494 CHAIN is a vector of if-else-if conditions. This is used to implement
12495 -Wduplicated-cond.
12496
12497 Returns the new statement. */
12498
12499 static tree
12500 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12501 const token_indent_info &guard_tinfo,
12502 vec<tree> *chain)
12503 {
12504 tree statement;
12505 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12506 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12507 token_indent_info body_tinfo
12508 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12509
12510 if (if_p != NULL)
12511 *if_p = false;
12512
12513 /* Mark if () ; with a special NOP_EXPR. */
12514 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12515 {
12516 cp_lexer_consume_token (parser->lexer);
12517 statement = add_stmt (build_empty_stmt (body_loc));
12518
12519 if (guard_tinfo.keyword == RID_IF
12520 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12521 warning_at (body_loc, OPT_Wempty_body,
12522 "suggest braces around empty body in an %<if%> statement");
12523 else if (guard_tinfo.keyword == RID_ELSE)
12524 warning_at (body_loc, OPT_Wempty_body,
12525 "suggest braces around empty body in an %<else%> statement");
12526 }
12527 /* if a compound is opened, we simply parse the statement directly. */
12528 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12529 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12530 /* If the token is not a `{', then we must take special action. */
12531 else
12532 {
12533 /* Create a compound-statement. */
12534 statement = begin_compound_stmt (0);
12535 /* Parse the dependent-statement. */
12536 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12537 &body_loc_after_labels);
12538 /* Finish the dummy compound-statement. */
12539 finish_compound_stmt (statement);
12540 }
12541
12542 token_indent_info next_tinfo
12543 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12544 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12545
12546 if (body_loc_after_labels != UNKNOWN_LOCATION
12547 && next_tinfo.type != CPP_SEMICOLON)
12548 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12549 guard_tinfo.location, guard_tinfo.keyword);
12550
12551 /* Return the statement. */
12552 return statement;
12553 }
12554
12555 /* For some dependent statements (like `while (cond) statement'), we
12556 have already created a scope. Therefore, even if the dependent
12557 statement is a compound-statement, we do not want to create another
12558 scope. */
12559
12560 static void
12561 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12562 const token_indent_info &guard_tinfo)
12563 {
12564 /* If the token is a `{', then we must take special action. */
12565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12566 {
12567 token_indent_info body_tinfo
12568 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12569 location_t loc_after_labels = UNKNOWN_LOCATION;
12570
12571 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12572 &loc_after_labels);
12573 token_indent_info next_tinfo
12574 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12575 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12576
12577 if (loc_after_labels != UNKNOWN_LOCATION
12578 && next_tinfo.type != CPP_SEMICOLON)
12579 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12580 guard_tinfo.location,
12581 guard_tinfo.keyword);
12582 }
12583 else
12584 {
12585 /* Avoid calling cp_parser_compound_statement, so that we
12586 don't create a new scope. Do everything else by hand. */
12587 matching_braces braces;
12588 braces.require_open (parser);
12589 /* If the next keyword is `__label__' we have a label declaration. */
12590 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12591 cp_parser_label_declaration (parser);
12592 /* Parse an (optional) statement-seq. */
12593 cp_parser_statement_seq_opt (parser, NULL_TREE);
12594 braces.require_close (parser);
12595 }
12596 }
12597
12598 /* Declarations [gram.dcl.dcl] */
12599
12600 /* Parse an optional declaration-sequence.
12601
12602 declaration-seq:
12603 declaration
12604 declaration-seq declaration */
12605
12606 static void
12607 cp_parser_declaration_seq_opt (cp_parser* parser)
12608 {
12609 while (true)
12610 {
12611 cp_token *token;
12612
12613 token = cp_lexer_peek_token (parser->lexer);
12614
12615 if (token->type == CPP_CLOSE_BRACE
12616 || token->type == CPP_EOF
12617 || token->type == CPP_PRAGMA_EOL)
12618 break;
12619
12620 if (token->type == CPP_SEMICOLON)
12621 {
12622 /* A declaration consisting of a single semicolon is
12623 invalid. Allow it unless we're being pedantic. */
12624 cp_lexer_consume_token (parser->lexer);
12625 if (!in_system_header_at (input_location))
12626 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12627 continue;
12628 }
12629
12630 /* If we're entering or exiting a region that's implicitly
12631 extern "C", modify the lang context appropriately. */
12632 if (!parser->implicit_extern_c && token->implicit_extern_c)
12633 {
12634 push_lang_context (lang_name_c);
12635 parser->implicit_extern_c = true;
12636 }
12637 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12638 {
12639 pop_lang_context ();
12640 parser->implicit_extern_c = false;
12641 }
12642
12643 if (token->type == CPP_PRAGMA)
12644 {
12645 /* A top-level declaration can consist solely of a #pragma.
12646 A nested declaration cannot, so this is done here and not
12647 in cp_parser_declaration. (A #pragma at block scope is
12648 handled in cp_parser_statement.) */
12649 cp_parser_pragma (parser, pragma_external, NULL);
12650 continue;
12651 }
12652
12653 /* Parse the declaration itself. */
12654 cp_parser_declaration (parser);
12655 }
12656 }
12657
12658 /* Parse a declaration.
12659
12660 declaration:
12661 block-declaration
12662 function-definition
12663 template-declaration
12664 explicit-instantiation
12665 explicit-specialization
12666 linkage-specification
12667 namespace-definition
12668
12669 C++17:
12670 deduction-guide
12671
12672 GNU extension:
12673
12674 declaration:
12675 __extension__ declaration */
12676
12677 static void
12678 cp_parser_declaration (cp_parser* parser)
12679 {
12680 cp_token token1;
12681 cp_token token2;
12682 int saved_pedantic;
12683 void *p;
12684 tree attributes = NULL_TREE;
12685
12686 /* Check for the `__extension__' keyword. */
12687 if (cp_parser_extension_opt (parser, &saved_pedantic))
12688 {
12689 /* Parse the qualified declaration. */
12690 cp_parser_declaration (parser);
12691 /* Restore the PEDANTIC flag. */
12692 pedantic = saved_pedantic;
12693
12694 return;
12695 }
12696
12697 /* Try to figure out what kind of declaration is present. */
12698 token1 = *cp_lexer_peek_token (parser->lexer);
12699
12700 if (token1.type != CPP_EOF)
12701 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12702 else
12703 {
12704 token2.type = CPP_EOF;
12705 token2.keyword = RID_MAX;
12706 }
12707
12708 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12709 p = obstack_alloc (&declarator_obstack, 0);
12710
12711 /* If the next token is `extern' and the following token is a string
12712 literal, then we have a linkage specification. */
12713 if (token1.keyword == RID_EXTERN
12714 && cp_parser_is_pure_string_literal (&token2))
12715 cp_parser_linkage_specification (parser);
12716 /* If the next token is `template', then we have either a template
12717 declaration, an explicit instantiation, or an explicit
12718 specialization. */
12719 else if (token1.keyword == RID_TEMPLATE)
12720 {
12721 /* `template <>' indicates a template specialization. */
12722 if (token2.type == CPP_LESS
12723 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12724 cp_parser_explicit_specialization (parser);
12725 /* `template <' indicates a template declaration. */
12726 else if (token2.type == CPP_LESS)
12727 cp_parser_template_declaration (parser, /*member_p=*/false);
12728 /* Anything else must be an explicit instantiation. */
12729 else
12730 cp_parser_explicit_instantiation (parser);
12731 }
12732 /* If the next token is `export', then we have a template
12733 declaration. */
12734 else if (token1.keyword == RID_EXPORT)
12735 cp_parser_template_declaration (parser, /*member_p=*/false);
12736 /* If the next token is `extern', 'static' or 'inline' and the one
12737 after that is `template', we have a GNU extended explicit
12738 instantiation directive. */
12739 else if (cp_parser_allow_gnu_extensions_p (parser)
12740 && (token1.keyword == RID_EXTERN
12741 || token1.keyword == RID_STATIC
12742 || token1.keyword == RID_INLINE)
12743 && token2.keyword == RID_TEMPLATE)
12744 cp_parser_explicit_instantiation (parser);
12745 /* If the next token is `namespace', check for a named or unnamed
12746 namespace definition. */
12747 else if (token1.keyword == RID_NAMESPACE
12748 && (/* A named namespace definition. */
12749 (token2.type == CPP_NAME
12750 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12751 != CPP_EQ))
12752 || (token2.type == CPP_OPEN_SQUARE
12753 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12754 == CPP_OPEN_SQUARE)
12755 /* An unnamed namespace definition. */
12756 || token2.type == CPP_OPEN_BRACE
12757 || token2.keyword == RID_ATTRIBUTE))
12758 cp_parser_namespace_definition (parser);
12759 /* An inline (associated) namespace definition. */
12760 else if (token1.keyword == RID_INLINE
12761 && token2.keyword == RID_NAMESPACE)
12762 cp_parser_namespace_definition (parser);
12763 /* Objective-C++ declaration/definition. */
12764 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12765 cp_parser_objc_declaration (parser, NULL_TREE);
12766 else if (c_dialect_objc ()
12767 && token1.keyword == RID_ATTRIBUTE
12768 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12769 cp_parser_objc_declaration (parser, attributes);
12770 /* At this point we may have a template declared by a concept
12771 introduction. */
12772 else if (flag_concepts
12773 && cp_parser_template_declaration_after_export (parser,
12774 /*member_p=*/false))
12775 /* We did. */;
12776 else
12777 /* Try to parse a block-declaration, or a function-definition. */
12778 cp_parser_block_declaration (parser, /*statement_p=*/false);
12779
12780 /* Free any declarators allocated. */
12781 obstack_free (&declarator_obstack, p);
12782 }
12783
12784 /* Parse a block-declaration.
12785
12786 block-declaration:
12787 simple-declaration
12788 asm-definition
12789 namespace-alias-definition
12790 using-declaration
12791 using-directive
12792
12793 GNU Extension:
12794
12795 block-declaration:
12796 __extension__ block-declaration
12797
12798 C++0x Extension:
12799
12800 block-declaration:
12801 static_assert-declaration
12802
12803 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12804 part of a declaration-statement. */
12805
12806 static void
12807 cp_parser_block_declaration (cp_parser *parser,
12808 bool statement_p)
12809 {
12810 cp_token *token1;
12811 int saved_pedantic;
12812
12813 /* Check for the `__extension__' keyword. */
12814 if (cp_parser_extension_opt (parser, &saved_pedantic))
12815 {
12816 /* Parse the qualified declaration. */
12817 cp_parser_block_declaration (parser, statement_p);
12818 /* Restore the PEDANTIC flag. */
12819 pedantic = saved_pedantic;
12820
12821 return;
12822 }
12823
12824 /* Peek at the next token to figure out which kind of declaration is
12825 present. */
12826 token1 = cp_lexer_peek_token (parser->lexer);
12827
12828 /* If the next keyword is `asm', we have an asm-definition. */
12829 if (token1->keyword == RID_ASM)
12830 {
12831 if (statement_p)
12832 cp_parser_commit_to_tentative_parse (parser);
12833 cp_parser_asm_definition (parser);
12834 }
12835 /* If the next keyword is `namespace', we have a
12836 namespace-alias-definition. */
12837 else if (token1->keyword == RID_NAMESPACE)
12838 cp_parser_namespace_alias_definition (parser);
12839 /* If the next keyword is `using', we have a
12840 using-declaration, a using-directive, or an alias-declaration. */
12841 else if (token1->keyword == RID_USING)
12842 {
12843 cp_token *token2;
12844
12845 if (statement_p)
12846 cp_parser_commit_to_tentative_parse (parser);
12847 /* If the token after `using' is `namespace', then we have a
12848 using-directive. */
12849 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12850 if (token2->keyword == RID_NAMESPACE)
12851 cp_parser_using_directive (parser);
12852 /* If the second token after 'using' is '=', then we have an
12853 alias-declaration. */
12854 else if (cxx_dialect >= cxx11
12855 && token2->type == CPP_NAME
12856 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12857 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12858 cp_parser_alias_declaration (parser);
12859 /* Otherwise, it's a using-declaration. */
12860 else
12861 cp_parser_using_declaration (parser,
12862 /*access_declaration_p=*/false);
12863 }
12864 /* If the next keyword is `__label__' we have a misplaced label
12865 declaration. */
12866 else if (token1->keyword == RID_LABEL)
12867 {
12868 cp_lexer_consume_token (parser->lexer);
12869 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12870 cp_parser_skip_to_end_of_statement (parser);
12871 /* If the next token is now a `;', consume it. */
12872 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12873 cp_lexer_consume_token (parser->lexer);
12874 }
12875 /* If the next token is `static_assert' we have a static assertion. */
12876 else if (token1->keyword == RID_STATIC_ASSERT)
12877 cp_parser_static_assert (parser, /*member_p=*/false);
12878 /* Anything else must be a simple-declaration. */
12879 else
12880 cp_parser_simple_declaration (parser, !statement_p,
12881 /*maybe_range_for_decl*/NULL);
12882 }
12883
12884 /* Parse a simple-declaration.
12885
12886 simple-declaration:
12887 decl-specifier-seq [opt] init-declarator-list [opt] ;
12888 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12889 brace-or-equal-initializer ;
12890
12891 init-declarator-list:
12892 init-declarator
12893 init-declarator-list , init-declarator
12894
12895 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12896 function-definition as a simple-declaration.
12897
12898 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12899 parsed declaration if it is an uninitialized single declarator not followed
12900 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12901 if present, will not be consumed. */
12902
12903 static void
12904 cp_parser_simple_declaration (cp_parser* parser,
12905 bool function_definition_allowed_p,
12906 tree *maybe_range_for_decl)
12907 {
12908 cp_decl_specifier_seq decl_specifiers;
12909 int declares_class_or_enum;
12910 bool saw_declarator;
12911 location_t comma_loc = UNKNOWN_LOCATION;
12912 location_t init_loc = UNKNOWN_LOCATION;
12913
12914 if (maybe_range_for_decl)
12915 *maybe_range_for_decl = NULL_TREE;
12916
12917 /* Defer access checks until we know what is being declared; the
12918 checks for names appearing in the decl-specifier-seq should be
12919 done as if we were in the scope of the thing being declared. */
12920 push_deferring_access_checks (dk_deferred);
12921
12922 /* Parse the decl-specifier-seq. We have to keep track of whether
12923 or not the decl-specifier-seq declares a named class or
12924 enumeration type, since that is the only case in which the
12925 init-declarator-list is allowed to be empty.
12926
12927 [dcl.dcl]
12928
12929 In a simple-declaration, the optional init-declarator-list can be
12930 omitted only when declaring a class or enumeration, that is when
12931 the decl-specifier-seq contains either a class-specifier, an
12932 elaborated-type-specifier, or an enum-specifier. */
12933 cp_parser_decl_specifier_seq (parser,
12934 CP_PARSER_FLAGS_OPTIONAL,
12935 &decl_specifiers,
12936 &declares_class_or_enum);
12937 /* We no longer need to defer access checks. */
12938 stop_deferring_access_checks ();
12939
12940 /* In a block scope, a valid declaration must always have a
12941 decl-specifier-seq. By not trying to parse declarators, we can
12942 resolve the declaration/expression ambiguity more quickly. */
12943 if (!function_definition_allowed_p
12944 && !decl_specifiers.any_specifiers_p)
12945 {
12946 cp_parser_error (parser, "expected declaration");
12947 goto done;
12948 }
12949
12950 /* If the next two tokens are both identifiers, the code is
12951 erroneous. The usual cause of this situation is code like:
12952
12953 T t;
12954
12955 where "T" should name a type -- but does not. */
12956 if (!decl_specifiers.any_type_specifiers_p
12957 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12958 {
12959 /* If parsing tentatively, we should commit; we really are
12960 looking at a declaration. */
12961 cp_parser_commit_to_tentative_parse (parser);
12962 /* Give up. */
12963 goto done;
12964 }
12965
12966 /* If we have seen at least one decl-specifier, and the next token
12967 is not a parenthesis, then we must be looking at a declaration.
12968 (After "int (" we might be looking at a functional cast.) */
12969 if (decl_specifiers.any_specifiers_p
12970 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12971 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12972 && !cp_parser_error_occurred (parser))
12973 cp_parser_commit_to_tentative_parse (parser);
12974
12975 /* Look for C++17 decomposition declaration. */
12976 for (size_t n = 1; ; n++)
12977 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12978 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12979 continue;
12980 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12981 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12982 && decl_specifiers.any_specifiers_p)
12983 {
12984 tree decl
12985 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12986 maybe_range_for_decl,
12987 &init_loc);
12988
12989 /* The next token should be either a `,' or a `;'. */
12990 cp_token *token = cp_lexer_peek_token (parser->lexer);
12991 /* If it's a `;', we are done. */
12992 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12993 goto finish;
12994 /* Anything else is an error. */
12995 else
12996 {
12997 /* If we have already issued an error message we don't need
12998 to issue another one. */
12999 if ((decl != error_mark_node
13000 && DECL_INITIAL (decl) != error_mark_node)
13001 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13002 cp_parser_error (parser, "expected %<,%> or %<;%>");
13003 /* Skip tokens until we reach the end of the statement. */
13004 cp_parser_skip_to_end_of_statement (parser);
13005 /* If the next token is now a `;', consume it. */
13006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13007 cp_lexer_consume_token (parser->lexer);
13008 goto done;
13009 }
13010 }
13011 else
13012 break;
13013
13014 tree last_type;
13015 bool auto_specifier_p;
13016 /* NULL_TREE if both variable and function declaration are allowed,
13017 error_mark_node if function declaration are not allowed and
13018 a FUNCTION_DECL that should be diagnosed if it is followed by
13019 variable declarations. */
13020 tree auto_function_declaration;
13021
13022 last_type = NULL_TREE;
13023 auto_specifier_p
13024 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13025 auto_function_declaration = NULL_TREE;
13026
13027 /* Keep going until we hit the `;' at the end of the simple
13028 declaration. */
13029 saw_declarator = false;
13030 while (cp_lexer_next_token_is_not (parser->lexer,
13031 CPP_SEMICOLON))
13032 {
13033 cp_token *token;
13034 bool function_definition_p;
13035 tree decl;
13036 tree auto_result = NULL_TREE;
13037
13038 if (saw_declarator)
13039 {
13040 /* If we are processing next declarator, comma is expected */
13041 token = cp_lexer_peek_token (parser->lexer);
13042 gcc_assert (token->type == CPP_COMMA);
13043 cp_lexer_consume_token (parser->lexer);
13044 if (maybe_range_for_decl)
13045 {
13046 *maybe_range_for_decl = error_mark_node;
13047 if (comma_loc == UNKNOWN_LOCATION)
13048 comma_loc = token->location;
13049 }
13050 }
13051 else
13052 saw_declarator = true;
13053
13054 /* Parse the init-declarator. */
13055 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13056 /*checks=*/NULL,
13057 function_definition_allowed_p,
13058 /*member_p=*/false,
13059 declares_class_or_enum,
13060 &function_definition_p,
13061 maybe_range_for_decl,
13062 &init_loc,
13063 &auto_result);
13064 /* If an error occurred while parsing tentatively, exit quickly.
13065 (That usually happens when in the body of a function; each
13066 statement is treated as a declaration-statement until proven
13067 otherwise.) */
13068 if (cp_parser_error_occurred (parser))
13069 goto done;
13070
13071 if (auto_specifier_p && cxx_dialect >= cxx14)
13072 {
13073 /* If the init-declarator-list contains more than one
13074 init-declarator, they shall all form declarations of
13075 variables. */
13076 if (auto_function_declaration == NULL_TREE)
13077 auto_function_declaration
13078 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13079 else if (TREE_CODE (decl) == FUNCTION_DECL
13080 || auto_function_declaration != error_mark_node)
13081 {
13082 error_at (decl_specifiers.locations[ds_type_spec],
13083 "non-variable %qD in declaration with more than one "
13084 "declarator with placeholder type",
13085 TREE_CODE (decl) == FUNCTION_DECL
13086 ? decl : auto_function_declaration);
13087 auto_function_declaration = error_mark_node;
13088 }
13089 }
13090
13091 if (auto_result
13092 && (!processing_template_decl || !type_uses_auto (auto_result)))
13093 {
13094 if (last_type
13095 && last_type != error_mark_node
13096 && !same_type_p (auto_result, last_type))
13097 {
13098 /* If the list of declarators contains more than one declarator,
13099 the type of each declared variable is determined as described
13100 above. If the type deduced for the template parameter U is not
13101 the same in each deduction, the program is ill-formed. */
13102 error_at (decl_specifiers.locations[ds_type_spec],
13103 "inconsistent deduction for %qT: %qT and then %qT",
13104 decl_specifiers.type, last_type, auto_result);
13105 last_type = error_mark_node;
13106 }
13107 else
13108 last_type = auto_result;
13109 }
13110
13111 /* Handle function definitions specially. */
13112 if (function_definition_p)
13113 {
13114 /* If the next token is a `,', then we are probably
13115 processing something like:
13116
13117 void f() {}, *p;
13118
13119 which is erroneous. */
13120 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13121 {
13122 cp_token *token = cp_lexer_peek_token (parser->lexer);
13123 error_at (token->location,
13124 "mixing"
13125 " declarations and function-definitions is forbidden");
13126 }
13127 /* Otherwise, we're done with the list of declarators. */
13128 else
13129 {
13130 pop_deferring_access_checks ();
13131 return;
13132 }
13133 }
13134 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13135 *maybe_range_for_decl = decl;
13136 /* The next token should be either a `,' or a `;'. */
13137 token = cp_lexer_peek_token (parser->lexer);
13138 /* If it's a `,', there are more declarators to come. */
13139 if (token->type == CPP_COMMA)
13140 /* will be consumed next time around */;
13141 /* If it's a `;', we are done. */
13142 else if (token->type == CPP_SEMICOLON)
13143 break;
13144 else if (maybe_range_for_decl)
13145 {
13146 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13147 permerror (decl_specifiers.locations[ds_type_spec],
13148 "types may not be defined in a for-range-declaration");
13149 break;
13150 }
13151 /* Anything else is an error. */
13152 else
13153 {
13154 /* If we have already issued an error message we don't need
13155 to issue another one. */
13156 if ((decl != error_mark_node
13157 && DECL_INITIAL (decl) != error_mark_node)
13158 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13159 cp_parser_error (parser, "expected %<,%> or %<;%>");
13160 /* Skip tokens until we reach the end of the statement. */
13161 cp_parser_skip_to_end_of_statement (parser);
13162 /* If the next token is now a `;', consume it. */
13163 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13164 cp_lexer_consume_token (parser->lexer);
13165 goto done;
13166 }
13167 /* After the first time around, a function-definition is not
13168 allowed -- even if it was OK at first. For example:
13169
13170 int i, f() {}
13171
13172 is not valid. */
13173 function_definition_allowed_p = false;
13174 }
13175
13176 /* Issue an error message if no declarators are present, and the
13177 decl-specifier-seq does not itself declare a class or
13178 enumeration: [dcl.dcl]/3. */
13179 if (!saw_declarator)
13180 {
13181 if (cp_parser_declares_only_class_p (parser))
13182 {
13183 if (!declares_class_or_enum
13184 && decl_specifiers.type
13185 && OVERLOAD_TYPE_P (decl_specifiers.type))
13186 /* Ensure an error is issued anyway when finish_decltype_type,
13187 called via cp_parser_decl_specifier_seq, returns a class or
13188 an enumeration (c++/51786). */
13189 decl_specifiers.type = NULL_TREE;
13190 shadow_tag (&decl_specifiers);
13191 }
13192 /* Perform any deferred access checks. */
13193 perform_deferred_access_checks (tf_warning_or_error);
13194 }
13195
13196 /* Consume the `;'. */
13197 finish:
13198 if (!maybe_range_for_decl)
13199 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13200 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13201 {
13202 if (init_loc != UNKNOWN_LOCATION)
13203 error_at (init_loc, "initializer in range-based %<for%> loop");
13204 if (comma_loc != UNKNOWN_LOCATION)
13205 error_at (comma_loc,
13206 "multiple declarations in range-based %<for%> loop");
13207 }
13208
13209 done:
13210 pop_deferring_access_checks ();
13211 }
13212
13213 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13214 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13215 initializer ; */
13216
13217 static tree
13218 cp_parser_decomposition_declaration (cp_parser *parser,
13219 cp_decl_specifier_seq *decl_specifiers,
13220 tree *maybe_range_for_decl,
13221 location_t *init_loc)
13222 {
13223 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13224 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13225 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13226
13227 /* Parse the identifier-list. */
13228 auto_vec<cp_expr, 10> v;
13229 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13230 while (true)
13231 {
13232 cp_expr e = cp_parser_identifier (parser);
13233 if (e.get_value () == error_mark_node)
13234 break;
13235 v.safe_push (e);
13236 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13237 break;
13238 cp_lexer_consume_token (parser->lexer);
13239 }
13240
13241 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13242 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13243 {
13244 end_loc = UNKNOWN_LOCATION;
13245 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13246 false);
13247 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13248 cp_lexer_consume_token (parser->lexer);
13249 else
13250 {
13251 cp_parser_skip_to_end_of_statement (parser);
13252 return error_mark_node;
13253 }
13254 }
13255
13256 if (cxx_dialect < cxx17)
13257 pedwarn (loc, 0, "structured bindings only available with "
13258 "-std=c++17 or -std=gnu++17");
13259
13260 tree pushed_scope;
13261 cp_declarator *declarator = make_declarator (cdk_decomp);
13262 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13263 declarator->id_loc = loc;
13264 if (ref_qual != REF_QUAL_NONE)
13265 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13266 ref_qual == REF_QUAL_RVALUE,
13267 NULL_TREE);
13268 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13269 NULL_TREE, decl_specifiers->attributes,
13270 &pushed_scope);
13271 tree orig_decl = decl;
13272
13273 unsigned int i;
13274 cp_expr e;
13275 cp_decl_specifier_seq decl_specs;
13276 clear_decl_specs (&decl_specs);
13277 decl_specs.type = make_auto ();
13278 tree prev = decl;
13279 FOR_EACH_VEC_ELT (v, i, e)
13280 {
13281 if (i == 0)
13282 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13283 else
13284 declarator->u.id.unqualified_name = e.get_value ();
13285 declarator->id_loc = e.get_location ();
13286 tree elt_pushed_scope;
13287 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13288 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13289 if (decl2 == error_mark_node)
13290 decl = error_mark_node;
13291 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13292 {
13293 /* Ensure we've diagnosed redeclaration if we aren't creating
13294 a new VAR_DECL. */
13295 gcc_assert (errorcount);
13296 decl = error_mark_node;
13297 }
13298 else
13299 prev = decl2;
13300 if (elt_pushed_scope)
13301 pop_scope (elt_pushed_scope);
13302 }
13303
13304 if (v.is_empty ())
13305 {
13306 error_at (loc, "empty structured binding declaration");
13307 decl = error_mark_node;
13308 }
13309
13310 if (maybe_range_for_decl == NULL
13311 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13312 {
13313 bool non_constant_p = false, is_direct_init = false;
13314 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13315 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13316 &non_constant_p);
13317 if (initializer == NULL_TREE
13318 || (TREE_CODE (initializer) == TREE_LIST
13319 && TREE_CHAIN (initializer))
13320 || (is_direct_init
13321 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13322 && CONSTRUCTOR_NELTS (initializer) != 1))
13323 {
13324 error_at (loc, "invalid initializer for structured binding "
13325 "declaration");
13326 initializer = error_mark_node;
13327 }
13328
13329 if (decl != error_mark_node)
13330 {
13331 cp_maybe_mangle_decomp (decl, prev, v.length ());
13332 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13333 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13334 cp_finish_decomp (decl, prev, v.length ());
13335 }
13336 }
13337 else if (decl != error_mark_node)
13338 {
13339 *maybe_range_for_decl = prev;
13340 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13341 the underlying DECL. */
13342 cp_finish_decomp (decl, prev, v.length ());
13343 }
13344
13345 if (pushed_scope)
13346 pop_scope (pushed_scope);
13347
13348 if (decl == error_mark_node && DECL_P (orig_decl))
13349 {
13350 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13351 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13352 }
13353
13354 return decl;
13355 }
13356
13357 /* Parse a decl-specifier-seq.
13358
13359 decl-specifier-seq:
13360 decl-specifier-seq [opt] decl-specifier
13361 decl-specifier attribute-specifier-seq [opt] (C++11)
13362
13363 decl-specifier:
13364 storage-class-specifier
13365 type-specifier
13366 function-specifier
13367 friend
13368 typedef
13369
13370 GNU Extension:
13371
13372 decl-specifier:
13373 attributes
13374
13375 Concepts Extension:
13376
13377 decl-specifier:
13378 concept
13379
13380 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13381
13382 The parser flags FLAGS is used to control type-specifier parsing.
13383
13384 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13385 flags:
13386
13387 1: one of the decl-specifiers is an elaborated-type-specifier
13388 (i.e., a type declaration)
13389 2: one of the decl-specifiers is an enum-specifier or a
13390 class-specifier (i.e., a type definition)
13391
13392 */
13393
13394 static void
13395 cp_parser_decl_specifier_seq (cp_parser* parser,
13396 cp_parser_flags flags,
13397 cp_decl_specifier_seq *decl_specs,
13398 int* declares_class_or_enum)
13399 {
13400 bool constructor_possible_p = !parser->in_declarator_p;
13401 bool found_decl_spec = false;
13402 cp_token *start_token = NULL;
13403 cp_decl_spec ds;
13404
13405 /* Clear DECL_SPECS. */
13406 clear_decl_specs (decl_specs);
13407
13408 /* Assume no class or enumeration type is declared. */
13409 *declares_class_or_enum = 0;
13410
13411 /* Keep reading specifiers until there are no more to read. */
13412 while (true)
13413 {
13414 bool constructor_p;
13415 cp_token *token;
13416 ds = ds_last;
13417
13418 /* Peek at the next token. */
13419 token = cp_lexer_peek_token (parser->lexer);
13420
13421 /* Save the first token of the decl spec list for error
13422 reporting. */
13423 if (!start_token)
13424 start_token = token;
13425 /* Handle attributes. */
13426 if (cp_next_tokens_can_be_attribute_p (parser))
13427 {
13428 /* Parse the attributes. */
13429 tree attrs = cp_parser_attributes_opt (parser);
13430
13431 /* In a sequence of declaration specifiers, c++11 attributes
13432 appertain to the type that precede them. In that case
13433 [dcl.spec]/1 says:
13434
13435 The attribute-specifier-seq affects the type only for
13436 the declaration it appears in, not other declarations
13437 involving the same type.
13438
13439 But for now let's force the user to position the
13440 attribute either at the beginning of the declaration or
13441 after the declarator-id, which would clearly mean that it
13442 applies to the declarator. */
13443 if (cxx11_attribute_p (attrs))
13444 {
13445 if (!found_decl_spec)
13446 /* The c++11 attribute is at the beginning of the
13447 declaration. It appertains to the entity being
13448 declared. */;
13449 else
13450 {
13451 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13452 {
13453 /* This is an attribute following a
13454 class-specifier. */
13455 if (decl_specs->type_definition_p)
13456 warn_misplaced_attr_for_class_type (token->location,
13457 decl_specs->type);
13458 attrs = NULL_TREE;
13459 }
13460 else
13461 {
13462 decl_specs->std_attributes
13463 = attr_chainon (decl_specs->std_attributes, attrs);
13464 if (decl_specs->locations[ds_std_attribute] == 0)
13465 decl_specs->locations[ds_std_attribute] = token->location;
13466 }
13467 continue;
13468 }
13469 }
13470
13471 decl_specs->attributes
13472 = attr_chainon (decl_specs->attributes, attrs);
13473 if (decl_specs->locations[ds_attribute] == 0)
13474 decl_specs->locations[ds_attribute] = token->location;
13475 continue;
13476 }
13477 /* Assume we will find a decl-specifier keyword. */
13478 found_decl_spec = true;
13479 /* If the next token is an appropriate keyword, we can simply
13480 add it to the list. */
13481 switch (token->keyword)
13482 {
13483 /* decl-specifier:
13484 friend
13485 constexpr */
13486 case RID_FRIEND:
13487 if (!at_class_scope_p ())
13488 {
13489 gcc_rich_location richloc (token->location);
13490 richloc.add_fixit_remove ();
13491 error_at (&richloc, "%<friend%> used outside of class");
13492 cp_lexer_purge_token (parser->lexer);
13493 }
13494 else
13495 {
13496 ds = ds_friend;
13497 /* Consume the token. */
13498 cp_lexer_consume_token (parser->lexer);
13499 }
13500 break;
13501
13502 case RID_CONSTEXPR:
13503 ds = ds_constexpr;
13504 cp_lexer_consume_token (parser->lexer);
13505 break;
13506
13507 case RID_CONCEPT:
13508 ds = ds_concept;
13509 cp_lexer_consume_token (parser->lexer);
13510 break;
13511
13512 /* function-specifier:
13513 inline
13514 virtual
13515 explicit */
13516 case RID_INLINE:
13517 case RID_VIRTUAL:
13518 case RID_EXPLICIT:
13519 cp_parser_function_specifier_opt (parser, decl_specs);
13520 break;
13521
13522 /* decl-specifier:
13523 typedef */
13524 case RID_TYPEDEF:
13525 ds = ds_typedef;
13526 /* Consume the token. */
13527 cp_lexer_consume_token (parser->lexer);
13528 /* A constructor declarator cannot appear in a typedef. */
13529 constructor_possible_p = false;
13530 /* The "typedef" keyword can only occur in a declaration; we
13531 may as well commit at this point. */
13532 cp_parser_commit_to_tentative_parse (parser);
13533
13534 if (decl_specs->storage_class != sc_none)
13535 decl_specs->conflicting_specifiers_p = true;
13536 break;
13537
13538 /* storage-class-specifier:
13539 auto
13540 register
13541 static
13542 extern
13543 mutable
13544
13545 GNU Extension:
13546 thread */
13547 case RID_AUTO:
13548 if (cxx_dialect == cxx98)
13549 {
13550 /* Consume the token. */
13551 cp_lexer_consume_token (parser->lexer);
13552
13553 /* Complain about `auto' as a storage specifier, if
13554 we're complaining about C++0x compatibility. */
13555 gcc_rich_location richloc (token->location);
13556 richloc.add_fixit_remove ();
13557 warning_at (&richloc, OPT_Wc__11_compat,
13558 "%<auto%> changes meaning in C++11; "
13559 "please remove it");
13560
13561 /* Set the storage class anyway. */
13562 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13563 token);
13564 }
13565 else
13566 /* C++0x auto type-specifier. */
13567 found_decl_spec = false;
13568 break;
13569
13570 case RID_REGISTER:
13571 case RID_STATIC:
13572 case RID_EXTERN:
13573 case RID_MUTABLE:
13574 /* Consume the token. */
13575 cp_lexer_consume_token (parser->lexer);
13576 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13577 token);
13578 break;
13579 case RID_THREAD:
13580 /* Consume the token. */
13581 ds = ds_thread;
13582 cp_lexer_consume_token (parser->lexer);
13583 break;
13584
13585 default:
13586 /* We did not yet find a decl-specifier yet. */
13587 found_decl_spec = false;
13588 break;
13589 }
13590
13591 if (found_decl_spec
13592 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13593 && token->keyword != RID_CONSTEXPR)
13594 error ("decl-specifier invalid in condition");
13595
13596 if (found_decl_spec
13597 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13598 && token->keyword != RID_MUTABLE
13599 && token->keyword != RID_CONSTEXPR)
13600 error_at (token->location, "%qD invalid in lambda",
13601 ridpointers[token->keyword]);
13602
13603 if (ds != ds_last)
13604 set_and_check_decl_spec_loc (decl_specs, ds, token);
13605
13606 /* Constructors are a special case. The `S' in `S()' is not a
13607 decl-specifier; it is the beginning of the declarator. */
13608 constructor_p
13609 = (!found_decl_spec
13610 && constructor_possible_p
13611 && (cp_parser_constructor_declarator_p
13612 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13613
13614 /* If we don't have a DECL_SPEC yet, then we must be looking at
13615 a type-specifier. */
13616 if (!found_decl_spec && !constructor_p)
13617 {
13618 int decl_spec_declares_class_or_enum;
13619 bool is_cv_qualifier;
13620 tree type_spec;
13621
13622 type_spec
13623 = cp_parser_type_specifier (parser, flags,
13624 decl_specs,
13625 /*is_declaration=*/true,
13626 &decl_spec_declares_class_or_enum,
13627 &is_cv_qualifier);
13628 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13629
13630 /* If this type-specifier referenced a user-defined type
13631 (a typedef, class-name, etc.), then we can't allow any
13632 more such type-specifiers henceforth.
13633
13634 [dcl.spec]
13635
13636 The longest sequence of decl-specifiers that could
13637 possibly be a type name is taken as the
13638 decl-specifier-seq of a declaration. The sequence shall
13639 be self-consistent as described below.
13640
13641 [dcl.type]
13642
13643 As a general rule, at most one type-specifier is allowed
13644 in the complete decl-specifier-seq of a declaration. The
13645 only exceptions are the following:
13646
13647 -- const or volatile can be combined with any other
13648 type-specifier.
13649
13650 -- signed or unsigned can be combined with char, long,
13651 short, or int.
13652
13653 -- ..
13654
13655 Example:
13656
13657 typedef char* Pc;
13658 void g (const int Pc);
13659
13660 Here, Pc is *not* part of the decl-specifier seq; it's
13661 the declarator. Therefore, once we see a type-specifier
13662 (other than a cv-qualifier), we forbid any additional
13663 user-defined types. We *do* still allow things like `int
13664 int' to be considered a decl-specifier-seq, and issue the
13665 error message later. */
13666 if (type_spec && !is_cv_qualifier)
13667 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13668 /* A constructor declarator cannot follow a type-specifier. */
13669 if (type_spec)
13670 {
13671 constructor_possible_p = false;
13672 found_decl_spec = true;
13673 if (!is_cv_qualifier)
13674 decl_specs->any_type_specifiers_p = true;
13675 }
13676 }
13677
13678 /* If we still do not have a DECL_SPEC, then there are no more
13679 decl-specifiers. */
13680 if (!found_decl_spec)
13681 break;
13682
13683 decl_specs->any_specifiers_p = true;
13684 /* After we see one decl-specifier, further decl-specifiers are
13685 always optional. */
13686 flags |= CP_PARSER_FLAGS_OPTIONAL;
13687 }
13688
13689 /* Don't allow a friend specifier with a class definition. */
13690 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13691 && (*declares_class_or_enum & 2))
13692 error_at (decl_specs->locations[ds_friend],
13693 "class definition may not be declared a friend");
13694 }
13695
13696 /* Parse an (optional) storage-class-specifier.
13697
13698 storage-class-specifier:
13699 auto
13700 register
13701 static
13702 extern
13703 mutable
13704
13705 GNU Extension:
13706
13707 storage-class-specifier:
13708 thread
13709
13710 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13711
13712 static tree
13713 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13714 {
13715 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13716 {
13717 case RID_AUTO:
13718 if (cxx_dialect != cxx98)
13719 return NULL_TREE;
13720 /* Fall through for C++98. */
13721 gcc_fallthrough ();
13722
13723 case RID_REGISTER:
13724 case RID_STATIC:
13725 case RID_EXTERN:
13726 case RID_MUTABLE:
13727 case RID_THREAD:
13728 /* Consume the token. */
13729 return cp_lexer_consume_token (parser->lexer)->u.value;
13730
13731 default:
13732 return NULL_TREE;
13733 }
13734 }
13735
13736 /* Parse an (optional) function-specifier.
13737
13738 function-specifier:
13739 inline
13740 virtual
13741 explicit
13742
13743 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13744 Updates DECL_SPECS, if it is non-NULL. */
13745
13746 static tree
13747 cp_parser_function_specifier_opt (cp_parser* parser,
13748 cp_decl_specifier_seq *decl_specs)
13749 {
13750 cp_token *token = cp_lexer_peek_token (parser->lexer);
13751 switch (token->keyword)
13752 {
13753 case RID_INLINE:
13754 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13755 break;
13756
13757 case RID_VIRTUAL:
13758 /* 14.5.2.3 [temp.mem]
13759
13760 A member function template shall not be virtual. */
13761 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13762 && current_class_type)
13763 error_at (token->location, "templates may not be %<virtual%>");
13764 else
13765 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13766 break;
13767
13768 case RID_EXPLICIT:
13769 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13770 break;
13771
13772 default:
13773 return NULL_TREE;
13774 }
13775
13776 /* Consume the token. */
13777 return cp_lexer_consume_token (parser->lexer)->u.value;
13778 }
13779
13780 /* Parse a linkage-specification.
13781
13782 linkage-specification:
13783 extern string-literal { declaration-seq [opt] }
13784 extern string-literal declaration */
13785
13786 static void
13787 cp_parser_linkage_specification (cp_parser* parser)
13788 {
13789 tree linkage;
13790
13791 /* Look for the `extern' keyword. */
13792 cp_token *extern_token
13793 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13794
13795 /* Look for the string-literal. */
13796 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13797 linkage = cp_parser_string_literal (parser, false, false);
13798
13799 /* Transform the literal into an identifier. If the literal is a
13800 wide-character string, or contains embedded NULs, then we can't
13801 handle it as the user wants. */
13802 if (strlen (TREE_STRING_POINTER (linkage))
13803 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13804 {
13805 cp_parser_error (parser, "invalid linkage-specification");
13806 /* Assume C++ linkage. */
13807 linkage = lang_name_cplusplus;
13808 }
13809 else
13810 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13811
13812 /* We're now using the new linkage. */
13813 push_lang_context (linkage);
13814
13815 /* Preserve the location of the the innermost linkage specification,
13816 tracking the locations of nested specifications via a local. */
13817 location_t saved_location
13818 = parser->innermost_linkage_specification_location;
13819 /* Construct a location ranging from the start of the "extern" to
13820 the end of the string-literal, with the caret at the start, e.g.:
13821 extern "C" {
13822 ^~~~~~~~~~
13823 */
13824 parser->innermost_linkage_specification_location
13825 = make_location (extern_token->location,
13826 extern_token->location,
13827 get_finish (string_token->location));
13828
13829 /* If the next token is a `{', then we're using the first
13830 production. */
13831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13832 {
13833 cp_ensure_no_omp_declare_simd (parser);
13834 cp_ensure_no_oacc_routine (parser);
13835
13836 /* Consume the `{' token. */
13837 matching_braces braces;
13838 braces.consume_open (parser)->location;
13839 /* Parse the declarations. */
13840 cp_parser_declaration_seq_opt (parser);
13841 /* Look for the closing `}'. */
13842 braces.require_close (parser);
13843 }
13844 /* Otherwise, there's just one declaration. */
13845 else
13846 {
13847 bool saved_in_unbraced_linkage_specification_p;
13848
13849 saved_in_unbraced_linkage_specification_p
13850 = parser->in_unbraced_linkage_specification_p;
13851 parser->in_unbraced_linkage_specification_p = true;
13852 cp_parser_declaration (parser);
13853 parser->in_unbraced_linkage_specification_p
13854 = saved_in_unbraced_linkage_specification_p;
13855 }
13856
13857 /* We're done with the linkage-specification. */
13858 pop_lang_context ();
13859
13860 /* Restore location of parent linkage specification, if any. */
13861 parser->innermost_linkage_specification_location = saved_location;
13862 }
13863
13864 /* Parse a static_assert-declaration.
13865
13866 static_assert-declaration:
13867 static_assert ( constant-expression , string-literal ) ;
13868 static_assert ( constant-expression ) ; (C++17)
13869
13870 If MEMBER_P, this static_assert is a class member. */
13871
13872 static void
13873 cp_parser_static_assert(cp_parser *parser, bool member_p)
13874 {
13875 cp_expr condition;
13876 location_t token_loc;
13877 tree message;
13878 bool dummy;
13879
13880 /* Peek at the `static_assert' token so we can keep track of exactly
13881 where the static assertion started. */
13882 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13883
13884 /* Look for the `static_assert' keyword. */
13885 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13886 RT_STATIC_ASSERT))
13887 return;
13888
13889 /* We know we are in a static assertion; commit to any tentative
13890 parse. */
13891 if (cp_parser_parsing_tentatively (parser))
13892 cp_parser_commit_to_tentative_parse (parser);
13893
13894 /* Parse the `(' starting the static assertion condition. */
13895 matching_parens parens;
13896 parens.require_open (parser);
13897
13898 /* Parse the constant-expression. Allow a non-constant expression
13899 here in order to give better diagnostics in finish_static_assert. */
13900 condition =
13901 cp_parser_constant_expression (parser,
13902 /*allow_non_constant_p=*/true,
13903 /*non_constant_p=*/&dummy);
13904
13905 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13906 {
13907 if (cxx_dialect < cxx17)
13908 pedwarn (input_location, OPT_Wpedantic,
13909 "static_assert without a message "
13910 "only available with -std=c++17 or -std=gnu++17");
13911 /* Eat the ')' */
13912 cp_lexer_consume_token (parser->lexer);
13913 message = build_string (1, "");
13914 TREE_TYPE (message) = char_array_type_node;
13915 fix_string_type (message);
13916 }
13917 else
13918 {
13919 /* Parse the separating `,'. */
13920 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13921
13922 /* Parse the string-literal message. */
13923 message = cp_parser_string_literal (parser,
13924 /*translate=*/false,
13925 /*wide_ok=*/true);
13926
13927 /* A `)' completes the static assertion. */
13928 if (!parens.require_close (parser))
13929 cp_parser_skip_to_closing_parenthesis (parser,
13930 /*recovering=*/true,
13931 /*or_comma=*/false,
13932 /*consume_paren=*/true);
13933 }
13934
13935 /* A semicolon terminates the declaration. */
13936 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13937
13938 /* Get the location for the static assertion. Use that of the
13939 condition if available, otherwise, use that of the "static_assert"
13940 token. */
13941 location_t assert_loc = condition.get_location ();
13942 if (assert_loc == UNKNOWN_LOCATION)
13943 assert_loc = token_loc;
13944
13945 /* Complete the static assertion, which may mean either processing
13946 the static assert now or saving it for template instantiation. */
13947 finish_static_assert (condition, message, assert_loc, member_p);
13948 }
13949
13950 /* Parse the expression in decltype ( expression ). */
13951
13952 static tree
13953 cp_parser_decltype_expr (cp_parser *parser,
13954 bool &id_expression_or_member_access_p)
13955 {
13956 cp_token *id_expr_start_token;
13957 tree expr;
13958
13959 /* Since we're going to preserve any side-effects from this parse, set up a
13960 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13961 in the expression. */
13962 tentative_firewall firewall (parser);
13963
13964 /* First, try parsing an id-expression. */
13965 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13966 cp_parser_parse_tentatively (parser);
13967 expr = cp_parser_id_expression (parser,
13968 /*template_keyword_p=*/false,
13969 /*check_dependency_p=*/true,
13970 /*template_p=*/NULL,
13971 /*declarator_p=*/false,
13972 /*optional_p=*/false);
13973
13974 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13975 {
13976 bool non_integral_constant_expression_p = false;
13977 tree id_expression = expr;
13978 cp_id_kind idk;
13979 const char *error_msg;
13980
13981 if (identifier_p (expr))
13982 /* Lookup the name we got back from the id-expression. */
13983 expr = cp_parser_lookup_name_simple (parser, expr,
13984 id_expr_start_token->location);
13985
13986 if (expr
13987 && expr != error_mark_node
13988 && TREE_CODE (expr) != TYPE_DECL
13989 && (TREE_CODE (expr) != BIT_NOT_EXPR
13990 || !TYPE_P (TREE_OPERAND (expr, 0)))
13991 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13992 {
13993 /* Complete lookup of the id-expression. */
13994 expr = (finish_id_expression
13995 (id_expression, expr, parser->scope, &idk,
13996 /*integral_constant_expression_p=*/false,
13997 /*allow_non_integral_constant_expression_p=*/true,
13998 &non_integral_constant_expression_p,
13999 /*template_p=*/false,
14000 /*done=*/true,
14001 /*address_p=*/false,
14002 /*template_arg_p=*/false,
14003 &error_msg,
14004 id_expr_start_token->location));
14005
14006 if (expr == error_mark_node)
14007 /* We found an id-expression, but it was something that we
14008 should not have found. This is an error, not something
14009 we can recover from, so note that we found an
14010 id-expression and we'll recover as gracefully as
14011 possible. */
14012 id_expression_or_member_access_p = true;
14013 }
14014
14015 if (expr
14016 && expr != error_mark_node
14017 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14018 /* We have an id-expression. */
14019 id_expression_or_member_access_p = true;
14020 }
14021
14022 if (!id_expression_or_member_access_p)
14023 {
14024 /* Abort the id-expression parse. */
14025 cp_parser_abort_tentative_parse (parser);
14026
14027 /* Parsing tentatively, again. */
14028 cp_parser_parse_tentatively (parser);
14029
14030 /* Parse a class member access. */
14031 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14032 /*cast_p=*/false, /*decltype*/true,
14033 /*member_access_only_p=*/true, NULL);
14034
14035 if (expr
14036 && expr != error_mark_node
14037 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14038 /* We have an id-expression. */
14039 id_expression_or_member_access_p = true;
14040 }
14041
14042 if (id_expression_or_member_access_p)
14043 /* We have parsed the complete id-expression or member access. */
14044 cp_parser_parse_definitely (parser);
14045 else
14046 {
14047 /* Abort our attempt to parse an id-expression or member access
14048 expression. */
14049 cp_parser_abort_tentative_parse (parser);
14050
14051 /* Parse a full expression. */
14052 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14053 /*decltype_p=*/true);
14054 }
14055
14056 return expr;
14057 }
14058
14059 /* Parse a `decltype' type. Returns the type.
14060
14061 simple-type-specifier:
14062 decltype ( expression )
14063 C++14 proposal:
14064 decltype ( auto ) */
14065
14066 static tree
14067 cp_parser_decltype (cp_parser *parser)
14068 {
14069 bool id_expression_or_member_access_p = false;
14070 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14071
14072 if (start_token->type == CPP_DECLTYPE)
14073 {
14074 /* Already parsed. */
14075 cp_lexer_consume_token (parser->lexer);
14076 return saved_checks_value (start_token->u.tree_check_value);
14077 }
14078
14079 /* Look for the `decltype' token. */
14080 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14081 return error_mark_node;
14082
14083 /* Parse the opening `('. */
14084 matching_parens parens;
14085 if (!parens.require_open (parser))
14086 return error_mark_node;
14087
14088 push_deferring_access_checks (dk_deferred);
14089
14090 tree expr = NULL_TREE;
14091
14092 if (cxx_dialect >= cxx14
14093 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14094 /* decltype (auto) */
14095 cp_lexer_consume_token (parser->lexer);
14096 else
14097 {
14098 /* decltype (expression) */
14099
14100 /* Types cannot be defined in a `decltype' expression. Save away the
14101 old message and set the new one. */
14102 const char *saved_message = parser->type_definition_forbidden_message;
14103 parser->type_definition_forbidden_message
14104 = G_("types may not be defined in %<decltype%> expressions");
14105
14106 /* The restrictions on constant-expressions do not apply inside
14107 decltype expressions. */
14108 bool saved_integral_constant_expression_p
14109 = parser->integral_constant_expression_p;
14110 bool saved_non_integral_constant_expression_p
14111 = parser->non_integral_constant_expression_p;
14112 parser->integral_constant_expression_p = false;
14113
14114 /* Within a parenthesized expression, a `>' token is always
14115 the greater-than operator. */
14116 bool saved_greater_than_is_operator_p
14117 = parser->greater_than_is_operator_p;
14118 parser->greater_than_is_operator_p = true;
14119
14120 /* Do not actually evaluate the expression. */
14121 ++cp_unevaluated_operand;
14122
14123 /* Do not warn about problems with the expression. */
14124 ++c_inhibit_evaluation_warnings;
14125
14126 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14127
14128 /* Go back to evaluating expressions. */
14129 --cp_unevaluated_operand;
14130 --c_inhibit_evaluation_warnings;
14131
14132 /* The `>' token might be the end of a template-id or
14133 template-parameter-list now. */
14134 parser->greater_than_is_operator_p
14135 = saved_greater_than_is_operator_p;
14136
14137 /* Restore the old message and the integral constant expression
14138 flags. */
14139 parser->type_definition_forbidden_message = saved_message;
14140 parser->integral_constant_expression_p
14141 = saved_integral_constant_expression_p;
14142 parser->non_integral_constant_expression_p
14143 = saved_non_integral_constant_expression_p;
14144 }
14145
14146 /* Parse to the closing `)'. */
14147 if (!parens.require_close (parser))
14148 {
14149 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14150 /*consume_paren=*/true);
14151 pop_deferring_access_checks ();
14152 return error_mark_node;
14153 }
14154
14155 if (!expr)
14156 {
14157 /* Build auto. */
14158 expr = make_decltype_auto ();
14159 AUTO_IS_DECLTYPE (expr) = true;
14160 }
14161 else
14162 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14163 tf_warning_or_error);
14164
14165 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14166 it again. */
14167 start_token->type = CPP_DECLTYPE;
14168 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14169 start_token->u.tree_check_value->value = expr;
14170 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14171 start_token->keyword = RID_MAX;
14172 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14173
14174 pop_to_parent_deferring_access_checks ();
14175
14176 return expr;
14177 }
14178
14179 /* Special member functions [gram.special] */
14180
14181 /* Parse a conversion-function-id.
14182
14183 conversion-function-id:
14184 operator conversion-type-id
14185
14186 Returns an IDENTIFIER_NODE representing the operator. */
14187
14188 static tree
14189 cp_parser_conversion_function_id (cp_parser* parser)
14190 {
14191 tree type;
14192 tree saved_scope;
14193 tree saved_qualifying_scope;
14194 tree saved_object_scope;
14195 tree pushed_scope = NULL_TREE;
14196
14197 /* Look for the `operator' token. */
14198 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14199 return error_mark_node;
14200 /* When we parse the conversion-type-id, the current scope will be
14201 reset. However, we need that information in able to look up the
14202 conversion function later, so we save it here. */
14203 saved_scope = parser->scope;
14204 saved_qualifying_scope = parser->qualifying_scope;
14205 saved_object_scope = parser->object_scope;
14206 /* We must enter the scope of the class so that the names of
14207 entities declared within the class are available in the
14208 conversion-type-id. For example, consider:
14209
14210 struct S {
14211 typedef int I;
14212 operator I();
14213 };
14214
14215 S::operator I() { ... }
14216
14217 In order to see that `I' is a type-name in the definition, we
14218 must be in the scope of `S'. */
14219 if (saved_scope)
14220 pushed_scope = push_scope (saved_scope);
14221 /* Parse the conversion-type-id. */
14222 type = cp_parser_conversion_type_id (parser);
14223 /* Leave the scope of the class, if any. */
14224 if (pushed_scope)
14225 pop_scope (pushed_scope);
14226 /* Restore the saved scope. */
14227 parser->scope = saved_scope;
14228 parser->qualifying_scope = saved_qualifying_scope;
14229 parser->object_scope = saved_object_scope;
14230 /* If the TYPE is invalid, indicate failure. */
14231 if (type == error_mark_node)
14232 return error_mark_node;
14233 return make_conv_op_name (type);
14234 }
14235
14236 /* Parse a conversion-type-id:
14237
14238 conversion-type-id:
14239 type-specifier-seq conversion-declarator [opt]
14240
14241 Returns the TYPE specified. */
14242
14243 static tree
14244 cp_parser_conversion_type_id (cp_parser* parser)
14245 {
14246 tree attributes;
14247 cp_decl_specifier_seq type_specifiers;
14248 cp_declarator *declarator;
14249 tree type_specified;
14250 const char *saved_message;
14251
14252 /* Parse the attributes. */
14253 attributes = cp_parser_attributes_opt (parser);
14254
14255 saved_message = parser->type_definition_forbidden_message;
14256 parser->type_definition_forbidden_message
14257 = G_("types may not be defined in a conversion-type-id");
14258
14259 /* Parse the type-specifiers. */
14260 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14261 /*is_trailing_return=*/false,
14262 &type_specifiers);
14263
14264 parser->type_definition_forbidden_message = saved_message;
14265
14266 /* If that didn't work, stop. */
14267 if (type_specifiers.type == error_mark_node)
14268 return error_mark_node;
14269 /* Parse the conversion-declarator. */
14270 declarator = cp_parser_conversion_declarator_opt (parser);
14271
14272 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14273 /*initialized=*/0, &attributes);
14274 if (attributes)
14275 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14276
14277 /* Don't give this error when parsing tentatively. This happens to
14278 work because we always parse this definitively once. */
14279 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14280 && type_uses_auto (type_specified))
14281 {
14282 if (cxx_dialect < cxx14)
14283 {
14284 error ("invalid use of %<auto%> in conversion operator");
14285 return error_mark_node;
14286 }
14287 else if (template_parm_scope_p ())
14288 warning (0, "use of %<auto%> in member template "
14289 "conversion operator can never be deduced");
14290 }
14291
14292 return type_specified;
14293 }
14294
14295 /* Parse an (optional) conversion-declarator.
14296
14297 conversion-declarator:
14298 ptr-operator conversion-declarator [opt]
14299
14300 */
14301
14302 static cp_declarator *
14303 cp_parser_conversion_declarator_opt (cp_parser* parser)
14304 {
14305 enum tree_code code;
14306 tree class_type, std_attributes = NULL_TREE;
14307 cp_cv_quals cv_quals;
14308
14309 /* We don't know if there's a ptr-operator next, or not. */
14310 cp_parser_parse_tentatively (parser);
14311 /* Try the ptr-operator. */
14312 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14313 &std_attributes);
14314 /* If it worked, look for more conversion-declarators. */
14315 if (cp_parser_parse_definitely (parser))
14316 {
14317 cp_declarator *declarator;
14318
14319 /* Parse another optional declarator. */
14320 declarator = cp_parser_conversion_declarator_opt (parser);
14321
14322 declarator = cp_parser_make_indirect_declarator
14323 (code, class_type, cv_quals, declarator, std_attributes);
14324
14325 return declarator;
14326 }
14327
14328 return NULL;
14329 }
14330
14331 /* Parse an (optional) ctor-initializer.
14332
14333 ctor-initializer:
14334 : mem-initializer-list */
14335
14336 static void
14337 cp_parser_ctor_initializer_opt (cp_parser* parser)
14338 {
14339 /* If the next token is not a `:', then there is no
14340 ctor-initializer. */
14341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14342 {
14343 /* Do default initialization of any bases and members. */
14344 if (DECL_CONSTRUCTOR_P (current_function_decl))
14345 finish_mem_initializers (NULL_TREE);
14346 return;
14347 }
14348
14349 /* Consume the `:' token. */
14350 cp_lexer_consume_token (parser->lexer);
14351 /* And the mem-initializer-list. */
14352 cp_parser_mem_initializer_list (parser);
14353 }
14354
14355 /* Parse a mem-initializer-list.
14356
14357 mem-initializer-list:
14358 mem-initializer ... [opt]
14359 mem-initializer ... [opt] , mem-initializer-list */
14360
14361 static void
14362 cp_parser_mem_initializer_list (cp_parser* parser)
14363 {
14364 tree mem_initializer_list = NULL_TREE;
14365 tree target_ctor = error_mark_node;
14366 cp_token *token = cp_lexer_peek_token (parser->lexer);
14367
14368 /* Let the semantic analysis code know that we are starting the
14369 mem-initializer-list. */
14370 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14371 error_at (token->location,
14372 "only constructors take member initializers");
14373
14374 /* Loop through the list. */
14375 while (true)
14376 {
14377 tree mem_initializer;
14378
14379 token = cp_lexer_peek_token (parser->lexer);
14380 /* Parse the mem-initializer. */
14381 mem_initializer = cp_parser_mem_initializer (parser);
14382 /* If the next token is a `...', we're expanding member initializers. */
14383 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14384 {
14385 /* Consume the `...'. */
14386 cp_lexer_consume_token (parser->lexer);
14387
14388 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14389 can be expanded but members cannot. */
14390 if (mem_initializer != error_mark_node
14391 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14392 {
14393 error_at (token->location,
14394 "cannot expand initializer for member %qD",
14395 TREE_PURPOSE (mem_initializer));
14396 mem_initializer = error_mark_node;
14397 }
14398
14399 /* Construct the pack expansion type. */
14400 if (mem_initializer != error_mark_node)
14401 mem_initializer = make_pack_expansion (mem_initializer);
14402 }
14403 if (target_ctor != error_mark_node
14404 && mem_initializer != error_mark_node)
14405 {
14406 error ("mem-initializer for %qD follows constructor delegation",
14407 TREE_PURPOSE (mem_initializer));
14408 mem_initializer = error_mark_node;
14409 }
14410 /* Look for a target constructor. */
14411 if (mem_initializer != error_mark_node
14412 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14413 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14414 {
14415 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14416 if (mem_initializer_list)
14417 {
14418 error ("constructor delegation follows mem-initializer for %qD",
14419 TREE_PURPOSE (mem_initializer_list));
14420 mem_initializer = error_mark_node;
14421 }
14422 target_ctor = mem_initializer;
14423 }
14424 /* Add it to the list, unless it was erroneous. */
14425 if (mem_initializer != error_mark_node)
14426 {
14427 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14428 mem_initializer_list = mem_initializer;
14429 }
14430 /* If the next token is not a `,', we're done. */
14431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14432 break;
14433 /* Consume the `,' token. */
14434 cp_lexer_consume_token (parser->lexer);
14435 }
14436
14437 /* Perform semantic analysis. */
14438 if (DECL_CONSTRUCTOR_P (current_function_decl))
14439 finish_mem_initializers (mem_initializer_list);
14440 }
14441
14442 /* Parse a mem-initializer.
14443
14444 mem-initializer:
14445 mem-initializer-id ( expression-list [opt] )
14446 mem-initializer-id braced-init-list
14447
14448 GNU extension:
14449
14450 mem-initializer:
14451 ( expression-list [opt] )
14452
14453 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14454 class) or FIELD_DECL (for a non-static data member) to initialize;
14455 the TREE_VALUE is the expression-list. An empty initialization
14456 list is represented by void_list_node. */
14457
14458 static tree
14459 cp_parser_mem_initializer (cp_parser* parser)
14460 {
14461 tree mem_initializer_id;
14462 tree expression_list;
14463 tree member;
14464 cp_token *token = cp_lexer_peek_token (parser->lexer);
14465
14466 /* Find out what is being initialized. */
14467 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14468 {
14469 permerror (token->location,
14470 "anachronistic old-style base class initializer");
14471 mem_initializer_id = NULL_TREE;
14472 }
14473 else
14474 {
14475 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14476 if (mem_initializer_id == error_mark_node)
14477 return mem_initializer_id;
14478 }
14479 member = expand_member_init (mem_initializer_id);
14480 if (member && !DECL_P (member))
14481 in_base_initializer = 1;
14482
14483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14484 {
14485 bool expr_non_constant_p;
14486 cp_lexer_set_source_position (parser->lexer);
14487 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14488 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14489 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14490 expression_list = build_tree_list (NULL_TREE, expression_list);
14491 }
14492 else
14493 {
14494 vec<tree, va_gc> *vec;
14495 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14496 /*cast_p=*/false,
14497 /*allow_expansion_p=*/true,
14498 /*non_constant_p=*/NULL);
14499 if (vec == NULL)
14500 return error_mark_node;
14501 expression_list = build_tree_list_vec (vec);
14502 release_tree_vector (vec);
14503 }
14504
14505 if (expression_list == error_mark_node)
14506 return error_mark_node;
14507 if (!expression_list)
14508 expression_list = void_type_node;
14509
14510 in_base_initializer = 0;
14511
14512 return member ? build_tree_list (member, expression_list) : error_mark_node;
14513 }
14514
14515 /* Parse a mem-initializer-id.
14516
14517 mem-initializer-id:
14518 :: [opt] nested-name-specifier [opt] class-name
14519 decltype-specifier (C++11)
14520 identifier
14521
14522 Returns a TYPE indicating the class to be initialized for the first
14523 production (and the second in C++11). Returns an IDENTIFIER_NODE
14524 indicating the data member to be initialized for the last production. */
14525
14526 static tree
14527 cp_parser_mem_initializer_id (cp_parser* parser)
14528 {
14529 bool global_scope_p;
14530 bool nested_name_specifier_p;
14531 bool template_p = false;
14532 tree id;
14533
14534 cp_token *token = cp_lexer_peek_token (parser->lexer);
14535
14536 /* `typename' is not allowed in this context ([temp.res]). */
14537 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14538 {
14539 error_at (token->location,
14540 "keyword %<typename%> not allowed in this context (a qualified "
14541 "member initializer is implicitly a type)");
14542 cp_lexer_consume_token (parser->lexer);
14543 }
14544 /* Look for the optional `::' operator. */
14545 global_scope_p
14546 = (cp_parser_global_scope_opt (parser,
14547 /*current_scope_valid_p=*/false)
14548 != NULL_TREE);
14549 /* Look for the optional nested-name-specifier. The simplest way to
14550 implement:
14551
14552 [temp.res]
14553
14554 The keyword `typename' is not permitted in a base-specifier or
14555 mem-initializer; in these contexts a qualified name that
14556 depends on a template-parameter is implicitly assumed to be a
14557 type name.
14558
14559 is to assume that we have seen the `typename' keyword at this
14560 point. */
14561 nested_name_specifier_p
14562 = (cp_parser_nested_name_specifier_opt (parser,
14563 /*typename_keyword_p=*/true,
14564 /*check_dependency_p=*/true,
14565 /*type_p=*/true,
14566 /*is_declaration=*/true)
14567 != NULL_TREE);
14568 if (nested_name_specifier_p)
14569 template_p = cp_parser_optional_template_keyword (parser);
14570 /* If there is a `::' operator or a nested-name-specifier, then we
14571 are definitely looking for a class-name. */
14572 if (global_scope_p || nested_name_specifier_p)
14573 return cp_parser_class_name (parser,
14574 /*typename_keyword_p=*/true,
14575 /*template_keyword_p=*/template_p,
14576 typename_type,
14577 /*check_dependency_p=*/true,
14578 /*class_head_p=*/false,
14579 /*is_declaration=*/true);
14580 /* Otherwise, we could also be looking for an ordinary identifier. */
14581 cp_parser_parse_tentatively (parser);
14582 if (cp_lexer_next_token_is_decltype (parser->lexer))
14583 /* Try a decltype-specifier. */
14584 id = cp_parser_decltype (parser);
14585 else
14586 /* Otherwise, try a class-name. */
14587 id = cp_parser_class_name (parser,
14588 /*typename_keyword_p=*/true,
14589 /*template_keyword_p=*/false,
14590 none_type,
14591 /*check_dependency_p=*/true,
14592 /*class_head_p=*/false,
14593 /*is_declaration=*/true);
14594 /* If we found one, we're done. */
14595 if (cp_parser_parse_definitely (parser))
14596 return id;
14597 /* Otherwise, look for an ordinary identifier. */
14598 return cp_parser_identifier (parser);
14599 }
14600
14601 /* Overloading [gram.over] */
14602
14603 /* Parse an operator-function-id.
14604
14605 operator-function-id:
14606 operator operator
14607
14608 Returns an IDENTIFIER_NODE for the operator which is a
14609 human-readable spelling of the identifier, e.g., `operator +'. */
14610
14611 static cp_expr
14612 cp_parser_operator_function_id (cp_parser* parser)
14613 {
14614 /* Look for the `operator' keyword. */
14615 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14616 return error_mark_node;
14617 /* And then the name of the operator itself. */
14618 return cp_parser_operator (parser);
14619 }
14620
14621 /* Return an identifier node for a user-defined literal operator.
14622 The suffix identifier is chained to the operator name identifier. */
14623
14624 tree
14625 cp_literal_operator_id (const char* name)
14626 {
14627 tree identifier;
14628 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14629 + strlen (name) + 10);
14630 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14631 identifier = get_identifier (buffer);
14632
14633 return identifier;
14634 }
14635
14636 /* Parse an operator.
14637
14638 operator:
14639 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14640 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14641 || ++ -- , ->* -> () []
14642
14643 GNU Extensions:
14644
14645 operator:
14646 <? >? <?= >?=
14647
14648 Returns an IDENTIFIER_NODE for the operator which is a
14649 human-readable spelling of the identifier, e.g., `operator +'. */
14650
14651 static cp_expr
14652 cp_parser_operator (cp_parser* parser)
14653 {
14654 tree id = NULL_TREE;
14655 cp_token *token;
14656 bool utf8 = false;
14657
14658 /* Peek at the next token. */
14659 token = cp_lexer_peek_token (parser->lexer);
14660
14661 location_t start_loc = token->location;
14662
14663 /* Figure out which operator we have. */
14664 enum tree_code op = ERROR_MARK;
14665 bool assop = false;
14666 bool consumed = false;
14667 switch (token->type)
14668 {
14669 case CPP_KEYWORD:
14670 {
14671 /* The keyword should be either `new' or `delete'. */
14672 if (token->keyword == RID_NEW)
14673 op = NEW_EXPR;
14674 else if (token->keyword == RID_DELETE)
14675 op = DELETE_EXPR;
14676 else
14677 break;
14678
14679 /* Consume the `new' or `delete' token. */
14680 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14681
14682 /* Peek at the next token. */
14683 token = cp_lexer_peek_token (parser->lexer);
14684 /* If it's a `[' token then this is the array variant of the
14685 operator. */
14686 if (token->type == CPP_OPEN_SQUARE)
14687 {
14688 /* Consume the `[' token. */
14689 cp_lexer_consume_token (parser->lexer);
14690 /* Look for the `]' token. */
14691 if (cp_token *close_token
14692 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14693 end_loc = close_token->location;
14694 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14695 }
14696 start_loc = make_location (start_loc, start_loc, end_loc);
14697 consumed = true;
14698 break;
14699 }
14700
14701 case CPP_PLUS:
14702 op = PLUS_EXPR;
14703 break;
14704
14705 case CPP_MINUS:
14706 op = MINUS_EXPR;
14707 break;
14708
14709 case CPP_MULT:
14710 op = MULT_EXPR;
14711 break;
14712
14713 case CPP_DIV:
14714 op = TRUNC_DIV_EXPR;
14715 break;
14716
14717 case CPP_MOD:
14718 op = TRUNC_MOD_EXPR;
14719 break;
14720
14721 case CPP_XOR:
14722 op = BIT_XOR_EXPR;
14723 break;
14724
14725 case CPP_AND:
14726 op = BIT_AND_EXPR;
14727 break;
14728
14729 case CPP_OR:
14730 op = BIT_IOR_EXPR;
14731 break;
14732
14733 case CPP_COMPL:
14734 op = BIT_NOT_EXPR;
14735 break;
14736
14737 case CPP_NOT:
14738 op = TRUTH_NOT_EXPR;
14739 break;
14740
14741 case CPP_EQ:
14742 assop = true;
14743 op = NOP_EXPR;
14744 break;
14745
14746 case CPP_LESS:
14747 op = LT_EXPR;
14748 break;
14749
14750 case CPP_GREATER:
14751 op = GT_EXPR;
14752 break;
14753
14754 case CPP_PLUS_EQ:
14755 assop = true;
14756 op = PLUS_EXPR;
14757 break;
14758
14759 case CPP_MINUS_EQ:
14760 assop = true;
14761 op = MINUS_EXPR;
14762 break;
14763
14764 case CPP_MULT_EQ:
14765 assop = true;
14766 op = MULT_EXPR;
14767 break;
14768
14769 case CPP_DIV_EQ:
14770 assop = true;
14771 op = TRUNC_DIV_EXPR;
14772 break;
14773
14774 case CPP_MOD_EQ:
14775 assop = true;
14776 op = TRUNC_MOD_EXPR;
14777 break;
14778
14779 case CPP_XOR_EQ:
14780 assop = true;
14781 op = BIT_XOR_EXPR;
14782 break;
14783
14784 case CPP_AND_EQ:
14785 assop = true;
14786 op = BIT_AND_EXPR;
14787 break;
14788
14789 case CPP_OR_EQ:
14790 assop = true;
14791 op = BIT_IOR_EXPR;
14792 break;
14793
14794 case CPP_LSHIFT:
14795 op = LSHIFT_EXPR;
14796 break;
14797
14798 case CPP_RSHIFT:
14799 op = RSHIFT_EXPR;
14800 break;
14801
14802 case CPP_LSHIFT_EQ:
14803 assop = true;
14804 op = LSHIFT_EXPR;
14805 break;
14806
14807 case CPP_RSHIFT_EQ:
14808 assop = true;
14809 op = RSHIFT_EXPR;
14810 break;
14811
14812 case CPP_EQ_EQ:
14813 op = EQ_EXPR;
14814 break;
14815
14816 case CPP_NOT_EQ:
14817 op = NE_EXPR;
14818 break;
14819
14820 case CPP_LESS_EQ:
14821 op = LE_EXPR;
14822 break;
14823
14824 case CPP_GREATER_EQ:
14825 op = GE_EXPR;
14826 break;
14827
14828 case CPP_AND_AND:
14829 op = TRUTH_ANDIF_EXPR;
14830 break;
14831
14832 case CPP_OR_OR:
14833 op = TRUTH_ORIF_EXPR;
14834 break;
14835
14836 case CPP_PLUS_PLUS:
14837 op = POSTINCREMENT_EXPR;
14838 break;
14839
14840 case CPP_MINUS_MINUS:
14841 op = PREDECREMENT_EXPR;
14842 break;
14843
14844 case CPP_COMMA:
14845 op = COMPOUND_EXPR;
14846 break;
14847
14848 case CPP_DEREF_STAR:
14849 op = MEMBER_REF;
14850 break;
14851
14852 case CPP_DEREF:
14853 op = COMPONENT_REF;
14854 break;
14855
14856 case CPP_OPEN_PAREN:
14857 {
14858 /* Consume the `('. */
14859 matching_parens parens;
14860 parens.consume_open (parser);
14861 /* Look for the matching `)'. */
14862 parens.require_close (parser);
14863 op = CALL_EXPR;
14864 consumed = true;
14865 break;
14866 }
14867
14868 case CPP_OPEN_SQUARE:
14869 /* Consume the `['. */
14870 cp_lexer_consume_token (parser->lexer);
14871 /* Look for the matching `]'. */
14872 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14873 op = ARRAY_REF;
14874 consumed = true;
14875 break;
14876
14877 case CPP_UTF8STRING:
14878 case CPP_UTF8STRING_USERDEF:
14879 utf8 = true;
14880 /* FALLTHRU */
14881 case CPP_STRING:
14882 case CPP_WSTRING:
14883 case CPP_STRING16:
14884 case CPP_STRING32:
14885 case CPP_STRING_USERDEF:
14886 case CPP_WSTRING_USERDEF:
14887 case CPP_STRING16_USERDEF:
14888 case CPP_STRING32_USERDEF:
14889 {
14890 tree str, string_tree;
14891 int sz, len;
14892
14893 if (cxx_dialect == cxx98)
14894 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14895
14896 /* Consume the string. */
14897 str = cp_parser_string_literal (parser, /*translate=*/true,
14898 /*wide_ok=*/true, /*lookup_udlit=*/false);
14899 if (str == error_mark_node)
14900 return error_mark_node;
14901 else if (TREE_CODE (str) == USERDEF_LITERAL)
14902 {
14903 string_tree = USERDEF_LITERAL_VALUE (str);
14904 id = USERDEF_LITERAL_SUFFIX_ID (str);
14905 }
14906 else
14907 {
14908 string_tree = str;
14909 /* Look for the suffix identifier. */
14910 token = cp_lexer_peek_token (parser->lexer);
14911 if (token->type == CPP_NAME)
14912 id = cp_parser_identifier (parser);
14913 else if (token->type == CPP_KEYWORD)
14914 {
14915 error ("unexpected keyword;"
14916 " remove space between quotes and suffix identifier");
14917 return error_mark_node;
14918 }
14919 else
14920 {
14921 error ("expected suffix identifier");
14922 return error_mark_node;
14923 }
14924 }
14925 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14926 (TREE_TYPE (TREE_TYPE (string_tree))));
14927 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14928 if (len != 0)
14929 {
14930 error ("expected empty string after %<operator%> keyword");
14931 return error_mark_node;
14932 }
14933 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14934 != char_type_node)
14935 {
14936 error ("invalid encoding prefix in literal operator");
14937 return error_mark_node;
14938 }
14939 if (id != error_mark_node)
14940 {
14941 const char *name = IDENTIFIER_POINTER (id);
14942 id = cp_literal_operator_id (name);
14943 }
14944 return id;
14945 }
14946
14947 default:
14948 /* Anything else is an error. */
14949 break;
14950 }
14951
14952 /* If we have selected an identifier, we need to consume the
14953 operator token. */
14954 if (op != ERROR_MARK)
14955 {
14956 id = ovl_op_identifier (assop, op);
14957 if (!consumed)
14958 cp_lexer_consume_token (parser->lexer);
14959 }
14960 /* Otherwise, no valid operator name was present. */
14961 else
14962 {
14963 cp_parser_error (parser, "expected operator");
14964 id = error_mark_node;
14965 }
14966
14967 return cp_expr (id, start_loc);
14968 }
14969
14970 /* Parse a template-declaration.
14971
14972 template-declaration:
14973 export [opt] template < template-parameter-list > declaration
14974
14975 If MEMBER_P is TRUE, this template-declaration occurs within a
14976 class-specifier.
14977
14978 The grammar rule given by the standard isn't correct. What
14979 is really meant is:
14980
14981 template-declaration:
14982 export [opt] template-parameter-list-seq
14983 decl-specifier-seq [opt] init-declarator [opt] ;
14984 export [opt] template-parameter-list-seq
14985 function-definition
14986
14987 template-parameter-list-seq:
14988 template-parameter-list-seq [opt]
14989 template < template-parameter-list >
14990
14991 Concept Extensions:
14992
14993 template-parameter-list-seq:
14994 template < template-parameter-list > requires-clause [opt]
14995
14996 requires-clause:
14997 requires logical-or-expression */
14998
14999 static void
15000 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15001 {
15002 /* Check for `export'. */
15003 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15004 {
15005 /* Consume the `export' token. */
15006 cp_lexer_consume_token (parser->lexer);
15007 /* Warn that we do not support `export'. */
15008 warning (0, "keyword %<export%> not implemented, and will be ignored");
15009 }
15010
15011 cp_parser_template_declaration_after_export (parser, member_p);
15012 }
15013
15014 /* Parse a template-parameter-list.
15015
15016 template-parameter-list:
15017 template-parameter
15018 template-parameter-list , template-parameter
15019
15020 Returns a TREE_LIST. Each node represents a template parameter.
15021 The nodes are connected via their TREE_CHAINs. */
15022
15023 static tree
15024 cp_parser_template_parameter_list (cp_parser* parser)
15025 {
15026 tree parameter_list = NULL_TREE;
15027
15028 begin_template_parm_list ();
15029
15030 /* The loop below parses the template parms. We first need to know
15031 the total number of template parms to be able to compute proper
15032 canonical types of each dependent type. So after the loop, when
15033 we know the total number of template parms,
15034 end_template_parm_list computes the proper canonical types and
15035 fixes up the dependent types accordingly. */
15036 while (true)
15037 {
15038 tree parameter;
15039 bool is_non_type;
15040 bool is_parameter_pack;
15041 location_t parm_loc;
15042
15043 /* Parse the template-parameter. */
15044 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15045 parameter = cp_parser_template_parameter (parser,
15046 &is_non_type,
15047 &is_parameter_pack);
15048 /* Add it to the list. */
15049 if (parameter != error_mark_node)
15050 parameter_list = process_template_parm (parameter_list,
15051 parm_loc,
15052 parameter,
15053 is_non_type,
15054 is_parameter_pack);
15055 else
15056 {
15057 tree err_parm = build_tree_list (parameter, parameter);
15058 parameter_list = chainon (parameter_list, err_parm);
15059 }
15060
15061 /* If the next token is not a `,', we're done. */
15062 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15063 break;
15064 /* Otherwise, consume the `,' token. */
15065 cp_lexer_consume_token (parser->lexer);
15066 }
15067
15068 return end_template_parm_list (parameter_list);
15069 }
15070
15071 /* Parse a introduction-list.
15072
15073 introduction-list:
15074 introduced-parameter
15075 introduction-list , introduced-parameter
15076
15077 introduced-parameter:
15078 ...[opt] identifier
15079
15080 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15081 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15082 WILDCARD_DECL will also have DECL_NAME set and token location in
15083 DECL_SOURCE_LOCATION. */
15084
15085 static tree
15086 cp_parser_introduction_list (cp_parser *parser)
15087 {
15088 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15089
15090 while (true)
15091 {
15092 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15093 if (is_pack)
15094 cp_lexer_consume_token (parser->lexer);
15095
15096 /* Build placeholder. */
15097 tree parm = build_nt (WILDCARD_DECL);
15098 DECL_SOURCE_LOCATION (parm)
15099 = cp_lexer_peek_token (parser->lexer)->location;
15100 DECL_NAME (parm) = cp_parser_identifier (parser);
15101 WILDCARD_PACK_P (parm) = is_pack;
15102 vec_safe_push (introduction_vec, parm);
15103
15104 /* If the next token is not a `,', we're done. */
15105 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15106 break;
15107 /* Otherwise, consume the `,' token. */
15108 cp_lexer_consume_token (parser->lexer);
15109 }
15110
15111 /* Convert the vec into a TREE_VEC. */
15112 tree introduction_list = make_tree_vec (introduction_vec->length ());
15113 unsigned int n;
15114 tree parm;
15115 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15116 TREE_VEC_ELT (introduction_list, n) = parm;
15117
15118 release_tree_vector (introduction_vec);
15119 return introduction_list;
15120 }
15121
15122 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15123 is an abstract declarator. */
15124
15125 static inline cp_declarator*
15126 get_id_declarator (cp_declarator *declarator)
15127 {
15128 cp_declarator *d = declarator;
15129 while (d && d->kind != cdk_id)
15130 d = d->declarator;
15131 return d;
15132 }
15133
15134 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15135 is an abstract declarator. */
15136
15137 static inline tree
15138 get_unqualified_id (cp_declarator *declarator)
15139 {
15140 declarator = get_id_declarator (declarator);
15141 if (declarator)
15142 return declarator->u.id.unqualified_name;
15143 else
15144 return NULL_TREE;
15145 }
15146
15147 /* Returns true if DECL represents a constrained-parameter. */
15148
15149 static inline bool
15150 is_constrained_parameter (tree decl)
15151 {
15152 return (decl
15153 && TREE_CODE (decl) == TYPE_DECL
15154 && CONSTRAINED_PARM_CONCEPT (decl)
15155 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15156 }
15157
15158 /* Returns true if PARM declares a constrained-parameter. */
15159
15160 static inline bool
15161 is_constrained_parameter (cp_parameter_declarator *parm)
15162 {
15163 return is_constrained_parameter (parm->decl_specifiers.type);
15164 }
15165
15166 /* Check that the type parameter is only a declarator-id, and that its
15167 type is not cv-qualified. */
15168
15169 bool
15170 cp_parser_check_constrained_type_parm (cp_parser *parser,
15171 cp_parameter_declarator *parm)
15172 {
15173 if (!parm->declarator)
15174 return true;
15175
15176 if (parm->declarator->kind != cdk_id)
15177 {
15178 cp_parser_error (parser, "invalid constrained type parameter");
15179 return false;
15180 }
15181
15182 /* Don't allow cv-qualified type parameters. */
15183 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15184 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15185 {
15186 cp_parser_error (parser, "cv-qualified type parameter");
15187 return false;
15188 }
15189
15190 return true;
15191 }
15192
15193 /* Finish parsing/processing a template type parameter and checking
15194 various restrictions. */
15195
15196 static inline tree
15197 cp_parser_constrained_type_template_parm (cp_parser *parser,
15198 tree id,
15199 cp_parameter_declarator* parmdecl)
15200 {
15201 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15202 return finish_template_type_parm (class_type_node, id);
15203 else
15204 return error_mark_node;
15205 }
15206
15207 static tree
15208 finish_constrained_template_template_parm (tree proto, tree id)
15209 {
15210 /* FIXME: This should probably be copied, and we may need to adjust
15211 the template parameter depths. */
15212 tree saved_parms = current_template_parms;
15213 begin_template_parm_list ();
15214 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15215 end_template_parm_list ();
15216
15217 tree parm = finish_template_template_parm (class_type_node, id);
15218 current_template_parms = saved_parms;
15219
15220 return parm;
15221 }
15222
15223 /* Finish parsing/processing a template template parameter by borrowing
15224 the template parameter list from the prototype parameter. */
15225
15226 static tree
15227 cp_parser_constrained_template_template_parm (cp_parser *parser,
15228 tree proto,
15229 tree id,
15230 cp_parameter_declarator *parmdecl)
15231 {
15232 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15233 return error_mark_node;
15234 return finish_constrained_template_template_parm (proto, id);
15235 }
15236
15237 /* Create a new non-type template parameter from the given PARM
15238 declarator. */
15239
15240 static tree
15241 constrained_non_type_template_parm (bool *is_non_type,
15242 cp_parameter_declarator *parm)
15243 {
15244 *is_non_type = true;
15245 cp_declarator *decl = parm->declarator;
15246 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15247 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15248 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15249 }
15250
15251 /* Build a constrained template parameter based on the PARMDECL
15252 declarator. The type of PARMDECL is the constrained type, which
15253 refers to the prototype template parameter that ultimately
15254 specifies the type of the declared parameter. */
15255
15256 static tree
15257 finish_constrained_parameter (cp_parser *parser,
15258 cp_parameter_declarator *parmdecl,
15259 bool *is_non_type,
15260 bool *is_parameter_pack)
15261 {
15262 tree decl = parmdecl->decl_specifiers.type;
15263 tree id = get_unqualified_id (parmdecl->declarator);
15264 tree def = parmdecl->default_argument;
15265 tree proto = DECL_INITIAL (decl);
15266
15267 /* A template parameter constrained by a variadic concept shall also
15268 be declared as a template parameter pack. */
15269 bool is_variadic = template_parameter_pack_p (proto);
15270 if (is_variadic && !*is_parameter_pack)
15271 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15272
15273 /* Build the parameter. Return an error if the declarator was invalid. */
15274 tree parm;
15275 if (TREE_CODE (proto) == TYPE_DECL)
15276 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15277 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15278 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15279 parmdecl);
15280 else
15281 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15282 if (parm == error_mark_node)
15283 return error_mark_node;
15284
15285 /* Finish the parameter decl and create a node attaching the
15286 default argument and constraint. */
15287 parm = build_tree_list (def, parm);
15288 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15289
15290 return parm;
15291 }
15292
15293 /* Returns true if the parsed type actually represents the declaration
15294 of a type template-parameter. */
15295
15296 static inline bool
15297 declares_constrained_type_template_parameter (tree type)
15298 {
15299 return (is_constrained_parameter (type)
15300 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15301 }
15302
15303
15304 /* Returns true if the parsed type actually represents the declaration of
15305 a template template-parameter. */
15306
15307 static bool
15308 declares_constrained_template_template_parameter (tree type)
15309 {
15310 return (is_constrained_parameter (type)
15311 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15312 }
15313
15314 /* Parse a default argument for a type template-parameter.
15315 Note that diagnostics are handled in cp_parser_template_parameter. */
15316
15317 static tree
15318 cp_parser_default_type_template_argument (cp_parser *parser)
15319 {
15320 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15321
15322 /* Consume the `=' token. */
15323 cp_lexer_consume_token (parser->lexer);
15324
15325 cp_token *token = cp_lexer_peek_token (parser->lexer);
15326
15327 /* Parse the default-argument. */
15328 push_deferring_access_checks (dk_no_deferred);
15329 tree default_argument = cp_parser_type_id (parser);
15330 pop_deferring_access_checks ();
15331
15332 if (flag_concepts && type_uses_auto (default_argument))
15333 {
15334 error_at (token->location,
15335 "invalid use of %<auto%> in default template argument");
15336 return error_mark_node;
15337 }
15338
15339 return default_argument;
15340 }
15341
15342 /* Parse a default argument for a template template-parameter. */
15343
15344 static tree
15345 cp_parser_default_template_template_argument (cp_parser *parser)
15346 {
15347 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15348
15349 bool is_template;
15350
15351 /* Consume the `='. */
15352 cp_lexer_consume_token (parser->lexer);
15353 /* Parse the id-expression. */
15354 push_deferring_access_checks (dk_no_deferred);
15355 /* save token before parsing the id-expression, for error
15356 reporting */
15357 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15358 tree default_argument
15359 = cp_parser_id_expression (parser,
15360 /*template_keyword_p=*/false,
15361 /*check_dependency_p=*/true,
15362 /*template_p=*/&is_template,
15363 /*declarator_p=*/false,
15364 /*optional_p=*/false);
15365 if (TREE_CODE (default_argument) == TYPE_DECL)
15366 /* If the id-expression was a template-id that refers to
15367 a template-class, we already have the declaration here,
15368 so no further lookup is needed. */
15369 ;
15370 else
15371 /* Look up the name. */
15372 default_argument
15373 = cp_parser_lookup_name (parser, default_argument,
15374 none_type,
15375 /*is_template=*/is_template,
15376 /*is_namespace=*/false,
15377 /*check_dependency=*/true,
15378 /*ambiguous_decls=*/NULL,
15379 token->location);
15380 /* See if the default argument is valid. */
15381 default_argument = check_template_template_default_arg (default_argument);
15382 pop_deferring_access_checks ();
15383 return default_argument;
15384 }
15385
15386 /* Parse a template-parameter.
15387
15388 template-parameter:
15389 type-parameter
15390 parameter-declaration
15391
15392 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15393 the parameter. The TREE_PURPOSE is the default value, if any.
15394 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15395 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15396 set to true iff this parameter is a parameter pack. */
15397
15398 static tree
15399 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15400 bool *is_parameter_pack)
15401 {
15402 cp_token *token;
15403 cp_parameter_declarator *parameter_declarator;
15404 tree parm;
15405
15406 /* Assume it is a type parameter or a template parameter. */
15407 *is_non_type = false;
15408 /* Assume it not a parameter pack. */
15409 *is_parameter_pack = false;
15410 /* Peek at the next token. */
15411 token = cp_lexer_peek_token (parser->lexer);
15412 /* If it is `template', we have a type-parameter. */
15413 if (token->keyword == RID_TEMPLATE)
15414 return cp_parser_type_parameter (parser, is_parameter_pack);
15415 /* If it is `class' or `typename' we do not know yet whether it is a
15416 type parameter or a non-type parameter. Consider:
15417
15418 template <typename T, typename T::X X> ...
15419
15420 or:
15421
15422 template <class C, class D*> ...
15423
15424 Here, the first parameter is a type parameter, and the second is
15425 a non-type parameter. We can tell by looking at the token after
15426 the identifier -- if it is a `,', `=', or `>' then we have a type
15427 parameter. */
15428 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15429 {
15430 /* Peek at the token after `class' or `typename'. */
15431 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15432 /* If it's an ellipsis, we have a template type parameter
15433 pack. */
15434 if (token->type == CPP_ELLIPSIS)
15435 return cp_parser_type_parameter (parser, is_parameter_pack);
15436 /* If it's an identifier, skip it. */
15437 if (token->type == CPP_NAME)
15438 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15439 /* Now, see if the token looks like the end of a template
15440 parameter. */
15441 if (token->type == CPP_COMMA
15442 || token->type == CPP_EQ
15443 || token->type == CPP_GREATER)
15444 return cp_parser_type_parameter (parser, is_parameter_pack);
15445 }
15446
15447 /* Otherwise, it is a non-type parameter or a constrained parameter.
15448
15449 [temp.param]
15450
15451 When parsing a default template-argument for a non-type
15452 template-parameter, the first non-nested `>' is taken as the end
15453 of the template parameter-list rather than a greater-than
15454 operator. */
15455 parameter_declarator
15456 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15457 /*parenthesized_p=*/NULL);
15458
15459 if (!parameter_declarator)
15460 return error_mark_node;
15461
15462 /* If the parameter declaration is marked as a parameter pack, set
15463 *IS_PARAMETER_PACK to notify the caller. */
15464 if (parameter_declarator->template_parameter_pack_p)
15465 *is_parameter_pack = true;
15466
15467 if (parameter_declarator->default_argument)
15468 {
15469 /* Can happen in some cases of erroneous input (c++/34892). */
15470 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15471 /* Consume the `...' for better error recovery. */
15472 cp_lexer_consume_token (parser->lexer);
15473 }
15474
15475 // The parameter may have been constrained.
15476 if (is_constrained_parameter (parameter_declarator))
15477 return finish_constrained_parameter (parser,
15478 parameter_declarator,
15479 is_non_type,
15480 is_parameter_pack);
15481
15482 // Now we're sure that the parameter is a non-type parameter.
15483 *is_non_type = true;
15484
15485 parm = grokdeclarator (parameter_declarator->declarator,
15486 &parameter_declarator->decl_specifiers,
15487 TPARM, /*initialized=*/0,
15488 /*attrlist=*/NULL);
15489 if (parm == error_mark_node)
15490 return error_mark_node;
15491
15492 return build_tree_list (parameter_declarator->default_argument, parm);
15493 }
15494
15495 /* Parse a type-parameter.
15496
15497 type-parameter:
15498 class identifier [opt]
15499 class identifier [opt] = type-id
15500 typename identifier [opt]
15501 typename identifier [opt] = type-id
15502 template < template-parameter-list > class identifier [opt]
15503 template < template-parameter-list > class identifier [opt]
15504 = id-expression
15505
15506 GNU Extension (variadic templates):
15507
15508 type-parameter:
15509 class ... identifier [opt]
15510 typename ... identifier [opt]
15511
15512 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15513 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15514 the declaration of the parameter.
15515
15516 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15517
15518 static tree
15519 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15520 {
15521 cp_token *token;
15522 tree parameter;
15523
15524 /* Look for a keyword to tell us what kind of parameter this is. */
15525 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15526 if (!token)
15527 return error_mark_node;
15528
15529 switch (token->keyword)
15530 {
15531 case RID_CLASS:
15532 case RID_TYPENAME:
15533 {
15534 tree identifier;
15535 tree default_argument;
15536
15537 /* If the next token is an ellipsis, we have a template
15538 argument pack. */
15539 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15540 {
15541 /* Consume the `...' token. */
15542 cp_lexer_consume_token (parser->lexer);
15543 maybe_warn_variadic_templates ();
15544
15545 *is_parameter_pack = true;
15546 }
15547
15548 /* If the next token is an identifier, then it names the
15549 parameter. */
15550 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15551 identifier = cp_parser_identifier (parser);
15552 else
15553 identifier = NULL_TREE;
15554
15555 /* Create the parameter. */
15556 parameter = finish_template_type_parm (class_type_node, identifier);
15557
15558 /* If the next token is an `=', we have a default argument. */
15559 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15560 {
15561 default_argument
15562 = cp_parser_default_type_template_argument (parser);
15563
15564 /* Template parameter packs cannot have default
15565 arguments. */
15566 if (*is_parameter_pack)
15567 {
15568 if (identifier)
15569 error_at (token->location,
15570 "template parameter pack %qD cannot have a "
15571 "default argument", identifier);
15572 else
15573 error_at (token->location,
15574 "template parameter packs cannot have "
15575 "default arguments");
15576 default_argument = NULL_TREE;
15577 }
15578 else if (check_for_bare_parameter_packs (default_argument))
15579 default_argument = error_mark_node;
15580 }
15581 else
15582 default_argument = NULL_TREE;
15583
15584 /* Create the combined representation of the parameter and the
15585 default argument. */
15586 parameter = build_tree_list (default_argument, parameter);
15587 }
15588 break;
15589
15590 case RID_TEMPLATE:
15591 {
15592 tree identifier;
15593 tree default_argument;
15594
15595 /* Look for the `<'. */
15596 cp_parser_require (parser, CPP_LESS, RT_LESS);
15597 /* Parse the template-parameter-list. */
15598 cp_parser_template_parameter_list (parser);
15599 /* Look for the `>'. */
15600 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15601
15602 // If template requirements are present, parse them.
15603 if (flag_concepts)
15604 {
15605 tree reqs = get_shorthand_constraints (current_template_parms);
15606 if (tree r = cp_parser_requires_clause_opt (parser))
15607 reqs = conjoin_constraints (reqs, normalize_expression (r));
15608 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15609 }
15610
15611 /* Look for the `class' or 'typename' keywords. */
15612 cp_parser_type_parameter_key (parser);
15613 /* If the next token is an ellipsis, we have a template
15614 argument pack. */
15615 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15616 {
15617 /* Consume the `...' token. */
15618 cp_lexer_consume_token (parser->lexer);
15619 maybe_warn_variadic_templates ();
15620
15621 *is_parameter_pack = true;
15622 }
15623 /* If the next token is an `=', then there is a
15624 default-argument. If the next token is a `>', we are at
15625 the end of the parameter-list. If the next token is a `,',
15626 then we are at the end of this parameter. */
15627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15628 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15629 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15630 {
15631 identifier = cp_parser_identifier (parser);
15632 /* Treat invalid names as if the parameter were nameless. */
15633 if (identifier == error_mark_node)
15634 identifier = NULL_TREE;
15635 }
15636 else
15637 identifier = NULL_TREE;
15638
15639 /* Create the template parameter. */
15640 parameter = finish_template_template_parm (class_type_node,
15641 identifier);
15642
15643 /* If the next token is an `=', then there is a
15644 default-argument. */
15645 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15646 {
15647 default_argument
15648 = cp_parser_default_template_template_argument (parser);
15649
15650 /* Template parameter packs cannot have default
15651 arguments. */
15652 if (*is_parameter_pack)
15653 {
15654 if (identifier)
15655 error_at (token->location,
15656 "template parameter pack %qD cannot "
15657 "have a default argument",
15658 identifier);
15659 else
15660 error_at (token->location, "template parameter packs cannot "
15661 "have default arguments");
15662 default_argument = NULL_TREE;
15663 }
15664 }
15665 else
15666 default_argument = NULL_TREE;
15667
15668 /* Create the combined representation of the parameter and the
15669 default argument. */
15670 parameter = build_tree_list (default_argument, parameter);
15671 }
15672 break;
15673
15674 default:
15675 gcc_unreachable ();
15676 break;
15677 }
15678
15679 return parameter;
15680 }
15681
15682 /* Parse a template-id.
15683
15684 template-id:
15685 template-name < template-argument-list [opt] >
15686
15687 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15688 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15689 returned. Otherwise, if the template-name names a function, or set
15690 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15691 names a class, returns a TYPE_DECL for the specialization.
15692
15693 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15694 uninstantiated templates. */
15695
15696 static tree
15697 cp_parser_template_id (cp_parser *parser,
15698 bool template_keyword_p,
15699 bool check_dependency_p,
15700 enum tag_types tag_type,
15701 bool is_declaration)
15702 {
15703 tree templ;
15704 tree arguments;
15705 tree template_id;
15706 cp_token_position start_of_id = 0;
15707 cp_token *next_token = NULL, *next_token_2 = NULL;
15708 bool is_identifier;
15709
15710 /* If the next token corresponds to a template-id, there is no need
15711 to reparse it. */
15712 cp_token *token = cp_lexer_peek_token (parser->lexer);
15713 if (token->type == CPP_TEMPLATE_ID)
15714 {
15715 cp_lexer_consume_token (parser->lexer);
15716 return saved_checks_value (token->u.tree_check_value);
15717 }
15718
15719 /* Avoid performing name lookup if there is no possibility of
15720 finding a template-id. */
15721 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15722 || (token->type == CPP_NAME
15723 && !cp_parser_nth_token_starts_template_argument_list_p
15724 (parser, 2)))
15725 {
15726 cp_parser_error (parser, "expected template-id");
15727 return error_mark_node;
15728 }
15729
15730 /* Remember where the template-id starts. */
15731 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15732 start_of_id = cp_lexer_token_position (parser->lexer, false);
15733
15734 push_deferring_access_checks (dk_deferred);
15735
15736 /* Parse the template-name. */
15737 is_identifier = false;
15738 templ = cp_parser_template_name (parser, template_keyword_p,
15739 check_dependency_p,
15740 is_declaration,
15741 tag_type,
15742 &is_identifier);
15743 if (templ == error_mark_node || is_identifier)
15744 {
15745 pop_deferring_access_checks ();
15746 return templ;
15747 }
15748
15749 /* Since we're going to preserve any side-effects from this parse, set up a
15750 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15751 in the template arguments. */
15752 tentative_firewall firewall (parser);
15753
15754 /* If we find the sequence `[:' after a template-name, it's probably
15755 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15756 parse correctly the argument list. */
15757 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15758 == CPP_OPEN_SQUARE)
15759 && next_token->flags & DIGRAPH
15760 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15761 == CPP_COLON)
15762 && !(next_token_2->flags & PREV_WHITE))
15763 {
15764 cp_parser_parse_tentatively (parser);
15765 /* Change `:' into `::'. */
15766 next_token_2->type = CPP_SCOPE;
15767 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15768 CPP_LESS. */
15769 cp_lexer_consume_token (parser->lexer);
15770
15771 /* Parse the arguments. */
15772 arguments = cp_parser_enclosed_template_argument_list (parser);
15773 if (!cp_parser_parse_definitely (parser))
15774 {
15775 /* If we couldn't parse an argument list, then we revert our changes
15776 and return simply an error. Maybe this is not a template-id
15777 after all. */
15778 next_token_2->type = CPP_COLON;
15779 cp_parser_error (parser, "expected %<<%>");
15780 pop_deferring_access_checks ();
15781 return error_mark_node;
15782 }
15783 /* Otherwise, emit an error about the invalid digraph, but continue
15784 parsing because we got our argument list. */
15785 if (permerror (next_token->location,
15786 "%<<::%> cannot begin a template-argument list"))
15787 {
15788 static bool hint = false;
15789 inform (next_token->location,
15790 "%<<:%> is an alternate spelling for %<[%>."
15791 " Insert whitespace between %<<%> and %<::%>");
15792 if (!hint && !flag_permissive)
15793 {
15794 inform (next_token->location, "(if you use %<-fpermissive%> "
15795 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15796 "accept your code)");
15797 hint = true;
15798 }
15799 }
15800 }
15801 else
15802 {
15803 /* Look for the `<' that starts the template-argument-list. */
15804 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15805 {
15806 pop_deferring_access_checks ();
15807 return error_mark_node;
15808 }
15809 /* Parse the arguments. */
15810 arguments = cp_parser_enclosed_template_argument_list (parser);
15811 }
15812
15813 /* Set the location to be of the form:
15814 template-name < template-argument-list [opt] >
15815 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15816 with caret == start at the start of the template-name,
15817 ranging until the closing '>'. */
15818 location_t finish_loc
15819 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15820 location_t combined_loc
15821 = make_location (token->location, token->location, finish_loc);
15822
15823 /* Build a representation of the specialization. */
15824 if (identifier_p (templ))
15825 template_id = build_min_nt_loc (combined_loc,
15826 TEMPLATE_ID_EXPR,
15827 templ, arguments);
15828 else if (DECL_TYPE_TEMPLATE_P (templ)
15829 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15830 {
15831 bool entering_scope;
15832 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15833 template (rather than some instantiation thereof) only if
15834 is not nested within some other construct. For example, in
15835 "template <typename T> void f(T) { A<T>::", A<T> is just an
15836 instantiation of A. */
15837 entering_scope = (template_parm_scope_p ()
15838 && cp_lexer_next_token_is (parser->lexer,
15839 CPP_SCOPE));
15840 template_id
15841 = finish_template_type (templ, arguments, entering_scope);
15842 }
15843 /* A template-like identifier may be a partial concept id. */
15844 else if (flag_concepts
15845 && (template_id = (cp_parser_maybe_partial_concept_id
15846 (parser, templ, arguments))))
15847 return template_id;
15848 else if (variable_template_p (templ))
15849 {
15850 template_id = lookup_template_variable (templ, arguments);
15851 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15852 SET_EXPR_LOCATION (template_id, combined_loc);
15853 }
15854 else
15855 {
15856 /* If it's not a class-template or a template-template, it should be
15857 a function-template. */
15858 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15859 || TREE_CODE (templ) == OVERLOAD
15860 || BASELINK_P (templ)));
15861
15862 template_id = lookup_template_function (templ, arguments);
15863 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15864 SET_EXPR_LOCATION (template_id, combined_loc);
15865 }
15866
15867 /* If parsing tentatively, replace the sequence of tokens that makes
15868 up the template-id with a CPP_TEMPLATE_ID token. That way,
15869 should we re-parse the token stream, we will not have to repeat
15870 the effort required to do the parse, nor will we issue duplicate
15871 error messages about problems during instantiation of the
15872 template. */
15873 if (start_of_id
15874 /* Don't do this if we had a parse error in a declarator; re-parsing
15875 might succeed if a name changes meaning (60361). */
15876 && !(cp_parser_error_occurred (parser)
15877 && cp_parser_parsing_tentatively (parser)
15878 && parser->in_declarator_p))
15879 {
15880 /* Reset the contents of the START_OF_ID token. */
15881 token->type = CPP_TEMPLATE_ID;
15882 token->location = combined_loc;
15883
15884 /* We must mark the lookup as kept, so we don't throw it away on
15885 the first parse. */
15886 if (is_overloaded_fn (template_id))
15887 lookup_keep (get_fns (template_id), true);
15888
15889 /* Retrieve any deferred checks. Do not pop this access checks yet
15890 so the memory will not be reclaimed during token replacing below. */
15891 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15892 token->u.tree_check_value->value = template_id;
15893 token->u.tree_check_value->checks = get_deferred_access_checks ();
15894 token->keyword = RID_MAX;
15895
15896 /* Purge all subsequent tokens. */
15897 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15898
15899 /* ??? Can we actually assume that, if template_id ==
15900 error_mark_node, we will have issued a diagnostic to the
15901 user, as opposed to simply marking the tentative parse as
15902 failed? */
15903 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15904 error_at (token->location, "parse error in template argument list");
15905 }
15906
15907 pop_to_parent_deferring_access_checks ();
15908 return template_id;
15909 }
15910
15911 /* Parse a template-name.
15912
15913 template-name:
15914 identifier
15915
15916 The standard should actually say:
15917
15918 template-name:
15919 identifier
15920 operator-function-id
15921
15922 A defect report has been filed about this issue.
15923
15924 A conversion-function-id cannot be a template name because they cannot
15925 be part of a template-id. In fact, looking at this code:
15926
15927 a.operator K<int>()
15928
15929 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15930 It is impossible to call a templated conversion-function-id with an
15931 explicit argument list, since the only allowed template parameter is
15932 the type to which it is converting.
15933
15934 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15935 `template' keyword, in a construction like:
15936
15937 T::template f<3>()
15938
15939 In that case `f' is taken to be a template-name, even though there
15940 is no way of knowing for sure.
15941
15942 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15943 name refers to a set of overloaded functions, at least one of which
15944 is a template, or an IDENTIFIER_NODE with the name of the template,
15945 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15946 names are looked up inside uninstantiated templates. */
15947
15948 static tree
15949 cp_parser_template_name (cp_parser* parser,
15950 bool template_keyword_p,
15951 bool check_dependency_p,
15952 bool is_declaration,
15953 enum tag_types tag_type,
15954 bool *is_identifier)
15955 {
15956 tree identifier;
15957 tree decl;
15958 cp_token *token = cp_lexer_peek_token (parser->lexer);
15959
15960 /* If the next token is `operator', then we have either an
15961 operator-function-id or a conversion-function-id. */
15962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15963 {
15964 /* We don't know whether we're looking at an
15965 operator-function-id or a conversion-function-id. */
15966 cp_parser_parse_tentatively (parser);
15967 /* Try an operator-function-id. */
15968 identifier = cp_parser_operator_function_id (parser);
15969 /* If that didn't work, try a conversion-function-id. */
15970 if (!cp_parser_parse_definitely (parser))
15971 {
15972 cp_parser_error (parser, "expected template-name");
15973 return error_mark_node;
15974 }
15975 }
15976 /* Look for the identifier. */
15977 else
15978 identifier = cp_parser_identifier (parser);
15979
15980 /* If we didn't find an identifier, we don't have a template-id. */
15981 if (identifier == error_mark_node)
15982 return error_mark_node;
15983
15984 /* If the name immediately followed the `template' keyword, then it
15985 is a template-name. However, if the next token is not `<', then
15986 we do not treat it as a template-name, since it is not being used
15987 as part of a template-id. This enables us to handle constructs
15988 like:
15989
15990 template <typename T> struct S { S(); };
15991 template <typename T> S<T>::S();
15992
15993 correctly. We would treat `S' as a template -- if it were `S<T>'
15994 -- but we do not if there is no `<'. */
15995
15996 if (processing_template_decl
15997 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15998 {
15999 /* In a declaration, in a dependent context, we pretend that the
16000 "template" keyword was present in order to improve error
16001 recovery. For example, given:
16002
16003 template <typename T> void f(T::X<int>);
16004
16005 we want to treat "X<int>" as a template-id. */
16006 if (is_declaration
16007 && !template_keyword_p
16008 && parser->scope && TYPE_P (parser->scope)
16009 && check_dependency_p
16010 && dependent_scope_p (parser->scope)
16011 /* Do not do this for dtors (or ctors), since they never
16012 need the template keyword before their name. */
16013 && !constructor_name_p (identifier, parser->scope))
16014 {
16015 cp_token_position start = 0;
16016
16017 /* Explain what went wrong. */
16018 error_at (token->location, "non-template %qD used as template",
16019 identifier);
16020 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16021 parser->scope, identifier);
16022 /* If parsing tentatively, find the location of the "<" token. */
16023 if (cp_parser_simulate_error (parser))
16024 start = cp_lexer_token_position (parser->lexer, true);
16025 /* Parse the template arguments so that we can issue error
16026 messages about them. */
16027 cp_lexer_consume_token (parser->lexer);
16028 cp_parser_enclosed_template_argument_list (parser);
16029 /* Skip tokens until we find a good place from which to
16030 continue parsing. */
16031 cp_parser_skip_to_closing_parenthesis (parser,
16032 /*recovering=*/true,
16033 /*or_comma=*/true,
16034 /*consume_paren=*/false);
16035 /* If parsing tentatively, permanently remove the
16036 template argument list. That will prevent duplicate
16037 error messages from being issued about the missing
16038 "template" keyword. */
16039 if (start)
16040 cp_lexer_purge_tokens_after (parser->lexer, start);
16041 if (is_identifier)
16042 *is_identifier = true;
16043 parser->context->object_type = NULL_TREE;
16044 return identifier;
16045 }
16046
16047 /* If the "template" keyword is present, then there is generally
16048 no point in doing name-lookup, so we just return IDENTIFIER.
16049 But, if the qualifying scope is non-dependent then we can
16050 (and must) do name-lookup normally. */
16051 if (template_keyword_p)
16052 {
16053 tree scope = (parser->scope ? parser->scope
16054 : parser->context->object_type);
16055 if (scope && TYPE_P (scope)
16056 && (!CLASS_TYPE_P (scope)
16057 || (check_dependency_p && dependent_type_p (scope))))
16058 {
16059 /* We're optimizing away the call to cp_parser_lookup_name, but
16060 we still need to do this. */
16061 parser->context->object_type = NULL_TREE;
16062 return identifier;
16063 }
16064 }
16065 }
16066
16067 /* Look up the name. */
16068 decl = cp_parser_lookup_name (parser, identifier,
16069 tag_type,
16070 /*is_template=*/true,
16071 /*is_namespace=*/false,
16072 check_dependency_p,
16073 /*ambiguous_decls=*/NULL,
16074 token->location);
16075
16076 decl = strip_using_decl (decl);
16077
16078 /* If DECL is a template, then the name was a template-name. */
16079 if (TREE_CODE (decl) == TEMPLATE_DECL)
16080 {
16081 if (TREE_DEPRECATED (decl)
16082 && deprecated_state != DEPRECATED_SUPPRESS)
16083 warn_deprecated_use (decl, NULL_TREE);
16084 }
16085 else
16086 {
16087 /* The standard does not explicitly indicate whether a name that
16088 names a set of overloaded declarations, some of which are
16089 templates, is a template-name. However, such a name should
16090 be a template-name; otherwise, there is no way to form a
16091 template-id for the overloaded templates. */
16092 bool found = false;
16093
16094 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16095 !found && iter; ++iter)
16096 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16097 found = true;
16098
16099 if (!found)
16100 {
16101 /* The name does not name a template. */
16102 cp_parser_error (parser, "expected template-name");
16103 return error_mark_node;
16104 }
16105 }
16106
16107 /* If DECL is dependent, and refers to a function, then just return
16108 its name; we will look it up again during template instantiation. */
16109 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16110 {
16111 tree scope = ovl_scope (decl);
16112 if (TYPE_P (scope) && dependent_type_p (scope))
16113 return identifier;
16114 }
16115
16116 return decl;
16117 }
16118
16119 /* Parse a template-argument-list.
16120
16121 template-argument-list:
16122 template-argument ... [opt]
16123 template-argument-list , template-argument ... [opt]
16124
16125 Returns a TREE_VEC containing the arguments. */
16126
16127 static tree
16128 cp_parser_template_argument_list (cp_parser* parser)
16129 {
16130 tree fixed_args[10];
16131 unsigned n_args = 0;
16132 unsigned alloced = 10;
16133 tree *arg_ary = fixed_args;
16134 tree vec;
16135 bool saved_in_template_argument_list_p;
16136 bool saved_ice_p;
16137 bool saved_non_ice_p;
16138
16139 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16140 parser->in_template_argument_list_p = true;
16141 /* Even if the template-id appears in an integral
16142 constant-expression, the contents of the argument list do
16143 not. */
16144 saved_ice_p = parser->integral_constant_expression_p;
16145 parser->integral_constant_expression_p = false;
16146 saved_non_ice_p = parser->non_integral_constant_expression_p;
16147 parser->non_integral_constant_expression_p = false;
16148
16149 /* Parse the arguments. */
16150 do
16151 {
16152 tree argument;
16153
16154 if (n_args)
16155 /* Consume the comma. */
16156 cp_lexer_consume_token (parser->lexer);
16157
16158 /* Parse the template-argument. */
16159 argument = cp_parser_template_argument (parser);
16160
16161 /* If the next token is an ellipsis, we're expanding a template
16162 argument pack. */
16163 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16164 {
16165 if (argument == error_mark_node)
16166 {
16167 cp_token *token = cp_lexer_peek_token (parser->lexer);
16168 error_at (token->location,
16169 "expected parameter pack before %<...%>");
16170 }
16171 /* Consume the `...' token. */
16172 cp_lexer_consume_token (parser->lexer);
16173
16174 /* Make the argument into a TYPE_PACK_EXPANSION or
16175 EXPR_PACK_EXPANSION. */
16176 argument = make_pack_expansion (argument);
16177 }
16178
16179 if (n_args == alloced)
16180 {
16181 alloced *= 2;
16182
16183 if (arg_ary == fixed_args)
16184 {
16185 arg_ary = XNEWVEC (tree, alloced);
16186 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16187 }
16188 else
16189 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16190 }
16191 arg_ary[n_args++] = argument;
16192 }
16193 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16194
16195 vec = make_tree_vec (n_args);
16196
16197 while (n_args--)
16198 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16199
16200 if (arg_ary != fixed_args)
16201 free (arg_ary);
16202 parser->non_integral_constant_expression_p = saved_non_ice_p;
16203 parser->integral_constant_expression_p = saved_ice_p;
16204 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16205 if (CHECKING_P)
16206 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16207 return vec;
16208 }
16209
16210 /* Parse a template-argument.
16211
16212 template-argument:
16213 assignment-expression
16214 type-id
16215 id-expression
16216
16217 The representation is that of an assignment-expression, type-id, or
16218 id-expression -- except that the qualified id-expression is
16219 evaluated, so that the value returned is either a DECL or an
16220 OVERLOAD.
16221
16222 Although the standard says "assignment-expression", it forbids
16223 throw-expressions or assignments in the template argument.
16224 Therefore, we use "conditional-expression" instead. */
16225
16226 static tree
16227 cp_parser_template_argument (cp_parser* parser)
16228 {
16229 tree argument;
16230 bool template_p;
16231 bool address_p;
16232 bool maybe_type_id = false;
16233 cp_token *token = NULL, *argument_start_token = NULL;
16234 location_t loc = 0;
16235 cp_id_kind idk;
16236
16237 /* There's really no way to know what we're looking at, so we just
16238 try each alternative in order.
16239
16240 [temp.arg]
16241
16242 In a template-argument, an ambiguity between a type-id and an
16243 expression is resolved to a type-id, regardless of the form of
16244 the corresponding template-parameter.
16245
16246 Therefore, we try a type-id first. */
16247 cp_parser_parse_tentatively (parser);
16248 argument = cp_parser_template_type_arg (parser);
16249 /* If there was no error parsing the type-id but the next token is a
16250 '>>', our behavior depends on which dialect of C++ we're
16251 parsing. In C++98, we probably found a typo for '> >'. But there
16252 are type-id which are also valid expressions. For instance:
16253
16254 struct X { int operator >> (int); };
16255 template <int V> struct Foo {};
16256 Foo<X () >> 5> r;
16257
16258 Here 'X()' is a valid type-id of a function type, but the user just
16259 wanted to write the expression "X() >> 5". Thus, we remember that we
16260 found a valid type-id, but we still try to parse the argument as an
16261 expression to see what happens.
16262
16263 In C++0x, the '>>' will be considered two separate '>'
16264 tokens. */
16265 if (!cp_parser_error_occurred (parser)
16266 && cxx_dialect == cxx98
16267 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16268 {
16269 maybe_type_id = true;
16270 cp_parser_abort_tentative_parse (parser);
16271 }
16272 else
16273 {
16274 /* If the next token isn't a `,' or a `>', then this argument wasn't
16275 really finished. This means that the argument is not a valid
16276 type-id. */
16277 if (!cp_parser_next_token_ends_template_argument_p (parser))
16278 cp_parser_error (parser, "expected template-argument");
16279 /* If that worked, we're done. */
16280 if (cp_parser_parse_definitely (parser))
16281 return argument;
16282 }
16283 /* We're still not sure what the argument will be. */
16284 cp_parser_parse_tentatively (parser);
16285 /* Try a template. */
16286 argument_start_token = cp_lexer_peek_token (parser->lexer);
16287 argument = cp_parser_id_expression (parser,
16288 /*template_keyword_p=*/false,
16289 /*check_dependency_p=*/true,
16290 &template_p,
16291 /*declarator_p=*/false,
16292 /*optional_p=*/false);
16293 /* If the next token isn't a `,' or a `>', then this argument wasn't
16294 really finished. */
16295 if (!cp_parser_next_token_ends_template_argument_p (parser))
16296 cp_parser_error (parser, "expected template-argument");
16297 if (!cp_parser_error_occurred (parser))
16298 {
16299 /* Figure out what is being referred to. If the id-expression
16300 was for a class template specialization, then we will have a
16301 TYPE_DECL at this point. There is no need to do name lookup
16302 at this point in that case. */
16303 if (TREE_CODE (argument) != TYPE_DECL)
16304 argument = cp_parser_lookup_name (parser, argument,
16305 none_type,
16306 /*is_template=*/template_p,
16307 /*is_namespace=*/false,
16308 /*check_dependency=*/true,
16309 /*ambiguous_decls=*/NULL,
16310 argument_start_token->location);
16311 /* Handle a constrained-type-specifier for a non-type template
16312 parameter. */
16313 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16314 argument = decl;
16315 else if (TREE_CODE (argument) != TEMPLATE_DECL
16316 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16317 cp_parser_error (parser, "expected template-name");
16318 }
16319 if (cp_parser_parse_definitely (parser))
16320 {
16321 if (TREE_DEPRECATED (argument))
16322 warn_deprecated_use (argument, NULL_TREE);
16323 return argument;
16324 }
16325 /* It must be a non-type argument. In C++17 any constant-expression is
16326 allowed. */
16327 if (cxx_dialect > cxx14)
16328 goto general_expr;
16329
16330 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16331
16332 -- an integral constant-expression of integral or enumeration
16333 type; or
16334
16335 -- the name of a non-type template-parameter; or
16336
16337 -- the name of an object or function with external linkage...
16338
16339 -- the address of an object or function with external linkage...
16340
16341 -- a pointer to member... */
16342 /* Look for a non-type template parameter. */
16343 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16344 {
16345 cp_parser_parse_tentatively (parser);
16346 argument = cp_parser_primary_expression (parser,
16347 /*address_p=*/false,
16348 /*cast_p=*/false,
16349 /*template_arg_p=*/true,
16350 &idk);
16351 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16352 || !cp_parser_next_token_ends_template_argument_p (parser))
16353 cp_parser_simulate_error (parser);
16354 if (cp_parser_parse_definitely (parser))
16355 return argument;
16356 }
16357
16358 /* If the next token is "&", the argument must be the address of an
16359 object or function with external linkage. */
16360 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16361 if (address_p)
16362 {
16363 loc = cp_lexer_peek_token (parser->lexer)->location;
16364 cp_lexer_consume_token (parser->lexer);
16365 }
16366 /* See if we might have an id-expression. */
16367 token = cp_lexer_peek_token (parser->lexer);
16368 if (token->type == CPP_NAME
16369 || token->keyword == RID_OPERATOR
16370 || token->type == CPP_SCOPE
16371 || token->type == CPP_TEMPLATE_ID
16372 || token->type == CPP_NESTED_NAME_SPECIFIER)
16373 {
16374 cp_parser_parse_tentatively (parser);
16375 argument = cp_parser_primary_expression (parser,
16376 address_p,
16377 /*cast_p=*/false,
16378 /*template_arg_p=*/true,
16379 &idk);
16380 if (cp_parser_error_occurred (parser)
16381 || !cp_parser_next_token_ends_template_argument_p (parser))
16382 cp_parser_abort_tentative_parse (parser);
16383 else
16384 {
16385 tree probe;
16386
16387 if (INDIRECT_REF_P (argument))
16388 {
16389 /* Strip the dereference temporarily. */
16390 gcc_assert (REFERENCE_REF_P (argument));
16391 argument = TREE_OPERAND (argument, 0);
16392 }
16393
16394 /* If we're in a template, we represent a qualified-id referring
16395 to a static data member as a SCOPE_REF even if the scope isn't
16396 dependent so that we can check access control later. */
16397 probe = argument;
16398 if (TREE_CODE (probe) == SCOPE_REF)
16399 probe = TREE_OPERAND (probe, 1);
16400 if (VAR_P (probe))
16401 {
16402 /* A variable without external linkage might still be a
16403 valid constant-expression, so no error is issued here
16404 if the external-linkage check fails. */
16405 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16406 cp_parser_simulate_error (parser);
16407 }
16408 else if (is_overloaded_fn (argument))
16409 /* All overloaded functions are allowed; if the external
16410 linkage test does not pass, an error will be issued
16411 later. */
16412 ;
16413 else if (address_p
16414 && (TREE_CODE (argument) == OFFSET_REF
16415 || TREE_CODE (argument) == SCOPE_REF))
16416 /* A pointer-to-member. */
16417 ;
16418 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16419 ;
16420 else
16421 cp_parser_simulate_error (parser);
16422
16423 if (cp_parser_parse_definitely (parser))
16424 {
16425 if (address_p)
16426 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16427 tf_warning_or_error);
16428 else
16429 argument = convert_from_reference (argument);
16430 return argument;
16431 }
16432 }
16433 }
16434 /* If the argument started with "&", there are no other valid
16435 alternatives at this point. */
16436 if (address_p)
16437 {
16438 cp_parser_error (parser, "invalid non-type template argument");
16439 return error_mark_node;
16440 }
16441
16442 general_expr:
16443 /* If the argument wasn't successfully parsed as a type-id followed
16444 by '>>', the argument can only be a constant expression now.
16445 Otherwise, we try parsing the constant-expression tentatively,
16446 because the argument could really be a type-id. */
16447 if (maybe_type_id)
16448 cp_parser_parse_tentatively (parser);
16449
16450 if (cxx_dialect <= cxx14)
16451 argument = cp_parser_constant_expression (parser);
16452 else
16453 {
16454 /* With C++17 generalized non-type template arguments we need to handle
16455 lvalue constant expressions, too. */
16456 argument = cp_parser_assignment_expression (parser);
16457 require_potential_constant_expression (argument);
16458 }
16459
16460 if (!maybe_type_id)
16461 return argument;
16462 if (!cp_parser_next_token_ends_template_argument_p (parser))
16463 cp_parser_error (parser, "expected template-argument");
16464 if (cp_parser_parse_definitely (parser))
16465 return argument;
16466 /* We did our best to parse the argument as a non type-id, but that
16467 was the only alternative that matched (albeit with a '>' after
16468 it). We can assume it's just a typo from the user, and a
16469 diagnostic will then be issued. */
16470 return cp_parser_template_type_arg (parser);
16471 }
16472
16473 /* Parse an explicit-instantiation.
16474
16475 explicit-instantiation:
16476 template declaration
16477
16478 Although the standard says `declaration', what it really means is:
16479
16480 explicit-instantiation:
16481 template decl-specifier-seq [opt] declarator [opt] ;
16482
16483 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16484 supposed to be allowed. A defect report has been filed about this
16485 issue.
16486
16487 GNU Extension:
16488
16489 explicit-instantiation:
16490 storage-class-specifier template
16491 decl-specifier-seq [opt] declarator [opt] ;
16492 function-specifier template
16493 decl-specifier-seq [opt] declarator [opt] ; */
16494
16495 static void
16496 cp_parser_explicit_instantiation (cp_parser* parser)
16497 {
16498 int declares_class_or_enum;
16499 cp_decl_specifier_seq decl_specifiers;
16500 tree extension_specifier = NULL_TREE;
16501
16502 timevar_push (TV_TEMPLATE_INST);
16503
16504 /* Look for an (optional) storage-class-specifier or
16505 function-specifier. */
16506 if (cp_parser_allow_gnu_extensions_p (parser))
16507 {
16508 extension_specifier
16509 = cp_parser_storage_class_specifier_opt (parser);
16510 if (!extension_specifier)
16511 extension_specifier
16512 = cp_parser_function_specifier_opt (parser,
16513 /*decl_specs=*/NULL);
16514 }
16515
16516 /* Look for the `template' keyword. */
16517 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16518 /* Let the front end know that we are processing an explicit
16519 instantiation. */
16520 begin_explicit_instantiation ();
16521 /* [temp.explicit] says that we are supposed to ignore access
16522 control while processing explicit instantiation directives. */
16523 push_deferring_access_checks (dk_no_check);
16524 /* Parse a decl-specifier-seq. */
16525 cp_parser_decl_specifier_seq (parser,
16526 CP_PARSER_FLAGS_OPTIONAL,
16527 &decl_specifiers,
16528 &declares_class_or_enum);
16529 /* If there was exactly one decl-specifier, and it declared a class,
16530 and there's no declarator, then we have an explicit type
16531 instantiation. */
16532 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16533 {
16534 tree type;
16535
16536 type = check_tag_decl (&decl_specifiers,
16537 /*explicit_type_instantiation_p=*/true);
16538 /* Turn access control back on for names used during
16539 template instantiation. */
16540 pop_deferring_access_checks ();
16541 if (type)
16542 do_type_instantiation (type, extension_specifier,
16543 /*complain=*/tf_error);
16544 }
16545 else
16546 {
16547 cp_declarator *declarator;
16548 tree decl;
16549
16550 /* Parse the declarator. */
16551 declarator
16552 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16553 /*ctor_dtor_or_conv_p=*/NULL,
16554 /*parenthesized_p=*/NULL,
16555 /*member_p=*/false,
16556 /*friend_p=*/false);
16557 if (declares_class_or_enum & 2)
16558 cp_parser_check_for_definition_in_return_type (declarator,
16559 decl_specifiers.type,
16560 decl_specifiers.locations[ds_type_spec]);
16561 if (declarator != cp_error_declarator)
16562 {
16563 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16564 permerror (decl_specifiers.locations[ds_inline],
16565 "explicit instantiation shall not use"
16566 " %<inline%> specifier");
16567 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16568 permerror (decl_specifiers.locations[ds_constexpr],
16569 "explicit instantiation shall not use"
16570 " %<constexpr%> specifier");
16571
16572 decl = grokdeclarator (declarator, &decl_specifiers,
16573 NORMAL, 0, &decl_specifiers.attributes);
16574 /* Turn access control back on for names used during
16575 template instantiation. */
16576 pop_deferring_access_checks ();
16577 /* Do the explicit instantiation. */
16578 do_decl_instantiation (decl, extension_specifier);
16579 }
16580 else
16581 {
16582 pop_deferring_access_checks ();
16583 /* Skip the body of the explicit instantiation. */
16584 cp_parser_skip_to_end_of_statement (parser);
16585 }
16586 }
16587 /* We're done with the instantiation. */
16588 end_explicit_instantiation ();
16589
16590 cp_parser_consume_semicolon_at_end_of_statement (parser);
16591
16592 timevar_pop (TV_TEMPLATE_INST);
16593 }
16594
16595 /* Parse an explicit-specialization.
16596
16597 explicit-specialization:
16598 template < > declaration
16599
16600 Although the standard says `declaration', what it really means is:
16601
16602 explicit-specialization:
16603 template <> decl-specifier [opt] init-declarator [opt] ;
16604 template <> function-definition
16605 template <> explicit-specialization
16606 template <> template-declaration */
16607
16608 static void
16609 cp_parser_explicit_specialization (cp_parser* parser)
16610 {
16611 bool need_lang_pop;
16612 cp_token *token = cp_lexer_peek_token (parser->lexer);
16613
16614 /* Look for the `template' keyword. */
16615 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16616 /* Look for the `<'. */
16617 cp_parser_require (parser, CPP_LESS, RT_LESS);
16618 /* Look for the `>'. */
16619 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16620 /* We have processed another parameter list. */
16621 ++parser->num_template_parameter_lists;
16622 /* [temp]
16623
16624 A template ... explicit specialization ... shall not have C
16625 linkage. */
16626 if (current_lang_name == lang_name_c)
16627 {
16628 error_at (token->location, "template specialization with C linkage");
16629 maybe_show_extern_c_location ();
16630 /* Give it C++ linkage to avoid confusing other parts of the
16631 front end. */
16632 push_lang_context (lang_name_cplusplus);
16633 need_lang_pop = true;
16634 }
16635 else
16636 need_lang_pop = false;
16637 /* Let the front end know that we are beginning a specialization. */
16638 if (!begin_specialization ())
16639 {
16640 end_specialization ();
16641 return;
16642 }
16643
16644 /* If the next keyword is `template', we need to figure out whether
16645 or not we're looking a template-declaration. */
16646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16647 {
16648 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16649 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16650 cp_parser_template_declaration_after_export (parser,
16651 /*member_p=*/false);
16652 else
16653 cp_parser_explicit_specialization (parser);
16654 }
16655 else
16656 /* Parse the dependent declaration. */
16657 cp_parser_single_declaration (parser,
16658 /*checks=*/NULL,
16659 /*member_p=*/false,
16660 /*explicit_specialization_p=*/true,
16661 /*friend_p=*/NULL);
16662 /* We're done with the specialization. */
16663 end_specialization ();
16664 /* For the erroneous case of a template with C linkage, we pushed an
16665 implicit C++ linkage scope; exit that scope now. */
16666 if (need_lang_pop)
16667 pop_lang_context ();
16668 /* We're done with this parameter list. */
16669 --parser->num_template_parameter_lists;
16670 }
16671
16672 /* Parse a type-specifier.
16673
16674 type-specifier:
16675 simple-type-specifier
16676 class-specifier
16677 enum-specifier
16678 elaborated-type-specifier
16679 cv-qualifier
16680
16681 GNU Extension:
16682
16683 type-specifier:
16684 __complex__
16685
16686 Returns a representation of the type-specifier. For a
16687 class-specifier, enum-specifier, or elaborated-type-specifier, a
16688 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16689
16690 The parser flags FLAGS is used to control type-specifier parsing.
16691
16692 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16693 in a decl-specifier-seq.
16694
16695 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16696 class-specifier, enum-specifier, or elaborated-type-specifier, then
16697 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16698 if a type is declared; 2 if it is defined. Otherwise, it is set to
16699 zero.
16700
16701 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16702 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16703 is set to FALSE. */
16704
16705 static tree
16706 cp_parser_type_specifier (cp_parser* parser,
16707 cp_parser_flags flags,
16708 cp_decl_specifier_seq *decl_specs,
16709 bool is_declaration,
16710 int* declares_class_or_enum,
16711 bool* is_cv_qualifier)
16712 {
16713 tree type_spec = NULL_TREE;
16714 cp_token *token;
16715 enum rid keyword;
16716 cp_decl_spec ds = ds_last;
16717
16718 /* Assume this type-specifier does not declare a new type. */
16719 if (declares_class_or_enum)
16720 *declares_class_or_enum = 0;
16721 /* And that it does not specify a cv-qualifier. */
16722 if (is_cv_qualifier)
16723 *is_cv_qualifier = false;
16724 /* Peek at the next token. */
16725 token = cp_lexer_peek_token (parser->lexer);
16726
16727 /* If we're looking at a keyword, we can use that to guide the
16728 production we choose. */
16729 keyword = token->keyword;
16730 switch (keyword)
16731 {
16732 case RID_ENUM:
16733 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16734 goto elaborated_type_specifier;
16735
16736 /* Look for the enum-specifier. */
16737 type_spec = cp_parser_enum_specifier (parser);
16738 /* If that worked, we're done. */
16739 if (type_spec)
16740 {
16741 if (declares_class_or_enum)
16742 *declares_class_or_enum = 2;
16743 if (decl_specs)
16744 cp_parser_set_decl_spec_type (decl_specs,
16745 type_spec,
16746 token,
16747 /*type_definition_p=*/true);
16748 return type_spec;
16749 }
16750 else
16751 goto elaborated_type_specifier;
16752
16753 /* Any of these indicate either a class-specifier, or an
16754 elaborated-type-specifier. */
16755 case RID_CLASS:
16756 case RID_STRUCT:
16757 case RID_UNION:
16758 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16759 goto elaborated_type_specifier;
16760
16761 /* Parse tentatively so that we can back up if we don't find a
16762 class-specifier. */
16763 cp_parser_parse_tentatively (parser);
16764 /* Look for the class-specifier. */
16765 type_spec = cp_parser_class_specifier (parser);
16766 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16767 /* If that worked, we're done. */
16768 if (cp_parser_parse_definitely (parser))
16769 {
16770 if (declares_class_or_enum)
16771 *declares_class_or_enum = 2;
16772 if (decl_specs)
16773 cp_parser_set_decl_spec_type (decl_specs,
16774 type_spec,
16775 token,
16776 /*type_definition_p=*/true);
16777 return type_spec;
16778 }
16779
16780 /* Fall through. */
16781 elaborated_type_specifier:
16782 /* We're declaring (not defining) a class or enum. */
16783 if (declares_class_or_enum)
16784 *declares_class_or_enum = 1;
16785
16786 /* Fall through. */
16787 case RID_TYPENAME:
16788 /* Look for an elaborated-type-specifier. */
16789 type_spec
16790 = (cp_parser_elaborated_type_specifier
16791 (parser,
16792 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16793 is_declaration));
16794 if (decl_specs)
16795 cp_parser_set_decl_spec_type (decl_specs,
16796 type_spec,
16797 token,
16798 /*type_definition_p=*/false);
16799 return type_spec;
16800
16801 case RID_CONST:
16802 ds = ds_const;
16803 if (is_cv_qualifier)
16804 *is_cv_qualifier = true;
16805 break;
16806
16807 case RID_VOLATILE:
16808 ds = ds_volatile;
16809 if (is_cv_qualifier)
16810 *is_cv_qualifier = true;
16811 break;
16812
16813 case RID_RESTRICT:
16814 ds = ds_restrict;
16815 if (is_cv_qualifier)
16816 *is_cv_qualifier = true;
16817 break;
16818
16819 case RID_COMPLEX:
16820 /* The `__complex__' keyword is a GNU extension. */
16821 ds = ds_complex;
16822 break;
16823
16824 default:
16825 break;
16826 }
16827
16828 /* Handle simple keywords. */
16829 if (ds != ds_last)
16830 {
16831 if (decl_specs)
16832 {
16833 set_and_check_decl_spec_loc (decl_specs, ds, token);
16834 decl_specs->any_specifiers_p = true;
16835 }
16836 return cp_lexer_consume_token (parser->lexer)->u.value;
16837 }
16838
16839 /* If we do not already have a type-specifier, assume we are looking
16840 at a simple-type-specifier. */
16841 type_spec = cp_parser_simple_type_specifier (parser,
16842 decl_specs,
16843 flags);
16844
16845 /* If we didn't find a type-specifier, and a type-specifier was not
16846 optional in this context, issue an error message. */
16847 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16848 {
16849 cp_parser_error (parser, "expected type specifier");
16850 return error_mark_node;
16851 }
16852
16853 return type_spec;
16854 }
16855
16856 /* Parse a simple-type-specifier.
16857
16858 simple-type-specifier:
16859 :: [opt] nested-name-specifier [opt] type-name
16860 :: [opt] nested-name-specifier template template-id
16861 char
16862 wchar_t
16863 bool
16864 short
16865 int
16866 long
16867 signed
16868 unsigned
16869 float
16870 double
16871 void
16872
16873 C++11 Extension:
16874
16875 simple-type-specifier:
16876 auto
16877 decltype ( expression )
16878 char16_t
16879 char32_t
16880 __underlying_type ( type-id )
16881
16882 C++17 extension:
16883
16884 nested-name-specifier(opt) template-name
16885
16886 GNU Extension:
16887
16888 simple-type-specifier:
16889 __int128
16890 __typeof__ unary-expression
16891 __typeof__ ( type-id )
16892 __typeof__ ( type-id ) { initializer-list , [opt] }
16893
16894 Concepts Extension:
16895
16896 simple-type-specifier:
16897 constrained-type-specifier
16898
16899 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16900 appropriately updated. */
16901
16902 static tree
16903 cp_parser_simple_type_specifier (cp_parser* parser,
16904 cp_decl_specifier_seq *decl_specs,
16905 cp_parser_flags flags)
16906 {
16907 tree type = NULL_TREE;
16908 cp_token *token;
16909 int idx;
16910
16911 /* Peek at the next token. */
16912 token = cp_lexer_peek_token (parser->lexer);
16913
16914 /* If we're looking at a keyword, things are easy. */
16915 switch (token->keyword)
16916 {
16917 case RID_CHAR:
16918 if (decl_specs)
16919 decl_specs->explicit_char_p = true;
16920 type = char_type_node;
16921 break;
16922 case RID_CHAR16:
16923 type = char16_type_node;
16924 break;
16925 case RID_CHAR32:
16926 type = char32_type_node;
16927 break;
16928 case RID_WCHAR:
16929 type = wchar_type_node;
16930 break;
16931 case RID_BOOL:
16932 type = boolean_type_node;
16933 break;
16934 case RID_SHORT:
16935 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16936 type = short_integer_type_node;
16937 break;
16938 case RID_INT:
16939 if (decl_specs)
16940 decl_specs->explicit_int_p = true;
16941 type = integer_type_node;
16942 break;
16943 case RID_INT_N_0:
16944 case RID_INT_N_1:
16945 case RID_INT_N_2:
16946 case RID_INT_N_3:
16947 idx = token->keyword - RID_INT_N_0;
16948 if (! int_n_enabled_p [idx])
16949 break;
16950 if (decl_specs)
16951 {
16952 decl_specs->explicit_intN_p = true;
16953 decl_specs->int_n_idx = idx;
16954 }
16955 type = int_n_trees [idx].signed_type;
16956 break;
16957 case RID_LONG:
16958 if (decl_specs)
16959 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16960 type = long_integer_type_node;
16961 break;
16962 case RID_SIGNED:
16963 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16964 type = integer_type_node;
16965 break;
16966 case RID_UNSIGNED:
16967 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16968 type = unsigned_type_node;
16969 break;
16970 case RID_FLOAT:
16971 type = float_type_node;
16972 break;
16973 case RID_DOUBLE:
16974 type = double_type_node;
16975 break;
16976 case RID_VOID:
16977 type = void_type_node;
16978 break;
16979
16980 case RID_AUTO:
16981 maybe_warn_cpp0x (CPP0X_AUTO);
16982 if (parser->auto_is_implicit_function_template_parm_p)
16983 {
16984 /* The 'auto' might be the placeholder return type for a function decl
16985 with trailing return type. */
16986 bool have_trailing_return_fn_decl = false;
16987
16988 cp_parser_parse_tentatively (parser);
16989 cp_lexer_consume_token (parser->lexer);
16990 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16991 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16992 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16993 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16994 {
16995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16996 {
16997 cp_lexer_consume_token (parser->lexer);
16998 cp_parser_skip_to_closing_parenthesis (parser,
16999 /*recovering*/false,
17000 /*or_comma*/false,
17001 /*consume_paren*/true);
17002 continue;
17003 }
17004
17005 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17006 {
17007 have_trailing_return_fn_decl = true;
17008 break;
17009 }
17010
17011 cp_lexer_consume_token (parser->lexer);
17012 }
17013 cp_parser_abort_tentative_parse (parser);
17014
17015 if (have_trailing_return_fn_decl)
17016 {
17017 type = make_auto ();
17018 break;
17019 }
17020
17021 if (cxx_dialect >= cxx14)
17022 {
17023 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17024 type = TREE_TYPE (type);
17025 }
17026 else
17027 type = error_mark_node;
17028
17029 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17030 {
17031 if (cxx_dialect < cxx14)
17032 error_at (token->location,
17033 "use of %<auto%> in lambda parameter declaration "
17034 "only available with "
17035 "-std=c++14 or -std=gnu++14");
17036 }
17037 else if (cxx_dialect < cxx14)
17038 error_at (token->location,
17039 "use of %<auto%> in parameter declaration "
17040 "only available with "
17041 "-std=c++14 or -std=gnu++14");
17042 else if (!flag_concepts)
17043 pedwarn (token->location, OPT_Wpedantic,
17044 "ISO C++ forbids use of %<auto%> in parameter "
17045 "declaration");
17046 }
17047 else
17048 type = make_auto ();
17049 break;
17050
17051 case RID_DECLTYPE:
17052 /* Since DR 743, decltype can either be a simple-type-specifier by
17053 itself or begin a nested-name-specifier. Parsing it will replace
17054 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17055 handling below decide what to do. */
17056 cp_parser_decltype (parser);
17057 cp_lexer_set_token_position (parser->lexer, token);
17058 break;
17059
17060 case RID_TYPEOF:
17061 /* Consume the `typeof' token. */
17062 cp_lexer_consume_token (parser->lexer);
17063 /* Parse the operand to `typeof'. */
17064 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17065 /* If it is not already a TYPE, take its type. */
17066 if (!TYPE_P (type))
17067 type = finish_typeof (type);
17068
17069 if (decl_specs)
17070 cp_parser_set_decl_spec_type (decl_specs, type,
17071 token,
17072 /*type_definition_p=*/false);
17073
17074 return type;
17075
17076 case RID_UNDERLYING_TYPE:
17077 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17078 if (decl_specs)
17079 cp_parser_set_decl_spec_type (decl_specs, type,
17080 token,
17081 /*type_definition_p=*/false);
17082
17083 return type;
17084
17085 case RID_BASES:
17086 case RID_DIRECT_BASES:
17087 type = cp_parser_trait_expr (parser, token->keyword);
17088 if (decl_specs)
17089 cp_parser_set_decl_spec_type (decl_specs, type,
17090 token,
17091 /*type_definition_p=*/false);
17092 return type;
17093 default:
17094 break;
17095 }
17096
17097 /* If token is an already-parsed decltype not followed by ::,
17098 it's a simple-type-specifier. */
17099 if (token->type == CPP_DECLTYPE
17100 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17101 {
17102 type = saved_checks_value (token->u.tree_check_value);
17103 if (decl_specs)
17104 {
17105 cp_parser_set_decl_spec_type (decl_specs, type,
17106 token,
17107 /*type_definition_p=*/false);
17108 /* Remember that we are handling a decltype in order to
17109 implement the resolution of DR 1510 when the argument
17110 isn't instantiation dependent. */
17111 decl_specs->decltype_p = true;
17112 }
17113 cp_lexer_consume_token (parser->lexer);
17114 return type;
17115 }
17116
17117 /* If the type-specifier was for a built-in type, we're done. */
17118 if (type)
17119 {
17120 /* Record the type. */
17121 if (decl_specs
17122 && (token->keyword != RID_SIGNED
17123 && token->keyword != RID_UNSIGNED
17124 && token->keyword != RID_SHORT
17125 && token->keyword != RID_LONG))
17126 cp_parser_set_decl_spec_type (decl_specs,
17127 type,
17128 token,
17129 /*type_definition_p=*/false);
17130 if (decl_specs)
17131 decl_specs->any_specifiers_p = true;
17132
17133 /* Consume the token. */
17134 cp_lexer_consume_token (parser->lexer);
17135
17136 if (type == error_mark_node)
17137 return error_mark_node;
17138
17139 /* There is no valid C++ program where a non-template type is
17140 followed by a "<". That usually indicates that the user thought
17141 that the type was a template. */
17142 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17143 token->location);
17144
17145 return TYPE_NAME (type);
17146 }
17147
17148 /* The type-specifier must be a user-defined type. */
17149 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17150 {
17151 bool qualified_p;
17152 bool global_p;
17153
17154 /* Don't gobble tokens or issue error messages if this is an
17155 optional type-specifier. */
17156 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17157 cp_parser_parse_tentatively (parser);
17158
17159 token = cp_lexer_peek_token (parser->lexer);
17160
17161 /* Look for the optional `::' operator. */
17162 global_p
17163 = (cp_parser_global_scope_opt (parser,
17164 /*current_scope_valid_p=*/false)
17165 != NULL_TREE);
17166 /* Look for the nested-name specifier. */
17167 qualified_p
17168 = (cp_parser_nested_name_specifier_opt (parser,
17169 /*typename_keyword_p=*/false,
17170 /*check_dependency_p=*/true,
17171 /*type_p=*/false,
17172 /*is_declaration=*/false)
17173 != NULL_TREE);
17174 /* If we have seen a nested-name-specifier, and the next token
17175 is `template', then we are using the template-id production. */
17176 if (parser->scope
17177 && cp_parser_optional_template_keyword (parser))
17178 {
17179 /* Look for the template-id. */
17180 type = cp_parser_template_id (parser,
17181 /*template_keyword_p=*/true,
17182 /*check_dependency_p=*/true,
17183 none_type,
17184 /*is_declaration=*/false);
17185 /* If the template-id did not name a type, we are out of
17186 luck. */
17187 if (TREE_CODE (type) != TYPE_DECL)
17188 {
17189 cp_parser_error (parser, "expected template-id for type");
17190 type = NULL_TREE;
17191 }
17192 }
17193 /* Otherwise, look for a type-name. */
17194 else
17195 type = cp_parser_type_name (parser);
17196 /* Keep track of all name-lookups performed in class scopes. */
17197 if (type
17198 && !global_p
17199 && !qualified_p
17200 && TREE_CODE (type) == TYPE_DECL
17201 && identifier_p (DECL_NAME (type)))
17202 maybe_note_name_used_in_class (DECL_NAME (type), type);
17203 /* If it didn't work out, we don't have a TYPE. */
17204 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17205 && !cp_parser_parse_definitely (parser))
17206 type = NULL_TREE;
17207 if (!type && cxx_dialect >= cxx17)
17208 {
17209 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17210 cp_parser_parse_tentatively (parser);
17211
17212 cp_parser_global_scope_opt (parser,
17213 /*current_scope_valid_p=*/false);
17214 cp_parser_nested_name_specifier_opt (parser,
17215 /*typename_keyword_p=*/false,
17216 /*check_dependency_p=*/true,
17217 /*type_p=*/false,
17218 /*is_declaration=*/false);
17219 tree name = cp_parser_identifier (parser);
17220 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17221 && parser->scope != error_mark_node)
17222 {
17223 tree tmpl = cp_parser_lookup_name (parser, name,
17224 none_type,
17225 /*is_template=*/false,
17226 /*is_namespace=*/false,
17227 /*check_dependency=*/true,
17228 /*ambiguous_decls=*/NULL,
17229 token->location);
17230 if (tmpl && tmpl != error_mark_node
17231 && (DECL_CLASS_TEMPLATE_P (tmpl)
17232 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17233 type = make_template_placeholder (tmpl);
17234 else
17235 {
17236 type = error_mark_node;
17237 if (!cp_parser_simulate_error (parser))
17238 cp_parser_name_lookup_error (parser, name, tmpl,
17239 NLE_TYPE, token->location);
17240 }
17241 }
17242 else
17243 type = error_mark_node;
17244
17245 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17246 && !cp_parser_parse_definitely (parser))
17247 type = NULL_TREE;
17248 }
17249 if (type && decl_specs)
17250 cp_parser_set_decl_spec_type (decl_specs, type,
17251 token,
17252 /*type_definition_p=*/false);
17253 }
17254
17255 /* If we didn't get a type-name, issue an error message. */
17256 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17257 {
17258 cp_parser_error (parser, "expected type-name");
17259 return error_mark_node;
17260 }
17261
17262 if (type && type != error_mark_node)
17263 {
17264 /* See if TYPE is an Objective-C type, and if so, parse and
17265 accept any protocol references following it. Do this before
17266 the cp_parser_check_for_invalid_template_id() call, because
17267 Objective-C types can be followed by '<...>' which would
17268 enclose protocol names rather than template arguments, and so
17269 everything is fine. */
17270 if (c_dialect_objc () && !parser->scope
17271 && (objc_is_id (type) || objc_is_class_name (type)))
17272 {
17273 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17274 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17275
17276 /* Clobber the "unqualified" type previously entered into
17277 DECL_SPECS with the new, improved protocol-qualified version. */
17278 if (decl_specs)
17279 decl_specs->type = qual_type;
17280
17281 return qual_type;
17282 }
17283
17284 /* There is no valid C++ program where a non-template type is
17285 followed by a "<". That usually indicates that the user
17286 thought that the type was a template. */
17287 cp_parser_check_for_invalid_template_id (parser, type,
17288 none_type,
17289 token->location);
17290 }
17291
17292 return type;
17293 }
17294
17295 /* Parse a type-name.
17296
17297 type-name:
17298 class-name
17299 enum-name
17300 typedef-name
17301 simple-template-id [in c++0x]
17302
17303 enum-name:
17304 identifier
17305
17306 typedef-name:
17307 identifier
17308
17309 Concepts:
17310
17311 type-name:
17312 concept-name
17313 partial-concept-id
17314
17315 concept-name:
17316 identifier
17317
17318 Returns a TYPE_DECL for the type. */
17319
17320 static tree
17321 cp_parser_type_name (cp_parser* parser)
17322 {
17323 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17324 }
17325
17326 /* See above. */
17327 static tree
17328 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17329 {
17330 tree type_decl;
17331
17332 /* We can't know yet whether it is a class-name or not. */
17333 cp_parser_parse_tentatively (parser);
17334 /* Try a class-name. */
17335 type_decl = cp_parser_class_name (parser,
17336 typename_keyword_p,
17337 /*template_keyword_p=*/false,
17338 none_type,
17339 /*check_dependency_p=*/true,
17340 /*class_head_p=*/false,
17341 /*is_declaration=*/false);
17342 /* If it's not a class-name, keep looking. */
17343 if (!cp_parser_parse_definitely (parser))
17344 {
17345 if (cxx_dialect < cxx11)
17346 /* It must be a typedef-name or an enum-name. */
17347 return cp_parser_nonclass_name (parser);
17348
17349 cp_parser_parse_tentatively (parser);
17350 /* It is either a simple-template-id representing an
17351 instantiation of an alias template... */
17352 type_decl = cp_parser_template_id (parser,
17353 /*template_keyword_p=*/false,
17354 /*check_dependency_p=*/true,
17355 none_type,
17356 /*is_declaration=*/false);
17357 /* Note that this must be an instantiation of an alias template
17358 because [temp.names]/6 says:
17359
17360 A template-id that names an alias template specialization
17361 is a type-name.
17362
17363 Whereas [temp.names]/7 says:
17364
17365 A simple-template-id that names a class template
17366 specialization is a class-name.
17367
17368 With concepts, this could also be a partial-concept-id that
17369 declares a non-type template parameter. */
17370 if (type_decl != NULL_TREE
17371 && TREE_CODE (type_decl) == TYPE_DECL
17372 && TYPE_DECL_ALIAS_P (type_decl))
17373 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17374 else if (is_constrained_parameter (type_decl))
17375 /* Don't do anything. */ ;
17376 else
17377 cp_parser_simulate_error (parser);
17378
17379 if (!cp_parser_parse_definitely (parser))
17380 /* ... Or a typedef-name or an enum-name. */
17381 return cp_parser_nonclass_name (parser);
17382 }
17383
17384 return type_decl;
17385 }
17386
17387 /* Check if DECL and ARGS can form a constrained-type-specifier.
17388 If ARGS is non-null, we try to form a concept check of the
17389 form DECL<?, ARGS> where ? is a wildcard that matches any
17390 kind of template argument. If ARGS is NULL, then we try to
17391 form a concept check of the form DECL<?>. */
17392
17393 static tree
17394 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17395 tree decl, tree args)
17396 {
17397 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17398
17399 /* If we a constrained-type-specifier cannot be deduced. */
17400 if (parser->prevent_constrained_type_specifiers)
17401 return NULL_TREE;
17402
17403 /* A constrained type specifier can only be found in an
17404 overload set or as a reference to a template declaration.
17405
17406 FIXME: This might be masking a bug. It's possible that
17407 that the deduction below is causing template specializations
17408 to be formed with the wildcard as an argument. */
17409 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17410 return NULL_TREE;
17411
17412 /* Try to build a call expression that evaluates the
17413 concept. This can fail if the overload set refers
17414 only to non-templates. */
17415 tree placeholder = build_nt (WILDCARD_DECL);
17416 tree check = build_concept_check (decl, placeholder, args);
17417 if (check == error_mark_node)
17418 return NULL_TREE;
17419
17420 /* Deduce the checked constraint and the prototype parameter.
17421
17422 FIXME: In certain cases, failure to deduce should be a
17423 diagnosable error. */
17424 tree conc;
17425 tree proto;
17426 if (!deduce_constrained_parameter (check, conc, proto))
17427 return NULL_TREE;
17428
17429 /* In template parameter scope, this results in a constrained
17430 parameter. Return a descriptor of that parm. */
17431 if (processing_template_parmlist)
17432 return build_constrained_parameter (conc, proto, args);
17433
17434 /* In a parameter-declaration-clause, constrained-type
17435 specifiers result in invented template parameters. */
17436 if (parser->auto_is_implicit_function_template_parm_p)
17437 {
17438 tree x = build_constrained_parameter (conc, proto, args);
17439 return synthesize_implicit_template_parm (parser, x);
17440 }
17441 else
17442 {
17443 /* Otherwise, we're in a context where the constrained
17444 type name is deduced and the constraint applies
17445 after deduction. */
17446 return make_constrained_auto (conc, args);
17447 }
17448
17449 return NULL_TREE;
17450 }
17451
17452 /* If DECL refers to a concept, return a TYPE_DECL representing
17453 the result of using the constrained type specifier in the
17454 current context. DECL refers to a concept if
17455
17456 - it is an overload set containing a function concept taking a single
17457 type argument, or
17458
17459 - it is a variable concept taking a single type argument. */
17460
17461 static tree
17462 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17463 {
17464 if (flag_concepts
17465 && (TREE_CODE (decl) == OVERLOAD
17466 || BASELINK_P (decl)
17467 || variable_concept_p (decl)))
17468 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17469 else
17470 return NULL_TREE;
17471 }
17472
17473 /* Check if DECL and ARGS form a partial-concept-id. If so,
17474 assign ID to the resulting constrained placeholder.
17475
17476 Returns true if the partial-concept-id designates a placeholder
17477 and false otherwise. Note that *id is set to NULL_TREE in
17478 this case. */
17479
17480 static tree
17481 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17482 {
17483 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17484 }
17485
17486 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17487 or a concept-name.
17488
17489 enum-name:
17490 identifier
17491
17492 typedef-name:
17493 identifier
17494
17495 concept-name:
17496 identifier
17497
17498 Returns a TYPE_DECL for the type. */
17499
17500 static tree
17501 cp_parser_nonclass_name (cp_parser* parser)
17502 {
17503 tree type_decl;
17504 tree identifier;
17505
17506 cp_token *token = cp_lexer_peek_token (parser->lexer);
17507 identifier = cp_parser_identifier (parser);
17508 if (identifier == error_mark_node)
17509 return error_mark_node;
17510
17511 /* Look up the type-name. */
17512 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17513
17514 type_decl = strip_using_decl (type_decl);
17515
17516 /* If we found an overload set, then it may refer to a concept-name. */
17517 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17518 type_decl = decl;
17519
17520 if (TREE_CODE (type_decl) != TYPE_DECL
17521 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17522 {
17523 /* See if this is an Objective-C type. */
17524 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17525 tree type = objc_get_protocol_qualified_type (identifier, protos);
17526 if (type)
17527 type_decl = TYPE_NAME (type);
17528 }
17529
17530 /* Issue an error if we did not find a type-name. */
17531 if (TREE_CODE (type_decl) != TYPE_DECL
17532 /* In Objective-C, we have the complication that class names are
17533 normally type names and start declarations (eg, the
17534 "NSObject" in "NSObject *object;"), but can be used in an
17535 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17536 is an expression. So, a classname followed by a dot is not a
17537 valid type-name. */
17538 || (objc_is_class_name (TREE_TYPE (type_decl))
17539 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17540 {
17541 if (!cp_parser_simulate_error (parser))
17542 cp_parser_name_lookup_error (parser, identifier, type_decl,
17543 NLE_TYPE, token->location);
17544 return error_mark_node;
17545 }
17546 /* Remember that the name was used in the definition of the
17547 current class so that we can check later to see if the
17548 meaning would have been different after the class was
17549 entirely defined. */
17550 else if (type_decl != error_mark_node
17551 && !parser->scope)
17552 maybe_note_name_used_in_class (identifier, type_decl);
17553
17554 return type_decl;
17555 }
17556
17557 /* Parse an elaborated-type-specifier. Note that the grammar given
17558 here incorporates the resolution to DR68.
17559
17560 elaborated-type-specifier:
17561 class-key :: [opt] nested-name-specifier [opt] identifier
17562 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17563 enum-key :: [opt] nested-name-specifier [opt] identifier
17564 typename :: [opt] nested-name-specifier identifier
17565 typename :: [opt] nested-name-specifier template [opt]
17566 template-id
17567
17568 GNU extension:
17569
17570 elaborated-type-specifier:
17571 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17572 class-key attributes :: [opt] nested-name-specifier [opt]
17573 template [opt] template-id
17574 enum attributes :: [opt] nested-name-specifier [opt] identifier
17575
17576 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17577 declared `friend'. If IS_DECLARATION is TRUE, then this
17578 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17579 something is being declared.
17580
17581 Returns the TYPE specified. */
17582
17583 static tree
17584 cp_parser_elaborated_type_specifier (cp_parser* parser,
17585 bool is_friend,
17586 bool is_declaration)
17587 {
17588 enum tag_types tag_type;
17589 tree identifier;
17590 tree type = NULL_TREE;
17591 tree attributes = NULL_TREE;
17592 tree globalscope;
17593 cp_token *token = NULL;
17594
17595 /* See if we're looking at the `enum' keyword. */
17596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17597 {
17598 /* Consume the `enum' token. */
17599 cp_lexer_consume_token (parser->lexer);
17600 /* Remember that it's an enumeration type. */
17601 tag_type = enum_type;
17602 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17603 enums) is used here. */
17604 cp_token *token = cp_lexer_peek_token (parser->lexer);
17605 if (cp_parser_is_keyword (token, RID_CLASS)
17606 || cp_parser_is_keyword (token, RID_STRUCT))
17607 {
17608 gcc_rich_location richloc (token->location);
17609 richloc.add_range (input_location, false);
17610 richloc.add_fixit_remove ();
17611 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17612 "a scoped enum must not use the %qD keyword",
17613 token->u.value);
17614 /* Consume the `struct' or `class' and parse it anyway. */
17615 cp_lexer_consume_token (parser->lexer);
17616 }
17617 /* Parse the attributes. */
17618 attributes = cp_parser_attributes_opt (parser);
17619 }
17620 /* Or, it might be `typename'. */
17621 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17622 RID_TYPENAME))
17623 {
17624 /* Consume the `typename' token. */
17625 cp_lexer_consume_token (parser->lexer);
17626 /* Remember that it's a `typename' type. */
17627 tag_type = typename_type;
17628 }
17629 /* Otherwise it must be a class-key. */
17630 else
17631 {
17632 tag_type = cp_parser_class_key (parser);
17633 if (tag_type == none_type)
17634 return error_mark_node;
17635 /* Parse the attributes. */
17636 attributes = cp_parser_attributes_opt (parser);
17637 }
17638
17639 /* Look for the `::' operator. */
17640 globalscope = cp_parser_global_scope_opt (parser,
17641 /*current_scope_valid_p=*/false);
17642 /* Look for the nested-name-specifier. */
17643 tree nested_name_specifier;
17644 if (tag_type == typename_type && !globalscope)
17645 {
17646 nested_name_specifier
17647 = cp_parser_nested_name_specifier (parser,
17648 /*typename_keyword_p=*/true,
17649 /*check_dependency_p=*/true,
17650 /*type_p=*/true,
17651 is_declaration);
17652 if (!nested_name_specifier)
17653 return error_mark_node;
17654 }
17655 else
17656 /* Even though `typename' is not present, the proposed resolution
17657 to Core Issue 180 says that in `class A<T>::B', `B' should be
17658 considered a type-name, even if `A<T>' is dependent. */
17659 nested_name_specifier
17660 = cp_parser_nested_name_specifier_opt (parser,
17661 /*typename_keyword_p=*/true,
17662 /*check_dependency_p=*/true,
17663 /*type_p=*/true,
17664 is_declaration);
17665 /* For everything but enumeration types, consider a template-id.
17666 For an enumeration type, consider only a plain identifier. */
17667 if (tag_type != enum_type)
17668 {
17669 bool template_p = false;
17670 tree decl;
17671
17672 /* Allow the `template' keyword. */
17673 template_p = cp_parser_optional_template_keyword (parser);
17674 /* If we didn't see `template', we don't know if there's a
17675 template-id or not. */
17676 if (!template_p)
17677 cp_parser_parse_tentatively (parser);
17678 /* Parse the template-id. */
17679 token = cp_lexer_peek_token (parser->lexer);
17680 decl = cp_parser_template_id (parser, template_p,
17681 /*check_dependency_p=*/true,
17682 tag_type,
17683 is_declaration);
17684 /* If we didn't find a template-id, look for an ordinary
17685 identifier. */
17686 if (!template_p && !cp_parser_parse_definitely (parser))
17687 ;
17688 /* We can get here when cp_parser_template_id, called by
17689 cp_parser_class_name with tag_type == none_type, succeeds
17690 and caches a BASELINK. Then, when called again here,
17691 instead of failing and returning an error_mark_node
17692 returns it (see template/typename17.C in C++11).
17693 ??? Could we diagnose this earlier? */
17694 else if (tag_type == typename_type && BASELINK_P (decl))
17695 {
17696 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17697 type = error_mark_node;
17698 }
17699 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17700 in effect, then we must assume that, upon instantiation, the
17701 template will correspond to a class. */
17702 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17703 && tag_type == typename_type)
17704 type = make_typename_type (parser->scope, decl,
17705 typename_type,
17706 /*complain=*/tf_error);
17707 /* If the `typename' keyword is in effect and DECL is not a type
17708 decl, then type is non existent. */
17709 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17710 ;
17711 else if (TREE_CODE (decl) == TYPE_DECL)
17712 {
17713 type = check_elaborated_type_specifier (tag_type, decl,
17714 /*allow_template_p=*/true);
17715
17716 /* If the next token is a semicolon, this must be a specialization,
17717 instantiation, or friend declaration. Check the scope while we
17718 still know whether or not we had a nested-name-specifier. */
17719 if (type != error_mark_node
17720 && !nested_name_specifier && !is_friend
17721 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17722 check_unqualified_spec_or_inst (type, token->location);
17723 }
17724 else if (decl == error_mark_node)
17725 type = error_mark_node;
17726 }
17727
17728 if (!type)
17729 {
17730 token = cp_lexer_peek_token (parser->lexer);
17731 identifier = cp_parser_identifier (parser);
17732
17733 if (identifier == error_mark_node)
17734 {
17735 parser->scope = NULL_TREE;
17736 return error_mark_node;
17737 }
17738
17739 /* For a `typename', we needn't call xref_tag. */
17740 if (tag_type == typename_type
17741 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17742 return cp_parser_make_typename_type (parser, identifier,
17743 token->location);
17744
17745 /* Template parameter lists apply only if we are not within a
17746 function parameter list. */
17747 bool template_parm_lists_apply
17748 = parser->num_template_parameter_lists;
17749 if (template_parm_lists_apply)
17750 for (cp_binding_level *s = current_binding_level;
17751 s && s->kind != sk_template_parms;
17752 s = s->level_chain)
17753 if (s->kind == sk_function_parms)
17754 template_parm_lists_apply = false;
17755
17756 /* Look up a qualified name in the usual way. */
17757 if (parser->scope)
17758 {
17759 tree decl;
17760 tree ambiguous_decls;
17761
17762 decl = cp_parser_lookup_name (parser, identifier,
17763 tag_type,
17764 /*is_template=*/false,
17765 /*is_namespace=*/false,
17766 /*check_dependency=*/true,
17767 &ambiguous_decls,
17768 token->location);
17769
17770 /* If the lookup was ambiguous, an error will already have been
17771 issued. */
17772 if (ambiguous_decls)
17773 return error_mark_node;
17774
17775 /* If we are parsing friend declaration, DECL may be a
17776 TEMPLATE_DECL tree node here. However, we need to check
17777 whether this TEMPLATE_DECL results in valid code. Consider
17778 the following example:
17779
17780 namespace N {
17781 template <class T> class C {};
17782 }
17783 class X {
17784 template <class T> friend class N::C; // #1, valid code
17785 };
17786 template <class T> class Y {
17787 friend class N::C; // #2, invalid code
17788 };
17789
17790 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17791 name lookup of `N::C'. We see that friend declaration must
17792 be template for the code to be valid. Note that
17793 processing_template_decl does not work here since it is
17794 always 1 for the above two cases. */
17795
17796 decl = (cp_parser_maybe_treat_template_as_class
17797 (decl, /*tag_name_p=*/is_friend
17798 && template_parm_lists_apply));
17799
17800 if (TREE_CODE (decl) != TYPE_DECL)
17801 {
17802 cp_parser_diagnose_invalid_type_name (parser,
17803 identifier,
17804 token->location);
17805 return error_mark_node;
17806 }
17807
17808 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17809 {
17810 bool allow_template = (template_parm_lists_apply
17811 || DECL_SELF_REFERENCE_P (decl));
17812 type = check_elaborated_type_specifier (tag_type, decl,
17813 allow_template);
17814
17815 if (type == error_mark_node)
17816 return error_mark_node;
17817 }
17818
17819 /* Forward declarations of nested types, such as
17820
17821 class C1::C2;
17822 class C1::C2::C3;
17823
17824 are invalid unless all components preceding the final '::'
17825 are complete. If all enclosing types are complete, these
17826 declarations become merely pointless.
17827
17828 Invalid forward declarations of nested types are errors
17829 caught elsewhere in parsing. Those that are pointless arrive
17830 here. */
17831
17832 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17833 && !is_friend && !processing_explicit_instantiation)
17834 warning (0, "declaration %qD does not declare anything", decl);
17835
17836 type = TREE_TYPE (decl);
17837 }
17838 else
17839 {
17840 /* An elaborated-type-specifier sometimes introduces a new type and
17841 sometimes names an existing type. Normally, the rule is that it
17842 introduces a new type only if there is not an existing type of
17843 the same name already in scope. For example, given:
17844
17845 struct S {};
17846 void f() { struct S s; }
17847
17848 the `struct S' in the body of `f' is the same `struct S' as in
17849 the global scope; the existing definition is used. However, if
17850 there were no global declaration, this would introduce a new
17851 local class named `S'.
17852
17853 An exception to this rule applies to the following code:
17854
17855 namespace N { struct S; }
17856
17857 Here, the elaborated-type-specifier names a new type
17858 unconditionally; even if there is already an `S' in the
17859 containing scope this declaration names a new type.
17860 This exception only applies if the elaborated-type-specifier
17861 forms the complete declaration:
17862
17863 [class.name]
17864
17865 A declaration consisting solely of `class-key identifier ;' is
17866 either a redeclaration of the name in the current scope or a
17867 forward declaration of the identifier as a class name. It
17868 introduces the name into the current scope.
17869
17870 We are in this situation precisely when the next token is a `;'.
17871
17872 An exception to the exception is that a `friend' declaration does
17873 *not* name a new type; i.e., given:
17874
17875 struct S { friend struct T; };
17876
17877 `T' is not a new type in the scope of `S'.
17878
17879 Also, `new struct S' or `sizeof (struct S)' never results in the
17880 definition of a new type; a new type can only be declared in a
17881 declaration context. */
17882
17883 tag_scope ts;
17884 bool template_p;
17885
17886 if (is_friend)
17887 /* Friends have special name lookup rules. */
17888 ts = ts_within_enclosing_non_class;
17889 else if (is_declaration
17890 && cp_lexer_next_token_is (parser->lexer,
17891 CPP_SEMICOLON))
17892 /* This is a `class-key identifier ;' */
17893 ts = ts_current;
17894 else
17895 ts = ts_global;
17896
17897 template_p =
17898 (template_parm_lists_apply
17899 && (cp_parser_next_token_starts_class_definition_p (parser)
17900 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17901 /* An unqualified name was used to reference this type, so
17902 there were no qualifying templates. */
17903 if (template_parm_lists_apply
17904 && !cp_parser_check_template_parameters (parser,
17905 /*num_templates=*/0,
17906 token->location,
17907 /*declarator=*/NULL))
17908 return error_mark_node;
17909 type = xref_tag (tag_type, identifier, ts, template_p);
17910 }
17911 }
17912
17913 if (type == error_mark_node)
17914 return error_mark_node;
17915
17916 /* Allow attributes on forward declarations of classes. */
17917 if (attributes)
17918 {
17919 if (TREE_CODE (type) == TYPENAME_TYPE)
17920 warning (OPT_Wattributes,
17921 "attributes ignored on uninstantiated type");
17922 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17923 && ! processing_explicit_instantiation)
17924 warning (OPT_Wattributes,
17925 "attributes ignored on template instantiation");
17926 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17927 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17928 else
17929 warning (OPT_Wattributes,
17930 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17931 }
17932
17933 if (tag_type != enum_type)
17934 {
17935 /* Indicate whether this class was declared as a `class' or as a
17936 `struct'. */
17937 if (CLASS_TYPE_P (type))
17938 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17939 cp_parser_check_class_key (tag_type, type);
17940 }
17941
17942 /* A "<" cannot follow an elaborated type specifier. If that
17943 happens, the user was probably trying to form a template-id. */
17944 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17945 token->location);
17946
17947 return type;
17948 }
17949
17950 /* Parse an enum-specifier.
17951
17952 enum-specifier:
17953 enum-head { enumerator-list [opt] }
17954 enum-head { enumerator-list , } [C++0x]
17955
17956 enum-head:
17957 enum-key identifier [opt] enum-base [opt]
17958 enum-key nested-name-specifier identifier enum-base [opt]
17959
17960 enum-key:
17961 enum
17962 enum class [C++0x]
17963 enum struct [C++0x]
17964
17965 enum-base: [C++0x]
17966 : type-specifier-seq
17967
17968 opaque-enum-specifier:
17969 enum-key identifier enum-base [opt] ;
17970
17971 GNU Extensions:
17972 enum-key attributes[opt] identifier [opt] enum-base [opt]
17973 { enumerator-list [opt] }attributes[opt]
17974 enum-key attributes[opt] identifier [opt] enum-base [opt]
17975 { enumerator-list, }attributes[opt] [C++0x]
17976
17977 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17978 if the token stream isn't an enum-specifier after all. */
17979
17980 static tree
17981 cp_parser_enum_specifier (cp_parser* parser)
17982 {
17983 tree identifier;
17984 tree type = NULL_TREE;
17985 tree prev_scope;
17986 tree nested_name_specifier = NULL_TREE;
17987 tree attributes;
17988 bool scoped_enum_p = false;
17989 bool has_underlying_type = false;
17990 bool nested_being_defined = false;
17991 bool new_value_list = false;
17992 bool is_new_type = false;
17993 bool is_unnamed = false;
17994 tree underlying_type = NULL_TREE;
17995 cp_token *type_start_token = NULL;
17996 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17997
17998 parser->colon_corrects_to_scope_p = false;
17999
18000 /* Parse tentatively so that we can back up if we don't find a
18001 enum-specifier. */
18002 cp_parser_parse_tentatively (parser);
18003
18004 /* Caller guarantees that the current token is 'enum', an identifier
18005 possibly follows, and the token after that is an opening brace.
18006 If we don't have an identifier, fabricate an anonymous name for
18007 the enumeration being defined. */
18008 cp_lexer_consume_token (parser->lexer);
18009
18010 /* Parse the "class" or "struct", which indicates a scoped
18011 enumeration type in C++0x. */
18012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18013 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18014 {
18015 if (cxx_dialect < cxx11)
18016 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18017
18018 /* Consume the `struct' or `class' token. */
18019 cp_lexer_consume_token (parser->lexer);
18020
18021 scoped_enum_p = true;
18022 }
18023
18024 attributes = cp_parser_attributes_opt (parser);
18025
18026 /* Clear the qualification. */
18027 parser->scope = NULL_TREE;
18028 parser->qualifying_scope = NULL_TREE;
18029 parser->object_scope = NULL_TREE;
18030
18031 /* Figure out in what scope the declaration is being placed. */
18032 prev_scope = current_scope ();
18033
18034 type_start_token = cp_lexer_peek_token (parser->lexer);
18035
18036 push_deferring_access_checks (dk_no_check);
18037 nested_name_specifier
18038 = cp_parser_nested_name_specifier_opt (parser,
18039 /*typename_keyword_p=*/true,
18040 /*check_dependency_p=*/false,
18041 /*type_p=*/false,
18042 /*is_declaration=*/false);
18043
18044 if (nested_name_specifier)
18045 {
18046 tree name;
18047
18048 identifier = cp_parser_identifier (parser);
18049 name = cp_parser_lookup_name (parser, identifier,
18050 enum_type,
18051 /*is_template=*/false,
18052 /*is_namespace=*/false,
18053 /*check_dependency=*/true,
18054 /*ambiguous_decls=*/NULL,
18055 input_location);
18056 if (name && name != error_mark_node)
18057 {
18058 type = TREE_TYPE (name);
18059 if (TREE_CODE (type) == TYPENAME_TYPE)
18060 {
18061 /* Are template enums allowed in ISO? */
18062 if (template_parm_scope_p ())
18063 pedwarn (type_start_token->location, OPT_Wpedantic,
18064 "%qD is an enumeration template", name);
18065 /* ignore a typename reference, for it will be solved by name
18066 in start_enum. */
18067 type = NULL_TREE;
18068 }
18069 }
18070 else if (nested_name_specifier == error_mark_node)
18071 /* We already issued an error. */;
18072 else
18073 {
18074 error_at (type_start_token->location,
18075 "%qD does not name an enumeration in %qT",
18076 identifier, nested_name_specifier);
18077 nested_name_specifier = error_mark_node;
18078 }
18079 }
18080 else
18081 {
18082 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18083 identifier = cp_parser_identifier (parser);
18084 else
18085 {
18086 identifier = make_anon_name ();
18087 is_unnamed = true;
18088 if (scoped_enum_p)
18089 error_at (type_start_token->location,
18090 "unnamed scoped enum is not allowed");
18091 }
18092 }
18093 pop_deferring_access_checks ();
18094
18095 /* Check for the `:' that denotes a specified underlying type in C++0x.
18096 Note that a ':' could also indicate a bitfield width, however. */
18097 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18098 {
18099 cp_decl_specifier_seq type_specifiers;
18100
18101 /* Consume the `:'. */
18102 cp_lexer_consume_token (parser->lexer);
18103
18104 /* Parse the type-specifier-seq. */
18105 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18106 /*is_trailing_return=*/false,
18107 &type_specifiers);
18108
18109 /* At this point this is surely not elaborated type specifier. */
18110 if (!cp_parser_parse_definitely (parser))
18111 return NULL_TREE;
18112
18113 if (cxx_dialect < cxx11)
18114 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18115
18116 has_underlying_type = true;
18117
18118 /* If that didn't work, stop. */
18119 if (type_specifiers.type != error_mark_node)
18120 {
18121 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18122 /*initialized=*/0, NULL);
18123 if (underlying_type == error_mark_node
18124 || check_for_bare_parameter_packs (underlying_type))
18125 underlying_type = NULL_TREE;
18126 }
18127 }
18128
18129 /* Look for the `{' but don't consume it yet. */
18130 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18131 {
18132 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18133 {
18134 cp_parser_error (parser, "expected %<{%>");
18135 if (has_underlying_type)
18136 {
18137 type = NULL_TREE;
18138 goto out;
18139 }
18140 }
18141 /* An opaque-enum-specifier must have a ';' here. */
18142 if ((scoped_enum_p || underlying_type)
18143 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18144 {
18145 cp_parser_error (parser, "expected %<;%> or %<{%>");
18146 if (has_underlying_type)
18147 {
18148 type = NULL_TREE;
18149 goto out;
18150 }
18151 }
18152 }
18153
18154 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18155 return NULL_TREE;
18156
18157 if (nested_name_specifier)
18158 {
18159 if (CLASS_TYPE_P (nested_name_specifier))
18160 {
18161 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18162 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18163 push_scope (nested_name_specifier);
18164 }
18165 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18166 {
18167 push_nested_namespace (nested_name_specifier);
18168 }
18169 }
18170
18171 /* Issue an error message if type-definitions are forbidden here. */
18172 if (!cp_parser_check_type_definition (parser))
18173 type = error_mark_node;
18174 else
18175 /* Create the new type. We do this before consuming the opening
18176 brace so the enum will be recorded as being on the line of its
18177 tag (or the 'enum' keyword, if there is no tag). */
18178 type = start_enum (identifier, type, underlying_type,
18179 attributes, scoped_enum_p, &is_new_type);
18180
18181 /* If the next token is not '{' it is an opaque-enum-specifier or an
18182 elaborated-type-specifier. */
18183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18184 {
18185 timevar_push (TV_PARSE_ENUM);
18186 if (nested_name_specifier
18187 && nested_name_specifier != error_mark_node)
18188 {
18189 /* The following catches invalid code such as:
18190 enum class S<int>::E { A, B, C }; */
18191 if (!processing_specialization
18192 && CLASS_TYPE_P (nested_name_specifier)
18193 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18194 error_at (type_start_token->location, "cannot add an enumerator "
18195 "list to a template instantiation");
18196
18197 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18198 {
18199 error_at (type_start_token->location,
18200 "%<%T::%E%> has not been declared",
18201 TYPE_CONTEXT (nested_name_specifier),
18202 nested_name_specifier);
18203 type = error_mark_node;
18204 }
18205 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18206 && !CLASS_TYPE_P (nested_name_specifier))
18207 {
18208 error_at (type_start_token->location, "nested name specifier "
18209 "%qT for enum declaration does not name a class "
18210 "or namespace", nested_name_specifier);
18211 type = error_mark_node;
18212 }
18213 /* If that scope does not contain the scope in which the
18214 class was originally declared, the program is invalid. */
18215 else if (prev_scope && !is_ancestor (prev_scope,
18216 nested_name_specifier))
18217 {
18218 if (at_namespace_scope_p ())
18219 error_at (type_start_token->location,
18220 "declaration of %qD in namespace %qD which does not "
18221 "enclose %qD",
18222 type, prev_scope, nested_name_specifier);
18223 else
18224 error_at (type_start_token->location,
18225 "declaration of %qD in %qD which does not "
18226 "enclose %qD",
18227 type, prev_scope, nested_name_specifier);
18228 type = error_mark_node;
18229 }
18230 /* If that scope is the scope where the declaration is being placed
18231 the program is invalid. */
18232 else if (CLASS_TYPE_P (nested_name_specifier)
18233 && CLASS_TYPE_P (prev_scope)
18234 && same_type_p (nested_name_specifier, prev_scope))
18235 {
18236 permerror (type_start_token->location,
18237 "extra qualification not allowed");
18238 nested_name_specifier = NULL_TREE;
18239 }
18240 }
18241
18242 if (scoped_enum_p)
18243 begin_scope (sk_scoped_enum, type);
18244
18245 /* Consume the opening brace. */
18246 matching_braces braces;
18247 braces.consume_open (parser);
18248
18249 if (type == error_mark_node)
18250 ; /* Nothing to add */
18251 else if (OPAQUE_ENUM_P (type)
18252 || (cxx_dialect > cxx98 && processing_specialization))
18253 {
18254 new_value_list = true;
18255 SET_OPAQUE_ENUM_P (type, false);
18256 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18257 }
18258 else
18259 {
18260 error_at (type_start_token->location,
18261 "multiple definition of %q#T", type);
18262 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18263 "previous definition here");
18264 type = error_mark_node;
18265 }
18266
18267 if (type == error_mark_node)
18268 cp_parser_skip_to_end_of_block_or_statement (parser);
18269 /* If the next token is not '}', then there are some enumerators. */
18270 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18271 {
18272 if (is_unnamed && !scoped_enum_p)
18273 pedwarn (type_start_token->location, OPT_Wpedantic,
18274 "ISO C++ forbids empty unnamed enum");
18275 }
18276 else
18277 cp_parser_enumerator_list (parser, type);
18278
18279 /* Consume the final '}'. */
18280 braces.require_close (parser);
18281
18282 if (scoped_enum_p)
18283 finish_scope ();
18284 timevar_pop (TV_PARSE_ENUM);
18285 }
18286 else
18287 {
18288 /* If a ';' follows, then it is an opaque-enum-specifier
18289 and additional restrictions apply. */
18290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18291 {
18292 if (is_unnamed)
18293 error_at (type_start_token->location,
18294 "opaque-enum-specifier without name");
18295 else if (nested_name_specifier)
18296 error_at (type_start_token->location,
18297 "opaque-enum-specifier must use a simple identifier");
18298 }
18299 }
18300
18301 /* Look for trailing attributes to apply to this enumeration, and
18302 apply them if appropriate. */
18303 if (cp_parser_allow_gnu_extensions_p (parser))
18304 {
18305 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18306 cplus_decl_attributes (&type,
18307 trailing_attr,
18308 (int) ATTR_FLAG_TYPE_IN_PLACE);
18309 }
18310
18311 /* Finish up the enumeration. */
18312 if (type != error_mark_node)
18313 {
18314 if (new_value_list)
18315 finish_enum_value_list (type);
18316 if (is_new_type)
18317 finish_enum (type);
18318 }
18319
18320 if (nested_name_specifier)
18321 {
18322 if (CLASS_TYPE_P (nested_name_specifier))
18323 {
18324 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18325 pop_scope (nested_name_specifier);
18326 }
18327 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18328 {
18329 pop_nested_namespace (nested_name_specifier);
18330 }
18331 }
18332 out:
18333 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18334 return type;
18335 }
18336
18337 /* Parse an enumerator-list. The enumerators all have the indicated
18338 TYPE.
18339
18340 enumerator-list:
18341 enumerator-definition
18342 enumerator-list , enumerator-definition */
18343
18344 static void
18345 cp_parser_enumerator_list (cp_parser* parser, tree type)
18346 {
18347 while (true)
18348 {
18349 /* Parse an enumerator-definition. */
18350 cp_parser_enumerator_definition (parser, type);
18351
18352 /* If the next token is not a ',', we've reached the end of
18353 the list. */
18354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18355 break;
18356 /* Otherwise, consume the `,' and keep going. */
18357 cp_lexer_consume_token (parser->lexer);
18358 /* If the next token is a `}', there is a trailing comma. */
18359 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18360 {
18361 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18362 pedwarn (input_location, OPT_Wpedantic,
18363 "comma at end of enumerator list");
18364 break;
18365 }
18366 }
18367 }
18368
18369 /* Parse an enumerator-definition. The enumerator has the indicated
18370 TYPE.
18371
18372 enumerator-definition:
18373 enumerator
18374 enumerator = constant-expression
18375
18376 enumerator:
18377 identifier
18378
18379 GNU Extensions:
18380
18381 enumerator-definition:
18382 enumerator attributes [opt]
18383 enumerator attributes [opt] = constant-expression */
18384
18385 static void
18386 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18387 {
18388 tree identifier;
18389 tree value;
18390 location_t loc;
18391
18392 /* Save the input location because we are interested in the location
18393 of the identifier and not the location of the explicit value. */
18394 loc = cp_lexer_peek_token (parser->lexer)->location;
18395
18396 /* Look for the identifier. */
18397 identifier = cp_parser_identifier (parser);
18398 if (identifier == error_mark_node)
18399 return;
18400
18401 /* Parse any specified attributes. */
18402 tree attrs = cp_parser_attributes_opt (parser);
18403
18404 /* If the next token is an '=', then there is an explicit value. */
18405 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18406 {
18407 /* Consume the `=' token. */
18408 cp_lexer_consume_token (parser->lexer);
18409 /* Parse the value. */
18410 value = cp_parser_constant_expression (parser);
18411 }
18412 else
18413 value = NULL_TREE;
18414
18415 /* If we are processing a template, make sure the initializer of the
18416 enumerator doesn't contain any bare template parameter pack. */
18417 if (check_for_bare_parameter_packs (value))
18418 value = error_mark_node;
18419
18420 /* Create the enumerator. */
18421 build_enumerator (identifier, value, type, attrs, loc);
18422 }
18423
18424 /* Parse a namespace-name.
18425
18426 namespace-name:
18427 original-namespace-name
18428 namespace-alias
18429
18430 Returns the NAMESPACE_DECL for the namespace. */
18431
18432 static tree
18433 cp_parser_namespace_name (cp_parser* parser)
18434 {
18435 tree identifier;
18436 tree namespace_decl;
18437
18438 cp_token *token = cp_lexer_peek_token (parser->lexer);
18439
18440 /* Get the name of the namespace. */
18441 identifier = cp_parser_identifier (parser);
18442 if (identifier == error_mark_node)
18443 return error_mark_node;
18444
18445 /* Look up the identifier in the currently active scope. Look only
18446 for namespaces, due to:
18447
18448 [basic.lookup.udir]
18449
18450 When looking up a namespace-name in a using-directive or alias
18451 definition, only namespace names are considered.
18452
18453 And:
18454
18455 [basic.lookup.qual]
18456
18457 During the lookup of a name preceding the :: scope resolution
18458 operator, object, function, and enumerator names are ignored.
18459
18460 (Note that cp_parser_qualifying_entity only calls this
18461 function if the token after the name is the scope resolution
18462 operator.) */
18463 namespace_decl = cp_parser_lookup_name (parser, identifier,
18464 none_type,
18465 /*is_template=*/false,
18466 /*is_namespace=*/true,
18467 /*check_dependency=*/true,
18468 /*ambiguous_decls=*/NULL,
18469 token->location);
18470 /* If it's not a namespace, issue an error. */
18471 if (namespace_decl == error_mark_node
18472 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18473 {
18474 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18475 {
18476 error_at (token->location, "%qD is not a namespace-name", identifier);
18477 if (namespace_decl == error_mark_node
18478 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18479 suggest_alternative_in_explicit_scope (token->location, identifier,
18480 parser->scope);
18481 }
18482 cp_parser_error (parser, "expected namespace-name");
18483 namespace_decl = error_mark_node;
18484 }
18485
18486 return namespace_decl;
18487 }
18488
18489 /* Parse a namespace-definition.
18490
18491 namespace-definition:
18492 named-namespace-definition
18493 unnamed-namespace-definition
18494
18495 named-namespace-definition:
18496 original-namespace-definition
18497 extension-namespace-definition
18498
18499 original-namespace-definition:
18500 namespace identifier { namespace-body }
18501
18502 extension-namespace-definition:
18503 namespace original-namespace-name { namespace-body }
18504
18505 unnamed-namespace-definition:
18506 namespace { namespace-body } */
18507
18508 static void
18509 cp_parser_namespace_definition (cp_parser* parser)
18510 {
18511 tree identifier;
18512 int nested_definition_count = 0;
18513
18514 cp_ensure_no_omp_declare_simd (parser);
18515 cp_ensure_no_oacc_routine (parser);
18516
18517 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18518
18519 if (is_inline)
18520 {
18521 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18522 cp_lexer_consume_token (parser->lexer);
18523 }
18524
18525 /* Look for the `namespace' keyword. */
18526 cp_token* token
18527 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18528
18529 /* Parse any specified attributes before the identifier. */
18530 tree attribs = cp_parser_attributes_opt (parser);
18531
18532 for (;;)
18533 {
18534 identifier = NULL_TREE;
18535
18536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18537 {
18538 identifier = cp_parser_identifier (parser);
18539
18540 /* Parse any attributes specified after the identifier. */
18541 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18542 }
18543
18544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18545 break;
18546
18547 if (!nested_definition_count && cxx_dialect < cxx17)
18548 pedwarn (input_location, OPT_Wpedantic,
18549 "nested namespace definitions only available with "
18550 "-std=c++17 or -std=gnu++17");
18551
18552 /* Nested namespace names can create new namespaces (unlike
18553 other qualified-ids). */
18554 if (int count = identifier ? push_namespace (identifier) : 0)
18555 nested_definition_count += count;
18556 else
18557 cp_parser_error (parser, "nested namespace name required");
18558 cp_lexer_consume_token (parser->lexer);
18559 }
18560
18561 if (nested_definition_count && !identifier)
18562 cp_parser_error (parser, "namespace name required");
18563
18564 if (nested_definition_count && attribs)
18565 error_at (token->location,
18566 "a nested namespace definition cannot have attributes");
18567 if (nested_definition_count && is_inline)
18568 error_at (token->location,
18569 "a nested namespace definition cannot be inline");
18570
18571 /* Start the namespace. */
18572 nested_definition_count += push_namespace (identifier, is_inline);
18573
18574 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18575
18576 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18577
18578 /* Look for the `{' to validate starting the namespace. */
18579 matching_braces braces;
18580 if (braces.require_open (parser))
18581 {
18582 /* Parse the body of the namespace. */
18583 cp_parser_namespace_body (parser);
18584
18585 /* Look for the final `}'. */
18586 braces.require_close (parser);
18587 }
18588
18589 if (has_visibility)
18590 pop_visibility (1);
18591
18592 /* Pop the nested namespace definitions. */
18593 while (nested_definition_count--)
18594 pop_namespace ();
18595 }
18596
18597 /* Parse a namespace-body.
18598
18599 namespace-body:
18600 declaration-seq [opt] */
18601
18602 static void
18603 cp_parser_namespace_body (cp_parser* parser)
18604 {
18605 cp_parser_declaration_seq_opt (parser);
18606 }
18607
18608 /* Parse a namespace-alias-definition.
18609
18610 namespace-alias-definition:
18611 namespace identifier = qualified-namespace-specifier ; */
18612
18613 static void
18614 cp_parser_namespace_alias_definition (cp_parser* parser)
18615 {
18616 tree identifier;
18617 tree namespace_specifier;
18618
18619 cp_token *token = cp_lexer_peek_token (parser->lexer);
18620
18621 /* Look for the `namespace' keyword. */
18622 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18623 /* Look for the identifier. */
18624 identifier = cp_parser_identifier (parser);
18625 if (identifier == error_mark_node)
18626 return;
18627 /* Look for the `=' token. */
18628 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18629 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18630 {
18631 error_at (token->location, "%<namespace%> definition is not allowed here");
18632 /* Skip the definition. */
18633 cp_lexer_consume_token (parser->lexer);
18634 if (cp_parser_skip_to_closing_brace (parser))
18635 cp_lexer_consume_token (parser->lexer);
18636 return;
18637 }
18638 cp_parser_require (parser, CPP_EQ, RT_EQ);
18639 /* Look for the qualified-namespace-specifier. */
18640 namespace_specifier
18641 = cp_parser_qualified_namespace_specifier (parser);
18642 /* Look for the `;' token. */
18643 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18644
18645 /* Register the alias in the symbol table. */
18646 do_namespace_alias (identifier, namespace_specifier);
18647 }
18648
18649 /* Parse a qualified-namespace-specifier.
18650
18651 qualified-namespace-specifier:
18652 :: [opt] nested-name-specifier [opt] namespace-name
18653
18654 Returns a NAMESPACE_DECL corresponding to the specified
18655 namespace. */
18656
18657 static tree
18658 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18659 {
18660 /* Look for the optional `::'. */
18661 cp_parser_global_scope_opt (parser,
18662 /*current_scope_valid_p=*/false);
18663
18664 /* Look for the optional nested-name-specifier. */
18665 cp_parser_nested_name_specifier_opt (parser,
18666 /*typename_keyword_p=*/false,
18667 /*check_dependency_p=*/true,
18668 /*type_p=*/false,
18669 /*is_declaration=*/true);
18670
18671 return cp_parser_namespace_name (parser);
18672 }
18673
18674 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18675 access declaration.
18676
18677 using-declaration:
18678 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18679 using :: unqualified-id ;
18680
18681 access-declaration:
18682 qualified-id ;
18683
18684 */
18685
18686 static bool
18687 cp_parser_using_declaration (cp_parser* parser,
18688 bool access_declaration_p)
18689 {
18690 cp_token *token;
18691 bool typename_p = false;
18692 bool global_scope_p;
18693 tree decl;
18694 tree identifier;
18695 tree qscope;
18696 int oldcount = errorcount;
18697 cp_token *diag_token = NULL;
18698
18699 if (access_declaration_p)
18700 {
18701 diag_token = cp_lexer_peek_token (parser->lexer);
18702 cp_parser_parse_tentatively (parser);
18703 }
18704 else
18705 {
18706 /* Look for the `using' keyword. */
18707 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18708
18709 again:
18710 /* Peek at the next token. */
18711 token = cp_lexer_peek_token (parser->lexer);
18712 /* See if it's `typename'. */
18713 if (token->keyword == RID_TYPENAME)
18714 {
18715 /* Remember that we've seen it. */
18716 typename_p = true;
18717 /* Consume the `typename' token. */
18718 cp_lexer_consume_token (parser->lexer);
18719 }
18720 }
18721
18722 /* Look for the optional global scope qualification. */
18723 global_scope_p
18724 = (cp_parser_global_scope_opt (parser,
18725 /*current_scope_valid_p=*/false)
18726 != NULL_TREE);
18727
18728 /* If we saw `typename', or didn't see `::', then there must be a
18729 nested-name-specifier present. */
18730 if (typename_p || !global_scope_p)
18731 {
18732 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18733 /*check_dependency_p=*/true,
18734 /*type_p=*/false,
18735 /*is_declaration=*/true);
18736 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18737 {
18738 cp_parser_skip_to_end_of_block_or_statement (parser);
18739 return false;
18740 }
18741 }
18742 /* Otherwise, we could be in either of the two productions. In that
18743 case, treat the nested-name-specifier as optional. */
18744 else
18745 qscope = cp_parser_nested_name_specifier_opt (parser,
18746 /*typename_keyword_p=*/false,
18747 /*check_dependency_p=*/true,
18748 /*type_p=*/false,
18749 /*is_declaration=*/true);
18750 if (!qscope)
18751 qscope = global_namespace;
18752 else if (UNSCOPED_ENUM_P (qscope))
18753 qscope = CP_TYPE_CONTEXT (qscope);
18754
18755 if (access_declaration_p && cp_parser_error_occurred (parser))
18756 /* Something has already gone wrong; there's no need to parse
18757 further. Since an error has occurred, the return value of
18758 cp_parser_parse_definitely will be false, as required. */
18759 return cp_parser_parse_definitely (parser);
18760
18761 token = cp_lexer_peek_token (parser->lexer);
18762 /* Parse the unqualified-id. */
18763 identifier = cp_parser_unqualified_id (parser,
18764 /*template_keyword_p=*/false,
18765 /*check_dependency_p=*/true,
18766 /*declarator_p=*/true,
18767 /*optional_p=*/false);
18768
18769 if (access_declaration_p)
18770 {
18771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18772 cp_parser_simulate_error (parser);
18773 if (!cp_parser_parse_definitely (parser))
18774 return false;
18775 }
18776 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18777 {
18778 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18779 if (cxx_dialect < cxx17
18780 && !in_system_header_at (ell->location))
18781 pedwarn (ell->location, 0,
18782 "pack expansion in using-declaration only available "
18783 "with -std=c++17 or -std=gnu++17");
18784 qscope = make_pack_expansion (qscope);
18785 }
18786
18787 /* The function we call to handle a using-declaration is different
18788 depending on what scope we are in. */
18789 if (qscope == error_mark_node || identifier == error_mark_node)
18790 ;
18791 else if (!identifier_p (identifier)
18792 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18793 /* [namespace.udecl]
18794
18795 A using declaration shall not name a template-id. */
18796 error_at (token->location,
18797 "a template-id may not appear in a using-declaration");
18798 else
18799 {
18800 if (at_class_scope_p ())
18801 {
18802 /* Create the USING_DECL. */
18803 decl = do_class_using_decl (qscope, identifier);
18804
18805 if (decl && typename_p)
18806 USING_DECL_TYPENAME_P (decl) = 1;
18807
18808 if (check_for_bare_parameter_packs (decl))
18809 {
18810 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18811 return false;
18812 }
18813 else
18814 /* Add it to the list of members in this class. */
18815 finish_member_declaration (decl);
18816 }
18817 else
18818 {
18819 decl = cp_parser_lookup_name_simple (parser,
18820 identifier,
18821 token->location);
18822 if (decl == error_mark_node)
18823 cp_parser_name_lookup_error (parser, identifier,
18824 decl, NLE_NULL,
18825 token->location);
18826 else if (check_for_bare_parameter_packs (decl))
18827 {
18828 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18829 return false;
18830 }
18831 else if (!at_namespace_scope_p ())
18832 finish_local_using_decl (decl, qscope, identifier);
18833 else
18834 finish_namespace_using_decl (decl, qscope, identifier);
18835 }
18836 }
18837
18838 if (!access_declaration_p
18839 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18840 {
18841 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18842 if (cxx_dialect < cxx17)
18843 pedwarn (comma->location, 0,
18844 "comma-separated list in using-declaration only available "
18845 "with -std=c++17 or -std=gnu++17");
18846 goto again;
18847 }
18848
18849 /* Look for the final `;'. */
18850 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18851
18852 if (access_declaration_p && errorcount == oldcount)
18853 warning_at (diag_token->location, OPT_Wdeprecated,
18854 "access declarations are deprecated "
18855 "in favour of using-declarations; "
18856 "suggestion: add the %<using%> keyword");
18857
18858 return true;
18859 }
18860
18861 /* Parse an alias-declaration.
18862
18863 alias-declaration:
18864 using identifier attribute-specifier-seq [opt] = type-id */
18865
18866 static tree
18867 cp_parser_alias_declaration (cp_parser* parser)
18868 {
18869 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18870 location_t id_location;
18871 cp_declarator *declarator;
18872 cp_decl_specifier_seq decl_specs;
18873 bool member_p;
18874 const char *saved_message = NULL;
18875
18876 /* Look for the `using' keyword. */
18877 cp_token *using_token
18878 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18879 if (using_token == NULL)
18880 return error_mark_node;
18881
18882 id_location = cp_lexer_peek_token (parser->lexer)->location;
18883 id = cp_parser_identifier (parser);
18884 if (id == error_mark_node)
18885 return error_mark_node;
18886
18887 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18888 attributes = cp_parser_attributes_opt (parser);
18889 if (attributes == error_mark_node)
18890 return error_mark_node;
18891
18892 cp_parser_require (parser, CPP_EQ, RT_EQ);
18893
18894 if (cp_parser_error_occurred (parser))
18895 return error_mark_node;
18896
18897 cp_parser_commit_to_tentative_parse (parser);
18898
18899 /* Now we are going to parse the type-id of the declaration. */
18900
18901 /*
18902 [dcl.type]/3 says:
18903
18904 "A type-specifier-seq shall not define a class or enumeration
18905 unless it appears in the type-id of an alias-declaration (7.1.3) that
18906 is not the declaration of a template-declaration."
18907
18908 In other words, if we currently are in an alias template, the
18909 type-id should not define a type.
18910
18911 So let's set parser->type_definition_forbidden_message in that
18912 case; cp_parser_check_type_definition (called by
18913 cp_parser_class_specifier) will then emit an error if a type is
18914 defined in the type-id. */
18915 if (parser->num_template_parameter_lists)
18916 {
18917 saved_message = parser->type_definition_forbidden_message;
18918 parser->type_definition_forbidden_message =
18919 G_("types may not be defined in alias template declarations");
18920 }
18921
18922 type = cp_parser_type_id (parser);
18923
18924 /* Restore the error message if need be. */
18925 if (parser->num_template_parameter_lists)
18926 parser->type_definition_forbidden_message = saved_message;
18927
18928 if (type == error_mark_node
18929 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18930 {
18931 cp_parser_skip_to_end_of_block_or_statement (parser);
18932 return error_mark_node;
18933 }
18934
18935 /* A typedef-name can also be introduced by an alias-declaration. The
18936 identifier following the using keyword becomes a typedef-name. It has
18937 the same semantics as if it were introduced by the typedef
18938 specifier. In particular, it does not define a new type and it shall
18939 not appear in the type-id. */
18940
18941 clear_decl_specs (&decl_specs);
18942 decl_specs.type = type;
18943 if (attributes != NULL_TREE)
18944 {
18945 decl_specs.attributes = attributes;
18946 set_and_check_decl_spec_loc (&decl_specs,
18947 ds_attribute,
18948 attrs_token);
18949 }
18950 set_and_check_decl_spec_loc (&decl_specs,
18951 ds_typedef,
18952 using_token);
18953 set_and_check_decl_spec_loc (&decl_specs,
18954 ds_alias,
18955 using_token);
18956
18957 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18958 declarator->id_loc = id_location;
18959
18960 member_p = at_class_scope_p ();
18961 if (member_p)
18962 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18963 NULL_TREE, attributes);
18964 else
18965 decl = start_decl (declarator, &decl_specs, 0,
18966 attributes, NULL_TREE, &pushed_scope);
18967 if (decl == error_mark_node)
18968 return decl;
18969
18970 // Attach constraints to the alias declaration.
18971 if (flag_concepts && current_template_parms)
18972 {
18973 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18974 tree constr = build_constraints (reqs, NULL_TREE);
18975 set_constraints (decl, constr);
18976 }
18977
18978 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18979
18980 if (pushed_scope)
18981 pop_scope (pushed_scope);
18982
18983 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18984 added into the symbol table; otherwise, return the TYPE_DECL. */
18985 if (DECL_LANG_SPECIFIC (decl)
18986 && DECL_TEMPLATE_INFO (decl)
18987 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18988 {
18989 decl = DECL_TI_TEMPLATE (decl);
18990 if (member_p)
18991 check_member_template (decl);
18992 }
18993
18994 return decl;
18995 }
18996
18997 /* Parse a using-directive.
18998
18999 using-directive:
19000 using namespace :: [opt] nested-name-specifier [opt]
19001 namespace-name ; */
19002
19003 static void
19004 cp_parser_using_directive (cp_parser* parser)
19005 {
19006 tree namespace_decl;
19007 tree attribs;
19008
19009 /* Look for the `using' keyword. */
19010 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19011 /* And the `namespace' keyword. */
19012 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19013 /* Look for the optional `::' operator. */
19014 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19015 /* And the optional nested-name-specifier. */
19016 cp_parser_nested_name_specifier_opt (parser,
19017 /*typename_keyword_p=*/false,
19018 /*check_dependency_p=*/true,
19019 /*type_p=*/false,
19020 /*is_declaration=*/true);
19021 /* Get the namespace being used. */
19022 namespace_decl = cp_parser_namespace_name (parser);
19023 /* And any specified attributes. */
19024 attribs = cp_parser_attributes_opt (parser);
19025
19026 /* Update the symbol table. */
19027 if (namespace_bindings_p ())
19028 finish_namespace_using_directive (namespace_decl, attribs);
19029 else
19030 finish_local_using_directive (namespace_decl, attribs);
19031
19032 /* Look for the final `;'. */
19033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19034 }
19035
19036 /* Parse an asm-definition.
19037
19038 asm-definition:
19039 asm ( string-literal ) ;
19040
19041 GNU Extension:
19042
19043 asm-definition:
19044 asm volatile [opt] ( string-literal ) ;
19045 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19046 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19047 : asm-operand-list [opt] ) ;
19048 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19049 : asm-operand-list [opt]
19050 : asm-clobber-list [opt] ) ;
19051 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19052 : asm-clobber-list [opt]
19053 : asm-goto-list ) ; */
19054
19055 static void
19056 cp_parser_asm_definition (cp_parser* parser)
19057 {
19058 tree string;
19059 tree outputs = NULL_TREE;
19060 tree inputs = NULL_TREE;
19061 tree clobbers = NULL_TREE;
19062 tree labels = NULL_TREE;
19063 tree asm_stmt;
19064 bool volatile_p = false;
19065 bool extended_p = false;
19066 bool invalid_inputs_p = false;
19067 bool invalid_outputs_p = false;
19068 bool goto_p = false;
19069 required_token missing = RT_NONE;
19070
19071 /* Look for the `asm' keyword. */
19072 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19073
19074 if (parser->in_function_body
19075 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19076 {
19077 error ("%<asm%> in %<constexpr%> function");
19078 cp_function_chain->invalid_constexpr = true;
19079 }
19080
19081 /* See if the next token is `volatile'. */
19082 if (cp_parser_allow_gnu_extensions_p (parser)
19083 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19084 {
19085 /* Remember that we saw the `volatile' keyword. */
19086 volatile_p = true;
19087 /* Consume the token. */
19088 cp_lexer_consume_token (parser->lexer);
19089 }
19090 if (cp_parser_allow_gnu_extensions_p (parser)
19091 && parser->in_function_body
19092 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19093 {
19094 /* Remember that we saw the `goto' keyword. */
19095 goto_p = true;
19096 /* Consume the token. */
19097 cp_lexer_consume_token (parser->lexer);
19098 }
19099 /* Look for the opening `('. */
19100 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19101 return;
19102 /* Look for the string. */
19103 string = cp_parser_string_literal (parser, false, false);
19104 if (string == error_mark_node)
19105 {
19106 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19107 /*consume_paren=*/true);
19108 return;
19109 }
19110
19111 /* If we're allowing GNU extensions, check for the extended assembly
19112 syntax. Unfortunately, the `:' tokens need not be separated by
19113 a space in C, and so, for compatibility, we tolerate that here
19114 too. Doing that means that we have to treat the `::' operator as
19115 two `:' tokens. */
19116 if (cp_parser_allow_gnu_extensions_p (parser)
19117 && parser->in_function_body
19118 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19119 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19120 {
19121 bool inputs_p = false;
19122 bool clobbers_p = false;
19123 bool labels_p = false;
19124
19125 /* The extended syntax was used. */
19126 extended_p = true;
19127
19128 /* Look for outputs. */
19129 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19130 {
19131 /* Consume the `:'. */
19132 cp_lexer_consume_token (parser->lexer);
19133 /* Parse the output-operands. */
19134 if (cp_lexer_next_token_is_not (parser->lexer,
19135 CPP_COLON)
19136 && cp_lexer_next_token_is_not (parser->lexer,
19137 CPP_SCOPE)
19138 && cp_lexer_next_token_is_not (parser->lexer,
19139 CPP_CLOSE_PAREN)
19140 && !goto_p)
19141 {
19142 outputs = cp_parser_asm_operand_list (parser);
19143 if (outputs == error_mark_node)
19144 invalid_outputs_p = true;
19145 }
19146 }
19147 /* If the next token is `::', there are no outputs, and the
19148 next token is the beginning of the inputs. */
19149 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19150 /* The inputs are coming next. */
19151 inputs_p = true;
19152
19153 /* Look for inputs. */
19154 if (inputs_p
19155 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19156 {
19157 /* Consume the `:' or `::'. */
19158 cp_lexer_consume_token (parser->lexer);
19159 /* Parse the output-operands. */
19160 if (cp_lexer_next_token_is_not (parser->lexer,
19161 CPP_COLON)
19162 && cp_lexer_next_token_is_not (parser->lexer,
19163 CPP_SCOPE)
19164 && cp_lexer_next_token_is_not (parser->lexer,
19165 CPP_CLOSE_PAREN))
19166 {
19167 inputs = cp_parser_asm_operand_list (parser);
19168 if (inputs == error_mark_node)
19169 invalid_inputs_p = true;
19170 }
19171 }
19172 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19173 /* The clobbers are coming next. */
19174 clobbers_p = true;
19175
19176 /* Look for clobbers. */
19177 if (clobbers_p
19178 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19179 {
19180 clobbers_p = true;
19181 /* Consume the `:' or `::'. */
19182 cp_lexer_consume_token (parser->lexer);
19183 /* Parse the clobbers. */
19184 if (cp_lexer_next_token_is_not (parser->lexer,
19185 CPP_COLON)
19186 && cp_lexer_next_token_is_not (parser->lexer,
19187 CPP_CLOSE_PAREN))
19188 clobbers = cp_parser_asm_clobber_list (parser);
19189 }
19190 else if (goto_p
19191 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19192 /* The labels are coming next. */
19193 labels_p = true;
19194
19195 /* Look for labels. */
19196 if (labels_p
19197 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19198 {
19199 labels_p = true;
19200 /* Consume the `:' or `::'. */
19201 cp_lexer_consume_token (parser->lexer);
19202 /* Parse the labels. */
19203 labels = cp_parser_asm_label_list (parser);
19204 }
19205
19206 if (goto_p && !labels_p)
19207 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19208 }
19209 else if (goto_p)
19210 missing = RT_COLON_SCOPE;
19211
19212 /* Look for the closing `)'. */
19213 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19214 missing ? missing : RT_CLOSE_PAREN))
19215 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19216 /*consume_paren=*/true);
19217 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19218
19219 if (!invalid_inputs_p && !invalid_outputs_p)
19220 {
19221 /* Create the ASM_EXPR. */
19222 if (parser->in_function_body)
19223 {
19224 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19225 inputs, clobbers, labels);
19226 /* If the extended syntax was not used, mark the ASM_EXPR. */
19227 if (!extended_p)
19228 {
19229 tree temp = asm_stmt;
19230 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19231 temp = TREE_OPERAND (temp, 0);
19232
19233 ASM_INPUT_P (temp) = 1;
19234 }
19235 }
19236 else
19237 symtab->finalize_toplevel_asm (string);
19238 }
19239 }
19240
19241 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19242 type that comes from the decl-specifier-seq. */
19243
19244 static tree
19245 strip_declarator_types (tree type, cp_declarator *declarator)
19246 {
19247 for (cp_declarator *d = declarator; d;)
19248 switch (d->kind)
19249 {
19250 case cdk_id:
19251 case cdk_decomp:
19252 case cdk_error:
19253 d = NULL;
19254 break;
19255
19256 default:
19257 if (TYPE_PTRMEMFUNC_P (type))
19258 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19259 type = TREE_TYPE (type);
19260 d = d->declarator;
19261 break;
19262 }
19263
19264 return type;
19265 }
19266
19267 /* Declarators [gram.dcl.decl] */
19268
19269 /* Parse an init-declarator.
19270
19271 init-declarator:
19272 declarator initializer [opt]
19273
19274 GNU Extension:
19275
19276 init-declarator:
19277 declarator asm-specification [opt] attributes [opt] initializer [opt]
19278
19279 function-definition:
19280 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19281 function-body
19282 decl-specifier-seq [opt] declarator function-try-block
19283
19284 GNU Extension:
19285
19286 function-definition:
19287 __extension__ function-definition
19288
19289 TM Extension:
19290
19291 function-definition:
19292 decl-specifier-seq [opt] declarator function-transaction-block
19293
19294 The DECL_SPECIFIERS apply to this declarator. Returns a
19295 representation of the entity declared. If MEMBER_P is TRUE, then
19296 this declarator appears in a class scope. The new DECL created by
19297 this declarator is returned.
19298
19299 The CHECKS are access checks that should be performed once we know
19300 what entity is being declared (and, therefore, what classes have
19301 befriended it).
19302
19303 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19304 for a function-definition here as well. If the declarator is a
19305 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19306 be TRUE upon return. By that point, the function-definition will
19307 have been completely parsed.
19308
19309 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19310 is FALSE.
19311
19312 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19313 parsed declaration if it is an uninitialized single declarator not followed
19314 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19315 if present, will not be consumed. If returned, this declarator will be
19316 created with SD_INITIALIZED but will not call cp_finish_decl.
19317
19318 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19319 and there is an initializer, the pointed location_t is set to the
19320 location of the '=' or `(', or '{' in C++11 token introducing the
19321 initializer. */
19322
19323 static tree
19324 cp_parser_init_declarator (cp_parser* parser,
19325 cp_decl_specifier_seq *decl_specifiers,
19326 vec<deferred_access_check, va_gc> *checks,
19327 bool function_definition_allowed_p,
19328 bool member_p,
19329 int declares_class_or_enum,
19330 bool* function_definition_p,
19331 tree* maybe_range_for_decl,
19332 location_t* init_loc,
19333 tree* auto_result)
19334 {
19335 cp_token *token = NULL, *asm_spec_start_token = NULL,
19336 *attributes_start_token = NULL;
19337 cp_declarator *declarator;
19338 tree prefix_attributes;
19339 tree attributes = NULL;
19340 tree asm_specification;
19341 tree initializer;
19342 tree decl = NULL_TREE;
19343 tree scope;
19344 int is_initialized;
19345 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19346 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19347 "(...)". */
19348 enum cpp_ttype initialization_kind;
19349 bool is_direct_init = false;
19350 bool is_non_constant_init;
19351 int ctor_dtor_or_conv_p;
19352 bool friend_p = cp_parser_friend_p (decl_specifiers);
19353 tree pushed_scope = NULL_TREE;
19354 bool range_for_decl_p = false;
19355 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19356 location_t tmp_init_loc = UNKNOWN_LOCATION;
19357
19358 /* Gather the attributes that were provided with the
19359 decl-specifiers. */
19360 prefix_attributes = decl_specifiers->attributes;
19361
19362 /* Assume that this is not the declarator for a function
19363 definition. */
19364 if (function_definition_p)
19365 *function_definition_p = false;
19366
19367 /* Default arguments are only permitted for function parameters. */
19368 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19369 parser->default_arg_ok_p = false;
19370
19371 /* Defer access checks while parsing the declarator; we cannot know
19372 what names are accessible until we know what is being
19373 declared. */
19374 resume_deferring_access_checks ();
19375
19376 token = cp_lexer_peek_token (parser->lexer);
19377
19378 /* Parse the declarator. */
19379 declarator
19380 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19381 &ctor_dtor_or_conv_p,
19382 /*parenthesized_p=*/NULL,
19383 member_p, friend_p);
19384 /* Gather up the deferred checks. */
19385 stop_deferring_access_checks ();
19386
19387 parser->default_arg_ok_p = saved_default_arg_ok_p;
19388
19389 /* If the DECLARATOR was erroneous, there's no need to go
19390 further. */
19391 if (declarator == cp_error_declarator)
19392 return error_mark_node;
19393
19394 /* Check that the number of template-parameter-lists is OK. */
19395 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19396 token->location))
19397 return error_mark_node;
19398
19399 if (declares_class_or_enum & 2)
19400 cp_parser_check_for_definition_in_return_type (declarator,
19401 decl_specifiers->type,
19402 decl_specifiers->locations[ds_type_spec]);
19403
19404 /* Figure out what scope the entity declared by the DECLARATOR is
19405 located in. `grokdeclarator' sometimes changes the scope, so
19406 we compute it now. */
19407 scope = get_scope_of_declarator (declarator);
19408
19409 /* Perform any lookups in the declared type which were thought to be
19410 dependent, but are not in the scope of the declarator. */
19411 decl_specifiers->type
19412 = maybe_update_decl_type (decl_specifiers->type, scope);
19413
19414 /* If we're allowing GNU extensions, look for an
19415 asm-specification. */
19416 if (cp_parser_allow_gnu_extensions_p (parser))
19417 {
19418 /* Look for an asm-specification. */
19419 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19420 asm_specification = cp_parser_asm_specification_opt (parser);
19421 }
19422 else
19423 asm_specification = NULL_TREE;
19424
19425 /* Look for attributes. */
19426 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19427 attributes = cp_parser_attributes_opt (parser);
19428
19429 /* Peek at the next token. */
19430 token = cp_lexer_peek_token (parser->lexer);
19431
19432 bool bogus_implicit_tmpl = false;
19433
19434 if (function_declarator_p (declarator))
19435 {
19436 /* Handle C++17 deduction guides. */
19437 if (!decl_specifiers->type
19438 && ctor_dtor_or_conv_p <= 0
19439 && cxx_dialect >= cxx17)
19440 {
19441 cp_declarator *id = get_id_declarator (declarator);
19442 tree name = id->u.id.unqualified_name;
19443 parser->scope = id->u.id.qualifying_scope;
19444 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19445 if (tmpl
19446 && (DECL_CLASS_TEMPLATE_P (tmpl)
19447 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19448 {
19449 id->u.id.unqualified_name = dguide_name (tmpl);
19450 id->u.id.sfk = sfk_deduction_guide;
19451 ctor_dtor_or_conv_p = 1;
19452 }
19453 }
19454
19455 /* Check to see if the token indicates the start of a
19456 function-definition. */
19457 if (cp_parser_token_starts_function_definition_p (token))
19458 {
19459 if (!function_definition_allowed_p)
19460 {
19461 /* If a function-definition should not appear here, issue an
19462 error message. */
19463 cp_parser_error (parser,
19464 "a function-definition is not allowed here");
19465 return error_mark_node;
19466 }
19467
19468 location_t func_brace_location
19469 = cp_lexer_peek_token (parser->lexer)->location;
19470
19471 /* Neither attributes nor an asm-specification are allowed
19472 on a function-definition. */
19473 if (asm_specification)
19474 error_at (asm_spec_start_token->location,
19475 "an asm-specification is not allowed "
19476 "on a function-definition");
19477 if (attributes)
19478 error_at (attributes_start_token->location,
19479 "attributes are not allowed "
19480 "on a function-definition");
19481 /* This is a function-definition. */
19482 *function_definition_p = true;
19483
19484 /* Parse the function definition. */
19485 if (member_p)
19486 decl = cp_parser_save_member_function_body (parser,
19487 decl_specifiers,
19488 declarator,
19489 prefix_attributes);
19490 else
19491 decl =
19492 (cp_parser_function_definition_from_specifiers_and_declarator
19493 (parser, decl_specifiers, prefix_attributes, declarator));
19494
19495 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19496 {
19497 /* This is where the prologue starts... */
19498 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19499 = func_brace_location;
19500 }
19501
19502 return decl;
19503 }
19504 }
19505 else if (parser->fully_implicit_function_template_p)
19506 {
19507 /* A non-template declaration involving a function parameter list
19508 containing an implicit template parameter will be made into a
19509 template. If the resulting declaration is not going to be an
19510 actual function then finish the template scope here to prevent it.
19511 An error message will be issued once we have a decl to talk about.
19512
19513 FIXME probably we should do type deduction rather than create an
19514 implicit template, but the standard currently doesn't allow it. */
19515 bogus_implicit_tmpl = true;
19516 finish_fully_implicit_template (parser, NULL_TREE);
19517 }
19518
19519 /* [dcl.dcl]
19520
19521 Only in function declarations for constructors, destructors, type
19522 conversions, and deduction guides can the decl-specifier-seq be omitted.
19523
19524 We explicitly postpone this check past the point where we handle
19525 function-definitions because we tolerate function-definitions
19526 that are missing their return types in some modes. */
19527 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19528 {
19529 cp_parser_error (parser,
19530 "expected constructor, destructor, or type conversion");
19531 return error_mark_node;
19532 }
19533
19534 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19535 if (token->type == CPP_EQ
19536 || token->type == CPP_OPEN_PAREN
19537 || token->type == CPP_OPEN_BRACE)
19538 {
19539 is_initialized = SD_INITIALIZED;
19540 initialization_kind = token->type;
19541 if (maybe_range_for_decl)
19542 *maybe_range_for_decl = error_mark_node;
19543 tmp_init_loc = token->location;
19544 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19545 *init_loc = tmp_init_loc;
19546
19547 if (token->type == CPP_EQ
19548 && function_declarator_p (declarator))
19549 {
19550 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19551 if (t2->keyword == RID_DEFAULT)
19552 is_initialized = SD_DEFAULTED;
19553 else if (t2->keyword == RID_DELETE)
19554 is_initialized = SD_DELETED;
19555 }
19556 }
19557 else
19558 {
19559 /* If the init-declarator isn't initialized and isn't followed by a
19560 `,' or `;', it's not a valid init-declarator. */
19561 if (token->type != CPP_COMMA
19562 && token->type != CPP_SEMICOLON)
19563 {
19564 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19565 range_for_decl_p = true;
19566 else
19567 {
19568 if (!maybe_range_for_decl)
19569 cp_parser_error (parser, "expected initializer");
19570 return error_mark_node;
19571 }
19572 }
19573 is_initialized = SD_UNINITIALIZED;
19574 initialization_kind = CPP_EOF;
19575 }
19576
19577 /* Because start_decl has side-effects, we should only call it if we
19578 know we're going ahead. By this point, we know that we cannot
19579 possibly be looking at any other construct. */
19580 cp_parser_commit_to_tentative_parse (parser);
19581
19582 /* Enter the newly declared entry in the symbol table. If we're
19583 processing a declaration in a class-specifier, we wait until
19584 after processing the initializer. */
19585 if (!member_p)
19586 {
19587 if (parser->in_unbraced_linkage_specification_p)
19588 decl_specifiers->storage_class = sc_extern;
19589 decl = start_decl (declarator, decl_specifiers,
19590 range_for_decl_p? SD_INITIALIZED : is_initialized,
19591 attributes, prefix_attributes, &pushed_scope);
19592 cp_finalize_omp_declare_simd (parser, decl);
19593 cp_finalize_oacc_routine (parser, decl, false);
19594 /* Adjust location of decl if declarator->id_loc is more appropriate:
19595 set, and decl wasn't merged with another decl, in which case its
19596 location would be different from input_location, and more accurate. */
19597 if (DECL_P (decl)
19598 && declarator->id_loc != UNKNOWN_LOCATION
19599 && DECL_SOURCE_LOCATION (decl) == input_location)
19600 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19601 }
19602 else if (scope)
19603 /* Enter the SCOPE. That way unqualified names appearing in the
19604 initializer will be looked up in SCOPE. */
19605 pushed_scope = push_scope (scope);
19606
19607 /* Perform deferred access control checks, now that we know in which
19608 SCOPE the declared entity resides. */
19609 if (!member_p && decl)
19610 {
19611 tree saved_current_function_decl = NULL_TREE;
19612
19613 /* If the entity being declared is a function, pretend that we
19614 are in its scope. If it is a `friend', it may have access to
19615 things that would not otherwise be accessible. */
19616 if (TREE_CODE (decl) == FUNCTION_DECL)
19617 {
19618 saved_current_function_decl = current_function_decl;
19619 current_function_decl = decl;
19620 }
19621
19622 /* Perform access checks for template parameters. */
19623 cp_parser_perform_template_parameter_access_checks (checks);
19624
19625 /* Perform the access control checks for the declarator and the
19626 decl-specifiers. */
19627 perform_deferred_access_checks (tf_warning_or_error);
19628
19629 /* Restore the saved value. */
19630 if (TREE_CODE (decl) == FUNCTION_DECL)
19631 current_function_decl = saved_current_function_decl;
19632 }
19633
19634 /* Parse the initializer. */
19635 initializer = NULL_TREE;
19636 is_direct_init = false;
19637 is_non_constant_init = true;
19638 if (is_initialized)
19639 {
19640 if (function_declarator_p (declarator))
19641 {
19642 if (initialization_kind == CPP_EQ)
19643 initializer = cp_parser_pure_specifier (parser);
19644 else
19645 {
19646 /* If the declaration was erroneous, we don't really
19647 know what the user intended, so just silently
19648 consume the initializer. */
19649 if (decl != error_mark_node)
19650 error_at (tmp_init_loc, "initializer provided for function");
19651 cp_parser_skip_to_closing_parenthesis (parser,
19652 /*recovering=*/true,
19653 /*or_comma=*/false,
19654 /*consume_paren=*/true);
19655 }
19656 }
19657 else
19658 {
19659 /* We want to record the extra mangling scope for in-class
19660 initializers of class members and initializers of static data
19661 member templates. The former involves deferring
19662 parsing of the initializer until end of class as with default
19663 arguments. So right here we only handle the latter. */
19664 if (!member_p && processing_template_decl && decl != error_mark_node)
19665 start_lambda_scope (decl);
19666 initializer = cp_parser_initializer (parser,
19667 &is_direct_init,
19668 &is_non_constant_init);
19669 if (!member_p && processing_template_decl && decl != error_mark_node)
19670 finish_lambda_scope ();
19671 if (initializer == error_mark_node)
19672 cp_parser_skip_to_end_of_statement (parser);
19673 }
19674 }
19675
19676 /* The old parser allows attributes to appear after a parenthesized
19677 initializer. Mark Mitchell proposed removing this functionality
19678 on the GCC mailing lists on 2002-08-13. This parser accepts the
19679 attributes -- but ignores them. */
19680 if (cp_parser_allow_gnu_extensions_p (parser)
19681 && initialization_kind == CPP_OPEN_PAREN)
19682 if (cp_parser_attributes_opt (parser))
19683 warning (OPT_Wattributes,
19684 "attributes after parenthesized initializer ignored");
19685
19686 /* And now complain about a non-function implicit template. */
19687 if (bogus_implicit_tmpl && decl != error_mark_node)
19688 error_at (DECL_SOURCE_LOCATION (decl),
19689 "non-function %qD declared as implicit template", decl);
19690
19691 /* For an in-class declaration, use `grokfield' to create the
19692 declaration. */
19693 if (member_p)
19694 {
19695 if (pushed_scope)
19696 {
19697 pop_scope (pushed_scope);
19698 pushed_scope = NULL_TREE;
19699 }
19700 decl = grokfield (declarator, decl_specifiers,
19701 initializer, !is_non_constant_init,
19702 /*asmspec=*/NULL_TREE,
19703 attr_chainon (attributes, prefix_attributes));
19704 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19705 cp_parser_save_default_args (parser, decl);
19706 cp_finalize_omp_declare_simd (parser, decl);
19707 cp_finalize_oacc_routine (parser, decl, false);
19708 }
19709
19710 /* Finish processing the declaration. But, skip member
19711 declarations. */
19712 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19713 {
19714 cp_finish_decl (decl,
19715 initializer, !is_non_constant_init,
19716 asm_specification,
19717 /* If the initializer is in parentheses, then this is
19718 a direct-initialization, which means that an
19719 `explicit' constructor is OK. Otherwise, an
19720 `explicit' constructor cannot be used. */
19721 ((is_direct_init || !is_initialized)
19722 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19723 }
19724 else if ((cxx_dialect != cxx98) && friend_p
19725 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19726 /* Core issue #226 (C++0x only): A default template-argument
19727 shall not be specified in a friend class template
19728 declaration. */
19729 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19730 /*is_partial=*/false, /*is_friend_decl=*/1);
19731
19732 if (!friend_p && pushed_scope)
19733 pop_scope (pushed_scope);
19734
19735 if (function_declarator_p (declarator)
19736 && parser->fully_implicit_function_template_p)
19737 {
19738 if (member_p)
19739 decl = finish_fully_implicit_template (parser, decl);
19740 else
19741 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19742 }
19743
19744 if (auto_result && is_initialized && decl_specifiers->type
19745 && type_uses_auto (decl_specifiers->type))
19746 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19747
19748 return decl;
19749 }
19750
19751 /* Parse a declarator.
19752
19753 declarator:
19754 direct-declarator
19755 ptr-operator declarator
19756
19757 abstract-declarator:
19758 ptr-operator abstract-declarator [opt]
19759 direct-abstract-declarator
19760
19761 GNU Extensions:
19762
19763 declarator:
19764 attributes [opt] direct-declarator
19765 attributes [opt] ptr-operator declarator
19766
19767 abstract-declarator:
19768 attributes [opt] ptr-operator abstract-declarator [opt]
19769 attributes [opt] direct-abstract-declarator
19770
19771 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19772 detect constructors, destructors, deduction guides, or conversion operators.
19773 It is set to -1 if the declarator is a name, and +1 if it is a
19774 function. Otherwise it is set to zero. Usually you just want to
19775 test for >0, but internally the negative value is used.
19776
19777 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19778 a decl-specifier-seq unless it declares a constructor, destructor,
19779 or conversion. It might seem that we could check this condition in
19780 semantic analysis, rather than parsing, but that makes it difficult
19781 to handle something like `f()'. We want to notice that there are
19782 no decl-specifiers, and therefore realize that this is an
19783 expression, not a declaration.)
19784
19785 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19786 the declarator is a direct-declarator of the form "(...)".
19787
19788 MEMBER_P is true iff this declarator is a member-declarator.
19789
19790 FRIEND_P is true iff this declarator is a friend. */
19791
19792 static cp_declarator *
19793 cp_parser_declarator (cp_parser* parser,
19794 cp_parser_declarator_kind dcl_kind,
19795 int* ctor_dtor_or_conv_p,
19796 bool* parenthesized_p,
19797 bool member_p, bool friend_p)
19798 {
19799 cp_declarator *declarator;
19800 enum tree_code code;
19801 cp_cv_quals cv_quals;
19802 tree class_type;
19803 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19804
19805 /* Assume this is not a constructor, destructor, or type-conversion
19806 operator. */
19807 if (ctor_dtor_or_conv_p)
19808 *ctor_dtor_or_conv_p = 0;
19809
19810 if (cp_parser_allow_gnu_extensions_p (parser))
19811 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19812
19813 /* Check for the ptr-operator production. */
19814 cp_parser_parse_tentatively (parser);
19815 /* Parse the ptr-operator. */
19816 code = cp_parser_ptr_operator (parser,
19817 &class_type,
19818 &cv_quals,
19819 &std_attributes);
19820
19821 /* If that worked, then we have a ptr-operator. */
19822 if (cp_parser_parse_definitely (parser))
19823 {
19824 /* If a ptr-operator was found, then this declarator was not
19825 parenthesized. */
19826 if (parenthesized_p)
19827 *parenthesized_p = true;
19828 /* The dependent declarator is optional if we are parsing an
19829 abstract-declarator. */
19830 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19831 cp_parser_parse_tentatively (parser);
19832
19833 /* Parse the dependent declarator. */
19834 declarator = cp_parser_declarator (parser, dcl_kind,
19835 /*ctor_dtor_or_conv_p=*/NULL,
19836 /*parenthesized_p=*/NULL,
19837 /*member_p=*/false,
19838 friend_p);
19839
19840 /* If we are parsing an abstract-declarator, we must handle the
19841 case where the dependent declarator is absent. */
19842 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19843 && !cp_parser_parse_definitely (parser))
19844 declarator = NULL;
19845
19846 declarator = cp_parser_make_indirect_declarator
19847 (code, class_type, cv_quals, declarator, std_attributes);
19848 }
19849 /* Everything else is a direct-declarator. */
19850 else
19851 {
19852 if (parenthesized_p)
19853 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19854 CPP_OPEN_PAREN);
19855 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19856 ctor_dtor_or_conv_p,
19857 member_p, friend_p);
19858 }
19859
19860 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19861 declarator->attributes = gnu_attributes;
19862 return declarator;
19863 }
19864
19865 /* Parse a direct-declarator or direct-abstract-declarator.
19866
19867 direct-declarator:
19868 declarator-id
19869 direct-declarator ( parameter-declaration-clause )
19870 cv-qualifier-seq [opt]
19871 ref-qualifier [opt]
19872 exception-specification [opt]
19873 direct-declarator [ constant-expression [opt] ]
19874 ( declarator )
19875
19876 direct-abstract-declarator:
19877 direct-abstract-declarator [opt]
19878 ( parameter-declaration-clause )
19879 cv-qualifier-seq [opt]
19880 ref-qualifier [opt]
19881 exception-specification [opt]
19882 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19883 ( abstract-declarator )
19884
19885 Returns a representation of the declarator. DCL_KIND is
19886 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19887 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19888 we are parsing a direct-declarator. It is
19889 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19890 of ambiguity we prefer an abstract declarator, as per
19891 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19892 as for cp_parser_declarator. */
19893
19894 static cp_declarator *
19895 cp_parser_direct_declarator (cp_parser* parser,
19896 cp_parser_declarator_kind dcl_kind,
19897 int* ctor_dtor_or_conv_p,
19898 bool member_p, bool friend_p)
19899 {
19900 cp_token *token;
19901 cp_declarator *declarator = NULL;
19902 tree scope = NULL_TREE;
19903 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19904 bool saved_in_declarator_p = parser->in_declarator_p;
19905 bool first = true;
19906 tree pushed_scope = NULL_TREE;
19907 cp_token *open_paren = NULL, *close_paren = NULL;
19908
19909 while (true)
19910 {
19911 /* Peek at the next token. */
19912 token = cp_lexer_peek_token (parser->lexer);
19913 if (token->type == CPP_OPEN_PAREN)
19914 {
19915 /* This is either a parameter-declaration-clause, or a
19916 parenthesized declarator. When we know we are parsing a
19917 named declarator, it must be a parenthesized declarator
19918 if FIRST is true. For instance, `(int)' is a
19919 parameter-declaration-clause, with an omitted
19920 direct-abstract-declarator. But `((*))', is a
19921 parenthesized abstract declarator. Finally, when T is a
19922 template parameter `(T)' is a
19923 parameter-declaration-clause, and not a parenthesized
19924 named declarator.
19925
19926 We first try and parse a parameter-declaration-clause,
19927 and then try a nested declarator (if FIRST is true).
19928
19929 It is not an error for it not to be a
19930 parameter-declaration-clause, even when FIRST is
19931 false. Consider,
19932
19933 int i (int);
19934 int i (3);
19935
19936 The first is the declaration of a function while the
19937 second is the definition of a variable, including its
19938 initializer.
19939
19940 Having seen only the parenthesis, we cannot know which of
19941 these two alternatives should be selected. Even more
19942 complex are examples like:
19943
19944 int i (int (a));
19945 int i (int (3));
19946
19947 The former is a function-declaration; the latter is a
19948 variable initialization.
19949
19950 Thus again, we try a parameter-declaration-clause, and if
19951 that fails, we back out and return. */
19952
19953 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19954 {
19955 tree params;
19956 bool is_declarator = false;
19957
19958 open_paren = NULL;
19959
19960 /* In a member-declarator, the only valid interpretation
19961 of a parenthesis is the start of a
19962 parameter-declaration-clause. (It is invalid to
19963 initialize a static data member with a parenthesized
19964 initializer; only the "=" form of initialization is
19965 permitted.) */
19966 if (!member_p)
19967 cp_parser_parse_tentatively (parser);
19968
19969 /* Consume the `('. */
19970 matching_parens parens;
19971 parens.consume_open (parser);
19972 if (first)
19973 {
19974 /* If this is going to be an abstract declarator, we're
19975 in a declarator and we can't have default args. */
19976 parser->default_arg_ok_p = false;
19977 parser->in_declarator_p = true;
19978 }
19979
19980 begin_scope (sk_function_parms, NULL_TREE);
19981
19982 /* Parse the parameter-declaration-clause. */
19983 params = cp_parser_parameter_declaration_clause (parser);
19984
19985 /* Consume the `)'. */
19986 parens.require_close (parser);
19987
19988 /* If all went well, parse the cv-qualifier-seq,
19989 ref-qualifier and the exception-specification. */
19990 if (member_p || cp_parser_parse_definitely (parser))
19991 {
19992 cp_cv_quals cv_quals;
19993 cp_virt_specifiers virt_specifiers;
19994 cp_ref_qualifier ref_qual;
19995 tree exception_specification;
19996 tree late_return;
19997 tree attrs;
19998 bool memfn = (member_p || (pushed_scope
19999 && CLASS_TYPE_P (pushed_scope)));
20000
20001 is_declarator = true;
20002
20003 if (ctor_dtor_or_conv_p)
20004 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20005 first = false;
20006
20007 /* Parse the cv-qualifier-seq. */
20008 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20009 /* Parse the ref-qualifier. */
20010 ref_qual = cp_parser_ref_qualifier_opt (parser);
20011 /* Parse the tx-qualifier. */
20012 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20013 /* And the exception-specification. */
20014 exception_specification
20015 = cp_parser_exception_specification_opt (parser);
20016
20017 attrs = cp_parser_std_attribute_spec_seq (parser);
20018
20019 /* In here, we handle cases where attribute is used after
20020 the function declaration. For example:
20021 void func (int x) __attribute__((vector(..))); */
20022 tree gnu_attrs = NULL_TREE;
20023 tree requires_clause = NULL_TREE;
20024 late_return = (cp_parser_late_return_type_opt
20025 (parser, declarator, requires_clause,
20026 memfn ? cv_quals : -1));
20027
20028 /* Parse the virt-specifier-seq. */
20029 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20030
20031 /* Create the function-declarator. */
20032 declarator = make_call_declarator (declarator,
20033 params,
20034 cv_quals,
20035 virt_specifiers,
20036 ref_qual,
20037 tx_qual,
20038 exception_specification,
20039 late_return,
20040 requires_clause);
20041 declarator->std_attributes = attrs;
20042 declarator->attributes = gnu_attrs;
20043 /* Any subsequent parameter lists are to do with
20044 return type, so are not those of the declared
20045 function. */
20046 parser->default_arg_ok_p = false;
20047 }
20048
20049 /* Remove the function parms from scope. */
20050 pop_bindings_and_leave_scope ();
20051
20052 if (is_declarator)
20053 /* Repeat the main loop. */
20054 continue;
20055 }
20056
20057 /* If this is the first, we can try a parenthesized
20058 declarator. */
20059 if (first)
20060 {
20061 bool saved_in_type_id_in_expr_p;
20062
20063 parser->default_arg_ok_p = saved_default_arg_ok_p;
20064 parser->in_declarator_p = saved_in_declarator_p;
20065
20066 open_paren = token;
20067 /* Consume the `('. */
20068 matching_parens parens;
20069 parens.consume_open (parser);
20070 /* Parse the nested declarator. */
20071 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20072 parser->in_type_id_in_expr_p = true;
20073 declarator
20074 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20075 /*parenthesized_p=*/NULL,
20076 member_p, friend_p);
20077 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20078 first = false;
20079 /* Expect a `)'. */
20080 close_paren = cp_lexer_peek_token (parser->lexer);
20081 if (!parens.require_close (parser))
20082 declarator = cp_error_declarator;
20083 if (declarator == cp_error_declarator)
20084 break;
20085
20086 goto handle_declarator;
20087 }
20088 /* Otherwise, we must be done. */
20089 else
20090 break;
20091 }
20092 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20093 && token->type == CPP_OPEN_SQUARE
20094 && !cp_next_tokens_can_be_attribute_p (parser))
20095 {
20096 /* Parse an array-declarator. */
20097 tree bounds, attrs;
20098
20099 if (ctor_dtor_or_conv_p)
20100 *ctor_dtor_or_conv_p = 0;
20101
20102 open_paren = NULL;
20103 first = false;
20104 parser->default_arg_ok_p = false;
20105 parser->in_declarator_p = true;
20106 /* Consume the `['. */
20107 cp_lexer_consume_token (parser->lexer);
20108 /* Peek at the next token. */
20109 token = cp_lexer_peek_token (parser->lexer);
20110 /* If the next token is `]', then there is no
20111 constant-expression. */
20112 if (token->type != CPP_CLOSE_SQUARE)
20113 {
20114 bool non_constant_p;
20115 bounds
20116 = cp_parser_constant_expression (parser,
20117 /*allow_non_constant=*/true,
20118 &non_constant_p);
20119 if (!non_constant_p)
20120 /* OK */;
20121 else if (error_operand_p (bounds))
20122 /* Already gave an error. */;
20123 else if (!parser->in_function_body
20124 || current_binding_level->kind == sk_function_parms)
20125 {
20126 /* Normally, the array bound must be an integral constant
20127 expression. However, as an extension, we allow VLAs
20128 in function scopes as long as they aren't part of a
20129 parameter declaration. */
20130 cp_parser_error (parser,
20131 "array bound is not an integer constant");
20132 bounds = error_mark_node;
20133 }
20134 else if (processing_template_decl
20135 && !type_dependent_expression_p (bounds))
20136 {
20137 /* Remember this wasn't a constant-expression. */
20138 bounds = build_nop (TREE_TYPE (bounds), bounds);
20139 TREE_SIDE_EFFECTS (bounds) = 1;
20140 }
20141 }
20142 else
20143 bounds = NULL_TREE;
20144 /* Look for the closing `]'. */
20145 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20146 {
20147 declarator = cp_error_declarator;
20148 break;
20149 }
20150
20151 attrs = cp_parser_std_attribute_spec_seq (parser);
20152 declarator = make_array_declarator (declarator, bounds);
20153 declarator->std_attributes = attrs;
20154 }
20155 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20156 {
20157 {
20158 tree qualifying_scope;
20159 tree unqualified_name;
20160 tree attrs;
20161 special_function_kind sfk;
20162 bool abstract_ok;
20163 bool pack_expansion_p = false;
20164 cp_token *declarator_id_start_token;
20165
20166 /* Parse a declarator-id */
20167 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20168 if (abstract_ok)
20169 {
20170 cp_parser_parse_tentatively (parser);
20171
20172 /* If we see an ellipsis, we should be looking at a
20173 parameter pack. */
20174 if (token->type == CPP_ELLIPSIS)
20175 {
20176 /* Consume the `...' */
20177 cp_lexer_consume_token (parser->lexer);
20178
20179 pack_expansion_p = true;
20180 }
20181 }
20182
20183 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20184 unqualified_name
20185 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20186 qualifying_scope = parser->scope;
20187 if (abstract_ok)
20188 {
20189 bool okay = false;
20190
20191 if (!unqualified_name && pack_expansion_p)
20192 {
20193 /* Check whether an error occurred. */
20194 okay = !cp_parser_error_occurred (parser);
20195
20196 /* We already consumed the ellipsis to mark a
20197 parameter pack, but we have no way to report it,
20198 so abort the tentative parse. We will be exiting
20199 immediately anyway. */
20200 cp_parser_abort_tentative_parse (parser);
20201 }
20202 else
20203 okay = cp_parser_parse_definitely (parser);
20204
20205 if (!okay)
20206 unqualified_name = error_mark_node;
20207 else if (unqualified_name
20208 && (qualifying_scope
20209 || (!identifier_p (unqualified_name))))
20210 {
20211 cp_parser_error (parser, "expected unqualified-id");
20212 unqualified_name = error_mark_node;
20213 }
20214 }
20215
20216 if (!unqualified_name)
20217 return NULL;
20218 if (unqualified_name == error_mark_node)
20219 {
20220 declarator = cp_error_declarator;
20221 pack_expansion_p = false;
20222 declarator->parameter_pack_p = false;
20223 break;
20224 }
20225
20226 attrs = cp_parser_std_attribute_spec_seq (parser);
20227
20228 if (qualifying_scope && at_namespace_scope_p ()
20229 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20230 {
20231 /* In the declaration of a member of a template class
20232 outside of the class itself, the SCOPE will sometimes
20233 be a TYPENAME_TYPE. For example, given:
20234
20235 template <typename T>
20236 int S<T>::R::i = 3;
20237
20238 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20239 this context, we must resolve S<T>::R to an ordinary
20240 type, rather than a typename type.
20241
20242 The reason we normally avoid resolving TYPENAME_TYPEs
20243 is that a specialization of `S' might render
20244 `S<T>::R' not a type. However, if `S' is
20245 specialized, then this `i' will not be used, so there
20246 is no harm in resolving the types here. */
20247 tree type;
20248
20249 /* Resolve the TYPENAME_TYPE. */
20250 type = resolve_typename_type (qualifying_scope,
20251 /*only_current_p=*/false);
20252 /* If that failed, the declarator is invalid. */
20253 if (TREE_CODE (type) == TYPENAME_TYPE)
20254 {
20255 if (typedef_variant_p (type))
20256 error_at (declarator_id_start_token->location,
20257 "cannot define member of dependent typedef "
20258 "%qT", type);
20259 else
20260 error_at (declarator_id_start_token->location,
20261 "%<%T::%E%> is not a type",
20262 TYPE_CONTEXT (qualifying_scope),
20263 TYPE_IDENTIFIER (qualifying_scope));
20264 }
20265 qualifying_scope = type;
20266 }
20267
20268 sfk = sfk_none;
20269
20270 if (unqualified_name)
20271 {
20272 tree class_type;
20273
20274 if (qualifying_scope
20275 && CLASS_TYPE_P (qualifying_scope))
20276 class_type = qualifying_scope;
20277 else
20278 class_type = current_class_type;
20279
20280 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20281 {
20282 tree name_type = TREE_TYPE (unqualified_name);
20283
20284 if (!class_type || !same_type_p (name_type, class_type))
20285 {
20286 /* We do not attempt to print the declarator
20287 here because we do not have enough
20288 information about its original syntactic
20289 form. */
20290 cp_parser_error (parser, "invalid declarator");
20291 declarator = cp_error_declarator;
20292 break;
20293 }
20294 else if (qualifying_scope
20295 && CLASSTYPE_USE_TEMPLATE (name_type))
20296 {
20297 error_at (declarator_id_start_token->location,
20298 "invalid use of constructor as a template");
20299 inform (declarator_id_start_token->location,
20300 "use %<%T::%D%> instead of %<%T::%D%> to "
20301 "name the constructor in a qualified name",
20302 class_type,
20303 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20304 class_type, name_type);
20305 declarator = cp_error_declarator;
20306 break;
20307 }
20308 unqualified_name = constructor_name (class_type);
20309 }
20310
20311 if (class_type)
20312 {
20313 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20314 sfk = sfk_destructor;
20315 else if (identifier_p (unqualified_name)
20316 && IDENTIFIER_CONV_OP_P (unqualified_name))
20317 sfk = sfk_conversion;
20318 else if (/* There's no way to declare a constructor
20319 for an unnamed type, even if the type
20320 got a name for linkage purposes. */
20321 !TYPE_WAS_UNNAMED (class_type)
20322 /* Handle correctly (c++/19200):
20323
20324 struct S {
20325 struct T{};
20326 friend void S(T);
20327 };
20328
20329 and also:
20330
20331 namespace N {
20332 void S();
20333 }
20334
20335 struct S {
20336 friend void N::S();
20337 }; */
20338 && (!friend_p || class_type == qualifying_scope)
20339 && constructor_name_p (unqualified_name,
20340 class_type))
20341 sfk = sfk_constructor;
20342 else if (is_overloaded_fn (unqualified_name)
20343 && DECL_CONSTRUCTOR_P (get_first_fn
20344 (unqualified_name)))
20345 sfk = sfk_constructor;
20346
20347 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20348 *ctor_dtor_or_conv_p = -1;
20349 }
20350 }
20351 declarator = make_id_declarator (qualifying_scope,
20352 unqualified_name,
20353 sfk);
20354 declarator->std_attributes = attrs;
20355 declarator->id_loc = token->location;
20356 declarator->parameter_pack_p = pack_expansion_p;
20357
20358 if (pack_expansion_p)
20359 maybe_warn_variadic_templates ();
20360 }
20361
20362 handle_declarator:;
20363 scope = get_scope_of_declarator (declarator);
20364 if (scope)
20365 {
20366 /* Any names that appear after the declarator-id for a
20367 member are looked up in the containing scope. */
20368 if (at_function_scope_p ())
20369 {
20370 /* But declarations with qualified-ids can't appear in a
20371 function. */
20372 cp_parser_error (parser, "qualified-id in declaration");
20373 declarator = cp_error_declarator;
20374 break;
20375 }
20376 pushed_scope = push_scope (scope);
20377 }
20378 parser->in_declarator_p = true;
20379 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20380 || (declarator && declarator->kind == cdk_id))
20381 /* Default args are only allowed on function
20382 declarations. */
20383 parser->default_arg_ok_p = saved_default_arg_ok_p;
20384 else
20385 parser->default_arg_ok_p = false;
20386
20387 first = false;
20388 }
20389 /* We're done. */
20390 else
20391 break;
20392 }
20393
20394 /* For an abstract declarator, we might wind up with nothing at this
20395 point. That's an error; the declarator is not optional. */
20396 if (!declarator)
20397 cp_parser_error (parser, "expected declarator");
20398 else if (open_paren)
20399 {
20400 /* Record overly parenthesized declarator so we can give a
20401 diagnostic about confusing decl/expr disambiguation. */
20402 if (declarator->kind == cdk_array)
20403 {
20404 /* If the open and close parens are on different lines, this
20405 is probably a formatting thing, so ignore. */
20406 expanded_location open = expand_location (open_paren->location);
20407 expanded_location close = expand_location (close_paren->location);
20408 if (open.line != close.line || open.file != close.file)
20409 open_paren = NULL;
20410 }
20411 if (open_paren)
20412 declarator->parenthesized = open_paren->location;
20413 }
20414
20415 /* If we entered a scope, we must exit it now. */
20416 if (pushed_scope)
20417 pop_scope (pushed_scope);
20418
20419 parser->default_arg_ok_p = saved_default_arg_ok_p;
20420 parser->in_declarator_p = saved_in_declarator_p;
20421
20422 return declarator;
20423 }
20424
20425 /* Parse a ptr-operator.
20426
20427 ptr-operator:
20428 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20429 * cv-qualifier-seq [opt]
20430 &
20431 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20432 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20433
20434 GNU Extension:
20435
20436 ptr-operator:
20437 & cv-qualifier-seq [opt]
20438
20439 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20440 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20441 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20442 filled in with the TYPE containing the member. *CV_QUALS is
20443 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20444 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20445 Note that the tree codes returned by this function have nothing
20446 to do with the types of trees that will be eventually be created
20447 to represent the pointer or reference type being parsed. They are
20448 just constants with suggestive names. */
20449 static enum tree_code
20450 cp_parser_ptr_operator (cp_parser* parser,
20451 tree* type,
20452 cp_cv_quals *cv_quals,
20453 tree *attributes)
20454 {
20455 enum tree_code code = ERROR_MARK;
20456 cp_token *token;
20457 tree attrs = NULL_TREE;
20458
20459 /* Assume that it's not a pointer-to-member. */
20460 *type = NULL_TREE;
20461 /* And that there are no cv-qualifiers. */
20462 *cv_quals = TYPE_UNQUALIFIED;
20463
20464 /* Peek at the next token. */
20465 token = cp_lexer_peek_token (parser->lexer);
20466
20467 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20468 if (token->type == CPP_MULT)
20469 code = INDIRECT_REF;
20470 else if (token->type == CPP_AND)
20471 code = ADDR_EXPR;
20472 else if ((cxx_dialect != cxx98) &&
20473 token->type == CPP_AND_AND) /* C++0x only */
20474 code = NON_LVALUE_EXPR;
20475
20476 if (code != ERROR_MARK)
20477 {
20478 /* Consume the `*', `&' or `&&'. */
20479 cp_lexer_consume_token (parser->lexer);
20480
20481 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20482 `&', if we are allowing GNU extensions. (The only qualifier
20483 that can legally appear after `&' is `restrict', but that is
20484 enforced during semantic analysis. */
20485 if (code == INDIRECT_REF
20486 || cp_parser_allow_gnu_extensions_p (parser))
20487 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20488
20489 attrs = cp_parser_std_attribute_spec_seq (parser);
20490 if (attributes != NULL)
20491 *attributes = attrs;
20492 }
20493 else
20494 {
20495 /* Try the pointer-to-member case. */
20496 cp_parser_parse_tentatively (parser);
20497 /* Look for the optional `::' operator. */
20498 cp_parser_global_scope_opt (parser,
20499 /*current_scope_valid_p=*/false);
20500 /* Look for the nested-name specifier. */
20501 token = cp_lexer_peek_token (parser->lexer);
20502 cp_parser_nested_name_specifier (parser,
20503 /*typename_keyword_p=*/false,
20504 /*check_dependency_p=*/true,
20505 /*type_p=*/false,
20506 /*is_declaration=*/false);
20507 /* If we found it, and the next token is a `*', then we are
20508 indeed looking at a pointer-to-member operator. */
20509 if (!cp_parser_error_occurred (parser)
20510 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20511 {
20512 /* Indicate that the `*' operator was used. */
20513 code = INDIRECT_REF;
20514
20515 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20516 error_at (token->location, "%qD is a namespace", parser->scope);
20517 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20518 error_at (token->location, "cannot form pointer to member of "
20519 "non-class %q#T", parser->scope);
20520 else
20521 {
20522 /* The type of which the member is a member is given by the
20523 current SCOPE. */
20524 *type = parser->scope;
20525 /* The next name will not be qualified. */
20526 parser->scope = NULL_TREE;
20527 parser->qualifying_scope = NULL_TREE;
20528 parser->object_scope = NULL_TREE;
20529 /* Look for optional c++11 attributes. */
20530 attrs = cp_parser_std_attribute_spec_seq (parser);
20531 if (attributes != NULL)
20532 *attributes = attrs;
20533 /* Look for the optional cv-qualifier-seq. */
20534 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20535 }
20536 }
20537 /* If that didn't work we don't have a ptr-operator. */
20538 if (!cp_parser_parse_definitely (parser))
20539 cp_parser_error (parser, "expected ptr-operator");
20540 }
20541
20542 return code;
20543 }
20544
20545 /* Parse an (optional) cv-qualifier-seq.
20546
20547 cv-qualifier-seq:
20548 cv-qualifier cv-qualifier-seq [opt]
20549
20550 cv-qualifier:
20551 const
20552 volatile
20553
20554 GNU Extension:
20555
20556 cv-qualifier:
20557 __restrict__
20558
20559 Returns a bitmask representing the cv-qualifiers. */
20560
20561 static cp_cv_quals
20562 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20563 {
20564 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20565
20566 while (true)
20567 {
20568 cp_token *token;
20569 cp_cv_quals cv_qualifier;
20570
20571 /* Peek at the next token. */
20572 token = cp_lexer_peek_token (parser->lexer);
20573 /* See if it's a cv-qualifier. */
20574 switch (token->keyword)
20575 {
20576 case RID_CONST:
20577 cv_qualifier = TYPE_QUAL_CONST;
20578 break;
20579
20580 case RID_VOLATILE:
20581 cv_qualifier = TYPE_QUAL_VOLATILE;
20582 break;
20583
20584 case RID_RESTRICT:
20585 cv_qualifier = TYPE_QUAL_RESTRICT;
20586 break;
20587
20588 default:
20589 cv_qualifier = TYPE_UNQUALIFIED;
20590 break;
20591 }
20592
20593 if (!cv_qualifier)
20594 break;
20595
20596 if (cv_quals & cv_qualifier)
20597 {
20598 gcc_rich_location richloc (token->location);
20599 richloc.add_fixit_remove ();
20600 error_at (&richloc, "duplicate cv-qualifier");
20601 cp_lexer_purge_token (parser->lexer);
20602 }
20603 else
20604 {
20605 cp_lexer_consume_token (parser->lexer);
20606 cv_quals |= cv_qualifier;
20607 }
20608 }
20609
20610 return cv_quals;
20611 }
20612
20613 /* Parse an (optional) ref-qualifier
20614
20615 ref-qualifier:
20616 &
20617 &&
20618
20619 Returns cp_ref_qualifier representing ref-qualifier. */
20620
20621 static cp_ref_qualifier
20622 cp_parser_ref_qualifier_opt (cp_parser* parser)
20623 {
20624 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20625
20626 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20627 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20628 return ref_qual;
20629
20630 while (true)
20631 {
20632 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20633 cp_token *token = cp_lexer_peek_token (parser->lexer);
20634
20635 switch (token->type)
20636 {
20637 case CPP_AND:
20638 curr_ref_qual = REF_QUAL_LVALUE;
20639 break;
20640
20641 case CPP_AND_AND:
20642 curr_ref_qual = REF_QUAL_RVALUE;
20643 break;
20644
20645 default:
20646 curr_ref_qual = REF_QUAL_NONE;
20647 break;
20648 }
20649
20650 if (!curr_ref_qual)
20651 break;
20652 else if (ref_qual)
20653 {
20654 error_at (token->location, "multiple ref-qualifiers");
20655 cp_lexer_purge_token (parser->lexer);
20656 }
20657 else
20658 {
20659 ref_qual = curr_ref_qual;
20660 cp_lexer_consume_token (parser->lexer);
20661 }
20662 }
20663
20664 return ref_qual;
20665 }
20666
20667 /* Parse an optional tx-qualifier.
20668
20669 tx-qualifier:
20670 transaction_safe
20671 transaction_safe_dynamic */
20672
20673 static tree
20674 cp_parser_tx_qualifier_opt (cp_parser *parser)
20675 {
20676 cp_token *token = cp_lexer_peek_token (parser->lexer);
20677 if (token->type == CPP_NAME)
20678 {
20679 tree name = token->u.value;
20680 const char *p = IDENTIFIER_POINTER (name);
20681 const int len = strlen ("transaction_safe");
20682 if (!strncmp (p, "transaction_safe", len))
20683 {
20684 p += len;
20685 if (*p == '\0'
20686 || !strcmp (p, "_dynamic"))
20687 {
20688 cp_lexer_consume_token (parser->lexer);
20689 if (!flag_tm)
20690 {
20691 error ("%qE requires %<-fgnu-tm%>", name);
20692 return NULL_TREE;
20693 }
20694 else
20695 return name;
20696 }
20697 }
20698 }
20699 return NULL_TREE;
20700 }
20701
20702 /* Parse an (optional) virt-specifier-seq.
20703
20704 virt-specifier-seq:
20705 virt-specifier virt-specifier-seq [opt]
20706
20707 virt-specifier:
20708 override
20709 final
20710
20711 Returns a bitmask representing the virt-specifiers. */
20712
20713 static cp_virt_specifiers
20714 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20715 {
20716 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20717
20718 while (true)
20719 {
20720 cp_token *token;
20721 cp_virt_specifiers virt_specifier;
20722
20723 /* Peek at the next token. */
20724 token = cp_lexer_peek_token (parser->lexer);
20725 /* See if it's a virt-specifier-qualifier. */
20726 if (token->type != CPP_NAME)
20727 break;
20728 if (id_equal (token->u.value, "override"))
20729 {
20730 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20731 virt_specifier = VIRT_SPEC_OVERRIDE;
20732 }
20733 else if (id_equal (token->u.value, "final"))
20734 {
20735 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20736 virt_specifier = VIRT_SPEC_FINAL;
20737 }
20738 else if (id_equal (token->u.value, "__final"))
20739 {
20740 virt_specifier = VIRT_SPEC_FINAL;
20741 }
20742 else
20743 break;
20744
20745 if (virt_specifiers & virt_specifier)
20746 {
20747 gcc_rich_location richloc (token->location);
20748 richloc.add_fixit_remove ();
20749 error_at (&richloc, "duplicate virt-specifier");
20750 cp_lexer_purge_token (parser->lexer);
20751 }
20752 else
20753 {
20754 cp_lexer_consume_token (parser->lexer);
20755 virt_specifiers |= virt_specifier;
20756 }
20757 }
20758 return virt_specifiers;
20759 }
20760
20761 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20762 is in scope even though it isn't real. */
20763
20764 void
20765 inject_this_parameter (tree ctype, cp_cv_quals quals)
20766 {
20767 tree this_parm;
20768
20769 if (current_class_ptr)
20770 {
20771 /* We don't clear this between NSDMIs. Is it already what we want? */
20772 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20773 if (DECL_P (current_class_ptr)
20774 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20775 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20776 && cp_type_quals (type) == quals)
20777 return;
20778 }
20779
20780 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20781 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20782 current_class_ptr = NULL_TREE;
20783 current_class_ref
20784 = cp_build_fold_indirect_ref (this_parm);
20785 current_class_ptr = this_parm;
20786 }
20787
20788 /* Return true iff our current scope is a non-static data member
20789 initializer. */
20790
20791 bool
20792 parsing_nsdmi (void)
20793 {
20794 /* We recognize NSDMI context by the context-less 'this' pointer set up
20795 by the function above. */
20796 if (current_class_ptr
20797 && TREE_CODE (current_class_ptr) == PARM_DECL
20798 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20799 return true;
20800 return false;
20801 }
20802
20803 /* Parse a late-specified return type, if any. This is not a separate
20804 non-terminal, but part of a function declarator, which looks like
20805
20806 -> trailing-type-specifier-seq abstract-declarator(opt)
20807
20808 Returns the type indicated by the type-id.
20809
20810 In addition to this, parse any queued up #pragma omp declare simd
20811 clauses, and #pragma acc routine clauses.
20812
20813 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20814 function. */
20815
20816 static tree
20817 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20818 tree& requires_clause, cp_cv_quals quals)
20819 {
20820 cp_token *token;
20821 tree type = NULL_TREE;
20822 bool declare_simd_p = (parser->omp_declare_simd
20823 && declarator
20824 && declarator->kind == cdk_id);
20825
20826 bool oacc_routine_p = (parser->oacc_routine
20827 && declarator
20828 && declarator->kind == cdk_id);
20829
20830 /* Peek at the next token. */
20831 token = cp_lexer_peek_token (parser->lexer);
20832 /* A late-specified return type is indicated by an initial '->'. */
20833 if (token->type != CPP_DEREF
20834 && token->keyword != RID_REQUIRES
20835 && !(token->type == CPP_NAME
20836 && token->u.value == ridpointers[RID_REQUIRES])
20837 && !(declare_simd_p || oacc_routine_p))
20838 return NULL_TREE;
20839
20840 tree save_ccp = current_class_ptr;
20841 tree save_ccr = current_class_ref;
20842 if (quals >= 0)
20843 {
20844 /* DR 1207: 'this' is in scope in the trailing return type. */
20845 inject_this_parameter (current_class_type, quals);
20846 }
20847
20848 if (token->type == CPP_DEREF)
20849 {
20850 /* Consume the ->. */
20851 cp_lexer_consume_token (parser->lexer);
20852
20853 type = cp_parser_trailing_type_id (parser);
20854 }
20855
20856 /* Function declarations may be followed by a trailing
20857 requires-clause. */
20858 requires_clause = cp_parser_requires_clause_opt (parser);
20859
20860 if (declare_simd_p)
20861 declarator->attributes
20862 = cp_parser_late_parsing_omp_declare_simd (parser,
20863 declarator->attributes);
20864 if (oacc_routine_p)
20865 declarator->attributes
20866 = cp_parser_late_parsing_oacc_routine (parser,
20867 declarator->attributes);
20868
20869 if (quals >= 0)
20870 {
20871 current_class_ptr = save_ccp;
20872 current_class_ref = save_ccr;
20873 }
20874
20875 return type;
20876 }
20877
20878 /* Parse a declarator-id.
20879
20880 declarator-id:
20881 id-expression
20882 :: [opt] nested-name-specifier [opt] type-name
20883
20884 In the `id-expression' case, the value returned is as for
20885 cp_parser_id_expression if the id-expression was an unqualified-id.
20886 If the id-expression was a qualified-id, then a SCOPE_REF is
20887 returned. The first operand is the scope (either a NAMESPACE_DECL
20888 or TREE_TYPE), but the second is still just a representation of an
20889 unqualified-id. */
20890
20891 static tree
20892 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20893 {
20894 tree id;
20895 /* The expression must be an id-expression. Assume that qualified
20896 names are the names of types so that:
20897
20898 template <class T>
20899 int S<T>::R::i = 3;
20900
20901 will work; we must treat `S<T>::R' as the name of a type.
20902 Similarly, assume that qualified names are templates, where
20903 required, so that:
20904
20905 template <class T>
20906 int S<T>::R<T>::i = 3;
20907
20908 will work, too. */
20909 id = cp_parser_id_expression (parser,
20910 /*template_keyword_p=*/false,
20911 /*check_dependency_p=*/false,
20912 /*template_p=*/NULL,
20913 /*declarator_p=*/true,
20914 optional_p);
20915 if (id && BASELINK_P (id))
20916 id = BASELINK_FUNCTIONS (id);
20917 return id;
20918 }
20919
20920 /* Parse a type-id.
20921
20922 type-id:
20923 type-specifier-seq abstract-declarator [opt]
20924
20925 Returns the TYPE specified. */
20926
20927 static tree
20928 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20929 bool is_trailing_return)
20930 {
20931 cp_decl_specifier_seq type_specifier_seq;
20932 cp_declarator *abstract_declarator;
20933
20934 /* Parse the type-specifier-seq. */
20935 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20936 is_trailing_return,
20937 &type_specifier_seq);
20938 if (is_template_arg && type_specifier_seq.type
20939 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20940 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20941 /* A bare template name as a template argument is a template template
20942 argument, not a placeholder, so fail parsing it as a type argument. */
20943 {
20944 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20945 cp_parser_simulate_error (parser);
20946 return error_mark_node;
20947 }
20948 if (type_specifier_seq.type == error_mark_node)
20949 return error_mark_node;
20950
20951 /* There might or might not be an abstract declarator. */
20952 cp_parser_parse_tentatively (parser);
20953 /* Look for the declarator. */
20954 abstract_declarator
20955 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20956 /*parenthesized_p=*/NULL,
20957 /*member_p=*/false,
20958 /*friend_p=*/false);
20959 /* Check to see if there really was a declarator. */
20960 if (!cp_parser_parse_definitely (parser))
20961 abstract_declarator = NULL;
20962
20963 if (type_specifier_seq.type
20964 /* The concepts TS allows 'auto' as a type-id. */
20965 && (!flag_concepts || parser->in_type_id_in_expr_p)
20966 /* None of the valid uses of 'auto' in C++14 involve the type-id
20967 nonterminal, but it is valid in a trailing-return-type. */
20968 && !(cxx_dialect >= cxx14 && is_trailing_return))
20969 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20970 {
20971 /* A type-id with type 'auto' is only ok if the abstract declarator
20972 is a function declarator with a late-specified return type.
20973
20974 A type-id with 'auto' is also valid in a trailing-return-type
20975 in a compound-requirement. */
20976 if (abstract_declarator
20977 && abstract_declarator->kind == cdk_function
20978 && abstract_declarator->u.function.late_return_type)
20979 /* OK */;
20980 else if (parser->in_result_type_constraint_p)
20981 /* OK */;
20982 else
20983 {
20984 location_t loc = type_specifier_seq.locations[ds_type_spec];
20985 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20986 {
20987 error_at (loc, "missing template arguments after %qT",
20988 auto_node);
20989 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20990 tmpl);
20991 }
20992 else
20993 error_at (loc, "invalid use of %qT", auto_node);
20994 return error_mark_node;
20995 }
20996 }
20997
20998 return groktypename (&type_specifier_seq, abstract_declarator,
20999 is_template_arg);
21000 }
21001
21002 static tree
21003 cp_parser_type_id (cp_parser *parser)
21004 {
21005 return cp_parser_type_id_1 (parser, false, false);
21006 }
21007
21008 static tree
21009 cp_parser_template_type_arg (cp_parser *parser)
21010 {
21011 tree r;
21012 const char *saved_message = parser->type_definition_forbidden_message;
21013 parser->type_definition_forbidden_message
21014 = G_("types may not be defined in template arguments");
21015 r = cp_parser_type_id_1 (parser, true, false);
21016 parser->type_definition_forbidden_message = saved_message;
21017 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21018 {
21019 error ("invalid use of %<auto%> in template argument");
21020 r = error_mark_node;
21021 }
21022 return r;
21023 }
21024
21025 static tree
21026 cp_parser_trailing_type_id (cp_parser *parser)
21027 {
21028 return cp_parser_type_id_1 (parser, false, true);
21029 }
21030
21031 /* Parse a type-specifier-seq.
21032
21033 type-specifier-seq:
21034 type-specifier type-specifier-seq [opt]
21035
21036 GNU extension:
21037
21038 type-specifier-seq:
21039 attributes type-specifier-seq [opt]
21040
21041 If IS_DECLARATION is true, we are at the start of a "condition" or
21042 exception-declaration, so we might be followed by a declarator-id.
21043
21044 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21045 i.e. we've just seen "->".
21046
21047 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21048
21049 static void
21050 cp_parser_type_specifier_seq (cp_parser* parser,
21051 bool is_declaration,
21052 bool is_trailing_return,
21053 cp_decl_specifier_seq *type_specifier_seq)
21054 {
21055 bool seen_type_specifier = false;
21056 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21057 cp_token *start_token = NULL;
21058
21059 /* Clear the TYPE_SPECIFIER_SEQ. */
21060 clear_decl_specs (type_specifier_seq);
21061
21062 /* In the context of a trailing return type, enum E { } is an
21063 elaborated-type-specifier followed by a function-body, not an
21064 enum-specifier. */
21065 if (is_trailing_return)
21066 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21067
21068 /* Parse the type-specifiers and attributes. */
21069 while (true)
21070 {
21071 tree type_specifier;
21072 bool is_cv_qualifier;
21073
21074 /* Check for attributes first. */
21075 if (cp_next_tokens_can_be_attribute_p (parser))
21076 {
21077 type_specifier_seq->attributes
21078 = attr_chainon (type_specifier_seq->attributes,
21079 cp_parser_attributes_opt (parser));
21080 continue;
21081 }
21082
21083 /* record the token of the beginning of the type specifier seq,
21084 for error reporting purposes*/
21085 if (!start_token)
21086 start_token = cp_lexer_peek_token (parser->lexer);
21087
21088 /* Look for the type-specifier. */
21089 type_specifier = cp_parser_type_specifier (parser,
21090 flags,
21091 type_specifier_seq,
21092 /*is_declaration=*/false,
21093 NULL,
21094 &is_cv_qualifier);
21095 if (!type_specifier)
21096 {
21097 /* If the first type-specifier could not be found, this is not a
21098 type-specifier-seq at all. */
21099 if (!seen_type_specifier)
21100 {
21101 /* Set in_declarator_p to avoid skipping to the semicolon. */
21102 int in_decl = parser->in_declarator_p;
21103 parser->in_declarator_p = true;
21104
21105 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21106 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21107 cp_parser_error (parser, "expected type-specifier");
21108
21109 parser->in_declarator_p = in_decl;
21110
21111 type_specifier_seq->type = error_mark_node;
21112 return;
21113 }
21114 /* If subsequent type-specifiers could not be found, the
21115 type-specifier-seq is complete. */
21116 break;
21117 }
21118
21119 seen_type_specifier = true;
21120 /* The standard says that a condition can be:
21121
21122 type-specifier-seq declarator = assignment-expression
21123
21124 However, given:
21125
21126 struct S {};
21127 if (int S = ...)
21128
21129 we should treat the "S" as a declarator, not as a
21130 type-specifier. The standard doesn't say that explicitly for
21131 type-specifier-seq, but it does say that for
21132 decl-specifier-seq in an ordinary declaration. Perhaps it
21133 would be clearer just to allow a decl-specifier-seq here, and
21134 then add a semantic restriction that if any decl-specifiers
21135 that are not type-specifiers appear, the program is invalid. */
21136 if (is_declaration && !is_cv_qualifier)
21137 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21138 }
21139 }
21140
21141 /* Return whether the function currently being declared has an associated
21142 template parameter list. */
21143
21144 static bool
21145 function_being_declared_is_template_p (cp_parser* parser)
21146 {
21147 if (!current_template_parms || processing_template_parmlist)
21148 return false;
21149
21150 if (parser->implicit_template_scope)
21151 return true;
21152
21153 if (at_class_scope_p ()
21154 && TYPE_BEING_DEFINED (current_class_type))
21155 return parser->num_template_parameter_lists != 0;
21156
21157 return ((int) parser->num_template_parameter_lists > template_class_depth
21158 (current_class_type));
21159 }
21160
21161 /* Parse a parameter-declaration-clause.
21162
21163 parameter-declaration-clause:
21164 parameter-declaration-list [opt] ... [opt]
21165 parameter-declaration-list , ...
21166
21167 Returns a representation for the parameter declarations. A return
21168 value of NULL indicates a parameter-declaration-clause consisting
21169 only of an ellipsis. */
21170
21171 static tree
21172 cp_parser_parameter_declaration_clause (cp_parser* parser)
21173 {
21174 tree parameters;
21175 cp_token *token;
21176 bool ellipsis_p;
21177 bool is_error;
21178
21179 struct cleanup {
21180 cp_parser* parser;
21181 int auto_is_implicit_function_template_parm_p;
21182 ~cleanup() {
21183 parser->auto_is_implicit_function_template_parm_p
21184 = auto_is_implicit_function_template_parm_p;
21185 }
21186 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21187
21188 (void) cleanup;
21189
21190 if (!processing_specialization
21191 && !processing_template_parmlist
21192 && !processing_explicit_instantiation)
21193 if (!current_function_decl
21194 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21195 parser->auto_is_implicit_function_template_parm_p = true;
21196
21197 /* Peek at the next token. */
21198 token = cp_lexer_peek_token (parser->lexer);
21199 /* Check for trivial parameter-declaration-clauses. */
21200 if (token->type == CPP_ELLIPSIS)
21201 {
21202 /* Consume the `...' token. */
21203 cp_lexer_consume_token (parser->lexer);
21204 return NULL_TREE;
21205 }
21206 else if (token->type == CPP_CLOSE_PAREN)
21207 /* There are no parameters. */
21208 {
21209 #ifndef NO_IMPLICIT_EXTERN_C
21210 if (in_system_header_at (input_location)
21211 && current_class_type == NULL
21212 && current_lang_name == lang_name_c)
21213 return NULL_TREE;
21214 else
21215 #endif
21216 return void_list_node;
21217 }
21218 /* Check for `(void)', too, which is a special case. */
21219 else if (token->keyword == RID_VOID
21220 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21221 == CPP_CLOSE_PAREN))
21222 {
21223 /* Consume the `void' token. */
21224 cp_lexer_consume_token (parser->lexer);
21225 /* There are no parameters. */
21226 return void_list_node;
21227 }
21228
21229 /* Parse the parameter-declaration-list. */
21230 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21231 /* If a parse error occurred while parsing the
21232 parameter-declaration-list, then the entire
21233 parameter-declaration-clause is erroneous. */
21234 if (is_error)
21235 return NULL;
21236
21237 /* Peek at the next token. */
21238 token = cp_lexer_peek_token (parser->lexer);
21239 /* If it's a `,', the clause should terminate with an ellipsis. */
21240 if (token->type == CPP_COMMA)
21241 {
21242 /* Consume the `,'. */
21243 cp_lexer_consume_token (parser->lexer);
21244 /* Expect an ellipsis. */
21245 ellipsis_p
21246 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21247 }
21248 /* It might also be `...' if the optional trailing `,' was
21249 omitted. */
21250 else if (token->type == CPP_ELLIPSIS)
21251 {
21252 /* Consume the `...' token. */
21253 cp_lexer_consume_token (parser->lexer);
21254 /* And remember that we saw it. */
21255 ellipsis_p = true;
21256 }
21257 else
21258 ellipsis_p = false;
21259
21260 /* Finish the parameter list. */
21261 if (!ellipsis_p)
21262 parameters = chainon (parameters, void_list_node);
21263
21264 return parameters;
21265 }
21266
21267 /* Parse a parameter-declaration-list.
21268
21269 parameter-declaration-list:
21270 parameter-declaration
21271 parameter-declaration-list , parameter-declaration
21272
21273 Returns a representation of the parameter-declaration-list, as for
21274 cp_parser_parameter_declaration_clause. However, the
21275 `void_list_node' is never appended to the list. Upon return,
21276 *IS_ERROR will be true iff an error occurred. */
21277
21278 static tree
21279 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21280 {
21281 tree parameters = NULL_TREE;
21282 tree *tail = &parameters;
21283 bool saved_in_unbraced_linkage_specification_p;
21284 int index = 0;
21285
21286 /* Assume all will go well. */
21287 *is_error = false;
21288 /* The special considerations that apply to a function within an
21289 unbraced linkage specifications do not apply to the parameters
21290 to the function. */
21291 saved_in_unbraced_linkage_specification_p
21292 = parser->in_unbraced_linkage_specification_p;
21293 parser->in_unbraced_linkage_specification_p = false;
21294
21295 /* Look for more parameters. */
21296 while (true)
21297 {
21298 cp_parameter_declarator *parameter;
21299 tree decl = error_mark_node;
21300 bool parenthesized_p = false;
21301 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21302 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21303 (current_template_parms)) : 0);
21304
21305 /* Parse the parameter. */
21306 parameter
21307 = cp_parser_parameter_declaration (parser,
21308 /*template_parm_p=*/false,
21309 &parenthesized_p);
21310
21311 /* We don't know yet if the enclosing context is deprecated, so wait
21312 and warn in grokparms if appropriate. */
21313 deprecated_state = DEPRECATED_SUPPRESS;
21314
21315 if (parameter)
21316 {
21317 /* If a function parameter pack was specified and an implicit template
21318 parameter was introduced during cp_parser_parameter_declaration,
21319 change any implicit parameters introduced into packs. */
21320 if (parser->implicit_template_parms
21321 && parameter->declarator
21322 && parameter->declarator->parameter_pack_p)
21323 {
21324 int latest_template_parm_idx = TREE_VEC_LENGTH
21325 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21326
21327 if (latest_template_parm_idx != template_parm_idx)
21328 parameter->decl_specifiers.type = convert_generic_types_to_packs
21329 (parameter->decl_specifiers.type,
21330 template_parm_idx, latest_template_parm_idx);
21331 }
21332
21333 decl = grokdeclarator (parameter->declarator,
21334 &parameter->decl_specifiers,
21335 PARM,
21336 parameter->default_argument != NULL_TREE,
21337 &parameter->decl_specifiers.attributes);
21338 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21339 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21340 }
21341
21342 deprecated_state = DEPRECATED_NORMAL;
21343
21344 /* If a parse error occurred parsing the parameter declaration,
21345 then the entire parameter-declaration-list is erroneous. */
21346 if (decl == error_mark_node)
21347 {
21348 *is_error = true;
21349 parameters = error_mark_node;
21350 break;
21351 }
21352
21353 if (parameter->decl_specifiers.attributes)
21354 cplus_decl_attributes (&decl,
21355 parameter->decl_specifiers.attributes,
21356 0);
21357 if (DECL_NAME (decl))
21358 decl = pushdecl (decl);
21359
21360 if (decl != error_mark_node)
21361 {
21362 retrofit_lang_decl (decl);
21363 DECL_PARM_INDEX (decl) = ++index;
21364 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21365 }
21366
21367 /* Add the new parameter to the list. */
21368 *tail = build_tree_list (parameter->default_argument, decl);
21369 tail = &TREE_CHAIN (*tail);
21370
21371 /* Peek at the next token. */
21372 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21373 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21374 /* These are for Objective-C++ */
21375 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21376 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21377 /* The parameter-declaration-list is complete. */
21378 break;
21379 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21380 {
21381 cp_token *token;
21382
21383 /* Peek at the next token. */
21384 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21385 /* If it's an ellipsis, then the list is complete. */
21386 if (token->type == CPP_ELLIPSIS)
21387 break;
21388 /* Otherwise, there must be more parameters. Consume the
21389 `,'. */
21390 cp_lexer_consume_token (parser->lexer);
21391 /* When parsing something like:
21392
21393 int i(float f, double d)
21394
21395 we can tell after seeing the declaration for "f" that we
21396 are not looking at an initialization of a variable "i",
21397 but rather at the declaration of a function "i".
21398
21399 Due to the fact that the parsing of template arguments
21400 (as specified to a template-id) requires backtracking we
21401 cannot use this technique when inside a template argument
21402 list. */
21403 if (!parser->in_template_argument_list_p
21404 && !parser->in_type_id_in_expr_p
21405 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21406 /* However, a parameter-declaration of the form
21407 "float(f)" (which is a valid declaration of a
21408 parameter "f") can also be interpreted as an
21409 expression (the conversion of "f" to "float"). */
21410 && !parenthesized_p)
21411 cp_parser_commit_to_tentative_parse (parser);
21412 }
21413 else
21414 {
21415 cp_parser_error (parser, "expected %<,%> or %<...%>");
21416 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21417 cp_parser_skip_to_closing_parenthesis (parser,
21418 /*recovering=*/true,
21419 /*or_comma=*/false,
21420 /*consume_paren=*/false);
21421 break;
21422 }
21423 }
21424
21425 parser->in_unbraced_linkage_specification_p
21426 = saved_in_unbraced_linkage_specification_p;
21427
21428 /* Reset implicit_template_scope if we are about to leave the function
21429 parameter list that introduced it. Note that for out-of-line member
21430 definitions, there will be one or more class scopes before we get to
21431 the template parameter scope. */
21432
21433 if (cp_binding_level *its = parser->implicit_template_scope)
21434 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21435 {
21436 while (maybe_its->kind == sk_class)
21437 maybe_its = maybe_its->level_chain;
21438 if (maybe_its == its)
21439 {
21440 parser->implicit_template_parms = 0;
21441 parser->implicit_template_scope = 0;
21442 }
21443 }
21444
21445 return parameters;
21446 }
21447
21448 /* Parse a parameter declaration.
21449
21450 parameter-declaration:
21451 decl-specifier-seq ... [opt] declarator
21452 decl-specifier-seq declarator = assignment-expression
21453 decl-specifier-seq ... [opt] abstract-declarator [opt]
21454 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21455
21456 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21457 declares a template parameter. (In that case, a non-nested `>'
21458 token encountered during the parsing of the assignment-expression
21459 is not interpreted as a greater-than operator.)
21460
21461 Returns a representation of the parameter, or NULL if an error
21462 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21463 true iff the declarator is of the form "(p)". */
21464
21465 static cp_parameter_declarator *
21466 cp_parser_parameter_declaration (cp_parser *parser,
21467 bool template_parm_p,
21468 bool *parenthesized_p)
21469 {
21470 int declares_class_or_enum;
21471 cp_decl_specifier_seq decl_specifiers;
21472 cp_declarator *declarator;
21473 tree default_argument;
21474 cp_token *token = NULL, *declarator_token_start = NULL;
21475 const char *saved_message;
21476 bool template_parameter_pack_p = false;
21477
21478 /* In a template parameter, `>' is not an operator.
21479
21480 [temp.param]
21481
21482 When parsing a default template-argument for a non-type
21483 template-parameter, the first non-nested `>' is taken as the end
21484 of the template parameter-list rather than a greater-than
21485 operator. */
21486
21487 /* Type definitions may not appear in parameter types. */
21488 saved_message = parser->type_definition_forbidden_message;
21489 parser->type_definition_forbidden_message
21490 = G_("types may not be defined in parameter types");
21491
21492 /* Parse the declaration-specifiers. */
21493 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21494 cp_parser_decl_specifier_seq (parser,
21495 CP_PARSER_FLAGS_NONE,
21496 &decl_specifiers,
21497 &declares_class_or_enum);
21498
21499 /* Complain about missing 'typename' or other invalid type names. */
21500 if (!decl_specifiers.any_type_specifiers_p
21501 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21502 decl_specifiers.type = error_mark_node;
21503
21504 /* If an error occurred, there's no reason to attempt to parse the
21505 rest of the declaration. */
21506 if (cp_parser_error_occurred (parser))
21507 {
21508 parser->type_definition_forbidden_message = saved_message;
21509 return NULL;
21510 }
21511
21512 /* Peek at the next token. */
21513 token = cp_lexer_peek_token (parser->lexer);
21514
21515 /* If the next token is a `)', `,', `=', `>', or `...', then there
21516 is no declarator. However, when variadic templates are enabled,
21517 there may be a declarator following `...'. */
21518 if (token->type == CPP_CLOSE_PAREN
21519 || token->type == CPP_COMMA
21520 || token->type == CPP_EQ
21521 || token->type == CPP_GREATER)
21522 {
21523 declarator = NULL;
21524 if (parenthesized_p)
21525 *parenthesized_p = false;
21526 }
21527 /* Otherwise, there should be a declarator. */
21528 else
21529 {
21530 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21531 parser->default_arg_ok_p = false;
21532
21533 /* After seeing a decl-specifier-seq, if the next token is not a
21534 "(", there is no possibility that the code is a valid
21535 expression. Therefore, if parsing tentatively, we commit at
21536 this point. */
21537 if (!parser->in_template_argument_list_p
21538 /* In an expression context, having seen:
21539
21540 (int((char ...
21541
21542 we cannot be sure whether we are looking at a
21543 function-type (taking a "char" as a parameter) or a cast
21544 of some object of type "char" to "int". */
21545 && !parser->in_type_id_in_expr_p
21546 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21547 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21548 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21549 cp_parser_commit_to_tentative_parse (parser);
21550 /* Parse the declarator. */
21551 declarator_token_start = token;
21552 declarator = cp_parser_declarator (parser,
21553 CP_PARSER_DECLARATOR_EITHER,
21554 /*ctor_dtor_or_conv_p=*/NULL,
21555 parenthesized_p,
21556 /*member_p=*/false,
21557 /*friend_p=*/false);
21558 parser->default_arg_ok_p = saved_default_arg_ok_p;
21559 /* After the declarator, allow more attributes. */
21560 decl_specifiers.attributes
21561 = attr_chainon (decl_specifiers.attributes,
21562 cp_parser_attributes_opt (parser));
21563
21564 /* If the declarator is a template parameter pack, remember that and
21565 clear the flag in the declarator itself so we don't get errors
21566 from grokdeclarator. */
21567 if (template_parm_p && declarator && declarator->parameter_pack_p)
21568 {
21569 declarator->parameter_pack_p = false;
21570 template_parameter_pack_p = true;
21571 }
21572 }
21573
21574 /* If the next token is an ellipsis, and we have not seen a declarator
21575 name, and if either the type of the declarator contains parameter
21576 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21577 for, eg, abbreviated integral type names), then we actually have a
21578 parameter pack expansion expression. Otherwise, leave the ellipsis
21579 for a C-style variadic function. */
21580 token = cp_lexer_peek_token (parser->lexer);
21581 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21582 {
21583 tree type = decl_specifiers.type;
21584
21585 if (type && DECL_P (type))
21586 type = TREE_TYPE (type);
21587
21588 if (((type
21589 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21590 && (template_parm_p || uses_parameter_packs (type)))
21591 || (!type && template_parm_p))
21592 && declarator_can_be_parameter_pack (declarator))
21593 {
21594 /* Consume the `...'. */
21595 cp_lexer_consume_token (parser->lexer);
21596 maybe_warn_variadic_templates ();
21597
21598 /* Build a pack expansion type */
21599 if (template_parm_p)
21600 template_parameter_pack_p = true;
21601 else if (declarator)
21602 declarator->parameter_pack_p = true;
21603 else
21604 decl_specifiers.type = make_pack_expansion (type);
21605 }
21606 }
21607
21608 /* The restriction on defining new types applies only to the type
21609 of the parameter, not to the default argument. */
21610 parser->type_definition_forbidden_message = saved_message;
21611
21612 /* If the next token is `=', then process a default argument. */
21613 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21614 {
21615 tree type = decl_specifiers.type;
21616 token = cp_lexer_peek_token (parser->lexer);
21617 /* If we are defining a class, then the tokens that make up the
21618 default argument must be saved and processed later. */
21619 if (!template_parm_p && at_class_scope_p ()
21620 && TYPE_BEING_DEFINED (current_class_type)
21621 && !LAMBDA_TYPE_P (current_class_type))
21622 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21623
21624 // A constrained-type-specifier may declare a type template-parameter.
21625 else if (declares_constrained_type_template_parameter (type))
21626 default_argument
21627 = cp_parser_default_type_template_argument (parser);
21628
21629 // A constrained-type-specifier may declare a template-template-parameter.
21630 else if (declares_constrained_template_template_parameter (type))
21631 default_argument
21632 = cp_parser_default_template_template_argument (parser);
21633
21634 /* Outside of a class definition, we can just parse the
21635 assignment-expression. */
21636 else
21637 default_argument
21638 = cp_parser_default_argument (parser, template_parm_p);
21639
21640 if (!parser->default_arg_ok_p)
21641 {
21642 permerror (token->location,
21643 "default arguments are only "
21644 "permitted for function parameters");
21645 }
21646 else if ((declarator && declarator->parameter_pack_p)
21647 || template_parameter_pack_p
21648 || (decl_specifiers.type
21649 && PACK_EXPANSION_P (decl_specifiers.type)))
21650 {
21651 /* Find the name of the parameter pack. */
21652 cp_declarator *id_declarator = declarator;
21653 while (id_declarator && id_declarator->kind != cdk_id)
21654 id_declarator = id_declarator->declarator;
21655
21656 if (id_declarator && id_declarator->kind == cdk_id)
21657 error_at (declarator_token_start->location,
21658 template_parm_p
21659 ? G_("template parameter pack %qD "
21660 "cannot have a default argument")
21661 : G_("parameter pack %qD cannot have "
21662 "a default argument"),
21663 id_declarator->u.id.unqualified_name);
21664 else
21665 error_at (declarator_token_start->location,
21666 template_parm_p
21667 ? G_("template parameter pack cannot have "
21668 "a default argument")
21669 : G_("parameter pack cannot have a "
21670 "default argument"));
21671
21672 default_argument = NULL_TREE;
21673 }
21674 }
21675 else
21676 default_argument = NULL_TREE;
21677
21678 /* Generate a location for the parameter, ranging from the start of the
21679 initial token to the end of the final token (using input_location for
21680 the latter, set up by cp_lexer_set_source_position_from_token when
21681 consuming tokens).
21682
21683 If we have a identifier, then use it for the caret location, e.g.
21684
21685 extern int callee (int one, int (*two)(int, int), float three);
21686 ~~~~~~^~~~~~~~~~~~~~
21687
21688 otherwise, reuse the start location for the caret location e.g.:
21689
21690 extern int callee (int one, int (*)(int, int), float three);
21691 ^~~~~~~~~~~~~~~~~
21692
21693 */
21694 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21695 ? declarator->id_loc
21696 : decl_spec_token_start->location);
21697 location_t param_loc = make_location (caret_loc,
21698 decl_spec_token_start->location,
21699 input_location);
21700
21701 return make_parameter_declarator (&decl_specifiers,
21702 declarator,
21703 default_argument,
21704 param_loc,
21705 template_parameter_pack_p);
21706 }
21707
21708 /* Parse a default argument and return it.
21709
21710 TEMPLATE_PARM_P is true if this is a default argument for a
21711 non-type template parameter. */
21712 static tree
21713 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21714 {
21715 tree default_argument = NULL_TREE;
21716 bool saved_greater_than_is_operator_p;
21717 bool saved_local_variables_forbidden_p;
21718 bool non_constant_p, is_direct_init;
21719
21720 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21721 set correctly. */
21722 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21723 parser->greater_than_is_operator_p = !template_parm_p;
21724 /* Local variable names (and the `this' keyword) may not
21725 appear in a default argument. */
21726 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21727 parser->local_variables_forbidden_p = true;
21728 /* Parse the assignment-expression. */
21729 if (template_parm_p)
21730 push_deferring_access_checks (dk_no_deferred);
21731 tree saved_class_ptr = NULL_TREE;
21732 tree saved_class_ref = NULL_TREE;
21733 /* The "this" pointer is not valid in a default argument. */
21734 if (cfun)
21735 {
21736 saved_class_ptr = current_class_ptr;
21737 cp_function_chain->x_current_class_ptr = NULL_TREE;
21738 saved_class_ref = current_class_ref;
21739 cp_function_chain->x_current_class_ref = NULL_TREE;
21740 }
21741 default_argument
21742 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21743 /* Restore the "this" pointer. */
21744 if (cfun)
21745 {
21746 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21747 cp_function_chain->x_current_class_ref = saved_class_ref;
21748 }
21749 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21750 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21751 if (template_parm_p)
21752 pop_deferring_access_checks ();
21753 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21754 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21755
21756 return default_argument;
21757 }
21758
21759 /* Parse a function-body.
21760
21761 function-body:
21762 compound_statement */
21763
21764 static void
21765 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21766 {
21767 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21768 ? BCS_TRY_BLOCK : BCS_NORMAL),
21769 true);
21770 }
21771
21772 /* Parse a ctor-initializer-opt followed by a function-body. Return
21773 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21774 is true we are parsing a function-try-block. */
21775
21776 static void
21777 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21778 bool in_function_try_block)
21779 {
21780 tree body, list;
21781 const bool check_body_p =
21782 DECL_CONSTRUCTOR_P (current_function_decl)
21783 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21784 tree last = NULL;
21785
21786 /* Begin the function body. */
21787 body = begin_function_body ();
21788 /* Parse the optional ctor-initializer. */
21789 cp_parser_ctor_initializer_opt (parser);
21790
21791 /* If we're parsing a constexpr constructor definition, we need
21792 to check that the constructor body is indeed empty. However,
21793 before we get to cp_parser_function_body lot of junk has been
21794 generated, so we can't just check that we have an empty block.
21795 Rather we take a snapshot of the outermost block, and check whether
21796 cp_parser_function_body changed its state. */
21797 if (check_body_p)
21798 {
21799 list = cur_stmt_list;
21800 if (STATEMENT_LIST_TAIL (list))
21801 last = STATEMENT_LIST_TAIL (list)->stmt;
21802 }
21803 /* Parse the function-body. */
21804 cp_parser_function_body (parser, in_function_try_block);
21805 if (check_body_p)
21806 check_constexpr_ctor_body (last, list, /*complain=*/true);
21807 /* Finish the function body. */
21808 finish_function_body (body);
21809 }
21810
21811 /* Parse an initializer.
21812
21813 initializer:
21814 = initializer-clause
21815 ( expression-list )
21816
21817 Returns an expression representing the initializer. If no
21818 initializer is present, NULL_TREE is returned.
21819
21820 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21821 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21822 set to TRUE if there is no initializer present. If there is an
21823 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21824 is set to true; otherwise it is set to false. */
21825
21826 static tree
21827 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21828 bool* non_constant_p)
21829 {
21830 cp_token *token;
21831 tree init;
21832
21833 /* Peek at the next token. */
21834 token = cp_lexer_peek_token (parser->lexer);
21835
21836 /* Let our caller know whether or not this initializer was
21837 parenthesized. */
21838 *is_direct_init = (token->type != CPP_EQ);
21839 /* Assume that the initializer is constant. */
21840 *non_constant_p = false;
21841
21842 if (token->type == CPP_EQ)
21843 {
21844 /* Consume the `='. */
21845 cp_lexer_consume_token (parser->lexer);
21846 /* Parse the initializer-clause. */
21847 init = cp_parser_initializer_clause (parser, non_constant_p);
21848 }
21849 else if (token->type == CPP_OPEN_PAREN)
21850 {
21851 vec<tree, va_gc> *vec;
21852 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21853 /*cast_p=*/false,
21854 /*allow_expansion_p=*/true,
21855 non_constant_p);
21856 if (vec == NULL)
21857 return error_mark_node;
21858 init = build_tree_list_vec (vec);
21859 release_tree_vector (vec);
21860 }
21861 else if (token->type == CPP_OPEN_BRACE)
21862 {
21863 cp_lexer_set_source_position (parser->lexer);
21864 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21865 init = cp_parser_braced_list (parser, non_constant_p);
21866 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21867 }
21868 else
21869 {
21870 /* Anything else is an error. */
21871 cp_parser_error (parser, "expected initializer");
21872 init = error_mark_node;
21873 }
21874
21875 if (check_for_bare_parameter_packs (init))
21876 init = error_mark_node;
21877
21878 return init;
21879 }
21880
21881 /* Parse an initializer-clause.
21882
21883 initializer-clause:
21884 assignment-expression
21885 braced-init-list
21886
21887 Returns an expression representing the initializer.
21888
21889 If the `assignment-expression' production is used the value
21890 returned is simply a representation for the expression.
21891
21892 Otherwise, calls cp_parser_braced_list. */
21893
21894 static cp_expr
21895 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21896 {
21897 cp_expr initializer;
21898
21899 /* Assume the expression is constant. */
21900 *non_constant_p = false;
21901
21902 /* If it is not a `{', then we are looking at an
21903 assignment-expression. */
21904 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21905 {
21906 initializer
21907 = cp_parser_constant_expression (parser,
21908 /*allow_non_constant_p=*/true,
21909 non_constant_p);
21910 }
21911 else
21912 initializer = cp_parser_braced_list (parser, non_constant_p);
21913
21914 return initializer;
21915 }
21916
21917 /* Parse a brace-enclosed initializer list.
21918
21919 braced-init-list:
21920 { initializer-list , [opt] }
21921 { designated-initializer-list , [opt] }
21922 { }
21923
21924 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21925 the elements of the initializer-list (or NULL, if the last
21926 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21927 NULL_TREE. There is no way to detect whether or not the optional
21928 trailing `,' was provided. NON_CONSTANT_P is as for
21929 cp_parser_initializer. */
21930
21931 static cp_expr
21932 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21933 {
21934 tree initializer;
21935 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21936
21937 /* Consume the `{' token. */
21938 matching_braces braces;
21939 braces.require_open (parser);
21940 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21941 initializer = make_node (CONSTRUCTOR);
21942 /* If it's not a `}', then there is a non-trivial initializer. */
21943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21944 {
21945 /* Parse the initializer list. */
21946 CONSTRUCTOR_ELTS (initializer)
21947 = cp_parser_initializer_list (parser, non_constant_p);
21948 /* A trailing `,' token is allowed. */
21949 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21950 cp_lexer_consume_token (parser->lexer);
21951 }
21952 else
21953 *non_constant_p = false;
21954 /* Now, there should be a trailing `}'. */
21955 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21956 braces.require_close (parser);
21957 TREE_TYPE (initializer) = init_list_type_node;
21958
21959 cp_expr result (initializer);
21960 /* Build a location of the form:
21961 { ... }
21962 ^~~~~~~
21963 with caret==start at the open brace, finish at the close brace. */
21964 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21965 result.set_location (combined_loc);
21966 return result;
21967 }
21968
21969 /* Consume tokens up to, and including, the next non-nested closing `]'.
21970 Returns true iff we found a closing `]'. */
21971
21972 static bool
21973 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21974 {
21975 unsigned square_depth = 0;
21976
21977 while (true)
21978 {
21979 cp_token * token = cp_lexer_peek_token (parser->lexer);
21980
21981 switch (token->type)
21982 {
21983 case CPP_EOF:
21984 case CPP_PRAGMA_EOL:
21985 /* If we've run out of tokens, then there is no closing `]'. */
21986 return false;
21987
21988 case CPP_OPEN_SQUARE:
21989 ++square_depth;
21990 break;
21991
21992 case CPP_CLOSE_SQUARE:
21993 if (!square_depth--)
21994 {
21995 cp_lexer_consume_token (parser->lexer);
21996 return true;
21997 }
21998 break;
21999
22000 default:
22001 break;
22002 }
22003
22004 /* Consume the token. */
22005 cp_lexer_consume_token (parser->lexer);
22006 }
22007 }
22008
22009 /* Return true if we are looking at an array-designator, false otherwise. */
22010
22011 static bool
22012 cp_parser_array_designator_p (cp_parser *parser)
22013 {
22014 /* Consume the `['. */
22015 cp_lexer_consume_token (parser->lexer);
22016
22017 cp_lexer_save_tokens (parser->lexer);
22018
22019 /* Skip tokens until the next token is a closing square bracket.
22020 If we find the closing `]', and the next token is a `=', then
22021 we are looking at an array designator. */
22022 bool array_designator_p
22023 = (cp_parser_skip_to_closing_square_bracket (parser)
22024 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22025
22026 /* Roll back the tokens we skipped. */
22027 cp_lexer_rollback_tokens (parser->lexer);
22028
22029 return array_designator_p;
22030 }
22031
22032 /* Parse an initializer-list.
22033
22034 initializer-list:
22035 initializer-clause ... [opt]
22036 initializer-list , initializer-clause ... [opt]
22037
22038 C++2A Extension:
22039
22040 designated-initializer-list:
22041 designated-initializer-clause
22042 designated-initializer-list , designated-initializer-clause
22043
22044 designated-initializer-clause:
22045 designator brace-or-equal-initializer
22046
22047 designator:
22048 . identifier
22049
22050 GNU Extension:
22051
22052 initializer-list:
22053 designation initializer-clause ...[opt]
22054 initializer-list , designation initializer-clause ...[opt]
22055
22056 designation:
22057 . identifier =
22058 identifier :
22059 [ constant-expression ] =
22060
22061 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22062 for the initializer. If the INDEX of the elt is non-NULL, it is the
22063 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22064 as for cp_parser_initializer. */
22065
22066 static vec<constructor_elt, va_gc> *
22067 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22068 {
22069 vec<constructor_elt, va_gc> *v = NULL;
22070 bool first_p = true;
22071 tree first_designator = NULL_TREE;
22072
22073 /* Assume all of the expressions are constant. */
22074 *non_constant_p = false;
22075
22076 /* Parse the rest of the list. */
22077 while (true)
22078 {
22079 cp_token *token;
22080 tree designator;
22081 tree initializer;
22082 bool clause_non_constant_p;
22083 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22084
22085 /* Handle the C++2A syntax, '. id ='. */
22086 if ((cxx_dialect >= cxx2a
22087 || cp_parser_allow_gnu_extensions_p (parser))
22088 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22089 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22090 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22091 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22092 == CPP_OPEN_BRACE)))
22093 {
22094 if (cxx_dialect < cxx2a)
22095 pedwarn (loc, OPT_Wpedantic,
22096 "C++ designated initializers only available with "
22097 "-std=c++2a or -std=gnu++2a");
22098 /* Consume the `.'. */
22099 cp_lexer_consume_token (parser->lexer);
22100 /* Consume the identifier. */
22101 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22102 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22103 /* Consume the `='. */
22104 cp_lexer_consume_token (parser->lexer);
22105 }
22106 /* Also, if the next token is an identifier and the following one is a
22107 colon, we are looking at the GNU designated-initializer
22108 syntax. */
22109 else if (cp_parser_allow_gnu_extensions_p (parser)
22110 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22111 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22112 == CPP_COLON))
22113 {
22114 /* Warn the user that they are using an extension. */
22115 pedwarn (loc, OPT_Wpedantic,
22116 "ISO C++ does not allow GNU designated initializers");
22117 /* Consume the identifier. */
22118 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22119 /* Consume the `:'. */
22120 cp_lexer_consume_token (parser->lexer);
22121 }
22122 /* Also handle C99 array designators, '[ const ] ='. */
22123 else if (cp_parser_allow_gnu_extensions_p (parser)
22124 && !c_dialect_objc ()
22125 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22126 {
22127 /* In C++11, [ could start a lambda-introducer. */
22128 bool non_const = false;
22129
22130 cp_parser_parse_tentatively (parser);
22131
22132 if (!cp_parser_array_designator_p (parser))
22133 {
22134 cp_parser_simulate_error (parser);
22135 designator = NULL_TREE;
22136 }
22137 else
22138 {
22139 designator = cp_parser_constant_expression (parser, true,
22140 &non_const);
22141 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22142 cp_parser_require (parser, CPP_EQ, RT_EQ);
22143 }
22144
22145 if (!cp_parser_parse_definitely (parser))
22146 designator = NULL_TREE;
22147 else if (non_const
22148 && (!require_potential_rvalue_constant_expression
22149 (designator)))
22150 designator = NULL_TREE;
22151 if (designator)
22152 /* Warn the user that they are using an extension. */
22153 pedwarn (loc, OPT_Wpedantic,
22154 "ISO C++ does not allow C99 designated initializers");
22155 }
22156 else
22157 designator = NULL_TREE;
22158
22159 if (first_p)
22160 {
22161 first_designator = designator;
22162 first_p = false;
22163 }
22164 else if (cxx_dialect >= cxx2a
22165 && first_designator != error_mark_node
22166 && (!first_designator != !designator))
22167 {
22168 error_at (loc, "either all initializer clauses should be designated "
22169 "or none of them should be");
22170 first_designator = error_mark_node;
22171 }
22172 else if (cxx_dialect < cxx2a && !first_designator)
22173 first_designator = designator;
22174
22175 /* Parse the initializer. */
22176 initializer = cp_parser_initializer_clause (parser,
22177 &clause_non_constant_p);
22178 /* If any clause is non-constant, so is the entire initializer. */
22179 if (clause_non_constant_p)
22180 *non_constant_p = true;
22181
22182 /* If we have an ellipsis, this is an initializer pack
22183 expansion. */
22184 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22185 {
22186 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22187
22188 /* Consume the `...'. */
22189 cp_lexer_consume_token (parser->lexer);
22190
22191 if (designator && cxx_dialect >= cxx2a)
22192 error_at (loc,
22193 "%<...%> not allowed in designated initializer list");
22194
22195 /* Turn the initializer into an initializer expansion. */
22196 initializer = make_pack_expansion (initializer);
22197 }
22198
22199 /* Add it to the vector. */
22200 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22201
22202 /* If the next token is not a comma, we have reached the end of
22203 the list. */
22204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22205 break;
22206
22207 /* Peek at the next token. */
22208 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22209 /* If the next token is a `}', then we're still done. An
22210 initializer-clause can have a trailing `,' after the
22211 initializer-list and before the closing `}'. */
22212 if (token->type == CPP_CLOSE_BRACE)
22213 break;
22214
22215 /* Consume the `,' token. */
22216 cp_lexer_consume_token (parser->lexer);
22217 }
22218
22219 /* The same identifier shall not appear in multiple designators
22220 of a designated-initializer-list. */
22221 if (first_designator)
22222 {
22223 unsigned int i;
22224 tree designator, val;
22225 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22226 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22227 {
22228 if (IDENTIFIER_MARKED (designator))
22229 {
22230 error_at (EXPR_LOC_OR_LOC (val, input_location),
22231 "%<.%s%> designator used multiple times in "
22232 "the same initializer list",
22233 IDENTIFIER_POINTER (designator));
22234 (*v)[i].index = NULL_TREE;
22235 }
22236 else
22237 IDENTIFIER_MARKED (designator) = 1;
22238 }
22239 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22240 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22241 IDENTIFIER_MARKED (designator) = 0;
22242 }
22243
22244 return v;
22245 }
22246
22247 /* Classes [gram.class] */
22248
22249 /* Parse a class-name.
22250
22251 class-name:
22252 identifier
22253 template-id
22254
22255 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22256 to indicate that names looked up in dependent types should be
22257 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22258 keyword has been used to indicate that the name that appears next
22259 is a template. TAG_TYPE indicates the explicit tag given before
22260 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22261 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22262 is the class being defined in a class-head. If ENUM_OK is TRUE,
22263 enum-names are also accepted.
22264
22265 Returns the TYPE_DECL representing the class. */
22266
22267 static tree
22268 cp_parser_class_name (cp_parser *parser,
22269 bool typename_keyword_p,
22270 bool template_keyword_p,
22271 enum tag_types tag_type,
22272 bool check_dependency_p,
22273 bool class_head_p,
22274 bool is_declaration,
22275 bool enum_ok)
22276 {
22277 tree decl;
22278 tree scope;
22279 bool typename_p;
22280 cp_token *token;
22281 tree identifier = NULL_TREE;
22282
22283 /* All class-names start with an identifier. */
22284 token = cp_lexer_peek_token (parser->lexer);
22285 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22286 {
22287 cp_parser_error (parser, "expected class-name");
22288 return error_mark_node;
22289 }
22290
22291 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22292 to a template-id, so we save it here. */
22293 scope = parser->scope;
22294 if (scope == error_mark_node)
22295 return error_mark_node;
22296
22297 /* Any name names a type if we're following the `typename' keyword
22298 in a qualified name where the enclosing scope is type-dependent. */
22299 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22300 && dependent_type_p (scope));
22301 /* Handle the common case (an identifier, but not a template-id)
22302 efficiently. */
22303 if (token->type == CPP_NAME
22304 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22305 {
22306 cp_token *identifier_token;
22307 bool ambiguous_p;
22308
22309 /* Look for the identifier. */
22310 identifier_token = cp_lexer_peek_token (parser->lexer);
22311 ambiguous_p = identifier_token->error_reported;
22312 identifier = cp_parser_identifier (parser);
22313 /* If the next token isn't an identifier, we are certainly not
22314 looking at a class-name. */
22315 if (identifier == error_mark_node)
22316 decl = error_mark_node;
22317 /* If we know this is a type-name, there's no need to look it
22318 up. */
22319 else if (typename_p)
22320 decl = identifier;
22321 else
22322 {
22323 tree ambiguous_decls;
22324 /* If we already know that this lookup is ambiguous, then
22325 we've already issued an error message; there's no reason
22326 to check again. */
22327 if (ambiguous_p)
22328 {
22329 cp_parser_simulate_error (parser);
22330 return error_mark_node;
22331 }
22332 /* If the next token is a `::', then the name must be a type
22333 name.
22334
22335 [basic.lookup.qual]
22336
22337 During the lookup for a name preceding the :: scope
22338 resolution operator, object, function, and enumerator
22339 names are ignored. */
22340 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22341 tag_type = scope_type;
22342 /* Look up the name. */
22343 decl = cp_parser_lookup_name (parser, identifier,
22344 tag_type,
22345 /*is_template=*/false,
22346 /*is_namespace=*/false,
22347 check_dependency_p,
22348 &ambiguous_decls,
22349 identifier_token->location);
22350 if (ambiguous_decls)
22351 {
22352 if (cp_parser_parsing_tentatively (parser))
22353 cp_parser_simulate_error (parser);
22354 return error_mark_node;
22355 }
22356 }
22357 }
22358 else
22359 {
22360 /* Try a template-id. */
22361 decl = cp_parser_template_id (parser, template_keyword_p,
22362 check_dependency_p,
22363 tag_type,
22364 is_declaration);
22365 if (decl == error_mark_node)
22366 return error_mark_node;
22367 }
22368
22369 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22370
22371 /* If this is a typename, create a TYPENAME_TYPE. */
22372 if (typename_p && decl != error_mark_node)
22373 {
22374 decl = make_typename_type (scope, decl, typename_type,
22375 /*complain=*/tf_error);
22376 if (decl != error_mark_node)
22377 decl = TYPE_NAME (decl);
22378 }
22379
22380 decl = strip_using_decl (decl);
22381
22382 /* Check to see that it is really the name of a class. */
22383 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22384 && identifier_p (TREE_OPERAND (decl, 0))
22385 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22386 /* Situations like this:
22387
22388 template <typename T> struct A {
22389 typename T::template X<int>::I i;
22390 };
22391
22392 are problematic. Is `T::template X<int>' a class-name? The
22393 standard does not seem to be definitive, but there is no other
22394 valid interpretation of the following `::'. Therefore, those
22395 names are considered class-names. */
22396 {
22397 decl = make_typename_type (scope, decl, tag_type, tf_error);
22398 if (decl != error_mark_node)
22399 decl = TYPE_NAME (decl);
22400 }
22401 else if (TREE_CODE (decl) != TYPE_DECL
22402 || TREE_TYPE (decl) == error_mark_node
22403 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22404 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22405 /* In Objective-C 2.0, a classname followed by '.' starts a
22406 dot-syntax expression, and it's not a type-name. */
22407 || (c_dialect_objc ()
22408 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22409 && objc_is_class_name (decl)))
22410 decl = error_mark_node;
22411
22412 if (decl == error_mark_node)
22413 cp_parser_error (parser, "expected class-name");
22414 else if (identifier && !parser->scope)
22415 maybe_note_name_used_in_class (identifier, decl);
22416
22417 return decl;
22418 }
22419
22420 /* Parse a class-specifier.
22421
22422 class-specifier:
22423 class-head { member-specification [opt] }
22424
22425 Returns the TREE_TYPE representing the class. */
22426
22427 static tree
22428 cp_parser_class_specifier_1 (cp_parser* parser)
22429 {
22430 tree type;
22431 tree attributes = NULL_TREE;
22432 bool nested_name_specifier_p;
22433 unsigned saved_num_template_parameter_lists;
22434 bool saved_in_function_body;
22435 unsigned char in_statement;
22436 bool in_switch_statement_p;
22437 bool saved_in_unbraced_linkage_specification_p;
22438 tree old_scope = NULL_TREE;
22439 tree scope = NULL_TREE;
22440 cp_token *closing_brace;
22441
22442 push_deferring_access_checks (dk_no_deferred);
22443
22444 /* Parse the class-head. */
22445 type = cp_parser_class_head (parser,
22446 &nested_name_specifier_p);
22447 /* If the class-head was a semantic disaster, skip the entire body
22448 of the class. */
22449 if (!type)
22450 {
22451 cp_parser_skip_to_end_of_block_or_statement (parser);
22452 pop_deferring_access_checks ();
22453 return error_mark_node;
22454 }
22455
22456 /* Look for the `{'. */
22457 matching_braces braces;
22458 if (!braces.require_open (parser))
22459 {
22460 pop_deferring_access_checks ();
22461 return error_mark_node;
22462 }
22463
22464 cp_ensure_no_omp_declare_simd (parser);
22465 cp_ensure_no_oacc_routine (parser);
22466
22467 /* Issue an error message if type-definitions are forbidden here. */
22468 cp_parser_check_type_definition (parser);
22469 /* Remember that we are defining one more class. */
22470 ++parser->num_classes_being_defined;
22471 /* Inside the class, surrounding template-parameter-lists do not
22472 apply. */
22473 saved_num_template_parameter_lists
22474 = parser->num_template_parameter_lists;
22475 parser->num_template_parameter_lists = 0;
22476 /* We are not in a function body. */
22477 saved_in_function_body = parser->in_function_body;
22478 parser->in_function_body = false;
22479 /* Or in a loop. */
22480 in_statement = parser->in_statement;
22481 parser->in_statement = 0;
22482 /* Or in a switch. */
22483 in_switch_statement_p = parser->in_switch_statement_p;
22484 parser->in_switch_statement_p = false;
22485 /* We are not immediately inside an extern "lang" block. */
22486 saved_in_unbraced_linkage_specification_p
22487 = parser->in_unbraced_linkage_specification_p;
22488 parser->in_unbraced_linkage_specification_p = false;
22489
22490 // Associate constraints with the type.
22491 if (flag_concepts)
22492 type = associate_classtype_constraints (type);
22493
22494 /* Start the class. */
22495 if (nested_name_specifier_p)
22496 {
22497 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22498 old_scope = push_inner_scope (scope);
22499 }
22500 type = begin_class_definition (type);
22501
22502 if (type == error_mark_node)
22503 /* If the type is erroneous, skip the entire body of the class. */
22504 cp_parser_skip_to_closing_brace (parser);
22505 else
22506 /* Parse the member-specification. */
22507 cp_parser_member_specification_opt (parser);
22508
22509 /* Look for the trailing `}'. */
22510 closing_brace = braces.require_close (parser);
22511 /* Look for trailing attributes to apply to this class. */
22512 if (cp_parser_allow_gnu_extensions_p (parser))
22513 attributes = cp_parser_gnu_attributes_opt (parser);
22514 if (type != error_mark_node)
22515 type = finish_struct (type, attributes);
22516 if (nested_name_specifier_p)
22517 pop_inner_scope (old_scope, scope);
22518
22519 /* We've finished a type definition. Check for the common syntax
22520 error of forgetting a semicolon after the definition. We need to
22521 be careful, as we can't just check for not-a-semicolon and be done
22522 with it; the user might have typed:
22523
22524 class X { } c = ...;
22525 class X { } *p = ...;
22526
22527 and so forth. Instead, enumerate all the possible tokens that
22528 might follow this production; if we don't see one of them, then
22529 complain and silently insert the semicolon. */
22530 {
22531 cp_token *token = cp_lexer_peek_token (parser->lexer);
22532 bool want_semicolon = true;
22533
22534 if (cp_next_tokens_can_be_std_attribute_p (parser))
22535 /* Don't try to parse c++11 attributes here. As per the
22536 grammar, that should be a task for
22537 cp_parser_decl_specifier_seq. */
22538 want_semicolon = false;
22539
22540 switch (token->type)
22541 {
22542 case CPP_NAME:
22543 case CPP_SEMICOLON:
22544 case CPP_MULT:
22545 case CPP_AND:
22546 case CPP_OPEN_PAREN:
22547 case CPP_CLOSE_PAREN:
22548 case CPP_COMMA:
22549 want_semicolon = false;
22550 break;
22551
22552 /* While it's legal for type qualifiers and storage class
22553 specifiers to follow type definitions in the grammar, only
22554 compiler testsuites contain code like that. Assume that if
22555 we see such code, then what we're really seeing is a case
22556 like:
22557
22558 class X { }
22559 const <type> var = ...;
22560
22561 or
22562
22563 class Y { }
22564 static <type> func (...) ...
22565
22566 i.e. the qualifier or specifier applies to the next
22567 declaration. To do so, however, we need to look ahead one
22568 more token to see if *that* token is a type specifier.
22569
22570 This code could be improved to handle:
22571
22572 class Z { }
22573 static const <type> var = ...; */
22574 case CPP_KEYWORD:
22575 if (keyword_is_decl_specifier (token->keyword))
22576 {
22577 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22578
22579 /* Handling user-defined types here would be nice, but very
22580 tricky. */
22581 want_semicolon
22582 = (lookahead->type == CPP_KEYWORD
22583 && keyword_begins_type_specifier (lookahead->keyword));
22584 }
22585 break;
22586 default:
22587 break;
22588 }
22589
22590 /* If we don't have a type, then something is very wrong and we
22591 shouldn't try to do anything clever. Likewise for not seeing the
22592 closing brace. */
22593 if (closing_brace && TYPE_P (type) && want_semicolon)
22594 {
22595 /* Locate the closing brace. */
22596 cp_token_position prev
22597 = cp_lexer_previous_token_position (parser->lexer);
22598 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22599 location_t loc = prev_token->location;
22600
22601 /* We want to suggest insertion of a ';' immediately *after* the
22602 closing brace, so, if we can, offset the location by 1 column. */
22603 location_t next_loc = loc;
22604 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22605 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22606
22607 rich_location richloc (line_table, next_loc);
22608
22609 /* If we successfully offset the location, suggest the fix-it. */
22610 if (next_loc != loc)
22611 richloc.add_fixit_insert_before (next_loc, ";");
22612
22613 if (CLASSTYPE_DECLARED_CLASS (type))
22614 error_at (&richloc,
22615 "expected %<;%> after class definition");
22616 else if (TREE_CODE (type) == RECORD_TYPE)
22617 error_at (&richloc,
22618 "expected %<;%> after struct definition");
22619 else if (TREE_CODE (type) == UNION_TYPE)
22620 error_at (&richloc,
22621 "expected %<;%> after union definition");
22622 else
22623 gcc_unreachable ();
22624
22625 /* Unget one token and smash it to look as though we encountered
22626 a semicolon in the input stream. */
22627 cp_lexer_set_token_position (parser->lexer, prev);
22628 token = cp_lexer_peek_token (parser->lexer);
22629 token->type = CPP_SEMICOLON;
22630 token->keyword = RID_MAX;
22631 }
22632 }
22633
22634 /* If this class is not itself within the scope of another class,
22635 then we need to parse the bodies of all of the queued function
22636 definitions. Note that the queued functions defined in a class
22637 are not always processed immediately following the
22638 class-specifier for that class. Consider:
22639
22640 struct A {
22641 struct B { void f() { sizeof (A); } };
22642 };
22643
22644 If `f' were processed before the processing of `A' were
22645 completed, there would be no way to compute the size of `A'.
22646 Note that the nesting we are interested in here is lexical --
22647 not the semantic nesting given by TYPE_CONTEXT. In particular,
22648 for:
22649
22650 struct A { struct B; };
22651 struct A::B { void f() { } };
22652
22653 there is no need to delay the parsing of `A::B::f'. */
22654 if (--parser->num_classes_being_defined == 0)
22655 {
22656 tree decl;
22657 tree class_type = NULL_TREE;
22658 tree pushed_scope = NULL_TREE;
22659 unsigned ix;
22660 cp_default_arg_entry *e;
22661 tree save_ccp, save_ccr;
22662
22663 /* In a first pass, parse default arguments to the functions.
22664 Then, in a second pass, parse the bodies of the functions.
22665 This two-phased approach handles cases like:
22666
22667 struct S {
22668 void f() { g(); }
22669 void g(int i = 3);
22670 };
22671
22672 */
22673 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22674 {
22675 decl = e->decl;
22676 /* If there are default arguments that have not yet been processed,
22677 take care of them now. */
22678 if (class_type != e->class_type)
22679 {
22680 if (pushed_scope)
22681 pop_scope (pushed_scope);
22682 class_type = e->class_type;
22683 pushed_scope = push_scope (class_type);
22684 }
22685 /* Make sure that any template parameters are in scope. */
22686 maybe_begin_member_template_processing (decl);
22687 /* Parse the default argument expressions. */
22688 cp_parser_late_parsing_default_args (parser, decl);
22689 /* Remove any template parameters from the symbol table. */
22690 maybe_end_member_template_processing ();
22691 }
22692 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22693 /* Now parse any NSDMIs. */
22694 save_ccp = current_class_ptr;
22695 save_ccr = current_class_ref;
22696 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22697 {
22698 if (class_type != DECL_CONTEXT (decl))
22699 {
22700 if (pushed_scope)
22701 pop_scope (pushed_scope);
22702 class_type = DECL_CONTEXT (decl);
22703 pushed_scope = push_scope (class_type);
22704 }
22705 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22706 cp_parser_late_parsing_nsdmi (parser, decl);
22707 }
22708 vec_safe_truncate (unparsed_nsdmis, 0);
22709 current_class_ptr = save_ccp;
22710 current_class_ref = save_ccr;
22711 if (pushed_scope)
22712 pop_scope (pushed_scope);
22713
22714 /* Now do some post-NSDMI bookkeeping. */
22715 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22716 after_nsdmi_defaulted_late_checks (class_type);
22717 vec_safe_truncate (unparsed_classes, 0);
22718 after_nsdmi_defaulted_late_checks (type);
22719
22720 /* Now parse the body of the functions. */
22721 if (flag_openmp)
22722 {
22723 /* OpenMP UDRs need to be parsed before all other functions. */
22724 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22725 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22726 cp_parser_late_parsing_for_member (parser, decl);
22727 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22728 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22729 cp_parser_late_parsing_for_member (parser, decl);
22730 }
22731 else
22732 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22733 cp_parser_late_parsing_for_member (parser, decl);
22734 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22735 }
22736 else
22737 vec_safe_push (unparsed_classes, type);
22738
22739 /* Put back any saved access checks. */
22740 pop_deferring_access_checks ();
22741
22742 /* Restore saved state. */
22743 parser->in_switch_statement_p = in_switch_statement_p;
22744 parser->in_statement = in_statement;
22745 parser->in_function_body = saved_in_function_body;
22746 parser->num_template_parameter_lists
22747 = saved_num_template_parameter_lists;
22748 parser->in_unbraced_linkage_specification_p
22749 = saved_in_unbraced_linkage_specification_p;
22750
22751 return type;
22752 }
22753
22754 static tree
22755 cp_parser_class_specifier (cp_parser* parser)
22756 {
22757 tree ret;
22758 timevar_push (TV_PARSE_STRUCT);
22759 ret = cp_parser_class_specifier_1 (parser);
22760 timevar_pop (TV_PARSE_STRUCT);
22761 return ret;
22762 }
22763
22764 /* Parse a class-head.
22765
22766 class-head:
22767 class-key identifier [opt] base-clause [opt]
22768 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22769 class-key nested-name-specifier [opt] template-id
22770 base-clause [opt]
22771
22772 class-virt-specifier:
22773 final
22774
22775 GNU Extensions:
22776 class-key attributes identifier [opt] base-clause [opt]
22777 class-key attributes nested-name-specifier identifier base-clause [opt]
22778 class-key attributes nested-name-specifier [opt] template-id
22779 base-clause [opt]
22780
22781 Upon return BASES is initialized to the list of base classes (or
22782 NULL, if there are none) in the same form returned by
22783 cp_parser_base_clause.
22784
22785 Returns the TYPE of the indicated class. Sets
22786 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22787 involving a nested-name-specifier was used, and FALSE otherwise.
22788
22789 Returns error_mark_node if this is not a class-head.
22790
22791 Returns NULL_TREE if the class-head is syntactically valid, but
22792 semantically invalid in a way that means we should skip the entire
22793 body of the class. */
22794
22795 static tree
22796 cp_parser_class_head (cp_parser* parser,
22797 bool* nested_name_specifier_p)
22798 {
22799 tree nested_name_specifier;
22800 enum tag_types class_key;
22801 tree id = NULL_TREE;
22802 tree type = NULL_TREE;
22803 tree attributes;
22804 tree bases;
22805 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22806 bool template_id_p = false;
22807 bool qualified_p = false;
22808 bool invalid_nested_name_p = false;
22809 bool invalid_explicit_specialization_p = false;
22810 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22811 tree pushed_scope = NULL_TREE;
22812 unsigned num_templates;
22813 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22814 /* Assume no nested-name-specifier will be present. */
22815 *nested_name_specifier_p = false;
22816 /* Assume no template parameter lists will be used in defining the
22817 type. */
22818 num_templates = 0;
22819 parser->colon_corrects_to_scope_p = false;
22820
22821 /* Look for the class-key. */
22822 class_key = cp_parser_class_key (parser);
22823 if (class_key == none_type)
22824 return error_mark_node;
22825
22826 location_t class_head_start_location = input_location;
22827
22828 /* Parse the attributes. */
22829 attributes = cp_parser_attributes_opt (parser);
22830
22831 /* If the next token is `::', that is invalid -- but sometimes
22832 people do try to write:
22833
22834 struct ::S {};
22835
22836 Handle this gracefully by accepting the extra qualifier, and then
22837 issuing an error about it later if this really is a
22838 class-head. If it turns out just to be an elaborated type
22839 specifier, remain silent. */
22840 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22841 qualified_p = true;
22842
22843 push_deferring_access_checks (dk_no_check);
22844
22845 /* Determine the name of the class. Begin by looking for an
22846 optional nested-name-specifier. */
22847 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22848 nested_name_specifier
22849 = cp_parser_nested_name_specifier_opt (parser,
22850 /*typename_keyword_p=*/false,
22851 /*check_dependency_p=*/false,
22852 /*type_p=*/true,
22853 /*is_declaration=*/false);
22854 /* If there was a nested-name-specifier, then there *must* be an
22855 identifier. */
22856
22857 cp_token *bad_template_keyword = NULL;
22858
22859 if (nested_name_specifier)
22860 {
22861 type_start_token = cp_lexer_peek_token (parser->lexer);
22862 /* Although the grammar says `identifier', it really means
22863 `class-name' or `template-name'. You are only allowed to
22864 define a class that has already been declared with this
22865 syntax.
22866
22867 The proposed resolution for Core Issue 180 says that wherever
22868 you see `class T::X' you should treat `X' as a type-name.
22869
22870 It is OK to define an inaccessible class; for example:
22871
22872 class A { class B; };
22873 class A::B {};
22874
22875 We do not know if we will see a class-name, or a
22876 template-name. We look for a class-name first, in case the
22877 class-name is a template-id; if we looked for the
22878 template-name first we would stop after the template-name. */
22879 cp_parser_parse_tentatively (parser);
22880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22881 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22882 type = cp_parser_class_name (parser,
22883 /*typename_keyword_p=*/false,
22884 /*template_keyword_p=*/false,
22885 class_type,
22886 /*check_dependency_p=*/false,
22887 /*class_head_p=*/true,
22888 /*is_declaration=*/false);
22889 /* If that didn't work, ignore the nested-name-specifier. */
22890 if (!cp_parser_parse_definitely (parser))
22891 {
22892 invalid_nested_name_p = true;
22893 type_start_token = cp_lexer_peek_token (parser->lexer);
22894 id = cp_parser_identifier (parser);
22895 if (id == error_mark_node)
22896 id = NULL_TREE;
22897 }
22898 /* If we could not find a corresponding TYPE, treat this
22899 declaration like an unqualified declaration. */
22900 if (type == error_mark_node)
22901 nested_name_specifier = NULL_TREE;
22902 /* Otherwise, count the number of templates used in TYPE and its
22903 containing scopes. */
22904 else
22905 {
22906 tree scope;
22907
22908 for (scope = TREE_TYPE (type);
22909 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22910 scope = get_containing_scope (scope))
22911 if (TYPE_P (scope)
22912 && CLASS_TYPE_P (scope)
22913 && CLASSTYPE_TEMPLATE_INFO (scope)
22914 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22915 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22916 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22917 ++num_templates;
22918 }
22919 }
22920 /* Otherwise, the identifier is optional. */
22921 else
22922 {
22923 /* We don't know whether what comes next is a template-id,
22924 an identifier, or nothing at all. */
22925 cp_parser_parse_tentatively (parser);
22926 /* Check for a template-id. */
22927 type_start_token = cp_lexer_peek_token (parser->lexer);
22928 id = cp_parser_template_id (parser,
22929 /*template_keyword_p=*/false,
22930 /*check_dependency_p=*/true,
22931 class_key,
22932 /*is_declaration=*/true);
22933 /* If that didn't work, it could still be an identifier. */
22934 if (!cp_parser_parse_definitely (parser))
22935 {
22936 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22937 {
22938 type_start_token = cp_lexer_peek_token (parser->lexer);
22939 id = cp_parser_identifier (parser);
22940 }
22941 else
22942 id = NULL_TREE;
22943 }
22944 else
22945 {
22946 template_id_p = true;
22947 ++num_templates;
22948 }
22949 }
22950
22951 pop_deferring_access_checks ();
22952
22953 if (id)
22954 {
22955 cp_parser_check_for_invalid_template_id (parser, id,
22956 class_key,
22957 type_start_token->location);
22958 }
22959 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22960
22961 /* If it's not a `:' or a `{' then we can't really be looking at a
22962 class-head, since a class-head only appears as part of a
22963 class-specifier. We have to detect this situation before calling
22964 xref_tag, since that has irreversible side-effects. */
22965 if (!cp_parser_next_token_starts_class_definition_p (parser))
22966 {
22967 cp_parser_error (parser, "expected %<{%> or %<:%>");
22968 type = error_mark_node;
22969 goto out;
22970 }
22971
22972 /* At this point, we're going ahead with the class-specifier, even
22973 if some other problem occurs. */
22974 cp_parser_commit_to_tentative_parse (parser);
22975 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22976 {
22977 cp_parser_error (parser,
22978 "cannot specify %<override%> for a class");
22979 type = error_mark_node;
22980 goto out;
22981 }
22982 /* Issue the error about the overly-qualified name now. */
22983 if (qualified_p)
22984 {
22985 cp_parser_error (parser,
22986 "global qualification of class name is invalid");
22987 type = error_mark_node;
22988 goto out;
22989 }
22990 else if (invalid_nested_name_p)
22991 {
22992 cp_parser_error (parser,
22993 "qualified name does not name a class");
22994 type = error_mark_node;
22995 goto out;
22996 }
22997 else if (nested_name_specifier)
22998 {
22999 tree scope;
23000
23001 if (bad_template_keyword)
23002 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23003 keyword template shall not appear at the top level. */
23004 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23005 "keyword %<template%> not allowed in class-head-name");
23006
23007 /* Reject typedef-names in class heads. */
23008 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23009 {
23010 error_at (type_start_token->location,
23011 "invalid class name in declaration of %qD",
23012 type);
23013 type = NULL_TREE;
23014 goto done;
23015 }
23016
23017 /* Figure out in what scope the declaration is being placed. */
23018 scope = current_scope ();
23019 /* If that scope does not contain the scope in which the
23020 class was originally declared, the program is invalid. */
23021 if (scope && !is_ancestor (scope, nested_name_specifier))
23022 {
23023 if (at_namespace_scope_p ())
23024 error_at (type_start_token->location,
23025 "declaration of %qD in namespace %qD which does not "
23026 "enclose %qD",
23027 type, scope, nested_name_specifier);
23028 else
23029 error_at (type_start_token->location,
23030 "declaration of %qD in %qD which does not enclose %qD",
23031 type, scope, nested_name_specifier);
23032 type = NULL_TREE;
23033 goto done;
23034 }
23035 /* [dcl.meaning]
23036
23037 A declarator-id shall not be qualified except for the
23038 definition of a ... nested class outside of its class
23039 ... [or] the definition or explicit instantiation of a
23040 class member of a namespace outside of its namespace. */
23041 if (scope == nested_name_specifier)
23042 {
23043 permerror (nested_name_specifier_token_start->location,
23044 "extra qualification not allowed");
23045 nested_name_specifier = NULL_TREE;
23046 num_templates = 0;
23047 }
23048 }
23049 /* An explicit-specialization must be preceded by "template <>". If
23050 it is not, try to recover gracefully. */
23051 if (at_namespace_scope_p ()
23052 && parser->num_template_parameter_lists == 0
23053 && !processing_template_parmlist
23054 && template_id_p)
23055 {
23056 /* Build a location of this form:
23057 struct typename <ARGS>
23058 ^~~~~~~~~~~~~~~~~~~~~~
23059 with caret==start at the start token, and
23060 finishing at the end of the type. */
23061 location_t reported_loc
23062 = make_location (class_head_start_location,
23063 class_head_start_location,
23064 get_finish (type_start_token->location));
23065 rich_location richloc (line_table, reported_loc);
23066 richloc.add_fixit_insert_before (class_head_start_location,
23067 "template <> ");
23068 error_at (&richloc,
23069 "an explicit specialization must be preceded by"
23070 " %<template <>%>");
23071 invalid_explicit_specialization_p = true;
23072 /* Take the same action that would have been taken by
23073 cp_parser_explicit_specialization. */
23074 ++parser->num_template_parameter_lists;
23075 begin_specialization ();
23076 }
23077 /* There must be no "return" statements between this point and the
23078 end of this function; set "type "to the correct return value and
23079 use "goto done;" to return. */
23080 /* Make sure that the right number of template parameters were
23081 present. */
23082 if (!cp_parser_check_template_parameters (parser, num_templates,
23083 type_start_token->location,
23084 /*declarator=*/NULL))
23085 {
23086 /* If something went wrong, there is no point in even trying to
23087 process the class-definition. */
23088 type = NULL_TREE;
23089 goto done;
23090 }
23091
23092 /* Look up the type. */
23093 if (template_id_p)
23094 {
23095 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23096 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23097 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23098 {
23099 error_at (type_start_token->location,
23100 "function template %qD redeclared as a class template", id);
23101 type = error_mark_node;
23102 }
23103 else
23104 {
23105 type = TREE_TYPE (id);
23106 type = maybe_process_partial_specialization (type);
23107
23108 /* Check the scope while we still know whether or not we had a
23109 nested-name-specifier. */
23110 if (type != error_mark_node)
23111 check_unqualified_spec_or_inst (type, type_start_token->location);
23112 }
23113 if (nested_name_specifier)
23114 pushed_scope = push_scope (nested_name_specifier);
23115 }
23116 else if (nested_name_specifier)
23117 {
23118 tree class_type;
23119
23120 /* Given:
23121
23122 template <typename T> struct S { struct T };
23123 template <typename T> struct S<T>::T { };
23124
23125 we will get a TYPENAME_TYPE when processing the definition of
23126 `S::T'. We need to resolve it to the actual type before we
23127 try to define it. */
23128 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23129 {
23130 class_type = resolve_typename_type (TREE_TYPE (type),
23131 /*only_current_p=*/false);
23132 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23133 type = TYPE_NAME (class_type);
23134 else
23135 {
23136 cp_parser_error (parser, "could not resolve typename type");
23137 type = error_mark_node;
23138 }
23139 }
23140
23141 if (maybe_process_partial_specialization (TREE_TYPE (type))
23142 == error_mark_node)
23143 {
23144 type = NULL_TREE;
23145 goto done;
23146 }
23147
23148 class_type = current_class_type;
23149 /* Enter the scope indicated by the nested-name-specifier. */
23150 pushed_scope = push_scope (nested_name_specifier);
23151 /* Get the canonical version of this type. */
23152 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23153 /* Call push_template_decl if it seems like we should be defining a
23154 template either from the template headers or the type we're
23155 defining, so that we diagnose both extra and missing headers. */
23156 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23157 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23158 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23159 {
23160 type = push_template_decl (type);
23161 if (type == error_mark_node)
23162 {
23163 type = NULL_TREE;
23164 goto done;
23165 }
23166 }
23167
23168 type = TREE_TYPE (type);
23169 *nested_name_specifier_p = true;
23170 }
23171 else /* The name is not a nested name. */
23172 {
23173 /* If the class was unnamed, create a dummy name. */
23174 if (!id)
23175 id = make_anon_name ();
23176 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23177 ? ts_within_enclosing_non_class
23178 : ts_current);
23179 type = xref_tag (class_key, id, tag_scope,
23180 parser->num_template_parameter_lists);
23181 }
23182
23183 /* Indicate whether this class was declared as a `class' or as a
23184 `struct'. */
23185 if (TREE_CODE (type) == RECORD_TYPE)
23186 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23187 cp_parser_check_class_key (class_key, type);
23188
23189 /* If this type was already complete, and we see another definition,
23190 that's an error. */
23191 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23192 {
23193 error_at (type_start_token->location, "redefinition of %q#T",
23194 type);
23195 inform (location_of (type), "previous definition of %q#T",
23196 type);
23197 type = NULL_TREE;
23198 goto done;
23199 }
23200 else if (type == error_mark_node)
23201 type = NULL_TREE;
23202
23203 if (type)
23204 {
23205 /* Apply attributes now, before any use of the class as a template
23206 argument in its base list. */
23207 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23208 fixup_attribute_variants (type);
23209 }
23210
23211 /* We will have entered the scope containing the class; the names of
23212 base classes should be looked up in that context. For example:
23213
23214 struct A { struct B {}; struct C; };
23215 struct A::C : B {};
23216
23217 is valid. */
23218
23219 /* Get the list of base-classes, if there is one. */
23220 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23221 {
23222 /* PR59482: enter the class scope so that base-specifiers are looked
23223 up correctly. */
23224 if (type)
23225 pushclass (type);
23226 bases = cp_parser_base_clause (parser);
23227 /* PR59482: get out of the previously pushed class scope so that the
23228 subsequent pops pop the right thing. */
23229 if (type)
23230 popclass ();
23231 }
23232 else
23233 bases = NULL_TREE;
23234
23235 /* If we're really defining a class, process the base classes.
23236 If they're invalid, fail. */
23237 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23238 xref_basetypes (type, bases);
23239
23240 done:
23241 /* Leave the scope given by the nested-name-specifier. We will
23242 enter the class scope itself while processing the members. */
23243 if (pushed_scope)
23244 pop_scope (pushed_scope);
23245
23246 if (invalid_explicit_specialization_p)
23247 {
23248 end_specialization ();
23249 --parser->num_template_parameter_lists;
23250 }
23251
23252 if (type)
23253 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23254 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23255 CLASSTYPE_FINAL (type) = 1;
23256 out:
23257 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23258 return type;
23259 }
23260
23261 /* Parse a class-key.
23262
23263 class-key:
23264 class
23265 struct
23266 union
23267
23268 Returns the kind of class-key specified, or none_type to indicate
23269 error. */
23270
23271 static enum tag_types
23272 cp_parser_class_key (cp_parser* parser)
23273 {
23274 cp_token *token;
23275 enum tag_types tag_type;
23276
23277 /* Look for the class-key. */
23278 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23279 if (!token)
23280 return none_type;
23281
23282 /* Check to see if the TOKEN is a class-key. */
23283 tag_type = cp_parser_token_is_class_key (token);
23284 if (!tag_type)
23285 cp_parser_error (parser, "expected class-key");
23286 return tag_type;
23287 }
23288
23289 /* Parse a type-parameter-key.
23290
23291 type-parameter-key:
23292 class
23293 typename
23294 */
23295
23296 static void
23297 cp_parser_type_parameter_key (cp_parser* parser)
23298 {
23299 /* Look for the type-parameter-key. */
23300 enum tag_types tag_type = none_type;
23301 cp_token *token = cp_lexer_peek_token (parser->lexer);
23302 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23303 {
23304 cp_lexer_consume_token (parser->lexer);
23305 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23306 /* typename is not allowed in a template template parameter
23307 by the standard until C++17. */
23308 pedwarn (token->location, OPT_Wpedantic,
23309 "ISO C++ forbids typename key in template template parameter;"
23310 " use -std=c++17 or -std=gnu++17");
23311 }
23312 else
23313 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23314
23315 return;
23316 }
23317
23318 /* Parse an (optional) member-specification.
23319
23320 member-specification:
23321 member-declaration member-specification [opt]
23322 access-specifier : member-specification [opt] */
23323
23324 static void
23325 cp_parser_member_specification_opt (cp_parser* parser)
23326 {
23327 while (true)
23328 {
23329 cp_token *token;
23330 enum rid keyword;
23331
23332 /* Peek at the next token. */
23333 token = cp_lexer_peek_token (parser->lexer);
23334 /* If it's a `}', or EOF then we've seen all the members. */
23335 if (token->type == CPP_CLOSE_BRACE
23336 || token->type == CPP_EOF
23337 || token->type == CPP_PRAGMA_EOL)
23338 break;
23339
23340 /* See if this token is a keyword. */
23341 keyword = token->keyword;
23342 switch (keyword)
23343 {
23344 case RID_PUBLIC:
23345 case RID_PROTECTED:
23346 case RID_PRIVATE:
23347 /* Consume the access-specifier. */
23348 cp_lexer_consume_token (parser->lexer);
23349 /* Remember which access-specifier is active. */
23350 current_access_specifier = token->u.value;
23351 /* Look for the `:'. */
23352 cp_parser_require (parser, CPP_COLON, RT_COLON);
23353 break;
23354
23355 default:
23356 /* Accept #pragmas at class scope. */
23357 if (token->type == CPP_PRAGMA)
23358 {
23359 cp_parser_pragma (parser, pragma_member, NULL);
23360 break;
23361 }
23362
23363 /* Otherwise, the next construction must be a
23364 member-declaration. */
23365 cp_parser_member_declaration (parser);
23366 }
23367 }
23368 }
23369
23370 /* Parse a member-declaration.
23371
23372 member-declaration:
23373 decl-specifier-seq [opt] member-declarator-list [opt] ;
23374 function-definition ; [opt]
23375 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23376 using-declaration
23377 template-declaration
23378 alias-declaration
23379
23380 member-declarator-list:
23381 member-declarator
23382 member-declarator-list , member-declarator
23383
23384 member-declarator:
23385 declarator pure-specifier [opt]
23386 declarator constant-initializer [opt]
23387 identifier [opt] : constant-expression
23388
23389 GNU Extensions:
23390
23391 member-declaration:
23392 __extension__ member-declaration
23393
23394 member-declarator:
23395 declarator attributes [opt] pure-specifier [opt]
23396 declarator attributes [opt] constant-initializer [opt]
23397 identifier [opt] attributes [opt] : constant-expression
23398
23399 C++0x Extensions:
23400
23401 member-declaration:
23402 static_assert-declaration */
23403
23404 static void
23405 cp_parser_member_declaration (cp_parser* parser)
23406 {
23407 cp_decl_specifier_seq decl_specifiers;
23408 tree prefix_attributes;
23409 tree decl;
23410 int declares_class_or_enum;
23411 bool friend_p;
23412 cp_token *token = NULL;
23413 cp_token *decl_spec_token_start = NULL;
23414 cp_token *initializer_token_start = NULL;
23415 int saved_pedantic;
23416 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23417
23418 /* Check for the `__extension__' keyword. */
23419 if (cp_parser_extension_opt (parser, &saved_pedantic))
23420 {
23421 /* Recurse. */
23422 cp_parser_member_declaration (parser);
23423 /* Restore the old value of the PEDANTIC flag. */
23424 pedantic = saved_pedantic;
23425
23426 return;
23427 }
23428
23429 /* Check for a template-declaration. */
23430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23431 {
23432 /* An explicit specialization here is an error condition, and we
23433 expect the specialization handler to detect and report this. */
23434 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23435 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23436 cp_parser_explicit_specialization (parser);
23437 else
23438 cp_parser_template_declaration (parser, /*member_p=*/true);
23439
23440 return;
23441 }
23442 /* Check for a template introduction. */
23443 else if (cp_parser_template_declaration_after_export (parser, true))
23444 return;
23445
23446 /* Check for a using-declaration. */
23447 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23448 {
23449 if (cxx_dialect < cxx11)
23450 {
23451 /* Parse the using-declaration. */
23452 cp_parser_using_declaration (parser,
23453 /*access_declaration_p=*/false);
23454 return;
23455 }
23456 else
23457 {
23458 tree decl;
23459 bool alias_decl_expected;
23460 cp_parser_parse_tentatively (parser);
23461 decl = cp_parser_alias_declaration (parser);
23462 /* Note that if we actually see the '=' token after the
23463 identifier, cp_parser_alias_declaration commits the
23464 tentative parse. In that case, we really expect an
23465 alias-declaration. Otherwise, we expect a using
23466 declaration. */
23467 alias_decl_expected =
23468 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23469 cp_parser_parse_definitely (parser);
23470
23471 if (alias_decl_expected)
23472 finish_member_declaration (decl);
23473 else
23474 cp_parser_using_declaration (parser,
23475 /*access_declaration_p=*/false);
23476 return;
23477 }
23478 }
23479
23480 /* Check for @defs. */
23481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23482 {
23483 tree ivar, member;
23484 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23485 ivar = ivar_chains;
23486 while (ivar)
23487 {
23488 member = ivar;
23489 ivar = TREE_CHAIN (member);
23490 TREE_CHAIN (member) = NULL_TREE;
23491 finish_member_declaration (member);
23492 }
23493 return;
23494 }
23495
23496 /* If the next token is `static_assert' we have a static assertion. */
23497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23498 {
23499 cp_parser_static_assert (parser, /*member_p=*/true);
23500 return;
23501 }
23502
23503 parser->colon_corrects_to_scope_p = false;
23504
23505 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23506 goto out;
23507
23508 /* Parse the decl-specifier-seq. */
23509 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23510 cp_parser_decl_specifier_seq (parser,
23511 CP_PARSER_FLAGS_OPTIONAL,
23512 &decl_specifiers,
23513 &declares_class_or_enum);
23514 /* Check for an invalid type-name. */
23515 if (!decl_specifiers.any_type_specifiers_p
23516 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23517 goto out;
23518 /* If there is no declarator, then the decl-specifier-seq should
23519 specify a type. */
23520 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23521 {
23522 /* If there was no decl-specifier-seq, and the next token is a
23523 `;', then we have something like:
23524
23525 struct S { ; };
23526
23527 [class.mem]
23528
23529 Each member-declaration shall declare at least one member
23530 name of the class. */
23531 if (!decl_specifiers.any_specifiers_p)
23532 {
23533 cp_token *token = cp_lexer_peek_token (parser->lexer);
23534 if (!in_system_header_at (token->location))
23535 {
23536 gcc_rich_location richloc (token->location);
23537 richloc.add_fixit_remove ();
23538 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23539 }
23540 }
23541 else
23542 {
23543 tree type;
23544
23545 /* See if this declaration is a friend. */
23546 friend_p = cp_parser_friend_p (&decl_specifiers);
23547 /* If there were decl-specifiers, check to see if there was
23548 a class-declaration. */
23549 type = check_tag_decl (&decl_specifiers,
23550 /*explicit_type_instantiation_p=*/false);
23551 /* Nested classes have already been added to the class, but
23552 a `friend' needs to be explicitly registered. */
23553 if (friend_p)
23554 {
23555 /* If the `friend' keyword was present, the friend must
23556 be introduced with a class-key. */
23557 if (!declares_class_or_enum && cxx_dialect < cxx11)
23558 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23559 "in C++03 a class-key must be used "
23560 "when declaring a friend");
23561 /* In this case:
23562
23563 template <typename T> struct A {
23564 friend struct A<T>::B;
23565 };
23566
23567 A<T>::B will be represented by a TYPENAME_TYPE, and
23568 therefore not recognized by check_tag_decl. */
23569 if (!type)
23570 {
23571 type = decl_specifiers.type;
23572 if (type && TREE_CODE (type) == TYPE_DECL)
23573 type = TREE_TYPE (type);
23574 }
23575 if (!type || !TYPE_P (type))
23576 error_at (decl_spec_token_start->location,
23577 "friend declaration does not name a class or "
23578 "function");
23579 else
23580 make_friend_class (current_class_type, type,
23581 /*complain=*/true);
23582 }
23583 /* If there is no TYPE, an error message will already have
23584 been issued. */
23585 else if (!type || type == error_mark_node)
23586 ;
23587 /* An anonymous aggregate has to be handled specially; such
23588 a declaration really declares a data member (with a
23589 particular type), as opposed to a nested class. */
23590 else if (ANON_AGGR_TYPE_P (type))
23591 {
23592 /* C++11 9.5/6. */
23593 if (decl_specifiers.storage_class != sc_none)
23594 error_at (decl_spec_token_start->location,
23595 "a storage class on an anonymous aggregate "
23596 "in class scope is not allowed");
23597
23598 /* Remove constructors and such from TYPE, now that we
23599 know it is an anonymous aggregate. */
23600 fixup_anonymous_aggr (type);
23601 /* And make the corresponding data member. */
23602 decl = build_decl (decl_spec_token_start->location,
23603 FIELD_DECL, NULL_TREE, type);
23604 /* Add it to the class. */
23605 finish_member_declaration (decl);
23606 }
23607 else
23608 cp_parser_check_access_in_redeclaration
23609 (TYPE_NAME (type),
23610 decl_spec_token_start->location);
23611 }
23612 }
23613 else
23614 {
23615 bool assume_semicolon = false;
23616
23617 /* Clear attributes from the decl_specifiers but keep them
23618 around as prefix attributes that apply them to the entity
23619 being declared. */
23620 prefix_attributes = decl_specifiers.attributes;
23621 decl_specifiers.attributes = NULL_TREE;
23622
23623 /* See if these declarations will be friends. */
23624 friend_p = cp_parser_friend_p (&decl_specifiers);
23625
23626 /* Keep going until we hit the `;' at the end of the
23627 declaration. */
23628 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23629 {
23630 tree attributes = NULL_TREE;
23631 tree first_attribute;
23632 tree initializer;
23633 bool named_bitfld = false;
23634
23635 /* Peek at the next token. */
23636 token = cp_lexer_peek_token (parser->lexer);
23637
23638 /* The following code wants to know early if it is a bit-field
23639 or some other declaration. Attributes can appear before
23640 the `:' token. Skip over them without consuming any tokens
23641 to peek if they are followed by `:'. */
23642 if (cp_next_tokens_can_be_attribute_p (parser)
23643 || (token->type == CPP_NAME
23644 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23645 && (named_bitfld = true)))
23646 {
23647 size_t n
23648 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23649 token = cp_lexer_peek_nth_token (parser->lexer, n);
23650 }
23651
23652 /* Check for a bitfield declaration. */
23653 if (token->type == CPP_COLON
23654 || (token->type == CPP_NAME
23655 && token == cp_lexer_peek_token (parser->lexer)
23656 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23657 && (named_bitfld = true)))
23658 {
23659 tree identifier;
23660 tree width;
23661 tree late_attributes = NULL_TREE;
23662
23663 if (named_bitfld)
23664 identifier = cp_parser_identifier (parser);
23665 else
23666 identifier = NULL_TREE;
23667
23668 /* Look for attributes that apply to the bitfield. */
23669 attributes = cp_parser_attributes_opt (parser);
23670
23671 /* Consume the `:' token. */
23672 cp_lexer_consume_token (parser->lexer);
23673
23674 /* Get the width of the bitfield. */
23675 width = cp_parser_constant_expression (parser, false, NULL,
23676 cxx_dialect >= cxx11);
23677
23678 /* In C++2A and as extension for C++11 and above we allow
23679 default member initializers for bit-fields. */
23680 initializer = NULL_TREE;
23681 if (cxx_dialect >= cxx11
23682 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23683 || cp_lexer_next_token_is (parser->lexer,
23684 CPP_OPEN_BRACE)))
23685 {
23686 location_t loc
23687 = cp_lexer_peek_token (parser->lexer)->location;
23688 if (cxx_dialect < cxx2a
23689 && !in_system_header_at (loc)
23690 && identifier != NULL_TREE)
23691 pedwarn (loc, 0,
23692 "default member initializers for bit-fields "
23693 "only available with -std=c++2a or "
23694 "-std=gnu++2a");
23695
23696 initializer = cp_parser_save_nsdmi (parser);
23697 if (identifier == NULL_TREE)
23698 {
23699 error_at (loc, "default member initializer for "
23700 "unnamed bit-field");
23701 initializer = NULL_TREE;
23702 }
23703 }
23704 else
23705 {
23706 /* Look for attributes that apply to the bitfield after
23707 the `:' token and width. This is where GCC used to
23708 parse attributes in the past, pedwarn if there is
23709 a std attribute. */
23710 if (cp_next_tokens_can_be_std_attribute_p (parser))
23711 pedwarn (input_location, OPT_Wpedantic,
23712 "ISO C++ allows bit-field attributes only "
23713 "before the %<:%> token");
23714
23715 late_attributes = cp_parser_attributes_opt (parser);
23716 }
23717
23718 attributes = attr_chainon (attributes, late_attributes);
23719
23720 /* Remember which attributes are prefix attributes and
23721 which are not. */
23722 first_attribute = attributes;
23723 /* Combine the attributes. */
23724 attributes = attr_chainon (prefix_attributes, attributes);
23725
23726 /* Create the bitfield declaration. */
23727 decl = grokbitfield (identifier
23728 ? make_id_declarator (NULL_TREE,
23729 identifier,
23730 sfk_none)
23731 : NULL,
23732 &decl_specifiers,
23733 width, initializer,
23734 attributes);
23735 }
23736 else
23737 {
23738 cp_declarator *declarator;
23739 tree asm_specification;
23740 int ctor_dtor_or_conv_p;
23741
23742 /* Parse the declarator. */
23743 declarator
23744 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23745 &ctor_dtor_or_conv_p,
23746 /*parenthesized_p=*/NULL,
23747 /*member_p=*/true,
23748 friend_p);
23749
23750 /* If something went wrong parsing the declarator, make sure
23751 that we at least consume some tokens. */
23752 if (declarator == cp_error_declarator)
23753 {
23754 /* Skip to the end of the statement. */
23755 cp_parser_skip_to_end_of_statement (parser);
23756 /* If the next token is not a semicolon, that is
23757 probably because we just skipped over the body of
23758 a function. So, we consume a semicolon if
23759 present, but do not issue an error message if it
23760 is not present. */
23761 if (cp_lexer_next_token_is (parser->lexer,
23762 CPP_SEMICOLON))
23763 cp_lexer_consume_token (parser->lexer);
23764 goto out;
23765 }
23766
23767 if (declares_class_or_enum & 2)
23768 cp_parser_check_for_definition_in_return_type
23769 (declarator, decl_specifiers.type,
23770 decl_specifiers.locations[ds_type_spec]);
23771
23772 /* Look for an asm-specification. */
23773 asm_specification = cp_parser_asm_specification_opt (parser);
23774 /* Look for attributes that apply to the declaration. */
23775 attributes = cp_parser_attributes_opt (parser);
23776 /* Remember which attributes are prefix attributes and
23777 which are not. */
23778 first_attribute = attributes;
23779 /* Combine the attributes. */
23780 attributes = attr_chainon (prefix_attributes, attributes);
23781
23782 /* If it's an `=', then we have a constant-initializer or a
23783 pure-specifier. It is not correct to parse the
23784 initializer before registering the member declaration
23785 since the member declaration should be in scope while
23786 its initializer is processed. However, the rest of the
23787 front end does not yet provide an interface that allows
23788 us to handle this correctly. */
23789 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23790 {
23791 /* In [class.mem]:
23792
23793 A pure-specifier shall be used only in the declaration of
23794 a virtual function.
23795
23796 A member-declarator can contain a constant-initializer
23797 only if it declares a static member of integral or
23798 enumeration type.
23799
23800 Therefore, if the DECLARATOR is for a function, we look
23801 for a pure-specifier; otherwise, we look for a
23802 constant-initializer. When we call `grokfield', it will
23803 perform more stringent semantics checks. */
23804 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23805 if (function_declarator_p (declarator)
23806 || (decl_specifiers.type
23807 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23808 && declarator->kind == cdk_id
23809 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23810 == FUNCTION_TYPE)))
23811 initializer = cp_parser_pure_specifier (parser);
23812 else if (decl_specifiers.storage_class != sc_static)
23813 initializer = cp_parser_save_nsdmi (parser);
23814 else if (cxx_dialect >= cxx11)
23815 {
23816 bool nonconst;
23817 /* Don't require a constant rvalue in C++11, since we
23818 might want a reference constant. We'll enforce
23819 constancy later. */
23820 cp_lexer_consume_token (parser->lexer);
23821 /* Parse the initializer. */
23822 initializer = cp_parser_initializer_clause (parser,
23823 &nonconst);
23824 }
23825 else
23826 /* Parse the initializer. */
23827 initializer = cp_parser_constant_initializer (parser);
23828 }
23829 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23830 && !function_declarator_p (declarator))
23831 {
23832 bool x;
23833 if (decl_specifiers.storage_class != sc_static)
23834 initializer = cp_parser_save_nsdmi (parser);
23835 else
23836 initializer = cp_parser_initializer (parser, &x, &x);
23837 }
23838 /* Otherwise, there is no initializer. */
23839 else
23840 initializer = NULL_TREE;
23841
23842 /* See if we are probably looking at a function
23843 definition. We are certainly not looking at a
23844 member-declarator. Calling `grokfield' has
23845 side-effects, so we must not do it unless we are sure
23846 that we are looking at a member-declarator. */
23847 if (cp_parser_token_starts_function_definition_p
23848 (cp_lexer_peek_token (parser->lexer)))
23849 {
23850 /* The grammar does not allow a pure-specifier to be
23851 used when a member function is defined. (It is
23852 possible that this fact is an oversight in the
23853 standard, since a pure function may be defined
23854 outside of the class-specifier. */
23855 if (initializer && initializer_token_start)
23856 error_at (initializer_token_start->location,
23857 "pure-specifier on function-definition");
23858 decl = cp_parser_save_member_function_body (parser,
23859 &decl_specifiers,
23860 declarator,
23861 attributes);
23862 if (parser->fully_implicit_function_template_p)
23863 decl = finish_fully_implicit_template (parser, decl);
23864 /* If the member was not a friend, declare it here. */
23865 if (!friend_p)
23866 finish_member_declaration (decl);
23867 /* Peek at the next token. */
23868 token = cp_lexer_peek_token (parser->lexer);
23869 /* If the next token is a semicolon, consume it. */
23870 if (token->type == CPP_SEMICOLON)
23871 {
23872 location_t semicolon_loc
23873 = cp_lexer_consume_token (parser->lexer)->location;
23874 gcc_rich_location richloc (semicolon_loc);
23875 richloc.add_fixit_remove ();
23876 warning_at (&richloc, OPT_Wextra_semi,
23877 "extra %<;%> after in-class "
23878 "function definition");
23879 }
23880 goto out;
23881 }
23882 else
23883 if (declarator->kind == cdk_function)
23884 declarator->id_loc = token->location;
23885 /* Create the declaration. */
23886 decl = grokfield (declarator, &decl_specifiers,
23887 initializer, /*init_const_expr_p=*/true,
23888 asm_specification, attributes);
23889 if (parser->fully_implicit_function_template_p)
23890 {
23891 if (friend_p)
23892 finish_fully_implicit_template (parser, 0);
23893 else
23894 decl = finish_fully_implicit_template (parser, decl);
23895 }
23896 }
23897
23898 cp_finalize_omp_declare_simd (parser, decl);
23899 cp_finalize_oacc_routine (parser, decl, false);
23900
23901 /* Reset PREFIX_ATTRIBUTES. */
23902 if (attributes != error_mark_node)
23903 {
23904 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23905 attributes = TREE_CHAIN (attributes);
23906 if (attributes)
23907 TREE_CHAIN (attributes) = NULL_TREE;
23908 }
23909
23910 /* If there is any qualification still in effect, clear it
23911 now; we will be starting fresh with the next declarator. */
23912 parser->scope = NULL_TREE;
23913 parser->qualifying_scope = NULL_TREE;
23914 parser->object_scope = NULL_TREE;
23915 /* If it's a `,', then there are more declarators. */
23916 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23917 {
23918 cp_lexer_consume_token (parser->lexer);
23919 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23920 {
23921 cp_token *token = cp_lexer_previous_token (parser->lexer);
23922 gcc_rich_location richloc (token->location);
23923 richloc.add_fixit_remove ();
23924 error_at (&richloc, "stray %<,%> at end of "
23925 "member declaration");
23926 }
23927 }
23928 /* If the next token isn't a `;', then we have a parse error. */
23929 else if (cp_lexer_next_token_is_not (parser->lexer,
23930 CPP_SEMICOLON))
23931 {
23932 /* The next token might be a ways away from where the
23933 actual semicolon is missing. Find the previous token
23934 and use that for our error position. */
23935 cp_token *token = cp_lexer_previous_token (parser->lexer);
23936 gcc_rich_location richloc (token->location);
23937 richloc.add_fixit_insert_after (";");
23938 error_at (&richloc, "expected %<;%> at end of "
23939 "member declaration");
23940
23941 /* Assume that the user meant to provide a semicolon. If
23942 we were to cp_parser_skip_to_end_of_statement, we might
23943 skip to a semicolon inside a member function definition
23944 and issue nonsensical error messages. */
23945 assume_semicolon = true;
23946 }
23947
23948 if (decl)
23949 {
23950 /* Add DECL to the list of members. */
23951 if (!friend_p
23952 /* Explicitly include, eg, NSDMIs, for better error
23953 recovery (c++/58650). */
23954 || !DECL_DECLARES_FUNCTION_P (decl))
23955 finish_member_declaration (decl);
23956
23957 if (TREE_CODE (decl) == FUNCTION_DECL)
23958 cp_parser_save_default_args (parser, decl);
23959 else if (TREE_CODE (decl) == FIELD_DECL
23960 && DECL_INITIAL (decl))
23961 /* Add DECL to the queue of NSDMI to be parsed later. */
23962 vec_safe_push (unparsed_nsdmis, decl);
23963 }
23964
23965 if (assume_semicolon)
23966 goto out;
23967 }
23968 }
23969
23970 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23971 out:
23972 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23973 }
23974
23975 /* Parse a pure-specifier.
23976
23977 pure-specifier:
23978 = 0
23979
23980 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23981 Otherwise, ERROR_MARK_NODE is returned. */
23982
23983 static tree
23984 cp_parser_pure_specifier (cp_parser* parser)
23985 {
23986 cp_token *token;
23987
23988 /* Look for the `=' token. */
23989 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23990 return error_mark_node;
23991 /* Look for the `0' token. */
23992 token = cp_lexer_peek_token (parser->lexer);
23993
23994 if (token->type == CPP_EOF
23995 || token->type == CPP_PRAGMA_EOL)
23996 return error_mark_node;
23997
23998 cp_lexer_consume_token (parser->lexer);
23999
24000 /* Accept = default or = delete in c++0x mode. */
24001 if (token->keyword == RID_DEFAULT
24002 || token->keyword == RID_DELETE)
24003 {
24004 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24005 return token->u.value;
24006 }
24007
24008 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24009 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24010 {
24011 cp_parser_error (parser,
24012 "invalid pure specifier (only %<= 0%> is allowed)");
24013 cp_parser_skip_to_end_of_statement (parser);
24014 return error_mark_node;
24015 }
24016 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24017 {
24018 error_at (token->location, "templates may not be %<virtual%>");
24019 return error_mark_node;
24020 }
24021
24022 return integer_zero_node;
24023 }
24024
24025 /* Parse a constant-initializer.
24026
24027 constant-initializer:
24028 = constant-expression
24029
24030 Returns a representation of the constant-expression. */
24031
24032 static tree
24033 cp_parser_constant_initializer (cp_parser* parser)
24034 {
24035 /* Look for the `=' token. */
24036 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24037 return error_mark_node;
24038
24039 /* It is invalid to write:
24040
24041 struct S { static const int i = { 7 }; };
24042
24043 */
24044 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24045 {
24046 cp_parser_error (parser,
24047 "a brace-enclosed initializer is not allowed here");
24048 /* Consume the opening brace. */
24049 matching_braces braces;
24050 braces.consume_open (parser);
24051 /* Skip the initializer. */
24052 cp_parser_skip_to_closing_brace (parser);
24053 /* Look for the trailing `}'. */
24054 braces.require_close (parser);
24055
24056 return error_mark_node;
24057 }
24058
24059 return cp_parser_constant_expression (parser);
24060 }
24061
24062 /* Derived classes [gram.class.derived] */
24063
24064 /* Parse a base-clause.
24065
24066 base-clause:
24067 : base-specifier-list
24068
24069 base-specifier-list:
24070 base-specifier ... [opt]
24071 base-specifier-list , base-specifier ... [opt]
24072
24073 Returns a TREE_LIST representing the base-classes, in the order in
24074 which they were declared. The representation of each node is as
24075 described by cp_parser_base_specifier.
24076
24077 In the case that no bases are specified, this function will return
24078 NULL_TREE, not ERROR_MARK_NODE. */
24079
24080 static tree
24081 cp_parser_base_clause (cp_parser* parser)
24082 {
24083 tree bases = NULL_TREE;
24084
24085 /* Look for the `:' that begins the list. */
24086 cp_parser_require (parser, CPP_COLON, RT_COLON);
24087
24088 /* Scan the base-specifier-list. */
24089 while (true)
24090 {
24091 cp_token *token;
24092 tree base;
24093 bool pack_expansion_p = false;
24094
24095 /* Look for the base-specifier. */
24096 base = cp_parser_base_specifier (parser);
24097 /* Look for the (optional) ellipsis. */
24098 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24099 {
24100 /* Consume the `...'. */
24101 cp_lexer_consume_token (parser->lexer);
24102
24103 pack_expansion_p = true;
24104 }
24105
24106 /* Add BASE to the front of the list. */
24107 if (base && base != error_mark_node)
24108 {
24109 if (pack_expansion_p)
24110 /* Make this a pack expansion type. */
24111 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24112
24113 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24114 {
24115 TREE_CHAIN (base) = bases;
24116 bases = base;
24117 }
24118 }
24119 /* Peek at the next token. */
24120 token = cp_lexer_peek_token (parser->lexer);
24121 /* If it's not a comma, then the list is complete. */
24122 if (token->type != CPP_COMMA)
24123 break;
24124 /* Consume the `,'. */
24125 cp_lexer_consume_token (parser->lexer);
24126 }
24127
24128 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24129 base class had a qualified name. However, the next name that
24130 appears is certainly not qualified. */
24131 parser->scope = NULL_TREE;
24132 parser->qualifying_scope = NULL_TREE;
24133 parser->object_scope = NULL_TREE;
24134
24135 return nreverse (bases);
24136 }
24137
24138 /* Parse a base-specifier.
24139
24140 base-specifier:
24141 :: [opt] nested-name-specifier [opt] class-name
24142 virtual access-specifier [opt] :: [opt] nested-name-specifier
24143 [opt] class-name
24144 access-specifier virtual [opt] :: [opt] nested-name-specifier
24145 [opt] class-name
24146
24147 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24148 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24149 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24150 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24151
24152 static tree
24153 cp_parser_base_specifier (cp_parser* parser)
24154 {
24155 cp_token *token;
24156 bool done = false;
24157 bool virtual_p = false;
24158 bool duplicate_virtual_error_issued_p = false;
24159 bool duplicate_access_error_issued_p = false;
24160 bool class_scope_p, template_p;
24161 tree access = access_default_node;
24162 tree type;
24163
24164 /* Process the optional `virtual' and `access-specifier'. */
24165 while (!done)
24166 {
24167 /* Peek at the next token. */
24168 token = cp_lexer_peek_token (parser->lexer);
24169 /* Process `virtual'. */
24170 switch (token->keyword)
24171 {
24172 case RID_VIRTUAL:
24173 /* If `virtual' appears more than once, issue an error. */
24174 if (virtual_p && !duplicate_virtual_error_issued_p)
24175 {
24176 cp_parser_error (parser,
24177 "%<virtual%> specified more than once in base-specifier");
24178 duplicate_virtual_error_issued_p = true;
24179 }
24180
24181 virtual_p = true;
24182
24183 /* Consume the `virtual' token. */
24184 cp_lexer_consume_token (parser->lexer);
24185
24186 break;
24187
24188 case RID_PUBLIC:
24189 case RID_PROTECTED:
24190 case RID_PRIVATE:
24191 /* If more than one access specifier appears, issue an
24192 error. */
24193 if (access != access_default_node
24194 && !duplicate_access_error_issued_p)
24195 {
24196 cp_parser_error (parser,
24197 "more than one access specifier in base-specifier");
24198 duplicate_access_error_issued_p = true;
24199 }
24200
24201 access = ridpointers[(int) token->keyword];
24202
24203 /* Consume the access-specifier. */
24204 cp_lexer_consume_token (parser->lexer);
24205
24206 break;
24207
24208 default:
24209 done = true;
24210 break;
24211 }
24212 }
24213 /* It is not uncommon to see programs mechanically, erroneously, use
24214 the 'typename' keyword to denote (dependent) qualified types
24215 as base classes. */
24216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24217 {
24218 token = cp_lexer_peek_token (parser->lexer);
24219 if (!processing_template_decl)
24220 error_at (token->location,
24221 "keyword %<typename%> not allowed outside of templates");
24222 else
24223 error_at (token->location,
24224 "keyword %<typename%> not allowed in this context "
24225 "(the base class is implicitly a type)");
24226 cp_lexer_consume_token (parser->lexer);
24227 }
24228
24229 /* Look for the optional `::' operator. */
24230 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24231 /* Look for the nested-name-specifier. The simplest way to
24232 implement:
24233
24234 [temp.res]
24235
24236 The keyword `typename' is not permitted in a base-specifier or
24237 mem-initializer; in these contexts a qualified name that
24238 depends on a template-parameter is implicitly assumed to be a
24239 type name.
24240
24241 is to pretend that we have seen the `typename' keyword at this
24242 point. */
24243 cp_parser_nested_name_specifier_opt (parser,
24244 /*typename_keyword_p=*/true,
24245 /*check_dependency_p=*/true,
24246 /*type_p=*/true,
24247 /*is_declaration=*/true);
24248 /* If the base class is given by a qualified name, assume that names
24249 we see are type names or templates, as appropriate. */
24250 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24251 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24252
24253 if (!parser->scope
24254 && cp_lexer_next_token_is_decltype (parser->lexer))
24255 /* DR 950 allows decltype as a base-specifier. */
24256 type = cp_parser_decltype (parser);
24257 else
24258 {
24259 /* Otherwise, look for the class-name. */
24260 type = cp_parser_class_name (parser,
24261 class_scope_p,
24262 template_p,
24263 typename_type,
24264 /*check_dependency_p=*/true,
24265 /*class_head_p=*/false,
24266 /*is_declaration=*/true);
24267 type = TREE_TYPE (type);
24268 }
24269
24270 if (type == error_mark_node)
24271 return error_mark_node;
24272
24273 return finish_base_specifier (type, access, virtual_p);
24274 }
24275
24276 /* Exception handling [gram.exception] */
24277
24278 /* Parse an (optional) noexcept-specification.
24279
24280 noexcept-specification:
24281 noexcept ( constant-expression ) [opt]
24282
24283 If no noexcept-specification is present, returns NULL_TREE.
24284 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24285 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24286 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24287 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24288 in which case a boolean condition is returned instead. */
24289
24290 static tree
24291 cp_parser_noexcept_specification_opt (cp_parser* parser,
24292 bool require_constexpr,
24293 bool* consumed_expr,
24294 bool return_cond)
24295 {
24296 cp_token *token;
24297 const char *saved_message;
24298
24299 /* Peek at the next token. */
24300 token = cp_lexer_peek_token (parser->lexer);
24301
24302 /* Is it a noexcept-specification? */
24303 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24304 {
24305 tree expr;
24306 cp_lexer_consume_token (parser->lexer);
24307
24308 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24309 {
24310 matching_parens parens;
24311 parens.consume_open (parser);
24312
24313 if (require_constexpr)
24314 {
24315 /* Types may not be defined in an exception-specification. */
24316 saved_message = parser->type_definition_forbidden_message;
24317 parser->type_definition_forbidden_message
24318 = G_("types may not be defined in an exception-specification");
24319
24320 expr = cp_parser_constant_expression (parser);
24321
24322 /* Restore the saved message. */
24323 parser->type_definition_forbidden_message = saved_message;
24324 }
24325 else
24326 {
24327 expr = cp_parser_expression (parser);
24328 *consumed_expr = true;
24329 }
24330
24331 parens.require_close (parser);
24332 }
24333 else
24334 {
24335 expr = boolean_true_node;
24336 if (!require_constexpr)
24337 *consumed_expr = false;
24338 }
24339
24340 /* We cannot build a noexcept-spec right away because this will check
24341 that expr is a constexpr. */
24342 if (!return_cond)
24343 return build_noexcept_spec (expr, tf_warning_or_error);
24344 else
24345 return expr;
24346 }
24347 else
24348 return NULL_TREE;
24349 }
24350
24351 /* Parse an (optional) exception-specification.
24352
24353 exception-specification:
24354 throw ( type-id-list [opt] )
24355
24356 Returns a TREE_LIST representing the exception-specification. The
24357 TREE_VALUE of each node is a type. */
24358
24359 static tree
24360 cp_parser_exception_specification_opt (cp_parser* parser)
24361 {
24362 cp_token *token;
24363 tree type_id_list;
24364 const char *saved_message;
24365
24366 /* Peek at the next token. */
24367 token = cp_lexer_peek_token (parser->lexer);
24368
24369 /* Is it a noexcept-specification? */
24370 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24371 false);
24372 if (type_id_list != NULL_TREE)
24373 return type_id_list;
24374
24375 /* If it's not `throw', then there's no exception-specification. */
24376 if (!cp_parser_is_keyword (token, RID_THROW))
24377 return NULL_TREE;
24378
24379 location_t loc = token->location;
24380
24381 /* Consume the `throw'. */
24382 cp_lexer_consume_token (parser->lexer);
24383
24384 /* Look for the `('. */
24385 matching_parens parens;
24386 parens.require_open (parser);
24387
24388 /* Peek at the next token. */
24389 token = cp_lexer_peek_token (parser->lexer);
24390 /* If it's not a `)', then there is a type-id-list. */
24391 if (token->type != CPP_CLOSE_PAREN)
24392 {
24393 /* Types may not be defined in an exception-specification. */
24394 saved_message = parser->type_definition_forbidden_message;
24395 parser->type_definition_forbidden_message
24396 = G_("types may not be defined in an exception-specification");
24397 /* Parse the type-id-list. */
24398 type_id_list = cp_parser_type_id_list (parser);
24399 /* Restore the saved message. */
24400 parser->type_definition_forbidden_message = saved_message;
24401
24402 if (cxx_dialect >= cxx17)
24403 {
24404 error_at (loc, "ISO C++17 does not allow dynamic exception "
24405 "specifications");
24406 type_id_list = NULL_TREE;
24407 }
24408 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24409 warning_at (loc, OPT_Wdeprecated,
24410 "dynamic exception specifications are deprecated in "
24411 "C++11");
24412 }
24413 /* In C++17, throw() is equivalent to noexcept (true). throw()
24414 is deprecated in C++11 and above as well, but is still widely used,
24415 so don't warn about it yet. */
24416 else if (cxx_dialect >= cxx17)
24417 type_id_list = noexcept_true_spec;
24418 else
24419 type_id_list = empty_except_spec;
24420
24421 /* Look for the `)'. */
24422 parens.require_close (parser);
24423
24424 return type_id_list;
24425 }
24426
24427 /* Parse an (optional) type-id-list.
24428
24429 type-id-list:
24430 type-id ... [opt]
24431 type-id-list , type-id ... [opt]
24432
24433 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24434 in the order that the types were presented. */
24435
24436 static tree
24437 cp_parser_type_id_list (cp_parser* parser)
24438 {
24439 tree types = NULL_TREE;
24440
24441 while (true)
24442 {
24443 cp_token *token;
24444 tree type;
24445
24446 token = cp_lexer_peek_token (parser->lexer);
24447
24448 /* Get the next type-id. */
24449 type = cp_parser_type_id (parser);
24450 /* Check for invalid 'auto'. */
24451 if (flag_concepts && type_uses_auto (type))
24452 {
24453 error_at (token->location,
24454 "invalid use of %<auto%> in exception-specification");
24455 type = error_mark_node;
24456 }
24457 /* Parse the optional ellipsis. */
24458 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24459 {
24460 /* Consume the `...'. */
24461 cp_lexer_consume_token (parser->lexer);
24462
24463 /* Turn the type into a pack expansion expression. */
24464 type = make_pack_expansion (type);
24465 }
24466 /* Add it to the list. */
24467 types = add_exception_specifier (types, type, /*complain=*/1);
24468 /* Peek at the next token. */
24469 token = cp_lexer_peek_token (parser->lexer);
24470 /* If it is not a `,', we are done. */
24471 if (token->type != CPP_COMMA)
24472 break;
24473 /* Consume the `,'. */
24474 cp_lexer_consume_token (parser->lexer);
24475 }
24476
24477 return nreverse (types);
24478 }
24479
24480 /* Parse a try-block.
24481
24482 try-block:
24483 try compound-statement handler-seq */
24484
24485 static tree
24486 cp_parser_try_block (cp_parser* parser)
24487 {
24488 tree try_block;
24489
24490 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24491 if (parser->in_function_body
24492 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24493 error ("%<try%> in %<constexpr%> function");
24494
24495 try_block = begin_try_block ();
24496 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24497 finish_try_block (try_block);
24498 cp_parser_handler_seq (parser);
24499 finish_handler_sequence (try_block);
24500
24501 return try_block;
24502 }
24503
24504 /* Parse a function-try-block.
24505
24506 function-try-block:
24507 try ctor-initializer [opt] function-body handler-seq */
24508
24509 static void
24510 cp_parser_function_try_block (cp_parser* parser)
24511 {
24512 tree compound_stmt;
24513 tree try_block;
24514
24515 /* Look for the `try' keyword. */
24516 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24517 return;
24518 /* Let the rest of the front end know where we are. */
24519 try_block = begin_function_try_block (&compound_stmt);
24520 /* Parse the function-body. */
24521 cp_parser_ctor_initializer_opt_and_function_body
24522 (parser, /*in_function_try_block=*/true);
24523 /* We're done with the `try' part. */
24524 finish_function_try_block (try_block);
24525 /* Parse the handlers. */
24526 cp_parser_handler_seq (parser);
24527 /* We're done with the handlers. */
24528 finish_function_handler_sequence (try_block, compound_stmt);
24529 }
24530
24531 /* Parse a handler-seq.
24532
24533 handler-seq:
24534 handler handler-seq [opt] */
24535
24536 static void
24537 cp_parser_handler_seq (cp_parser* parser)
24538 {
24539 while (true)
24540 {
24541 cp_token *token;
24542
24543 /* Parse the handler. */
24544 cp_parser_handler (parser);
24545 /* Peek at the next token. */
24546 token = cp_lexer_peek_token (parser->lexer);
24547 /* If it's not `catch' then there are no more handlers. */
24548 if (!cp_parser_is_keyword (token, RID_CATCH))
24549 break;
24550 }
24551 }
24552
24553 /* Parse a handler.
24554
24555 handler:
24556 catch ( exception-declaration ) compound-statement */
24557
24558 static void
24559 cp_parser_handler (cp_parser* parser)
24560 {
24561 tree handler;
24562 tree declaration;
24563
24564 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24565 handler = begin_handler ();
24566 matching_parens parens;
24567 parens.require_open (parser);
24568 declaration = cp_parser_exception_declaration (parser);
24569 finish_handler_parms (declaration, handler);
24570 parens.require_close (parser);
24571 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24572 finish_handler (handler);
24573 }
24574
24575 /* Parse an exception-declaration.
24576
24577 exception-declaration:
24578 type-specifier-seq declarator
24579 type-specifier-seq abstract-declarator
24580 type-specifier-seq
24581 ...
24582
24583 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24584 ellipsis variant is used. */
24585
24586 static tree
24587 cp_parser_exception_declaration (cp_parser* parser)
24588 {
24589 cp_decl_specifier_seq type_specifiers;
24590 cp_declarator *declarator;
24591 const char *saved_message;
24592
24593 /* If it's an ellipsis, it's easy to handle. */
24594 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24595 {
24596 /* Consume the `...' token. */
24597 cp_lexer_consume_token (parser->lexer);
24598 return NULL_TREE;
24599 }
24600
24601 /* Types may not be defined in exception-declarations. */
24602 saved_message = parser->type_definition_forbidden_message;
24603 parser->type_definition_forbidden_message
24604 = G_("types may not be defined in exception-declarations");
24605
24606 /* Parse the type-specifier-seq. */
24607 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24608 /*is_trailing_return=*/false,
24609 &type_specifiers);
24610 /* If it's a `)', then there is no declarator. */
24611 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24612 declarator = NULL;
24613 else
24614 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24615 /*ctor_dtor_or_conv_p=*/NULL,
24616 /*parenthesized_p=*/NULL,
24617 /*member_p=*/false,
24618 /*friend_p=*/false);
24619
24620 /* Restore the saved message. */
24621 parser->type_definition_forbidden_message = saved_message;
24622
24623 if (!type_specifiers.any_specifiers_p)
24624 return error_mark_node;
24625
24626 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24627 }
24628
24629 /* Parse a throw-expression.
24630
24631 throw-expression:
24632 throw assignment-expression [opt]
24633
24634 Returns a THROW_EXPR representing the throw-expression. */
24635
24636 static tree
24637 cp_parser_throw_expression (cp_parser* parser)
24638 {
24639 tree expression;
24640 cp_token* token;
24641
24642 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24643 token = cp_lexer_peek_token (parser->lexer);
24644 /* Figure out whether or not there is an assignment-expression
24645 following the "throw" keyword. */
24646 if (token->type == CPP_COMMA
24647 || token->type == CPP_SEMICOLON
24648 || token->type == CPP_CLOSE_PAREN
24649 || token->type == CPP_CLOSE_SQUARE
24650 || token->type == CPP_CLOSE_BRACE
24651 || token->type == CPP_COLON)
24652 expression = NULL_TREE;
24653 else
24654 expression = cp_parser_assignment_expression (parser);
24655
24656 return build_throw (expression);
24657 }
24658
24659 /* GNU Extensions */
24660
24661 /* Parse an (optional) asm-specification.
24662
24663 asm-specification:
24664 asm ( string-literal )
24665
24666 If the asm-specification is present, returns a STRING_CST
24667 corresponding to the string-literal. Otherwise, returns
24668 NULL_TREE. */
24669
24670 static tree
24671 cp_parser_asm_specification_opt (cp_parser* parser)
24672 {
24673 cp_token *token;
24674 tree asm_specification;
24675
24676 /* Peek at the next token. */
24677 token = cp_lexer_peek_token (parser->lexer);
24678 /* If the next token isn't the `asm' keyword, then there's no
24679 asm-specification. */
24680 if (!cp_parser_is_keyword (token, RID_ASM))
24681 return NULL_TREE;
24682
24683 /* Consume the `asm' token. */
24684 cp_lexer_consume_token (parser->lexer);
24685 /* Look for the `('. */
24686 matching_parens parens;
24687 parens.require_open (parser);
24688
24689 /* Look for the string-literal. */
24690 asm_specification = cp_parser_string_literal (parser, false, false);
24691
24692 /* Look for the `)'. */
24693 parens.require_close (parser);
24694
24695 return asm_specification;
24696 }
24697
24698 /* Parse an asm-operand-list.
24699
24700 asm-operand-list:
24701 asm-operand
24702 asm-operand-list , asm-operand
24703
24704 asm-operand:
24705 string-literal ( expression )
24706 [ string-literal ] string-literal ( expression )
24707
24708 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24709 each node is the expression. The TREE_PURPOSE is itself a
24710 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24711 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24712 is a STRING_CST for the string literal before the parenthesis. Returns
24713 ERROR_MARK_NODE if any of the operands are invalid. */
24714
24715 static tree
24716 cp_parser_asm_operand_list (cp_parser* parser)
24717 {
24718 tree asm_operands = NULL_TREE;
24719 bool invalid_operands = false;
24720
24721 while (true)
24722 {
24723 tree string_literal;
24724 tree expression;
24725 tree name;
24726
24727 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24728 {
24729 /* Consume the `[' token. */
24730 cp_lexer_consume_token (parser->lexer);
24731 /* Read the operand name. */
24732 name = cp_parser_identifier (parser);
24733 if (name != error_mark_node)
24734 name = build_string (IDENTIFIER_LENGTH (name),
24735 IDENTIFIER_POINTER (name));
24736 /* Look for the closing `]'. */
24737 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24738 }
24739 else
24740 name = NULL_TREE;
24741 /* Look for the string-literal. */
24742 string_literal = cp_parser_string_literal (parser, false, false);
24743
24744 /* Look for the `('. */
24745 matching_parens parens;
24746 parens.require_open (parser);
24747 /* Parse the expression. */
24748 expression = cp_parser_expression (parser);
24749 /* Look for the `)'. */
24750 parens.require_close (parser);
24751
24752 if (name == error_mark_node
24753 || string_literal == error_mark_node
24754 || expression == error_mark_node)
24755 invalid_operands = true;
24756
24757 /* Add this operand to the list. */
24758 asm_operands = tree_cons (build_tree_list (name, string_literal),
24759 expression,
24760 asm_operands);
24761 /* If the next token is not a `,', there are no more
24762 operands. */
24763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24764 break;
24765 /* Consume the `,'. */
24766 cp_lexer_consume_token (parser->lexer);
24767 }
24768
24769 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24770 }
24771
24772 /* Parse an asm-clobber-list.
24773
24774 asm-clobber-list:
24775 string-literal
24776 asm-clobber-list , string-literal
24777
24778 Returns a TREE_LIST, indicating the clobbers in the order that they
24779 appeared. The TREE_VALUE of each node is a STRING_CST. */
24780
24781 static tree
24782 cp_parser_asm_clobber_list (cp_parser* parser)
24783 {
24784 tree clobbers = NULL_TREE;
24785
24786 while (true)
24787 {
24788 tree string_literal;
24789
24790 /* Look for the string literal. */
24791 string_literal = cp_parser_string_literal (parser, false, false);
24792 /* Add it to the list. */
24793 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24794 /* If the next token is not a `,', then the list is
24795 complete. */
24796 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24797 break;
24798 /* Consume the `,' token. */
24799 cp_lexer_consume_token (parser->lexer);
24800 }
24801
24802 return clobbers;
24803 }
24804
24805 /* Parse an asm-label-list.
24806
24807 asm-label-list:
24808 identifier
24809 asm-label-list , identifier
24810
24811 Returns a TREE_LIST, indicating the labels in the order that they
24812 appeared. The TREE_VALUE of each node is a label. */
24813
24814 static tree
24815 cp_parser_asm_label_list (cp_parser* parser)
24816 {
24817 tree labels = NULL_TREE;
24818
24819 while (true)
24820 {
24821 tree identifier, label, name;
24822
24823 /* Look for the identifier. */
24824 identifier = cp_parser_identifier (parser);
24825 if (!error_operand_p (identifier))
24826 {
24827 label = lookup_label (identifier);
24828 if (TREE_CODE (label) == LABEL_DECL)
24829 {
24830 TREE_USED (label) = 1;
24831 check_goto (label);
24832 name = build_string (IDENTIFIER_LENGTH (identifier),
24833 IDENTIFIER_POINTER (identifier));
24834 labels = tree_cons (name, label, labels);
24835 }
24836 }
24837 /* If the next token is not a `,', then the list is
24838 complete. */
24839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24840 break;
24841 /* Consume the `,' token. */
24842 cp_lexer_consume_token (parser->lexer);
24843 }
24844
24845 return nreverse (labels);
24846 }
24847
24848 /* Return TRUE iff the next tokens in the stream are possibly the
24849 beginning of a GNU extension attribute. */
24850
24851 static bool
24852 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24853 {
24854 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24855 }
24856
24857 /* Return TRUE iff the next tokens in the stream are possibly the
24858 beginning of a standard C++-11 attribute specifier. */
24859
24860 static bool
24861 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24862 {
24863 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24864 }
24865
24866 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24867 beginning of a standard C++-11 attribute specifier. */
24868
24869 static bool
24870 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24871 {
24872 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24873
24874 return (cxx_dialect >= cxx11
24875 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24876 || (token->type == CPP_OPEN_SQUARE
24877 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24878 && token->type == CPP_OPEN_SQUARE)));
24879 }
24880
24881 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24882 beginning of a GNU extension attribute. */
24883
24884 static bool
24885 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24886 {
24887 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24888
24889 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24890 }
24891
24892 /* Return true iff the next tokens can be the beginning of either a
24893 GNU attribute list, or a standard C++11 attribute sequence. */
24894
24895 static bool
24896 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24897 {
24898 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24899 || cp_next_tokens_can_be_std_attribute_p (parser));
24900 }
24901
24902 /* Return true iff the next Nth tokens can be the beginning of either
24903 a GNU attribute list, or a standard C++11 attribute sequence. */
24904
24905 static bool
24906 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24907 {
24908 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24909 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24910 }
24911
24912 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24913 of GNU attributes, or return NULL. */
24914
24915 static tree
24916 cp_parser_attributes_opt (cp_parser *parser)
24917 {
24918 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24919 return cp_parser_gnu_attributes_opt (parser);
24920 return cp_parser_std_attribute_spec_seq (parser);
24921 }
24922
24923 /* Parse an (optional) series of attributes.
24924
24925 attributes:
24926 attributes attribute
24927
24928 attribute:
24929 __attribute__ (( attribute-list [opt] ))
24930
24931 The return value is as for cp_parser_gnu_attribute_list. */
24932
24933 static tree
24934 cp_parser_gnu_attributes_opt (cp_parser* parser)
24935 {
24936 tree attributes = NULL_TREE;
24937
24938 while (true)
24939 {
24940 cp_token *token;
24941 tree attribute_list;
24942 bool ok = true;
24943
24944 /* Peek at the next token. */
24945 token = cp_lexer_peek_token (parser->lexer);
24946 /* If it's not `__attribute__', then we're done. */
24947 if (token->keyword != RID_ATTRIBUTE)
24948 break;
24949
24950 /* Consume the `__attribute__' keyword. */
24951 cp_lexer_consume_token (parser->lexer);
24952 /* Look for the two `(' tokens. */
24953 matching_parens outer_parens;
24954 outer_parens.require_open (parser);
24955 matching_parens inner_parens;
24956 inner_parens.require_open (parser);
24957
24958 /* Peek at the next token. */
24959 token = cp_lexer_peek_token (parser->lexer);
24960 if (token->type != CPP_CLOSE_PAREN)
24961 /* Parse the attribute-list. */
24962 attribute_list = cp_parser_gnu_attribute_list (parser);
24963 else
24964 /* If the next token is a `)', then there is no attribute
24965 list. */
24966 attribute_list = NULL;
24967
24968 /* Look for the two `)' tokens. */
24969 if (!inner_parens.require_close (parser))
24970 ok = false;
24971 if (!outer_parens.require_close (parser))
24972 ok = false;
24973 if (!ok)
24974 cp_parser_skip_to_end_of_statement (parser);
24975
24976 /* Add these new attributes to the list. */
24977 attributes = attr_chainon (attributes, attribute_list);
24978 }
24979
24980 return attributes;
24981 }
24982
24983 /* Parse a GNU attribute-list.
24984
24985 attribute-list:
24986 attribute
24987 attribute-list , attribute
24988
24989 attribute:
24990 identifier
24991 identifier ( identifier )
24992 identifier ( identifier , expression-list )
24993 identifier ( expression-list )
24994
24995 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24996 to an attribute. The TREE_PURPOSE of each node is the identifier
24997 indicating which attribute is in use. The TREE_VALUE represents
24998 the arguments, if any. */
24999
25000 static tree
25001 cp_parser_gnu_attribute_list (cp_parser* parser)
25002 {
25003 tree attribute_list = NULL_TREE;
25004 bool save_translate_strings_p = parser->translate_strings_p;
25005
25006 parser->translate_strings_p = false;
25007 while (true)
25008 {
25009 cp_token *token;
25010 tree identifier;
25011 tree attribute;
25012
25013 /* Look for the identifier. We also allow keywords here; for
25014 example `__attribute__ ((const))' is legal. */
25015 token = cp_lexer_peek_token (parser->lexer);
25016 if (token->type == CPP_NAME
25017 || token->type == CPP_KEYWORD)
25018 {
25019 tree arguments = NULL_TREE;
25020
25021 /* Consume the token, but save it since we need it for the
25022 SIMD enabled function parsing. */
25023 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25024
25025 /* Save away the identifier that indicates which attribute
25026 this is. */
25027 identifier = (token->type == CPP_KEYWORD)
25028 /* For keywords, use the canonical spelling, not the
25029 parsed identifier. */
25030 ? ridpointers[(int) token->keyword]
25031 : id_token->u.value;
25032
25033 identifier = canonicalize_attr_name (identifier);
25034 attribute = build_tree_list (identifier, NULL_TREE);
25035
25036 /* Peek at the next token. */
25037 token = cp_lexer_peek_token (parser->lexer);
25038 /* If it's an `(', then parse the attribute arguments. */
25039 if (token->type == CPP_OPEN_PAREN)
25040 {
25041 vec<tree, va_gc> *vec;
25042 int attr_flag = (attribute_takes_identifier_p (identifier)
25043 ? id_attr : normal_attr);
25044 vec = cp_parser_parenthesized_expression_list
25045 (parser, attr_flag, /*cast_p=*/false,
25046 /*allow_expansion_p=*/false,
25047 /*non_constant_p=*/NULL);
25048 if (vec == NULL)
25049 arguments = error_mark_node;
25050 else
25051 {
25052 arguments = build_tree_list_vec (vec);
25053 release_tree_vector (vec);
25054 }
25055 /* Save the arguments away. */
25056 TREE_VALUE (attribute) = arguments;
25057 }
25058
25059 if (arguments != error_mark_node)
25060 {
25061 /* Add this attribute to the list. */
25062 TREE_CHAIN (attribute) = attribute_list;
25063 attribute_list = attribute;
25064 }
25065
25066 token = cp_lexer_peek_token (parser->lexer);
25067 }
25068 /* Now, look for more attributes. If the next token isn't a
25069 `,', we're done. */
25070 if (token->type != CPP_COMMA)
25071 break;
25072
25073 /* Consume the comma and keep going. */
25074 cp_lexer_consume_token (parser->lexer);
25075 }
25076 parser->translate_strings_p = save_translate_strings_p;
25077
25078 /* We built up the list in reverse order. */
25079 return nreverse (attribute_list);
25080 }
25081
25082 /* Parse a standard C++11 attribute.
25083
25084 The returned representation is a TREE_LIST which TREE_PURPOSE is
25085 the scoped name of the attribute, and the TREE_VALUE is its
25086 arguments list.
25087
25088 Note that the scoped name of the attribute is itself a TREE_LIST
25089 which TREE_PURPOSE is the namespace of the attribute, and
25090 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25091 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25092 and which TREE_PURPOSE is directly the attribute name.
25093
25094 Clients of the attribute code should use get_attribute_namespace
25095 and get_attribute_name to get the actual namespace and name of
25096 attributes, regardless of their being GNU or C++11 attributes.
25097
25098 attribute:
25099 attribute-token attribute-argument-clause [opt]
25100
25101 attribute-token:
25102 identifier
25103 attribute-scoped-token
25104
25105 attribute-scoped-token:
25106 attribute-namespace :: identifier
25107
25108 attribute-namespace:
25109 identifier
25110
25111 attribute-argument-clause:
25112 ( balanced-token-seq )
25113
25114 balanced-token-seq:
25115 balanced-token [opt]
25116 balanced-token-seq balanced-token
25117
25118 balanced-token:
25119 ( balanced-token-seq )
25120 [ balanced-token-seq ]
25121 { balanced-token-seq }. */
25122
25123 static tree
25124 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25125 {
25126 tree attribute, attr_id = NULL_TREE, arguments;
25127 cp_token *token;
25128
25129 /* First, parse name of the attribute, a.k.a attribute-token. */
25130
25131 token = cp_lexer_peek_token (parser->lexer);
25132 if (token->type == CPP_NAME)
25133 attr_id = token->u.value;
25134 else if (token->type == CPP_KEYWORD)
25135 attr_id = ridpointers[(int) token->keyword];
25136 else if (token->flags & NAMED_OP)
25137 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25138
25139 if (attr_id == NULL_TREE)
25140 return NULL_TREE;
25141
25142 cp_lexer_consume_token (parser->lexer);
25143
25144 token = cp_lexer_peek_token (parser->lexer);
25145 if (token->type == CPP_SCOPE)
25146 {
25147 /* We are seeing a scoped attribute token. */
25148
25149 cp_lexer_consume_token (parser->lexer);
25150 if (attr_ns)
25151 error_at (token->location, "attribute using prefix used together "
25152 "with scoped attribute token");
25153 attr_ns = attr_id;
25154
25155 token = cp_lexer_consume_token (parser->lexer);
25156 if (token->type == CPP_NAME)
25157 attr_id = token->u.value;
25158 else if (token->type == CPP_KEYWORD)
25159 attr_id = ridpointers[(int) token->keyword];
25160 else if (token->flags & NAMED_OP)
25161 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25162 else
25163 {
25164 error_at (token->location,
25165 "expected an identifier for the attribute name");
25166 return error_mark_node;
25167 }
25168
25169 attr_id = canonicalize_attr_name (attr_id);
25170 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25171 NULL_TREE);
25172 token = cp_lexer_peek_token (parser->lexer);
25173 }
25174 else if (attr_ns)
25175 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25176 NULL_TREE);
25177 else
25178 {
25179 attr_id = canonicalize_attr_name (attr_id);
25180 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25181 NULL_TREE);
25182 /* C++11 noreturn attribute is equivalent to GNU's. */
25183 if (is_attribute_p ("noreturn", attr_id))
25184 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25185 /* C++14 deprecated attribute is equivalent to GNU's. */
25186 else if (is_attribute_p ("deprecated", attr_id))
25187 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25188 /* C++17 fallthrough attribute is equivalent to GNU's. */
25189 else if (is_attribute_p ("fallthrough", attr_id))
25190 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25191 /* Transactional Memory TS optimize_for_synchronized attribute is
25192 equivalent to GNU transaction_callable. */
25193 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25194 TREE_PURPOSE (attribute)
25195 = get_identifier ("transaction_callable");
25196 /* Transactional Memory attributes are GNU attributes. */
25197 else if (tm_attr_to_mask (attr_id))
25198 TREE_PURPOSE (attribute) = attr_id;
25199 }
25200
25201 /* Now parse the optional argument clause of the attribute. */
25202
25203 if (token->type != CPP_OPEN_PAREN)
25204 return attribute;
25205
25206 {
25207 vec<tree, va_gc> *vec;
25208 int attr_flag = normal_attr;
25209
25210 if (attr_ns == get_identifier ("gnu")
25211 && attribute_takes_identifier_p (attr_id))
25212 /* A GNU attribute that takes an identifier in parameter. */
25213 attr_flag = id_attr;
25214
25215 vec = cp_parser_parenthesized_expression_list
25216 (parser, attr_flag, /*cast_p=*/false,
25217 /*allow_expansion_p=*/true,
25218 /*non_constant_p=*/NULL);
25219 if (vec == NULL)
25220 arguments = error_mark_node;
25221 else
25222 {
25223 arguments = build_tree_list_vec (vec);
25224 release_tree_vector (vec);
25225 }
25226
25227 if (arguments == error_mark_node)
25228 attribute = error_mark_node;
25229 else
25230 TREE_VALUE (attribute) = arguments;
25231 }
25232
25233 return attribute;
25234 }
25235
25236 /* Check that the attribute ATTRIBUTE appears at most once in the
25237 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25238 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25239 isn't implemented yet in GCC. */
25240
25241 static void
25242 cp_parser_check_std_attribute (tree attributes, tree attribute)
25243 {
25244 if (attributes)
25245 {
25246 tree name = get_attribute_name (attribute);
25247 if (is_attribute_p ("noreturn", name)
25248 && lookup_attribute ("noreturn", attributes))
25249 error ("attribute %<noreturn%> can appear at most once "
25250 "in an attribute-list");
25251 else if (is_attribute_p ("deprecated", name)
25252 && lookup_attribute ("deprecated", attributes))
25253 error ("attribute %<deprecated%> can appear at most once "
25254 "in an attribute-list");
25255 }
25256 }
25257
25258 /* Parse a list of standard C++-11 attributes.
25259
25260 attribute-list:
25261 attribute [opt]
25262 attribute-list , attribute[opt]
25263 attribute ...
25264 attribute-list , attribute ...
25265 */
25266
25267 static tree
25268 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25269 {
25270 tree attributes = NULL_TREE, attribute = NULL_TREE;
25271 cp_token *token = NULL;
25272
25273 while (true)
25274 {
25275 attribute = cp_parser_std_attribute (parser, attr_ns);
25276 if (attribute == error_mark_node)
25277 break;
25278 if (attribute != NULL_TREE)
25279 {
25280 cp_parser_check_std_attribute (attributes, attribute);
25281 TREE_CHAIN (attribute) = attributes;
25282 attributes = attribute;
25283 }
25284 token = cp_lexer_peek_token (parser->lexer);
25285 if (token->type == CPP_ELLIPSIS)
25286 {
25287 cp_lexer_consume_token (parser->lexer);
25288 if (attribute == NULL_TREE)
25289 error_at (token->location,
25290 "expected attribute before %<...%>");
25291 else
25292 {
25293 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25294 if (pack == error_mark_node)
25295 return error_mark_node;
25296 TREE_VALUE (attribute) = pack;
25297 }
25298 token = cp_lexer_peek_token (parser->lexer);
25299 }
25300 if (token->type != CPP_COMMA)
25301 break;
25302 cp_lexer_consume_token (parser->lexer);
25303 }
25304 attributes = nreverse (attributes);
25305 return attributes;
25306 }
25307
25308 /* Parse a standard C++-11 attribute specifier.
25309
25310 attribute-specifier:
25311 [ [ attribute-using-prefix [opt] attribute-list ] ]
25312 alignment-specifier
25313
25314 attribute-using-prefix:
25315 using attribute-namespace :
25316
25317 alignment-specifier:
25318 alignas ( type-id ... [opt] )
25319 alignas ( alignment-expression ... [opt] ). */
25320
25321 static tree
25322 cp_parser_std_attribute_spec (cp_parser *parser)
25323 {
25324 tree attributes = NULL_TREE;
25325 cp_token *token = cp_lexer_peek_token (parser->lexer);
25326
25327 if (token->type == CPP_OPEN_SQUARE
25328 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25329 {
25330 tree attr_ns = NULL_TREE;
25331
25332 cp_lexer_consume_token (parser->lexer);
25333 cp_lexer_consume_token (parser->lexer);
25334
25335 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25336 {
25337 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25338 if (token->type == CPP_NAME)
25339 attr_ns = token->u.value;
25340 else if (token->type == CPP_KEYWORD)
25341 attr_ns = ridpointers[(int) token->keyword];
25342 else if (token->flags & NAMED_OP)
25343 attr_ns = get_identifier (cpp_type2name (token->type,
25344 token->flags));
25345 if (attr_ns
25346 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25347 {
25348 if (cxx_dialect < cxx17
25349 && !in_system_header_at (input_location))
25350 pedwarn (input_location, 0,
25351 "attribute using prefix only available "
25352 "with -std=c++17 or -std=gnu++17");
25353
25354 cp_lexer_consume_token (parser->lexer);
25355 cp_lexer_consume_token (parser->lexer);
25356 cp_lexer_consume_token (parser->lexer);
25357 }
25358 else
25359 attr_ns = NULL_TREE;
25360 }
25361
25362 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25363
25364 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25365 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25366 cp_parser_skip_to_end_of_statement (parser);
25367 else
25368 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25369 when we are sure that we have actually parsed them. */
25370 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25371 }
25372 else
25373 {
25374 tree alignas_expr;
25375
25376 /* Look for an alignment-specifier. */
25377
25378 token = cp_lexer_peek_token (parser->lexer);
25379
25380 if (token->type != CPP_KEYWORD
25381 || token->keyword != RID_ALIGNAS)
25382 return NULL_TREE;
25383
25384 cp_lexer_consume_token (parser->lexer);
25385 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25386
25387 matching_parens parens;
25388 if (!parens.require_open (parser))
25389 return error_mark_node;
25390
25391 cp_parser_parse_tentatively (parser);
25392 alignas_expr = cp_parser_type_id (parser);
25393
25394 if (!cp_parser_parse_definitely (parser))
25395 {
25396 alignas_expr = cp_parser_assignment_expression (parser);
25397 if (alignas_expr == error_mark_node)
25398 cp_parser_skip_to_end_of_statement (parser);
25399 if (alignas_expr == NULL_TREE
25400 || alignas_expr == error_mark_node)
25401 return alignas_expr;
25402 }
25403
25404 alignas_expr = cxx_alignas_expr (alignas_expr);
25405 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25406
25407 /* Handle alignas (pack...). */
25408 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25409 {
25410 cp_lexer_consume_token (parser->lexer);
25411 alignas_expr = make_pack_expansion (alignas_expr);
25412 }
25413
25414 /* Something went wrong, so don't build the attribute. */
25415 if (alignas_expr == error_mark_node)
25416 return error_mark_node;
25417
25418 if (!parens.require_close (parser))
25419 return error_mark_node;
25420
25421 /* Build the C++-11 representation of an 'aligned'
25422 attribute. */
25423 attributes =
25424 build_tree_list (build_tree_list (get_identifier ("gnu"),
25425 get_identifier ("aligned")),
25426 alignas_expr);
25427 }
25428
25429 return attributes;
25430 }
25431
25432 /* Parse a standard C++-11 attribute-specifier-seq.
25433
25434 attribute-specifier-seq:
25435 attribute-specifier-seq [opt] attribute-specifier
25436 */
25437
25438 static tree
25439 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25440 {
25441 tree attr_specs = NULL_TREE;
25442 tree attr_last = NULL_TREE;
25443
25444 while (true)
25445 {
25446 tree attr_spec = cp_parser_std_attribute_spec (parser);
25447 if (attr_spec == NULL_TREE)
25448 break;
25449 if (attr_spec == error_mark_node)
25450 return error_mark_node;
25451
25452 if (attr_last)
25453 TREE_CHAIN (attr_last) = attr_spec;
25454 else
25455 attr_specs = attr_last = attr_spec;
25456 attr_last = tree_last (attr_last);
25457 }
25458
25459 return attr_specs;
25460 }
25461
25462 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25463 return index of the first token after balanced-token, or N on failure. */
25464
25465 static size_t
25466 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25467 {
25468 size_t orig_n = n;
25469 int nparens = 0, nbraces = 0, nsquares = 0;
25470 do
25471 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25472 {
25473 case CPP_EOF:
25474 case CPP_PRAGMA_EOL:
25475 /* Ran out of tokens. */
25476 return orig_n;
25477 case CPP_OPEN_PAREN:
25478 ++nparens;
25479 break;
25480 case CPP_OPEN_BRACE:
25481 ++nbraces;
25482 break;
25483 case CPP_OPEN_SQUARE:
25484 ++nsquares;
25485 break;
25486 case CPP_CLOSE_PAREN:
25487 --nparens;
25488 break;
25489 case CPP_CLOSE_BRACE:
25490 --nbraces;
25491 break;
25492 case CPP_CLOSE_SQUARE:
25493 --nsquares;
25494 break;
25495 default:
25496 break;
25497 }
25498 while (nparens || nbraces || nsquares);
25499 return n;
25500 }
25501
25502 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25503 return index of the first token after the GNU attribute tokens, or N on
25504 failure. */
25505
25506 static size_t
25507 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25508 {
25509 while (true)
25510 {
25511 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25512 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25513 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25514 break;
25515
25516 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25517 if (n2 == n + 2)
25518 break;
25519 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25520 break;
25521 n = n2 + 1;
25522 }
25523 return n;
25524 }
25525
25526 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25527 next token), return index of the first token after the standard C++11
25528 attribute tokens, or N on failure. */
25529
25530 static size_t
25531 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25532 {
25533 while (true)
25534 {
25535 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25536 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25537 {
25538 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25539 if (n2 == n + 1)
25540 break;
25541 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25542 break;
25543 n = n2 + 1;
25544 }
25545 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25546 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25547 {
25548 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25549 if (n2 == n + 1)
25550 break;
25551 n = n2;
25552 }
25553 else
25554 break;
25555 }
25556 return n;
25557 }
25558
25559 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25560 as the next token), return index of the first token after the attribute
25561 tokens, or N on failure. */
25562
25563 static size_t
25564 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25565 {
25566 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25567 return cp_parser_skip_gnu_attributes_opt (parser, n);
25568 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25569 }
25570
25571 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25572 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25573 current value of the PEDANTIC flag, regardless of whether or not
25574 the `__extension__' keyword is present. The caller is responsible
25575 for restoring the value of the PEDANTIC flag. */
25576
25577 static bool
25578 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25579 {
25580 /* Save the old value of the PEDANTIC flag. */
25581 *saved_pedantic = pedantic;
25582
25583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25584 {
25585 /* Consume the `__extension__' token. */
25586 cp_lexer_consume_token (parser->lexer);
25587 /* We're not being pedantic while the `__extension__' keyword is
25588 in effect. */
25589 pedantic = 0;
25590
25591 return true;
25592 }
25593
25594 return false;
25595 }
25596
25597 /* Parse a label declaration.
25598
25599 label-declaration:
25600 __label__ label-declarator-seq ;
25601
25602 label-declarator-seq:
25603 identifier , label-declarator-seq
25604 identifier */
25605
25606 static void
25607 cp_parser_label_declaration (cp_parser* parser)
25608 {
25609 /* Look for the `__label__' keyword. */
25610 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25611
25612 while (true)
25613 {
25614 tree identifier;
25615
25616 /* Look for an identifier. */
25617 identifier = cp_parser_identifier (parser);
25618 /* If we failed, stop. */
25619 if (identifier == error_mark_node)
25620 break;
25621 /* Declare it as a label. */
25622 finish_label_decl (identifier);
25623 /* If the next token is a `;', stop. */
25624 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25625 break;
25626 /* Look for the `,' separating the label declarations. */
25627 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25628 }
25629
25630 /* Look for the final `;'. */
25631 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25632 }
25633
25634 // -------------------------------------------------------------------------- //
25635 // Requires Clause
25636
25637 // Parse a requires clause.
25638 //
25639 // requires-clause:
25640 // 'requires' logical-or-expression
25641 //
25642 // The required logical-or-expression must be a constant expression. Note
25643 // that we don't check that the expression is constepxr here. We defer until
25644 // we analyze constraints and then, we only check atomic constraints.
25645 static tree
25646 cp_parser_requires_clause (cp_parser *parser)
25647 {
25648 // Parse the requires clause so that it is not automatically folded.
25649 ++processing_template_decl;
25650 tree expr = cp_parser_binary_expression (parser, false, false,
25651 PREC_NOT_OPERATOR, NULL);
25652 if (check_for_bare_parameter_packs (expr))
25653 expr = error_mark_node;
25654 --processing_template_decl;
25655 return expr;
25656 }
25657
25658 // Optionally parse a requires clause:
25659 static tree
25660 cp_parser_requires_clause_opt (cp_parser *parser)
25661 {
25662 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25663 if (tok->keyword != RID_REQUIRES)
25664 {
25665 if (!flag_concepts && tok->type == CPP_NAME
25666 && tok->u.value == ridpointers[RID_REQUIRES])
25667 {
25668 error_at (cp_lexer_peek_token (parser->lexer)->location,
25669 "%<requires%> only available with -fconcepts");
25670 /* Parse and discard the requires-clause. */
25671 cp_lexer_consume_token (parser->lexer);
25672 cp_parser_requires_clause (parser);
25673 }
25674 return NULL_TREE;
25675 }
25676 cp_lexer_consume_token (parser->lexer);
25677 return cp_parser_requires_clause (parser);
25678 }
25679
25680
25681 /*---------------------------------------------------------------------------
25682 Requires expressions
25683 ---------------------------------------------------------------------------*/
25684
25685 /* Parse a requires expression
25686
25687 requirement-expression:
25688 'requires' requirement-parameter-list [opt] requirement-body */
25689 static tree
25690 cp_parser_requires_expression (cp_parser *parser)
25691 {
25692 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25693 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25694
25695 /* A requires-expression shall appear only within a concept
25696 definition or a requires-clause.
25697
25698 TODO: Implement this diagnostic correctly. */
25699 if (!processing_template_decl)
25700 {
25701 error_at (loc, "a requires expression cannot appear outside a template");
25702 cp_parser_skip_to_end_of_statement (parser);
25703 return error_mark_node;
25704 }
25705
25706 tree parms, reqs;
25707 {
25708 /* Local parameters are delared as variables within the scope
25709 of the expression. They are not visible past the end of
25710 the expression. Expressions within the requires-expression
25711 are unevaluated. */
25712 struct scope_sentinel
25713 {
25714 scope_sentinel ()
25715 {
25716 ++cp_unevaluated_operand;
25717 begin_scope (sk_block, NULL_TREE);
25718 }
25719
25720 ~scope_sentinel ()
25721 {
25722 pop_bindings_and_leave_scope ();
25723 --cp_unevaluated_operand;
25724 }
25725 } s;
25726
25727 /* Parse the optional parameter list. */
25728 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25729 {
25730 parms = cp_parser_requirement_parameter_list (parser);
25731 if (parms == error_mark_node)
25732 return error_mark_node;
25733 }
25734 else
25735 parms = NULL_TREE;
25736
25737 /* Parse the requirement body. */
25738 reqs = cp_parser_requirement_body (parser);
25739 if (reqs == error_mark_node)
25740 return error_mark_node;
25741 }
25742
25743 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25744 the parm chain. */
25745 grokparms (parms, &parms);
25746 return finish_requires_expr (parms, reqs);
25747 }
25748
25749 /* Parse a parameterized requirement.
25750
25751 requirement-parameter-list:
25752 '(' parameter-declaration-clause ')' */
25753 static tree
25754 cp_parser_requirement_parameter_list (cp_parser *parser)
25755 {
25756 matching_parens parens;
25757 if (!parens.require_open (parser))
25758 return error_mark_node;
25759
25760 tree parms = cp_parser_parameter_declaration_clause (parser);
25761
25762 if (!parens.require_close (parser))
25763 return error_mark_node;
25764
25765 return parms;
25766 }
25767
25768 /* Parse the body of a requirement.
25769
25770 requirement-body:
25771 '{' requirement-list '}' */
25772 static tree
25773 cp_parser_requirement_body (cp_parser *parser)
25774 {
25775 matching_braces braces;
25776 if (!braces.require_open (parser))
25777 return error_mark_node;
25778
25779 tree reqs = cp_parser_requirement_list (parser);
25780
25781 if (!braces.require_close (parser))
25782 return error_mark_node;
25783
25784 return reqs;
25785 }
25786
25787 /* Parse a list of requirements.
25788
25789 requirement-list:
25790 requirement
25791 requirement-list ';' requirement[opt] */
25792 static tree
25793 cp_parser_requirement_list (cp_parser *parser)
25794 {
25795 tree result = NULL_TREE;
25796 while (true)
25797 {
25798 tree req = cp_parser_requirement (parser);
25799 if (req == error_mark_node)
25800 return error_mark_node;
25801
25802 result = tree_cons (NULL_TREE, req, result);
25803
25804 /* If we see a semi-colon, consume it. */
25805 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25806 cp_lexer_consume_token (parser->lexer);
25807
25808 /* Stop processing at the end of the list. */
25809 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25810 break;
25811 }
25812
25813 /* Reverse the order of requirements so they are analyzed in
25814 declaration order. */
25815 return nreverse (result);
25816 }
25817
25818 /* Parse a syntactic requirement or type requirement.
25819
25820 requirement:
25821 simple-requirement
25822 compound-requirement
25823 type-requirement
25824 nested-requirement */
25825 static tree
25826 cp_parser_requirement (cp_parser *parser)
25827 {
25828 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25829 return cp_parser_compound_requirement (parser);
25830 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25831 return cp_parser_type_requirement (parser);
25832 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25833 return cp_parser_nested_requirement (parser);
25834 else
25835 return cp_parser_simple_requirement (parser);
25836 }
25837
25838 /* Parse a simple requirement.
25839
25840 simple-requirement:
25841 expression ';' */
25842 static tree
25843 cp_parser_simple_requirement (cp_parser *parser)
25844 {
25845 tree expr = cp_parser_expression (parser, NULL, false, false);
25846 if (!expr || expr == error_mark_node)
25847 return error_mark_node;
25848
25849 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25850 return error_mark_node;
25851
25852 return finish_simple_requirement (expr);
25853 }
25854
25855 /* Parse a type requirement
25856
25857 type-requirement
25858 nested-name-specifier [opt] required-type-name ';'
25859
25860 required-type-name:
25861 type-name
25862 'template' [opt] simple-template-id */
25863 static tree
25864 cp_parser_type_requirement (cp_parser *parser)
25865 {
25866 cp_lexer_consume_token (parser->lexer);
25867
25868 // Save the scope before parsing name specifiers.
25869 tree saved_scope = parser->scope;
25870 tree saved_object_scope = parser->object_scope;
25871 tree saved_qualifying_scope = parser->qualifying_scope;
25872 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25873 cp_parser_nested_name_specifier_opt (parser,
25874 /*typename_keyword_p=*/true,
25875 /*check_dependency_p=*/false,
25876 /*type_p=*/true,
25877 /*is_declaration=*/false);
25878
25879 tree type;
25880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25881 {
25882 cp_lexer_consume_token (parser->lexer);
25883 type = cp_parser_template_id (parser,
25884 /*template_keyword_p=*/true,
25885 /*check_dependency=*/false,
25886 /*tag_type=*/none_type,
25887 /*is_declaration=*/false);
25888 type = make_typename_type (parser->scope, type, typename_type,
25889 /*complain=*/tf_error);
25890 }
25891 else
25892 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25893
25894 if (TREE_CODE (type) == TYPE_DECL)
25895 type = TREE_TYPE (type);
25896
25897 parser->scope = saved_scope;
25898 parser->object_scope = saved_object_scope;
25899 parser->qualifying_scope = saved_qualifying_scope;
25900
25901 if (type == error_mark_node)
25902 cp_parser_skip_to_end_of_statement (parser);
25903
25904 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25905 return error_mark_node;
25906 if (type == error_mark_node)
25907 return error_mark_node;
25908
25909 return finish_type_requirement (type);
25910 }
25911
25912 /* Parse a compound requirement
25913
25914 compound-requirement:
25915 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25916 static tree
25917 cp_parser_compound_requirement (cp_parser *parser)
25918 {
25919 /* Parse an expression enclosed in '{ }'s. */
25920 matching_braces braces;
25921 if (!braces.require_open (parser))
25922 return error_mark_node;
25923
25924 tree expr = cp_parser_expression (parser, NULL, false, false);
25925 if (!expr || expr == error_mark_node)
25926 return error_mark_node;
25927
25928 if (!braces.require_close (parser))
25929 return error_mark_node;
25930
25931 /* Parse the optional noexcept. */
25932 bool noexcept_p = false;
25933 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25934 {
25935 cp_lexer_consume_token (parser->lexer);
25936 noexcept_p = true;
25937 }
25938
25939 /* Parse the optional trailing return type. */
25940 tree type = NULL_TREE;
25941 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25942 {
25943 cp_lexer_consume_token (parser->lexer);
25944 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25945 parser->in_result_type_constraint_p = true;
25946 type = cp_parser_trailing_type_id (parser);
25947 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25948 if (type == error_mark_node)
25949 return error_mark_node;
25950 }
25951
25952 return finish_compound_requirement (expr, type, noexcept_p);
25953 }
25954
25955 /* Parse a nested requirement. This is the same as a requires clause.
25956
25957 nested-requirement:
25958 requires-clause */
25959 static tree
25960 cp_parser_nested_requirement (cp_parser *parser)
25961 {
25962 cp_lexer_consume_token (parser->lexer);
25963 tree req = cp_parser_requires_clause (parser);
25964 if (req == error_mark_node)
25965 return error_mark_node;
25966 return finish_nested_requirement (req);
25967 }
25968
25969 /* Support Functions */
25970
25971 /* Return the appropriate prefer_type argument for lookup_name_real based on
25972 tag_type and template_mem_access. */
25973
25974 static inline int
25975 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25976 {
25977 /* DR 141: When looking in the current enclosing context for a template-name
25978 after -> or ., only consider class templates. */
25979 if (template_mem_access)
25980 return 2;
25981 switch (tag_type)
25982 {
25983 case none_type: return 0; // No preference.
25984 case scope_type: return 1; // Type or namespace.
25985 default: return 2; // Type only.
25986 }
25987 }
25988
25989 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25990 NAME should have one of the representations used for an
25991 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25992 is returned. If PARSER->SCOPE is a dependent type, then a
25993 SCOPE_REF is returned.
25994
25995 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25996 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25997 was formed. Abstractly, such entities should not be passed to this
25998 function, because they do not need to be looked up, but it is
25999 simpler to check for this special case here, rather than at the
26000 call-sites.
26001
26002 In cases not explicitly covered above, this function returns a
26003 DECL, OVERLOAD, or baselink representing the result of the lookup.
26004 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26005 is returned.
26006
26007 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26008 (e.g., "struct") that was used. In that case bindings that do not
26009 refer to types are ignored.
26010
26011 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26012 ignored.
26013
26014 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26015 are ignored.
26016
26017 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26018 types.
26019
26020 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26021 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26022 NULL_TREE otherwise. */
26023
26024 static cp_expr
26025 cp_parser_lookup_name (cp_parser *parser, tree name,
26026 enum tag_types tag_type,
26027 bool is_template,
26028 bool is_namespace,
26029 bool check_dependency,
26030 tree *ambiguous_decls,
26031 location_t name_location)
26032 {
26033 tree decl;
26034 tree object_type = parser->context->object_type;
26035
26036 /* Assume that the lookup will be unambiguous. */
26037 if (ambiguous_decls)
26038 *ambiguous_decls = NULL_TREE;
26039
26040 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26041 no longer valid. Note that if we are parsing tentatively, and
26042 the parse fails, OBJECT_TYPE will be automatically restored. */
26043 parser->context->object_type = NULL_TREE;
26044
26045 if (name == error_mark_node)
26046 return error_mark_node;
26047
26048 /* A template-id has already been resolved; there is no lookup to
26049 do. */
26050 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26051 return name;
26052 if (BASELINK_P (name))
26053 {
26054 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26055 == TEMPLATE_ID_EXPR);
26056 return name;
26057 }
26058
26059 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26060 it should already have been checked to make sure that the name
26061 used matches the type being destroyed. */
26062 if (TREE_CODE (name) == BIT_NOT_EXPR)
26063 {
26064 tree type;
26065
26066 /* Figure out to which type this destructor applies. */
26067 if (parser->scope)
26068 type = parser->scope;
26069 else if (object_type)
26070 type = object_type;
26071 else
26072 type = current_class_type;
26073 /* If that's not a class type, there is no destructor. */
26074 if (!type || !CLASS_TYPE_P (type))
26075 return error_mark_node;
26076
26077 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26078 lazily_declare_fn (sfk_destructor, type);
26079
26080 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26081 return dtor;
26082
26083 return error_mark_node;
26084 }
26085
26086 /* By this point, the NAME should be an ordinary identifier. If
26087 the id-expression was a qualified name, the qualifying scope is
26088 stored in PARSER->SCOPE at this point. */
26089 gcc_assert (identifier_p (name));
26090
26091 /* Perform the lookup. */
26092 if (parser->scope)
26093 {
26094 bool dependent_p;
26095
26096 if (parser->scope == error_mark_node)
26097 return error_mark_node;
26098
26099 /* If the SCOPE is dependent, the lookup must be deferred until
26100 the template is instantiated -- unless we are explicitly
26101 looking up names in uninstantiated templates. Even then, we
26102 cannot look up the name if the scope is not a class type; it
26103 might, for example, be a template type parameter. */
26104 dependent_p = (TYPE_P (parser->scope)
26105 && dependent_scope_p (parser->scope));
26106 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26107 && dependent_p)
26108 /* Defer lookup. */
26109 decl = error_mark_node;
26110 else
26111 {
26112 tree pushed_scope = NULL_TREE;
26113
26114 /* If PARSER->SCOPE is a dependent type, then it must be a
26115 class type, and we must not be checking dependencies;
26116 otherwise, we would have processed this lookup above. So
26117 that PARSER->SCOPE is not considered a dependent base by
26118 lookup_member, we must enter the scope here. */
26119 if (dependent_p)
26120 pushed_scope = push_scope (parser->scope);
26121
26122 /* If the PARSER->SCOPE is a template specialization, it
26123 may be instantiated during name lookup. In that case,
26124 errors may be issued. Even if we rollback the current
26125 tentative parse, those errors are valid. */
26126 decl = lookup_qualified_name (parser->scope, name,
26127 prefer_type_arg (tag_type),
26128 /*complain=*/true);
26129
26130 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26131 lookup result and the nested-name-specifier nominates a class C:
26132 * if the name specified after the nested-name-specifier, when
26133 looked up in C, is the injected-class-name of C (Clause 9), or
26134 * if the name specified after the nested-name-specifier is the
26135 same as the identifier or the simple-template-id's template-
26136 name in the last component of the nested-name-specifier,
26137 the name is instead considered to name the constructor of
26138 class C. [ Note: for example, the constructor is not an
26139 acceptable lookup result in an elaborated-type-specifier so
26140 the constructor would not be used in place of the
26141 injected-class-name. --end note ] Such a constructor name
26142 shall be used only in the declarator-id of a declaration that
26143 names a constructor or in a using-declaration. */
26144 if (tag_type == none_type
26145 && DECL_SELF_REFERENCE_P (decl)
26146 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26147 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26148 prefer_type_arg (tag_type),
26149 /*complain=*/true);
26150
26151 /* If we have a single function from a using decl, pull it out. */
26152 if (TREE_CODE (decl) == OVERLOAD
26153 && !really_overloaded_fn (decl))
26154 decl = OVL_FUNCTION (decl);
26155
26156 if (pushed_scope)
26157 pop_scope (pushed_scope);
26158 }
26159
26160 /* If the scope is a dependent type and either we deferred lookup or
26161 we did lookup but didn't find the name, rememeber the name. */
26162 if (decl == error_mark_node && TYPE_P (parser->scope)
26163 && dependent_type_p (parser->scope))
26164 {
26165 if (tag_type)
26166 {
26167 tree type;
26168
26169 /* The resolution to Core Issue 180 says that `struct
26170 A::B' should be considered a type-name, even if `A'
26171 is dependent. */
26172 type = make_typename_type (parser->scope, name, tag_type,
26173 /*complain=*/tf_error);
26174 if (type != error_mark_node)
26175 decl = TYPE_NAME (type);
26176 }
26177 else if (is_template
26178 && (cp_parser_next_token_ends_template_argument_p (parser)
26179 || cp_lexer_next_token_is (parser->lexer,
26180 CPP_CLOSE_PAREN)))
26181 decl = make_unbound_class_template (parser->scope,
26182 name, NULL_TREE,
26183 /*complain=*/tf_error);
26184 else
26185 decl = build_qualified_name (/*type=*/NULL_TREE,
26186 parser->scope, name,
26187 is_template);
26188 }
26189 parser->qualifying_scope = parser->scope;
26190 parser->object_scope = NULL_TREE;
26191 }
26192 else if (object_type)
26193 {
26194 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26195 OBJECT_TYPE is not a class. */
26196 if (CLASS_TYPE_P (object_type))
26197 /* If the OBJECT_TYPE is a template specialization, it may
26198 be instantiated during name lookup. In that case, errors
26199 may be issued. Even if we rollback the current tentative
26200 parse, those errors are valid. */
26201 decl = lookup_member (object_type,
26202 name,
26203 /*protect=*/0,
26204 prefer_type_arg (tag_type),
26205 tf_warning_or_error);
26206 else
26207 decl = NULL_TREE;
26208
26209 if (!decl)
26210 /* Look it up in the enclosing context. DR 141: When looking for a
26211 template-name after -> or ., only consider class templates. */
26212 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26213 /*nonclass=*/0,
26214 /*block_p=*/true, is_namespace, 0);
26215 if (object_type == unknown_type_node)
26216 /* The object is type-dependent, so we can't look anything up; we used
26217 this to get the DR 141 behavior. */
26218 object_type = NULL_TREE;
26219 parser->object_scope = object_type;
26220 parser->qualifying_scope = NULL_TREE;
26221 }
26222 else
26223 {
26224 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26225 /*nonclass=*/0,
26226 /*block_p=*/true, is_namespace, 0);
26227 parser->qualifying_scope = NULL_TREE;
26228 parser->object_scope = NULL_TREE;
26229 }
26230
26231 /* If the lookup failed, let our caller know. */
26232 if (!decl || decl == error_mark_node)
26233 return error_mark_node;
26234
26235 /* Pull out the template from an injected-class-name (or multiple). */
26236 if (is_template)
26237 decl = maybe_get_template_decl_from_type_decl (decl);
26238
26239 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26240 if (TREE_CODE (decl) == TREE_LIST)
26241 {
26242 if (ambiguous_decls)
26243 *ambiguous_decls = decl;
26244 /* The error message we have to print is too complicated for
26245 cp_parser_error, so we incorporate its actions directly. */
26246 if (!cp_parser_simulate_error (parser))
26247 {
26248 error_at (name_location, "reference to %qD is ambiguous",
26249 name);
26250 print_candidates (decl);
26251 }
26252 return error_mark_node;
26253 }
26254
26255 gcc_assert (DECL_P (decl)
26256 || TREE_CODE (decl) == OVERLOAD
26257 || TREE_CODE (decl) == SCOPE_REF
26258 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26259 || BASELINK_P (decl));
26260
26261 /* If we have resolved the name of a member declaration, check to
26262 see if the declaration is accessible. When the name resolves to
26263 set of overloaded functions, accessibility is checked when
26264 overload resolution is done.
26265
26266 During an explicit instantiation, access is not checked at all,
26267 as per [temp.explicit]. */
26268 if (DECL_P (decl))
26269 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26270
26271 maybe_record_typedef_use (decl);
26272
26273 return cp_expr (decl, name_location);
26274 }
26275
26276 /* Like cp_parser_lookup_name, but for use in the typical case where
26277 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26278 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26279
26280 static tree
26281 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26282 {
26283 return cp_parser_lookup_name (parser, name,
26284 none_type,
26285 /*is_template=*/false,
26286 /*is_namespace=*/false,
26287 /*check_dependency=*/true,
26288 /*ambiguous_decls=*/NULL,
26289 location);
26290 }
26291
26292 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26293 the current context, return the TYPE_DECL. If TAG_NAME_P is
26294 true, the DECL indicates the class being defined in a class-head,
26295 or declared in an elaborated-type-specifier.
26296
26297 Otherwise, return DECL. */
26298
26299 static tree
26300 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26301 {
26302 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26303 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26304
26305 struct A {
26306 template <typename T> struct B;
26307 };
26308
26309 template <typename T> struct A::B {};
26310
26311 Similarly, in an elaborated-type-specifier:
26312
26313 namespace N { struct X{}; }
26314
26315 struct A {
26316 template <typename T> friend struct N::X;
26317 };
26318
26319 However, if the DECL refers to a class type, and we are in
26320 the scope of the class, then the name lookup automatically
26321 finds the TYPE_DECL created by build_self_reference rather
26322 than a TEMPLATE_DECL. For example, in:
26323
26324 template <class T> struct S {
26325 S s;
26326 };
26327
26328 there is no need to handle such case. */
26329
26330 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26331 return DECL_TEMPLATE_RESULT (decl);
26332
26333 return decl;
26334 }
26335
26336 /* If too many, or too few, template-parameter lists apply to the
26337 declarator, issue an error message. Returns TRUE if all went well,
26338 and FALSE otherwise. */
26339
26340 static bool
26341 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26342 cp_declarator *declarator,
26343 location_t declarator_location)
26344 {
26345 switch (declarator->kind)
26346 {
26347 case cdk_id:
26348 {
26349 unsigned num_templates = 0;
26350 tree scope = declarator->u.id.qualifying_scope;
26351
26352 if (scope)
26353 num_templates = num_template_headers_for_class (scope);
26354 else if (TREE_CODE (declarator->u.id.unqualified_name)
26355 == TEMPLATE_ID_EXPR)
26356 /* If the DECLARATOR has the form `X<y>' then it uses one
26357 additional level of template parameters. */
26358 ++num_templates;
26359
26360 return cp_parser_check_template_parameters
26361 (parser, num_templates, declarator_location, declarator);
26362 }
26363
26364 case cdk_function:
26365 case cdk_array:
26366 case cdk_pointer:
26367 case cdk_reference:
26368 case cdk_ptrmem:
26369 return (cp_parser_check_declarator_template_parameters
26370 (parser, declarator->declarator, declarator_location));
26371
26372 case cdk_decomp:
26373 case cdk_error:
26374 return true;
26375
26376 default:
26377 gcc_unreachable ();
26378 }
26379 return false;
26380 }
26381
26382 /* NUM_TEMPLATES were used in the current declaration. If that is
26383 invalid, return FALSE and issue an error messages. Otherwise,
26384 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26385 declarator and we can print more accurate diagnostics. */
26386
26387 static bool
26388 cp_parser_check_template_parameters (cp_parser* parser,
26389 unsigned num_templates,
26390 location_t location,
26391 cp_declarator *declarator)
26392 {
26393 /* If there are the same number of template classes and parameter
26394 lists, that's OK. */
26395 if (parser->num_template_parameter_lists == num_templates)
26396 return true;
26397 /* If there are more, but only one more, then we are referring to a
26398 member template. That's OK too. */
26399 if (parser->num_template_parameter_lists == num_templates + 1)
26400 return true;
26401 /* If there are more template classes than parameter lists, we have
26402 something like:
26403
26404 template <class T> void S<T>::R<T>::f (); */
26405 if (parser->num_template_parameter_lists < num_templates)
26406 {
26407 if (declarator && !current_function_decl)
26408 error_at (location, "specializing member %<%T::%E%> "
26409 "requires %<template<>%> syntax",
26410 declarator->u.id.qualifying_scope,
26411 declarator->u.id.unqualified_name);
26412 else if (declarator)
26413 error_at (location, "invalid declaration of %<%T::%E%>",
26414 declarator->u.id.qualifying_scope,
26415 declarator->u.id.unqualified_name);
26416 else
26417 error_at (location, "too few template-parameter-lists");
26418 return false;
26419 }
26420 /* Otherwise, there are too many template parameter lists. We have
26421 something like:
26422
26423 template <class T> template <class U> void S::f(); */
26424 error_at (location, "too many template-parameter-lists");
26425 return false;
26426 }
26427
26428 /* Parse an optional `::' token indicating that the following name is
26429 from the global namespace. If so, PARSER->SCOPE is set to the
26430 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26431 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26432 Returns the new value of PARSER->SCOPE, if the `::' token is
26433 present, and NULL_TREE otherwise. */
26434
26435 static tree
26436 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26437 {
26438 cp_token *token;
26439
26440 /* Peek at the next token. */
26441 token = cp_lexer_peek_token (parser->lexer);
26442 /* If we're looking at a `::' token then we're starting from the
26443 global namespace, not our current location. */
26444 if (token->type == CPP_SCOPE)
26445 {
26446 /* Consume the `::' token. */
26447 cp_lexer_consume_token (parser->lexer);
26448 /* Set the SCOPE so that we know where to start the lookup. */
26449 parser->scope = global_namespace;
26450 parser->qualifying_scope = global_namespace;
26451 parser->object_scope = NULL_TREE;
26452
26453 return parser->scope;
26454 }
26455 else if (!current_scope_valid_p)
26456 {
26457 parser->scope = NULL_TREE;
26458 parser->qualifying_scope = NULL_TREE;
26459 parser->object_scope = NULL_TREE;
26460 }
26461
26462 return NULL_TREE;
26463 }
26464
26465 /* Returns TRUE if the upcoming token sequence is the start of a
26466 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26467 declarator is preceded by the `friend' specifier. */
26468
26469 static bool
26470 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26471 {
26472 bool constructor_p;
26473 bool outside_class_specifier_p;
26474 tree nested_name_specifier;
26475 cp_token *next_token;
26476
26477 /* The common case is that this is not a constructor declarator, so
26478 try to avoid doing lots of work if at all possible. It's not
26479 valid declare a constructor at function scope. */
26480 if (parser->in_function_body)
26481 return false;
26482 /* And only certain tokens can begin a constructor declarator. */
26483 next_token = cp_lexer_peek_token (parser->lexer);
26484 if (next_token->type != CPP_NAME
26485 && next_token->type != CPP_SCOPE
26486 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26487 && next_token->type != CPP_TEMPLATE_ID)
26488 return false;
26489
26490 /* Parse tentatively; we are going to roll back all of the tokens
26491 consumed here. */
26492 cp_parser_parse_tentatively (parser);
26493 /* Assume that we are looking at a constructor declarator. */
26494 constructor_p = true;
26495
26496 /* Look for the optional `::' operator. */
26497 cp_parser_global_scope_opt (parser,
26498 /*current_scope_valid_p=*/false);
26499 /* Look for the nested-name-specifier. */
26500 nested_name_specifier
26501 = (cp_parser_nested_name_specifier_opt (parser,
26502 /*typename_keyword_p=*/false,
26503 /*check_dependency_p=*/false,
26504 /*type_p=*/false,
26505 /*is_declaration=*/false));
26506
26507 outside_class_specifier_p = (!at_class_scope_p ()
26508 || !TYPE_BEING_DEFINED (current_class_type)
26509 || friend_p);
26510
26511 /* Outside of a class-specifier, there must be a
26512 nested-name-specifier. Except in C++17 mode, where we
26513 might be declaring a guiding declaration. */
26514 if (!nested_name_specifier && outside_class_specifier_p
26515 && cxx_dialect < cxx17)
26516 constructor_p = false;
26517 else if (nested_name_specifier == error_mark_node)
26518 constructor_p = false;
26519
26520 /* If we have a class scope, this is easy; DR 147 says that S::S always
26521 names the constructor, and no other qualified name could. */
26522 if (constructor_p && nested_name_specifier
26523 && CLASS_TYPE_P (nested_name_specifier))
26524 {
26525 tree id = cp_parser_unqualified_id (parser,
26526 /*template_keyword_p=*/false,
26527 /*check_dependency_p=*/false,
26528 /*declarator_p=*/true,
26529 /*optional_p=*/false);
26530 if (is_overloaded_fn (id))
26531 id = DECL_NAME (get_first_fn (id));
26532 if (!constructor_name_p (id, nested_name_specifier))
26533 constructor_p = false;
26534 }
26535 /* If we still think that this might be a constructor-declarator,
26536 look for a class-name. */
26537 else if (constructor_p)
26538 {
26539 /* If we have:
26540
26541 template <typename T> struct S {
26542 S();
26543 };
26544
26545 we must recognize that the nested `S' names a class. */
26546 if (cxx_dialect >= cxx17)
26547 cp_parser_parse_tentatively (parser);
26548
26549 tree type_decl;
26550 type_decl = cp_parser_class_name (parser,
26551 /*typename_keyword_p=*/false,
26552 /*template_keyword_p=*/false,
26553 none_type,
26554 /*check_dependency_p=*/false,
26555 /*class_head_p=*/false,
26556 /*is_declaration=*/false);
26557
26558 if (cxx_dialect >= cxx17
26559 && !cp_parser_parse_definitely (parser))
26560 {
26561 type_decl = NULL_TREE;
26562 tree tmpl = cp_parser_template_name (parser,
26563 /*template_keyword*/false,
26564 /*check_dependency_p*/false,
26565 /*is_declaration*/false,
26566 none_type,
26567 /*is_identifier*/NULL);
26568 if (DECL_CLASS_TEMPLATE_P (tmpl)
26569 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26570 /* It's a deduction guide, return true. */;
26571 else
26572 cp_parser_simulate_error (parser);
26573 }
26574
26575 /* If there was no class-name, then this is not a constructor.
26576 Otherwise, if we are in a class-specifier and we aren't
26577 handling a friend declaration, check that its type matches
26578 current_class_type (c++/38313). Note: error_mark_node
26579 is left alone for error recovery purposes. */
26580 constructor_p = (!cp_parser_error_occurred (parser)
26581 && (outside_class_specifier_p
26582 || type_decl == NULL_TREE
26583 || type_decl == error_mark_node
26584 || same_type_p (current_class_type,
26585 TREE_TYPE (type_decl))));
26586
26587 /* If we're still considering a constructor, we have to see a `(',
26588 to begin the parameter-declaration-clause, followed by either a
26589 `)', an `...', or a decl-specifier. We need to check for a
26590 type-specifier to avoid being fooled into thinking that:
26591
26592 S (f) (int);
26593
26594 is a constructor. (It is actually a function named `f' that
26595 takes one parameter (of type `int') and returns a value of type
26596 `S'. */
26597 if (constructor_p
26598 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26599 constructor_p = false;
26600
26601 if (constructor_p
26602 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26603 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26604 /* A parameter declaration begins with a decl-specifier,
26605 which is either the "attribute" keyword, a storage class
26606 specifier, or (usually) a type-specifier. */
26607 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26608 {
26609 tree type;
26610 tree pushed_scope = NULL_TREE;
26611 unsigned saved_num_template_parameter_lists;
26612
26613 /* Names appearing in the type-specifier should be looked up
26614 in the scope of the class. */
26615 if (current_class_type)
26616 type = NULL_TREE;
26617 else if (type_decl)
26618 {
26619 type = TREE_TYPE (type_decl);
26620 if (TREE_CODE (type) == TYPENAME_TYPE)
26621 {
26622 type = resolve_typename_type (type,
26623 /*only_current_p=*/false);
26624 if (TREE_CODE (type) == TYPENAME_TYPE)
26625 {
26626 cp_parser_abort_tentative_parse (parser);
26627 return false;
26628 }
26629 }
26630 pushed_scope = push_scope (type);
26631 }
26632
26633 /* Inside the constructor parameter list, surrounding
26634 template-parameter-lists do not apply. */
26635 saved_num_template_parameter_lists
26636 = parser->num_template_parameter_lists;
26637 parser->num_template_parameter_lists = 0;
26638
26639 /* Look for the type-specifier. */
26640 cp_parser_type_specifier (parser,
26641 CP_PARSER_FLAGS_NONE,
26642 /*decl_specs=*/NULL,
26643 /*is_declarator=*/true,
26644 /*declares_class_or_enum=*/NULL,
26645 /*is_cv_qualifier=*/NULL);
26646
26647 parser->num_template_parameter_lists
26648 = saved_num_template_parameter_lists;
26649
26650 /* Leave the scope of the class. */
26651 if (pushed_scope)
26652 pop_scope (pushed_scope);
26653
26654 constructor_p = !cp_parser_error_occurred (parser);
26655 }
26656 }
26657
26658 /* We did not really want to consume any tokens. */
26659 cp_parser_abort_tentative_parse (parser);
26660
26661 return constructor_p;
26662 }
26663
26664 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26665 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26666 they must be performed once we are in the scope of the function.
26667
26668 Returns the function defined. */
26669
26670 static tree
26671 cp_parser_function_definition_from_specifiers_and_declarator
26672 (cp_parser* parser,
26673 cp_decl_specifier_seq *decl_specifiers,
26674 tree attributes,
26675 const cp_declarator *declarator)
26676 {
26677 tree fn;
26678 bool success_p;
26679
26680 /* Begin the function-definition. */
26681 success_p = start_function (decl_specifiers, declarator, attributes);
26682
26683 /* The things we're about to see are not directly qualified by any
26684 template headers we've seen thus far. */
26685 reset_specialization ();
26686
26687 /* If there were names looked up in the decl-specifier-seq that we
26688 did not check, check them now. We must wait until we are in the
26689 scope of the function to perform the checks, since the function
26690 might be a friend. */
26691 perform_deferred_access_checks (tf_warning_or_error);
26692
26693 if (success_p)
26694 {
26695 cp_finalize_omp_declare_simd (parser, current_function_decl);
26696 parser->omp_declare_simd = NULL;
26697 cp_finalize_oacc_routine (parser, current_function_decl, true);
26698 parser->oacc_routine = NULL;
26699 }
26700
26701 if (!success_p)
26702 {
26703 /* Skip the entire function. */
26704 cp_parser_skip_to_end_of_block_or_statement (parser);
26705 fn = error_mark_node;
26706 }
26707 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26708 {
26709 /* Seen already, skip it. An error message has already been output. */
26710 cp_parser_skip_to_end_of_block_or_statement (parser);
26711 fn = current_function_decl;
26712 current_function_decl = NULL_TREE;
26713 /* If this is a function from a class, pop the nested class. */
26714 if (current_class_name)
26715 pop_nested_class ();
26716 }
26717 else
26718 {
26719 timevar_id_t tv;
26720 if (DECL_DECLARED_INLINE_P (current_function_decl))
26721 tv = TV_PARSE_INLINE;
26722 else
26723 tv = TV_PARSE_FUNC;
26724 timevar_push (tv);
26725 fn = cp_parser_function_definition_after_declarator (parser,
26726 /*inline_p=*/false);
26727 timevar_pop (tv);
26728 }
26729
26730 return fn;
26731 }
26732
26733 /* Parse the part of a function-definition that follows the
26734 declarator. INLINE_P is TRUE iff this function is an inline
26735 function defined within a class-specifier.
26736
26737 Returns the function defined. */
26738
26739 static tree
26740 cp_parser_function_definition_after_declarator (cp_parser* parser,
26741 bool inline_p)
26742 {
26743 tree fn;
26744 bool saved_in_unbraced_linkage_specification_p;
26745 bool saved_in_function_body;
26746 unsigned saved_num_template_parameter_lists;
26747 cp_token *token;
26748 bool fully_implicit_function_template_p
26749 = parser->fully_implicit_function_template_p;
26750 parser->fully_implicit_function_template_p = false;
26751 tree implicit_template_parms
26752 = parser->implicit_template_parms;
26753 parser->implicit_template_parms = 0;
26754 cp_binding_level* implicit_template_scope
26755 = parser->implicit_template_scope;
26756 parser->implicit_template_scope = 0;
26757
26758 saved_in_function_body = parser->in_function_body;
26759 parser->in_function_body = true;
26760 /* If the next token is `return', then the code may be trying to
26761 make use of the "named return value" extension that G++ used to
26762 support. */
26763 token = cp_lexer_peek_token (parser->lexer);
26764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26765 {
26766 /* Consume the `return' keyword. */
26767 cp_lexer_consume_token (parser->lexer);
26768 /* Look for the identifier that indicates what value is to be
26769 returned. */
26770 cp_parser_identifier (parser);
26771 /* Issue an error message. */
26772 error_at (token->location,
26773 "named return values are no longer supported");
26774 /* Skip tokens until we reach the start of the function body. */
26775 while (true)
26776 {
26777 cp_token *token = cp_lexer_peek_token (parser->lexer);
26778 if (token->type == CPP_OPEN_BRACE
26779 || token->type == CPP_EOF
26780 || token->type == CPP_PRAGMA_EOL)
26781 break;
26782 cp_lexer_consume_token (parser->lexer);
26783 }
26784 }
26785 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26786 anything declared inside `f'. */
26787 saved_in_unbraced_linkage_specification_p
26788 = parser->in_unbraced_linkage_specification_p;
26789 parser->in_unbraced_linkage_specification_p = false;
26790 /* Inside the function, surrounding template-parameter-lists do not
26791 apply. */
26792 saved_num_template_parameter_lists
26793 = parser->num_template_parameter_lists;
26794 parser->num_template_parameter_lists = 0;
26795
26796 /* If the next token is `try', `__transaction_atomic', or
26797 `__transaction_relaxed`, then we are looking at either function-try-block
26798 or function-transaction-block. Note that all of these include the
26799 function-body. */
26800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26801 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26802 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26803 RID_TRANSACTION_RELAXED))
26804 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26805 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26806 cp_parser_function_try_block (parser);
26807 else
26808 cp_parser_ctor_initializer_opt_and_function_body
26809 (parser, /*in_function_try_block=*/false);
26810
26811 /* Finish the function. */
26812 fn = finish_function (inline_p);
26813 /* Generate code for it, if necessary. */
26814 expand_or_defer_fn (fn);
26815 /* Restore the saved values. */
26816 parser->in_unbraced_linkage_specification_p
26817 = saved_in_unbraced_linkage_specification_p;
26818 parser->num_template_parameter_lists
26819 = saved_num_template_parameter_lists;
26820 parser->in_function_body = saved_in_function_body;
26821
26822 parser->fully_implicit_function_template_p
26823 = fully_implicit_function_template_p;
26824 parser->implicit_template_parms
26825 = implicit_template_parms;
26826 parser->implicit_template_scope
26827 = implicit_template_scope;
26828
26829 if (parser->fully_implicit_function_template_p)
26830 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26831
26832 return fn;
26833 }
26834
26835 /* Parse a template-declaration body (following argument list). */
26836
26837 static void
26838 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26839 tree parameter_list,
26840 bool member_p)
26841 {
26842 tree decl = NULL_TREE;
26843 bool friend_p = false;
26844
26845 /* We just processed one more parameter list. */
26846 ++parser->num_template_parameter_lists;
26847
26848 /* Get the deferred access checks from the parameter list. These
26849 will be checked once we know what is being declared, as for a
26850 member template the checks must be performed in the scope of the
26851 class containing the member. */
26852 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26853
26854 /* Tentatively parse for a new template parameter list, which can either be
26855 the template keyword or a template introduction. */
26856 if (cp_parser_template_declaration_after_export (parser, member_p))
26857 /* OK */;
26858 else if (cxx_dialect >= cxx11
26859 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26860 decl = cp_parser_alias_declaration (parser);
26861 else
26862 {
26863 /* There are no access checks when parsing a template, as we do not
26864 know if a specialization will be a friend. */
26865 push_deferring_access_checks (dk_no_check);
26866 cp_token *token = cp_lexer_peek_token (parser->lexer);
26867 decl = cp_parser_single_declaration (parser,
26868 checks,
26869 member_p,
26870 /*explicit_specialization_p=*/false,
26871 &friend_p);
26872 pop_deferring_access_checks ();
26873
26874 /* If this is a member template declaration, let the front
26875 end know. */
26876 if (member_p && !friend_p && decl)
26877 {
26878 if (TREE_CODE (decl) == TYPE_DECL)
26879 cp_parser_check_access_in_redeclaration (decl, token->location);
26880
26881 decl = finish_member_template_decl (decl);
26882 }
26883 else if (friend_p && decl
26884 && DECL_DECLARES_TYPE_P (decl))
26885 make_friend_class (current_class_type, TREE_TYPE (decl),
26886 /*complain=*/true);
26887 }
26888 /* We are done with the current parameter list. */
26889 --parser->num_template_parameter_lists;
26890
26891 pop_deferring_access_checks ();
26892
26893 /* Finish up. */
26894 finish_template_decl (parameter_list);
26895
26896 /* Check the template arguments for a literal operator template. */
26897 if (decl
26898 && DECL_DECLARES_FUNCTION_P (decl)
26899 && UDLIT_OPER_P (DECL_NAME (decl)))
26900 {
26901 bool ok = true;
26902 if (parameter_list == NULL_TREE)
26903 ok = false;
26904 else
26905 {
26906 int num_parms = TREE_VEC_LENGTH (parameter_list);
26907 if (num_parms == 1)
26908 {
26909 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26910 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26911 if (TREE_TYPE (parm) != char_type_node
26912 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26913 ok = false;
26914 }
26915 else if (num_parms == 2 && cxx_dialect >= cxx14)
26916 {
26917 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26918 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26919 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26920 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26921 if (parm == error_mark_node
26922 || TREE_TYPE (parm) != TREE_TYPE (type)
26923 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26924 ok = false;
26925 }
26926 else
26927 ok = false;
26928 }
26929 if (!ok)
26930 {
26931 if (cxx_dialect >= cxx14)
26932 error ("literal operator template %qD has invalid parameter list."
26933 " Expected non-type template argument pack <char...>"
26934 " or <typename CharT, CharT...>",
26935 decl);
26936 else
26937 error ("literal operator template %qD has invalid parameter list."
26938 " Expected non-type template argument pack <char...>",
26939 decl);
26940 }
26941 }
26942
26943 /* Register member declarations. */
26944 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26945 finish_member_declaration (decl);
26946 /* If DECL is a function template, we must return to parse it later.
26947 (Even though there is no definition, there might be default
26948 arguments that need handling.) */
26949 if (member_p && decl
26950 && DECL_DECLARES_FUNCTION_P (decl))
26951 vec_safe_push (unparsed_funs_with_definitions, decl);
26952 }
26953
26954 /* Parse a template introduction header for a template-declaration. Returns
26955 false if tentative parse fails. */
26956
26957 static bool
26958 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26959 {
26960 cp_parser_parse_tentatively (parser);
26961
26962 tree saved_scope = parser->scope;
26963 tree saved_object_scope = parser->object_scope;
26964 tree saved_qualifying_scope = parser->qualifying_scope;
26965
26966 /* Look for the optional `::' operator. */
26967 cp_parser_global_scope_opt (parser,
26968 /*current_scope_valid_p=*/false);
26969 /* Look for the nested-name-specifier. */
26970 cp_parser_nested_name_specifier_opt (parser,
26971 /*typename_keyword_p=*/false,
26972 /*check_dependency_p=*/true,
26973 /*type_p=*/false,
26974 /*is_declaration=*/false);
26975
26976 cp_token *token = cp_lexer_peek_token (parser->lexer);
26977 tree concept_name = cp_parser_identifier (parser);
26978
26979 /* Look up the concept for which we will be matching
26980 template parameters. */
26981 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26982 token->location);
26983 parser->scope = saved_scope;
26984 parser->object_scope = saved_object_scope;
26985 parser->qualifying_scope = saved_qualifying_scope;
26986
26987 if (concept_name == error_mark_node)
26988 cp_parser_simulate_error (parser);
26989
26990 /* Look for opening brace for introduction. */
26991 matching_braces braces;
26992 braces.require_open (parser);
26993
26994 if (!cp_parser_parse_definitely (parser))
26995 return false;
26996
26997 push_deferring_access_checks (dk_deferred);
26998
26999 /* Build vector of placeholder parameters and grab
27000 matching identifiers. */
27001 tree introduction_list = cp_parser_introduction_list (parser);
27002
27003 /* The introduction-list shall not be empty. */
27004 int nargs = TREE_VEC_LENGTH (introduction_list);
27005 if (nargs == 0)
27006 {
27007 error ("empty introduction-list");
27008 return true;
27009 }
27010
27011 /* Look for closing brace for introduction. */
27012 if (!braces.require_close (parser))
27013 return true;
27014
27015 if (tmpl_decl == error_mark_node)
27016 {
27017 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27018 token->location);
27019 return true;
27020 }
27021
27022 /* Build and associate the constraint. */
27023 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27024 if (parms && parms != error_mark_node)
27025 {
27026 cp_parser_template_declaration_after_parameters (parser, parms,
27027 member_p);
27028 return true;
27029 }
27030
27031 error_at (token->location, "no matching concept for template-introduction");
27032 return true;
27033 }
27034
27035 /* Parse a normal template-declaration following the template keyword. */
27036
27037 static void
27038 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27039 {
27040 tree parameter_list;
27041 bool need_lang_pop;
27042 location_t location = input_location;
27043
27044 /* Look for the `<' token. */
27045 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27046 return;
27047 if (at_class_scope_p () && current_function_decl)
27048 {
27049 /* 14.5.2.2 [temp.mem]
27050
27051 A local class shall not have member templates. */
27052 error_at (location,
27053 "invalid declaration of member template in local class");
27054 cp_parser_skip_to_end_of_block_or_statement (parser);
27055 return;
27056 }
27057 /* [temp]
27058
27059 A template ... shall not have C linkage. */
27060 if (current_lang_name == lang_name_c)
27061 {
27062 error_at (location, "template with C linkage");
27063 maybe_show_extern_c_location ();
27064 /* Give it C++ linkage to avoid confusing other parts of the
27065 front end. */
27066 push_lang_context (lang_name_cplusplus);
27067 need_lang_pop = true;
27068 }
27069 else
27070 need_lang_pop = false;
27071
27072 /* We cannot perform access checks on the template parameter
27073 declarations until we know what is being declared, just as we
27074 cannot check the decl-specifier list. */
27075 push_deferring_access_checks (dk_deferred);
27076
27077 /* If the next token is `>', then we have an invalid
27078 specialization. Rather than complain about an invalid template
27079 parameter, issue an error message here. */
27080 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27081 {
27082 cp_parser_error (parser, "invalid explicit specialization");
27083 begin_specialization ();
27084 parameter_list = NULL_TREE;
27085 }
27086 else
27087 {
27088 /* Parse the template parameters. */
27089 parameter_list = cp_parser_template_parameter_list (parser);
27090 }
27091
27092 /* Look for the `>'. */
27093 cp_parser_skip_to_end_of_template_parameter_list (parser);
27094
27095 /* Manage template requirements */
27096 if (flag_concepts)
27097 {
27098 tree reqs = get_shorthand_constraints (current_template_parms);
27099 if (tree r = cp_parser_requires_clause_opt (parser))
27100 reqs = conjoin_constraints (reqs, normalize_expression (r));
27101 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27102 }
27103
27104 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27105 member_p);
27106
27107 /* For the erroneous case of a template with C linkage, we pushed an
27108 implicit C++ linkage scope; exit that scope now. */
27109 if (need_lang_pop)
27110 pop_lang_context ();
27111 }
27112
27113 /* Parse a template-declaration, assuming that the `export' (and
27114 `extern') keywords, if present, has already been scanned. MEMBER_P
27115 is as for cp_parser_template_declaration. */
27116
27117 static bool
27118 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27119 {
27120 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27121 {
27122 cp_lexer_consume_token (parser->lexer);
27123 cp_parser_explicit_template_declaration (parser, member_p);
27124 return true;
27125 }
27126 else if (flag_concepts)
27127 return cp_parser_template_introduction (parser, member_p);
27128
27129 return false;
27130 }
27131
27132 /* Perform the deferred access checks from a template-parameter-list.
27133 CHECKS is a TREE_LIST of access checks, as returned by
27134 get_deferred_access_checks. */
27135
27136 static void
27137 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27138 {
27139 ++processing_template_parmlist;
27140 perform_access_checks (checks, tf_warning_or_error);
27141 --processing_template_parmlist;
27142 }
27143
27144 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27145 `function-definition' sequence that follows a template header.
27146 If MEMBER_P is true, this declaration appears in a class scope.
27147
27148 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27149 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27150
27151 static tree
27152 cp_parser_single_declaration (cp_parser* parser,
27153 vec<deferred_access_check, va_gc> *checks,
27154 bool member_p,
27155 bool explicit_specialization_p,
27156 bool* friend_p)
27157 {
27158 int declares_class_or_enum;
27159 tree decl = NULL_TREE;
27160 cp_decl_specifier_seq decl_specifiers;
27161 bool function_definition_p = false;
27162 cp_token *decl_spec_token_start;
27163
27164 /* This function is only used when processing a template
27165 declaration. */
27166 gcc_assert (innermost_scope_kind () == sk_template_parms
27167 || innermost_scope_kind () == sk_template_spec);
27168
27169 /* Defer access checks until we know what is being declared. */
27170 push_deferring_access_checks (dk_deferred);
27171
27172 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27173 alternative. */
27174 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27175 cp_parser_decl_specifier_seq (parser,
27176 CP_PARSER_FLAGS_OPTIONAL,
27177 &decl_specifiers,
27178 &declares_class_or_enum);
27179 if (friend_p)
27180 *friend_p = cp_parser_friend_p (&decl_specifiers);
27181
27182 /* There are no template typedefs. */
27183 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27184 {
27185 error_at (decl_spec_token_start->location,
27186 "template declaration of %<typedef%>");
27187 decl = error_mark_node;
27188 }
27189
27190 /* Gather up the access checks that occurred the
27191 decl-specifier-seq. */
27192 stop_deferring_access_checks ();
27193
27194 /* Check for the declaration of a template class. */
27195 if (declares_class_or_enum)
27196 {
27197 if (cp_parser_declares_only_class_p (parser)
27198 || (declares_class_or_enum & 2))
27199 {
27200 // If this is a declaration, but not a definition, associate
27201 // any constraints with the type declaration. Constraints
27202 // are associated with definitions in cp_parser_class_specifier.
27203 if (declares_class_or_enum == 1)
27204 associate_classtype_constraints (decl_specifiers.type);
27205
27206 decl = shadow_tag (&decl_specifiers);
27207
27208 /* In this case:
27209
27210 struct C {
27211 friend template <typename T> struct A<T>::B;
27212 };
27213
27214 A<T>::B will be represented by a TYPENAME_TYPE, and
27215 therefore not recognized by shadow_tag. */
27216 if (friend_p && *friend_p
27217 && !decl
27218 && decl_specifiers.type
27219 && TYPE_P (decl_specifiers.type))
27220 decl = decl_specifiers.type;
27221
27222 if (decl && decl != error_mark_node)
27223 decl = TYPE_NAME (decl);
27224 else
27225 decl = error_mark_node;
27226
27227 /* Perform access checks for template parameters. */
27228 cp_parser_perform_template_parameter_access_checks (checks);
27229
27230 /* Give a helpful diagnostic for
27231 template <class T> struct A { } a;
27232 if we aren't already recovering from an error. */
27233 if (!cp_parser_declares_only_class_p (parser)
27234 && !seen_error ())
27235 {
27236 error_at (cp_lexer_peek_token (parser->lexer)->location,
27237 "a class template declaration must not declare "
27238 "anything else");
27239 cp_parser_skip_to_end_of_block_or_statement (parser);
27240 goto out;
27241 }
27242 }
27243 }
27244
27245 /* Complain about missing 'typename' or other invalid type names. */
27246 if (!decl_specifiers.any_type_specifiers_p
27247 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27248 {
27249 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27250 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27251 the rest of this declaration. */
27252 decl = error_mark_node;
27253 goto out;
27254 }
27255
27256 /* If it's not a template class, try for a template function. If
27257 the next token is a `;', then this declaration does not declare
27258 anything. But, if there were errors in the decl-specifiers, then
27259 the error might well have come from an attempted class-specifier.
27260 In that case, there's no need to warn about a missing declarator. */
27261 if (!decl
27262 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27263 || decl_specifiers.type != error_mark_node))
27264 {
27265 decl = cp_parser_init_declarator (parser,
27266 &decl_specifiers,
27267 checks,
27268 /*function_definition_allowed_p=*/true,
27269 member_p,
27270 declares_class_or_enum,
27271 &function_definition_p,
27272 NULL, NULL, NULL);
27273
27274 /* 7.1.1-1 [dcl.stc]
27275
27276 A storage-class-specifier shall not be specified in an explicit
27277 specialization... */
27278 if (decl
27279 && explicit_specialization_p
27280 && decl_specifiers.storage_class != sc_none)
27281 {
27282 error_at (decl_spec_token_start->location,
27283 "explicit template specialization cannot have a storage class");
27284 decl = error_mark_node;
27285 }
27286
27287 if (decl && VAR_P (decl))
27288 check_template_variable (decl);
27289 }
27290
27291 /* Look for a trailing `;' after the declaration. */
27292 if (!function_definition_p
27293 && (decl == error_mark_node
27294 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27295 cp_parser_skip_to_end_of_block_or_statement (parser);
27296
27297 out:
27298 pop_deferring_access_checks ();
27299
27300 /* Clear any current qualification; whatever comes next is the start
27301 of something new. */
27302 parser->scope = NULL_TREE;
27303 parser->qualifying_scope = NULL_TREE;
27304 parser->object_scope = NULL_TREE;
27305
27306 return decl;
27307 }
27308
27309 /* Parse a cast-expression that is not the operand of a unary "&". */
27310
27311 static cp_expr
27312 cp_parser_simple_cast_expression (cp_parser *parser)
27313 {
27314 return cp_parser_cast_expression (parser, /*address_p=*/false,
27315 /*cast_p=*/false, /*decltype*/false, NULL);
27316 }
27317
27318 /* Parse a functional cast to TYPE. Returns an expression
27319 representing the cast. */
27320
27321 static cp_expr
27322 cp_parser_functional_cast (cp_parser* parser, tree type)
27323 {
27324 vec<tree, va_gc> *vec;
27325 tree expression_list;
27326 cp_expr cast;
27327 bool nonconst_p;
27328
27329 location_t start_loc = input_location;
27330
27331 if (!type)
27332 type = error_mark_node;
27333
27334 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27335 {
27336 cp_lexer_set_source_position (parser->lexer);
27337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27338 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27339 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27340 if (TREE_CODE (type) == TYPE_DECL)
27341 type = TREE_TYPE (type);
27342
27343 cast = finish_compound_literal (type, expression_list,
27344 tf_warning_or_error, fcl_functional);
27345 /* Create a location of the form:
27346 type_name{i, f}
27347 ^~~~~~~~~~~~~~~
27348 with caret == start at the start of the type name,
27349 finishing at the closing brace. */
27350 location_t finish_loc
27351 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27352 location_t combined_loc = make_location (start_loc, start_loc,
27353 finish_loc);
27354 cast.set_location (combined_loc);
27355 return cast;
27356 }
27357
27358
27359 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27360 /*cast_p=*/true,
27361 /*allow_expansion_p=*/true,
27362 /*non_constant_p=*/NULL);
27363 if (vec == NULL)
27364 expression_list = error_mark_node;
27365 else
27366 {
27367 expression_list = build_tree_list_vec (vec);
27368 release_tree_vector (vec);
27369 }
27370
27371 cast = build_functional_cast (type, expression_list,
27372 tf_warning_or_error);
27373 /* [expr.const]/1: In an integral constant expression "only type
27374 conversions to integral or enumeration type can be used". */
27375 if (TREE_CODE (type) == TYPE_DECL)
27376 type = TREE_TYPE (type);
27377 if (cast != error_mark_node
27378 && !cast_valid_in_integral_constant_expression_p (type)
27379 && cp_parser_non_integral_constant_expression (parser,
27380 NIC_CONSTRUCTOR))
27381 return error_mark_node;
27382
27383 /* Create a location of the form:
27384 float(i)
27385 ^~~~~~~~
27386 with caret == start at the start of the type name,
27387 finishing at the closing paren. */
27388 location_t finish_loc
27389 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27390 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27391 cast.set_location (combined_loc);
27392 return cast;
27393 }
27394
27395 /* Save the tokens that make up the body of a member function defined
27396 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27397 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27398 specifiers applied to the declaration. Returns the FUNCTION_DECL
27399 for the member function. */
27400
27401 static tree
27402 cp_parser_save_member_function_body (cp_parser* parser,
27403 cp_decl_specifier_seq *decl_specifiers,
27404 cp_declarator *declarator,
27405 tree attributes)
27406 {
27407 cp_token *first;
27408 cp_token *last;
27409 tree fn;
27410 bool function_try_block = false;
27411
27412 /* Create the FUNCTION_DECL. */
27413 fn = grokmethod (decl_specifiers, declarator, attributes);
27414 cp_finalize_omp_declare_simd (parser, fn);
27415 cp_finalize_oacc_routine (parser, fn, true);
27416 /* If something went badly wrong, bail out now. */
27417 if (fn == error_mark_node)
27418 {
27419 /* If there's a function-body, skip it. */
27420 if (cp_parser_token_starts_function_definition_p
27421 (cp_lexer_peek_token (parser->lexer)))
27422 cp_parser_skip_to_end_of_block_or_statement (parser);
27423 return error_mark_node;
27424 }
27425
27426 /* Remember it, if there default args to post process. */
27427 cp_parser_save_default_args (parser, fn);
27428
27429 /* Save away the tokens that make up the body of the
27430 function. */
27431 first = parser->lexer->next_token;
27432
27433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27434 cp_lexer_consume_token (parser->lexer);
27435 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27436 RID_TRANSACTION_ATOMIC))
27437 {
27438 cp_lexer_consume_token (parser->lexer);
27439 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27441 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27442 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27443 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27444 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27445 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27446 {
27447 cp_lexer_consume_token (parser->lexer);
27448 cp_lexer_consume_token (parser->lexer);
27449 cp_lexer_consume_token (parser->lexer);
27450 cp_lexer_consume_token (parser->lexer);
27451 cp_lexer_consume_token (parser->lexer);
27452 }
27453 else
27454 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27455 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27456 {
27457 cp_lexer_consume_token (parser->lexer);
27458 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27459 break;
27460 }
27461 }
27462
27463 /* Handle function try blocks. */
27464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27465 {
27466 cp_lexer_consume_token (parser->lexer);
27467 function_try_block = true;
27468 }
27469 /* We can have braced-init-list mem-initializers before the fn body. */
27470 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27471 {
27472 cp_lexer_consume_token (parser->lexer);
27473 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27474 {
27475 /* cache_group will stop after an un-nested { } pair, too. */
27476 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27477 break;
27478
27479 /* variadic mem-inits have ... after the ')'. */
27480 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27481 cp_lexer_consume_token (parser->lexer);
27482 }
27483 }
27484 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27485 /* Handle function try blocks. */
27486 if (function_try_block)
27487 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27488 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27489 last = parser->lexer->next_token;
27490
27491 /* Save away the inline definition; we will process it when the
27492 class is complete. */
27493 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27494 DECL_PENDING_INLINE_P (fn) = 1;
27495
27496 /* We need to know that this was defined in the class, so that
27497 friend templates are handled correctly. */
27498 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27499
27500 /* Add FN to the queue of functions to be parsed later. */
27501 vec_safe_push (unparsed_funs_with_definitions, fn);
27502
27503 return fn;
27504 }
27505
27506 /* Save the tokens that make up the in-class initializer for a non-static
27507 data member. Returns a DEFAULT_ARG. */
27508
27509 static tree
27510 cp_parser_save_nsdmi (cp_parser* parser)
27511 {
27512 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27513 }
27514
27515 /* Parse a template-argument-list, as well as the trailing ">" (but
27516 not the opening "<"). See cp_parser_template_argument_list for the
27517 return value. */
27518
27519 static tree
27520 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27521 {
27522 tree arguments;
27523 tree saved_scope;
27524 tree saved_qualifying_scope;
27525 tree saved_object_scope;
27526 bool saved_greater_than_is_operator_p;
27527 int saved_unevaluated_operand;
27528 int saved_inhibit_evaluation_warnings;
27529
27530 /* [temp.names]
27531
27532 When parsing a template-id, the first non-nested `>' is taken as
27533 the end of the template-argument-list rather than a greater-than
27534 operator. */
27535 saved_greater_than_is_operator_p
27536 = parser->greater_than_is_operator_p;
27537 parser->greater_than_is_operator_p = false;
27538 /* Parsing the argument list may modify SCOPE, so we save it
27539 here. */
27540 saved_scope = parser->scope;
27541 saved_qualifying_scope = parser->qualifying_scope;
27542 saved_object_scope = parser->object_scope;
27543 /* We need to evaluate the template arguments, even though this
27544 template-id may be nested within a "sizeof". */
27545 saved_unevaluated_operand = cp_unevaluated_operand;
27546 cp_unevaluated_operand = 0;
27547 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27548 c_inhibit_evaluation_warnings = 0;
27549 /* Parse the template-argument-list itself. */
27550 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27551 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27552 arguments = NULL_TREE;
27553 else
27554 arguments = cp_parser_template_argument_list (parser);
27555 /* Look for the `>' that ends the template-argument-list. If we find
27556 a '>>' instead, it's probably just a typo. */
27557 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27558 {
27559 if (cxx_dialect != cxx98)
27560 {
27561 /* In C++0x, a `>>' in a template argument list or cast
27562 expression is considered to be two separate `>'
27563 tokens. So, change the current token to a `>', but don't
27564 consume it: it will be consumed later when the outer
27565 template argument list (or cast expression) is parsed.
27566 Note that this replacement of `>' for `>>' is necessary
27567 even if we are parsing tentatively: in the tentative
27568 case, after calling
27569 cp_parser_enclosed_template_argument_list we will always
27570 throw away all of the template arguments and the first
27571 closing `>', either because the template argument list
27572 was erroneous or because we are replacing those tokens
27573 with a CPP_TEMPLATE_ID token. The second `>' (which will
27574 not have been thrown away) is needed either to close an
27575 outer template argument list or to complete a new-style
27576 cast. */
27577 cp_token *token = cp_lexer_peek_token (parser->lexer);
27578 token->type = CPP_GREATER;
27579 }
27580 else if (!saved_greater_than_is_operator_p)
27581 {
27582 /* If we're in a nested template argument list, the '>>' has
27583 to be a typo for '> >'. We emit the error message, but we
27584 continue parsing and we push a '>' as next token, so that
27585 the argument list will be parsed correctly. Note that the
27586 global source location is still on the token before the
27587 '>>', so we need to say explicitly where we want it. */
27588 cp_token *token = cp_lexer_peek_token (parser->lexer);
27589 gcc_rich_location richloc (token->location);
27590 richloc.add_fixit_replace ("> >");
27591 error_at (&richloc, "%<>>%> should be %<> >%> "
27592 "within a nested template argument list");
27593
27594 token->type = CPP_GREATER;
27595 }
27596 else
27597 {
27598 /* If this is not a nested template argument list, the '>>'
27599 is a typo for '>'. Emit an error message and continue.
27600 Same deal about the token location, but here we can get it
27601 right by consuming the '>>' before issuing the diagnostic. */
27602 cp_token *token = cp_lexer_consume_token (parser->lexer);
27603 error_at (token->location,
27604 "spurious %<>>%>, use %<>%> to terminate "
27605 "a template argument list");
27606 }
27607 }
27608 else
27609 cp_parser_skip_to_end_of_template_parameter_list (parser);
27610 /* The `>' token might be a greater-than operator again now. */
27611 parser->greater_than_is_operator_p
27612 = saved_greater_than_is_operator_p;
27613 /* Restore the SAVED_SCOPE. */
27614 parser->scope = saved_scope;
27615 parser->qualifying_scope = saved_qualifying_scope;
27616 parser->object_scope = saved_object_scope;
27617 cp_unevaluated_operand = saved_unevaluated_operand;
27618 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27619
27620 return arguments;
27621 }
27622
27623 /* MEMBER_FUNCTION is a member function, or a friend. If default
27624 arguments, or the body of the function have not yet been parsed,
27625 parse them now. */
27626
27627 static void
27628 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27629 {
27630 timevar_push (TV_PARSE_INMETH);
27631 /* If this member is a template, get the underlying
27632 FUNCTION_DECL. */
27633 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27634 member_function = DECL_TEMPLATE_RESULT (member_function);
27635
27636 /* There should not be any class definitions in progress at this
27637 point; the bodies of members are only parsed outside of all class
27638 definitions. */
27639 gcc_assert (parser->num_classes_being_defined == 0);
27640 /* While we're parsing the member functions we might encounter more
27641 classes. We want to handle them right away, but we don't want
27642 them getting mixed up with functions that are currently in the
27643 queue. */
27644 push_unparsed_function_queues (parser);
27645
27646 /* Make sure that any template parameters are in scope. */
27647 maybe_begin_member_template_processing (member_function);
27648
27649 /* If the body of the function has not yet been parsed, parse it
27650 now. */
27651 if (DECL_PENDING_INLINE_P (member_function))
27652 {
27653 tree function_scope;
27654 cp_token_cache *tokens;
27655
27656 /* The function is no longer pending; we are processing it. */
27657 tokens = DECL_PENDING_INLINE_INFO (member_function);
27658 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27659 DECL_PENDING_INLINE_P (member_function) = 0;
27660
27661 /* If this is a local class, enter the scope of the containing
27662 function. */
27663 function_scope = current_function_decl;
27664 if (function_scope)
27665 push_function_context ();
27666
27667 /* Push the body of the function onto the lexer stack. */
27668 cp_parser_push_lexer_for_tokens (parser, tokens);
27669
27670 /* Let the front end know that we going to be defining this
27671 function. */
27672 start_preparsed_function (member_function, NULL_TREE,
27673 SF_PRE_PARSED | SF_INCLASS_INLINE);
27674
27675 /* Don't do access checking if it is a templated function. */
27676 if (processing_template_decl)
27677 push_deferring_access_checks (dk_no_check);
27678
27679 /* #pragma omp declare reduction needs special parsing. */
27680 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27681 {
27682 parser->lexer->in_pragma = true;
27683 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27684 finish_function (/*inline_p=*/true);
27685 cp_check_omp_declare_reduction (member_function);
27686 }
27687 else
27688 /* Now, parse the body of the function. */
27689 cp_parser_function_definition_after_declarator (parser,
27690 /*inline_p=*/true);
27691
27692 if (processing_template_decl)
27693 pop_deferring_access_checks ();
27694
27695 /* Leave the scope of the containing function. */
27696 if (function_scope)
27697 pop_function_context ();
27698 cp_parser_pop_lexer (parser);
27699 }
27700
27701 /* Remove any template parameters from the symbol table. */
27702 maybe_end_member_template_processing ();
27703
27704 /* Restore the queue. */
27705 pop_unparsed_function_queues (parser);
27706 timevar_pop (TV_PARSE_INMETH);
27707 }
27708
27709 /* If DECL contains any default args, remember it on the unparsed
27710 functions queue. */
27711
27712 static void
27713 cp_parser_save_default_args (cp_parser* parser, tree decl)
27714 {
27715 tree probe;
27716
27717 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27718 probe;
27719 probe = TREE_CHAIN (probe))
27720 if (TREE_PURPOSE (probe))
27721 {
27722 cp_default_arg_entry entry = {current_class_type, decl};
27723 vec_safe_push (unparsed_funs_with_default_args, entry);
27724 break;
27725 }
27726 }
27727
27728 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27729 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27730 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27731 from the parameter-type-list. */
27732
27733 static tree
27734 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27735 tree default_arg, tree parmtype)
27736 {
27737 cp_token_cache *tokens;
27738 tree parsed_arg;
27739 bool dummy;
27740
27741 if (default_arg == error_mark_node)
27742 return error_mark_node;
27743
27744 /* Push the saved tokens for the default argument onto the parser's
27745 lexer stack. */
27746 tokens = DEFARG_TOKENS (default_arg);
27747 cp_parser_push_lexer_for_tokens (parser, tokens);
27748
27749 start_lambda_scope (decl);
27750
27751 /* Parse the default argument. */
27752 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27753 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27755
27756 finish_lambda_scope ();
27757
27758 if (parsed_arg == error_mark_node)
27759 cp_parser_skip_to_end_of_statement (parser);
27760
27761 if (!processing_template_decl)
27762 {
27763 /* In a non-template class, check conversions now. In a template,
27764 we'll wait and instantiate these as needed. */
27765 if (TREE_CODE (decl) == PARM_DECL)
27766 parsed_arg = check_default_argument (parmtype, parsed_arg,
27767 tf_warning_or_error);
27768 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27769 parsed_arg = error_mark_node;
27770 else
27771 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27772 }
27773
27774 /* If the token stream has not been completely used up, then
27775 there was extra junk after the end of the default
27776 argument. */
27777 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27778 {
27779 if (TREE_CODE (decl) == PARM_DECL)
27780 cp_parser_error (parser, "expected %<,%>");
27781 else
27782 cp_parser_error (parser, "expected %<;%>");
27783 }
27784
27785 /* Revert to the main lexer. */
27786 cp_parser_pop_lexer (parser);
27787
27788 return parsed_arg;
27789 }
27790
27791 /* FIELD is a non-static data member with an initializer which we saved for
27792 later; parse it now. */
27793
27794 static void
27795 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27796 {
27797 tree def;
27798
27799 maybe_begin_member_template_processing (field);
27800
27801 push_unparsed_function_queues (parser);
27802 def = cp_parser_late_parse_one_default_arg (parser, field,
27803 DECL_INITIAL (field),
27804 NULL_TREE);
27805 pop_unparsed_function_queues (parser);
27806
27807 maybe_end_member_template_processing ();
27808
27809 DECL_INITIAL (field) = def;
27810 }
27811
27812 /* FN is a FUNCTION_DECL which may contains a parameter with an
27813 unparsed DEFAULT_ARG. Parse the default args now. This function
27814 assumes that the current scope is the scope in which the default
27815 argument should be processed. */
27816
27817 static void
27818 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27819 {
27820 bool saved_local_variables_forbidden_p;
27821 tree parm, parmdecl;
27822
27823 /* While we're parsing the default args, we might (due to the
27824 statement expression extension) encounter more classes. We want
27825 to handle them right away, but we don't want them getting mixed
27826 up with default args that are currently in the queue. */
27827 push_unparsed_function_queues (parser);
27828
27829 /* Local variable names (and the `this' keyword) may not appear
27830 in a default argument. */
27831 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27832 parser->local_variables_forbidden_p = true;
27833
27834 push_defarg_context (fn);
27835
27836 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27837 parmdecl = DECL_ARGUMENTS (fn);
27838 parm && parm != void_list_node;
27839 parm = TREE_CHAIN (parm),
27840 parmdecl = DECL_CHAIN (parmdecl))
27841 {
27842 tree default_arg = TREE_PURPOSE (parm);
27843 tree parsed_arg;
27844 vec<tree, va_gc> *insts;
27845 tree copy;
27846 unsigned ix;
27847
27848 if (!default_arg)
27849 continue;
27850
27851 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27852 /* This can happen for a friend declaration for a function
27853 already declared with default arguments. */
27854 continue;
27855
27856 parsed_arg
27857 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27858 default_arg,
27859 TREE_VALUE (parm));
27860 TREE_PURPOSE (parm) = parsed_arg;
27861
27862 /* Update any instantiations we've already created. */
27863 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27864 vec_safe_iterate (insts, ix, &copy); ix++)
27865 TREE_PURPOSE (copy) = parsed_arg;
27866 }
27867
27868 pop_defarg_context ();
27869
27870 /* Make sure no default arg is missing. */
27871 check_default_args (fn);
27872
27873 /* Restore the state of local_variables_forbidden_p. */
27874 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27875
27876 /* Restore the queue. */
27877 pop_unparsed_function_queues (parser);
27878 }
27879
27880 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27881
27882 sizeof ... ( identifier )
27883
27884 where the 'sizeof' token has already been consumed. */
27885
27886 static tree
27887 cp_parser_sizeof_pack (cp_parser *parser)
27888 {
27889 /* Consume the `...'. */
27890 cp_lexer_consume_token (parser->lexer);
27891 maybe_warn_variadic_templates ();
27892
27893 matching_parens parens;
27894 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27895 if (paren)
27896 parens.consume_open (parser);
27897 else
27898 permerror (cp_lexer_peek_token (parser->lexer)->location,
27899 "%<sizeof...%> argument must be surrounded by parentheses");
27900
27901 cp_token *token = cp_lexer_peek_token (parser->lexer);
27902 tree name = cp_parser_identifier (parser);
27903 if (name == error_mark_node)
27904 return error_mark_node;
27905 /* The name is not qualified. */
27906 parser->scope = NULL_TREE;
27907 parser->qualifying_scope = NULL_TREE;
27908 parser->object_scope = NULL_TREE;
27909 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27910 if (expr == error_mark_node)
27911 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27912 token->location);
27913 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27914 expr = TREE_TYPE (expr);
27915 else if (TREE_CODE (expr) == CONST_DECL)
27916 expr = DECL_INITIAL (expr);
27917 expr = make_pack_expansion (expr);
27918 PACK_EXPANSION_SIZEOF_P (expr) = true;
27919
27920 if (paren)
27921 parens.require_close (parser);
27922
27923 return expr;
27924 }
27925
27926 /* Parse the operand of `sizeof' (or a similar operator). Returns
27927 either a TYPE or an expression, depending on the form of the
27928 input. The KEYWORD indicates which kind of expression we have
27929 encountered. */
27930
27931 static tree
27932 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27933 {
27934 tree expr = NULL_TREE;
27935 const char *saved_message;
27936 char *tmp;
27937 bool saved_integral_constant_expression_p;
27938 bool saved_non_integral_constant_expression_p;
27939
27940 /* If it's a `...', then we are computing the length of a parameter
27941 pack. */
27942 if (keyword == RID_SIZEOF
27943 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27944 return cp_parser_sizeof_pack (parser);
27945
27946 /* Types cannot be defined in a `sizeof' expression. Save away the
27947 old message. */
27948 saved_message = parser->type_definition_forbidden_message;
27949 /* And create the new one. */
27950 tmp = concat ("types may not be defined in %<",
27951 IDENTIFIER_POINTER (ridpointers[keyword]),
27952 "%> expressions", NULL);
27953 parser->type_definition_forbidden_message = tmp;
27954
27955 /* The restrictions on constant-expressions do not apply inside
27956 sizeof expressions. */
27957 saved_integral_constant_expression_p
27958 = parser->integral_constant_expression_p;
27959 saved_non_integral_constant_expression_p
27960 = parser->non_integral_constant_expression_p;
27961 parser->integral_constant_expression_p = false;
27962
27963 /* Do not actually evaluate the expression. */
27964 ++cp_unevaluated_operand;
27965 ++c_inhibit_evaluation_warnings;
27966 /* If it's a `(', then we might be looking at the type-id
27967 construction. */
27968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27969 {
27970 tree type = NULL_TREE;
27971
27972 /* We can't be sure yet whether we're looking at a type-id or an
27973 expression. */
27974 cp_parser_parse_tentatively (parser);
27975
27976 matching_parens parens;
27977 parens.consume_open (parser);
27978
27979 /* Note: as a GNU Extension, compound literals are considered
27980 postfix-expressions as they are in C99, so they are valid
27981 arguments to sizeof. See comment in cp_parser_cast_expression
27982 for details. */
27983 if (cp_parser_compound_literal_p (parser))
27984 cp_parser_simulate_error (parser);
27985 else
27986 {
27987 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27988 parser->in_type_id_in_expr_p = true;
27989 /* Look for the type-id. */
27990 type = cp_parser_type_id (parser);
27991 /* Look for the closing `)'. */
27992 parens.require_close (parser);
27993 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27994 }
27995
27996 /* If all went well, then we're done. */
27997 if (cp_parser_parse_definitely (parser))
27998 {
27999 cp_decl_specifier_seq decl_specs;
28000
28001 /* Build a trivial decl-specifier-seq. */
28002 clear_decl_specs (&decl_specs);
28003 decl_specs.type = type;
28004
28005 /* Call grokdeclarator to figure out what type this is. */
28006 expr = grokdeclarator (NULL,
28007 &decl_specs,
28008 TYPENAME,
28009 /*initialized=*/0,
28010 /*attrlist=*/NULL);
28011 }
28012 }
28013
28014 /* If the type-id production did not work out, then we must be
28015 looking at the unary-expression production. */
28016 if (!expr)
28017 expr = cp_parser_unary_expression (parser);
28018
28019 /* Go back to evaluating expressions. */
28020 --cp_unevaluated_operand;
28021 --c_inhibit_evaluation_warnings;
28022
28023 /* Free the message we created. */
28024 free (tmp);
28025 /* And restore the old one. */
28026 parser->type_definition_forbidden_message = saved_message;
28027 parser->integral_constant_expression_p
28028 = saved_integral_constant_expression_p;
28029 parser->non_integral_constant_expression_p
28030 = saved_non_integral_constant_expression_p;
28031
28032 return expr;
28033 }
28034
28035 /* If the current declaration has no declarator, return true. */
28036
28037 static bool
28038 cp_parser_declares_only_class_p (cp_parser *parser)
28039 {
28040 /* If the next token is a `;' or a `,' then there is no
28041 declarator. */
28042 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28043 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28044 }
28045
28046 /* Update the DECL_SPECS to reflect the storage class indicated by
28047 KEYWORD. */
28048
28049 static void
28050 cp_parser_set_storage_class (cp_parser *parser,
28051 cp_decl_specifier_seq *decl_specs,
28052 enum rid keyword,
28053 cp_token *token)
28054 {
28055 cp_storage_class storage_class;
28056
28057 if (parser->in_unbraced_linkage_specification_p)
28058 {
28059 error_at (token->location, "invalid use of %qD in linkage specification",
28060 ridpointers[keyword]);
28061 return;
28062 }
28063 else if (decl_specs->storage_class != sc_none)
28064 {
28065 decl_specs->conflicting_specifiers_p = true;
28066 return;
28067 }
28068
28069 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28070 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28071 && decl_specs->gnu_thread_keyword_p)
28072 {
28073 pedwarn (decl_specs->locations[ds_thread], 0,
28074 "%<__thread%> before %qD", ridpointers[keyword]);
28075 }
28076
28077 switch (keyword)
28078 {
28079 case RID_AUTO:
28080 storage_class = sc_auto;
28081 break;
28082 case RID_REGISTER:
28083 storage_class = sc_register;
28084 break;
28085 case RID_STATIC:
28086 storage_class = sc_static;
28087 break;
28088 case RID_EXTERN:
28089 storage_class = sc_extern;
28090 break;
28091 case RID_MUTABLE:
28092 storage_class = sc_mutable;
28093 break;
28094 default:
28095 gcc_unreachable ();
28096 }
28097 decl_specs->storage_class = storage_class;
28098 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28099
28100 /* A storage class specifier cannot be applied alongside a typedef
28101 specifier. If there is a typedef specifier present then set
28102 conflicting_specifiers_p which will trigger an error later
28103 on in grokdeclarator. */
28104 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28105 decl_specs->conflicting_specifiers_p = true;
28106 }
28107
28108 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28109 is true, the type is a class or enum definition. */
28110
28111 static void
28112 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28113 tree type_spec,
28114 cp_token *token,
28115 bool type_definition_p)
28116 {
28117 decl_specs->any_specifiers_p = true;
28118
28119 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28120 (with, for example, in "typedef int wchar_t;") we remember that
28121 this is what happened. In system headers, we ignore these
28122 declarations so that G++ can work with system headers that are not
28123 C++-safe. */
28124 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28125 && !type_definition_p
28126 && (type_spec == boolean_type_node
28127 || type_spec == char16_type_node
28128 || type_spec == char32_type_node
28129 || type_spec == wchar_type_node)
28130 && (decl_specs->type
28131 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28132 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28133 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28134 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28135 {
28136 decl_specs->redefined_builtin_type = type_spec;
28137 set_and_check_decl_spec_loc (decl_specs,
28138 ds_redefined_builtin_type_spec,
28139 token);
28140 if (!decl_specs->type)
28141 {
28142 decl_specs->type = type_spec;
28143 decl_specs->type_definition_p = false;
28144 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28145 }
28146 }
28147 else if (decl_specs->type)
28148 decl_specs->multiple_types_p = true;
28149 else
28150 {
28151 decl_specs->type = type_spec;
28152 decl_specs->type_definition_p = type_definition_p;
28153 decl_specs->redefined_builtin_type = NULL_TREE;
28154 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28155 }
28156 }
28157
28158 /* True iff TOKEN is the GNU keyword __thread. */
28159
28160 static bool
28161 token_is__thread (cp_token *token)
28162 {
28163 gcc_assert (token->keyword == RID_THREAD);
28164 return id_equal (token->u.value, "__thread");
28165 }
28166
28167 /* Set the location for a declarator specifier and check if it is
28168 duplicated.
28169
28170 DECL_SPECS is the sequence of declarator specifiers onto which to
28171 set the location.
28172
28173 DS is the single declarator specifier to set which location is to
28174 be set onto the existing sequence of declarators.
28175
28176 LOCATION is the location for the declarator specifier to
28177 consider. */
28178
28179 static void
28180 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28181 cp_decl_spec ds, cp_token *token)
28182 {
28183 gcc_assert (ds < ds_last);
28184
28185 if (decl_specs == NULL)
28186 return;
28187
28188 source_location location = token->location;
28189
28190 if (decl_specs->locations[ds] == 0)
28191 {
28192 decl_specs->locations[ds] = location;
28193 if (ds == ds_thread)
28194 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28195 }
28196 else
28197 {
28198 if (ds == ds_long)
28199 {
28200 if (decl_specs->locations[ds_long_long] != 0)
28201 error_at (location,
28202 "%<long long long%> is too long for GCC");
28203 else
28204 {
28205 decl_specs->locations[ds_long_long] = location;
28206 pedwarn_cxx98 (location,
28207 OPT_Wlong_long,
28208 "ISO C++ 1998 does not support %<long long%>");
28209 }
28210 }
28211 else if (ds == ds_thread)
28212 {
28213 bool gnu = token_is__thread (token);
28214 if (gnu != decl_specs->gnu_thread_keyword_p)
28215 error_at (location,
28216 "both %<__thread%> and %<thread_local%> specified");
28217 else
28218 {
28219 gcc_rich_location richloc (location);
28220 richloc.add_fixit_remove ();
28221 error_at (&richloc, "duplicate %qD", token->u.value);
28222 }
28223 }
28224 else
28225 {
28226 static const char *const decl_spec_names[] = {
28227 "signed",
28228 "unsigned",
28229 "short",
28230 "long",
28231 "const",
28232 "volatile",
28233 "restrict",
28234 "inline",
28235 "virtual",
28236 "explicit",
28237 "friend",
28238 "typedef",
28239 "using",
28240 "constexpr",
28241 "__complex"
28242 };
28243 gcc_rich_location richloc (location);
28244 richloc.add_fixit_remove ();
28245 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28246 }
28247 }
28248 }
28249
28250 /* Return true iff the declarator specifier DS is present in the
28251 sequence of declarator specifiers DECL_SPECS. */
28252
28253 bool
28254 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28255 cp_decl_spec ds)
28256 {
28257 gcc_assert (ds < ds_last);
28258
28259 if (decl_specs == NULL)
28260 return false;
28261
28262 return decl_specs->locations[ds] != 0;
28263 }
28264
28265 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28266 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28267
28268 static bool
28269 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28270 {
28271 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28272 }
28273
28274 /* Issue an error message indicating that TOKEN_DESC was expected.
28275 If KEYWORD is true, it indicated this function is called by
28276 cp_parser_require_keword and the required token can only be
28277 a indicated keyword.
28278
28279 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28280 within any error as the location of an "opening" token matching
28281 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28282 RT_CLOSE_PAREN). */
28283
28284 static void
28285 cp_parser_required_error (cp_parser *parser,
28286 required_token token_desc,
28287 bool keyword,
28288 location_t matching_location)
28289 {
28290 if (cp_parser_simulate_error (parser))
28291 return;
28292
28293 const char *gmsgid = NULL;
28294 switch (token_desc)
28295 {
28296 case RT_NEW:
28297 gmsgid = G_("expected %<new%>");
28298 break;
28299 case RT_DELETE:
28300 gmsgid = G_("expected %<delete%>");
28301 break;
28302 case RT_RETURN:
28303 gmsgid = G_("expected %<return%>");
28304 break;
28305 case RT_WHILE:
28306 gmsgid = G_("expected %<while%>");
28307 break;
28308 case RT_EXTERN:
28309 gmsgid = G_("expected %<extern%>");
28310 break;
28311 case RT_STATIC_ASSERT:
28312 gmsgid = G_("expected %<static_assert%>");
28313 break;
28314 case RT_DECLTYPE:
28315 gmsgid = G_("expected %<decltype%>");
28316 break;
28317 case RT_OPERATOR:
28318 gmsgid = G_("expected %<operator%>");
28319 break;
28320 case RT_CLASS:
28321 gmsgid = G_("expected %<class%>");
28322 break;
28323 case RT_TEMPLATE:
28324 gmsgid = G_("expected %<template%>");
28325 break;
28326 case RT_NAMESPACE:
28327 gmsgid = G_("expected %<namespace%>");
28328 break;
28329 case RT_USING:
28330 gmsgid = G_("expected %<using%>");
28331 break;
28332 case RT_ASM:
28333 gmsgid = G_("expected %<asm%>");
28334 break;
28335 case RT_TRY:
28336 gmsgid = G_("expected %<try%>");
28337 break;
28338 case RT_CATCH:
28339 gmsgid = G_("expected %<catch%>");
28340 break;
28341 case RT_THROW:
28342 gmsgid = G_("expected %<throw%>");
28343 break;
28344 case RT_LABEL:
28345 gmsgid = G_("expected %<__label__%>");
28346 break;
28347 case RT_AT_TRY:
28348 gmsgid = G_("expected %<@try%>");
28349 break;
28350 case RT_AT_SYNCHRONIZED:
28351 gmsgid = G_("expected %<@synchronized%>");
28352 break;
28353 case RT_AT_THROW:
28354 gmsgid = G_("expected %<@throw%>");
28355 break;
28356 case RT_TRANSACTION_ATOMIC:
28357 gmsgid = G_("expected %<__transaction_atomic%>");
28358 break;
28359 case RT_TRANSACTION_RELAXED:
28360 gmsgid = G_("expected %<__transaction_relaxed%>");
28361 break;
28362 default:
28363 break;
28364 }
28365
28366 if (!gmsgid && !keyword)
28367 {
28368 switch (token_desc)
28369 {
28370 case RT_SEMICOLON:
28371 gmsgid = G_("expected %<;%>");
28372 break;
28373 case RT_OPEN_PAREN:
28374 gmsgid = G_("expected %<(%>");
28375 break;
28376 case RT_CLOSE_BRACE:
28377 gmsgid = G_("expected %<}%>");
28378 break;
28379 case RT_OPEN_BRACE:
28380 gmsgid = G_("expected %<{%>");
28381 break;
28382 case RT_CLOSE_SQUARE:
28383 gmsgid = G_("expected %<]%>");
28384 break;
28385 case RT_OPEN_SQUARE:
28386 gmsgid = G_("expected %<[%>");
28387 break;
28388 case RT_COMMA:
28389 gmsgid = G_("expected %<,%>");
28390 break;
28391 case RT_SCOPE:
28392 gmsgid = G_("expected %<::%>");
28393 break;
28394 case RT_LESS:
28395 gmsgid = G_("expected %<<%>");
28396 break;
28397 case RT_GREATER:
28398 gmsgid = G_("expected %<>%>");
28399 break;
28400 case RT_EQ:
28401 gmsgid = G_("expected %<=%>");
28402 break;
28403 case RT_ELLIPSIS:
28404 gmsgid = G_("expected %<...%>");
28405 break;
28406 case RT_MULT:
28407 gmsgid = G_("expected %<*%>");
28408 break;
28409 case RT_COMPL:
28410 gmsgid = G_("expected %<~%>");
28411 break;
28412 case RT_COLON:
28413 gmsgid = G_("expected %<:%>");
28414 break;
28415 case RT_COLON_SCOPE:
28416 gmsgid = G_("expected %<:%> or %<::%>");
28417 break;
28418 case RT_CLOSE_PAREN:
28419 gmsgid = G_("expected %<)%>");
28420 break;
28421 case RT_COMMA_CLOSE_PAREN:
28422 gmsgid = G_("expected %<,%> or %<)%>");
28423 break;
28424 case RT_PRAGMA_EOL:
28425 gmsgid = G_("expected end of line");
28426 break;
28427 case RT_NAME:
28428 gmsgid = G_("expected identifier");
28429 break;
28430 case RT_SELECT:
28431 gmsgid = G_("expected selection-statement");
28432 break;
28433 case RT_ITERATION:
28434 gmsgid = G_("expected iteration-statement");
28435 break;
28436 case RT_JUMP:
28437 gmsgid = G_("expected jump-statement");
28438 break;
28439 case RT_CLASS_KEY:
28440 gmsgid = G_("expected class-key");
28441 break;
28442 case RT_CLASS_TYPENAME_TEMPLATE:
28443 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28444 break;
28445 default:
28446 gcc_unreachable ();
28447 }
28448 }
28449
28450 if (gmsgid)
28451 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28452 }
28453
28454
28455 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28456 issue an error message indicating that TOKEN_DESC was expected.
28457
28458 Returns the token consumed, if the token had the appropriate type.
28459 Otherwise, returns NULL.
28460
28461 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28462 within any error as the location of an "opening" token matching
28463 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28464 RT_CLOSE_PAREN). */
28465
28466 static cp_token *
28467 cp_parser_require (cp_parser* parser,
28468 enum cpp_ttype type,
28469 required_token token_desc,
28470 location_t matching_location)
28471 {
28472 if (cp_lexer_next_token_is (parser->lexer, type))
28473 return cp_lexer_consume_token (parser->lexer);
28474 else
28475 {
28476 /* Output the MESSAGE -- unless we're parsing tentatively. */
28477 if (!cp_parser_simulate_error (parser))
28478 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28479 matching_location);
28480 return NULL;
28481 }
28482 }
28483
28484 /* An error message is produced if the next token is not '>'.
28485 All further tokens are skipped until the desired token is
28486 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28487
28488 static void
28489 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28490 {
28491 /* Current level of '< ... >'. */
28492 unsigned level = 0;
28493 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28494 unsigned nesting_depth = 0;
28495
28496 /* Are we ready, yet? If not, issue error message. */
28497 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28498 return;
28499
28500 /* Skip tokens until the desired token is found. */
28501 while (true)
28502 {
28503 /* Peek at the next token. */
28504 switch (cp_lexer_peek_token (parser->lexer)->type)
28505 {
28506 case CPP_LESS:
28507 if (!nesting_depth)
28508 ++level;
28509 break;
28510
28511 case CPP_RSHIFT:
28512 if (cxx_dialect == cxx98)
28513 /* C++0x views the `>>' operator as two `>' tokens, but
28514 C++98 does not. */
28515 break;
28516 else if (!nesting_depth && level-- == 0)
28517 {
28518 /* We've hit a `>>' where the first `>' closes the
28519 template argument list, and the second `>' is
28520 spurious. Just consume the `>>' and stop; we've
28521 already produced at least one error. */
28522 cp_lexer_consume_token (parser->lexer);
28523 return;
28524 }
28525 /* Fall through for C++0x, so we handle the second `>' in
28526 the `>>'. */
28527 gcc_fallthrough ();
28528
28529 case CPP_GREATER:
28530 if (!nesting_depth && level-- == 0)
28531 {
28532 /* We've reached the token we want, consume it and stop. */
28533 cp_lexer_consume_token (parser->lexer);
28534 return;
28535 }
28536 break;
28537
28538 case CPP_OPEN_PAREN:
28539 case CPP_OPEN_SQUARE:
28540 ++nesting_depth;
28541 break;
28542
28543 case CPP_CLOSE_PAREN:
28544 case CPP_CLOSE_SQUARE:
28545 if (nesting_depth-- == 0)
28546 return;
28547 break;
28548
28549 case CPP_EOF:
28550 case CPP_PRAGMA_EOL:
28551 case CPP_SEMICOLON:
28552 case CPP_OPEN_BRACE:
28553 case CPP_CLOSE_BRACE:
28554 /* The '>' was probably forgotten, don't look further. */
28555 return;
28556
28557 default:
28558 break;
28559 }
28560
28561 /* Consume this token. */
28562 cp_lexer_consume_token (parser->lexer);
28563 }
28564 }
28565
28566 /* If the next token is the indicated keyword, consume it. Otherwise,
28567 issue an error message indicating that TOKEN_DESC was expected.
28568
28569 Returns the token consumed, if the token had the appropriate type.
28570 Otherwise, returns NULL. */
28571
28572 static cp_token *
28573 cp_parser_require_keyword (cp_parser* parser,
28574 enum rid keyword,
28575 required_token token_desc)
28576 {
28577 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28578
28579 if (token && token->keyword != keyword)
28580 {
28581 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28582 UNKNOWN_LOCATION);
28583 return NULL;
28584 }
28585
28586 return token;
28587 }
28588
28589 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28590 function-definition. */
28591
28592 static bool
28593 cp_parser_token_starts_function_definition_p (cp_token* token)
28594 {
28595 return (/* An ordinary function-body begins with an `{'. */
28596 token->type == CPP_OPEN_BRACE
28597 /* A ctor-initializer begins with a `:'. */
28598 || token->type == CPP_COLON
28599 /* A function-try-block begins with `try'. */
28600 || token->keyword == RID_TRY
28601 /* A function-transaction-block begins with `__transaction_atomic'
28602 or `__transaction_relaxed'. */
28603 || token->keyword == RID_TRANSACTION_ATOMIC
28604 || token->keyword == RID_TRANSACTION_RELAXED
28605 /* The named return value extension begins with `return'. */
28606 || token->keyword == RID_RETURN);
28607 }
28608
28609 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28610 definition. */
28611
28612 static bool
28613 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28614 {
28615 cp_token *token;
28616
28617 token = cp_lexer_peek_token (parser->lexer);
28618 return (token->type == CPP_OPEN_BRACE
28619 || (token->type == CPP_COLON
28620 && !parser->colon_doesnt_start_class_def_p));
28621 }
28622
28623 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28624 C++0x) ending a template-argument. */
28625
28626 static bool
28627 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28628 {
28629 cp_token *token;
28630
28631 token = cp_lexer_peek_token (parser->lexer);
28632 return (token->type == CPP_COMMA
28633 || token->type == CPP_GREATER
28634 || token->type == CPP_ELLIPSIS
28635 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28636 }
28637
28638 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28639 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28640
28641 static bool
28642 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28643 size_t n)
28644 {
28645 cp_token *token;
28646
28647 token = cp_lexer_peek_nth_token (parser->lexer, n);
28648 if (token->type == CPP_LESS)
28649 return true;
28650 /* Check for the sequence `<::' in the original code. It would be lexed as
28651 `[:', where `[' is a digraph, and there is no whitespace before
28652 `:'. */
28653 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28654 {
28655 cp_token *token2;
28656 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28657 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28658 return true;
28659 }
28660 return false;
28661 }
28662
28663 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28664 or none_type otherwise. */
28665
28666 static enum tag_types
28667 cp_parser_token_is_class_key (cp_token* token)
28668 {
28669 switch (token->keyword)
28670 {
28671 case RID_CLASS:
28672 return class_type;
28673 case RID_STRUCT:
28674 return record_type;
28675 case RID_UNION:
28676 return union_type;
28677
28678 default:
28679 return none_type;
28680 }
28681 }
28682
28683 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28684 or none_type otherwise or if the token is null. */
28685
28686 static enum tag_types
28687 cp_parser_token_is_type_parameter_key (cp_token* token)
28688 {
28689 if (!token)
28690 return none_type;
28691
28692 switch (token->keyword)
28693 {
28694 case RID_CLASS:
28695 return class_type;
28696 case RID_TYPENAME:
28697 return typename_type;
28698
28699 default:
28700 return none_type;
28701 }
28702 }
28703
28704 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28705
28706 static void
28707 cp_parser_check_class_key (enum tag_types class_key, tree type)
28708 {
28709 if (type == error_mark_node)
28710 return;
28711 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28712 {
28713 if (permerror (input_location, "%qs tag used in naming %q#T",
28714 class_key == union_type ? "union"
28715 : class_key == record_type ? "struct" : "class",
28716 type))
28717 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28718 "%q#T was previously declared here", type);
28719 }
28720 }
28721
28722 /* Issue an error message if DECL is redeclared with different
28723 access than its original declaration [class.access.spec/3].
28724 This applies to nested classes, nested class templates and
28725 enumerations [class.mem/1]. */
28726
28727 static void
28728 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28729 {
28730 if (!decl
28731 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28732 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28733 return;
28734
28735 if ((TREE_PRIVATE (decl)
28736 != (current_access_specifier == access_private_node))
28737 || (TREE_PROTECTED (decl)
28738 != (current_access_specifier == access_protected_node)))
28739 error_at (location, "%qD redeclared with different access", decl);
28740 }
28741
28742 /* Look for the `template' keyword, as a syntactic disambiguator.
28743 Return TRUE iff it is present, in which case it will be
28744 consumed. */
28745
28746 static bool
28747 cp_parser_optional_template_keyword (cp_parser *parser)
28748 {
28749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28750 {
28751 /* In C++98 the `template' keyword can only be used within templates;
28752 outside templates the parser can always figure out what is a
28753 template and what is not. In C++11, per the resolution of DR 468,
28754 `template' is allowed in cases where it is not strictly necessary. */
28755 if (!processing_template_decl
28756 && pedantic && cxx_dialect == cxx98)
28757 {
28758 cp_token *token = cp_lexer_peek_token (parser->lexer);
28759 pedwarn (token->location, OPT_Wpedantic,
28760 "in C++98 %<template%> (as a disambiguator) is only "
28761 "allowed within templates");
28762 /* If this part of the token stream is rescanned, the same
28763 error message would be generated. So, we purge the token
28764 from the stream. */
28765 cp_lexer_purge_token (parser->lexer);
28766 return false;
28767 }
28768 else
28769 {
28770 /* Consume the `template' keyword. */
28771 cp_lexer_consume_token (parser->lexer);
28772 return true;
28773 }
28774 }
28775 return false;
28776 }
28777
28778 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28779 set PARSER->SCOPE, and perform other related actions. */
28780
28781 static void
28782 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28783 {
28784 struct tree_check *check_value;
28785
28786 /* Get the stored value. */
28787 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28788 /* Set the scope from the stored value. */
28789 parser->scope = saved_checks_value (check_value);
28790 parser->qualifying_scope = check_value->qualifying_scope;
28791 parser->object_scope = NULL_TREE;
28792 }
28793
28794 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28795 encounter the end of a block before what we were looking for. */
28796
28797 static bool
28798 cp_parser_cache_group (cp_parser *parser,
28799 enum cpp_ttype end,
28800 unsigned depth)
28801 {
28802 while (true)
28803 {
28804 cp_token *token = cp_lexer_peek_token (parser->lexer);
28805
28806 /* Abort a parenthesized expression if we encounter a semicolon. */
28807 if ((end == CPP_CLOSE_PAREN || depth == 0)
28808 && token->type == CPP_SEMICOLON)
28809 return true;
28810 /* If we've reached the end of the file, stop. */
28811 if (token->type == CPP_EOF
28812 || (end != CPP_PRAGMA_EOL
28813 && token->type == CPP_PRAGMA_EOL))
28814 return true;
28815 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28816 /* We've hit the end of an enclosing block, so there's been some
28817 kind of syntax error. */
28818 return true;
28819
28820 /* Consume the token. */
28821 cp_lexer_consume_token (parser->lexer);
28822 /* See if it starts a new group. */
28823 if (token->type == CPP_OPEN_BRACE)
28824 {
28825 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28826 /* In theory this should probably check end == '}', but
28827 cp_parser_save_member_function_body needs it to exit
28828 after either '}' or ')' when called with ')'. */
28829 if (depth == 0)
28830 return false;
28831 }
28832 else if (token->type == CPP_OPEN_PAREN)
28833 {
28834 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28835 if (depth == 0 && end == CPP_CLOSE_PAREN)
28836 return false;
28837 }
28838 else if (token->type == CPP_PRAGMA)
28839 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28840 else if (token->type == end)
28841 return false;
28842 }
28843 }
28844
28845 /* Like above, for caching a default argument or NSDMI. Both of these are
28846 terminated by a non-nested comma, but it can be unclear whether or not a
28847 comma is nested in a template argument list unless we do more parsing.
28848 In order to handle this ambiguity, when we encounter a ',' after a '<'
28849 we try to parse what follows as a parameter-declaration-list (in the
28850 case of a default argument) or a member-declarator (in the case of an
28851 NSDMI). If that succeeds, then we stop caching. */
28852
28853 static tree
28854 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28855 {
28856 unsigned depth = 0;
28857 int maybe_template_id = 0;
28858 cp_token *first_token;
28859 cp_token *token;
28860 tree default_argument;
28861
28862 /* Add tokens until we have processed the entire default
28863 argument. We add the range [first_token, token). */
28864 first_token = cp_lexer_peek_token (parser->lexer);
28865 if (first_token->type == CPP_OPEN_BRACE)
28866 {
28867 /* For list-initialization, this is straightforward. */
28868 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28869 token = cp_lexer_peek_token (parser->lexer);
28870 }
28871 else while (true)
28872 {
28873 bool done = false;
28874
28875 /* Peek at the next token. */
28876 token = cp_lexer_peek_token (parser->lexer);
28877 /* What we do depends on what token we have. */
28878 switch (token->type)
28879 {
28880 /* In valid code, a default argument must be
28881 immediately followed by a `,' `)', or `...'. */
28882 case CPP_COMMA:
28883 if (depth == 0 && maybe_template_id)
28884 {
28885 /* If we've seen a '<', we might be in a
28886 template-argument-list. Until Core issue 325 is
28887 resolved, we don't know how this situation ought
28888 to be handled, so try to DTRT. We check whether
28889 what comes after the comma is a valid parameter
28890 declaration list. If it is, then the comma ends
28891 the default argument; otherwise the default
28892 argument continues. */
28893 bool error = false;
28894 cp_token *peek;
28895
28896 /* Set ITALP so cp_parser_parameter_declaration_list
28897 doesn't decide to commit to this parse. */
28898 bool saved_italp = parser->in_template_argument_list_p;
28899 parser->in_template_argument_list_p = true;
28900
28901 cp_parser_parse_tentatively (parser);
28902
28903 if (nsdmi)
28904 {
28905 /* Parse declarators until we reach a non-comma or
28906 somthing that cannot be an initializer.
28907 Just checking whether we're looking at a single
28908 declarator is insufficient. Consider:
28909 int var = tuple<T,U>::x;
28910 The template parameter 'U' looks exactly like a
28911 declarator. */
28912 do
28913 {
28914 int ctor_dtor_or_conv_p;
28915 cp_lexer_consume_token (parser->lexer);
28916 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28917 &ctor_dtor_or_conv_p,
28918 /*parenthesized_p=*/NULL,
28919 /*member_p=*/true,
28920 /*friend_p=*/false);
28921 peek = cp_lexer_peek_token (parser->lexer);
28922 if (cp_parser_error_occurred (parser))
28923 break;
28924 }
28925 while (peek->type == CPP_COMMA);
28926 /* If we met an '=' or ';' then the original comma
28927 was the end of the NSDMI. Otherwise assume
28928 we're still in the NSDMI. */
28929 error = (peek->type != CPP_EQ
28930 && peek->type != CPP_SEMICOLON);
28931 }
28932 else
28933 {
28934 cp_lexer_consume_token (parser->lexer);
28935 begin_scope (sk_function_parms, NULL_TREE);
28936 cp_parser_parameter_declaration_list (parser, &error);
28937 pop_bindings_and_leave_scope ();
28938 }
28939 if (!cp_parser_error_occurred (parser) && !error)
28940 done = true;
28941 cp_parser_abort_tentative_parse (parser);
28942
28943 parser->in_template_argument_list_p = saved_italp;
28944 break;
28945 }
28946 /* FALLTHRU */
28947 case CPP_CLOSE_PAREN:
28948 case CPP_ELLIPSIS:
28949 /* If we run into a non-nested `;', `}', or `]',
28950 then the code is invalid -- but the default
28951 argument is certainly over. */
28952 case CPP_SEMICOLON:
28953 case CPP_CLOSE_BRACE:
28954 case CPP_CLOSE_SQUARE:
28955 if (depth == 0
28956 /* Handle correctly int n = sizeof ... ( p ); */
28957 && token->type != CPP_ELLIPSIS)
28958 done = true;
28959 /* Update DEPTH, if necessary. */
28960 else if (token->type == CPP_CLOSE_PAREN
28961 || token->type == CPP_CLOSE_BRACE
28962 || token->type == CPP_CLOSE_SQUARE)
28963 --depth;
28964 break;
28965
28966 case CPP_OPEN_PAREN:
28967 case CPP_OPEN_SQUARE:
28968 case CPP_OPEN_BRACE:
28969 ++depth;
28970 break;
28971
28972 case CPP_LESS:
28973 if (depth == 0)
28974 /* This might be the comparison operator, or it might
28975 start a template argument list. */
28976 ++maybe_template_id;
28977 break;
28978
28979 case CPP_RSHIFT:
28980 if (cxx_dialect == cxx98)
28981 break;
28982 /* Fall through for C++0x, which treats the `>>'
28983 operator like two `>' tokens in certain
28984 cases. */
28985 gcc_fallthrough ();
28986
28987 case CPP_GREATER:
28988 if (depth == 0)
28989 {
28990 /* This might be an operator, or it might close a
28991 template argument list. But if a previous '<'
28992 started a template argument list, this will have
28993 closed it, so we can't be in one anymore. */
28994 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28995 if (maybe_template_id < 0)
28996 maybe_template_id = 0;
28997 }
28998 break;
28999
29000 /* If we run out of tokens, issue an error message. */
29001 case CPP_EOF:
29002 case CPP_PRAGMA_EOL:
29003 error_at (token->location, "file ends in default argument");
29004 return error_mark_node;
29005
29006 case CPP_NAME:
29007 case CPP_SCOPE:
29008 /* In these cases, we should look for template-ids.
29009 For example, if the default argument is
29010 `X<int, double>()', we need to do name lookup to
29011 figure out whether or not `X' is a template; if
29012 so, the `,' does not end the default argument.
29013
29014 That is not yet done. */
29015 break;
29016
29017 default:
29018 break;
29019 }
29020
29021 /* If we've reached the end, stop. */
29022 if (done)
29023 break;
29024
29025 /* Add the token to the token block. */
29026 token = cp_lexer_consume_token (parser->lexer);
29027 }
29028
29029 /* Create a DEFAULT_ARG to represent the unparsed default
29030 argument. */
29031 default_argument = make_node (DEFAULT_ARG);
29032 DEFARG_TOKENS (default_argument)
29033 = cp_token_cache_new (first_token, token);
29034 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29035
29036 return default_argument;
29037 }
29038
29039 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29040
29041 location_t
29042 defarg_location (tree default_argument)
29043 {
29044 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29045 location_t start = tokens->first->location;
29046 location_t end = tokens->last->location;
29047 return make_location (start, start, end);
29048 }
29049
29050 /* Begin parsing tentatively. We always save tokens while parsing
29051 tentatively so that if the tentative parsing fails we can restore the
29052 tokens. */
29053
29054 static void
29055 cp_parser_parse_tentatively (cp_parser* parser)
29056 {
29057 /* Enter a new parsing context. */
29058 parser->context = cp_parser_context_new (parser->context);
29059 /* Begin saving tokens. */
29060 cp_lexer_save_tokens (parser->lexer);
29061 /* In order to avoid repetitive access control error messages,
29062 access checks are queued up until we are no longer parsing
29063 tentatively. */
29064 push_deferring_access_checks (dk_deferred);
29065 }
29066
29067 /* Commit to the currently active tentative parse. */
29068
29069 static void
29070 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29071 {
29072 cp_parser_context *context;
29073 cp_lexer *lexer;
29074
29075 /* Mark all of the levels as committed. */
29076 lexer = parser->lexer;
29077 for (context = parser->context; context->next; context = context->next)
29078 {
29079 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29080 break;
29081 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29082 while (!cp_lexer_saving_tokens (lexer))
29083 lexer = lexer->next;
29084 cp_lexer_commit_tokens (lexer);
29085 }
29086 }
29087
29088 /* Commit to the topmost currently active tentative parse.
29089
29090 Note that this function shouldn't be called when there are
29091 irreversible side-effects while in a tentative state. For
29092 example, we shouldn't create a permanent entry in the symbol
29093 table, or issue an error message that might not apply if the
29094 tentative parse is aborted. */
29095
29096 static void
29097 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29098 {
29099 cp_parser_context *context = parser->context;
29100 cp_lexer *lexer = parser->lexer;
29101
29102 if (context)
29103 {
29104 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29105 return;
29106 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29107
29108 while (!cp_lexer_saving_tokens (lexer))
29109 lexer = lexer->next;
29110 cp_lexer_commit_tokens (lexer);
29111 }
29112 }
29113
29114 /* Abort the currently active tentative parse. All consumed tokens
29115 will be rolled back, and no diagnostics will be issued. */
29116
29117 static void
29118 cp_parser_abort_tentative_parse (cp_parser* parser)
29119 {
29120 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29121 || errorcount > 0);
29122 cp_parser_simulate_error (parser);
29123 /* Now, pretend that we want to see if the construct was
29124 successfully parsed. */
29125 cp_parser_parse_definitely (parser);
29126 }
29127
29128 /* Stop parsing tentatively. If a parse error has occurred, restore the
29129 token stream. Otherwise, commit to the tokens we have consumed.
29130 Returns true if no error occurred; false otherwise. */
29131
29132 static bool
29133 cp_parser_parse_definitely (cp_parser* parser)
29134 {
29135 bool error_occurred;
29136 cp_parser_context *context;
29137
29138 /* Remember whether or not an error occurred, since we are about to
29139 destroy that information. */
29140 error_occurred = cp_parser_error_occurred (parser);
29141 /* Remove the topmost context from the stack. */
29142 context = parser->context;
29143 parser->context = context->next;
29144 /* If no parse errors occurred, commit to the tentative parse. */
29145 if (!error_occurred)
29146 {
29147 /* Commit to the tokens read tentatively, unless that was
29148 already done. */
29149 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29150 cp_lexer_commit_tokens (parser->lexer);
29151
29152 pop_to_parent_deferring_access_checks ();
29153 }
29154 /* Otherwise, if errors occurred, roll back our state so that things
29155 are just as they were before we began the tentative parse. */
29156 else
29157 {
29158 cp_lexer_rollback_tokens (parser->lexer);
29159 pop_deferring_access_checks ();
29160 }
29161 /* Add the context to the front of the free list. */
29162 context->next = cp_parser_context_free_list;
29163 cp_parser_context_free_list = context;
29164
29165 return !error_occurred;
29166 }
29167
29168 /* Returns true if we are parsing tentatively and are not committed to
29169 this tentative parse. */
29170
29171 static bool
29172 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29173 {
29174 return (cp_parser_parsing_tentatively (parser)
29175 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29176 }
29177
29178 /* Returns nonzero iff an error has occurred during the most recent
29179 tentative parse. */
29180
29181 static bool
29182 cp_parser_error_occurred (cp_parser* parser)
29183 {
29184 return (cp_parser_parsing_tentatively (parser)
29185 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29186 }
29187
29188 /* Returns nonzero if GNU extensions are allowed. */
29189
29190 static bool
29191 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29192 {
29193 return parser->allow_gnu_extensions_p;
29194 }
29195 \f
29196 /* Objective-C++ Productions */
29197
29198
29199 /* Parse an Objective-C expression, which feeds into a primary-expression
29200 above.
29201
29202 objc-expression:
29203 objc-message-expression
29204 objc-string-literal
29205 objc-encode-expression
29206 objc-protocol-expression
29207 objc-selector-expression
29208
29209 Returns a tree representation of the expression. */
29210
29211 static cp_expr
29212 cp_parser_objc_expression (cp_parser* parser)
29213 {
29214 /* Try to figure out what kind of declaration is present. */
29215 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29216
29217 switch (kwd->type)
29218 {
29219 case CPP_OPEN_SQUARE:
29220 return cp_parser_objc_message_expression (parser);
29221
29222 case CPP_OBJC_STRING:
29223 kwd = cp_lexer_consume_token (parser->lexer);
29224 return objc_build_string_object (kwd->u.value);
29225
29226 case CPP_KEYWORD:
29227 switch (kwd->keyword)
29228 {
29229 case RID_AT_ENCODE:
29230 return cp_parser_objc_encode_expression (parser);
29231
29232 case RID_AT_PROTOCOL:
29233 return cp_parser_objc_protocol_expression (parser);
29234
29235 case RID_AT_SELECTOR:
29236 return cp_parser_objc_selector_expression (parser);
29237
29238 default:
29239 break;
29240 }
29241 /* FALLTHRU */
29242 default:
29243 error_at (kwd->location,
29244 "misplaced %<@%D%> Objective-C++ construct",
29245 kwd->u.value);
29246 cp_parser_skip_to_end_of_block_or_statement (parser);
29247 }
29248
29249 return error_mark_node;
29250 }
29251
29252 /* Parse an Objective-C message expression.
29253
29254 objc-message-expression:
29255 [ objc-message-receiver objc-message-args ]
29256
29257 Returns a representation of an Objective-C message. */
29258
29259 static tree
29260 cp_parser_objc_message_expression (cp_parser* parser)
29261 {
29262 tree receiver, messageargs;
29263
29264 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29265 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29266 receiver = cp_parser_objc_message_receiver (parser);
29267 messageargs = cp_parser_objc_message_args (parser);
29268 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29269 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29270
29271 tree result = objc_build_message_expr (receiver, messageargs);
29272
29273 /* Construct a location e.g.
29274 [self func1:5]
29275 ^~~~~~~~~~~~~~
29276 ranging from the '[' to the ']', with the caret at the start. */
29277 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29278 protected_set_expr_location (result, combined_loc);
29279
29280 return result;
29281 }
29282
29283 /* Parse an objc-message-receiver.
29284
29285 objc-message-receiver:
29286 expression
29287 simple-type-specifier
29288
29289 Returns a representation of the type or expression. */
29290
29291 static tree
29292 cp_parser_objc_message_receiver (cp_parser* parser)
29293 {
29294 tree rcv;
29295
29296 /* An Objective-C message receiver may be either (1) a type
29297 or (2) an expression. */
29298 cp_parser_parse_tentatively (parser);
29299 rcv = cp_parser_expression (parser);
29300
29301 /* If that worked out, fine. */
29302 if (cp_parser_parse_definitely (parser))
29303 return rcv;
29304
29305 cp_parser_parse_tentatively (parser);
29306 rcv = cp_parser_simple_type_specifier (parser,
29307 /*decl_specs=*/NULL,
29308 CP_PARSER_FLAGS_NONE);
29309
29310 if (cp_parser_parse_definitely (parser))
29311 return objc_get_class_reference (rcv);
29312
29313 cp_parser_error (parser, "objective-c++ message receiver expected");
29314 return error_mark_node;
29315 }
29316
29317 /* Parse the arguments and selectors comprising an Objective-C message.
29318
29319 objc-message-args:
29320 objc-selector
29321 objc-selector-args
29322 objc-selector-args , objc-comma-args
29323
29324 objc-selector-args:
29325 objc-selector [opt] : assignment-expression
29326 objc-selector-args objc-selector [opt] : assignment-expression
29327
29328 objc-comma-args:
29329 assignment-expression
29330 objc-comma-args , assignment-expression
29331
29332 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29333 selector arguments and TREE_VALUE containing a list of comma
29334 arguments. */
29335
29336 static tree
29337 cp_parser_objc_message_args (cp_parser* parser)
29338 {
29339 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29340 bool maybe_unary_selector_p = true;
29341 cp_token *token = cp_lexer_peek_token (parser->lexer);
29342
29343 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29344 {
29345 tree selector = NULL_TREE, arg;
29346
29347 if (token->type != CPP_COLON)
29348 selector = cp_parser_objc_selector (parser);
29349
29350 /* Detect if we have a unary selector. */
29351 if (maybe_unary_selector_p
29352 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29353 return build_tree_list (selector, NULL_TREE);
29354
29355 maybe_unary_selector_p = false;
29356 cp_parser_require (parser, CPP_COLON, RT_COLON);
29357 arg = cp_parser_assignment_expression (parser);
29358
29359 sel_args
29360 = chainon (sel_args,
29361 build_tree_list (selector, arg));
29362
29363 token = cp_lexer_peek_token (parser->lexer);
29364 }
29365
29366 /* Handle non-selector arguments, if any. */
29367 while (token->type == CPP_COMMA)
29368 {
29369 tree arg;
29370
29371 cp_lexer_consume_token (parser->lexer);
29372 arg = cp_parser_assignment_expression (parser);
29373
29374 addl_args
29375 = chainon (addl_args,
29376 build_tree_list (NULL_TREE, arg));
29377
29378 token = cp_lexer_peek_token (parser->lexer);
29379 }
29380
29381 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29382 {
29383 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29384 return build_tree_list (error_mark_node, error_mark_node);
29385 }
29386
29387 return build_tree_list (sel_args, addl_args);
29388 }
29389
29390 /* Parse an Objective-C encode expression.
29391
29392 objc-encode-expression:
29393 @encode objc-typename
29394
29395 Returns an encoded representation of the type argument. */
29396
29397 static cp_expr
29398 cp_parser_objc_encode_expression (cp_parser* parser)
29399 {
29400 tree type;
29401 cp_token *token;
29402 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29403
29404 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29405 matching_parens parens;
29406 parens.require_open (parser);
29407 token = cp_lexer_peek_token (parser->lexer);
29408 type = complete_type (cp_parser_type_id (parser));
29409 parens.require_close (parser);
29410
29411 if (!type)
29412 {
29413 error_at (token->location,
29414 "%<@encode%> must specify a type as an argument");
29415 return error_mark_node;
29416 }
29417
29418 /* This happens if we find @encode(T) (where T is a template
29419 typename or something dependent on a template typename) when
29420 parsing a template. In that case, we can't compile it
29421 immediately, but we rather create an AT_ENCODE_EXPR which will
29422 need to be instantiated when the template is used.
29423 */
29424 if (dependent_type_p (type))
29425 {
29426 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29427 TREE_READONLY (value) = 1;
29428 return value;
29429 }
29430
29431
29432 /* Build a location of the form:
29433 @encode(int)
29434 ^~~~~~~~~~~~
29435 with caret==start at the @ token, finishing at the close paren. */
29436 location_t combined_loc
29437 = make_location (start_loc, start_loc,
29438 cp_lexer_previous_token (parser->lexer)->location);
29439
29440 return cp_expr (objc_build_encode_expr (type), combined_loc);
29441 }
29442
29443 /* Parse an Objective-C @defs expression. */
29444
29445 static tree
29446 cp_parser_objc_defs_expression (cp_parser *parser)
29447 {
29448 tree name;
29449
29450 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29451 matching_parens parens;
29452 parens.require_open (parser);
29453 name = cp_parser_identifier (parser);
29454 parens.require_close (parser);
29455
29456 return objc_get_class_ivars (name);
29457 }
29458
29459 /* Parse an Objective-C protocol expression.
29460
29461 objc-protocol-expression:
29462 @protocol ( identifier )
29463
29464 Returns a representation of the protocol expression. */
29465
29466 static tree
29467 cp_parser_objc_protocol_expression (cp_parser* parser)
29468 {
29469 tree proto;
29470 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29471
29472 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29473 matching_parens parens;
29474 parens.require_open (parser);
29475 proto = cp_parser_identifier (parser);
29476 parens.require_close (parser);
29477
29478 /* Build a location of the form:
29479 @protocol(prot)
29480 ^~~~~~~~~~~~~~~
29481 with caret==start at the @ token, finishing at the close paren. */
29482 location_t combined_loc
29483 = make_location (start_loc, start_loc,
29484 cp_lexer_previous_token (parser->lexer)->location);
29485 tree result = objc_build_protocol_expr (proto);
29486 protected_set_expr_location (result, combined_loc);
29487 return result;
29488 }
29489
29490 /* Parse an Objective-C selector expression.
29491
29492 objc-selector-expression:
29493 @selector ( objc-method-signature )
29494
29495 objc-method-signature:
29496 objc-selector
29497 objc-selector-seq
29498
29499 objc-selector-seq:
29500 objc-selector :
29501 objc-selector-seq objc-selector :
29502
29503 Returns a representation of the method selector. */
29504
29505 static tree
29506 cp_parser_objc_selector_expression (cp_parser* parser)
29507 {
29508 tree sel_seq = NULL_TREE;
29509 bool maybe_unary_selector_p = true;
29510 cp_token *token;
29511 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29512
29513 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29514 matching_parens parens;
29515 parens.require_open (parser);
29516 token = cp_lexer_peek_token (parser->lexer);
29517
29518 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29519 || token->type == CPP_SCOPE)
29520 {
29521 tree selector = NULL_TREE;
29522
29523 if (token->type != CPP_COLON
29524 || token->type == CPP_SCOPE)
29525 selector = cp_parser_objc_selector (parser);
29526
29527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29528 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29529 {
29530 /* Detect if we have a unary selector. */
29531 if (maybe_unary_selector_p)
29532 {
29533 sel_seq = selector;
29534 goto finish_selector;
29535 }
29536 else
29537 {
29538 cp_parser_error (parser, "expected %<:%>");
29539 }
29540 }
29541 maybe_unary_selector_p = false;
29542 token = cp_lexer_consume_token (parser->lexer);
29543
29544 if (token->type == CPP_SCOPE)
29545 {
29546 sel_seq
29547 = chainon (sel_seq,
29548 build_tree_list (selector, NULL_TREE));
29549 sel_seq
29550 = chainon (sel_seq,
29551 build_tree_list (NULL_TREE, NULL_TREE));
29552 }
29553 else
29554 sel_seq
29555 = chainon (sel_seq,
29556 build_tree_list (selector, NULL_TREE));
29557
29558 token = cp_lexer_peek_token (parser->lexer);
29559 }
29560
29561 finish_selector:
29562 parens.require_close (parser);
29563
29564
29565 /* Build a location of the form:
29566 @selector(func)
29567 ^~~~~~~~~~~~~~~
29568 with caret==start at the @ token, finishing at the close paren. */
29569 location_t combined_loc
29570 = make_location (loc, loc,
29571 cp_lexer_previous_token (parser->lexer)->location);
29572 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29573 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29574 protected_set_expr_location (result, combined_loc);
29575 return result;
29576 }
29577
29578 /* Parse a list of identifiers.
29579
29580 objc-identifier-list:
29581 identifier
29582 objc-identifier-list , identifier
29583
29584 Returns a TREE_LIST of identifier nodes. */
29585
29586 static tree
29587 cp_parser_objc_identifier_list (cp_parser* parser)
29588 {
29589 tree identifier;
29590 tree list;
29591 cp_token *sep;
29592
29593 identifier = cp_parser_identifier (parser);
29594 if (identifier == error_mark_node)
29595 return error_mark_node;
29596
29597 list = build_tree_list (NULL_TREE, identifier);
29598 sep = cp_lexer_peek_token (parser->lexer);
29599
29600 while (sep->type == CPP_COMMA)
29601 {
29602 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29603 identifier = cp_parser_identifier (parser);
29604 if (identifier == error_mark_node)
29605 return list;
29606
29607 list = chainon (list, build_tree_list (NULL_TREE,
29608 identifier));
29609 sep = cp_lexer_peek_token (parser->lexer);
29610 }
29611
29612 return list;
29613 }
29614
29615 /* Parse an Objective-C alias declaration.
29616
29617 objc-alias-declaration:
29618 @compatibility_alias identifier identifier ;
29619
29620 This function registers the alias mapping with the Objective-C front end.
29621 It returns nothing. */
29622
29623 static void
29624 cp_parser_objc_alias_declaration (cp_parser* parser)
29625 {
29626 tree alias, orig;
29627
29628 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29629 alias = cp_parser_identifier (parser);
29630 orig = cp_parser_identifier (parser);
29631 objc_declare_alias (alias, orig);
29632 cp_parser_consume_semicolon_at_end_of_statement (parser);
29633 }
29634
29635 /* Parse an Objective-C class forward-declaration.
29636
29637 objc-class-declaration:
29638 @class objc-identifier-list ;
29639
29640 The function registers the forward declarations with the Objective-C
29641 front end. It returns nothing. */
29642
29643 static void
29644 cp_parser_objc_class_declaration (cp_parser* parser)
29645 {
29646 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29647 while (true)
29648 {
29649 tree id;
29650
29651 id = cp_parser_identifier (parser);
29652 if (id == error_mark_node)
29653 break;
29654
29655 objc_declare_class (id);
29656
29657 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29658 cp_lexer_consume_token (parser->lexer);
29659 else
29660 break;
29661 }
29662 cp_parser_consume_semicolon_at_end_of_statement (parser);
29663 }
29664
29665 /* Parse a list of Objective-C protocol references.
29666
29667 objc-protocol-refs-opt:
29668 objc-protocol-refs [opt]
29669
29670 objc-protocol-refs:
29671 < objc-identifier-list >
29672
29673 Returns a TREE_LIST of identifiers, if any. */
29674
29675 static tree
29676 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29677 {
29678 tree protorefs = NULL_TREE;
29679
29680 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29681 {
29682 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29683 protorefs = cp_parser_objc_identifier_list (parser);
29684 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29685 }
29686
29687 return protorefs;
29688 }
29689
29690 /* Parse a Objective-C visibility specification. */
29691
29692 static void
29693 cp_parser_objc_visibility_spec (cp_parser* parser)
29694 {
29695 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29696
29697 switch (vis->keyword)
29698 {
29699 case RID_AT_PRIVATE:
29700 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29701 break;
29702 case RID_AT_PROTECTED:
29703 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29704 break;
29705 case RID_AT_PUBLIC:
29706 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29707 break;
29708 case RID_AT_PACKAGE:
29709 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29710 break;
29711 default:
29712 return;
29713 }
29714
29715 /* Eat '@private'/'@protected'/'@public'. */
29716 cp_lexer_consume_token (parser->lexer);
29717 }
29718
29719 /* Parse an Objective-C method type. Return 'true' if it is a class
29720 (+) method, and 'false' if it is an instance (-) method. */
29721
29722 static inline bool
29723 cp_parser_objc_method_type (cp_parser* parser)
29724 {
29725 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29726 return true;
29727 else
29728 return false;
29729 }
29730
29731 /* Parse an Objective-C protocol qualifier. */
29732
29733 static tree
29734 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29735 {
29736 tree quals = NULL_TREE, node;
29737 cp_token *token = cp_lexer_peek_token (parser->lexer);
29738
29739 node = token->u.value;
29740
29741 while (node && identifier_p (node)
29742 && (node == ridpointers [(int) RID_IN]
29743 || node == ridpointers [(int) RID_OUT]
29744 || node == ridpointers [(int) RID_INOUT]
29745 || node == ridpointers [(int) RID_BYCOPY]
29746 || node == ridpointers [(int) RID_BYREF]
29747 || node == ridpointers [(int) RID_ONEWAY]))
29748 {
29749 quals = tree_cons (NULL_TREE, node, quals);
29750 cp_lexer_consume_token (parser->lexer);
29751 token = cp_lexer_peek_token (parser->lexer);
29752 node = token->u.value;
29753 }
29754
29755 return quals;
29756 }
29757
29758 /* Parse an Objective-C typename. */
29759
29760 static tree
29761 cp_parser_objc_typename (cp_parser* parser)
29762 {
29763 tree type_name = NULL_TREE;
29764
29765 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29766 {
29767 tree proto_quals, cp_type = NULL_TREE;
29768
29769 matching_parens parens;
29770 parens.consume_open (parser); /* Eat '('. */
29771 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29772
29773 /* An ObjC type name may consist of just protocol qualifiers, in which
29774 case the type shall default to 'id'. */
29775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29776 {
29777 cp_type = cp_parser_type_id (parser);
29778
29779 /* If the type could not be parsed, an error has already
29780 been produced. For error recovery, behave as if it had
29781 not been specified, which will use the default type
29782 'id'. */
29783 if (cp_type == error_mark_node)
29784 {
29785 cp_type = NULL_TREE;
29786 /* We need to skip to the closing parenthesis as
29787 cp_parser_type_id() does not seem to do it for
29788 us. */
29789 cp_parser_skip_to_closing_parenthesis (parser,
29790 /*recovering=*/true,
29791 /*or_comma=*/false,
29792 /*consume_paren=*/false);
29793 }
29794 }
29795
29796 parens.require_close (parser);
29797 type_name = build_tree_list (proto_quals, cp_type);
29798 }
29799
29800 return type_name;
29801 }
29802
29803 /* Check to see if TYPE refers to an Objective-C selector name. */
29804
29805 static bool
29806 cp_parser_objc_selector_p (enum cpp_ttype type)
29807 {
29808 return (type == CPP_NAME || type == CPP_KEYWORD
29809 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29810 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29811 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29812 || type == CPP_XOR || type == CPP_XOR_EQ);
29813 }
29814
29815 /* Parse an Objective-C selector. */
29816
29817 static tree
29818 cp_parser_objc_selector (cp_parser* parser)
29819 {
29820 cp_token *token = cp_lexer_consume_token (parser->lexer);
29821
29822 if (!cp_parser_objc_selector_p (token->type))
29823 {
29824 error_at (token->location, "invalid Objective-C++ selector name");
29825 return error_mark_node;
29826 }
29827
29828 /* C++ operator names are allowed to appear in ObjC selectors. */
29829 switch (token->type)
29830 {
29831 case CPP_AND_AND: return get_identifier ("and");
29832 case CPP_AND_EQ: return get_identifier ("and_eq");
29833 case CPP_AND: return get_identifier ("bitand");
29834 case CPP_OR: return get_identifier ("bitor");
29835 case CPP_COMPL: return get_identifier ("compl");
29836 case CPP_NOT: return get_identifier ("not");
29837 case CPP_NOT_EQ: return get_identifier ("not_eq");
29838 case CPP_OR_OR: return get_identifier ("or");
29839 case CPP_OR_EQ: return get_identifier ("or_eq");
29840 case CPP_XOR: return get_identifier ("xor");
29841 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29842 default: return token->u.value;
29843 }
29844 }
29845
29846 /* Parse an Objective-C params list. */
29847
29848 static tree
29849 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29850 {
29851 tree params = NULL_TREE;
29852 bool maybe_unary_selector_p = true;
29853 cp_token *token = cp_lexer_peek_token (parser->lexer);
29854
29855 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29856 {
29857 tree selector = NULL_TREE, type_name, identifier;
29858 tree parm_attr = NULL_TREE;
29859
29860 if (token->keyword == RID_ATTRIBUTE)
29861 break;
29862
29863 if (token->type != CPP_COLON)
29864 selector = cp_parser_objc_selector (parser);
29865
29866 /* Detect if we have a unary selector. */
29867 if (maybe_unary_selector_p
29868 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29869 {
29870 params = selector; /* Might be followed by attributes. */
29871 break;
29872 }
29873
29874 maybe_unary_selector_p = false;
29875 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29876 {
29877 /* Something went quite wrong. There should be a colon
29878 here, but there is not. Stop parsing parameters. */
29879 break;
29880 }
29881 type_name = cp_parser_objc_typename (parser);
29882 /* New ObjC allows attributes on parameters too. */
29883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29884 parm_attr = cp_parser_attributes_opt (parser);
29885 identifier = cp_parser_identifier (parser);
29886
29887 params
29888 = chainon (params,
29889 objc_build_keyword_decl (selector,
29890 type_name,
29891 identifier,
29892 parm_attr));
29893
29894 token = cp_lexer_peek_token (parser->lexer);
29895 }
29896
29897 if (params == NULL_TREE)
29898 {
29899 cp_parser_error (parser, "objective-c++ method declaration is expected");
29900 return error_mark_node;
29901 }
29902
29903 /* We allow tail attributes for the method. */
29904 if (token->keyword == RID_ATTRIBUTE)
29905 {
29906 *attributes = cp_parser_attributes_opt (parser);
29907 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29908 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29909 return params;
29910 cp_parser_error (parser,
29911 "method attributes must be specified at the end");
29912 return error_mark_node;
29913 }
29914
29915 if (params == NULL_TREE)
29916 {
29917 cp_parser_error (parser, "objective-c++ method declaration is expected");
29918 return error_mark_node;
29919 }
29920 return params;
29921 }
29922
29923 /* Parse the non-keyword Objective-C params. */
29924
29925 static tree
29926 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29927 tree* attributes)
29928 {
29929 tree params = make_node (TREE_LIST);
29930 cp_token *token = cp_lexer_peek_token (parser->lexer);
29931 *ellipsisp = false; /* Initially, assume no ellipsis. */
29932
29933 while (token->type == CPP_COMMA)
29934 {
29935 cp_parameter_declarator *parmdecl;
29936 tree parm;
29937
29938 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29939 token = cp_lexer_peek_token (parser->lexer);
29940
29941 if (token->type == CPP_ELLIPSIS)
29942 {
29943 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29944 *ellipsisp = true;
29945 token = cp_lexer_peek_token (parser->lexer);
29946 break;
29947 }
29948
29949 /* TODO: parse attributes for tail parameters. */
29950 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29951 parm = grokdeclarator (parmdecl->declarator,
29952 &parmdecl->decl_specifiers,
29953 PARM, /*initialized=*/0,
29954 /*attrlist=*/NULL);
29955
29956 chainon (params, build_tree_list (NULL_TREE, parm));
29957 token = cp_lexer_peek_token (parser->lexer);
29958 }
29959
29960 /* We allow tail attributes for the method. */
29961 if (token->keyword == RID_ATTRIBUTE)
29962 {
29963 if (*attributes == NULL_TREE)
29964 {
29965 *attributes = cp_parser_attributes_opt (parser);
29966 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29967 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29968 return params;
29969 }
29970 else
29971 /* We have an error, but parse the attributes, so that we can
29972 carry on. */
29973 *attributes = cp_parser_attributes_opt (parser);
29974
29975 cp_parser_error (parser,
29976 "method attributes must be specified at the end");
29977 return error_mark_node;
29978 }
29979
29980 return params;
29981 }
29982
29983 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29984
29985 static void
29986 cp_parser_objc_interstitial_code (cp_parser* parser)
29987 {
29988 cp_token *token = cp_lexer_peek_token (parser->lexer);
29989
29990 /* If the next token is `extern' and the following token is a string
29991 literal, then we have a linkage specification. */
29992 if (token->keyword == RID_EXTERN
29993 && cp_parser_is_pure_string_literal
29994 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29995 cp_parser_linkage_specification (parser);
29996 /* Handle #pragma, if any. */
29997 else if (token->type == CPP_PRAGMA)
29998 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29999 /* Allow stray semicolons. */
30000 else if (token->type == CPP_SEMICOLON)
30001 cp_lexer_consume_token (parser->lexer);
30002 /* Mark methods as optional or required, when building protocols. */
30003 else if (token->keyword == RID_AT_OPTIONAL)
30004 {
30005 cp_lexer_consume_token (parser->lexer);
30006 objc_set_method_opt (true);
30007 }
30008 else if (token->keyword == RID_AT_REQUIRED)
30009 {
30010 cp_lexer_consume_token (parser->lexer);
30011 objc_set_method_opt (false);
30012 }
30013 else if (token->keyword == RID_NAMESPACE)
30014 cp_parser_namespace_definition (parser);
30015 /* Other stray characters must generate errors. */
30016 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30017 {
30018 cp_lexer_consume_token (parser->lexer);
30019 error ("stray %qs between Objective-C++ methods",
30020 token->type == CPP_OPEN_BRACE ? "{" : "}");
30021 }
30022 /* Finally, try to parse a block-declaration, or a function-definition. */
30023 else
30024 cp_parser_block_declaration (parser, /*statement_p=*/false);
30025 }
30026
30027 /* Parse a method signature. */
30028
30029 static tree
30030 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30031 {
30032 tree rettype, kwdparms, optparms;
30033 bool ellipsis = false;
30034 bool is_class_method;
30035
30036 is_class_method = cp_parser_objc_method_type (parser);
30037 rettype = cp_parser_objc_typename (parser);
30038 *attributes = NULL_TREE;
30039 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30040 if (kwdparms == error_mark_node)
30041 return error_mark_node;
30042 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30043 if (optparms == error_mark_node)
30044 return error_mark_node;
30045
30046 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30047 }
30048
30049 static bool
30050 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30051 {
30052 tree tattr;
30053 cp_lexer_save_tokens (parser->lexer);
30054 tattr = cp_parser_attributes_opt (parser);
30055 gcc_assert (tattr) ;
30056
30057 /* If the attributes are followed by a method introducer, this is not allowed.
30058 Dump the attributes and flag the situation. */
30059 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30060 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30061 return true;
30062
30063 /* Otherwise, the attributes introduce some interstitial code, possibly so
30064 rewind to allow that check. */
30065 cp_lexer_rollback_tokens (parser->lexer);
30066 return false;
30067 }
30068
30069 /* Parse an Objective-C method prototype list. */
30070
30071 static void
30072 cp_parser_objc_method_prototype_list (cp_parser* parser)
30073 {
30074 cp_token *token = cp_lexer_peek_token (parser->lexer);
30075
30076 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30077 {
30078 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30079 {
30080 tree attributes, sig;
30081 bool is_class_method;
30082 if (token->type == CPP_PLUS)
30083 is_class_method = true;
30084 else
30085 is_class_method = false;
30086 sig = cp_parser_objc_method_signature (parser, &attributes);
30087 if (sig == error_mark_node)
30088 {
30089 cp_parser_skip_to_end_of_block_or_statement (parser);
30090 token = cp_lexer_peek_token (parser->lexer);
30091 continue;
30092 }
30093 objc_add_method_declaration (is_class_method, sig, attributes);
30094 cp_parser_consume_semicolon_at_end_of_statement (parser);
30095 }
30096 else if (token->keyword == RID_AT_PROPERTY)
30097 cp_parser_objc_at_property_declaration (parser);
30098 else if (token->keyword == RID_ATTRIBUTE
30099 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30100 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30101 OPT_Wattributes,
30102 "prefix attributes are ignored for methods");
30103 else
30104 /* Allow for interspersed non-ObjC++ code. */
30105 cp_parser_objc_interstitial_code (parser);
30106
30107 token = cp_lexer_peek_token (parser->lexer);
30108 }
30109
30110 if (token->type != CPP_EOF)
30111 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30112 else
30113 cp_parser_error (parser, "expected %<@end%>");
30114
30115 objc_finish_interface ();
30116 }
30117
30118 /* Parse an Objective-C method definition list. */
30119
30120 static void
30121 cp_parser_objc_method_definition_list (cp_parser* parser)
30122 {
30123 cp_token *token = cp_lexer_peek_token (parser->lexer);
30124
30125 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30126 {
30127 tree meth;
30128
30129 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30130 {
30131 cp_token *ptk;
30132 tree sig, attribute;
30133 bool is_class_method;
30134 if (token->type == CPP_PLUS)
30135 is_class_method = true;
30136 else
30137 is_class_method = false;
30138 push_deferring_access_checks (dk_deferred);
30139 sig = cp_parser_objc_method_signature (parser, &attribute);
30140 if (sig == error_mark_node)
30141 {
30142 cp_parser_skip_to_end_of_block_or_statement (parser);
30143 token = cp_lexer_peek_token (parser->lexer);
30144 continue;
30145 }
30146 objc_start_method_definition (is_class_method, sig, attribute,
30147 NULL_TREE);
30148
30149 /* For historical reasons, we accept an optional semicolon. */
30150 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30151 cp_lexer_consume_token (parser->lexer);
30152
30153 ptk = cp_lexer_peek_token (parser->lexer);
30154 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30155 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30156 {
30157 perform_deferred_access_checks (tf_warning_or_error);
30158 stop_deferring_access_checks ();
30159 meth = cp_parser_function_definition_after_declarator (parser,
30160 false);
30161 pop_deferring_access_checks ();
30162 objc_finish_method_definition (meth);
30163 }
30164 }
30165 /* The following case will be removed once @synthesize is
30166 completely implemented. */
30167 else if (token->keyword == RID_AT_PROPERTY)
30168 cp_parser_objc_at_property_declaration (parser);
30169 else if (token->keyword == RID_AT_SYNTHESIZE)
30170 cp_parser_objc_at_synthesize_declaration (parser);
30171 else if (token->keyword == RID_AT_DYNAMIC)
30172 cp_parser_objc_at_dynamic_declaration (parser);
30173 else if (token->keyword == RID_ATTRIBUTE
30174 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30175 warning_at (token->location, OPT_Wattributes,
30176 "prefix attributes are ignored for methods");
30177 else
30178 /* Allow for interspersed non-ObjC++ code. */
30179 cp_parser_objc_interstitial_code (parser);
30180
30181 token = cp_lexer_peek_token (parser->lexer);
30182 }
30183
30184 if (token->type != CPP_EOF)
30185 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30186 else
30187 cp_parser_error (parser, "expected %<@end%>");
30188
30189 objc_finish_implementation ();
30190 }
30191
30192 /* Parse Objective-C ivars. */
30193
30194 static void
30195 cp_parser_objc_class_ivars (cp_parser* parser)
30196 {
30197 cp_token *token = cp_lexer_peek_token (parser->lexer);
30198
30199 if (token->type != CPP_OPEN_BRACE)
30200 return; /* No ivars specified. */
30201
30202 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30203 token = cp_lexer_peek_token (parser->lexer);
30204
30205 while (token->type != CPP_CLOSE_BRACE
30206 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30207 {
30208 cp_decl_specifier_seq declspecs;
30209 int decl_class_or_enum_p;
30210 tree prefix_attributes;
30211
30212 cp_parser_objc_visibility_spec (parser);
30213
30214 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30215 break;
30216
30217 cp_parser_decl_specifier_seq (parser,
30218 CP_PARSER_FLAGS_OPTIONAL,
30219 &declspecs,
30220 &decl_class_or_enum_p);
30221
30222 /* auto, register, static, extern, mutable. */
30223 if (declspecs.storage_class != sc_none)
30224 {
30225 cp_parser_error (parser, "invalid type for instance variable");
30226 declspecs.storage_class = sc_none;
30227 }
30228
30229 /* thread_local. */
30230 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30231 {
30232 cp_parser_error (parser, "invalid type for instance variable");
30233 declspecs.locations[ds_thread] = 0;
30234 }
30235
30236 /* typedef. */
30237 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30238 {
30239 cp_parser_error (parser, "invalid type for instance variable");
30240 declspecs.locations[ds_typedef] = 0;
30241 }
30242
30243 prefix_attributes = declspecs.attributes;
30244 declspecs.attributes = NULL_TREE;
30245
30246 /* Keep going until we hit the `;' at the end of the
30247 declaration. */
30248 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30249 {
30250 tree width = NULL_TREE, attributes, first_attribute, decl;
30251 cp_declarator *declarator = NULL;
30252 int ctor_dtor_or_conv_p;
30253
30254 /* Check for a (possibly unnamed) bitfield declaration. */
30255 token = cp_lexer_peek_token (parser->lexer);
30256 if (token->type == CPP_COLON)
30257 goto eat_colon;
30258
30259 if (token->type == CPP_NAME
30260 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30261 == CPP_COLON))
30262 {
30263 /* Get the name of the bitfield. */
30264 declarator = make_id_declarator (NULL_TREE,
30265 cp_parser_identifier (parser),
30266 sfk_none);
30267
30268 eat_colon:
30269 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30270 /* Get the width of the bitfield. */
30271 width
30272 = cp_parser_constant_expression (parser);
30273 }
30274 else
30275 {
30276 /* Parse the declarator. */
30277 declarator
30278 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30279 &ctor_dtor_or_conv_p,
30280 /*parenthesized_p=*/NULL,
30281 /*member_p=*/false,
30282 /*friend_p=*/false);
30283 }
30284
30285 /* Look for attributes that apply to the ivar. */
30286 attributes = cp_parser_attributes_opt (parser);
30287 /* Remember which attributes are prefix attributes and
30288 which are not. */
30289 first_attribute = attributes;
30290 /* Combine the attributes. */
30291 attributes = attr_chainon (prefix_attributes, attributes);
30292
30293 if (width)
30294 /* Create the bitfield declaration. */
30295 decl = grokbitfield (declarator, &declspecs,
30296 width, NULL_TREE, attributes);
30297 else
30298 decl = grokfield (declarator, &declspecs,
30299 NULL_TREE, /*init_const_expr_p=*/false,
30300 NULL_TREE, attributes);
30301
30302 /* Add the instance variable. */
30303 if (decl != error_mark_node && decl != NULL_TREE)
30304 objc_add_instance_variable (decl);
30305
30306 /* Reset PREFIX_ATTRIBUTES. */
30307 if (attributes != error_mark_node)
30308 {
30309 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30310 attributes = TREE_CHAIN (attributes);
30311 if (attributes)
30312 TREE_CHAIN (attributes) = NULL_TREE;
30313 }
30314
30315 token = cp_lexer_peek_token (parser->lexer);
30316
30317 if (token->type == CPP_COMMA)
30318 {
30319 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30320 continue;
30321 }
30322 break;
30323 }
30324
30325 cp_parser_consume_semicolon_at_end_of_statement (parser);
30326 token = cp_lexer_peek_token (parser->lexer);
30327 }
30328
30329 if (token->keyword == RID_AT_END)
30330 cp_parser_error (parser, "expected %<}%>");
30331
30332 /* Do not consume the RID_AT_END, so it will be read again as terminating
30333 the @interface of @implementation. */
30334 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30335 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30336
30337 /* For historical reasons, we accept an optional semicolon. */
30338 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30339 cp_lexer_consume_token (parser->lexer);
30340 }
30341
30342 /* Parse an Objective-C protocol declaration. */
30343
30344 static void
30345 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30346 {
30347 tree proto, protorefs;
30348 cp_token *tok;
30349
30350 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30351 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30352 {
30353 tok = cp_lexer_peek_token (parser->lexer);
30354 error_at (tok->location, "identifier expected after %<@protocol%>");
30355 cp_parser_consume_semicolon_at_end_of_statement (parser);
30356 return;
30357 }
30358
30359 /* See if we have a forward declaration or a definition. */
30360 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30361
30362 /* Try a forward declaration first. */
30363 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30364 {
30365 while (true)
30366 {
30367 tree id;
30368
30369 id = cp_parser_identifier (parser);
30370 if (id == error_mark_node)
30371 break;
30372
30373 objc_declare_protocol (id, attributes);
30374
30375 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30376 cp_lexer_consume_token (parser->lexer);
30377 else
30378 break;
30379 }
30380 cp_parser_consume_semicolon_at_end_of_statement (parser);
30381 }
30382
30383 /* Ok, we got a full-fledged definition (or at least should). */
30384 else
30385 {
30386 proto = cp_parser_identifier (parser);
30387 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30388 objc_start_protocol (proto, protorefs, attributes);
30389 cp_parser_objc_method_prototype_list (parser);
30390 }
30391 }
30392
30393 /* Parse an Objective-C superclass or category. */
30394
30395 static void
30396 cp_parser_objc_superclass_or_category (cp_parser *parser,
30397 bool iface_p,
30398 tree *super,
30399 tree *categ, bool *is_class_extension)
30400 {
30401 cp_token *next = cp_lexer_peek_token (parser->lexer);
30402
30403 *super = *categ = NULL_TREE;
30404 *is_class_extension = false;
30405 if (next->type == CPP_COLON)
30406 {
30407 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30408 *super = cp_parser_identifier (parser);
30409 }
30410 else if (next->type == CPP_OPEN_PAREN)
30411 {
30412 matching_parens parens;
30413 parens.consume_open (parser); /* Eat '('. */
30414
30415 /* If there is no category name, and this is an @interface, we
30416 have a class extension. */
30417 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30418 {
30419 *categ = NULL_TREE;
30420 *is_class_extension = true;
30421 }
30422 else
30423 *categ = cp_parser_identifier (parser);
30424
30425 parens.require_close (parser);
30426 }
30427 }
30428
30429 /* Parse an Objective-C class interface. */
30430
30431 static void
30432 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30433 {
30434 tree name, super, categ, protos;
30435 bool is_class_extension;
30436
30437 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30438 name = cp_parser_identifier (parser);
30439 if (name == error_mark_node)
30440 {
30441 /* It's hard to recover because even if valid @interface stuff
30442 is to follow, we can't compile it (or validate it) if we
30443 don't even know which class it refers to. Let's assume this
30444 was a stray '@interface' token in the stream and skip it.
30445 */
30446 return;
30447 }
30448 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30449 &is_class_extension);
30450 protos = cp_parser_objc_protocol_refs_opt (parser);
30451
30452 /* We have either a class or a category on our hands. */
30453 if (categ || is_class_extension)
30454 objc_start_category_interface (name, categ, protos, attributes);
30455 else
30456 {
30457 objc_start_class_interface (name, super, protos, attributes);
30458 /* Handle instance variable declarations, if any. */
30459 cp_parser_objc_class_ivars (parser);
30460 objc_continue_interface ();
30461 }
30462
30463 cp_parser_objc_method_prototype_list (parser);
30464 }
30465
30466 /* Parse an Objective-C class implementation. */
30467
30468 static void
30469 cp_parser_objc_class_implementation (cp_parser* parser)
30470 {
30471 tree name, super, categ;
30472 bool is_class_extension;
30473
30474 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30475 name = cp_parser_identifier (parser);
30476 if (name == error_mark_node)
30477 {
30478 /* It's hard to recover because even if valid @implementation
30479 stuff is to follow, we can't compile it (or validate it) if
30480 we don't even know which class it refers to. Let's assume
30481 this was a stray '@implementation' token in the stream and
30482 skip it.
30483 */
30484 return;
30485 }
30486 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30487 &is_class_extension);
30488
30489 /* We have either a class or a category on our hands. */
30490 if (categ)
30491 objc_start_category_implementation (name, categ);
30492 else
30493 {
30494 objc_start_class_implementation (name, super);
30495 /* Handle instance variable declarations, if any. */
30496 cp_parser_objc_class_ivars (parser);
30497 objc_continue_implementation ();
30498 }
30499
30500 cp_parser_objc_method_definition_list (parser);
30501 }
30502
30503 /* Consume the @end token and finish off the implementation. */
30504
30505 static void
30506 cp_parser_objc_end_implementation (cp_parser* parser)
30507 {
30508 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30509 objc_finish_implementation ();
30510 }
30511
30512 /* Parse an Objective-C declaration. */
30513
30514 static void
30515 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30516 {
30517 /* Try to figure out what kind of declaration is present. */
30518 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30519
30520 if (attributes)
30521 switch (kwd->keyword)
30522 {
30523 case RID_AT_ALIAS:
30524 case RID_AT_CLASS:
30525 case RID_AT_END:
30526 error_at (kwd->location, "attributes may not be specified before"
30527 " the %<@%D%> Objective-C++ keyword",
30528 kwd->u.value);
30529 attributes = NULL;
30530 break;
30531 case RID_AT_IMPLEMENTATION:
30532 warning_at (kwd->location, OPT_Wattributes,
30533 "prefix attributes are ignored before %<@%D%>",
30534 kwd->u.value);
30535 attributes = NULL;
30536 default:
30537 break;
30538 }
30539
30540 switch (kwd->keyword)
30541 {
30542 case RID_AT_ALIAS:
30543 cp_parser_objc_alias_declaration (parser);
30544 break;
30545 case RID_AT_CLASS:
30546 cp_parser_objc_class_declaration (parser);
30547 break;
30548 case RID_AT_PROTOCOL:
30549 cp_parser_objc_protocol_declaration (parser, attributes);
30550 break;
30551 case RID_AT_INTERFACE:
30552 cp_parser_objc_class_interface (parser, attributes);
30553 break;
30554 case RID_AT_IMPLEMENTATION:
30555 cp_parser_objc_class_implementation (parser);
30556 break;
30557 case RID_AT_END:
30558 cp_parser_objc_end_implementation (parser);
30559 break;
30560 default:
30561 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30562 kwd->u.value);
30563 cp_parser_skip_to_end_of_block_or_statement (parser);
30564 }
30565 }
30566
30567 /* Parse an Objective-C try-catch-finally statement.
30568
30569 objc-try-catch-finally-stmt:
30570 @try compound-statement objc-catch-clause-seq [opt]
30571 objc-finally-clause [opt]
30572
30573 objc-catch-clause-seq:
30574 objc-catch-clause objc-catch-clause-seq [opt]
30575
30576 objc-catch-clause:
30577 @catch ( objc-exception-declaration ) compound-statement
30578
30579 objc-finally-clause:
30580 @finally compound-statement
30581
30582 objc-exception-declaration:
30583 parameter-declaration
30584 '...'
30585
30586 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30587
30588 Returns NULL_TREE.
30589
30590 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30591 for C. Keep them in sync. */
30592
30593 static tree
30594 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30595 {
30596 location_t location;
30597 tree stmt;
30598
30599 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30600 location = cp_lexer_peek_token (parser->lexer)->location;
30601 objc_maybe_warn_exceptions (location);
30602 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30603 node, lest it get absorbed into the surrounding block. */
30604 stmt = push_stmt_list ();
30605 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30606 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30607
30608 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30609 {
30610 cp_parameter_declarator *parm;
30611 tree parameter_declaration = error_mark_node;
30612 bool seen_open_paren = false;
30613 matching_parens parens;
30614
30615 cp_lexer_consume_token (parser->lexer);
30616 if (parens.require_open (parser))
30617 seen_open_paren = true;
30618 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30619 {
30620 /* We have "@catch (...)" (where the '...' are literally
30621 what is in the code). Skip the '...'.
30622 parameter_declaration is set to NULL_TREE, and
30623 objc_being_catch_clauses() knows that that means
30624 '...'. */
30625 cp_lexer_consume_token (parser->lexer);
30626 parameter_declaration = NULL_TREE;
30627 }
30628 else
30629 {
30630 /* We have "@catch (NSException *exception)" or something
30631 like that. Parse the parameter declaration. */
30632 parm = cp_parser_parameter_declaration (parser, false, NULL);
30633 if (parm == NULL)
30634 parameter_declaration = error_mark_node;
30635 else
30636 parameter_declaration = grokdeclarator (parm->declarator,
30637 &parm->decl_specifiers,
30638 PARM, /*initialized=*/0,
30639 /*attrlist=*/NULL);
30640 }
30641 if (seen_open_paren)
30642 parens.require_close (parser);
30643 else
30644 {
30645 /* If there was no open parenthesis, we are recovering from
30646 an error, and we are trying to figure out what mistake
30647 the user has made. */
30648
30649 /* If there is an immediate closing parenthesis, the user
30650 probably forgot the opening one (ie, they typed "@catch
30651 NSException *e)". Parse the closing parenthesis and keep
30652 going. */
30653 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30654 cp_lexer_consume_token (parser->lexer);
30655
30656 /* If these is no immediate closing parenthesis, the user
30657 probably doesn't know that parenthesis are required at
30658 all (ie, they typed "@catch NSException *e"). So, just
30659 forget about the closing parenthesis and keep going. */
30660 }
30661 objc_begin_catch_clause (parameter_declaration);
30662 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30663 objc_finish_catch_clause ();
30664 }
30665 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30666 {
30667 cp_lexer_consume_token (parser->lexer);
30668 location = cp_lexer_peek_token (parser->lexer)->location;
30669 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30670 node, lest it get absorbed into the surrounding block. */
30671 stmt = push_stmt_list ();
30672 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30673 objc_build_finally_clause (location, pop_stmt_list (stmt));
30674 }
30675
30676 return objc_finish_try_stmt ();
30677 }
30678
30679 /* Parse an Objective-C synchronized statement.
30680
30681 objc-synchronized-stmt:
30682 @synchronized ( expression ) compound-statement
30683
30684 Returns NULL_TREE. */
30685
30686 static tree
30687 cp_parser_objc_synchronized_statement (cp_parser *parser)
30688 {
30689 location_t location;
30690 tree lock, stmt;
30691
30692 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30693
30694 location = cp_lexer_peek_token (parser->lexer)->location;
30695 objc_maybe_warn_exceptions (location);
30696 matching_parens parens;
30697 parens.require_open (parser);
30698 lock = cp_parser_expression (parser);
30699 parens.require_close (parser);
30700
30701 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30702 node, lest it get absorbed into the surrounding block. */
30703 stmt = push_stmt_list ();
30704 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30705
30706 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30707 }
30708
30709 /* Parse an Objective-C throw statement.
30710
30711 objc-throw-stmt:
30712 @throw assignment-expression [opt] ;
30713
30714 Returns a constructed '@throw' statement. */
30715
30716 static tree
30717 cp_parser_objc_throw_statement (cp_parser *parser)
30718 {
30719 tree expr = NULL_TREE;
30720 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30721
30722 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30723
30724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30725 expr = cp_parser_expression (parser);
30726
30727 cp_parser_consume_semicolon_at_end_of_statement (parser);
30728
30729 return objc_build_throw_stmt (loc, expr);
30730 }
30731
30732 /* Parse an Objective-C statement. */
30733
30734 static tree
30735 cp_parser_objc_statement (cp_parser * parser)
30736 {
30737 /* Try to figure out what kind of declaration is present. */
30738 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30739
30740 switch (kwd->keyword)
30741 {
30742 case RID_AT_TRY:
30743 return cp_parser_objc_try_catch_finally_statement (parser);
30744 case RID_AT_SYNCHRONIZED:
30745 return cp_parser_objc_synchronized_statement (parser);
30746 case RID_AT_THROW:
30747 return cp_parser_objc_throw_statement (parser);
30748 default:
30749 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30750 kwd->u.value);
30751 cp_parser_skip_to_end_of_block_or_statement (parser);
30752 }
30753
30754 return error_mark_node;
30755 }
30756
30757 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30758 look ahead to see if an objc keyword follows the attributes. This
30759 is to detect the use of prefix attributes on ObjC @interface and
30760 @protocol. */
30761
30762 static bool
30763 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30764 {
30765 cp_lexer_save_tokens (parser->lexer);
30766 *attrib = cp_parser_attributes_opt (parser);
30767 gcc_assert (*attrib);
30768 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30769 {
30770 cp_lexer_commit_tokens (parser->lexer);
30771 return true;
30772 }
30773 cp_lexer_rollback_tokens (parser->lexer);
30774 return false;
30775 }
30776
30777 /* This routine is a minimal replacement for
30778 c_parser_struct_declaration () used when parsing the list of
30779 types/names or ObjC++ properties. For example, when parsing the
30780 code
30781
30782 @property (readonly) int a, b, c;
30783
30784 this function is responsible for parsing "int a, int b, int c" and
30785 returning the declarations as CHAIN of DECLs.
30786
30787 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30788 similar parsing. */
30789 static tree
30790 cp_parser_objc_struct_declaration (cp_parser *parser)
30791 {
30792 tree decls = NULL_TREE;
30793 cp_decl_specifier_seq declspecs;
30794 int decl_class_or_enum_p;
30795 tree prefix_attributes;
30796
30797 cp_parser_decl_specifier_seq (parser,
30798 CP_PARSER_FLAGS_NONE,
30799 &declspecs,
30800 &decl_class_or_enum_p);
30801
30802 if (declspecs.type == error_mark_node)
30803 return error_mark_node;
30804
30805 /* auto, register, static, extern, mutable. */
30806 if (declspecs.storage_class != sc_none)
30807 {
30808 cp_parser_error (parser, "invalid type for property");
30809 declspecs.storage_class = sc_none;
30810 }
30811
30812 /* thread_local. */
30813 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30814 {
30815 cp_parser_error (parser, "invalid type for property");
30816 declspecs.locations[ds_thread] = 0;
30817 }
30818
30819 /* typedef. */
30820 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30821 {
30822 cp_parser_error (parser, "invalid type for property");
30823 declspecs.locations[ds_typedef] = 0;
30824 }
30825
30826 prefix_attributes = declspecs.attributes;
30827 declspecs.attributes = NULL_TREE;
30828
30829 /* Keep going until we hit the `;' at the end of the declaration. */
30830 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30831 {
30832 tree attributes, first_attribute, decl;
30833 cp_declarator *declarator;
30834 cp_token *token;
30835
30836 /* Parse the declarator. */
30837 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30838 NULL, NULL, false, false);
30839
30840 /* Look for attributes that apply to the ivar. */
30841 attributes = cp_parser_attributes_opt (parser);
30842 /* Remember which attributes are prefix attributes and
30843 which are not. */
30844 first_attribute = attributes;
30845 /* Combine the attributes. */
30846 attributes = attr_chainon (prefix_attributes, attributes);
30847
30848 decl = grokfield (declarator, &declspecs,
30849 NULL_TREE, /*init_const_expr_p=*/false,
30850 NULL_TREE, attributes);
30851
30852 if (decl == error_mark_node || decl == NULL_TREE)
30853 return error_mark_node;
30854
30855 /* Reset PREFIX_ATTRIBUTES. */
30856 if (attributes != error_mark_node)
30857 {
30858 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30859 attributes = TREE_CHAIN (attributes);
30860 if (attributes)
30861 TREE_CHAIN (attributes) = NULL_TREE;
30862 }
30863
30864 DECL_CHAIN (decl) = decls;
30865 decls = decl;
30866
30867 token = cp_lexer_peek_token (parser->lexer);
30868 if (token->type == CPP_COMMA)
30869 {
30870 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30871 continue;
30872 }
30873 else
30874 break;
30875 }
30876 return decls;
30877 }
30878
30879 /* Parse an Objective-C @property declaration. The syntax is:
30880
30881 objc-property-declaration:
30882 '@property' objc-property-attributes[opt] struct-declaration ;
30883
30884 objc-property-attributes:
30885 '(' objc-property-attribute-list ')'
30886
30887 objc-property-attribute-list:
30888 objc-property-attribute
30889 objc-property-attribute-list, objc-property-attribute
30890
30891 objc-property-attribute
30892 'getter' = identifier
30893 'setter' = identifier
30894 'readonly'
30895 'readwrite'
30896 'assign'
30897 'retain'
30898 'copy'
30899 'nonatomic'
30900
30901 For example:
30902 @property NSString *name;
30903 @property (readonly) id object;
30904 @property (retain, nonatomic, getter=getTheName) id name;
30905 @property int a, b, c;
30906
30907 PS: This function is identical to
30908 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30909 static void
30910 cp_parser_objc_at_property_declaration (cp_parser *parser)
30911 {
30912 /* The following variables hold the attributes of the properties as
30913 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30914 seen. When we see an attribute, we set them to 'true' (if they
30915 are boolean properties) or to the identifier (if they have an
30916 argument, ie, for getter and setter). Note that here we only
30917 parse the list of attributes, check the syntax and accumulate the
30918 attributes that we find. objc_add_property_declaration() will
30919 then process the information. */
30920 bool property_assign = false;
30921 bool property_copy = false;
30922 tree property_getter_ident = NULL_TREE;
30923 bool property_nonatomic = false;
30924 bool property_readonly = false;
30925 bool property_readwrite = false;
30926 bool property_retain = false;
30927 tree property_setter_ident = NULL_TREE;
30928
30929 /* 'properties' is the list of properties that we read. Usually a
30930 single one, but maybe more (eg, in "@property int a, b, c;" there
30931 are three). */
30932 tree properties;
30933 location_t loc;
30934
30935 loc = cp_lexer_peek_token (parser->lexer)->location;
30936
30937 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30938
30939 /* Parse the optional attribute list... */
30940 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30941 {
30942 /* Eat the '('. */
30943 matching_parens parens;
30944 parens.consume_open (parser);
30945
30946 while (true)
30947 {
30948 bool syntax_error = false;
30949 cp_token *token = cp_lexer_peek_token (parser->lexer);
30950 enum rid keyword;
30951
30952 if (token->type != CPP_NAME)
30953 {
30954 cp_parser_error (parser, "expected identifier");
30955 break;
30956 }
30957 keyword = C_RID_CODE (token->u.value);
30958 cp_lexer_consume_token (parser->lexer);
30959 switch (keyword)
30960 {
30961 case RID_ASSIGN: property_assign = true; break;
30962 case RID_COPY: property_copy = true; break;
30963 case RID_NONATOMIC: property_nonatomic = true; break;
30964 case RID_READONLY: property_readonly = true; break;
30965 case RID_READWRITE: property_readwrite = true; break;
30966 case RID_RETAIN: property_retain = true; break;
30967
30968 case RID_GETTER:
30969 case RID_SETTER:
30970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30971 {
30972 if (keyword == RID_GETTER)
30973 cp_parser_error (parser,
30974 "missing %<=%> (after %<getter%> attribute)");
30975 else
30976 cp_parser_error (parser,
30977 "missing %<=%> (after %<setter%> attribute)");
30978 syntax_error = true;
30979 break;
30980 }
30981 cp_lexer_consume_token (parser->lexer); /* eat the = */
30982 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30983 {
30984 cp_parser_error (parser, "expected identifier");
30985 syntax_error = true;
30986 break;
30987 }
30988 if (keyword == RID_SETTER)
30989 {
30990 if (property_setter_ident != NULL_TREE)
30991 {
30992 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30993 cp_lexer_consume_token (parser->lexer);
30994 }
30995 else
30996 property_setter_ident = cp_parser_objc_selector (parser);
30997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30998 cp_parser_error (parser, "setter name must terminate with %<:%>");
30999 else
31000 cp_lexer_consume_token (parser->lexer);
31001 }
31002 else
31003 {
31004 if (property_getter_ident != NULL_TREE)
31005 {
31006 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31007 cp_lexer_consume_token (parser->lexer);
31008 }
31009 else
31010 property_getter_ident = cp_parser_objc_selector (parser);
31011 }
31012 break;
31013 default:
31014 cp_parser_error (parser, "unknown property attribute");
31015 syntax_error = true;
31016 break;
31017 }
31018
31019 if (syntax_error)
31020 break;
31021
31022 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31023 cp_lexer_consume_token (parser->lexer);
31024 else
31025 break;
31026 }
31027
31028 /* FIXME: "@property (setter, assign);" will generate a spurious
31029 "error: expected ‘)’ before ‘,’ token". This is because
31030 cp_parser_require, unlike the C counterpart, will produce an
31031 error even if we are in error recovery. */
31032 if (!parens.require_close (parser))
31033 {
31034 cp_parser_skip_to_closing_parenthesis (parser,
31035 /*recovering=*/true,
31036 /*or_comma=*/false,
31037 /*consume_paren=*/true);
31038 }
31039 }
31040
31041 /* ... and the property declaration(s). */
31042 properties = cp_parser_objc_struct_declaration (parser);
31043
31044 if (properties == error_mark_node)
31045 {
31046 cp_parser_skip_to_end_of_statement (parser);
31047 /* If the next token is now a `;', consume it. */
31048 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31049 cp_lexer_consume_token (parser->lexer);
31050 return;
31051 }
31052
31053 if (properties == NULL_TREE)
31054 cp_parser_error (parser, "expected identifier");
31055 else
31056 {
31057 /* Comma-separated properties are chained together in
31058 reverse order; add them one by one. */
31059 properties = nreverse (properties);
31060
31061 for (; properties; properties = TREE_CHAIN (properties))
31062 objc_add_property_declaration (loc, copy_node (properties),
31063 property_readonly, property_readwrite,
31064 property_assign, property_retain,
31065 property_copy, property_nonatomic,
31066 property_getter_ident, property_setter_ident);
31067 }
31068
31069 cp_parser_consume_semicolon_at_end_of_statement (parser);
31070 }
31071
31072 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31073
31074 objc-synthesize-declaration:
31075 @synthesize objc-synthesize-identifier-list ;
31076
31077 objc-synthesize-identifier-list:
31078 objc-synthesize-identifier
31079 objc-synthesize-identifier-list, objc-synthesize-identifier
31080
31081 objc-synthesize-identifier
31082 identifier
31083 identifier = identifier
31084
31085 For example:
31086 @synthesize MyProperty;
31087 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31088
31089 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31090 for C. Keep them in sync.
31091 */
31092 static void
31093 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31094 {
31095 tree list = NULL_TREE;
31096 location_t loc;
31097 loc = cp_lexer_peek_token (parser->lexer)->location;
31098
31099 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31100 while (true)
31101 {
31102 tree property, ivar;
31103 property = cp_parser_identifier (parser);
31104 if (property == error_mark_node)
31105 {
31106 cp_parser_consume_semicolon_at_end_of_statement (parser);
31107 return;
31108 }
31109 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31110 {
31111 cp_lexer_consume_token (parser->lexer);
31112 ivar = cp_parser_identifier (parser);
31113 if (ivar == error_mark_node)
31114 {
31115 cp_parser_consume_semicolon_at_end_of_statement (parser);
31116 return;
31117 }
31118 }
31119 else
31120 ivar = NULL_TREE;
31121 list = chainon (list, build_tree_list (ivar, property));
31122 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31123 cp_lexer_consume_token (parser->lexer);
31124 else
31125 break;
31126 }
31127 cp_parser_consume_semicolon_at_end_of_statement (parser);
31128 objc_add_synthesize_declaration (loc, list);
31129 }
31130
31131 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31132
31133 objc-dynamic-declaration:
31134 @dynamic identifier-list ;
31135
31136 For example:
31137 @dynamic MyProperty;
31138 @dynamic MyProperty, AnotherProperty;
31139
31140 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31141 for C. Keep them in sync.
31142 */
31143 static void
31144 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31145 {
31146 tree list = NULL_TREE;
31147 location_t loc;
31148 loc = cp_lexer_peek_token (parser->lexer)->location;
31149
31150 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31151 while (true)
31152 {
31153 tree property;
31154 property = cp_parser_identifier (parser);
31155 if (property == error_mark_node)
31156 {
31157 cp_parser_consume_semicolon_at_end_of_statement (parser);
31158 return;
31159 }
31160 list = chainon (list, build_tree_list (NULL, property));
31161 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31162 cp_lexer_consume_token (parser->lexer);
31163 else
31164 break;
31165 }
31166 cp_parser_consume_semicolon_at_end_of_statement (parser);
31167 objc_add_dynamic_declaration (loc, list);
31168 }
31169
31170 \f
31171 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31172
31173 /* Returns name of the next clause.
31174 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31175 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31176 returned and the token is consumed. */
31177
31178 static pragma_omp_clause
31179 cp_parser_omp_clause_name (cp_parser *parser)
31180 {
31181 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31182
31183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31184 result = PRAGMA_OACC_CLAUSE_AUTO;
31185 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31186 result = PRAGMA_OMP_CLAUSE_IF;
31187 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31188 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31189 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31190 result = PRAGMA_OACC_CLAUSE_DELETE;
31191 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31192 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31193 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31194 result = PRAGMA_OMP_CLAUSE_FOR;
31195 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31196 {
31197 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31198 const char *p = IDENTIFIER_POINTER (id);
31199
31200 switch (p[0])
31201 {
31202 case 'a':
31203 if (!strcmp ("aligned", p))
31204 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31205 else if (!strcmp ("async", p))
31206 result = PRAGMA_OACC_CLAUSE_ASYNC;
31207 break;
31208 case 'c':
31209 if (!strcmp ("collapse", p))
31210 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31211 else if (!strcmp ("copy", p))
31212 result = PRAGMA_OACC_CLAUSE_COPY;
31213 else if (!strcmp ("copyin", p))
31214 result = PRAGMA_OMP_CLAUSE_COPYIN;
31215 else if (!strcmp ("copyout", p))
31216 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31217 else if (!strcmp ("copyprivate", p))
31218 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31219 else if (!strcmp ("create", p))
31220 result = PRAGMA_OACC_CLAUSE_CREATE;
31221 break;
31222 case 'd':
31223 if (!strcmp ("defaultmap", p))
31224 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31225 else if (!strcmp ("depend", p))
31226 result = PRAGMA_OMP_CLAUSE_DEPEND;
31227 else if (!strcmp ("device", p))
31228 result = PRAGMA_OMP_CLAUSE_DEVICE;
31229 else if (!strcmp ("deviceptr", p))
31230 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31231 else if (!strcmp ("device_resident", p))
31232 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31233 else if (!strcmp ("dist_schedule", p))
31234 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31235 break;
31236 case 'f':
31237 if (!strcmp ("final", p))
31238 result = PRAGMA_OMP_CLAUSE_FINAL;
31239 else if (!strcmp ("firstprivate", p))
31240 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31241 else if (!strcmp ("from", p))
31242 result = PRAGMA_OMP_CLAUSE_FROM;
31243 break;
31244 case 'g':
31245 if (!strcmp ("gang", p))
31246 result = PRAGMA_OACC_CLAUSE_GANG;
31247 else if (!strcmp ("grainsize", p))
31248 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31249 break;
31250 case 'h':
31251 if (!strcmp ("hint", p))
31252 result = PRAGMA_OMP_CLAUSE_HINT;
31253 else if (!strcmp ("host", p))
31254 result = PRAGMA_OACC_CLAUSE_HOST;
31255 break;
31256 case 'i':
31257 if (!strcmp ("inbranch", p))
31258 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31259 else if (!strcmp ("independent", p))
31260 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31261 else if (!strcmp ("is_device_ptr", p))
31262 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31263 break;
31264 case 'l':
31265 if (!strcmp ("lastprivate", p))
31266 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31267 else if (!strcmp ("linear", p))
31268 result = PRAGMA_OMP_CLAUSE_LINEAR;
31269 else if (!strcmp ("link", p))
31270 result = PRAGMA_OMP_CLAUSE_LINK;
31271 break;
31272 case 'm':
31273 if (!strcmp ("map", p))
31274 result = PRAGMA_OMP_CLAUSE_MAP;
31275 else if (!strcmp ("mergeable", p))
31276 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31277 break;
31278 case 'n':
31279 if (!strcmp ("nogroup", p))
31280 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31281 else if (!strcmp ("notinbranch", p))
31282 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31283 else if (!strcmp ("nowait", p))
31284 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31285 else if (!strcmp ("num_gangs", p))
31286 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31287 else if (!strcmp ("num_tasks", p))
31288 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31289 else if (!strcmp ("num_teams", p))
31290 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31291 else if (!strcmp ("num_threads", p))
31292 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31293 else if (!strcmp ("num_workers", p))
31294 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31295 break;
31296 case 'o':
31297 if (!strcmp ("ordered", p))
31298 result = PRAGMA_OMP_CLAUSE_ORDERED;
31299 break;
31300 case 'p':
31301 if (!strcmp ("parallel", p))
31302 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31303 else if (!strcmp ("present", p))
31304 result = PRAGMA_OACC_CLAUSE_PRESENT;
31305 else if (!strcmp ("present_or_copy", p)
31306 || !strcmp ("pcopy", p))
31307 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31308 else if (!strcmp ("present_or_copyin", p)
31309 || !strcmp ("pcopyin", p))
31310 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31311 else if (!strcmp ("present_or_copyout", p)
31312 || !strcmp ("pcopyout", p))
31313 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31314 else if (!strcmp ("present_or_create", p)
31315 || !strcmp ("pcreate", p))
31316 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31317 else if (!strcmp ("priority", p))
31318 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31319 else if (!strcmp ("proc_bind", p))
31320 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31321 break;
31322 case 'r':
31323 if (!strcmp ("reduction", p))
31324 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31325 break;
31326 case 's':
31327 if (!strcmp ("safelen", p))
31328 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31329 else if (!strcmp ("schedule", p))
31330 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31331 else if (!strcmp ("sections", p))
31332 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31333 else if (!strcmp ("self", p))
31334 result = PRAGMA_OACC_CLAUSE_SELF;
31335 else if (!strcmp ("seq", p))
31336 result = PRAGMA_OACC_CLAUSE_SEQ;
31337 else if (!strcmp ("shared", p))
31338 result = PRAGMA_OMP_CLAUSE_SHARED;
31339 else if (!strcmp ("simd", p))
31340 result = PRAGMA_OMP_CLAUSE_SIMD;
31341 else if (!strcmp ("simdlen", p))
31342 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31343 break;
31344 case 't':
31345 if (!strcmp ("taskgroup", p))
31346 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31347 else if (!strcmp ("thread_limit", p))
31348 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31349 else if (!strcmp ("threads", p))
31350 result = PRAGMA_OMP_CLAUSE_THREADS;
31351 else if (!strcmp ("tile", p))
31352 result = PRAGMA_OACC_CLAUSE_TILE;
31353 else if (!strcmp ("to", p))
31354 result = PRAGMA_OMP_CLAUSE_TO;
31355 break;
31356 case 'u':
31357 if (!strcmp ("uniform", p))
31358 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31359 else if (!strcmp ("untied", p))
31360 result = PRAGMA_OMP_CLAUSE_UNTIED;
31361 else if (!strcmp ("use_device", p))
31362 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31363 else if (!strcmp ("use_device_ptr", p))
31364 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31365 break;
31366 case 'v':
31367 if (!strcmp ("vector", p))
31368 result = PRAGMA_OACC_CLAUSE_VECTOR;
31369 else if (!strcmp ("vector_length", p))
31370 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31371 break;
31372 case 'w':
31373 if (!strcmp ("wait", p))
31374 result = PRAGMA_OACC_CLAUSE_WAIT;
31375 else if (!strcmp ("worker", p))
31376 result = PRAGMA_OACC_CLAUSE_WORKER;
31377 break;
31378 }
31379 }
31380
31381 if (result != PRAGMA_OMP_CLAUSE_NONE)
31382 cp_lexer_consume_token (parser->lexer);
31383
31384 return result;
31385 }
31386
31387 /* Validate that a clause of the given type does not already exist. */
31388
31389 static void
31390 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31391 const char *name, location_t location)
31392 {
31393 tree c;
31394
31395 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31396 if (OMP_CLAUSE_CODE (c) == code)
31397 {
31398 error_at (location, "too many %qs clauses", name);
31399 break;
31400 }
31401 }
31402
31403 /* OpenMP 2.5:
31404 variable-list:
31405 identifier
31406 variable-list , identifier
31407
31408 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31409 colon). An opening parenthesis will have been consumed by the caller.
31410
31411 If KIND is nonzero, create the appropriate node and install the decl
31412 in OMP_CLAUSE_DECL and add the node to the head of the list.
31413
31414 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31415 return the list created.
31416
31417 COLON can be NULL if only closing parenthesis should end the list,
31418 or pointer to bool which will receive false if the list is terminated
31419 by closing parenthesis or true if the list is terminated by colon. */
31420
31421 static tree
31422 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31423 tree list, bool *colon)
31424 {
31425 cp_token *token;
31426 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31427 if (colon)
31428 {
31429 parser->colon_corrects_to_scope_p = false;
31430 *colon = false;
31431 }
31432 while (1)
31433 {
31434 tree name, decl;
31435
31436 token = cp_lexer_peek_token (parser->lexer);
31437 if (kind != 0
31438 && current_class_ptr
31439 && cp_parser_is_keyword (token, RID_THIS))
31440 {
31441 decl = finish_this_expr ();
31442 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31443 || CONVERT_EXPR_P (decl))
31444 decl = TREE_OPERAND (decl, 0);
31445 cp_lexer_consume_token (parser->lexer);
31446 }
31447 else
31448 {
31449 name = cp_parser_id_expression (parser, /*template_p=*/false,
31450 /*check_dependency_p=*/true,
31451 /*template_p=*/NULL,
31452 /*declarator_p=*/false,
31453 /*optional_p=*/false);
31454 if (name == error_mark_node)
31455 goto skip_comma;
31456
31457 if (identifier_p (name))
31458 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31459 else
31460 decl = name;
31461 if (decl == error_mark_node)
31462 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31463 token->location);
31464 }
31465 if (decl == error_mark_node)
31466 ;
31467 else if (kind != 0)
31468 {
31469 switch (kind)
31470 {
31471 case OMP_CLAUSE__CACHE_:
31472 /* The OpenACC cache directive explicitly only allows "array
31473 elements or subarrays". */
31474 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31475 {
31476 error_at (token->location, "expected %<[%>");
31477 decl = error_mark_node;
31478 break;
31479 }
31480 /* FALLTHROUGH. */
31481 case OMP_CLAUSE_MAP:
31482 case OMP_CLAUSE_FROM:
31483 case OMP_CLAUSE_TO:
31484 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31485 {
31486 location_t loc
31487 = cp_lexer_peek_token (parser->lexer)->location;
31488 cp_id_kind idk = CP_ID_KIND_NONE;
31489 cp_lexer_consume_token (parser->lexer);
31490 decl = convert_from_reference (decl);
31491 decl
31492 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31493 decl, false,
31494 &idk, loc);
31495 }
31496 /* FALLTHROUGH. */
31497 case OMP_CLAUSE_DEPEND:
31498 case OMP_CLAUSE_REDUCTION:
31499 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31500 {
31501 tree low_bound = NULL_TREE, length = NULL_TREE;
31502
31503 parser->colon_corrects_to_scope_p = false;
31504 cp_lexer_consume_token (parser->lexer);
31505 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31506 low_bound = cp_parser_expression (parser);
31507 if (!colon)
31508 parser->colon_corrects_to_scope_p
31509 = saved_colon_corrects_to_scope_p;
31510 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31511 length = integer_one_node;
31512 else
31513 {
31514 /* Look for `:'. */
31515 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31516 goto skip_comma;
31517 if (!cp_lexer_next_token_is (parser->lexer,
31518 CPP_CLOSE_SQUARE))
31519 length = cp_parser_expression (parser);
31520 }
31521 /* Look for the closing `]'. */
31522 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31523 RT_CLOSE_SQUARE))
31524 goto skip_comma;
31525
31526 decl = tree_cons (low_bound, length, decl);
31527 }
31528 break;
31529 default:
31530 break;
31531 }
31532
31533 tree u = build_omp_clause (token->location, kind);
31534 OMP_CLAUSE_DECL (u) = decl;
31535 OMP_CLAUSE_CHAIN (u) = list;
31536 list = u;
31537 }
31538 else
31539 list = tree_cons (decl, NULL_TREE, list);
31540
31541 get_comma:
31542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31543 break;
31544 cp_lexer_consume_token (parser->lexer);
31545 }
31546
31547 if (colon)
31548 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31549
31550 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31551 {
31552 *colon = true;
31553 cp_parser_require (parser, CPP_COLON, RT_COLON);
31554 return list;
31555 }
31556
31557 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31558 {
31559 int ending;
31560
31561 /* Try to resync to an unnested comma. Copied from
31562 cp_parser_parenthesized_expression_list. */
31563 skip_comma:
31564 if (colon)
31565 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31566 ending = cp_parser_skip_to_closing_parenthesis (parser,
31567 /*recovering=*/true,
31568 /*or_comma=*/true,
31569 /*consume_paren=*/true);
31570 if (ending < 0)
31571 goto get_comma;
31572 }
31573
31574 return list;
31575 }
31576
31577 /* Similarly, but expect leading and trailing parenthesis. This is a very
31578 common case for omp clauses. */
31579
31580 static tree
31581 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31582 {
31583 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31584 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31585 return list;
31586 }
31587
31588 /* OpenACC 2.0:
31589 copy ( variable-list )
31590 copyin ( variable-list )
31591 copyout ( variable-list )
31592 create ( variable-list )
31593 delete ( variable-list )
31594 present ( variable-list )
31595 present_or_copy ( variable-list )
31596 pcopy ( variable-list )
31597 present_or_copyin ( variable-list )
31598 pcopyin ( variable-list )
31599 present_or_copyout ( variable-list )
31600 pcopyout ( variable-list )
31601 present_or_create ( variable-list )
31602 pcreate ( variable-list ) */
31603
31604 static tree
31605 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31606 tree list)
31607 {
31608 enum gomp_map_kind kind;
31609 switch (c_kind)
31610 {
31611 case PRAGMA_OACC_CLAUSE_COPY:
31612 kind = GOMP_MAP_FORCE_TOFROM;
31613 break;
31614 case PRAGMA_OACC_CLAUSE_COPYIN:
31615 kind = GOMP_MAP_FORCE_TO;
31616 break;
31617 case PRAGMA_OACC_CLAUSE_COPYOUT:
31618 kind = GOMP_MAP_FORCE_FROM;
31619 break;
31620 case PRAGMA_OACC_CLAUSE_CREATE:
31621 kind = GOMP_MAP_FORCE_ALLOC;
31622 break;
31623 case PRAGMA_OACC_CLAUSE_DELETE:
31624 kind = GOMP_MAP_DELETE;
31625 break;
31626 case PRAGMA_OACC_CLAUSE_DEVICE:
31627 kind = GOMP_MAP_FORCE_TO;
31628 break;
31629 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31630 kind = GOMP_MAP_DEVICE_RESIDENT;
31631 break;
31632 case PRAGMA_OACC_CLAUSE_HOST:
31633 case PRAGMA_OACC_CLAUSE_SELF:
31634 kind = GOMP_MAP_FORCE_FROM;
31635 break;
31636 case PRAGMA_OACC_CLAUSE_LINK:
31637 kind = GOMP_MAP_LINK;
31638 break;
31639 case PRAGMA_OACC_CLAUSE_PRESENT:
31640 kind = GOMP_MAP_FORCE_PRESENT;
31641 break;
31642 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31643 kind = GOMP_MAP_TOFROM;
31644 break;
31645 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31646 kind = GOMP_MAP_TO;
31647 break;
31648 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31649 kind = GOMP_MAP_FROM;
31650 break;
31651 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31652 kind = GOMP_MAP_ALLOC;
31653 break;
31654 default:
31655 gcc_unreachable ();
31656 }
31657 tree nl, c;
31658 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31659
31660 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31661 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31662
31663 return nl;
31664 }
31665
31666 /* OpenACC 2.0:
31667 deviceptr ( variable-list ) */
31668
31669 static tree
31670 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31671 {
31672 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31673 tree vars, t;
31674
31675 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31676 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31677 variable-list must only allow for pointer variables. */
31678 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31679 for (t = vars; t; t = TREE_CHAIN (t))
31680 {
31681 tree v = TREE_PURPOSE (t);
31682 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31683 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31684 OMP_CLAUSE_DECL (u) = v;
31685 OMP_CLAUSE_CHAIN (u) = list;
31686 list = u;
31687 }
31688
31689 return list;
31690 }
31691
31692 /* OpenACC 2.0:
31693 auto
31694 independent
31695 nohost
31696 seq */
31697
31698 static tree
31699 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31700 enum omp_clause_code code,
31701 tree list, location_t location)
31702 {
31703 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31704 tree c = build_omp_clause (location, code);
31705 OMP_CLAUSE_CHAIN (c) = list;
31706 return c;
31707 }
31708
31709 /* OpenACC:
31710 num_gangs ( expression )
31711 num_workers ( expression )
31712 vector_length ( expression ) */
31713
31714 static tree
31715 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31716 const char *str, tree list)
31717 {
31718 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31719
31720 matching_parens parens;
31721 if (!parens.require_open (parser))
31722 return list;
31723
31724 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31725
31726 if (t == error_mark_node
31727 || !parens.require_close (parser))
31728 {
31729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31730 /*or_comma=*/false,
31731 /*consume_paren=*/true);
31732 return list;
31733 }
31734
31735 check_no_duplicate_clause (list, code, str, loc);
31736
31737 tree c = build_omp_clause (loc, code);
31738 OMP_CLAUSE_OPERAND (c, 0) = t;
31739 OMP_CLAUSE_CHAIN (c) = list;
31740 return c;
31741 }
31742
31743 /* OpenACC:
31744
31745 gang [( gang-arg-list )]
31746 worker [( [num:] int-expr )]
31747 vector [( [length:] int-expr )]
31748
31749 where gang-arg is one of:
31750
31751 [num:] int-expr
31752 static: size-expr
31753
31754 and size-expr may be:
31755
31756 *
31757 int-expr
31758 */
31759
31760 static tree
31761 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31762 const char *str, tree list)
31763 {
31764 const char *id = "num";
31765 cp_lexer *lexer = parser->lexer;
31766 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31767 location_t loc = cp_lexer_peek_token (lexer)->location;
31768
31769 if (kind == OMP_CLAUSE_VECTOR)
31770 id = "length";
31771
31772 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31773 {
31774 matching_parens parens;
31775 parens.consume_open (parser);
31776
31777 do
31778 {
31779 cp_token *next = cp_lexer_peek_token (lexer);
31780 int idx = 0;
31781
31782 /* Gang static argument. */
31783 if (kind == OMP_CLAUSE_GANG
31784 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31785 {
31786 cp_lexer_consume_token (lexer);
31787
31788 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31789 goto cleanup_error;
31790
31791 idx = 1;
31792 if (ops[idx] != NULL)
31793 {
31794 cp_parser_error (parser, "too many %<static%> arguments");
31795 goto cleanup_error;
31796 }
31797
31798 /* Check for the '*' argument. */
31799 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31800 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31801 || cp_lexer_nth_token_is (parser->lexer, 2,
31802 CPP_CLOSE_PAREN)))
31803 {
31804 cp_lexer_consume_token (lexer);
31805 ops[idx] = integer_minus_one_node;
31806
31807 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31808 {
31809 cp_lexer_consume_token (lexer);
31810 continue;
31811 }
31812 else break;
31813 }
31814 }
31815 /* Worker num: argument and vector length: arguments. */
31816 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31817 && id_equal (next->u.value, id)
31818 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31819 {
31820 cp_lexer_consume_token (lexer); /* id */
31821 cp_lexer_consume_token (lexer); /* ':' */
31822 }
31823
31824 /* Now collect the actual argument. */
31825 if (ops[idx] != NULL_TREE)
31826 {
31827 cp_parser_error (parser, "unexpected argument");
31828 goto cleanup_error;
31829 }
31830
31831 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31832 false);
31833 if (expr == error_mark_node)
31834 goto cleanup_error;
31835
31836 mark_exp_read (expr);
31837 ops[idx] = expr;
31838
31839 if (kind == OMP_CLAUSE_GANG
31840 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31841 {
31842 cp_lexer_consume_token (lexer);
31843 continue;
31844 }
31845 break;
31846 }
31847 while (1);
31848
31849 if (!parens.require_close (parser))
31850 goto cleanup_error;
31851 }
31852
31853 check_no_duplicate_clause (list, kind, str, loc);
31854
31855 c = build_omp_clause (loc, kind);
31856
31857 if (ops[1])
31858 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31859
31860 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31861 OMP_CLAUSE_CHAIN (c) = list;
31862
31863 return c;
31864
31865 cleanup_error:
31866 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31867 return list;
31868 }
31869
31870 /* OpenACC 2.0:
31871 tile ( size-expr-list ) */
31872
31873 static tree
31874 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31875 {
31876 tree c, expr = error_mark_node;
31877 tree tile = NULL_TREE;
31878
31879 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31880 so, but the spec authors never considered such a case and have
31881 differing opinions on what it might mean, including 'not
31882 allowed'.) */
31883 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31884 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31885 clause_loc);
31886
31887 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31888 return list;
31889
31890 do
31891 {
31892 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31893 return list;
31894
31895 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31896 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31897 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31898 {
31899 cp_lexer_consume_token (parser->lexer);
31900 expr = integer_zero_node;
31901 }
31902 else
31903 expr = cp_parser_constant_expression (parser);
31904
31905 tile = tree_cons (NULL_TREE, expr, tile);
31906 }
31907 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31908
31909 /* Consume the trailing ')'. */
31910 cp_lexer_consume_token (parser->lexer);
31911
31912 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31913 tile = nreverse (tile);
31914 OMP_CLAUSE_TILE_LIST (c) = tile;
31915 OMP_CLAUSE_CHAIN (c) = list;
31916 return c;
31917 }
31918
31919 /* OpenACC 2.0
31920 Parse wait clause or directive parameters. */
31921
31922 static tree
31923 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31924 {
31925 vec<tree, va_gc> *args;
31926 tree t, args_tree;
31927
31928 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31929 /*cast_p=*/false,
31930 /*allow_expansion_p=*/true,
31931 /*non_constant_p=*/NULL);
31932
31933 if (args == NULL || args->length () == 0)
31934 {
31935 cp_parser_error (parser, "expected integer expression before ')'");
31936 if (args != NULL)
31937 release_tree_vector (args);
31938 return list;
31939 }
31940
31941 args_tree = build_tree_list_vec (args);
31942
31943 release_tree_vector (args);
31944
31945 for (t = args_tree; t; t = TREE_CHAIN (t))
31946 {
31947 tree targ = TREE_VALUE (t);
31948
31949 if (targ != error_mark_node)
31950 {
31951 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31952 error ("%<wait%> expression must be integral");
31953 else
31954 {
31955 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31956
31957 targ = mark_rvalue_use (targ);
31958 OMP_CLAUSE_DECL (c) = targ;
31959 OMP_CLAUSE_CHAIN (c) = list;
31960 list = c;
31961 }
31962 }
31963 }
31964
31965 return list;
31966 }
31967
31968 /* OpenACC:
31969 wait ( int-expr-list ) */
31970
31971 static tree
31972 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31973 {
31974 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31975
31976 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31977 return list;
31978
31979 list = cp_parser_oacc_wait_list (parser, location, list);
31980
31981 return list;
31982 }
31983
31984 /* OpenMP 3.0:
31985 collapse ( constant-expression ) */
31986
31987 static tree
31988 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31989 {
31990 tree c, num;
31991 location_t loc;
31992 HOST_WIDE_INT n;
31993
31994 loc = cp_lexer_peek_token (parser->lexer)->location;
31995 matching_parens parens;
31996 if (!parens.require_open (parser))
31997 return list;
31998
31999 num = cp_parser_constant_expression (parser);
32000
32001 if (!parens.require_close (parser))
32002 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32003 /*or_comma=*/false,
32004 /*consume_paren=*/true);
32005
32006 if (num == error_mark_node)
32007 return list;
32008 num = fold_non_dependent_expr (num);
32009 if (!tree_fits_shwi_p (num)
32010 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32011 || (n = tree_to_shwi (num)) <= 0
32012 || (int) n != n)
32013 {
32014 error_at (loc, "collapse argument needs positive constant integer expression");
32015 return list;
32016 }
32017
32018 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32019 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32020 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32021 OMP_CLAUSE_CHAIN (c) = list;
32022 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32023
32024 return c;
32025 }
32026
32027 /* OpenMP 2.5:
32028 default ( none | shared )
32029
32030 OpenACC:
32031 default ( none | present ) */
32032
32033 static tree
32034 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32035 location_t location, bool is_oacc)
32036 {
32037 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32038 tree c;
32039
32040 matching_parens parens;
32041 if (!parens.require_open (parser))
32042 return list;
32043 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32044 {
32045 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32046 const char *p = IDENTIFIER_POINTER (id);
32047
32048 switch (p[0])
32049 {
32050 case 'n':
32051 if (strcmp ("none", p) != 0)
32052 goto invalid_kind;
32053 kind = OMP_CLAUSE_DEFAULT_NONE;
32054 break;
32055
32056 case 'p':
32057 if (strcmp ("present", p) != 0 || !is_oacc)
32058 goto invalid_kind;
32059 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32060 break;
32061
32062 case 's':
32063 if (strcmp ("shared", p) != 0 || is_oacc)
32064 goto invalid_kind;
32065 kind = OMP_CLAUSE_DEFAULT_SHARED;
32066 break;
32067
32068 default:
32069 goto invalid_kind;
32070 }
32071
32072 cp_lexer_consume_token (parser->lexer);
32073 }
32074 else
32075 {
32076 invalid_kind:
32077 if (is_oacc)
32078 cp_parser_error (parser, "expected %<none%> or %<present%>");
32079 else
32080 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32081 }
32082
32083 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32084 || !parens.require_close (parser))
32085 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32086 /*or_comma=*/false,
32087 /*consume_paren=*/true);
32088
32089 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32090 return list;
32091
32092 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32093 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32094 OMP_CLAUSE_CHAIN (c) = list;
32095 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32096
32097 return c;
32098 }
32099
32100 /* OpenMP 3.1:
32101 final ( expression ) */
32102
32103 static tree
32104 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32105 {
32106 tree t, c;
32107
32108 matching_parens parens;
32109 if (!parens.require_open (parser))
32110 return list;
32111
32112 t = cp_parser_condition (parser);
32113
32114 if (t == error_mark_node
32115 || !parens.require_close (parser))
32116 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32117 /*or_comma=*/false,
32118 /*consume_paren=*/true);
32119
32120 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32121
32122 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32123 OMP_CLAUSE_FINAL_EXPR (c) = t;
32124 OMP_CLAUSE_CHAIN (c) = list;
32125
32126 return c;
32127 }
32128
32129 /* OpenMP 2.5:
32130 if ( expression )
32131
32132 OpenMP 4.5:
32133 if ( directive-name-modifier : expression )
32134
32135 directive-name-modifier:
32136 parallel | task | taskloop | target data | target | target update
32137 | target enter data | target exit data */
32138
32139 static tree
32140 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32141 bool is_omp)
32142 {
32143 tree t, c;
32144 enum tree_code if_modifier = ERROR_MARK;
32145
32146 matching_parens parens;
32147 if (!parens.require_open (parser))
32148 return list;
32149
32150 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32151 {
32152 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32153 const char *p = IDENTIFIER_POINTER (id);
32154 int n = 2;
32155
32156 if (strcmp ("parallel", p) == 0)
32157 if_modifier = OMP_PARALLEL;
32158 else if (strcmp ("task", p) == 0)
32159 if_modifier = OMP_TASK;
32160 else if (strcmp ("taskloop", p) == 0)
32161 if_modifier = OMP_TASKLOOP;
32162 else if (strcmp ("target", p) == 0)
32163 {
32164 if_modifier = OMP_TARGET;
32165 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32166 {
32167 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32168 p = IDENTIFIER_POINTER (id);
32169 if (strcmp ("data", p) == 0)
32170 if_modifier = OMP_TARGET_DATA;
32171 else if (strcmp ("update", p) == 0)
32172 if_modifier = OMP_TARGET_UPDATE;
32173 else if (strcmp ("enter", p) == 0)
32174 if_modifier = OMP_TARGET_ENTER_DATA;
32175 else if (strcmp ("exit", p) == 0)
32176 if_modifier = OMP_TARGET_EXIT_DATA;
32177 if (if_modifier != OMP_TARGET)
32178 n = 3;
32179 else
32180 {
32181 location_t loc
32182 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32183 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32184 "or %<exit%>");
32185 if_modifier = ERROR_MARK;
32186 }
32187 if (if_modifier == OMP_TARGET_ENTER_DATA
32188 || if_modifier == OMP_TARGET_EXIT_DATA)
32189 {
32190 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32191 {
32192 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32193 p = IDENTIFIER_POINTER (id);
32194 if (strcmp ("data", p) == 0)
32195 n = 4;
32196 }
32197 if (n != 4)
32198 {
32199 location_t loc
32200 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32201 error_at (loc, "expected %<data%>");
32202 if_modifier = ERROR_MARK;
32203 }
32204 }
32205 }
32206 }
32207 if (if_modifier != ERROR_MARK)
32208 {
32209 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32210 {
32211 while (n-- > 0)
32212 cp_lexer_consume_token (parser->lexer);
32213 }
32214 else
32215 {
32216 if (n > 2)
32217 {
32218 location_t loc
32219 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32220 error_at (loc, "expected %<:%>");
32221 }
32222 if_modifier = ERROR_MARK;
32223 }
32224 }
32225 }
32226
32227 t = cp_parser_condition (parser);
32228
32229 if (t == error_mark_node
32230 || !parens.require_close (parser))
32231 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32232 /*or_comma=*/false,
32233 /*consume_paren=*/true);
32234
32235 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32236 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32237 {
32238 if (if_modifier != ERROR_MARK
32239 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32240 {
32241 const char *p = NULL;
32242 switch (if_modifier)
32243 {
32244 case OMP_PARALLEL: p = "parallel"; break;
32245 case OMP_TASK: p = "task"; break;
32246 case OMP_TASKLOOP: p = "taskloop"; break;
32247 case OMP_TARGET_DATA: p = "target data"; break;
32248 case OMP_TARGET: p = "target"; break;
32249 case OMP_TARGET_UPDATE: p = "target update"; break;
32250 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32251 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32252 default: gcc_unreachable ();
32253 }
32254 error_at (location, "too many %<if%> clauses with %qs modifier",
32255 p);
32256 return list;
32257 }
32258 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32259 {
32260 if (!is_omp)
32261 error_at (location, "too many %<if%> clauses");
32262 else
32263 error_at (location, "too many %<if%> clauses without modifier");
32264 return list;
32265 }
32266 else if (if_modifier == ERROR_MARK
32267 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32268 {
32269 error_at (location, "if any %<if%> clause has modifier, then all "
32270 "%<if%> clauses have to use modifier");
32271 return list;
32272 }
32273 }
32274
32275 c = build_omp_clause (location, OMP_CLAUSE_IF);
32276 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32277 OMP_CLAUSE_IF_EXPR (c) = t;
32278 OMP_CLAUSE_CHAIN (c) = list;
32279
32280 return c;
32281 }
32282
32283 /* OpenMP 3.1:
32284 mergeable */
32285
32286 static tree
32287 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32288 tree list, location_t location)
32289 {
32290 tree c;
32291
32292 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32293 location);
32294
32295 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32296 OMP_CLAUSE_CHAIN (c) = list;
32297 return c;
32298 }
32299
32300 /* OpenMP 2.5:
32301 nowait */
32302
32303 static tree
32304 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32305 tree list, location_t location)
32306 {
32307 tree c;
32308
32309 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32310
32311 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32312 OMP_CLAUSE_CHAIN (c) = list;
32313 return c;
32314 }
32315
32316 /* OpenMP 2.5:
32317 num_threads ( expression ) */
32318
32319 static tree
32320 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32321 location_t location)
32322 {
32323 tree t, c;
32324
32325 matching_parens parens;
32326 if (!parens.require_open (parser))
32327 return list;
32328
32329 t = cp_parser_expression (parser);
32330
32331 if (t == error_mark_node
32332 || !parens.require_close (parser))
32333 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32334 /*or_comma=*/false,
32335 /*consume_paren=*/true);
32336
32337 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32338 "num_threads", location);
32339
32340 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32341 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32342 OMP_CLAUSE_CHAIN (c) = list;
32343
32344 return c;
32345 }
32346
32347 /* OpenMP 4.5:
32348 num_tasks ( expression ) */
32349
32350 static tree
32351 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32352 location_t location)
32353 {
32354 tree t, c;
32355
32356 matching_parens parens;
32357 if (!parens.require_open (parser))
32358 return list;
32359
32360 t = cp_parser_expression (parser);
32361
32362 if (t == error_mark_node
32363 || !parens.require_close (parser))
32364 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32365 /*or_comma=*/false,
32366 /*consume_paren=*/true);
32367
32368 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32369 "num_tasks", location);
32370
32371 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32372 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32373 OMP_CLAUSE_CHAIN (c) = list;
32374
32375 return c;
32376 }
32377
32378 /* OpenMP 4.5:
32379 grainsize ( expression ) */
32380
32381 static tree
32382 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32383 location_t location)
32384 {
32385 tree t, c;
32386
32387 matching_parens parens;
32388 if (!parens.require_open (parser))
32389 return list;
32390
32391 t = cp_parser_expression (parser);
32392
32393 if (t == error_mark_node
32394 || !parens.require_close (parser))
32395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32396 /*or_comma=*/false,
32397 /*consume_paren=*/true);
32398
32399 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32400 "grainsize", location);
32401
32402 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32403 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32404 OMP_CLAUSE_CHAIN (c) = list;
32405
32406 return c;
32407 }
32408
32409 /* OpenMP 4.5:
32410 priority ( expression ) */
32411
32412 static tree
32413 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32414 location_t location)
32415 {
32416 tree t, c;
32417
32418 matching_parens parens;
32419 if (!parens.require_open (parser))
32420 return list;
32421
32422 t = cp_parser_expression (parser);
32423
32424 if (t == error_mark_node
32425 || !parens.require_close (parser))
32426 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32427 /*or_comma=*/false,
32428 /*consume_paren=*/true);
32429
32430 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32431 "priority", location);
32432
32433 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32434 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32435 OMP_CLAUSE_CHAIN (c) = list;
32436
32437 return c;
32438 }
32439
32440 /* OpenMP 4.5:
32441 hint ( expression ) */
32442
32443 static tree
32444 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32445 location_t location)
32446 {
32447 tree t, c;
32448
32449 matching_parens parens;
32450 if (!parens.require_open (parser))
32451 return list;
32452
32453 t = cp_parser_expression (parser);
32454
32455 if (t == error_mark_node
32456 || !parens.require_close (parser))
32457 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32458 /*or_comma=*/false,
32459 /*consume_paren=*/true);
32460
32461 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32462
32463 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32464 OMP_CLAUSE_HINT_EXPR (c) = t;
32465 OMP_CLAUSE_CHAIN (c) = list;
32466
32467 return c;
32468 }
32469
32470 /* OpenMP 4.5:
32471 defaultmap ( tofrom : scalar ) */
32472
32473 static tree
32474 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32475 location_t location)
32476 {
32477 tree c, id;
32478 const char *p;
32479
32480 matching_parens parens;
32481 if (!parens.require_open (parser))
32482 return list;
32483
32484 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32485 {
32486 cp_parser_error (parser, "expected %<tofrom%>");
32487 goto out_err;
32488 }
32489 id = cp_lexer_peek_token (parser->lexer)->u.value;
32490 p = IDENTIFIER_POINTER (id);
32491 if (strcmp (p, "tofrom") != 0)
32492 {
32493 cp_parser_error (parser, "expected %<tofrom%>");
32494 goto out_err;
32495 }
32496 cp_lexer_consume_token (parser->lexer);
32497 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32498 goto out_err;
32499
32500 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32501 {
32502 cp_parser_error (parser, "expected %<scalar%>");
32503 goto out_err;
32504 }
32505 id = cp_lexer_peek_token (parser->lexer)->u.value;
32506 p = IDENTIFIER_POINTER (id);
32507 if (strcmp (p, "scalar") != 0)
32508 {
32509 cp_parser_error (parser, "expected %<scalar%>");
32510 goto out_err;
32511 }
32512 cp_lexer_consume_token (parser->lexer);
32513 if (!parens.require_close (parser))
32514 goto out_err;
32515
32516 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32517 location);
32518
32519 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32520 OMP_CLAUSE_CHAIN (c) = list;
32521 return c;
32522
32523 out_err:
32524 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32525 /*or_comma=*/false,
32526 /*consume_paren=*/true);
32527 return list;
32528 }
32529
32530 /* OpenMP 2.5:
32531 ordered
32532
32533 OpenMP 4.5:
32534 ordered ( constant-expression ) */
32535
32536 static tree
32537 cp_parser_omp_clause_ordered (cp_parser *parser,
32538 tree list, location_t location)
32539 {
32540 tree c, num = NULL_TREE;
32541 HOST_WIDE_INT n;
32542
32543 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32544 "ordered", location);
32545
32546 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32547 {
32548 matching_parens parens;
32549 parens.consume_open (parser);
32550
32551 num = cp_parser_constant_expression (parser);
32552
32553 if (!parens.require_close (parser))
32554 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32555 /*or_comma=*/false,
32556 /*consume_paren=*/true);
32557
32558 if (num == error_mark_node)
32559 return list;
32560 num = fold_non_dependent_expr (num);
32561 if (!tree_fits_shwi_p (num)
32562 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32563 || (n = tree_to_shwi (num)) <= 0
32564 || (int) n != n)
32565 {
32566 error_at (location,
32567 "ordered argument needs positive constant integer "
32568 "expression");
32569 return list;
32570 }
32571 }
32572
32573 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32574 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32575 OMP_CLAUSE_CHAIN (c) = list;
32576 return c;
32577 }
32578
32579 /* OpenMP 2.5:
32580 reduction ( reduction-operator : variable-list )
32581
32582 reduction-operator:
32583 One of: + * - & ^ | && ||
32584
32585 OpenMP 3.1:
32586
32587 reduction-operator:
32588 One of: + * - & ^ | && || min max
32589
32590 OpenMP 4.0:
32591
32592 reduction-operator:
32593 One of: + * - & ^ | && ||
32594 id-expression */
32595
32596 static tree
32597 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32598 {
32599 enum tree_code code = ERROR_MARK;
32600 tree nlist, c, id = NULL_TREE;
32601
32602 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32603 return list;
32604
32605 switch (cp_lexer_peek_token (parser->lexer)->type)
32606 {
32607 case CPP_PLUS: code = PLUS_EXPR; break;
32608 case CPP_MULT: code = MULT_EXPR; break;
32609 case CPP_MINUS: code = MINUS_EXPR; break;
32610 case CPP_AND: code = BIT_AND_EXPR; break;
32611 case CPP_XOR: code = BIT_XOR_EXPR; break;
32612 case CPP_OR: code = BIT_IOR_EXPR; break;
32613 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32614 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32615 default: break;
32616 }
32617
32618 if (code != ERROR_MARK)
32619 cp_lexer_consume_token (parser->lexer);
32620 else
32621 {
32622 bool saved_colon_corrects_to_scope_p;
32623 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32624 parser->colon_corrects_to_scope_p = false;
32625 id = cp_parser_id_expression (parser, /*template_p=*/false,
32626 /*check_dependency_p=*/true,
32627 /*template_p=*/NULL,
32628 /*declarator_p=*/false,
32629 /*optional_p=*/false);
32630 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32631 if (identifier_p (id))
32632 {
32633 const char *p = IDENTIFIER_POINTER (id);
32634
32635 if (strcmp (p, "min") == 0)
32636 code = MIN_EXPR;
32637 else if (strcmp (p, "max") == 0)
32638 code = MAX_EXPR;
32639 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32640 code = PLUS_EXPR;
32641 else if (id == ovl_op_identifier (false, MULT_EXPR))
32642 code = MULT_EXPR;
32643 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32644 code = MINUS_EXPR;
32645 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32646 code = BIT_AND_EXPR;
32647 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32648 code = BIT_IOR_EXPR;
32649 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32650 code = BIT_XOR_EXPR;
32651 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32652 code = TRUTH_ANDIF_EXPR;
32653 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32654 code = TRUTH_ORIF_EXPR;
32655 id = omp_reduction_id (code, id, NULL_TREE);
32656 tree scope = parser->scope;
32657 if (scope)
32658 id = build_qualified_name (NULL_TREE, scope, id, false);
32659 parser->scope = NULL_TREE;
32660 parser->qualifying_scope = NULL_TREE;
32661 parser->object_scope = NULL_TREE;
32662 }
32663 else
32664 {
32665 error ("invalid reduction-identifier");
32666 resync_fail:
32667 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32668 /*or_comma=*/false,
32669 /*consume_paren=*/true);
32670 return list;
32671 }
32672 }
32673
32674 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32675 goto resync_fail;
32676
32677 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32678 NULL);
32679 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32680 {
32681 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32682 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32683 }
32684
32685 return nlist;
32686 }
32687
32688 /* OpenMP 2.5:
32689 schedule ( schedule-kind )
32690 schedule ( schedule-kind , expression )
32691
32692 schedule-kind:
32693 static | dynamic | guided | runtime | auto
32694
32695 OpenMP 4.5:
32696 schedule ( schedule-modifier : schedule-kind )
32697 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32698
32699 schedule-modifier:
32700 simd
32701 monotonic
32702 nonmonotonic */
32703
32704 static tree
32705 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32706 {
32707 tree c, t;
32708 int modifiers = 0, nmodifiers = 0;
32709
32710 matching_parens parens;
32711 if (!parens.require_open (parser))
32712 return list;
32713
32714 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32715
32716 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32717 {
32718 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32719 const char *p = IDENTIFIER_POINTER (id);
32720 if (strcmp ("simd", p) == 0)
32721 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32722 else if (strcmp ("monotonic", p) == 0)
32723 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32724 else if (strcmp ("nonmonotonic", p) == 0)
32725 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32726 else
32727 break;
32728 cp_lexer_consume_token (parser->lexer);
32729 if (nmodifiers++ == 0
32730 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32731 cp_lexer_consume_token (parser->lexer);
32732 else
32733 {
32734 cp_parser_require (parser, CPP_COLON, RT_COLON);
32735 break;
32736 }
32737 }
32738
32739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32740 {
32741 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32742 const char *p = IDENTIFIER_POINTER (id);
32743
32744 switch (p[0])
32745 {
32746 case 'd':
32747 if (strcmp ("dynamic", p) != 0)
32748 goto invalid_kind;
32749 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32750 break;
32751
32752 case 'g':
32753 if (strcmp ("guided", p) != 0)
32754 goto invalid_kind;
32755 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32756 break;
32757
32758 case 'r':
32759 if (strcmp ("runtime", p) != 0)
32760 goto invalid_kind;
32761 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32762 break;
32763
32764 default:
32765 goto invalid_kind;
32766 }
32767 }
32768 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32769 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32770 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32771 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32772 else
32773 goto invalid_kind;
32774 cp_lexer_consume_token (parser->lexer);
32775
32776 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32777 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32778 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32779 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32780 {
32781 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32782 "specified");
32783 modifiers = 0;
32784 }
32785
32786 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32787 {
32788 cp_token *token;
32789 cp_lexer_consume_token (parser->lexer);
32790
32791 token = cp_lexer_peek_token (parser->lexer);
32792 t = cp_parser_assignment_expression (parser);
32793
32794 if (t == error_mark_node)
32795 goto resync_fail;
32796 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32797 error_at (token->location, "schedule %<runtime%> does not take "
32798 "a %<chunk_size%> parameter");
32799 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32800 error_at (token->location, "schedule %<auto%> does not take "
32801 "a %<chunk_size%> parameter");
32802 else
32803 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32804
32805 if (!parens.require_close (parser))
32806 goto resync_fail;
32807 }
32808 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32809 goto resync_fail;
32810
32811 OMP_CLAUSE_SCHEDULE_KIND (c)
32812 = (enum omp_clause_schedule_kind)
32813 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32814
32815 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32816 OMP_CLAUSE_CHAIN (c) = list;
32817 return c;
32818
32819 invalid_kind:
32820 cp_parser_error (parser, "invalid schedule kind");
32821 resync_fail:
32822 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32823 /*or_comma=*/false,
32824 /*consume_paren=*/true);
32825 return list;
32826 }
32827
32828 /* OpenMP 3.0:
32829 untied */
32830
32831 static tree
32832 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32833 tree list, location_t location)
32834 {
32835 tree c;
32836
32837 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32838
32839 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32840 OMP_CLAUSE_CHAIN (c) = list;
32841 return c;
32842 }
32843
32844 /* OpenMP 4.0:
32845 inbranch
32846 notinbranch */
32847
32848 static tree
32849 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32850 tree list, location_t location)
32851 {
32852 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32853 tree c = build_omp_clause (location, code);
32854 OMP_CLAUSE_CHAIN (c) = list;
32855 return c;
32856 }
32857
32858 /* OpenMP 4.0:
32859 parallel
32860 for
32861 sections
32862 taskgroup */
32863
32864 static tree
32865 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32866 enum omp_clause_code code,
32867 tree list, location_t location)
32868 {
32869 tree c = build_omp_clause (location, code);
32870 OMP_CLAUSE_CHAIN (c) = list;
32871 return c;
32872 }
32873
32874 /* OpenMP 4.5:
32875 nogroup */
32876
32877 static tree
32878 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32879 tree list, location_t location)
32880 {
32881 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32882 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32883 OMP_CLAUSE_CHAIN (c) = list;
32884 return c;
32885 }
32886
32887 /* OpenMP 4.5:
32888 simd
32889 threads */
32890
32891 static tree
32892 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32893 enum omp_clause_code code,
32894 tree list, location_t location)
32895 {
32896 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32897 tree c = build_omp_clause (location, code);
32898 OMP_CLAUSE_CHAIN (c) = list;
32899 return c;
32900 }
32901
32902 /* OpenMP 4.0:
32903 num_teams ( expression ) */
32904
32905 static tree
32906 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32907 location_t location)
32908 {
32909 tree t, c;
32910
32911 matching_parens parens;
32912 if (!parens.require_open (parser))
32913 return list;
32914
32915 t = cp_parser_expression (parser);
32916
32917 if (t == error_mark_node
32918 || !parens.require_close (parser))
32919 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32920 /*or_comma=*/false,
32921 /*consume_paren=*/true);
32922
32923 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32924 "num_teams", location);
32925
32926 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32927 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32928 OMP_CLAUSE_CHAIN (c) = list;
32929
32930 return c;
32931 }
32932
32933 /* OpenMP 4.0:
32934 thread_limit ( expression ) */
32935
32936 static tree
32937 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32938 location_t location)
32939 {
32940 tree t, c;
32941
32942 matching_parens parens;
32943 if (!parens.require_open (parser))
32944 return list;
32945
32946 t = cp_parser_expression (parser);
32947
32948 if (t == error_mark_node
32949 || !parens.require_close (parser))
32950 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32951 /*or_comma=*/false,
32952 /*consume_paren=*/true);
32953
32954 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32955 "thread_limit", location);
32956
32957 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32958 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32959 OMP_CLAUSE_CHAIN (c) = list;
32960
32961 return c;
32962 }
32963
32964 /* OpenMP 4.0:
32965 aligned ( variable-list )
32966 aligned ( variable-list : constant-expression ) */
32967
32968 static tree
32969 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32970 {
32971 tree nlist, c, alignment = NULL_TREE;
32972 bool colon;
32973
32974 matching_parens parens;
32975 if (!parens.require_open (parser))
32976 return list;
32977
32978 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32979 &colon);
32980
32981 if (colon)
32982 {
32983 alignment = cp_parser_constant_expression (parser);
32984
32985 if (!parens.require_close (parser))
32986 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32987 /*or_comma=*/false,
32988 /*consume_paren=*/true);
32989
32990 if (alignment == error_mark_node)
32991 alignment = NULL_TREE;
32992 }
32993
32994 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32995 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32996
32997 return nlist;
32998 }
32999
33000 /* OpenMP 4.0:
33001 linear ( variable-list )
33002 linear ( variable-list : expression )
33003
33004 OpenMP 4.5:
33005 linear ( modifier ( variable-list ) )
33006 linear ( modifier ( variable-list ) : expression ) */
33007
33008 static tree
33009 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33010 bool declare_simd)
33011 {
33012 tree nlist, c, step = integer_one_node;
33013 bool colon;
33014 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33015
33016 matching_parens parens;
33017 if (!parens.require_open (parser))
33018 return list;
33019
33020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33021 {
33022 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33023 const char *p = IDENTIFIER_POINTER (id);
33024
33025 if (strcmp ("ref", p) == 0)
33026 kind = OMP_CLAUSE_LINEAR_REF;
33027 else if (strcmp ("val", p) == 0)
33028 kind = OMP_CLAUSE_LINEAR_VAL;
33029 else if (strcmp ("uval", p) == 0)
33030 kind = OMP_CLAUSE_LINEAR_UVAL;
33031 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33032 cp_lexer_consume_token (parser->lexer);
33033 else
33034 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33035 }
33036
33037 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33038 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33039 &colon);
33040 else
33041 {
33042 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33043 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33044 if (colon)
33045 cp_parser_require (parser, CPP_COLON, RT_COLON);
33046 else if (!parens.require_close (parser))
33047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33048 /*or_comma=*/false,
33049 /*consume_paren=*/true);
33050 }
33051
33052 if (colon)
33053 {
33054 step = NULL_TREE;
33055 if (declare_simd
33056 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33057 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33058 {
33059 cp_token *token = cp_lexer_peek_token (parser->lexer);
33060 cp_parser_parse_tentatively (parser);
33061 step = cp_parser_id_expression (parser, /*template_p=*/false,
33062 /*check_dependency_p=*/true,
33063 /*template_p=*/NULL,
33064 /*declarator_p=*/false,
33065 /*optional_p=*/false);
33066 if (step != error_mark_node)
33067 step = cp_parser_lookup_name_simple (parser, step, token->location);
33068 if (step == error_mark_node)
33069 {
33070 step = NULL_TREE;
33071 cp_parser_abort_tentative_parse (parser);
33072 }
33073 else if (!cp_parser_parse_definitely (parser))
33074 step = NULL_TREE;
33075 }
33076 if (!step)
33077 step = cp_parser_expression (parser);
33078
33079 if (!parens.require_close (parser))
33080 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33081 /*or_comma=*/false,
33082 /*consume_paren=*/true);
33083
33084 if (step == error_mark_node)
33085 return list;
33086 }
33087
33088 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33089 {
33090 OMP_CLAUSE_LINEAR_STEP (c) = step;
33091 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33092 }
33093
33094 return nlist;
33095 }
33096
33097 /* OpenMP 4.0:
33098 safelen ( constant-expression ) */
33099
33100 static tree
33101 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33102 location_t location)
33103 {
33104 tree t, c;
33105
33106 matching_parens parens;
33107 if (!parens.require_open (parser))
33108 return list;
33109
33110 t = cp_parser_constant_expression (parser);
33111
33112 if (t == error_mark_node
33113 || !parens.require_close (parser))
33114 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33115 /*or_comma=*/false,
33116 /*consume_paren=*/true);
33117
33118 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33119
33120 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33121 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33122 OMP_CLAUSE_CHAIN (c) = list;
33123
33124 return c;
33125 }
33126
33127 /* OpenMP 4.0:
33128 simdlen ( constant-expression ) */
33129
33130 static tree
33131 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33132 location_t location)
33133 {
33134 tree t, c;
33135
33136 matching_parens parens;
33137 if (!parens.require_open (parser))
33138 return list;
33139
33140 t = cp_parser_constant_expression (parser);
33141
33142 if (t == error_mark_node
33143 || !parens.require_close (parser))
33144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33145 /*or_comma=*/false,
33146 /*consume_paren=*/true);
33147
33148 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33149
33150 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33151 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33152 OMP_CLAUSE_CHAIN (c) = list;
33153
33154 return c;
33155 }
33156
33157 /* OpenMP 4.5:
33158 vec:
33159 identifier [+/- integer]
33160 vec , identifier [+/- integer]
33161 */
33162
33163 static tree
33164 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33165 tree list)
33166 {
33167 tree vec = NULL;
33168
33169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33170 {
33171 cp_parser_error (parser, "expected identifier");
33172 return list;
33173 }
33174
33175 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33176 {
33177 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33178 tree t, identifier = cp_parser_identifier (parser);
33179 tree addend = NULL;
33180
33181 if (identifier == error_mark_node)
33182 t = error_mark_node;
33183 else
33184 {
33185 t = cp_parser_lookup_name_simple
33186 (parser, identifier,
33187 cp_lexer_peek_token (parser->lexer)->location);
33188 if (t == error_mark_node)
33189 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33190 id_loc);
33191 }
33192
33193 bool neg = false;
33194 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33195 neg = true;
33196 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33197 {
33198 addend = integer_zero_node;
33199 goto add_to_vector;
33200 }
33201 cp_lexer_consume_token (parser->lexer);
33202
33203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33204 {
33205 cp_parser_error (parser, "expected integer");
33206 return list;
33207 }
33208
33209 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33210 if (TREE_CODE (addend) != INTEGER_CST)
33211 {
33212 cp_parser_error (parser, "expected integer");
33213 return list;
33214 }
33215 cp_lexer_consume_token (parser->lexer);
33216
33217 add_to_vector:
33218 if (t != error_mark_node)
33219 {
33220 vec = tree_cons (addend, t, vec);
33221 if (neg)
33222 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33223 }
33224
33225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33226 break;
33227
33228 cp_lexer_consume_token (parser->lexer);
33229 }
33230
33231 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33232 {
33233 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33234 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33235 OMP_CLAUSE_DECL (u) = nreverse (vec);
33236 OMP_CLAUSE_CHAIN (u) = list;
33237 return u;
33238 }
33239 return list;
33240 }
33241
33242 /* OpenMP 4.0:
33243 depend ( depend-kind : variable-list )
33244
33245 depend-kind:
33246 in | out | inout
33247
33248 OpenMP 4.5:
33249 depend ( source )
33250
33251 depend ( sink : vec ) */
33252
33253 static tree
33254 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33255 {
33256 tree nlist, c;
33257 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33258
33259 matching_parens parens;
33260 if (!parens.require_open (parser))
33261 return list;
33262
33263 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33264 {
33265 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33266 const char *p = IDENTIFIER_POINTER (id);
33267
33268 if (strcmp ("in", p) == 0)
33269 kind = OMP_CLAUSE_DEPEND_IN;
33270 else if (strcmp ("inout", p) == 0)
33271 kind = OMP_CLAUSE_DEPEND_INOUT;
33272 else if (strcmp ("out", p) == 0)
33273 kind = OMP_CLAUSE_DEPEND_OUT;
33274 else if (strcmp ("source", p) == 0)
33275 kind = OMP_CLAUSE_DEPEND_SOURCE;
33276 else if (strcmp ("sink", p) == 0)
33277 kind = OMP_CLAUSE_DEPEND_SINK;
33278 else
33279 goto invalid_kind;
33280 }
33281 else
33282 goto invalid_kind;
33283
33284 cp_lexer_consume_token (parser->lexer);
33285
33286 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33287 {
33288 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33289 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33290 OMP_CLAUSE_DECL (c) = NULL_TREE;
33291 OMP_CLAUSE_CHAIN (c) = list;
33292 if (!parens.require_close (parser))
33293 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33294 /*or_comma=*/false,
33295 /*consume_paren=*/true);
33296 return c;
33297 }
33298
33299 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33300 goto resync_fail;
33301
33302 if (kind == OMP_CLAUSE_DEPEND_SINK)
33303 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33304 else
33305 {
33306 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33307 list, NULL);
33308
33309 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33310 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33311 }
33312 return nlist;
33313
33314 invalid_kind:
33315 cp_parser_error (parser, "invalid depend kind");
33316 resync_fail:
33317 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33318 /*or_comma=*/false,
33319 /*consume_paren=*/true);
33320 return list;
33321 }
33322
33323 /* OpenMP 4.0:
33324 map ( map-kind : variable-list )
33325 map ( variable-list )
33326
33327 map-kind:
33328 alloc | to | from | tofrom
33329
33330 OpenMP 4.5:
33331 map-kind:
33332 alloc | to | from | tofrom | release | delete
33333
33334 map ( always [,] map-kind: variable-list ) */
33335
33336 static tree
33337 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33338 {
33339 tree nlist, c;
33340 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33341 bool always = false;
33342
33343 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33344 return list;
33345
33346 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33347 {
33348 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33349 const char *p = IDENTIFIER_POINTER (id);
33350
33351 if (strcmp ("always", p) == 0)
33352 {
33353 int nth = 2;
33354 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33355 nth++;
33356 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33357 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33358 == RID_DELETE))
33359 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33360 == CPP_COLON))
33361 {
33362 always = true;
33363 cp_lexer_consume_token (parser->lexer);
33364 if (nth == 3)
33365 cp_lexer_consume_token (parser->lexer);
33366 }
33367 }
33368 }
33369
33370 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33371 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33372 {
33373 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33374 const char *p = IDENTIFIER_POINTER (id);
33375
33376 if (strcmp ("alloc", p) == 0)
33377 kind = GOMP_MAP_ALLOC;
33378 else if (strcmp ("to", p) == 0)
33379 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33380 else if (strcmp ("from", p) == 0)
33381 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33382 else if (strcmp ("tofrom", p) == 0)
33383 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33384 else if (strcmp ("release", p) == 0)
33385 kind = GOMP_MAP_RELEASE;
33386 else
33387 {
33388 cp_parser_error (parser, "invalid map kind");
33389 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33390 /*or_comma=*/false,
33391 /*consume_paren=*/true);
33392 return list;
33393 }
33394 cp_lexer_consume_token (parser->lexer);
33395 cp_lexer_consume_token (parser->lexer);
33396 }
33397 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33398 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33399 {
33400 kind = GOMP_MAP_DELETE;
33401 cp_lexer_consume_token (parser->lexer);
33402 cp_lexer_consume_token (parser->lexer);
33403 }
33404
33405 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33406 NULL);
33407
33408 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33409 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33410
33411 return nlist;
33412 }
33413
33414 /* OpenMP 4.0:
33415 device ( expression ) */
33416
33417 static tree
33418 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33419 location_t location)
33420 {
33421 tree t, c;
33422
33423 matching_parens parens;
33424 if (!parens.require_open (parser))
33425 return list;
33426
33427 t = cp_parser_expression (parser);
33428
33429 if (t == error_mark_node
33430 || !parens.require_close (parser))
33431 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33432 /*or_comma=*/false,
33433 /*consume_paren=*/true);
33434
33435 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33436 "device", location);
33437
33438 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33439 OMP_CLAUSE_DEVICE_ID (c) = t;
33440 OMP_CLAUSE_CHAIN (c) = list;
33441
33442 return c;
33443 }
33444
33445 /* OpenMP 4.0:
33446 dist_schedule ( static )
33447 dist_schedule ( static , expression ) */
33448
33449 static tree
33450 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33451 location_t location)
33452 {
33453 tree c, t;
33454
33455 matching_parens parens;
33456 if (!parens.require_open (parser))
33457 return list;
33458
33459 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33460
33461 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33462 goto invalid_kind;
33463 cp_lexer_consume_token (parser->lexer);
33464
33465 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33466 {
33467 cp_lexer_consume_token (parser->lexer);
33468
33469 t = cp_parser_assignment_expression (parser);
33470
33471 if (t == error_mark_node)
33472 goto resync_fail;
33473 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33474
33475 if (!parens.require_close (parser))
33476 goto resync_fail;
33477 }
33478 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33479 goto resync_fail;
33480
33481 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33482 location);
33483 OMP_CLAUSE_CHAIN (c) = list;
33484 return c;
33485
33486 invalid_kind:
33487 cp_parser_error (parser, "invalid dist_schedule kind");
33488 resync_fail:
33489 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33490 /*or_comma=*/false,
33491 /*consume_paren=*/true);
33492 return list;
33493 }
33494
33495 /* OpenMP 4.0:
33496 proc_bind ( proc-bind-kind )
33497
33498 proc-bind-kind:
33499 master | close | spread */
33500
33501 static tree
33502 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33503 location_t location)
33504 {
33505 tree c;
33506 enum omp_clause_proc_bind_kind kind;
33507
33508 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33509 return list;
33510
33511 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33512 {
33513 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33514 const char *p = IDENTIFIER_POINTER (id);
33515
33516 if (strcmp ("master", p) == 0)
33517 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33518 else if (strcmp ("close", p) == 0)
33519 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33520 else if (strcmp ("spread", p) == 0)
33521 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33522 else
33523 goto invalid_kind;
33524 }
33525 else
33526 goto invalid_kind;
33527
33528 cp_lexer_consume_token (parser->lexer);
33529 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33530 goto resync_fail;
33531
33532 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33533 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33534 location);
33535 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33536 OMP_CLAUSE_CHAIN (c) = list;
33537 return c;
33538
33539 invalid_kind:
33540 cp_parser_error (parser, "invalid depend kind");
33541 resync_fail:
33542 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33543 /*or_comma=*/false,
33544 /*consume_paren=*/true);
33545 return list;
33546 }
33547
33548 /* OpenACC:
33549 async [( int-expr )] */
33550
33551 static tree
33552 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33553 {
33554 tree c, t;
33555 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33556
33557 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33558
33559 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33560 {
33561 matching_parens parens;
33562 parens.consume_open (parser);
33563
33564 t = cp_parser_expression (parser);
33565 if (t == error_mark_node
33566 || !parens.require_close (parser))
33567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33568 /*or_comma=*/false,
33569 /*consume_paren=*/true);
33570 }
33571
33572 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33573
33574 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33575 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33576 OMP_CLAUSE_CHAIN (c) = list;
33577 list = c;
33578
33579 return list;
33580 }
33581
33582 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33583 is a bitmask in MASK. Return the list of clauses found. */
33584
33585 static tree
33586 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33587 const char *where, cp_token *pragma_tok,
33588 bool finish_p = true)
33589 {
33590 tree clauses = NULL;
33591 bool first = true;
33592
33593 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33594 {
33595 location_t here;
33596 pragma_omp_clause c_kind;
33597 omp_clause_code code;
33598 const char *c_name;
33599 tree prev = clauses;
33600
33601 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33602 cp_lexer_consume_token (parser->lexer);
33603
33604 here = cp_lexer_peek_token (parser->lexer)->location;
33605 c_kind = cp_parser_omp_clause_name (parser);
33606
33607 switch (c_kind)
33608 {
33609 case PRAGMA_OACC_CLAUSE_ASYNC:
33610 clauses = cp_parser_oacc_clause_async (parser, clauses);
33611 c_name = "async";
33612 break;
33613 case PRAGMA_OACC_CLAUSE_AUTO:
33614 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33615 clauses, here);
33616 c_name = "auto";
33617 break;
33618 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33619 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33620 c_name = "collapse";
33621 break;
33622 case PRAGMA_OACC_CLAUSE_COPY:
33623 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33624 c_name = "copy";
33625 break;
33626 case PRAGMA_OACC_CLAUSE_COPYIN:
33627 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33628 c_name = "copyin";
33629 break;
33630 case PRAGMA_OACC_CLAUSE_COPYOUT:
33631 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33632 c_name = "copyout";
33633 break;
33634 case PRAGMA_OACC_CLAUSE_CREATE:
33635 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33636 c_name = "create";
33637 break;
33638 case PRAGMA_OACC_CLAUSE_DELETE:
33639 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33640 c_name = "delete";
33641 break;
33642 case PRAGMA_OMP_CLAUSE_DEFAULT:
33643 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33644 c_name = "default";
33645 break;
33646 case PRAGMA_OACC_CLAUSE_DEVICE:
33647 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33648 c_name = "device";
33649 break;
33650 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33651 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33652 c_name = "deviceptr";
33653 break;
33654 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33655 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33656 c_name = "device_resident";
33657 break;
33658 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33659 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33660 clauses);
33661 c_name = "firstprivate";
33662 break;
33663 case PRAGMA_OACC_CLAUSE_GANG:
33664 c_name = "gang";
33665 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33666 c_name, clauses);
33667 break;
33668 case PRAGMA_OACC_CLAUSE_HOST:
33669 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33670 c_name = "host";
33671 break;
33672 case PRAGMA_OACC_CLAUSE_IF:
33673 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33674 c_name = "if";
33675 break;
33676 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33677 clauses = cp_parser_oacc_simple_clause (parser,
33678 OMP_CLAUSE_INDEPENDENT,
33679 clauses, here);
33680 c_name = "independent";
33681 break;
33682 case PRAGMA_OACC_CLAUSE_LINK:
33683 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33684 c_name = "link";
33685 break;
33686 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33687 code = OMP_CLAUSE_NUM_GANGS;
33688 c_name = "num_gangs";
33689 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33690 clauses);
33691 break;
33692 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33693 c_name = "num_workers";
33694 code = OMP_CLAUSE_NUM_WORKERS;
33695 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33696 clauses);
33697 break;
33698 case PRAGMA_OACC_CLAUSE_PRESENT:
33699 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33700 c_name = "present";
33701 break;
33702 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33703 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33704 c_name = "present_or_copy";
33705 break;
33706 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33707 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33708 c_name = "present_or_copyin";
33709 break;
33710 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33711 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33712 c_name = "present_or_copyout";
33713 break;
33714 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33715 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33716 c_name = "present_or_create";
33717 break;
33718 case PRAGMA_OACC_CLAUSE_PRIVATE:
33719 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33720 clauses);
33721 c_name = "private";
33722 break;
33723 case PRAGMA_OACC_CLAUSE_REDUCTION:
33724 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33725 c_name = "reduction";
33726 break;
33727 case PRAGMA_OACC_CLAUSE_SELF:
33728 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33729 c_name = "self";
33730 break;
33731 case PRAGMA_OACC_CLAUSE_SEQ:
33732 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33733 clauses, here);
33734 c_name = "seq";
33735 break;
33736 case PRAGMA_OACC_CLAUSE_TILE:
33737 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33738 c_name = "tile";
33739 break;
33740 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33741 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33742 clauses);
33743 c_name = "use_device";
33744 break;
33745 case PRAGMA_OACC_CLAUSE_VECTOR:
33746 c_name = "vector";
33747 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33748 c_name, clauses);
33749 break;
33750 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33751 c_name = "vector_length";
33752 code = OMP_CLAUSE_VECTOR_LENGTH;
33753 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33754 clauses);
33755 break;
33756 case PRAGMA_OACC_CLAUSE_WAIT:
33757 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33758 c_name = "wait";
33759 break;
33760 case PRAGMA_OACC_CLAUSE_WORKER:
33761 c_name = "worker";
33762 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33763 c_name, clauses);
33764 break;
33765 default:
33766 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33767 goto saw_error;
33768 }
33769
33770 first = false;
33771
33772 if (((mask >> c_kind) & 1) == 0)
33773 {
33774 /* Remove the invalid clause(s) from the list to avoid
33775 confusing the rest of the compiler. */
33776 clauses = prev;
33777 error_at (here, "%qs is not valid for %qs", c_name, where);
33778 }
33779 }
33780
33781 saw_error:
33782 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33783
33784 if (finish_p)
33785 return finish_omp_clauses (clauses, C_ORT_ACC);
33786
33787 return clauses;
33788 }
33789
33790 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33791 is a bitmask in MASK. Return the list of clauses found; the result
33792 of clause default goes in *pdefault. */
33793
33794 static tree
33795 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33796 const char *where, cp_token *pragma_tok,
33797 bool finish_p = true)
33798 {
33799 tree clauses = NULL;
33800 bool first = true;
33801 cp_token *token = NULL;
33802
33803 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33804 {
33805 pragma_omp_clause c_kind;
33806 const char *c_name;
33807 tree prev = clauses;
33808
33809 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33810 cp_lexer_consume_token (parser->lexer);
33811
33812 token = cp_lexer_peek_token (parser->lexer);
33813 c_kind = cp_parser_omp_clause_name (parser);
33814
33815 switch (c_kind)
33816 {
33817 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33818 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33819 token->location);
33820 c_name = "collapse";
33821 break;
33822 case PRAGMA_OMP_CLAUSE_COPYIN:
33823 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33824 c_name = "copyin";
33825 break;
33826 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33827 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33828 clauses);
33829 c_name = "copyprivate";
33830 break;
33831 case PRAGMA_OMP_CLAUSE_DEFAULT:
33832 clauses = cp_parser_omp_clause_default (parser, clauses,
33833 token->location, false);
33834 c_name = "default";
33835 break;
33836 case PRAGMA_OMP_CLAUSE_FINAL:
33837 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33838 c_name = "final";
33839 break;
33840 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33841 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33842 clauses);
33843 c_name = "firstprivate";
33844 break;
33845 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33846 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33847 token->location);
33848 c_name = "grainsize";
33849 break;
33850 case PRAGMA_OMP_CLAUSE_HINT:
33851 clauses = cp_parser_omp_clause_hint (parser, clauses,
33852 token->location);
33853 c_name = "hint";
33854 break;
33855 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33856 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33857 token->location);
33858 c_name = "defaultmap";
33859 break;
33860 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33861 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33862 clauses);
33863 c_name = "use_device_ptr";
33864 break;
33865 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33866 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33867 clauses);
33868 c_name = "is_device_ptr";
33869 break;
33870 case PRAGMA_OMP_CLAUSE_IF:
33871 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33872 true);
33873 c_name = "if";
33874 break;
33875 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33876 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33877 clauses);
33878 c_name = "lastprivate";
33879 break;
33880 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33881 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33882 token->location);
33883 c_name = "mergeable";
33884 break;
33885 case PRAGMA_OMP_CLAUSE_NOWAIT:
33886 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33887 c_name = "nowait";
33888 break;
33889 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33890 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33891 token->location);
33892 c_name = "num_tasks";
33893 break;
33894 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33895 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33896 token->location);
33897 c_name = "num_threads";
33898 break;
33899 case PRAGMA_OMP_CLAUSE_ORDERED:
33900 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33901 token->location);
33902 c_name = "ordered";
33903 break;
33904 case PRAGMA_OMP_CLAUSE_PRIORITY:
33905 clauses = cp_parser_omp_clause_priority (parser, clauses,
33906 token->location);
33907 c_name = "priority";
33908 break;
33909 case PRAGMA_OMP_CLAUSE_PRIVATE:
33910 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33911 clauses);
33912 c_name = "private";
33913 break;
33914 case PRAGMA_OMP_CLAUSE_REDUCTION:
33915 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33916 c_name = "reduction";
33917 break;
33918 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33919 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33920 token->location);
33921 c_name = "schedule";
33922 break;
33923 case PRAGMA_OMP_CLAUSE_SHARED:
33924 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33925 clauses);
33926 c_name = "shared";
33927 break;
33928 case PRAGMA_OMP_CLAUSE_UNTIED:
33929 clauses = cp_parser_omp_clause_untied (parser, clauses,
33930 token->location);
33931 c_name = "untied";
33932 break;
33933 case PRAGMA_OMP_CLAUSE_INBRANCH:
33934 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33935 clauses, token->location);
33936 c_name = "inbranch";
33937 break;
33938 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33939 clauses = cp_parser_omp_clause_branch (parser,
33940 OMP_CLAUSE_NOTINBRANCH,
33941 clauses, token->location);
33942 c_name = "notinbranch";
33943 break;
33944 case PRAGMA_OMP_CLAUSE_PARALLEL:
33945 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33946 clauses, token->location);
33947 c_name = "parallel";
33948 if (!first)
33949 {
33950 clause_not_first:
33951 error_at (token->location, "%qs must be the first clause of %qs",
33952 c_name, where);
33953 clauses = prev;
33954 }
33955 break;
33956 case PRAGMA_OMP_CLAUSE_FOR:
33957 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33958 clauses, token->location);
33959 c_name = "for";
33960 if (!first)
33961 goto clause_not_first;
33962 break;
33963 case PRAGMA_OMP_CLAUSE_SECTIONS:
33964 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33965 clauses, token->location);
33966 c_name = "sections";
33967 if (!first)
33968 goto clause_not_first;
33969 break;
33970 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33971 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33972 clauses, token->location);
33973 c_name = "taskgroup";
33974 if (!first)
33975 goto clause_not_first;
33976 break;
33977 case PRAGMA_OMP_CLAUSE_LINK:
33978 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33979 c_name = "to";
33980 break;
33981 case PRAGMA_OMP_CLAUSE_TO:
33982 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33983 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33984 clauses);
33985 else
33986 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33987 c_name = "to";
33988 break;
33989 case PRAGMA_OMP_CLAUSE_FROM:
33990 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33991 c_name = "from";
33992 break;
33993 case PRAGMA_OMP_CLAUSE_UNIFORM:
33994 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33995 clauses);
33996 c_name = "uniform";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33999 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34000 token->location);
34001 c_name = "num_teams";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34004 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34005 token->location);
34006 c_name = "thread_limit";
34007 break;
34008 case PRAGMA_OMP_CLAUSE_ALIGNED:
34009 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34010 c_name = "aligned";
34011 break;
34012 case PRAGMA_OMP_CLAUSE_LINEAR:
34013 {
34014 bool declare_simd = false;
34015 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34016 declare_simd = true;
34017 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34018 }
34019 c_name = "linear";
34020 break;
34021 case PRAGMA_OMP_CLAUSE_DEPEND:
34022 clauses = cp_parser_omp_clause_depend (parser, clauses,
34023 token->location);
34024 c_name = "depend";
34025 break;
34026 case PRAGMA_OMP_CLAUSE_MAP:
34027 clauses = cp_parser_omp_clause_map (parser, clauses);
34028 c_name = "map";
34029 break;
34030 case PRAGMA_OMP_CLAUSE_DEVICE:
34031 clauses = cp_parser_omp_clause_device (parser, clauses,
34032 token->location);
34033 c_name = "device";
34034 break;
34035 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34036 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34037 token->location);
34038 c_name = "dist_schedule";
34039 break;
34040 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34041 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34042 token->location);
34043 c_name = "proc_bind";
34044 break;
34045 case PRAGMA_OMP_CLAUSE_SAFELEN:
34046 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34047 token->location);
34048 c_name = "safelen";
34049 break;
34050 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34051 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34052 token->location);
34053 c_name = "simdlen";
34054 break;
34055 case PRAGMA_OMP_CLAUSE_NOGROUP:
34056 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34057 token->location);
34058 c_name = "nogroup";
34059 break;
34060 case PRAGMA_OMP_CLAUSE_THREADS:
34061 clauses
34062 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34063 clauses, token->location);
34064 c_name = "threads";
34065 break;
34066 case PRAGMA_OMP_CLAUSE_SIMD:
34067 clauses
34068 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34069 clauses, token->location);
34070 c_name = "simd";
34071 break;
34072 default:
34073 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34074 goto saw_error;
34075 }
34076
34077 first = false;
34078
34079 if (((mask >> c_kind) & 1) == 0)
34080 {
34081 /* Remove the invalid clause(s) from the list to avoid
34082 confusing the rest of the compiler. */
34083 clauses = prev;
34084 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34085 }
34086 }
34087 saw_error:
34088 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34089 if (finish_p)
34090 {
34091 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34092 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34093 else
34094 return finish_omp_clauses (clauses, C_ORT_OMP);
34095 }
34096 return clauses;
34097 }
34098
34099 /* OpenMP 2.5:
34100 structured-block:
34101 statement
34102
34103 In practice, we're also interested in adding the statement to an
34104 outer node. So it is convenient if we work around the fact that
34105 cp_parser_statement calls add_stmt. */
34106
34107 static unsigned
34108 cp_parser_begin_omp_structured_block (cp_parser *parser)
34109 {
34110 unsigned save = parser->in_statement;
34111
34112 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34113 This preserves the "not within loop or switch" style error messages
34114 for nonsense cases like
34115 void foo() {
34116 #pragma omp single
34117 break;
34118 }
34119 */
34120 if (parser->in_statement)
34121 parser->in_statement = IN_OMP_BLOCK;
34122
34123 return save;
34124 }
34125
34126 static void
34127 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34128 {
34129 parser->in_statement = save;
34130 }
34131
34132 static tree
34133 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34134 {
34135 tree stmt = begin_omp_structured_block ();
34136 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34137
34138 cp_parser_statement (parser, NULL_TREE, false, if_p);
34139
34140 cp_parser_end_omp_structured_block (parser, save);
34141 return finish_omp_structured_block (stmt);
34142 }
34143
34144 /* OpenMP 2.5:
34145 # pragma omp atomic new-line
34146 expression-stmt
34147
34148 expression-stmt:
34149 x binop= expr | x++ | ++x | x-- | --x
34150 binop:
34151 +, *, -, /, &, ^, |, <<, >>
34152
34153 where x is an lvalue expression with scalar type.
34154
34155 OpenMP 3.1:
34156 # pragma omp atomic new-line
34157 update-stmt
34158
34159 # pragma omp atomic read new-line
34160 read-stmt
34161
34162 # pragma omp atomic write new-line
34163 write-stmt
34164
34165 # pragma omp atomic update new-line
34166 update-stmt
34167
34168 # pragma omp atomic capture new-line
34169 capture-stmt
34170
34171 # pragma omp atomic capture new-line
34172 capture-block
34173
34174 read-stmt:
34175 v = x
34176 write-stmt:
34177 x = expr
34178 update-stmt:
34179 expression-stmt | x = x binop expr
34180 capture-stmt:
34181 v = expression-stmt
34182 capture-block:
34183 { v = x; update-stmt; } | { update-stmt; v = x; }
34184
34185 OpenMP 4.0:
34186 update-stmt:
34187 expression-stmt | x = x binop expr | x = expr binop x
34188 capture-stmt:
34189 v = update-stmt
34190 capture-block:
34191 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34192
34193 where x and v are lvalue expressions with scalar type. */
34194
34195 static void
34196 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34197 {
34198 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34199 tree rhs1 = NULL_TREE, orig_lhs;
34200 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34201 bool structured_block = false;
34202 bool seq_cst = false;
34203
34204 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34205 {
34206 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34207 const char *p = IDENTIFIER_POINTER (id);
34208
34209 if (!strcmp (p, "seq_cst"))
34210 {
34211 seq_cst = true;
34212 cp_lexer_consume_token (parser->lexer);
34213 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34214 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34215 cp_lexer_consume_token (parser->lexer);
34216 }
34217 }
34218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34219 {
34220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34221 const char *p = IDENTIFIER_POINTER (id);
34222
34223 if (!strcmp (p, "read"))
34224 code = OMP_ATOMIC_READ;
34225 else if (!strcmp (p, "write"))
34226 code = NOP_EXPR;
34227 else if (!strcmp (p, "update"))
34228 code = OMP_ATOMIC;
34229 else if (!strcmp (p, "capture"))
34230 code = OMP_ATOMIC_CAPTURE_NEW;
34231 else
34232 p = NULL;
34233 if (p)
34234 cp_lexer_consume_token (parser->lexer);
34235 }
34236 if (!seq_cst)
34237 {
34238 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34239 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34240 cp_lexer_consume_token (parser->lexer);
34241
34242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34243 {
34244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34245 const char *p = IDENTIFIER_POINTER (id);
34246
34247 if (!strcmp (p, "seq_cst"))
34248 {
34249 seq_cst = true;
34250 cp_lexer_consume_token (parser->lexer);
34251 }
34252 }
34253 }
34254 cp_parser_require_pragma_eol (parser, pragma_tok);
34255
34256 switch (code)
34257 {
34258 case OMP_ATOMIC_READ:
34259 case NOP_EXPR: /* atomic write */
34260 v = cp_parser_unary_expression (parser);
34261 if (v == error_mark_node)
34262 goto saw_error;
34263 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34264 goto saw_error;
34265 if (code == NOP_EXPR)
34266 lhs = cp_parser_expression (parser);
34267 else
34268 lhs = cp_parser_unary_expression (parser);
34269 if (lhs == error_mark_node)
34270 goto saw_error;
34271 if (code == NOP_EXPR)
34272 {
34273 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34274 opcode. */
34275 code = OMP_ATOMIC;
34276 rhs = lhs;
34277 lhs = v;
34278 v = NULL_TREE;
34279 }
34280 goto done;
34281 case OMP_ATOMIC_CAPTURE_NEW:
34282 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34283 {
34284 cp_lexer_consume_token (parser->lexer);
34285 structured_block = true;
34286 }
34287 else
34288 {
34289 v = cp_parser_unary_expression (parser);
34290 if (v == error_mark_node)
34291 goto saw_error;
34292 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34293 goto saw_error;
34294 }
34295 default:
34296 break;
34297 }
34298
34299 restart:
34300 lhs = cp_parser_unary_expression (parser);
34301 orig_lhs = lhs;
34302 switch (TREE_CODE (lhs))
34303 {
34304 case ERROR_MARK:
34305 goto saw_error;
34306
34307 case POSTINCREMENT_EXPR:
34308 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34309 code = OMP_ATOMIC_CAPTURE_OLD;
34310 /* FALLTHROUGH */
34311 case PREINCREMENT_EXPR:
34312 lhs = TREE_OPERAND (lhs, 0);
34313 opcode = PLUS_EXPR;
34314 rhs = integer_one_node;
34315 break;
34316
34317 case POSTDECREMENT_EXPR:
34318 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34319 code = OMP_ATOMIC_CAPTURE_OLD;
34320 /* FALLTHROUGH */
34321 case PREDECREMENT_EXPR:
34322 lhs = TREE_OPERAND (lhs, 0);
34323 opcode = MINUS_EXPR;
34324 rhs = integer_one_node;
34325 break;
34326
34327 case COMPOUND_EXPR:
34328 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34329 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34330 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34331 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34332 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34333 (TREE_OPERAND (lhs, 1), 0), 0)))
34334 == BOOLEAN_TYPE)
34335 /* Undo effects of boolean_increment for post {in,de}crement. */
34336 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34337 /* FALLTHRU */
34338 case MODIFY_EXPR:
34339 if (TREE_CODE (lhs) == MODIFY_EXPR
34340 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34341 {
34342 /* Undo effects of boolean_increment. */
34343 if (integer_onep (TREE_OPERAND (lhs, 1)))
34344 {
34345 /* This is pre or post increment. */
34346 rhs = TREE_OPERAND (lhs, 1);
34347 lhs = TREE_OPERAND (lhs, 0);
34348 opcode = NOP_EXPR;
34349 if (code == OMP_ATOMIC_CAPTURE_NEW
34350 && !structured_block
34351 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34352 code = OMP_ATOMIC_CAPTURE_OLD;
34353 break;
34354 }
34355 }
34356 /* FALLTHRU */
34357 default:
34358 switch (cp_lexer_peek_token (parser->lexer)->type)
34359 {
34360 case CPP_MULT_EQ:
34361 opcode = MULT_EXPR;
34362 break;
34363 case CPP_DIV_EQ:
34364 opcode = TRUNC_DIV_EXPR;
34365 break;
34366 case CPP_PLUS_EQ:
34367 opcode = PLUS_EXPR;
34368 break;
34369 case CPP_MINUS_EQ:
34370 opcode = MINUS_EXPR;
34371 break;
34372 case CPP_LSHIFT_EQ:
34373 opcode = LSHIFT_EXPR;
34374 break;
34375 case CPP_RSHIFT_EQ:
34376 opcode = RSHIFT_EXPR;
34377 break;
34378 case CPP_AND_EQ:
34379 opcode = BIT_AND_EXPR;
34380 break;
34381 case CPP_OR_EQ:
34382 opcode = BIT_IOR_EXPR;
34383 break;
34384 case CPP_XOR_EQ:
34385 opcode = BIT_XOR_EXPR;
34386 break;
34387 case CPP_EQ:
34388 enum cp_parser_prec oprec;
34389 cp_token *token;
34390 cp_lexer_consume_token (parser->lexer);
34391 cp_parser_parse_tentatively (parser);
34392 rhs1 = cp_parser_simple_cast_expression (parser);
34393 if (rhs1 == error_mark_node)
34394 {
34395 cp_parser_abort_tentative_parse (parser);
34396 cp_parser_simple_cast_expression (parser);
34397 goto saw_error;
34398 }
34399 token = cp_lexer_peek_token (parser->lexer);
34400 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34401 {
34402 cp_parser_abort_tentative_parse (parser);
34403 cp_parser_parse_tentatively (parser);
34404 rhs = cp_parser_binary_expression (parser, false, true,
34405 PREC_NOT_OPERATOR, NULL);
34406 if (rhs == error_mark_node)
34407 {
34408 cp_parser_abort_tentative_parse (parser);
34409 cp_parser_binary_expression (parser, false, true,
34410 PREC_NOT_OPERATOR, NULL);
34411 goto saw_error;
34412 }
34413 switch (TREE_CODE (rhs))
34414 {
34415 case MULT_EXPR:
34416 case TRUNC_DIV_EXPR:
34417 case RDIV_EXPR:
34418 case PLUS_EXPR:
34419 case MINUS_EXPR:
34420 case LSHIFT_EXPR:
34421 case RSHIFT_EXPR:
34422 case BIT_AND_EXPR:
34423 case BIT_IOR_EXPR:
34424 case BIT_XOR_EXPR:
34425 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34426 {
34427 if (cp_parser_parse_definitely (parser))
34428 {
34429 opcode = TREE_CODE (rhs);
34430 rhs1 = TREE_OPERAND (rhs, 0);
34431 rhs = TREE_OPERAND (rhs, 1);
34432 goto stmt_done;
34433 }
34434 else
34435 goto saw_error;
34436 }
34437 break;
34438 default:
34439 break;
34440 }
34441 cp_parser_abort_tentative_parse (parser);
34442 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34443 {
34444 rhs = cp_parser_expression (parser);
34445 if (rhs == error_mark_node)
34446 goto saw_error;
34447 opcode = NOP_EXPR;
34448 rhs1 = NULL_TREE;
34449 goto stmt_done;
34450 }
34451 cp_parser_error (parser,
34452 "invalid form of %<#pragma omp atomic%>");
34453 goto saw_error;
34454 }
34455 if (!cp_parser_parse_definitely (parser))
34456 goto saw_error;
34457 switch (token->type)
34458 {
34459 case CPP_SEMICOLON:
34460 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34461 {
34462 code = OMP_ATOMIC_CAPTURE_OLD;
34463 v = lhs;
34464 lhs = NULL_TREE;
34465 lhs1 = rhs1;
34466 rhs1 = NULL_TREE;
34467 cp_lexer_consume_token (parser->lexer);
34468 goto restart;
34469 }
34470 else if (structured_block)
34471 {
34472 opcode = NOP_EXPR;
34473 rhs = rhs1;
34474 rhs1 = NULL_TREE;
34475 goto stmt_done;
34476 }
34477 cp_parser_error (parser,
34478 "invalid form of %<#pragma omp atomic%>");
34479 goto saw_error;
34480 case CPP_MULT:
34481 opcode = MULT_EXPR;
34482 break;
34483 case CPP_DIV:
34484 opcode = TRUNC_DIV_EXPR;
34485 break;
34486 case CPP_PLUS:
34487 opcode = PLUS_EXPR;
34488 break;
34489 case CPP_MINUS:
34490 opcode = MINUS_EXPR;
34491 break;
34492 case CPP_LSHIFT:
34493 opcode = LSHIFT_EXPR;
34494 break;
34495 case CPP_RSHIFT:
34496 opcode = RSHIFT_EXPR;
34497 break;
34498 case CPP_AND:
34499 opcode = BIT_AND_EXPR;
34500 break;
34501 case CPP_OR:
34502 opcode = BIT_IOR_EXPR;
34503 break;
34504 case CPP_XOR:
34505 opcode = BIT_XOR_EXPR;
34506 break;
34507 default:
34508 cp_parser_error (parser,
34509 "invalid operator for %<#pragma omp atomic%>");
34510 goto saw_error;
34511 }
34512 oprec = TOKEN_PRECEDENCE (token);
34513 gcc_assert (oprec != PREC_NOT_OPERATOR);
34514 if (commutative_tree_code (opcode))
34515 oprec = (enum cp_parser_prec) (oprec - 1);
34516 cp_lexer_consume_token (parser->lexer);
34517 rhs = cp_parser_binary_expression (parser, false, false,
34518 oprec, NULL);
34519 if (rhs == error_mark_node)
34520 goto saw_error;
34521 goto stmt_done;
34522 /* FALLTHROUGH */
34523 default:
34524 cp_parser_error (parser,
34525 "invalid operator for %<#pragma omp atomic%>");
34526 goto saw_error;
34527 }
34528 cp_lexer_consume_token (parser->lexer);
34529
34530 rhs = cp_parser_expression (parser);
34531 if (rhs == error_mark_node)
34532 goto saw_error;
34533 break;
34534 }
34535 stmt_done:
34536 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34537 {
34538 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34539 goto saw_error;
34540 v = cp_parser_unary_expression (parser);
34541 if (v == error_mark_node)
34542 goto saw_error;
34543 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34544 goto saw_error;
34545 lhs1 = cp_parser_unary_expression (parser);
34546 if (lhs1 == error_mark_node)
34547 goto saw_error;
34548 }
34549 if (structured_block)
34550 {
34551 cp_parser_consume_semicolon_at_end_of_statement (parser);
34552 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34553 }
34554 done:
34555 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34556 if (!structured_block)
34557 cp_parser_consume_semicolon_at_end_of_statement (parser);
34558 return;
34559
34560 saw_error:
34561 cp_parser_skip_to_end_of_block_or_statement (parser);
34562 if (structured_block)
34563 {
34564 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34565 cp_lexer_consume_token (parser->lexer);
34566 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34567 {
34568 cp_parser_skip_to_end_of_block_or_statement (parser);
34569 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34570 cp_lexer_consume_token (parser->lexer);
34571 }
34572 }
34573 }
34574
34575
34576 /* OpenMP 2.5:
34577 # pragma omp barrier new-line */
34578
34579 static void
34580 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34581 {
34582 cp_parser_require_pragma_eol (parser, pragma_tok);
34583 finish_omp_barrier ();
34584 }
34585
34586 /* OpenMP 2.5:
34587 # pragma omp critical [(name)] new-line
34588 structured-block
34589
34590 OpenMP 4.5:
34591 # pragma omp critical [(name) [hint(expression)]] new-line
34592 structured-block */
34593
34594 #define OMP_CRITICAL_CLAUSE_MASK \
34595 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34596
34597 static tree
34598 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34599 {
34600 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34601
34602 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34603 {
34604 matching_parens parens;
34605 parens.consume_open (parser);
34606
34607 name = cp_parser_identifier (parser);
34608
34609 if (name == error_mark_node
34610 || !parens.require_close (parser))
34611 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34612 /*or_comma=*/false,
34613 /*consume_paren=*/true);
34614 if (name == error_mark_node)
34615 name = NULL;
34616
34617 clauses = cp_parser_omp_all_clauses (parser,
34618 OMP_CRITICAL_CLAUSE_MASK,
34619 "#pragma omp critical", pragma_tok);
34620 }
34621 else
34622 cp_parser_require_pragma_eol (parser, pragma_tok);
34623
34624 stmt = cp_parser_omp_structured_block (parser, if_p);
34625 return c_finish_omp_critical (input_location, stmt, name, clauses);
34626 }
34627
34628 /* OpenMP 2.5:
34629 # pragma omp flush flush-vars[opt] new-line
34630
34631 flush-vars:
34632 ( variable-list ) */
34633
34634 static void
34635 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34636 {
34637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34638 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34639 cp_parser_require_pragma_eol (parser, pragma_tok);
34640
34641 finish_omp_flush ();
34642 }
34643
34644 /* Helper function, to parse omp for increment expression. */
34645
34646 static tree
34647 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34648 {
34649 tree cond = cp_parser_binary_expression (parser, false, true,
34650 PREC_NOT_OPERATOR, NULL);
34651 if (cond == error_mark_node
34652 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34653 {
34654 cp_parser_skip_to_end_of_statement (parser);
34655 return error_mark_node;
34656 }
34657
34658 switch (TREE_CODE (cond))
34659 {
34660 case GT_EXPR:
34661 case GE_EXPR:
34662 case LT_EXPR:
34663 case LE_EXPR:
34664 break;
34665 case NE_EXPR:
34666 /* Fall through: OpenMP disallows NE_EXPR. */
34667 gcc_fallthrough ();
34668 default:
34669 return error_mark_node;
34670 }
34671
34672 /* If decl is an iterator, preserve LHS and RHS of the relational
34673 expr until finish_omp_for. */
34674 if (decl
34675 && (type_dependent_expression_p (decl)
34676 || CLASS_TYPE_P (TREE_TYPE (decl))))
34677 return cond;
34678
34679 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34680 TREE_CODE (cond),
34681 TREE_OPERAND (cond, 0), ERROR_MARK,
34682 TREE_OPERAND (cond, 1), ERROR_MARK,
34683 /*overload=*/NULL, tf_warning_or_error);
34684 }
34685
34686 /* Helper function, to parse omp for increment expression. */
34687
34688 static tree
34689 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34690 {
34691 cp_token *token = cp_lexer_peek_token (parser->lexer);
34692 enum tree_code op;
34693 tree lhs, rhs;
34694 cp_id_kind idk;
34695 bool decl_first;
34696
34697 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34698 {
34699 op = (token->type == CPP_PLUS_PLUS
34700 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34701 cp_lexer_consume_token (parser->lexer);
34702 lhs = cp_parser_simple_cast_expression (parser);
34703 if (lhs != decl
34704 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34705 return error_mark_node;
34706 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34707 }
34708
34709 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34710 if (lhs != decl
34711 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34712 return error_mark_node;
34713
34714 token = cp_lexer_peek_token (parser->lexer);
34715 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34716 {
34717 op = (token->type == CPP_PLUS_PLUS
34718 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34719 cp_lexer_consume_token (parser->lexer);
34720 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34721 }
34722
34723 op = cp_parser_assignment_operator_opt (parser);
34724 if (op == ERROR_MARK)
34725 return error_mark_node;
34726
34727 if (op != NOP_EXPR)
34728 {
34729 rhs = cp_parser_assignment_expression (parser);
34730 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34731 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34732 }
34733
34734 lhs = cp_parser_binary_expression (parser, false, false,
34735 PREC_ADDITIVE_EXPRESSION, NULL);
34736 token = cp_lexer_peek_token (parser->lexer);
34737 decl_first = (lhs == decl
34738 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34739 if (decl_first)
34740 lhs = NULL_TREE;
34741 if (token->type != CPP_PLUS
34742 && token->type != CPP_MINUS)
34743 return error_mark_node;
34744
34745 do
34746 {
34747 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34748 cp_lexer_consume_token (parser->lexer);
34749 rhs = cp_parser_binary_expression (parser, false, false,
34750 PREC_ADDITIVE_EXPRESSION, NULL);
34751 token = cp_lexer_peek_token (parser->lexer);
34752 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34753 {
34754 if (lhs == NULL_TREE)
34755 {
34756 if (op == PLUS_EXPR)
34757 lhs = rhs;
34758 else
34759 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34760 tf_warning_or_error);
34761 }
34762 else
34763 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34764 ERROR_MARK, NULL, tf_warning_or_error);
34765 }
34766 }
34767 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34768
34769 if (!decl_first)
34770 {
34771 if ((rhs != decl
34772 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34773 || op == MINUS_EXPR)
34774 return error_mark_node;
34775 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34776 }
34777 else
34778 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34779
34780 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34781 }
34782
34783 /* Parse the initialization statement of an OpenMP for loop.
34784
34785 Return true if the resulting construct should have an
34786 OMP_CLAUSE_PRIVATE added to it. */
34787
34788 static tree
34789 cp_parser_omp_for_loop_init (cp_parser *parser,
34790 tree &this_pre_body,
34791 vec<tree, va_gc> *for_block,
34792 tree &init,
34793 tree &orig_init,
34794 tree &decl,
34795 tree &real_decl)
34796 {
34797 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34798 return NULL_TREE;
34799
34800 tree add_private_clause = NULL_TREE;
34801
34802 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34803
34804 init-expr:
34805 var = lb
34806 integer-type var = lb
34807 random-access-iterator-type var = lb
34808 pointer-type var = lb
34809 */
34810 cp_decl_specifier_seq type_specifiers;
34811
34812 /* First, try to parse as an initialized declaration. See
34813 cp_parser_condition, from whence the bulk of this is copied. */
34814
34815 cp_parser_parse_tentatively (parser);
34816 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34817 /*is_trailing_return=*/false,
34818 &type_specifiers);
34819 if (cp_parser_parse_definitely (parser))
34820 {
34821 /* If parsing a type specifier seq succeeded, then this
34822 MUST be a initialized declaration. */
34823 tree asm_specification, attributes;
34824 cp_declarator *declarator;
34825
34826 declarator = cp_parser_declarator (parser,
34827 CP_PARSER_DECLARATOR_NAMED,
34828 /*ctor_dtor_or_conv_p=*/NULL,
34829 /*parenthesized_p=*/NULL,
34830 /*member_p=*/false,
34831 /*friend_p=*/false);
34832 attributes = cp_parser_attributes_opt (parser);
34833 asm_specification = cp_parser_asm_specification_opt (parser);
34834
34835 if (declarator == cp_error_declarator)
34836 cp_parser_skip_to_end_of_statement (parser);
34837
34838 else
34839 {
34840 tree pushed_scope, auto_node;
34841
34842 decl = start_decl (declarator, &type_specifiers,
34843 SD_INITIALIZED, attributes,
34844 /*prefix_attributes=*/NULL_TREE,
34845 &pushed_scope);
34846
34847 auto_node = type_uses_auto (TREE_TYPE (decl));
34848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34849 {
34850 if (cp_lexer_next_token_is (parser->lexer,
34851 CPP_OPEN_PAREN))
34852 error ("parenthesized initialization is not allowed in "
34853 "OpenMP %<for%> loop");
34854 else
34855 /* Trigger an error. */
34856 cp_parser_require (parser, CPP_EQ, RT_EQ);
34857
34858 init = error_mark_node;
34859 cp_parser_skip_to_end_of_statement (parser);
34860 }
34861 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34862 || type_dependent_expression_p (decl)
34863 || auto_node)
34864 {
34865 bool is_direct_init, is_non_constant_init;
34866
34867 init = cp_parser_initializer (parser,
34868 &is_direct_init,
34869 &is_non_constant_init);
34870
34871 if (auto_node)
34872 {
34873 TREE_TYPE (decl)
34874 = do_auto_deduction (TREE_TYPE (decl), init,
34875 auto_node);
34876
34877 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34878 && !type_dependent_expression_p (decl))
34879 goto non_class;
34880 }
34881
34882 cp_finish_decl (decl, init, !is_non_constant_init,
34883 asm_specification,
34884 LOOKUP_ONLYCONVERTING);
34885 orig_init = init;
34886 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34887 {
34888 vec_safe_push (for_block, this_pre_body);
34889 init = NULL_TREE;
34890 }
34891 else
34892 {
34893 init = pop_stmt_list (this_pre_body);
34894 if (init && TREE_CODE (init) == STATEMENT_LIST)
34895 {
34896 tree_stmt_iterator i = tsi_start (init);
34897 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34898 while (!tsi_end_p (i))
34899 {
34900 tree t = tsi_stmt (i);
34901 if (TREE_CODE (t) == DECL_EXPR
34902 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34903 {
34904 tsi_delink (&i);
34905 vec_safe_push (for_block, t);
34906 continue;
34907 }
34908 break;
34909 }
34910 if (tsi_one_before_end_p (i))
34911 {
34912 tree t = tsi_stmt (i);
34913 tsi_delink (&i);
34914 free_stmt_list (init);
34915 init = t;
34916 }
34917 }
34918 }
34919 this_pre_body = NULL_TREE;
34920 }
34921 else
34922 {
34923 /* Consume '='. */
34924 cp_lexer_consume_token (parser->lexer);
34925 init = cp_parser_assignment_expression (parser);
34926
34927 non_class:
34928 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34929 init = error_mark_node;
34930 else
34931 cp_finish_decl (decl, NULL_TREE,
34932 /*init_const_expr_p=*/false,
34933 asm_specification,
34934 LOOKUP_ONLYCONVERTING);
34935 }
34936
34937 if (pushed_scope)
34938 pop_scope (pushed_scope);
34939 }
34940 }
34941 else
34942 {
34943 cp_id_kind idk;
34944 /* If parsing a type specifier sequence failed, then
34945 this MUST be a simple expression. */
34946 cp_parser_parse_tentatively (parser);
34947 decl = cp_parser_primary_expression (parser, false, false,
34948 false, &idk);
34949 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34950 if (!cp_parser_error_occurred (parser)
34951 && decl
34952 && (TREE_CODE (decl) == COMPONENT_REF
34953 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34954 {
34955 cp_parser_abort_tentative_parse (parser);
34956 cp_parser_parse_tentatively (parser);
34957 cp_token *token = cp_lexer_peek_token (parser->lexer);
34958 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34959 /*check_dependency_p=*/true,
34960 /*template_p=*/NULL,
34961 /*declarator_p=*/false,
34962 /*optional_p=*/false);
34963 if (name != error_mark_node
34964 && last_tok == cp_lexer_peek_token (parser->lexer))
34965 {
34966 decl = cp_parser_lookup_name_simple (parser, name,
34967 token->location);
34968 if (TREE_CODE (decl) == FIELD_DECL)
34969 add_private_clause = omp_privatize_field (decl, false);
34970 }
34971 cp_parser_abort_tentative_parse (parser);
34972 cp_parser_parse_tentatively (parser);
34973 decl = cp_parser_primary_expression (parser, false, false,
34974 false, &idk);
34975 }
34976 if (!cp_parser_error_occurred (parser)
34977 && decl
34978 && DECL_P (decl)
34979 && CLASS_TYPE_P (TREE_TYPE (decl)))
34980 {
34981 tree rhs;
34982
34983 cp_parser_parse_definitely (parser);
34984 cp_parser_require (parser, CPP_EQ, RT_EQ);
34985 rhs = cp_parser_assignment_expression (parser);
34986 orig_init = rhs;
34987 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34988 decl, NOP_EXPR,
34989 rhs,
34990 tf_warning_or_error));
34991 if (!add_private_clause)
34992 add_private_clause = decl;
34993 }
34994 else
34995 {
34996 decl = NULL;
34997 cp_parser_abort_tentative_parse (parser);
34998 init = cp_parser_expression (parser);
34999 if (init)
35000 {
35001 if (TREE_CODE (init) == MODIFY_EXPR
35002 || TREE_CODE (init) == MODOP_EXPR)
35003 real_decl = TREE_OPERAND (init, 0);
35004 }
35005 }
35006 }
35007 return add_private_clause;
35008 }
35009
35010 /* Parse the restricted form of the for statement allowed by OpenMP. */
35011
35012 static tree
35013 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35014 tree *cclauses, bool *if_p)
35015 {
35016 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35017 tree real_decl, initv, condv, incrv, declv;
35018 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35019 location_t loc_first;
35020 bool collapse_err = false;
35021 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35022 vec<tree, va_gc> *for_block = make_tree_vector ();
35023 auto_vec<tree, 4> orig_inits;
35024 bool tiling = false;
35025
35026 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35027 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35028 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35029 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35030 {
35031 tiling = true;
35032 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35033 }
35034 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35035 && OMP_CLAUSE_ORDERED_EXPR (cl))
35036 {
35037 ordered_cl = cl;
35038 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35039 }
35040
35041 if (ordered && ordered < collapse)
35042 {
35043 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35044 "%<ordered%> clause parameter is less than %<collapse%>");
35045 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35046 = build_int_cst (NULL_TREE, collapse);
35047 ordered = collapse;
35048 }
35049 if (ordered)
35050 {
35051 for (tree *pc = &clauses; *pc; )
35052 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35053 {
35054 error_at (OMP_CLAUSE_LOCATION (*pc),
35055 "%<linear%> clause may not be specified together "
35056 "with %<ordered%> clause with a parameter");
35057 *pc = OMP_CLAUSE_CHAIN (*pc);
35058 }
35059 else
35060 pc = &OMP_CLAUSE_CHAIN (*pc);
35061 }
35062
35063 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35064 count = ordered ? ordered : collapse;
35065
35066 declv = make_tree_vec (count);
35067 initv = make_tree_vec (count);
35068 condv = make_tree_vec (count);
35069 incrv = make_tree_vec (count);
35070
35071 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35072
35073 for (i = 0; i < count; i++)
35074 {
35075 int bracecount = 0;
35076 tree add_private_clause = NULL_TREE;
35077 location_t loc;
35078
35079 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35080 {
35081 if (!collapse_err)
35082 cp_parser_error (parser, "for statement expected");
35083 return NULL;
35084 }
35085 loc = cp_lexer_consume_token (parser->lexer)->location;
35086
35087 matching_parens parens;
35088 if (!parens.require_open (parser))
35089 return NULL;
35090
35091 init = orig_init = decl = real_decl = NULL;
35092 this_pre_body = push_stmt_list ();
35093
35094 add_private_clause
35095 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35096 init, orig_init, decl, real_decl);
35097
35098 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35099 if (this_pre_body)
35100 {
35101 this_pre_body = pop_stmt_list (this_pre_body);
35102 if (pre_body)
35103 {
35104 tree t = pre_body;
35105 pre_body = push_stmt_list ();
35106 add_stmt (t);
35107 add_stmt (this_pre_body);
35108 pre_body = pop_stmt_list (pre_body);
35109 }
35110 else
35111 pre_body = this_pre_body;
35112 }
35113
35114 if (decl)
35115 real_decl = decl;
35116 if (cclauses != NULL
35117 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35118 && real_decl != NULL_TREE)
35119 {
35120 tree *c;
35121 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35122 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35123 && OMP_CLAUSE_DECL (*c) == real_decl)
35124 {
35125 error_at (loc, "iteration variable %qD"
35126 " should not be firstprivate", real_decl);
35127 *c = OMP_CLAUSE_CHAIN (*c);
35128 }
35129 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35130 && OMP_CLAUSE_DECL (*c) == real_decl)
35131 {
35132 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35133 tree l = *c;
35134 *c = OMP_CLAUSE_CHAIN (*c);
35135 if (code == OMP_SIMD)
35136 {
35137 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35138 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35139 }
35140 else
35141 {
35142 OMP_CLAUSE_CHAIN (l) = clauses;
35143 clauses = l;
35144 }
35145 add_private_clause = NULL_TREE;
35146 }
35147 else
35148 {
35149 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35150 && OMP_CLAUSE_DECL (*c) == real_decl)
35151 add_private_clause = NULL_TREE;
35152 c = &OMP_CLAUSE_CHAIN (*c);
35153 }
35154 }
35155
35156 if (add_private_clause)
35157 {
35158 tree c;
35159 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35160 {
35161 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35162 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35163 && OMP_CLAUSE_DECL (c) == decl)
35164 break;
35165 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35166 && OMP_CLAUSE_DECL (c) == decl)
35167 error_at (loc, "iteration variable %qD "
35168 "should not be firstprivate",
35169 decl);
35170 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35171 && OMP_CLAUSE_DECL (c) == decl)
35172 error_at (loc, "iteration variable %qD should not be reduction",
35173 decl);
35174 }
35175 if (c == NULL)
35176 {
35177 if (code != OMP_SIMD)
35178 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35179 else if (collapse == 1)
35180 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35181 else
35182 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35183 OMP_CLAUSE_DECL (c) = add_private_clause;
35184 c = finish_omp_clauses (c, C_ORT_OMP);
35185 if (c)
35186 {
35187 OMP_CLAUSE_CHAIN (c) = clauses;
35188 clauses = c;
35189 /* For linear, signal that we need to fill up
35190 the so far unknown linear step. */
35191 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35192 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35193 }
35194 }
35195 }
35196
35197 cond = NULL;
35198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35199 cond = cp_parser_omp_for_cond (parser, decl);
35200 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35201
35202 incr = NULL;
35203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35204 {
35205 /* If decl is an iterator, preserve the operator on decl
35206 until finish_omp_for. */
35207 if (real_decl
35208 && ((processing_template_decl
35209 && (TREE_TYPE (real_decl) == NULL_TREE
35210 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35211 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35212 incr = cp_parser_omp_for_incr (parser, real_decl);
35213 else
35214 incr = cp_parser_expression (parser);
35215 if (!EXPR_HAS_LOCATION (incr))
35216 protected_set_expr_location (incr, input_location);
35217 }
35218
35219 if (!parens.require_close (parser))
35220 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35221 /*or_comma=*/false,
35222 /*consume_paren=*/true);
35223
35224 TREE_VEC_ELT (declv, i) = decl;
35225 TREE_VEC_ELT (initv, i) = init;
35226 TREE_VEC_ELT (condv, i) = cond;
35227 TREE_VEC_ELT (incrv, i) = incr;
35228 if (orig_init)
35229 {
35230 orig_inits.safe_grow_cleared (i + 1);
35231 orig_inits[i] = orig_init;
35232 }
35233
35234 if (i == count - 1)
35235 break;
35236
35237 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35238 in between the collapsed for loops to be still considered perfectly
35239 nested. Hopefully the final version clarifies this.
35240 For now handle (multiple) {'s and empty statements. */
35241 cp_parser_parse_tentatively (parser);
35242 for (;;)
35243 {
35244 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35245 break;
35246 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35247 {
35248 cp_lexer_consume_token (parser->lexer);
35249 bracecount++;
35250 }
35251 else if (bracecount
35252 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35253 cp_lexer_consume_token (parser->lexer);
35254 else
35255 {
35256 loc = cp_lexer_peek_token (parser->lexer)->location;
35257 error_at (loc, "not enough for loops to collapse");
35258 collapse_err = true;
35259 cp_parser_abort_tentative_parse (parser);
35260 declv = NULL_TREE;
35261 break;
35262 }
35263 }
35264
35265 if (declv)
35266 {
35267 cp_parser_parse_definitely (parser);
35268 nbraces += bracecount;
35269 }
35270 }
35271
35272 if (nbraces)
35273 if_p = NULL;
35274
35275 /* Note that we saved the original contents of this flag when we entered
35276 the structured block, and so we don't need to re-save it here. */
35277 parser->in_statement = IN_OMP_FOR;
35278
35279 /* Note that the grammar doesn't call for a structured block here,
35280 though the loop as a whole is a structured block. */
35281 body = push_stmt_list ();
35282 cp_parser_statement (parser, NULL_TREE, false, if_p);
35283 body = pop_stmt_list (body);
35284
35285 if (declv == NULL_TREE)
35286 ret = NULL_TREE;
35287 else
35288 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35289 body, pre_body, &orig_inits, clauses);
35290
35291 while (nbraces)
35292 {
35293 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35294 {
35295 cp_lexer_consume_token (parser->lexer);
35296 nbraces--;
35297 }
35298 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35299 cp_lexer_consume_token (parser->lexer);
35300 else
35301 {
35302 if (!collapse_err)
35303 {
35304 error_at (cp_lexer_peek_token (parser->lexer)->location,
35305 "collapsed loops not perfectly nested");
35306 }
35307 collapse_err = true;
35308 cp_parser_statement_seq_opt (parser, NULL);
35309 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35310 break;
35311 }
35312 }
35313
35314 while (!for_block->is_empty ())
35315 {
35316 tree t = for_block->pop ();
35317 if (TREE_CODE (t) == STATEMENT_LIST)
35318 add_stmt (pop_stmt_list (t));
35319 else
35320 add_stmt (t);
35321 }
35322 release_tree_vector (for_block);
35323
35324 return ret;
35325 }
35326
35327 /* Helper function for OpenMP parsing, split clauses and call
35328 finish_omp_clauses on each of the set of clauses afterwards. */
35329
35330 static void
35331 cp_omp_split_clauses (location_t loc, enum tree_code code,
35332 omp_clause_mask mask, tree clauses, tree *cclauses)
35333 {
35334 int i;
35335 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35336 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35337 if (cclauses[i])
35338 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35339 }
35340
35341 /* OpenMP 4.0:
35342 #pragma omp simd simd-clause[optseq] new-line
35343 for-loop */
35344
35345 #define OMP_SIMD_CLAUSE_MASK \
35346 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35354
35355 static tree
35356 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35357 char *p_name, omp_clause_mask mask, tree *cclauses,
35358 bool *if_p)
35359 {
35360 tree clauses, sb, ret;
35361 unsigned int save;
35362 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35363
35364 strcat (p_name, " simd");
35365 mask |= OMP_SIMD_CLAUSE_MASK;
35366
35367 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35368 cclauses == NULL);
35369 if (cclauses)
35370 {
35371 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35372 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35373 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35374 OMP_CLAUSE_ORDERED);
35375 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35376 {
35377 error_at (OMP_CLAUSE_LOCATION (c),
35378 "%<ordered%> clause with parameter may not be specified "
35379 "on %qs construct", p_name);
35380 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35381 }
35382 }
35383
35384 sb = begin_omp_structured_block ();
35385 save = cp_parser_begin_omp_structured_block (parser);
35386
35387 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35388
35389 cp_parser_end_omp_structured_block (parser, save);
35390 add_stmt (finish_omp_structured_block (sb));
35391
35392 return ret;
35393 }
35394
35395 /* OpenMP 2.5:
35396 #pragma omp for for-clause[optseq] new-line
35397 for-loop
35398
35399 OpenMP 4.0:
35400 #pragma omp for simd for-simd-clause[optseq] new-line
35401 for-loop */
35402
35403 #define OMP_FOR_CLAUSE_MASK \
35404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35413
35414 static tree
35415 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35416 char *p_name, omp_clause_mask mask, tree *cclauses,
35417 bool *if_p)
35418 {
35419 tree clauses, sb, ret;
35420 unsigned int save;
35421 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35422
35423 strcat (p_name, " for");
35424 mask |= OMP_FOR_CLAUSE_MASK;
35425 /* parallel for{, simd} disallows nowait clause, but for
35426 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35427 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35428 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35429 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35430 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35431 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35432
35433 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35434 {
35435 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35436 const char *p = IDENTIFIER_POINTER (id);
35437
35438 if (strcmp (p, "simd") == 0)
35439 {
35440 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35441 if (cclauses == NULL)
35442 cclauses = cclauses_buf;
35443
35444 cp_lexer_consume_token (parser->lexer);
35445 if (!flag_openmp) /* flag_openmp_simd */
35446 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35447 cclauses, if_p);
35448 sb = begin_omp_structured_block ();
35449 save = cp_parser_begin_omp_structured_block (parser);
35450 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35451 cclauses, if_p);
35452 cp_parser_end_omp_structured_block (parser, save);
35453 tree body = finish_omp_structured_block (sb);
35454 if (ret == NULL)
35455 return ret;
35456 ret = make_node (OMP_FOR);
35457 TREE_TYPE (ret) = void_type_node;
35458 OMP_FOR_BODY (ret) = body;
35459 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35460 SET_EXPR_LOCATION (ret, loc);
35461 add_stmt (ret);
35462 return ret;
35463 }
35464 }
35465 if (!flag_openmp) /* flag_openmp_simd */
35466 {
35467 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35468 return NULL_TREE;
35469 }
35470
35471 /* Composite distribute parallel for disallows linear clause. */
35472 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35473 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35474
35475 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35476 cclauses == NULL);
35477 if (cclauses)
35478 {
35479 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35480 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35481 }
35482
35483 sb = begin_omp_structured_block ();
35484 save = cp_parser_begin_omp_structured_block (parser);
35485
35486 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35487
35488 cp_parser_end_omp_structured_block (parser, save);
35489 add_stmt (finish_omp_structured_block (sb));
35490
35491 return ret;
35492 }
35493
35494 /* OpenMP 2.5:
35495 # pragma omp master new-line
35496 structured-block */
35497
35498 static tree
35499 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35500 {
35501 cp_parser_require_pragma_eol (parser, pragma_tok);
35502 return c_finish_omp_master (input_location,
35503 cp_parser_omp_structured_block (parser, if_p));
35504 }
35505
35506 /* OpenMP 2.5:
35507 # pragma omp ordered new-line
35508 structured-block
35509
35510 OpenMP 4.5:
35511 # pragma omp ordered ordered-clauses new-line
35512 structured-block */
35513
35514 #define OMP_ORDERED_CLAUSE_MASK \
35515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35517
35518 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35519 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35520
35521 static bool
35522 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35523 enum pragma_context context, bool *if_p)
35524 {
35525 location_t loc = pragma_tok->location;
35526
35527 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35528 {
35529 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35530 const char *p = IDENTIFIER_POINTER (id);
35531
35532 if (strcmp (p, "depend") == 0)
35533 {
35534 if (!flag_openmp) /* flag_openmp_simd */
35535 {
35536 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35537 return false;
35538 }
35539 if (context == pragma_stmt)
35540 {
35541 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35542 "%<depend%> clause may only be used in compound "
35543 "statements");
35544 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35545 return false;
35546 }
35547 tree clauses
35548 = cp_parser_omp_all_clauses (parser,
35549 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35550 "#pragma omp ordered", pragma_tok);
35551 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35552 return false;
35553 }
35554 }
35555
35556 tree clauses
35557 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35558 "#pragma omp ordered", pragma_tok);
35559
35560 if (!flag_openmp /* flag_openmp_simd */
35561 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35562 return false;
35563
35564 c_finish_omp_ordered (loc, clauses,
35565 cp_parser_omp_structured_block (parser, if_p));
35566 return true;
35567 }
35568
35569 /* OpenMP 2.5:
35570
35571 section-scope:
35572 { section-sequence }
35573
35574 section-sequence:
35575 section-directive[opt] structured-block
35576 section-sequence section-directive structured-block */
35577
35578 static tree
35579 cp_parser_omp_sections_scope (cp_parser *parser)
35580 {
35581 tree stmt, substmt;
35582 bool error_suppress = false;
35583 cp_token *tok;
35584
35585 matching_braces braces;
35586 if (!braces.require_open (parser))
35587 return NULL_TREE;
35588
35589 stmt = push_stmt_list ();
35590
35591 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35592 != PRAGMA_OMP_SECTION)
35593 {
35594 substmt = cp_parser_omp_structured_block (parser, NULL);
35595 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35596 add_stmt (substmt);
35597 }
35598
35599 while (1)
35600 {
35601 tok = cp_lexer_peek_token (parser->lexer);
35602 if (tok->type == CPP_CLOSE_BRACE)
35603 break;
35604 if (tok->type == CPP_EOF)
35605 break;
35606
35607 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35608 {
35609 cp_lexer_consume_token (parser->lexer);
35610 cp_parser_require_pragma_eol (parser, tok);
35611 error_suppress = false;
35612 }
35613 else if (!error_suppress)
35614 {
35615 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35616 error_suppress = true;
35617 }
35618
35619 substmt = cp_parser_omp_structured_block (parser, NULL);
35620 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35621 add_stmt (substmt);
35622 }
35623 braces.require_close (parser);
35624
35625 substmt = pop_stmt_list (stmt);
35626
35627 stmt = make_node (OMP_SECTIONS);
35628 TREE_TYPE (stmt) = void_type_node;
35629 OMP_SECTIONS_BODY (stmt) = substmt;
35630
35631 add_stmt (stmt);
35632 return stmt;
35633 }
35634
35635 /* OpenMP 2.5:
35636 # pragma omp sections sections-clause[optseq] newline
35637 sections-scope */
35638
35639 #define OMP_SECTIONS_CLAUSE_MASK \
35640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35645
35646 static tree
35647 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35648 char *p_name, omp_clause_mask mask, tree *cclauses)
35649 {
35650 tree clauses, ret;
35651 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35652
35653 strcat (p_name, " sections");
35654 mask |= OMP_SECTIONS_CLAUSE_MASK;
35655 if (cclauses)
35656 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35657
35658 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35659 cclauses == NULL);
35660 if (cclauses)
35661 {
35662 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35663 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35664 }
35665
35666 ret = cp_parser_omp_sections_scope (parser);
35667 if (ret)
35668 OMP_SECTIONS_CLAUSES (ret) = clauses;
35669
35670 return ret;
35671 }
35672
35673 /* OpenMP 2.5:
35674 # pragma omp parallel parallel-clause[optseq] new-line
35675 structured-block
35676 # pragma omp parallel for parallel-for-clause[optseq] new-line
35677 structured-block
35678 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35679 structured-block
35680
35681 OpenMP 4.0:
35682 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35683 structured-block */
35684
35685 #define OMP_PARALLEL_CLAUSE_MASK \
35686 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35695
35696 static tree
35697 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35698 char *p_name, omp_clause_mask mask, tree *cclauses,
35699 bool *if_p)
35700 {
35701 tree stmt, clauses, block;
35702 unsigned int save;
35703 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35704
35705 strcat (p_name, " parallel");
35706 mask |= OMP_PARALLEL_CLAUSE_MASK;
35707 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35708 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35709 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35710 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35711
35712 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35713 {
35714 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35715 if (cclauses == NULL)
35716 cclauses = cclauses_buf;
35717
35718 cp_lexer_consume_token (parser->lexer);
35719 if (!flag_openmp) /* flag_openmp_simd */
35720 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35721 if_p);
35722 block = begin_omp_parallel ();
35723 save = cp_parser_begin_omp_structured_block (parser);
35724 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35725 if_p);
35726 cp_parser_end_omp_structured_block (parser, save);
35727 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35728 block);
35729 if (ret == NULL_TREE)
35730 return ret;
35731 OMP_PARALLEL_COMBINED (stmt) = 1;
35732 return stmt;
35733 }
35734 /* When combined with distribute, parallel has to be followed by for.
35735 #pragma omp target parallel is allowed though. */
35736 else if (cclauses
35737 && (mask & (OMP_CLAUSE_MASK_1
35738 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35739 {
35740 error_at (loc, "expected %<for%> after %qs", p_name);
35741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35742 return NULL_TREE;
35743 }
35744 else if (!flag_openmp) /* flag_openmp_simd */
35745 {
35746 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35747 return NULL_TREE;
35748 }
35749 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35750 {
35751 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35752 const char *p = IDENTIFIER_POINTER (id);
35753 if (strcmp (p, "sections") == 0)
35754 {
35755 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35756 cclauses = cclauses_buf;
35757
35758 cp_lexer_consume_token (parser->lexer);
35759 block = begin_omp_parallel ();
35760 save = cp_parser_begin_omp_structured_block (parser);
35761 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35762 cp_parser_end_omp_structured_block (parser, save);
35763 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35764 block);
35765 OMP_PARALLEL_COMBINED (stmt) = 1;
35766 return stmt;
35767 }
35768 }
35769
35770 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35771 cclauses == NULL);
35772 if (cclauses)
35773 {
35774 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35775 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35776 }
35777
35778 block = begin_omp_parallel ();
35779 save = cp_parser_begin_omp_structured_block (parser);
35780 cp_parser_statement (parser, NULL_TREE, false, if_p);
35781 cp_parser_end_omp_structured_block (parser, save);
35782 stmt = finish_omp_parallel (clauses, block);
35783 return stmt;
35784 }
35785
35786 /* OpenMP 2.5:
35787 # pragma omp single single-clause[optseq] new-line
35788 structured-block */
35789
35790 #define OMP_SINGLE_CLAUSE_MASK \
35791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35795
35796 static tree
35797 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35798 {
35799 tree stmt = make_node (OMP_SINGLE);
35800 TREE_TYPE (stmt) = void_type_node;
35801
35802 OMP_SINGLE_CLAUSES (stmt)
35803 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35804 "#pragma omp single", pragma_tok);
35805 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35806
35807 return add_stmt (stmt);
35808 }
35809
35810 /* OpenMP 3.0:
35811 # pragma omp task task-clause[optseq] new-line
35812 structured-block */
35813
35814 #define OMP_TASK_CLAUSE_MASK \
35815 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35825
35826 static tree
35827 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35828 {
35829 tree clauses, block;
35830 unsigned int save;
35831
35832 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35833 "#pragma omp task", pragma_tok);
35834 block = begin_omp_task ();
35835 save = cp_parser_begin_omp_structured_block (parser);
35836 cp_parser_statement (parser, NULL_TREE, false, if_p);
35837 cp_parser_end_omp_structured_block (parser, save);
35838 return finish_omp_task (clauses, block);
35839 }
35840
35841 /* OpenMP 3.0:
35842 # pragma omp taskwait new-line */
35843
35844 static void
35845 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35846 {
35847 cp_parser_require_pragma_eol (parser, pragma_tok);
35848 finish_omp_taskwait ();
35849 }
35850
35851 /* OpenMP 3.1:
35852 # pragma omp taskyield new-line */
35853
35854 static void
35855 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35856 {
35857 cp_parser_require_pragma_eol (parser, pragma_tok);
35858 finish_omp_taskyield ();
35859 }
35860
35861 /* OpenMP 4.0:
35862 # pragma omp taskgroup new-line
35863 structured-block */
35864
35865 static tree
35866 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35867 {
35868 cp_parser_require_pragma_eol (parser, pragma_tok);
35869 return c_finish_omp_taskgroup (input_location,
35870 cp_parser_omp_structured_block (parser,
35871 if_p));
35872 }
35873
35874
35875 /* OpenMP 2.5:
35876 # pragma omp threadprivate (variable-list) */
35877
35878 static void
35879 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35880 {
35881 tree vars;
35882
35883 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35884 cp_parser_require_pragma_eol (parser, pragma_tok);
35885
35886 finish_omp_threadprivate (vars);
35887 }
35888
35889 /* OpenMP 4.0:
35890 # pragma omp cancel cancel-clause[optseq] new-line */
35891
35892 #define OMP_CANCEL_CLAUSE_MASK \
35893 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35898
35899 static void
35900 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35901 {
35902 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35903 "#pragma omp cancel", pragma_tok);
35904 finish_omp_cancel (clauses);
35905 }
35906
35907 /* OpenMP 4.0:
35908 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35909
35910 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35911 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35915
35916 static void
35917 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35918 enum pragma_context context)
35919 {
35920 tree clauses;
35921 bool point_seen = false;
35922
35923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35924 {
35925 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35926 const char *p = IDENTIFIER_POINTER (id);
35927
35928 if (strcmp (p, "point") == 0)
35929 {
35930 cp_lexer_consume_token (parser->lexer);
35931 point_seen = true;
35932 }
35933 }
35934 if (!point_seen)
35935 {
35936 cp_parser_error (parser, "expected %<point%>");
35937 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35938 return;
35939 }
35940
35941 if (context != pragma_compound)
35942 {
35943 if (context == pragma_stmt)
35944 error_at (pragma_tok->location,
35945 "%<#pragma %s%> may only be used in compound statements",
35946 "omp cancellation point");
35947 else
35948 cp_parser_error (parser, "expected declaration specifiers");
35949 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35950 return;
35951 }
35952
35953 clauses = cp_parser_omp_all_clauses (parser,
35954 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35955 "#pragma omp cancellation point",
35956 pragma_tok);
35957 finish_omp_cancellation_point (clauses);
35958 }
35959
35960 /* OpenMP 4.0:
35961 #pragma omp distribute distribute-clause[optseq] new-line
35962 for-loop */
35963
35964 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35965 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35970
35971 static tree
35972 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35973 char *p_name, omp_clause_mask mask, tree *cclauses,
35974 bool *if_p)
35975 {
35976 tree clauses, sb, ret;
35977 unsigned int save;
35978 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35979
35980 strcat (p_name, " distribute");
35981 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35982
35983 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35984 {
35985 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35986 const char *p = IDENTIFIER_POINTER (id);
35987 bool simd = false;
35988 bool parallel = false;
35989
35990 if (strcmp (p, "simd") == 0)
35991 simd = true;
35992 else
35993 parallel = strcmp (p, "parallel") == 0;
35994 if (parallel || simd)
35995 {
35996 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35997 if (cclauses == NULL)
35998 cclauses = cclauses_buf;
35999 cp_lexer_consume_token (parser->lexer);
36000 if (!flag_openmp) /* flag_openmp_simd */
36001 {
36002 if (simd)
36003 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36004 cclauses, if_p);
36005 else
36006 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36007 cclauses, if_p);
36008 }
36009 sb = begin_omp_structured_block ();
36010 save = cp_parser_begin_omp_structured_block (parser);
36011 if (simd)
36012 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36013 cclauses, if_p);
36014 else
36015 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36016 cclauses, if_p);
36017 cp_parser_end_omp_structured_block (parser, save);
36018 tree body = finish_omp_structured_block (sb);
36019 if (ret == NULL)
36020 return ret;
36021 ret = make_node (OMP_DISTRIBUTE);
36022 TREE_TYPE (ret) = void_type_node;
36023 OMP_FOR_BODY (ret) = body;
36024 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36025 SET_EXPR_LOCATION (ret, loc);
36026 add_stmt (ret);
36027 return ret;
36028 }
36029 }
36030 if (!flag_openmp) /* flag_openmp_simd */
36031 {
36032 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36033 return NULL_TREE;
36034 }
36035
36036 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36037 cclauses == NULL);
36038 if (cclauses)
36039 {
36040 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36041 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36042 }
36043
36044 sb = begin_omp_structured_block ();
36045 save = cp_parser_begin_omp_structured_block (parser);
36046
36047 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36048
36049 cp_parser_end_omp_structured_block (parser, save);
36050 add_stmt (finish_omp_structured_block (sb));
36051
36052 return ret;
36053 }
36054
36055 /* OpenMP 4.0:
36056 # pragma omp teams teams-clause[optseq] new-line
36057 structured-block */
36058
36059 #define OMP_TEAMS_CLAUSE_MASK \
36060 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36067
36068 static tree
36069 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36070 char *p_name, omp_clause_mask mask, tree *cclauses,
36071 bool *if_p)
36072 {
36073 tree clauses, sb, ret;
36074 unsigned int save;
36075 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36076
36077 strcat (p_name, " teams");
36078 mask |= OMP_TEAMS_CLAUSE_MASK;
36079
36080 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36081 {
36082 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36083 const char *p = IDENTIFIER_POINTER (id);
36084 if (strcmp (p, "distribute") == 0)
36085 {
36086 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36087 if (cclauses == NULL)
36088 cclauses = cclauses_buf;
36089
36090 cp_lexer_consume_token (parser->lexer);
36091 if (!flag_openmp) /* flag_openmp_simd */
36092 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36093 cclauses, if_p);
36094 sb = begin_omp_structured_block ();
36095 save = cp_parser_begin_omp_structured_block (parser);
36096 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36097 cclauses, if_p);
36098 cp_parser_end_omp_structured_block (parser, save);
36099 tree body = finish_omp_structured_block (sb);
36100 if (ret == NULL)
36101 return ret;
36102 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36103 ret = make_node (OMP_TEAMS);
36104 TREE_TYPE (ret) = void_type_node;
36105 OMP_TEAMS_CLAUSES (ret) = clauses;
36106 OMP_TEAMS_BODY (ret) = body;
36107 OMP_TEAMS_COMBINED (ret) = 1;
36108 SET_EXPR_LOCATION (ret, loc);
36109 return add_stmt (ret);
36110 }
36111 }
36112 if (!flag_openmp) /* flag_openmp_simd */
36113 {
36114 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36115 return NULL_TREE;
36116 }
36117
36118 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36119 cclauses == NULL);
36120 if (cclauses)
36121 {
36122 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36123 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36124 }
36125
36126 tree stmt = make_node (OMP_TEAMS);
36127 TREE_TYPE (stmt) = void_type_node;
36128 OMP_TEAMS_CLAUSES (stmt) = clauses;
36129 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36130 SET_EXPR_LOCATION (stmt, loc);
36131
36132 return add_stmt (stmt);
36133 }
36134
36135 /* OpenMP 4.0:
36136 # pragma omp target data target-data-clause[optseq] new-line
36137 structured-block */
36138
36139 #define OMP_TARGET_DATA_CLAUSE_MASK \
36140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36144
36145 static tree
36146 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36147 {
36148 tree clauses
36149 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36150 "#pragma omp target data", pragma_tok);
36151 int map_seen = 0;
36152 for (tree *pc = &clauses; *pc;)
36153 {
36154 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36155 switch (OMP_CLAUSE_MAP_KIND (*pc))
36156 {
36157 case GOMP_MAP_TO:
36158 case GOMP_MAP_ALWAYS_TO:
36159 case GOMP_MAP_FROM:
36160 case GOMP_MAP_ALWAYS_FROM:
36161 case GOMP_MAP_TOFROM:
36162 case GOMP_MAP_ALWAYS_TOFROM:
36163 case GOMP_MAP_ALLOC:
36164 map_seen = 3;
36165 break;
36166 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36167 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36168 case GOMP_MAP_ALWAYS_POINTER:
36169 break;
36170 default:
36171 map_seen |= 1;
36172 error_at (OMP_CLAUSE_LOCATION (*pc),
36173 "%<#pragma omp target data%> with map-type other "
36174 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36175 "on %<map%> clause");
36176 *pc = OMP_CLAUSE_CHAIN (*pc);
36177 continue;
36178 }
36179 pc = &OMP_CLAUSE_CHAIN (*pc);
36180 }
36181
36182 if (map_seen != 3)
36183 {
36184 if (map_seen == 0)
36185 error_at (pragma_tok->location,
36186 "%<#pragma omp target data%> must contain at least "
36187 "one %<map%> clause");
36188 return NULL_TREE;
36189 }
36190
36191 tree stmt = make_node (OMP_TARGET_DATA);
36192 TREE_TYPE (stmt) = void_type_node;
36193 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36194
36195 keep_next_level (true);
36196 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36197
36198 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36199 return add_stmt (stmt);
36200 }
36201
36202 /* OpenMP 4.5:
36203 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36204 structured-block */
36205
36206 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36212
36213 static tree
36214 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36215 enum pragma_context context)
36216 {
36217 bool data_seen = false;
36218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36219 {
36220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36221 const char *p = IDENTIFIER_POINTER (id);
36222
36223 if (strcmp (p, "data") == 0)
36224 {
36225 cp_lexer_consume_token (parser->lexer);
36226 data_seen = true;
36227 }
36228 }
36229 if (!data_seen)
36230 {
36231 cp_parser_error (parser, "expected %<data%>");
36232 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36233 return NULL_TREE;
36234 }
36235
36236 if (context == pragma_stmt)
36237 {
36238 error_at (pragma_tok->location,
36239 "%<#pragma %s%> may only be used in compound statements",
36240 "omp target enter data");
36241 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36242 return NULL_TREE;
36243 }
36244
36245 tree clauses
36246 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36247 "#pragma omp target enter data", pragma_tok);
36248 int map_seen = 0;
36249 for (tree *pc = &clauses; *pc;)
36250 {
36251 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36252 switch (OMP_CLAUSE_MAP_KIND (*pc))
36253 {
36254 case GOMP_MAP_TO:
36255 case GOMP_MAP_ALWAYS_TO:
36256 case GOMP_MAP_ALLOC:
36257 map_seen = 3;
36258 break;
36259 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36260 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36261 case GOMP_MAP_ALWAYS_POINTER:
36262 break;
36263 default:
36264 map_seen |= 1;
36265 error_at (OMP_CLAUSE_LOCATION (*pc),
36266 "%<#pragma omp target enter data%> with map-type other "
36267 "than %<to%> or %<alloc%> on %<map%> clause");
36268 *pc = OMP_CLAUSE_CHAIN (*pc);
36269 continue;
36270 }
36271 pc = &OMP_CLAUSE_CHAIN (*pc);
36272 }
36273
36274 if (map_seen != 3)
36275 {
36276 if (map_seen == 0)
36277 error_at (pragma_tok->location,
36278 "%<#pragma omp target enter data%> must contain at least "
36279 "one %<map%> clause");
36280 return NULL_TREE;
36281 }
36282
36283 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36284 TREE_TYPE (stmt) = void_type_node;
36285 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36286 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36287 return add_stmt (stmt);
36288 }
36289
36290 /* OpenMP 4.5:
36291 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36292 structured-block */
36293
36294 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36295 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36300
36301 static tree
36302 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36303 enum pragma_context context)
36304 {
36305 bool data_seen = false;
36306 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36307 {
36308 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36309 const char *p = IDENTIFIER_POINTER (id);
36310
36311 if (strcmp (p, "data") == 0)
36312 {
36313 cp_lexer_consume_token (parser->lexer);
36314 data_seen = true;
36315 }
36316 }
36317 if (!data_seen)
36318 {
36319 cp_parser_error (parser, "expected %<data%>");
36320 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36321 return NULL_TREE;
36322 }
36323
36324 if (context == pragma_stmt)
36325 {
36326 error_at (pragma_tok->location,
36327 "%<#pragma %s%> may only be used in compound statements",
36328 "omp target exit data");
36329 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36330 return NULL_TREE;
36331 }
36332
36333 tree clauses
36334 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36335 "#pragma omp target exit data", pragma_tok);
36336 int map_seen = 0;
36337 for (tree *pc = &clauses; *pc;)
36338 {
36339 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36340 switch (OMP_CLAUSE_MAP_KIND (*pc))
36341 {
36342 case GOMP_MAP_FROM:
36343 case GOMP_MAP_ALWAYS_FROM:
36344 case GOMP_MAP_RELEASE:
36345 case GOMP_MAP_DELETE:
36346 map_seen = 3;
36347 break;
36348 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36349 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36350 case GOMP_MAP_ALWAYS_POINTER:
36351 break;
36352 default:
36353 map_seen |= 1;
36354 error_at (OMP_CLAUSE_LOCATION (*pc),
36355 "%<#pragma omp target exit data%> with map-type other "
36356 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36357 " clause");
36358 *pc = OMP_CLAUSE_CHAIN (*pc);
36359 continue;
36360 }
36361 pc = &OMP_CLAUSE_CHAIN (*pc);
36362 }
36363
36364 if (map_seen != 3)
36365 {
36366 if (map_seen == 0)
36367 error_at (pragma_tok->location,
36368 "%<#pragma omp target exit data%> must contain at least "
36369 "one %<map%> clause");
36370 return NULL_TREE;
36371 }
36372
36373 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36374 TREE_TYPE (stmt) = void_type_node;
36375 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36376 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36377 return add_stmt (stmt);
36378 }
36379
36380 /* OpenMP 4.0:
36381 # pragma omp target update target-update-clause[optseq] new-line */
36382
36383 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36384 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36390
36391 static bool
36392 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36393 enum pragma_context context)
36394 {
36395 if (context == pragma_stmt)
36396 {
36397 error_at (pragma_tok->location,
36398 "%<#pragma %s%> may only be used in compound statements",
36399 "omp target update");
36400 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36401 return false;
36402 }
36403
36404 tree clauses
36405 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36406 "#pragma omp target update", pragma_tok);
36407 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36408 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36409 {
36410 error_at (pragma_tok->location,
36411 "%<#pragma omp target update%> must contain at least one "
36412 "%<from%> or %<to%> clauses");
36413 return false;
36414 }
36415
36416 tree stmt = make_node (OMP_TARGET_UPDATE);
36417 TREE_TYPE (stmt) = void_type_node;
36418 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36419 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36420 add_stmt (stmt);
36421 return false;
36422 }
36423
36424 /* OpenMP 4.0:
36425 # pragma omp target target-clause[optseq] new-line
36426 structured-block */
36427
36428 #define OMP_TARGET_CLAUSE_MASK \
36429 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36438
36439 static bool
36440 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36441 enum pragma_context context, bool *if_p)
36442 {
36443 tree *pc = NULL, stmt;
36444
36445 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36446 {
36447 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36448 const char *p = IDENTIFIER_POINTER (id);
36449 enum tree_code ccode = ERROR_MARK;
36450
36451 if (strcmp (p, "teams") == 0)
36452 ccode = OMP_TEAMS;
36453 else if (strcmp (p, "parallel") == 0)
36454 ccode = OMP_PARALLEL;
36455 else if (strcmp (p, "simd") == 0)
36456 ccode = OMP_SIMD;
36457 if (ccode != ERROR_MARK)
36458 {
36459 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36460 char p_name[sizeof ("#pragma omp target teams distribute "
36461 "parallel for simd")];
36462
36463 cp_lexer_consume_token (parser->lexer);
36464 strcpy (p_name, "#pragma omp target");
36465 if (!flag_openmp) /* flag_openmp_simd */
36466 {
36467 tree stmt;
36468 switch (ccode)
36469 {
36470 case OMP_TEAMS:
36471 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36472 OMP_TARGET_CLAUSE_MASK,
36473 cclauses, if_p);
36474 break;
36475 case OMP_PARALLEL:
36476 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36477 OMP_TARGET_CLAUSE_MASK,
36478 cclauses, if_p);
36479 break;
36480 case OMP_SIMD:
36481 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36482 OMP_TARGET_CLAUSE_MASK,
36483 cclauses, if_p);
36484 break;
36485 default:
36486 gcc_unreachable ();
36487 }
36488 return stmt != NULL_TREE;
36489 }
36490 keep_next_level (true);
36491 tree sb = begin_omp_structured_block (), ret;
36492 unsigned save = cp_parser_begin_omp_structured_block (parser);
36493 switch (ccode)
36494 {
36495 case OMP_TEAMS:
36496 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36497 OMP_TARGET_CLAUSE_MASK, cclauses,
36498 if_p);
36499 break;
36500 case OMP_PARALLEL:
36501 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36502 OMP_TARGET_CLAUSE_MASK, cclauses,
36503 if_p);
36504 break;
36505 case OMP_SIMD:
36506 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36507 OMP_TARGET_CLAUSE_MASK, cclauses,
36508 if_p);
36509 break;
36510 default:
36511 gcc_unreachable ();
36512 }
36513 cp_parser_end_omp_structured_block (parser, save);
36514 tree body = finish_omp_structured_block (sb);
36515 if (ret == NULL_TREE)
36516 return false;
36517 if (ccode == OMP_TEAMS && !processing_template_decl)
36518 {
36519 /* For combined target teams, ensure the num_teams and
36520 thread_limit clause expressions are evaluated on the host,
36521 before entering the target construct. */
36522 tree c;
36523 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36524 c; c = OMP_CLAUSE_CHAIN (c))
36525 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36526 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36527 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36528 {
36529 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36530 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36531 if (expr == error_mark_node)
36532 continue;
36533 tree tmp = TARGET_EXPR_SLOT (expr);
36534 add_stmt (expr);
36535 OMP_CLAUSE_OPERAND (c, 0) = expr;
36536 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36537 OMP_CLAUSE_FIRSTPRIVATE);
36538 OMP_CLAUSE_DECL (tc) = tmp;
36539 OMP_CLAUSE_CHAIN (tc)
36540 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36541 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36542 }
36543 }
36544 tree stmt = make_node (OMP_TARGET);
36545 TREE_TYPE (stmt) = void_type_node;
36546 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36547 OMP_TARGET_BODY (stmt) = body;
36548 OMP_TARGET_COMBINED (stmt) = 1;
36549 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36550 add_stmt (stmt);
36551 pc = &OMP_TARGET_CLAUSES (stmt);
36552 goto check_clauses;
36553 }
36554 else if (!flag_openmp) /* flag_openmp_simd */
36555 {
36556 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36557 return false;
36558 }
36559 else if (strcmp (p, "data") == 0)
36560 {
36561 cp_lexer_consume_token (parser->lexer);
36562 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36563 return true;
36564 }
36565 else if (strcmp (p, "enter") == 0)
36566 {
36567 cp_lexer_consume_token (parser->lexer);
36568 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36569 return false;
36570 }
36571 else if (strcmp (p, "exit") == 0)
36572 {
36573 cp_lexer_consume_token (parser->lexer);
36574 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36575 return false;
36576 }
36577 else if (strcmp (p, "update") == 0)
36578 {
36579 cp_lexer_consume_token (parser->lexer);
36580 return cp_parser_omp_target_update (parser, pragma_tok, context);
36581 }
36582 }
36583 if (!flag_openmp) /* flag_openmp_simd */
36584 {
36585 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36586 return false;
36587 }
36588
36589 stmt = make_node (OMP_TARGET);
36590 TREE_TYPE (stmt) = void_type_node;
36591
36592 OMP_TARGET_CLAUSES (stmt)
36593 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36594 "#pragma omp target", pragma_tok);
36595 pc = &OMP_TARGET_CLAUSES (stmt);
36596 keep_next_level (true);
36597 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36598
36599 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36600 add_stmt (stmt);
36601
36602 check_clauses:
36603 while (*pc)
36604 {
36605 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36606 switch (OMP_CLAUSE_MAP_KIND (*pc))
36607 {
36608 case GOMP_MAP_TO:
36609 case GOMP_MAP_ALWAYS_TO:
36610 case GOMP_MAP_FROM:
36611 case GOMP_MAP_ALWAYS_FROM:
36612 case GOMP_MAP_TOFROM:
36613 case GOMP_MAP_ALWAYS_TOFROM:
36614 case GOMP_MAP_ALLOC:
36615 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36616 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36617 case GOMP_MAP_ALWAYS_POINTER:
36618 break;
36619 default:
36620 error_at (OMP_CLAUSE_LOCATION (*pc),
36621 "%<#pragma omp target%> with map-type other "
36622 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36623 "on %<map%> clause");
36624 *pc = OMP_CLAUSE_CHAIN (*pc);
36625 continue;
36626 }
36627 pc = &OMP_CLAUSE_CHAIN (*pc);
36628 }
36629 return true;
36630 }
36631
36632 /* OpenACC 2.0:
36633 # pragma acc cache (variable-list) new-line
36634 */
36635
36636 static tree
36637 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36638 {
36639 tree stmt, clauses;
36640
36641 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36642 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36643
36644 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36645
36646 stmt = make_node (OACC_CACHE);
36647 TREE_TYPE (stmt) = void_type_node;
36648 OACC_CACHE_CLAUSES (stmt) = clauses;
36649 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36650 add_stmt (stmt);
36651
36652 return stmt;
36653 }
36654
36655 /* OpenACC 2.0:
36656 # pragma acc data oacc-data-clause[optseq] new-line
36657 structured-block */
36658
36659 #define OACC_DATA_CLAUSE_MASK \
36660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36671
36672 static tree
36673 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36674 {
36675 tree stmt, clauses, block;
36676 unsigned int save;
36677
36678 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36679 "#pragma acc data", pragma_tok);
36680
36681 block = begin_omp_parallel ();
36682 save = cp_parser_begin_omp_structured_block (parser);
36683 cp_parser_statement (parser, NULL_TREE, false, if_p);
36684 cp_parser_end_omp_structured_block (parser, save);
36685 stmt = finish_oacc_data (clauses, block);
36686 return stmt;
36687 }
36688
36689 /* OpenACC 2.0:
36690 # pragma acc host_data <clauses> new-line
36691 structured-block */
36692
36693 #define OACC_HOST_DATA_CLAUSE_MASK \
36694 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36695
36696 static tree
36697 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36698 {
36699 tree stmt, clauses, block;
36700 unsigned int save;
36701
36702 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36703 "#pragma acc host_data", pragma_tok);
36704
36705 block = begin_omp_parallel ();
36706 save = cp_parser_begin_omp_structured_block (parser);
36707 cp_parser_statement (parser, NULL_TREE, false, if_p);
36708 cp_parser_end_omp_structured_block (parser, save);
36709 stmt = finish_oacc_host_data (clauses, block);
36710 return stmt;
36711 }
36712
36713 /* OpenACC 2.0:
36714 # pragma acc declare oacc-data-clause[optseq] new-line
36715 */
36716
36717 #define OACC_DECLARE_CLAUSE_MASK \
36718 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36730
36731 static tree
36732 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36733 {
36734 tree clauses, stmt;
36735 bool error = false;
36736
36737 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36738 "#pragma acc declare", pragma_tok, true);
36739
36740
36741 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36742 {
36743 error_at (pragma_tok->location,
36744 "no valid clauses specified in %<#pragma acc declare%>");
36745 return NULL_TREE;
36746 }
36747
36748 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36749 {
36750 location_t loc = OMP_CLAUSE_LOCATION (t);
36751 tree decl = OMP_CLAUSE_DECL (t);
36752 if (!DECL_P (decl))
36753 {
36754 error_at (loc, "array section in %<#pragma acc declare%>");
36755 error = true;
36756 continue;
36757 }
36758 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36759 switch (OMP_CLAUSE_MAP_KIND (t))
36760 {
36761 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36762 case GOMP_MAP_FORCE_ALLOC:
36763 case GOMP_MAP_FORCE_TO:
36764 case GOMP_MAP_FORCE_DEVICEPTR:
36765 case GOMP_MAP_DEVICE_RESIDENT:
36766 break;
36767
36768 case GOMP_MAP_LINK:
36769 if (!global_bindings_p ()
36770 && (TREE_STATIC (decl)
36771 || !DECL_EXTERNAL (decl)))
36772 {
36773 error_at (loc,
36774 "%qD must be a global variable in "
36775 "%<#pragma acc declare link%>",
36776 decl);
36777 error = true;
36778 continue;
36779 }
36780 break;
36781
36782 default:
36783 if (global_bindings_p ())
36784 {
36785 error_at (loc, "invalid OpenACC clause at file scope");
36786 error = true;
36787 continue;
36788 }
36789 if (DECL_EXTERNAL (decl))
36790 {
36791 error_at (loc,
36792 "invalid use of %<extern%> variable %qD "
36793 "in %<#pragma acc declare%>", decl);
36794 error = true;
36795 continue;
36796 }
36797 else if (TREE_PUBLIC (decl))
36798 {
36799 error_at (loc,
36800 "invalid use of %<global%> variable %qD "
36801 "in %<#pragma acc declare%>", decl);
36802 error = true;
36803 continue;
36804 }
36805 break;
36806 }
36807
36808 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36809 || lookup_attribute ("omp declare target link",
36810 DECL_ATTRIBUTES (decl)))
36811 {
36812 error_at (loc, "variable %qD used more than once with "
36813 "%<#pragma acc declare%>", decl);
36814 error = true;
36815 continue;
36816 }
36817
36818 if (!error)
36819 {
36820 tree id;
36821
36822 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36823 id = get_identifier ("omp declare target link");
36824 else
36825 id = get_identifier ("omp declare target");
36826
36827 DECL_ATTRIBUTES (decl)
36828 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36829 if (global_bindings_p ())
36830 {
36831 symtab_node *node = symtab_node::get (decl);
36832 if (node != NULL)
36833 {
36834 node->offloadable = 1;
36835 if (ENABLE_OFFLOADING)
36836 {
36837 g->have_offload = true;
36838 if (is_a <varpool_node *> (node))
36839 vec_safe_push (offload_vars, decl);
36840 }
36841 }
36842 }
36843 }
36844 }
36845
36846 if (error || global_bindings_p ())
36847 return NULL_TREE;
36848
36849 stmt = make_node (OACC_DECLARE);
36850 TREE_TYPE (stmt) = void_type_node;
36851 OACC_DECLARE_CLAUSES (stmt) = clauses;
36852 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36853
36854 add_stmt (stmt);
36855
36856 return NULL_TREE;
36857 }
36858
36859 /* OpenACC 2.0:
36860 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36861
36862 or
36863
36864 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36865
36866 LOC is the location of the #pragma token.
36867 */
36868
36869 #define OACC_ENTER_DATA_CLAUSE_MASK \
36870 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36877
36878 #define OACC_EXIT_DATA_CLAUSE_MASK \
36879 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36884
36885 static tree
36886 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36887 bool enter)
36888 {
36889 location_t loc = pragma_tok->location;
36890 tree stmt, clauses;
36891 const char *p = "";
36892
36893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36894 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36895
36896 if (strcmp (p, "data") != 0)
36897 {
36898 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36899 enter ? "enter" : "exit");
36900 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36901 return NULL_TREE;
36902 }
36903
36904 cp_lexer_consume_token (parser->lexer);
36905
36906 if (enter)
36907 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36908 "#pragma acc enter data", pragma_tok);
36909 else
36910 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36911 "#pragma acc exit data", pragma_tok);
36912
36913 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36914 {
36915 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36916 enter ? "enter" : "exit");
36917 return NULL_TREE;
36918 }
36919
36920 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36921 TREE_TYPE (stmt) = void_type_node;
36922 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36923 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36924 add_stmt (stmt);
36925 return stmt;
36926 }
36927
36928 /* OpenACC 2.0:
36929 # pragma acc loop oacc-loop-clause[optseq] new-line
36930 structured-block */
36931
36932 #define OACC_LOOP_CLAUSE_MASK \
36933 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36943
36944 static tree
36945 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36946 omp_clause_mask mask, tree *cclauses, bool *if_p)
36947 {
36948 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36949
36950 strcat (p_name, " loop");
36951 mask |= OACC_LOOP_CLAUSE_MASK;
36952
36953 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36954 cclauses == NULL);
36955 if (cclauses)
36956 {
36957 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36958 if (*cclauses)
36959 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36960 if (clauses)
36961 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36962 }
36963
36964 tree block = begin_omp_structured_block ();
36965 int save = cp_parser_begin_omp_structured_block (parser);
36966 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36967 cp_parser_end_omp_structured_block (parser, save);
36968 add_stmt (finish_omp_structured_block (block));
36969
36970 return stmt;
36971 }
36972
36973 /* OpenACC 2.0:
36974 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36975 structured-block
36976
36977 or
36978
36979 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36980 structured-block
36981 */
36982
36983 #define OACC_KERNELS_CLAUSE_MASK \
36984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37001
37002 #define OACC_PARALLEL_CLAUSE_MASK \
37003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37023
37024 static tree
37025 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37026 char *p_name, bool *if_p)
37027 {
37028 omp_clause_mask mask;
37029 enum tree_code code;
37030 switch (cp_parser_pragma_kind (pragma_tok))
37031 {
37032 case PRAGMA_OACC_KERNELS:
37033 strcat (p_name, " kernels");
37034 mask = OACC_KERNELS_CLAUSE_MASK;
37035 code = OACC_KERNELS;
37036 break;
37037 case PRAGMA_OACC_PARALLEL:
37038 strcat (p_name, " parallel");
37039 mask = OACC_PARALLEL_CLAUSE_MASK;
37040 code = OACC_PARALLEL;
37041 break;
37042 default:
37043 gcc_unreachable ();
37044 }
37045
37046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37047 {
37048 const char *p
37049 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37050 if (strcmp (p, "loop") == 0)
37051 {
37052 cp_lexer_consume_token (parser->lexer);
37053 tree block = begin_omp_parallel ();
37054 tree clauses;
37055 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37056 if_p);
37057 return finish_omp_construct (code, block, clauses);
37058 }
37059 }
37060
37061 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37062
37063 tree block = begin_omp_parallel ();
37064 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37065 cp_parser_statement (parser, NULL_TREE, false, if_p);
37066 cp_parser_end_omp_structured_block (parser, save);
37067 return finish_omp_construct (code, block, clauses);
37068 }
37069
37070 /* OpenACC 2.0:
37071 # pragma acc update oacc-update-clause[optseq] new-line
37072 */
37073
37074 #define OACC_UPDATE_CLAUSE_MASK \
37075 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37081
37082 static tree
37083 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37084 {
37085 tree stmt, clauses;
37086
37087 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37088 "#pragma acc update", pragma_tok);
37089
37090 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37091 {
37092 error_at (pragma_tok->location,
37093 "%<#pragma acc update%> must contain at least one "
37094 "%<device%> or %<host%> or %<self%> clause");
37095 return NULL_TREE;
37096 }
37097
37098 stmt = make_node (OACC_UPDATE);
37099 TREE_TYPE (stmt) = void_type_node;
37100 OACC_UPDATE_CLAUSES (stmt) = clauses;
37101 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37102 add_stmt (stmt);
37103 return stmt;
37104 }
37105
37106 /* OpenACC 2.0:
37107 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37108
37109 LOC is the location of the #pragma token.
37110 */
37111
37112 #define OACC_WAIT_CLAUSE_MASK \
37113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37114
37115 static tree
37116 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37117 {
37118 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37119 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37120
37121 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37122 list = cp_parser_oacc_wait_list (parser, loc, list);
37123
37124 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37125 "#pragma acc wait", pragma_tok);
37126
37127 stmt = c_finish_oacc_wait (loc, list, clauses);
37128 stmt = finish_expr_stmt (stmt);
37129
37130 return stmt;
37131 }
37132
37133 /* OpenMP 4.0:
37134 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37135
37136 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37143
37144 static void
37145 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37146 enum pragma_context context)
37147 {
37148 bool first_p = parser->omp_declare_simd == NULL;
37149 cp_omp_declare_simd_data data;
37150 if (first_p)
37151 {
37152 data.error_seen = false;
37153 data.fndecl_seen = false;
37154 data.tokens = vNULL;
37155 data.clauses = NULL_TREE;
37156 /* It is safe to take the address of a local variable; it will only be
37157 used while this scope is live. */
37158 parser->omp_declare_simd = &data;
37159 }
37160
37161 /* Store away all pragma tokens. */
37162 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37163 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37164 cp_lexer_consume_token (parser->lexer);
37165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37166 parser->omp_declare_simd->error_seen = true;
37167 cp_parser_require_pragma_eol (parser, pragma_tok);
37168 struct cp_token_cache *cp
37169 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37170 parser->omp_declare_simd->tokens.safe_push (cp);
37171
37172 if (first_p)
37173 {
37174 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37175 cp_parser_pragma (parser, context, NULL);
37176 switch (context)
37177 {
37178 case pragma_external:
37179 cp_parser_declaration (parser);
37180 break;
37181 case pragma_member:
37182 cp_parser_member_declaration (parser);
37183 break;
37184 case pragma_objc_icode:
37185 cp_parser_block_declaration (parser, /*statement_p=*/false);
37186 break;
37187 default:
37188 cp_parser_declaration_statement (parser);
37189 break;
37190 }
37191 if (parser->omp_declare_simd
37192 && !parser->omp_declare_simd->error_seen
37193 && !parser->omp_declare_simd->fndecl_seen)
37194 error_at (pragma_tok->location,
37195 "%<#pragma omp declare simd%> not immediately followed by "
37196 "function declaration or definition");
37197 data.tokens.release ();
37198 parser->omp_declare_simd = NULL;
37199 }
37200 }
37201
37202 /* Finalize #pragma omp declare simd clauses after direct declarator has
37203 been parsed, and put that into "omp declare simd" attribute. */
37204
37205 static tree
37206 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37207 {
37208 struct cp_token_cache *ce;
37209 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37210 int i;
37211
37212 if (!data->error_seen && data->fndecl_seen)
37213 {
37214 error ("%<#pragma omp declare simd%> not immediately followed by "
37215 "a single function declaration or definition");
37216 data->error_seen = true;
37217 }
37218 if (data->error_seen)
37219 return attrs;
37220
37221 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37222 {
37223 tree c, cl;
37224
37225 cp_parser_push_lexer_for_tokens (parser, ce);
37226 parser->lexer->in_pragma = true;
37227 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37228 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37229 cp_lexer_consume_token (parser->lexer);
37230 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37231 "#pragma omp declare simd", pragma_tok);
37232 cp_parser_pop_lexer (parser);
37233 if (cl)
37234 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37235 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37236 TREE_CHAIN (c) = attrs;
37237 if (processing_template_decl)
37238 ATTR_IS_DEPENDENT (c) = 1;
37239 attrs = c;
37240 }
37241
37242 data->fndecl_seen = true;
37243 return attrs;
37244 }
37245
37246
37247 /* OpenMP 4.0:
37248 # pragma omp declare target new-line
37249 declarations and definitions
37250 # pragma omp end declare target new-line
37251
37252 OpenMP 4.5:
37253 # pragma omp declare target ( extended-list ) new-line
37254
37255 # pragma omp declare target declare-target-clauses[seq] new-line */
37256
37257 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37258 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37260
37261 static void
37262 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37263 {
37264 tree clauses = NULL_TREE;
37265 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37266 clauses
37267 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37268 "#pragma omp declare target", pragma_tok);
37269 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37270 {
37271 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37272 clauses);
37273 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37274 cp_parser_require_pragma_eol (parser, pragma_tok);
37275 }
37276 else
37277 {
37278 cp_parser_require_pragma_eol (parser, pragma_tok);
37279 scope_chain->omp_declare_target_attribute++;
37280 return;
37281 }
37282 if (scope_chain->omp_declare_target_attribute)
37283 error_at (pragma_tok->location,
37284 "%<#pragma omp declare target%> with clauses in between "
37285 "%<#pragma omp declare target%> without clauses and "
37286 "%<#pragma omp end declare target%>");
37287 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37288 {
37289 tree t = OMP_CLAUSE_DECL (c), id;
37290 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37291 tree at2 = lookup_attribute ("omp declare target link",
37292 DECL_ATTRIBUTES (t));
37293 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37294 {
37295 id = get_identifier ("omp declare target link");
37296 std::swap (at1, at2);
37297 }
37298 else
37299 id = get_identifier ("omp declare target");
37300 if (at2)
37301 {
37302 error_at (OMP_CLAUSE_LOCATION (c),
37303 "%qD specified both in declare target %<link%> and %<to%>"
37304 " clauses", t);
37305 continue;
37306 }
37307 if (!at1)
37308 {
37309 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37310 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37311 continue;
37312
37313 symtab_node *node = symtab_node::get (t);
37314 if (node != NULL)
37315 {
37316 node->offloadable = 1;
37317 if (ENABLE_OFFLOADING)
37318 {
37319 g->have_offload = true;
37320 if (is_a <varpool_node *> (node))
37321 vec_safe_push (offload_vars, t);
37322 }
37323 }
37324 }
37325 }
37326 }
37327
37328 static void
37329 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37330 {
37331 const char *p = "";
37332 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37333 {
37334 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37335 p = IDENTIFIER_POINTER (id);
37336 }
37337 if (strcmp (p, "declare") == 0)
37338 {
37339 cp_lexer_consume_token (parser->lexer);
37340 p = "";
37341 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37342 {
37343 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37344 p = IDENTIFIER_POINTER (id);
37345 }
37346 if (strcmp (p, "target") == 0)
37347 cp_lexer_consume_token (parser->lexer);
37348 else
37349 {
37350 cp_parser_error (parser, "expected %<target%>");
37351 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37352 return;
37353 }
37354 }
37355 else
37356 {
37357 cp_parser_error (parser, "expected %<declare%>");
37358 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37359 return;
37360 }
37361 cp_parser_require_pragma_eol (parser, pragma_tok);
37362 if (!scope_chain->omp_declare_target_attribute)
37363 error_at (pragma_tok->location,
37364 "%<#pragma omp end declare target%> without corresponding "
37365 "%<#pragma omp declare target%>");
37366 else
37367 scope_chain->omp_declare_target_attribute--;
37368 }
37369
37370 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37371 expression and optional initializer clause of
37372 #pragma omp declare reduction. We store the expression(s) as
37373 either 3, 6 or 7 special statements inside of the artificial function's
37374 body. The first two statements are DECL_EXPRs for the artificial
37375 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37376 expression that uses those variables.
37377 If there was any INITIALIZER clause, this is followed by further statements,
37378 the fourth and fifth statements are DECL_EXPRs for the artificial
37379 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37380 constructor variant (first token after open paren is not omp_priv),
37381 then the sixth statement is a statement with the function call expression
37382 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37383 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37384 to initialize the OMP_PRIV artificial variable and there is seventh
37385 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37386
37387 static bool
37388 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37389 {
37390 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37391 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37392 type = TREE_TYPE (type);
37393 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37394 DECL_ARTIFICIAL (omp_out) = 1;
37395 pushdecl (omp_out);
37396 add_decl_expr (omp_out);
37397 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37398 DECL_ARTIFICIAL (omp_in) = 1;
37399 pushdecl (omp_in);
37400 add_decl_expr (omp_in);
37401 tree combiner;
37402 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37403
37404 keep_next_level (true);
37405 tree block = begin_omp_structured_block ();
37406 combiner = cp_parser_expression (parser);
37407 finish_expr_stmt (combiner);
37408 block = finish_omp_structured_block (block);
37409 add_stmt (block);
37410
37411 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37412 return false;
37413
37414 const char *p = "";
37415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37416 {
37417 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37418 p = IDENTIFIER_POINTER (id);
37419 }
37420
37421 if (strcmp (p, "initializer") == 0)
37422 {
37423 cp_lexer_consume_token (parser->lexer);
37424 matching_parens parens;
37425 if (!parens.require_open (parser))
37426 return false;
37427
37428 p = "";
37429 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37430 {
37431 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37432 p = IDENTIFIER_POINTER (id);
37433 }
37434
37435 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37436 DECL_ARTIFICIAL (omp_priv) = 1;
37437 pushdecl (omp_priv);
37438 add_decl_expr (omp_priv);
37439 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37440 DECL_ARTIFICIAL (omp_orig) = 1;
37441 pushdecl (omp_orig);
37442 add_decl_expr (omp_orig);
37443
37444 keep_next_level (true);
37445 block = begin_omp_structured_block ();
37446
37447 bool ctor = false;
37448 if (strcmp (p, "omp_priv") == 0)
37449 {
37450 bool is_direct_init, is_non_constant_init;
37451 ctor = true;
37452 cp_lexer_consume_token (parser->lexer);
37453 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37454 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37455 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37456 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37457 == CPP_CLOSE_PAREN
37458 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37459 == CPP_CLOSE_PAREN))
37460 {
37461 finish_omp_structured_block (block);
37462 error ("invalid initializer clause");
37463 return false;
37464 }
37465 initializer = cp_parser_initializer (parser, &is_direct_init,
37466 &is_non_constant_init);
37467 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37468 NULL_TREE, LOOKUP_ONLYCONVERTING);
37469 }
37470 else
37471 {
37472 cp_parser_parse_tentatively (parser);
37473 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37474 /*check_dependency_p=*/true,
37475 /*template_p=*/NULL,
37476 /*declarator_p=*/false,
37477 /*optional_p=*/false);
37478 vec<tree, va_gc> *args;
37479 if (fn_name == error_mark_node
37480 || cp_parser_error_occurred (parser)
37481 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37482 || ((args = cp_parser_parenthesized_expression_list
37483 (parser, non_attr, /*cast_p=*/false,
37484 /*allow_expansion_p=*/true,
37485 /*non_constant_p=*/NULL)),
37486 cp_parser_error_occurred (parser)))
37487 {
37488 finish_omp_structured_block (block);
37489 cp_parser_abort_tentative_parse (parser);
37490 cp_parser_error (parser, "expected id-expression (arguments)");
37491 return false;
37492 }
37493 unsigned int i;
37494 tree arg;
37495 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37496 if (arg == omp_priv
37497 || (TREE_CODE (arg) == ADDR_EXPR
37498 && TREE_OPERAND (arg, 0) == omp_priv))
37499 break;
37500 cp_parser_abort_tentative_parse (parser);
37501 if (arg == NULL_TREE)
37502 error ("one of the initializer call arguments should be %<omp_priv%>"
37503 " or %<&omp_priv%>");
37504 initializer = cp_parser_postfix_expression (parser, false, false, false,
37505 false, NULL);
37506 finish_expr_stmt (initializer);
37507 }
37508
37509 block = finish_omp_structured_block (block);
37510 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37511 add_stmt (block);
37512
37513 if (ctor)
37514 add_decl_expr (omp_orig);
37515
37516 if (!parens.require_close (parser))
37517 return false;
37518 }
37519
37520 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37521 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37522 UNKNOWN_LOCATION);
37523
37524 return true;
37525 }
37526
37527 /* OpenMP 4.0
37528 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37529 initializer-clause[opt] new-line
37530
37531 initializer-clause:
37532 initializer (omp_priv initializer)
37533 initializer (function-name (argument-list)) */
37534
37535 static void
37536 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37537 enum pragma_context)
37538 {
37539 auto_vec<tree> types;
37540 enum tree_code reduc_code = ERROR_MARK;
37541 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37542 unsigned int i;
37543 cp_token *first_token;
37544 cp_token_cache *cp;
37545 int errs;
37546 void *p;
37547
37548 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37549 p = obstack_alloc (&declarator_obstack, 0);
37550
37551 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37552 goto fail;
37553
37554 switch (cp_lexer_peek_token (parser->lexer)->type)
37555 {
37556 case CPP_PLUS:
37557 reduc_code = PLUS_EXPR;
37558 break;
37559 case CPP_MULT:
37560 reduc_code = MULT_EXPR;
37561 break;
37562 case CPP_MINUS:
37563 reduc_code = MINUS_EXPR;
37564 break;
37565 case CPP_AND:
37566 reduc_code = BIT_AND_EXPR;
37567 break;
37568 case CPP_XOR:
37569 reduc_code = BIT_XOR_EXPR;
37570 break;
37571 case CPP_OR:
37572 reduc_code = BIT_IOR_EXPR;
37573 break;
37574 case CPP_AND_AND:
37575 reduc_code = TRUTH_ANDIF_EXPR;
37576 break;
37577 case CPP_OR_OR:
37578 reduc_code = TRUTH_ORIF_EXPR;
37579 break;
37580 case CPP_NAME:
37581 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37582 break;
37583 default:
37584 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37585 "%<|%>, %<&&%>, %<||%> or identifier");
37586 goto fail;
37587 }
37588
37589 if (reduc_code != ERROR_MARK)
37590 cp_lexer_consume_token (parser->lexer);
37591
37592 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37593 if (reduc_id == error_mark_node)
37594 goto fail;
37595
37596 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37597 goto fail;
37598
37599 /* Types may not be defined in declare reduction type list. */
37600 const char *saved_message;
37601 saved_message = parser->type_definition_forbidden_message;
37602 parser->type_definition_forbidden_message
37603 = G_("types may not be defined in declare reduction type list");
37604 bool saved_colon_corrects_to_scope_p;
37605 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37606 parser->colon_corrects_to_scope_p = false;
37607 bool saved_colon_doesnt_start_class_def_p;
37608 saved_colon_doesnt_start_class_def_p
37609 = parser->colon_doesnt_start_class_def_p;
37610 parser->colon_doesnt_start_class_def_p = true;
37611
37612 while (true)
37613 {
37614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37615 type = cp_parser_type_id (parser);
37616 if (type == error_mark_node)
37617 ;
37618 else if (ARITHMETIC_TYPE_P (type)
37619 && (orig_reduc_id == NULL_TREE
37620 || (TREE_CODE (type) != COMPLEX_TYPE
37621 && (id_equal (orig_reduc_id, "min")
37622 || id_equal (orig_reduc_id, "max")))))
37623 error_at (loc, "predeclared arithmetic type %qT in "
37624 "%<#pragma omp declare reduction%>", type);
37625 else if (TREE_CODE (type) == FUNCTION_TYPE
37626 || TREE_CODE (type) == METHOD_TYPE
37627 || TREE_CODE (type) == ARRAY_TYPE)
37628 error_at (loc, "function or array type %qT in "
37629 "%<#pragma omp declare reduction%>", type);
37630 else if (TREE_CODE (type) == REFERENCE_TYPE)
37631 error_at (loc, "reference type %qT in "
37632 "%<#pragma omp declare reduction%>", type);
37633 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37634 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37635 "%<#pragma omp declare reduction%>", type);
37636 else
37637 types.safe_push (type);
37638
37639 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37640 cp_lexer_consume_token (parser->lexer);
37641 else
37642 break;
37643 }
37644
37645 /* Restore the saved message. */
37646 parser->type_definition_forbidden_message = saved_message;
37647 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37648 parser->colon_doesnt_start_class_def_p
37649 = saved_colon_doesnt_start_class_def_p;
37650
37651 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37652 || types.is_empty ())
37653 {
37654 fail:
37655 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37656 goto done;
37657 }
37658
37659 first_token = cp_lexer_peek_token (parser->lexer);
37660 cp = NULL;
37661 errs = errorcount;
37662 FOR_EACH_VEC_ELT (types, i, type)
37663 {
37664 tree fntype
37665 = build_function_type_list (void_type_node,
37666 cp_build_reference_type (type, false),
37667 NULL_TREE);
37668 tree this_reduc_id = reduc_id;
37669 if (!dependent_type_p (type))
37670 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37671 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37672 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37673 DECL_ARTIFICIAL (fndecl) = 1;
37674 DECL_EXTERNAL (fndecl) = 1;
37675 DECL_DECLARED_INLINE_P (fndecl) = 1;
37676 DECL_IGNORED_P (fndecl) = 1;
37677 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37678 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37679 DECL_ATTRIBUTES (fndecl)
37680 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37681 DECL_ATTRIBUTES (fndecl));
37682 if (processing_template_decl)
37683 fndecl = push_template_decl (fndecl);
37684 bool block_scope = false;
37685 tree block = NULL_TREE;
37686 if (current_function_decl)
37687 {
37688 block_scope = true;
37689 DECL_CONTEXT (fndecl) = global_namespace;
37690 if (!processing_template_decl)
37691 pushdecl (fndecl);
37692 }
37693 else if (current_class_type)
37694 {
37695 if (cp == NULL)
37696 {
37697 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37698 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37699 cp_lexer_consume_token (parser->lexer);
37700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37701 goto fail;
37702 cp = cp_token_cache_new (first_token,
37703 cp_lexer_peek_nth_token (parser->lexer,
37704 2));
37705 }
37706 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37707 finish_member_declaration (fndecl);
37708 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37709 DECL_PENDING_INLINE_P (fndecl) = 1;
37710 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37711 continue;
37712 }
37713 else
37714 {
37715 DECL_CONTEXT (fndecl) = current_namespace;
37716 pushdecl (fndecl);
37717 }
37718 if (!block_scope)
37719 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37720 else
37721 block = begin_omp_structured_block ();
37722 if (cp)
37723 {
37724 cp_parser_push_lexer_for_tokens (parser, cp);
37725 parser->lexer->in_pragma = true;
37726 }
37727 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37728 {
37729 if (!block_scope)
37730 finish_function (/*inline_p=*/false);
37731 else
37732 DECL_CONTEXT (fndecl) = current_function_decl;
37733 if (cp)
37734 cp_parser_pop_lexer (parser);
37735 goto fail;
37736 }
37737 if (cp)
37738 cp_parser_pop_lexer (parser);
37739 if (!block_scope)
37740 finish_function (/*inline_p=*/false);
37741 else
37742 {
37743 DECL_CONTEXT (fndecl) = current_function_decl;
37744 block = finish_omp_structured_block (block);
37745 if (TREE_CODE (block) == BIND_EXPR)
37746 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37747 else if (TREE_CODE (block) == STATEMENT_LIST)
37748 DECL_SAVED_TREE (fndecl) = block;
37749 if (processing_template_decl)
37750 add_decl_expr (fndecl);
37751 }
37752 cp_check_omp_declare_reduction (fndecl);
37753 if (cp == NULL && types.length () > 1)
37754 cp = cp_token_cache_new (first_token,
37755 cp_lexer_peek_nth_token (parser->lexer, 2));
37756 if (errs != errorcount)
37757 break;
37758 }
37759
37760 cp_parser_require_pragma_eol (parser, pragma_tok);
37761
37762 done:
37763 /* Free any declarators allocated. */
37764 obstack_free (&declarator_obstack, p);
37765 }
37766
37767 /* OpenMP 4.0
37768 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37769 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37770 initializer-clause[opt] new-line
37771 #pragma omp declare target new-line */
37772
37773 static bool
37774 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37775 enum pragma_context context)
37776 {
37777 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37778 {
37779 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37780 const char *p = IDENTIFIER_POINTER (id);
37781
37782 if (strcmp (p, "simd") == 0)
37783 {
37784 cp_lexer_consume_token (parser->lexer);
37785 cp_parser_omp_declare_simd (parser, pragma_tok,
37786 context);
37787 return true;
37788 }
37789 cp_ensure_no_omp_declare_simd (parser);
37790 if (strcmp (p, "reduction") == 0)
37791 {
37792 cp_lexer_consume_token (parser->lexer);
37793 cp_parser_omp_declare_reduction (parser, pragma_tok,
37794 context);
37795 return false;
37796 }
37797 if (!flag_openmp) /* flag_openmp_simd */
37798 {
37799 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37800 return false;
37801 }
37802 if (strcmp (p, "target") == 0)
37803 {
37804 cp_lexer_consume_token (parser->lexer);
37805 cp_parser_omp_declare_target (parser, pragma_tok);
37806 return false;
37807 }
37808 }
37809 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37810 "or %<target%>");
37811 cp_parser_require_pragma_eol (parser, pragma_tok);
37812 return false;
37813 }
37814
37815 /* OpenMP 4.5:
37816 #pragma omp taskloop taskloop-clause[optseq] new-line
37817 for-loop
37818
37819 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37820 for-loop */
37821
37822 #define OMP_TASKLOOP_CLAUSE_MASK \
37823 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37837
37838 static tree
37839 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37840 char *p_name, omp_clause_mask mask, tree *cclauses,
37841 bool *if_p)
37842 {
37843 tree clauses, sb, ret;
37844 unsigned int save;
37845 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37846
37847 strcat (p_name, " taskloop");
37848 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37849
37850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37851 {
37852 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37853 const char *p = IDENTIFIER_POINTER (id);
37854
37855 if (strcmp (p, "simd") == 0)
37856 {
37857 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37858 if (cclauses == NULL)
37859 cclauses = cclauses_buf;
37860
37861 cp_lexer_consume_token (parser->lexer);
37862 if (!flag_openmp) /* flag_openmp_simd */
37863 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37864 cclauses, if_p);
37865 sb = begin_omp_structured_block ();
37866 save = cp_parser_begin_omp_structured_block (parser);
37867 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37868 cclauses, if_p);
37869 cp_parser_end_omp_structured_block (parser, save);
37870 tree body = finish_omp_structured_block (sb);
37871 if (ret == NULL)
37872 return ret;
37873 ret = make_node (OMP_TASKLOOP);
37874 TREE_TYPE (ret) = void_type_node;
37875 OMP_FOR_BODY (ret) = body;
37876 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37877 SET_EXPR_LOCATION (ret, loc);
37878 add_stmt (ret);
37879 return ret;
37880 }
37881 }
37882 if (!flag_openmp) /* flag_openmp_simd */
37883 {
37884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37885 return NULL_TREE;
37886 }
37887
37888 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37889 cclauses == NULL);
37890 if (cclauses)
37891 {
37892 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37893 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37894 }
37895
37896 sb = begin_omp_structured_block ();
37897 save = cp_parser_begin_omp_structured_block (parser);
37898
37899 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37900 if_p);
37901
37902 cp_parser_end_omp_structured_block (parser, save);
37903 add_stmt (finish_omp_structured_block (sb));
37904
37905 return ret;
37906 }
37907
37908
37909 /* OpenACC 2.0:
37910 # pragma acc routine oacc-routine-clause[optseq] new-line
37911 function-definition
37912
37913 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37914 */
37915
37916 #define OACC_ROUTINE_CLAUSE_MASK \
37917 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37921
37922
37923 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37924 component, which must resolve to a declared namespace-scope
37925 function. The clauses are either processed directly (for a named
37926 function), or defered until the immediatley following declaration
37927 is parsed. */
37928
37929 static void
37930 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37931 enum pragma_context context)
37932 {
37933 gcc_checking_assert (context == pragma_external);
37934 /* The checking for "another pragma following this one" in the "no optional
37935 '( name )'" case makes sure that we dont re-enter. */
37936 gcc_checking_assert (parser->oacc_routine == NULL);
37937
37938 cp_oacc_routine_data data;
37939 data.error_seen = false;
37940 data.fndecl_seen = false;
37941 data.tokens = vNULL;
37942 data.clauses = NULL_TREE;
37943 data.loc = pragma_tok->location;
37944 /* It is safe to take the address of a local variable; it will only be
37945 used while this scope is live. */
37946 parser->oacc_routine = &data;
37947
37948 /* Look for optional '( name )'. */
37949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37950 {
37951 matching_parens parens;
37952 parens.consume_open (parser); /* '(' */
37953
37954 /* We parse the name as an id-expression. If it resolves to
37955 anything other than a non-overloaded function at namespace
37956 scope, it's an error. */
37957 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37958 tree name = cp_parser_id_expression (parser,
37959 /*template_keyword_p=*/false,
37960 /*check_dependency_p=*/false,
37961 /*template_p=*/NULL,
37962 /*declarator_p=*/false,
37963 /*optional_p=*/false);
37964 tree decl = (identifier_p (name)
37965 ? cp_parser_lookup_name_simple (parser, name, name_loc)
37966 : name);
37967 if (name != error_mark_node && decl == error_mark_node)
37968 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37969
37970 if (decl == error_mark_node
37971 || !parens.require_close (parser))
37972 {
37973 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37974 parser->oacc_routine = NULL;
37975 return;
37976 }
37977
37978 data.clauses
37979 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37980 "#pragma acc routine",
37981 cp_lexer_peek_token (parser->lexer));
37982
37983 if (decl && is_overloaded_fn (decl)
37984 && (TREE_CODE (decl) != FUNCTION_DECL
37985 || DECL_FUNCTION_TEMPLATE_P (decl)))
37986 {
37987 error_at (name_loc,
37988 "%<#pragma acc routine%> names a set of overloads");
37989 parser->oacc_routine = NULL;
37990 return;
37991 }
37992
37993 /* Perhaps we should use the same rule as declarations in different
37994 namespaces? */
37995 if (!DECL_NAMESPACE_SCOPE_P (decl))
37996 {
37997 error_at (name_loc,
37998 "%qD does not refer to a namespace scope function", decl);
37999 parser->oacc_routine = NULL;
38000 return;
38001 }
38002
38003 if (TREE_CODE (decl) != FUNCTION_DECL)
38004 {
38005 error_at (name_loc, "%qD does not refer to a function", decl);
38006 parser->oacc_routine = NULL;
38007 return;
38008 }
38009
38010 cp_finalize_oacc_routine (parser, decl, false);
38011 parser->oacc_routine = NULL;
38012 }
38013 else /* No optional '( name )'. */
38014 {
38015 /* Store away all pragma tokens. */
38016 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38017 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38018 cp_lexer_consume_token (parser->lexer);
38019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38020 parser->oacc_routine->error_seen = true;
38021 cp_parser_require_pragma_eol (parser, pragma_tok);
38022 struct cp_token_cache *cp
38023 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38024 parser->oacc_routine->tokens.safe_push (cp);
38025
38026 /* Emit a helpful diagnostic if there's another pragma following this
38027 one. */
38028 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38029 {
38030 cp_ensure_no_oacc_routine (parser);
38031 data.tokens.release ();
38032 /* ..., and then just keep going. */
38033 return;
38034 }
38035
38036 /* We only have to consider the pragma_external case here. */
38037 cp_parser_declaration (parser);
38038 if (parser->oacc_routine
38039 && !parser->oacc_routine->fndecl_seen)
38040 cp_ensure_no_oacc_routine (parser);
38041 else
38042 parser->oacc_routine = NULL;
38043 data.tokens.release ();
38044 }
38045 }
38046
38047 /* Finalize #pragma acc routine clauses after direct declarator has
38048 been parsed. */
38049
38050 static tree
38051 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38052 {
38053 struct cp_token_cache *ce;
38054 cp_oacc_routine_data *data = parser->oacc_routine;
38055
38056 if (!data->error_seen && data->fndecl_seen)
38057 {
38058 error_at (data->loc,
38059 "%<#pragma acc routine%> not immediately followed by "
38060 "a single function declaration or definition");
38061 data->error_seen = true;
38062 }
38063 if (data->error_seen)
38064 return attrs;
38065
38066 gcc_checking_assert (data->tokens.length () == 1);
38067 ce = data->tokens[0];
38068
38069 cp_parser_push_lexer_for_tokens (parser, ce);
38070 parser->lexer->in_pragma = true;
38071 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38072
38073 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38074 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38075 parser->oacc_routine->clauses
38076 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38077 "#pragma acc routine", pragma_tok);
38078 cp_parser_pop_lexer (parser);
38079 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38080 fndecl_seen. */
38081
38082 return attrs;
38083 }
38084
38085 /* Apply any saved OpenACC routine clauses to a just-parsed
38086 declaration. */
38087
38088 static void
38089 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38090 {
38091 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38092 {
38093 /* Keep going if we're in error reporting mode. */
38094 if (parser->oacc_routine->error_seen
38095 || fndecl == error_mark_node)
38096 return;
38097
38098 if (parser->oacc_routine->fndecl_seen)
38099 {
38100 error_at (parser->oacc_routine->loc,
38101 "%<#pragma acc routine%> not immediately followed by"
38102 " a single function declaration or definition");
38103 parser->oacc_routine = NULL;
38104 return;
38105 }
38106 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38107 {
38108 cp_ensure_no_oacc_routine (parser);
38109 return;
38110 }
38111
38112 if (oacc_get_fn_attrib (fndecl))
38113 {
38114 error_at (parser->oacc_routine->loc,
38115 "%<#pragma acc routine%> already applied to %qD", fndecl);
38116 parser->oacc_routine = NULL;
38117 return;
38118 }
38119
38120 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38121 {
38122 error_at (parser->oacc_routine->loc,
38123 TREE_USED (fndecl)
38124 ? G_("%<#pragma acc routine%> must be applied before use")
38125 : G_("%<#pragma acc routine%> must be applied before "
38126 "definition"));
38127 parser->oacc_routine = NULL;
38128 return;
38129 }
38130
38131 /* Process the routine's dimension clauses. */
38132 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38133 oacc_replace_fn_attrib (fndecl, dims);
38134
38135 /* Add an "omp declare target" attribute. */
38136 DECL_ATTRIBUTES (fndecl)
38137 = tree_cons (get_identifier ("omp declare target"),
38138 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38139
38140 /* Don't unset parser->oacc_routine here: we may still need it to
38141 diagnose wrong usage. But, remember that we've used this "#pragma acc
38142 routine". */
38143 parser->oacc_routine->fndecl_seen = true;
38144 }
38145 }
38146
38147 /* Main entry point to OpenMP statement pragmas. */
38148
38149 static void
38150 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38151 {
38152 tree stmt;
38153 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38154 omp_clause_mask mask (0);
38155
38156 switch (cp_parser_pragma_kind (pragma_tok))
38157 {
38158 case PRAGMA_OACC_ATOMIC:
38159 cp_parser_omp_atomic (parser, pragma_tok);
38160 return;
38161 case PRAGMA_OACC_CACHE:
38162 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38163 break;
38164 case PRAGMA_OACC_DATA:
38165 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38166 break;
38167 case PRAGMA_OACC_ENTER_DATA:
38168 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38169 break;
38170 case PRAGMA_OACC_EXIT_DATA:
38171 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38172 break;
38173 case PRAGMA_OACC_HOST_DATA:
38174 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38175 break;
38176 case PRAGMA_OACC_KERNELS:
38177 case PRAGMA_OACC_PARALLEL:
38178 strcpy (p_name, "#pragma acc");
38179 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38180 if_p);
38181 break;
38182 case PRAGMA_OACC_LOOP:
38183 strcpy (p_name, "#pragma acc");
38184 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38185 if_p);
38186 break;
38187 case PRAGMA_OACC_UPDATE:
38188 stmt = cp_parser_oacc_update (parser, pragma_tok);
38189 break;
38190 case PRAGMA_OACC_WAIT:
38191 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38192 break;
38193 case PRAGMA_OMP_ATOMIC:
38194 cp_parser_omp_atomic (parser, pragma_tok);
38195 return;
38196 case PRAGMA_OMP_CRITICAL:
38197 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38198 break;
38199 case PRAGMA_OMP_DISTRIBUTE:
38200 strcpy (p_name, "#pragma omp");
38201 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38202 if_p);
38203 break;
38204 case PRAGMA_OMP_FOR:
38205 strcpy (p_name, "#pragma omp");
38206 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38207 if_p);
38208 break;
38209 case PRAGMA_OMP_MASTER:
38210 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38211 break;
38212 case PRAGMA_OMP_PARALLEL:
38213 strcpy (p_name, "#pragma omp");
38214 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38215 if_p);
38216 break;
38217 case PRAGMA_OMP_SECTIONS:
38218 strcpy (p_name, "#pragma omp");
38219 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38220 break;
38221 case PRAGMA_OMP_SIMD:
38222 strcpy (p_name, "#pragma omp");
38223 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38224 if_p);
38225 break;
38226 case PRAGMA_OMP_SINGLE:
38227 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38228 break;
38229 case PRAGMA_OMP_TASK:
38230 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38231 break;
38232 case PRAGMA_OMP_TASKGROUP:
38233 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38234 break;
38235 case PRAGMA_OMP_TASKLOOP:
38236 strcpy (p_name, "#pragma omp");
38237 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38238 if_p);
38239 break;
38240 case PRAGMA_OMP_TEAMS:
38241 strcpy (p_name, "#pragma omp");
38242 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38243 if_p);
38244 break;
38245 default:
38246 gcc_unreachable ();
38247 }
38248
38249 protected_set_expr_location (stmt, pragma_tok->location);
38250 }
38251 \f
38252 /* Transactional Memory parsing routines. */
38253
38254 /* Parse a transaction attribute.
38255
38256 txn-attribute:
38257 attribute
38258 [ [ identifier ] ]
38259
38260 We use this instead of cp_parser_attributes_opt for transactions to avoid
38261 the pedwarn in C++98 mode. */
38262
38263 static tree
38264 cp_parser_txn_attribute_opt (cp_parser *parser)
38265 {
38266 cp_token *token;
38267 tree attr_name, attr = NULL;
38268
38269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38270 return cp_parser_attributes_opt (parser);
38271
38272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38273 return NULL_TREE;
38274 cp_lexer_consume_token (parser->lexer);
38275 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38276 goto error1;
38277
38278 token = cp_lexer_peek_token (parser->lexer);
38279 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38280 {
38281 token = cp_lexer_consume_token (parser->lexer);
38282
38283 attr_name = (token->type == CPP_KEYWORD
38284 /* For keywords, use the canonical spelling,
38285 not the parsed identifier. */
38286 ? ridpointers[(int) token->keyword]
38287 : token->u.value);
38288 attr = build_tree_list (attr_name, NULL_TREE);
38289 }
38290 else
38291 cp_parser_error (parser, "expected identifier");
38292
38293 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38294 error1:
38295 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38296 return attr;
38297 }
38298
38299 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38300
38301 transaction-statement:
38302 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38303 compound-statement
38304 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38305 */
38306
38307 static tree
38308 cp_parser_transaction (cp_parser *parser, cp_token *token)
38309 {
38310 unsigned char old_in = parser->in_transaction;
38311 unsigned char this_in = 1, new_in;
38312 enum rid keyword = token->keyword;
38313 tree stmt, attrs, noex;
38314
38315 cp_lexer_consume_token (parser->lexer);
38316
38317 if (keyword == RID_TRANSACTION_RELAXED
38318 || keyword == RID_SYNCHRONIZED)
38319 this_in |= TM_STMT_ATTR_RELAXED;
38320 else
38321 {
38322 attrs = cp_parser_txn_attribute_opt (parser);
38323 if (attrs)
38324 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38325 }
38326
38327 /* Parse a noexcept specification. */
38328 if (keyword == RID_ATOMIC_NOEXCEPT)
38329 noex = boolean_true_node;
38330 else if (keyword == RID_ATOMIC_CANCEL)
38331 {
38332 /* cancel-and-throw is unimplemented. */
38333 sorry ("atomic_cancel");
38334 noex = NULL_TREE;
38335 }
38336 else
38337 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38338
38339 /* Keep track if we're in the lexical scope of an outer transaction. */
38340 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38341
38342 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38343
38344 parser->in_transaction = new_in;
38345 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38346 parser->in_transaction = old_in;
38347
38348 finish_transaction_stmt (stmt, NULL, this_in, noex);
38349
38350 return stmt;
38351 }
38352
38353 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38354
38355 transaction-expression:
38356 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38357 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38358 */
38359
38360 static tree
38361 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38362 {
38363 unsigned char old_in = parser->in_transaction;
38364 unsigned char this_in = 1;
38365 cp_token *token;
38366 tree expr, noex;
38367 bool noex_expr;
38368 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38369
38370 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38371 || keyword == RID_TRANSACTION_RELAXED);
38372
38373 if (!flag_tm)
38374 error_at (loc,
38375 keyword == RID_TRANSACTION_RELAXED
38376 ? G_("%<__transaction_relaxed%> without transactional memory "
38377 "support enabled")
38378 : G_("%<__transaction_atomic%> without transactional memory "
38379 "support enabled"));
38380
38381 token = cp_parser_require_keyword (parser, keyword,
38382 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38383 : RT_TRANSACTION_RELAXED));
38384 gcc_assert (token != NULL);
38385
38386 if (keyword == RID_TRANSACTION_RELAXED)
38387 this_in |= TM_STMT_ATTR_RELAXED;
38388
38389 /* Set this early. This might mean that we allow transaction_cancel in
38390 an expression that we find out later actually has to be a constexpr.
38391 However, we expect that cxx_constant_value will be able to deal with
38392 this; also, if the noexcept has no constexpr, then what we parse next
38393 really is a transaction's body. */
38394 parser->in_transaction = this_in;
38395
38396 /* Parse a noexcept specification. */
38397 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38398 true);
38399
38400 if (!noex || !noex_expr
38401 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38402 {
38403 matching_parens parens;
38404 parens.require_open (parser);
38405
38406 expr = cp_parser_expression (parser);
38407 expr = finish_parenthesized_expr (expr);
38408
38409 parens.require_close (parser);
38410 }
38411 else
38412 {
38413 /* The only expression that is available got parsed for the noexcept
38414 already. noexcept is true then. */
38415 expr = noex;
38416 noex = boolean_true_node;
38417 }
38418
38419 expr = build_transaction_expr (token->location, expr, this_in, noex);
38420 parser->in_transaction = old_in;
38421
38422 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38423 return error_mark_node;
38424
38425 return (flag_tm ? expr : error_mark_node);
38426 }
38427
38428 /* Parse a function-transaction-block.
38429
38430 function-transaction-block:
38431 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38432 function-body
38433 __transaction_atomic txn-attribute[opt] function-try-block
38434 __transaction_relaxed ctor-initializer[opt] function-body
38435 __transaction_relaxed function-try-block
38436 */
38437
38438 static void
38439 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38440 {
38441 unsigned char old_in = parser->in_transaction;
38442 unsigned char new_in = 1;
38443 tree compound_stmt, stmt, attrs;
38444 cp_token *token;
38445
38446 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38447 || keyword == RID_TRANSACTION_RELAXED);
38448 token = cp_parser_require_keyword (parser, keyword,
38449 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38450 : RT_TRANSACTION_RELAXED));
38451 gcc_assert (token != NULL);
38452
38453 if (keyword == RID_TRANSACTION_RELAXED)
38454 new_in |= TM_STMT_ATTR_RELAXED;
38455 else
38456 {
38457 attrs = cp_parser_txn_attribute_opt (parser);
38458 if (attrs)
38459 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38460 }
38461
38462 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38463
38464 parser->in_transaction = new_in;
38465
38466 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38467 cp_parser_function_try_block (parser);
38468 else
38469 cp_parser_ctor_initializer_opt_and_function_body
38470 (parser, /*in_function_try_block=*/false);
38471
38472 parser->in_transaction = old_in;
38473
38474 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38475 }
38476
38477 /* Parse a __transaction_cancel statement.
38478
38479 cancel-statement:
38480 __transaction_cancel txn-attribute[opt] ;
38481 __transaction_cancel txn-attribute[opt] throw-expression ;
38482
38483 ??? Cancel and throw is not yet implemented. */
38484
38485 static tree
38486 cp_parser_transaction_cancel (cp_parser *parser)
38487 {
38488 cp_token *token;
38489 bool is_outer = false;
38490 tree stmt, attrs;
38491
38492 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38493 RT_TRANSACTION_CANCEL);
38494 gcc_assert (token != NULL);
38495
38496 attrs = cp_parser_txn_attribute_opt (parser);
38497 if (attrs)
38498 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38499
38500 /* ??? Parse cancel-and-throw here. */
38501
38502 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38503
38504 if (!flag_tm)
38505 {
38506 error_at (token->location, "%<__transaction_cancel%> without "
38507 "transactional memory support enabled");
38508 return error_mark_node;
38509 }
38510 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38511 {
38512 error_at (token->location, "%<__transaction_cancel%> within a "
38513 "%<__transaction_relaxed%>");
38514 return error_mark_node;
38515 }
38516 else if (is_outer)
38517 {
38518 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38519 && !is_tm_may_cancel_outer (current_function_decl))
38520 {
38521 error_at (token->location, "outer %<__transaction_cancel%> not "
38522 "within outer %<__transaction_atomic%>");
38523 error_at (token->location,
38524 " or a %<transaction_may_cancel_outer%> function");
38525 return error_mark_node;
38526 }
38527 }
38528 else if (parser->in_transaction == 0)
38529 {
38530 error_at (token->location, "%<__transaction_cancel%> not within "
38531 "%<__transaction_atomic%>");
38532 return error_mark_node;
38533 }
38534
38535 stmt = build_tm_abort_call (token->location, is_outer);
38536 add_stmt (stmt);
38537
38538 return stmt;
38539 }
38540 \f
38541 /* The parser. */
38542
38543 static GTY (()) cp_parser *the_parser;
38544
38545 \f
38546 /* Special handling for the first token or line in the file. The first
38547 thing in the file might be #pragma GCC pch_preprocess, which loads a
38548 PCH file, which is a GC collection point. So we need to handle this
38549 first pragma without benefit of an existing lexer structure.
38550
38551 Always returns one token to the caller in *FIRST_TOKEN. This is
38552 either the true first token of the file, or the first token after
38553 the initial pragma. */
38554
38555 static void
38556 cp_parser_initial_pragma (cp_token *first_token)
38557 {
38558 tree name = NULL;
38559
38560 cp_lexer_get_preprocessor_token (NULL, first_token);
38561 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38562 return;
38563
38564 cp_lexer_get_preprocessor_token (NULL, first_token);
38565 if (first_token->type == CPP_STRING)
38566 {
38567 name = first_token->u.value;
38568
38569 cp_lexer_get_preprocessor_token (NULL, first_token);
38570 if (first_token->type != CPP_PRAGMA_EOL)
38571 error_at (first_token->location,
38572 "junk at end of %<#pragma GCC pch_preprocess%>");
38573 }
38574 else
38575 error_at (first_token->location, "expected string literal");
38576
38577 /* Skip to the end of the pragma. */
38578 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38579 cp_lexer_get_preprocessor_token (NULL, first_token);
38580
38581 /* Now actually load the PCH file. */
38582 if (name)
38583 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38584
38585 /* Read one more token to return to our caller. We have to do this
38586 after reading the PCH file in, since its pointers have to be
38587 live. */
38588 cp_lexer_get_preprocessor_token (NULL, first_token);
38589 }
38590
38591 /* Parse a pragma GCC ivdep. */
38592
38593 static bool
38594 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38595 {
38596 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38597 return true;
38598 }
38599
38600 /* Parse a pragma GCC unroll. */
38601
38602 static unsigned short
38603 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38604 {
38605 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38606 tree expr = cp_parser_constant_expression (parser);
38607 unsigned short unroll;
38608 expr = maybe_constant_value (expr);
38609 HOST_WIDE_INT lunroll = 0;
38610 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38611 || TREE_CODE (expr) != INTEGER_CST
38612 || (lunroll = tree_to_shwi (expr)) < 0
38613 || lunroll >= USHRT_MAX)
38614 {
38615 error_at (location, "%<#pragma GCC unroll%> requires an"
38616 " assignment-expression that evaluates to a non-negative"
38617 " integral constant less than %u", USHRT_MAX);
38618 unroll = 0;
38619 }
38620 else
38621 {
38622 unroll = (unsigned short)lunroll;
38623 if (unroll == 0)
38624 unroll = 1;
38625 }
38626 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38627 return unroll;
38628 }
38629
38630 /* Normal parsing of a pragma token. Here we can (and must) use the
38631 regular lexer. */
38632
38633 static bool
38634 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38635 {
38636 cp_token *pragma_tok;
38637 unsigned int id;
38638 tree stmt;
38639 bool ret;
38640
38641 pragma_tok = cp_lexer_consume_token (parser->lexer);
38642 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38643 parser->lexer->in_pragma = true;
38644
38645 id = cp_parser_pragma_kind (pragma_tok);
38646 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38647 cp_ensure_no_omp_declare_simd (parser);
38648 switch (id)
38649 {
38650 case PRAGMA_GCC_PCH_PREPROCESS:
38651 error_at (pragma_tok->location,
38652 "%<#pragma GCC pch_preprocess%> must be first");
38653 break;
38654
38655 case PRAGMA_OMP_BARRIER:
38656 switch (context)
38657 {
38658 case pragma_compound:
38659 cp_parser_omp_barrier (parser, pragma_tok);
38660 return false;
38661 case pragma_stmt:
38662 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38663 "used in compound statements", "omp barrier");
38664 break;
38665 default:
38666 goto bad_stmt;
38667 }
38668 break;
38669
38670 case PRAGMA_OMP_FLUSH:
38671 switch (context)
38672 {
38673 case pragma_compound:
38674 cp_parser_omp_flush (parser, pragma_tok);
38675 return false;
38676 case pragma_stmt:
38677 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38678 "used in compound statements", "omp flush");
38679 break;
38680 default:
38681 goto bad_stmt;
38682 }
38683 break;
38684
38685 case PRAGMA_OMP_TASKWAIT:
38686 switch (context)
38687 {
38688 case pragma_compound:
38689 cp_parser_omp_taskwait (parser, pragma_tok);
38690 return false;
38691 case pragma_stmt:
38692 error_at (pragma_tok->location,
38693 "%<#pragma %s%> may only be used in compound statements",
38694 "omp taskwait");
38695 break;
38696 default:
38697 goto bad_stmt;
38698 }
38699 break;
38700
38701 case PRAGMA_OMP_TASKYIELD:
38702 switch (context)
38703 {
38704 case pragma_compound:
38705 cp_parser_omp_taskyield (parser, pragma_tok);
38706 return false;
38707 case pragma_stmt:
38708 error_at (pragma_tok->location,
38709 "%<#pragma %s%> may only be used in compound statements",
38710 "omp taskyield");
38711 break;
38712 default:
38713 goto bad_stmt;
38714 }
38715 break;
38716
38717 case PRAGMA_OMP_CANCEL:
38718 switch (context)
38719 {
38720 case pragma_compound:
38721 cp_parser_omp_cancel (parser, pragma_tok);
38722 return false;
38723 case pragma_stmt:
38724 error_at (pragma_tok->location,
38725 "%<#pragma %s%> may only be used in compound statements",
38726 "omp cancel");
38727 break;
38728 default:
38729 goto bad_stmt;
38730 }
38731 break;
38732
38733 case PRAGMA_OMP_CANCELLATION_POINT:
38734 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38735 return false;
38736
38737 case PRAGMA_OMP_THREADPRIVATE:
38738 cp_parser_omp_threadprivate (parser, pragma_tok);
38739 return false;
38740
38741 case PRAGMA_OMP_DECLARE:
38742 return cp_parser_omp_declare (parser, pragma_tok, context);
38743
38744 case PRAGMA_OACC_DECLARE:
38745 cp_parser_oacc_declare (parser, pragma_tok);
38746 return false;
38747
38748 case PRAGMA_OACC_ENTER_DATA:
38749 if (context == pragma_stmt)
38750 {
38751 error_at (pragma_tok->location,
38752 "%<#pragma %s%> may only be used in compound statements",
38753 "acc enter data");
38754 break;
38755 }
38756 else if (context != pragma_compound)
38757 goto bad_stmt;
38758 cp_parser_omp_construct (parser, pragma_tok, if_p);
38759 return true;
38760
38761 case PRAGMA_OACC_EXIT_DATA:
38762 if (context == pragma_stmt)
38763 {
38764 error_at (pragma_tok->location,
38765 "%<#pragma %s%> may only be used in compound statements",
38766 "acc exit data");
38767 break;
38768 }
38769 else if (context != pragma_compound)
38770 goto bad_stmt;
38771 cp_parser_omp_construct (parser, pragma_tok, if_p);
38772 return true;
38773
38774 case PRAGMA_OACC_ROUTINE:
38775 if (context != pragma_external)
38776 {
38777 error_at (pragma_tok->location,
38778 "%<#pragma acc routine%> must be at file scope");
38779 break;
38780 }
38781 cp_parser_oacc_routine (parser, pragma_tok, context);
38782 return false;
38783
38784 case PRAGMA_OACC_UPDATE:
38785 if (context == pragma_stmt)
38786 {
38787 error_at (pragma_tok->location,
38788 "%<#pragma %s%> may only be used in compound statements",
38789 "acc update");
38790 break;
38791 }
38792 else if (context != pragma_compound)
38793 goto bad_stmt;
38794 cp_parser_omp_construct (parser, pragma_tok, if_p);
38795 return true;
38796
38797 case PRAGMA_OACC_WAIT:
38798 if (context == pragma_stmt)
38799 {
38800 error_at (pragma_tok->location,
38801 "%<#pragma %s%> may only be used in compound statements",
38802 "acc wait");
38803 break;
38804 }
38805 else if (context != pragma_compound)
38806 goto bad_stmt;
38807 cp_parser_omp_construct (parser, pragma_tok, if_p);
38808 return true;
38809
38810 case PRAGMA_OACC_ATOMIC:
38811 case PRAGMA_OACC_CACHE:
38812 case PRAGMA_OACC_DATA:
38813 case PRAGMA_OACC_HOST_DATA:
38814 case PRAGMA_OACC_KERNELS:
38815 case PRAGMA_OACC_PARALLEL:
38816 case PRAGMA_OACC_LOOP:
38817 case PRAGMA_OMP_ATOMIC:
38818 case PRAGMA_OMP_CRITICAL:
38819 case PRAGMA_OMP_DISTRIBUTE:
38820 case PRAGMA_OMP_FOR:
38821 case PRAGMA_OMP_MASTER:
38822 case PRAGMA_OMP_PARALLEL:
38823 case PRAGMA_OMP_SECTIONS:
38824 case PRAGMA_OMP_SIMD:
38825 case PRAGMA_OMP_SINGLE:
38826 case PRAGMA_OMP_TASK:
38827 case PRAGMA_OMP_TASKGROUP:
38828 case PRAGMA_OMP_TASKLOOP:
38829 case PRAGMA_OMP_TEAMS:
38830 if (context != pragma_stmt && context != pragma_compound)
38831 goto bad_stmt;
38832 stmt = push_omp_privatization_clauses (false);
38833 cp_parser_omp_construct (parser, pragma_tok, if_p);
38834 pop_omp_privatization_clauses (stmt);
38835 return true;
38836
38837 case PRAGMA_OMP_ORDERED:
38838 if (context != pragma_stmt && context != pragma_compound)
38839 goto bad_stmt;
38840 stmt = push_omp_privatization_clauses (false);
38841 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38842 pop_omp_privatization_clauses (stmt);
38843 return ret;
38844
38845 case PRAGMA_OMP_TARGET:
38846 if (context != pragma_stmt && context != pragma_compound)
38847 goto bad_stmt;
38848 stmt = push_omp_privatization_clauses (false);
38849 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38850 pop_omp_privatization_clauses (stmt);
38851 return ret;
38852
38853 case PRAGMA_OMP_END_DECLARE_TARGET:
38854 cp_parser_omp_end_declare_target (parser, pragma_tok);
38855 return false;
38856
38857 case PRAGMA_OMP_SECTION:
38858 error_at (pragma_tok->location,
38859 "%<#pragma omp section%> may only be used in "
38860 "%<#pragma omp sections%> construct");
38861 break;
38862
38863 case PRAGMA_IVDEP:
38864 {
38865 if (context == pragma_external)
38866 {
38867 error_at (pragma_tok->location,
38868 "%<#pragma GCC ivdep%> must be inside a function");
38869 break;
38870 }
38871 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38872 unsigned short unroll;
38873 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38874 if (tok->type == CPP_PRAGMA
38875 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38876 {
38877 tok = cp_lexer_consume_token (parser->lexer);
38878 unroll = cp_parser_pragma_unroll (parser, tok);
38879 tok = cp_lexer_peek_token (the_parser->lexer);
38880 }
38881 else
38882 unroll = 0;
38883 if (tok->type != CPP_KEYWORD
38884 || (tok->keyword != RID_FOR
38885 && tok->keyword != RID_WHILE
38886 && tok->keyword != RID_DO))
38887 {
38888 cp_parser_error (parser, "for, while or do statement expected");
38889 return false;
38890 }
38891 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38892 return true;
38893 }
38894
38895 case PRAGMA_UNROLL:
38896 {
38897 if (context == pragma_external)
38898 {
38899 error_at (pragma_tok->location,
38900 "%<#pragma GCC unroll%> must be inside a function");
38901 break;
38902 }
38903 const unsigned short unroll
38904 = cp_parser_pragma_unroll (parser, pragma_tok);
38905 bool ivdep;
38906 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38907 if (tok->type == CPP_PRAGMA
38908 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
38909 {
38910 tok = cp_lexer_consume_token (parser->lexer);
38911 ivdep = cp_parser_pragma_ivdep (parser, tok);
38912 tok = cp_lexer_peek_token (the_parser->lexer);
38913 }
38914 else
38915 ivdep = false;
38916 if (tok->type != CPP_KEYWORD
38917 || (tok->keyword != RID_FOR
38918 && tok->keyword != RID_WHILE
38919 && tok->keyword != RID_DO))
38920 {
38921 cp_parser_error (parser, "for, while or do statement expected");
38922 return false;
38923 }
38924 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
38925 return true;
38926 }
38927
38928 default:
38929 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38930 c_invoke_pragma_handler (id);
38931 break;
38932
38933 bad_stmt:
38934 cp_parser_error (parser, "expected declaration specifiers");
38935 break;
38936 }
38937
38938 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38939 return false;
38940 }
38941
38942 /* The interface the pragma parsers have to the lexer. */
38943
38944 enum cpp_ttype
38945 pragma_lex (tree *value, location_t *loc)
38946 {
38947 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38948 enum cpp_ttype ret = tok->type;
38949
38950 *value = tok->u.value;
38951 if (loc)
38952 *loc = tok->location;
38953
38954 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38955 ret = CPP_EOF;
38956 else if (ret == CPP_STRING)
38957 *value = cp_parser_string_literal (the_parser, false, false);
38958 else
38959 {
38960 if (ret == CPP_KEYWORD)
38961 ret = CPP_NAME;
38962 cp_lexer_consume_token (the_parser->lexer);
38963 }
38964
38965 return ret;
38966 }
38967
38968 \f
38969 /* External interface. */
38970
38971 /* Parse one entire translation unit. */
38972
38973 void
38974 c_parse_file (void)
38975 {
38976 static bool already_called = false;
38977
38978 if (already_called)
38979 fatal_error (input_location,
38980 "inter-module optimizations not implemented for C++");
38981 already_called = true;
38982
38983 the_parser = cp_parser_new ();
38984 push_deferring_access_checks (flag_access_control
38985 ? dk_no_deferred : dk_no_check);
38986 cp_parser_translation_unit (the_parser);
38987 the_parser = NULL;
38988 }
38989
38990 /* Create an identifier for a generic parameter type (a synthesized
38991 template parameter implied by `auto' or a concept identifier). */
38992
38993 static GTY(()) int generic_parm_count;
38994 static tree
38995 make_generic_type_name ()
38996 {
38997 char buf[32];
38998 sprintf (buf, "auto:%d", ++generic_parm_count);
38999 return get_identifier (buf);
39000 }
39001
39002 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39003 (creating a new template parameter list if necessary). Returns the newly
39004 created template type parm. */
39005
39006 static tree
39007 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39008 {
39009 gcc_assert (current_binding_level->kind == sk_function_parms);
39010
39011 /* Before committing to modifying any scope, if we're in an
39012 implicit template scope, and we're trying to synthesize a
39013 constrained parameter, try to find a previous parameter with
39014 the same name. This is the same-type rule for abbreviated
39015 function templates.
39016
39017 NOTE: We can generate implicit parameters when tentatively
39018 parsing a nested name specifier, only to reject that parse
39019 later. However, matching the same template-id as part of a
39020 direct-declarator should generate an identical template
39021 parameter, so this rule will merge them. */
39022 if (parser->implicit_template_scope && constr)
39023 {
39024 tree t = parser->implicit_template_parms;
39025 while (t)
39026 {
39027 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39028 {
39029 tree d = TREE_VALUE (t);
39030 if (TREE_CODE (d) == PARM_DECL)
39031 /* Return the TEMPLATE_PARM_INDEX. */
39032 d = DECL_INITIAL (d);
39033 return d;
39034 }
39035 t = TREE_CHAIN (t);
39036 }
39037 }
39038
39039 /* We are either continuing a function template that already contains implicit
39040 template parameters, creating a new fully-implicit function template, or
39041 extending an existing explicit function template with implicit template
39042 parameters. */
39043
39044 cp_binding_level *const entry_scope = current_binding_level;
39045
39046 bool become_template = false;
39047 cp_binding_level *parent_scope = 0;
39048
39049 if (parser->implicit_template_scope)
39050 {
39051 gcc_assert (parser->implicit_template_parms);
39052
39053 current_binding_level = parser->implicit_template_scope;
39054 }
39055 else
39056 {
39057 /* Roll back to the existing template parameter scope (in the case of
39058 extending an explicit function template) or introduce a new template
39059 parameter scope ahead of the function parameter scope (or class scope
39060 in the case of out-of-line member definitions). The function scope is
39061 added back after template parameter synthesis below. */
39062
39063 cp_binding_level *scope = entry_scope;
39064
39065 while (scope->kind == sk_function_parms)
39066 {
39067 parent_scope = scope;
39068 scope = scope->level_chain;
39069 }
39070 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39071 {
39072 /* If not defining a class, then any class scope is a scope level in
39073 an out-of-line member definition. In this case simply wind back
39074 beyond the first such scope to inject the template parameter list.
39075 Otherwise wind back to the class being defined. The latter can
39076 occur in class member friend declarations such as:
39077
39078 class A {
39079 void foo (auto);
39080 };
39081 class B {
39082 friend void A::foo (auto);
39083 };
39084
39085 The template parameter list synthesized for the friend declaration
39086 must be injected in the scope of 'B'. This can also occur in
39087 erroneous cases such as:
39088
39089 struct A {
39090 struct B {
39091 void foo (auto);
39092 };
39093 void B::foo (auto) {}
39094 };
39095
39096 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39097 but, nevertheless, the template parameter list synthesized for the
39098 declarator should be injected into the scope of 'A' as if the
39099 ill-formed template was specified explicitly. */
39100
39101 while (scope->kind == sk_class && !scope->defining_class_p)
39102 {
39103 parent_scope = scope;
39104 scope = scope->level_chain;
39105 }
39106 }
39107
39108 current_binding_level = scope;
39109
39110 if (scope->kind != sk_template_parms
39111 || !function_being_declared_is_template_p (parser))
39112 {
39113 /* Introduce a new template parameter list for implicit template
39114 parameters. */
39115
39116 become_template = true;
39117
39118 parser->implicit_template_scope
39119 = begin_scope (sk_template_parms, NULL);
39120
39121 ++processing_template_decl;
39122
39123 parser->fully_implicit_function_template_p = true;
39124 ++parser->num_template_parameter_lists;
39125 }
39126 else
39127 {
39128 /* Synthesize implicit template parameters at the end of the explicit
39129 template parameter list. */
39130
39131 gcc_assert (current_template_parms);
39132
39133 parser->implicit_template_scope = scope;
39134
39135 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39136 parser->implicit_template_parms
39137 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39138 }
39139 }
39140
39141 /* Synthesize a new template parameter and track the current template
39142 parameter chain with implicit_template_parms. */
39143
39144 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39145 tree synth_id = make_generic_type_name ();
39146 tree synth_tmpl_parm;
39147 bool non_type = false;
39148
39149 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39150 synth_tmpl_parm
39151 = finish_template_type_parm (class_type_node, synth_id);
39152 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39153 synth_tmpl_parm
39154 = finish_constrained_template_template_parm (proto, synth_id);
39155 else
39156 {
39157 synth_tmpl_parm = copy_decl (proto);
39158 DECL_NAME (synth_tmpl_parm) = synth_id;
39159 non_type = true;
39160 }
39161
39162 // Attach the constraint to the parm before processing.
39163 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39164 TREE_TYPE (node) = constr;
39165 tree new_parm
39166 = process_template_parm (parser->implicit_template_parms,
39167 input_location,
39168 node,
39169 /*non_type=*/non_type,
39170 /*param_pack=*/false);
39171
39172 // Chain the new parameter to the list of implicit parameters.
39173 if (parser->implicit_template_parms)
39174 parser->implicit_template_parms
39175 = TREE_CHAIN (parser->implicit_template_parms);
39176 else
39177 parser->implicit_template_parms = new_parm;
39178
39179 tree new_decl = get_local_decls ();
39180 if (non_type)
39181 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39182 new_decl = DECL_INITIAL (new_decl);
39183
39184 /* If creating a fully implicit function template, start the new implicit
39185 template parameter list with this synthesized type, otherwise grow the
39186 current template parameter list. */
39187
39188 if (become_template)
39189 {
39190 parent_scope->level_chain = current_binding_level;
39191
39192 tree new_parms = make_tree_vec (1);
39193 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39194 current_template_parms = tree_cons (size_int (processing_template_decl),
39195 new_parms, current_template_parms);
39196 }
39197 else
39198 {
39199 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39200 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39201 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39202 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39203 }
39204
39205 // If the new parameter was constrained, we need to add that to the
39206 // constraints in the template parameter list.
39207 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39208 {
39209 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39210 reqs = conjoin_constraints (reqs, req);
39211 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39212 }
39213
39214 current_binding_level = entry_scope;
39215
39216 return new_decl;
39217 }
39218
39219 /* Finish the declaration of a fully implicit function template. Such a
39220 template has no explicit template parameter list so has not been through the
39221 normal template head and tail processing. synthesize_implicit_template_parm
39222 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39223 provided if the declaration is a class member such that its template
39224 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39225 form is returned. Otherwise NULL_TREE is returned. */
39226
39227 static tree
39228 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39229 {
39230 gcc_assert (parser->fully_implicit_function_template_p);
39231
39232 if (member_decl_opt && member_decl_opt != error_mark_node
39233 && DECL_VIRTUAL_P (member_decl_opt))
39234 {
39235 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39236 "implicit templates may not be %<virtual%>");
39237 DECL_VIRTUAL_P (member_decl_opt) = false;
39238 }
39239
39240 if (member_decl_opt)
39241 member_decl_opt = finish_member_template_decl (member_decl_opt);
39242 end_template_decl ();
39243
39244 parser->fully_implicit_function_template_p = false;
39245 --parser->num_template_parameter_lists;
39246
39247 return member_decl_opt;
39248 }
39249
39250 /* Helper function for diagnostics that have complained about things
39251 being used with 'extern "C"' linkage.
39252
39253 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39254
39255 void
39256 maybe_show_extern_c_location (void)
39257 {
39258 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39259 inform (the_parser->innermost_linkage_specification_location,
39260 "%<extern \"C\"%> linkage started here");
39261 }
39262
39263 #include "gt-cp-parser.h"