]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
PR c++/60503 - wrong lambda attribute syntax.
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-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, "String expressions should be translated "
560 "to execution character set",
561 parser->translate_strings_p);
562 cp_debug_print_flag (file, "Parsing function body outside of a "
563 "local class", parser->in_function_body);
564 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
565 parser->colon_corrects_to_scope_p);
566 cp_debug_print_flag (file, "Colon doesn't start a class definition",
567 parser->colon_doesnt_start_class_def_p);
568 if (parser->type_definition_forbidden_message)
569 fprintf (file, "Error message for forbidden type definitions: %s\n",
570 parser->type_definition_forbidden_message);
571 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
572 fprintf (file, "Number of class definitions in progress: %u\n",
573 parser->num_classes_being_defined);
574 fprintf (file, "Number of template parameter lists for the current "
575 "declaration: %u\n", parser->num_template_parameter_lists);
576 cp_debug_parser_tokens (file, parser, window_size);
577 token = parser->lexer->next_token;
578 fprintf (file, "Next token to parse:\n");
579 fprintf (file, "\tToken: ");
580 cp_lexer_print_token (file, token);
581 eloc = expand_location (token->location);
582 fprintf (file, "\n\tFile: %s\n", eloc.file);
583 fprintf (file, "\tLine: %d\n", eloc.line);
584 fprintf (file, "\tColumn: %d\n", eloc.column);
585 }
586
587 DEBUG_FUNCTION void
588 debug (cp_parser &ref)
589 {
590 cp_debug_parser (stderr, &ref);
591 }
592
593 DEBUG_FUNCTION void
594 debug (cp_parser *ptr)
595 {
596 if (ptr)
597 debug (*ptr);
598 else
599 fprintf (stderr, "<nil>\n");
600 }
601
602 /* Allocate memory for a new lexer object and return it. */
603
604 static cp_lexer *
605 cp_lexer_alloc (void)
606 {
607 cp_lexer *lexer;
608
609 c_common_no_more_pch ();
610
611 /* Allocate the memory. */
612 lexer = ggc_cleared_alloc<cp_lexer> ();
613
614 /* Initially we are not debugging. */
615 lexer->debugging_p = false;
616
617 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
618
619 /* Create the buffer. */
620 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
621
622 return lexer;
623 }
624
625
626 /* Create a new main C++ lexer, the lexer that gets tokens from the
627 preprocessor. */
628
629 static cp_lexer *
630 cp_lexer_new_main (void)
631 {
632 cp_lexer *lexer;
633 cp_token token;
634
635 /* It's possible that parsing the first pragma will load a PCH file,
636 which is a GC collection point. So we have to do that before
637 allocating any memory. */
638 cp_parser_initial_pragma (&token);
639
640 lexer = cp_lexer_alloc ();
641
642 /* Put the first token in the buffer. */
643 lexer->buffer->quick_push (token);
644
645 /* Get the remaining tokens from the preprocessor. */
646 while (token.type != CPP_EOF)
647 {
648 cp_lexer_get_preprocessor_token (lexer, &token);
649 vec_safe_push (lexer->buffer, token);
650 }
651
652 lexer->last_token = lexer->buffer->address ()
653 + lexer->buffer->length ()
654 - 1;
655 lexer->next_token = lexer->buffer->length ()
656 ? lexer->buffer->address ()
657 : &eof_token;
658
659 /* Subsequent preprocessor diagnostics should use compiler
660 diagnostic functions to get the compiler source location. */
661 done_lexing = true;
662
663 gcc_assert (!lexer->next_token->purged_p);
664 return lexer;
665 }
666
667 /* Create a new lexer whose token stream is primed with the tokens in
668 CACHE. When these tokens are exhausted, no new tokens will be read. */
669
670 static cp_lexer *
671 cp_lexer_new_from_tokens (cp_token_cache *cache)
672 {
673 cp_token *first = cache->first;
674 cp_token *last = cache->last;
675 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
676
677 /* We do not own the buffer. */
678 lexer->buffer = NULL;
679 lexer->next_token = first == last ? &eof_token : first;
680 lexer->last_token = last;
681
682 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
683
684 /* Initially we are not debugging. */
685 lexer->debugging_p = false;
686
687 gcc_assert (!lexer->next_token->purged_p);
688 return lexer;
689 }
690
691 /* Frees all resources associated with LEXER. */
692
693 static void
694 cp_lexer_destroy (cp_lexer *lexer)
695 {
696 vec_free (lexer->buffer);
697 lexer->saved_tokens.release ();
698 ggc_free (lexer);
699 }
700
701 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
702 be used. The point of this flag is to help the compiler to fold away calls
703 to cp_lexer_debugging_p within this source file at compile time, when the
704 lexer is not being debugged. */
705
706 #define LEXER_DEBUGGING_ENABLED_P false
707
708 /* Returns nonzero if debugging information should be output. */
709
710 static inline bool
711 cp_lexer_debugging_p (cp_lexer *lexer)
712 {
713 if (!LEXER_DEBUGGING_ENABLED_P)
714 return false;
715
716 return lexer->debugging_p;
717 }
718
719
720 static inline cp_token_position
721 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
722 {
723 gcc_assert (!previous_p || lexer->next_token != &eof_token);
724
725 return lexer->next_token - previous_p;
726 }
727
728 static inline cp_token *
729 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
730 {
731 return pos;
732 }
733
734 static inline void
735 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
736 {
737 lexer->next_token = cp_lexer_token_at (lexer, pos);
738 }
739
740 static inline cp_token_position
741 cp_lexer_previous_token_position (cp_lexer *lexer)
742 {
743 if (lexer->next_token == &eof_token)
744 return lexer->last_token - 1;
745 else
746 return cp_lexer_token_position (lexer, true);
747 }
748
749 static inline cp_token *
750 cp_lexer_previous_token (cp_lexer *lexer)
751 {
752 cp_token_position tp = cp_lexer_previous_token_position (lexer);
753
754 /* Skip past purged tokens. */
755 while (tp->purged_p)
756 {
757 gcc_assert (tp != vec_safe_address (lexer->buffer));
758 tp--;
759 }
760
761 return cp_lexer_token_at (lexer, tp);
762 }
763
764 /* nonzero if we are presently saving tokens. */
765
766 static inline int
767 cp_lexer_saving_tokens (const cp_lexer* lexer)
768 {
769 return lexer->saved_tokens.length () != 0;
770 }
771
772 /* Store the next token from the preprocessor in *TOKEN. Return true
773 if we reach EOF. If LEXER is NULL, assume we are handling an
774 initial #pragma pch_preprocess, and thus want the lexer to return
775 processed strings. */
776
777 static void
778 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
779 {
780 static int is_extern_c = 0;
781
782 /* Get a new token from the preprocessor. */
783 token->type
784 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
785 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
786 token->keyword = RID_MAX;
787 token->purged_p = false;
788 token->error_reported = false;
789
790 /* On some systems, some header files are surrounded by an
791 implicit extern "C" block. Set a flag in the token if it
792 comes from such a header. */
793 is_extern_c += pending_lang_change;
794 pending_lang_change = 0;
795 token->implicit_extern_c = is_extern_c > 0;
796
797 /* Check to see if this token is a keyword. */
798 if (token->type == CPP_NAME)
799 {
800 if (IDENTIFIER_KEYWORD_P (token->u.value))
801 {
802 /* Mark this token as a keyword. */
803 token->type = CPP_KEYWORD;
804 /* Record which keyword. */
805 token->keyword = C_RID_CODE (token->u.value);
806 }
807 else
808 {
809 if (warn_cxx11_compat
810 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
811 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
812 {
813 /* Warn about the C++0x keyword (but still treat it as
814 an identifier). */
815 warning (OPT_Wc__11_compat,
816 "identifier %qE is a keyword in C++11",
817 token->u.value);
818
819 /* Clear out the C_RID_CODE so we don't warn about this
820 particular identifier-turned-keyword again. */
821 C_SET_RID_CODE (token->u.value, RID_MAX);
822 }
823
824 token->keyword = RID_MAX;
825 }
826 }
827 else if (token->type == CPP_AT_NAME)
828 {
829 /* This only happens in Objective-C++; it must be a keyword. */
830 token->type = CPP_KEYWORD;
831 switch (C_RID_CODE (token->u.value))
832 {
833 /* Replace 'class' with '@class', 'private' with '@private',
834 etc. This prevents confusion with the C++ keyword
835 'class', and makes the tokens consistent with other
836 Objective-C 'AT' keywords. For example '@class' is
837 reported as RID_AT_CLASS which is consistent with
838 '@synchronized', which is reported as
839 RID_AT_SYNCHRONIZED.
840 */
841 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
842 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
843 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
844 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
845 case RID_THROW: token->keyword = RID_AT_THROW; break;
846 case RID_TRY: token->keyword = RID_AT_TRY; break;
847 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
848 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
849 default: token->keyword = C_RID_CODE (token->u.value);
850 }
851 }
852 }
853
854 /* Update the globals input_location and the input file stack from TOKEN. */
855 static inline void
856 cp_lexer_set_source_position_from_token (cp_token *token)
857 {
858 if (token->type != CPP_EOF)
859 {
860 input_location = token->location;
861 }
862 }
863
864 /* Update the globals input_location and the input file stack from LEXER. */
865 static inline void
866 cp_lexer_set_source_position (cp_lexer *lexer)
867 {
868 cp_token *token = cp_lexer_peek_token (lexer);
869 cp_lexer_set_source_position_from_token (token);
870 }
871
872 /* Return a pointer to the next token in the token stream, but do not
873 consume it. */
874
875 static inline cp_token *
876 cp_lexer_peek_token (cp_lexer *lexer)
877 {
878 if (cp_lexer_debugging_p (lexer))
879 {
880 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
881 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
882 putc ('\n', cp_lexer_debug_stream);
883 }
884 return lexer->next_token;
885 }
886
887 /* Return true if the next token has the indicated TYPE. */
888
889 static inline bool
890 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
891 {
892 return cp_lexer_peek_token (lexer)->type == type;
893 }
894
895 /* Return true if the next token does not have the indicated TYPE. */
896
897 static inline bool
898 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
899 {
900 return !cp_lexer_next_token_is (lexer, type);
901 }
902
903 /* Return true if the next token is the indicated KEYWORD. */
904
905 static inline bool
906 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
907 {
908 return cp_lexer_peek_token (lexer)->keyword == keyword;
909 }
910
911 static inline bool
912 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
913 {
914 return cp_lexer_peek_nth_token (lexer, n)->type == type;
915 }
916
917 static inline bool
918 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
919 {
920 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
921 }
922
923 /* Return true if KEYWORD can start a decl-specifier. */
924
925 bool
926 cp_keyword_starts_decl_specifier_p (enum rid keyword)
927 {
928 switch (keyword)
929 {
930 /* auto specifier: storage-class-specifier in C++,
931 simple-type-specifier in C++0x. */
932 case RID_AUTO:
933 /* Storage classes. */
934 case RID_REGISTER:
935 case RID_STATIC:
936 case RID_EXTERN:
937 case RID_MUTABLE:
938 case RID_THREAD:
939 /* Elaborated type specifiers. */
940 case RID_ENUM:
941 case RID_CLASS:
942 case RID_STRUCT:
943 case RID_UNION:
944 case RID_TYPENAME:
945 /* Simple type specifiers. */
946 case RID_CHAR:
947 case RID_CHAR16:
948 case RID_CHAR32:
949 case RID_WCHAR:
950 case RID_BOOL:
951 case RID_SHORT:
952 case RID_INT:
953 case RID_LONG:
954 case RID_SIGNED:
955 case RID_UNSIGNED:
956 case RID_FLOAT:
957 case RID_DOUBLE:
958 case RID_VOID:
959 /* GNU extensions. */
960 case RID_ATTRIBUTE:
961 case RID_TYPEOF:
962 /* C++0x extensions. */
963 case RID_DECLTYPE:
964 case RID_UNDERLYING_TYPE:
965 case RID_CONSTEXPR:
966 return true;
967
968 default:
969 if (keyword >= RID_FIRST_INT_N
970 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
971 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
972 return true;
973 return false;
974 }
975 }
976
977 /* Return true if the next token is a keyword for a decl-specifier. */
978
979 static bool
980 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
981 {
982 cp_token *token;
983
984 token = cp_lexer_peek_token (lexer);
985 return cp_keyword_starts_decl_specifier_p (token->keyword);
986 }
987
988 /* Returns TRUE iff the token T begins a decltype type. */
989
990 static bool
991 token_is_decltype (cp_token *t)
992 {
993 return (t->keyword == RID_DECLTYPE
994 || t->type == CPP_DECLTYPE);
995 }
996
997 /* Returns TRUE iff the next token begins a decltype type. */
998
999 static bool
1000 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1001 {
1002 cp_token *t = cp_lexer_peek_token (lexer);
1003 return token_is_decltype (t);
1004 }
1005
1006 /* Called when processing a token with tree_check_value; perform or defer the
1007 associated checks and return the value. */
1008
1009 static tree
1010 saved_checks_value (struct tree_check *check_value)
1011 {
1012 /* Perform any access checks that were deferred. */
1013 vec<deferred_access_check, va_gc> *checks;
1014 deferred_access_check *chk;
1015 checks = check_value->checks;
1016 if (checks)
1017 {
1018 int i;
1019 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1020 perform_or_defer_access_check (chk->binfo,
1021 chk->decl,
1022 chk->diag_decl, tf_warning_or_error);
1023 }
1024 /* Return the stored value. */
1025 return check_value->value;
1026 }
1027
1028 /* Return a pointer to the Nth token in the token stream. If N is 1,
1029 then this is precisely equivalent to cp_lexer_peek_token (except
1030 that it is not inline). One would like to disallow that case, but
1031 there is one case (cp_parser_nth_token_starts_template_id) where
1032 the caller passes a variable for N and it might be 1. */
1033
1034 static cp_token *
1035 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1036 {
1037 cp_token *token;
1038
1039 /* N is 1-based, not zero-based. */
1040 gcc_assert (n > 0);
1041
1042 if (cp_lexer_debugging_p (lexer))
1043 fprintf (cp_lexer_debug_stream,
1044 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1045
1046 --n;
1047 token = lexer->next_token;
1048 gcc_assert (!n || token != &eof_token);
1049 while (n != 0)
1050 {
1051 ++token;
1052 if (token == lexer->last_token)
1053 {
1054 token = &eof_token;
1055 break;
1056 }
1057
1058 if (!token->purged_p)
1059 --n;
1060 }
1061
1062 if (cp_lexer_debugging_p (lexer))
1063 {
1064 cp_lexer_print_token (cp_lexer_debug_stream, token);
1065 putc ('\n', cp_lexer_debug_stream);
1066 }
1067
1068 return token;
1069 }
1070
1071 /* Return the next token, and advance the lexer's next_token pointer
1072 to point to the next non-purged token. */
1073
1074 static cp_token *
1075 cp_lexer_consume_token (cp_lexer* lexer)
1076 {
1077 cp_token *token = lexer->next_token;
1078
1079 gcc_assert (token != &eof_token);
1080 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1081
1082 do
1083 {
1084 lexer->next_token++;
1085 if (lexer->next_token == lexer->last_token)
1086 {
1087 lexer->next_token = &eof_token;
1088 break;
1089 }
1090
1091 }
1092 while (lexer->next_token->purged_p);
1093
1094 cp_lexer_set_source_position_from_token (token);
1095
1096 /* Provide debugging output. */
1097 if (cp_lexer_debugging_p (lexer))
1098 {
1099 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1100 cp_lexer_print_token (cp_lexer_debug_stream, token);
1101 putc ('\n', cp_lexer_debug_stream);
1102 }
1103
1104 return token;
1105 }
1106
1107 /* Permanently remove the next token from the token stream, and
1108 advance the next_token pointer to refer to the next non-purged
1109 token. */
1110
1111 static void
1112 cp_lexer_purge_token (cp_lexer *lexer)
1113 {
1114 cp_token *tok = lexer->next_token;
1115
1116 gcc_assert (tok != &eof_token);
1117 tok->purged_p = true;
1118 tok->location = UNKNOWN_LOCATION;
1119 tok->u.value = NULL_TREE;
1120 tok->keyword = RID_MAX;
1121
1122 do
1123 {
1124 tok++;
1125 if (tok == lexer->last_token)
1126 {
1127 tok = &eof_token;
1128 break;
1129 }
1130 }
1131 while (tok->purged_p);
1132 lexer->next_token = tok;
1133 }
1134
1135 /* Permanently remove all tokens after TOK, up to, but not
1136 including, the token that will be returned next by
1137 cp_lexer_peek_token. */
1138
1139 static void
1140 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1141 {
1142 cp_token *peek = lexer->next_token;
1143
1144 if (peek == &eof_token)
1145 peek = lexer->last_token;
1146
1147 gcc_assert (tok < peek);
1148
1149 for ( tok += 1; tok != peek; tok += 1)
1150 {
1151 tok->purged_p = true;
1152 tok->location = UNKNOWN_LOCATION;
1153 tok->u.value = NULL_TREE;
1154 tok->keyword = RID_MAX;
1155 }
1156 }
1157
1158 /* Begin saving tokens. All tokens consumed after this point will be
1159 preserved. */
1160
1161 static void
1162 cp_lexer_save_tokens (cp_lexer* lexer)
1163 {
1164 /* Provide debugging output. */
1165 if (cp_lexer_debugging_p (lexer))
1166 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1167
1168 lexer->saved_tokens.safe_push (lexer->next_token);
1169 }
1170
1171 /* Commit to the portion of the token stream most recently saved. */
1172
1173 static void
1174 cp_lexer_commit_tokens (cp_lexer* lexer)
1175 {
1176 /* Provide debugging output. */
1177 if (cp_lexer_debugging_p (lexer))
1178 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1179
1180 lexer->saved_tokens.pop ();
1181 }
1182
1183 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1184 to the token stream. Stop saving tokens. */
1185
1186 static void
1187 cp_lexer_rollback_tokens (cp_lexer* lexer)
1188 {
1189 /* Provide debugging output. */
1190 if (cp_lexer_debugging_p (lexer))
1191 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1192
1193 lexer->next_token = lexer->saved_tokens.pop ();
1194 }
1195
1196 /* RAII wrapper around the above functions, with sanity checking. Creating
1197 a variable saves tokens, which are committed when the variable is
1198 destroyed unless they are explicitly rolled back by calling the rollback
1199 member function. */
1200
1201 struct saved_token_sentinel
1202 {
1203 cp_lexer *lexer;
1204 unsigned len;
1205 bool commit;
1206 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1207 {
1208 len = lexer->saved_tokens.length ();
1209 cp_lexer_save_tokens (lexer);
1210 }
1211 void rollback ()
1212 {
1213 cp_lexer_rollback_tokens (lexer);
1214 commit = false;
1215 }
1216 ~saved_token_sentinel()
1217 {
1218 if (commit)
1219 cp_lexer_commit_tokens (lexer);
1220 gcc_assert (lexer->saved_tokens.length () == len);
1221 }
1222 };
1223
1224 /* Print a representation of the TOKEN on the STREAM. */
1225
1226 static void
1227 cp_lexer_print_token (FILE * stream, cp_token *token)
1228 {
1229 /* We don't use cpp_type2name here because the parser defines
1230 a few tokens of its own. */
1231 static const char *const token_names[] = {
1232 /* cpplib-defined token types */
1233 #define OP(e, s) #e,
1234 #define TK(e, s) #e,
1235 TTYPE_TABLE
1236 #undef OP
1237 #undef TK
1238 /* C++ parser token types - see "Manifest constants", above. */
1239 "KEYWORD",
1240 "TEMPLATE_ID",
1241 "NESTED_NAME_SPECIFIER",
1242 };
1243
1244 /* For some tokens, print the associated data. */
1245 switch (token->type)
1246 {
1247 case CPP_KEYWORD:
1248 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1249 For example, `struct' is mapped to an INTEGER_CST. */
1250 if (!identifier_p (token->u.value))
1251 break;
1252 /* fall through */
1253 case CPP_NAME:
1254 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1255 break;
1256
1257 case CPP_STRING:
1258 case CPP_STRING16:
1259 case CPP_STRING32:
1260 case CPP_WSTRING:
1261 case CPP_UTF8STRING:
1262 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1263 break;
1264
1265 case CPP_NUMBER:
1266 print_generic_expr (stream, token->u.value);
1267 break;
1268
1269 default:
1270 /* If we have a name for the token, print it out. Otherwise, we
1271 simply give the numeric code. */
1272 if (token->type < ARRAY_SIZE(token_names))
1273 fputs (token_names[token->type], stream);
1274 else
1275 fprintf (stream, "[%d]", token->type);
1276 break;
1277 }
1278 }
1279
1280 DEBUG_FUNCTION void
1281 debug (cp_token &ref)
1282 {
1283 cp_lexer_print_token (stderr, &ref);
1284 fprintf (stderr, "\n");
1285 }
1286
1287 DEBUG_FUNCTION void
1288 debug (cp_token *ptr)
1289 {
1290 if (ptr)
1291 debug (*ptr);
1292 else
1293 fprintf (stderr, "<nil>\n");
1294 }
1295
1296
1297 /* Start emitting debugging information. */
1298
1299 static void
1300 cp_lexer_start_debugging (cp_lexer* lexer)
1301 {
1302 if (!LEXER_DEBUGGING_ENABLED_P)
1303 fatal_error (input_location,
1304 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1305
1306 lexer->debugging_p = true;
1307 cp_lexer_debug_stream = stderr;
1308 }
1309
1310 /* Stop emitting debugging information. */
1311
1312 static void
1313 cp_lexer_stop_debugging (cp_lexer* lexer)
1314 {
1315 if (!LEXER_DEBUGGING_ENABLED_P)
1316 fatal_error (input_location,
1317 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1318
1319 lexer->debugging_p = false;
1320 cp_lexer_debug_stream = NULL;
1321 }
1322
1323 /* Create a new cp_token_cache, representing a range of tokens. */
1324
1325 static cp_token_cache *
1326 cp_token_cache_new (cp_token *first, cp_token *last)
1327 {
1328 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1329 cache->first = first;
1330 cache->last = last;
1331 return cache;
1332 }
1333
1334 /* Diagnose if #pragma omp declare simd isn't followed immediately
1335 by function declaration or definition. */
1336
1337 static inline void
1338 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1339 {
1340 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1341 {
1342 error ("%<#pragma omp declare simd%> not immediately followed by "
1343 "function declaration or definition");
1344 parser->omp_declare_simd = NULL;
1345 }
1346 }
1347
1348 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1349 and put that into "omp declare simd" attribute. */
1350
1351 static inline void
1352 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1353 {
1354 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1355 {
1356 if (fndecl == error_mark_node)
1357 {
1358 parser->omp_declare_simd = NULL;
1359 return;
1360 }
1361 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1362 {
1363 cp_ensure_no_omp_declare_simd (parser);
1364 return;
1365 }
1366 }
1367 }
1368
1369 /* Diagnose if #pragma acc routine isn't followed immediately by function
1370 declaration or definition. */
1371
1372 static inline void
1373 cp_ensure_no_oacc_routine (cp_parser *parser)
1374 {
1375 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1376 {
1377 error_at (parser->oacc_routine->loc,
1378 "%<#pragma acc routine%> not immediately followed by "
1379 "function declaration or definition");
1380 parser->oacc_routine = NULL;
1381 }
1382 }
1383 \f
1384 /* Decl-specifiers. */
1385
1386 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1387
1388 static void
1389 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1390 {
1391 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1392 }
1393
1394 /* Declarators. */
1395
1396 /* Nothing other than the parser should be creating declarators;
1397 declarators are a semi-syntactic representation of C++ entities.
1398 Other parts of the front end that need to create entities (like
1399 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1400
1401 static cp_declarator *make_call_declarator
1402 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1403 static cp_declarator *make_array_declarator
1404 (cp_declarator *, tree);
1405 static cp_declarator *make_pointer_declarator
1406 (cp_cv_quals, cp_declarator *, tree);
1407 static cp_declarator *make_reference_declarator
1408 (cp_cv_quals, cp_declarator *, bool, tree);
1409 static cp_declarator *make_ptrmem_declarator
1410 (cp_cv_quals, tree, cp_declarator *, tree);
1411
1412 /* An erroneous declarator. */
1413 static cp_declarator *cp_error_declarator;
1414
1415 /* The obstack on which declarators and related data structures are
1416 allocated. */
1417 static struct obstack declarator_obstack;
1418
1419 /* Alloc BYTES from the declarator memory pool. */
1420
1421 static inline void *
1422 alloc_declarator (size_t bytes)
1423 {
1424 return obstack_alloc (&declarator_obstack, bytes);
1425 }
1426
1427 /* Allocate a declarator of the indicated KIND. Clear fields that are
1428 common to all declarators. */
1429
1430 static cp_declarator *
1431 make_declarator (cp_declarator_kind kind)
1432 {
1433 cp_declarator *declarator;
1434
1435 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1436 declarator->kind = kind;
1437 declarator->parenthesized = UNKNOWN_LOCATION;
1438 declarator->attributes = NULL_TREE;
1439 declarator->std_attributes = NULL_TREE;
1440 declarator->declarator = NULL;
1441 declarator->parameter_pack_p = false;
1442 declarator->id_loc = UNKNOWN_LOCATION;
1443
1444 return declarator;
1445 }
1446
1447 /* Make a declarator for a generalized identifier. If
1448 QUALIFYING_SCOPE is non-NULL, the identifier is
1449 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1450 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1451 is, if any. */
1452
1453 static cp_declarator *
1454 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1455 special_function_kind sfk)
1456 {
1457 cp_declarator *declarator;
1458
1459 /* It is valid to write:
1460
1461 class C { void f(); };
1462 typedef C D;
1463 void D::f();
1464
1465 The standard is not clear about whether `typedef const C D' is
1466 legal; as of 2002-09-15 the committee is considering that
1467 question. EDG 3.0 allows that syntax. Therefore, we do as
1468 well. */
1469 if (qualifying_scope && TYPE_P (qualifying_scope))
1470 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1471
1472 gcc_assert (identifier_p (unqualified_name)
1473 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1474 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1475
1476 declarator = make_declarator (cdk_id);
1477 declarator->u.id.qualifying_scope = qualifying_scope;
1478 declarator->u.id.unqualified_name = unqualified_name;
1479 declarator->u.id.sfk = sfk;
1480
1481 return declarator;
1482 }
1483
1484 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1485 of modifiers such as const or volatile to apply to the pointer
1486 type, represented as identifiers. ATTRIBUTES represent the attributes that
1487 appertain to the pointer or reference. */
1488
1489 cp_declarator *
1490 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1491 tree attributes)
1492 {
1493 cp_declarator *declarator;
1494
1495 declarator = make_declarator (cdk_pointer);
1496 declarator->declarator = target;
1497 declarator->u.pointer.qualifiers = cv_qualifiers;
1498 declarator->u.pointer.class_type = NULL_TREE;
1499 if (target)
1500 {
1501 declarator->id_loc = target->id_loc;
1502 declarator->parameter_pack_p = target->parameter_pack_p;
1503 target->parameter_pack_p = false;
1504 }
1505 else
1506 declarator->parameter_pack_p = false;
1507
1508 declarator->std_attributes = attributes;
1509
1510 return declarator;
1511 }
1512
1513 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1514 represent the attributes that appertain to the pointer or
1515 reference. */
1516
1517 cp_declarator *
1518 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1519 bool rvalue_ref, tree attributes)
1520 {
1521 cp_declarator *declarator;
1522
1523 declarator = make_declarator (cdk_reference);
1524 declarator->declarator = target;
1525 declarator->u.reference.qualifiers = cv_qualifiers;
1526 declarator->u.reference.rvalue_ref = rvalue_ref;
1527 if (target)
1528 {
1529 declarator->id_loc = target->id_loc;
1530 declarator->parameter_pack_p = target->parameter_pack_p;
1531 target->parameter_pack_p = false;
1532 }
1533 else
1534 declarator->parameter_pack_p = false;
1535
1536 declarator->std_attributes = attributes;
1537
1538 return declarator;
1539 }
1540
1541 /* Like make_pointer_declarator -- but for a pointer to a non-static
1542 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1543 appertain to the pointer or reference. */
1544
1545 cp_declarator *
1546 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1547 cp_declarator *pointee,
1548 tree attributes)
1549 {
1550 cp_declarator *declarator;
1551
1552 declarator = make_declarator (cdk_ptrmem);
1553 declarator->declarator = pointee;
1554 declarator->u.pointer.qualifiers = cv_qualifiers;
1555 declarator->u.pointer.class_type = class_type;
1556
1557 if (pointee)
1558 {
1559 declarator->parameter_pack_p = pointee->parameter_pack_p;
1560 pointee->parameter_pack_p = false;
1561 }
1562 else
1563 declarator->parameter_pack_p = false;
1564
1565 declarator->std_attributes = attributes;
1566
1567 return declarator;
1568 }
1569
1570 /* Make a declarator for the function given by TARGET, with the
1571 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1572 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1573 indicates what exceptions can be thrown. */
1574
1575 cp_declarator *
1576 make_call_declarator (cp_declarator *target,
1577 tree parms,
1578 cp_cv_quals cv_qualifiers,
1579 cp_virt_specifiers virt_specifiers,
1580 cp_ref_qualifier ref_qualifier,
1581 tree tx_qualifier,
1582 tree exception_specification,
1583 tree late_return_type,
1584 tree requires_clause)
1585 {
1586 cp_declarator *declarator;
1587
1588 declarator = make_declarator (cdk_function);
1589 declarator->declarator = target;
1590 declarator->u.function.parameters = parms;
1591 declarator->u.function.qualifiers = cv_qualifiers;
1592 declarator->u.function.virt_specifiers = virt_specifiers;
1593 declarator->u.function.ref_qualifier = ref_qualifier;
1594 declarator->u.function.tx_qualifier = tx_qualifier;
1595 declarator->u.function.exception_specification = exception_specification;
1596 declarator->u.function.late_return_type = late_return_type;
1597 declarator->u.function.requires_clause = requires_clause;
1598 if (target)
1599 {
1600 declarator->id_loc = target->id_loc;
1601 declarator->parameter_pack_p = target->parameter_pack_p;
1602 target->parameter_pack_p = false;
1603 }
1604 else
1605 declarator->parameter_pack_p = false;
1606
1607 return declarator;
1608 }
1609
1610 /* Make a declarator for an array of BOUNDS elements, each of which is
1611 defined by ELEMENT. */
1612
1613 cp_declarator *
1614 make_array_declarator (cp_declarator *element, tree bounds)
1615 {
1616 cp_declarator *declarator;
1617
1618 declarator = make_declarator (cdk_array);
1619 declarator->declarator = element;
1620 declarator->u.array.bounds = bounds;
1621 if (element)
1622 {
1623 declarator->id_loc = element->id_loc;
1624 declarator->parameter_pack_p = element->parameter_pack_p;
1625 element->parameter_pack_p = false;
1626 }
1627 else
1628 declarator->parameter_pack_p = false;
1629
1630 return declarator;
1631 }
1632
1633 /* Determine whether the declarator we've seen so far can be a
1634 parameter pack, when followed by an ellipsis. */
1635 static bool
1636 declarator_can_be_parameter_pack (cp_declarator *declarator)
1637 {
1638 if (declarator && declarator->parameter_pack_p)
1639 /* We already saw an ellipsis. */
1640 return false;
1641
1642 /* Search for a declarator name, or any other declarator that goes
1643 after the point where the ellipsis could appear in a parameter
1644 pack. If we find any of these, then this declarator can not be
1645 made into a parameter pack. */
1646 bool found = false;
1647 while (declarator && !found)
1648 {
1649 switch ((int)declarator->kind)
1650 {
1651 case cdk_id:
1652 case cdk_array:
1653 case cdk_decomp:
1654 found = true;
1655 break;
1656
1657 case cdk_error:
1658 return true;
1659
1660 default:
1661 declarator = declarator->declarator;
1662 break;
1663 }
1664 }
1665
1666 return !found;
1667 }
1668
1669 cp_parameter_declarator *no_parameters;
1670
1671 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1672 DECLARATOR and DEFAULT_ARGUMENT. */
1673
1674 cp_parameter_declarator *
1675 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1676 cp_declarator *declarator,
1677 tree default_argument,
1678 location_t loc,
1679 bool template_parameter_pack_p = false)
1680 {
1681 cp_parameter_declarator *parameter;
1682
1683 parameter = ((cp_parameter_declarator *)
1684 alloc_declarator (sizeof (cp_parameter_declarator)));
1685 parameter->next = NULL;
1686 if (decl_specifiers)
1687 parameter->decl_specifiers = *decl_specifiers;
1688 else
1689 clear_decl_specs (&parameter->decl_specifiers);
1690 parameter->declarator = declarator;
1691 parameter->default_argument = default_argument;
1692 parameter->template_parameter_pack_p = template_parameter_pack_p;
1693 parameter->loc = loc;
1694
1695 return parameter;
1696 }
1697
1698 /* Returns true iff DECLARATOR is a declaration for a function. */
1699
1700 static bool
1701 function_declarator_p (const cp_declarator *declarator)
1702 {
1703 while (declarator)
1704 {
1705 if (declarator->kind == cdk_function
1706 && declarator->declarator->kind == cdk_id)
1707 return true;
1708 if (declarator->kind == cdk_id
1709 || declarator->kind == cdk_decomp
1710 || declarator->kind == cdk_error)
1711 return false;
1712 declarator = declarator->declarator;
1713 }
1714 return false;
1715 }
1716
1717 /* The parser. */
1718
1719 /* Overview
1720 --------
1721
1722 A cp_parser parses the token stream as specified by the C++
1723 grammar. Its job is purely parsing, not semantic analysis. For
1724 example, the parser breaks the token stream into declarators,
1725 expressions, statements, and other similar syntactic constructs.
1726 It does not check that the types of the expressions on either side
1727 of an assignment-statement are compatible, or that a function is
1728 not declared with a parameter of type `void'.
1729
1730 The parser invokes routines elsewhere in the compiler to perform
1731 semantic analysis and to build up the abstract syntax tree for the
1732 code processed.
1733
1734 The parser (and the template instantiation code, which is, in a
1735 way, a close relative of parsing) are the only parts of the
1736 compiler that should be calling push_scope and pop_scope, or
1737 related functions. The parser (and template instantiation code)
1738 keeps track of what scope is presently active; everything else
1739 should simply honor that. (The code that generates static
1740 initializers may also need to set the scope, in order to check
1741 access control correctly when emitting the initializers.)
1742
1743 Methodology
1744 -----------
1745
1746 The parser is of the standard recursive-descent variety. Upcoming
1747 tokens in the token stream are examined in order to determine which
1748 production to use when parsing a non-terminal. Some C++ constructs
1749 require arbitrary look ahead to disambiguate. For example, it is
1750 impossible, in the general case, to tell whether a statement is an
1751 expression or declaration without scanning the entire statement.
1752 Therefore, the parser is capable of "parsing tentatively." When the
1753 parser is not sure what construct comes next, it enters this mode.
1754 Then, while we attempt to parse the construct, the parser queues up
1755 error messages, rather than issuing them immediately, and saves the
1756 tokens it consumes. If the construct is parsed successfully, the
1757 parser "commits", i.e., it issues any queued error messages and
1758 the tokens that were being preserved are permanently discarded.
1759 If, however, the construct is not parsed successfully, the parser
1760 rolls back its state completely so that it can resume parsing using
1761 a different alternative.
1762
1763 Future Improvements
1764 -------------------
1765
1766 The performance of the parser could probably be improved substantially.
1767 We could often eliminate the need to parse tentatively by looking ahead
1768 a little bit. In some places, this approach might not entirely eliminate
1769 the need to parse tentatively, but it might still speed up the average
1770 case. */
1771
1772 /* Flags that are passed to some parsing functions. These values can
1773 be bitwise-ored together. */
1774
1775 enum
1776 {
1777 /* No flags. */
1778 CP_PARSER_FLAGS_NONE = 0x0,
1779 /* The construct is optional. If it is not present, then no error
1780 should be issued. */
1781 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1782 /* When parsing a type-specifier, treat user-defined type-names
1783 as non-type identifiers. */
1784 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1785 /* When parsing a type-specifier, do not try to parse a class-specifier
1786 or enum-specifier. */
1787 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1788 /* When parsing a decl-specifier-seq, only allow type-specifier or
1789 constexpr. */
1790 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1791 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1792 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1793 };
1794
1795 /* This type is used for parameters and variables which hold
1796 combinations of the above flags. */
1797 typedef int cp_parser_flags;
1798
1799 /* The different kinds of declarators we want to parse. */
1800
1801 enum cp_parser_declarator_kind
1802 {
1803 /* We want an abstract declarator. */
1804 CP_PARSER_DECLARATOR_ABSTRACT,
1805 /* We want a named declarator. */
1806 CP_PARSER_DECLARATOR_NAMED,
1807 /* We don't mind, but the name must be an unqualified-id. */
1808 CP_PARSER_DECLARATOR_EITHER
1809 };
1810
1811 /* The precedence values used to parse binary expressions. The minimum value
1812 of PREC must be 1, because zero is reserved to quickly discriminate
1813 binary operators from other tokens. */
1814
1815 enum cp_parser_prec
1816 {
1817 PREC_NOT_OPERATOR,
1818 PREC_LOGICAL_OR_EXPRESSION,
1819 PREC_LOGICAL_AND_EXPRESSION,
1820 PREC_INCLUSIVE_OR_EXPRESSION,
1821 PREC_EXCLUSIVE_OR_EXPRESSION,
1822 PREC_AND_EXPRESSION,
1823 PREC_EQUALITY_EXPRESSION,
1824 PREC_RELATIONAL_EXPRESSION,
1825 PREC_SHIFT_EXPRESSION,
1826 PREC_ADDITIVE_EXPRESSION,
1827 PREC_MULTIPLICATIVE_EXPRESSION,
1828 PREC_PM_EXPRESSION,
1829 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1830 };
1831
1832 /* A mapping from a token type to a corresponding tree node type, with a
1833 precedence value. */
1834
1835 struct cp_parser_binary_operations_map_node
1836 {
1837 /* The token type. */
1838 enum cpp_ttype token_type;
1839 /* The corresponding tree code. */
1840 enum tree_code tree_type;
1841 /* The precedence of this operator. */
1842 enum cp_parser_prec prec;
1843 };
1844
1845 struct cp_parser_expression_stack_entry
1846 {
1847 /* Left hand side of the binary operation we are currently
1848 parsing. */
1849 cp_expr lhs;
1850 /* Original tree code for left hand side, if it was a binary
1851 expression itself (used for -Wparentheses). */
1852 enum tree_code lhs_type;
1853 /* Tree code for the binary operation we are parsing. */
1854 enum tree_code tree_type;
1855 /* Precedence of the binary operation we are parsing. */
1856 enum cp_parser_prec prec;
1857 /* Location of the binary operation we are parsing. */
1858 location_t loc;
1859 };
1860
1861 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1862 entries because precedence levels on the stack are monotonically
1863 increasing. */
1864 typedef struct cp_parser_expression_stack_entry
1865 cp_parser_expression_stack[NUM_PREC_VALUES];
1866
1867 /* Prototypes. */
1868
1869 /* Constructors and destructors. */
1870
1871 static cp_parser_context *cp_parser_context_new
1872 (cp_parser_context *);
1873
1874 /* Class variables. */
1875
1876 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1877
1878 /* The operator-precedence table used by cp_parser_binary_expression.
1879 Transformed into an associative array (binops_by_token) by
1880 cp_parser_new. */
1881
1882 static const cp_parser_binary_operations_map_node binops[] = {
1883 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1884 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1885
1886 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1887 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1888 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889
1890 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1891 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1892
1893 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1894 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1895
1896 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1897 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1898 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1900
1901 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1902 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1903
1904 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1905
1906 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1907
1908 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1909
1910 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1911
1912 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1913 };
1914
1915 /* The same as binops, but initialized by cp_parser_new so that
1916 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1917 for speed. */
1918 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1919
1920 /* Constructors and destructors. */
1921
1922 /* Construct a new context. The context below this one on the stack
1923 is given by NEXT. */
1924
1925 static cp_parser_context *
1926 cp_parser_context_new (cp_parser_context* next)
1927 {
1928 cp_parser_context *context;
1929
1930 /* Allocate the storage. */
1931 if (cp_parser_context_free_list != NULL)
1932 {
1933 /* Pull the first entry from the free list. */
1934 context = cp_parser_context_free_list;
1935 cp_parser_context_free_list = context->next;
1936 memset (context, 0, sizeof (*context));
1937 }
1938 else
1939 context = ggc_cleared_alloc<cp_parser_context> ();
1940
1941 /* No errors have occurred yet in this context. */
1942 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1943 /* If this is not the bottommost context, copy information that we
1944 need from the previous context. */
1945 if (next)
1946 {
1947 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1948 expression, then we are parsing one in this context, too. */
1949 context->object_type = next->object_type;
1950 /* Thread the stack. */
1951 context->next = next;
1952 }
1953
1954 return context;
1955 }
1956
1957 /* Managing the unparsed function queues. */
1958
1959 #define unparsed_funs_with_default_args \
1960 parser->unparsed_queues->last ().funs_with_default_args
1961 #define unparsed_funs_with_definitions \
1962 parser->unparsed_queues->last ().funs_with_definitions
1963 #define unparsed_nsdmis \
1964 parser->unparsed_queues->last ().nsdmis
1965 #define unparsed_classes \
1966 parser->unparsed_queues->last ().classes
1967
1968 static void
1969 push_unparsed_function_queues (cp_parser *parser)
1970 {
1971 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1972 vec_safe_push (parser->unparsed_queues, e);
1973 }
1974
1975 static void
1976 pop_unparsed_function_queues (cp_parser *parser)
1977 {
1978 release_tree_vector (unparsed_funs_with_definitions);
1979 parser->unparsed_queues->pop ();
1980 }
1981
1982 /* Prototypes. */
1983
1984 /* Constructors and destructors. */
1985
1986 static cp_parser *cp_parser_new
1987 (void);
1988
1989 /* Routines to parse various constructs.
1990
1991 Those that return `tree' will return the error_mark_node (rather
1992 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1993 Sometimes, they will return an ordinary node if error-recovery was
1994 attempted, even though a parse error occurred. So, to check
1995 whether or not a parse error occurred, you should always use
1996 cp_parser_error_occurred. If the construct is optional (indicated
1997 either by an `_opt' in the name of the function that does the
1998 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1999 the construct is not present. */
2000
2001 /* Lexical conventions [gram.lex] */
2002
2003 static cp_expr cp_parser_identifier
2004 (cp_parser *);
2005 static cp_expr cp_parser_string_literal
2006 (cp_parser *, bool, bool, bool);
2007 static cp_expr cp_parser_userdef_char_literal
2008 (cp_parser *);
2009 static tree cp_parser_userdef_string_literal
2010 (tree);
2011 static cp_expr cp_parser_userdef_numeric_literal
2012 (cp_parser *);
2013
2014 /* Basic concepts [gram.basic] */
2015
2016 static void cp_parser_translation_unit (cp_parser *);
2017
2018 /* Expressions [gram.expr] */
2019
2020 static cp_expr cp_parser_primary_expression
2021 (cp_parser *, bool, bool, bool, cp_id_kind *);
2022 static cp_expr cp_parser_id_expression
2023 (cp_parser *, bool, bool, bool *, bool, bool);
2024 static cp_expr cp_parser_unqualified_id
2025 (cp_parser *, bool, bool, bool, bool);
2026 static tree cp_parser_nested_name_specifier_opt
2027 (cp_parser *, bool, bool, bool, bool, bool = false);
2028 static tree cp_parser_nested_name_specifier
2029 (cp_parser *, bool, bool, bool, bool);
2030 static tree cp_parser_qualifying_entity
2031 (cp_parser *, bool, bool, bool, bool, bool);
2032 static cp_expr cp_parser_postfix_expression
2033 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2034 static tree cp_parser_postfix_open_square_expression
2035 (cp_parser *, tree, bool, bool);
2036 static tree cp_parser_postfix_dot_deref_expression
2037 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2038 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2039 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2040 bool = false);
2041 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2042 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2043 static void cp_parser_pseudo_destructor_name
2044 (cp_parser *, tree, tree *, tree *);
2045 static cp_expr cp_parser_unary_expression
2046 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2047 static enum tree_code cp_parser_unary_operator
2048 (cp_token *);
2049 static tree cp_parser_new_expression
2050 (cp_parser *);
2051 static vec<tree, va_gc> *cp_parser_new_placement
2052 (cp_parser *);
2053 static tree cp_parser_new_type_id
2054 (cp_parser *, tree *);
2055 static cp_declarator *cp_parser_new_declarator_opt
2056 (cp_parser *);
2057 static cp_declarator *cp_parser_direct_new_declarator
2058 (cp_parser *);
2059 static vec<tree, va_gc> *cp_parser_new_initializer
2060 (cp_parser *);
2061 static tree cp_parser_delete_expression
2062 (cp_parser *);
2063 static cp_expr cp_parser_cast_expression
2064 (cp_parser *, bool, bool, bool, cp_id_kind *);
2065 static cp_expr cp_parser_binary_expression
2066 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2067 static tree cp_parser_question_colon_clause
2068 (cp_parser *, cp_expr);
2069 static cp_expr cp_parser_assignment_expression
2070 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2071 static enum tree_code cp_parser_assignment_operator_opt
2072 (cp_parser *);
2073 static cp_expr cp_parser_expression
2074 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2075 static cp_expr cp_parser_constant_expression
2076 (cp_parser *, bool = false, bool * = NULL, bool = false);
2077 static cp_expr cp_parser_builtin_offsetof
2078 (cp_parser *);
2079 static cp_expr cp_parser_lambda_expression
2080 (cp_parser *);
2081 static void cp_parser_lambda_introducer
2082 (cp_parser *, tree);
2083 static bool cp_parser_lambda_declarator_opt
2084 (cp_parser *, tree);
2085 static void cp_parser_lambda_body
2086 (cp_parser *, tree);
2087
2088 /* Statements [gram.stmt.stmt] */
2089
2090 static void cp_parser_statement
2091 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2092 static void cp_parser_label_for_labeled_statement
2093 (cp_parser *, tree);
2094 static tree cp_parser_expression_statement
2095 (cp_parser *, tree);
2096 static tree cp_parser_compound_statement
2097 (cp_parser *, tree, int, bool);
2098 static void cp_parser_statement_seq_opt
2099 (cp_parser *, tree);
2100 static tree cp_parser_selection_statement
2101 (cp_parser *, bool *, vec<tree> *);
2102 static tree cp_parser_condition
2103 (cp_parser *);
2104 static tree cp_parser_iteration_statement
2105 (cp_parser *, bool *, bool, unsigned short);
2106 static bool cp_parser_init_statement
2107 (cp_parser *, tree *decl);
2108 static tree cp_parser_for
2109 (cp_parser *, bool, unsigned short);
2110 static tree cp_parser_c_for
2111 (cp_parser *, tree, tree, bool, unsigned short);
2112 static tree cp_parser_range_for
2113 (cp_parser *, tree, tree, tree, bool, unsigned short);
2114 static void do_range_for_auto_deduction
2115 (tree, tree);
2116 static tree cp_parser_perform_range_for_lookup
2117 (tree, tree *, tree *);
2118 static tree cp_parser_range_for_member_function
2119 (tree, tree);
2120 static tree cp_parser_jump_statement
2121 (cp_parser *);
2122 static void cp_parser_declaration_statement
2123 (cp_parser *);
2124
2125 static tree cp_parser_implicitly_scoped_statement
2126 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2127 static void cp_parser_already_scoped_statement
2128 (cp_parser *, bool *, const token_indent_info &);
2129
2130 /* Declarations [gram.dcl.dcl] */
2131
2132 static void cp_parser_declaration_seq_opt
2133 (cp_parser *);
2134 static void cp_parser_declaration
2135 (cp_parser *);
2136 static void cp_parser_toplevel_declaration
2137 (cp_parser *);
2138 static void cp_parser_block_declaration
2139 (cp_parser *, bool);
2140 static void cp_parser_simple_declaration
2141 (cp_parser *, bool, tree *);
2142 static void cp_parser_decl_specifier_seq
2143 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2144 static tree cp_parser_storage_class_specifier_opt
2145 (cp_parser *);
2146 static tree cp_parser_function_specifier_opt
2147 (cp_parser *, cp_decl_specifier_seq *);
2148 static tree cp_parser_type_specifier
2149 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2150 int *, bool *);
2151 static tree cp_parser_simple_type_specifier
2152 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2153 static tree cp_parser_type_name
2154 (cp_parser *, bool);
2155 static tree cp_parser_type_name
2156 (cp_parser *);
2157 static tree cp_parser_nonclass_name
2158 (cp_parser* parser);
2159 static tree cp_parser_elaborated_type_specifier
2160 (cp_parser *, bool, bool);
2161 static tree cp_parser_enum_specifier
2162 (cp_parser *);
2163 static void cp_parser_enumerator_list
2164 (cp_parser *, tree);
2165 static void cp_parser_enumerator_definition
2166 (cp_parser *, tree);
2167 static tree cp_parser_namespace_name
2168 (cp_parser *);
2169 static void cp_parser_namespace_definition
2170 (cp_parser *);
2171 static void cp_parser_namespace_body
2172 (cp_parser *);
2173 static tree cp_parser_qualified_namespace_specifier
2174 (cp_parser *);
2175 static void cp_parser_namespace_alias_definition
2176 (cp_parser *);
2177 static bool cp_parser_using_declaration
2178 (cp_parser *, bool);
2179 static void cp_parser_using_directive
2180 (cp_parser *);
2181 static tree cp_parser_alias_declaration
2182 (cp_parser *);
2183 static void cp_parser_asm_definition
2184 (cp_parser *);
2185 static void cp_parser_linkage_specification
2186 (cp_parser *);
2187 static void cp_parser_static_assert
2188 (cp_parser *, bool);
2189 static tree cp_parser_decltype
2190 (cp_parser *);
2191 static tree cp_parser_decomposition_declaration
2192 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2193
2194 /* Declarators [gram.dcl.decl] */
2195
2196 static tree cp_parser_init_declarator
2197 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2198 bool, bool, int, bool *, tree *, location_t *, tree *);
2199 static cp_declarator *cp_parser_declarator
2200 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2201 static cp_declarator *cp_parser_direct_declarator
2202 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2203 static enum tree_code cp_parser_ptr_operator
2204 (cp_parser *, tree *, cp_cv_quals *, tree *);
2205 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2206 (cp_parser *);
2207 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2208 (cp_parser *);
2209 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2210 (cp_parser *);
2211 static tree cp_parser_tx_qualifier_opt
2212 (cp_parser *);
2213 static tree cp_parser_late_return_type_opt
2214 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2215 static tree cp_parser_declarator_id
2216 (cp_parser *, bool);
2217 static tree cp_parser_type_id
2218 (cp_parser *, location_t * = NULL);
2219 static tree cp_parser_template_type_arg
2220 (cp_parser *);
2221 static tree cp_parser_trailing_type_id (cp_parser *);
2222 static tree cp_parser_type_id_1
2223 (cp_parser *, bool, bool, location_t *);
2224 static void cp_parser_type_specifier_seq
2225 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2226 static tree cp_parser_parameter_declaration_clause
2227 (cp_parser *);
2228 static tree cp_parser_parameter_declaration_list
2229 (cp_parser *);
2230 static cp_parameter_declarator *cp_parser_parameter_declaration
2231 (cp_parser *, bool, bool *);
2232 static tree cp_parser_default_argument
2233 (cp_parser *, bool);
2234 static void cp_parser_function_body
2235 (cp_parser *, bool);
2236 static tree cp_parser_initializer
2237 (cp_parser *, bool *, bool *, bool = false);
2238 static cp_expr cp_parser_initializer_clause
2239 (cp_parser *, bool *);
2240 static cp_expr cp_parser_braced_list
2241 (cp_parser*, bool*);
2242 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2243 (cp_parser *, bool *);
2244
2245 static void cp_parser_ctor_initializer_opt_and_function_body
2246 (cp_parser *, bool);
2247
2248 static tree cp_parser_late_parsing_omp_declare_simd
2249 (cp_parser *, tree);
2250
2251 static tree cp_parser_late_parsing_oacc_routine
2252 (cp_parser *, tree);
2253
2254 static tree synthesize_implicit_template_parm
2255 (cp_parser *, tree);
2256 static tree finish_fully_implicit_template
2257 (cp_parser *, tree);
2258 static void abort_fully_implicit_template
2259 (cp_parser *);
2260
2261 /* Classes [gram.class] */
2262
2263 static tree cp_parser_class_name
2264 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2265 static tree cp_parser_class_specifier
2266 (cp_parser *);
2267 static tree cp_parser_class_head
2268 (cp_parser *, bool *);
2269 static enum tag_types cp_parser_class_key
2270 (cp_parser *);
2271 static void cp_parser_type_parameter_key
2272 (cp_parser* parser);
2273 static void cp_parser_member_specification_opt
2274 (cp_parser *);
2275 static void cp_parser_member_declaration
2276 (cp_parser *);
2277 static tree cp_parser_pure_specifier
2278 (cp_parser *);
2279 static tree cp_parser_constant_initializer
2280 (cp_parser *);
2281
2282 /* Derived classes [gram.class.derived] */
2283
2284 static tree cp_parser_base_clause
2285 (cp_parser *);
2286 static tree cp_parser_base_specifier
2287 (cp_parser *);
2288
2289 /* Special member functions [gram.special] */
2290
2291 static tree cp_parser_conversion_function_id
2292 (cp_parser *);
2293 static tree cp_parser_conversion_type_id
2294 (cp_parser *);
2295 static cp_declarator *cp_parser_conversion_declarator_opt
2296 (cp_parser *);
2297 static void cp_parser_ctor_initializer_opt
2298 (cp_parser *);
2299 static void cp_parser_mem_initializer_list
2300 (cp_parser *);
2301 static tree cp_parser_mem_initializer
2302 (cp_parser *);
2303 static tree cp_parser_mem_initializer_id
2304 (cp_parser *);
2305
2306 /* Overloading [gram.over] */
2307
2308 static cp_expr cp_parser_operator_function_id
2309 (cp_parser *);
2310 static cp_expr cp_parser_operator
2311 (cp_parser *);
2312
2313 /* Templates [gram.temp] */
2314
2315 static void cp_parser_template_declaration
2316 (cp_parser *, bool);
2317 static tree cp_parser_template_parameter_list
2318 (cp_parser *);
2319 static tree cp_parser_template_parameter
2320 (cp_parser *, bool *, bool *);
2321 static tree cp_parser_type_parameter
2322 (cp_parser *, bool *);
2323 static tree cp_parser_template_id
2324 (cp_parser *, bool, bool, enum tag_types, bool);
2325 static tree cp_parser_template_name
2326 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2327 static tree cp_parser_template_argument_list
2328 (cp_parser *);
2329 static tree cp_parser_template_argument
2330 (cp_parser *);
2331 static void cp_parser_explicit_instantiation
2332 (cp_parser *);
2333 static void cp_parser_explicit_specialization
2334 (cp_parser *);
2335
2336 /* Exception handling [gram.exception] */
2337
2338 static tree cp_parser_try_block
2339 (cp_parser *);
2340 static void cp_parser_function_try_block
2341 (cp_parser *);
2342 static void cp_parser_handler_seq
2343 (cp_parser *);
2344 static void cp_parser_handler
2345 (cp_parser *);
2346 static tree cp_parser_exception_declaration
2347 (cp_parser *);
2348 static tree cp_parser_throw_expression
2349 (cp_parser *);
2350 static tree cp_parser_exception_specification_opt
2351 (cp_parser *);
2352 static tree cp_parser_type_id_list
2353 (cp_parser *);
2354
2355 /* GNU Extensions */
2356
2357 static tree cp_parser_asm_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_asm_operand_list
2360 (cp_parser *);
2361 static tree cp_parser_asm_clobber_list
2362 (cp_parser *);
2363 static tree cp_parser_asm_label_list
2364 (cp_parser *);
2365 static bool cp_next_tokens_can_be_attribute_p
2366 (cp_parser *);
2367 static bool cp_next_tokens_can_be_gnu_attribute_p
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_std_attribute_p
2370 (cp_parser *);
2371 static bool cp_nth_tokens_can_be_std_attribute_p
2372 (cp_parser *, size_t);
2373 static bool cp_nth_tokens_can_be_gnu_attribute_p
2374 (cp_parser *, size_t);
2375 static bool cp_nth_tokens_can_be_attribute_p
2376 (cp_parser *, size_t);
2377 static tree cp_parser_attributes_opt
2378 (cp_parser *);
2379 static tree cp_parser_gnu_attributes_opt
2380 (cp_parser *);
2381 static tree cp_parser_gnu_attribute_list
2382 (cp_parser *);
2383 static tree cp_parser_std_attribute
2384 (cp_parser *, tree);
2385 static tree cp_parser_std_attribute_spec
2386 (cp_parser *);
2387 static tree cp_parser_std_attribute_spec_seq
2388 (cp_parser *);
2389 static size_t cp_parser_skip_attributes_opt
2390 (cp_parser *, size_t);
2391 static bool cp_parser_extension_opt
2392 (cp_parser *, int *);
2393 static void cp_parser_label_declaration
2394 (cp_parser *);
2395
2396 /* Concept Extensions */
2397
2398 static tree cp_parser_requires_clause
2399 (cp_parser *);
2400 static tree cp_parser_requires_clause_opt
2401 (cp_parser *);
2402 static tree cp_parser_requires_expression
2403 (cp_parser *);
2404 static tree cp_parser_requirement_parameter_list
2405 (cp_parser *);
2406 static tree cp_parser_requirement_body
2407 (cp_parser *);
2408 static tree cp_parser_requirement_list
2409 (cp_parser *);
2410 static tree cp_parser_requirement
2411 (cp_parser *);
2412 static tree cp_parser_simple_requirement
2413 (cp_parser *);
2414 static tree cp_parser_compound_requirement
2415 (cp_parser *);
2416 static tree cp_parser_type_requirement
2417 (cp_parser *);
2418 static tree cp_parser_nested_requirement
2419 (cp_parser *);
2420
2421 /* Transactional Memory Extensions */
2422
2423 static tree cp_parser_transaction
2424 (cp_parser *, cp_token *);
2425 static tree cp_parser_transaction_expression
2426 (cp_parser *, enum rid);
2427 static void cp_parser_function_transaction
2428 (cp_parser *, enum rid);
2429 static tree cp_parser_transaction_cancel
2430 (cp_parser *);
2431
2432 enum pragma_context {
2433 pragma_external,
2434 pragma_member,
2435 pragma_objc_icode,
2436 pragma_stmt,
2437 pragma_compound
2438 };
2439 static bool cp_parser_pragma
2440 (cp_parser *, enum pragma_context, bool *);
2441
2442 /* Objective-C++ Productions */
2443
2444 static tree cp_parser_objc_message_receiver
2445 (cp_parser *);
2446 static tree cp_parser_objc_message_args
2447 (cp_parser *);
2448 static tree cp_parser_objc_message_expression
2449 (cp_parser *);
2450 static cp_expr cp_parser_objc_encode_expression
2451 (cp_parser *);
2452 static tree cp_parser_objc_defs_expression
2453 (cp_parser *);
2454 static tree cp_parser_objc_protocol_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_selector_expression
2457 (cp_parser *);
2458 static cp_expr cp_parser_objc_expression
2459 (cp_parser *);
2460 static bool cp_parser_objc_selector_p
2461 (enum cpp_ttype);
2462 static tree cp_parser_objc_selector
2463 (cp_parser *);
2464 static tree cp_parser_objc_protocol_refs_opt
2465 (cp_parser *);
2466 static void cp_parser_objc_declaration
2467 (cp_parser *, tree);
2468 static tree cp_parser_objc_statement
2469 (cp_parser *);
2470 static bool cp_parser_objc_valid_prefix_attributes
2471 (cp_parser *, tree *);
2472 static void cp_parser_objc_at_property_declaration
2473 (cp_parser *) ;
2474 static void cp_parser_objc_at_synthesize_declaration
2475 (cp_parser *) ;
2476 static void cp_parser_objc_at_dynamic_declaration
2477 (cp_parser *) ;
2478 static tree cp_parser_objc_struct_declaration
2479 (cp_parser *) ;
2480
2481 /* Utility Routines */
2482
2483 static cp_expr cp_parser_lookup_name
2484 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2485 static tree cp_parser_lookup_name_simple
2486 (cp_parser *, tree, location_t);
2487 static tree cp_parser_maybe_treat_template_as_class
2488 (tree, bool);
2489 static bool cp_parser_check_declarator_template_parameters
2490 (cp_parser *, cp_declarator *, location_t);
2491 static bool cp_parser_check_template_parameters
2492 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2493 static cp_expr cp_parser_simple_cast_expression
2494 (cp_parser *);
2495 static tree cp_parser_global_scope_opt
2496 (cp_parser *, bool);
2497 static bool cp_parser_constructor_declarator_p
2498 (cp_parser *, bool);
2499 static tree cp_parser_function_definition_from_specifiers_and_declarator
2500 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2501 static tree cp_parser_function_definition_after_declarator
2502 (cp_parser *, bool);
2503 static bool cp_parser_template_declaration_after_export
2504 (cp_parser *, bool);
2505 static void cp_parser_perform_template_parameter_access_checks
2506 (vec<deferred_access_check, va_gc> *);
2507 static tree cp_parser_single_declaration
2508 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2509 static cp_expr cp_parser_functional_cast
2510 (cp_parser *, tree);
2511 static tree cp_parser_save_member_function_body
2512 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2513 static tree cp_parser_save_nsdmi
2514 (cp_parser *);
2515 static tree cp_parser_enclosed_template_argument_list
2516 (cp_parser *);
2517 static void cp_parser_save_default_args
2518 (cp_parser *, tree);
2519 static void cp_parser_late_parsing_for_member
2520 (cp_parser *, tree);
2521 static tree cp_parser_late_parse_one_default_arg
2522 (cp_parser *, tree, tree, tree);
2523 static void cp_parser_late_parsing_nsdmi
2524 (cp_parser *, tree);
2525 static void cp_parser_late_parsing_default_args
2526 (cp_parser *, tree);
2527 static tree cp_parser_sizeof_operand
2528 (cp_parser *, enum rid);
2529 static cp_expr cp_parser_trait_expr
2530 (cp_parser *, enum rid);
2531 static bool cp_parser_declares_only_class_p
2532 (cp_parser *);
2533 static void cp_parser_set_storage_class
2534 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2535 static void cp_parser_set_decl_spec_type
2536 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2537 static void set_and_check_decl_spec_loc
2538 (cp_decl_specifier_seq *decl_specs,
2539 cp_decl_spec ds, cp_token *);
2540 static bool cp_parser_friend_p
2541 (const cp_decl_specifier_seq *);
2542 static void cp_parser_required_error
2543 (cp_parser *, required_token, bool, location_t);
2544 static cp_token *cp_parser_require
2545 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2546 static cp_token *cp_parser_require_keyword
2547 (cp_parser *, enum rid, required_token);
2548 static bool cp_parser_token_starts_function_definition_p
2549 (cp_token *);
2550 static bool cp_parser_next_token_starts_class_definition_p
2551 (cp_parser *);
2552 static bool cp_parser_next_token_ends_template_argument_p
2553 (cp_parser *);
2554 static bool cp_parser_nth_token_starts_template_argument_list_p
2555 (cp_parser *, size_t);
2556 static enum tag_types cp_parser_token_is_class_key
2557 (cp_token *);
2558 static enum tag_types cp_parser_token_is_type_parameter_key
2559 (cp_token *);
2560 static void cp_parser_check_class_key
2561 (enum tag_types, tree type);
2562 static void cp_parser_check_access_in_redeclaration
2563 (tree type, location_t location);
2564 static bool cp_parser_optional_template_keyword
2565 (cp_parser *);
2566 static void cp_parser_pre_parsed_nested_name_specifier
2567 (cp_parser *);
2568 static bool cp_parser_cache_group
2569 (cp_parser *, enum cpp_ttype, unsigned);
2570 static tree cp_parser_cache_defarg
2571 (cp_parser *parser, bool nsdmi);
2572 static void cp_parser_parse_tentatively
2573 (cp_parser *);
2574 static void cp_parser_commit_to_tentative_parse
2575 (cp_parser *);
2576 static void cp_parser_commit_to_topmost_tentative_parse
2577 (cp_parser *);
2578 static void cp_parser_abort_tentative_parse
2579 (cp_parser *);
2580 static bool cp_parser_parse_definitely
2581 (cp_parser *);
2582 static inline bool cp_parser_parsing_tentatively
2583 (cp_parser *);
2584 static bool cp_parser_uncommitted_to_tentative_parse_p
2585 (cp_parser *);
2586 static void cp_parser_error
2587 (cp_parser *, const char *);
2588 static void cp_parser_name_lookup_error
2589 (cp_parser *, tree, tree, name_lookup_error, location_t);
2590 static bool cp_parser_simulate_error
2591 (cp_parser *);
2592 static bool cp_parser_check_type_definition
2593 (cp_parser *);
2594 static void cp_parser_check_for_definition_in_return_type
2595 (cp_declarator *, tree, location_t type_location);
2596 static void cp_parser_check_for_invalid_template_id
2597 (cp_parser *, tree, enum tag_types, location_t location);
2598 static bool cp_parser_non_integral_constant_expression
2599 (cp_parser *, non_integral_constant);
2600 static void cp_parser_diagnose_invalid_type_name
2601 (cp_parser *, tree, location_t);
2602 static bool cp_parser_parse_and_diagnose_invalid_type_name
2603 (cp_parser *);
2604 static int cp_parser_skip_to_closing_parenthesis
2605 (cp_parser *, bool, bool, bool);
2606 static void cp_parser_skip_to_end_of_statement
2607 (cp_parser *);
2608 static void cp_parser_consume_semicolon_at_end_of_statement
2609 (cp_parser *);
2610 static void cp_parser_skip_to_end_of_block_or_statement
2611 (cp_parser *);
2612 static bool cp_parser_skip_to_closing_brace
2613 (cp_parser *);
2614 static void cp_parser_skip_to_end_of_template_parameter_list
2615 (cp_parser *);
2616 static void cp_parser_skip_to_pragma_eol
2617 (cp_parser*, cp_token *);
2618 static bool cp_parser_error_occurred
2619 (cp_parser *);
2620 static bool cp_parser_allow_gnu_extensions_p
2621 (cp_parser *);
2622 static bool cp_parser_is_pure_string_literal
2623 (cp_token *);
2624 static bool cp_parser_is_string_literal
2625 (cp_token *);
2626 static bool cp_parser_is_keyword
2627 (cp_token *, enum rid);
2628 static tree cp_parser_make_typename_type
2629 (cp_parser *, tree, location_t location);
2630 static cp_declarator * cp_parser_make_indirect_declarator
2631 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2632 static bool cp_parser_compound_literal_p
2633 (cp_parser *);
2634 static bool cp_parser_array_designator_p
2635 (cp_parser *);
2636 static bool cp_parser_init_statement_p
2637 (cp_parser *);
2638 static bool cp_parser_skip_to_closing_square_bracket
2639 (cp_parser *);
2640
2641 /* Concept-related syntactic transformations */
2642
2643 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2644 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2645
2646 // -------------------------------------------------------------------------- //
2647 // Unevaluated Operand Guard
2648 //
2649 // Implementation of an RAII helper for unevaluated operand parsing.
2650 cp_unevaluated::cp_unevaluated ()
2651 {
2652 ++cp_unevaluated_operand;
2653 ++c_inhibit_evaluation_warnings;
2654 }
2655
2656 cp_unevaluated::~cp_unevaluated ()
2657 {
2658 --c_inhibit_evaluation_warnings;
2659 --cp_unevaluated_operand;
2660 }
2661
2662 // -------------------------------------------------------------------------- //
2663 // Tentative Parsing
2664
2665 /* Returns nonzero if we are parsing tentatively. */
2666
2667 static inline bool
2668 cp_parser_parsing_tentatively (cp_parser* parser)
2669 {
2670 return parser->context->next != NULL;
2671 }
2672
2673 /* Returns nonzero if TOKEN is a string literal. */
2674
2675 static bool
2676 cp_parser_is_pure_string_literal (cp_token* token)
2677 {
2678 return (token->type == CPP_STRING ||
2679 token->type == CPP_STRING16 ||
2680 token->type == CPP_STRING32 ||
2681 token->type == CPP_WSTRING ||
2682 token->type == CPP_UTF8STRING);
2683 }
2684
2685 /* Returns nonzero if TOKEN is a string literal
2686 of a user-defined string literal. */
2687
2688 static bool
2689 cp_parser_is_string_literal (cp_token* token)
2690 {
2691 return (cp_parser_is_pure_string_literal (token) ||
2692 token->type == CPP_STRING_USERDEF ||
2693 token->type == CPP_STRING16_USERDEF ||
2694 token->type == CPP_STRING32_USERDEF ||
2695 token->type == CPP_WSTRING_USERDEF ||
2696 token->type == CPP_UTF8STRING_USERDEF);
2697 }
2698
2699 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2700
2701 static bool
2702 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2703 {
2704 return token->keyword == keyword;
2705 }
2706
2707 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2708 PRAGMA_NONE. */
2709
2710 static enum pragma_kind
2711 cp_parser_pragma_kind (cp_token *token)
2712 {
2713 if (token->type != CPP_PRAGMA)
2714 return PRAGMA_NONE;
2715 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2716 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2717 }
2718
2719 /* Helper function for cp_parser_error.
2720 Having peeked a token of kind TOK1_KIND that might signify
2721 a conflict marker, peek successor tokens to determine
2722 if we actually do have a conflict marker.
2723 Specifically, we consider a run of 7 '<', '=' or '>' characters
2724 at the start of a line as a conflict marker.
2725 These come through the lexer as three pairs and a single,
2726 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2727 If it returns true, *OUT_LOC is written to with the location/range
2728 of the marker. */
2729
2730 static bool
2731 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2732 location_t *out_loc)
2733 {
2734 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2735 if (token2->type != tok1_kind)
2736 return false;
2737 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2738 if (token3->type != tok1_kind)
2739 return false;
2740 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2741 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2742 return false;
2743
2744 /* It must be at the start of the line. */
2745 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2746 if (LOCATION_COLUMN (start_loc) != 1)
2747 return false;
2748
2749 /* We have a conflict marker. Construct a location of the form:
2750 <<<<<<<
2751 ^~~~~~~
2752 with start == caret, finishing at the end of the marker. */
2753 location_t finish_loc = get_finish (token4->location);
2754 *out_loc = make_location (start_loc, start_loc, finish_loc);
2755
2756 return true;
2757 }
2758
2759 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2760 RT_CLOSE_PAREN. */
2761
2762 static const char *
2763 get_matching_symbol (required_token token_desc)
2764 {
2765 switch (token_desc)
2766 {
2767 default:
2768 gcc_unreachable ();
2769 return "";
2770 case RT_CLOSE_BRACE:
2771 return "{";
2772 case RT_CLOSE_PAREN:
2773 return "(";
2774 }
2775 }
2776
2777 /* Attempt to convert TOKEN_DESC from a required_token to an
2778 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2779
2780 static enum cpp_ttype
2781 get_required_cpp_ttype (required_token token_desc)
2782 {
2783 switch (token_desc)
2784 {
2785 case RT_SEMICOLON:
2786 return CPP_SEMICOLON;
2787 case RT_OPEN_PAREN:
2788 return CPP_OPEN_PAREN;
2789 case RT_CLOSE_BRACE:
2790 return CPP_CLOSE_BRACE;
2791 case RT_OPEN_BRACE:
2792 return CPP_OPEN_BRACE;
2793 case RT_CLOSE_SQUARE:
2794 return CPP_CLOSE_SQUARE;
2795 case RT_OPEN_SQUARE:
2796 return CPP_OPEN_SQUARE;
2797 case RT_COMMA:
2798 return CPP_COMMA;
2799 case RT_COLON:
2800 return CPP_COLON;
2801 case RT_CLOSE_PAREN:
2802 return CPP_CLOSE_PAREN;
2803
2804 default:
2805 /* Use CPP_EOF as a "no completions possible" code. */
2806 return CPP_EOF;
2807 }
2808 }
2809
2810
2811 /* Subroutine of cp_parser_error and cp_parser_required_error.
2812
2813 Issue a diagnostic of the form
2814 FILE:LINE: MESSAGE before TOKEN
2815 where TOKEN is the next token in the input stream. MESSAGE
2816 (specified by the caller) is usually of the form "expected
2817 OTHER-TOKEN".
2818
2819 This bypasses the check for tentative passing, and potentially
2820 adds material needed by cp_parser_required_error.
2821
2822 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2823 suggesting insertion of the missing token.
2824
2825 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2826 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2827 location. */
2828
2829 static void
2830 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2831 required_token missing_token_desc,
2832 location_t matching_location)
2833 {
2834 cp_token *token = cp_lexer_peek_token (parser->lexer);
2835 /* This diagnostic makes more sense if it is tagged to the line
2836 of the token we just peeked at. */
2837 cp_lexer_set_source_position_from_token (token);
2838
2839 if (token->type == CPP_PRAGMA)
2840 {
2841 error_at (token->location,
2842 "%<#pragma%> is not allowed here");
2843 cp_parser_skip_to_pragma_eol (parser, token);
2844 return;
2845 }
2846
2847 /* If this is actually a conflict marker, report it as such. */
2848 if (token->type == CPP_LSHIFT
2849 || token->type == CPP_RSHIFT
2850 || token->type == CPP_EQ_EQ)
2851 {
2852 location_t loc;
2853 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2854 {
2855 error_at (loc, "version control conflict marker in file");
2856 expanded_location token_exploc = expand_location (token->location);
2857 /* Consume tokens until the end of the source line. */
2858 while (1)
2859 {
2860 cp_lexer_consume_token (parser->lexer);
2861 cp_token *next = cp_lexer_peek_token (parser->lexer);
2862 if (next == NULL)
2863 break;
2864 expanded_location next_exploc = expand_location (next->location);
2865 if (next_exploc.file != token_exploc.file)
2866 break;
2867 if (next_exploc.line != token_exploc.line)
2868 break;
2869 }
2870 return;
2871 }
2872 }
2873
2874 gcc_rich_location richloc (input_location);
2875
2876 bool added_matching_location = false;
2877
2878 if (missing_token_desc != RT_NONE)
2879 {
2880 /* Potentially supply a fix-it hint, suggesting to add the
2881 missing token immediately after the *previous* token.
2882 This may move the primary location within richloc. */
2883 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2884 location_t prev_token_loc
2885 = cp_lexer_previous_token (parser->lexer)->location;
2886 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2887
2888 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2889 Attempt to consolidate diagnostics by printing it as a
2890 secondary range within the main diagnostic. */
2891 if (matching_location != UNKNOWN_LOCATION)
2892 added_matching_location
2893 = richloc.add_location_if_nearby (matching_location);
2894 }
2895
2896 /* Actually emit the error. */
2897 c_parse_error (gmsgid,
2898 /* Because c_parser_error does not understand
2899 CPP_KEYWORD, keywords are treated like
2900 identifiers. */
2901 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2902 token->u.value, token->flags, &richloc);
2903
2904 if (missing_token_desc != RT_NONE)
2905 {
2906 /* If we weren't able to consolidate matching_location, then
2907 print it as a secondary diagnostic. */
2908 if (matching_location != UNKNOWN_LOCATION
2909 && !added_matching_location)
2910 inform (matching_location, "to match this %qs",
2911 get_matching_symbol (missing_token_desc));
2912 }
2913 }
2914
2915 /* If not parsing tentatively, issue a diagnostic of the form
2916 FILE:LINE: MESSAGE before TOKEN
2917 where TOKEN is the next token in the input stream. MESSAGE
2918 (specified by the caller) is usually of the form "expected
2919 OTHER-TOKEN". */
2920
2921 static void
2922 cp_parser_error (cp_parser* parser, const char* gmsgid)
2923 {
2924 if (!cp_parser_simulate_error (parser))
2925 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2926 }
2927
2928 /* Issue an error about name-lookup failing. NAME is the
2929 IDENTIFIER_NODE DECL is the result of
2930 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2931 the thing that we hoped to find. */
2932
2933 static void
2934 cp_parser_name_lookup_error (cp_parser* parser,
2935 tree name,
2936 tree decl,
2937 name_lookup_error desired,
2938 location_t location)
2939 {
2940 /* If name lookup completely failed, tell the user that NAME was not
2941 declared. */
2942 if (decl == error_mark_node)
2943 {
2944 if (parser->scope && parser->scope != global_namespace)
2945 error_at (location, "%<%E::%E%> has not been declared",
2946 parser->scope, name);
2947 else if (parser->scope == global_namespace)
2948 error_at (location, "%<::%E%> has not been declared", name);
2949 else if (parser->object_scope
2950 && !CLASS_TYPE_P (parser->object_scope))
2951 error_at (location, "request for member %qE in non-class type %qT",
2952 name, parser->object_scope);
2953 else if (parser->object_scope)
2954 error_at (location, "%<%T::%E%> has not been declared",
2955 parser->object_scope, name);
2956 else
2957 error_at (location, "%qE has not been declared", name);
2958 }
2959 else if (parser->scope && parser->scope != global_namespace)
2960 {
2961 switch (desired)
2962 {
2963 case NLE_TYPE:
2964 error_at (location, "%<%E::%E%> is not a type",
2965 parser->scope, name);
2966 break;
2967 case NLE_CXX98:
2968 error_at (location, "%<%E::%E%> is not a class or namespace",
2969 parser->scope, name);
2970 break;
2971 case NLE_NOT_CXX98:
2972 error_at (location,
2973 "%<%E::%E%> is not a class, namespace, or enumeration",
2974 parser->scope, name);
2975 break;
2976 default:
2977 gcc_unreachable ();
2978
2979 }
2980 }
2981 else if (parser->scope == global_namespace)
2982 {
2983 switch (desired)
2984 {
2985 case NLE_TYPE:
2986 error_at (location, "%<::%E%> is not a type", name);
2987 break;
2988 case NLE_CXX98:
2989 error_at (location, "%<::%E%> is not a class or namespace", name);
2990 break;
2991 case NLE_NOT_CXX98:
2992 error_at (location,
2993 "%<::%E%> is not a class, namespace, or enumeration",
2994 name);
2995 break;
2996 default:
2997 gcc_unreachable ();
2998 }
2999 }
3000 else
3001 {
3002 switch (desired)
3003 {
3004 case NLE_TYPE:
3005 error_at (location, "%qE is not a type", name);
3006 break;
3007 case NLE_CXX98:
3008 error_at (location, "%qE is not a class or namespace", name);
3009 break;
3010 case NLE_NOT_CXX98:
3011 error_at (location,
3012 "%qE is not a class, namespace, or enumeration", name);
3013 break;
3014 default:
3015 gcc_unreachable ();
3016 }
3017 }
3018 }
3019
3020 /* If we are parsing tentatively, remember that an error has occurred
3021 during this tentative parse. Returns true if the error was
3022 simulated; false if a message should be issued by the caller. */
3023
3024 static bool
3025 cp_parser_simulate_error (cp_parser* parser)
3026 {
3027 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3028 {
3029 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3030 return true;
3031 }
3032 return false;
3033 }
3034
3035 /* This function is called when a type is defined. If type
3036 definitions are forbidden at this point, an error message is
3037 issued. */
3038
3039 static bool
3040 cp_parser_check_type_definition (cp_parser* parser)
3041 {
3042 /* If types are forbidden here, issue a message. */
3043 if (parser->type_definition_forbidden_message)
3044 {
3045 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3046 in the message need to be interpreted. */
3047 error (parser->type_definition_forbidden_message);
3048 return false;
3049 }
3050 return true;
3051 }
3052
3053 /* This function is called when the DECLARATOR is processed. The TYPE
3054 was a type defined in the decl-specifiers. If it is invalid to
3055 define a type in the decl-specifiers for DECLARATOR, an error is
3056 issued. TYPE_LOCATION is the location of TYPE and is used
3057 for error reporting. */
3058
3059 static void
3060 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3061 tree type, location_t type_location)
3062 {
3063 /* [dcl.fct] forbids type definitions in return types.
3064 Unfortunately, it's not easy to know whether or not we are
3065 processing a return type until after the fact. */
3066 while (declarator
3067 && (declarator->kind == cdk_pointer
3068 || declarator->kind == cdk_reference
3069 || declarator->kind == cdk_ptrmem))
3070 declarator = declarator->declarator;
3071 if (declarator
3072 && declarator->kind == cdk_function)
3073 {
3074 error_at (type_location,
3075 "new types may not be defined in a return type");
3076 inform (type_location,
3077 "(perhaps a semicolon is missing after the definition of %qT)",
3078 type);
3079 }
3080 }
3081
3082 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3083 "<" in any valid C++ program. If the next token is indeed "<",
3084 issue a message warning the user about what appears to be an
3085 invalid attempt to form a template-id. LOCATION is the location
3086 of the type-specifier (TYPE) */
3087
3088 static void
3089 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3090 tree type,
3091 enum tag_types tag_type,
3092 location_t location)
3093 {
3094 cp_token_position start = 0;
3095
3096 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3097 {
3098 if (TREE_CODE (type) == TYPE_DECL)
3099 type = TREE_TYPE (type);
3100 if (TYPE_P (type) && !template_placeholder_p (type))
3101 error_at (location, "%qT is not a template", type);
3102 else if (identifier_p (type))
3103 {
3104 if (tag_type != none_type)
3105 error_at (location, "%qE is not a class template", type);
3106 else
3107 error_at (location, "%qE is not a template", type);
3108 }
3109 else
3110 error_at (location, "invalid template-id");
3111 /* Remember the location of the invalid "<". */
3112 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3113 start = cp_lexer_token_position (parser->lexer, true);
3114 /* Consume the "<". */
3115 cp_lexer_consume_token (parser->lexer);
3116 /* Parse the template arguments. */
3117 cp_parser_enclosed_template_argument_list (parser);
3118 /* Permanently remove the invalid template arguments so that
3119 this error message is not issued again. */
3120 if (start)
3121 cp_lexer_purge_tokens_after (parser->lexer, start);
3122 }
3123 }
3124
3125 /* If parsing an integral constant-expression, issue an error message
3126 about the fact that THING appeared and return true. Otherwise,
3127 return false. In either case, set
3128 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3129
3130 static bool
3131 cp_parser_non_integral_constant_expression (cp_parser *parser,
3132 non_integral_constant thing)
3133 {
3134 parser->non_integral_constant_expression_p = true;
3135 if (parser->integral_constant_expression_p)
3136 {
3137 if (!parser->allow_non_integral_constant_expression_p)
3138 {
3139 const char *msg = NULL;
3140 switch (thing)
3141 {
3142 case NIC_FLOAT:
3143 pedwarn (input_location, OPT_Wpedantic,
3144 "ISO C++ forbids using a floating-point literal "
3145 "in a constant-expression");
3146 return true;
3147 case NIC_CAST:
3148 error ("a cast to a type other than an integral or "
3149 "enumeration type cannot appear in a "
3150 "constant-expression");
3151 return true;
3152 case NIC_TYPEID:
3153 error ("%<typeid%> operator "
3154 "cannot appear in a constant-expression");
3155 return true;
3156 case NIC_NCC:
3157 error ("non-constant compound literals "
3158 "cannot appear in a constant-expression");
3159 return true;
3160 case NIC_FUNC_CALL:
3161 error ("a function call "
3162 "cannot appear in a constant-expression");
3163 return true;
3164 case NIC_INC:
3165 error ("an increment "
3166 "cannot appear in a constant-expression");
3167 return true;
3168 case NIC_DEC:
3169 error ("an decrement "
3170 "cannot appear in a constant-expression");
3171 return true;
3172 case NIC_ARRAY_REF:
3173 error ("an array reference "
3174 "cannot appear in a constant-expression");
3175 return true;
3176 case NIC_ADDR_LABEL:
3177 error ("the address of a label "
3178 "cannot appear in a constant-expression");
3179 return true;
3180 case NIC_OVERLOADED:
3181 error ("calls to overloaded operators "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_ASSIGNMENT:
3185 error ("an assignment cannot appear in a constant-expression");
3186 return true;
3187 case NIC_COMMA:
3188 error ("a comma operator "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_CONSTRUCTOR:
3192 error ("a call to a constructor "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_TRANSACTION:
3196 error ("a transaction expression "
3197 "cannot appear in a constant-expression");
3198 return true;
3199 case NIC_THIS:
3200 msg = "this";
3201 break;
3202 case NIC_FUNC_NAME:
3203 msg = "__FUNCTION__";
3204 break;
3205 case NIC_PRETTY_FUNC:
3206 msg = "__PRETTY_FUNCTION__";
3207 break;
3208 case NIC_C99_FUNC:
3209 msg = "__func__";
3210 break;
3211 case NIC_VA_ARG:
3212 msg = "va_arg";
3213 break;
3214 case NIC_ARROW:
3215 msg = "->";
3216 break;
3217 case NIC_POINT:
3218 msg = ".";
3219 break;
3220 case NIC_STAR:
3221 msg = "*";
3222 break;
3223 case NIC_ADDR:
3224 msg = "&";
3225 break;
3226 case NIC_PREINCREMENT:
3227 msg = "++";
3228 break;
3229 case NIC_PREDECREMENT:
3230 msg = "--";
3231 break;
3232 case NIC_NEW:
3233 msg = "new";
3234 break;
3235 case NIC_DEL:
3236 msg = "delete";
3237 break;
3238 default:
3239 gcc_unreachable ();
3240 }
3241 if (msg)
3242 error ("%qs cannot appear in a constant-expression", msg);
3243 return true;
3244 }
3245 }
3246 return false;
3247 }
3248
3249 /* Emit a diagnostic for an invalid type name. This function commits
3250 to the current active tentative parse, if any. (Otherwise, the
3251 problematic construct might be encountered again later, resulting
3252 in duplicate error messages.) LOCATION is the location of ID. */
3253
3254 static void
3255 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3256 location_t location)
3257 {
3258 tree decl, ambiguous_decls;
3259 cp_parser_commit_to_tentative_parse (parser);
3260 /* Try to lookup the identifier. */
3261 decl = cp_parser_lookup_name (parser, id, none_type,
3262 /*is_template=*/false,
3263 /*is_namespace=*/false,
3264 /*check_dependency=*/true,
3265 &ambiguous_decls, location);
3266 if (ambiguous_decls)
3267 /* If the lookup was ambiguous, an error will already have
3268 been issued. */
3269 return;
3270 /* If the lookup found a template-name, it means that the user forgot
3271 to specify an argument list. Emit a useful error message. */
3272 if (DECL_TYPE_TEMPLATE_P (decl))
3273 {
3274 auto_diagnostic_group d;
3275 error_at (location,
3276 "invalid use of template-name %qE without an argument list",
3277 decl);
3278 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3279 inform (location, "class template argument deduction is only available "
3280 "with -std=c++17 or -std=gnu++17");
3281 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3282 }
3283 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3284 error_at (location, "invalid use of destructor %qD as a type", id);
3285 else if (TREE_CODE (decl) == TYPE_DECL)
3286 /* Something like 'unsigned A a;' */
3287 error_at (location, "invalid combination of multiple type-specifiers");
3288 else if (!parser->scope)
3289 {
3290 /* Issue an error message. */
3291 auto_diagnostic_group d;
3292 name_hint hint;
3293 if (TREE_CODE (id) == IDENTIFIER_NODE)
3294 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3295 if (const char *suggestion = hint.suggestion ())
3296 {
3297 gcc_rich_location richloc (location);
3298 richloc.add_fixit_replace (suggestion);
3299 error_at (&richloc,
3300 "%qE does not name a type; did you mean %qs?",
3301 id, suggestion);
3302 }
3303 else
3304 error_at (location, "%qE does not name a type", id);
3305 /* If we're in a template class, it's possible that the user was
3306 referring to a type from a base class. For example:
3307
3308 template <typename T> struct A { typedef T X; };
3309 template <typename T> struct B : public A<T> { X x; };
3310
3311 The user should have said "typename A<T>::X". */
3312 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3313 inform (location, "C++11 %<constexpr%> only available with "
3314 "-std=c++11 or -std=gnu++11");
3315 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3316 inform (location, "C++11 %<noexcept%> only available with "
3317 "-std=c++11 or -std=gnu++11");
3318 else if (cxx_dialect < cxx11
3319 && TREE_CODE (id) == IDENTIFIER_NODE
3320 && id_equal (id, "thread_local"))
3321 inform (location, "C++11 %<thread_local%> only available with "
3322 "-std=c++11 or -std=gnu++11");
3323 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3324 inform (location, "%<concept%> only available with -fconcepts");
3325 else if (processing_template_decl && current_class_type
3326 && TYPE_BINFO (current_class_type))
3327 {
3328 tree b;
3329
3330 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3331 b;
3332 b = TREE_CHAIN (b))
3333 {
3334 tree base_type = BINFO_TYPE (b);
3335 if (CLASS_TYPE_P (base_type)
3336 && dependent_type_p (base_type))
3337 {
3338 tree field;
3339 /* Go from a particular instantiation of the
3340 template (which will have an empty TYPE_FIELDs),
3341 to the main version. */
3342 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3343 for (field = TYPE_FIELDS (base_type);
3344 field;
3345 field = DECL_CHAIN (field))
3346 if (TREE_CODE (field) == TYPE_DECL
3347 && DECL_NAME (field) == id)
3348 {
3349 inform (location,
3350 "(perhaps %<typename %T::%E%> was intended)",
3351 BINFO_TYPE (b), id);
3352 break;
3353 }
3354 if (field)
3355 break;
3356 }
3357 }
3358 }
3359 }
3360 /* Here we diagnose qualified-ids where the scope is actually correct,
3361 but the identifier does not resolve to a valid type name. */
3362 else if (parser->scope != error_mark_node)
3363 {
3364 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3365 {
3366 auto_diagnostic_group d;
3367 name_hint hint;
3368 if (decl == error_mark_node)
3369 hint = suggest_alternative_in_explicit_scope (location, id,
3370 parser->scope);
3371 const char *suggestion = hint.suggestion ();
3372 gcc_rich_location richloc (location_of (id));
3373 if (suggestion)
3374 richloc.add_fixit_replace (suggestion);
3375 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3376 {
3377 if (suggestion)
3378 error_at (&richloc,
3379 "%qE in namespace %qE does not name a template"
3380 " type; did you mean %qs?",
3381 id, parser->scope, suggestion);
3382 else
3383 error_at (&richloc,
3384 "%qE in namespace %qE does not name a template type",
3385 id, parser->scope);
3386 }
3387 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3388 {
3389 if (suggestion)
3390 error_at (&richloc,
3391 "%qE in namespace %qE does not name a template"
3392 " type; did you mean %qs?",
3393 TREE_OPERAND (id, 0), parser->scope, suggestion);
3394 else
3395 error_at (&richloc,
3396 "%qE in namespace %qE does not name a template"
3397 " type",
3398 TREE_OPERAND (id, 0), parser->scope);
3399 }
3400 else
3401 {
3402 if (suggestion)
3403 error_at (&richloc,
3404 "%qE in namespace %qE does not name a type"
3405 "; did you mean %qs?",
3406 id, parser->scope, suggestion);
3407 else
3408 error_at (&richloc,
3409 "%qE in namespace %qE does not name a type",
3410 id, parser->scope);
3411 }
3412 if (DECL_P (decl))
3413 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3414 }
3415 else if (CLASS_TYPE_P (parser->scope)
3416 && constructor_name_p (id, parser->scope))
3417 {
3418 /* A<T>::A<T>() */
3419 auto_diagnostic_group d;
3420 error_at (location, "%<%T::%E%> names the constructor, not"
3421 " the type", parser->scope, id);
3422 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3423 error_at (location, "and %qT has no template constructors",
3424 parser->scope);
3425 }
3426 else if (TYPE_P (parser->scope)
3427 && dependent_scope_p (parser->scope))
3428 {
3429 gcc_rich_location richloc (location);
3430 richloc.add_fixit_insert_before ("typename ");
3431 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3432 error_at (&richloc,
3433 "need %<typename%> before %<%T::%D::%E%> because "
3434 "%<%T::%D%> is a dependent scope",
3435 TYPE_CONTEXT (parser->scope),
3436 TYPENAME_TYPE_FULLNAME (parser->scope),
3437 id,
3438 TYPE_CONTEXT (parser->scope),
3439 TYPENAME_TYPE_FULLNAME (parser->scope));
3440 else
3441 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3442 "%qT is a dependent scope",
3443 parser->scope, id, parser->scope);
3444 }
3445 else if (TYPE_P (parser->scope))
3446 {
3447 auto_diagnostic_group d;
3448 if (!COMPLETE_TYPE_P (parser->scope))
3449 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3450 parser->scope);
3451 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3452 error_at (location_of (id),
3453 "%qE in %q#T does not name a template type",
3454 id, parser->scope);
3455 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3456 error_at (location_of (id),
3457 "%qE in %q#T does not name a template type",
3458 TREE_OPERAND (id, 0), parser->scope);
3459 else
3460 error_at (location_of (id),
3461 "%qE in %q#T does not name a type",
3462 id, parser->scope);
3463 if (DECL_P (decl))
3464 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3465 }
3466 else
3467 gcc_unreachable ();
3468 }
3469 }
3470
3471 /* Check for a common situation where a type-name should be present,
3472 but is not, and issue a sensible error message. Returns true if an
3473 invalid type-name was detected.
3474
3475 The situation handled by this function are variable declarations of the
3476 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3477 Usually, `ID' should name a type, but if we got here it means that it
3478 does not. We try to emit the best possible error message depending on
3479 how exactly the id-expression looks like. */
3480
3481 static bool
3482 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3483 {
3484 tree id;
3485 cp_token *token = cp_lexer_peek_token (parser->lexer);
3486
3487 /* Avoid duplicate error about ambiguous lookup. */
3488 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3489 {
3490 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3491 if (next->type == CPP_NAME && next->error_reported)
3492 goto out;
3493 }
3494
3495 cp_parser_parse_tentatively (parser);
3496 id = cp_parser_id_expression (parser,
3497 /*template_keyword_p=*/false,
3498 /*check_dependency_p=*/true,
3499 /*template_p=*/NULL,
3500 /*declarator_p=*/false,
3501 /*optional_p=*/false);
3502 /* If the next token is a (, this is a function with no explicit return
3503 type, i.e. constructor, destructor or conversion op. */
3504 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3505 || TREE_CODE (id) == TYPE_DECL)
3506 {
3507 cp_parser_abort_tentative_parse (parser);
3508 return false;
3509 }
3510 if (!cp_parser_parse_definitely (parser))
3511 return false;
3512
3513 /* Emit a diagnostic for the invalid type. */
3514 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3515 out:
3516 /* If we aren't in the middle of a declarator (i.e. in a
3517 parameter-declaration-clause), skip to the end of the declaration;
3518 there's no point in trying to process it. */
3519 if (!parser->in_declarator_p)
3520 cp_parser_skip_to_end_of_block_or_statement (parser);
3521 return true;
3522 }
3523
3524 /* Consume tokens up to, and including, the next non-nested closing `)'.
3525 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3526 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3527 found an unnested token of that type. */
3528
3529 static int
3530 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3531 bool recovering,
3532 cpp_ttype or_ttype,
3533 bool consume_paren)
3534 {
3535 unsigned paren_depth = 0;
3536 unsigned brace_depth = 0;
3537 unsigned square_depth = 0;
3538 unsigned condop_depth = 0;
3539
3540 if (recovering && or_ttype == CPP_EOF
3541 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3542 return 0;
3543
3544 while (true)
3545 {
3546 cp_token * token = cp_lexer_peek_token (parser->lexer);
3547
3548 /* Have we found what we're looking for before the closing paren? */
3549 if (token->type == or_ttype && or_ttype != CPP_EOF
3550 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3551 return -1;
3552
3553 switch (token->type)
3554 {
3555 case CPP_EOF:
3556 case CPP_PRAGMA_EOL:
3557 /* If we've run out of tokens, then there is no closing `)'. */
3558 return 0;
3559
3560 /* This is good for lambda expression capture-lists. */
3561 case CPP_OPEN_SQUARE:
3562 ++square_depth;
3563 break;
3564 case CPP_CLOSE_SQUARE:
3565 if (!square_depth--)
3566 return 0;
3567 break;
3568
3569 case CPP_SEMICOLON:
3570 /* This matches the processing in skip_to_end_of_statement. */
3571 if (!brace_depth)
3572 return 0;
3573 break;
3574
3575 case CPP_OPEN_BRACE:
3576 ++brace_depth;
3577 break;
3578 case CPP_CLOSE_BRACE:
3579 if (!brace_depth--)
3580 return 0;
3581 break;
3582
3583 case CPP_OPEN_PAREN:
3584 if (!brace_depth)
3585 ++paren_depth;
3586 break;
3587
3588 case CPP_CLOSE_PAREN:
3589 if (!brace_depth && !paren_depth--)
3590 {
3591 if (consume_paren)
3592 cp_lexer_consume_token (parser->lexer);
3593 return 1;
3594 }
3595 break;
3596
3597 case CPP_QUERY:
3598 if (!brace_depth && !paren_depth && !square_depth)
3599 ++condop_depth;
3600 break;
3601
3602 case CPP_COLON:
3603 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3604 condop_depth--;
3605 break;
3606
3607 default:
3608 break;
3609 }
3610
3611 /* Consume the token. */
3612 cp_lexer_consume_token (parser->lexer);
3613 }
3614 }
3615
3616 /* Consume tokens up to, and including, the next non-nested closing `)'.
3617 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3618 are doing error recovery. Returns -1 if OR_COMMA is true and we
3619 found an unnested token of that type. */
3620
3621 static int
3622 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3623 bool recovering,
3624 bool or_comma,
3625 bool consume_paren)
3626 {
3627 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3628 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3629 ttype, consume_paren);
3630 }
3631
3632 /* Consume tokens until we reach the end of the current statement.
3633 Normally, that will be just before consuming a `;'. However, if a
3634 non-nested `}' comes first, then we stop before consuming that. */
3635
3636 static void
3637 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3638 {
3639 unsigned nesting_depth = 0;
3640
3641 /* Unwind generic function template scope if necessary. */
3642 if (parser->fully_implicit_function_template_p)
3643 abort_fully_implicit_template (parser);
3644
3645 while (true)
3646 {
3647 cp_token *token = cp_lexer_peek_token (parser->lexer);
3648
3649 switch (token->type)
3650 {
3651 case CPP_EOF:
3652 case CPP_PRAGMA_EOL:
3653 /* If we've run out of tokens, stop. */
3654 return;
3655
3656 case CPP_SEMICOLON:
3657 /* If the next token is a `;', we have reached the end of the
3658 statement. */
3659 if (!nesting_depth)
3660 return;
3661 break;
3662
3663 case CPP_CLOSE_BRACE:
3664 /* If this is a non-nested '}', stop before consuming it.
3665 That way, when confronted with something like:
3666
3667 { 3 + }
3668
3669 we stop before consuming the closing '}', even though we
3670 have not yet reached a `;'. */
3671 if (nesting_depth == 0)
3672 return;
3673
3674 /* If it is the closing '}' for a block that we have
3675 scanned, stop -- but only after consuming the token.
3676 That way given:
3677
3678 void f g () { ... }
3679 typedef int I;
3680
3681 we will stop after the body of the erroneously declared
3682 function, but before consuming the following `typedef'
3683 declaration. */
3684 if (--nesting_depth == 0)
3685 {
3686 cp_lexer_consume_token (parser->lexer);
3687 return;
3688 }
3689 break;
3690
3691 case CPP_OPEN_BRACE:
3692 ++nesting_depth;
3693 break;
3694
3695 default:
3696 break;
3697 }
3698
3699 /* Consume the token. */
3700 cp_lexer_consume_token (parser->lexer);
3701 }
3702 }
3703
3704 /* This function is called at the end of a statement or declaration.
3705 If the next token is a semicolon, it is consumed; otherwise, error
3706 recovery is attempted. */
3707
3708 static void
3709 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3710 {
3711 /* Look for the trailing `;'. */
3712 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3713 {
3714 /* If there is additional (erroneous) input, skip to the end of
3715 the statement. */
3716 cp_parser_skip_to_end_of_statement (parser);
3717 /* If the next token is now a `;', consume it. */
3718 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3719 cp_lexer_consume_token (parser->lexer);
3720 }
3721 }
3722
3723 /* Skip tokens until we have consumed an entire block, or until we
3724 have consumed a non-nested `;'. */
3725
3726 static void
3727 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3728 {
3729 int nesting_depth = 0;
3730
3731 /* Unwind generic function template scope if necessary. */
3732 if (parser->fully_implicit_function_template_p)
3733 abort_fully_implicit_template (parser);
3734
3735 while (nesting_depth >= 0)
3736 {
3737 cp_token *token = cp_lexer_peek_token (parser->lexer);
3738
3739 switch (token->type)
3740 {
3741 case CPP_EOF:
3742 case CPP_PRAGMA_EOL:
3743 /* If we've run out of tokens, stop. */
3744 return;
3745
3746 case CPP_SEMICOLON:
3747 /* Stop if this is an unnested ';'. */
3748 if (!nesting_depth)
3749 nesting_depth = -1;
3750 break;
3751
3752 case CPP_CLOSE_BRACE:
3753 /* Stop if this is an unnested '}', or closes the outermost
3754 nesting level. */
3755 nesting_depth--;
3756 if (nesting_depth < 0)
3757 return;
3758 if (!nesting_depth)
3759 nesting_depth = -1;
3760 break;
3761
3762 case CPP_OPEN_BRACE:
3763 /* Nest. */
3764 nesting_depth++;
3765 break;
3766
3767 default:
3768 break;
3769 }
3770
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser->lexer);
3773 }
3774 }
3775
3776 /* Skip tokens until a non-nested closing curly brace is the next
3777 token, or there are no more tokens. Return true in the first case,
3778 false otherwise. */
3779
3780 static bool
3781 cp_parser_skip_to_closing_brace (cp_parser *parser)
3782 {
3783 unsigned nesting_depth = 0;
3784
3785 while (true)
3786 {
3787 cp_token *token = cp_lexer_peek_token (parser->lexer);
3788
3789 switch (token->type)
3790 {
3791 case CPP_EOF:
3792 case CPP_PRAGMA_EOL:
3793 /* If we've run out of tokens, stop. */
3794 return false;
3795
3796 case CPP_CLOSE_BRACE:
3797 /* If the next token is a non-nested `}', then we have reached
3798 the end of the current block. */
3799 if (nesting_depth-- == 0)
3800 return true;
3801 break;
3802
3803 case CPP_OPEN_BRACE:
3804 /* If it the next token is a `{', then we are entering a new
3805 block. Consume the entire block. */
3806 ++nesting_depth;
3807 break;
3808
3809 default:
3810 break;
3811 }
3812
3813 /* Consume the token. */
3814 cp_lexer_consume_token (parser->lexer);
3815 }
3816 }
3817
3818 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3819 parameter is the PRAGMA token, allowing us to purge the entire pragma
3820 sequence. */
3821
3822 static void
3823 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3824 {
3825 cp_token *token;
3826
3827 parser->lexer->in_pragma = false;
3828
3829 do
3830 token = cp_lexer_consume_token (parser->lexer);
3831 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3832
3833 /* Ensure that the pragma is not parsed again. */
3834 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3835 }
3836
3837 /* Require pragma end of line, resyncing with it as necessary. The
3838 arguments are as for cp_parser_skip_to_pragma_eol. */
3839
3840 static void
3841 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3842 {
3843 parser->lexer->in_pragma = false;
3844 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3845 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3846 }
3847
3848 /* This is a simple wrapper around make_typename_type. When the id is
3849 an unresolved identifier node, we can provide a superior diagnostic
3850 using cp_parser_diagnose_invalid_type_name. */
3851
3852 static tree
3853 cp_parser_make_typename_type (cp_parser *parser, tree id,
3854 location_t id_location)
3855 {
3856 tree result;
3857 if (identifier_p (id))
3858 {
3859 result = make_typename_type (parser->scope, id, typename_type,
3860 /*complain=*/tf_none);
3861 if (result == error_mark_node)
3862 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3863 return result;
3864 }
3865 return make_typename_type (parser->scope, id, typename_type, tf_error);
3866 }
3867
3868 /* This is a wrapper around the
3869 make_{pointer,ptrmem,reference}_declarator functions that decides
3870 which one to call based on the CODE and CLASS_TYPE arguments. The
3871 CODE argument should be one of the values returned by
3872 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3873 appertain to the pointer or reference. */
3874
3875 static cp_declarator *
3876 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3877 cp_cv_quals cv_qualifiers,
3878 cp_declarator *target,
3879 tree attributes)
3880 {
3881 if (code == ERROR_MARK || target == cp_error_declarator)
3882 return cp_error_declarator;
3883
3884 if (code == INDIRECT_REF)
3885 if (class_type == NULL_TREE)
3886 return make_pointer_declarator (cv_qualifiers, target, attributes);
3887 else
3888 return make_ptrmem_declarator (cv_qualifiers, class_type,
3889 target, attributes);
3890 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3891 return make_reference_declarator (cv_qualifiers, target,
3892 false, attributes);
3893 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3894 return make_reference_declarator (cv_qualifiers, target,
3895 true, attributes);
3896 gcc_unreachable ();
3897 }
3898
3899 /* Create a new C++ parser. */
3900
3901 static cp_parser *
3902 cp_parser_new (void)
3903 {
3904 cp_parser *parser;
3905 cp_lexer *lexer;
3906 unsigned i;
3907
3908 /* cp_lexer_new_main is called before doing GC allocation because
3909 cp_lexer_new_main might load a PCH file. */
3910 lexer = cp_lexer_new_main ();
3911
3912 /* Initialize the binops_by_token so that we can get the tree
3913 directly from the token. */
3914 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3915 binops_by_token[binops[i].token_type] = binops[i];
3916
3917 parser = ggc_cleared_alloc<cp_parser> ();
3918 parser->lexer = lexer;
3919 parser->context = cp_parser_context_new (NULL);
3920
3921 /* For now, we always accept GNU extensions. */
3922 parser->allow_gnu_extensions_p = 1;
3923
3924 /* The `>' token is a greater-than operator, not the end of a
3925 template-id. */
3926 parser->greater_than_is_operator_p = true;
3927
3928 parser->default_arg_ok_p = true;
3929
3930 /* We are not parsing a constant-expression. */
3931 parser->integral_constant_expression_p = false;
3932 parser->allow_non_integral_constant_expression_p = false;
3933 parser->non_integral_constant_expression_p = false;
3934
3935 /* Local variable names are not forbidden. */
3936 parser->local_variables_forbidden_p = false;
3937
3938 /* We are not processing an `extern "C"' declaration. */
3939 parser->in_unbraced_linkage_specification_p = false;
3940
3941 /* We are not processing a declarator. */
3942 parser->in_declarator_p = false;
3943
3944 /* We are not processing a template-argument-list. */
3945 parser->in_template_argument_list_p = false;
3946
3947 /* We are not in an iteration statement. */
3948 parser->in_statement = 0;
3949
3950 /* We are not in a switch statement. */
3951 parser->in_switch_statement_p = false;
3952
3953 /* We are not parsing a type-id inside an expression. */
3954 parser->in_type_id_in_expr_p = false;
3955
3956 /* String literals should be translated to the execution character set. */
3957 parser->translate_strings_p = true;
3958
3959 /* We are not parsing a function body. */
3960 parser->in_function_body = false;
3961
3962 /* We can correct until told otherwise. */
3963 parser->colon_corrects_to_scope_p = true;
3964
3965 /* The unparsed function queue is empty. */
3966 push_unparsed_function_queues (parser);
3967
3968 /* There are no classes being defined. */
3969 parser->num_classes_being_defined = 0;
3970
3971 /* No template parameters apply. */
3972 parser->num_template_parameter_lists = 0;
3973
3974 /* Special parsing data structures. */
3975 parser->omp_declare_simd = NULL;
3976 parser->oacc_routine = NULL;
3977
3978 /* Not declaring an implicit function template. */
3979 parser->auto_is_implicit_function_template_parm_p = false;
3980 parser->fully_implicit_function_template_p = false;
3981 parser->implicit_template_parms = 0;
3982 parser->implicit_template_scope = 0;
3983
3984 /* Allow constrained-type-specifiers. */
3985 parser->prevent_constrained_type_specifiers = 0;
3986
3987 /* We haven't yet seen an 'extern "C"'. */
3988 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3989
3990 return parser;
3991 }
3992
3993 /* Create a cp_lexer structure which will emit the tokens in CACHE
3994 and push it onto the parser's lexer stack. This is used for delayed
3995 parsing of in-class method bodies and default arguments, and should
3996 not be confused with tentative parsing. */
3997 static void
3998 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3999 {
4000 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4001 lexer->next = parser->lexer;
4002 parser->lexer = lexer;
4003
4004 /* Move the current source position to that of the first token in the
4005 new lexer. */
4006 cp_lexer_set_source_position_from_token (lexer->next_token);
4007 }
4008
4009 /* Pop the top lexer off the parser stack. This is never used for the
4010 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4011 static void
4012 cp_parser_pop_lexer (cp_parser *parser)
4013 {
4014 cp_lexer *lexer = parser->lexer;
4015 parser->lexer = lexer->next;
4016 cp_lexer_destroy (lexer);
4017
4018 /* Put the current source position back where it was before this
4019 lexer was pushed. */
4020 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4021 }
4022
4023 /* Lexical conventions [gram.lex] */
4024
4025 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4026 identifier. */
4027
4028 static cp_expr
4029 cp_parser_identifier (cp_parser* parser)
4030 {
4031 cp_token *token;
4032
4033 /* Look for the identifier. */
4034 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4035 /* Return the value. */
4036 if (token)
4037 return cp_expr (token->u.value, token->location);
4038 else
4039 return error_mark_node;
4040 }
4041
4042 /* Parse a sequence of adjacent string constants. Returns a
4043 TREE_STRING representing the combined, nul-terminated string
4044 constant. If TRANSLATE is true, translate the string to the
4045 execution character set. If WIDE_OK is true, a wide string is
4046 invalid here.
4047
4048 C++98 [lex.string] says that if a narrow string literal token is
4049 adjacent to a wide string literal token, the behavior is undefined.
4050 However, C99 6.4.5p4 says that this results in a wide string literal.
4051 We follow C99 here, for consistency with the C front end.
4052
4053 This code is largely lifted from lex_string() in c-lex.c.
4054
4055 FUTURE: ObjC++ will need to handle @-strings here. */
4056 static cp_expr
4057 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4058 bool lookup_udlit = true)
4059 {
4060 tree value;
4061 size_t count;
4062 struct obstack str_ob;
4063 struct obstack loc_ob;
4064 cpp_string str, istr, *strs;
4065 cp_token *tok;
4066 enum cpp_ttype type, curr_type;
4067 int have_suffix_p = 0;
4068 tree string_tree;
4069 tree suffix_id = NULL_TREE;
4070 bool curr_tok_is_userdef_p = false;
4071
4072 tok = cp_lexer_peek_token (parser->lexer);
4073 if (!cp_parser_is_string_literal (tok))
4074 {
4075 cp_parser_error (parser, "expected string-literal");
4076 return error_mark_node;
4077 }
4078
4079 location_t loc = tok->location;
4080
4081 if (cpp_userdef_string_p (tok->type))
4082 {
4083 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4084 curr_type = cpp_userdef_string_remove_type (tok->type);
4085 curr_tok_is_userdef_p = true;
4086 }
4087 else
4088 {
4089 string_tree = tok->u.value;
4090 curr_type = tok->type;
4091 }
4092 type = curr_type;
4093
4094 /* Try to avoid the overhead of creating and destroying an obstack
4095 for the common case of just one string. */
4096 if (!cp_parser_is_string_literal
4097 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4098 {
4099 cp_lexer_consume_token (parser->lexer);
4100
4101 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4102 str.len = TREE_STRING_LENGTH (string_tree);
4103 count = 1;
4104
4105 if (curr_tok_is_userdef_p)
4106 {
4107 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4108 have_suffix_p = 1;
4109 curr_type = cpp_userdef_string_remove_type (tok->type);
4110 }
4111 else
4112 curr_type = tok->type;
4113
4114 strs = &str;
4115 }
4116 else
4117 {
4118 location_t last_tok_loc = tok->location;
4119 gcc_obstack_init (&str_ob);
4120 gcc_obstack_init (&loc_ob);
4121 count = 0;
4122
4123 do
4124 {
4125 cp_lexer_consume_token (parser->lexer);
4126 count++;
4127 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4128 str.len = TREE_STRING_LENGTH (string_tree);
4129
4130 if (curr_tok_is_userdef_p)
4131 {
4132 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4133 if (have_suffix_p == 0)
4134 {
4135 suffix_id = curr_suffix_id;
4136 have_suffix_p = 1;
4137 }
4138 else if (have_suffix_p == 1
4139 && curr_suffix_id != suffix_id)
4140 {
4141 error ("inconsistent user-defined literal suffixes"
4142 " %qD and %qD in string literal",
4143 suffix_id, curr_suffix_id);
4144 have_suffix_p = -1;
4145 }
4146 curr_type = cpp_userdef_string_remove_type (tok->type);
4147 }
4148 else
4149 curr_type = tok->type;
4150
4151 if (type != curr_type)
4152 {
4153 if (type == CPP_STRING)
4154 type = curr_type;
4155 else if (curr_type != CPP_STRING)
4156 {
4157 rich_location rich_loc (line_table, tok->location);
4158 rich_loc.add_range (last_tok_loc);
4159 error_at (&rich_loc,
4160 "unsupported non-standard concatenation "
4161 "of string literals");
4162 }
4163 }
4164
4165 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4166 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4167
4168 last_tok_loc = tok->location;
4169
4170 tok = cp_lexer_peek_token (parser->lexer);
4171 if (cpp_userdef_string_p (tok->type))
4172 {
4173 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4174 curr_type = cpp_userdef_string_remove_type (tok->type);
4175 curr_tok_is_userdef_p = true;
4176 }
4177 else
4178 {
4179 string_tree = tok->u.value;
4180 curr_type = tok->type;
4181 curr_tok_is_userdef_p = false;
4182 }
4183 }
4184 while (cp_parser_is_string_literal (tok));
4185
4186 /* A string literal built by concatenation has its caret=start at
4187 the start of the initial string, and its finish at the finish of
4188 the final string literal. */
4189 loc = make_location (loc, loc, get_finish (last_tok_loc));
4190
4191 strs = (cpp_string *) obstack_finish (&str_ob);
4192 }
4193
4194 if (type != CPP_STRING && !wide_ok)
4195 {
4196 cp_parser_error (parser, "a wide string is invalid in this context");
4197 type = CPP_STRING;
4198 }
4199
4200 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4201 (parse_in, strs, count, &istr, type))
4202 {
4203 value = build_string (istr.len, (const char *)istr.text);
4204 free (CONST_CAST (unsigned char *, istr.text));
4205 if (count > 1)
4206 {
4207 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4208 gcc_assert (g_string_concat_db);
4209 g_string_concat_db->record_string_concatenation (count, locs);
4210 }
4211
4212 switch (type)
4213 {
4214 default:
4215 case CPP_STRING:
4216 case CPP_UTF8STRING:
4217 TREE_TYPE (value) = char_array_type_node;
4218 break;
4219 case CPP_STRING16:
4220 TREE_TYPE (value) = char16_array_type_node;
4221 break;
4222 case CPP_STRING32:
4223 TREE_TYPE (value) = char32_array_type_node;
4224 break;
4225 case CPP_WSTRING:
4226 TREE_TYPE (value) = wchar_array_type_node;
4227 break;
4228 }
4229
4230 value = fix_string_type (value);
4231
4232 if (have_suffix_p)
4233 {
4234 tree literal = build_userdef_literal (suffix_id, value,
4235 OT_NONE, NULL_TREE);
4236 if (lookup_udlit)
4237 value = cp_parser_userdef_string_literal (literal);
4238 else
4239 value = literal;
4240 }
4241 }
4242 else
4243 /* cpp_interpret_string has issued an error. */
4244 value = error_mark_node;
4245
4246 if (count > 1)
4247 {
4248 obstack_free (&str_ob, 0);
4249 obstack_free (&loc_ob, 0);
4250 }
4251
4252 return cp_expr (value, loc);
4253 }
4254
4255 /* Look up a literal operator with the name and the exact arguments. */
4256
4257 static tree
4258 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4259 {
4260 tree decl;
4261 decl = lookup_name (name);
4262 if (!decl || !is_overloaded_fn (decl))
4263 return error_mark_node;
4264
4265 for (lkp_iterator iter (decl); iter; ++iter)
4266 {
4267 unsigned int ix;
4268 bool found = true;
4269 tree fn = *iter;
4270 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4271 if (parmtypes != NULL_TREE)
4272 {
4273 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4274 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4275 {
4276 tree tparm = TREE_VALUE (parmtypes);
4277 tree targ = TREE_TYPE ((*args)[ix]);
4278 bool ptr = TYPE_PTR_P (tparm);
4279 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4280 if ((ptr || arr || !same_type_p (tparm, targ))
4281 && (!ptr || !arr
4282 || !same_type_p (TREE_TYPE (tparm),
4283 TREE_TYPE (targ))))
4284 found = false;
4285 }
4286 if (found
4287 && ix == vec_safe_length (args)
4288 /* May be this should be sufficient_parms_p instead,
4289 depending on how exactly should user-defined literals
4290 work in presence of default arguments on the literal
4291 operator parameters. */
4292 && parmtypes == void_list_node)
4293 return decl;
4294 }
4295 }
4296
4297 return error_mark_node;
4298 }
4299
4300 /* Parse a user-defined char constant. Returns a call to a user-defined
4301 literal operator taking the character as an argument. */
4302
4303 static cp_expr
4304 cp_parser_userdef_char_literal (cp_parser *parser)
4305 {
4306 cp_token *token = cp_lexer_consume_token (parser->lexer);
4307 tree literal = token->u.value;
4308 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4309 tree value = USERDEF_LITERAL_VALUE (literal);
4310 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4311 tree decl, result;
4312
4313 /* Build up a call to the user-defined operator */
4314 /* Lookup the name we got back from the id-expression. */
4315 vec<tree, va_gc> *args = make_tree_vector ();
4316 vec_safe_push (args, value);
4317 decl = lookup_literal_operator (name, args);
4318 if (!decl || decl == error_mark_node)
4319 {
4320 error ("unable to find character literal operator %qD with %qT argument",
4321 name, TREE_TYPE (value));
4322 release_tree_vector (args);
4323 return error_mark_node;
4324 }
4325 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4326 release_tree_vector (args);
4327 return result;
4328 }
4329
4330 /* A subroutine of cp_parser_userdef_numeric_literal to
4331 create a char... template parameter pack from a string node. */
4332
4333 static tree
4334 make_char_string_pack (tree value)
4335 {
4336 tree charvec;
4337 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4338 const char *str = TREE_STRING_POINTER (value);
4339 int i, len = TREE_STRING_LENGTH (value) - 1;
4340 tree argvec = make_tree_vec (1);
4341
4342 /* Fill in CHARVEC with all of the parameters. */
4343 charvec = make_tree_vec (len);
4344 for (i = 0; i < len; ++i)
4345 {
4346 unsigned char s[3] = { '\'', str[i], '\'' };
4347 cpp_string in = { 3, s };
4348 cpp_string out = { 0, 0 };
4349 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4350 return NULL_TREE;
4351 gcc_assert (out.len == 2);
4352 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4353 out.text[0]);
4354 }
4355
4356 /* Build the argument packs. */
4357 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4358
4359 TREE_VEC_ELT (argvec, 0) = argpack;
4360
4361 return argvec;
4362 }
4363
4364 /* A subroutine of cp_parser_userdef_numeric_literal to
4365 create a char... template parameter pack from a string node. */
4366
4367 static tree
4368 make_string_pack (tree value)
4369 {
4370 tree charvec;
4371 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4372 const unsigned char *str
4373 = (const unsigned char *) TREE_STRING_POINTER (value);
4374 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4375 int len = TREE_STRING_LENGTH (value) / sz - 1;
4376 tree argvec = make_tree_vec (2);
4377
4378 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4379 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4380
4381 /* First template parm is character type. */
4382 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4383
4384 /* Fill in CHARVEC with all of the parameters. */
4385 charvec = make_tree_vec (len);
4386 for (int i = 0; i < len; ++i)
4387 TREE_VEC_ELT (charvec, i)
4388 = double_int_to_tree (str_char_type_node,
4389 double_int::from_buffer (str + i * sz, sz));
4390
4391 /* Build the argument packs. */
4392 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4393
4394 TREE_VEC_ELT (argvec, 1) = argpack;
4395
4396 return argvec;
4397 }
4398
4399 /* Parse a user-defined numeric constant. returns a call to a user-defined
4400 literal operator. */
4401
4402 static cp_expr
4403 cp_parser_userdef_numeric_literal (cp_parser *parser)
4404 {
4405 cp_token *token = cp_lexer_consume_token (parser->lexer);
4406 tree literal = token->u.value;
4407 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4408 tree value = USERDEF_LITERAL_VALUE (literal);
4409 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4410 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4411 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4412 tree decl, result;
4413 vec<tree, va_gc> *args;
4414
4415 /* Look for a literal operator taking the exact type of numeric argument
4416 as the literal value. */
4417 args = make_tree_vector ();
4418 vec_safe_push (args, value);
4419 decl = lookup_literal_operator (name, args);
4420 if (decl && decl != error_mark_node)
4421 {
4422 result = finish_call_expr (decl, &args, false, true,
4423 tf_warning_or_error);
4424
4425 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4426 {
4427 warning_at (token->location, OPT_Woverflow,
4428 "integer literal exceeds range of %qT type",
4429 long_long_unsigned_type_node);
4430 }
4431 else
4432 {
4433 if (overflow > 0)
4434 warning_at (token->location, OPT_Woverflow,
4435 "floating literal exceeds range of %qT type",
4436 long_double_type_node);
4437 else if (overflow < 0)
4438 warning_at (token->location, OPT_Woverflow,
4439 "floating literal truncated to zero");
4440 }
4441
4442 release_tree_vector (args);
4443 return result;
4444 }
4445 release_tree_vector (args);
4446
4447 /* If the numeric argument didn't work, look for a raw literal
4448 operator taking a const char* argument consisting of the number
4449 in string format. */
4450 args = make_tree_vector ();
4451 vec_safe_push (args, num_string);
4452 decl = lookup_literal_operator (name, args);
4453 if (decl && decl != error_mark_node)
4454 {
4455 result = finish_call_expr (decl, &args, false, true,
4456 tf_warning_or_error);
4457 release_tree_vector (args);
4458 return result;
4459 }
4460 release_tree_vector (args);
4461
4462 /* If the raw literal didn't work, look for a non-type template
4463 function with parameter pack char.... Call the function with
4464 template parameter characters representing the number. */
4465 args = make_tree_vector ();
4466 decl = lookup_literal_operator (name, args);
4467 if (decl && decl != error_mark_node)
4468 {
4469 tree tmpl_args = make_char_string_pack (num_string);
4470 if (tmpl_args == NULL_TREE)
4471 {
4472 error ("failed to translate literal to execution character set %qT",
4473 num_string);
4474 return error_mark_node;
4475 }
4476 decl = lookup_template_function (decl, tmpl_args);
4477 result = finish_call_expr (decl, &args, false, true,
4478 tf_warning_or_error);
4479 release_tree_vector (args);
4480 return result;
4481 }
4482
4483 release_tree_vector (args);
4484
4485 /* In C++14 the standard library defines complex number suffixes that
4486 conflict with GNU extensions. Prefer them if <complex> is #included. */
4487 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4488 bool i14 = (cxx_dialect > cxx11
4489 && (id_equal (suffix_id, "i")
4490 || id_equal (suffix_id, "if")
4491 || id_equal (suffix_id, "il")));
4492 diagnostic_t kind = DK_ERROR;
4493 int opt = 0;
4494
4495 if (i14 && ext)
4496 {
4497 tree cxlit = lookup_qualified_name (std_node,
4498 get_identifier ("complex_literals"),
4499 0, false, false);
4500 if (cxlit == error_mark_node)
4501 {
4502 /* No <complex>, so pedwarn and use GNU semantics. */
4503 kind = DK_PEDWARN;
4504 opt = OPT_Wpedantic;
4505 }
4506 }
4507
4508 bool complained
4509 = emit_diagnostic (kind, input_location, opt,
4510 "unable to find numeric literal operator %qD", name);
4511
4512 if (!complained)
4513 /* Don't inform either. */;
4514 else if (i14)
4515 {
4516 inform (token->location, "add %<using namespace std::complex_literals%> "
4517 "(from <complex>) to enable the C++14 user-defined literal "
4518 "suffixes");
4519 if (ext)
4520 inform (token->location, "or use %<j%> instead of %<i%> for the "
4521 "GNU built-in suffix");
4522 }
4523 else if (!ext)
4524 inform (token->location, "use -fext-numeric-literals "
4525 "to enable more built-in suffixes");
4526
4527 if (kind == DK_ERROR)
4528 value = error_mark_node;
4529 else
4530 {
4531 /* Use the built-in semantics. */
4532 tree type;
4533 if (id_equal (suffix_id, "i"))
4534 {
4535 if (TREE_CODE (value) == INTEGER_CST)
4536 type = integer_type_node;
4537 else
4538 type = double_type_node;
4539 }
4540 else if (id_equal (suffix_id, "if"))
4541 type = float_type_node;
4542 else /* if (id_equal (suffix_id, "il")) */
4543 type = long_double_type_node;
4544
4545 value = build_complex (build_complex_type (type),
4546 fold_convert (type, integer_zero_node),
4547 fold_convert (type, value));
4548 }
4549
4550 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4551 /* Avoid repeated diagnostics. */
4552 token->u.value = value;
4553 return value;
4554 }
4555
4556 /* Parse a user-defined string constant. Returns a call to a user-defined
4557 literal operator taking a character pointer and the length of the string
4558 as arguments. */
4559
4560 static tree
4561 cp_parser_userdef_string_literal (tree literal)
4562 {
4563 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4564 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4565 tree value = USERDEF_LITERAL_VALUE (literal);
4566 int len = TREE_STRING_LENGTH (value)
4567 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4568 tree decl, result;
4569 vec<tree, va_gc> *args;
4570
4571 /* Build up a call to the user-defined operator. */
4572 /* Lookup the name we got back from the id-expression. */
4573 args = make_tree_vector ();
4574 vec_safe_push (args, value);
4575 vec_safe_push (args, build_int_cst (size_type_node, len));
4576 decl = lookup_literal_operator (name, args);
4577
4578 if (decl && decl != error_mark_node)
4579 {
4580 result = finish_call_expr (decl, &args, false, true,
4581 tf_warning_or_error);
4582 release_tree_vector (args);
4583 return result;
4584 }
4585 release_tree_vector (args);
4586
4587 /* Look for a template function with typename parameter CharT
4588 and parameter pack CharT... Call the function with
4589 template parameter characters representing the string. */
4590 args = make_tree_vector ();
4591 decl = lookup_literal_operator (name, args);
4592 if (decl && decl != error_mark_node)
4593 {
4594 tree tmpl_args = make_string_pack (value);
4595 decl = lookup_template_function (decl, tmpl_args);
4596 result = finish_call_expr (decl, &args, false, true,
4597 tf_warning_or_error);
4598 release_tree_vector (args);
4599 return result;
4600 }
4601 release_tree_vector (args);
4602
4603 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4604 name, TREE_TYPE (value), size_type_node);
4605 return error_mark_node;
4606 }
4607
4608
4609 /* Basic concepts [gram.basic] */
4610
4611 /* Parse a translation-unit.
4612
4613 translation-unit:
4614 declaration-seq [opt] */
4615
4616 static void
4617 cp_parser_translation_unit (cp_parser* parser)
4618 {
4619 gcc_checking_assert (!cp_error_declarator);
4620
4621 /* Create the declarator obstack. */
4622 gcc_obstack_init (&declarator_obstack);
4623 /* Create the error declarator. */
4624 cp_error_declarator = make_declarator (cdk_error);
4625 /* Create the empty parameter list. */
4626 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4627 UNKNOWN_LOCATION);
4628 /* Remember where the base of the declarator obstack lies. */
4629 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4630
4631 bool implicit_extern_c = false;
4632
4633 for (;;)
4634 {
4635 cp_token *token = cp_lexer_peek_token (parser->lexer);
4636
4637 /* If we're entering or exiting a region that's implicitly
4638 extern "C", modify the lang context appropriately. */
4639 if (implicit_extern_c
4640 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4641 {
4642 implicit_extern_c = !implicit_extern_c;
4643 if (implicit_extern_c)
4644 push_lang_context (lang_name_c);
4645 else
4646 pop_lang_context ();
4647 }
4648
4649 if (token->type == CPP_EOF)
4650 break;
4651
4652 if (token->type == CPP_CLOSE_BRACE)
4653 {
4654 cp_parser_error (parser, "expected declaration");
4655 cp_lexer_consume_token (parser->lexer);
4656 /* If the next token is now a `;', consume it. */
4657 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4658 cp_lexer_consume_token (parser->lexer);
4659 }
4660 else
4661 cp_parser_toplevel_declaration (parser);
4662 }
4663
4664 /* Get rid of the token array; we don't need it any more. */
4665 cp_lexer_destroy (parser->lexer);
4666 parser->lexer = NULL;
4667
4668 /* The EOF should have reset this. */
4669 gcc_checking_assert (!implicit_extern_c);
4670
4671 /* Make sure the declarator obstack was fully cleaned up. */
4672 gcc_assert (obstack_next_free (&declarator_obstack)
4673 == declarator_obstack_base);
4674 }
4675
4676 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4677 decltype context. */
4678
4679 static inline tsubst_flags_t
4680 complain_flags (bool decltype_p)
4681 {
4682 tsubst_flags_t complain = tf_warning_or_error;
4683 if (decltype_p)
4684 complain |= tf_decltype;
4685 return complain;
4686 }
4687
4688 /* We're about to parse a collection of statements. If we're currently
4689 parsing tentatively, set up a firewall so that any nested
4690 cp_parser_commit_to_tentative_parse won't affect the current context. */
4691
4692 static cp_token_position
4693 cp_parser_start_tentative_firewall (cp_parser *parser)
4694 {
4695 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4696 return 0;
4697
4698 cp_parser_parse_tentatively (parser);
4699 cp_parser_commit_to_topmost_tentative_parse (parser);
4700 return cp_lexer_token_position (parser->lexer, false);
4701 }
4702
4703 /* We've finished parsing the collection of statements. Wrap up the
4704 firewall and replace the relevant tokens with the parsed form. */
4705
4706 static void
4707 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4708 tree expr)
4709 {
4710 if (!start)
4711 return;
4712
4713 /* Finish the firewall level. */
4714 cp_parser_parse_definitely (parser);
4715 /* And remember the result of the parse for when we try again. */
4716 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4717 token->type = CPP_PREPARSED_EXPR;
4718 token->u.value = expr;
4719 token->keyword = RID_MAX;
4720 cp_lexer_purge_tokens_after (parser->lexer, start);
4721 }
4722
4723 /* Like the above functions, but let the user modify the tokens. Used by
4724 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4725 later parses, so it makes sense to localize the effects of
4726 cp_parser_commit_to_tentative_parse. */
4727
4728 struct tentative_firewall
4729 {
4730 cp_parser *parser;
4731 bool set;
4732
4733 tentative_firewall (cp_parser *p): parser(p)
4734 {
4735 /* If we're currently parsing tentatively, start a committed level as a
4736 firewall and then an inner tentative parse. */
4737 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4738 {
4739 cp_parser_parse_tentatively (parser);
4740 cp_parser_commit_to_topmost_tentative_parse (parser);
4741 cp_parser_parse_tentatively (parser);
4742 }
4743 }
4744
4745 ~tentative_firewall()
4746 {
4747 if (set)
4748 {
4749 /* Finish the inner tentative parse and the firewall, propagating any
4750 uncommitted error state to the outer tentative parse. */
4751 bool err = cp_parser_error_occurred (parser);
4752 cp_parser_parse_definitely (parser);
4753 cp_parser_parse_definitely (parser);
4754 if (err)
4755 cp_parser_simulate_error (parser);
4756 }
4757 }
4758 };
4759
4760 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4761 This class is for tracking such a matching pair of symbols.
4762 In particular, it tracks the location of the first token,
4763 so that if the second token is missing, we can highlight the
4764 location of the first token when notifying the user about the
4765 problem. */
4766
4767 template <typename traits_t>
4768 class token_pair
4769 {
4770 public:
4771 /* token_pair's ctor. */
4772 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4773
4774 /* If the next token is the opening symbol for this pair, consume it and
4775 return true.
4776 Otherwise, issue an error and return false.
4777 In either case, record the location of the opening token. */
4778
4779 bool require_open (cp_parser *parser)
4780 {
4781 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4782 return cp_parser_require (parser, traits_t::open_token_type,
4783 traits_t::required_token_open);
4784 }
4785
4786 /* Consume the next token from PARSER, recording its location as
4787 that of the opening token within the pair. */
4788
4789 cp_token * consume_open (cp_parser *parser)
4790 {
4791 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4792 gcc_assert (tok->type == traits_t::open_token_type);
4793 m_open_loc = tok->location;
4794 return tok;
4795 }
4796
4797 /* If the next token is the closing symbol for this pair, consume it
4798 and return it.
4799 Otherwise, issue an error, highlighting the location of the
4800 corresponding opening token, and return NULL. */
4801
4802 cp_token *require_close (cp_parser *parser) const
4803 {
4804 return cp_parser_require (parser, traits_t::close_token_type,
4805 traits_t::required_token_close,
4806 m_open_loc);
4807 }
4808
4809 private:
4810 location_t m_open_loc;
4811 };
4812
4813 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4814
4815 struct matching_paren_traits
4816 {
4817 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4818 static const enum required_token required_token_open = RT_OPEN_PAREN;
4819 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4820 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4821 };
4822
4823 /* "matching_parens" is a token_pair<T> class for tracking matching
4824 pairs of parentheses. */
4825
4826 typedef token_pair<matching_paren_traits> matching_parens;
4827
4828 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4829
4830 struct matching_brace_traits
4831 {
4832 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4833 static const enum required_token required_token_open = RT_OPEN_BRACE;
4834 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4835 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4836 };
4837
4838 /* "matching_braces" is a token_pair<T> class for tracking matching
4839 pairs of braces. */
4840
4841 typedef token_pair<matching_brace_traits> matching_braces;
4842
4843
4844 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4845 enclosing parentheses. */
4846
4847 static cp_expr
4848 cp_parser_statement_expr (cp_parser *parser)
4849 {
4850 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4851
4852 /* Consume the '('. */
4853 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4854 matching_parens parens;
4855 parens.consume_open (parser);
4856 /* Start the statement-expression. */
4857 tree expr = begin_stmt_expr ();
4858 /* Parse the compound-statement. */
4859 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4860 /* Finish up. */
4861 expr = finish_stmt_expr (expr, false);
4862 /* Consume the ')'. */
4863 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4864 if (!parens.require_close (parser))
4865 cp_parser_skip_to_end_of_statement (parser);
4866
4867 cp_parser_end_tentative_firewall (parser, start, expr);
4868 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4869 return cp_expr (expr, combined_loc);
4870 }
4871
4872 /* Expressions [gram.expr] */
4873
4874 /* Parse a fold-operator.
4875
4876 fold-operator:
4877 - * / % ^ & | = < > << >>
4878 = -= *= /= %= ^= &= |= <<= >>=
4879 == != <= >= && || , .* ->*
4880
4881 This returns the tree code corresponding to the matched operator
4882 as an int. When the current token matches a compound assignment
4883 opertor, the resulting tree code is the negative value of the
4884 non-assignment operator. */
4885
4886 static int
4887 cp_parser_fold_operator (cp_token *token)
4888 {
4889 switch (token->type)
4890 {
4891 case CPP_PLUS: return PLUS_EXPR;
4892 case CPP_MINUS: return MINUS_EXPR;
4893 case CPP_MULT: return MULT_EXPR;
4894 case CPP_DIV: return TRUNC_DIV_EXPR;
4895 case CPP_MOD: return TRUNC_MOD_EXPR;
4896 case CPP_XOR: return BIT_XOR_EXPR;
4897 case CPP_AND: return BIT_AND_EXPR;
4898 case CPP_OR: return BIT_IOR_EXPR;
4899 case CPP_LSHIFT: return LSHIFT_EXPR;
4900 case CPP_RSHIFT: return RSHIFT_EXPR;
4901
4902 case CPP_EQ: return -NOP_EXPR;
4903 case CPP_PLUS_EQ: return -PLUS_EXPR;
4904 case CPP_MINUS_EQ: return -MINUS_EXPR;
4905 case CPP_MULT_EQ: return -MULT_EXPR;
4906 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4907 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4908 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4909 case CPP_AND_EQ: return -BIT_AND_EXPR;
4910 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4911 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4912 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4913
4914 case CPP_EQ_EQ: return EQ_EXPR;
4915 case CPP_NOT_EQ: return NE_EXPR;
4916 case CPP_LESS: return LT_EXPR;
4917 case CPP_GREATER: return GT_EXPR;
4918 case CPP_LESS_EQ: return LE_EXPR;
4919 case CPP_GREATER_EQ: return GE_EXPR;
4920
4921 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4922 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4923
4924 case CPP_COMMA: return COMPOUND_EXPR;
4925
4926 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4927 case CPP_DEREF_STAR: return MEMBER_REF;
4928
4929 default: return ERROR_MARK;
4930 }
4931 }
4932
4933 /* Returns true if CODE indicates a binary expression, which is not allowed in
4934 the LHS of a fold-expression. More codes will need to be added to use this
4935 function in other contexts. */
4936
4937 static bool
4938 is_binary_op (tree_code code)
4939 {
4940 switch (code)
4941 {
4942 case PLUS_EXPR:
4943 case POINTER_PLUS_EXPR:
4944 case MINUS_EXPR:
4945 case MULT_EXPR:
4946 case TRUNC_DIV_EXPR:
4947 case TRUNC_MOD_EXPR:
4948 case BIT_XOR_EXPR:
4949 case BIT_AND_EXPR:
4950 case BIT_IOR_EXPR:
4951 case LSHIFT_EXPR:
4952 case RSHIFT_EXPR:
4953
4954 case MODOP_EXPR:
4955
4956 case EQ_EXPR:
4957 case NE_EXPR:
4958 case LE_EXPR:
4959 case GE_EXPR:
4960 case LT_EXPR:
4961 case GT_EXPR:
4962
4963 case TRUTH_ANDIF_EXPR:
4964 case TRUTH_ORIF_EXPR:
4965
4966 case COMPOUND_EXPR:
4967
4968 case DOTSTAR_EXPR:
4969 case MEMBER_REF:
4970 return true;
4971
4972 default:
4973 return false;
4974 }
4975 }
4976
4977 /* If the next token is a suitable fold operator, consume it and return as
4978 the function above. */
4979
4980 static int
4981 cp_parser_fold_operator (cp_parser *parser)
4982 {
4983 cp_token* token = cp_lexer_peek_token (parser->lexer);
4984 int code = cp_parser_fold_operator (token);
4985 if (code != ERROR_MARK)
4986 cp_lexer_consume_token (parser->lexer);
4987 return code;
4988 }
4989
4990 /* Parse a fold-expression.
4991
4992 fold-expression:
4993 ( ... folding-operator cast-expression)
4994 ( cast-expression folding-operator ... )
4995 ( cast-expression folding operator ... folding-operator cast-expression)
4996
4997 Note that the '(' and ')' are matched in primary expression. */
4998
4999 static cp_expr
5000 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5001 {
5002 cp_id_kind pidk;
5003
5004 // Left fold.
5005 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5006 {
5007 cp_lexer_consume_token (parser->lexer);
5008 int op = cp_parser_fold_operator (parser);
5009 if (op == ERROR_MARK)
5010 {
5011 cp_parser_error (parser, "expected binary operator");
5012 return error_mark_node;
5013 }
5014
5015 tree expr = cp_parser_cast_expression (parser, false, false,
5016 false, &pidk);
5017 if (expr == error_mark_node)
5018 return error_mark_node;
5019 return finish_left_unary_fold_expr (expr, op);
5020 }
5021
5022 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5023 int op = cp_parser_fold_operator (parser);
5024 if (op == ERROR_MARK)
5025 {
5026 cp_parser_error (parser, "expected binary operator");
5027 return error_mark_node;
5028 }
5029
5030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5031 {
5032 cp_parser_error (parser, "expected ...");
5033 return error_mark_node;
5034 }
5035 cp_lexer_consume_token (parser->lexer);
5036
5037 /* The operands of a fold-expression are cast-expressions, so binary or
5038 conditional expressions are not allowed. We check this here to avoid
5039 tentative parsing. */
5040 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5041 /* OK, the expression was parenthesized. */;
5042 else if (is_binary_op (TREE_CODE (expr1)))
5043 error_at (location_of (expr1),
5044 "binary expression in operand of fold-expression");
5045 else if (TREE_CODE (expr1) == COND_EXPR
5046 || (REFERENCE_REF_P (expr1)
5047 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5048 error_at (location_of (expr1),
5049 "conditional expression in operand of fold-expression");
5050
5051 // Right fold.
5052 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5053 return finish_right_unary_fold_expr (expr1, op);
5054
5055 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5056 {
5057 cp_parser_error (parser, "mismatched operator in fold-expression");
5058 return error_mark_node;
5059 }
5060 cp_lexer_consume_token (parser->lexer);
5061
5062 // Binary left or right fold.
5063 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5064 if (expr2 == error_mark_node)
5065 return error_mark_node;
5066 return finish_binary_fold_expr (expr1, expr2, op);
5067 }
5068
5069 /* Parse a primary-expression.
5070
5071 primary-expression:
5072 literal
5073 this
5074 ( expression )
5075 id-expression
5076 lambda-expression (C++11)
5077
5078 GNU Extensions:
5079
5080 primary-expression:
5081 ( compound-statement )
5082 __builtin_va_arg ( assignment-expression , type-id )
5083 __builtin_offsetof ( type-id , offsetof-expression )
5084
5085 C++ Extensions:
5086 __has_nothrow_assign ( type-id )
5087 __has_nothrow_constructor ( type-id )
5088 __has_nothrow_copy ( type-id )
5089 __has_trivial_assign ( type-id )
5090 __has_trivial_constructor ( type-id )
5091 __has_trivial_copy ( type-id )
5092 __has_trivial_destructor ( type-id )
5093 __has_virtual_destructor ( type-id )
5094 __is_abstract ( type-id )
5095 __is_base_of ( type-id , type-id )
5096 __is_class ( type-id )
5097 __is_empty ( type-id )
5098 __is_enum ( type-id )
5099 __is_final ( type-id )
5100 __is_literal_type ( type-id )
5101 __is_pod ( type-id )
5102 __is_polymorphic ( type-id )
5103 __is_std_layout ( type-id )
5104 __is_trivial ( type-id )
5105 __is_union ( type-id )
5106
5107 Objective-C++ Extension:
5108
5109 primary-expression:
5110 objc-expression
5111
5112 literal:
5113 __null
5114
5115 ADDRESS_P is true iff this expression was immediately preceded by
5116 "&" and therefore might denote a pointer-to-member. CAST_P is true
5117 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5118 true iff this expression is a template argument.
5119
5120 Returns a representation of the expression. Upon return, *IDK
5121 indicates what kind of id-expression (if any) was present. */
5122
5123 static cp_expr
5124 cp_parser_primary_expression (cp_parser *parser,
5125 bool address_p,
5126 bool cast_p,
5127 bool template_arg_p,
5128 bool decltype_p,
5129 cp_id_kind *idk)
5130 {
5131 cp_token *token = NULL;
5132
5133 /* Assume the primary expression is not an id-expression. */
5134 *idk = CP_ID_KIND_NONE;
5135
5136 /* Peek at the next token. */
5137 token = cp_lexer_peek_token (parser->lexer);
5138 switch ((int) token->type)
5139 {
5140 /* literal:
5141 integer-literal
5142 character-literal
5143 floating-literal
5144 string-literal
5145 boolean-literal
5146 pointer-literal
5147 user-defined-literal */
5148 case CPP_CHAR:
5149 case CPP_CHAR16:
5150 case CPP_CHAR32:
5151 case CPP_WCHAR:
5152 case CPP_UTF8CHAR:
5153 case CPP_NUMBER:
5154 case CPP_PREPARSED_EXPR:
5155 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5156 return cp_parser_userdef_numeric_literal (parser);
5157 token = cp_lexer_consume_token (parser->lexer);
5158 if (TREE_CODE (token->u.value) == FIXED_CST)
5159 {
5160 error_at (token->location,
5161 "fixed-point types not supported in C++");
5162 return error_mark_node;
5163 }
5164 /* Floating-point literals are only allowed in an integral
5165 constant expression if they are cast to an integral or
5166 enumeration type. */
5167 if (TREE_CODE (token->u.value) == REAL_CST
5168 && parser->integral_constant_expression_p
5169 && pedantic)
5170 {
5171 /* CAST_P will be set even in invalid code like "int(2.7 +
5172 ...)". Therefore, we have to check that the next token
5173 is sure to end the cast. */
5174 if (cast_p)
5175 {
5176 cp_token *next_token;
5177
5178 next_token = cp_lexer_peek_token (parser->lexer);
5179 if (/* The comma at the end of an
5180 enumerator-definition. */
5181 next_token->type != CPP_COMMA
5182 /* The curly brace at the end of an enum-specifier. */
5183 && next_token->type != CPP_CLOSE_BRACE
5184 /* The end of a statement. */
5185 && next_token->type != CPP_SEMICOLON
5186 /* The end of the cast-expression. */
5187 && next_token->type != CPP_CLOSE_PAREN
5188 /* The end of an array bound. */
5189 && next_token->type != CPP_CLOSE_SQUARE
5190 /* The closing ">" in a template-argument-list. */
5191 && (next_token->type != CPP_GREATER
5192 || parser->greater_than_is_operator_p)
5193 /* C++0x only: A ">>" treated like two ">" tokens,
5194 in a template-argument-list. */
5195 && (next_token->type != CPP_RSHIFT
5196 || (cxx_dialect == cxx98)
5197 || parser->greater_than_is_operator_p))
5198 cast_p = false;
5199 }
5200
5201 /* If we are within a cast, then the constraint that the
5202 cast is to an integral or enumeration type will be
5203 checked at that point. If we are not within a cast, then
5204 this code is invalid. */
5205 if (!cast_p)
5206 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5207 }
5208 return cp_expr (token->u.value, token->location);
5209
5210 case CPP_CHAR_USERDEF:
5211 case CPP_CHAR16_USERDEF:
5212 case CPP_CHAR32_USERDEF:
5213 case CPP_WCHAR_USERDEF:
5214 case CPP_UTF8CHAR_USERDEF:
5215 return cp_parser_userdef_char_literal (parser);
5216
5217 case CPP_STRING:
5218 case CPP_STRING16:
5219 case CPP_STRING32:
5220 case CPP_WSTRING:
5221 case CPP_UTF8STRING:
5222 case CPP_STRING_USERDEF:
5223 case CPP_STRING16_USERDEF:
5224 case CPP_STRING32_USERDEF:
5225 case CPP_WSTRING_USERDEF:
5226 case CPP_UTF8STRING_USERDEF:
5227 /* ??? Should wide strings be allowed when parser->translate_strings_p
5228 is false (i.e. in attributes)? If not, we can kill the third
5229 argument to cp_parser_string_literal. */
5230 return cp_parser_string_literal (parser,
5231 parser->translate_strings_p,
5232 true);
5233
5234 case CPP_OPEN_PAREN:
5235 /* If we see `( { ' then we are looking at the beginning of
5236 a GNU statement-expression. */
5237 if (cp_parser_allow_gnu_extensions_p (parser)
5238 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5239 {
5240 /* Statement-expressions are not allowed by the standard. */
5241 pedwarn (token->location, OPT_Wpedantic,
5242 "ISO C++ forbids braced-groups within expressions");
5243
5244 /* And they're not allowed outside of a function-body; you
5245 cannot, for example, write:
5246
5247 int i = ({ int j = 3; j + 1; });
5248
5249 at class or namespace scope. */
5250 if (!parser->in_function_body
5251 || parser->in_template_argument_list_p)
5252 {
5253 error_at (token->location,
5254 "statement-expressions are not allowed outside "
5255 "functions nor in template-argument lists");
5256 cp_parser_skip_to_end_of_block_or_statement (parser);
5257 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5258 cp_lexer_consume_token (parser->lexer);
5259 return error_mark_node;
5260 }
5261 else
5262 return cp_parser_statement_expr (parser);
5263 }
5264 /* Otherwise it's a normal parenthesized expression. */
5265 {
5266 cp_expr expr;
5267 bool saved_greater_than_is_operator_p;
5268
5269 location_t open_paren_loc = token->location;
5270
5271 /* Consume the `('. */
5272 matching_parens parens;
5273 parens.consume_open (parser);
5274 /* Within a parenthesized expression, a `>' token is always
5275 the greater-than operator. */
5276 saved_greater_than_is_operator_p
5277 = parser->greater_than_is_operator_p;
5278 parser->greater_than_is_operator_p = true;
5279
5280 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5281 /* Left fold expression. */
5282 expr = NULL_TREE;
5283 else
5284 /* Parse the parenthesized expression. */
5285 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5286
5287 token = cp_lexer_peek_token (parser->lexer);
5288 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5289 {
5290 expr = cp_parser_fold_expression (parser, expr);
5291 if (expr != error_mark_node
5292 && cxx_dialect < cxx17
5293 && !in_system_header_at (input_location))
5294 pedwarn (input_location, 0, "fold-expressions only available "
5295 "with -std=c++17 or -std=gnu++17");
5296 }
5297 else
5298 /* Let the front end know that this expression was
5299 enclosed in parentheses. This matters in case, for
5300 example, the expression is of the form `A::B', since
5301 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5302 not. */
5303 expr = finish_parenthesized_expr (expr);
5304
5305 /* DR 705: Wrapping an unqualified name in parentheses
5306 suppresses arg-dependent lookup. We want to pass back
5307 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5308 (c++/37862), but none of the others. */
5309 if (*idk != CP_ID_KIND_QUALIFIED)
5310 *idk = CP_ID_KIND_NONE;
5311
5312 /* The `>' token might be the end of a template-id or
5313 template-parameter-list now. */
5314 parser->greater_than_is_operator_p
5315 = saved_greater_than_is_operator_p;
5316
5317 /* Consume the `)'. */
5318 token = cp_lexer_peek_token (parser->lexer);
5319 location_t close_paren_loc = token->location;
5320 expr.set_range (open_paren_loc, close_paren_loc);
5321 if (!parens.require_close (parser)
5322 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5323 cp_parser_skip_to_end_of_statement (parser);
5324
5325 return expr;
5326 }
5327
5328 case CPP_OPEN_SQUARE:
5329 {
5330 if (c_dialect_objc ())
5331 {
5332 /* We might have an Objective-C++ message. */
5333 cp_parser_parse_tentatively (parser);
5334 tree msg = cp_parser_objc_message_expression (parser);
5335 /* If that works out, we're done ... */
5336 if (cp_parser_parse_definitely (parser))
5337 return msg;
5338 /* ... else, fall though to see if it's a lambda. */
5339 }
5340 cp_expr lam = cp_parser_lambda_expression (parser);
5341 /* Don't warn about a failed tentative parse. */
5342 if (cp_parser_error_occurred (parser))
5343 return error_mark_node;
5344 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5345 return lam;
5346 }
5347
5348 case CPP_OBJC_STRING:
5349 if (c_dialect_objc ())
5350 /* We have an Objective-C++ string literal. */
5351 return cp_parser_objc_expression (parser);
5352 cp_parser_error (parser, "expected primary-expression");
5353 return error_mark_node;
5354
5355 case CPP_KEYWORD:
5356 switch (token->keyword)
5357 {
5358 /* These two are the boolean literals. */
5359 case RID_TRUE:
5360 cp_lexer_consume_token (parser->lexer);
5361 return cp_expr (boolean_true_node, token->location);
5362 case RID_FALSE:
5363 cp_lexer_consume_token (parser->lexer);
5364 return cp_expr (boolean_false_node, token->location);
5365
5366 /* The `__null' literal. */
5367 case RID_NULL:
5368 cp_lexer_consume_token (parser->lexer);
5369 return cp_expr (null_node, token->location);
5370
5371 /* The `nullptr' literal. */
5372 case RID_NULLPTR:
5373 cp_lexer_consume_token (parser->lexer);
5374 return cp_expr (nullptr_node, token->location);
5375
5376 /* Recognize the `this' keyword. */
5377 case RID_THIS:
5378 cp_lexer_consume_token (parser->lexer);
5379 if (parser->local_variables_forbidden_p)
5380 {
5381 error_at (token->location,
5382 "%<this%> may not be used in this context");
5383 return error_mark_node;
5384 }
5385 /* Pointers cannot appear in constant-expressions. */
5386 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5387 return error_mark_node;
5388 return cp_expr (finish_this_expr (), token->location);
5389
5390 /* The `operator' keyword can be the beginning of an
5391 id-expression. */
5392 case RID_OPERATOR:
5393 goto id_expression;
5394
5395 case RID_FUNCTION_NAME:
5396 case RID_PRETTY_FUNCTION_NAME:
5397 case RID_C99_FUNCTION_NAME:
5398 {
5399 non_integral_constant name;
5400
5401 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5402 __func__ are the names of variables -- but they are
5403 treated specially. Therefore, they are handled here,
5404 rather than relying on the generic id-expression logic
5405 below. Grammatically, these names are id-expressions.
5406
5407 Consume the token. */
5408 token = cp_lexer_consume_token (parser->lexer);
5409
5410 switch (token->keyword)
5411 {
5412 case RID_FUNCTION_NAME:
5413 name = NIC_FUNC_NAME;
5414 break;
5415 case RID_PRETTY_FUNCTION_NAME:
5416 name = NIC_PRETTY_FUNC;
5417 break;
5418 case RID_C99_FUNCTION_NAME:
5419 name = NIC_C99_FUNC;
5420 break;
5421 default:
5422 gcc_unreachable ();
5423 }
5424
5425 if (cp_parser_non_integral_constant_expression (parser, name))
5426 return error_mark_node;
5427
5428 /* Look up the name. */
5429 return finish_fname (token->u.value);
5430 }
5431
5432 case RID_VA_ARG:
5433 {
5434 tree expression;
5435 tree type;
5436 source_location type_location;
5437 location_t start_loc
5438 = cp_lexer_peek_token (parser->lexer)->location;
5439 /* The `__builtin_va_arg' construct is used to handle
5440 `va_arg'. Consume the `__builtin_va_arg' token. */
5441 cp_lexer_consume_token (parser->lexer);
5442 /* Look for the opening `('. */
5443 matching_parens parens;
5444 parens.require_open (parser);
5445 /* Now, parse the assignment-expression. */
5446 expression = cp_parser_assignment_expression (parser);
5447 /* Look for the `,'. */
5448 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5449 type_location = cp_lexer_peek_token (parser->lexer)->location;
5450 /* Parse the type-id. */
5451 {
5452 type_id_in_expr_sentinel s (parser);
5453 type = cp_parser_type_id (parser);
5454 }
5455 /* Look for the closing `)'. */
5456 location_t finish_loc
5457 = cp_lexer_peek_token (parser->lexer)->location;
5458 parens.require_close (parser);
5459 /* Using `va_arg' in a constant-expression is not
5460 allowed. */
5461 if (cp_parser_non_integral_constant_expression (parser,
5462 NIC_VA_ARG))
5463 return error_mark_node;
5464 /* Construct a location of the form:
5465 __builtin_va_arg (v, int)
5466 ~~~~~~~~~~~~~~~~~~~~~^~~~
5467 with the caret at the type, ranging from the start of the
5468 "__builtin_va_arg" token to the close paren. */
5469 location_t combined_loc
5470 = make_location (type_location, start_loc, finish_loc);
5471 return build_x_va_arg (combined_loc, expression, type);
5472 }
5473
5474 case RID_OFFSETOF:
5475 return cp_parser_builtin_offsetof (parser);
5476
5477 case RID_HAS_NOTHROW_ASSIGN:
5478 case RID_HAS_NOTHROW_CONSTRUCTOR:
5479 case RID_HAS_NOTHROW_COPY:
5480 case RID_HAS_TRIVIAL_ASSIGN:
5481 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5482 case RID_HAS_TRIVIAL_COPY:
5483 case RID_HAS_TRIVIAL_DESTRUCTOR:
5484 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5485 case RID_HAS_VIRTUAL_DESTRUCTOR:
5486 case RID_IS_ABSTRACT:
5487 case RID_IS_AGGREGATE:
5488 case RID_IS_BASE_OF:
5489 case RID_IS_CLASS:
5490 case RID_IS_EMPTY:
5491 case RID_IS_ENUM:
5492 case RID_IS_FINAL:
5493 case RID_IS_LITERAL_TYPE:
5494 case RID_IS_POD:
5495 case RID_IS_POLYMORPHIC:
5496 case RID_IS_SAME_AS:
5497 case RID_IS_STD_LAYOUT:
5498 case RID_IS_TRIVIAL:
5499 case RID_IS_TRIVIALLY_ASSIGNABLE:
5500 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5501 case RID_IS_TRIVIALLY_COPYABLE:
5502 case RID_IS_UNION:
5503 case RID_IS_ASSIGNABLE:
5504 case RID_IS_CONSTRUCTIBLE:
5505 return cp_parser_trait_expr (parser, token->keyword);
5506
5507 // C++ concepts
5508 case RID_REQUIRES:
5509 return cp_parser_requires_expression (parser);
5510
5511 /* Objective-C++ expressions. */
5512 case RID_AT_ENCODE:
5513 case RID_AT_PROTOCOL:
5514 case RID_AT_SELECTOR:
5515 return cp_parser_objc_expression (parser);
5516
5517 case RID_TEMPLATE:
5518 if (parser->in_function_body
5519 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5520 == CPP_LESS))
5521 {
5522 error_at (token->location,
5523 "a template declaration cannot appear at block scope");
5524 cp_parser_skip_to_end_of_block_or_statement (parser);
5525 return error_mark_node;
5526 }
5527 /* FALLTHRU */
5528 default:
5529 cp_parser_error (parser, "expected primary-expression");
5530 return error_mark_node;
5531 }
5532
5533 /* An id-expression can start with either an identifier, a
5534 `::' as the beginning of a qualified-id, or the "operator"
5535 keyword. */
5536 case CPP_NAME:
5537 case CPP_SCOPE:
5538 case CPP_TEMPLATE_ID:
5539 case CPP_NESTED_NAME_SPECIFIER:
5540 {
5541 id_expression:
5542 cp_expr id_expression;
5543 cp_expr decl;
5544 const char *error_msg;
5545 bool template_p;
5546 bool done;
5547 cp_token *id_expr_token;
5548
5549 /* Parse the id-expression. */
5550 id_expression
5551 = cp_parser_id_expression (parser,
5552 /*template_keyword_p=*/false,
5553 /*check_dependency_p=*/true,
5554 &template_p,
5555 /*declarator_p=*/false,
5556 /*optional_p=*/false);
5557 if (id_expression == error_mark_node)
5558 return error_mark_node;
5559 id_expr_token = token;
5560 token = cp_lexer_peek_token (parser->lexer);
5561 done = (token->type != CPP_OPEN_SQUARE
5562 && token->type != CPP_OPEN_PAREN
5563 && token->type != CPP_DOT
5564 && token->type != CPP_DEREF
5565 && token->type != CPP_PLUS_PLUS
5566 && token->type != CPP_MINUS_MINUS);
5567 /* If we have a template-id, then no further lookup is
5568 required. If the template-id was for a template-class, we
5569 will sometimes have a TYPE_DECL at this point. */
5570 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5571 || TREE_CODE (id_expression) == TYPE_DECL)
5572 decl = id_expression;
5573 /* Look up the name. */
5574 else
5575 {
5576 tree ambiguous_decls;
5577
5578 /* If we already know that this lookup is ambiguous, then
5579 we've already issued an error message; there's no reason
5580 to check again. */
5581 if (id_expr_token->type == CPP_NAME
5582 && id_expr_token->error_reported)
5583 {
5584 cp_parser_simulate_error (parser);
5585 return error_mark_node;
5586 }
5587
5588 decl = cp_parser_lookup_name (parser, id_expression,
5589 none_type,
5590 template_p,
5591 /*is_namespace=*/false,
5592 /*check_dependency=*/true,
5593 &ambiguous_decls,
5594 id_expr_token->location);
5595 /* If the lookup was ambiguous, an error will already have
5596 been issued. */
5597 if (ambiguous_decls)
5598 return error_mark_node;
5599
5600 /* In Objective-C++, we may have an Objective-C 2.0
5601 dot-syntax for classes here. */
5602 if (c_dialect_objc ()
5603 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5604 && TREE_CODE (decl) == TYPE_DECL
5605 && objc_is_class_name (decl))
5606 {
5607 tree component;
5608 cp_lexer_consume_token (parser->lexer);
5609 component = cp_parser_identifier (parser);
5610 if (component == error_mark_node)
5611 return error_mark_node;
5612
5613 tree result = objc_build_class_component_ref (id_expression,
5614 component);
5615 /* Build a location of the form:
5616 expr.component
5617 ~~~~~^~~~~~~~~
5618 with caret at the start of the component name (at
5619 input_location), ranging from the start of the id_expression
5620 to the end of the component name. */
5621 location_t combined_loc
5622 = make_location (input_location, id_expression.get_start (),
5623 get_finish (input_location));
5624 protected_set_expr_location (result, combined_loc);
5625 return result;
5626 }
5627
5628 /* In Objective-C++, an instance variable (ivar) may be preferred
5629 to whatever cp_parser_lookup_name() found.
5630 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5631 rest of c-family, we have to do a little extra work to preserve
5632 any location information in cp_expr "decl". Given that
5633 objc_lookup_ivar is implemented in "c-family" and "objc", we
5634 have a trip through the pure "tree" type, rather than cp_expr.
5635 Naively copying it back to "decl" would implicitly give the
5636 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5637 store an EXPR_LOCATION. Hence we only update "decl" (and
5638 hence its location_t) if we get back a different tree node. */
5639 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5640 id_expression);
5641 if (decl_tree != decl.get_value ())
5642 decl = cp_expr (decl_tree);
5643
5644 /* If name lookup gives us a SCOPE_REF, then the
5645 qualifying scope was dependent. */
5646 if (TREE_CODE (decl) == SCOPE_REF)
5647 {
5648 /* At this point, we do not know if DECL is a valid
5649 integral constant expression. We assume that it is
5650 in fact such an expression, so that code like:
5651
5652 template <int N> struct A {
5653 int a[B<N>::i];
5654 };
5655
5656 is accepted. At template-instantiation time, we
5657 will check that B<N>::i is actually a constant. */
5658 return decl;
5659 }
5660 /* Check to see if DECL is a local variable in a context
5661 where that is forbidden. */
5662 if (parser->local_variables_forbidden_p
5663 && local_variable_p (decl))
5664 {
5665 error_at (id_expr_token->location,
5666 "local variable %qD may not appear in this context",
5667 decl.get_value ());
5668 return error_mark_node;
5669 }
5670 }
5671
5672 if (processing_template_decl)
5673 if (tree fns = maybe_get_fns (decl))
5674 /* It's too difficult to mark ths in all the places where
5675 we know for sure we need to keep the lookup, so do it
5676 now. The cost is extra GC to recycle the lookups
5677 resolved at parse time. */
5678 lookup_keep (fns);
5679
5680 decl = (finish_id_expression
5681 (id_expression, decl, parser->scope,
5682 idk,
5683 parser->integral_constant_expression_p,
5684 parser->allow_non_integral_constant_expression_p,
5685 &parser->non_integral_constant_expression_p,
5686 template_p, done, address_p,
5687 template_arg_p,
5688 &error_msg,
5689 id_expression.get_location ()));
5690 if (error_msg)
5691 cp_parser_error (parser, error_msg);
5692 decl.set_location (id_expr_token->location);
5693 return decl;
5694 }
5695
5696 /* Anything else is an error. */
5697 default:
5698 cp_parser_error (parser, "expected primary-expression");
5699 return error_mark_node;
5700 }
5701 }
5702
5703 static inline cp_expr
5704 cp_parser_primary_expression (cp_parser *parser,
5705 bool address_p,
5706 bool cast_p,
5707 bool template_arg_p,
5708 cp_id_kind *idk)
5709 {
5710 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5711 /*decltype*/false, idk);
5712 }
5713
5714 /* Parse an id-expression.
5715
5716 id-expression:
5717 unqualified-id
5718 qualified-id
5719
5720 qualified-id:
5721 :: [opt] nested-name-specifier template [opt] unqualified-id
5722 :: identifier
5723 :: operator-function-id
5724 :: template-id
5725
5726 Return a representation of the unqualified portion of the
5727 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5728 a `::' or nested-name-specifier.
5729
5730 Often, if the id-expression was a qualified-id, the caller will
5731 want to make a SCOPE_REF to represent the qualified-id. This
5732 function does not do this in order to avoid wastefully creating
5733 SCOPE_REFs when they are not required.
5734
5735 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5736 `template' keyword.
5737
5738 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5739 uninstantiated templates.
5740
5741 If *TEMPLATE_P is non-NULL, it is set to true iff the
5742 `template' keyword is used to explicitly indicate that the entity
5743 named is a template.
5744
5745 If DECLARATOR_P is true, the id-expression is appearing as part of
5746 a declarator, rather than as part of an expression. */
5747
5748 static cp_expr
5749 cp_parser_id_expression (cp_parser *parser,
5750 bool template_keyword_p,
5751 bool check_dependency_p,
5752 bool *template_p,
5753 bool declarator_p,
5754 bool optional_p)
5755 {
5756 bool global_scope_p;
5757 bool nested_name_specifier_p;
5758
5759 /* Assume the `template' keyword was not used. */
5760 if (template_p)
5761 *template_p = template_keyword_p;
5762
5763 /* Look for the optional `::' operator. */
5764 global_scope_p
5765 = (!template_keyword_p
5766 && (cp_parser_global_scope_opt (parser,
5767 /*current_scope_valid_p=*/false)
5768 != NULL_TREE));
5769
5770 /* Look for the optional nested-name-specifier. */
5771 nested_name_specifier_p
5772 = (cp_parser_nested_name_specifier_opt (parser,
5773 /*typename_keyword_p=*/false,
5774 check_dependency_p,
5775 /*type_p=*/false,
5776 declarator_p,
5777 template_keyword_p)
5778 != NULL_TREE);
5779
5780 /* If there is a nested-name-specifier, then we are looking at
5781 the first qualified-id production. */
5782 if (nested_name_specifier_p)
5783 {
5784 tree saved_scope;
5785 tree saved_object_scope;
5786 tree saved_qualifying_scope;
5787 cp_expr unqualified_id;
5788 bool is_template;
5789
5790 /* See if the next token is the `template' keyword. */
5791 if (!template_p)
5792 template_p = &is_template;
5793 *template_p = cp_parser_optional_template_keyword (parser);
5794 /* Name lookup we do during the processing of the
5795 unqualified-id might obliterate SCOPE. */
5796 saved_scope = parser->scope;
5797 saved_object_scope = parser->object_scope;
5798 saved_qualifying_scope = parser->qualifying_scope;
5799 /* Process the final unqualified-id. */
5800 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5801 check_dependency_p,
5802 declarator_p,
5803 /*optional_p=*/false);
5804 /* Restore the SAVED_SCOPE for our caller. */
5805 parser->scope = saved_scope;
5806 parser->object_scope = saved_object_scope;
5807 parser->qualifying_scope = saved_qualifying_scope;
5808
5809 return unqualified_id;
5810 }
5811 /* Otherwise, if we are in global scope, then we are looking at one
5812 of the other qualified-id productions. */
5813 else if (global_scope_p)
5814 {
5815 cp_token *token;
5816 tree id;
5817
5818 /* Peek at the next token. */
5819 token = cp_lexer_peek_token (parser->lexer);
5820
5821 /* If it's an identifier, and the next token is not a "<", then
5822 we can avoid the template-id case. This is an optimization
5823 for this common case. */
5824 if (token->type == CPP_NAME
5825 && !cp_parser_nth_token_starts_template_argument_list_p
5826 (parser, 2))
5827 return cp_parser_identifier (parser);
5828
5829 cp_parser_parse_tentatively (parser);
5830 /* Try a template-id. */
5831 id = cp_parser_template_id (parser,
5832 /*template_keyword_p=*/false,
5833 /*check_dependency_p=*/true,
5834 none_type,
5835 declarator_p);
5836 /* If that worked, we're done. */
5837 if (cp_parser_parse_definitely (parser))
5838 return id;
5839
5840 /* Peek at the next token. (Changes in the token buffer may
5841 have invalidated the pointer obtained above.) */
5842 token = cp_lexer_peek_token (parser->lexer);
5843
5844 switch (token->type)
5845 {
5846 case CPP_NAME:
5847 return cp_parser_identifier (parser);
5848
5849 case CPP_KEYWORD:
5850 if (token->keyword == RID_OPERATOR)
5851 return cp_parser_operator_function_id (parser);
5852 /* Fall through. */
5853
5854 default:
5855 cp_parser_error (parser, "expected id-expression");
5856 return error_mark_node;
5857 }
5858 }
5859 else
5860 return cp_parser_unqualified_id (parser, template_keyword_p,
5861 /*check_dependency_p=*/true,
5862 declarator_p,
5863 optional_p);
5864 }
5865
5866 /* Parse an unqualified-id.
5867
5868 unqualified-id:
5869 identifier
5870 operator-function-id
5871 conversion-function-id
5872 ~ class-name
5873 template-id
5874
5875 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5876 keyword, in a construct like `A::template ...'.
5877
5878 Returns a representation of unqualified-id. For the `identifier'
5879 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5880 production a BIT_NOT_EXPR is returned; the operand of the
5881 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5882 other productions, see the documentation accompanying the
5883 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5884 names are looked up in uninstantiated templates. If DECLARATOR_P
5885 is true, the unqualified-id is appearing as part of a declarator,
5886 rather than as part of an expression. */
5887
5888 static cp_expr
5889 cp_parser_unqualified_id (cp_parser* parser,
5890 bool template_keyword_p,
5891 bool check_dependency_p,
5892 bool declarator_p,
5893 bool optional_p)
5894 {
5895 cp_token *token;
5896
5897 /* Peek at the next token. */
5898 token = cp_lexer_peek_token (parser->lexer);
5899
5900 switch ((int) token->type)
5901 {
5902 case CPP_NAME:
5903 {
5904 tree id;
5905
5906 /* We don't know yet whether or not this will be a
5907 template-id. */
5908 cp_parser_parse_tentatively (parser);
5909 /* Try a template-id. */
5910 id = cp_parser_template_id (parser, template_keyword_p,
5911 check_dependency_p,
5912 none_type,
5913 declarator_p);
5914 /* If it worked, we're done. */
5915 if (cp_parser_parse_definitely (parser))
5916 return id;
5917 /* Otherwise, it's an ordinary identifier. */
5918 return cp_parser_identifier (parser);
5919 }
5920
5921 case CPP_TEMPLATE_ID:
5922 return cp_parser_template_id (parser, template_keyword_p,
5923 check_dependency_p,
5924 none_type,
5925 declarator_p);
5926
5927 case CPP_COMPL:
5928 {
5929 tree type_decl;
5930 tree qualifying_scope;
5931 tree object_scope;
5932 tree scope;
5933 bool done;
5934
5935 /* Consume the `~' token. */
5936 cp_lexer_consume_token (parser->lexer);
5937 /* Parse the class-name. The standard, as written, seems to
5938 say that:
5939
5940 template <typename T> struct S { ~S (); };
5941 template <typename T> S<T>::~S() {}
5942
5943 is invalid, since `~' must be followed by a class-name, but
5944 `S<T>' is dependent, and so not known to be a class.
5945 That's not right; we need to look in uninstantiated
5946 templates. A further complication arises from:
5947
5948 template <typename T> void f(T t) {
5949 t.T::~T();
5950 }
5951
5952 Here, it is not possible to look up `T' in the scope of `T'
5953 itself. We must look in both the current scope, and the
5954 scope of the containing complete expression.
5955
5956 Yet another issue is:
5957
5958 struct S {
5959 int S;
5960 ~S();
5961 };
5962
5963 S::~S() {}
5964
5965 The standard does not seem to say that the `S' in `~S'
5966 should refer to the type `S' and not the data member
5967 `S::S'. */
5968
5969 /* DR 244 says that we look up the name after the "~" in the
5970 same scope as we looked up the qualifying name. That idea
5971 isn't fully worked out; it's more complicated than that. */
5972 scope = parser->scope;
5973 object_scope = parser->object_scope;
5974 qualifying_scope = parser->qualifying_scope;
5975
5976 /* Check for invalid scopes. */
5977 if (scope == error_mark_node)
5978 {
5979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5980 cp_lexer_consume_token (parser->lexer);
5981 return error_mark_node;
5982 }
5983 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5984 {
5985 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5986 error_at (token->location,
5987 "scope %qT before %<~%> is not a class-name",
5988 scope);
5989 cp_parser_simulate_error (parser);
5990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5991 cp_lexer_consume_token (parser->lexer);
5992 return error_mark_node;
5993 }
5994 gcc_assert (!scope || TYPE_P (scope));
5995
5996 /* If the name is of the form "X::~X" it's OK even if X is a
5997 typedef. */
5998 token = cp_lexer_peek_token (parser->lexer);
5999 if (scope
6000 && token->type == CPP_NAME
6001 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6002 != CPP_LESS)
6003 && (token->u.value == TYPE_IDENTIFIER (scope)
6004 || (CLASS_TYPE_P (scope)
6005 && constructor_name_p (token->u.value, scope))))
6006 {
6007 cp_lexer_consume_token (parser->lexer);
6008 return build_nt (BIT_NOT_EXPR, scope);
6009 }
6010
6011 /* ~auto means the destructor of whatever the object is. */
6012 if (cp_parser_is_keyword (token, RID_AUTO))
6013 {
6014 if (cxx_dialect < cxx14)
6015 pedwarn (input_location, 0,
6016 "%<~auto%> only available with "
6017 "-std=c++14 or -std=gnu++14");
6018 cp_lexer_consume_token (parser->lexer);
6019 return build_nt (BIT_NOT_EXPR, make_auto ());
6020 }
6021
6022 /* If there was an explicit qualification (S::~T), first look
6023 in the scope given by the qualification (i.e., S).
6024
6025 Note: in the calls to cp_parser_class_name below we pass
6026 typename_type so that lookup finds the injected-class-name
6027 rather than the constructor. */
6028 done = false;
6029 type_decl = NULL_TREE;
6030 if (scope)
6031 {
6032 cp_parser_parse_tentatively (parser);
6033 type_decl = cp_parser_class_name (parser,
6034 /*typename_keyword_p=*/false,
6035 /*template_keyword_p=*/false,
6036 typename_type,
6037 /*check_dependency=*/false,
6038 /*class_head_p=*/false,
6039 declarator_p);
6040 if (cp_parser_parse_definitely (parser))
6041 done = true;
6042 }
6043 /* In "N::S::~S", look in "N" as well. */
6044 if (!done && scope && qualifying_scope)
6045 {
6046 cp_parser_parse_tentatively (parser);
6047 parser->scope = qualifying_scope;
6048 parser->object_scope = NULL_TREE;
6049 parser->qualifying_scope = NULL_TREE;
6050 type_decl
6051 = cp_parser_class_name (parser,
6052 /*typename_keyword_p=*/false,
6053 /*template_keyword_p=*/false,
6054 typename_type,
6055 /*check_dependency=*/false,
6056 /*class_head_p=*/false,
6057 declarator_p);
6058 if (cp_parser_parse_definitely (parser))
6059 done = true;
6060 }
6061 /* In "p->S::~T", look in the scope given by "*p" as well. */
6062 else if (!done && object_scope)
6063 {
6064 cp_parser_parse_tentatively (parser);
6065 parser->scope = object_scope;
6066 parser->object_scope = NULL_TREE;
6067 parser->qualifying_scope = NULL_TREE;
6068 type_decl
6069 = cp_parser_class_name (parser,
6070 /*typename_keyword_p=*/false,
6071 /*template_keyword_p=*/false,
6072 typename_type,
6073 /*check_dependency=*/false,
6074 /*class_head_p=*/false,
6075 declarator_p);
6076 if (cp_parser_parse_definitely (parser))
6077 done = true;
6078 }
6079 /* Look in the surrounding context. */
6080 if (!done)
6081 {
6082 parser->scope = NULL_TREE;
6083 parser->object_scope = NULL_TREE;
6084 parser->qualifying_scope = NULL_TREE;
6085 if (processing_template_decl)
6086 cp_parser_parse_tentatively (parser);
6087 type_decl
6088 = cp_parser_class_name (parser,
6089 /*typename_keyword_p=*/false,
6090 /*template_keyword_p=*/false,
6091 typename_type,
6092 /*check_dependency=*/false,
6093 /*class_head_p=*/false,
6094 declarator_p);
6095 if (processing_template_decl
6096 && ! cp_parser_parse_definitely (parser))
6097 {
6098 /* We couldn't find a type with this name. If we're parsing
6099 tentatively, fail and try something else. */
6100 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6101 {
6102 cp_parser_simulate_error (parser);
6103 return error_mark_node;
6104 }
6105 /* Otherwise, accept it and check for a match at instantiation
6106 time. */
6107 type_decl = cp_parser_identifier (parser);
6108 if (type_decl != error_mark_node)
6109 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6110 return type_decl;
6111 }
6112 }
6113 /* If an error occurred, assume that the name of the
6114 destructor is the same as the name of the qualifying
6115 class. That allows us to keep parsing after running
6116 into ill-formed destructor names. */
6117 if (type_decl == error_mark_node && scope)
6118 return build_nt (BIT_NOT_EXPR, scope);
6119 else if (type_decl == error_mark_node)
6120 return error_mark_node;
6121
6122 /* Check that destructor name and scope match. */
6123 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6124 {
6125 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6126 error_at (token->location,
6127 "declaration of %<~%T%> as member of %qT",
6128 type_decl, scope);
6129 cp_parser_simulate_error (parser);
6130 return error_mark_node;
6131 }
6132
6133 /* [class.dtor]
6134
6135 A typedef-name that names a class shall not be used as the
6136 identifier in the declarator for a destructor declaration. */
6137 if (declarator_p
6138 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6139 && !DECL_SELF_REFERENCE_P (type_decl)
6140 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6141 error_at (token->location,
6142 "typedef-name %qD used as destructor declarator",
6143 type_decl);
6144
6145 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6146 }
6147
6148 case CPP_KEYWORD:
6149 if (token->keyword == RID_OPERATOR)
6150 {
6151 cp_expr id;
6152
6153 /* This could be a template-id, so we try that first. */
6154 cp_parser_parse_tentatively (parser);
6155 /* Try a template-id. */
6156 id = cp_parser_template_id (parser, template_keyword_p,
6157 /*check_dependency_p=*/true,
6158 none_type,
6159 declarator_p);
6160 /* If that worked, we're done. */
6161 if (cp_parser_parse_definitely (parser))
6162 return id;
6163 /* We still don't know whether we're looking at an
6164 operator-function-id or a conversion-function-id. */
6165 cp_parser_parse_tentatively (parser);
6166 /* Try an operator-function-id. */
6167 id = cp_parser_operator_function_id (parser);
6168 /* If that didn't work, try a conversion-function-id. */
6169 if (!cp_parser_parse_definitely (parser))
6170 id = cp_parser_conversion_function_id (parser);
6171
6172 return id;
6173 }
6174 /* Fall through. */
6175
6176 default:
6177 if (optional_p)
6178 return NULL_TREE;
6179 cp_parser_error (parser, "expected unqualified-id");
6180 return error_mark_node;
6181 }
6182 }
6183
6184 /* Parse an (optional) nested-name-specifier.
6185
6186 nested-name-specifier: [C++98]
6187 class-or-namespace-name :: nested-name-specifier [opt]
6188 class-or-namespace-name :: template nested-name-specifier [opt]
6189
6190 nested-name-specifier: [C++0x]
6191 type-name ::
6192 namespace-name ::
6193 nested-name-specifier identifier ::
6194 nested-name-specifier template [opt] simple-template-id ::
6195
6196 PARSER->SCOPE should be set appropriately before this function is
6197 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6198 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6199 in name lookups.
6200
6201 Sets PARSER->SCOPE to the class (TYPE) or namespace
6202 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6203 it unchanged if there is no nested-name-specifier. Returns the new
6204 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6205
6206 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6207 part of a declaration and/or decl-specifier. */
6208
6209 static tree
6210 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6211 bool typename_keyword_p,
6212 bool check_dependency_p,
6213 bool type_p,
6214 bool is_declaration,
6215 bool template_keyword_p /* = false */)
6216 {
6217 bool success = false;
6218 cp_token_position start = 0;
6219 cp_token *token;
6220
6221 /* Remember where the nested-name-specifier starts. */
6222 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6223 {
6224 start = cp_lexer_token_position (parser->lexer, false);
6225 push_deferring_access_checks (dk_deferred);
6226 }
6227
6228 while (true)
6229 {
6230 tree new_scope;
6231 tree old_scope;
6232 tree saved_qualifying_scope;
6233
6234 /* Spot cases that cannot be the beginning of a
6235 nested-name-specifier. */
6236 token = cp_lexer_peek_token (parser->lexer);
6237
6238 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6239 the already parsed nested-name-specifier. */
6240 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6241 {
6242 /* Grab the nested-name-specifier and continue the loop. */
6243 cp_parser_pre_parsed_nested_name_specifier (parser);
6244 /* If we originally encountered this nested-name-specifier
6245 with IS_DECLARATION set to false, we will not have
6246 resolved TYPENAME_TYPEs, so we must do so here. */
6247 if (is_declaration
6248 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6249 {
6250 new_scope = resolve_typename_type (parser->scope,
6251 /*only_current_p=*/false);
6252 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6253 parser->scope = new_scope;
6254 }
6255 success = true;
6256 continue;
6257 }
6258
6259 /* Spot cases that cannot be the beginning of a
6260 nested-name-specifier. On the second and subsequent times
6261 through the loop, we look for the `template' keyword. */
6262 if (success && token->keyword == RID_TEMPLATE)
6263 ;
6264 /* A template-id can start a nested-name-specifier. */
6265 else if (token->type == CPP_TEMPLATE_ID)
6266 ;
6267 /* DR 743: decltype can be used in a nested-name-specifier. */
6268 else if (token_is_decltype (token))
6269 ;
6270 else
6271 {
6272 /* If the next token is not an identifier, then it is
6273 definitely not a type-name or namespace-name. */
6274 if (token->type != CPP_NAME)
6275 break;
6276 /* If the following token is neither a `<' (to begin a
6277 template-id), nor a `::', then we are not looking at a
6278 nested-name-specifier. */
6279 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6280
6281 if (token->type == CPP_COLON
6282 && parser->colon_corrects_to_scope_p
6283 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6284 {
6285 gcc_rich_location richloc (token->location);
6286 richloc.add_fixit_replace ("::");
6287 error_at (&richloc,
6288 "found %<:%> in nested-name-specifier, "
6289 "expected %<::%>");
6290 token->type = CPP_SCOPE;
6291 }
6292
6293 if (token->type != CPP_SCOPE
6294 && !cp_parser_nth_token_starts_template_argument_list_p
6295 (parser, 2))
6296 break;
6297 }
6298
6299 /* The nested-name-specifier is optional, so we parse
6300 tentatively. */
6301 cp_parser_parse_tentatively (parser);
6302
6303 /* Look for the optional `template' keyword, if this isn't the
6304 first time through the loop. */
6305 if (success)
6306 template_keyword_p = cp_parser_optional_template_keyword (parser);
6307
6308 /* Save the old scope since the name lookup we are about to do
6309 might destroy it. */
6310 old_scope = parser->scope;
6311 saved_qualifying_scope = parser->qualifying_scope;
6312 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6313 look up names in "X<T>::I" in order to determine that "Y" is
6314 a template. So, if we have a typename at this point, we make
6315 an effort to look through it. */
6316 if (is_declaration
6317 && !typename_keyword_p
6318 && parser->scope
6319 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6320 parser->scope = resolve_typename_type (parser->scope,
6321 /*only_current_p=*/false);
6322 /* Parse the qualifying entity. */
6323 new_scope
6324 = cp_parser_qualifying_entity (parser,
6325 typename_keyword_p,
6326 template_keyword_p,
6327 check_dependency_p,
6328 type_p,
6329 is_declaration);
6330 /* Look for the `::' token. */
6331 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6332
6333 /* If we found what we wanted, we keep going; otherwise, we're
6334 done. */
6335 if (!cp_parser_parse_definitely (parser))
6336 {
6337 bool error_p = false;
6338
6339 /* Restore the OLD_SCOPE since it was valid before the
6340 failed attempt at finding the last
6341 class-or-namespace-name. */
6342 parser->scope = old_scope;
6343 parser->qualifying_scope = saved_qualifying_scope;
6344
6345 /* If the next token is a decltype, and the one after that is a
6346 `::', then the decltype has failed to resolve to a class or
6347 enumeration type. Give this error even when parsing
6348 tentatively since it can't possibly be valid--and we're going
6349 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6350 won't get another chance.*/
6351 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6352 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6353 == CPP_SCOPE))
6354 {
6355 token = cp_lexer_consume_token (parser->lexer);
6356 error_at (token->location, "decltype evaluates to %qT, "
6357 "which is not a class or enumeration type",
6358 token->u.tree_check_value->value);
6359 parser->scope = error_mark_node;
6360 error_p = true;
6361 /* As below. */
6362 success = true;
6363 cp_lexer_consume_token (parser->lexer);
6364 }
6365
6366 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6367 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6368 {
6369 /* If we have a non-type template-id followed by ::, it can't
6370 possibly be valid. */
6371 token = cp_lexer_peek_token (parser->lexer);
6372 tree tid = token->u.tree_check_value->value;
6373 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6374 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6375 {
6376 tree tmpl = NULL_TREE;
6377 if (is_overloaded_fn (tid))
6378 {
6379 tree fns = get_fns (tid);
6380 if (OVL_SINGLE_P (fns))
6381 tmpl = OVL_FIRST (fns);
6382 error_at (token->location, "function template-id %qD "
6383 "in nested-name-specifier", tid);
6384 }
6385 else
6386 {
6387 /* Variable template. */
6388 tmpl = TREE_OPERAND (tid, 0);
6389 gcc_assert (variable_template_p (tmpl));
6390 error_at (token->location, "variable template-id %qD "
6391 "in nested-name-specifier", tid);
6392 }
6393 if (tmpl)
6394 inform (DECL_SOURCE_LOCATION (tmpl),
6395 "%qD declared here", tmpl);
6396
6397 parser->scope = error_mark_node;
6398 error_p = true;
6399 /* As below. */
6400 success = true;
6401 cp_lexer_consume_token (parser->lexer);
6402 cp_lexer_consume_token (parser->lexer);
6403 }
6404 }
6405
6406 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6407 break;
6408 /* If the next token is an identifier, and the one after
6409 that is a `::', then any valid interpretation would have
6410 found a class-or-namespace-name. */
6411 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6412 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6413 == CPP_SCOPE)
6414 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6415 != CPP_COMPL))
6416 {
6417 token = cp_lexer_consume_token (parser->lexer);
6418 if (!error_p)
6419 {
6420 if (!token->error_reported)
6421 {
6422 tree decl;
6423 tree ambiguous_decls;
6424
6425 decl = cp_parser_lookup_name (parser, token->u.value,
6426 none_type,
6427 /*is_template=*/false,
6428 /*is_namespace=*/false,
6429 /*check_dependency=*/true,
6430 &ambiguous_decls,
6431 token->location);
6432 if (TREE_CODE (decl) == TEMPLATE_DECL)
6433 error_at (token->location,
6434 "%qD used without template arguments",
6435 decl);
6436 else if (ambiguous_decls)
6437 {
6438 // cp_parser_lookup_name has the same diagnostic,
6439 // thus make sure to emit it at most once.
6440 if (cp_parser_uncommitted_to_tentative_parse_p
6441 (parser))
6442 {
6443 error_at (token->location,
6444 "reference to %qD is ambiguous",
6445 token->u.value);
6446 print_candidates (ambiguous_decls);
6447 }
6448 decl = error_mark_node;
6449 }
6450 else
6451 {
6452 if (cxx_dialect != cxx98)
6453 cp_parser_name_lookup_error
6454 (parser, token->u.value, decl, NLE_NOT_CXX98,
6455 token->location);
6456 else
6457 cp_parser_name_lookup_error
6458 (parser, token->u.value, decl, NLE_CXX98,
6459 token->location);
6460 }
6461 }
6462 parser->scope = error_mark_node;
6463 error_p = true;
6464 /* Treat this as a successful nested-name-specifier
6465 due to:
6466
6467 [basic.lookup.qual]
6468
6469 If the name found is not a class-name (clause
6470 _class_) or namespace-name (_namespace.def_), the
6471 program is ill-formed. */
6472 success = true;
6473 }
6474 cp_lexer_consume_token (parser->lexer);
6475 }
6476 break;
6477 }
6478 /* We've found one valid nested-name-specifier. */
6479 success = true;
6480 /* Name lookup always gives us a DECL. */
6481 if (TREE_CODE (new_scope) == TYPE_DECL)
6482 new_scope = TREE_TYPE (new_scope);
6483 /* Uses of "template" must be followed by actual templates. */
6484 if (template_keyword_p
6485 && !(CLASS_TYPE_P (new_scope)
6486 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6487 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6488 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6489 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6490 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6491 == TEMPLATE_ID_EXPR)))
6492 permerror (input_location, TYPE_P (new_scope)
6493 ? G_("%qT is not a template")
6494 : G_("%qD is not a template"),
6495 new_scope);
6496 /* If it is a class scope, try to complete it; we are about to
6497 be looking up names inside the class. */
6498 if (TYPE_P (new_scope)
6499 /* Since checking types for dependency can be expensive,
6500 avoid doing it if the type is already complete. */
6501 && !COMPLETE_TYPE_P (new_scope)
6502 /* Do not try to complete dependent types. */
6503 && !dependent_type_p (new_scope))
6504 {
6505 new_scope = complete_type (new_scope);
6506 /* If it is a typedef to current class, use the current
6507 class instead, as the typedef won't have any names inside
6508 it yet. */
6509 if (!COMPLETE_TYPE_P (new_scope)
6510 && currently_open_class (new_scope))
6511 new_scope = TYPE_MAIN_VARIANT (new_scope);
6512 }
6513 /* Make sure we look in the right scope the next time through
6514 the loop. */
6515 parser->scope = new_scope;
6516 }
6517
6518 /* If parsing tentatively, replace the sequence of tokens that makes
6519 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6520 token. That way, should we re-parse the token stream, we will
6521 not have to repeat the effort required to do the parse, nor will
6522 we issue duplicate error messages. */
6523 if (success && start)
6524 {
6525 cp_token *token;
6526
6527 token = cp_lexer_token_at (parser->lexer, start);
6528 /* Reset the contents of the START token. */
6529 token->type = CPP_NESTED_NAME_SPECIFIER;
6530 /* Retrieve any deferred checks. Do not pop this access checks yet
6531 so the memory will not be reclaimed during token replacing below. */
6532 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6533 token->u.tree_check_value->value = parser->scope;
6534 token->u.tree_check_value->checks = get_deferred_access_checks ();
6535 token->u.tree_check_value->qualifying_scope =
6536 parser->qualifying_scope;
6537 token->keyword = RID_MAX;
6538
6539 /* Purge all subsequent tokens. */
6540 cp_lexer_purge_tokens_after (parser->lexer, start);
6541 }
6542
6543 if (start)
6544 pop_to_parent_deferring_access_checks ();
6545
6546 return success ? parser->scope : NULL_TREE;
6547 }
6548
6549 /* Parse a nested-name-specifier. See
6550 cp_parser_nested_name_specifier_opt for details. This function
6551 behaves identically, except that it will an issue an error if no
6552 nested-name-specifier is present. */
6553
6554 static tree
6555 cp_parser_nested_name_specifier (cp_parser *parser,
6556 bool typename_keyword_p,
6557 bool check_dependency_p,
6558 bool type_p,
6559 bool is_declaration)
6560 {
6561 tree scope;
6562
6563 /* Look for the nested-name-specifier. */
6564 scope = cp_parser_nested_name_specifier_opt (parser,
6565 typename_keyword_p,
6566 check_dependency_p,
6567 type_p,
6568 is_declaration);
6569 /* If it was not present, issue an error message. */
6570 if (!scope)
6571 {
6572 cp_parser_error (parser, "expected nested-name-specifier");
6573 parser->scope = NULL_TREE;
6574 }
6575
6576 return scope;
6577 }
6578
6579 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6580 this is either a class-name or a namespace-name (which corresponds
6581 to the class-or-namespace-name production in the grammar). For
6582 C++0x, it can also be a type-name that refers to an enumeration
6583 type or a simple-template-id.
6584
6585 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6586 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6587 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6588 TYPE_P is TRUE iff the next name should be taken as a class-name,
6589 even the same name is declared to be another entity in the same
6590 scope.
6591
6592 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6593 specified by the class-or-namespace-name. If neither is found the
6594 ERROR_MARK_NODE is returned. */
6595
6596 static tree
6597 cp_parser_qualifying_entity (cp_parser *parser,
6598 bool typename_keyword_p,
6599 bool template_keyword_p,
6600 bool check_dependency_p,
6601 bool type_p,
6602 bool is_declaration)
6603 {
6604 tree saved_scope;
6605 tree saved_qualifying_scope;
6606 tree saved_object_scope;
6607 tree scope;
6608 bool only_class_p;
6609 bool successful_parse_p;
6610
6611 /* DR 743: decltype can appear in a nested-name-specifier. */
6612 if (cp_lexer_next_token_is_decltype (parser->lexer))
6613 {
6614 scope = cp_parser_decltype (parser);
6615 if (TREE_CODE (scope) != ENUMERAL_TYPE
6616 && !MAYBE_CLASS_TYPE_P (scope))
6617 {
6618 cp_parser_simulate_error (parser);
6619 return error_mark_node;
6620 }
6621 if (TYPE_NAME (scope))
6622 scope = TYPE_NAME (scope);
6623 return scope;
6624 }
6625
6626 /* Before we try to parse the class-name, we must save away the
6627 current PARSER->SCOPE since cp_parser_class_name will destroy
6628 it. */
6629 saved_scope = parser->scope;
6630 saved_qualifying_scope = parser->qualifying_scope;
6631 saved_object_scope = parser->object_scope;
6632 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6633 there is no need to look for a namespace-name. */
6634 only_class_p = template_keyword_p
6635 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6636 if (!only_class_p)
6637 cp_parser_parse_tentatively (parser);
6638 scope = cp_parser_class_name (parser,
6639 typename_keyword_p,
6640 template_keyword_p,
6641 type_p ? class_type : none_type,
6642 check_dependency_p,
6643 /*class_head_p=*/false,
6644 is_declaration,
6645 /*enum_ok=*/cxx_dialect > cxx98);
6646 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6647 /* If that didn't work, try for a namespace-name. */
6648 if (!only_class_p && !successful_parse_p)
6649 {
6650 /* Restore the saved scope. */
6651 parser->scope = saved_scope;
6652 parser->qualifying_scope = saved_qualifying_scope;
6653 parser->object_scope = saved_object_scope;
6654 /* If we are not looking at an identifier followed by the scope
6655 resolution operator, then this is not part of a
6656 nested-name-specifier. (Note that this function is only used
6657 to parse the components of a nested-name-specifier.) */
6658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6659 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6660 return error_mark_node;
6661 scope = cp_parser_namespace_name (parser);
6662 }
6663
6664 return scope;
6665 }
6666
6667 /* Return true if we are looking at a compound-literal, false otherwise. */
6668
6669 static bool
6670 cp_parser_compound_literal_p (cp_parser *parser)
6671 {
6672 cp_lexer_save_tokens (parser->lexer);
6673
6674 /* Skip tokens until the next token is a closing parenthesis.
6675 If we find the closing `)', and the next token is a `{', then
6676 we are looking at a compound-literal. */
6677 bool compound_literal_p
6678 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6679 /*consume_paren=*/true)
6680 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6681
6682 /* Roll back the tokens we skipped. */
6683 cp_lexer_rollback_tokens (parser->lexer);
6684
6685 return compound_literal_p;
6686 }
6687
6688 /* Return true if EXPR is the integer constant zero or a complex constant
6689 of zero, without any folding, but ignoring location wrappers. */
6690
6691 bool
6692 literal_integer_zerop (const_tree expr)
6693 {
6694 return (location_wrapper_p (expr)
6695 && integer_zerop (TREE_OPERAND (expr, 0)));
6696 }
6697
6698 /* Parse a postfix-expression.
6699
6700 postfix-expression:
6701 primary-expression
6702 postfix-expression [ expression ]
6703 postfix-expression ( expression-list [opt] )
6704 simple-type-specifier ( expression-list [opt] )
6705 typename :: [opt] nested-name-specifier identifier
6706 ( expression-list [opt] )
6707 typename :: [opt] nested-name-specifier template [opt] template-id
6708 ( expression-list [opt] )
6709 postfix-expression . template [opt] id-expression
6710 postfix-expression -> template [opt] id-expression
6711 postfix-expression . pseudo-destructor-name
6712 postfix-expression -> pseudo-destructor-name
6713 postfix-expression ++
6714 postfix-expression --
6715 dynamic_cast < type-id > ( expression )
6716 static_cast < type-id > ( expression )
6717 reinterpret_cast < type-id > ( expression )
6718 const_cast < type-id > ( expression )
6719 typeid ( expression )
6720 typeid ( type-id )
6721
6722 GNU Extension:
6723
6724 postfix-expression:
6725 ( type-id ) { initializer-list , [opt] }
6726
6727 This extension is a GNU version of the C99 compound-literal
6728 construct. (The C99 grammar uses `type-name' instead of `type-id',
6729 but they are essentially the same concept.)
6730
6731 If ADDRESS_P is true, the postfix expression is the operand of the
6732 `&' operator. CAST_P is true if this expression is the target of a
6733 cast.
6734
6735 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6736 class member access expressions [expr.ref].
6737
6738 Returns a representation of the expression. */
6739
6740 static cp_expr
6741 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6742 bool member_access_only_p, bool decltype_p,
6743 cp_id_kind * pidk_return)
6744 {
6745 cp_token *token;
6746 location_t loc;
6747 enum rid keyword;
6748 cp_id_kind idk = CP_ID_KIND_NONE;
6749 cp_expr postfix_expression = NULL_TREE;
6750 bool is_member_access = false;
6751
6752 /* Peek at the next token. */
6753 token = cp_lexer_peek_token (parser->lexer);
6754 loc = token->location;
6755 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6756
6757 /* Some of the productions are determined by keywords. */
6758 keyword = token->keyword;
6759 switch (keyword)
6760 {
6761 case RID_DYNCAST:
6762 case RID_STATCAST:
6763 case RID_REINTCAST:
6764 case RID_CONSTCAST:
6765 {
6766 tree type;
6767 cp_expr expression;
6768 const char *saved_message;
6769 bool saved_in_type_id_in_expr_p;
6770
6771 /* All of these can be handled in the same way from the point
6772 of view of parsing. Begin by consuming the token
6773 identifying the cast. */
6774 cp_lexer_consume_token (parser->lexer);
6775
6776 /* New types cannot be defined in the cast. */
6777 saved_message = parser->type_definition_forbidden_message;
6778 parser->type_definition_forbidden_message
6779 = G_("types may not be defined in casts");
6780
6781 /* Look for the opening `<'. */
6782 cp_parser_require (parser, CPP_LESS, RT_LESS);
6783 /* Parse the type to which we are casting. */
6784 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6785 parser->in_type_id_in_expr_p = true;
6786 type = cp_parser_type_id (parser);
6787 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6788 /* Look for the closing `>'. */
6789 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6790 /* Restore the old message. */
6791 parser->type_definition_forbidden_message = saved_message;
6792
6793 bool saved_greater_than_is_operator_p
6794 = parser->greater_than_is_operator_p;
6795 parser->greater_than_is_operator_p = true;
6796
6797 /* And the expression which is being cast. */
6798 matching_parens parens;
6799 parens.require_open (parser);
6800 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6801 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6802 RT_CLOSE_PAREN);
6803 location_t end_loc = close_paren ?
6804 close_paren->location : UNKNOWN_LOCATION;
6805
6806 parser->greater_than_is_operator_p
6807 = saved_greater_than_is_operator_p;
6808
6809 /* Only type conversions to integral or enumeration types
6810 can be used in constant-expressions. */
6811 if (!cast_valid_in_integral_constant_expression_p (type)
6812 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6813 {
6814 postfix_expression = error_mark_node;
6815 break;
6816 }
6817
6818 switch (keyword)
6819 {
6820 case RID_DYNCAST:
6821 postfix_expression
6822 = build_dynamic_cast (type, expression, tf_warning_or_error);
6823 break;
6824 case RID_STATCAST:
6825 postfix_expression
6826 = build_static_cast (type, expression, tf_warning_or_error);
6827 break;
6828 case RID_REINTCAST:
6829 postfix_expression
6830 = build_reinterpret_cast (type, expression,
6831 tf_warning_or_error);
6832 break;
6833 case RID_CONSTCAST:
6834 postfix_expression
6835 = build_const_cast (type, expression, tf_warning_or_error);
6836 break;
6837 default:
6838 gcc_unreachable ();
6839 }
6840
6841 /* Construct a location e.g. :
6842 reinterpret_cast <int *> (expr)
6843 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6844 ranging from the start of the "*_cast" token to the final closing
6845 paren, with the caret at the start. */
6846 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6847 postfix_expression.set_location (cp_cast_loc);
6848 }
6849 break;
6850
6851 case RID_TYPEID:
6852 {
6853 tree type;
6854 const char *saved_message;
6855 bool saved_in_type_id_in_expr_p;
6856
6857 /* Consume the `typeid' token. */
6858 cp_lexer_consume_token (parser->lexer);
6859 /* Look for the `(' token. */
6860 matching_parens parens;
6861 parens.require_open (parser);
6862 /* Types cannot be defined in a `typeid' expression. */
6863 saved_message = parser->type_definition_forbidden_message;
6864 parser->type_definition_forbidden_message
6865 = G_("types may not be defined in a %<typeid%> expression");
6866 /* We can't be sure yet whether we're looking at a type-id or an
6867 expression. */
6868 cp_parser_parse_tentatively (parser);
6869 /* Try a type-id first. */
6870 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6871 parser->in_type_id_in_expr_p = true;
6872 type = cp_parser_type_id (parser);
6873 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6874 /* Look for the `)' token. Otherwise, we can't be sure that
6875 we're not looking at an expression: consider `typeid (int
6876 (3))', for example. */
6877 cp_token *close_paren = parens.require_close (parser);
6878 /* If all went well, simply lookup the type-id. */
6879 if (cp_parser_parse_definitely (parser))
6880 postfix_expression = get_typeid (type, tf_warning_or_error);
6881 /* Otherwise, fall back to the expression variant. */
6882 else
6883 {
6884 tree expression;
6885
6886 /* Look for an expression. */
6887 expression = cp_parser_expression (parser, & idk);
6888 /* Compute its typeid. */
6889 postfix_expression = build_typeid (expression, tf_warning_or_error);
6890 /* Look for the `)' token. */
6891 close_paren = parens.require_close (parser);
6892 }
6893 /* Restore the saved message. */
6894 parser->type_definition_forbidden_message = saved_message;
6895 /* `typeid' may not appear in an integral constant expression. */
6896 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6897 postfix_expression = error_mark_node;
6898
6899 /* Construct a location e.g. :
6900 typeid (expr)
6901 ^~~~~~~~~~~~~
6902 ranging from the start of the "typeid" token to the final closing
6903 paren, with the caret at the start. */
6904 if (close_paren)
6905 {
6906 location_t typeid_loc
6907 = make_location (start_loc, start_loc, close_paren->location);
6908 postfix_expression.set_location (typeid_loc);
6909 postfix_expression.maybe_add_location_wrapper ();
6910 }
6911 }
6912 break;
6913
6914 case RID_TYPENAME:
6915 {
6916 tree type;
6917 /* The syntax permitted here is the same permitted for an
6918 elaborated-type-specifier. */
6919 ++parser->prevent_constrained_type_specifiers;
6920 type = cp_parser_elaborated_type_specifier (parser,
6921 /*is_friend=*/false,
6922 /*is_declaration=*/false);
6923 --parser->prevent_constrained_type_specifiers;
6924 postfix_expression = cp_parser_functional_cast (parser, type);
6925 }
6926 break;
6927
6928 case RID_ADDRESSOF:
6929 case RID_BUILTIN_SHUFFLE:
6930 case RID_BUILTIN_LAUNDER:
6931 {
6932 vec<tree, va_gc> *vec;
6933 unsigned int i;
6934 tree p;
6935
6936 cp_lexer_consume_token (parser->lexer);
6937 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6938 /*cast_p=*/false, /*allow_expansion_p=*/true,
6939 /*non_constant_p=*/NULL);
6940 if (vec == NULL)
6941 {
6942 postfix_expression = error_mark_node;
6943 break;
6944 }
6945
6946 FOR_EACH_VEC_ELT (*vec, i, p)
6947 mark_exp_read (p);
6948
6949 switch (keyword)
6950 {
6951 case RID_ADDRESSOF:
6952 if (vec->length () == 1)
6953 postfix_expression
6954 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6955 else
6956 {
6957 error_at (loc, "wrong number of arguments to "
6958 "%<__builtin_addressof%>");
6959 postfix_expression = error_mark_node;
6960 }
6961 break;
6962
6963 case RID_BUILTIN_LAUNDER:
6964 if (vec->length () == 1)
6965 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6966 tf_warning_or_error);
6967 else
6968 {
6969 error_at (loc, "wrong number of arguments to "
6970 "%<__builtin_launder%>");
6971 postfix_expression = error_mark_node;
6972 }
6973 break;
6974
6975 case RID_BUILTIN_SHUFFLE:
6976 if (vec->length () == 2)
6977 postfix_expression
6978 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6979 (*vec)[1], tf_warning_or_error);
6980 else if (vec->length () == 3)
6981 postfix_expression
6982 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6983 (*vec)[2], tf_warning_or_error);
6984 else
6985 {
6986 error_at (loc, "wrong number of arguments to "
6987 "%<__builtin_shuffle%>");
6988 postfix_expression = error_mark_node;
6989 }
6990 break;
6991
6992 default:
6993 gcc_unreachable ();
6994 }
6995 break;
6996 }
6997
6998 default:
6999 {
7000 tree type;
7001
7002 /* If the next thing is a simple-type-specifier, we may be
7003 looking at a functional cast. We could also be looking at
7004 an id-expression. So, we try the functional cast, and if
7005 that doesn't work we fall back to the primary-expression. */
7006 cp_parser_parse_tentatively (parser);
7007 /* Look for the simple-type-specifier. */
7008 ++parser->prevent_constrained_type_specifiers;
7009 type = cp_parser_simple_type_specifier (parser,
7010 /*decl_specs=*/NULL,
7011 CP_PARSER_FLAGS_NONE);
7012 --parser->prevent_constrained_type_specifiers;
7013 /* Parse the cast itself. */
7014 if (!cp_parser_error_occurred (parser))
7015 postfix_expression
7016 = cp_parser_functional_cast (parser, type);
7017 /* If that worked, we're done. */
7018 if (cp_parser_parse_definitely (parser))
7019 break;
7020
7021 /* If the functional-cast didn't work out, try a
7022 compound-literal. */
7023 if (cp_parser_allow_gnu_extensions_p (parser)
7024 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7025 {
7026 cp_expr initializer = NULL_TREE;
7027
7028 cp_parser_parse_tentatively (parser);
7029
7030 matching_parens parens;
7031 parens.consume_open (parser);
7032
7033 /* Avoid calling cp_parser_type_id pointlessly, see comment
7034 in cp_parser_cast_expression about c++/29234. */
7035 if (!cp_parser_compound_literal_p (parser))
7036 cp_parser_simulate_error (parser);
7037 else
7038 {
7039 /* Parse the type. */
7040 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7041 parser->in_type_id_in_expr_p = true;
7042 type = cp_parser_type_id (parser);
7043 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7044 parens.require_close (parser);
7045 }
7046
7047 /* If things aren't going well, there's no need to
7048 keep going. */
7049 if (!cp_parser_error_occurred (parser))
7050 {
7051 bool non_constant_p;
7052 /* Parse the brace-enclosed initializer list. */
7053 initializer = cp_parser_braced_list (parser,
7054 &non_constant_p);
7055 }
7056 /* If that worked, we're definitely looking at a
7057 compound-literal expression. */
7058 if (cp_parser_parse_definitely (parser))
7059 {
7060 /* Warn the user that a compound literal is not
7061 allowed in standard C++. */
7062 pedwarn (input_location, OPT_Wpedantic,
7063 "ISO C++ forbids compound-literals");
7064 /* For simplicity, we disallow compound literals in
7065 constant-expressions. We could
7066 allow compound literals of integer type, whose
7067 initializer was a constant, in constant
7068 expressions. Permitting that usage, as a further
7069 extension, would not change the meaning of any
7070 currently accepted programs. (Of course, as
7071 compound literals are not part of ISO C++, the
7072 standard has nothing to say.) */
7073 if (cp_parser_non_integral_constant_expression (parser,
7074 NIC_NCC))
7075 {
7076 postfix_expression = error_mark_node;
7077 break;
7078 }
7079 /* Form the representation of the compound-literal. */
7080 postfix_expression
7081 = finish_compound_literal (type, initializer,
7082 tf_warning_or_error, fcl_c99);
7083 postfix_expression.set_location (initializer.get_location ());
7084 break;
7085 }
7086 }
7087
7088 /* It must be a primary-expression. */
7089 postfix_expression
7090 = cp_parser_primary_expression (parser, address_p, cast_p,
7091 /*template_arg_p=*/false,
7092 decltype_p,
7093 &idk);
7094 }
7095 break;
7096 }
7097
7098 /* Note that we don't need to worry about calling build_cplus_new on a
7099 class-valued CALL_EXPR in decltype when it isn't the end of the
7100 postfix-expression; unary_complex_lvalue will take care of that for
7101 all these cases. */
7102
7103 /* Keep looping until the postfix-expression is complete. */
7104 while (true)
7105 {
7106 if (idk == CP_ID_KIND_UNQUALIFIED
7107 && identifier_p (postfix_expression)
7108 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7109 /* It is not a Koenig lookup function call. */
7110 postfix_expression
7111 = unqualified_name_lookup_error (postfix_expression);
7112
7113 /* Peek at the next token. */
7114 token = cp_lexer_peek_token (parser->lexer);
7115
7116 switch (token->type)
7117 {
7118 case CPP_OPEN_SQUARE:
7119 if (cp_next_tokens_can_be_std_attribute_p (parser))
7120 {
7121 cp_parser_error (parser,
7122 "two consecutive %<[%> shall "
7123 "only introduce an attribute");
7124 return error_mark_node;
7125 }
7126 postfix_expression
7127 = cp_parser_postfix_open_square_expression (parser,
7128 postfix_expression,
7129 false,
7130 decltype_p);
7131 postfix_expression.set_range (start_loc,
7132 postfix_expression.get_location ());
7133
7134 idk = CP_ID_KIND_NONE;
7135 is_member_access = false;
7136 break;
7137
7138 case CPP_OPEN_PAREN:
7139 /* postfix-expression ( expression-list [opt] ) */
7140 {
7141 bool koenig_p;
7142 bool is_builtin_constant_p;
7143 bool saved_integral_constant_expression_p = false;
7144 bool saved_non_integral_constant_expression_p = false;
7145 tsubst_flags_t complain = complain_flags (decltype_p);
7146 vec<tree, va_gc> *args;
7147 location_t close_paren_loc = UNKNOWN_LOCATION;
7148
7149 is_member_access = false;
7150
7151 is_builtin_constant_p
7152 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7153 if (is_builtin_constant_p)
7154 {
7155 /* The whole point of __builtin_constant_p is to allow
7156 non-constant expressions to appear as arguments. */
7157 saved_integral_constant_expression_p
7158 = parser->integral_constant_expression_p;
7159 saved_non_integral_constant_expression_p
7160 = parser->non_integral_constant_expression_p;
7161 parser->integral_constant_expression_p = false;
7162 }
7163 args = (cp_parser_parenthesized_expression_list
7164 (parser, non_attr,
7165 /*cast_p=*/false, /*allow_expansion_p=*/true,
7166 /*non_constant_p=*/NULL,
7167 /*close_paren_loc=*/&close_paren_loc,
7168 /*wrap_locations_p=*/true));
7169 if (is_builtin_constant_p)
7170 {
7171 parser->integral_constant_expression_p
7172 = saved_integral_constant_expression_p;
7173 parser->non_integral_constant_expression_p
7174 = saved_non_integral_constant_expression_p;
7175 }
7176
7177 if (args == NULL)
7178 {
7179 postfix_expression = error_mark_node;
7180 break;
7181 }
7182
7183 /* Function calls are not permitted in
7184 constant-expressions. */
7185 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7186 && cp_parser_non_integral_constant_expression (parser,
7187 NIC_FUNC_CALL))
7188 {
7189 postfix_expression = error_mark_node;
7190 release_tree_vector (args);
7191 break;
7192 }
7193
7194 koenig_p = false;
7195 if (idk == CP_ID_KIND_UNQUALIFIED
7196 || idk == CP_ID_KIND_TEMPLATE_ID)
7197 {
7198 if (identifier_p (postfix_expression)
7199 /* In C++2A, we may need to perform ADL for a template
7200 name. */
7201 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7202 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7203 {
7204 if (!args->is_empty ())
7205 {
7206 koenig_p = true;
7207 if (!any_type_dependent_arguments_p (args))
7208 postfix_expression
7209 = perform_koenig_lookup (postfix_expression, args,
7210 complain);
7211 }
7212 else
7213 postfix_expression
7214 = unqualified_fn_lookup_error (postfix_expression);
7215 }
7216 /* We do not perform argument-dependent lookup if
7217 normal lookup finds a non-function, in accordance
7218 with the expected resolution of DR 218. */
7219 else if (!args->is_empty ()
7220 && is_overloaded_fn (postfix_expression))
7221 {
7222 tree fn = get_first_fn (postfix_expression);
7223 fn = STRIP_TEMPLATE (fn);
7224
7225 /* Do not do argument dependent lookup if regular
7226 lookup finds a member function or a block-scope
7227 function declaration. [basic.lookup.argdep]/3 */
7228 if (!DECL_FUNCTION_MEMBER_P (fn)
7229 && !DECL_LOCAL_FUNCTION_P (fn))
7230 {
7231 koenig_p = true;
7232 if (!any_type_dependent_arguments_p (args))
7233 postfix_expression
7234 = perform_koenig_lookup (postfix_expression, args,
7235 complain);
7236 }
7237 }
7238 }
7239
7240 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7241 {
7242 tree instance = TREE_OPERAND (postfix_expression, 0);
7243 tree fn = TREE_OPERAND (postfix_expression, 1);
7244
7245 if (processing_template_decl
7246 && (type_dependent_object_expression_p (instance)
7247 || (!BASELINK_P (fn)
7248 && TREE_CODE (fn) != FIELD_DECL)
7249 || type_dependent_expression_p (fn)
7250 || any_type_dependent_arguments_p (args)))
7251 {
7252 maybe_generic_this_capture (instance, fn);
7253 postfix_expression
7254 = build_min_nt_call_vec (postfix_expression, args);
7255 release_tree_vector (args);
7256 break;
7257 }
7258
7259 if (BASELINK_P (fn))
7260 {
7261 postfix_expression
7262 = (build_new_method_call
7263 (instance, fn, &args, NULL_TREE,
7264 (idk == CP_ID_KIND_QUALIFIED
7265 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7266 : LOOKUP_NORMAL),
7267 /*fn_p=*/NULL,
7268 complain));
7269 }
7270 else
7271 postfix_expression
7272 = finish_call_expr (postfix_expression, &args,
7273 /*disallow_virtual=*/false,
7274 /*koenig_p=*/false,
7275 complain);
7276 }
7277 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7278 || TREE_CODE (postfix_expression) == MEMBER_REF
7279 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7280 postfix_expression = (build_offset_ref_call_from_tree
7281 (postfix_expression, &args,
7282 complain));
7283 else if (idk == CP_ID_KIND_QUALIFIED)
7284 /* A call to a static class member, or a namespace-scope
7285 function. */
7286 postfix_expression
7287 = finish_call_expr (postfix_expression, &args,
7288 /*disallow_virtual=*/true,
7289 koenig_p,
7290 complain);
7291 else
7292 /* All other function calls. */
7293 postfix_expression
7294 = finish_call_expr (postfix_expression, &args,
7295 /*disallow_virtual=*/false,
7296 koenig_p,
7297 complain);
7298
7299 if (close_paren_loc != UNKNOWN_LOCATION)
7300 {
7301 location_t combined_loc = make_location (token->location,
7302 start_loc,
7303 close_paren_loc);
7304 postfix_expression.set_location (combined_loc);
7305 }
7306
7307 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7308 idk = CP_ID_KIND_NONE;
7309
7310 release_tree_vector (args);
7311 }
7312 break;
7313
7314 case CPP_DOT:
7315 case CPP_DEREF:
7316 /* postfix-expression . template [opt] id-expression
7317 postfix-expression . pseudo-destructor-name
7318 postfix-expression -> template [opt] id-expression
7319 postfix-expression -> pseudo-destructor-name */
7320
7321 /* Consume the `.' or `->' operator. */
7322 cp_lexer_consume_token (parser->lexer);
7323
7324 postfix_expression
7325 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7326 postfix_expression,
7327 false, &idk, loc);
7328
7329 is_member_access = true;
7330 break;
7331
7332 case CPP_PLUS_PLUS:
7333 /* postfix-expression ++ */
7334 /* Consume the `++' token. */
7335 cp_lexer_consume_token (parser->lexer);
7336 /* Generate a representation for the complete expression. */
7337 postfix_expression
7338 = finish_increment_expr (postfix_expression,
7339 POSTINCREMENT_EXPR);
7340 /* Increments may not appear in constant-expressions. */
7341 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7342 postfix_expression = error_mark_node;
7343 idk = CP_ID_KIND_NONE;
7344 is_member_access = false;
7345 break;
7346
7347 case CPP_MINUS_MINUS:
7348 /* postfix-expression -- */
7349 /* Consume the `--' token. */
7350 cp_lexer_consume_token (parser->lexer);
7351 /* Generate a representation for the complete expression. */
7352 postfix_expression
7353 = finish_increment_expr (postfix_expression,
7354 POSTDECREMENT_EXPR);
7355 /* Decrements may not appear in constant-expressions. */
7356 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7357 postfix_expression = error_mark_node;
7358 idk = CP_ID_KIND_NONE;
7359 is_member_access = false;
7360 break;
7361
7362 default:
7363 if (pidk_return != NULL)
7364 * pidk_return = idk;
7365 if (member_access_only_p)
7366 return is_member_access
7367 ? postfix_expression
7368 : cp_expr (error_mark_node);
7369 else
7370 return postfix_expression;
7371 }
7372 }
7373
7374 /* We should never get here. */
7375 gcc_unreachable ();
7376 return error_mark_node;
7377 }
7378
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7381
7382 postfix-expression [ expression ]
7383 postfix-expression [ braced-init-list ] (C++11)
7384
7385 FOR_OFFSETOF is set if we're being called in that context, which
7386 changes how we deal with integer constant expressions. */
7387
7388 static tree
7389 cp_parser_postfix_open_square_expression (cp_parser *parser,
7390 tree postfix_expression,
7391 bool for_offsetof,
7392 bool decltype_p)
7393 {
7394 tree index = NULL_TREE;
7395 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7396 bool saved_greater_than_is_operator_p;
7397
7398 /* Consume the `[' token. */
7399 cp_lexer_consume_token (parser->lexer);
7400
7401 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7402 parser->greater_than_is_operator_p = true;
7403
7404 /* Parse the index expression. */
7405 /* ??? For offsetof, there is a question of what to allow here. If
7406 offsetof is not being used in an integral constant expression context,
7407 then we *could* get the right answer by computing the value at runtime.
7408 If we are in an integral constant expression context, then we might
7409 could accept any constant expression; hard to say without analysis.
7410 Rather than open the barn door too wide right away, allow only integer
7411 constant expressions here. */
7412 if (for_offsetof)
7413 index = cp_parser_constant_expression (parser);
7414 else
7415 {
7416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7417 {
7418 bool expr_nonconst_p;
7419 cp_lexer_set_source_position (parser->lexer);
7420 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7421 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7422 }
7423 else
7424 index = cp_parser_expression (parser);
7425 }
7426
7427 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7428
7429 /* Look for the closing `]'. */
7430 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7431
7432 /* Build the ARRAY_REF. */
7433 postfix_expression = grok_array_decl (loc, postfix_expression,
7434 index, decltype_p);
7435
7436 /* When not doing offsetof, array references are not permitted in
7437 constant-expressions. */
7438 if (!for_offsetof
7439 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7440 postfix_expression = error_mark_node;
7441
7442 return postfix_expression;
7443 }
7444
7445 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7446 dereference of incomplete type, returns true if error_mark_node should
7447 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7448 and *DEPENDENT_P. */
7449
7450 bool
7451 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7452 bool *dependent_p)
7453 {
7454 /* In a template, be permissive by treating an object expression
7455 of incomplete type as dependent (after a pedwarn). */
7456 diagnostic_t kind = (processing_template_decl
7457 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7458
7459 switch (TREE_CODE (*postfix_expression))
7460 {
7461 case CAST_EXPR:
7462 case REINTERPRET_CAST_EXPR:
7463 case CONST_CAST_EXPR:
7464 case STATIC_CAST_EXPR:
7465 case DYNAMIC_CAST_EXPR:
7466 case IMPLICIT_CONV_EXPR:
7467 case VIEW_CONVERT_EXPR:
7468 case NON_LVALUE_EXPR:
7469 kind = DK_ERROR;
7470 break;
7471 case OVERLOAD:
7472 /* Don't emit any diagnostic for OVERLOADs. */
7473 kind = DK_IGNORED;
7474 break;
7475 default:
7476 /* Avoid clobbering e.g. DECLs. */
7477 if (!EXPR_P (*postfix_expression))
7478 kind = DK_ERROR;
7479 break;
7480 }
7481
7482 if (kind == DK_IGNORED)
7483 return false;
7484
7485 location_t exploc = location_of (*postfix_expression);
7486 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7487 if (!MAYBE_CLASS_TYPE_P (*scope))
7488 return true;
7489 if (kind == DK_ERROR)
7490 *scope = *postfix_expression = error_mark_node;
7491 else if (processing_template_decl)
7492 {
7493 *dependent_p = true;
7494 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7495 }
7496 return false;
7497 }
7498
7499 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7500 by cp_parser_builtin_offsetof. We're looking for
7501
7502 postfix-expression . template [opt] id-expression
7503 postfix-expression . pseudo-destructor-name
7504 postfix-expression -> template [opt] id-expression
7505 postfix-expression -> pseudo-destructor-name
7506
7507 FOR_OFFSETOF is set if we're being called in that context. That sorta
7508 limits what of the above we'll actually accept, but nevermind.
7509 TOKEN_TYPE is the "." or "->" token, which will already have been
7510 removed from the stream. */
7511
7512 static tree
7513 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7514 enum cpp_ttype token_type,
7515 cp_expr postfix_expression,
7516 bool for_offsetof, cp_id_kind *idk,
7517 location_t location)
7518 {
7519 tree name;
7520 bool dependent_p;
7521 bool pseudo_destructor_p;
7522 tree scope = NULL_TREE;
7523 location_t start_loc = postfix_expression.get_start ();
7524
7525 /* If this is a `->' operator, dereference the pointer. */
7526 if (token_type == CPP_DEREF)
7527 postfix_expression = build_x_arrow (location, postfix_expression,
7528 tf_warning_or_error);
7529 /* Check to see whether or not the expression is type-dependent and
7530 not the current instantiation. */
7531 dependent_p = type_dependent_object_expression_p (postfix_expression);
7532 /* The identifier following the `->' or `.' is not qualified. */
7533 parser->scope = NULL_TREE;
7534 parser->qualifying_scope = NULL_TREE;
7535 parser->object_scope = NULL_TREE;
7536 *idk = CP_ID_KIND_NONE;
7537
7538 /* Enter the scope corresponding to the type of the object
7539 given by the POSTFIX_EXPRESSION. */
7540 if (!dependent_p)
7541 {
7542 scope = TREE_TYPE (postfix_expression);
7543 /* According to the standard, no expression should ever have
7544 reference type. Unfortunately, we do not currently match
7545 the standard in this respect in that our internal representation
7546 of an expression may have reference type even when the standard
7547 says it does not. Therefore, we have to manually obtain the
7548 underlying type here. */
7549 scope = non_reference (scope);
7550 /* The type of the POSTFIX_EXPRESSION must be complete. */
7551 /* Unlike the object expression in other contexts, *this is not
7552 required to be of complete type for purposes of class member
7553 access (5.2.5) outside the member function body. */
7554 if (postfix_expression != current_class_ref
7555 && scope != error_mark_node
7556 && !currently_open_class (scope))
7557 {
7558 scope = complete_type (scope);
7559 if (!COMPLETE_TYPE_P (scope)
7560 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7561 &dependent_p))
7562 return error_mark_node;
7563 }
7564
7565 if (!dependent_p)
7566 {
7567 /* Let the name lookup machinery know that we are processing a
7568 class member access expression. */
7569 parser->context->object_type = scope;
7570 /* If something went wrong, we want to be able to discern that case,
7571 as opposed to the case where there was no SCOPE due to the type
7572 of expression being dependent. */
7573 if (!scope)
7574 scope = error_mark_node;
7575 /* If the SCOPE was erroneous, make the various semantic analysis
7576 functions exit quickly -- and without issuing additional error
7577 messages. */
7578 if (scope == error_mark_node)
7579 postfix_expression = error_mark_node;
7580 }
7581 }
7582
7583 if (dependent_p)
7584 /* Tell cp_parser_lookup_name that there was an object, even though it's
7585 type-dependent. */
7586 parser->context->object_type = unknown_type_node;
7587
7588 /* Assume this expression is not a pseudo-destructor access. */
7589 pseudo_destructor_p = false;
7590
7591 /* If the SCOPE is a scalar type, then, if this is a valid program,
7592 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7593 is type dependent, it can be pseudo-destructor-name or something else.
7594 Try to parse it as pseudo-destructor-name first. */
7595 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7596 {
7597 tree s;
7598 tree type;
7599
7600 cp_parser_parse_tentatively (parser);
7601 /* Parse the pseudo-destructor-name. */
7602 s = NULL_TREE;
7603 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7604 &s, &type);
7605 if (dependent_p
7606 && (cp_parser_error_occurred (parser)
7607 || !SCALAR_TYPE_P (type)))
7608 cp_parser_abort_tentative_parse (parser);
7609 else if (cp_parser_parse_definitely (parser))
7610 {
7611 pseudo_destructor_p = true;
7612 postfix_expression
7613 = finish_pseudo_destructor_expr (postfix_expression,
7614 s, type, location);
7615 }
7616 }
7617
7618 if (!pseudo_destructor_p)
7619 {
7620 /* If the SCOPE is not a scalar type, we are looking at an
7621 ordinary class member access expression, rather than a
7622 pseudo-destructor-name. */
7623 bool template_p;
7624 cp_token *token = cp_lexer_peek_token (parser->lexer);
7625 /* Parse the id-expression. */
7626 name = (cp_parser_id_expression
7627 (parser,
7628 cp_parser_optional_template_keyword (parser),
7629 /*check_dependency_p=*/true,
7630 &template_p,
7631 /*declarator_p=*/false,
7632 /*optional_p=*/false));
7633 /* In general, build a SCOPE_REF if the member name is qualified.
7634 However, if the name was not dependent and has already been
7635 resolved; there is no need to build the SCOPE_REF. For example;
7636
7637 struct X { void f(); };
7638 template <typename T> void f(T* t) { t->X::f(); }
7639
7640 Even though "t" is dependent, "X::f" is not and has been resolved
7641 to a BASELINK; there is no need to include scope information. */
7642
7643 /* But we do need to remember that there was an explicit scope for
7644 virtual function calls. */
7645 if (parser->scope)
7646 *idk = CP_ID_KIND_QUALIFIED;
7647
7648 /* If the name is a template-id that names a type, we will get a
7649 TYPE_DECL here. That is invalid code. */
7650 if (TREE_CODE (name) == TYPE_DECL)
7651 {
7652 error_at (token->location, "invalid use of %qD", name);
7653 postfix_expression = error_mark_node;
7654 }
7655 else
7656 {
7657 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7658 {
7659 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7660 {
7661 error_at (token->location, "%<%D::%D%> is not a class member",
7662 parser->scope, name);
7663 postfix_expression = error_mark_node;
7664 }
7665 else
7666 name = build_qualified_name (/*type=*/NULL_TREE,
7667 parser->scope,
7668 name,
7669 template_p);
7670 parser->scope = NULL_TREE;
7671 parser->qualifying_scope = NULL_TREE;
7672 parser->object_scope = NULL_TREE;
7673 }
7674 if (parser->scope && name && BASELINK_P (name))
7675 adjust_result_of_qualified_name_lookup
7676 (name, parser->scope, scope);
7677 postfix_expression
7678 = finish_class_member_access_expr (postfix_expression, name,
7679 template_p,
7680 tf_warning_or_error);
7681 /* Build a location e.g.:
7682 ptr->access_expr
7683 ~~~^~~~~~~~~~~~~
7684 where the caret is at the deref token, ranging from
7685 the start of postfix_expression to the end of the access expr. */
7686 location_t end_loc
7687 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7688 location_t combined_loc
7689 = make_location (input_location, start_loc, end_loc);
7690 protected_set_expr_location (postfix_expression, combined_loc);
7691 }
7692 }
7693
7694 /* We no longer need to look up names in the scope of the object on
7695 the left-hand side of the `.' or `->' operator. */
7696 parser->context->object_type = NULL_TREE;
7697
7698 /* Outside of offsetof, these operators may not appear in
7699 constant-expressions. */
7700 if (!for_offsetof
7701 && (cp_parser_non_integral_constant_expression
7702 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7703 postfix_expression = error_mark_node;
7704
7705 return postfix_expression;
7706 }
7707
7708 /* Parse a parenthesized expression-list.
7709
7710 expression-list:
7711 assignment-expression
7712 expression-list, assignment-expression
7713
7714 attribute-list:
7715 expression-list
7716 identifier
7717 identifier, expression-list
7718
7719 CAST_P is true if this expression is the target of a cast.
7720
7721 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7722 argument pack.
7723
7724 WRAP_LOCATIONS_P is true if expressions within this list for which
7725 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7726 their source locations.
7727
7728 Returns a vector of trees. Each element is a representation of an
7729 assignment-expression. NULL is returned if the ( and or ) are
7730 missing. An empty, but allocated, vector is returned on no
7731 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7732 if we are parsing an attribute list for an attribute that wants a
7733 plain identifier argument, normal_attr for an attribute that wants
7734 an expression, or non_attr if we aren't parsing an attribute list. If
7735 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7736 not all of the expressions in the list were constant.
7737 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7738 will be written to with the location of the closing parenthesis. If
7739 an error occurs, it may or may not be written to. */
7740
7741 static vec<tree, va_gc> *
7742 cp_parser_parenthesized_expression_list (cp_parser* parser,
7743 int is_attribute_list,
7744 bool cast_p,
7745 bool allow_expansion_p,
7746 bool *non_constant_p,
7747 location_t *close_paren_loc,
7748 bool wrap_locations_p)
7749 {
7750 vec<tree, va_gc> *expression_list;
7751 bool fold_expr_p = is_attribute_list != non_attr;
7752 tree identifier = NULL_TREE;
7753 bool saved_greater_than_is_operator_p;
7754
7755 /* Assume all the expressions will be constant. */
7756 if (non_constant_p)
7757 *non_constant_p = false;
7758
7759 matching_parens parens;
7760 if (!parens.require_open (parser))
7761 return NULL;
7762
7763 expression_list = make_tree_vector ();
7764
7765 /* Within a parenthesized expression, a `>' token is always
7766 the greater-than operator. */
7767 saved_greater_than_is_operator_p
7768 = parser->greater_than_is_operator_p;
7769 parser->greater_than_is_operator_p = true;
7770
7771 cp_expr expr (NULL_TREE);
7772
7773 /* Consume expressions until there are no more. */
7774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7775 while (true)
7776 {
7777 /* At the beginning of attribute lists, check to see if the
7778 next token is an identifier. */
7779 if (is_attribute_list == id_attr
7780 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7781 {
7782 cp_token *token;
7783
7784 /* Consume the identifier. */
7785 token = cp_lexer_consume_token (parser->lexer);
7786 /* Save the identifier. */
7787 identifier = token->u.value;
7788 }
7789 else
7790 {
7791 bool expr_non_constant_p;
7792
7793 /* Parse the next assignment-expression. */
7794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7795 {
7796 /* A braced-init-list. */
7797 cp_lexer_set_source_position (parser->lexer);
7798 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7799 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7800 if (non_constant_p && expr_non_constant_p)
7801 *non_constant_p = true;
7802 }
7803 else if (non_constant_p)
7804 {
7805 expr = (cp_parser_constant_expression
7806 (parser, /*allow_non_constant_p=*/true,
7807 &expr_non_constant_p));
7808 if (expr_non_constant_p)
7809 *non_constant_p = true;
7810 }
7811 else
7812 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7813 cast_p);
7814
7815 if (fold_expr_p)
7816 expr = instantiate_non_dependent_expr (expr);
7817
7818 /* If we have an ellipsis, then this is an expression
7819 expansion. */
7820 if (allow_expansion_p
7821 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7822 {
7823 /* Consume the `...'. */
7824 cp_lexer_consume_token (parser->lexer);
7825
7826 /* Build the argument pack. */
7827 expr = make_pack_expansion (expr);
7828 }
7829
7830 if (wrap_locations_p)
7831 expr.maybe_add_location_wrapper ();
7832
7833 /* Add it to the list. We add error_mark_node
7834 expressions to the list, so that we can still tell if
7835 the correct form for a parenthesized expression-list
7836 is found. That gives better errors. */
7837 vec_safe_push (expression_list, expr.get_value ());
7838
7839 if (expr == error_mark_node)
7840 goto skip_comma;
7841 }
7842
7843 /* After the first item, attribute lists look the same as
7844 expression lists. */
7845 is_attribute_list = non_attr;
7846
7847 get_comma:;
7848 /* If the next token isn't a `,', then we are done. */
7849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7850 break;
7851
7852 /* Otherwise, consume the `,' and keep going. */
7853 cp_lexer_consume_token (parser->lexer);
7854 }
7855
7856 if (close_paren_loc)
7857 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7858
7859 if (!parens.require_close (parser))
7860 {
7861 int ending;
7862
7863 skip_comma:;
7864 /* We try and resync to an unnested comma, as that will give the
7865 user better diagnostics. */
7866 ending = cp_parser_skip_to_closing_parenthesis (parser,
7867 /*recovering=*/true,
7868 /*or_comma=*/true,
7869 /*consume_paren=*/true);
7870 if (ending < 0)
7871 goto get_comma;
7872 if (!ending)
7873 {
7874 parser->greater_than_is_operator_p
7875 = saved_greater_than_is_operator_p;
7876 return NULL;
7877 }
7878 }
7879
7880 parser->greater_than_is_operator_p
7881 = saved_greater_than_is_operator_p;
7882
7883 if (identifier)
7884 vec_safe_insert (expression_list, 0, identifier);
7885
7886 return expression_list;
7887 }
7888
7889 /* Parse a pseudo-destructor-name.
7890
7891 pseudo-destructor-name:
7892 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7893 :: [opt] nested-name-specifier template template-id :: ~ type-name
7894 :: [opt] nested-name-specifier [opt] ~ type-name
7895
7896 If either of the first two productions is used, sets *SCOPE to the
7897 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7898 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7899 or ERROR_MARK_NODE if the parse fails. */
7900
7901 static void
7902 cp_parser_pseudo_destructor_name (cp_parser* parser,
7903 tree object,
7904 tree* scope,
7905 tree* type)
7906 {
7907 bool nested_name_specifier_p;
7908
7909 /* Handle ~auto. */
7910 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7911 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7912 && !type_dependent_expression_p (object))
7913 {
7914 if (cxx_dialect < cxx14)
7915 pedwarn (input_location, 0,
7916 "%<~auto%> only available with "
7917 "-std=c++14 or -std=gnu++14");
7918 cp_lexer_consume_token (parser->lexer);
7919 cp_lexer_consume_token (parser->lexer);
7920 *scope = NULL_TREE;
7921 *type = TREE_TYPE (object);
7922 return;
7923 }
7924
7925 /* Assume that things will not work out. */
7926 *type = error_mark_node;
7927
7928 /* Look for the optional `::' operator. */
7929 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7930 /* Look for the optional nested-name-specifier. */
7931 nested_name_specifier_p
7932 = (cp_parser_nested_name_specifier_opt (parser,
7933 /*typename_keyword_p=*/false,
7934 /*check_dependency_p=*/true,
7935 /*type_p=*/false,
7936 /*is_declaration=*/false)
7937 != NULL_TREE);
7938 /* Now, if we saw a nested-name-specifier, we might be doing the
7939 second production. */
7940 if (nested_name_specifier_p
7941 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7942 {
7943 /* Consume the `template' keyword. */
7944 cp_lexer_consume_token (parser->lexer);
7945 /* Parse the template-id. */
7946 cp_parser_template_id (parser,
7947 /*template_keyword_p=*/true,
7948 /*check_dependency_p=*/false,
7949 class_type,
7950 /*is_declaration=*/true);
7951 /* Look for the `::' token. */
7952 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7953 }
7954 /* If the next token is not a `~', then there might be some
7955 additional qualification. */
7956 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7957 {
7958 /* At this point, we're looking for "type-name :: ~". The type-name
7959 must not be a class-name, since this is a pseudo-destructor. So,
7960 it must be either an enum-name, or a typedef-name -- both of which
7961 are just identifiers. So, we peek ahead to check that the "::"
7962 and "~" tokens are present; if they are not, then we can avoid
7963 calling type_name. */
7964 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7965 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7966 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7967 {
7968 cp_parser_error (parser, "non-scalar type");
7969 return;
7970 }
7971
7972 /* Look for the type-name. */
7973 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7974 if (*scope == error_mark_node)
7975 return;
7976
7977 /* Look for the `::' token. */
7978 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7979 }
7980 else
7981 *scope = NULL_TREE;
7982
7983 /* Look for the `~'. */
7984 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7985
7986 /* Once we see the ~, this has to be a pseudo-destructor. */
7987 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7988 cp_parser_commit_to_topmost_tentative_parse (parser);
7989
7990 /* Look for the type-name again. We are not responsible for
7991 checking that it matches the first type-name. */
7992 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7993 }
7994
7995 /* Parse a unary-expression.
7996
7997 unary-expression:
7998 postfix-expression
7999 ++ cast-expression
8000 -- cast-expression
8001 unary-operator cast-expression
8002 sizeof unary-expression
8003 sizeof ( type-id )
8004 alignof ( type-id ) [C++0x]
8005 new-expression
8006 delete-expression
8007
8008 GNU Extensions:
8009
8010 unary-expression:
8011 __extension__ cast-expression
8012 __alignof__ unary-expression
8013 __alignof__ ( type-id )
8014 alignof unary-expression [C++0x]
8015 __real__ cast-expression
8016 __imag__ cast-expression
8017 && identifier
8018 sizeof ( type-id ) { initializer-list , [opt] }
8019 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8020 __alignof__ ( type-id ) { initializer-list , [opt] }
8021
8022 ADDRESS_P is true iff the unary-expression is appearing as the
8023 operand of the `&' operator. CAST_P is true if this expression is
8024 the target of a cast.
8025
8026 Returns a representation of the expression. */
8027
8028 static cp_expr
8029 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8030 bool address_p, bool cast_p, bool decltype_p)
8031 {
8032 cp_token *token;
8033 enum tree_code unary_operator;
8034
8035 /* Peek at the next token. */
8036 token = cp_lexer_peek_token (parser->lexer);
8037 /* Some keywords give away the kind of expression. */
8038 if (token->type == CPP_KEYWORD)
8039 {
8040 enum rid keyword = token->keyword;
8041
8042 switch (keyword)
8043 {
8044 case RID_ALIGNOF:
8045 case RID_SIZEOF:
8046 {
8047 tree operand, ret;
8048 enum tree_code op;
8049 location_t start_loc = token->location;
8050
8051 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8052 bool std_alignof = id_equal (token->u.value, "alignof");
8053
8054 /* Consume the token. */
8055 cp_lexer_consume_token (parser->lexer);
8056 /* Parse the operand. */
8057 operand = cp_parser_sizeof_operand (parser, keyword);
8058
8059 if (TYPE_P (operand))
8060 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8061 true);
8062 else
8063 {
8064 /* ISO C++ defines alignof only with types, not with
8065 expressions. So pedwarn if alignof is used with a non-
8066 type expression. However, __alignof__ is ok. */
8067 if (std_alignof)
8068 pedwarn (token->location, OPT_Wpedantic,
8069 "ISO C++ does not allow %<alignof%> "
8070 "with a non-type");
8071
8072 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8073 }
8074 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8075 SIZEOF_EXPR with the original operand. */
8076 if (op == SIZEOF_EXPR && ret != error_mark_node)
8077 {
8078 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8079 {
8080 if (!processing_template_decl && TYPE_P (operand))
8081 {
8082 ret = build_min (SIZEOF_EXPR, size_type_node,
8083 build1 (NOP_EXPR, operand,
8084 error_mark_node));
8085 SIZEOF_EXPR_TYPE_P (ret) = 1;
8086 }
8087 else
8088 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8089 TREE_SIDE_EFFECTS (ret) = 0;
8090 TREE_READONLY (ret) = 1;
8091 }
8092 }
8093
8094 /* Construct a location e.g. :
8095 alignof (expr)
8096 ^~~~~~~~~~~~~~
8097 with start == caret at the start of the "alignof"/"sizeof"
8098 token, with the endpoint at the final closing paren. */
8099 location_t finish_loc
8100 = cp_lexer_previous_token (parser->lexer)->location;
8101 location_t compound_loc
8102 = make_location (start_loc, start_loc, finish_loc);
8103
8104 cp_expr ret_expr (ret);
8105 ret_expr.set_location (compound_loc);
8106 ret_expr = ret_expr.maybe_add_location_wrapper ();
8107 return ret_expr;
8108 }
8109
8110 case RID_NEW:
8111 return cp_parser_new_expression (parser);
8112
8113 case RID_DELETE:
8114 return cp_parser_delete_expression (parser);
8115
8116 case RID_EXTENSION:
8117 {
8118 /* The saved value of the PEDANTIC flag. */
8119 int saved_pedantic;
8120 tree expr;
8121
8122 /* Save away the PEDANTIC flag. */
8123 cp_parser_extension_opt (parser, &saved_pedantic);
8124 /* Parse the cast-expression. */
8125 expr = cp_parser_simple_cast_expression (parser);
8126 /* Restore the PEDANTIC flag. */
8127 pedantic = saved_pedantic;
8128
8129 return expr;
8130 }
8131
8132 case RID_REALPART:
8133 case RID_IMAGPART:
8134 {
8135 tree expression;
8136
8137 /* Consume the `__real__' or `__imag__' token. */
8138 cp_lexer_consume_token (parser->lexer);
8139 /* Parse the cast-expression. */
8140 expression = cp_parser_simple_cast_expression (parser);
8141 /* Create the complete representation. */
8142 return build_x_unary_op (token->location,
8143 (keyword == RID_REALPART
8144 ? REALPART_EXPR : IMAGPART_EXPR),
8145 expression,
8146 tf_warning_or_error);
8147 }
8148 break;
8149
8150 case RID_TRANSACTION_ATOMIC:
8151 case RID_TRANSACTION_RELAXED:
8152 return cp_parser_transaction_expression (parser, keyword);
8153
8154 case RID_NOEXCEPT:
8155 {
8156 tree expr;
8157 const char *saved_message;
8158 bool saved_integral_constant_expression_p;
8159 bool saved_non_integral_constant_expression_p;
8160 bool saved_greater_than_is_operator_p;
8161
8162 location_t start_loc = token->location;
8163
8164 cp_lexer_consume_token (parser->lexer);
8165 matching_parens parens;
8166 parens.require_open (parser);
8167
8168 saved_message = parser->type_definition_forbidden_message;
8169 parser->type_definition_forbidden_message
8170 = G_("types may not be defined in %<noexcept%> expressions");
8171
8172 saved_integral_constant_expression_p
8173 = parser->integral_constant_expression_p;
8174 saved_non_integral_constant_expression_p
8175 = parser->non_integral_constant_expression_p;
8176 parser->integral_constant_expression_p = false;
8177
8178 saved_greater_than_is_operator_p
8179 = parser->greater_than_is_operator_p;
8180 parser->greater_than_is_operator_p = true;
8181
8182 ++cp_unevaluated_operand;
8183 ++c_inhibit_evaluation_warnings;
8184 ++cp_noexcept_operand;
8185 expr = cp_parser_expression (parser);
8186 --cp_noexcept_operand;
8187 --c_inhibit_evaluation_warnings;
8188 --cp_unevaluated_operand;
8189
8190 parser->greater_than_is_operator_p
8191 = saved_greater_than_is_operator_p;
8192
8193 parser->integral_constant_expression_p
8194 = saved_integral_constant_expression_p;
8195 parser->non_integral_constant_expression_p
8196 = saved_non_integral_constant_expression_p;
8197
8198 parser->type_definition_forbidden_message = saved_message;
8199
8200 location_t finish_loc
8201 = cp_lexer_peek_token (parser->lexer)->location;
8202 parens.require_close (parser);
8203
8204 /* Construct a location of the form:
8205 noexcept (expr)
8206 ^~~~~~~~~~~~~~~
8207 with start == caret, finishing at the close-paren. */
8208 location_t noexcept_loc
8209 = make_location (start_loc, start_loc, finish_loc);
8210
8211 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8212 noexcept_loc);
8213 }
8214
8215 default:
8216 break;
8217 }
8218 }
8219
8220 /* Look for the `:: new' and `:: delete', which also signal the
8221 beginning of a new-expression, or delete-expression,
8222 respectively. If the next token is `::', then it might be one of
8223 these. */
8224 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8225 {
8226 enum rid keyword;
8227
8228 /* See if the token after the `::' is one of the keywords in
8229 which we're interested. */
8230 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8231 /* If it's `new', we have a new-expression. */
8232 if (keyword == RID_NEW)
8233 return cp_parser_new_expression (parser);
8234 /* Similarly, for `delete'. */
8235 else if (keyword == RID_DELETE)
8236 return cp_parser_delete_expression (parser);
8237 }
8238
8239 /* Look for a unary operator. */
8240 unary_operator = cp_parser_unary_operator (token);
8241 /* The `++' and `--' operators can be handled similarly, even though
8242 they are not technically unary-operators in the grammar. */
8243 if (unary_operator == ERROR_MARK)
8244 {
8245 if (token->type == CPP_PLUS_PLUS)
8246 unary_operator = PREINCREMENT_EXPR;
8247 else if (token->type == CPP_MINUS_MINUS)
8248 unary_operator = PREDECREMENT_EXPR;
8249 /* Handle the GNU address-of-label extension. */
8250 else if (cp_parser_allow_gnu_extensions_p (parser)
8251 && token->type == CPP_AND_AND)
8252 {
8253 tree identifier;
8254 tree expression;
8255 location_t start_loc = token->location;
8256
8257 /* Consume the '&&' token. */
8258 cp_lexer_consume_token (parser->lexer);
8259 /* Look for the identifier. */
8260 location_t finish_loc
8261 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8262 identifier = cp_parser_identifier (parser);
8263 /* Construct a location of the form:
8264 &&label
8265 ^~~~~~~
8266 with caret==start at the "&&", finish at the end of the label. */
8267 location_t combined_loc
8268 = make_location (start_loc, start_loc, finish_loc);
8269 /* Create an expression representing the address. */
8270 expression = finish_label_address_expr (identifier, combined_loc);
8271 if (cp_parser_non_integral_constant_expression (parser,
8272 NIC_ADDR_LABEL))
8273 expression = error_mark_node;
8274 return expression;
8275 }
8276 }
8277 if (unary_operator != ERROR_MARK)
8278 {
8279 cp_expr cast_expression;
8280 cp_expr expression = error_mark_node;
8281 non_integral_constant non_constant_p = NIC_NONE;
8282 location_t loc = token->location;
8283 tsubst_flags_t complain = complain_flags (decltype_p);
8284
8285 /* Consume the operator token. */
8286 token = cp_lexer_consume_token (parser->lexer);
8287 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8288
8289 /* Parse the cast-expression. */
8290 cast_expression
8291 = cp_parser_cast_expression (parser,
8292 unary_operator == ADDR_EXPR,
8293 /*cast_p=*/false,
8294 /*decltype*/false,
8295 pidk);
8296
8297 /* Make a location:
8298 OP_TOKEN CAST_EXPRESSION
8299 ^~~~~~~~~~~~~~~~~~~~~~~~~
8300 with start==caret at the operator token, and
8301 extending to the end of the cast_expression. */
8302 loc = make_location (loc, loc, cast_expression.get_finish ());
8303
8304 /* Now, build an appropriate representation. */
8305 switch (unary_operator)
8306 {
8307 case INDIRECT_REF:
8308 non_constant_p = NIC_STAR;
8309 expression = build_x_indirect_ref (loc, cast_expression,
8310 RO_UNARY_STAR,
8311 complain);
8312 /* TODO: build_x_indirect_ref does not always honor the
8313 location, so ensure it is set. */
8314 expression.set_location (loc);
8315 break;
8316
8317 case ADDR_EXPR:
8318 non_constant_p = NIC_ADDR;
8319 /* Fall through. */
8320 case BIT_NOT_EXPR:
8321 expression = build_x_unary_op (loc, unary_operator,
8322 cast_expression,
8323 complain);
8324 /* TODO: build_x_unary_op does not always honor the location,
8325 so ensure it is set. */
8326 expression.set_location (loc);
8327 break;
8328
8329 case PREINCREMENT_EXPR:
8330 case PREDECREMENT_EXPR:
8331 non_constant_p = unary_operator == PREINCREMENT_EXPR
8332 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8333 /* Fall through. */
8334 case NEGATE_EXPR:
8335 /* Immediately fold negation of a constant, unless the constant is 0
8336 (since -0 == 0) or it would overflow. */
8337 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8338 && CONSTANT_CLASS_P (cast_expression)
8339 && !integer_zerop (cast_expression)
8340 && !TREE_OVERFLOW (cast_expression))
8341 {
8342 tree folded = fold_build1 (unary_operator,
8343 TREE_TYPE (cast_expression),
8344 cast_expression);
8345 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8346 {
8347 expression = cp_expr (folded, loc);
8348 break;
8349 }
8350 }
8351 /* Fall through. */
8352 case UNARY_PLUS_EXPR:
8353 case TRUTH_NOT_EXPR:
8354 expression = finish_unary_op_expr (loc, unary_operator,
8355 cast_expression, complain);
8356 break;
8357
8358 default:
8359 gcc_unreachable ();
8360 }
8361
8362 if (non_constant_p != NIC_NONE
8363 && cp_parser_non_integral_constant_expression (parser,
8364 non_constant_p))
8365 expression = error_mark_node;
8366
8367 return expression;
8368 }
8369
8370 return cp_parser_postfix_expression (parser, address_p, cast_p,
8371 /*member_access_only_p=*/false,
8372 decltype_p,
8373 pidk);
8374 }
8375
8376 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8377 unary-operator, the corresponding tree code is returned. */
8378
8379 static enum tree_code
8380 cp_parser_unary_operator (cp_token* token)
8381 {
8382 switch (token->type)
8383 {
8384 case CPP_MULT:
8385 return INDIRECT_REF;
8386
8387 case CPP_AND:
8388 return ADDR_EXPR;
8389
8390 case CPP_PLUS:
8391 return UNARY_PLUS_EXPR;
8392
8393 case CPP_MINUS:
8394 return NEGATE_EXPR;
8395
8396 case CPP_NOT:
8397 return TRUTH_NOT_EXPR;
8398
8399 case CPP_COMPL:
8400 return BIT_NOT_EXPR;
8401
8402 default:
8403 return ERROR_MARK;
8404 }
8405 }
8406
8407 /* Parse a new-expression.
8408
8409 new-expression:
8410 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8411 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8412
8413 Returns a representation of the expression. */
8414
8415 static tree
8416 cp_parser_new_expression (cp_parser* parser)
8417 {
8418 bool global_scope_p;
8419 vec<tree, va_gc> *placement;
8420 tree type;
8421 vec<tree, va_gc> *initializer;
8422 tree nelts = NULL_TREE;
8423 tree ret;
8424
8425 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8426
8427 /* Look for the optional `::' operator. */
8428 global_scope_p
8429 = (cp_parser_global_scope_opt (parser,
8430 /*current_scope_valid_p=*/false)
8431 != NULL_TREE);
8432 /* Look for the `new' operator. */
8433 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8434 /* There's no easy way to tell a new-placement from the
8435 `( type-id )' construct. */
8436 cp_parser_parse_tentatively (parser);
8437 /* Look for a new-placement. */
8438 placement = cp_parser_new_placement (parser);
8439 /* If that didn't work out, there's no new-placement. */
8440 if (!cp_parser_parse_definitely (parser))
8441 {
8442 if (placement != NULL)
8443 release_tree_vector (placement);
8444 placement = NULL;
8445 }
8446
8447 /* If the next token is a `(', then we have a parenthesized
8448 type-id. */
8449 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8450 {
8451 cp_token *token;
8452 const char *saved_message = parser->type_definition_forbidden_message;
8453
8454 /* Consume the `('. */
8455 matching_parens parens;
8456 parens.consume_open (parser);
8457
8458 /* Parse the type-id. */
8459 parser->type_definition_forbidden_message
8460 = G_("types may not be defined in a new-expression");
8461 {
8462 type_id_in_expr_sentinel s (parser);
8463 type = cp_parser_type_id (parser);
8464 }
8465 parser->type_definition_forbidden_message = saved_message;
8466
8467 /* Look for the closing `)'. */
8468 parens.require_close (parser);
8469 token = cp_lexer_peek_token (parser->lexer);
8470 /* There should not be a direct-new-declarator in this production,
8471 but GCC used to allowed this, so we check and emit a sensible error
8472 message for this case. */
8473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8474 {
8475 error_at (token->location,
8476 "array bound forbidden after parenthesized type-id");
8477 inform (token->location,
8478 "try removing the parentheses around the type-id");
8479 cp_parser_direct_new_declarator (parser);
8480 }
8481 }
8482 /* Otherwise, there must be a new-type-id. */
8483 else
8484 type = cp_parser_new_type_id (parser, &nelts);
8485
8486 /* If the next token is a `(' or '{', then we have a new-initializer. */
8487 cp_token *token = cp_lexer_peek_token (parser->lexer);
8488 if (token->type == CPP_OPEN_PAREN
8489 || token->type == CPP_OPEN_BRACE)
8490 initializer = cp_parser_new_initializer (parser);
8491 else
8492 initializer = NULL;
8493
8494 /* A new-expression may not appear in an integral constant
8495 expression. */
8496 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8497 ret = error_mark_node;
8498 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8499 of a new-type-id or type-id of a new-expression, the new-expression shall
8500 contain a new-initializer of the form ( assignment-expression )".
8501 Additionally, consistently with the spirit of DR 1467, we want to accept
8502 'new auto { 2 }' too. */
8503 else if ((ret = type_uses_auto (type))
8504 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8505 && (vec_safe_length (initializer) != 1
8506 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8507 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8508 {
8509 error_at (token->location,
8510 "initialization of new-expression for type %<auto%> "
8511 "requires exactly one element");
8512 ret = error_mark_node;
8513 }
8514 else
8515 {
8516 /* Construct a location e.g.:
8517 ptr = new int[100]
8518 ^~~~~~~~~~~~
8519 with caret == start at the start of the "new" token, and the end
8520 at the end of the final token we consumed. */
8521 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8522 location_t end_loc = get_finish (end_tok->location);
8523 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8524
8525 /* Create a representation of the new-expression. */
8526 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8527 tf_warning_or_error);
8528 protected_set_expr_location (ret, combined_loc);
8529 }
8530
8531 if (placement != NULL)
8532 release_tree_vector (placement);
8533 if (initializer != NULL)
8534 release_tree_vector (initializer);
8535
8536 return ret;
8537 }
8538
8539 /* Parse a new-placement.
8540
8541 new-placement:
8542 ( expression-list )
8543
8544 Returns the same representation as for an expression-list. */
8545
8546 static vec<tree, va_gc> *
8547 cp_parser_new_placement (cp_parser* parser)
8548 {
8549 vec<tree, va_gc> *expression_list;
8550
8551 /* Parse the expression-list. */
8552 expression_list = (cp_parser_parenthesized_expression_list
8553 (parser, non_attr, /*cast_p=*/false,
8554 /*allow_expansion_p=*/true,
8555 /*non_constant_p=*/NULL));
8556
8557 if (expression_list && expression_list->is_empty ())
8558 error ("expected expression-list or type-id");
8559
8560 return expression_list;
8561 }
8562
8563 /* Parse a new-type-id.
8564
8565 new-type-id:
8566 type-specifier-seq new-declarator [opt]
8567
8568 Returns the TYPE allocated. If the new-type-id indicates an array
8569 type, *NELTS is set to the number of elements in the last array
8570 bound; the TYPE will not include the last array bound. */
8571
8572 static tree
8573 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8574 {
8575 cp_decl_specifier_seq type_specifier_seq;
8576 cp_declarator *new_declarator;
8577 cp_declarator *declarator;
8578 cp_declarator *outer_declarator;
8579 const char *saved_message;
8580
8581 /* The type-specifier sequence must not contain type definitions.
8582 (It cannot contain declarations of new types either, but if they
8583 are not definitions we will catch that because they are not
8584 complete.) */
8585 saved_message = parser->type_definition_forbidden_message;
8586 parser->type_definition_forbidden_message
8587 = G_("types may not be defined in a new-type-id");
8588 /* Parse the type-specifier-seq. */
8589 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8590 /*is_trailing_return=*/false,
8591 &type_specifier_seq);
8592 /* Restore the old message. */
8593 parser->type_definition_forbidden_message = saved_message;
8594
8595 if (type_specifier_seq.type == error_mark_node)
8596 return error_mark_node;
8597
8598 /* Parse the new-declarator. */
8599 new_declarator = cp_parser_new_declarator_opt (parser);
8600
8601 /* Determine the number of elements in the last array dimension, if
8602 any. */
8603 *nelts = NULL_TREE;
8604 /* Skip down to the last array dimension. */
8605 declarator = new_declarator;
8606 outer_declarator = NULL;
8607 while (declarator && (declarator->kind == cdk_pointer
8608 || declarator->kind == cdk_ptrmem))
8609 {
8610 outer_declarator = declarator;
8611 declarator = declarator->declarator;
8612 }
8613 while (declarator
8614 && declarator->kind == cdk_array
8615 && declarator->declarator
8616 && declarator->declarator->kind == cdk_array)
8617 {
8618 outer_declarator = declarator;
8619 declarator = declarator->declarator;
8620 }
8621
8622 if (declarator && declarator->kind == cdk_array)
8623 {
8624 *nelts = declarator->u.array.bounds;
8625 if (*nelts == error_mark_node)
8626 *nelts = integer_one_node;
8627
8628 if (outer_declarator)
8629 outer_declarator->declarator = declarator->declarator;
8630 else
8631 new_declarator = NULL;
8632 }
8633
8634 return groktypename (&type_specifier_seq, new_declarator, false);
8635 }
8636
8637 /* Parse an (optional) new-declarator.
8638
8639 new-declarator:
8640 ptr-operator new-declarator [opt]
8641 direct-new-declarator
8642
8643 Returns the declarator. */
8644
8645 static cp_declarator *
8646 cp_parser_new_declarator_opt (cp_parser* parser)
8647 {
8648 enum tree_code code;
8649 tree type, std_attributes = NULL_TREE;
8650 cp_cv_quals cv_quals;
8651
8652 /* We don't know if there's a ptr-operator next, or not. */
8653 cp_parser_parse_tentatively (parser);
8654 /* Look for a ptr-operator. */
8655 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8656 /* If that worked, look for more new-declarators. */
8657 if (cp_parser_parse_definitely (parser))
8658 {
8659 cp_declarator *declarator;
8660
8661 /* Parse another optional declarator. */
8662 declarator = cp_parser_new_declarator_opt (parser);
8663
8664 declarator = cp_parser_make_indirect_declarator
8665 (code, type, cv_quals, declarator, std_attributes);
8666
8667 return declarator;
8668 }
8669
8670 /* If the next token is a `[', there is a direct-new-declarator. */
8671 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8672 return cp_parser_direct_new_declarator (parser);
8673
8674 return NULL;
8675 }
8676
8677 /* Parse a direct-new-declarator.
8678
8679 direct-new-declarator:
8680 [ expression ]
8681 direct-new-declarator [constant-expression]
8682
8683 */
8684
8685 static cp_declarator *
8686 cp_parser_direct_new_declarator (cp_parser* parser)
8687 {
8688 cp_declarator *declarator = NULL;
8689
8690 while (true)
8691 {
8692 tree expression;
8693 cp_token *token;
8694
8695 /* Look for the opening `['. */
8696 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8697
8698 token = cp_lexer_peek_token (parser->lexer);
8699 expression = cp_parser_expression (parser);
8700 /* The standard requires that the expression have integral
8701 type. DR 74 adds enumeration types. We believe that the
8702 real intent is that these expressions be handled like the
8703 expression in a `switch' condition, which also allows
8704 classes with a single conversion to integral or
8705 enumeration type. */
8706 if (!processing_template_decl)
8707 {
8708 expression
8709 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8710 expression,
8711 /*complain=*/true);
8712 if (!expression)
8713 {
8714 error_at (token->location,
8715 "expression in new-declarator must have integral "
8716 "or enumeration type");
8717 expression = error_mark_node;
8718 }
8719 }
8720
8721 /* Look for the closing `]'. */
8722 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8723
8724 /* Add this bound to the declarator. */
8725 declarator = make_array_declarator (declarator, expression);
8726
8727 /* If the next token is not a `[', then there are no more
8728 bounds. */
8729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8730 break;
8731 }
8732
8733 return declarator;
8734 }
8735
8736 /* Parse a new-initializer.
8737
8738 new-initializer:
8739 ( expression-list [opt] )
8740 braced-init-list
8741
8742 Returns a representation of the expression-list. */
8743
8744 static vec<tree, va_gc> *
8745 cp_parser_new_initializer (cp_parser* parser)
8746 {
8747 vec<tree, va_gc> *expression_list;
8748
8749 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8750 {
8751 tree t;
8752 bool expr_non_constant_p;
8753 cp_lexer_set_source_position (parser->lexer);
8754 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8755 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8756 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8757 expression_list = make_tree_vector_single (t);
8758 }
8759 else
8760 expression_list = (cp_parser_parenthesized_expression_list
8761 (parser, non_attr, /*cast_p=*/false,
8762 /*allow_expansion_p=*/true,
8763 /*non_constant_p=*/NULL));
8764
8765 return expression_list;
8766 }
8767
8768 /* Parse a delete-expression.
8769
8770 delete-expression:
8771 :: [opt] delete cast-expression
8772 :: [opt] delete [ ] cast-expression
8773
8774 Returns a representation of the expression. */
8775
8776 static tree
8777 cp_parser_delete_expression (cp_parser* parser)
8778 {
8779 bool global_scope_p;
8780 bool array_p;
8781 tree expression;
8782
8783 /* Look for the optional `::' operator. */
8784 global_scope_p
8785 = (cp_parser_global_scope_opt (parser,
8786 /*current_scope_valid_p=*/false)
8787 != NULL_TREE);
8788 /* Look for the `delete' keyword. */
8789 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8790 /* See if the array syntax is in use. */
8791 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8792 {
8793 /* Consume the `[' token. */
8794 cp_lexer_consume_token (parser->lexer);
8795 /* Look for the `]' token. */
8796 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8797 /* Remember that this is the `[]' construct. */
8798 array_p = true;
8799 }
8800 else
8801 array_p = false;
8802
8803 /* Parse the cast-expression. */
8804 expression = cp_parser_simple_cast_expression (parser);
8805
8806 /* A delete-expression may not appear in an integral constant
8807 expression. */
8808 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8809 return error_mark_node;
8810
8811 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8812 tf_warning_or_error);
8813 }
8814
8815 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8816 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8817 0 otherwise. */
8818
8819 static int
8820 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8821 {
8822 cp_token *token = cp_lexer_peek_token (parser->lexer);
8823 switch (token->type)
8824 {
8825 case CPP_COMMA:
8826 case CPP_SEMICOLON:
8827 case CPP_QUERY:
8828 case CPP_COLON:
8829 case CPP_CLOSE_SQUARE:
8830 case CPP_CLOSE_PAREN:
8831 case CPP_CLOSE_BRACE:
8832 case CPP_OPEN_BRACE:
8833 case CPP_DOT:
8834 case CPP_DOT_STAR:
8835 case CPP_DEREF:
8836 case CPP_DEREF_STAR:
8837 case CPP_DIV:
8838 case CPP_MOD:
8839 case CPP_LSHIFT:
8840 case CPP_RSHIFT:
8841 case CPP_LESS:
8842 case CPP_GREATER:
8843 case CPP_LESS_EQ:
8844 case CPP_GREATER_EQ:
8845 case CPP_EQ_EQ:
8846 case CPP_NOT_EQ:
8847 case CPP_EQ:
8848 case CPP_MULT_EQ:
8849 case CPP_DIV_EQ:
8850 case CPP_MOD_EQ:
8851 case CPP_PLUS_EQ:
8852 case CPP_MINUS_EQ:
8853 case CPP_RSHIFT_EQ:
8854 case CPP_LSHIFT_EQ:
8855 case CPP_AND_EQ:
8856 case CPP_XOR_EQ:
8857 case CPP_OR_EQ:
8858 case CPP_XOR:
8859 case CPP_OR:
8860 case CPP_OR_OR:
8861 case CPP_EOF:
8862 case CPP_ELLIPSIS:
8863 return 0;
8864
8865 case CPP_OPEN_PAREN:
8866 /* In ((type ()) () the last () isn't a valid cast-expression,
8867 so the whole must be parsed as postfix-expression. */
8868 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8869 != CPP_CLOSE_PAREN;
8870
8871 case CPP_OPEN_SQUARE:
8872 /* '[' may start a primary-expression in obj-c++ and in C++11,
8873 as a lambda-expression, eg, '(void)[]{}'. */
8874 if (cxx_dialect >= cxx11)
8875 return -1;
8876 return c_dialect_objc ();
8877
8878 case CPP_PLUS_PLUS:
8879 case CPP_MINUS_MINUS:
8880 /* '++' and '--' may or may not start a cast-expression:
8881
8882 struct T { void operator++(int); };
8883 void f() { (T())++; }
8884
8885 vs
8886
8887 int a;
8888 (int)++a; */
8889 return -1;
8890
8891 default:
8892 return 1;
8893 }
8894 }
8895
8896 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8897 in the order: const_cast, static_cast, reinterpret_cast.
8898
8899 Don't suggest dynamic_cast.
8900
8901 Return the first legal cast kind found, or NULL otherwise. */
8902
8903 static const char *
8904 get_cast_suggestion (tree dst_type, tree orig_expr)
8905 {
8906 tree trial;
8907
8908 /* Reuse the parser logic by attempting to build the various kinds of
8909 cast, with "complain" disabled.
8910 Identify the first such cast that is valid. */
8911
8912 /* Don't attempt to run such logic within template processing. */
8913 if (processing_template_decl)
8914 return NULL;
8915
8916 /* First try const_cast. */
8917 trial = build_const_cast (dst_type, orig_expr, tf_none);
8918 if (trial != error_mark_node)
8919 return "const_cast";
8920
8921 /* If that fails, try static_cast. */
8922 trial = build_static_cast (dst_type, orig_expr, tf_none);
8923 if (trial != error_mark_node)
8924 return "static_cast";
8925
8926 /* Finally, try reinterpret_cast. */
8927 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8928 if (trial != error_mark_node)
8929 return "reinterpret_cast";
8930
8931 /* No such cast possible. */
8932 return NULL;
8933 }
8934
8935 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8936 suggesting how to convert a C-style cast of the form:
8937
8938 (DST_TYPE)ORIG_EXPR
8939
8940 to a C++-style cast.
8941
8942 The primary range of RICHLOC is asssumed to be that of the original
8943 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8944 of the parens in the C-style cast. */
8945
8946 static void
8947 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8948 location_t close_paren_loc, tree orig_expr,
8949 tree dst_type)
8950 {
8951 /* This function is non-trivial, so bail out now if the warning isn't
8952 going to be emitted. */
8953 if (!warn_old_style_cast)
8954 return;
8955
8956 /* Try to find a legal C++ cast, trying them in order:
8957 const_cast, static_cast, reinterpret_cast. */
8958 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8959 if (!cast_suggestion)
8960 return;
8961
8962 /* Replace the open paren with "CAST_SUGGESTION<". */
8963 pretty_printer pp;
8964 pp_printf (&pp, "%s<", cast_suggestion);
8965 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8966
8967 /* Replace the close paren with "> (". */
8968 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8969
8970 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8971 rich_loc->add_fixit_insert_after (")");
8972 }
8973
8974
8975 /* Parse a cast-expression.
8976
8977 cast-expression:
8978 unary-expression
8979 ( type-id ) cast-expression
8980
8981 ADDRESS_P is true iff the unary-expression is appearing as the
8982 operand of the `&' operator. CAST_P is true if this expression is
8983 the target of a cast.
8984
8985 Returns a representation of the expression. */
8986
8987 static cp_expr
8988 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8989 bool decltype_p, cp_id_kind * pidk)
8990 {
8991 /* If it's a `(', then we might be looking at a cast. */
8992 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8993 {
8994 tree type = NULL_TREE;
8995 cp_expr expr (NULL_TREE);
8996 int cast_expression = 0;
8997 const char *saved_message;
8998
8999 /* There's no way to know yet whether or not this is a cast.
9000 For example, `(int (3))' is a unary-expression, while `(int)
9001 3' is a cast. So, we resort to parsing tentatively. */
9002 cp_parser_parse_tentatively (parser);
9003 /* Types may not be defined in a cast. */
9004 saved_message = parser->type_definition_forbidden_message;
9005 parser->type_definition_forbidden_message
9006 = G_("types may not be defined in casts");
9007 /* Consume the `('. */
9008 matching_parens parens;
9009 cp_token *open_paren = parens.consume_open (parser);
9010 location_t open_paren_loc = open_paren->location;
9011 location_t close_paren_loc = UNKNOWN_LOCATION;
9012
9013 /* A very tricky bit is that `(struct S) { 3 }' is a
9014 compound-literal (which we permit in C++ as an extension).
9015 But, that construct is not a cast-expression -- it is a
9016 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9017 is legal; if the compound-literal were a cast-expression,
9018 you'd need an extra set of parentheses.) But, if we parse
9019 the type-id, and it happens to be a class-specifier, then we
9020 will commit to the parse at that point, because we cannot
9021 undo the action that is done when creating a new class. So,
9022 then we cannot back up and do a postfix-expression.
9023
9024 Another tricky case is the following (c++/29234):
9025
9026 struct S { void operator () (); };
9027
9028 void foo ()
9029 {
9030 ( S()() );
9031 }
9032
9033 As a type-id we parse the parenthesized S()() as a function
9034 returning a function, groktypename complains and we cannot
9035 back up in this case either.
9036
9037 Therefore, we scan ahead to the closing `)', and check to see
9038 if the tokens after the `)' can start a cast-expression. Otherwise
9039 we are dealing with an unary-expression, a postfix-expression
9040 or something else.
9041
9042 Yet another tricky case, in C++11, is the following (c++/54891):
9043
9044 (void)[]{};
9045
9046 The issue is that usually, besides the case of lambda-expressions,
9047 the parenthesized type-id cannot be followed by '[', and, eg, we
9048 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9049 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9050 we don't commit, we try a cast-expression, then an unary-expression.
9051
9052 Save tokens so that we can put them back. */
9053 cp_lexer_save_tokens (parser->lexer);
9054
9055 /* We may be looking at a cast-expression. */
9056 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9057 /*consume_paren=*/true))
9058 cast_expression
9059 = cp_parser_tokens_start_cast_expression (parser);
9060
9061 /* Roll back the tokens we skipped. */
9062 cp_lexer_rollback_tokens (parser->lexer);
9063 /* If we aren't looking at a cast-expression, simulate an error so
9064 that the call to cp_parser_error_occurred below returns true. */
9065 if (!cast_expression)
9066 cp_parser_simulate_error (parser);
9067 else
9068 {
9069 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9070 parser->in_type_id_in_expr_p = true;
9071 /* Look for the type-id. */
9072 type = cp_parser_type_id (parser);
9073 /* Look for the closing `)'. */
9074 cp_token *close_paren = parens.require_close (parser);
9075 if (close_paren)
9076 close_paren_loc = close_paren->location;
9077 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9078 }
9079
9080 /* Restore the saved message. */
9081 parser->type_definition_forbidden_message = saved_message;
9082
9083 /* At this point this can only be either a cast or a
9084 parenthesized ctor such as `(T ())' that looks like a cast to
9085 function returning T. */
9086 if (!cp_parser_error_occurred (parser))
9087 {
9088 /* Only commit if the cast-expression doesn't start with
9089 '++', '--', or '[' in C++11. */
9090 if (cast_expression > 0)
9091 cp_parser_commit_to_topmost_tentative_parse (parser);
9092
9093 expr = cp_parser_cast_expression (parser,
9094 /*address_p=*/false,
9095 /*cast_p=*/true,
9096 /*decltype_p=*/false,
9097 pidk);
9098
9099 if (cp_parser_parse_definitely (parser))
9100 {
9101 /* Warn about old-style casts, if so requested. */
9102 if (warn_old_style_cast
9103 && !in_system_header_at (input_location)
9104 && !VOID_TYPE_P (type)
9105 && current_lang_name != lang_name_c)
9106 {
9107 gcc_rich_location rich_loc (input_location);
9108 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9109 expr, type);
9110 warning_at (&rich_loc, OPT_Wold_style_cast,
9111 "use of old-style cast to %q#T", type);
9112 }
9113
9114 /* Only type conversions to integral or enumeration types
9115 can be used in constant-expressions. */
9116 if (!cast_valid_in_integral_constant_expression_p (type)
9117 && cp_parser_non_integral_constant_expression (parser,
9118 NIC_CAST))
9119 return error_mark_node;
9120
9121 /* Perform the cast. */
9122 /* Make a location:
9123 (TYPE) EXPR
9124 ^~~~~~~~~~~
9125 with start==caret at the open paren, extending to the
9126 end of "expr". */
9127 location_t cast_loc = make_location (open_paren_loc,
9128 open_paren_loc,
9129 expr.get_finish ());
9130 expr = build_c_cast (cast_loc, type, expr);
9131 return expr;
9132 }
9133 }
9134 else
9135 cp_parser_abort_tentative_parse (parser);
9136 }
9137
9138 /* If we get here, then it's not a cast, so it must be a
9139 unary-expression. */
9140 return cp_parser_unary_expression (parser, pidk, address_p,
9141 cast_p, decltype_p);
9142 }
9143
9144 /* Parse a binary expression of the general form:
9145
9146 pm-expression:
9147 cast-expression
9148 pm-expression .* cast-expression
9149 pm-expression ->* cast-expression
9150
9151 multiplicative-expression:
9152 pm-expression
9153 multiplicative-expression * pm-expression
9154 multiplicative-expression / pm-expression
9155 multiplicative-expression % pm-expression
9156
9157 additive-expression:
9158 multiplicative-expression
9159 additive-expression + multiplicative-expression
9160 additive-expression - multiplicative-expression
9161
9162 shift-expression:
9163 additive-expression
9164 shift-expression << additive-expression
9165 shift-expression >> additive-expression
9166
9167 relational-expression:
9168 shift-expression
9169 relational-expression < shift-expression
9170 relational-expression > shift-expression
9171 relational-expression <= shift-expression
9172 relational-expression >= shift-expression
9173
9174 GNU Extension:
9175
9176 relational-expression:
9177 relational-expression <? shift-expression
9178 relational-expression >? shift-expression
9179
9180 equality-expression:
9181 relational-expression
9182 equality-expression == relational-expression
9183 equality-expression != relational-expression
9184
9185 and-expression:
9186 equality-expression
9187 and-expression & equality-expression
9188
9189 exclusive-or-expression:
9190 and-expression
9191 exclusive-or-expression ^ and-expression
9192
9193 inclusive-or-expression:
9194 exclusive-or-expression
9195 inclusive-or-expression | exclusive-or-expression
9196
9197 logical-and-expression:
9198 inclusive-or-expression
9199 logical-and-expression && inclusive-or-expression
9200
9201 logical-or-expression:
9202 logical-and-expression
9203 logical-or-expression || logical-and-expression
9204
9205 All these are implemented with a single function like:
9206
9207 binary-expression:
9208 simple-cast-expression
9209 binary-expression <token> binary-expression
9210
9211 CAST_P is true if this expression is the target of a cast.
9212
9213 The binops_by_token map is used to get the tree codes for each <token> type.
9214 binary-expressions are associated according to a precedence table. */
9215
9216 #define TOKEN_PRECEDENCE(token) \
9217 (((token->type == CPP_GREATER \
9218 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9219 && !parser->greater_than_is_operator_p) \
9220 ? PREC_NOT_OPERATOR \
9221 : binops_by_token[token->type].prec)
9222
9223 static cp_expr
9224 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9225 bool no_toplevel_fold_p,
9226 bool decltype_p,
9227 enum cp_parser_prec prec,
9228 cp_id_kind * pidk)
9229 {
9230 cp_parser_expression_stack stack;
9231 cp_parser_expression_stack_entry *sp = &stack[0];
9232 cp_parser_expression_stack_entry current;
9233 cp_expr rhs;
9234 cp_token *token;
9235 enum tree_code rhs_type;
9236 enum cp_parser_prec new_prec, lookahead_prec;
9237 tree overload;
9238
9239 /* Parse the first expression. */
9240 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9241 ? TRUTH_NOT_EXPR : ERROR_MARK);
9242 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9243 cast_p, decltype_p, pidk);
9244 current.prec = prec;
9245
9246 if (cp_parser_error_occurred (parser))
9247 return error_mark_node;
9248
9249 for (;;)
9250 {
9251 /* Get an operator token. */
9252 token = cp_lexer_peek_token (parser->lexer);
9253
9254 if (warn_cxx11_compat
9255 && token->type == CPP_RSHIFT
9256 && !parser->greater_than_is_operator_p)
9257 {
9258 if (warning_at (token->location, OPT_Wc__11_compat,
9259 "%<>>%> operator is treated"
9260 " as two right angle brackets in C++11"))
9261 inform (token->location,
9262 "suggest parentheses around %<>>%> expression");
9263 }
9264
9265 new_prec = TOKEN_PRECEDENCE (token);
9266 if (new_prec != PREC_NOT_OPERATOR
9267 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9268 /* This is a fold-expression; handle it later. */
9269 new_prec = PREC_NOT_OPERATOR;
9270
9271 /* Popping an entry off the stack means we completed a subexpression:
9272 - either we found a token which is not an operator (`>' where it is not
9273 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9274 will happen repeatedly;
9275 - or, we found an operator which has lower priority. This is the case
9276 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9277 parsing `3 * 4'. */
9278 if (new_prec <= current.prec)
9279 {
9280 if (sp == stack)
9281 break;
9282 else
9283 goto pop;
9284 }
9285
9286 get_rhs:
9287 current.tree_type = binops_by_token[token->type].tree_type;
9288 current.loc = token->location;
9289
9290 /* We used the operator token. */
9291 cp_lexer_consume_token (parser->lexer);
9292
9293 /* For "false && x" or "true || x", x will never be executed;
9294 disable warnings while evaluating it. */
9295 if (current.tree_type == TRUTH_ANDIF_EXPR)
9296 c_inhibit_evaluation_warnings +=
9297 cp_fully_fold (current.lhs) == truthvalue_false_node;
9298 else if (current.tree_type == TRUTH_ORIF_EXPR)
9299 c_inhibit_evaluation_warnings +=
9300 cp_fully_fold (current.lhs) == truthvalue_true_node;
9301
9302 /* Extract another operand. It may be the RHS of this expression
9303 or the LHS of a new, higher priority expression. */
9304 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9305 ? TRUTH_NOT_EXPR : ERROR_MARK);
9306 rhs = cp_parser_simple_cast_expression (parser);
9307
9308 /* Get another operator token. Look up its precedence to avoid
9309 building a useless (immediately popped) stack entry for common
9310 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9311 token = cp_lexer_peek_token (parser->lexer);
9312 lookahead_prec = TOKEN_PRECEDENCE (token);
9313 if (lookahead_prec != PREC_NOT_OPERATOR
9314 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9315 lookahead_prec = PREC_NOT_OPERATOR;
9316 if (lookahead_prec > new_prec)
9317 {
9318 /* ... and prepare to parse the RHS of the new, higher priority
9319 expression. Since precedence levels on the stack are
9320 monotonically increasing, we do not have to care about
9321 stack overflows. */
9322 *sp = current;
9323 ++sp;
9324 current.lhs = rhs;
9325 current.lhs_type = rhs_type;
9326 current.prec = new_prec;
9327 new_prec = lookahead_prec;
9328 goto get_rhs;
9329
9330 pop:
9331 lookahead_prec = new_prec;
9332 /* If the stack is not empty, we have parsed into LHS the right side
9333 (`4' in the example above) of an expression we had suspended.
9334 We can use the information on the stack to recover the LHS (`3')
9335 from the stack together with the tree code (`MULT_EXPR'), and
9336 the precedence of the higher level subexpression
9337 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9338 which will be used to actually build the additive expression. */
9339 rhs = current.lhs;
9340 rhs_type = current.lhs_type;
9341 --sp;
9342 current = *sp;
9343 }
9344
9345 /* Undo the disabling of warnings done above. */
9346 if (current.tree_type == TRUTH_ANDIF_EXPR)
9347 c_inhibit_evaluation_warnings -=
9348 cp_fully_fold (current.lhs) == truthvalue_false_node;
9349 else if (current.tree_type == TRUTH_ORIF_EXPR)
9350 c_inhibit_evaluation_warnings -=
9351 cp_fully_fold (current.lhs) == truthvalue_true_node;
9352
9353 if (warn_logical_not_paren
9354 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9355 && current.lhs_type == TRUTH_NOT_EXPR
9356 /* Avoid warning for !!x == y. */
9357 && (TREE_CODE (current.lhs) != NE_EXPR
9358 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9359 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9360 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9361 /* Avoid warning for !b == y where b is boolean. */
9362 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9363 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9364 != BOOLEAN_TYPE))))
9365 /* Avoid warning for !!b == y where b is boolean. */
9366 && (!DECL_P (current.lhs)
9367 || TREE_TYPE (current.lhs) == NULL_TREE
9368 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9369 warn_logical_not_parentheses (current.loc, current.tree_type,
9370 current.lhs, maybe_constant_value (rhs));
9371
9372 overload = NULL;
9373
9374 location_t combined_loc = make_location (current.loc,
9375 current.lhs.get_start (),
9376 rhs.get_finish ());
9377
9378 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9379 ERROR_MARK for everything that is not a binary expression.
9380 This makes warn_about_parentheses miss some warnings that
9381 involve unary operators. For unary expressions we should
9382 pass the correct tree_code unless the unary expression was
9383 surrounded by parentheses.
9384 */
9385 if (no_toplevel_fold_p
9386 && lookahead_prec <= current.prec
9387 && sp == stack)
9388 {
9389 if (current.lhs == error_mark_node || rhs == error_mark_node)
9390 current.lhs = error_mark_node;
9391 else
9392 {
9393 current.lhs
9394 = build_min (current.tree_type,
9395 TREE_CODE_CLASS (current.tree_type)
9396 == tcc_comparison
9397 ? boolean_type_node : TREE_TYPE (current.lhs),
9398 current.lhs.get_value (), rhs.get_value ());
9399 SET_EXPR_LOCATION (current.lhs, combined_loc);
9400 }
9401 }
9402 else
9403 {
9404 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9405 current.lhs, current.lhs_type,
9406 rhs, rhs_type, &overload,
9407 complain_flags (decltype_p));
9408 /* TODO: build_x_binary_op doesn't always honor the location. */
9409 current.lhs.set_location (combined_loc);
9410 }
9411 current.lhs_type = current.tree_type;
9412
9413 /* If the binary operator required the use of an overloaded operator,
9414 then this expression cannot be an integral constant-expression.
9415 An overloaded operator can be used even if both operands are
9416 otherwise permissible in an integral constant-expression if at
9417 least one of the operands is of enumeration type. */
9418
9419 if (overload
9420 && cp_parser_non_integral_constant_expression (parser,
9421 NIC_OVERLOADED))
9422 return error_mark_node;
9423 }
9424
9425 return current.lhs;
9426 }
9427
9428 static cp_expr
9429 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9430 bool no_toplevel_fold_p,
9431 enum cp_parser_prec prec,
9432 cp_id_kind * pidk)
9433 {
9434 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9435 /*decltype*/false, prec, pidk);
9436 }
9437
9438 /* Parse the `? expression : assignment-expression' part of a
9439 conditional-expression. The LOGICAL_OR_EXPR is the
9440 logical-or-expression that started the conditional-expression.
9441 Returns a representation of the entire conditional-expression.
9442
9443 This routine is used by cp_parser_assignment_expression.
9444
9445 ? expression : assignment-expression
9446
9447 GNU Extensions:
9448
9449 ? : assignment-expression */
9450
9451 static tree
9452 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9453 {
9454 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9455 cp_expr assignment_expr;
9456 struct cp_token *token;
9457 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9458
9459 /* Consume the `?' token. */
9460 cp_lexer_consume_token (parser->lexer);
9461 token = cp_lexer_peek_token (parser->lexer);
9462 if (cp_parser_allow_gnu_extensions_p (parser)
9463 && token->type == CPP_COLON)
9464 {
9465 pedwarn (token->location, OPT_Wpedantic,
9466 "ISO C++ does not allow ?: with omitted middle operand");
9467 /* Implicit true clause. */
9468 expr = NULL_TREE;
9469 c_inhibit_evaluation_warnings +=
9470 folded_logical_or_expr == truthvalue_true_node;
9471 warn_for_omitted_condop (token->location, logical_or_expr);
9472 }
9473 else
9474 {
9475 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9476 parser->colon_corrects_to_scope_p = false;
9477 /* Parse the expression. */
9478 c_inhibit_evaluation_warnings +=
9479 folded_logical_or_expr == truthvalue_false_node;
9480 expr = cp_parser_expression (parser);
9481 c_inhibit_evaluation_warnings +=
9482 ((folded_logical_or_expr == truthvalue_true_node)
9483 - (folded_logical_or_expr == truthvalue_false_node));
9484 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9485 }
9486
9487 /* The next token should be a `:'. */
9488 cp_parser_require (parser, CPP_COLON, RT_COLON);
9489 /* Parse the assignment-expression. */
9490 assignment_expr = cp_parser_assignment_expression (parser);
9491 c_inhibit_evaluation_warnings -=
9492 folded_logical_or_expr == truthvalue_true_node;
9493
9494 /* Make a location:
9495 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9496 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9497 with the caret at the "?", ranging from the start of
9498 the logical_or_expr to the end of the assignment_expr. */
9499 loc = make_location (loc,
9500 logical_or_expr.get_start (),
9501 assignment_expr.get_finish ());
9502
9503 /* Build the conditional-expression. */
9504 return build_x_conditional_expr (loc, logical_or_expr,
9505 expr,
9506 assignment_expr,
9507 tf_warning_or_error);
9508 }
9509
9510 /* Parse an assignment-expression.
9511
9512 assignment-expression:
9513 conditional-expression
9514 logical-or-expression assignment-operator assignment_expression
9515 throw-expression
9516
9517 CAST_P is true if this expression is the target of a cast.
9518 DECLTYPE_P is true if this expression is the operand of decltype.
9519
9520 Returns a representation for the expression. */
9521
9522 static cp_expr
9523 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9524 bool cast_p, bool decltype_p)
9525 {
9526 cp_expr expr;
9527
9528 /* If the next token is the `throw' keyword, then we're looking at
9529 a throw-expression. */
9530 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9531 expr = cp_parser_throw_expression (parser);
9532 /* Otherwise, it must be that we are looking at a
9533 logical-or-expression. */
9534 else
9535 {
9536 /* Parse the binary expressions (logical-or-expression). */
9537 expr = cp_parser_binary_expression (parser, cast_p, false,
9538 decltype_p,
9539 PREC_NOT_OPERATOR, pidk);
9540 /* If the next token is a `?' then we're actually looking at a
9541 conditional-expression. */
9542 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9543 return cp_parser_question_colon_clause (parser, expr);
9544 else
9545 {
9546 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9547
9548 /* If it's an assignment-operator, we're using the second
9549 production. */
9550 enum tree_code assignment_operator
9551 = cp_parser_assignment_operator_opt (parser);
9552 if (assignment_operator != ERROR_MARK)
9553 {
9554 bool non_constant_p;
9555
9556 /* Parse the right-hand side of the assignment. */
9557 cp_expr rhs = cp_parser_initializer_clause (parser,
9558 &non_constant_p);
9559
9560 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9561 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9562
9563 /* An assignment may not appear in a
9564 constant-expression. */
9565 if (cp_parser_non_integral_constant_expression (parser,
9566 NIC_ASSIGNMENT))
9567 return error_mark_node;
9568 /* Build the assignment expression. Its default
9569 location:
9570 LHS = RHS
9571 ~~~~^~~~~
9572 is the location of the '=' token as the
9573 caret, ranging from the start of the lhs to the
9574 end of the rhs. */
9575 loc = make_location (loc,
9576 expr.get_start (),
9577 rhs.get_finish ());
9578 expr = build_x_modify_expr (loc, expr,
9579 assignment_operator,
9580 rhs,
9581 complain_flags (decltype_p));
9582 /* TODO: build_x_modify_expr doesn't honor the location,
9583 so we must set it here. */
9584 expr.set_location (loc);
9585 }
9586 }
9587 }
9588
9589 return expr;
9590 }
9591
9592 /* Parse an (optional) assignment-operator.
9593
9594 assignment-operator: one of
9595 = *= /= %= += -= >>= <<= &= ^= |=
9596
9597 GNU Extension:
9598
9599 assignment-operator: one of
9600 <?= >?=
9601
9602 If the next token is an assignment operator, the corresponding tree
9603 code is returned, and the token is consumed. For example, for
9604 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9605 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9606 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9607 operator, ERROR_MARK is returned. */
9608
9609 static enum tree_code
9610 cp_parser_assignment_operator_opt (cp_parser* parser)
9611 {
9612 enum tree_code op;
9613 cp_token *token;
9614
9615 /* Peek at the next token. */
9616 token = cp_lexer_peek_token (parser->lexer);
9617
9618 switch (token->type)
9619 {
9620 case CPP_EQ:
9621 op = NOP_EXPR;
9622 break;
9623
9624 case CPP_MULT_EQ:
9625 op = MULT_EXPR;
9626 break;
9627
9628 case CPP_DIV_EQ:
9629 op = TRUNC_DIV_EXPR;
9630 break;
9631
9632 case CPP_MOD_EQ:
9633 op = TRUNC_MOD_EXPR;
9634 break;
9635
9636 case CPP_PLUS_EQ:
9637 op = PLUS_EXPR;
9638 break;
9639
9640 case CPP_MINUS_EQ:
9641 op = MINUS_EXPR;
9642 break;
9643
9644 case CPP_RSHIFT_EQ:
9645 op = RSHIFT_EXPR;
9646 break;
9647
9648 case CPP_LSHIFT_EQ:
9649 op = LSHIFT_EXPR;
9650 break;
9651
9652 case CPP_AND_EQ:
9653 op = BIT_AND_EXPR;
9654 break;
9655
9656 case CPP_XOR_EQ:
9657 op = BIT_XOR_EXPR;
9658 break;
9659
9660 case CPP_OR_EQ:
9661 op = BIT_IOR_EXPR;
9662 break;
9663
9664 default:
9665 /* Nothing else is an assignment operator. */
9666 op = ERROR_MARK;
9667 }
9668
9669 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9670 if (op != ERROR_MARK
9671 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9672 op = ERROR_MARK;
9673
9674 /* If it was an assignment operator, consume it. */
9675 if (op != ERROR_MARK)
9676 cp_lexer_consume_token (parser->lexer);
9677
9678 return op;
9679 }
9680
9681 /* Parse an expression.
9682
9683 expression:
9684 assignment-expression
9685 expression , assignment-expression
9686
9687 CAST_P is true if this expression is the target of a cast.
9688 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9689 except possibly parenthesized or on the RHS of a comma (N3276).
9690
9691 Returns a representation of the expression. */
9692
9693 static cp_expr
9694 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9695 bool cast_p, bool decltype_p)
9696 {
9697 cp_expr expression = NULL_TREE;
9698 location_t loc = UNKNOWN_LOCATION;
9699
9700 while (true)
9701 {
9702 cp_expr assignment_expression;
9703
9704 /* Parse the next assignment-expression. */
9705 assignment_expression
9706 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9707
9708 /* We don't create a temporary for a call that is the immediate operand
9709 of decltype or on the RHS of a comma. But when we see a comma, we
9710 need to create a temporary for a call on the LHS. */
9711 if (decltype_p && !processing_template_decl
9712 && TREE_CODE (assignment_expression) == CALL_EXPR
9713 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9714 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9715 assignment_expression
9716 = build_cplus_new (TREE_TYPE (assignment_expression),
9717 assignment_expression, tf_warning_or_error);
9718
9719 /* If this is the first assignment-expression, we can just
9720 save it away. */
9721 if (!expression)
9722 expression = assignment_expression;
9723 else
9724 {
9725 /* Create a location with caret at the comma, ranging
9726 from the start of the LHS to the end of the RHS. */
9727 loc = make_location (loc,
9728 expression.get_start (),
9729 assignment_expression.get_finish ());
9730 expression = build_x_compound_expr (loc, expression,
9731 assignment_expression,
9732 complain_flags (decltype_p));
9733 expression.set_location (loc);
9734 }
9735 /* If the next token is not a comma, or we're in a fold-expression, then
9736 we are done with the expression. */
9737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9738 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9739 break;
9740 /* Consume the `,'. */
9741 loc = cp_lexer_peek_token (parser->lexer)->location;
9742 cp_lexer_consume_token (parser->lexer);
9743 /* A comma operator cannot appear in a constant-expression. */
9744 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9745 expression = error_mark_node;
9746 }
9747
9748 return expression;
9749 }
9750
9751 /* Parse a constant-expression.
9752
9753 constant-expression:
9754 conditional-expression
9755
9756 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9757 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9758 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9759 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9760 only parse a conditional-expression, otherwise parse an
9761 assignment-expression. See below for rationale. */
9762
9763 static cp_expr
9764 cp_parser_constant_expression (cp_parser* parser,
9765 bool allow_non_constant_p,
9766 bool *non_constant_p,
9767 bool strict_p)
9768 {
9769 bool saved_integral_constant_expression_p;
9770 bool saved_allow_non_integral_constant_expression_p;
9771 bool saved_non_integral_constant_expression_p;
9772 cp_expr expression;
9773
9774 /* It might seem that we could simply parse the
9775 conditional-expression, and then check to see if it were
9776 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9777 one that the compiler can figure out is constant, possibly after
9778 doing some simplifications or optimizations. The standard has a
9779 precise definition of constant-expression, and we must honor
9780 that, even though it is somewhat more restrictive.
9781
9782 For example:
9783
9784 int i[(2, 3)];
9785
9786 is not a legal declaration, because `(2, 3)' is not a
9787 constant-expression. The `,' operator is forbidden in a
9788 constant-expression. However, GCC's constant-folding machinery
9789 will fold this operation to an INTEGER_CST for `3'. */
9790
9791 /* Save the old settings. */
9792 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9793 saved_allow_non_integral_constant_expression_p
9794 = parser->allow_non_integral_constant_expression_p;
9795 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9796 /* We are now parsing a constant-expression. */
9797 parser->integral_constant_expression_p = true;
9798 parser->allow_non_integral_constant_expression_p
9799 = (allow_non_constant_p || cxx_dialect >= cxx11);
9800 parser->non_integral_constant_expression_p = false;
9801 /* Although the grammar says "conditional-expression", when not STRICT_P,
9802 we parse an "assignment-expression", which also permits
9803 "throw-expression" and the use of assignment operators. In the case
9804 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9805 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9806 actually essential that we look for an assignment-expression.
9807 For example, cp_parser_initializer_clauses uses this function to
9808 determine whether a particular assignment-expression is in fact
9809 constant. */
9810 if (strict_p)
9811 {
9812 /* Parse the binary expressions (logical-or-expression). */
9813 expression = cp_parser_binary_expression (parser, false, false, false,
9814 PREC_NOT_OPERATOR, NULL);
9815 /* If the next token is a `?' then we're actually looking at
9816 a conditional-expression; otherwise we're done. */
9817 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9818 expression = cp_parser_question_colon_clause (parser, expression);
9819 }
9820 else
9821 expression = cp_parser_assignment_expression (parser);
9822 /* Restore the old settings. */
9823 parser->integral_constant_expression_p
9824 = saved_integral_constant_expression_p;
9825 parser->allow_non_integral_constant_expression_p
9826 = saved_allow_non_integral_constant_expression_p;
9827 if (cxx_dialect >= cxx11)
9828 {
9829 /* Require an rvalue constant expression here; that's what our
9830 callers expect. Reference constant expressions are handled
9831 separately in e.g. cp_parser_template_argument. */
9832 tree decay = expression;
9833 if (TREE_TYPE (expression)
9834 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9835 decay = build_address (expression);
9836 bool is_const = potential_rvalue_constant_expression (decay);
9837 parser->non_integral_constant_expression_p = !is_const;
9838 if (!is_const && !allow_non_constant_p)
9839 require_potential_rvalue_constant_expression (decay);
9840 }
9841 if (allow_non_constant_p)
9842 *non_constant_p = parser->non_integral_constant_expression_p;
9843 parser->non_integral_constant_expression_p
9844 = saved_non_integral_constant_expression_p;
9845
9846 return expression;
9847 }
9848
9849 /* Parse __builtin_offsetof.
9850
9851 offsetof-expression:
9852 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9853
9854 offsetof-member-designator:
9855 id-expression
9856 | offsetof-member-designator "." id-expression
9857 | offsetof-member-designator "[" expression "]"
9858 | offsetof-member-designator "->" id-expression */
9859
9860 static cp_expr
9861 cp_parser_builtin_offsetof (cp_parser *parser)
9862 {
9863 int save_ice_p, save_non_ice_p;
9864 tree type;
9865 cp_expr expr;
9866 cp_id_kind dummy;
9867 cp_token *token;
9868 location_t finish_loc;
9869
9870 /* We're about to accept non-integral-constant things, but will
9871 definitely yield an integral constant expression. Save and
9872 restore these values around our local parsing. */
9873 save_ice_p = parser->integral_constant_expression_p;
9874 save_non_ice_p = parser->non_integral_constant_expression_p;
9875
9876 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9877
9878 /* Consume the "__builtin_offsetof" token. */
9879 cp_lexer_consume_token (parser->lexer);
9880 /* Consume the opening `('. */
9881 matching_parens parens;
9882 parens.require_open (parser);
9883 /* Parse the type-id. */
9884 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9885 {
9886 const char *saved_message = parser->type_definition_forbidden_message;
9887 parser->type_definition_forbidden_message
9888 = G_("types may not be defined within __builtin_offsetof");
9889 type = cp_parser_type_id (parser);
9890 parser->type_definition_forbidden_message = saved_message;
9891 }
9892 /* Look for the `,'. */
9893 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9894 token = cp_lexer_peek_token (parser->lexer);
9895
9896 /* Build the (type *)null that begins the traditional offsetof macro. */
9897 tree object_ptr
9898 = build_static_cast (build_pointer_type (type), null_pointer_node,
9899 tf_warning_or_error);
9900
9901 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9902 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9903 true, &dummy, token->location);
9904 while (true)
9905 {
9906 token = cp_lexer_peek_token (parser->lexer);
9907 switch (token->type)
9908 {
9909 case CPP_OPEN_SQUARE:
9910 /* offsetof-member-designator "[" expression "]" */
9911 expr = cp_parser_postfix_open_square_expression (parser, expr,
9912 true, false);
9913 break;
9914
9915 case CPP_DEREF:
9916 /* offsetof-member-designator "->" identifier */
9917 expr = grok_array_decl (token->location, expr,
9918 integer_zero_node, false);
9919 /* FALLTHRU */
9920
9921 case CPP_DOT:
9922 /* offsetof-member-designator "." identifier */
9923 cp_lexer_consume_token (parser->lexer);
9924 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9925 expr, true, &dummy,
9926 token->location);
9927 break;
9928
9929 case CPP_CLOSE_PAREN:
9930 /* Consume the ")" token. */
9931 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9932 cp_lexer_consume_token (parser->lexer);
9933 goto success;
9934
9935 default:
9936 /* Error. We know the following require will fail, but
9937 that gives the proper error message. */
9938 parens.require_close (parser);
9939 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9940 expr = error_mark_node;
9941 goto failure;
9942 }
9943 }
9944
9945 success:
9946 /* Make a location of the form:
9947 __builtin_offsetof (struct s, f)
9948 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9949 with caret at the type-id, ranging from the start of the
9950 "_builtin_offsetof" token to the close paren. */
9951 loc = make_location (loc, start_loc, finish_loc);
9952 /* The result will be an INTEGER_CST, so we need to explicitly
9953 preserve the location. */
9954 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9955
9956 failure:
9957 parser->integral_constant_expression_p = save_ice_p;
9958 parser->non_integral_constant_expression_p = save_non_ice_p;
9959
9960 expr = expr.maybe_add_location_wrapper ();
9961 return expr;
9962 }
9963
9964 /* Parse a trait expression.
9965
9966 Returns a representation of the expression, the underlying type
9967 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9968
9969 static cp_expr
9970 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9971 {
9972 cp_trait_kind kind;
9973 tree type1, type2 = NULL_TREE;
9974 bool binary = false;
9975 bool variadic = false;
9976
9977 switch (keyword)
9978 {
9979 case RID_HAS_NOTHROW_ASSIGN:
9980 kind = CPTK_HAS_NOTHROW_ASSIGN;
9981 break;
9982 case RID_HAS_NOTHROW_CONSTRUCTOR:
9983 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9984 break;
9985 case RID_HAS_NOTHROW_COPY:
9986 kind = CPTK_HAS_NOTHROW_COPY;
9987 break;
9988 case RID_HAS_TRIVIAL_ASSIGN:
9989 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9990 break;
9991 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9992 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9993 break;
9994 case RID_HAS_TRIVIAL_COPY:
9995 kind = CPTK_HAS_TRIVIAL_COPY;
9996 break;
9997 case RID_HAS_TRIVIAL_DESTRUCTOR:
9998 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9999 break;
10000 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10001 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10002 break;
10003 case RID_HAS_VIRTUAL_DESTRUCTOR:
10004 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10005 break;
10006 case RID_IS_ABSTRACT:
10007 kind = CPTK_IS_ABSTRACT;
10008 break;
10009 case RID_IS_AGGREGATE:
10010 kind = CPTK_IS_AGGREGATE;
10011 break;
10012 case RID_IS_BASE_OF:
10013 kind = CPTK_IS_BASE_OF;
10014 binary = true;
10015 break;
10016 case RID_IS_CLASS:
10017 kind = CPTK_IS_CLASS;
10018 break;
10019 case RID_IS_EMPTY:
10020 kind = CPTK_IS_EMPTY;
10021 break;
10022 case RID_IS_ENUM:
10023 kind = CPTK_IS_ENUM;
10024 break;
10025 case RID_IS_FINAL:
10026 kind = CPTK_IS_FINAL;
10027 break;
10028 case RID_IS_LITERAL_TYPE:
10029 kind = CPTK_IS_LITERAL_TYPE;
10030 break;
10031 case RID_IS_POD:
10032 kind = CPTK_IS_POD;
10033 break;
10034 case RID_IS_POLYMORPHIC:
10035 kind = CPTK_IS_POLYMORPHIC;
10036 break;
10037 case RID_IS_SAME_AS:
10038 kind = CPTK_IS_SAME_AS;
10039 binary = true;
10040 break;
10041 case RID_IS_STD_LAYOUT:
10042 kind = CPTK_IS_STD_LAYOUT;
10043 break;
10044 case RID_IS_TRIVIAL:
10045 kind = CPTK_IS_TRIVIAL;
10046 break;
10047 case RID_IS_TRIVIALLY_ASSIGNABLE:
10048 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10049 binary = true;
10050 break;
10051 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10052 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10053 variadic = true;
10054 break;
10055 case RID_IS_TRIVIALLY_COPYABLE:
10056 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10057 break;
10058 case RID_IS_UNION:
10059 kind = CPTK_IS_UNION;
10060 break;
10061 case RID_UNDERLYING_TYPE:
10062 kind = CPTK_UNDERLYING_TYPE;
10063 break;
10064 case RID_BASES:
10065 kind = CPTK_BASES;
10066 break;
10067 case RID_DIRECT_BASES:
10068 kind = CPTK_DIRECT_BASES;
10069 break;
10070 case RID_IS_ASSIGNABLE:
10071 kind = CPTK_IS_ASSIGNABLE;
10072 binary = true;
10073 break;
10074 case RID_IS_CONSTRUCTIBLE:
10075 kind = CPTK_IS_CONSTRUCTIBLE;
10076 variadic = true;
10077 break;
10078 default:
10079 gcc_unreachable ();
10080 }
10081
10082 /* Get location of initial token. */
10083 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10084
10085 /* Consume the token. */
10086 cp_lexer_consume_token (parser->lexer);
10087
10088 matching_parens parens;
10089 parens.require_open (parser);
10090
10091 {
10092 type_id_in_expr_sentinel s (parser);
10093 type1 = cp_parser_type_id (parser);
10094 }
10095
10096 if (type1 == error_mark_node)
10097 return error_mark_node;
10098
10099 if (binary)
10100 {
10101 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10102
10103 {
10104 type_id_in_expr_sentinel s (parser);
10105 type2 = cp_parser_type_id (parser);
10106 }
10107
10108 if (type2 == error_mark_node)
10109 return error_mark_node;
10110 }
10111 else if (variadic)
10112 {
10113 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10114 {
10115 cp_lexer_consume_token (parser->lexer);
10116 tree elt = cp_parser_type_id (parser);
10117 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10118 {
10119 cp_lexer_consume_token (parser->lexer);
10120 elt = make_pack_expansion (elt);
10121 }
10122 if (elt == error_mark_node)
10123 return error_mark_node;
10124 type2 = tree_cons (NULL_TREE, elt, type2);
10125 }
10126 }
10127
10128 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10129 parens.require_close (parser);
10130
10131 /* Construct a location of the form:
10132 __is_trivially_copyable(_Tp)
10133 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10134 with start == caret, finishing at the close-paren. */
10135 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10136
10137 /* Complete the trait expression, which may mean either processing
10138 the trait expr now or saving it for template instantiation. */
10139 switch (kind)
10140 {
10141 case CPTK_UNDERLYING_TYPE:
10142 return cp_expr (finish_underlying_type (type1), trait_loc);
10143 case CPTK_BASES:
10144 return cp_expr (finish_bases (type1, false), trait_loc);
10145 case CPTK_DIRECT_BASES:
10146 return cp_expr (finish_bases (type1, true), trait_loc);
10147 default:
10148 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10149 }
10150 }
10151
10152 /* Parse a lambda expression.
10153
10154 lambda-expression:
10155 lambda-introducer lambda-declarator [opt] compound-statement
10156
10157 Returns a representation of the expression. */
10158
10159 static cp_expr
10160 cp_parser_lambda_expression (cp_parser* parser)
10161 {
10162 tree lambda_expr = build_lambda_expr ();
10163 tree type;
10164 bool ok = true;
10165 cp_token *token = cp_lexer_peek_token (parser->lexer);
10166 cp_token_position start = 0;
10167
10168 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10169
10170 if (cp_unevaluated_operand)
10171 {
10172 if (!token->error_reported)
10173 {
10174 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10175 "lambda-expression in unevaluated context");
10176 token->error_reported = true;
10177 }
10178 ok = false;
10179 }
10180 else if (parser->in_template_argument_list_p)
10181 {
10182 if (!token->error_reported)
10183 {
10184 error_at (token->location, "lambda-expression in template-argument");
10185 token->error_reported = true;
10186 }
10187 ok = false;
10188 }
10189
10190 /* We may be in the middle of deferred access check. Disable
10191 it now. */
10192 push_deferring_access_checks (dk_no_deferred);
10193
10194 cp_parser_lambda_introducer (parser, lambda_expr);
10195
10196 type = begin_lambda_type (lambda_expr);
10197 if (type == error_mark_node)
10198 return error_mark_node;
10199
10200 record_lambda_scope (lambda_expr);
10201
10202 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10203 determine_visibility (TYPE_NAME (type));
10204
10205 /* Now that we've started the type, add the capture fields for any
10206 explicit captures. */
10207 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10208
10209 {
10210 /* Inside the class, surrounding template-parameter-lists do not apply. */
10211 unsigned int saved_num_template_parameter_lists
10212 = parser->num_template_parameter_lists;
10213 unsigned char in_statement = parser->in_statement;
10214 bool in_switch_statement_p = parser->in_switch_statement_p;
10215 bool fully_implicit_function_template_p
10216 = parser->fully_implicit_function_template_p;
10217 tree implicit_template_parms = parser->implicit_template_parms;
10218 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10219 bool auto_is_implicit_function_template_parm_p
10220 = parser->auto_is_implicit_function_template_parm_p;
10221
10222 parser->num_template_parameter_lists = 0;
10223 parser->in_statement = 0;
10224 parser->in_switch_statement_p = false;
10225 parser->fully_implicit_function_template_p = false;
10226 parser->implicit_template_parms = 0;
10227 parser->implicit_template_scope = 0;
10228 parser->auto_is_implicit_function_template_parm_p = false;
10229
10230 /* By virtue of defining a local class, a lambda expression has access to
10231 the private variables of enclosing classes. */
10232
10233 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10234
10235 if (ok && cp_parser_error_occurred (parser))
10236 ok = false;
10237
10238 if (ok)
10239 {
10240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10241 && cp_parser_start_tentative_firewall (parser))
10242 start = token;
10243 cp_parser_lambda_body (parser, lambda_expr);
10244 }
10245 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10246 {
10247 if (cp_parser_skip_to_closing_brace (parser))
10248 cp_lexer_consume_token (parser->lexer);
10249 }
10250
10251 /* The capture list was built up in reverse order; fix that now. */
10252 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10253 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10254
10255 if (ok)
10256 maybe_add_lambda_conv_op (type);
10257
10258 type = finish_struct (type, /*attributes=*/NULL_TREE);
10259
10260 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10261 parser->in_statement = in_statement;
10262 parser->in_switch_statement_p = in_switch_statement_p;
10263 parser->fully_implicit_function_template_p
10264 = fully_implicit_function_template_p;
10265 parser->implicit_template_parms = implicit_template_parms;
10266 parser->implicit_template_scope = implicit_template_scope;
10267 parser->auto_is_implicit_function_template_parm_p
10268 = auto_is_implicit_function_template_parm_p;
10269 }
10270
10271 /* This field is only used during parsing of the lambda. */
10272 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10273
10274 /* This lambda shouldn't have any proxies left at this point. */
10275 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10276 /* And now that we're done, push proxies for an enclosing lambda. */
10277 insert_pending_capture_proxies ();
10278
10279 /* Update the lambda expression to a range. */
10280 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10281 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10282 token->location,
10283 end_tok->location);
10284
10285 if (ok)
10286 lambda_expr = build_lambda_object (lambda_expr);
10287 else
10288 lambda_expr = error_mark_node;
10289
10290 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10291
10292 pop_deferring_access_checks ();
10293
10294 return lambda_expr;
10295 }
10296
10297 /* Parse the beginning of a lambda expression.
10298
10299 lambda-introducer:
10300 [ lambda-capture [opt] ]
10301
10302 LAMBDA_EXPR is the current representation of the lambda expression. */
10303
10304 static void
10305 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10306 {
10307 /* Need commas after the first capture. */
10308 bool first = true;
10309
10310 /* Eat the leading `['. */
10311 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10312
10313 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10314 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10315 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10316 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10317 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10318 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10319
10320 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10321 {
10322 cp_lexer_consume_token (parser->lexer);
10323 first = false;
10324
10325 if (!(at_function_scope_p () || parsing_nsdmi ()))
10326 error ("non-local lambda expression cannot have a capture-default");
10327 }
10328
10329 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10330 {
10331 cp_token* capture_token;
10332 tree capture_id;
10333 tree capture_init_expr;
10334 cp_id_kind idk = CP_ID_KIND_NONE;
10335 bool explicit_init_p = false;
10336
10337 enum capture_kind_type
10338 {
10339 BY_COPY,
10340 BY_REFERENCE
10341 };
10342 enum capture_kind_type capture_kind = BY_COPY;
10343
10344 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10345 {
10346 error ("expected end of capture-list");
10347 return;
10348 }
10349
10350 if (first)
10351 first = false;
10352 else
10353 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10354
10355 /* Possibly capture `this'. */
10356 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10357 {
10358 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10359 if (cxx_dialect < cxx2a
10360 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10361 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10362 "with by-copy capture default");
10363 cp_lexer_consume_token (parser->lexer);
10364 add_capture (lambda_expr,
10365 /*id=*/this_identifier,
10366 /*initializer=*/finish_this_expr (),
10367 /*by_reference_p=*/true,
10368 explicit_init_p);
10369 continue;
10370 }
10371
10372 /* Possibly capture `*this'. */
10373 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10374 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10375 {
10376 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10377 if (cxx_dialect < cxx17)
10378 pedwarn (loc, 0, "%<*this%> capture only available with "
10379 "-std=c++17 or -std=gnu++17");
10380 cp_lexer_consume_token (parser->lexer);
10381 cp_lexer_consume_token (parser->lexer);
10382 add_capture (lambda_expr,
10383 /*id=*/this_identifier,
10384 /*initializer=*/finish_this_expr (),
10385 /*by_reference_p=*/false,
10386 explicit_init_p);
10387 continue;
10388 }
10389
10390 /* Remember whether we want to capture as a reference or not. */
10391 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10392 {
10393 capture_kind = BY_REFERENCE;
10394 cp_lexer_consume_token (parser->lexer);
10395 }
10396
10397 /* Get the identifier. */
10398 capture_token = cp_lexer_peek_token (parser->lexer);
10399 capture_id = cp_parser_identifier (parser);
10400
10401 if (capture_id == error_mark_node)
10402 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10403 delimiters, but I modified this to stop on unnested ']' as well. It
10404 was already changed to stop on unnested '}', so the
10405 "closing_parenthesis" name is no more misleading with my change. */
10406 {
10407 cp_parser_skip_to_closing_parenthesis (parser,
10408 /*recovering=*/true,
10409 /*or_comma=*/true,
10410 /*consume_paren=*/true);
10411 break;
10412 }
10413
10414 /* Find the initializer for this capture. */
10415 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10416 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10417 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10418 {
10419 bool direct, non_constant;
10420 /* An explicit initializer exists. */
10421 if (cxx_dialect < cxx14)
10422 pedwarn (input_location, 0,
10423 "lambda capture initializers "
10424 "only available with -std=c++14 or -std=gnu++14");
10425 capture_init_expr = cp_parser_initializer (parser, &direct,
10426 &non_constant, true);
10427 explicit_init_p = true;
10428 if (capture_init_expr == NULL_TREE)
10429 {
10430 error ("empty initializer for lambda init-capture");
10431 capture_init_expr = error_mark_node;
10432 }
10433 }
10434 else
10435 {
10436 const char* error_msg;
10437
10438 /* Turn the identifier into an id-expression. */
10439 capture_init_expr
10440 = cp_parser_lookup_name_simple (parser, capture_id,
10441 capture_token->location);
10442
10443 if (capture_init_expr == error_mark_node)
10444 {
10445 unqualified_name_lookup_error (capture_id);
10446 continue;
10447 }
10448 else if (!VAR_P (capture_init_expr)
10449 && TREE_CODE (capture_init_expr) != PARM_DECL)
10450 {
10451 error_at (capture_token->location,
10452 "capture of non-variable %qE",
10453 capture_init_expr);
10454 if (DECL_P (capture_init_expr))
10455 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10456 "%q#D declared here", capture_init_expr);
10457 continue;
10458 }
10459 if (VAR_P (capture_init_expr)
10460 && decl_storage_duration (capture_init_expr) != dk_auto)
10461 {
10462 if (pedwarn (capture_token->location, 0, "capture of variable "
10463 "%qD with non-automatic storage duration",
10464 capture_init_expr))
10465 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10466 "%q#D declared here", capture_init_expr);
10467 continue;
10468 }
10469
10470 capture_init_expr
10471 = finish_id_expression
10472 (capture_id,
10473 capture_init_expr,
10474 parser->scope,
10475 &idk,
10476 /*integral_constant_expression_p=*/false,
10477 /*allow_non_integral_constant_expression_p=*/false,
10478 /*non_integral_constant_expression_p=*/NULL,
10479 /*template_p=*/false,
10480 /*done=*/true,
10481 /*address_p=*/false,
10482 /*template_arg_p=*/false,
10483 &error_msg,
10484 capture_token->location);
10485
10486 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10487 {
10488 cp_lexer_consume_token (parser->lexer);
10489 capture_init_expr = make_pack_expansion (capture_init_expr);
10490 }
10491 }
10492
10493 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10494 && !explicit_init_p)
10495 {
10496 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10497 && capture_kind == BY_COPY)
10498 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10499 "of %qD redundant with by-copy capture default",
10500 capture_id);
10501 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10502 && capture_kind == BY_REFERENCE)
10503 pedwarn (capture_token->location, 0, "explicit by-reference "
10504 "capture of %qD redundant with by-reference capture "
10505 "default", capture_id);
10506 }
10507
10508 add_capture (lambda_expr,
10509 capture_id,
10510 capture_init_expr,
10511 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10512 explicit_init_p);
10513
10514 /* If there is any qualification still in effect, clear it
10515 now; we will be starting fresh with the next capture. */
10516 parser->scope = NULL_TREE;
10517 parser->qualifying_scope = NULL_TREE;
10518 parser->object_scope = NULL_TREE;
10519 }
10520
10521 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10522 }
10523
10524 /* Parse the (optional) middle of a lambda expression.
10525
10526 lambda-declarator:
10527 < template-parameter-list [opt] >
10528 ( parameter-declaration-clause [opt] )
10529 attribute-specifier [opt]
10530 decl-specifier-seq [opt]
10531 exception-specification [opt]
10532 lambda-return-type-clause [opt]
10533
10534 LAMBDA_EXPR is the current representation of the lambda expression. */
10535
10536 static bool
10537 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10538 {
10539 /* 5.1.1.4 of the standard says:
10540 If a lambda-expression does not include a lambda-declarator, it is as if
10541 the lambda-declarator were ().
10542 This means an empty parameter list, no attributes, and no exception
10543 specification. */
10544 tree param_list = void_list_node;
10545 tree attributes = NULL_TREE;
10546 tree exception_spec = NULL_TREE;
10547 tree template_param_list = NULL_TREE;
10548 tree tx_qual = NULL_TREE;
10549 tree return_type = NULL_TREE;
10550 cp_decl_specifier_seq lambda_specs;
10551 clear_decl_specs (&lambda_specs);
10552
10553 /* The template-parameter-list is optional, but must begin with
10554 an opening angle if present. */
10555 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10556 {
10557 if (cxx_dialect < cxx14)
10558 pedwarn (parser->lexer->next_token->location, 0,
10559 "lambda templates are only available with "
10560 "-std=c++14 or -std=gnu++14");
10561 else if (cxx_dialect < cxx2a)
10562 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10563 "lambda templates are only available with "
10564 "-std=c++2a or -std=gnu++2a");
10565
10566 cp_lexer_consume_token (parser->lexer);
10567
10568 template_param_list = cp_parser_template_parameter_list (parser);
10569
10570 cp_parser_skip_to_end_of_template_parameter_list (parser);
10571
10572 /* We just processed one more parameter list. */
10573 ++parser->num_template_parameter_lists;
10574 }
10575
10576 /* The parameter-declaration-clause is optional (unless
10577 template-parameter-list was given), but must begin with an
10578 opening parenthesis if present. */
10579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10580 {
10581 matching_parens parens;
10582 parens.consume_open (parser);
10583
10584 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10585
10586 /* Parse parameters. */
10587 param_list = cp_parser_parameter_declaration_clause (parser);
10588
10589 /* Default arguments shall not be specified in the
10590 parameter-declaration-clause of a lambda-declarator. */
10591 if (cxx_dialect < cxx14)
10592 for (tree t = param_list; t; t = TREE_CHAIN (t))
10593 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10594 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10595 "default argument specified for lambda parameter");
10596
10597 parens.require_close (parser);
10598
10599 /* In the decl-specifier-seq of the lambda-declarator, each
10600 decl-specifier shall either be mutable or constexpr. */
10601 int declares_class_or_enum;
10602 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10603 cp_parser_decl_specifier_seq (parser,
10604 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10605 &lambda_specs, &declares_class_or_enum);
10606 if (lambda_specs.storage_class == sc_mutable)
10607 {
10608 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10609 if (lambda_specs.conflicting_specifiers_p)
10610 error_at (lambda_specs.locations[ds_storage_class],
10611 "duplicate %<mutable%>");
10612 }
10613
10614 tx_qual = cp_parser_tx_qualifier_opt (parser);
10615
10616 /* Parse optional exception specification. */
10617 exception_spec = cp_parser_exception_specification_opt (parser);
10618
10619 attributes = cp_parser_std_attribute_spec_seq (parser);
10620
10621 /* Parse optional trailing return type. */
10622 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10623 {
10624 cp_lexer_consume_token (parser->lexer);
10625 return_type = cp_parser_trailing_type_id (parser);
10626 }
10627
10628 /* The function parameters must be in scope all the way until after the
10629 trailing-return-type in case of decltype. */
10630 pop_bindings_and_leave_scope ();
10631 }
10632 else if (template_param_list != NULL_TREE) // generate diagnostic
10633 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10634
10635 /* Create the function call operator.
10636
10637 Messing with declarators like this is no uglier than building up the
10638 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10639 other code. */
10640 {
10641 cp_decl_specifier_seq return_type_specs;
10642 cp_declarator* declarator;
10643 tree fco;
10644 int quals;
10645 void *p;
10646
10647 clear_decl_specs (&return_type_specs);
10648 return_type_specs.type = make_auto ();
10649
10650 if (lambda_specs.locations[ds_constexpr])
10651 {
10652 if (cxx_dialect >= cxx17)
10653 return_type_specs.locations[ds_constexpr]
10654 = lambda_specs.locations[ds_constexpr];
10655 else
10656 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10657 "lambda only available with -std=c++17 or -std=gnu++17");
10658 }
10659
10660 p = obstack_alloc (&declarator_obstack, 0);
10661
10662 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10663
10664 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10665 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10666 declarator = make_call_declarator (declarator, param_list, quals,
10667 VIRT_SPEC_UNSPECIFIED,
10668 REF_QUAL_NONE,
10669 tx_qual,
10670 exception_spec,
10671 return_type,
10672 /*requires_clause*/NULL_TREE);
10673 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10674 declarator->std_attributes = attributes;
10675
10676 fco = grokmethod (&return_type_specs,
10677 declarator,
10678 NULL_TREE);
10679 if (fco != error_mark_node)
10680 {
10681 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10682 DECL_ARTIFICIAL (fco) = 1;
10683 /* Give the object parameter a different name. */
10684 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10685 DECL_LAMBDA_FUNCTION (fco) = 1;
10686 }
10687 if (template_param_list)
10688 {
10689 fco = finish_member_template_decl (fco);
10690 finish_template_decl (template_param_list);
10691 --parser->num_template_parameter_lists;
10692 }
10693 else if (parser->fully_implicit_function_template_p)
10694 fco = finish_fully_implicit_template (parser, fco);
10695
10696 finish_member_declaration (fco);
10697
10698 obstack_free (&declarator_obstack, p);
10699
10700 return (fco != error_mark_node);
10701 }
10702 }
10703
10704 /* Parse the body of a lambda expression, which is simply
10705
10706 compound-statement
10707
10708 but which requires special handling.
10709 LAMBDA_EXPR is the current representation of the lambda expression. */
10710
10711 static void
10712 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10713 {
10714 bool nested = (current_function_decl != NULL_TREE);
10715 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10716 bool in_function_body = parser->in_function_body;
10717
10718 if (nested)
10719 push_function_context ();
10720 else
10721 /* Still increment function_depth so that we don't GC in the
10722 middle of an expression. */
10723 ++function_depth;
10724
10725 vec<tree> omp_privatization_save;
10726 save_omp_privatization_clauses (omp_privatization_save);
10727 /* Clear this in case we're in the middle of a default argument. */
10728 parser->local_variables_forbidden_p = false;
10729 parser->in_function_body = true;
10730
10731 {
10732 local_specialization_stack s (lss_copy);
10733 tree fco = lambda_function (lambda_expr);
10734 tree body = start_lambda_function (fco, lambda_expr);
10735 matching_braces braces;
10736
10737 if (braces.require_open (parser))
10738 {
10739 tree compound_stmt = begin_compound_stmt (0);
10740
10741 /* Originally C++11 required us to peek for 'return expr'; and
10742 process it specially here to deduce the return type. N3638
10743 removed the need for that. */
10744
10745 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10746 cp_parser_label_declaration (parser);
10747 cp_parser_statement_seq_opt (parser, NULL_TREE);
10748 braces.require_close (parser);
10749
10750 finish_compound_stmt (compound_stmt);
10751 }
10752
10753 finish_lambda_function (body);
10754 }
10755
10756 restore_omp_privatization_clauses (omp_privatization_save);
10757 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10758 parser->in_function_body = in_function_body;
10759 if (nested)
10760 pop_function_context();
10761 else
10762 --function_depth;
10763 }
10764
10765 /* Statements [gram.stmt.stmt] */
10766
10767 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10768
10769 static void
10770 add_debug_begin_stmt (location_t loc)
10771 {
10772 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10773 return;
10774 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10775 /* A concept is never expanded normally. */
10776 return;
10777
10778 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10779 SET_EXPR_LOCATION (stmt, loc);
10780 add_stmt (stmt);
10781 }
10782
10783 /* Parse a statement.
10784
10785 statement:
10786 labeled-statement
10787 expression-statement
10788 compound-statement
10789 selection-statement
10790 iteration-statement
10791 jump-statement
10792 declaration-statement
10793 try-block
10794
10795 C++11:
10796
10797 statement:
10798 labeled-statement
10799 attribute-specifier-seq (opt) expression-statement
10800 attribute-specifier-seq (opt) compound-statement
10801 attribute-specifier-seq (opt) selection-statement
10802 attribute-specifier-seq (opt) iteration-statement
10803 attribute-specifier-seq (opt) jump-statement
10804 declaration-statement
10805 attribute-specifier-seq (opt) try-block
10806
10807 init-statement:
10808 expression-statement
10809 simple-declaration
10810
10811 TM Extension:
10812
10813 statement:
10814 atomic-statement
10815
10816 IN_COMPOUND is true when the statement is nested inside a
10817 cp_parser_compound_statement; this matters for certain pragmas.
10818
10819 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10820 is a (possibly labeled) if statement which is not enclosed in braces
10821 and has an else clause. This is used to implement -Wparentheses.
10822
10823 CHAIN is a vector of if-else-if conditions. */
10824
10825 static void
10826 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10827 bool in_compound, bool *if_p, vec<tree> *chain,
10828 location_t *loc_after_labels)
10829 {
10830 tree statement, std_attrs = NULL_TREE;
10831 cp_token *token;
10832 location_t statement_location, attrs_location;
10833
10834 restart:
10835 if (if_p != NULL)
10836 *if_p = false;
10837 /* There is no statement yet. */
10838 statement = NULL_TREE;
10839
10840 saved_token_sentinel saved_tokens (parser->lexer);
10841 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10842 if (c_dialect_objc ())
10843 /* In obj-c++, seeing '[[' might be the either the beginning of
10844 c++11 attributes, or a nested objc-message-expression. So
10845 let's parse the c++11 attributes tentatively. */
10846 cp_parser_parse_tentatively (parser);
10847 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10848 if (c_dialect_objc ())
10849 {
10850 if (!cp_parser_parse_definitely (parser))
10851 std_attrs = NULL_TREE;
10852 }
10853
10854 /* Peek at the next token. */
10855 token = cp_lexer_peek_token (parser->lexer);
10856 /* Remember the location of the first token in the statement. */
10857 statement_location = token->location;
10858 add_debug_begin_stmt (statement_location);
10859 /* If this is a keyword, then that will often determine what kind of
10860 statement we have. */
10861 if (token->type == CPP_KEYWORD)
10862 {
10863 enum rid keyword = token->keyword;
10864
10865 switch (keyword)
10866 {
10867 case RID_CASE:
10868 case RID_DEFAULT:
10869 /* Looks like a labeled-statement with a case label.
10870 Parse the label, and then use tail recursion to parse
10871 the statement. */
10872 cp_parser_label_for_labeled_statement (parser, std_attrs);
10873 in_compound = false;
10874 goto restart;
10875
10876 case RID_IF:
10877 case RID_SWITCH:
10878 statement = cp_parser_selection_statement (parser, if_p, chain);
10879 break;
10880
10881 case RID_WHILE:
10882 case RID_DO:
10883 case RID_FOR:
10884 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10885 break;
10886
10887 case RID_BREAK:
10888 case RID_CONTINUE:
10889 case RID_RETURN:
10890 case RID_GOTO:
10891 statement = cp_parser_jump_statement (parser);
10892 break;
10893
10894 /* Objective-C++ exception-handling constructs. */
10895 case RID_AT_TRY:
10896 case RID_AT_CATCH:
10897 case RID_AT_FINALLY:
10898 case RID_AT_SYNCHRONIZED:
10899 case RID_AT_THROW:
10900 statement = cp_parser_objc_statement (parser);
10901 break;
10902
10903 case RID_TRY:
10904 statement = cp_parser_try_block (parser);
10905 break;
10906
10907 case RID_NAMESPACE:
10908 /* This must be a namespace alias definition. */
10909 cp_parser_declaration_statement (parser);
10910 return;
10911
10912 case RID_TRANSACTION_ATOMIC:
10913 case RID_TRANSACTION_RELAXED:
10914 case RID_SYNCHRONIZED:
10915 case RID_ATOMIC_NOEXCEPT:
10916 case RID_ATOMIC_CANCEL:
10917 statement = cp_parser_transaction (parser, token);
10918 break;
10919 case RID_TRANSACTION_CANCEL:
10920 statement = cp_parser_transaction_cancel (parser);
10921 break;
10922
10923 default:
10924 /* It might be a keyword like `int' that can start a
10925 declaration-statement. */
10926 break;
10927 }
10928 }
10929 else if (token->type == CPP_NAME)
10930 {
10931 /* If the next token is a `:', then we are looking at a
10932 labeled-statement. */
10933 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10934 if (token->type == CPP_COLON)
10935 {
10936 /* Looks like a labeled-statement with an ordinary label.
10937 Parse the label, and then use tail recursion to parse
10938 the statement. */
10939
10940 cp_parser_label_for_labeled_statement (parser, std_attrs);
10941 in_compound = false;
10942 goto restart;
10943 }
10944 }
10945 /* Anything that starts with a `{' must be a compound-statement. */
10946 else if (token->type == CPP_OPEN_BRACE)
10947 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10948 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10949 a statement all its own. */
10950 else if (token->type == CPP_PRAGMA)
10951 {
10952 /* Only certain OpenMP pragmas are attached to statements, and thus
10953 are considered statements themselves. All others are not. In
10954 the context of a compound, accept the pragma as a "statement" and
10955 return so that we can check for a close brace. Otherwise we
10956 require a real statement and must go back and read one. */
10957 if (in_compound)
10958 cp_parser_pragma (parser, pragma_compound, if_p);
10959 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10960 goto restart;
10961 return;
10962 }
10963 else if (token->type == CPP_EOF)
10964 {
10965 cp_parser_error (parser, "expected statement");
10966 return;
10967 }
10968
10969 /* Everything else must be a declaration-statement or an
10970 expression-statement. Try for the declaration-statement
10971 first, unless we are looking at a `;', in which case we know that
10972 we have an expression-statement. */
10973 if (!statement)
10974 {
10975 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10976 {
10977 if (std_attrs != NULL_TREE)
10978 {
10979 /* Attributes should be parsed as part of the the
10980 declaration, so let's un-parse them. */
10981 saved_tokens.rollback();
10982 std_attrs = NULL_TREE;
10983 }
10984
10985 cp_parser_parse_tentatively (parser);
10986 /* Try to parse the declaration-statement. */
10987 cp_parser_declaration_statement (parser);
10988 /* If that worked, we're done. */
10989 if (cp_parser_parse_definitely (parser))
10990 return;
10991 }
10992 /* All preceding labels have been parsed at this point. */
10993 if (loc_after_labels != NULL)
10994 *loc_after_labels = statement_location;
10995
10996 /* Look for an expression-statement instead. */
10997 statement = cp_parser_expression_statement (parser, in_statement_expr);
10998
10999 /* Handle [[fallthrough]];. */
11000 if (attribute_fallthrough_p (std_attrs))
11001 {
11002 /* The next token after the fallthrough attribute is ';'. */
11003 if (statement == NULL_TREE)
11004 {
11005 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11006 statement = build_call_expr_internal_loc (statement_location,
11007 IFN_FALLTHROUGH,
11008 void_type_node, 0);
11009 finish_expr_stmt (statement);
11010 }
11011 else
11012 warning_at (statement_location, OPT_Wattributes,
11013 "%<fallthrough%> attribute not followed by %<;%>");
11014 std_attrs = NULL_TREE;
11015 }
11016 }
11017
11018 /* Set the line number for the statement. */
11019 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11020 SET_EXPR_LOCATION (statement, statement_location);
11021
11022 /* Allow "[[fallthrough]];", but warn otherwise. */
11023 if (std_attrs != NULL_TREE)
11024 warning_at (attrs_location,
11025 OPT_Wattributes,
11026 "attributes at the beginning of statement are ignored");
11027 }
11028
11029 /* Append ATTR to attribute list ATTRS. */
11030
11031 static tree
11032 attr_chainon (tree attrs, tree attr)
11033 {
11034 if (attrs == error_mark_node)
11035 return error_mark_node;
11036 if (attr == error_mark_node)
11037 return error_mark_node;
11038 return chainon (attrs, attr);
11039 }
11040
11041 /* Parse the label for a labeled-statement, i.e.
11042
11043 identifier :
11044 case constant-expression :
11045 default :
11046
11047 GNU Extension:
11048 case constant-expression ... constant-expression : statement
11049
11050 When a label is parsed without errors, the label is added to the
11051 parse tree by the finish_* functions, so this function doesn't
11052 have to return the label. */
11053
11054 static void
11055 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11056 {
11057 cp_token *token;
11058 tree label = NULL_TREE;
11059 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11060
11061 /* The next token should be an identifier. */
11062 token = cp_lexer_peek_token (parser->lexer);
11063 if (token->type != CPP_NAME
11064 && token->type != CPP_KEYWORD)
11065 {
11066 cp_parser_error (parser, "expected labeled-statement");
11067 return;
11068 }
11069
11070 /* Remember whether this case or a user-defined label is allowed to fall
11071 through to. */
11072 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11073
11074 parser->colon_corrects_to_scope_p = false;
11075 switch (token->keyword)
11076 {
11077 case RID_CASE:
11078 {
11079 tree expr, expr_hi;
11080 cp_token *ellipsis;
11081
11082 /* Consume the `case' token. */
11083 cp_lexer_consume_token (parser->lexer);
11084 /* Parse the constant-expression. */
11085 expr = cp_parser_constant_expression (parser);
11086 if (check_for_bare_parameter_packs (expr))
11087 expr = error_mark_node;
11088
11089 ellipsis = cp_lexer_peek_token (parser->lexer);
11090 if (ellipsis->type == CPP_ELLIPSIS)
11091 {
11092 /* Consume the `...' token. */
11093 cp_lexer_consume_token (parser->lexer);
11094 expr_hi = cp_parser_constant_expression (parser);
11095 if (check_for_bare_parameter_packs (expr_hi))
11096 expr_hi = error_mark_node;
11097
11098 /* We don't need to emit warnings here, as the common code
11099 will do this for us. */
11100 }
11101 else
11102 expr_hi = NULL_TREE;
11103
11104 if (parser->in_switch_statement_p)
11105 {
11106 tree l = finish_case_label (token->location, expr, expr_hi);
11107 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11108 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11109 }
11110 else
11111 error_at (token->location,
11112 "case label %qE not within a switch statement",
11113 expr);
11114 }
11115 break;
11116
11117 case RID_DEFAULT:
11118 /* Consume the `default' token. */
11119 cp_lexer_consume_token (parser->lexer);
11120
11121 if (parser->in_switch_statement_p)
11122 {
11123 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11124 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11125 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11126 }
11127 else
11128 error_at (token->location, "case label not within a switch statement");
11129 break;
11130
11131 default:
11132 /* Anything else must be an ordinary label. */
11133 label = finish_label_stmt (cp_parser_identifier (parser));
11134 if (label && TREE_CODE (label) == LABEL_DECL)
11135 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11136 break;
11137 }
11138
11139 /* Require the `:' token. */
11140 cp_parser_require (parser, CPP_COLON, RT_COLON);
11141
11142 /* An ordinary label may optionally be followed by attributes.
11143 However, this is only permitted if the attributes are then
11144 followed by a semicolon. This is because, for backward
11145 compatibility, when parsing
11146 lab: __attribute__ ((unused)) int i;
11147 we want the attribute to attach to "i", not "lab". */
11148 if (label != NULL_TREE
11149 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11150 {
11151 tree attrs;
11152 cp_parser_parse_tentatively (parser);
11153 attrs = cp_parser_gnu_attributes_opt (parser);
11154 if (attrs == NULL_TREE
11155 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11156 cp_parser_abort_tentative_parse (parser);
11157 else if (!cp_parser_parse_definitely (parser))
11158 ;
11159 else
11160 attributes = attr_chainon (attributes, attrs);
11161 }
11162
11163 if (attributes != NULL_TREE)
11164 cplus_decl_attributes (&label, attributes, 0);
11165
11166 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11167 }
11168
11169 /* Parse an expression-statement.
11170
11171 expression-statement:
11172 expression [opt] ;
11173
11174 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11175 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11176 indicates whether this expression-statement is part of an
11177 expression statement. */
11178
11179 static tree
11180 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11181 {
11182 tree statement = NULL_TREE;
11183 cp_token *token = cp_lexer_peek_token (parser->lexer);
11184 location_t loc = token->location;
11185
11186 /* There might be attribute fallthrough. */
11187 tree attr = cp_parser_gnu_attributes_opt (parser);
11188
11189 /* If the next token is a ';', then there is no expression
11190 statement. */
11191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11192 {
11193 statement = cp_parser_expression (parser);
11194 if (statement == error_mark_node
11195 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11196 {
11197 cp_parser_skip_to_end_of_block_or_statement (parser);
11198 return error_mark_node;
11199 }
11200 }
11201
11202 /* Handle [[fallthrough]];. */
11203 if (attribute_fallthrough_p (attr))
11204 {
11205 /* The next token after the fallthrough attribute is ';'. */
11206 if (statement == NULL_TREE)
11207 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11208 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11209 void_type_node, 0);
11210 else
11211 warning_at (loc, OPT_Wattributes,
11212 "%<fallthrough%> attribute not followed by %<;%>");
11213 attr = NULL_TREE;
11214 }
11215
11216 /* Allow "[[fallthrough]];", but warn otherwise. */
11217 if (attr != NULL_TREE)
11218 warning_at (loc, OPT_Wattributes,
11219 "attributes at the beginning of statement are ignored");
11220
11221 /* Give a helpful message for "A<T>::type t;" and the like. */
11222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11223 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11224 {
11225 if (TREE_CODE (statement) == SCOPE_REF)
11226 error_at (token->location, "need %<typename%> before %qE because "
11227 "%qT is a dependent scope",
11228 statement, TREE_OPERAND (statement, 0));
11229 else if (is_overloaded_fn (statement)
11230 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11231 {
11232 /* A::A a; */
11233 tree fn = get_first_fn (statement);
11234 error_at (token->location,
11235 "%<%T::%D%> names the constructor, not the type",
11236 DECL_CONTEXT (fn), DECL_NAME (fn));
11237 }
11238 }
11239
11240 /* Consume the final `;'. */
11241 cp_parser_consume_semicolon_at_end_of_statement (parser);
11242
11243 if (in_statement_expr
11244 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11245 /* This is the final expression statement of a statement
11246 expression. */
11247 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11248 else if (statement)
11249 statement = finish_expr_stmt (statement);
11250
11251 return statement;
11252 }
11253
11254 /* Parse a compound-statement.
11255
11256 compound-statement:
11257 { statement-seq [opt] }
11258
11259 GNU extension:
11260
11261 compound-statement:
11262 { label-declaration-seq [opt] statement-seq [opt] }
11263
11264 label-declaration-seq:
11265 label-declaration
11266 label-declaration-seq label-declaration
11267
11268 Returns a tree representing the statement. */
11269
11270 static tree
11271 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11272 int bcs_flags, bool function_body)
11273 {
11274 tree compound_stmt;
11275 matching_braces braces;
11276
11277 /* Consume the `{'. */
11278 if (!braces.require_open (parser))
11279 return error_mark_node;
11280 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11281 && !function_body && cxx_dialect < cxx14)
11282 pedwarn (input_location, OPT_Wpedantic,
11283 "compound-statement in %<constexpr%> function");
11284 /* Begin the compound-statement. */
11285 compound_stmt = begin_compound_stmt (bcs_flags);
11286 /* If the next keyword is `__label__' we have a label declaration. */
11287 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11288 cp_parser_label_declaration (parser);
11289 /* Parse an (optional) statement-seq. */
11290 cp_parser_statement_seq_opt (parser, in_statement_expr);
11291 /* Finish the compound-statement. */
11292 finish_compound_stmt (compound_stmt);
11293 /* Consume the `}'. */
11294 braces.require_close (parser);
11295
11296 return compound_stmt;
11297 }
11298
11299 /* Parse an (optional) statement-seq.
11300
11301 statement-seq:
11302 statement
11303 statement-seq [opt] statement */
11304
11305 static void
11306 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11307 {
11308 /* Scan statements until there aren't any more. */
11309 while (true)
11310 {
11311 cp_token *token = cp_lexer_peek_token (parser->lexer);
11312
11313 /* If we are looking at a `}', then we have run out of
11314 statements; the same is true if we have reached the end
11315 of file, or have stumbled upon a stray '@end'. */
11316 if (token->type == CPP_CLOSE_BRACE
11317 || token->type == CPP_EOF
11318 || token->type == CPP_PRAGMA_EOL
11319 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11320 break;
11321
11322 /* If we are in a compound statement and find 'else' then
11323 something went wrong. */
11324 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11325 {
11326 if (parser->in_statement & IN_IF_STMT)
11327 break;
11328 else
11329 {
11330 token = cp_lexer_consume_token (parser->lexer);
11331 error_at (token->location, "%<else%> without a previous %<if%>");
11332 }
11333 }
11334
11335 /* Parse the statement. */
11336 cp_parser_statement (parser, in_statement_expr, true, NULL);
11337 }
11338 }
11339
11340 /* Return true if this is the C++20 version of range-based-for with
11341 init-statement. */
11342
11343 static bool
11344 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11345 {
11346 bool r = false;
11347
11348 /* Save tokens so that we can put them back. */
11349 cp_lexer_save_tokens (parser->lexer);
11350
11351 /* There has to be an unnested ; followed by an unnested :. */
11352 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11353 /*recovering=*/false,
11354 CPP_SEMICOLON,
11355 /*consume_paren=*/false) != -1)
11356 goto out;
11357
11358 /* We found the semicolon, eat it now. */
11359 cp_lexer_consume_token (parser->lexer);
11360
11361 /* Now look for ':' that is not nested in () or {}. */
11362 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11363 /*recovering=*/false,
11364 CPP_COLON,
11365 /*consume_paren=*/false) == -1);
11366
11367 out:
11368 /* Roll back the tokens we skipped. */
11369 cp_lexer_rollback_tokens (parser->lexer);
11370
11371 return r;
11372 }
11373
11374 /* Return true if we're looking at (init; cond), false otherwise. */
11375
11376 static bool
11377 cp_parser_init_statement_p (cp_parser *parser)
11378 {
11379 /* Save tokens so that we can put them back. */
11380 cp_lexer_save_tokens (parser->lexer);
11381
11382 /* Look for ';' that is not nested in () or {}. */
11383 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11384 /*recovering=*/false,
11385 CPP_SEMICOLON,
11386 /*consume_paren=*/false);
11387
11388 /* Roll back the tokens we skipped. */
11389 cp_lexer_rollback_tokens (parser->lexer);
11390
11391 return ret == -1;
11392 }
11393
11394 /* Parse a selection-statement.
11395
11396 selection-statement:
11397 if ( init-statement [opt] condition ) statement
11398 if ( init-statement [opt] condition ) statement else statement
11399 switch ( init-statement [opt] condition ) statement
11400
11401 Returns the new IF_STMT or SWITCH_STMT.
11402
11403 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11404 is a (possibly labeled) if statement which is not enclosed in
11405 braces and has an else clause. This is used to implement
11406 -Wparentheses.
11407
11408 CHAIN is a vector of if-else-if conditions. This is used to implement
11409 -Wduplicated-cond. */
11410
11411 static tree
11412 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11413 vec<tree> *chain)
11414 {
11415 cp_token *token;
11416 enum rid keyword;
11417 token_indent_info guard_tinfo;
11418
11419 if (if_p != NULL)
11420 *if_p = false;
11421
11422 /* Peek at the next token. */
11423 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11424 guard_tinfo = get_token_indent_info (token);
11425
11426 /* See what kind of keyword it is. */
11427 keyword = token->keyword;
11428 switch (keyword)
11429 {
11430 case RID_IF:
11431 case RID_SWITCH:
11432 {
11433 tree statement;
11434 tree condition;
11435
11436 bool cx = false;
11437 if (keyword == RID_IF
11438 && cp_lexer_next_token_is_keyword (parser->lexer,
11439 RID_CONSTEXPR))
11440 {
11441 cx = true;
11442 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11443 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11444 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11445 "with -std=c++17 or -std=gnu++17");
11446 }
11447
11448 /* Look for the `('. */
11449 matching_parens parens;
11450 if (!parens.require_open (parser))
11451 {
11452 cp_parser_skip_to_end_of_statement (parser);
11453 return error_mark_node;
11454 }
11455
11456 /* Begin the selection-statement. */
11457 if (keyword == RID_IF)
11458 {
11459 statement = begin_if_stmt ();
11460 IF_STMT_CONSTEXPR_P (statement) = cx;
11461 }
11462 else
11463 statement = begin_switch_stmt ();
11464
11465 /* Parse the optional init-statement. */
11466 if (cp_parser_init_statement_p (parser))
11467 {
11468 tree decl;
11469 if (cxx_dialect < cxx17)
11470 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11471 "init-statement in selection statements only available "
11472 "with -std=c++17 or -std=gnu++17");
11473 cp_parser_init_statement (parser, &decl);
11474 }
11475
11476 /* Parse the condition. */
11477 condition = cp_parser_condition (parser);
11478 /* Look for the `)'. */
11479 if (!parens.require_close (parser))
11480 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11481 /*consume_paren=*/true);
11482
11483 if (keyword == RID_IF)
11484 {
11485 bool nested_if;
11486 unsigned char in_statement;
11487
11488 /* Add the condition. */
11489 condition = finish_if_stmt_cond (condition, statement);
11490
11491 if (warn_duplicated_cond)
11492 warn_duplicated_cond_add_or_warn (token->location, condition,
11493 &chain);
11494
11495 /* Parse the then-clause. */
11496 in_statement = parser->in_statement;
11497 parser->in_statement |= IN_IF_STMT;
11498
11499 /* Outside a template, the non-selected branch of a constexpr
11500 if is a 'discarded statement', i.e. unevaluated. */
11501 bool was_discarded = in_discarded_stmt;
11502 bool discard_then = (cx && !processing_template_decl
11503 && integer_zerop (condition));
11504 if (discard_then)
11505 {
11506 in_discarded_stmt = true;
11507 ++c_inhibit_evaluation_warnings;
11508 }
11509
11510 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11511 guard_tinfo);
11512
11513 parser->in_statement = in_statement;
11514
11515 finish_then_clause (statement);
11516
11517 if (discard_then)
11518 {
11519 THEN_CLAUSE (statement) = NULL_TREE;
11520 in_discarded_stmt = was_discarded;
11521 --c_inhibit_evaluation_warnings;
11522 }
11523
11524 /* If the next token is `else', parse the else-clause. */
11525 if (cp_lexer_next_token_is_keyword (parser->lexer,
11526 RID_ELSE))
11527 {
11528 bool discard_else = (cx && !processing_template_decl
11529 && integer_nonzerop (condition));
11530 if (discard_else)
11531 {
11532 in_discarded_stmt = true;
11533 ++c_inhibit_evaluation_warnings;
11534 }
11535
11536 guard_tinfo
11537 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11538 /* Consume the `else' keyword. */
11539 cp_lexer_consume_token (parser->lexer);
11540 if (warn_duplicated_cond)
11541 {
11542 if (cp_lexer_next_token_is_keyword (parser->lexer,
11543 RID_IF)
11544 && chain == NULL)
11545 {
11546 /* We've got "if (COND) else if (COND2)". Start
11547 the condition chain and add COND as the first
11548 element. */
11549 chain = new vec<tree> ();
11550 if (!CONSTANT_CLASS_P (condition)
11551 && !TREE_SIDE_EFFECTS (condition))
11552 {
11553 /* Wrap it in a NOP_EXPR so that we can set the
11554 location of the condition. */
11555 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11556 condition);
11557 SET_EXPR_LOCATION (e, token->location);
11558 chain->safe_push (e);
11559 }
11560 }
11561 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11562 RID_IF))
11563 {
11564 /* This is if-else without subsequent if. Zap the
11565 condition chain; we would have already warned at
11566 this point. */
11567 delete chain;
11568 chain = NULL;
11569 }
11570 }
11571 begin_else_clause (statement);
11572 /* Parse the else-clause. */
11573 cp_parser_implicitly_scoped_statement (parser, NULL,
11574 guard_tinfo, chain);
11575
11576 finish_else_clause (statement);
11577
11578 /* If we are currently parsing a then-clause, then
11579 IF_P will not be NULL. We set it to true to
11580 indicate that this if statement has an else clause.
11581 This may trigger the Wparentheses warning below
11582 when we get back up to the parent if statement. */
11583 if (if_p != NULL)
11584 *if_p = true;
11585
11586 if (discard_else)
11587 {
11588 ELSE_CLAUSE (statement) = NULL_TREE;
11589 in_discarded_stmt = was_discarded;
11590 --c_inhibit_evaluation_warnings;
11591 }
11592 }
11593 else
11594 {
11595 /* This if statement does not have an else clause. If
11596 NESTED_IF is true, then the then-clause has an if
11597 statement which does have an else clause. We warn
11598 about the potential ambiguity. */
11599 if (nested_if)
11600 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11601 "suggest explicit braces to avoid ambiguous"
11602 " %<else%>");
11603 if (warn_duplicated_cond)
11604 {
11605 /* We don't need the condition chain anymore. */
11606 delete chain;
11607 chain = NULL;
11608 }
11609 }
11610
11611 /* Now we're all done with the if-statement. */
11612 finish_if_stmt (statement);
11613 }
11614 else
11615 {
11616 bool in_switch_statement_p;
11617 unsigned char in_statement;
11618
11619 /* Add the condition. */
11620 finish_switch_cond (condition, statement);
11621
11622 /* Parse the body of the switch-statement. */
11623 in_switch_statement_p = parser->in_switch_statement_p;
11624 in_statement = parser->in_statement;
11625 parser->in_switch_statement_p = true;
11626 parser->in_statement |= IN_SWITCH_STMT;
11627 cp_parser_implicitly_scoped_statement (parser, if_p,
11628 guard_tinfo);
11629 parser->in_switch_statement_p = in_switch_statement_p;
11630 parser->in_statement = in_statement;
11631
11632 /* Now we're all done with the switch-statement. */
11633 finish_switch_stmt (statement);
11634 }
11635
11636 return statement;
11637 }
11638 break;
11639
11640 default:
11641 cp_parser_error (parser, "expected selection-statement");
11642 return error_mark_node;
11643 }
11644 }
11645
11646 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11647 If we have seen at least one decl-specifier, and the next token
11648 is not a parenthesis, then we must be looking at a declaration.
11649 (After "int (" we might be looking at a functional cast.) */
11650
11651 static void
11652 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11653 bool any_specifiers_p)
11654 {
11655 if (any_specifiers_p
11656 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11657 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11658 && !cp_parser_error_occurred (parser))
11659 cp_parser_commit_to_tentative_parse (parser);
11660 }
11661
11662 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11663 The declarator shall not specify a function or an array. Returns
11664 TRUE if the declarator is valid, FALSE otherwise. */
11665
11666 static bool
11667 cp_parser_check_condition_declarator (cp_parser* parser,
11668 cp_declarator *declarator,
11669 location_t loc)
11670 {
11671 if (declarator == cp_error_declarator
11672 || function_declarator_p (declarator)
11673 || declarator->kind == cdk_array)
11674 {
11675 if (declarator == cp_error_declarator)
11676 /* Already complained. */;
11677 else if (declarator->kind == cdk_array)
11678 error_at (loc, "condition declares an array");
11679 else
11680 error_at (loc, "condition declares a function");
11681 if (parser->fully_implicit_function_template_p)
11682 abort_fully_implicit_template (parser);
11683 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11684 /*or_comma=*/false,
11685 /*consume_paren=*/false);
11686 return false;
11687 }
11688 else
11689 return true;
11690 }
11691
11692 /* Parse a condition.
11693
11694 condition:
11695 expression
11696 type-specifier-seq declarator = initializer-clause
11697 type-specifier-seq declarator braced-init-list
11698
11699 GNU Extension:
11700
11701 condition:
11702 type-specifier-seq declarator asm-specification [opt]
11703 attributes [opt] = assignment-expression
11704
11705 Returns the expression that should be tested. */
11706
11707 static tree
11708 cp_parser_condition (cp_parser* parser)
11709 {
11710 cp_decl_specifier_seq type_specifiers;
11711 const char *saved_message;
11712 int declares_class_or_enum;
11713
11714 /* Try the declaration first. */
11715 cp_parser_parse_tentatively (parser);
11716 /* New types are not allowed in the type-specifier-seq for a
11717 condition. */
11718 saved_message = parser->type_definition_forbidden_message;
11719 parser->type_definition_forbidden_message
11720 = G_("types may not be defined in conditions");
11721 /* Parse the type-specifier-seq. */
11722 cp_parser_decl_specifier_seq (parser,
11723 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11724 &type_specifiers,
11725 &declares_class_or_enum);
11726 /* Restore the saved message. */
11727 parser->type_definition_forbidden_message = saved_message;
11728
11729 cp_parser_maybe_commit_to_declaration (parser,
11730 type_specifiers.any_specifiers_p);
11731
11732 /* If all is well, we might be looking at a declaration. */
11733 if (!cp_parser_error_occurred (parser))
11734 {
11735 tree decl;
11736 tree asm_specification;
11737 tree attributes;
11738 cp_declarator *declarator;
11739 tree initializer = NULL_TREE;
11740 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11741
11742 /* Parse the declarator. */
11743 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11744 /*ctor_dtor_or_conv_p=*/NULL,
11745 /*parenthesized_p=*/NULL,
11746 /*member_p=*/false,
11747 /*friend_p=*/false);
11748 /* Parse the attributes. */
11749 attributes = cp_parser_attributes_opt (parser);
11750 /* Parse the asm-specification. */
11751 asm_specification = cp_parser_asm_specification_opt (parser);
11752 /* If the next token is not an `=' or '{', then we might still be
11753 looking at an expression. For example:
11754
11755 if (A(a).x)
11756
11757 looks like a decl-specifier-seq and a declarator -- but then
11758 there is no `=', so this is an expression. */
11759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11760 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11761 cp_parser_simulate_error (parser);
11762
11763 /* If we did see an `=' or '{', then we are looking at a declaration
11764 for sure. */
11765 if (cp_parser_parse_definitely (parser))
11766 {
11767 tree pushed_scope;
11768 bool non_constant_p = false;
11769 int flags = LOOKUP_ONLYCONVERTING;
11770
11771 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11772 return error_mark_node;
11773
11774 /* Create the declaration. */
11775 decl = start_decl (declarator, &type_specifiers,
11776 /*initialized_p=*/true,
11777 attributes, /*prefix_attributes=*/NULL_TREE,
11778 &pushed_scope);
11779
11780 /* Parse the initializer. */
11781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11782 {
11783 initializer = cp_parser_braced_list (parser, &non_constant_p);
11784 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11785 flags = 0;
11786 }
11787 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11788 {
11789 /* Consume the `='. */
11790 cp_lexer_consume_token (parser->lexer);
11791 initializer = cp_parser_initializer_clause (parser,
11792 &non_constant_p);
11793 }
11794 else
11795 {
11796 cp_parser_error (parser, "expected initializer");
11797 initializer = error_mark_node;
11798 }
11799 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11800 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11801
11802 /* Process the initializer. */
11803 cp_finish_decl (decl,
11804 initializer, !non_constant_p,
11805 asm_specification,
11806 flags);
11807
11808 if (pushed_scope)
11809 pop_scope (pushed_scope);
11810
11811 return convert_from_reference (decl);
11812 }
11813 }
11814 /* If we didn't even get past the declarator successfully, we are
11815 definitely not looking at a declaration. */
11816 else
11817 cp_parser_abort_tentative_parse (parser);
11818
11819 /* Otherwise, we are looking at an expression. */
11820 return cp_parser_expression (parser);
11821 }
11822
11823 /* Parses a for-statement or range-for-statement until the closing ')',
11824 not included. */
11825
11826 static tree
11827 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11828 {
11829 tree init, scope, decl;
11830 bool is_range_for;
11831
11832 /* Begin the for-statement. */
11833 scope = begin_for_scope (&init);
11834
11835 /* Parse the initialization. */
11836 is_range_for = cp_parser_init_statement (parser, &decl);
11837
11838 if (is_range_for)
11839 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11840 else
11841 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11842 }
11843
11844 static tree
11845 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11846 unsigned short unroll)
11847 {
11848 /* Normal for loop */
11849 tree condition = NULL_TREE;
11850 tree expression = NULL_TREE;
11851 tree stmt;
11852
11853 stmt = begin_for_stmt (scope, init);
11854 /* The init-statement has already been parsed in
11855 cp_parser_init_statement, so no work is needed here. */
11856 finish_init_stmt (stmt);
11857
11858 /* If there's a condition, process it. */
11859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11860 condition = cp_parser_condition (parser);
11861 else if (ivdep)
11862 {
11863 cp_parser_error (parser, "missing loop condition in loop with "
11864 "%<GCC ivdep%> pragma");
11865 condition = error_mark_node;
11866 }
11867 else if (unroll)
11868 {
11869 cp_parser_error (parser, "missing loop condition in loop with "
11870 "%<GCC unroll%> pragma");
11871 condition = error_mark_node;
11872 }
11873 finish_for_cond (condition, stmt, ivdep, unroll);
11874 /* Look for the `;'. */
11875 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11876
11877 /* If there's an expression, process it. */
11878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11879 expression = cp_parser_expression (parser);
11880 finish_for_expr (expression, stmt);
11881
11882 return stmt;
11883 }
11884
11885 /* Tries to parse a range-based for-statement:
11886
11887 range-based-for:
11888 decl-specifier-seq declarator : expression
11889
11890 The decl-specifier-seq declarator and the `:' are already parsed by
11891 cp_parser_init_statement. If processing_template_decl it returns a
11892 newly created RANGE_FOR_STMT; if not, it is converted to a
11893 regular FOR_STMT. */
11894
11895 static tree
11896 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11897 bool ivdep, unsigned short unroll)
11898 {
11899 tree stmt, range_expr;
11900 auto_vec <cxx_binding *, 16> bindings;
11901 auto_vec <tree, 16> names;
11902 tree decomp_first_name = NULL_TREE;
11903 unsigned int decomp_cnt = 0;
11904
11905 /* Get the range declaration momentarily out of the way so that
11906 the range expression doesn't clash with it. */
11907 if (range_decl != error_mark_node)
11908 {
11909 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11910 {
11911 tree v = DECL_VALUE_EXPR (range_decl);
11912 /* For decomposition declaration get all of the corresponding
11913 declarations out of the way. */
11914 if (TREE_CODE (v) == ARRAY_REF
11915 && VAR_P (TREE_OPERAND (v, 0))
11916 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11917 {
11918 tree d = range_decl;
11919 range_decl = TREE_OPERAND (v, 0);
11920 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11921 decomp_first_name = d;
11922 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11923 {
11924 tree name = DECL_NAME (d);
11925 names.safe_push (name);
11926 bindings.safe_push (IDENTIFIER_BINDING (name));
11927 IDENTIFIER_BINDING (name)
11928 = IDENTIFIER_BINDING (name)->previous;
11929 }
11930 }
11931 }
11932 if (names.is_empty ())
11933 {
11934 tree name = DECL_NAME (range_decl);
11935 names.safe_push (name);
11936 bindings.safe_push (IDENTIFIER_BINDING (name));
11937 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11938 }
11939 }
11940
11941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11942 {
11943 bool expr_non_constant_p;
11944 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11945 }
11946 else
11947 range_expr = cp_parser_expression (parser);
11948
11949 /* Put the range declaration(s) back into scope. */
11950 for (unsigned int i = 0; i < names.length (); i++)
11951 {
11952 cxx_binding *binding = bindings[i];
11953 binding->previous = IDENTIFIER_BINDING (names[i]);
11954 IDENTIFIER_BINDING (names[i]) = binding;
11955 }
11956
11957 /* If in template, STMT is converted to a normal for-statement
11958 at instantiation. If not, it is done just ahead. */
11959 if (processing_template_decl)
11960 {
11961 if (check_for_bare_parameter_packs (range_expr))
11962 range_expr = error_mark_node;
11963 stmt = begin_range_for_stmt (scope, init);
11964 if (ivdep)
11965 RANGE_FOR_IVDEP (stmt) = 1;
11966 if (unroll)
11967 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11968 finish_range_for_decl (stmt, range_decl, range_expr);
11969 if (!type_dependent_expression_p (range_expr)
11970 /* do_auto_deduction doesn't mess with template init-lists. */
11971 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11972 do_range_for_auto_deduction (range_decl, range_expr);
11973 }
11974 else
11975 {
11976 stmt = begin_for_stmt (scope, init);
11977 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11978 decomp_first_name, decomp_cnt, ivdep,
11979 unroll);
11980 }
11981 return stmt;
11982 }
11983
11984 /* Subroutine of cp_convert_range_for: given the initializer expression,
11985 builds up the range temporary. */
11986
11987 static tree
11988 build_range_temp (tree range_expr)
11989 {
11990 tree range_type, range_temp;
11991
11992 /* Find out the type deduced by the declaration
11993 `auto &&__range = range_expr'. */
11994 range_type = cp_build_reference_type (make_auto (), true);
11995 range_type = do_auto_deduction (range_type, range_expr,
11996 type_uses_auto (range_type));
11997
11998 /* Create the __range variable. */
11999 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12000 range_type);
12001 TREE_USED (range_temp) = 1;
12002 DECL_ARTIFICIAL (range_temp) = 1;
12003
12004 return range_temp;
12005 }
12006
12007 /* Used by cp_parser_range_for in template context: we aren't going to
12008 do a full conversion yet, but we still need to resolve auto in the
12009 type of the for-range-declaration if present. This is basically
12010 a shortcut version of cp_convert_range_for. */
12011
12012 static void
12013 do_range_for_auto_deduction (tree decl, tree range_expr)
12014 {
12015 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12016 if (auto_node)
12017 {
12018 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12019 range_temp = convert_from_reference (build_range_temp (range_expr));
12020 iter_type = (cp_parser_perform_range_for_lookup
12021 (range_temp, &begin_dummy, &end_dummy));
12022 if (iter_type)
12023 {
12024 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12025 iter_type);
12026 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12027 RO_UNARY_STAR,
12028 tf_warning_or_error);
12029 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12030 iter_decl, auto_node);
12031 }
12032 }
12033 }
12034
12035 /* Converts a range-based for-statement into a normal
12036 for-statement, as per the definition.
12037
12038 for (RANGE_DECL : RANGE_EXPR)
12039 BLOCK
12040
12041 should be equivalent to:
12042
12043 {
12044 auto &&__range = RANGE_EXPR;
12045 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12046 __begin != __end;
12047 ++__begin)
12048 {
12049 RANGE_DECL = *__begin;
12050 BLOCK
12051 }
12052 }
12053
12054 If RANGE_EXPR is an array:
12055 BEGIN_EXPR = __range
12056 END_EXPR = __range + ARRAY_SIZE(__range)
12057 Else if RANGE_EXPR has a member 'begin' or 'end':
12058 BEGIN_EXPR = __range.begin()
12059 END_EXPR = __range.end()
12060 Else:
12061 BEGIN_EXPR = begin(__range)
12062 END_EXPR = end(__range);
12063
12064 If __range has a member 'begin' but not 'end', or vice versa, we must
12065 still use the second alternative (it will surely fail, however).
12066 When calling begin()/end() in the third alternative we must use
12067 argument dependent lookup, but always considering 'std' as an associated
12068 namespace. */
12069
12070 tree
12071 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12072 tree decomp_first_name, unsigned int decomp_cnt,
12073 bool ivdep, unsigned short unroll)
12074 {
12075 tree begin, end;
12076 tree iter_type, begin_expr, end_expr;
12077 tree condition, expression;
12078
12079 range_expr = mark_lvalue_use (range_expr);
12080
12081 if (range_decl == error_mark_node || range_expr == error_mark_node)
12082 /* If an error happened previously do nothing or else a lot of
12083 unhelpful errors would be issued. */
12084 begin_expr = end_expr = iter_type = error_mark_node;
12085 else
12086 {
12087 tree range_temp;
12088
12089 if (VAR_P (range_expr)
12090 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12091 /* Can't bind a reference to an array of runtime bound. */
12092 range_temp = range_expr;
12093 else
12094 {
12095 range_temp = build_range_temp (range_expr);
12096 pushdecl (range_temp);
12097 cp_finish_decl (range_temp, range_expr,
12098 /*is_constant_init*/false, NULL_TREE,
12099 LOOKUP_ONLYCONVERTING);
12100 range_temp = convert_from_reference (range_temp);
12101 }
12102 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12103 &begin_expr, &end_expr);
12104 }
12105
12106 /* The new for initialization statement. */
12107 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12108 iter_type);
12109 TREE_USED (begin) = 1;
12110 DECL_ARTIFICIAL (begin) = 1;
12111 pushdecl (begin);
12112 cp_finish_decl (begin, begin_expr,
12113 /*is_constant_init*/false, NULL_TREE,
12114 LOOKUP_ONLYCONVERTING);
12115
12116 if (cxx_dialect >= cxx17)
12117 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12118 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12119 TREE_USED (end) = 1;
12120 DECL_ARTIFICIAL (end) = 1;
12121 pushdecl (end);
12122 cp_finish_decl (end, end_expr,
12123 /*is_constant_init*/false, NULL_TREE,
12124 LOOKUP_ONLYCONVERTING);
12125
12126 finish_init_stmt (statement);
12127
12128 /* The new for condition. */
12129 condition = build_x_binary_op (input_location, NE_EXPR,
12130 begin, ERROR_MARK,
12131 end, ERROR_MARK,
12132 NULL, tf_warning_or_error);
12133 finish_for_cond (condition, statement, ivdep, unroll);
12134
12135 /* The new increment expression. */
12136 expression = finish_unary_op_expr (input_location,
12137 PREINCREMENT_EXPR, begin,
12138 tf_warning_or_error);
12139 finish_for_expr (expression, statement);
12140
12141 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12142 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12143
12144 /* The declaration is initialized with *__begin inside the loop body. */
12145 cp_finish_decl (range_decl,
12146 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12147 tf_warning_or_error),
12148 /*is_constant_init*/false, NULL_TREE,
12149 LOOKUP_ONLYCONVERTING);
12150 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12151 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12152
12153 return statement;
12154 }
12155
12156 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12157 We need to solve both at the same time because the method used
12158 depends on the existence of members begin or end.
12159 Returns the type deduced for the iterator expression. */
12160
12161 static tree
12162 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12163 {
12164 if (error_operand_p (range))
12165 {
12166 *begin = *end = error_mark_node;
12167 return error_mark_node;
12168 }
12169
12170 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12171 {
12172 error ("range-based %<for%> expression of type %qT "
12173 "has incomplete type", TREE_TYPE (range));
12174 *begin = *end = error_mark_node;
12175 return error_mark_node;
12176 }
12177 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12178 {
12179 /* If RANGE is an array, we will use pointer arithmetic. */
12180 *begin = decay_conversion (range, tf_warning_or_error);
12181 *end = build_binary_op (input_location, PLUS_EXPR,
12182 range,
12183 array_type_nelts_top (TREE_TYPE (range)),
12184 false);
12185 return TREE_TYPE (*begin);
12186 }
12187 else
12188 {
12189 /* If it is not an array, we must do a bit of magic. */
12190 tree id_begin, id_end;
12191 tree member_begin, member_end;
12192
12193 *begin = *end = error_mark_node;
12194
12195 id_begin = get_identifier ("begin");
12196 id_end = get_identifier ("end");
12197 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12198 /*protect=*/2, /*want_type=*/false,
12199 tf_warning_or_error);
12200 member_end = lookup_member (TREE_TYPE (range), id_end,
12201 /*protect=*/2, /*want_type=*/false,
12202 tf_warning_or_error);
12203
12204 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12205 {
12206 /* Use the member functions. */
12207 *begin = cp_parser_range_for_member_function (range, id_begin);
12208 *end = cp_parser_range_for_member_function (range, id_end);
12209 }
12210 else
12211 {
12212 /* Use global functions with ADL. */
12213 vec<tree, va_gc> *vec;
12214 vec = make_tree_vector ();
12215
12216 vec_safe_push (vec, range);
12217
12218 member_begin = perform_koenig_lookup (id_begin, vec,
12219 tf_warning_or_error);
12220 *begin = finish_call_expr (member_begin, &vec, false, true,
12221 tf_warning_or_error);
12222 member_end = perform_koenig_lookup (id_end, vec,
12223 tf_warning_or_error);
12224 *end = finish_call_expr (member_end, &vec, false, true,
12225 tf_warning_or_error);
12226
12227 release_tree_vector (vec);
12228 }
12229
12230 /* Last common checks. */
12231 if (*begin == error_mark_node || *end == error_mark_node)
12232 {
12233 /* If one of the expressions is an error do no more checks. */
12234 *begin = *end = error_mark_node;
12235 return error_mark_node;
12236 }
12237 else if (type_dependent_expression_p (*begin)
12238 || type_dependent_expression_p (*end))
12239 /* Can happen, when, eg, in a template context, Koenig lookup
12240 can't resolve begin/end (c++/58503). */
12241 return NULL_TREE;
12242 else
12243 {
12244 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12245 /* The unqualified type of the __begin and __end temporaries should
12246 be the same, as required by the multiple auto declaration. */
12247 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12248 {
12249 if (cxx_dialect >= cxx17
12250 && (build_x_binary_op (input_location, NE_EXPR,
12251 *begin, ERROR_MARK,
12252 *end, ERROR_MARK,
12253 NULL, tf_none)
12254 != error_mark_node))
12255 /* P0184R0 allows __begin and __end to have different types,
12256 but make sure they are comparable so we can give a better
12257 diagnostic. */;
12258 else
12259 error ("inconsistent begin/end types in range-based %<for%> "
12260 "statement: %qT and %qT",
12261 TREE_TYPE (*begin), TREE_TYPE (*end));
12262 }
12263 return iter_type;
12264 }
12265 }
12266 }
12267
12268 /* Helper function for cp_parser_perform_range_for_lookup.
12269 Builds a tree for RANGE.IDENTIFIER(). */
12270
12271 static tree
12272 cp_parser_range_for_member_function (tree range, tree identifier)
12273 {
12274 tree member, res;
12275 vec<tree, va_gc> *vec;
12276
12277 member = finish_class_member_access_expr (range, identifier,
12278 false, tf_warning_or_error);
12279 if (member == error_mark_node)
12280 return error_mark_node;
12281
12282 vec = make_tree_vector ();
12283 res = finish_call_expr (member, &vec,
12284 /*disallow_virtual=*/false,
12285 /*koenig_p=*/false,
12286 tf_warning_or_error);
12287 release_tree_vector (vec);
12288 return res;
12289 }
12290
12291 /* Parse an iteration-statement.
12292
12293 iteration-statement:
12294 while ( condition ) statement
12295 do statement while ( expression ) ;
12296 for ( init-statement condition [opt] ; expression [opt] )
12297 statement
12298
12299 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12300
12301 static tree
12302 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12303 unsigned short unroll)
12304 {
12305 cp_token *token;
12306 enum rid keyword;
12307 tree statement;
12308 unsigned char in_statement;
12309 token_indent_info guard_tinfo;
12310
12311 /* Peek at the next token. */
12312 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12313 if (!token)
12314 return error_mark_node;
12315
12316 guard_tinfo = get_token_indent_info (token);
12317
12318 /* Remember whether or not we are already within an iteration
12319 statement. */
12320 in_statement = parser->in_statement;
12321
12322 /* See what kind of keyword it is. */
12323 keyword = token->keyword;
12324 switch (keyword)
12325 {
12326 case RID_WHILE:
12327 {
12328 tree condition;
12329
12330 /* Begin the while-statement. */
12331 statement = begin_while_stmt ();
12332 /* Look for the `('. */
12333 matching_parens parens;
12334 parens.require_open (parser);
12335 /* Parse the condition. */
12336 condition = cp_parser_condition (parser);
12337 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12338 /* Look for the `)'. */
12339 parens.require_close (parser);
12340 /* Parse the dependent statement. */
12341 parser->in_statement = IN_ITERATION_STMT;
12342 bool prev = note_iteration_stmt_body_start ();
12343 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12344 note_iteration_stmt_body_end (prev);
12345 parser->in_statement = in_statement;
12346 /* We're done with the while-statement. */
12347 finish_while_stmt (statement);
12348 }
12349 break;
12350
12351 case RID_DO:
12352 {
12353 tree expression;
12354
12355 /* Begin the do-statement. */
12356 statement = begin_do_stmt ();
12357 /* Parse the body of the do-statement. */
12358 parser->in_statement = IN_ITERATION_STMT;
12359 bool prev = note_iteration_stmt_body_start ();
12360 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12361 note_iteration_stmt_body_end (prev);
12362 parser->in_statement = in_statement;
12363 finish_do_body (statement);
12364 /* Look for the `while' keyword. */
12365 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12366 /* Look for the `('. */
12367 matching_parens parens;
12368 parens.require_open (parser);
12369 /* Parse the expression. */
12370 expression = cp_parser_expression (parser);
12371 /* We're done with the do-statement. */
12372 finish_do_stmt (expression, statement, ivdep, unroll);
12373 /* Look for the `)'. */
12374 parens.require_close (parser);
12375 /* Look for the `;'. */
12376 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12377 }
12378 break;
12379
12380 case RID_FOR:
12381 {
12382 /* Look for the `('. */
12383 matching_parens parens;
12384 parens.require_open (parser);
12385
12386 statement = cp_parser_for (parser, ivdep, unroll);
12387
12388 /* Look for the `)'. */
12389 parens.require_close (parser);
12390
12391 /* Parse the body of the for-statement. */
12392 parser->in_statement = IN_ITERATION_STMT;
12393 bool prev = note_iteration_stmt_body_start ();
12394 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12395 note_iteration_stmt_body_end (prev);
12396 parser->in_statement = in_statement;
12397
12398 /* We're done with the for-statement. */
12399 finish_for_stmt (statement);
12400 }
12401 break;
12402
12403 default:
12404 cp_parser_error (parser, "expected iteration-statement");
12405 statement = error_mark_node;
12406 break;
12407 }
12408
12409 return statement;
12410 }
12411
12412 /* Parse a init-statement or the declarator of a range-based-for.
12413 Returns true if a range-based-for declaration is seen.
12414
12415 init-statement:
12416 expression-statement
12417 simple-declaration */
12418
12419 static bool
12420 cp_parser_init_statement (cp_parser *parser, tree *decl)
12421 {
12422 /* If the next token is a `;', then we have an empty
12423 expression-statement. Grammatically, this is also a
12424 simple-declaration, but an invalid one, because it does not
12425 declare anything. Therefore, if we did not handle this case
12426 specially, we would issue an error message about an invalid
12427 declaration. */
12428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12429 {
12430 bool is_range_for = false;
12431 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12432
12433 /* Try to parse the init-statement. */
12434 if (cp_parser_range_based_for_with_init_p (parser))
12435 {
12436 tree dummy;
12437 cp_parser_parse_tentatively (parser);
12438 /* Parse the declaration. */
12439 cp_parser_simple_declaration (parser,
12440 /*function_definition_allowed_p=*/false,
12441 &dummy);
12442 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12443 if (!cp_parser_parse_definitely (parser))
12444 /* That didn't work, try to parse it as an expression-statement. */
12445 cp_parser_expression_statement (parser, NULL_TREE);
12446
12447 if (cxx_dialect < cxx2a)
12448 {
12449 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12450 "range-based %<for%> loops with initializer only "
12451 "available with -std=c++2a or -std=gnu++2a");
12452 *decl = error_mark_node;
12453 }
12454 }
12455
12456 /* A colon is used in range-based for. */
12457 parser->colon_corrects_to_scope_p = false;
12458
12459 /* We're going to speculatively look for a declaration, falling back
12460 to an expression, if necessary. */
12461 cp_parser_parse_tentatively (parser);
12462 /* Parse the declaration. */
12463 cp_parser_simple_declaration (parser,
12464 /*function_definition_allowed_p=*/false,
12465 decl);
12466 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12467 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12468 {
12469 /* It is a range-for, consume the ':'. */
12470 cp_lexer_consume_token (parser->lexer);
12471 is_range_for = true;
12472 if (cxx_dialect < cxx11)
12473 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12474 "range-based %<for%> loops only available with "
12475 "-std=c++11 or -std=gnu++11");
12476 }
12477 else
12478 /* The ';' is not consumed yet because we told
12479 cp_parser_simple_declaration not to. */
12480 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12481
12482 if (cp_parser_parse_definitely (parser))
12483 return is_range_for;
12484 /* If the tentative parse failed, then we shall need to look for an
12485 expression-statement. */
12486 }
12487 /* If we are here, it is an expression-statement. */
12488 cp_parser_expression_statement (parser, NULL_TREE);
12489 return false;
12490 }
12491
12492 /* Parse a jump-statement.
12493
12494 jump-statement:
12495 break ;
12496 continue ;
12497 return expression [opt] ;
12498 return braced-init-list ;
12499 goto identifier ;
12500
12501 GNU extension:
12502
12503 jump-statement:
12504 goto * expression ;
12505
12506 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12507
12508 static tree
12509 cp_parser_jump_statement (cp_parser* parser)
12510 {
12511 tree statement = error_mark_node;
12512 cp_token *token;
12513 enum rid keyword;
12514 unsigned char in_statement;
12515
12516 /* Peek at the next token. */
12517 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12518 if (!token)
12519 return error_mark_node;
12520
12521 /* See what kind of keyword it is. */
12522 keyword = token->keyword;
12523 switch (keyword)
12524 {
12525 case RID_BREAK:
12526 in_statement = parser->in_statement & ~IN_IF_STMT;
12527 switch (in_statement)
12528 {
12529 case 0:
12530 error_at (token->location, "break statement not within loop or switch");
12531 break;
12532 default:
12533 gcc_assert ((in_statement & IN_SWITCH_STMT)
12534 || in_statement == IN_ITERATION_STMT);
12535 statement = finish_break_stmt ();
12536 if (in_statement == IN_ITERATION_STMT)
12537 break_maybe_infinite_loop ();
12538 break;
12539 case IN_OMP_BLOCK:
12540 error_at (token->location, "invalid exit from OpenMP structured block");
12541 break;
12542 case IN_OMP_FOR:
12543 error_at (token->location, "break statement used with OpenMP for loop");
12544 break;
12545 }
12546 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12547 break;
12548
12549 case RID_CONTINUE:
12550 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12551 {
12552 case 0:
12553 error_at (token->location, "continue statement not within a loop");
12554 break;
12555 /* Fall through. */
12556 case IN_ITERATION_STMT:
12557 case IN_OMP_FOR:
12558 statement = finish_continue_stmt ();
12559 break;
12560 case IN_OMP_BLOCK:
12561 error_at (token->location, "invalid exit from OpenMP structured block");
12562 break;
12563 default:
12564 gcc_unreachable ();
12565 }
12566 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12567 break;
12568
12569 case RID_RETURN:
12570 {
12571 tree expr;
12572 bool expr_non_constant_p;
12573
12574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12575 {
12576 cp_lexer_set_source_position (parser->lexer);
12577 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12578 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12579 }
12580 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12581 expr = cp_parser_expression (parser);
12582 else
12583 /* If the next token is a `;', then there is no
12584 expression. */
12585 expr = NULL_TREE;
12586 /* Build the return-statement. */
12587 if (current_function_auto_return_pattern && in_discarded_stmt)
12588 /* Don't deduce from a discarded return statement. */;
12589 else
12590 statement = finish_return_stmt (expr);
12591 /* Look for the final `;'. */
12592 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12593 }
12594 break;
12595
12596 case RID_GOTO:
12597 if (parser->in_function_body
12598 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12599 {
12600 error ("%<goto%> in %<constexpr%> function");
12601 cp_function_chain->invalid_constexpr = true;
12602 }
12603
12604 /* Create the goto-statement. */
12605 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12606 {
12607 /* Issue a warning about this use of a GNU extension. */
12608 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12609 /* Consume the '*' token. */
12610 cp_lexer_consume_token (parser->lexer);
12611 /* Parse the dependent expression. */
12612 finish_goto_stmt (cp_parser_expression (parser));
12613 }
12614 else
12615 finish_goto_stmt (cp_parser_identifier (parser));
12616 /* Look for the final `;'. */
12617 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12618 break;
12619
12620 default:
12621 cp_parser_error (parser, "expected jump-statement");
12622 break;
12623 }
12624
12625 return statement;
12626 }
12627
12628 /* Parse a declaration-statement.
12629
12630 declaration-statement:
12631 block-declaration */
12632
12633 static void
12634 cp_parser_declaration_statement (cp_parser* parser)
12635 {
12636 void *p;
12637
12638 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12639 p = obstack_alloc (&declarator_obstack, 0);
12640
12641 /* Parse the block-declaration. */
12642 cp_parser_block_declaration (parser, /*statement_p=*/true);
12643
12644 /* Free any declarators allocated. */
12645 obstack_free (&declarator_obstack, p);
12646 }
12647
12648 /* Some dependent statements (like `if (cond) statement'), are
12649 implicitly in their own scope. In other words, if the statement is
12650 a single statement (as opposed to a compound-statement), it is
12651 none-the-less treated as if it were enclosed in braces. Any
12652 declarations appearing in the dependent statement are out of scope
12653 after control passes that point. This function parses a statement,
12654 but ensures that is in its own scope, even if it is not a
12655 compound-statement.
12656
12657 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12658 is a (possibly labeled) if statement which is not enclosed in
12659 braces and has an else clause. This is used to implement
12660 -Wparentheses.
12661
12662 CHAIN is a vector of if-else-if conditions. This is used to implement
12663 -Wduplicated-cond.
12664
12665 Returns the new statement. */
12666
12667 static tree
12668 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12669 const token_indent_info &guard_tinfo,
12670 vec<tree> *chain)
12671 {
12672 tree statement;
12673 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12674 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12675 token_indent_info body_tinfo
12676 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12677
12678 if (if_p != NULL)
12679 *if_p = false;
12680
12681 /* Mark if () ; with a special NOP_EXPR. */
12682 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12683 {
12684 cp_lexer_consume_token (parser->lexer);
12685 statement = add_stmt (build_empty_stmt (body_loc));
12686
12687 if (guard_tinfo.keyword == RID_IF
12688 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12689 warning_at (body_loc, OPT_Wempty_body,
12690 "suggest braces around empty body in an %<if%> statement");
12691 else if (guard_tinfo.keyword == RID_ELSE)
12692 warning_at (body_loc, OPT_Wempty_body,
12693 "suggest braces around empty body in an %<else%> statement");
12694 }
12695 /* if a compound is opened, we simply parse the statement directly. */
12696 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12697 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12698 /* If the token is not a `{', then we must take special action. */
12699 else
12700 {
12701 /* Create a compound-statement. */
12702 statement = begin_compound_stmt (0);
12703 /* Parse the dependent-statement. */
12704 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12705 &body_loc_after_labels);
12706 /* Finish the dummy compound-statement. */
12707 finish_compound_stmt (statement);
12708 }
12709
12710 token_indent_info next_tinfo
12711 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12712 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12713
12714 if (body_loc_after_labels != UNKNOWN_LOCATION
12715 && next_tinfo.type != CPP_SEMICOLON)
12716 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12717 guard_tinfo.location, guard_tinfo.keyword);
12718
12719 /* Return the statement. */
12720 return statement;
12721 }
12722
12723 /* For some dependent statements (like `while (cond) statement'), we
12724 have already created a scope. Therefore, even if the dependent
12725 statement is a compound-statement, we do not want to create another
12726 scope. */
12727
12728 static void
12729 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12730 const token_indent_info &guard_tinfo)
12731 {
12732 /* If the token is a `{', then we must take special action. */
12733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12734 {
12735 token_indent_info body_tinfo
12736 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12737 location_t loc_after_labels = UNKNOWN_LOCATION;
12738
12739 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12740 &loc_after_labels);
12741 token_indent_info next_tinfo
12742 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12743 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12744
12745 if (loc_after_labels != UNKNOWN_LOCATION
12746 && next_tinfo.type != CPP_SEMICOLON)
12747 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12748 guard_tinfo.location,
12749 guard_tinfo.keyword);
12750 }
12751 else
12752 {
12753 /* Avoid calling cp_parser_compound_statement, so that we
12754 don't create a new scope. Do everything else by hand. */
12755 matching_braces braces;
12756 braces.require_open (parser);
12757 /* If the next keyword is `__label__' we have a label declaration. */
12758 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12759 cp_parser_label_declaration (parser);
12760 /* Parse an (optional) statement-seq. */
12761 cp_parser_statement_seq_opt (parser, NULL_TREE);
12762 braces.require_close (parser);
12763 }
12764 }
12765
12766 /* Declarations [gram.dcl.dcl] */
12767
12768 /* Parse an optional declaration-sequence.
12769
12770 declaration-seq:
12771 declaration
12772 declaration-seq declaration */
12773
12774 static void
12775 cp_parser_declaration_seq_opt (cp_parser* parser)
12776 {
12777 while (true)
12778 {
12779 cp_token *token = cp_lexer_peek_token (parser->lexer);
12780
12781 if (token->type == CPP_CLOSE_BRACE
12782 || token->type == CPP_EOF)
12783 break;
12784 else
12785 cp_parser_toplevel_declaration (parser);
12786 }
12787 }
12788
12789 /* Parse a declaration.
12790
12791 declaration:
12792 block-declaration
12793 function-definition
12794 template-declaration
12795 explicit-instantiation
12796 explicit-specialization
12797 linkage-specification
12798 namespace-definition
12799
12800 C++17:
12801 deduction-guide
12802
12803 GNU extension:
12804
12805 declaration:
12806 __extension__ declaration */
12807
12808 static void
12809 cp_parser_declaration (cp_parser* parser)
12810 {
12811 cp_token token1;
12812 cp_token token2;
12813 int saved_pedantic;
12814 void *p;
12815 tree attributes = NULL_TREE;
12816
12817 /* Check for the `__extension__' keyword. */
12818 if (cp_parser_extension_opt (parser, &saved_pedantic))
12819 {
12820 /* Parse the qualified declaration. */
12821 cp_parser_declaration (parser);
12822 /* Restore the PEDANTIC flag. */
12823 pedantic = saved_pedantic;
12824
12825 return;
12826 }
12827
12828 /* Try to figure out what kind of declaration is present. */
12829 token1 = *cp_lexer_peek_token (parser->lexer);
12830
12831 if (token1.type != CPP_EOF)
12832 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12833 else
12834 {
12835 token2.type = CPP_EOF;
12836 token2.keyword = RID_MAX;
12837 }
12838
12839 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12840 p = obstack_alloc (&declarator_obstack, 0);
12841
12842 /* If the next token is `extern' and the following token is a string
12843 literal, then we have a linkage specification. */
12844 if (token1.keyword == RID_EXTERN
12845 && cp_parser_is_pure_string_literal (&token2))
12846 cp_parser_linkage_specification (parser);
12847 /* If the next token is `template', then we have either a template
12848 declaration, an explicit instantiation, or an explicit
12849 specialization. */
12850 else if (token1.keyword == RID_TEMPLATE)
12851 {
12852 /* `template <>' indicates a template specialization. */
12853 if (token2.type == CPP_LESS
12854 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12855 cp_parser_explicit_specialization (parser);
12856 /* `template <' indicates a template declaration. */
12857 else if (token2.type == CPP_LESS)
12858 cp_parser_template_declaration (parser, /*member_p=*/false);
12859 /* Anything else must be an explicit instantiation. */
12860 else
12861 cp_parser_explicit_instantiation (parser);
12862 }
12863 /* If the next token is `export', then we have a template
12864 declaration. */
12865 else if (token1.keyword == RID_EXPORT)
12866 cp_parser_template_declaration (parser, /*member_p=*/false);
12867 /* If the next token is `extern', 'static' or 'inline' and the one
12868 after that is `template', we have a GNU extended explicit
12869 instantiation directive. */
12870 else if (cp_parser_allow_gnu_extensions_p (parser)
12871 && (token1.keyword == RID_EXTERN
12872 || token1.keyword == RID_STATIC
12873 || token1.keyword == RID_INLINE)
12874 && token2.keyword == RID_TEMPLATE)
12875 cp_parser_explicit_instantiation (parser);
12876 /* If the next token is `namespace', check for a named or unnamed
12877 namespace definition. */
12878 else if (token1.keyword == RID_NAMESPACE
12879 && (/* A named namespace definition. */
12880 (token2.type == CPP_NAME
12881 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12882 != CPP_EQ))
12883 || (token2.type == CPP_OPEN_SQUARE
12884 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12885 == CPP_OPEN_SQUARE)
12886 /* An unnamed namespace definition. */
12887 || token2.type == CPP_OPEN_BRACE
12888 || token2.keyword == RID_ATTRIBUTE))
12889 cp_parser_namespace_definition (parser);
12890 /* An inline (associated) namespace definition. */
12891 else if (token1.keyword == RID_INLINE
12892 && token2.keyword == RID_NAMESPACE)
12893 cp_parser_namespace_definition (parser);
12894 /* Objective-C++ declaration/definition. */
12895 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12896 cp_parser_objc_declaration (parser, NULL_TREE);
12897 else if (c_dialect_objc ()
12898 && token1.keyword == RID_ATTRIBUTE
12899 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12900 cp_parser_objc_declaration (parser, attributes);
12901 /* At this point we may have a template declared by a concept
12902 introduction. */
12903 else if (flag_concepts
12904 && cp_parser_template_declaration_after_export (parser,
12905 /*member_p=*/false))
12906 /* We did. */;
12907 else
12908 /* Try to parse a block-declaration, or a function-definition. */
12909 cp_parser_block_declaration (parser, /*statement_p=*/false);
12910
12911 /* Free any declarators allocated. */
12912 obstack_free (&declarator_obstack, p);
12913 }
12914
12915 /* Parse a namespace-scope declaration. */
12916
12917 static void
12918 cp_parser_toplevel_declaration (cp_parser* parser)
12919 {
12920 cp_token *token = cp_lexer_peek_token (parser->lexer);
12921
12922 if (token->type == CPP_PRAGMA)
12923 /* A top-level declaration can consist solely of a #pragma. A
12924 nested declaration cannot, so this is done here and not in
12925 cp_parser_declaration. (A #pragma at block scope is
12926 handled in cp_parser_statement.) */
12927 cp_parser_pragma (parser, pragma_external, NULL);
12928 else if (token->type == CPP_SEMICOLON)
12929 {
12930 /* A declaration consisting of a single semicolon is
12931 invalid. Allow it unless we're being pedantic. */
12932 cp_lexer_consume_token (parser->lexer);
12933 if (!in_system_header_at (input_location))
12934 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12935 }
12936 else
12937 /* Parse the declaration itself. */
12938 cp_parser_declaration (parser);
12939 }
12940
12941 /* Parse a block-declaration.
12942
12943 block-declaration:
12944 simple-declaration
12945 asm-definition
12946 namespace-alias-definition
12947 using-declaration
12948 using-directive
12949
12950 GNU Extension:
12951
12952 block-declaration:
12953 __extension__ block-declaration
12954
12955 C++0x Extension:
12956
12957 block-declaration:
12958 static_assert-declaration
12959
12960 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12961 part of a declaration-statement. */
12962
12963 static void
12964 cp_parser_block_declaration (cp_parser *parser,
12965 bool statement_p)
12966 {
12967 cp_token *token1;
12968 int saved_pedantic;
12969
12970 /* Check for the `__extension__' keyword. */
12971 if (cp_parser_extension_opt (parser, &saved_pedantic))
12972 {
12973 /* Parse the qualified declaration. */
12974 cp_parser_block_declaration (parser, statement_p);
12975 /* Restore the PEDANTIC flag. */
12976 pedantic = saved_pedantic;
12977
12978 return;
12979 }
12980
12981 /* Peek at the next token to figure out which kind of declaration is
12982 present. */
12983 token1 = cp_lexer_peek_token (parser->lexer);
12984
12985 /* If the next keyword is `asm', we have an asm-definition. */
12986 if (token1->keyword == RID_ASM)
12987 {
12988 if (statement_p)
12989 cp_parser_commit_to_tentative_parse (parser);
12990 cp_parser_asm_definition (parser);
12991 }
12992 /* If the next keyword is `namespace', we have a
12993 namespace-alias-definition. */
12994 else if (token1->keyword == RID_NAMESPACE)
12995 cp_parser_namespace_alias_definition (parser);
12996 /* If the next keyword is `using', we have a
12997 using-declaration, a using-directive, or an alias-declaration. */
12998 else if (token1->keyword == RID_USING)
12999 {
13000 cp_token *token2;
13001
13002 if (statement_p)
13003 cp_parser_commit_to_tentative_parse (parser);
13004 /* If the token after `using' is `namespace', then we have a
13005 using-directive. */
13006 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13007 if (token2->keyword == RID_NAMESPACE)
13008 cp_parser_using_directive (parser);
13009 /* If the second token after 'using' is '=', then we have an
13010 alias-declaration. */
13011 else if (cxx_dialect >= cxx11
13012 && token2->type == CPP_NAME
13013 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13014 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13015 cp_parser_alias_declaration (parser);
13016 /* Otherwise, it's a using-declaration. */
13017 else
13018 cp_parser_using_declaration (parser,
13019 /*access_declaration_p=*/false);
13020 }
13021 /* If the next keyword is `__label__' we have a misplaced label
13022 declaration. */
13023 else if (token1->keyword == RID_LABEL)
13024 {
13025 cp_lexer_consume_token (parser->lexer);
13026 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13027 cp_parser_skip_to_end_of_statement (parser);
13028 /* If the next token is now a `;', consume it. */
13029 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13030 cp_lexer_consume_token (parser->lexer);
13031 }
13032 /* If the next token is `static_assert' we have a static assertion. */
13033 else if (token1->keyword == RID_STATIC_ASSERT)
13034 cp_parser_static_assert (parser, /*member_p=*/false);
13035 /* Anything else must be a simple-declaration. */
13036 else
13037 cp_parser_simple_declaration (parser, !statement_p,
13038 /*maybe_range_for_decl*/NULL);
13039 }
13040
13041 /* Parse a simple-declaration.
13042
13043 simple-declaration:
13044 decl-specifier-seq [opt] init-declarator-list [opt] ;
13045 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13046 brace-or-equal-initializer ;
13047
13048 init-declarator-list:
13049 init-declarator
13050 init-declarator-list , init-declarator
13051
13052 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13053 function-definition as a simple-declaration.
13054
13055 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13056 parsed declaration if it is an uninitialized single declarator not followed
13057 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13058 if present, will not be consumed. */
13059
13060 static void
13061 cp_parser_simple_declaration (cp_parser* parser,
13062 bool function_definition_allowed_p,
13063 tree *maybe_range_for_decl)
13064 {
13065 cp_decl_specifier_seq decl_specifiers;
13066 int declares_class_or_enum;
13067 bool saw_declarator;
13068 location_t comma_loc = UNKNOWN_LOCATION;
13069 location_t init_loc = UNKNOWN_LOCATION;
13070
13071 if (maybe_range_for_decl)
13072 *maybe_range_for_decl = NULL_TREE;
13073
13074 /* Defer access checks until we know what is being declared; the
13075 checks for names appearing in the decl-specifier-seq should be
13076 done as if we were in the scope of the thing being declared. */
13077 push_deferring_access_checks (dk_deferred);
13078
13079 /* Parse the decl-specifier-seq. We have to keep track of whether
13080 or not the decl-specifier-seq declares a named class or
13081 enumeration type, since that is the only case in which the
13082 init-declarator-list is allowed to be empty.
13083
13084 [dcl.dcl]
13085
13086 In a simple-declaration, the optional init-declarator-list can be
13087 omitted only when declaring a class or enumeration, that is when
13088 the decl-specifier-seq contains either a class-specifier, an
13089 elaborated-type-specifier, or an enum-specifier. */
13090 cp_parser_decl_specifier_seq (parser,
13091 CP_PARSER_FLAGS_OPTIONAL,
13092 &decl_specifiers,
13093 &declares_class_or_enum);
13094 /* We no longer need to defer access checks. */
13095 stop_deferring_access_checks ();
13096
13097 /* In a block scope, a valid declaration must always have a
13098 decl-specifier-seq. By not trying to parse declarators, we can
13099 resolve the declaration/expression ambiguity more quickly. */
13100 if (!function_definition_allowed_p
13101 && !decl_specifiers.any_specifiers_p)
13102 {
13103 cp_parser_error (parser, "expected declaration");
13104 goto done;
13105 }
13106
13107 /* If the next two tokens are both identifiers, the code is
13108 erroneous. The usual cause of this situation is code like:
13109
13110 T t;
13111
13112 where "T" should name a type -- but does not. */
13113 if (!decl_specifiers.any_type_specifiers_p
13114 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13115 {
13116 /* If parsing tentatively, we should commit; we really are
13117 looking at a declaration. */
13118 cp_parser_commit_to_tentative_parse (parser);
13119 /* Give up. */
13120 goto done;
13121 }
13122
13123 cp_parser_maybe_commit_to_declaration (parser,
13124 decl_specifiers.any_specifiers_p);
13125
13126 /* Look for C++17 decomposition declaration. */
13127 for (size_t n = 1; ; n++)
13128 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13129 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13130 continue;
13131 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13132 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13133 && decl_specifiers.any_specifiers_p)
13134 {
13135 tree decl
13136 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13137 maybe_range_for_decl,
13138 &init_loc);
13139
13140 /* The next token should be either a `,' or a `;'. */
13141 cp_token *token = cp_lexer_peek_token (parser->lexer);
13142 /* If it's a `;', we are done. */
13143 if (token->type == CPP_SEMICOLON)
13144 goto finish;
13145 else if (maybe_range_for_decl)
13146 {
13147 if (*maybe_range_for_decl == NULL_TREE)
13148 *maybe_range_for_decl = error_mark_node;
13149 goto finish;
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 }
13168 else
13169 break;
13170
13171 tree last_type;
13172 bool auto_specifier_p;
13173 /* NULL_TREE if both variable and function declaration are allowed,
13174 error_mark_node if function declaration are not allowed and
13175 a FUNCTION_DECL that should be diagnosed if it is followed by
13176 variable declarations. */
13177 tree auto_function_declaration;
13178
13179 last_type = NULL_TREE;
13180 auto_specifier_p
13181 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13182 auto_function_declaration = NULL_TREE;
13183
13184 /* Keep going until we hit the `;' at the end of the simple
13185 declaration. */
13186 saw_declarator = false;
13187 while (cp_lexer_next_token_is_not (parser->lexer,
13188 CPP_SEMICOLON))
13189 {
13190 cp_token *token;
13191 bool function_definition_p;
13192 tree decl;
13193 tree auto_result = NULL_TREE;
13194
13195 if (saw_declarator)
13196 {
13197 /* If we are processing next declarator, comma is expected */
13198 token = cp_lexer_peek_token (parser->lexer);
13199 gcc_assert (token->type == CPP_COMMA);
13200 cp_lexer_consume_token (parser->lexer);
13201 if (maybe_range_for_decl)
13202 {
13203 *maybe_range_for_decl = error_mark_node;
13204 if (comma_loc == UNKNOWN_LOCATION)
13205 comma_loc = token->location;
13206 }
13207 }
13208 else
13209 saw_declarator = true;
13210
13211 /* Parse the init-declarator. */
13212 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13213 /*checks=*/NULL,
13214 function_definition_allowed_p,
13215 /*member_p=*/false,
13216 declares_class_or_enum,
13217 &function_definition_p,
13218 maybe_range_for_decl,
13219 &init_loc,
13220 &auto_result);
13221 /* If an error occurred while parsing tentatively, exit quickly.
13222 (That usually happens when in the body of a function; each
13223 statement is treated as a declaration-statement until proven
13224 otherwise.) */
13225 if (cp_parser_error_occurred (parser))
13226 goto done;
13227
13228 if (auto_specifier_p && cxx_dialect >= cxx14)
13229 {
13230 /* If the init-declarator-list contains more than one
13231 init-declarator, they shall all form declarations of
13232 variables. */
13233 if (auto_function_declaration == NULL_TREE)
13234 auto_function_declaration
13235 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13236 else if (TREE_CODE (decl) == FUNCTION_DECL
13237 || auto_function_declaration != error_mark_node)
13238 {
13239 error_at (decl_specifiers.locations[ds_type_spec],
13240 "non-variable %qD in declaration with more than one "
13241 "declarator with placeholder type",
13242 TREE_CODE (decl) == FUNCTION_DECL
13243 ? decl : auto_function_declaration);
13244 auto_function_declaration = error_mark_node;
13245 }
13246 }
13247
13248 if (auto_result
13249 && (!processing_template_decl || !type_uses_auto (auto_result)))
13250 {
13251 if (last_type
13252 && last_type != error_mark_node
13253 && !same_type_p (auto_result, last_type))
13254 {
13255 /* If the list of declarators contains more than one declarator,
13256 the type of each declared variable is determined as described
13257 above. If the type deduced for the template parameter U is not
13258 the same in each deduction, the program is ill-formed. */
13259 error_at (decl_specifiers.locations[ds_type_spec],
13260 "inconsistent deduction for %qT: %qT and then %qT",
13261 decl_specifiers.type, last_type, auto_result);
13262 last_type = error_mark_node;
13263 }
13264 else
13265 last_type = auto_result;
13266 }
13267
13268 /* Handle function definitions specially. */
13269 if (function_definition_p)
13270 {
13271 /* If the next token is a `,', then we are probably
13272 processing something like:
13273
13274 void f() {}, *p;
13275
13276 which is erroneous. */
13277 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13278 {
13279 cp_token *token = cp_lexer_peek_token (parser->lexer);
13280 error_at (token->location,
13281 "mixing"
13282 " declarations and function-definitions is forbidden");
13283 }
13284 /* Otherwise, we're done with the list of declarators. */
13285 else
13286 {
13287 pop_deferring_access_checks ();
13288 return;
13289 }
13290 }
13291 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13292 *maybe_range_for_decl = decl;
13293 /* The next token should be either a `,' or a `;'. */
13294 token = cp_lexer_peek_token (parser->lexer);
13295 /* If it's a `,', there are more declarators to come. */
13296 if (token->type == CPP_COMMA)
13297 /* will be consumed next time around */;
13298 /* If it's a `;', we are done. */
13299 else if (token->type == CPP_SEMICOLON)
13300 break;
13301 else if (maybe_range_for_decl)
13302 {
13303 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13304 permerror (decl_specifiers.locations[ds_type_spec],
13305 "types may not be defined in a for-range-declaration");
13306 break;
13307 }
13308 /* Anything else is an error. */
13309 else
13310 {
13311 /* If we have already issued an error message we don't need
13312 to issue another one. */
13313 if ((decl != error_mark_node
13314 && DECL_INITIAL (decl) != error_mark_node)
13315 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13316 cp_parser_error (parser, "expected %<,%> or %<;%>");
13317 /* Skip tokens until we reach the end of the statement. */
13318 cp_parser_skip_to_end_of_statement (parser);
13319 /* If the next token is now a `;', consume it. */
13320 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13321 cp_lexer_consume_token (parser->lexer);
13322 goto done;
13323 }
13324 /* After the first time around, a function-definition is not
13325 allowed -- even if it was OK at first. For example:
13326
13327 int i, f() {}
13328
13329 is not valid. */
13330 function_definition_allowed_p = false;
13331 }
13332
13333 /* Issue an error message if no declarators are present, and the
13334 decl-specifier-seq does not itself declare a class or
13335 enumeration: [dcl.dcl]/3. */
13336 if (!saw_declarator)
13337 {
13338 if (cp_parser_declares_only_class_p (parser))
13339 {
13340 if (!declares_class_or_enum
13341 && decl_specifiers.type
13342 && OVERLOAD_TYPE_P (decl_specifiers.type))
13343 /* Ensure an error is issued anyway when finish_decltype_type,
13344 called via cp_parser_decl_specifier_seq, returns a class or
13345 an enumeration (c++/51786). */
13346 decl_specifiers.type = NULL_TREE;
13347 shadow_tag (&decl_specifiers);
13348 }
13349 /* Perform any deferred access checks. */
13350 perform_deferred_access_checks (tf_warning_or_error);
13351 }
13352
13353 /* Consume the `;'. */
13354 finish:
13355 if (!maybe_range_for_decl)
13356 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13357 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13358 {
13359 if (init_loc != UNKNOWN_LOCATION)
13360 error_at (init_loc, "initializer in range-based %<for%> loop");
13361 if (comma_loc != UNKNOWN_LOCATION)
13362 error_at (comma_loc,
13363 "multiple declarations in range-based %<for%> loop");
13364 }
13365
13366 done:
13367 pop_deferring_access_checks ();
13368 }
13369
13370 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13371 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13372 initializer ; */
13373
13374 static tree
13375 cp_parser_decomposition_declaration (cp_parser *parser,
13376 cp_decl_specifier_seq *decl_specifiers,
13377 tree *maybe_range_for_decl,
13378 location_t *init_loc)
13379 {
13380 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13381 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13382 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13383
13384 /* Parse the identifier-list. */
13385 auto_vec<cp_expr, 10> v;
13386 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13387 while (true)
13388 {
13389 cp_expr e = cp_parser_identifier (parser);
13390 if (e.get_value () == error_mark_node)
13391 break;
13392 v.safe_push (e);
13393 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13394 break;
13395 cp_lexer_consume_token (parser->lexer);
13396 }
13397
13398 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13399 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13400 {
13401 end_loc = UNKNOWN_LOCATION;
13402 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13403 false);
13404 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13405 cp_lexer_consume_token (parser->lexer);
13406 else
13407 {
13408 cp_parser_skip_to_end_of_statement (parser);
13409 return error_mark_node;
13410 }
13411 }
13412
13413 if (cxx_dialect < cxx17)
13414 pedwarn (loc, 0, "structured bindings only available with "
13415 "-std=c++17 or -std=gnu++17");
13416
13417 tree pushed_scope;
13418 cp_declarator *declarator = make_declarator (cdk_decomp);
13419 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13420 declarator->id_loc = loc;
13421 if (ref_qual != REF_QUAL_NONE)
13422 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13423 ref_qual == REF_QUAL_RVALUE,
13424 NULL_TREE);
13425 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13426 NULL_TREE, decl_specifiers->attributes,
13427 &pushed_scope);
13428 tree orig_decl = decl;
13429
13430 unsigned int i;
13431 cp_expr e;
13432 cp_decl_specifier_seq decl_specs;
13433 clear_decl_specs (&decl_specs);
13434 decl_specs.type = make_auto ();
13435 tree prev = decl;
13436 FOR_EACH_VEC_ELT (v, i, e)
13437 {
13438 if (i == 0)
13439 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13440 else
13441 declarator->u.id.unqualified_name = e.get_value ();
13442 declarator->id_loc = e.get_location ();
13443 tree elt_pushed_scope;
13444 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13445 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13446 if (decl2 == error_mark_node)
13447 decl = error_mark_node;
13448 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13449 {
13450 /* Ensure we've diagnosed redeclaration if we aren't creating
13451 a new VAR_DECL. */
13452 gcc_assert (errorcount);
13453 decl = error_mark_node;
13454 }
13455 else
13456 prev = decl2;
13457 if (elt_pushed_scope)
13458 pop_scope (elt_pushed_scope);
13459 }
13460
13461 if (v.is_empty ())
13462 {
13463 error_at (loc, "empty structured binding declaration");
13464 decl = error_mark_node;
13465 }
13466
13467 if (maybe_range_for_decl == NULL
13468 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13469 {
13470 bool non_constant_p = false, is_direct_init = false;
13471 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13472 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13473 &non_constant_p);
13474 if (initializer == NULL_TREE
13475 || (TREE_CODE (initializer) == TREE_LIST
13476 && TREE_CHAIN (initializer))
13477 || (is_direct_init
13478 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13479 && CONSTRUCTOR_NELTS (initializer) != 1))
13480 {
13481 error_at (loc, "invalid initializer for structured binding "
13482 "declaration");
13483 initializer = error_mark_node;
13484 }
13485
13486 if (decl != error_mark_node)
13487 {
13488 cp_maybe_mangle_decomp (decl, prev, v.length ());
13489 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13490 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13491 cp_finish_decomp (decl, prev, v.length ());
13492 }
13493 }
13494 else if (decl != error_mark_node)
13495 {
13496 *maybe_range_for_decl = prev;
13497 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13498 the underlying DECL. */
13499 cp_finish_decomp (decl, prev, v.length ());
13500 }
13501
13502 if (pushed_scope)
13503 pop_scope (pushed_scope);
13504
13505 if (decl == error_mark_node && DECL_P (orig_decl))
13506 {
13507 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13508 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13509 }
13510
13511 return decl;
13512 }
13513
13514 /* Parse a decl-specifier-seq.
13515
13516 decl-specifier-seq:
13517 decl-specifier-seq [opt] decl-specifier
13518 decl-specifier attribute-specifier-seq [opt] (C++11)
13519
13520 decl-specifier:
13521 storage-class-specifier
13522 type-specifier
13523 function-specifier
13524 friend
13525 typedef
13526
13527 GNU Extension:
13528
13529 decl-specifier:
13530 attributes
13531
13532 Concepts Extension:
13533
13534 decl-specifier:
13535 concept
13536
13537 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13538
13539 The parser flags FLAGS is used to control type-specifier parsing.
13540
13541 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13542 flags:
13543
13544 1: one of the decl-specifiers is an elaborated-type-specifier
13545 (i.e., a type declaration)
13546 2: one of the decl-specifiers is an enum-specifier or a
13547 class-specifier (i.e., a type definition)
13548
13549 */
13550
13551 static void
13552 cp_parser_decl_specifier_seq (cp_parser* parser,
13553 cp_parser_flags flags,
13554 cp_decl_specifier_seq *decl_specs,
13555 int* declares_class_or_enum)
13556 {
13557 bool constructor_possible_p = !parser->in_declarator_p;
13558 bool found_decl_spec = false;
13559 cp_token *start_token = NULL;
13560 cp_decl_spec ds;
13561
13562 /* Clear DECL_SPECS. */
13563 clear_decl_specs (decl_specs);
13564
13565 /* Assume no class or enumeration type is declared. */
13566 *declares_class_or_enum = 0;
13567
13568 /* Keep reading specifiers until there are no more to read. */
13569 while (true)
13570 {
13571 bool constructor_p;
13572 cp_token *token;
13573 ds = ds_last;
13574
13575 /* Peek at the next token. */
13576 token = cp_lexer_peek_token (parser->lexer);
13577
13578 /* Save the first token of the decl spec list for error
13579 reporting. */
13580 if (!start_token)
13581 start_token = token;
13582 /* Handle attributes. */
13583 if (cp_next_tokens_can_be_attribute_p (parser))
13584 {
13585 /* Parse the attributes. */
13586 tree attrs = cp_parser_attributes_opt (parser);
13587
13588 /* In a sequence of declaration specifiers, c++11 attributes
13589 appertain to the type that precede them. In that case
13590 [dcl.spec]/1 says:
13591
13592 The attribute-specifier-seq affects the type only for
13593 the declaration it appears in, not other declarations
13594 involving the same type.
13595
13596 But for now let's force the user to position the
13597 attribute either at the beginning of the declaration or
13598 after the declarator-id, which would clearly mean that it
13599 applies to the declarator. */
13600 if (cxx11_attribute_p (attrs))
13601 {
13602 if (!found_decl_spec)
13603 /* The c++11 attribute is at the beginning of the
13604 declaration. It appertains to the entity being
13605 declared. */;
13606 else
13607 {
13608 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13609 {
13610 /* This is an attribute following a
13611 class-specifier. */
13612 if (decl_specs->type_definition_p)
13613 warn_misplaced_attr_for_class_type (token->location,
13614 decl_specs->type);
13615 attrs = NULL_TREE;
13616 }
13617 else
13618 {
13619 decl_specs->std_attributes
13620 = attr_chainon (decl_specs->std_attributes, attrs);
13621 if (decl_specs->locations[ds_std_attribute] == 0)
13622 decl_specs->locations[ds_std_attribute] = token->location;
13623 }
13624 continue;
13625 }
13626 }
13627
13628 decl_specs->attributes
13629 = attr_chainon (decl_specs->attributes, attrs);
13630 if (decl_specs->locations[ds_attribute] == 0)
13631 decl_specs->locations[ds_attribute] = token->location;
13632 continue;
13633 }
13634 /* Assume we will find a decl-specifier keyword. */
13635 found_decl_spec = true;
13636 /* If the next token is an appropriate keyword, we can simply
13637 add it to the list. */
13638 switch (token->keyword)
13639 {
13640 /* decl-specifier:
13641 friend
13642 constexpr */
13643 case RID_FRIEND:
13644 if (!at_class_scope_p ())
13645 {
13646 gcc_rich_location richloc (token->location);
13647 richloc.add_fixit_remove ();
13648 error_at (&richloc, "%<friend%> used outside of class");
13649 cp_lexer_purge_token (parser->lexer);
13650 }
13651 else
13652 {
13653 ds = ds_friend;
13654 /* Consume the token. */
13655 cp_lexer_consume_token (parser->lexer);
13656 }
13657 break;
13658
13659 case RID_CONSTEXPR:
13660 ds = ds_constexpr;
13661 cp_lexer_consume_token (parser->lexer);
13662 break;
13663
13664 case RID_CONCEPT:
13665 ds = ds_concept;
13666 cp_lexer_consume_token (parser->lexer);
13667 break;
13668
13669 /* function-specifier:
13670 inline
13671 virtual
13672 explicit */
13673 case RID_INLINE:
13674 case RID_VIRTUAL:
13675 case RID_EXPLICIT:
13676 cp_parser_function_specifier_opt (parser, decl_specs);
13677 break;
13678
13679 /* decl-specifier:
13680 typedef */
13681 case RID_TYPEDEF:
13682 ds = ds_typedef;
13683 /* Consume the token. */
13684 cp_lexer_consume_token (parser->lexer);
13685 /* A constructor declarator cannot appear in a typedef. */
13686 constructor_possible_p = false;
13687 /* The "typedef" keyword can only occur in a declaration; we
13688 may as well commit at this point. */
13689 cp_parser_commit_to_tentative_parse (parser);
13690
13691 if (decl_specs->storage_class != sc_none)
13692 decl_specs->conflicting_specifiers_p = true;
13693 break;
13694
13695 /* storage-class-specifier:
13696 auto
13697 register
13698 static
13699 extern
13700 mutable
13701
13702 GNU Extension:
13703 thread */
13704 case RID_AUTO:
13705 if (cxx_dialect == cxx98)
13706 {
13707 /* Consume the token. */
13708 cp_lexer_consume_token (parser->lexer);
13709
13710 /* Complain about `auto' as a storage specifier, if
13711 we're complaining about C++0x compatibility. */
13712 gcc_rich_location richloc (token->location);
13713 richloc.add_fixit_remove ();
13714 warning_at (&richloc, OPT_Wc__11_compat,
13715 "%<auto%> changes meaning in C++11; "
13716 "please remove it");
13717
13718 /* Set the storage class anyway. */
13719 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13720 token);
13721 }
13722 else
13723 /* C++0x auto type-specifier. */
13724 found_decl_spec = false;
13725 break;
13726
13727 case RID_REGISTER:
13728 case RID_STATIC:
13729 case RID_EXTERN:
13730 case RID_MUTABLE:
13731 /* Consume the token. */
13732 cp_lexer_consume_token (parser->lexer);
13733 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13734 token);
13735 break;
13736 case RID_THREAD:
13737 /* Consume the token. */
13738 ds = ds_thread;
13739 cp_lexer_consume_token (parser->lexer);
13740 break;
13741
13742 default:
13743 /* We did not yet find a decl-specifier yet. */
13744 found_decl_spec = false;
13745 break;
13746 }
13747
13748 if (found_decl_spec
13749 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13750 && token->keyword != RID_CONSTEXPR)
13751 error ("decl-specifier invalid in condition");
13752
13753 if (found_decl_spec
13754 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13755 && token->keyword != RID_MUTABLE
13756 && token->keyword != RID_CONSTEXPR)
13757 error_at (token->location, "%qD invalid in lambda",
13758 ridpointers[token->keyword]);
13759
13760 if (ds != ds_last)
13761 set_and_check_decl_spec_loc (decl_specs, ds, token);
13762
13763 /* Constructors are a special case. The `S' in `S()' is not a
13764 decl-specifier; it is the beginning of the declarator. */
13765 constructor_p
13766 = (!found_decl_spec
13767 && constructor_possible_p
13768 && (cp_parser_constructor_declarator_p
13769 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13770
13771 /* If we don't have a DECL_SPEC yet, then we must be looking at
13772 a type-specifier. */
13773 if (!found_decl_spec && !constructor_p)
13774 {
13775 int decl_spec_declares_class_or_enum;
13776 bool is_cv_qualifier;
13777 tree type_spec;
13778
13779 type_spec
13780 = cp_parser_type_specifier (parser, flags,
13781 decl_specs,
13782 /*is_declaration=*/true,
13783 &decl_spec_declares_class_or_enum,
13784 &is_cv_qualifier);
13785 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13786
13787 /* If this type-specifier referenced a user-defined type
13788 (a typedef, class-name, etc.), then we can't allow any
13789 more such type-specifiers henceforth.
13790
13791 [dcl.spec]
13792
13793 The longest sequence of decl-specifiers that could
13794 possibly be a type name is taken as the
13795 decl-specifier-seq of a declaration. The sequence shall
13796 be self-consistent as described below.
13797
13798 [dcl.type]
13799
13800 As a general rule, at most one type-specifier is allowed
13801 in the complete decl-specifier-seq of a declaration. The
13802 only exceptions are the following:
13803
13804 -- const or volatile can be combined with any other
13805 type-specifier.
13806
13807 -- signed or unsigned can be combined with char, long,
13808 short, or int.
13809
13810 -- ..
13811
13812 Example:
13813
13814 typedef char* Pc;
13815 void g (const int Pc);
13816
13817 Here, Pc is *not* part of the decl-specifier seq; it's
13818 the declarator. Therefore, once we see a type-specifier
13819 (other than a cv-qualifier), we forbid any additional
13820 user-defined types. We *do* still allow things like `int
13821 int' to be considered a decl-specifier-seq, and issue the
13822 error message later. */
13823 if (type_spec && !is_cv_qualifier)
13824 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13825 /* A constructor declarator cannot follow a type-specifier. */
13826 if (type_spec)
13827 {
13828 constructor_possible_p = false;
13829 found_decl_spec = true;
13830 if (!is_cv_qualifier)
13831 decl_specs->any_type_specifiers_p = true;
13832
13833 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13834 error_at (token->location, "type-specifier invalid in lambda");
13835 }
13836 }
13837
13838 /* If we still do not have a DECL_SPEC, then there are no more
13839 decl-specifiers. */
13840 if (!found_decl_spec)
13841 break;
13842
13843 decl_specs->any_specifiers_p = true;
13844 /* After we see one decl-specifier, further decl-specifiers are
13845 always optional. */
13846 flags |= CP_PARSER_FLAGS_OPTIONAL;
13847 }
13848
13849 /* Don't allow a friend specifier with a class definition. */
13850 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13851 && (*declares_class_or_enum & 2))
13852 error_at (decl_specs->locations[ds_friend],
13853 "class definition may not be declared a friend");
13854 }
13855
13856 /* Parse an (optional) storage-class-specifier.
13857
13858 storage-class-specifier:
13859 auto
13860 register
13861 static
13862 extern
13863 mutable
13864
13865 GNU Extension:
13866
13867 storage-class-specifier:
13868 thread
13869
13870 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13871
13872 static tree
13873 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13874 {
13875 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13876 {
13877 case RID_AUTO:
13878 if (cxx_dialect != cxx98)
13879 return NULL_TREE;
13880 /* Fall through for C++98. */
13881 gcc_fallthrough ();
13882
13883 case RID_REGISTER:
13884 case RID_STATIC:
13885 case RID_EXTERN:
13886 case RID_MUTABLE:
13887 case RID_THREAD:
13888 /* Consume the token. */
13889 return cp_lexer_consume_token (parser->lexer)->u.value;
13890
13891 default:
13892 return NULL_TREE;
13893 }
13894 }
13895
13896 /* Parse an (optional) function-specifier.
13897
13898 function-specifier:
13899 inline
13900 virtual
13901 explicit
13902
13903 C++2A Extension:
13904 explicit(constant-expression)
13905
13906 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13907 Updates DECL_SPECS, if it is non-NULL. */
13908
13909 static tree
13910 cp_parser_function_specifier_opt (cp_parser* parser,
13911 cp_decl_specifier_seq *decl_specs)
13912 {
13913 cp_token *token = cp_lexer_peek_token (parser->lexer);
13914 switch (token->keyword)
13915 {
13916 case RID_INLINE:
13917 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13918 break;
13919
13920 case RID_VIRTUAL:
13921 /* 14.5.2.3 [temp.mem]
13922
13923 A member function template shall not be virtual. */
13924 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13925 && current_class_type)
13926 error_at (token->location, "templates may not be %<virtual%>");
13927 else
13928 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13929 break;
13930
13931 case RID_EXPLICIT:
13932 {
13933 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
13934 /* If we see '(', it's C++20 explicit(bool). */
13935 tree expr;
13936 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13937 {
13938 matching_parens parens;
13939 parens.consume_open (parser);
13940
13941 /* New types are not allowed in an explicit-specifier. */
13942 const char *saved_message
13943 = parser->type_definition_forbidden_message;
13944 parser->type_definition_forbidden_message
13945 = G_("types may not be defined in explicit-specifier");
13946
13947 if (cxx_dialect < cxx2a)
13948 pedwarn (token->location, 0,
13949 "%<explicit(bool)%> only available with -std=c++2a "
13950 "or -std=gnu++2a");
13951
13952 /* Parse the constant-expression. */
13953 expr = cp_parser_constant_expression (parser);
13954
13955 /* Restore the saved message. */
13956 parser->type_definition_forbidden_message = saved_message;
13957 parens.require_close (parser);
13958 }
13959 else
13960 /* The explicit-specifier explicit without a constant-expression is
13961 equivalent to the explicit-specifier explicit(true). */
13962 expr = boolean_true_node;
13963
13964 /* [dcl.fct.spec]
13965 "the constant-expression, if supplied, shall be a contextually
13966 converted constant expression of type bool." */
13967 expr = build_explicit_specifier (expr, tf_warning_or_error);
13968 /* We could evaluate it -- mark the decl as appropriate. */
13969 if (expr == boolean_true_node)
13970 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13971 else if (expr == boolean_false_node)
13972 /* Don't mark the decl as explicit. */;
13973 else if (decl_specs)
13974 /* The expression was value-dependent. Remember it so that we can
13975 substitute it later. */
13976 decl_specs->explicit_specifier = expr;
13977 return id;
13978 }
13979
13980 default:
13981 return NULL_TREE;
13982 }
13983
13984 /* Consume the token. */
13985 return cp_lexer_consume_token (parser->lexer)->u.value;
13986 }
13987
13988 /* Parse a linkage-specification.
13989
13990 linkage-specification:
13991 extern string-literal { declaration-seq [opt] }
13992 extern string-literal declaration */
13993
13994 static void
13995 cp_parser_linkage_specification (cp_parser* parser)
13996 {
13997 tree linkage;
13998
13999 /* Look for the `extern' keyword. */
14000 cp_token *extern_token
14001 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14002
14003 /* Look for the string-literal. */
14004 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14005 linkage = cp_parser_string_literal (parser, false, false);
14006
14007 /* Transform the literal into an identifier. If the literal is a
14008 wide-character string, or contains embedded NULs, then we can't
14009 handle it as the user wants. */
14010 if (strlen (TREE_STRING_POINTER (linkage))
14011 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14012 {
14013 cp_parser_error (parser, "invalid linkage-specification");
14014 /* Assume C++ linkage. */
14015 linkage = lang_name_cplusplus;
14016 }
14017 else
14018 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14019
14020 /* We're now using the new linkage. */
14021 push_lang_context (linkage);
14022
14023 /* Preserve the location of the the innermost linkage specification,
14024 tracking the locations of nested specifications via a local. */
14025 location_t saved_location
14026 = parser->innermost_linkage_specification_location;
14027 /* Construct a location ranging from the start of the "extern" to
14028 the end of the string-literal, with the caret at the start, e.g.:
14029 extern "C" {
14030 ^~~~~~~~~~
14031 */
14032 parser->innermost_linkage_specification_location
14033 = make_location (extern_token->location,
14034 extern_token->location,
14035 get_finish (string_token->location));
14036
14037 /* If the next token is a `{', then we're using the first
14038 production. */
14039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14040 {
14041 cp_ensure_no_omp_declare_simd (parser);
14042 cp_ensure_no_oacc_routine (parser);
14043
14044 /* Consume the `{' token. */
14045 matching_braces braces;
14046 braces.consume_open (parser)->location;
14047 /* Parse the declarations. */
14048 cp_parser_declaration_seq_opt (parser);
14049 /* Look for the closing `}'. */
14050 braces.require_close (parser);
14051 }
14052 /* Otherwise, there's just one declaration. */
14053 else
14054 {
14055 bool saved_in_unbraced_linkage_specification_p;
14056
14057 saved_in_unbraced_linkage_specification_p
14058 = parser->in_unbraced_linkage_specification_p;
14059 parser->in_unbraced_linkage_specification_p = true;
14060 cp_parser_declaration (parser);
14061 parser->in_unbraced_linkage_specification_p
14062 = saved_in_unbraced_linkage_specification_p;
14063 }
14064
14065 /* We're done with the linkage-specification. */
14066 pop_lang_context ();
14067
14068 /* Restore location of parent linkage specification, if any. */
14069 parser->innermost_linkage_specification_location = saved_location;
14070 }
14071
14072 /* Parse a static_assert-declaration.
14073
14074 static_assert-declaration:
14075 static_assert ( constant-expression , string-literal ) ;
14076 static_assert ( constant-expression ) ; (C++17)
14077
14078 If MEMBER_P, this static_assert is a class member. */
14079
14080 static void
14081 cp_parser_static_assert(cp_parser *parser, bool member_p)
14082 {
14083 cp_expr condition;
14084 location_t token_loc;
14085 tree message;
14086 bool dummy;
14087
14088 /* Peek at the `static_assert' token so we can keep track of exactly
14089 where the static assertion started. */
14090 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14091
14092 /* Look for the `static_assert' keyword. */
14093 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14094 RT_STATIC_ASSERT))
14095 return;
14096
14097 /* We know we are in a static assertion; commit to any tentative
14098 parse. */
14099 if (cp_parser_parsing_tentatively (parser))
14100 cp_parser_commit_to_tentative_parse (parser);
14101
14102 /* Parse the `(' starting the static assertion condition. */
14103 matching_parens parens;
14104 parens.require_open (parser);
14105
14106 /* Parse the constant-expression. Allow a non-constant expression
14107 here in order to give better diagnostics in finish_static_assert. */
14108 condition =
14109 cp_parser_constant_expression (parser,
14110 /*allow_non_constant_p=*/true,
14111 /*non_constant_p=*/&dummy);
14112
14113 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14114 {
14115 if (cxx_dialect < cxx17)
14116 pedwarn (input_location, OPT_Wpedantic,
14117 "static_assert without a message "
14118 "only available with -std=c++17 or -std=gnu++17");
14119 /* Eat the ')' */
14120 cp_lexer_consume_token (parser->lexer);
14121 message = build_string (1, "");
14122 TREE_TYPE (message) = char_array_type_node;
14123 fix_string_type (message);
14124 }
14125 else
14126 {
14127 /* Parse the separating `,'. */
14128 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14129
14130 /* Parse the string-literal message. */
14131 message = cp_parser_string_literal (parser,
14132 /*translate=*/false,
14133 /*wide_ok=*/true);
14134
14135 /* A `)' completes the static assertion. */
14136 if (!parens.require_close (parser))
14137 cp_parser_skip_to_closing_parenthesis (parser,
14138 /*recovering=*/true,
14139 /*or_comma=*/false,
14140 /*consume_paren=*/true);
14141 }
14142
14143 /* A semicolon terminates the declaration. */
14144 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14145
14146 /* Get the location for the static assertion. Use that of the
14147 condition if available, otherwise, use that of the "static_assert"
14148 token. */
14149 location_t assert_loc = condition.get_location ();
14150 if (assert_loc == UNKNOWN_LOCATION)
14151 assert_loc = token_loc;
14152
14153 /* Complete the static assertion, which may mean either processing
14154 the static assert now or saving it for template instantiation. */
14155 finish_static_assert (condition, message, assert_loc, member_p);
14156 }
14157
14158 /* Parse the expression in decltype ( expression ). */
14159
14160 static tree
14161 cp_parser_decltype_expr (cp_parser *parser,
14162 bool &id_expression_or_member_access_p)
14163 {
14164 cp_token *id_expr_start_token;
14165 tree expr;
14166
14167 /* Since we're going to preserve any side-effects from this parse, set up a
14168 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14169 in the expression. */
14170 tentative_firewall firewall (parser);
14171
14172 /* First, try parsing an id-expression. */
14173 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14174 cp_parser_parse_tentatively (parser);
14175 expr = cp_parser_id_expression (parser,
14176 /*template_keyword_p=*/false,
14177 /*check_dependency_p=*/true,
14178 /*template_p=*/NULL,
14179 /*declarator_p=*/false,
14180 /*optional_p=*/false);
14181
14182 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14183 {
14184 bool non_integral_constant_expression_p = false;
14185 tree id_expression = expr;
14186 cp_id_kind idk;
14187 const char *error_msg;
14188
14189 if (identifier_p (expr))
14190 /* Lookup the name we got back from the id-expression. */
14191 expr = cp_parser_lookup_name_simple (parser, expr,
14192 id_expr_start_token->location);
14193
14194 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14195 /* A template without args is not a complete id-expression. */
14196 expr = error_mark_node;
14197
14198 if (expr
14199 && expr != error_mark_node
14200 && TREE_CODE (expr) != TYPE_DECL
14201 && (TREE_CODE (expr) != BIT_NOT_EXPR
14202 || !TYPE_P (TREE_OPERAND (expr, 0)))
14203 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14204 {
14205 /* Complete lookup of the id-expression. */
14206 expr = (finish_id_expression
14207 (id_expression, expr, parser->scope, &idk,
14208 /*integral_constant_expression_p=*/false,
14209 /*allow_non_integral_constant_expression_p=*/true,
14210 &non_integral_constant_expression_p,
14211 /*template_p=*/false,
14212 /*done=*/true,
14213 /*address_p=*/false,
14214 /*template_arg_p=*/false,
14215 &error_msg,
14216 id_expr_start_token->location));
14217
14218 if (expr == error_mark_node)
14219 /* We found an id-expression, but it was something that we
14220 should not have found. This is an error, not something
14221 we can recover from, so note that we found an
14222 id-expression and we'll recover as gracefully as
14223 possible. */
14224 id_expression_or_member_access_p = true;
14225 }
14226
14227 if (expr
14228 && expr != error_mark_node
14229 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14230 /* We have an id-expression. */
14231 id_expression_or_member_access_p = true;
14232 }
14233
14234 if (!id_expression_or_member_access_p)
14235 {
14236 /* Abort the id-expression parse. */
14237 cp_parser_abort_tentative_parse (parser);
14238
14239 /* Parsing tentatively, again. */
14240 cp_parser_parse_tentatively (parser);
14241
14242 /* Parse a class member access. */
14243 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14244 /*cast_p=*/false, /*decltype*/true,
14245 /*member_access_only_p=*/true, NULL);
14246
14247 if (expr
14248 && expr != error_mark_node
14249 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14250 /* We have an id-expression. */
14251 id_expression_or_member_access_p = true;
14252 }
14253
14254 if (id_expression_or_member_access_p)
14255 /* We have parsed the complete id-expression or member access. */
14256 cp_parser_parse_definitely (parser);
14257 else
14258 {
14259 /* Abort our attempt to parse an id-expression or member access
14260 expression. */
14261 cp_parser_abort_tentative_parse (parser);
14262
14263 /* Commit to the tentative_firewall so we get syntax errors. */
14264 cp_parser_commit_to_tentative_parse (parser);
14265
14266 /* Parse a full expression. */
14267 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14268 /*decltype_p=*/true);
14269 }
14270
14271 return expr;
14272 }
14273
14274 /* Parse a `decltype' type. Returns the type.
14275
14276 simple-type-specifier:
14277 decltype ( expression )
14278 C++14 proposal:
14279 decltype ( auto ) */
14280
14281 static tree
14282 cp_parser_decltype (cp_parser *parser)
14283 {
14284 bool id_expression_or_member_access_p = false;
14285 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14286
14287 if (start_token->type == CPP_DECLTYPE)
14288 {
14289 /* Already parsed. */
14290 cp_lexer_consume_token (parser->lexer);
14291 return saved_checks_value (start_token->u.tree_check_value);
14292 }
14293
14294 /* Look for the `decltype' token. */
14295 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14296 return error_mark_node;
14297
14298 /* Parse the opening `('. */
14299 matching_parens parens;
14300 if (!parens.require_open (parser))
14301 return error_mark_node;
14302
14303 push_deferring_access_checks (dk_deferred);
14304
14305 tree expr = NULL_TREE;
14306
14307 if (cxx_dialect >= cxx14
14308 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14309 /* decltype (auto) */
14310 cp_lexer_consume_token (parser->lexer);
14311 else
14312 {
14313 /* decltype (expression) */
14314
14315 /* Types cannot be defined in a `decltype' expression. Save away the
14316 old message and set the new one. */
14317 const char *saved_message = parser->type_definition_forbidden_message;
14318 parser->type_definition_forbidden_message
14319 = G_("types may not be defined in %<decltype%> expressions");
14320
14321 /* The restrictions on constant-expressions do not apply inside
14322 decltype expressions. */
14323 bool saved_integral_constant_expression_p
14324 = parser->integral_constant_expression_p;
14325 bool saved_non_integral_constant_expression_p
14326 = parser->non_integral_constant_expression_p;
14327 parser->integral_constant_expression_p = false;
14328
14329 /* Within a parenthesized expression, a `>' token is always
14330 the greater-than operator. */
14331 bool saved_greater_than_is_operator_p
14332 = parser->greater_than_is_operator_p;
14333 parser->greater_than_is_operator_p = true;
14334
14335 /* Do not actually evaluate the expression. */
14336 ++cp_unevaluated_operand;
14337
14338 /* Do not warn about problems with the expression. */
14339 ++c_inhibit_evaluation_warnings;
14340
14341 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14342
14343 /* Go back to evaluating expressions. */
14344 --cp_unevaluated_operand;
14345 --c_inhibit_evaluation_warnings;
14346
14347 /* The `>' token might be the end of a template-id or
14348 template-parameter-list now. */
14349 parser->greater_than_is_operator_p
14350 = saved_greater_than_is_operator_p;
14351
14352 /* Restore the old message and the integral constant expression
14353 flags. */
14354 parser->type_definition_forbidden_message = saved_message;
14355 parser->integral_constant_expression_p
14356 = saved_integral_constant_expression_p;
14357 parser->non_integral_constant_expression_p
14358 = saved_non_integral_constant_expression_p;
14359 }
14360
14361 /* Parse to the closing `)'. */
14362 if (!parens.require_close (parser))
14363 {
14364 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14365 /*consume_paren=*/true);
14366 pop_deferring_access_checks ();
14367 return error_mark_node;
14368 }
14369
14370 if (!expr)
14371 {
14372 /* Build auto. */
14373 expr = make_decltype_auto ();
14374 AUTO_IS_DECLTYPE (expr) = true;
14375 }
14376 else
14377 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14378 tf_warning_or_error);
14379
14380 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14381 it again. */
14382 start_token->type = CPP_DECLTYPE;
14383 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14384 start_token->u.tree_check_value->value = expr;
14385 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14386 start_token->keyword = RID_MAX;
14387 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14388
14389 pop_to_parent_deferring_access_checks ();
14390
14391 return expr;
14392 }
14393
14394 /* Special member functions [gram.special] */
14395
14396 /* Parse a conversion-function-id.
14397
14398 conversion-function-id:
14399 operator conversion-type-id
14400
14401 Returns an IDENTIFIER_NODE representing the operator. */
14402
14403 static tree
14404 cp_parser_conversion_function_id (cp_parser* parser)
14405 {
14406 tree type;
14407 tree saved_scope;
14408 tree saved_qualifying_scope;
14409 tree saved_object_scope;
14410 tree pushed_scope = NULL_TREE;
14411
14412 /* Look for the `operator' token. */
14413 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14414 return error_mark_node;
14415 /* When we parse the conversion-type-id, the current scope will be
14416 reset. However, we need that information in able to look up the
14417 conversion function later, so we save it here. */
14418 saved_scope = parser->scope;
14419 saved_qualifying_scope = parser->qualifying_scope;
14420 saved_object_scope = parser->object_scope;
14421 /* We must enter the scope of the class so that the names of
14422 entities declared within the class are available in the
14423 conversion-type-id. For example, consider:
14424
14425 struct S {
14426 typedef int I;
14427 operator I();
14428 };
14429
14430 S::operator I() { ... }
14431
14432 In order to see that `I' is a type-name in the definition, we
14433 must be in the scope of `S'. */
14434 if (saved_scope)
14435 pushed_scope = push_scope (saved_scope);
14436 /* Parse the conversion-type-id. */
14437 type = cp_parser_conversion_type_id (parser);
14438 /* Leave the scope of the class, if any. */
14439 if (pushed_scope)
14440 pop_scope (pushed_scope);
14441 /* Restore the saved scope. */
14442 parser->scope = saved_scope;
14443 parser->qualifying_scope = saved_qualifying_scope;
14444 parser->object_scope = saved_object_scope;
14445 /* If the TYPE is invalid, indicate failure. */
14446 if (type == error_mark_node)
14447 return error_mark_node;
14448 return make_conv_op_name (type);
14449 }
14450
14451 /* Parse a conversion-type-id:
14452
14453 conversion-type-id:
14454 type-specifier-seq conversion-declarator [opt]
14455
14456 Returns the TYPE specified. */
14457
14458 static tree
14459 cp_parser_conversion_type_id (cp_parser* parser)
14460 {
14461 tree attributes;
14462 cp_decl_specifier_seq type_specifiers;
14463 cp_declarator *declarator;
14464 tree type_specified;
14465 const char *saved_message;
14466
14467 /* Parse the attributes. */
14468 attributes = cp_parser_attributes_opt (parser);
14469
14470 saved_message = parser->type_definition_forbidden_message;
14471 parser->type_definition_forbidden_message
14472 = G_("types may not be defined in a conversion-type-id");
14473
14474 /* Parse the type-specifiers. */
14475 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14476 /*is_trailing_return=*/false,
14477 &type_specifiers);
14478
14479 parser->type_definition_forbidden_message = saved_message;
14480
14481 /* If that didn't work, stop. */
14482 if (type_specifiers.type == error_mark_node)
14483 return error_mark_node;
14484 /* Parse the conversion-declarator. */
14485 declarator = cp_parser_conversion_declarator_opt (parser);
14486
14487 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14488 /*initialized=*/0, &attributes);
14489 if (attributes)
14490 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14491
14492 /* Don't give this error when parsing tentatively. This happens to
14493 work because we always parse this definitively once. */
14494 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14495 && type_uses_auto (type_specified))
14496 {
14497 if (cxx_dialect < cxx14)
14498 {
14499 error ("invalid use of %<auto%> in conversion operator");
14500 return error_mark_node;
14501 }
14502 else if (template_parm_scope_p ())
14503 warning (0, "use of %<auto%> in member template "
14504 "conversion operator can never be deduced");
14505 }
14506
14507 return type_specified;
14508 }
14509
14510 /* Parse an (optional) conversion-declarator.
14511
14512 conversion-declarator:
14513 ptr-operator conversion-declarator [opt]
14514
14515 */
14516
14517 static cp_declarator *
14518 cp_parser_conversion_declarator_opt (cp_parser* parser)
14519 {
14520 enum tree_code code;
14521 tree class_type, std_attributes = NULL_TREE;
14522 cp_cv_quals cv_quals;
14523
14524 /* We don't know if there's a ptr-operator next, or not. */
14525 cp_parser_parse_tentatively (parser);
14526 /* Try the ptr-operator. */
14527 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14528 &std_attributes);
14529 /* If it worked, look for more conversion-declarators. */
14530 if (cp_parser_parse_definitely (parser))
14531 {
14532 cp_declarator *declarator;
14533
14534 /* Parse another optional declarator. */
14535 declarator = cp_parser_conversion_declarator_opt (parser);
14536
14537 declarator = cp_parser_make_indirect_declarator
14538 (code, class_type, cv_quals, declarator, std_attributes);
14539
14540 return declarator;
14541 }
14542
14543 return NULL;
14544 }
14545
14546 /* Parse an (optional) ctor-initializer.
14547
14548 ctor-initializer:
14549 : mem-initializer-list */
14550
14551 static void
14552 cp_parser_ctor_initializer_opt (cp_parser* parser)
14553 {
14554 /* If the next token is not a `:', then there is no
14555 ctor-initializer. */
14556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14557 {
14558 /* Do default initialization of any bases and members. */
14559 if (DECL_CONSTRUCTOR_P (current_function_decl))
14560 finish_mem_initializers (NULL_TREE);
14561 return;
14562 }
14563
14564 /* Consume the `:' token. */
14565 cp_lexer_consume_token (parser->lexer);
14566 /* And the mem-initializer-list. */
14567 cp_parser_mem_initializer_list (parser);
14568 }
14569
14570 /* Parse a mem-initializer-list.
14571
14572 mem-initializer-list:
14573 mem-initializer ... [opt]
14574 mem-initializer ... [opt] , mem-initializer-list */
14575
14576 static void
14577 cp_parser_mem_initializer_list (cp_parser* parser)
14578 {
14579 tree mem_initializer_list = NULL_TREE;
14580 tree target_ctor = error_mark_node;
14581 cp_token *token = cp_lexer_peek_token (parser->lexer);
14582
14583 /* Let the semantic analysis code know that we are starting the
14584 mem-initializer-list. */
14585 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14586 error_at (token->location,
14587 "only constructors take member initializers");
14588
14589 /* Loop through the list. */
14590 while (true)
14591 {
14592 tree mem_initializer;
14593
14594 token = cp_lexer_peek_token (parser->lexer);
14595 /* Parse the mem-initializer. */
14596 mem_initializer = cp_parser_mem_initializer (parser);
14597 /* If the next token is a `...', we're expanding member initializers. */
14598 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14599 if (ellipsis
14600 || (mem_initializer != error_mark_node
14601 && check_for_bare_parameter_packs (TREE_PURPOSE
14602 (mem_initializer))))
14603 {
14604 /* Consume the `...'. */
14605 if (ellipsis)
14606 cp_lexer_consume_token (parser->lexer);
14607
14608 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14609 can be expanded but members cannot. */
14610 if (mem_initializer != error_mark_node
14611 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14612 {
14613 error_at (token->location,
14614 "cannot expand initializer for member %qD",
14615 TREE_PURPOSE (mem_initializer));
14616 mem_initializer = error_mark_node;
14617 }
14618
14619 /* Construct the pack expansion type. */
14620 if (mem_initializer != error_mark_node)
14621 mem_initializer = make_pack_expansion (mem_initializer);
14622 }
14623 if (target_ctor != error_mark_node
14624 && mem_initializer != error_mark_node)
14625 {
14626 error ("mem-initializer for %qD follows constructor delegation",
14627 TREE_PURPOSE (mem_initializer));
14628 mem_initializer = error_mark_node;
14629 }
14630 /* Look for a target constructor. */
14631 if (mem_initializer != error_mark_node
14632 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14633 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14634 {
14635 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14636 if (mem_initializer_list)
14637 {
14638 error ("constructor delegation follows mem-initializer for %qD",
14639 TREE_PURPOSE (mem_initializer_list));
14640 mem_initializer = error_mark_node;
14641 }
14642 target_ctor = mem_initializer;
14643 }
14644 /* Add it to the list, unless it was erroneous. */
14645 if (mem_initializer != error_mark_node)
14646 {
14647 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14648 mem_initializer_list = mem_initializer;
14649 }
14650 /* If the next token is not a `,', we're done. */
14651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14652 break;
14653 /* Consume the `,' token. */
14654 cp_lexer_consume_token (parser->lexer);
14655 }
14656
14657 /* Perform semantic analysis. */
14658 if (DECL_CONSTRUCTOR_P (current_function_decl))
14659 finish_mem_initializers (mem_initializer_list);
14660 }
14661
14662 /* Parse a mem-initializer.
14663
14664 mem-initializer:
14665 mem-initializer-id ( expression-list [opt] )
14666 mem-initializer-id braced-init-list
14667
14668 GNU extension:
14669
14670 mem-initializer:
14671 ( expression-list [opt] )
14672
14673 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14674 class) or FIELD_DECL (for a non-static data member) to initialize;
14675 the TREE_VALUE is the expression-list. An empty initialization
14676 list is represented by void_list_node. */
14677
14678 static tree
14679 cp_parser_mem_initializer (cp_parser* parser)
14680 {
14681 tree mem_initializer_id;
14682 tree expression_list;
14683 tree member;
14684 cp_token *token = cp_lexer_peek_token (parser->lexer);
14685
14686 /* Find out what is being initialized. */
14687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14688 {
14689 permerror (token->location,
14690 "anachronistic old-style base class initializer");
14691 mem_initializer_id = NULL_TREE;
14692 }
14693 else
14694 {
14695 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14696 if (mem_initializer_id == error_mark_node)
14697 return mem_initializer_id;
14698 }
14699 member = expand_member_init (mem_initializer_id);
14700 if (member && !DECL_P (member))
14701 in_base_initializer = 1;
14702
14703 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14704 {
14705 bool expr_non_constant_p;
14706 cp_lexer_set_source_position (parser->lexer);
14707 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14708 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14709 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14710 expression_list = build_tree_list (NULL_TREE, expression_list);
14711 }
14712 else
14713 {
14714 vec<tree, va_gc> *vec;
14715 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14716 /*cast_p=*/false,
14717 /*allow_expansion_p=*/true,
14718 /*non_constant_p=*/NULL);
14719 if (vec == NULL)
14720 return error_mark_node;
14721 expression_list = build_tree_list_vec (vec);
14722 release_tree_vector (vec);
14723 }
14724
14725 if (expression_list == error_mark_node)
14726 return error_mark_node;
14727 if (!expression_list)
14728 expression_list = void_type_node;
14729
14730 in_base_initializer = 0;
14731
14732 return member ? build_tree_list (member, expression_list) : error_mark_node;
14733 }
14734
14735 /* Parse a mem-initializer-id.
14736
14737 mem-initializer-id:
14738 :: [opt] nested-name-specifier [opt] class-name
14739 decltype-specifier (C++11)
14740 identifier
14741
14742 Returns a TYPE indicating the class to be initialized for the first
14743 production (and the second in C++11). Returns an IDENTIFIER_NODE
14744 indicating the data member to be initialized for the last production. */
14745
14746 static tree
14747 cp_parser_mem_initializer_id (cp_parser* parser)
14748 {
14749 bool global_scope_p;
14750 bool nested_name_specifier_p;
14751 bool template_p = false;
14752 tree id;
14753
14754 cp_token *token = cp_lexer_peek_token (parser->lexer);
14755
14756 /* `typename' is not allowed in this context ([temp.res]). */
14757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14758 {
14759 error_at (token->location,
14760 "keyword %<typename%> not allowed in this context (a qualified "
14761 "member initializer is implicitly a type)");
14762 cp_lexer_consume_token (parser->lexer);
14763 }
14764 /* Look for the optional `::' operator. */
14765 global_scope_p
14766 = (cp_parser_global_scope_opt (parser,
14767 /*current_scope_valid_p=*/false)
14768 != NULL_TREE);
14769 /* Look for the optional nested-name-specifier. The simplest way to
14770 implement:
14771
14772 [temp.res]
14773
14774 The keyword `typename' is not permitted in a base-specifier or
14775 mem-initializer; in these contexts a qualified name that
14776 depends on a template-parameter is implicitly assumed to be a
14777 type name.
14778
14779 is to assume that we have seen the `typename' keyword at this
14780 point. */
14781 nested_name_specifier_p
14782 = (cp_parser_nested_name_specifier_opt (parser,
14783 /*typename_keyword_p=*/true,
14784 /*check_dependency_p=*/true,
14785 /*type_p=*/true,
14786 /*is_declaration=*/true)
14787 != NULL_TREE);
14788 if (nested_name_specifier_p)
14789 template_p = cp_parser_optional_template_keyword (parser);
14790 /* If there is a `::' operator or a nested-name-specifier, then we
14791 are definitely looking for a class-name. */
14792 if (global_scope_p || nested_name_specifier_p)
14793 return cp_parser_class_name (parser,
14794 /*typename_keyword_p=*/true,
14795 /*template_keyword_p=*/template_p,
14796 typename_type,
14797 /*check_dependency_p=*/true,
14798 /*class_head_p=*/false,
14799 /*is_declaration=*/true);
14800 /* Otherwise, we could also be looking for an ordinary identifier. */
14801 cp_parser_parse_tentatively (parser);
14802 if (cp_lexer_next_token_is_decltype (parser->lexer))
14803 /* Try a decltype-specifier. */
14804 id = cp_parser_decltype (parser);
14805 else
14806 /* Otherwise, try a class-name. */
14807 id = cp_parser_class_name (parser,
14808 /*typename_keyword_p=*/true,
14809 /*template_keyword_p=*/false,
14810 none_type,
14811 /*check_dependency_p=*/true,
14812 /*class_head_p=*/false,
14813 /*is_declaration=*/true);
14814 /* If we found one, we're done. */
14815 if (cp_parser_parse_definitely (parser))
14816 return id;
14817 /* Otherwise, look for an ordinary identifier. */
14818 return cp_parser_identifier (parser);
14819 }
14820
14821 /* Overloading [gram.over] */
14822
14823 /* Parse an operator-function-id.
14824
14825 operator-function-id:
14826 operator operator
14827
14828 Returns an IDENTIFIER_NODE for the operator which is a
14829 human-readable spelling of the identifier, e.g., `operator +'. */
14830
14831 static cp_expr
14832 cp_parser_operator_function_id (cp_parser* parser)
14833 {
14834 /* Look for the `operator' keyword. */
14835 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14836 return error_mark_node;
14837 /* And then the name of the operator itself. */
14838 return cp_parser_operator (parser);
14839 }
14840
14841 /* Return an identifier node for a user-defined literal operator.
14842 The suffix identifier is chained to the operator name identifier. */
14843
14844 tree
14845 cp_literal_operator_id (const char* name)
14846 {
14847 tree identifier;
14848 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14849 + strlen (name) + 10);
14850 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14851 identifier = get_identifier (buffer);
14852
14853 return identifier;
14854 }
14855
14856 /* Parse an operator.
14857
14858 operator:
14859 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14860 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14861 || ++ -- , ->* -> () []
14862
14863 GNU Extensions:
14864
14865 operator:
14866 <? >? <?= >?=
14867
14868 Returns an IDENTIFIER_NODE for the operator which is a
14869 human-readable spelling of the identifier, e.g., `operator +'. */
14870
14871 static cp_expr
14872 cp_parser_operator (cp_parser* parser)
14873 {
14874 tree id = NULL_TREE;
14875 cp_token *token;
14876 bool utf8 = false;
14877
14878 /* Peek at the next token. */
14879 token = cp_lexer_peek_token (parser->lexer);
14880
14881 location_t start_loc = token->location;
14882
14883 /* Figure out which operator we have. */
14884 enum tree_code op = ERROR_MARK;
14885 bool assop = false;
14886 bool consumed = false;
14887 switch (token->type)
14888 {
14889 case CPP_KEYWORD:
14890 {
14891 /* The keyword should be either `new' or `delete'. */
14892 if (token->keyword == RID_NEW)
14893 op = NEW_EXPR;
14894 else if (token->keyword == RID_DELETE)
14895 op = DELETE_EXPR;
14896 else
14897 break;
14898
14899 /* Consume the `new' or `delete' token. */
14900 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14901
14902 /* Peek at the next token. */
14903 token = cp_lexer_peek_token (parser->lexer);
14904 /* If it's a `[' token then this is the array variant of the
14905 operator. */
14906 if (token->type == CPP_OPEN_SQUARE)
14907 {
14908 /* Consume the `[' token. */
14909 cp_lexer_consume_token (parser->lexer);
14910 /* Look for the `]' token. */
14911 if (cp_token *close_token
14912 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14913 end_loc = close_token->location;
14914 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14915 }
14916 start_loc = make_location (start_loc, start_loc, end_loc);
14917 consumed = true;
14918 break;
14919 }
14920
14921 case CPP_PLUS:
14922 op = PLUS_EXPR;
14923 break;
14924
14925 case CPP_MINUS:
14926 op = MINUS_EXPR;
14927 break;
14928
14929 case CPP_MULT:
14930 op = MULT_EXPR;
14931 break;
14932
14933 case CPP_DIV:
14934 op = TRUNC_DIV_EXPR;
14935 break;
14936
14937 case CPP_MOD:
14938 op = TRUNC_MOD_EXPR;
14939 break;
14940
14941 case CPP_XOR:
14942 op = BIT_XOR_EXPR;
14943 break;
14944
14945 case CPP_AND:
14946 op = BIT_AND_EXPR;
14947 break;
14948
14949 case CPP_OR:
14950 op = BIT_IOR_EXPR;
14951 break;
14952
14953 case CPP_COMPL:
14954 op = BIT_NOT_EXPR;
14955 break;
14956
14957 case CPP_NOT:
14958 op = TRUTH_NOT_EXPR;
14959 break;
14960
14961 case CPP_EQ:
14962 assop = true;
14963 op = NOP_EXPR;
14964 break;
14965
14966 case CPP_LESS:
14967 op = LT_EXPR;
14968 break;
14969
14970 case CPP_GREATER:
14971 op = GT_EXPR;
14972 break;
14973
14974 case CPP_PLUS_EQ:
14975 assop = true;
14976 op = PLUS_EXPR;
14977 break;
14978
14979 case CPP_MINUS_EQ:
14980 assop = true;
14981 op = MINUS_EXPR;
14982 break;
14983
14984 case CPP_MULT_EQ:
14985 assop = true;
14986 op = MULT_EXPR;
14987 break;
14988
14989 case CPP_DIV_EQ:
14990 assop = true;
14991 op = TRUNC_DIV_EXPR;
14992 break;
14993
14994 case CPP_MOD_EQ:
14995 assop = true;
14996 op = TRUNC_MOD_EXPR;
14997 break;
14998
14999 case CPP_XOR_EQ:
15000 assop = true;
15001 op = BIT_XOR_EXPR;
15002 break;
15003
15004 case CPP_AND_EQ:
15005 assop = true;
15006 op = BIT_AND_EXPR;
15007 break;
15008
15009 case CPP_OR_EQ:
15010 assop = true;
15011 op = BIT_IOR_EXPR;
15012 break;
15013
15014 case CPP_LSHIFT:
15015 op = LSHIFT_EXPR;
15016 break;
15017
15018 case CPP_RSHIFT:
15019 op = RSHIFT_EXPR;
15020 break;
15021
15022 case CPP_LSHIFT_EQ:
15023 assop = true;
15024 op = LSHIFT_EXPR;
15025 break;
15026
15027 case CPP_RSHIFT_EQ:
15028 assop = true;
15029 op = RSHIFT_EXPR;
15030 break;
15031
15032 case CPP_EQ_EQ:
15033 op = EQ_EXPR;
15034 break;
15035
15036 case CPP_NOT_EQ:
15037 op = NE_EXPR;
15038 break;
15039
15040 case CPP_LESS_EQ:
15041 op = LE_EXPR;
15042 break;
15043
15044 case CPP_GREATER_EQ:
15045 op = GE_EXPR;
15046 break;
15047
15048 case CPP_AND_AND:
15049 op = TRUTH_ANDIF_EXPR;
15050 break;
15051
15052 case CPP_OR_OR:
15053 op = TRUTH_ORIF_EXPR;
15054 break;
15055
15056 case CPP_PLUS_PLUS:
15057 op = POSTINCREMENT_EXPR;
15058 break;
15059
15060 case CPP_MINUS_MINUS:
15061 op = PREDECREMENT_EXPR;
15062 break;
15063
15064 case CPP_COMMA:
15065 op = COMPOUND_EXPR;
15066 break;
15067
15068 case CPP_DEREF_STAR:
15069 op = MEMBER_REF;
15070 break;
15071
15072 case CPP_DEREF:
15073 op = COMPONENT_REF;
15074 break;
15075
15076 case CPP_OPEN_PAREN:
15077 {
15078 /* Consume the `('. */
15079 matching_parens parens;
15080 parens.consume_open (parser);
15081 /* Look for the matching `)'. */
15082 parens.require_close (parser);
15083 op = CALL_EXPR;
15084 consumed = true;
15085 break;
15086 }
15087
15088 case CPP_OPEN_SQUARE:
15089 /* Consume the `['. */
15090 cp_lexer_consume_token (parser->lexer);
15091 /* Look for the matching `]'. */
15092 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15093 op = ARRAY_REF;
15094 consumed = true;
15095 break;
15096
15097 case CPP_UTF8STRING:
15098 case CPP_UTF8STRING_USERDEF:
15099 utf8 = true;
15100 /* FALLTHRU */
15101 case CPP_STRING:
15102 case CPP_WSTRING:
15103 case CPP_STRING16:
15104 case CPP_STRING32:
15105 case CPP_STRING_USERDEF:
15106 case CPP_WSTRING_USERDEF:
15107 case CPP_STRING16_USERDEF:
15108 case CPP_STRING32_USERDEF:
15109 {
15110 tree str, string_tree;
15111 int sz, len;
15112
15113 if (cxx_dialect == cxx98)
15114 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15115
15116 /* Consume the string. */
15117 str = cp_parser_string_literal (parser, /*translate=*/true,
15118 /*wide_ok=*/true, /*lookup_udlit=*/false);
15119 if (str == error_mark_node)
15120 return error_mark_node;
15121 else if (TREE_CODE (str) == USERDEF_LITERAL)
15122 {
15123 string_tree = USERDEF_LITERAL_VALUE (str);
15124 id = USERDEF_LITERAL_SUFFIX_ID (str);
15125 }
15126 else
15127 {
15128 string_tree = str;
15129 /* Look for the suffix identifier. */
15130 token = cp_lexer_peek_token (parser->lexer);
15131 if (token->type == CPP_NAME)
15132 id = cp_parser_identifier (parser);
15133 else if (token->type == CPP_KEYWORD)
15134 {
15135 error ("unexpected keyword;"
15136 " remove space between quotes and suffix identifier");
15137 return error_mark_node;
15138 }
15139 else
15140 {
15141 error ("expected suffix identifier");
15142 return error_mark_node;
15143 }
15144 }
15145 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15146 (TREE_TYPE (TREE_TYPE (string_tree))));
15147 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15148 if (len != 0)
15149 {
15150 error ("expected empty string after %<operator%> keyword");
15151 return error_mark_node;
15152 }
15153 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15154 != char_type_node)
15155 {
15156 error ("invalid encoding prefix in literal operator");
15157 return error_mark_node;
15158 }
15159 if (id != error_mark_node)
15160 {
15161 const char *name = IDENTIFIER_POINTER (id);
15162 id = cp_literal_operator_id (name);
15163 }
15164 return id;
15165 }
15166
15167 default:
15168 /* Anything else is an error. */
15169 break;
15170 }
15171
15172 /* If we have selected an identifier, we need to consume the
15173 operator token. */
15174 if (op != ERROR_MARK)
15175 {
15176 id = ovl_op_identifier (assop, op);
15177 if (!consumed)
15178 cp_lexer_consume_token (parser->lexer);
15179 }
15180 /* Otherwise, no valid operator name was present. */
15181 else
15182 {
15183 cp_parser_error (parser, "expected operator");
15184 id = error_mark_node;
15185 }
15186
15187 return cp_expr (id, start_loc);
15188 }
15189
15190 /* Parse a template-declaration.
15191
15192 template-declaration:
15193 export [opt] template < template-parameter-list > declaration
15194
15195 If MEMBER_P is TRUE, this template-declaration occurs within a
15196 class-specifier.
15197
15198 The grammar rule given by the standard isn't correct. What
15199 is really meant is:
15200
15201 template-declaration:
15202 export [opt] template-parameter-list-seq
15203 decl-specifier-seq [opt] init-declarator [opt] ;
15204 export [opt] template-parameter-list-seq
15205 function-definition
15206
15207 template-parameter-list-seq:
15208 template-parameter-list-seq [opt]
15209 template < template-parameter-list >
15210
15211 Concept Extensions:
15212
15213 template-parameter-list-seq:
15214 template < template-parameter-list > requires-clause [opt]
15215
15216 requires-clause:
15217 requires logical-or-expression */
15218
15219 static void
15220 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15221 {
15222 /* Check for `export'. */
15223 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15224 {
15225 /* Consume the `export' token. */
15226 cp_lexer_consume_token (parser->lexer);
15227 /* Warn that we do not support `export'. */
15228 warning (0, "keyword %<export%> not implemented, and will be ignored");
15229 }
15230
15231 cp_parser_template_declaration_after_export (parser, member_p);
15232 }
15233
15234 /* Parse a template-parameter-list.
15235
15236 template-parameter-list:
15237 template-parameter
15238 template-parameter-list , template-parameter
15239
15240 Returns a TREE_LIST. Each node represents a template parameter.
15241 The nodes are connected via their TREE_CHAINs. */
15242
15243 static tree
15244 cp_parser_template_parameter_list (cp_parser* parser)
15245 {
15246 tree parameter_list = NULL_TREE;
15247
15248 begin_template_parm_list ();
15249
15250 /* The loop below parses the template parms. We first need to know
15251 the total number of template parms to be able to compute proper
15252 canonical types of each dependent type. So after the loop, when
15253 we know the total number of template parms,
15254 end_template_parm_list computes the proper canonical types and
15255 fixes up the dependent types accordingly. */
15256 while (true)
15257 {
15258 tree parameter;
15259 bool is_non_type;
15260 bool is_parameter_pack;
15261 location_t parm_loc;
15262
15263 /* Parse the template-parameter. */
15264 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15265 parameter = cp_parser_template_parameter (parser,
15266 &is_non_type,
15267 &is_parameter_pack);
15268 /* Add it to the list. */
15269 if (parameter != error_mark_node)
15270 parameter_list = process_template_parm (parameter_list,
15271 parm_loc,
15272 parameter,
15273 is_non_type,
15274 is_parameter_pack);
15275 else
15276 {
15277 tree err_parm = build_tree_list (parameter, parameter);
15278 parameter_list = chainon (parameter_list, err_parm);
15279 }
15280
15281 /* If the next token is not a `,', we're done. */
15282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15283 break;
15284 /* Otherwise, consume the `,' token. */
15285 cp_lexer_consume_token (parser->lexer);
15286 }
15287
15288 return end_template_parm_list (parameter_list);
15289 }
15290
15291 /* Parse a introduction-list.
15292
15293 introduction-list:
15294 introduced-parameter
15295 introduction-list , introduced-parameter
15296
15297 introduced-parameter:
15298 ...[opt] identifier
15299
15300 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15301 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15302 WILDCARD_DECL will also have DECL_NAME set and token location in
15303 DECL_SOURCE_LOCATION. */
15304
15305 static tree
15306 cp_parser_introduction_list (cp_parser *parser)
15307 {
15308 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15309
15310 while (true)
15311 {
15312 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15313 if (is_pack)
15314 cp_lexer_consume_token (parser->lexer);
15315
15316 tree identifier = cp_parser_identifier (parser);
15317 if (identifier == error_mark_node)
15318 break;
15319
15320 /* Build placeholder. */
15321 tree parm = build_nt (WILDCARD_DECL);
15322 DECL_SOURCE_LOCATION (parm)
15323 = cp_lexer_peek_token (parser->lexer)->location;
15324 DECL_NAME (parm) = identifier;
15325 WILDCARD_PACK_P (parm) = is_pack;
15326 vec_safe_push (introduction_vec, parm);
15327
15328 /* If the next token is not a `,', we're done. */
15329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15330 break;
15331 /* Otherwise, consume the `,' token. */
15332 cp_lexer_consume_token (parser->lexer);
15333 }
15334
15335 /* Convert the vec into a TREE_VEC. */
15336 tree introduction_list = make_tree_vec (introduction_vec->length ());
15337 unsigned int n;
15338 tree parm;
15339 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15340 TREE_VEC_ELT (introduction_list, n) = parm;
15341
15342 release_tree_vector (introduction_vec);
15343 return introduction_list;
15344 }
15345
15346 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15347 is an abstract declarator. */
15348
15349 static inline cp_declarator*
15350 get_id_declarator (cp_declarator *declarator)
15351 {
15352 cp_declarator *d = declarator;
15353 while (d && d->kind != cdk_id)
15354 d = d->declarator;
15355 return d;
15356 }
15357
15358 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15359 is an abstract declarator. */
15360
15361 static inline tree
15362 get_unqualified_id (cp_declarator *declarator)
15363 {
15364 declarator = get_id_declarator (declarator);
15365 if (declarator)
15366 return declarator->u.id.unqualified_name;
15367 else
15368 return NULL_TREE;
15369 }
15370
15371 /* Returns true if DECL represents a constrained-parameter. */
15372
15373 static inline bool
15374 is_constrained_parameter (tree decl)
15375 {
15376 return (decl
15377 && TREE_CODE (decl) == TYPE_DECL
15378 && CONSTRAINED_PARM_CONCEPT (decl)
15379 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15380 }
15381
15382 /* Returns true if PARM declares a constrained-parameter. */
15383
15384 static inline bool
15385 is_constrained_parameter (cp_parameter_declarator *parm)
15386 {
15387 return is_constrained_parameter (parm->decl_specifiers.type);
15388 }
15389
15390 /* Check that the type parameter is only a declarator-id, and that its
15391 type is not cv-qualified. */
15392
15393 bool
15394 cp_parser_check_constrained_type_parm (cp_parser *parser,
15395 cp_parameter_declarator *parm)
15396 {
15397 if (!parm->declarator)
15398 return true;
15399
15400 if (parm->declarator->kind != cdk_id)
15401 {
15402 cp_parser_error (parser, "invalid constrained type parameter");
15403 return false;
15404 }
15405
15406 /* Don't allow cv-qualified type parameters. */
15407 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15408 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15409 {
15410 cp_parser_error (parser, "cv-qualified type parameter");
15411 return false;
15412 }
15413
15414 return true;
15415 }
15416
15417 /* Finish parsing/processing a template type parameter and checking
15418 various restrictions. */
15419
15420 static inline tree
15421 cp_parser_constrained_type_template_parm (cp_parser *parser,
15422 tree id,
15423 cp_parameter_declarator* parmdecl)
15424 {
15425 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15426 return finish_template_type_parm (class_type_node, id);
15427 else
15428 return error_mark_node;
15429 }
15430
15431 static tree
15432 finish_constrained_template_template_parm (tree proto, tree id)
15433 {
15434 /* FIXME: This should probably be copied, and we may need to adjust
15435 the template parameter depths. */
15436 tree saved_parms = current_template_parms;
15437 begin_template_parm_list ();
15438 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15439 end_template_parm_list ();
15440
15441 tree parm = finish_template_template_parm (class_type_node, id);
15442 current_template_parms = saved_parms;
15443
15444 return parm;
15445 }
15446
15447 /* Finish parsing/processing a template template parameter by borrowing
15448 the template parameter list from the prototype parameter. */
15449
15450 static tree
15451 cp_parser_constrained_template_template_parm (cp_parser *parser,
15452 tree proto,
15453 tree id,
15454 cp_parameter_declarator *parmdecl)
15455 {
15456 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15457 return error_mark_node;
15458 return finish_constrained_template_template_parm (proto, id);
15459 }
15460
15461 /* Create a new non-type template parameter from the given PARM
15462 declarator. */
15463
15464 static tree
15465 constrained_non_type_template_parm (bool *is_non_type,
15466 cp_parameter_declarator *parm)
15467 {
15468 *is_non_type = true;
15469 cp_declarator *decl = parm->declarator;
15470 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15471 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15472 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15473 }
15474
15475 /* Build a constrained template parameter based on the PARMDECL
15476 declarator. The type of PARMDECL is the constrained type, which
15477 refers to the prototype template parameter that ultimately
15478 specifies the type of the declared parameter. */
15479
15480 static tree
15481 finish_constrained_parameter (cp_parser *parser,
15482 cp_parameter_declarator *parmdecl,
15483 bool *is_non_type,
15484 bool *is_parameter_pack)
15485 {
15486 tree decl = parmdecl->decl_specifiers.type;
15487 tree id = get_unqualified_id (parmdecl->declarator);
15488 tree def = parmdecl->default_argument;
15489 tree proto = DECL_INITIAL (decl);
15490
15491 /* A template parameter constrained by a variadic concept shall also
15492 be declared as a template parameter pack. */
15493 bool is_variadic = template_parameter_pack_p (proto);
15494 if (is_variadic && !*is_parameter_pack)
15495 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15496
15497 /* Build the parameter. Return an error if the declarator was invalid. */
15498 tree parm;
15499 if (TREE_CODE (proto) == TYPE_DECL)
15500 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15501 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15502 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15503 parmdecl);
15504 else
15505 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15506 if (parm == error_mark_node)
15507 return error_mark_node;
15508
15509 /* Finish the parameter decl and create a node attaching the
15510 default argument and constraint. */
15511 parm = build_tree_list (def, parm);
15512 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15513
15514 return parm;
15515 }
15516
15517 /* Returns true if the parsed type actually represents the declaration
15518 of a type template-parameter. */
15519
15520 static inline bool
15521 declares_constrained_type_template_parameter (tree type)
15522 {
15523 return (is_constrained_parameter (type)
15524 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15525 }
15526
15527
15528 /* Returns true if the parsed type actually represents the declaration of
15529 a template template-parameter. */
15530
15531 static bool
15532 declares_constrained_template_template_parameter (tree type)
15533 {
15534 return (is_constrained_parameter (type)
15535 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15536 }
15537
15538 /* Parse a default argument for a type template-parameter.
15539 Note that diagnostics are handled in cp_parser_template_parameter. */
15540
15541 static tree
15542 cp_parser_default_type_template_argument (cp_parser *parser)
15543 {
15544 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15545
15546 /* Consume the `=' token. */
15547 cp_lexer_consume_token (parser->lexer);
15548
15549 cp_token *token = cp_lexer_peek_token (parser->lexer);
15550
15551 /* Parse the default-argument. */
15552 push_deferring_access_checks (dk_no_deferred);
15553 tree default_argument = cp_parser_type_id (parser);
15554 pop_deferring_access_checks ();
15555
15556 if (flag_concepts && type_uses_auto (default_argument))
15557 {
15558 error_at (token->location,
15559 "invalid use of %<auto%> in default template argument");
15560 return error_mark_node;
15561 }
15562
15563 return default_argument;
15564 }
15565
15566 /* Parse a default argument for a template template-parameter. */
15567
15568 static tree
15569 cp_parser_default_template_template_argument (cp_parser *parser)
15570 {
15571 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15572
15573 bool is_template;
15574
15575 /* Consume the `='. */
15576 cp_lexer_consume_token (parser->lexer);
15577 /* Parse the id-expression. */
15578 push_deferring_access_checks (dk_no_deferred);
15579 /* save token before parsing the id-expression, for error
15580 reporting */
15581 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15582 tree default_argument
15583 = cp_parser_id_expression (parser,
15584 /*template_keyword_p=*/false,
15585 /*check_dependency_p=*/true,
15586 /*template_p=*/&is_template,
15587 /*declarator_p=*/false,
15588 /*optional_p=*/false);
15589 if (TREE_CODE (default_argument) == TYPE_DECL)
15590 /* If the id-expression was a template-id that refers to
15591 a template-class, we already have the declaration here,
15592 so no further lookup is needed. */
15593 ;
15594 else
15595 /* Look up the name. */
15596 default_argument
15597 = cp_parser_lookup_name (parser, default_argument,
15598 none_type,
15599 /*is_template=*/is_template,
15600 /*is_namespace=*/false,
15601 /*check_dependency=*/true,
15602 /*ambiguous_decls=*/NULL,
15603 token->location);
15604 /* See if the default argument is valid. */
15605 default_argument = check_template_template_default_arg (default_argument);
15606 pop_deferring_access_checks ();
15607 return default_argument;
15608 }
15609
15610 /* Parse a template-parameter.
15611
15612 template-parameter:
15613 type-parameter
15614 parameter-declaration
15615
15616 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15617 the parameter. The TREE_PURPOSE is the default value, if any.
15618 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15619 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15620 set to true iff this parameter is a parameter pack. */
15621
15622 static tree
15623 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15624 bool *is_parameter_pack)
15625 {
15626 cp_token *token;
15627 cp_parameter_declarator *parameter_declarator;
15628 tree parm;
15629
15630 /* Assume it is a type parameter or a template parameter. */
15631 *is_non_type = false;
15632 /* Assume it not a parameter pack. */
15633 *is_parameter_pack = false;
15634 /* Peek at the next token. */
15635 token = cp_lexer_peek_token (parser->lexer);
15636 /* If it is `template', we have a type-parameter. */
15637 if (token->keyword == RID_TEMPLATE)
15638 return cp_parser_type_parameter (parser, is_parameter_pack);
15639 /* If it is `class' or `typename' we do not know yet whether it is a
15640 type parameter or a non-type parameter. Consider:
15641
15642 template <typename T, typename T::X X> ...
15643
15644 or:
15645
15646 template <class C, class D*> ...
15647
15648 Here, the first parameter is a type parameter, and the second is
15649 a non-type parameter. We can tell by looking at the token after
15650 the identifier -- if it is a `,', `=', or `>' then we have a type
15651 parameter. */
15652 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15653 {
15654 /* Peek at the token after `class' or `typename'. */
15655 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15656 /* If it's an ellipsis, we have a template type parameter
15657 pack. */
15658 if (token->type == CPP_ELLIPSIS)
15659 return cp_parser_type_parameter (parser, is_parameter_pack);
15660 /* If it's an identifier, skip it. */
15661 if (token->type == CPP_NAME)
15662 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15663 /* Now, see if the token looks like the end of a template
15664 parameter. */
15665 if (token->type == CPP_COMMA
15666 || token->type == CPP_EQ
15667 || token->type == CPP_GREATER)
15668 return cp_parser_type_parameter (parser, is_parameter_pack);
15669 }
15670
15671 /* Otherwise, it is a non-type parameter or a constrained parameter.
15672
15673 [temp.param]
15674
15675 When parsing a default template-argument for a non-type
15676 template-parameter, the first non-nested `>' is taken as the end
15677 of the template parameter-list rather than a greater-than
15678 operator. */
15679 parameter_declarator
15680 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15681 /*parenthesized_p=*/NULL);
15682
15683 if (!parameter_declarator)
15684 return error_mark_node;
15685
15686 /* If the parameter declaration is marked as a parameter pack, set
15687 *IS_PARAMETER_PACK to notify the caller. */
15688 if (parameter_declarator->template_parameter_pack_p)
15689 *is_parameter_pack = true;
15690
15691 if (parameter_declarator->default_argument)
15692 {
15693 /* Can happen in some cases of erroneous input (c++/34892). */
15694 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15695 /* Consume the `...' for better error recovery. */
15696 cp_lexer_consume_token (parser->lexer);
15697 }
15698
15699 // The parameter may have been constrained.
15700 if (is_constrained_parameter (parameter_declarator))
15701 return finish_constrained_parameter (parser,
15702 parameter_declarator,
15703 is_non_type,
15704 is_parameter_pack);
15705
15706 // Now we're sure that the parameter is a non-type parameter.
15707 *is_non_type = true;
15708
15709 parm = grokdeclarator (parameter_declarator->declarator,
15710 &parameter_declarator->decl_specifiers,
15711 TPARM, /*initialized=*/0,
15712 /*attrlist=*/NULL);
15713 if (parm == error_mark_node)
15714 return error_mark_node;
15715
15716 return build_tree_list (parameter_declarator->default_argument, parm);
15717 }
15718
15719 /* Parse a type-parameter.
15720
15721 type-parameter:
15722 class identifier [opt]
15723 class identifier [opt] = type-id
15724 typename identifier [opt]
15725 typename identifier [opt] = type-id
15726 template < template-parameter-list > class identifier [opt]
15727 template < template-parameter-list > class identifier [opt]
15728 = id-expression
15729
15730 GNU Extension (variadic templates):
15731
15732 type-parameter:
15733 class ... identifier [opt]
15734 typename ... identifier [opt]
15735
15736 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15737 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15738 the declaration of the parameter.
15739
15740 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15741
15742 static tree
15743 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15744 {
15745 cp_token *token;
15746 tree parameter;
15747
15748 /* Look for a keyword to tell us what kind of parameter this is. */
15749 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15750 if (!token)
15751 return error_mark_node;
15752
15753 switch (token->keyword)
15754 {
15755 case RID_CLASS:
15756 case RID_TYPENAME:
15757 {
15758 tree identifier;
15759 tree default_argument;
15760
15761 /* If the next token is an ellipsis, we have a template
15762 argument pack. */
15763 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15764 {
15765 /* Consume the `...' token. */
15766 cp_lexer_consume_token (parser->lexer);
15767 maybe_warn_variadic_templates ();
15768
15769 *is_parameter_pack = true;
15770 }
15771
15772 /* If the next token is an identifier, then it names the
15773 parameter. */
15774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15775 identifier = cp_parser_identifier (parser);
15776 else
15777 identifier = NULL_TREE;
15778
15779 /* Create the parameter. */
15780 parameter = finish_template_type_parm (class_type_node, identifier);
15781
15782 /* If the next token is an `=', we have a default argument. */
15783 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15784 {
15785 default_argument
15786 = cp_parser_default_type_template_argument (parser);
15787
15788 /* Template parameter packs cannot have default
15789 arguments. */
15790 if (*is_parameter_pack)
15791 {
15792 if (identifier)
15793 error_at (token->location,
15794 "template parameter pack %qD cannot have a "
15795 "default argument", identifier);
15796 else
15797 error_at (token->location,
15798 "template parameter packs cannot have "
15799 "default arguments");
15800 default_argument = NULL_TREE;
15801 }
15802 else if (check_for_bare_parameter_packs (default_argument))
15803 default_argument = error_mark_node;
15804 }
15805 else
15806 default_argument = NULL_TREE;
15807
15808 /* Create the combined representation of the parameter and the
15809 default argument. */
15810 parameter = build_tree_list (default_argument, parameter);
15811 }
15812 break;
15813
15814 case RID_TEMPLATE:
15815 {
15816 tree identifier;
15817 tree default_argument;
15818
15819 /* Look for the `<'. */
15820 cp_parser_require (parser, CPP_LESS, RT_LESS);
15821 /* Parse the template-parameter-list. */
15822 cp_parser_template_parameter_list (parser);
15823 /* Look for the `>'. */
15824 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15825
15826 // If template requirements are present, parse them.
15827 if (flag_concepts)
15828 {
15829 tree reqs = get_shorthand_constraints (current_template_parms);
15830 if (tree r = cp_parser_requires_clause_opt (parser))
15831 reqs = conjoin_constraints (reqs, normalize_expression (r));
15832 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15833 }
15834
15835 /* Look for the `class' or 'typename' keywords. */
15836 cp_parser_type_parameter_key (parser);
15837 /* If the next token is an ellipsis, we have a template
15838 argument pack. */
15839 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15840 {
15841 /* Consume the `...' token. */
15842 cp_lexer_consume_token (parser->lexer);
15843 maybe_warn_variadic_templates ();
15844
15845 *is_parameter_pack = true;
15846 }
15847 /* If the next token is an `=', then there is a
15848 default-argument. If the next token is a `>', we are at
15849 the end of the parameter-list. If the next token is a `,',
15850 then we are at the end of this parameter. */
15851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15852 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15853 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15854 {
15855 identifier = cp_parser_identifier (parser);
15856 /* Treat invalid names as if the parameter were nameless. */
15857 if (identifier == error_mark_node)
15858 identifier = NULL_TREE;
15859 }
15860 else
15861 identifier = NULL_TREE;
15862
15863 /* Create the template parameter. */
15864 parameter = finish_template_template_parm (class_type_node,
15865 identifier);
15866
15867 /* If the next token is an `=', then there is a
15868 default-argument. */
15869 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15870 {
15871 default_argument
15872 = cp_parser_default_template_template_argument (parser);
15873
15874 /* Template parameter packs cannot have default
15875 arguments. */
15876 if (*is_parameter_pack)
15877 {
15878 if (identifier)
15879 error_at (token->location,
15880 "template parameter pack %qD cannot "
15881 "have a default argument",
15882 identifier);
15883 else
15884 error_at (token->location, "template parameter packs cannot "
15885 "have default arguments");
15886 default_argument = NULL_TREE;
15887 }
15888 }
15889 else
15890 default_argument = NULL_TREE;
15891
15892 /* Create the combined representation of the parameter and the
15893 default argument. */
15894 parameter = build_tree_list (default_argument, parameter);
15895 }
15896 break;
15897
15898 default:
15899 gcc_unreachable ();
15900 break;
15901 }
15902
15903 return parameter;
15904 }
15905
15906 /* Parse a template-id.
15907
15908 template-id:
15909 template-name < template-argument-list [opt] >
15910
15911 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15912 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15913 returned. Otherwise, if the template-name names a function, or set
15914 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15915 names a class, returns a TYPE_DECL for the specialization.
15916
15917 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15918 uninstantiated templates. */
15919
15920 static tree
15921 cp_parser_template_id (cp_parser *parser,
15922 bool template_keyword_p,
15923 bool check_dependency_p,
15924 enum tag_types tag_type,
15925 bool is_declaration)
15926 {
15927 tree templ;
15928 tree arguments;
15929 tree template_id;
15930 cp_token_position start_of_id = 0;
15931 cp_token *next_token = NULL, *next_token_2 = NULL;
15932 bool is_identifier;
15933
15934 /* If the next token corresponds to a template-id, there is no need
15935 to reparse it. */
15936 cp_token *token = cp_lexer_peek_token (parser->lexer);
15937 if (token->type == CPP_TEMPLATE_ID)
15938 {
15939 cp_lexer_consume_token (parser->lexer);
15940 return saved_checks_value (token->u.tree_check_value);
15941 }
15942
15943 /* Avoid performing name lookup if there is no possibility of
15944 finding a template-id. */
15945 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15946 || (token->type == CPP_NAME
15947 && !cp_parser_nth_token_starts_template_argument_list_p
15948 (parser, 2)))
15949 {
15950 cp_parser_error (parser, "expected template-id");
15951 return error_mark_node;
15952 }
15953
15954 /* Remember where the template-id starts. */
15955 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15956 start_of_id = cp_lexer_token_position (parser->lexer, false);
15957
15958 push_deferring_access_checks (dk_deferred);
15959
15960 /* Parse the template-name. */
15961 is_identifier = false;
15962 templ = cp_parser_template_name (parser, template_keyword_p,
15963 check_dependency_p,
15964 is_declaration,
15965 tag_type,
15966 &is_identifier);
15967 if (templ == error_mark_node || is_identifier)
15968 {
15969 pop_deferring_access_checks ();
15970 return templ;
15971 }
15972
15973 /* Since we're going to preserve any side-effects from this parse, set up a
15974 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15975 in the template arguments. */
15976 tentative_firewall firewall (parser);
15977
15978 /* If we find the sequence `[:' after a template-name, it's probably
15979 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15980 parse correctly the argument list. */
15981 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15982 == CPP_OPEN_SQUARE)
15983 && next_token->flags & DIGRAPH
15984 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15985 == CPP_COLON)
15986 && !(next_token_2->flags & PREV_WHITE))
15987 {
15988 cp_parser_parse_tentatively (parser);
15989 /* Change `:' into `::'. */
15990 next_token_2->type = CPP_SCOPE;
15991 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15992 CPP_LESS. */
15993 cp_lexer_consume_token (parser->lexer);
15994
15995 /* Parse the arguments. */
15996 arguments = cp_parser_enclosed_template_argument_list (parser);
15997 if (!cp_parser_parse_definitely (parser))
15998 {
15999 /* If we couldn't parse an argument list, then we revert our changes
16000 and return simply an error. Maybe this is not a template-id
16001 after all. */
16002 next_token_2->type = CPP_COLON;
16003 cp_parser_error (parser, "expected %<<%>");
16004 pop_deferring_access_checks ();
16005 return error_mark_node;
16006 }
16007 /* Otherwise, emit an error about the invalid digraph, but continue
16008 parsing because we got our argument list. */
16009 if (permerror (next_token->location,
16010 "%<<::%> cannot begin a template-argument list"))
16011 {
16012 static bool hint = false;
16013 inform (next_token->location,
16014 "%<<:%> is an alternate spelling for %<[%>."
16015 " Insert whitespace between %<<%> and %<::%>");
16016 if (!hint && !flag_permissive)
16017 {
16018 inform (next_token->location, "(if you use %<-fpermissive%> "
16019 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16020 "accept your code)");
16021 hint = true;
16022 }
16023 }
16024 }
16025 else
16026 {
16027 /* Look for the `<' that starts the template-argument-list. */
16028 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16029 {
16030 pop_deferring_access_checks ();
16031 return error_mark_node;
16032 }
16033 /* Parse the arguments. */
16034 arguments = cp_parser_enclosed_template_argument_list (parser);
16035
16036 if ((cxx_dialect > cxx17)
16037 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16038 && !template_keyword_p
16039 && (cp_parser_error_occurred (parser)
16040 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16041 {
16042 /* This didn't go well. */
16043 if (TREE_CODE (templ) == FUNCTION_DECL)
16044 {
16045 /* C++2A says that "function-name < a;" is now ill-formed. */
16046 if (cp_parser_error_occurred (parser))
16047 {
16048 error_at (token->location, "invalid template-argument-list");
16049 inform (token->location, "function name as the left hand "
16050 "operand of %<<%> is ill-formed in C++2a; wrap the "
16051 "function name in %<()%>");
16052 }
16053 else
16054 /* We expect "f<targs>" to be followed by "(args)". */
16055 error_at (cp_lexer_peek_token (parser->lexer)->location,
16056 "expected %<(%> after template-argument-list");
16057 if (start_of_id)
16058 /* Purge all subsequent tokens. */
16059 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16060 }
16061 else
16062 cp_parser_simulate_error (parser);
16063 pop_deferring_access_checks ();
16064 return error_mark_node;
16065 }
16066 }
16067
16068 /* Set the location to be of the form:
16069 template-name < template-argument-list [opt] >
16070 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16071 with caret == start at the start of the template-name,
16072 ranging until the closing '>'. */
16073 location_t finish_loc
16074 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16075 location_t combined_loc
16076 = make_location (token->location, token->location, finish_loc);
16077
16078 /* Check for concepts autos where they don't belong. We could
16079 identify types in some cases of idnetifier TEMPL, looking ahead
16080 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16081 types. We reject them in functions, but if what we have is an
16082 identifier, even with none_type we can't conclude it's NOT a
16083 type, we have to wait for template substitution. */
16084 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16085 template_id = error_mark_node;
16086 /* Build a representation of the specialization. */
16087 else if (identifier_p (templ))
16088 template_id = build_min_nt_loc (combined_loc,
16089 TEMPLATE_ID_EXPR,
16090 templ, arguments);
16091 else if (DECL_TYPE_TEMPLATE_P (templ)
16092 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16093 {
16094 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16095 template (rather than some instantiation thereof) only if
16096 is not nested within some other construct. For example, in
16097 "template <typename T> void f(T) { A<T>::", A<T> is just an
16098 instantiation of A. */
16099 bool entering_scope
16100 = (template_parm_scope_p ()
16101 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16102 template_id
16103 = finish_template_type (templ, arguments, entering_scope);
16104 }
16105 /* A template-like identifier may be a partial concept id. */
16106 else if (flag_concepts
16107 && (template_id = (cp_parser_maybe_partial_concept_id
16108 (parser, templ, arguments))))
16109 return template_id;
16110 else if (variable_template_p (templ))
16111 {
16112 template_id = lookup_template_variable (templ, arguments);
16113 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16114 SET_EXPR_LOCATION (template_id, combined_loc);
16115 }
16116 else
16117 {
16118 /* If it's not a class-template or a template-template, it should be
16119 a function-template. */
16120 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16121 || TREE_CODE (templ) == OVERLOAD
16122 || TREE_CODE (templ) == FUNCTION_DECL
16123 || BASELINK_P (templ)));
16124
16125 template_id = lookup_template_function (templ, arguments);
16126 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16127 SET_EXPR_LOCATION (template_id, combined_loc);
16128 }
16129
16130 /* If parsing tentatively, replace the sequence of tokens that makes
16131 up the template-id with a CPP_TEMPLATE_ID token. That way,
16132 should we re-parse the token stream, we will not have to repeat
16133 the effort required to do the parse, nor will we issue duplicate
16134 error messages about problems during instantiation of the
16135 template. */
16136 if (start_of_id
16137 /* Don't do this if we had a parse error in a declarator; re-parsing
16138 might succeed if a name changes meaning (60361). */
16139 && !(cp_parser_error_occurred (parser)
16140 && cp_parser_parsing_tentatively (parser)
16141 && parser->in_declarator_p))
16142 {
16143 /* Reset the contents of the START_OF_ID token. */
16144 token->type = CPP_TEMPLATE_ID;
16145 token->location = combined_loc;
16146
16147 /* Retrieve any deferred checks. Do not pop this access checks yet
16148 so the memory will not be reclaimed during token replacing below. */
16149 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16150 token->u.tree_check_value->value = template_id;
16151 token->u.tree_check_value->checks = get_deferred_access_checks ();
16152 token->keyword = RID_MAX;
16153
16154 /* Purge all subsequent tokens. */
16155 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16156
16157 /* ??? Can we actually assume that, if template_id ==
16158 error_mark_node, we will have issued a diagnostic to the
16159 user, as opposed to simply marking the tentative parse as
16160 failed? */
16161 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16162 error_at (token->location, "parse error in template argument list");
16163 }
16164
16165 pop_to_parent_deferring_access_checks ();
16166 return template_id;
16167 }
16168
16169 /* Parse a template-name.
16170
16171 template-name:
16172 identifier
16173
16174 The standard should actually say:
16175
16176 template-name:
16177 identifier
16178 operator-function-id
16179
16180 A defect report has been filed about this issue.
16181
16182 A conversion-function-id cannot be a template name because they cannot
16183 be part of a template-id. In fact, looking at this code:
16184
16185 a.operator K<int>()
16186
16187 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16188 It is impossible to call a templated conversion-function-id with an
16189 explicit argument list, since the only allowed template parameter is
16190 the type to which it is converting.
16191
16192 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16193 `template' keyword, in a construction like:
16194
16195 T::template f<3>()
16196
16197 In that case `f' is taken to be a template-name, even though there
16198 is no way of knowing for sure.
16199
16200 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16201 name refers to a set of overloaded functions, at least one of which
16202 is a template, or an IDENTIFIER_NODE with the name of the template,
16203 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16204 names are looked up inside uninstantiated templates. */
16205
16206 static tree
16207 cp_parser_template_name (cp_parser* parser,
16208 bool template_keyword_p,
16209 bool check_dependency_p,
16210 bool is_declaration,
16211 enum tag_types tag_type,
16212 bool *is_identifier)
16213 {
16214 tree identifier;
16215 tree decl;
16216 cp_token *token = cp_lexer_peek_token (parser->lexer);
16217
16218 /* If the next token is `operator', then we have either an
16219 operator-function-id or a conversion-function-id. */
16220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16221 {
16222 /* We don't know whether we're looking at an
16223 operator-function-id or a conversion-function-id. */
16224 cp_parser_parse_tentatively (parser);
16225 /* Try an operator-function-id. */
16226 identifier = cp_parser_operator_function_id (parser);
16227 /* If that didn't work, try a conversion-function-id. */
16228 if (!cp_parser_parse_definitely (parser))
16229 {
16230 cp_parser_error (parser, "expected template-name");
16231 return error_mark_node;
16232 }
16233 }
16234 /* Look for the identifier. */
16235 else
16236 identifier = cp_parser_identifier (parser);
16237
16238 /* If we didn't find an identifier, we don't have a template-id. */
16239 if (identifier == error_mark_node)
16240 return error_mark_node;
16241
16242 /* If the name immediately followed the `template' keyword, then it
16243 is a template-name. However, if the next token is not `<', then
16244 we do not treat it as a template-name, since it is not being used
16245 as part of a template-id. This enables us to handle constructs
16246 like:
16247
16248 template <typename T> struct S { S(); };
16249 template <typename T> S<T>::S();
16250
16251 correctly. We would treat `S' as a template -- if it were `S<T>'
16252 -- but we do not if there is no `<'. */
16253
16254 if (processing_template_decl
16255 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16256 {
16257 /* In a declaration, in a dependent context, we pretend that the
16258 "template" keyword was present in order to improve error
16259 recovery. For example, given:
16260
16261 template <typename T> void f(T::X<int>);
16262
16263 we want to treat "X<int>" as a template-id. */
16264 if (is_declaration
16265 && !template_keyword_p
16266 && parser->scope && TYPE_P (parser->scope)
16267 && check_dependency_p
16268 && dependent_scope_p (parser->scope)
16269 /* Do not do this for dtors (or ctors), since they never
16270 need the template keyword before their name. */
16271 && !constructor_name_p (identifier, parser->scope))
16272 {
16273 cp_token_position start = 0;
16274
16275 /* Explain what went wrong. */
16276 error_at (token->location, "non-template %qD used as template",
16277 identifier);
16278 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16279 parser->scope, identifier);
16280 /* If parsing tentatively, find the location of the "<" token. */
16281 if (cp_parser_simulate_error (parser))
16282 start = cp_lexer_token_position (parser->lexer, true);
16283 /* Parse the template arguments so that we can issue error
16284 messages about them. */
16285 cp_lexer_consume_token (parser->lexer);
16286 cp_parser_enclosed_template_argument_list (parser);
16287 /* Skip tokens until we find a good place from which to
16288 continue parsing. */
16289 cp_parser_skip_to_closing_parenthesis (parser,
16290 /*recovering=*/true,
16291 /*or_comma=*/true,
16292 /*consume_paren=*/false);
16293 /* If parsing tentatively, permanently remove the
16294 template argument list. That will prevent duplicate
16295 error messages from being issued about the missing
16296 "template" keyword. */
16297 if (start)
16298 cp_lexer_purge_tokens_after (parser->lexer, start);
16299 if (is_identifier)
16300 *is_identifier = true;
16301 parser->context->object_type = NULL_TREE;
16302 return identifier;
16303 }
16304
16305 /* If the "template" keyword is present, then there is generally
16306 no point in doing name-lookup, so we just return IDENTIFIER.
16307 But, if the qualifying scope is non-dependent then we can
16308 (and must) do name-lookup normally. */
16309 if (template_keyword_p)
16310 {
16311 tree scope = (parser->scope ? parser->scope
16312 : parser->context->object_type);
16313 if (scope && TYPE_P (scope)
16314 && (!CLASS_TYPE_P (scope)
16315 || (check_dependency_p && dependent_type_p (scope))))
16316 {
16317 /* We're optimizing away the call to cp_parser_lookup_name, but
16318 we still need to do this. */
16319 parser->context->object_type = NULL_TREE;
16320 return identifier;
16321 }
16322 }
16323 }
16324
16325 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16326 const bool scoped_p = ((parser->scope ? parser->scope
16327 : parser->context->object_type) != NULL_TREE);
16328
16329 /* Look up the name. */
16330 decl = cp_parser_lookup_name (parser, identifier,
16331 tag_type,
16332 /*is_template=*/true,
16333 /*is_namespace=*/false,
16334 check_dependency_p,
16335 /*ambiguous_decls=*/NULL,
16336 token->location);
16337
16338 decl = strip_using_decl (decl);
16339
16340 /* If DECL is a template, then the name was a template-name. */
16341 if (TREE_CODE (decl) == TEMPLATE_DECL)
16342 {
16343 if (TREE_DEPRECATED (decl)
16344 && deprecated_state != DEPRECATED_SUPPRESS)
16345 warn_deprecated_use (decl, NULL_TREE);
16346 }
16347 else
16348 {
16349 /* The standard does not explicitly indicate whether a name that
16350 names a set of overloaded declarations, some of which are
16351 templates, is a template-name. However, such a name should
16352 be a template-name; otherwise, there is no way to form a
16353 template-id for the overloaded templates. */
16354 bool found = false;
16355
16356 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16357 !found && iter; ++iter)
16358 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16359 found = true;
16360
16361 if (!found
16362 && (cxx_dialect > cxx17)
16363 && !scoped_p
16364 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16365 {
16366 /* [temp.names] says "A name is also considered to refer to a template
16367 if it is an unqualified-id followed by a < and name lookup finds
16368 either one or more functions or finds nothing." */
16369
16370 /* The "more functions" case. Just use the OVERLOAD as normally.
16371 We don't use is_overloaded_fn here to avoid considering
16372 BASELINKs. */
16373 if (TREE_CODE (decl) == OVERLOAD
16374 /* Name lookup found one function. */
16375 || TREE_CODE (decl) == FUNCTION_DECL)
16376 found = true;
16377 /* Name lookup found nothing. */
16378 else if (decl == error_mark_node)
16379 return identifier;
16380 }
16381
16382 if (!found)
16383 {
16384 /* The name does not name a template. */
16385 cp_parser_error (parser, "expected template-name");
16386 return error_mark_node;
16387 }
16388 }
16389
16390 return decl;
16391 }
16392
16393 /* Parse a template-argument-list.
16394
16395 template-argument-list:
16396 template-argument ... [opt]
16397 template-argument-list , template-argument ... [opt]
16398
16399 Returns a TREE_VEC containing the arguments. */
16400
16401 static tree
16402 cp_parser_template_argument_list (cp_parser* parser)
16403 {
16404 tree fixed_args[10];
16405 unsigned n_args = 0;
16406 unsigned alloced = 10;
16407 tree *arg_ary = fixed_args;
16408 tree vec;
16409 bool saved_in_template_argument_list_p;
16410 bool saved_ice_p;
16411 bool saved_non_ice_p;
16412
16413 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16414 parser->in_template_argument_list_p = true;
16415 /* Even if the template-id appears in an integral
16416 constant-expression, the contents of the argument list do
16417 not. */
16418 saved_ice_p = parser->integral_constant_expression_p;
16419 parser->integral_constant_expression_p = false;
16420 saved_non_ice_p = parser->non_integral_constant_expression_p;
16421 parser->non_integral_constant_expression_p = false;
16422
16423 /* Parse the arguments. */
16424 do
16425 {
16426 tree argument;
16427
16428 if (n_args)
16429 /* Consume the comma. */
16430 cp_lexer_consume_token (parser->lexer);
16431
16432 /* Parse the template-argument. */
16433 argument = cp_parser_template_argument (parser);
16434
16435 /* If the next token is an ellipsis, we're expanding a template
16436 argument pack. */
16437 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16438 {
16439 if (argument == error_mark_node)
16440 {
16441 cp_token *token = cp_lexer_peek_token (parser->lexer);
16442 error_at (token->location,
16443 "expected parameter pack before %<...%>");
16444 }
16445 /* Consume the `...' token. */
16446 cp_lexer_consume_token (parser->lexer);
16447
16448 /* Make the argument into a TYPE_PACK_EXPANSION or
16449 EXPR_PACK_EXPANSION. */
16450 argument = make_pack_expansion (argument);
16451 }
16452
16453 if (n_args == alloced)
16454 {
16455 alloced *= 2;
16456
16457 if (arg_ary == fixed_args)
16458 {
16459 arg_ary = XNEWVEC (tree, alloced);
16460 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16461 }
16462 else
16463 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16464 }
16465 arg_ary[n_args++] = argument;
16466 }
16467 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16468
16469 vec = make_tree_vec (n_args);
16470
16471 while (n_args--)
16472 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16473
16474 if (arg_ary != fixed_args)
16475 free (arg_ary);
16476 parser->non_integral_constant_expression_p = saved_non_ice_p;
16477 parser->integral_constant_expression_p = saved_ice_p;
16478 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16479 if (CHECKING_P)
16480 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16481 return vec;
16482 }
16483
16484 /* Parse a template-argument.
16485
16486 template-argument:
16487 assignment-expression
16488 type-id
16489 id-expression
16490
16491 The representation is that of an assignment-expression, type-id, or
16492 id-expression -- except that the qualified id-expression is
16493 evaluated, so that the value returned is either a DECL or an
16494 OVERLOAD.
16495
16496 Although the standard says "assignment-expression", it forbids
16497 throw-expressions or assignments in the template argument.
16498 Therefore, we use "conditional-expression" instead. */
16499
16500 static tree
16501 cp_parser_template_argument (cp_parser* parser)
16502 {
16503 tree argument;
16504 bool template_p;
16505 bool address_p;
16506 bool maybe_type_id = false;
16507 cp_token *token = NULL, *argument_start_token = NULL;
16508 location_t loc = 0;
16509 cp_id_kind idk;
16510
16511 /* There's really no way to know what we're looking at, so we just
16512 try each alternative in order.
16513
16514 [temp.arg]
16515
16516 In a template-argument, an ambiguity between a type-id and an
16517 expression is resolved to a type-id, regardless of the form of
16518 the corresponding template-parameter.
16519
16520 Therefore, we try a type-id first. */
16521 cp_parser_parse_tentatively (parser);
16522 argument = cp_parser_template_type_arg (parser);
16523 /* If there was no error parsing the type-id but the next token is a
16524 '>>', our behavior depends on which dialect of C++ we're
16525 parsing. In C++98, we probably found a typo for '> >'. But there
16526 are type-id which are also valid expressions. For instance:
16527
16528 struct X { int operator >> (int); };
16529 template <int V> struct Foo {};
16530 Foo<X () >> 5> r;
16531
16532 Here 'X()' is a valid type-id of a function type, but the user just
16533 wanted to write the expression "X() >> 5". Thus, we remember that we
16534 found a valid type-id, but we still try to parse the argument as an
16535 expression to see what happens.
16536
16537 In C++0x, the '>>' will be considered two separate '>'
16538 tokens. */
16539 if (!cp_parser_error_occurred (parser)
16540 && cxx_dialect == cxx98
16541 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16542 {
16543 maybe_type_id = true;
16544 cp_parser_abort_tentative_parse (parser);
16545 }
16546 else
16547 {
16548 /* If the next token isn't a `,' or a `>', then this argument wasn't
16549 really finished. This means that the argument is not a valid
16550 type-id. */
16551 if (!cp_parser_next_token_ends_template_argument_p (parser))
16552 cp_parser_error (parser, "expected template-argument");
16553 /* If that worked, we're done. */
16554 if (cp_parser_parse_definitely (parser))
16555 return argument;
16556 }
16557 /* We're still not sure what the argument will be. */
16558 cp_parser_parse_tentatively (parser);
16559 /* Try a template. */
16560 argument_start_token = cp_lexer_peek_token (parser->lexer);
16561 argument = cp_parser_id_expression (parser,
16562 /*template_keyword_p=*/false,
16563 /*check_dependency_p=*/true,
16564 &template_p,
16565 /*declarator_p=*/false,
16566 /*optional_p=*/false);
16567 /* If the next token isn't a `,' or a `>', then this argument wasn't
16568 really finished. */
16569 if (!cp_parser_next_token_ends_template_argument_p (parser))
16570 cp_parser_error (parser, "expected template-argument");
16571 if (!cp_parser_error_occurred (parser))
16572 {
16573 /* Figure out what is being referred to. If the id-expression
16574 was for a class template specialization, then we will have a
16575 TYPE_DECL at this point. There is no need to do name lookup
16576 at this point in that case. */
16577 if (TREE_CODE (argument) != TYPE_DECL)
16578 argument = cp_parser_lookup_name (parser, argument,
16579 none_type,
16580 /*is_template=*/template_p,
16581 /*is_namespace=*/false,
16582 /*check_dependency=*/true,
16583 /*ambiguous_decls=*/NULL,
16584 argument_start_token->location);
16585 /* Handle a constrained-type-specifier for a non-type template
16586 parameter. */
16587 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16588 argument = decl;
16589 else if (TREE_CODE (argument) != TEMPLATE_DECL
16590 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16591 cp_parser_error (parser, "expected template-name");
16592 }
16593 if (cp_parser_parse_definitely (parser))
16594 {
16595 if (TREE_DEPRECATED (argument))
16596 warn_deprecated_use (argument, NULL_TREE);
16597 return argument;
16598 }
16599 /* It must be a non-type argument. In C++17 any constant-expression is
16600 allowed. */
16601 if (cxx_dialect > cxx14)
16602 goto general_expr;
16603
16604 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16605
16606 -- an integral constant-expression of integral or enumeration
16607 type; or
16608
16609 -- the name of a non-type template-parameter; or
16610
16611 -- the name of an object or function with external linkage...
16612
16613 -- the address of an object or function with external linkage...
16614
16615 -- a pointer to member... */
16616 /* Look for a non-type template parameter. */
16617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16618 {
16619 cp_parser_parse_tentatively (parser);
16620 argument = cp_parser_primary_expression (parser,
16621 /*address_p=*/false,
16622 /*cast_p=*/false,
16623 /*template_arg_p=*/true,
16624 &idk);
16625 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16626 || !cp_parser_next_token_ends_template_argument_p (parser))
16627 cp_parser_simulate_error (parser);
16628 if (cp_parser_parse_definitely (parser))
16629 return argument;
16630 }
16631
16632 /* If the next token is "&", the argument must be the address of an
16633 object or function with external linkage. */
16634 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16635 if (address_p)
16636 {
16637 loc = cp_lexer_peek_token (parser->lexer)->location;
16638 cp_lexer_consume_token (parser->lexer);
16639 }
16640 /* See if we might have an id-expression. */
16641 token = cp_lexer_peek_token (parser->lexer);
16642 if (token->type == CPP_NAME
16643 || token->keyword == RID_OPERATOR
16644 || token->type == CPP_SCOPE
16645 || token->type == CPP_TEMPLATE_ID
16646 || token->type == CPP_NESTED_NAME_SPECIFIER)
16647 {
16648 cp_parser_parse_tentatively (parser);
16649 argument = cp_parser_primary_expression (parser,
16650 address_p,
16651 /*cast_p=*/false,
16652 /*template_arg_p=*/true,
16653 &idk);
16654 if (cp_parser_error_occurred (parser)
16655 || !cp_parser_next_token_ends_template_argument_p (parser))
16656 cp_parser_abort_tentative_parse (parser);
16657 else
16658 {
16659 tree probe;
16660
16661 if (INDIRECT_REF_P (argument))
16662 {
16663 /* Strip the dereference temporarily. */
16664 gcc_assert (REFERENCE_REF_P (argument));
16665 argument = TREE_OPERAND (argument, 0);
16666 }
16667
16668 /* If we're in a template, we represent a qualified-id referring
16669 to a static data member as a SCOPE_REF even if the scope isn't
16670 dependent so that we can check access control later. */
16671 probe = argument;
16672 if (TREE_CODE (probe) == SCOPE_REF)
16673 probe = TREE_OPERAND (probe, 1);
16674 if (VAR_P (probe))
16675 {
16676 /* A variable without external linkage might still be a
16677 valid constant-expression, so no error is issued here
16678 if the external-linkage check fails. */
16679 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16680 cp_parser_simulate_error (parser);
16681 }
16682 else if (is_overloaded_fn (argument))
16683 /* All overloaded functions are allowed; if the external
16684 linkage test does not pass, an error will be issued
16685 later. */
16686 ;
16687 else if (address_p
16688 && (TREE_CODE (argument) == OFFSET_REF
16689 || TREE_CODE (argument) == SCOPE_REF))
16690 /* A pointer-to-member. */
16691 ;
16692 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16693 ;
16694 else
16695 cp_parser_simulate_error (parser);
16696
16697 if (cp_parser_parse_definitely (parser))
16698 {
16699 if (address_p)
16700 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16701 tf_warning_or_error);
16702 else
16703 argument = convert_from_reference (argument);
16704 return argument;
16705 }
16706 }
16707 }
16708 /* If the argument started with "&", there are no other valid
16709 alternatives at this point. */
16710 if (address_p)
16711 {
16712 cp_parser_error (parser, "invalid non-type template argument");
16713 return error_mark_node;
16714 }
16715
16716 general_expr:
16717 /* If the argument wasn't successfully parsed as a type-id followed
16718 by '>>', the argument can only be a constant expression now.
16719 Otherwise, we try parsing the constant-expression tentatively,
16720 because the argument could really be a type-id. */
16721 if (maybe_type_id)
16722 cp_parser_parse_tentatively (parser);
16723
16724 if (cxx_dialect <= cxx14)
16725 argument = cp_parser_constant_expression (parser);
16726 else
16727 {
16728 /* With C++17 generalized non-type template arguments we need to handle
16729 lvalue constant expressions, too. */
16730 argument = cp_parser_assignment_expression (parser);
16731 require_potential_constant_expression (argument);
16732 }
16733
16734 if (!maybe_type_id)
16735 return argument;
16736 if (!cp_parser_next_token_ends_template_argument_p (parser))
16737 cp_parser_error (parser, "expected template-argument");
16738 if (cp_parser_parse_definitely (parser))
16739 return argument;
16740 /* We did our best to parse the argument as a non type-id, but that
16741 was the only alternative that matched (albeit with a '>' after
16742 it). We can assume it's just a typo from the user, and a
16743 diagnostic will then be issued. */
16744 return cp_parser_template_type_arg (parser);
16745 }
16746
16747 /* Parse an explicit-instantiation.
16748
16749 explicit-instantiation:
16750 template declaration
16751
16752 Although the standard says `declaration', what it really means is:
16753
16754 explicit-instantiation:
16755 template decl-specifier-seq [opt] declarator [opt] ;
16756
16757 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16758 supposed to be allowed. A defect report has been filed about this
16759 issue.
16760
16761 GNU Extension:
16762
16763 explicit-instantiation:
16764 storage-class-specifier template
16765 decl-specifier-seq [opt] declarator [opt] ;
16766 function-specifier template
16767 decl-specifier-seq [opt] declarator [opt] ; */
16768
16769 static void
16770 cp_parser_explicit_instantiation (cp_parser* parser)
16771 {
16772 int declares_class_or_enum;
16773 cp_decl_specifier_seq decl_specifiers;
16774 tree extension_specifier = NULL_TREE;
16775
16776 timevar_push (TV_TEMPLATE_INST);
16777
16778 /* Look for an (optional) storage-class-specifier or
16779 function-specifier. */
16780 if (cp_parser_allow_gnu_extensions_p (parser))
16781 {
16782 extension_specifier
16783 = cp_parser_storage_class_specifier_opt (parser);
16784 if (!extension_specifier)
16785 extension_specifier
16786 = cp_parser_function_specifier_opt (parser,
16787 /*decl_specs=*/NULL);
16788 }
16789
16790 /* Look for the `template' keyword. */
16791 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16792 /* Let the front end know that we are processing an explicit
16793 instantiation. */
16794 begin_explicit_instantiation ();
16795 /* [temp.explicit] says that we are supposed to ignore access
16796 control while processing explicit instantiation directives. */
16797 push_deferring_access_checks (dk_no_check);
16798 /* Parse a decl-specifier-seq. */
16799 cp_parser_decl_specifier_seq (parser,
16800 CP_PARSER_FLAGS_OPTIONAL,
16801 &decl_specifiers,
16802 &declares_class_or_enum);
16803 /* If there was exactly one decl-specifier, and it declared a class,
16804 and there's no declarator, then we have an explicit type
16805 instantiation. */
16806 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16807 {
16808 tree type;
16809
16810 type = check_tag_decl (&decl_specifiers,
16811 /*explicit_type_instantiation_p=*/true);
16812 /* Turn access control back on for names used during
16813 template instantiation. */
16814 pop_deferring_access_checks ();
16815 if (type)
16816 do_type_instantiation (type, extension_specifier,
16817 /*complain=*/tf_error);
16818 }
16819 else
16820 {
16821 cp_declarator *declarator;
16822 tree decl;
16823
16824 /* Parse the declarator. */
16825 declarator
16826 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16827 /*ctor_dtor_or_conv_p=*/NULL,
16828 /*parenthesized_p=*/NULL,
16829 /*member_p=*/false,
16830 /*friend_p=*/false);
16831 if (declares_class_or_enum & 2)
16832 cp_parser_check_for_definition_in_return_type (declarator,
16833 decl_specifiers.type,
16834 decl_specifiers.locations[ds_type_spec]);
16835 if (declarator != cp_error_declarator)
16836 {
16837 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16838 permerror (decl_specifiers.locations[ds_inline],
16839 "explicit instantiation shall not use"
16840 " %<inline%> specifier");
16841 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16842 permerror (decl_specifiers.locations[ds_constexpr],
16843 "explicit instantiation shall not use"
16844 " %<constexpr%> specifier");
16845
16846 decl = grokdeclarator (declarator, &decl_specifiers,
16847 NORMAL, 0, &decl_specifiers.attributes);
16848 /* Turn access control back on for names used during
16849 template instantiation. */
16850 pop_deferring_access_checks ();
16851 /* Do the explicit instantiation. */
16852 do_decl_instantiation (decl, extension_specifier);
16853 }
16854 else
16855 {
16856 pop_deferring_access_checks ();
16857 /* Skip the body of the explicit instantiation. */
16858 cp_parser_skip_to_end_of_statement (parser);
16859 }
16860 }
16861 /* We're done with the instantiation. */
16862 end_explicit_instantiation ();
16863
16864 cp_parser_consume_semicolon_at_end_of_statement (parser);
16865
16866 timevar_pop (TV_TEMPLATE_INST);
16867 }
16868
16869 /* Parse an explicit-specialization.
16870
16871 explicit-specialization:
16872 template < > declaration
16873
16874 Although the standard says `declaration', what it really means is:
16875
16876 explicit-specialization:
16877 template <> decl-specifier [opt] init-declarator [opt] ;
16878 template <> function-definition
16879 template <> explicit-specialization
16880 template <> template-declaration */
16881
16882 static void
16883 cp_parser_explicit_specialization (cp_parser* parser)
16884 {
16885 bool need_lang_pop;
16886 cp_token *token = cp_lexer_peek_token (parser->lexer);
16887
16888 /* Look for the `template' keyword. */
16889 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16890 /* Look for the `<'. */
16891 cp_parser_require (parser, CPP_LESS, RT_LESS);
16892 /* Look for the `>'. */
16893 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16894 /* We have processed another parameter list. */
16895 ++parser->num_template_parameter_lists;
16896 /* [temp]
16897
16898 A template ... explicit specialization ... shall not have C
16899 linkage. */
16900 if (current_lang_name == lang_name_c)
16901 {
16902 error_at (token->location, "template specialization with C linkage");
16903 maybe_show_extern_c_location ();
16904 /* Give it C++ linkage to avoid confusing other parts of the
16905 front end. */
16906 push_lang_context (lang_name_cplusplus);
16907 need_lang_pop = true;
16908 }
16909 else
16910 need_lang_pop = false;
16911 /* Let the front end know that we are beginning a specialization. */
16912 if (!begin_specialization ())
16913 {
16914 end_specialization ();
16915 return;
16916 }
16917
16918 /* If the next keyword is `template', we need to figure out whether
16919 or not we're looking a template-declaration. */
16920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16921 {
16922 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16923 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16924 cp_parser_template_declaration_after_export (parser,
16925 /*member_p=*/false);
16926 else
16927 cp_parser_explicit_specialization (parser);
16928 }
16929 else
16930 /* Parse the dependent declaration. */
16931 cp_parser_single_declaration (parser,
16932 /*checks=*/NULL,
16933 /*member_p=*/false,
16934 /*explicit_specialization_p=*/true,
16935 /*friend_p=*/NULL);
16936 /* We're done with the specialization. */
16937 end_specialization ();
16938 /* For the erroneous case of a template with C linkage, we pushed an
16939 implicit C++ linkage scope; exit that scope now. */
16940 if (need_lang_pop)
16941 pop_lang_context ();
16942 /* We're done with this parameter list. */
16943 --parser->num_template_parameter_lists;
16944 }
16945
16946 /* Parse a type-specifier.
16947
16948 type-specifier:
16949 simple-type-specifier
16950 class-specifier
16951 enum-specifier
16952 elaborated-type-specifier
16953 cv-qualifier
16954
16955 GNU Extension:
16956
16957 type-specifier:
16958 __complex__
16959
16960 Returns a representation of the type-specifier. For a
16961 class-specifier, enum-specifier, or elaborated-type-specifier, a
16962 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16963
16964 The parser flags FLAGS is used to control type-specifier parsing.
16965
16966 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16967 in a decl-specifier-seq.
16968
16969 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16970 class-specifier, enum-specifier, or elaborated-type-specifier, then
16971 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16972 if a type is declared; 2 if it is defined. Otherwise, it is set to
16973 zero.
16974
16975 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16976 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16977 is set to FALSE. */
16978
16979 static tree
16980 cp_parser_type_specifier (cp_parser* parser,
16981 cp_parser_flags flags,
16982 cp_decl_specifier_seq *decl_specs,
16983 bool is_declaration,
16984 int* declares_class_or_enum,
16985 bool* is_cv_qualifier)
16986 {
16987 tree type_spec = NULL_TREE;
16988 cp_token *token;
16989 enum rid keyword;
16990 cp_decl_spec ds = ds_last;
16991
16992 /* Assume this type-specifier does not declare a new type. */
16993 if (declares_class_or_enum)
16994 *declares_class_or_enum = 0;
16995 /* And that it does not specify a cv-qualifier. */
16996 if (is_cv_qualifier)
16997 *is_cv_qualifier = false;
16998 /* Peek at the next token. */
16999 token = cp_lexer_peek_token (parser->lexer);
17000
17001 /* If we're looking at a keyword, we can use that to guide the
17002 production we choose. */
17003 keyword = token->keyword;
17004 switch (keyword)
17005 {
17006 case RID_ENUM:
17007 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17008 goto elaborated_type_specifier;
17009
17010 /* Look for the enum-specifier. */
17011 type_spec = cp_parser_enum_specifier (parser);
17012 /* If that worked, we're done. */
17013 if (type_spec)
17014 {
17015 if (declares_class_or_enum)
17016 *declares_class_or_enum = 2;
17017 if (decl_specs)
17018 cp_parser_set_decl_spec_type (decl_specs,
17019 type_spec,
17020 token,
17021 /*type_definition_p=*/true);
17022 return type_spec;
17023 }
17024 else
17025 goto elaborated_type_specifier;
17026
17027 /* Any of these indicate either a class-specifier, or an
17028 elaborated-type-specifier. */
17029 case RID_CLASS:
17030 case RID_STRUCT:
17031 case RID_UNION:
17032 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17033 goto elaborated_type_specifier;
17034
17035 /* Parse tentatively so that we can back up if we don't find a
17036 class-specifier. */
17037 cp_parser_parse_tentatively (parser);
17038 /* Look for the class-specifier. */
17039 type_spec = cp_parser_class_specifier (parser);
17040 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17041 /* If that worked, we're done. */
17042 if (cp_parser_parse_definitely (parser))
17043 {
17044 if (declares_class_or_enum)
17045 *declares_class_or_enum = 2;
17046 if (decl_specs)
17047 cp_parser_set_decl_spec_type (decl_specs,
17048 type_spec,
17049 token,
17050 /*type_definition_p=*/true);
17051 return type_spec;
17052 }
17053
17054 /* Fall through. */
17055 elaborated_type_specifier:
17056 /* We're declaring (not defining) a class or enum. */
17057 if (declares_class_or_enum)
17058 *declares_class_or_enum = 1;
17059
17060 /* Fall through. */
17061 case RID_TYPENAME:
17062 /* Look for an elaborated-type-specifier. */
17063 type_spec
17064 = (cp_parser_elaborated_type_specifier
17065 (parser,
17066 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17067 is_declaration));
17068 if (decl_specs)
17069 cp_parser_set_decl_spec_type (decl_specs,
17070 type_spec,
17071 token,
17072 /*type_definition_p=*/false);
17073 return type_spec;
17074
17075 case RID_CONST:
17076 ds = ds_const;
17077 if (is_cv_qualifier)
17078 *is_cv_qualifier = true;
17079 break;
17080
17081 case RID_VOLATILE:
17082 ds = ds_volatile;
17083 if (is_cv_qualifier)
17084 *is_cv_qualifier = true;
17085 break;
17086
17087 case RID_RESTRICT:
17088 ds = ds_restrict;
17089 if (is_cv_qualifier)
17090 *is_cv_qualifier = true;
17091 break;
17092
17093 case RID_COMPLEX:
17094 /* The `__complex__' keyword is a GNU extension. */
17095 ds = ds_complex;
17096 break;
17097
17098 default:
17099 break;
17100 }
17101
17102 /* Handle simple keywords. */
17103 if (ds != ds_last)
17104 {
17105 if (decl_specs)
17106 {
17107 set_and_check_decl_spec_loc (decl_specs, ds, token);
17108 decl_specs->any_specifiers_p = true;
17109 }
17110 return cp_lexer_consume_token (parser->lexer)->u.value;
17111 }
17112
17113 /* If we do not already have a type-specifier, assume we are looking
17114 at a simple-type-specifier. */
17115 type_spec = cp_parser_simple_type_specifier (parser,
17116 decl_specs,
17117 flags);
17118
17119 /* If we didn't find a type-specifier, and a type-specifier was not
17120 optional in this context, issue an error message. */
17121 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17122 {
17123 cp_parser_error (parser, "expected type specifier");
17124 return error_mark_node;
17125 }
17126
17127 return type_spec;
17128 }
17129
17130 /* Parse a simple-type-specifier.
17131
17132 simple-type-specifier:
17133 :: [opt] nested-name-specifier [opt] type-name
17134 :: [opt] nested-name-specifier template template-id
17135 char
17136 wchar_t
17137 bool
17138 short
17139 int
17140 long
17141 signed
17142 unsigned
17143 float
17144 double
17145 void
17146
17147 C++11 Extension:
17148
17149 simple-type-specifier:
17150 auto
17151 decltype ( expression )
17152 char16_t
17153 char32_t
17154 __underlying_type ( type-id )
17155
17156 C++17 extension:
17157
17158 nested-name-specifier(opt) template-name
17159
17160 GNU Extension:
17161
17162 simple-type-specifier:
17163 __int128
17164 __typeof__ unary-expression
17165 __typeof__ ( type-id )
17166 __typeof__ ( type-id ) { initializer-list , [opt] }
17167
17168 Concepts Extension:
17169
17170 simple-type-specifier:
17171 constrained-type-specifier
17172
17173 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17174 appropriately updated. */
17175
17176 static tree
17177 cp_parser_simple_type_specifier (cp_parser* parser,
17178 cp_decl_specifier_seq *decl_specs,
17179 cp_parser_flags flags)
17180 {
17181 tree type = NULL_TREE;
17182 cp_token *token;
17183 int idx;
17184
17185 /* Peek at the next token. */
17186 token = cp_lexer_peek_token (parser->lexer);
17187
17188 /* If we're looking at a keyword, things are easy. */
17189 switch (token->keyword)
17190 {
17191 case RID_CHAR:
17192 if (decl_specs)
17193 decl_specs->explicit_char_p = true;
17194 type = char_type_node;
17195 break;
17196 case RID_CHAR16:
17197 type = char16_type_node;
17198 break;
17199 case RID_CHAR32:
17200 type = char32_type_node;
17201 break;
17202 case RID_WCHAR:
17203 type = wchar_type_node;
17204 break;
17205 case RID_BOOL:
17206 type = boolean_type_node;
17207 break;
17208 case RID_SHORT:
17209 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17210 type = short_integer_type_node;
17211 break;
17212 case RID_INT:
17213 if (decl_specs)
17214 decl_specs->explicit_int_p = true;
17215 type = integer_type_node;
17216 break;
17217 case RID_INT_N_0:
17218 case RID_INT_N_1:
17219 case RID_INT_N_2:
17220 case RID_INT_N_3:
17221 idx = token->keyword - RID_INT_N_0;
17222 if (! int_n_enabled_p [idx])
17223 break;
17224 if (decl_specs)
17225 {
17226 decl_specs->explicit_intN_p = true;
17227 decl_specs->int_n_idx = idx;
17228 }
17229 type = int_n_trees [idx].signed_type;
17230 break;
17231 case RID_LONG:
17232 if (decl_specs)
17233 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17234 type = long_integer_type_node;
17235 break;
17236 case RID_SIGNED:
17237 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17238 type = integer_type_node;
17239 break;
17240 case RID_UNSIGNED:
17241 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17242 type = unsigned_type_node;
17243 break;
17244 case RID_FLOAT:
17245 type = float_type_node;
17246 break;
17247 case RID_DOUBLE:
17248 type = double_type_node;
17249 break;
17250 case RID_VOID:
17251 type = void_type_node;
17252 break;
17253
17254 case RID_AUTO:
17255 maybe_warn_cpp0x (CPP0X_AUTO);
17256 if (parser->auto_is_implicit_function_template_parm_p)
17257 {
17258 /* The 'auto' might be the placeholder return type for a function decl
17259 with trailing return type. */
17260 bool have_trailing_return_fn_decl = false;
17261
17262 cp_parser_parse_tentatively (parser);
17263 cp_lexer_consume_token (parser->lexer);
17264 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17265 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17266 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17267 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17268 {
17269 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17270 {
17271 cp_lexer_consume_token (parser->lexer);
17272 cp_parser_skip_to_closing_parenthesis (parser,
17273 /*recovering*/false,
17274 /*or_comma*/false,
17275 /*consume_paren*/true);
17276 continue;
17277 }
17278
17279 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17280 {
17281 have_trailing_return_fn_decl = true;
17282 break;
17283 }
17284
17285 cp_lexer_consume_token (parser->lexer);
17286 }
17287 cp_parser_abort_tentative_parse (parser);
17288
17289 if (have_trailing_return_fn_decl)
17290 {
17291 type = make_auto ();
17292 break;
17293 }
17294
17295 if (cxx_dialect >= cxx14)
17296 {
17297 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17298 type = TREE_TYPE (type);
17299 }
17300 else
17301 type = error_mark_node;
17302
17303 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17304 {
17305 if (cxx_dialect < cxx14)
17306 error_at (token->location,
17307 "use of %<auto%> in lambda parameter declaration "
17308 "only available with "
17309 "-std=c++14 or -std=gnu++14");
17310 }
17311 else if (cxx_dialect < cxx14)
17312 error_at (token->location,
17313 "use of %<auto%> in parameter declaration "
17314 "only available with "
17315 "-std=c++14 or -std=gnu++14");
17316 else if (!flag_concepts)
17317 pedwarn (token->location, 0,
17318 "use of %<auto%> in parameter declaration "
17319 "only available with -fconcepts");
17320 }
17321 else
17322 type = make_auto ();
17323 break;
17324
17325 case RID_DECLTYPE:
17326 /* Since DR 743, decltype can either be a simple-type-specifier by
17327 itself or begin a nested-name-specifier. Parsing it will replace
17328 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17329 handling below decide what to do. */
17330 cp_parser_decltype (parser);
17331 cp_lexer_set_token_position (parser->lexer, token);
17332 break;
17333
17334 case RID_TYPEOF:
17335 /* Consume the `typeof' token. */
17336 cp_lexer_consume_token (parser->lexer);
17337 /* Parse the operand to `typeof'. */
17338 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17339 /* If it is not already a TYPE, take its type. */
17340 if (!TYPE_P (type))
17341 type = finish_typeof (type);
17342
17343 if (decl_specs)
17344 cp_parser_set_decl_spec_type (decl_specs, type,
17345 token,
17346 /*type_definition_p=*/false);
17347
17348 return type;
17349
17350 case RID_UNDERLYING_TYPE:
17351 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17352 if (decl_specs)
17353 cp_parser_set_decl_spec_type (decl_specs, type,
17354 token,
17355 /*type_definition_p=*/false);
17356
17357 return type;
17358
17359 case RID_BASES:
17360 case RID_DIRECT_BASES:
17361 type = cp_parser_trait_expr (parser, token->keyword);
17362 if (decl_specs)
17363 cp_parser_set_decl_spec_type (decl_specs, type,
17364 token,
17365 /*type_definition_p=*/false);
17366 return type;
17367 default:
17368 break;
17369 }
17370
17371 /* If token is an already-parsed decltype not followed by ::,
17372 it's a simple-type-specifier. */
17373 if (token->type == CPP_DECLTYPE
17374 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17375 {
17376 type = saved_checks_value (token->u.tree_check_value);
17377 if (decl_specs)
17378 {
17379 cp_parser_set_decl_spec_type (decl_specs, type,
17380 token,
17381 /*type_definition_p=*/false);
17382 /* Remember that we are handling a decltype in order to
17383 implement the resolution of DR 1510 when the argument
17384 isn't instantiation dependent. */
17385 decl_specs->decltype_p = true;
17386 }
17387 cp_lexer_consume_token (parser->lexer);
17388 return type;
17389 }
17390
17391 /* If the type-specifier was for a built-in type, we're done. */
17392 if (type)
17393 {
17394 /* Record the type. */
17395 if (decl_specs
17396 && (token->keyword != RID_SIGNED
17397 && token->keyword != RID_UNSIGNED
17398 && token->keyword != RID_SHORT
17399 && token->keyword != RID_LONG))
17400 cp_parser_set_decl_spec_type (decl_specs,
17401 type,
17402 token,
17403 /*type_definition_p=*/false);
17404 if (decl_specs)
17405 decl_specs->any_specifiers_p = true;
17406
17407 /* Consume the token. */
17408 cp_lexer_consume_token (parser->lexer);
17409
17410 if (type == error_mark_node)
17411 return error_mark_node;
17412
17413 /* There is no valid C++ program where a non-template type is
17414 followed by a "<". That usually indicates that the user thought
17415 that the type was a template. */
17416 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17417 token->location);
17418
17419 return TYPE_NAME (type);
17420 }
17421
17422 /* The type-specifier must be a user-defined type. */
17423 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17424 {
17425 bool qualified_p;
17426 bool global_p;
17427
17428 /* Don't gobble tokens or issue error messages if this is an
17429 optional type-specifier. */
17430 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17431 cp_parser_parse_tentatively (parser);
17432
17433 token = cp_lexer_peek_token (parser->lexer);
17434
17435 /* Look for the optional `::' operator. */
17436 global_p
17437 = (cp_parser_global_scope_opt (parser,
17438 /*current_scope_valid_p=*/false)
17439 != NULL_TREE);
17440 /* Look for the nested-name specifier. */
17441 qualified_p
17442 = (cp_parser_nested_name_specifier_opt (parser,
17443 /*typename_keyword_p=*/false,
17444 /*check_dependency_p=*/true,
17445 /*type_p=*/false,
17446 /*is_declaration=*/false)
17447 != NULL_TREE);
17448 /* If we have seen a nested-name-specifier, and the next token
17449 is `template', then we are using the template-id production. */
17450 if (parser->scope
17451 && cp_parser_optional_template_keyword (parser))
17452 {
17453 /* Look for the template-id. */
17454 type = cp_parser_template_id (parser,
17455 /*template_keyword_p=*/true,
17456 /*check_dependency_p=*/true,
17457 none_type,
17458 /*is_declaration=*/false);
17459 /* If the template-id did not name a type, we are out of
17460 luck. */
17461 if (TREE_CODE (type) != TYPE_DECL)
17462 {
17463 cp_parser_error (parser, "expected template-id for type");
17464 type = NULL_TREE;
17465 }
17466 }
17467 /* Otherwise, look for a type-name. */
17468 else
17469 type = cp_parser_type_name (parser);
17470 /* Keep track of all name-lookups performed in class scopes. */
17471 if (type
17472 && !global_p
17473 && !qualified_p
17474 && TREE_CODE (type) == TYPE_DECL
17475 && identifier_p (DECL_NAME (type)))
17476 maybe_note_name_used_in_class (DECL_NAME (type), type);
17477 /* If it didn't work out, we don't have a TYPE. */
17478 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17479 && !cp_parser_parse_definitely (parser))
17480 type = NULL_TREE;
17481 if (!type && cxx_dialect >= cxx17)
17482 {
17483 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17484 cp_parser_parse_tentatively (parser);
17485
17486 cp_parser_global_scope_opt (parser,
17487 /*current_scope_valid_p=*/false);
17488 cp_parser_nested_name_specifier_opt (parser,
17489 /*typename_keyword_p=*/false,
17490 /*check_dependency_p=*/true,
17491 /*type_p=*/false,
17492 /*is_declaration=*/false);
17493 tree name = cp_parser_identifier (parser);
17494 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17495 && parser->scope != error_mark_node)
17496 {
17497 tree tmpl = cp_parser_lookup_name (parser, name,
17498 none_type,
17499 /*is_template=*/false,
17500 /*is_namespace=*/false,
17501 /*check_dependency=*/true,
17502 /*ambiguous_decls=*/NULL,
17503 token->location);
17504 if (tmpl && tmpl != error_mark_node
17505 && (DECL_CLASS_TEMPLATE_P (tmpl)
17506 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17507 type = make_template_placeholder (tmpl);
17508 else
17509 {
17510 type = error_mark_node;
17511 if (!cp_parser_simulate_error (parser))
17512 cp_parser_name_lookup_error (parser, name, tmpl,
17513 NLE_TYPE, token->location);
17514 }
17515 }
17516 else
17517 type = error_mark_node;
17518
17519 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17520 && !cp_parser_parse_definitely (parser))
17521 type = NULL_TREE;
17522 }
17523 if (type && decl_specs)
17524 cp_parser_set_decl_spec_type (decl_specs, type,
17525 token,
17526 /*type_definition_p=*/false);
17527 }
17528
17529 /* If we didn't get a type-name, issue an error message. */
17530 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17531 {
17532 cp_parser_error (parser, "expected type-name");
17533 return error_mark_node;
17534 }
17535
17536 if (type && type != error_mark_node)
17537 {
17538 /* See if TYPE is an Objective-C type, and if so, parse and
17539 accept any protocol references following it. Do this before
17540 the cp_parser_check_for_invalid_template_id() call, because
17541 Objective-C types can be followed by '<...>' which would
17542 enclose protocol names rather than template arguments, and so
17543 everything is fine. */
17544 if (c_dialect_objc () && !parser->scope
17545 && (objc_is_id (type) || objc_is_class_name (type)))
17546 {
17547 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17548 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17549
17550 /* Clobber the "unqualified" type previously entered into
17551 DECL_SPECS with the new, improved protocol-qualified version. */
17552 if (decl_specs)
17553 decl_specs->type = qual_type;
17554
17555 return qual_type;
17556 }
17557
17558 /* There is no valid C++ program where a non-template type is
17559 followed by a "<". That usually indicates that the user
17560 thought that the type was a template. */
17561 cp_parser_check_for_invalid_template_id (parser, type,
17562 none_type,
17563 token->location);
17564 }
17565
17566 return type;
17567 }
17568
17569 /* Parse a type-name.
17570
17571 type-name:
17572 class-name
17573 enum-name
17574 typedef-name
17575 simple-template-id [in c++0x]
17576
17577 enum-name:
17578 identifier
17579
17580 typedef-name:
17581 identifier
17582
17583 Concepts:
17584
17585 type-name:
17586 concept-name
17587 partial-concept-id
17588
17589 concept-name:
17590 identifier
17591
17592 Returns a TYPE_DECL for the type. */
17593
17594 static tree
17595 cp_parser_type_name (cp_parser* parser)
17596 {
17597 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17598 }
17599
17600 /* See above. */
17601 static tree
17602 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17603 {
17604 tree type_decl;
17605
17606 /* We can't know yet whether it is a class-name or not. */
17607 cp_parser_parse_tentatively (parser);
17608 /* Try a class-name. */
17609 type_decl = cp_parser_class_name (parser,
17610 typename_keyword_p,
17611 /*template_keyword_p=*/false,
17612 none_type,
17613 /*check_dependency_p=*/true,
17614 /*class_head_p=*/false,
17615 /*is_declaration=*/false);
17616 /* If it's not a class-name, keep looking. */
17617 if (!cp_parser_parse_definitely (parser))
17618 {
17619 if (cxx_dialect < cxx11)
17620 /* It must be a typedef-name or an enum-name. */
17621 return cp_parser_nonclass_name (parser);
17622
17623 cp_parser_parse_tentatively (parser);
17624 /* It is either a simple-template-id representing an
17625 instantiation of an alias template... */
17626 type_decl = cp_parser_template_id (parser,
17627 /*template_keyword_p=*/false,
17628 /*check_dependency_p=*/true,
17629 none_type,
17630 /*is_declaration=*/false);
17631 /* Note that this must be an instantiation of an alias template
17632 because [temp.names]/6 says:
17633
17634 A template-id that names an alias template specialization
17635 is a type-name.
17636
17637 Whereas [temp.names]/7 says:
17638
17639 A simple-template-id that names a class template
17640 specialization is a class-name.
17641
17642 With concepts, this could also be a partial-concept-id that
17643 declares a non-type template parameter. */
17644 if (type_decl != NULL_TREE
17645 && TREE_CODE (type_decl) == TYPE_DECL
17646 && TYPE_DECL_ALIAS_P (type_decl))
17647 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17648 else if (is_constrained_parameter (type_decl))
17649 /* Don't do anything. */ ;
17650 else
17651 cp_parser_simulate_error (parser);
17652
17653 if (!cp_parser_parse_definitely (parser))
17654 /* ... Or a typedef-name or an enum-name. */
17655 return cp_parser_nonclass_name (parser);
17656 }
17657
17658 return type_decl;
17659 }
17660
17661 /* Check if DECL and ARGS can form a constrained-type-specifier.
17662 If ARGS is non-null, we try to form a concept check of the
17663 form DECL<?, ARGS> where ? is a wildcard that matches any
17664 kind of template argument. If ARGS is NULL, then we try to
17665 form a concept check of the form DECL<?>. */
17666
17667 static tree
17668 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17669 tree decl, tree args)
17670 {
17671 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17672
17673 /* If we a constrained-type-specifier cannot be deduced. */
17674 if (parser->prevent_constrained_type_specifiers)
17675 return NULL_TREE;
17676
17677 /* A constrained type specifier can only be found in an
17678 overload set or as a reference to a template declaration.
17679
17680 FIXME: This might be masking a bug. It's possible that
17681 that the deduction below is causing template specializations
17682 to be formed with the wildcard as an argument. */
17683 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17684 return NULL_TREE;
17685
17686 /* Try to build a call expression that evaluates the
17687 concept. This can fail if the overload set refers
17688 only to non-templates. */
17689 tree placeholder = build_nt (WILDCARD_DECL);
17690 tree check = build_concept_check (decl, placeholder, args);
17691 if (check == error_mark_node)
17692 return NULL_TREE;
17693
17694 /* Deduce the checked constraint and the prototype parameter.
17695
17696 FIXME: In certain cases, failure to deduce should be a
17697 diagnosable error. */
17698 tree conc;
17699 tree proto;
17700 if (!deduce_constrained_parameter (check, conc, proto))
17701 return NULL_TREE;
17702
17703 /* In template parameter scope, this results in a constrained
17704 parameter. Return a descriptor of that parm. */
17705 if (processing_template_parmlist)
17706 return build_constrained_parameter (conc, proto, args);
17707
17708 /* In a parameter-declaration-clause, constrained-type
17709 specifiers result in invented template parameters. */
17710 if (parser->auto_is_implicit_function_template_parm_p)
17711 {
17712 tree x = build_constrained_parameter (conc, proto, args);
17713 return synthesize_implicit_template_parm (parser, x);
17714 }
17715 else
17716 {
17717 /* Otherwise, we're in a context where the constrained
17718 type name is deduced and the constraint applies
17719 after deduction. */
17720 return make_constrained_auto (conc, args);
17721 }
17722
17723 return NULL_TREE;
17724 }
17725
17726 /* If DECL refers to a concept, return a TYPE_DECL representing
17727 the result of using the constrained type specifier in the
17728 current context. DECL refers to a concept if
17729
17730 - it is an overload set containing a function concept taking a single
17731 type argument, or
17732
17733 - it is a variable concept taking a single type argument. */
17734
17735 static tree
17736 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17737 {
17738 if (flag_concepts
17739 && (TREE_CODE (decl) == OVERLOAD
17740 || BASELINK_P (decl)
17741 || variable_concept_p (decl)))
17742 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17743 else
17744 return NULL_TREE;
17745 }
17746
17747 /* Check if DECL and ARGS form a partial-concept-id. If so,
17748 assign ID to the resulting constrained placeholder.
17749
17750 Returns true if the partial-concept-id designates a placeholder
17751 and false otherwise. Note that *id is set to NULL_TREE in
17752 this case. */
17753
17754 static tree
17755 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17756 {
17757 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17758 }
17759
17760 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17761 or a concept-name.
17762
17763 enum-name:
17764 identifier
17765
17766 typedef-name:
17767 identifier
17768
17769 concept-name:
17770 identifier
17771
17772 Returns a TYPE_DECL for the type. */
17773
17774 static tree
17775 cp_parser_nonclass_name (cp_parser* parser)
17776 {
17777 tree type_decl;
17778 tree identifier;
17779
17780 cp_token *token = cp_lexer_peek_token (parser->lexer);
17781 identifier = cp_parser_identifier (parser);
17782 if (identifier == error_mark_node)
17783 return error_mark_node;
17784
17785 /* Look up the type-name. */
17786 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17787
17788 type_decl = strip_using_decl (type_decl);
17789
17790 /* If we found an overload set, then it may refer to a concept-name. */
17791 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17792 type_decl = decl;
17793
17794 if (TREE_CODE (type_decl) != TYPE_DECL
17795 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17796 {
17797 /* See if this is an Objective-C type. */
17798 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17799 tree type = objc_get_protocol_qualified_type (identifier, protos);
17800 if (type)
17801 type_decl = TYPE_NAME (type);
17802 }
17803
17804 /* Issue an error if we did not find a type-name. */
17805 if (TREE_CODE (type_decl) != TYPE_DECL
17806 /* In Objective-C, we have the complication that class names are
17807 normally type names and start declarations (eg, the
17808 "NSObject" in "NSObject *object;"), but can be used in an
17809 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17810 is an expression. So, a classname followed by a dot is not a
17811 valid type-name. */
17812 || (objc_is_class_name (TREE_TYPE (type_decl))
17813 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17814 {
17815 if (!cp_parser_simulate_error (parser))
17816 cp_parser_name_lookup_error (parser, identifier, type_decl,
17817 NLE_TYPE, token->location);
17818 return error_mark_node;
17819 }
17820 /* Remember that the name was used in the definition of the
17821 current class so that we can check later to see if the
17822 meaning would have been different after the class was
17823 entirely defined. */
17824 else if (type_decl != error_mark_node
17825 && !parser->scope)
17826 maybe_note_name_used_in_class (identifier, type_decl);
17827
17828 return type_decl;
17829 }
17830
17831 /* Parse an elaborated-type-specifier. Note that the grammar given
17832 here incorporates the resolution to DR68.
17833
17834 elaborated-type-specifier:
17835 class-key :: [opt] nested-name-specifier [opt] identifier
17836 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17837 enum-key :: [opt] nested-name-specifier [opt] identifier
17838 typename :: [opt] nested-name-specifier identifier
17839 typename :: [opt] nested-name-specifier template [opt]
17840 template-id
17841
17842 GNU extension:
17843
17844 elaborated-type-specifier:
17845 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17846 class-key attributes :: [opt] nested-name-specifier [opt]
17847 template [opt] template-id
17848 enum attributes :: [opt] nested-name-specifier [opt] identifier
17849
17850 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17851 declared `friend'. If IS_DECLARATION is TRUE, then this
17852 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17853 something is being declared.
17854
17855 Returns the TYPE specified. */
17856
17857 static tree
17858 cp_parser_elaborated_type_specifier (cp_parser* parser,
17859 bool is_friend,
17860 bool is_declaration)
17861 {
17862 enum tag_types tag_type;
17863 tree identifier;
17864 tree type = NULL_TREE;
17865 tree attributes = NULL_TREE;
17866 tree globalscope;
17867 cp_token *token = NULL;
17868
17869 /* See if we're looking at the `enum' keyword. */
17870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17871 {
17872 /* Consume the `enum' token. */
17873 cp_lexer_consume_token (parser->lexer);
17874 /* Remember that it's an enumeration type. */
17875 tag_type = enum_type;
17876 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17877 enums) is used here. */
17878 cp_token *token = cp_lexer_peek_token (parser->lexer);
17879 if (cp_parser_is_keyword (token, RID_CLASS)
17880 || cp_parser_is_keyword (token, RID_STRUCT))
17881 {
17882 gcc_rich_location richloc (token->location);
17883 richloc.add_range (input_location);
17884 richloc.add_fixit_remove ();
17885 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17886 "a scoped enum must not use the %qD keyword",
17887 token->u.value);
17888 /* Consume the `struct' or `class' and parse it anyway. */
17889 cp_lexer_consume_token (parser->lexer);
17890 }
17891 /* Parse the attributes. */
17892 attributes = cp_parser_attributes_opt (parser);
17893 }
17894 /* Or, it might be `typename'. */
17895 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17896 RID_TYPENAME))
17897 {
17898 /* Consume the `typename' token. */
17899 cp_lexer_consume_token (parser->lexer);
17900 /* Remember that it's a `typename' type. */
17901 tag_type = typename_type;
17902 }
17903 /* Otherwise it must be a class-key. */
17904 else
17905 {
17906 tag_type = cp_parser_class_key (parser);
17907 if (tag_type == none_type)
17908 return error_mark_node;
17909 /* Parse the attributes. */
17910 attributes = cp_parser_attributes_opt (parser);
17911 }
17912
17913 /* Look for the `::' operator. */
17914 globalscope = cp_parser_global_scope_opt (parser,
17915 /*current_scope_valid_p=*/false);
17916 /* Look for the nested-name-specifier. */
17917 tree nested_name_specifier;
17918 if (tag_type == typename_type && !globalscope)
17919 {
17920 nested_name_specifier
17921 = cp_parser_nested_name_specifier (parser,
17922 /*typename_keyword_p=*/true,
17923 /*check_dependency_p=*/true,
17924 /*type_p=*/true,
17925 is_declaration);
17926 if (!nested_name_specifier)
17927 return error_mark_node;
17928 }
17929 else
17930 /* Even though `typename' is not present, the proposed resolution
17931 to Core Issue 180 says that in `class A<T>::B', `B' should be
17932 considered a type-name, even if `A<T>' is dependent. */
17933 nested_name_specifier
17934 = cp_parser_nested_name_specifier_opt (parser,
17935 /*typename_keyword_p=*/true,
17936 /*check_dependency_p=*/true,
17937 /*type_p=*/true,
17938 is_declaration);
17939 /* For everything but enumeration types, consider a template-id.
17940 For an enumeration type, consider only a plain identifier. */
17941 if (tag_type != enum_type)
17942 {
17943 bool template_p = false;
17944 tree decl;
17945
17946 /* Allow the `template' keyword. */
17947 template_p = cp_parser_optional_template_keyword (parser);
17948 /* If we didn't see `template', we don't know if there's a
17949 template-id or not. */
17950 if (!template_p)
17951 cp_parser_parse_tentatively (parser);
17952 /* Parse the template-id. */
17953 token = cp_lexer_peek_token (parser->lexer);
17954 decl = cp_parser_template_id (parser, template_p,
17955 /*check_dependency_p=*/true,
17956 tag_type,
17957 is_declaration);
17958 /* If we didn't find a template-id, look for an ordinary
17959 identifier. */
17960 if (!template_p && !cp_parser_parse_definitely (parser))
17961 ;
17962 /* We can get here when cp_parser_template_id, called by
17963 cp_parser_class_name with tag_type == none_type, succeeds
17964 and caches a BASELINK. Then, when called again here,
17965 instead of failing and returning an error_mark_node
17966 returns it (see template/typename17.C in C++11).
17967 ??? Could we diagnose this earlier? */
17968 else if (tag_type == typename_type && BASELINK_P (decl))
17969 {
17970 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17971 type = error_mark_node;
17972 }
17973 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17974 in effect, then we must assume that, upon instantiation, the
17975 template will correspond to a class. */
17976 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17977 && tag_type == typename_type)
17978 type = make_typename_type (parser->scope, decl,
17979 typename_type,
17980 /*complain=*/tf_error);
17981 /* If the `typename' keyword is in effect and DECL is not a type
17982 decl, then type is non existent. */
17983 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17984 ;
17985 else if (TREE_CODE (decl) == TYPE_DECL)
17986 {
17987 type = check_elaborated_type_specifier (tag_type, decl,
17988 /*allow_template_p=*/true);
17989
17990 /* If the next token is a semicolon, this must be a specialization,
17991 instantiation, or friend declaration. Check the scope while we
17992 still know whether or not we had a nested-name-specifier. */
17993 if (type != error_mark_node
17994 && !nested_name_specifier && !is_friend
17995 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17996 check_unqualified_spec_or_inst (type, token->location);
17997 }
17998 else if (decl == error_mark_node)
17999 type = error_mark_node;
18000 }
18001
18002 if (!type)
18003 {
18004 token = cp_lexer_peek_token (parser->lexer);
18005 identifier = cp_parser_identifier (parser);
18006
18007 if (identifier == error_mark_node)
18008 {
18009 parser->scope = NULL_TREE;
18010 return error_mark_node;
18011 }
18012
18013 /* For a `typename', we needn't call xref_tag. */
18014 if (tag_type == typename_type
18015 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18016 return cp_parser_make_typename_type (parser, identifier,
18017 token->location);
18018
18019 /* Template parameter lists apply only if we are not within a
18020 function parameter list. */
18021 bool template_parm_lists_apply
18022 = parser->num_template_parameter_lists;
18023 if (template_parm_lists_apply)
18024 for (cp_binding_level *s = current_binding_level;
18025 s && s->kind != sk_template_parms;
18026 s = s->level_chain)
18027 if (s->kind == sk_function_parms)
18028 template_parm_lists_apply = false;
18029
18030 /* Look up a qualified name in the usual way. */
18031 if (parser->scope)
18032 {
18033 tree decl;
18034 tree ambiguous_decls;
18035
18036 decl = cp_parser_lookup_name (parser, identifier,
18037 tag_type,
18038 /*is_template=*/false,
18039 /*is_namespace=*/false,
18040 /*check_dependency=*/true,
18041 &ambiguous_decls,
18042 token->location);
18043
18044 /* If the lookup was ambiguous, an error will already have been
18045 issued. */
18046 if (ambiguous_decls)
18047 return error_mark_node;
18048
18049 /* If we are parsing friend declaration, DECL may be a
18050 TEMPLATE_DECL tree node here. However, we need to check
18051 whether this TEMPLATE_DECL results in valid code. Consider
18052 the following example:
18053
18054 namespace N {
18055 template <class T> class C {};
18056 }
18057 class X {
18058 template <class T> friend class N::C; // #1, valid code
18059 };
18060 template <class T> class Y {
18061 friend class N::C; // #2, invalid code
18062 };
18063
18064 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18065 name lookup of `N::C'. We see that friend declaration must
18066 be template for the code to be valid. Note that
18067 processing_template_decl does not work here since it is
18068 always 1 for the above two cases. */
18069
18070 decl = (cp_parser_maybe_treat_template_as_class
18071 (decl, /*tag_name_p=*/is_friend
18072 && template_parm_lists_apply));
18073
18074 if (TREE_CODE (decl) != TYPE_DECL)
18075 {
18076 cp_parser_diagnose_invalid_type_name (parser,
18077 identifier,
18078 token->location);
18079 return error_mark_node;
18080 }
18081
18082 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18083 {
18084 bool allow_template = (template_parm_lists_apply
18085 || DECL_SELF_REFERENCE_P (decl));
18086 type = check_elaborated_type_specifier (tag_type, decl,
18087 allow_template);
18088
18089 if (type == error_mark_node)
18090 return error_mark_node;
18091 }
18092
18093 /* Forward declarations of nested types, such as
18094
18095 class C1::C2;
18096 class C1::C2::C3;
18097
18098 are invalid unless all components preceding the final '::'
18099 are complete. If all enclosing types are complete, these
18100 declarations become merely pointless.
18101
18102 Invalid forward declarations of nested types are errors
18103 caught elsewhere in parsing. Those that are pointless arrive
18104 here. */
18105
18106 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18107 && !is_friend && !processing_explicit_instantiation)
18108 warning (0, "declaration %qD does not declare anything", decl);
18109
18110 type = TREE_TYPE (decl);
18111 }
18112 else
18113 {
18114 /* An elaborated-type-specifier sometimes introduces a new type and
18115 sometimes names an existing type. Normally, the rule is that it
18116 introduces a new type only if there is not an existing type of
18117 the same name already in scope. For example, given:
18118
18119 struct S {};
18120 void f() { struct S s; }
18121
18122 the `struct S' in the body of `f' is the same `struct S' as in
18123 the global scope; the existing definition is used. However, if
18124 there were no global declaration, this would introduce a new
18125 local class named `S'.
18126
18127 An exception to this rule applies to the following code:
18128
18129 namespace N { struct S; }
18130
18131 Here, the elaborated-type-specifier names a new type
18132 unconditionally; even if there is already an `S' in the
18133 containing scope this declaration names a new type.
18134 This exception only applies if the elaborated-type-specifier
18135 forms the complete declaration:
18136
18137 [class.name]
18138
18139 A declaration consisting solely of `class-key identifier ;' is
18140 either a redeclaration of the name in the current scope or a
18141 forward declaration of the identifier as a class name. It
18142 introduces the name into the current scope.
18143
18144 We are in this situation precisely when the next token is a `;'.
18145
18146 An exception to the exception is that a `friend' declaration does
18147 *not* name a new type; i.e., given:
18148
18149 struct S { friend struct T; };
18150
18151 `T' is not a new type in the scope of `S'.
18152
18153 Also, `new struct S' or `sizeof (struct S)' never results in the
18154 definition of a new type; a new type can only be declared in a
18155 declaration context. */
18156
18157 tag_scope ts;
18158 bool template_p;
18159
18160 if (is_friend)
18161 /* Friends have special name lookup rules. */
18162 ts = ts_within_enclosing_non_class;
18163 else if (is_declaration
18164 && cp_lexer_next_token_is (parser->lexer,
18165 CPP_SEMICOLON))
18166 /* This is a `class-key identifier ;' */
18167 ts = ts_current;
18168 else
18169 ts = ts_global;
18170
18171 template_p =
18172 (template_parm_lists_apply
18173 && (cp_parser_next_token_starts_class_definition_p (parser)
18174 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18175 /* An unqualified name was used to reference this type, so
18176 there were no qualifying templates. */
18177 if (template_parm_lists_apply
18178 && !cp_parser_check_template_parameters (parser,
18179 /*num_templates=*/0,
18180 /*template_id*/false,
18181 token->location,
18182 /*declarator=*/NULL))
18183 return error_mark_node;
18184 type = xref_tag (tag_type, identifier, ts, template_p);
18185 }
18186 }
18187
18188 if (type == error_mark_node)
18189 return error_mark_node;
18190
18191 /* Allow attributes on forward declarations of classes. */
18192 if (attributes)
18193 {
18194 if (TREE_CODE (type) == TYPENAME_TYPE)
18195 warning (OPT_Wattributes,
18196 "attributes ignored on uninstantiated type");
18197 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18198 && ! processing_explicit_instantiation)
18199 warning (OPT_Wattributes,
18200 "attributes ignored on template instantiation");
18201 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18202 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18203 else
18204 warning (OPT_Wattributes,
18205 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18206 }
18207
18208 if (tag_type != enum_type)
18209 {
18210 /* Indicate whether this class was declared as a `class' or as a
18211 `struct'. */
18212 if (CLASS_TYPE_P (type))
18213 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18214 cp_parser_check_class_key (tag_type, type);
18215 }
18216
18217 /* A "<" cannot follow an elaborated type specifier. If that
18218 happens, the user was probably trying to form a template-id. */
18219 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18220 token->location);
18221
18222 return type;
18223 }
18224
18225 /* Parse an enum-specifier.
18226
18227 enum-specifier:
18228 enum-head { enumerator-list [opt] }
18229 enum-head { enumerator-list , } [C++0x]
18230
18231 enum-head:
18232 enum-key identifier [opt] enum-base [opt]
18233 enum-key nested-name-specifier identifier enum-base [opt]
18234
18235 enum-key:
18236 enum
18237 enum class [C++0x]
18238 enum struct [C++0x]
18239
18240 enum-base: [C++0x]
18241 : type-specifier-seq
18242
18243 opaque-enum-specifier:
18244 enum-key identifier enum-base [opt] ;
18245
18246 GNU Extensions:
18247 enum-key attributes[opt] identifier [opt] enum-base [opt]
18248 { enumerator-list [opt] }attributes[opt]
18249 enum-key attributes[opt] identifier [opt] enum-base [opt]
18250 { enumerator-list, }attributes[opt] [C++0x]
18251
18252 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18253 if the token stream isn't an enum-specifier after all. */
18254
18255 static tree
18256 cp_parser_enum_specifier (cp_parser* parser)
18257 {
18258 tree identifier;
18259 tree type = NULL_TREE;
18260 tree prev_scope;
18261 tree nested_name_specifier = NULL_TREE;
18262 tree attributes;
18263 bool scoped_enum_p = false;
18264 bool has_underlying_type = false;
18265 bool nested_being_defined = false;
18266 bool new_value_list = false;
18267 bool is_new_type = false;
18268 bool is_unnamed = false;
18269 tree underlying_type = NULL_TREE;
18270 cp_token *type_start_token = NULL;
18271 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18272
18273 parser->colon_corrects_to_scope_p = false;
18274
18275 /* Parse tentatively so that we can back up if we don't find a
18276 enum-specifier. */
18277 cp_parser_parse_tentatively (parser);
18278
18279 /* Caller guarantees that the current token is 'enum', an identifier
18280 possibly follows, and the token after that is an opening brace.
18281 If we don't have an identifier, fabricate an anonymous name for
18282 the enumeration being defined. */
18283 cp_lexer_consume_token (parser->lexer);
18284
18285 /* Parse the "class" or "struct", which indicates a scoped
18286 enumeration type in C++0x. */
18287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18288 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18289 {
18290 if (cxx_dialect < cxx11)
18291 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18292
18293 /* Consume the `struct' or `class' token. */
18294 cp_lexer_consume_token (parser->lexer);
18295
18296 scoped_enum_p = true;
18297 }
18298
18299 attributes = cp_parser_attributes_opt (parser);
18300
18301 /* Clear the qualification. */
18302 parser->scope = NULL_TREE;
18303 parser->qualifying_scope = NULL_TREE;
18304 parser->object_scope = NULL_TREE;
18305
18306 /* Figure out in what scope the declaration is being placed. */
18307 prev_scope = current_scope ();
18308
18309 type_start_token = cp_lexer_peek_token (parser->lexer);
18310
18311 push_deferring_access_checks (dk_no_check);
18312 nested_name_specifier
18313 = cp_parser_nested_name_specifier_opt (parser,
18314 /*typename_keyword_p=*/true,
18315 /*check_dependency_p=*/false,
18316 /*type_p=*/false,
18317 /*is_declaration=*/false);
18318
18319 if (nested_name_specifier)
18320 {
18321 tree name;
18322
18323 identifier = cp_parser_identifier (parser);
18324 name = cp_parser_lookup_name (parser, identifier,
18325 enum_type,
18326 /*is_template=*/false,
18327 /*is_namespace=*/false,
18328 /*check_dependency=*/true,
18329 /*ambiguous_decls=*/NULL,
18330 input_location);
18331 if (name && name != error_mark_node)
18332 {
18333 type = TREE_TYPE (name);
18334 if (TREE_CODE (type) == TYPENAME_TYPE)
18335 {
18336 /* Are template enums allowed in ISO? */
18337 if (template_parm_scope_p ())
18338 pedwarn (type_start_token->location, OPT_Wpedantic,
18339 "%qD is an enumeration template", name);
18340 /* ignore a typename reference, for it will be solved by name
18341 in start_enum. */
18342 type = NULL_TREE;
18343 }
18344 }
18345 else if (nested_name_specifier == error_mark_node)
18346 /* We already issued an error. */;
18347 else
18348 {
18349 error_at (type_start_token->location,
18350 "%qD does not name an enumeration in %qT",
18351 identifier, nested_name_specifier);
18352 nested_name_specifier = error_mark_node;
18353 }
18354 }
18355 else
18356 {
18357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18358 identifier = cp_parser_identifier (parser);
18359 else
18360 {
18361 identifier = make_anon_name ();
18362 is_unnamed = true;
18363 if (scoped_enum_p)
18364 error_at (type_start_token->location,
18365 "unnamed scoped enum is not allowed");
18366 }
18367 }
18368 pop_deferring_access_checks ();
18369
18370 /* Check for the `:' that denotes a specified underlying type in C++0x.
18371 Note that a ':' could also indicate a bitfield width, however. */
18372 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18373 {
18374 cp_decl_specifier_seq type_specifiers;
18375
18376 /* Consume the `:'. */
18377 cp_lexer_consume_token (parser->lexer);
18378
18379 /* Parse the type-specifier-seq. */
18380 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18381 /*is_trailing_return=*/false,
18382 &type_specifiers);
18383
18384 /* At this point this is surely not elaborated type specifier. */
18385 if (!cp_parser_parse_definitely (parser))
18386 return NULL_TREE;
18387
18388 if (cxx_dialect < cxx11)
18389 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18390
18391 has_underlying_type = true;
18392
18393 /* If that didn't work, stop. */
18394 if (type_specifiers.type != error_mark_node)
18395 {
18396 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18397 /*initialized=*/0, NULL);
18398 if (underlying_type == error_mark_node
18399 || check_for_bare_parameter_packs (underlying_type))
18400 underlying_type = NULL_TREE;
18401 }
18402 }
18403
18404 /* Look for the `{' but don't consume it yet. */
18405 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18406 {
18407 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18408 {
18409 cp_parser_error (parser, "expected %<{%>");
18410 if (has_underlying_type)
18411 {
18412 type = NULL_TREE;
18413 goto out;
18414 }
18415 }
18416 /* An opaque-enum-specifier must have a ';' here. */
18417 if ((scoped_enum_p || underlying_type)
18418 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18419 {
18420 cp_parser_error (parser, "expected %<;%> or %<{%>");
18421 if (has_underlying_type)
18422 {
18423 type = NULL_TREE;
18424 goto out;
18425 }
18426 }
18427 }
18428
18429 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18430 return NULL_TREE;
18431
18432 if (nested_name_specifier)
18433 {
18434 if (CLASS_TYPE_P (nested_name_specifier))
18435 {
18436 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18437 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18438 push_scope (nested_name_specifier);
18439 }
18440 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18441 {
18442 push_nested_namespace (nested_name_specifier);
18443 }
18444 }
18445
18446 /* Issue an error message if type-definitions are forbidden here. */
18447 if (!cp_parser_check_type_definition (parser))
18448 type = error_mark_node;
18449 else
18450 /* Create the new type. We do this before consuming the opening
18451 brace so the enum will be recorded as being on the line of its
18452 tag (or the 'enum' keyword, if there is no tag). */
18453 type = start_enum (identifier, type, underlying_type,
18454 attributes, scoped_enum_p, &is_new_type);
18455
18456 /* If the next token is not '{' it is an opaque-enum-specifier or an
18457 elaborated-type-specifier. */
18458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18459 {
18460 timevar_push (TV_PARSE_ENUM);
18461 if (nested_name_specifier
18462 && nested_name_specifier != error_mark_node)
18463 {
18464 /* The following catches invalid code such as:
18465 enum class S<int>::E { A, B, C }; */
18466 if (!processing_specialization
18467 && CLASS_TYPE_P (nested_name_specifier)
18468 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18469 error_at (type_start_token->location, "cannot add an enumerator "
18470 "list to a template instantiation");
18471
18472 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18473 {
18474 error_at (type_start_token->location,
18475 "%<%T::%E%> has not been declared",
18476 TYPE_CONTEXT (nested_name_specifier),
18477 nested_name_specifier);
18478 type = error_mark_node;
18479 }
18480 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18481 && !CLASS_TYPE_P (nested_name_specifier))
18482 {
18483 error_at (type_start_token->location, "nested name specifier "
18484 "%qT for enum declaration does not name a class "
18485 "or namespace", nested_name_specifier);
18486 type = error_mark_node;
18487 }
18488 /* If that scope does not contain the scope in which the
18489 class was originally declared, the program is invalid. */
18490 else if (prev_scope && !is_ancestor (prev_scope,
18491 nested_name_specifier))
18492 {
18493 if (at_namespace_scope_p ())
18494 error_at (type_start_token->location,
18495 "declaration of %qD in namespace %qD which does not "
18496 "enclose %qD",
18497 type, prev_scope, nested_name_specifier);
18498 else
18499 error_at (type_start_token->location,
18500 "declaration of %qD in %qD which does not "
18501 "enclose %qD",
18502 type, prev_scope, nested_name_specifier);
18503 type = error_mark_node;
18504 }
18505 /* If that scope is the scope where the declaration is being placed
18506 the program is invalid. */
18507 else if (CLASS_TYPE_P (nested_name_specifier)
18508 && CLASS_TYPE_P (prev_scope)
18509 && same_type_p (nested_name_specifier, prev_scope))
18510 {
18511 permerror (type_start_token->location,
18512 "extra qualification not allowed");
18513 nested_name_specifier = NULL_TREE;
18514 }
18515 }
18516
18517 if (scoped_enum_p)
18518 begin_scope (sk_scoped_enum, type);
18519
18520 /* Consume the opening brace. */
18521 matching_braces braces;
18522 braces.consume_open (parser);
18523
18524 if (type == error_mark_node)
18525 ; /* Nothing to add */
18526 else if (OPAQUE_ENUM_P (type)
18527 || (cxx_dialect > cxx98 && processing_specialization))
18528 {
18529 new_value_list = true;
18530 SET_OPAQUE_ENUM_P (type, false);
18531 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18532 }
18533 else
18534 {
18535 error_at (type_start_token->location,
18536 "multiple definition of %q#T", type);
18537 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18538 "previous definition here");
18539 type = error_mark_node;
18540 }
18541
18542 if (type == error_mark_node)
18543 cp_parser_skip_to_end_of_block_or_statement (parser);
18544 /* If the next token is not '}', then there are some enumerators. */
18545 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18546 {
18547 if (is_unnamed && !scoped_enum_p)
18548 pedwarn (type_start_token->location, OPT_Wpedantic,
18549 "ISO C++ forbids empty unnamed enum");
18550 }
18551 else
18552 cp_parser_enumerator_list (parser, type);
18553
18554 /* Consume the final '}'. */
18555 braces.require_close (parser);
18556
18557 if (scoped_enum_p)
18558 finish_scope ();
18559 timevar_pop (TV_PARSE_ENUM);
18560 }
18561 else
18562 {
18563 /* If a ';' follows, then it is an opaque-enum-specifier
18564 and additional restrictions apply. */
18565 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18566 {
18567 if (is_unnamed)
18568 error_at (type_start_token->location,
18569 "opaque-enum-specifier without name");
18570 else if (nested_name_specifier)
18571 error_at (type_start_token->location,
18572 "opaque-enum-specifier must use a simple identifier");
18573 }
18574 }
18575
18576 /* Look for trailing attributes to apply to this enumeration, and
18577 apply them if appropriate. */
18578 if (cp_parser_allow_gnu_extensions_p (parser))
18579 {
18580 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18581 cplus_decl_attributes (&type,
18582 trailing_attr,
18583 (int) ATTR_FLAG_TYPE_IN_PLACE);
18584 }
18585
18586 /* Finish up the enumeration. */
18587 if (type != error_mark_node)
18588 {
18589 if (new_value_list)
18590 finish_enum_value_list (type);
18591 if (is_new_type)
18592 finish_enum (type);
18593 }
18594
18595 if (nested_name_specifier)
18596 {
18597 if (CLASS_TYPE_P (nested_name_specifier))
18598 {
18599 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18600 pop_scope (nested_name_specifier);
18601 }
18602 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18603 {
18604 pop_nested_namespace (nested_name_specifier);
18605 }
18606 }
18607 out:
18608 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18609 return type;
18610 }
18611
18612 /* Parse an enumerator-list. The enumerators all have the indicated
18613 TYPE.
18614
18615 enumerator-list:
18616 enumerator-definition
18617 enumerator-list , enumerator-definition */
18618
18619 static void
18620 cp_parser_enumerator_list (cp_parser* parser, tree type)
18621 {
18622 while (true)
18623 {
18624 /* Parse an enumerator-definition. */
18625 cp_parser_enumerator_definition (parser, type);
18626
18627 /* If the next token is not a ',', we've reached the end of
18628 the list. */
18629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18630 break;
18631 /* Otherwise, consume the `,' and keep going. */
18632 cp_lexer_consume_token (parser->lexer);
18633 /* If the next token is a `}', there is a trailing comma. */
18634 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18635 {
18636 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18637 pedwarn (input_location, OPT_Wpedantic,
18638 "comma at end of enumerator list");
18639 break;
18640 }
18641 }
18642 }
18643
18644 /* Parse an enumerator-definition. The enumerator has the indicated
18645 TYPE.
18646
18647 enumerator-definition:
18648 enumerator
18649 enumerator = constant-expression
18650
18651 enumerator:
18652 identifier
18653
18654 GNU Extensions:
18655
18656 enumerator-definition:
18657 enumerator attributes [opt]
18658 enumerator attributes [opt] = constant-expression */
18659
18660 static void
18661 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18662 {
18663 tree identifier;
18664 tree value;
18665 location_t loc;
18666
18667 /* Save the input location because we are interested in the location
18668 of the identifier and not the location of the explicit value. */
18669 loc = cp_lexer_peek_token (parser->lexer)->location;
18670
18671 /* Look for the identifier. */
18672 identifier = cp_parser_identifier (parser);
18673 if (identifier == error_mark_node)
18674 return;
18675
18676 /* Parse any specified attributes. */
18677 tree attrs = cp_parser_attributes_opt (parser);
18678
18679 /* If the next token is an '=', then there is an explicit value. */
18680 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18681 {
18682 /* Consume the `=' token. */
18683 cp_lexer_consume_token (parser->lexer);
18684 /* Parse the value. */
18685 value = cp_parser_constant_expression (parser);
18686 }
18687 else
18688 value = NULL_TREE;
18689
18690 /* If we are processing a template, make sure the initializer of the
18691 enumerator doesn't contain any bare template parameter pack. */
18692 if (check_for_bare_parameter_packs (value))
18693 value = error_mark_node;
18694
18695 /* Create the enumerator. */
18696 build_enumerator (identifier, value, type, attrs, loc);
18697 }
18698
18699 /* Parse a namespace-name.
18700
18701 namespace-name:
18702 original-namespace-name
18703 namespace-alias
18704
18705 Returns the NAMESPACE_DECL for the namespace. */
18706
18707 static tree
18708 cp_parser_namespace_name (cp_parser* parser)
18709 {
18710 tree identifier;
18711 tree namespace_decl;
18712
18713 cp_token *token = cp_lexer_peek_token (parser->lexer);
18714
18715 /* Get the name of the namespace. */
18716 identifier = cp_parser_identifier (parser);
18717 if (identifier == error_mark_node)
18718 return error_mark_node;
18719
18720 /* Look up the identifier in the currently active scope. Look only
18721 for namespaces, due to:
18722
18723 [basic.lookup.udir]
18724
18725 When looking up a namespace-name in a using-directive or alias
18726 definition, only namespace names are considered.
18727
18728 And:
18729
18730 [basic.lookup.qual]
18731
18732 During the lookup of a name preceding the :: scope resolution
18733 operator, object, function, and enumerator names are ignored.
18734
18735 (Note that cp_parser_qualifying_entity only calls this
18736 function if the token after the name is the scope resolution
18737 operator.) */
18738 namespace_decl = cp_parser_lookup_name (parser, identifier,
18739 none_type,
18740 /*is_template=*/false,
18741 /*is_namespace=*/true,
18742 /*check_dependency=*/true,
18743 /*ambiguous_decls=*/NULL,
18744 token->location);
18745 /* If it's not a namespace, issue an error. */
18746 if (namespace_decl == error_mark_node
18747 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18748 {
18749 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18750 {
18751 auto_diagnostic_group d;
18752 name_hint hint;
18753 if (namespace_decl == error_mark_node
18754 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18755 hint = suggest_alternative_in_explicit_scope (token->location,
18756 identifier,
18757 parser->scope);
18758 if (const char *suggestion = hint.suggestion ())
18759 {
18760 gcc_rich_location richloc (token->location);
18761 richloc.add_fixit_replace (suggestion);
18762 error_at (&richloc,
18763 "%qD is not a namespace-name; did you mean %qs?",
18764 identifier, suggestion);
18765 }
18766 else
18767 error_at (token->location, "%qD is not a namespace-name",
18768 identifier);
18769 }
18770 else
18771 cp_parser_error (parser, "expected namespace-name");
18772 namespace_decl = error_mark_node;
18773 }
18774
18775 return namespace_decl;
18776 }
18777
18778 /* Parse a namespace-definition.
18779
18780 namespace-definition:
18781 named-namespace-definition
18782 unnamed-namespace-definition
18783
18784 named-namespace-definition:
18785 original-namespace-definition
18786 extension-namespace-definition
18787
18788 original-namespace-definition:
18789 namespace identifier { namespace-body }
18790
18791 extension-namespace-definition:
18792 namespace original-namespace-name { namespace-body }
18793
18794 unnamed-namespace-definition:
18795 namespace { namespace-body } */
18796
18797 static void
18798 cp_parser_namespace_definition (cp_parser* parser)
18799 {
18800 tree identifier;
18801 int nested_definition_count = 0;
18802
18803 cp_ensure_no_omp_declare_simd (parser);
18804 cp_ensure_no_oacc_routine (parser);
18805
18806 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18807
18808 if (is_inline)
18809 {
18810 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18811 cp_lexer_consume_token (parser->lexer);
18812 }
18813
18814 /* Look for the `namespace' keyword. */
18815 cp_token* token
18816 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18817
18818 /* Parse any specified attributes before the identifier. */
18819 tree attribs = cp_parser_attributes_opt (parser);
18820
18821 for (;;)
18822 {
18823 identifier = NULL_TREE;
18824
18825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18826 {
18827 identifier = cp_parser_identifier (parser);
18828
18829 if (cp_next_tokens_can_be_std_attribute_p (parser))
18830 pedwarn (input_location, OPT_Wpedantic,
18831 "standard attributes on namespaces must precede "
18832 "the namespace name");
18833
18834 /* Parse any attributes specified after the identifier. */
18835 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18836 }
18837
18838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18839 break;
18840
18841 if (!nested_definition_count && cxx_dialect < cxx17)
18842 pedwarn (input_location, OPT_Wpedantic,
18843 "nested namespace definitions only available with "
18844 "-std=c++17 or -std=gnu++17");
18845
18846 /* Nested namespace names can create new namespaces (unlike
18847 other qualified-ids). */
18848 if (int count = identifier ? push_namespace (identifier) : 0)
18849 nested_definition_count += count;
18850 else
18851 cp_parser_error (parser, "nested namespace name required");
18852 cp_lexer_consume_token (parser->lexer);
18853 }
18854
18855 if (nested_definition_count && !identifier)
18856 cp_parser_error (parser, "namespace name required");
18857
18858 if (nested_definition_count && attribs)
18859 error_at (token->location,
18860 "a nested namespace definition cannot have attributes");
18861 if (nested_definition_count && is_inline)
18862 error_at (token->location,
18863 "a nested namespace definition cannot be inline");
18864
18865 /* Start the namespace. */
18866 nested_definition_count += push_namespace (identifier, is_inline);
18867
18868 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18869
18870 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18871
18872 /* Look for the `{' to validate starting the namespace. */
18873 matching_braces braces;
18874 if (braces.require_open (parser))
18875 {
18876 /* Parse the body of the namespace. */
18877 cp_parser_namespace_body (parser);
18878
18879 /* Look for the final `}'. */
18880 braces.require_close (parser);
18881 }
18882
18883 if (has_visibility)
18884 pop_visibility (1);
18885
18886 /* Pop the nested namespace definitions. */
18887 while (nested_definition_count--)
18888 pop_namespace ();
18889 }
18890
18891 /* Parse a namespace-body.
18892
18893 namespace-body:
18894 declaration-seq [opt] */
18895
18896 static void
18897 cp_parser_namespace_body (cp_parser* parser)
18898 {
18899 cp_parser_declaration_seq_opt (parser);
18900 }
18901
18902 /* Parse a namespace-alias-definition.
18903
18904 namespace-alias-definition:
18905 namespace identifier = qualified-namespace-specifier ; */
18906
18907 static void
18908 cp_parser_namespace_alias_definition (cp_parser* parser)
18909 {
18910 tree identifier;
18911 tree namespace_specifier;
18912
18913 cp_token *token = cp_lexer_peek_token (parser->lexer);
18914
18915 /* Look for the `namespace' keyword. */
18916 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18917 /* Look for the identifier. */
18918 identifier = cp_parser_identifier (parser);
18919 if (identifier == error_mark_node)
18920 return;
18921 /* Look for the `=' token. */
18922 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18923 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18924 {
18925 error_at (token->location, "%<namespace%> definition is not allowed here");
18926 /* Skip the definition. */
18927 cp_lexer_consume_token (parser->lexer);
18928 if (cp_parser_skip_to_closing_brace (parser))
18929 cp_lexer_consume_token (parser->lexer);
18930 return;
18931 }
18932 cp_parser_require (parser, CPP_EQ, RT_EQ);
18933 /* Look for the qualified-namespace-specifier. */
18934 namespace_specifier
18935 = cp_parser_qualified_namespace_specifier (parser);
18936 /* Look for the `;' token. */
18937 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18938
18939 /* Register the alias in the symbol table. */
18940 do_namespace_alias (identifier, namespace_specifier);
18941 }
18942
18943 /* Parse a qualified-namespace-specifier.
18944
18945 qualified-namespace-specifier:
18946 :: [opt] nested-name-specifier [opt] namespace-name
18947
18948 Returns a NAMESPACE_DECL corresponding to the specified
18949 namespace. */
18950
18951 static tree
18952 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18953 {
18954 /* Look for the optional `::'. */
18955 cp_parser_global_scope_opt (parser,
18956 /*current_scope_valid_p=*/false);
18957
18958 /* Look for the optional nested-name-specifier. */
18959 cp_parser_nested_name_specifier_opt (parser,
18960 /*typename_keyword_p=*/false,
18961 /*check_dependency_p=*/true,
18962 /*type_p=*/false,
18963 /*is_declaration=*/true);
18964
18965 return cp_parser_namespace_name (parser);
18966 }
18967
18968 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18969 access declaration.
18970
18971 using-declaration:
18972 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18973 using :: unqualified-id ;
18974
18975 access-declaration:
18976 qualified-id ;
18977
18978 */
18979
18980 static bool
18981 cp_parser_using_declaration (cp_parser* parser,
18982 bool access_declaration_p)
18983 {
18984 cp_token *token;
18985 bool typename_p = false;
18986 bool global_scope_p;
18987 tree decl;
18988 tree identifier;
18989 tree qscope;
18990 int oldcount = errorcount;
18991 cp_token *diag_token = NULL;
18992
18993 if (access_declaration_p)
18994 {
18995 diag_token = cp_lexer_peek_token (parser->lexer);
18996 cp_parser_parse_tentatively (parser);
18997 }
18998 else
18999 {
19000 /* Look for the `using' keyword. */
19001 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19002
19003 again:
19004 /* Peek at the next token. */
19005 token = cp_lexer_peek_token (parser->lexer);
19006 /* See if it's `typename'. */
19007 if (token->keyword == RID_TYPENAME)
19008 {
19009 /* Remember that we've seen it. */
19010 typename_p = true;
19011 /* Consume the `typename' token. */
19012 cp_lexer_consume_token (parser->lexer);
19013 }
19014 }
19015
19016 /* Look for the optional global scope qualification. */
19017 global_scope_p
19018 = (cp_parser_global_scope_opt (parser,
19019 /*current_scope_valid_p=*/false)
19020 != NULL_TREE);
19021
19022 /* If we saw `typename', or didn't see `::', then there must be a
19023 nested-name-specifier present. */
19024 if (typename_p || !global_scope_p)
19025 {
19026 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19027 /*check_dependency_p=*/true,
19028 /*type_p=*/false,
19029 /*is_declaration=*/true);
19030 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19031 {
19032 cp_parser_skip_to_end_of_block_or_statement (parser);
19033 return false;
19034 }
19035 }
19036 /* Otherwise, we could be in either of the two productions. In that
19037 case, treat the nested-name-specifier as optional. */
19038 else
19039 qscope = cp_parser_nested_name_specifier_opt (parser,
19040 /*typename_keyword_p=*/false,
19041 /*check_dependency_p=*/true,
19042 /*type_p=*/false,
19043 /*is_declaration=*/true);
19044 if (!qscope)
19045 qscope = global_namespace;
19046 else if (UNSCOPED_ENUM_P (qscope))
19047 qscope = CP_TYPE_CONTEXT (qscope);
19048
19049 if (access_declaration_p && cp_parser_error_occurred (parser))
19050 /* Something has already gone wrong; there's no need to parse
19051 further. Since an error has occurred, the return value of
19052 cp_parser_parse_definitely will be false, as required. */
19053 return cp_parser_parse_definitely (parser);
19054
19055 token = cp_lexer_peek_token (parser->lexer);
19056 /* Parse the unqualified-id. */
19057 identifier = cp_parser_unqualified_id (parser,
19058 /*template_keyword_p=*/false,
19059 /*check_dependency_p=*/true,
19060 /*declarator_p=*/true,
19061 /*optional_p=*/false);
19062
19063 if (access_declaration_p)
19064 {
19065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19066 cp_parser_simulate_error (parser);
19067 if (!cp_parser_parse_definitely (parser))
19068 return false;
19069 }
19070 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19071 {
19072 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19073 if (cxx_dialect < cxx17
19074 && !in_system_header_at (ell->location))
19075 pedwarn (ell->location, 0,
19076 "pack expansion in using-declaration only available "
19077 "with -std=c++17 or -std=gnu++17");
19078 qscope = make_pack_expansion (qscope);
19079 }
19080
19081 /* The function we call to handle a using-declaration is different
19082 depending on what scope we are in. */
19083 if (qscope == error_mark_node || identifier == error_mark_node)
19084 ;
19085 else if (!identifier_p (identifier)
19086 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19087 /* [namespace.udecl]
19088
19089 A using declaration shall not name a template-id. */
19090 error_at (token->location,
19091 "a template-id may not appear in a using-declaration");
19092 else
19093 {
19094 if (at_class_scope_p ())
19095 {
19096 /* Create the USING_DECL. */
19097 decl = do_class_using_decl (qscope, identifier);
19098
19099 if (decl && typename_p)
19100 USING_DECL_TYPENAME_P (decl) = 1;
19101
19102 if (check_for_bare_parameter_packs (decl))
19103 {
19104 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19105 return false;
19106 }
19107 else
19108 /* Add it to the list of members in this class. */
19109 finish_member_declaration (decl);
19110 }
19111 else
19112 {
19113 decl = cp_parser_lookup_name_simple (parser,
19114 identifier,
19115 token->location);
19116 if (decl == error_mark_node)
19117 cp_parser_name_lookup_error (parser, identifier,
19118 decl, NLE_NULL,
19119 token->location);
19120 else if (check_for_bare_parameter_packs (decl))
19121 {
19122 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19123 return false;
19124 }
19125 else if (!at_namespace_scope_p ())
19126 finish_local_using_decl (decl, qscope, identifier);
19127 else
19128 finish_namespace_using_decl (decl, qscope, identifier);
19129 }
19130 }
19131
19132 if (!access_declaration_p
19133 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19134 {
19135 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19136 if (cxx_dialect < cxx17)
19137 pedwarn (comma->location, 0,
19138 "comma-separated list in using-declaration only available "
19139 "with -std=c++17 or -std=gnu++17");
19140 goto again;
19141 }
19142
19143 /* Look for the final `;'. */
19144 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19145
19146 if (access_declaration_p && errorcount == oldcount)
19147 warning_at (diag_token->location, OPT_Wdeprecated,
19148 "access declarations are deprecated "
19149 "in favour of using-declarations; "
19150 "suggestion: add the %<using%> keyword");
19151
19152 return true;
19153 }
19154
19155 /* Parse an alias-declaration.
19156
19157 alias-declaration:
19158 using identifier attribute-specifier-seq [opt] = type-id */
19159
19160 static tree
19161 cp_parser_alias_declaration (cp_parser* parser)
19162 {
19163 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19164 location_t id_location, type_location;
19165 cp_declarator *declarator;
19166 cp_decl_specifier_seq decl_specs;
19167 bool member_p;
19168 const char *saved_message = NULL;
19169
19170 /* Look for the `using' keyword. */
19171 cp_token *using_token
19172 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19173 if (using_token == NULL)
19174 return error_mark_node;
19175
19176 id_location = cp_lexer_peek_token (parser->lexer)->location;
19177 id = cp_parser_identifier (parser);
19178 if (id == error_mark_node)
19179 return error_mark_node;
19180
19181 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19182 attributes = cp_parser_attributes_opt (parser);
19183 if (attributes == error_mark_node)
19184 return error_mark_node;
19185
19186 cp_parser_require (parser, CPP_EQ, RT_EQ);
19187
19188 if (cp_parser_error_occurred (parser))
19189 return error_mark_node;
19190
19191 cp_parser_commit_to_tentative_parse (parser);
19192
19193 /* Now we are going to parse the type-id of the declaration. */
19194
19195 /*
19196 [dcl.type]/3 says:
19197
19198 "A type-specifier-seq shall not define a class or enumeration
19199 unless it appears in the type-id of an alias-declaration (7.1.3) that
19200 is not the declaration of a template-declaration."
19201
19202 In other words, if we currently are in an alias template, the
19203 type-id should not define a type.
19204
19205 So let's set parser->type_definition_forbidden_message in that
19206 case; cp_parser_check_type_definition (called by
19207 cp_parser_class_specifier) will then emit an error if a type is
19208 defined in the type-id. */
19209 if (parser->num_template_parameter_lists)
19210 {
19211 saved_message = parser->type_definition_forbidden_message;
19212 parser->type_definition_forbidden_message =
19213 G_("types may not be defined in alias template declarations");
19214 }
19215
19216 type = cp_parser_type_id (parser, &type_location);
19217
19218 /* Restore the error message if need be. */
19219 if (parser->num_template_parameter_lists)
19220 parser->type_definition_forbidden_message = saved_message;
19221
19222 if (type == error_mark_node
19223 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19224 {
19225 cp_parser_skip_to_end_of_block_or_statement (parser);
19226 return error_mark_node;
19227 }
19228
19229 /* A typedef-name can also be introduced by an alias-declaration. The
19230 identifier following the using keyword becomes a typedef-name. It has
19231 the same semantics as if it were introduced by the typedef
19232 specifier. In particular, it does not define a new type and it shall
19233 not appear in the type-id. */
19234
19235 clear_decl_specs (&decl_specs);
19236 decl_specs.type = type;
19237 if (attributes != NULL_TREE)
19238 {
19239 decl_specs.attributes = attributes;
19240 set_and_check_decl_spec_loc (&decl_specs,
19241 ds_attribute,
19242 attrs_token);
19243 }
19244 set_and_check_decl_spec_loc (&decl_specs,
19245 ds_typedef,
19246 using_token);
19247 set_and_check_decl_spec_loc (&decl_specs,
19248 ds_alias,
19249 using_token);
19250 decl_specs.locations[ds_type_spec] = type_location;
19251
19252 if (parser->num_template_parameter_lists
19253 && !cp_parser_check_template_parameters (parser,
19254 /*num_templates=*/0,
19255 /*template_id*/false,
19256 id_location,
19257 /*declarator=*/NULL))
19258 return error_mark_node;
19259
19260 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19261 declarator->id_loc = id_location;
19262
19263 member_p = at_class_scope_p ();
19264 if (member_p)
19265 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19266 NULL_TREE, attributes);
19267 else
19268 decl = start_decl (declarator, &decl_specs, 0,
19269 attributes, NULL_TREE, &pushed_scope);
19270 if (decl == error_mark_node)
19271 return decl;
19272
19273 // Attach constraints to the alias declaration.
19274 if (flag_concepts && current_template_parms)
19275 {
19276 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19277 tree constr = build_constraints (reqs, NULL_TREE);
19278 set_constraints (decl, constr);
19279 }
19280
19281 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19282
19283 if (pushed_scope)
19284 pop_scope (pushed_scope);
19285
19286 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19287 added into the symbol table; otherwise, return the TYPE_DECL. */
19288 if (DECL_LANG_SPECIFIC (decl)
19289 && DECL_TEMPLATE_INFO (decl)
19290 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19291 {
19292 decl = DECL_TI_TEMPLATE (decl);
19293 if (member_p)
19294 check_member_template (decl);
19295 }
19296
19297 return decl;
19298 }
19299
19300 /* Parse a using-directive.
19301
19302 using-directive:
19303 using namespace :: [opt] nested-name-specifier [opt]
19304 namespace-name ; */
19305
19306 static void
19307 cp_parser_using_directive (cp_parser* parser)
19308 {
19309 tree namespace_decl;
19310 tree attribs;
19311
19312 /* Look for the `using' keyword. */
19313 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19314 /* And the `namespace' keyword. */
19315 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19316 /* Look for the optional `::' operator. */
19317 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19318 /* And the optional nested-name-specifier. */
19319 cp_parser_nested_name_specifier_opt (parser,
19320 /*typename_keyword_p=*/false,
19321 /*check_dependency_p=*/true,
19322 /*type_p=*/false,
19323 /*is_declaration=*/true);
19324 /* Get the namespace being used. */
19325 namespace_decl = cp_parser_namespace_name (parser);
19326 /* And any specified attributes. */
19327 attribs = cp_parser_attributes_opt (parser);
19328
19329 /* Update the symbol table. */
19330 if (namespace_bindings_p ())
19331 finish_namespace_using_directive (namespace_decl, attribs);
19332 else
19333 finish_local_using_directive (namespace_decl, attribs);
19334
19335 /* Look for the final `;'. */
19336 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19337 }
19338
19339 /* Parse an asm-definition.
19340
19341 asm-definition:
19342 asm ( string-literal ) ;
19343
19344 GNU Extension:
19345
19346 asm-definition:
19347 asm volatile [opt] ( string-literal ) ;
19348 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19349 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19350 : asm-operand-list [opt] ) ;
19351 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19352 : asm-operand-list [opt]
19353 : asm-clobber-list [opt] ) ;
19354 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19355 : asm-clobber-list [opt]
19356 : asm-goto-list ) ; */
19357
19358 static void
19359 cp_parser_asm_definition (cp_parser* parser)
19360 {
19361 tree string;
19362 tree outputs = NULL_TREE;
19363 tree inputs = NULL_TREE;
19364 tree clobbers = NULL_TREE;
19365 tree labels = NULL_TREE;
19366 tree asm_stmt;
19367 bool volatile_p = false;
19368 bool extended_p = false;
19369 bool invalid_inputs_p = false;
19370 bool invalid_outputs_p = false;
19371 bool goto_p = false;
19372 required_token missing = RT_NONE;
19373
19374 /* Look for the `asm' keyword. */
19375 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19376
19377 if (parser->in_function_body
19378 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19379 {
19380 error ("%<asm%> in %<constexpr%> function");
19381 cp_function_chain->invalid_constexpr = true;
19382 }
19383
19384 /* See if the next token is `volatile'. */
19385 if (cp_parser_allow_gnu_extensions_p (parser)
19386 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19387 {
19388 /* Remember that we saw the `volatile' keyword. */
19389 volatile_p = true;
19390 /* Consume the token. */
19391 cp_lexer_consume_token (parser->lexer);
19392 }
19393 if (cp_parser_allow_gnu_extensions_p (parser)
19394 && parser->in_function_body
19395 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19396 {
19397 /* Remember that we saw the `goto' keyword. */
19398 goto_p = true;
19399 /* Consume the token. */
19400 cp_lexer_consume_token (parser->lexer);
19401 }
19402 /* Look for the opening `('. */
19403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19404 return;
19405 /* Look for the string. */
19406 string = cp_parser_string_literal (parser, false, false);
19407 if (string == error_mark_node)
19408 {
19409 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19410 /*consume_paren=*/true);
19411 return;
19412 }
19413
19414 /* If we're allowing GNU extensions, check for the extended assembly
19415 syntax. Unfortunately, the `:' tokens need not be separated by
19416 a space in C, and so, for compatibility, we tolerate that here
19417 too. Doing that means that we have to treat the `::' operator as
19418 two `:' tokens. */
19419 if (cp_parser_allow_gnu_extensions_p (parser)
19420 && parser->in_function_body
19421 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19422 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19423 {
19424 bool inputs_p = false;
19425 bool clobbers_p = false;
19426 bool labels_p = false;
19427
19428 /* The extended syntax was used. */
19429 extended_p = true;
19430
19431 /* Look for outputs. */
19432 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19433 {
19434 /* Consume the `:'. */
19435 cp_lexer_consume_token (parser->lexer);
19436 /* Parse the output-operands. */
19437 if (cp_lexer_next_token_is_not (parser->lexer,
19438 CPP_COLON)
19439 && cp_lexer_next_token_is_not (parser->lexer,
19440 CPP_SCOPE)
19441 && cp_lexer_next_token_is_not (parser->lexer,
19442 CPP_CLOSE_PAREN)
19443 && !goto_p)
19444 {
19445 outputs = cp_parser_asm_operand_list (parser);
19446 if (outputs == error_mark_node)
19447 invalid_outputs_p = true;
19448 }
19449 }
19450 /* If the next token is `::', there are no outputs, and the
19451 next token is the beginning of the inputs. */
19452 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19453 /* The inputs are coming next. */
19454 inputs_p = true;
19455
19456 /* Look for inputs. */
19457 if (inputs_p
19458 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19459 {
19460 /* Consume the `:' or `::'. */
19461 cp_lexer_consume_token (parser->lexer);
19462 /* Parse the output-operands. */
19463 if (cp_lexer_next_token_is_not (parser->lexer,
19464 CPP_COLON)
19465 && cp_lexer_next_token_is_not (parser->lexer,
19466 CPP_SCOPE)
19467 && cp_lexer_next_token_is_not (parser->lexer,
19468 CPP_CLOSE_PAREN))
19469 {
19470 inputs = cp_parser_asm_operand_list (parser);
19471 if (inputs == error_mark_node)
19472 invalid_inputs_p = true;
19473 }
19474 }
19475 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19476 /* The clobbers are coming next. */
19477 clobbers_p = true;
19478
19479 /* Look for clobbers. */
19480 if (clobbers_p
19481 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19482 {
19483 clobbers_p = true;
19484 /* Consume the `:' or `::'. */
19485 cp_lexer_consume_token (parser->lexer);
19486 /* Parse the clobbers. */
19487 if (cp_lexer_next_token_is_not (parser->lexer,
19488 CPP_COLON)
19489 && cp_lexer_next_token_is_not (parser->lexer,
19490 CPP_CLOSE_PAREN))
19491 clobbers = cp_parser_asm_clobber_list (parser);
19492 }
19493 else if (goto_p
19494 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19495 /* The labels are coming next. */
19496 labels_p = true;
19497
19498 /* Look for labels. */
19499 if (labels_p
19500 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19501 {
19502 labels_p = true;
19503 /* Consume the `:' or `::'. */
19504 cp_lexer_consume_token (parser->lexer);
19505 /* Parse the labels. */
19506 labels = cp_parser_asm_label_list (parser);
19507 }
19508
19509 if (goto_p && !labels_p)
19510 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19511 }
19512 else if (goto_p)
19513 missing = RT_COLON_SCOPE;
19514
19515 /* Look for the closing `)'. */
19516 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19517 missing ? missing : RT_CLOSE_PAREN))
19518 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19519 /*consume_paren=*/true);
19520 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19521
19522 if (!invalid_inputs_p && !invalid_outputs_p)
19523 {
19524 /* Create the ASM_EXPR. */
19525 if (parser->in_function_body)
19526 {
19527 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19528 inputs, clobbers, labels);
19529 /* If the extended syntax was not used, mark the ASM_EXPR. */
19530 if (!extended_p)
19531 {
19532 tree temp = asm_stmt;
19533 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19534 temp = TREE_OPERAND (temp, 0);
19535
19536 ASM_INPUT_P (temp) = 1;
19537 }
19538 }
19539 else
19540 symtab->finalize_toplevel_asm (string);
19541 }
19542 }
19543
19544 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19545 type that comes from the decl-specifier-seq. */
19546
19547 static tree
19548 strip_declarator_types (tree type, cp_declarator *declarator)
19549 {
19550 for (cp_declarator *d = declarator; d;)
19551 switch (d->kind)
19552 {
19553 case cdk_id:
19554 case cdk_decomp:
19555 case cdk_error:
19556 d = NULL;
19557 break;
19558
19559 default:
19560 if (TYPE_PTRMEMFUNC_P (type))
19561 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19562 type = TREE_TYPE (type);
19563 d = d->declarator;
19564 break;
19565 }
19566
19567 return type;
19568 }
19569
19570 /* Declarators [gram.dcl.decl] */
19571
19572 /* Parse an init-declarator.
19573
19574 init-declarator:
19575 declarator initializer [opt]
19576
19577 GNU Extension:
19578
19579 init-declarator:
19580 declarator asm-specification [opt] attributes [opt] initializer [opt]
19581
19582 function-definition:
19583 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19584 function-body
19585 decl-specifier-seq [opt] declarator function-try-block
19586
19587 GNU Extension:
19588
19589 function-definition:
19590 __extension__ function-definition
19591
19592 TM Extension:
19593
19594 function-definition:
19595 decl-specifier-seq [opt] declarator function-transaction-block
19596
19597 The DECL_SPECIFIERS apply to this declarator. Returns a
19598 representation of the entity declared. If MEMBER_P is TRUE, then
19599 this declarator appears in a class scope. The new DECL created by
19600 this declarator is returned.
19601
19602 The CHECKS are access checks that should be performed once we know
19603 what entity is being declared (and, therefore, what classes have
19604 befriended it).
19605
19606 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19607 for a function-definition here as well. If the declarator is a
19608 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19609 be TRUE upon return. By that point, the function-definition will
19610 have been completely parsed.
19611
19612 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19613 is FALSE.
19614
19615 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19616 parsed declaration if it is an uninitialized single declarator not followed
19617 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19618 if present, will not be consumed. If returned, this declarator will be
19619 created with SD_INITIALIZED but will not call cp_finish_decl.
19620
19621 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19622 and there is an initializer, the pointed location_t is set to the
19623 location of the '=' or `(', or '{' in C++11 token introducing the
19624 initializer. */
19625
19626 static tree
19627 cp_parser_init_declarator (cp_parser* parser,
19628 cp_decl_specifier_seq *decl_specifiers,
19629 vec<deferred_access_check, va_gc> *checks,
19630 bool function_definition_allowed_p,
19631 bool member_p,
19632 int declares_class_or_enum,
19633 bool* function_definition_p,
19634 tree* maybe_range_for_decl,
19635 location_t* init_loc,
19636 tree* auto_result)
19637 {
19638 cp_token *token = NULL, *asm_spec_start_token = NULL,
19639 *attributes_start_token = NULL;
19640 cp_declarator *declarator;
19641 tree prefix_attributes;
19642 tree attributes = NULL;
19643 tree asm_specification;
19644 tree initializer;
19645 tree decl = NULL_TREE;
19646 tree scope;
19647 int is_initialized;
19648 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19649 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19650 "(...)". */
19651 enum cpp_ttype initialization_kind;
19652 bool is_direct_init = false;
19653 bool is_non_constant_init;
19654 int ctor_dtor_or_conv_p;
19655 bool friend_p = cp_parser_friend_p (decl_specifiers);
19656 tree pushed_scope = NULL_TREE;
19657 bool range_for_decl_p = false;
19658 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19659 location_t tmp_init_loc = UNKNOWN_LOCATION;
19660
19661 /* Gather the attributes that were provided with the
19662 decl-specifiers. */
19663 prefix_attributes = decl_specifiers->attributes;
19664
19665 /* Assume that this is not the declarator for a function
19666 definition. */
19667 if (function_definition_p)
19668 *function_definition_p = false;
19669
19670 /* Default arguments are only permitted for function parameters. */
19671 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19672 parser->default_arg_ok_p = false;
19673
19674 /* Defer access checks while parsing the declarator; we cannot know
19675 what names are accessible until we know what is being
19676 declared. */
19677 resume_deferring_access_checks ();
19678
19679 token = cp_lexer_peek_token (parser->lexer);
19680
19681 /* Parse the declarator. */
19682 declarator
19683 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19684 &ctor_dtor_or_conv_p,
19685 /*parenthesized_p=*/NULL,
19686 member_p, friend_p);
19687 /* Gather up the deferred checks. */
19688 stop_deferring_access_checks ();
19689
19690 parser->default_arg_ok_p = saved_default_arg_ok_p;
19691
19692 /* If the DECLARATOR was erroneous, there's no need to go
19693 further. */
19694 if (declarator == cp_error_declarator)
19695 return error_mark_node;
19696
19697 /* Check that the number of template-parameter-lists is OK. */
19698 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19699 token->location))
19700 return error_mark_node;
19701
19702 if (declares_class_or_enum & 2)
19703 cp_parser_check_for_definition_in_return_type (declarator,
19704 decl_specifiers->type,
19705 decl_specifiers->locations[ds_type_spec]);
19706
19707 /* Figure out what scope the entity declared by the DECLARATOR is
19708 located in. `grokdeclarator' sometimes changes the scope, so
19709 we compute it now. */
19710 scope = get_scope_of_declarator (declarator);
19711
19712 /* Perform any lookups in the declared type which were thought to be
19713 dependent, but are not in the scope of the declarator. */
19714 decl_specifiers->type
19715 = maybe_update_decl_type (decl_specifiers->type, scope);
19716
19717 /* If we're allowing GNU extensions, look for an
19718 asm-specification. */
19719 if (cp_parser_allow_gnu_extensions_p (parser))
19720 {
19721 /* Look for an asm-specification. */
19722 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19723 asm_specification = cp_parser_asm_specification_opt (parser);
19724 }
19725 else
19726 asm_specification = NULL_TREE;
19727
19728 /* Look for attributes. */
19729 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19730 attributes = cp_parser_attributes_opt (parser);
19731
19732 /* Peek at the next token. */
19733 token = cp_lexer_peek_token (parser->lexer);
19734
19735 bool bogus_implicit_tmpl = false;
19736
19737 if (function_declarator_p (declarator))
19738 {
19739 /* Handle C++17 deduction guides. */
19740 if (!decl_specifiers->type
19741 && ctor_dtor_or_conv_p <= 0
19742 && cxx_dialect >= cxx17)
19743 {
19744 cp_declarator *id = get_id_declarator (declarator);
19745 tree name = id->u.id.unqualified_name;
19746 parser->scope = id->u.id.qualifying_scope;
19747 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19748 if (tmpl
19749 && (DECL_CLASS_TEMPLATE_P (tmpl)
19750 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19751 {
19752 id->u.id.unqualified_name = dguide_name (tmpl);
19753 id->u.id.sfk = sfk_deduction_guide;
19754 ctor_dtor_or_conv_p = 1;
19755 }
19756 }
19757
19758 /* Check to see if the token indicates the start of a
19759 function-definition. */
19760 if (cp_parser_token_starts_function_definition_p (token))
19761 {
19762 if (!function_definition_allowed_p)
19763 {
19764 /* If a function-definition should not appear here, issue an
19765 error message. */
19766 cp_parser_error (parser,
19767 "a function-definition is not allowed here");
19768 return error_mark_node;
19769 }
19770
19771 location_t func_brace_location
19772 = cp_lexer_peek_token (parser->lexer)->location;
19773
19774 /* Neither attributes nor an asm-specification are allowed
19775 on a function-definition. */
19776 if (asm_specification)
19777 error_at (asm_spec_start_token->location,
19778 "an asm-specification is not allowed "
19779 "on a function-definition");
19780 if (attributes)
19781 error_at (attributes_start_token->location,
19782 "attributes are not allowed "
19783 "on a function-definition");
19784 /* This is a function-definition. */
19785 *function_definition_p = true;
19786
19787 /* Parse the function definition. */
19788 if (member_p)
19789 decl = cp_parser_save_member_function_body (parser,
19790 decl_specifiers,
19791 declarator,
19792 prefix_attributes);
19793 else
19794 decl =
19795 (cp_parser_function_definition_from_specifiers_and_declarator
19796 (parser, decl_specifiers, prefix_attributes, declarator));
19797
19798 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19799 {
19800 /* This is where the prologue starts... */
19801 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19802 = func_brace_location;
19803 }
19804
19805 return decl;
19806 }
19807 }
19808 else if (parser->fully_implicit_function_template_p)
19809 {
19810 /* A non-template declaration involving a function parameter list
19811 containing an implicit template parameter will be made into a
19812 template. If the resulting declaration is not going to be an
19813 actual function then finish the template scope here to prevent it.
19814 An error message will be issued once we have a decl to talk about.
19815
19816 FIXME probably we should do type deduction rather than create an
19817 implicit template, but the standard currently doesn't allow it. */
19818 bogus_implicit_tmpl = true;
19819 finish_fully_implicit_template (parser, NULL_TREE);
19820 }
19821
19822 /* [dcl.dcl]
19823
19824 Only in function declarations for constructors, destructors, type
19825 conversions, and deduction guides can the decl-specifier-seq be omitted.
19826
19827 We explicitly postpone this check past the point where we handle
19828 function-definitions because we tolerate function-definitions
19829 that are missing their return types in some modes. */
19830 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19831 {
19832 cp_parser_error (parser,
19833 "expected constructor, destructor, or type conversion");
19834 return error_mark_node;
19835 }
19836
19837 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19838 if (token->type == CPP_EQ
19839 || token->type == CPP_OPEN_PAREN
19840 || token->type == CPP_OPEN_BRACE)
19841 {
19842 is_initialized = SD_INITIALIZED;
19843 initialization_kind = token->type;
19844 if (maybe_range_for_decl)
19845 *maybe_range_for_decl = error_mark_node;
19846 tmp_init_loc = token->location;
19847 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19848 *init_loc = tmp_init_loc;
19849
19850 if (token->type == CPP_EQ
19851 && function_declarator_p (declarator))
19852 {
19853 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19854 if (t2->keyword == RID_DEFAULT)
19855 is_initialized = SD_DEFAULTED;
19856 else if (t2->keyword == RID_DELETE)
19857 is_initialized = SD_DELETED;
19858 }
19859 }
19860 else
19861 {
19862 /* If the init-declarator isn't initialized and isn't followed by a
19863 `,' or `;', it's not a valid init-declarator. */
19864 if (token->type != CPP_COMMA
19865 && token->type != CPP_SEMICOLON)
19866 {
19867 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19868 range_for_decl_p = true;
19869 else
19870 {
19871 if (!maybe_range_for_decl)
19872 cp_parser_error (parser, "expected initializer");
19873 return error_mark_node;
19874 }
19875 }
19876 is_initialized = SD_UNINITIALIZED;
19877 initialization_kind = CPP_EOF;
19878 }
19879
19880 /* Because start_decl has side-effects, we should only call it if we
19881 know we're going ahead. By this point, we know that we cannot
19882 possibly be looking at any other construct. */
19883 cp_parser_commit_to_tentative_parse (parser);
19884
19885 /* Enter the newly declared entry in the symbol table. If we're
19886 processing a declaration in a class-specifier, we wait until
19887 after processing the initializer. */
19888 if (!member_p)
19889 {
19890 if (parser->in_unbraced_linkage_specification_p)
19891 decl_specifiers->storage_class = sc_extern;
19892 decl = start_decl (declarator, decl_specifiers,
19893 range_for_decl_p? SD_INITIALIZED : is_initialized,
19894 attributes, prefix_attributes, &pushed_scope);
19895 cp_finalize_omp_declare_simd (parser, decl);
19896 cp_finalize_oacc_routine (parser, decl, false);
19897 /* Adjust location of decl if declarator->id_loc is more appropriate:
19898 set, and decl wasn't merged with another decl, in which case its
19899 location would be different from input_location, and more accurate. */
19900 if (DECL_P (decl)
19901 && declarator->id_loc != UNKNOWN_LOCATION
19902 && DECL_SOURCE_LOCATION (decl) == input_location)
19903 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19904 }
19905 else if (scope)
19906 /* Enter the SCOPE. That way unqualified names appearing in the
19907 initializer will be looked up in SCOPE. */
19908 pushed_scope = push_scope (scope);
19909
19910 /* Perform deferred access control checks, now that we know in which
19911 SCOPE the declared entity resides. */
19912 if (!member_p && decl)
19913 {
19914 tree saved_current_function_decl = NULL_TREE;
19915
19916 /* If the entity being declared is a function, pretend that we
19917 are in its scope. If it is a `friend', it may have access to
19918 things that would not otherwise be accessible. */
19919 if (TREE_CODE (decl) == FUNCTION_DECL)
19920 {
19921 saved_current_function_decl = current_function_decl;
19922 current_function_decl = decl;
19923 }
19924
19925 /* Perform access checks for template parameters. */
19926 cp_parser_perform_template_parameter_access_checks (checks);
19927
19928 /* Perform the access control checks for the declarator and the
19929 decl-specifiers. */
19930 perform_deferred_access_checks (tf_warning_or_error);
19931
19932 /* Restore the saved value. */
19933 if (TREE_CODE (decl) == FUNCTION_DECL)
19934 current_function_decl = saved_current_function_decl;
19935 }
19936
19937 /* Parse the initializer. */
19938 initializer = NULL_TREE;
19939 is_direct_init = false;
19940 is_non_constant_init = true;
19941 if (is_initialized)
19942 {
19943 if (function_declarator_p (declarator))
19944 {
19945 if (initialization_kind == CPP_EQ)
19946 initializer = cp_parser_pure_specifier (parser);
19947 else
19948 {
19949 /* If the declaration was erroneous, we don't really
19950 know what the user intended, so just silently
19951 consume the initializer. */
19952 if (decl != error_mark_node)
19953 error_at (tmp_init_loc, "initializer provided for function");
19954 cp_parser_skip_to_closing_parenthesis (parser,
19955 /*recovering=*/true,
19956 /*or_comma=*/false,
19957 /*consume_paren=*/true);
19958 }
19959 }
19960 else
19961 {
19962 /* We want to record the extra mangling scope for in-class
19963 initializers of class members and initializers of static data
19964 member templates. The former involves deferring
19965 parsing of the initializer until end of class as with default
19966 arguments. So right here we only handle the latter. */
19967 if (!member_p && processing_template_decl && decl != error_mark_node)
19968 start_lambda_scope (decl);
19969 initializer = cp_parser_initializer (parser,
19970 &is_direct_init,
19971 &is_non_constant_init);
19972 if (!member_p && processing_template_decl && decl != error_mark_node)
19973 finish_lambda_scope ();
19974 if (initializer == error_mark_node)
19975 cp_parser_skip_to_end_of_statement (parser);
19976 }
19977 }
19978
19979 /* The old parser allows attributes to appear after a parenthesized
19980 initializer. Mark Mitchell proposed removing this functionality
19981 on the GCC mailing lists on 2002-08-13. This parser accepts the
19982 attributes -- but ignores them. Made a permerror in GCC 8. */
19983 if (cp_parser_allow_gnu_extensions_p (parser)
19984 && initialization_kind == CPP_OPEN_PAREN
19985 && cp_parser_attributes_opt (parser)
19986 && permerror (input_location,
19987 "attributes after parenthesized initializer ignored"))
19988 {
19989 static bool hint;
19990 if (flag_permissive && !hint)
19991 {
19992 hint = true;
19993 inform (input_location,
19994 "this flexibility is deprecated and will be removed");
19995 }
19996 }
19997
19998 /* And now complain about a non-function implicit template. */
19999 if (bogus_implicit_tmpl && decl != error_mark_node)
20000 error_at (DECL_SOURCE_LOCATION (decl),
20001 "non-function %qD declared as implicit template", decl);
20002
20003 /* For an in-class declaration, use `grokfield' to create the
20004 declaration. */
20005 if (member_p)
20006 {
20007 if (pushed_scope)
20008 {
20009 pop_scope (pushed_scope);
20010 pushed_scope = NULL_TREE;
20011 }
20012 decl = grokfield (declarator, decl_specifiers,
20013 initializer, !is_non_constant_init,
20014 /*asmspec=*/NULL_TREE,
20015 attr_chainon (attributes, prefix_attributes));
20016 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20017 cp_parser_save_default_args (parser, decl);
20018 cp_finalize_omp_declare_simd (parser, decl);
20019 cp_finalize_oacc_routine (parser, decl, false);
20020 }
20021
20022 /* Finish processing the declaration. But, skip member
20023 declarations. */
20024 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20025 {
20026 cp_finish_decl (decl,
20027 initializer, !is_non_constant_init,
20028 asm_specification,
20029 /* If the initializer is in parentheses, then this is
20030 a direct-initialization, which means that an
20031 `explicit' constructor is OK. Otherwise, an
20032 `explicit' constructor cannot be used. */
20033 ((is_direct_init || !is_initialized)
20034 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20035 }
20036 else if ((cxx_dialect != cxx98) && friend_p
20037 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20038 /* Core issue #226 (C++0x only): A default template-argument
20039 shall not be specified in a friend class template
20040 declaration. */
20041 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20042 /*is_partial=*/false, /*is_friend_decl=*/1);
20043
20044 if (!friend_p && pushed_scope)
20045 pop_scope (pushed_scope);
20046
20047 if (function_declarator_p (declarator)
20048 && parser->fully_implicit_function_template_p)
20049 {
20050 if (member_p)
20051 decl = finish_fully_implicit_template (parser, decl);
20052 else
20053 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20054 }
20055
20056 if (auto_result && is_initialized && decl_specifiers->type
20057 && type_uses_auto (decl_specifiers->type))
20058 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20059
20060 return decl;
20061 }
20062
20063 /* Parse a declarator.
20064
20065 declarator:
20066 direct-declarator
20067 ptr-operator declarator
20068
20069 abstract-declarator:
20070 ptr-operator abstract-declarator [opt]
20071 direct-abstract-declarator
20072
20073 GNU Extensions:
20074
20075 declarator:
20076 attributes [opt] direct-declarator
20077 attributes [opt] ptr-operator declarator
20078
20079 abstract-declarator:
20080 attributes [opt] ptr-operator abstract-declarator [opt]
20081 attributes [opt] direct-abstract-declarator
20082
20083 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20084 detect constructors, destructors, deduction guides, or conversion operators.
20085 It is set to -1 if the declarator is a name, and +1 if it is a
20086 function. Otherwise it is set to zero. Usually you just want to
20087 test for >0, but internally the negative value is used.
20088
20089 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20090 a decl-specifier-seq unless it declares a constructor, destructor,
20091 or conversion. It might seem that we could check this condition in
20092 semantic analysis, rather than parsing, but that makes it difficult
20093 to handle something like `f()'. We want to notice that there are
20094 no decl-specifiers, and therefore realize that this is an
20095 expression, not a declaration.)
20096
20097 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20098 the declarator is a direct-declarator of the form "(...)".
20099
20100 MEMBER_P is true iff this declarator is a member-declarator.
20101
20102 FRIEND_P is true iff this declarator is a friend. */
20103
20104 static cp_declarator *
20105 cp_parser_declarator (cp_parser* parser,
20106 cp_parser_declarator_kind dcl_kind,
20107 int* ctor_dtor_or_conv_p,
20108 bool* parenthesized_p,
20109 bool member_p, bool friend_p)
20110 {
20111 cp_declarator *declarator;
20112 enum tree_code code;
20113 cp_cv_quals cv_quals;
20114 tree class_type;
20115 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20116
20117 /* Assume this is not a constructor, destructor, or type-conversion
20118 operator. */
20119 if (ctor_dtor_or_conv_p)
20120 *ctor_dtor_or_conv_p = 0;
20121
20122 if (cp_parser_allow_gnu_extensions_p (parser))
20123 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20124
20125 /* Check for the ptr-operator production. */
20126 cp_parser_parse_tentatively (parser);
20127 /* Parse the ptr-operator. */
20128 code = cp_parser_ptr_operator (parser,
20129 &class_type,
20130 &cv_quals,
20131 &std_attributes);
20132
20133 /* If that worked, then we have a ptr-operator. */
20134 if (cp_parser_parse_definitely (parser))
20135 {
20136 /* If a ptr-operator was found, then this declarator was not
20137 parenthesized. */
20138 if (parenthesized_p)
20139 *parenthesized_p = true;
20140 /* The dependent declarator is optional if we are parsing an
20141 abstract-declarator. */
20142 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20143 cp_parser_parse_tentatively (parser);
20144
20145 /* Parse the dependent declarator. */
20146 declarator = cp_parser_declarator (parser, dcl_kind,
20147 /*ctor_dtor_or_conv_p=*/NULL,
20148 /*parenthesized_p=*/NULL,
20149 /*member_p=*/false,
20150 friend_p);
20151
20152 /* If we are parsing an abstract-declarator, we must handle the
20153 case where the dependent declarator is absent. */
20154 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20155 && !cp_parser_parse_definitely (parser))
20156 declarator = NULL;
20157
20158 declarator = cp_parser_make_indirect_declarator
20159 (code, class_type, cv_quals, declarator, std_attributes);
20160 }
20161 /* Everything else is a direct-declarator. */
20162 else
20163 {
20164 if (parenthesized_p)
20165 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20166 CPP_OPEN_PAREN);
20167 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20168 ctor_dtor_or_conv_p,
20169 member_p, friend_p);
20170 }
20171
20172 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20173 declarator->attributes = gnu_attributes;
20174 return declarator;
20175 }
20176
20177 /* Parse a direct-declarator or direct-abstract-declarator.
20178
20179 direct-declarator:
20180 declarator-id
20181 direct-declarator ( parameter-declaration-clause )
20182 cv-qualifier-seq [opt]
20183 ref-qualifier [opt]
20184 exception-specification [opt]
20185 direct-declarator [ constant-expression [opt] ]
20186 ( declarator )
20187
20188 direct-abstract-declarator:
20189 direct-abstract-declarator [opt]
20190 ( parameter-declaration-clause )
20191 cv-qualifier-seq [opt]
20192 ref-qualifier [opt]
20193 exception-specification [opt]
20194 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20195 ( abstract-declarator )
20196
20197 Returns a representation of the declarator. DCL_KIND is
20198 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20199 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20200 we are parsing a direct-declarator. It is
20201 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20202 of ambiguity we prefer an abstract declarator, as per
20203 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20204 as for cp_parser_declarator. */
20205
20206 static cp_declarator *
20207 cp_parser_direct_declarator (cp_parser* parser,
20208 cp_parser_declarator_kind dcl_kind,
20209 int* ctor_dtor_or_conv_p,
20210 bool member_p, bool friend_p)
20211 {
20212 cp_token *token;
20213 cp_declarator *declarator = NULL;
20214 tree scope = NULL_TREE;
20215 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20216 bool saved_in_declarator_p = parser->in_declarator_p;
20217 bool first = true;
20218 tree pushed_scope = NULL_TREE;
20219 cp_token *open_paren = NULL, *close_paren = NULL;
20220
20221 while (true)
20222 {
20223 /* Peek at the next token. */
20224 token = cp_lexer_peek_token (parser->lexer);
20225 if (token->type == CPP_OPEN_PAREN)
20226 {
20227 /* This is either a parameter-declaration-clause, or a
20228 parenthesized declarator. When we know we are parsing a
20229 named declarator, it must be a parenthesized declarator
20230 if FIRST is true. For instance, `(int)' is a
20231 parameter-declaration-clause, with an omitted
20232 direct-abstract-declarator. But `((*))', is a
20233 parenthesized abstract declarator. Finally, when T is a
20234 template parameter `(T)' is a
20235 parameter-declaration-clause, and not a parenthesized
20236 named declarator.
20237
20238 We first try and parse a parameter-declaration-clause,
20239 and then try a nested declarator (if FIRST is true).
20240
20241 It is not an error for it not to be a
20242 parameter-declaration-clause, even when FIRST is
20243 false. Consider,
20244
20245 int i (int);
20246 int i (3);
20247
20248 The first is the declaration of a function while the
20249 second is the definition of a variable, including its
20250 initializer.
20251
20252 Having seen only the parenthesis, we cannot know which of
20253 these two alternatives should be selected. Even more
20254 complex are examples like:
20255
20256 int i (int (a));
20257 int i (int (3));
20258
20259 The former is a function-declaration; the latter is a
20260 variable initialization.
20261
20262 Thus again, we try a parameter-declaration-clause, and if
20263 that fails, we back out and return. */
20264
20265 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20266 {
20267 tree params;
20268 bool is_declarator = false;
20269
20270 open_paren = NULL;
20271
20272 /* In a member-declarator, the only valid interpretation
20273 of a parenthesis is the start of a
20274 parameter-declaration-clause. (It is invalid to
20275 initialize a static data member with a parenthesized
20276 initializer; only the "=" form of initialization is
20277 permitted.) */
20278 if (!member_p)
20279 cp_parser_parse_tentatively (parser);
20280
20281 /* Consume the `('. */
20282 matching_parens parens;
20283 parens.consume_open (parser);
20284 if (first)
20285 {
20286 /* If this is going to be an abstract declarator, we're
20287 in a declarator and we can't have default args. */
20288 parser->default_arg_ok_p = false;
20289 parser->in_declarator_p = true;
20290 }
20291
20292 begin_scope (sk_function_parms, NULL_TREE);
20293
20294 /* Parse the parameter-declaration-clause. */
20295 params = cp_parser_parameter_declaration_clause (parser);
20296
20297 /* Consume the `)'. */
20298 parens.require_close (parser);
20299
20300 /* If all went well, parse the cv-qualifier-seq,
20301 ref-qualifier and the exception-specification. */
20302 if (member_p || cp_parser_parse_definitely (parser))
20303 {
20304 cp_cv_quals cv_quals;
20305 cp_virt_specifiers virt_specifiers;
20306 cp_ref_qualifier ref_qual;
20307 tree exception_specification;
20308 tree late_return;
20309 tree attrs;
20310 bool memfn = (member_p || (pushed_scope
20311 && CLASS_TYPE_P (pushed_scope)));
20312
20313 is_declarator = true;
20314
20315 if (ctor_dtor_or_conv_p)
20316 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20317 first = false;
20318
20319 /* Parse the cv-qualifier-seq. */
20320 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20321 /* Parse the ref-qualifier. */
20322 ref_qual = cp_parser_ref_qualifier_opt (parser);
20323 /* Parse the tx-qualifier. */
20324 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20325 /* And the exception-specification. */
20326 exception_specification
20327 = cp_parser_exception_specification_opt (parser);
20328
20329 attrs = cp_parser_std_attribute_spec_seq (parser);
20330
20331 /* In here, we handle cases where attribute is used after
20332 the function declaration. For example:
20333 void func (int x) __attribute__((vector(..))); */
20334 tree gnu_attrs = NULL_TREE;
20335 tree requires_clause = NULL_TREE;
20336 late_return = (cp_parser_late_return_type_opt
20337 (parser, declarator, requires_clause,
20338 memfn ? cv_quals : -1));
20339
20340 /* Parse the virt-specifier-seq. */
20341 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20342
20343 /* Create the function-declarator. */
20344 declarator = make_call_declarator (declarator,
20345 params,
20346 cv_quals,
20347 virt_specifiers,
20348 ref_qual,
20349 tx_qual,
20350 exception_specification,
20351 late_return,
20352 requires_clause);
20353 declarator->std_attributes = attrs;
20354 declarator->attributes = gnu_attrs;
20355 /* Any subsequent parameter lists are to do with
20356 return type, so are not those of the declared
20357 function. */
20358 parser->default_arg_ok_p = false;
20359 }
20360
20361 /* Remove the function parms from scope. */
20362 pop_bindings_and_leave_scope ();
20363
20364 if (is_declarator)
20365 /* Repeat the main loop. */
20366 continue;
20367 }
20368
20369 /* If this is the first, we can try a parenthesized
20370 declarator. */
20371 if (first)
20372 {
20373 bool saved_in_type_id_in_expr_p;
20374
20375 parser->default_arg_ok_p = saved_default_arg_ok_p;
20376 parser->in_declarator_p = saved_in_declarator_p;
20377
20378 open_paren = token;
20379 /* Consume the `('. */
20380 matching_parens parens;
20381 parens.consume_open (parser);
20382 /* Parse the nested declarator. */
20383 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20384 parser->in_type_id_in_expr_p = true;
20385 declarator
20386 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20387 /*parenthesized_p=*/NULL,
20388 member_p, friend_p);
20389 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20390 first = false;
20391 /* Expect a `)'. */
20392 close_paren = cp_lexer_peek_token (parser->lexer);
20393 if (!parens.require_close (parser))
20394 declarator = cp_error_declarator;
20395 if (declarator == cp_error_declarator)
20396 break;
20397
20398 goto handle_declarator;
20399 }
20400 /* Otherwise, we must be done. */
20401 else
20402 break;
20403 }
20404 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20405 && token->type == CPP_OPEN_SQUARE
20406 && !cp_next_tokens_can_be_attribute_p (parser))
20407 {
20408 /* Parse an array-declarator. */
20409 tree bounds, attrs;
20410
20411 if (ctor_dtor_or_conv_p)
20412 *ctor_dtor_or_conv_p = 0;
20413
20414 open_paren = NULL;
20415 first = false;
20416 parser->default_arg_ok_p = false;
20417 parser->in_declarator_p = true;
20418 /* Consume the `['. */
20419 cp_lexer_consume_token (parser->lexer);
20420 /* Peek at the next token. */
20421 token = cp_lexer_peek_token (parser->lexer);
20422 /* If the next token is `]', then there is no
20423 constant-expression. */
20424 if (token->type != CPP_CLOSE_SQUARE)
20425 {
20426 bool non_constant_p;
20427 bounds
20428 = cp_parser_constant_expression (parser,
20429 /*allow_non_constant=*/true,
20430 &non_constant_p);
20431 if (!non_constant_p)
20432 /* OK */;
20433 else if (error_operand_p (bounds))
20434 /* Already gave an error. */;
20435 else if (!parser->in_function_body
20436 || current_binding_level->kind == sk_function_parms)
20437 {
20438 /* Normally, the array bound must be an integral constant
20439 expression. However, as an extension, we allow VLAs
20440 in function scopes as long as they aren't part of a
20441 parameter declaration. */
20442 cp_parser_error (parser,
20443 "array bound is not an integer constant");
20444 bounds = error_mark_node;
20445 }
20446 else if (processing_template_decl
20447 && !type_dependent_expression_p (bounds))
20448 {
20449 /* Remember this wasn't a constant-expression. */
20450 bounds = build_nop (TREE_TYPE (bounds), bounds);
20451 TREE_SIDE_EFFECTS (bounds) = 1;
20452 }
20453 }
20454 else
20455 bounds = NULL_TREE;
20456 /* Look for the closing `]'. */
20457 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20458 {
20459 declarator = cp_error_declarator;
20460 break;
20461 }
20462
20463 attrs = cp_parser_std_attribute_spec_seq (parser);
20464 declarator = make_array_declarator (declarator, bounds);
20465 declarator->std_attributes = attrs;
20466 }
20467 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20468 {
20469 {
20470 tree qualifying_scope;
20471 tree unqualified_name;
20472 tree attrs;
20473 special_function_kind sfk;
20474 bool abstract_ok;
20475 bool pack_expansion_p = false;
20476 cp_token *declarator_id_start_token;
20477
20478 /* Parse a declarator-id */
20479 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20480 if (abstract_ok)
20481 {
20482 cp_parser_parse_tentatively (parser);
20483
20484 /* If we see an ellipsis, we should be looking at a
20485 parameter pack. */
20486 if (token->type == CPP_ELLIPSIS)
20487 {
20488 /* Consume the `...' */
20489 cp_lexer_consume_token (parser->lexer);
20490
20491 pack_expansion_p = true;
20492 }
20493 }
20494
20495 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20496 unqualified_name
20497 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20498 qualifying_scope = parser->scope;
20499 if (abstract_ok)
20500 {
20501 bool okay = false;
20502
20503 if (!unqualified_name && pack_expansion_p)
20504 {
20505 /* Check whether an error occurred. */
20506 okay = !cp_parser_error_occurred (parser);
20507
20508 /* We already consumed the ellipsis to mark a
20509 parameter pack, but we have no way to report it,
20510 so abort the tentative parse. We will be exiting
20511 immediately anyway. */
20512 cp_parser_abort_tentative_parse (parser);
20513 }
20514 else
20515 okay = cp_parser_parse_definitely (parser);
20516
20517 if (!okay)
20518 unqualified_name = error_mark_node;
20519 else if (unqualified_name
20520 && (qualifying_scope
20521 || (!identifier_p (unqualified_name))))
20522 {
20523 cp_parser_error (parser, "expected unqualified-id");
20524 unqualified_name = error_mark_node;
20525 }
20526 }
20527
20528 if (!unqualified_name)
20529 return NULL;
20530 if (unqualified_name == error_mark_node)
20531 {
20532 declarator = cp_error_declarator;
20533 pack_expansion_p = false;
20534 declarator->parameter_pack_p = false;
20535 break;
20536 }
20537
20538 attrs = cp_parser_std_attribute_spec_seq (parser);
20539
20540 if (qualifying_scope && at_namespace_scope_p ()
20541 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20542 {
20543 /* In the declaration of a member of a template class
20544 outside of the class itself, the SCOPE will sometimes
20545 be a TYPENAME_TYPE. For example, given:
20546
20547 template <typename T>
20548 int S<T>::R::i = 3;
20549
20550 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20551 this context, we must resolve S<T>::R to an ordinary
20552 type, rather than a typename type.
20553
20554 The reason we normally avoid resolving TYPENAME_TYPEs
20555 is that a specialization of `S' might render
20556 `S<T>::R' not a type. However, if `S' is
20557 specialized, then this `i' will not be used, so there
20558 is no harm in resolving the types here. */
20559 tree type;
20560
20561 /* Resolve the TYPENAME_TYPE. */
20562 type = resolve_typename_type (qualifying_scope,
20563 /*only_current_p=*/false);
20564 /* If that failed, the declarator is invalid. */
20565 if (TREE_CODE (type) == TYPENAME_TYPE)
20566 {
20567 if (typedef_variant_p (type))
20568 error_at (declarator_id_start_token->location,
20569 "cannot define member of dependent typedef "
20570 "%qT", type);
20571 else
20572 error_at (declarator_id_start_token->location,
20573 "%<%T::%E%> is not a type",
20574 TYPE_CONTEXT (qualifying_scope),
20575 TYPE_IDENTIFIER (qualifying_scope));
20576 }
20577 qualifying_scope = type;
20578 }
20579
20580 sfk = sfk_none;
20581
20582 if (unqualified_name)
20583 {
20584 tree class_type;
20585
20586 if (qualifying_scope
20587 && CLASS_TYPE_P (qualifying_scope))
20588 class_type = qualifying_scope;
20589 else
20590 class_type = current_class_type;
20591
20592 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20593 {
20594 tree name_type = TREE_TYPE (unqualified_name);
20595
20596 if (!class_type || !same_type_p (name_type, class_type))
20597 {
20598 /* We do not attempt to print the declarator
20599 here because we do not have enough
20600 information about its original syntactic
20601 form. */
20602 cp_parser_error (parser, "invalid declarator");
20603 declarator = cp_error_declarator;
20604 break;
20605 }
20606 else if (qualifying_scope
20607 && CLASSTYPE_USE_TEMPLATE (name_type))
20608 {
20609 error_at (declarator_id_start_token->location,
20610 "invalid use of constructor as a template");
20611 inform (declarator_id_start_token->location,
20612 "use %<%T::%D%> instead of %<%T::%D%> to "
20613 "name the constructor in a qualified name",
20614 class_type,
20615 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20616 class_type, name_type);
20617 declarator = cp_error_declarator;
20618 break;
20619 }
20620 unqualified_name = constructor_name (class_type);
20621 }
20622
20623 if (class_type)
20624 {
20625 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20626 sfk = sfk_destructor;
20627 else if (identifier_p (unqualified_name)
20628 && IDENTIFIER_CONV_OP_P (unqualified_name))
20629 sfk = sfk_conversion;
20630 else if (/* There's no way to declare a constructor
20631 for an unnamed type, even if the type
20632 got a name for linkage purposes. */
20633 !TYPE_WAS_UNNAMED (class_type)
20634 /* Handle correctly (c++/19200):
20635
20636 struct S {
20637 struct T{};
20638 friend void S(T);
20639 };
20640
20641 and also:
20642
20643 namespace N {
20644 void S();
20645 }
20646
20647 struct S {
20648 friend void N::S();
20649 }; */
20650 && (!friend_p || class_type == qualifying_scope)
20651 && constructor_name_p (unqualified_name,
20652 class_type))
20653 sfk = sfk_constructor;
20654 else if (is_overloaded_fn (unqualified_name)
20655 && DECL_CONSTRUCTOR_P (get_first_fn
20656 (unqualified_name)))
20657 sfk = sfk_constructor;
20658
20659 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20660 *ctor_dtor_or_conv_p = -1;
20661 }
20662 }
20663 declarator = make_id_declarator (qualifying_scope,
20664 unqualified_name,
20665 sfk);
20666 declarator->std_attributes = attrs;
20667 declarator->id_loc = token->location;
20668 declarator->parameter_pack_p = pack_expansion_p;
20669
20670 if (pack_expansion_p)
20671 maybe_warn_variadic_templates ();
20672 }
20673
20674 handle_declarator:;
20675 scope = get_scope_of_declarator (declarator);
20676 if (scope)
20677 {
20678 /* Any names that appear after the declarator-id for a
20679 member are looked up in the containing scope. */
20680 if (at_function_scope_p ())
20681 {
20682 /* But declarations with qualified-ids can't appear in a
20683 function. */
20684 cp_parser_error (parser, "qualified-id in declaration");
20685 declarator = cp_error_declarator;
20686 break;
20687 }
20688 pushed_scope = push_scope (scope);
20689 }
20690 parser->in_declarator_p = true;
20691 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20692 || (declarator && declarator->kind == cdk_id))
20693 /* Default args are only allowed on function
20694 declarations. */
20695 parser->default_arg_ok_p = saved_default_arg_ok_p;
20696 else
20697 parser->default_arg_ok_p = false;
20698
20699 first = false;
20700 }
20701 /* We're done. */
20702 else
20703 break;
20704 }
20705
20706 /* For an abstract declarator, we might wind up with nothing at this
20707 point. That's an error; the declarator is not optional. */
20708 if (!declarator)
20709 cp_parser_error (parser, "expected declarator");
20710 else if (open_paren)
20711 {
20712 /* Record overly parenthesized declarator so we can give a
20713 diagnostic about confusing decl/expr disambiguation. */
20714 if (declarator->kind == cdk_array)
20715 {
20716 /* If the open and close parens are on different lines, this
20717 is probably a formatting thing, so ignore. */
20718 expanded_location open = expand_location (open_paren->location);
20719 expanded_location close = expand_location (close_paren->location);
20720 if (open.line != close.line || open.file != close.file)
20721 open_paren = NULL;
20722 }
20723 if (open_paren)
20724 declarator->parenthesized = open_paren->location;
20725 }
20726
20727 /* If we entered a scope, we must exit it now. */
20728 if (pushed_scope)
20729 pop_scope (pushed_scope);
20730
20731 parser->default_arg_ok_p = saved_default_arg_ok_p;
20732 parser->in_declarator_p = saved_in_declarator_p;
20733
20734 return declarator;
20735 }
20736
20737 /* Parse a ptr-operator.
20738
20739 ptr-operator:
20740 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20741 * cv-qualifier-seq [opt]
20742 &
20743 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20744 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20745
20746 GNU Extension:
20747
20748 ptr-operator:
20749 & cv-qualifier-seq [opt]
20750
20751 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20752 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20753 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20754 filled in with the TYPE containing the member. *CV_QUALS is
20755 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20756 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20757 Note that the tree codes returned by this function have nothing
20758 to do with the types of trees that will be eventually be created
20759 to represent the pointer or reference type being parsed. They are
20760 just constants with suggestive names. */
20761 static enum tree_code
20762 cp_parser_ptr_operator (cp_parser* parser,
20763 tree* type,
20764 cp_cv_quals *cv_quals,
20765 tree *attributes)
20766 {
20767 enum tree_code code = ERROR_MARK;
20768 cp_token *token;
20769 tree attrs = NULL_TREE;
20770
20771 /* Assume that it's not a pointer-to-member. */
20772 *type = NULL_TREE;
20773 /* And that there are no cv-qualifiers. */
20774 *cv_quals = TYPE_UNQUALIFIED;
20775
20776 /* Peek at the next token. */
20777 token = cp_lexer_peek_token (parser->lexer);
20778
20779 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20780 if (token->type == CPP_MULT)
20781 code = INDIRECT_REF;
20782 else if (token->type == CPP_AND)
20783 code = ADDR_EXPR;
20784 else if ((cxx_dialect != cxx98) &&
20785 token->type == CPP_AND_AND) /* C++0x only */
20786 code = NON_LVALUE_EXPR;
20787
20788 if (code != ERROR_MARK)
20789 {
20790 /* Consume the `*', `&' or `&&'. */
20791 cp_lexer_consume_token (parser->lexer);
20792
20793 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20794 `&', if we are allowing GNU extensions. (The only qualifier
20795 that can legally appear after `&' is `restrict', but that is
20796 enforced during semantic analysis. */
20797 if (code == INDIRECT_REF
20798 || cp_parser_allow_gnu_extensions_p (parser))
20799 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20800
20801 attrs = cp_parser_std_attribute_spec_seq (parser);
20802 if (attributes != NULL)
20803 *attributes = attrs;
20804 }
20805 else
20806 {
20807 /* Try the pointer-to-member case. */
20808 cp_parser_parse_tentatively (parser);
20809 /* Look for the optional `::' operator. */
20810 cp_parser_global_scope_opt (parser,
20811 /*current_scope_valid_p=*/false);
20812 /* Look for the nested-name specifier. */
20813 token = cp_lexer_peek_token (parser->lexer);
20814 cp_parser_nested_name_specifier (parser,
20815 /*typename_keyword_p=*/false,
20816 /*check_dependency_p=*/true,
20817 /*type_p=*/false,
20818 /*is_declaration=*/false);
20819 /* If we found it, and the next token is a `*', then we are
20820 indeed looking at a pointer-to-member operator. */
20821 if (!cp_parser_error_occurred (parser)
20822 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20823 {
20824 /* Indicate that the `*' operator was used. */
20825 code = INDIRECT_REF;
20826
20827 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20828 error_at (token->location, "%qD is a namespace", parser->scope);
20829 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20830 error_at (token->location, "cannot form pointer to member of "
20831 "non-class %q#T", parser->scope);
20832 else
20833 {
20834 /* The type of which the member is a member is given by the
20835 current SCOPE. */
20836 *type = parser->scope;
20837 /* The next name will not be qualified. */
20838 parser->scope = NULL_TREE;
20839 parser->qualifying_scope = NULL_TREE;
20840 parser->object_scope = NULL_TREE;
20841 /* Look for optional c++11 attributes. */
20842 attrs = cp_parser_std_attribute_spec_seq (parser);
20843 if (attributes != NULL)
20844 *attributes = attrs;
20845 /* Look for the optional cv-qualifier-seq. */
20846 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20847 }
20848 }
20849 /* If that didn't work we don't have a ptr-operator. */
20850 if (!cp_parser_parse_definitely (parser))
20851 cp_parser_error (parser, "expected ptr-operator");
20852 }
20853
20854 return code;
20855 }
20856
20857 /* Parse an (optional) cv-qualifier-seq.
20858
20859 cv-qualifier-seq:
20860 cv-qualifier cv-qualifier-seq [opt]
20861
20862 cv-qualifier:
20863 const
20864 volatile
20865
20866 GNU Extension:
20867
20868 cv-qualifier:
20869 __restrict__
20870
20871 Returns a bitmask representing the cv-qualifiers. */
20872
20873 static cp_cv_quals
20874 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20875 {
20876 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20877
20878 while (true)
20879 {
20880 cp_token *token;
20881 cp_cv_quals cv_qualifier;
20882
20883 /* Peek at the next token. */
20884 token = cp_lexer_peek_token (parser->lexer);
20885 /* See if it's a cv-qualifier. */
20886 switch (token->keyword)
20887 {
20888 case RID_CONST:
20889 cv_qualifier = TYPE_QUAL_CONST;
20890 break;
20891
20892 case RID_VOLATILE:
20893 cv_qualifier = TYPE_QUAL_VOLATILE;
20894 break;
20895
20896 case RID_RESTRICT:
20897 cv_qualifier = TYPE_QUAL_RESTRICT;
20898 break;
20899
20900 default:
20901 cv_qualifier = TYPE_UNQUALIFIED;
20902 break;
20903 }
20904
20905 if (!cv_qualifier)
20906 break;
20907
20908 if (cv_quals & cv_qualifier)
20909 {
20910 gcc_rich_location richloc (token->location);
20911 richloc.add_fixit_remove ();
20912 error_at (&richloc, "duplicate cv-qualifier");
20913 cp_lexer_purge_token (parser->lexer);
20914 }
20915 else
20916 {
20917 cp_lexer_consume_token (parser->lexer);
20918 cv_quals |= cv_qualifier;
20919 }
20920 }
20921
20922 return cv_quals;
20923 }
20924
20925 /* Parse an (optional) ref-qualifier
20926
20927 ref-qualifier:
20928 &
20929 &&
20930
20931 Returns cp_ref_qualifier representing ref-qualifier. */
20932
20933 static cp_ref_qualifier
20934 cp_parser_ref_qualifier_opt (cp_parser* parser)
20935 {
20936 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20937
20938 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20939 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20940 return ref_qual;
20941
20942 while (true)
20943 {
20944 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20945 cp_token *token = cp_lexer_peek_token (parser->lexer);
20946
20947 switch (token->type)
20948 {
20949 case CPP_AND:
20950 curr_ref_qual = REF_QUAL_LVALUE;
20951 break;
20952
20953 case CPP_AND_AND:
20954 curr_ref_qual = REF_QUAL_RVALUE;
20955 break;
20956
20957 default:
20958 curr_ref_qual = REF_QUAL_NONE;
20959 break;
20960 }
20961
20962 if (!curr_ref_qual)
20963 break;
20964 else if (ref_qual)
20965 {
20966 error_at (token->location, "multiple ref-qualifiers");
20967 cp_lexer_purge_token (parser->lexer);
20968 }
20969 else
20970 {
20971 ref_qual = curr_ref_qual;
20972 cp_lexer_consume_token (parser->lexer);
20973 }
20974 }
20975
20976 return ref_qual;
20977 }
20978
20979 /* Parse an optional tx-qualifier.
20980
20981 tx-qualifier:
20982 transaction_safe
20983 transaction_safe_dynamic */
20984
20985 static tree
20986 cp_parser_tx_qualifier_opt (cp_parser *parser)
20987 {
20988 cp_token *token = cp_lexer_peek_token (parser->lexer);
20989 if (token->type == CPP_NAME)
20990 {
20991 tree name = token->u.value;
20992 const char *p = IDENTIFIER_POINTER (name);
20993 const int len = strlen ("transaction_safe");
20994 if (!strncmp (p, "transaction_safe", len))
20995 {
20996 p += len;
20997 if (*p == '\0'
20998 || !strcmp (p, "_dynamic"))
20999 {
21000 cp_lexer_consume_token (parser->lexer);
21001 if (!flag_tm)
21002 {
21003 error ("%qE requires %<-fgnu-tm%>", name);
21004 return NULL_TREE;
21005 }
21006 else
21007 return name;
21008 }
21009 }
21010 }
21011 return NULL_TREE;
21012 }
21013
21014 /* Parse an (optional) virt-specifier-seq.
21015
21016 virt-specifier-seq:
21017 virt-specifier virt-specifier-seq [opt]
21018
21019 virt-specifier:
21020 override
21021 final
21022
21023 Returns a bitmask representing the virt-specifiers. */
21024
21025 static cp_virt_specifiers
21026 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21027 {
21028 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21029
21030 while (true)
21031 {
21032 cp_token *token;
21033 cp_virt_specifiers virt_specifier;
21034
21035 /* Peek at the next token. */
21036 token = cp_lexer_peek_token (parser->lexer);
21037 /* See if it's a virt-specifier-qualifier. */
21038 if (token->type != CPP_NAME)
21039 break;
21040 if (id_equal (token->u.value, "override"))
21041 {
21042 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21043 virt_specifier = VIRT_SPEC_OVERRIDE;
21044 }
21045 else if (id_equal (token->u.value, "final"))
21046 {
21047 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21048 virt_specifier = VIRT_SPEC_FINAL;
21049 }
21050 else if (id_equal (token->u.value, "__final"))
21051 {
21052 virt_specifier = VIRT_SPEC_FINAL;
21053 }
21054 else
21055 break;
21056
21057 if (virt_specifiers & virt_specifier)
21058 {
21059 gcc_rich_location richloc (token->location);
21060 richloc.add_fixit_remove ();
21061 error_at (&richloc, "duplicate virt-specifier");
21062 cp_lexer_purge_token (parser->lexer);
21063 }
21064 else
21065 {
21066 cp_lexer_consume_token (parser->lexer);
21067 virt_specifiers |= virt_specifier;
21068 }
21069 }
21070 return virt_specifiers;
21071 }
21072
21073 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21074 is in scope even though it isn't real. */
21075
21076 void
21077 inject_this_parameter (tree ctype, cp_cv_quals quals)
21078 {
21079 tree this_parm;
21080
21081 if (current_class_ptr)
21082 {
21083 /* We don't clear this between NSDMIs. Is it already what we want? */
21084 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21085 if (DECL_P (current_class_ptr)
21086 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21087 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21088 && cp_type_quals (type) == quals)
21089 return;
21090 }
21091
21092 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21093 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21094 current_class_ptr = NULL_TREE;
21095 current_class_ref
21096 = cp_build_fold_indirect_ref (this_parm);
21097 current_class_ptr = this_parm;
21098 }
21099
21100 /* Return true iff our current scope is a non-static data member
21101 initializer. */
21102
21103 bool
21104 parsing_nsdmi (void)
21105 {
21106 /* We recognize NSDMI context by the context-less 'this' pointer set up
21107 by the function above. */
21108 if (current_class_ptr
21109 && TREE_CODE (current_class_ptr) == PARM_DECL
21110 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21111 return true;
21112 return false;
21113 }
21114
21115 /* Parse a late-specified return type, if any. This is not a separate
21116 non-terminal, but part of a function declarator, which looks like
21117
21118 -> trailing-type-specifier-seq abstract-declarator(opt)
21119
21120 Returns the type indicated by the type-id.
21121
21122 In addition to this, parse any queued up #pragma omp declare simd
21123 clauses, and #pragma acc routine clauses.
21124
21125 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21126 function. */
21127
21128 static tree
21129 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21130 tree& requires_clause, cp_cv_quals quals)
21131 {
21132 cp_token *token;
21133 tree type = NULL_TREE;
21134 bool declare_simd_p = (parser->omp_declare_simd
21135 && declarator
21136 && declarator->kind == cdk_id);
21137
21138 bool oacc_routine_p = (parser->oacc_routine
21139 && declarator
21140 && declarator->kind == cdk_id);
21141
21142 /* Peek at the next token. */
21143 token = cp_lexer_peek_token (parser->lexer);
21144 /* A late-specified return type is indicated by an initial '->'. */
21145 if (token->type != CPP_DEREF
21146 && token->keyword != RID_REQUIRES
21147 && !(token->type == CPP_NAME
21148 && token->u.value == ridpointers[RID_REQUIRES])
21149 && !(declare_simd_p || oacc_routine_p))
21150 return NULL_TREE;
21151
21152 tree save_ccp = current_class_ptr;
21153 tree save_ccr = current_class_ref;
21154 if (quals >= 0)
21155 {
21156 /* DR 1207: 'this' is in scope in the trailing return type. */
21157 inject_this_parameter (current_class_type, quals);
21158 }
21159
21160 if (token->type == CPP_DEREF)
21161 {
21162 /* Consume the ->. */
21163 cp_lexer_consume_token (parser->lexer);
21164
21165 type = cp_parser_trailing_type_id (parser);
21166 }
21167
21168 /* Function declarations may be followed by a trailing
21169 requires-clause. */
21170 requires_clause = cp_parser_requires_clause_opt (parser);
21171
21172 if (declare_simd_p)
21173 declarator->attributes
21174 = cp_parser_late_parsing_omp_declare_simd (parser,
21175 declarator->attributes);
21176 if (oacc_routine_p)
21177 declarator->attributes
21178 = cp_parser_late_parsing_oacc_routine (parser,
21179 declarator->attributes);
21180
21181 if (quals >= 0)
21182 {
21183 current_class_ptr = save_ccp;
21184 current_class_ref = save_ccr;
21185 }
21186
21187 return type;
21188 }
21189
21190 /* Parse a declarator-id.
21191
21192 declarator-id:
21193 id-expression
21194 :: [opt] nested-name-specifier [opt] type-name
21195
21196 In the `id-expression' case, the value returned is as for
21197 cp_parser_id_expression if the id-expression was an unqualified-id.
21198 If the id-expression was a qualified-id, then a SCOPE_REF is
21199 returned. The first operand is the scope (either a NAMESPACE_DECL
21200 or TREE_TYPE), but the second is still just a representation of an
21201 unqualified-id. */
21202
21203 static tree
21204 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21205 {
21206 tree id;
21207 /* The expression must be an id-expression. Assume that qualified
21208 names are the names of types so that:
21209
21210 template <class T>
21211 int S<T>::R::i = 3;
21212
21213 will work; we must treat `S<T>::R' as the name of a type.
21214 Similarly, assume that qualified names are templates, where
21215 required, so that:
21216
21217 template <class T>
21218 int S<T>::R<T>::i = 3;
21219
21220 will work, too. */
21221 id = cp_parser_id_expression (parser,
21222 /*template_keyword_p=*/false,
21223 /*check_dependency_p=*/false,
21224 /*template_p=*/NULL,
21225 /*declarator_p=*/true,
21226 optional_p);
21227 if (id && BASELINK_P (id))
21228 id = BASELINK_FUNCTIONS (id);
21229 return id;
21230 }
21231
21232 /* Parse a type-id.
21233
21234 type-id:
21235 type-specifier-seq abstract-declarator [opt]
21236
21237 Returns the TYPE specified. */
21238
21239 static tree
21240 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21241 bool is_trailing_return, location_t * type_location)
21242 {
21243 cp_decl_specifier_seq type_specifier_seq;
21244 cp_declarator *abstract_declarator;
21245
21246 /* Parse the type-specifier-seq. */
21247 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21248 is_trailing_return,
21249 &type_specifier_seq);
21250 if (type_location)
21251 *type_location = type_specifier_seq.locations[ds_type_spec];
21252
21253 if (is_template_arg && type_specifier_seq.type
21254 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21255 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21256 /* A bare template name as a template argument is a template template
21257 argument, not a placeholder, so fail parsing it as a type argument. */
21258 {
21259 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21260 cp_parser_simulate_error (parser);
21261 return error_mark_node;
21262 }
21263 if (type_specifier_seq.type == error_mark_node)
21264 return error_mark_node;
21265
21266 /* There might or might not be an abstract declarator. */
21267 cp_parser_parse_tentatively (parser);
21268 /* Look for the declarator. */
21269 abstract_declarator
21270 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21271 /*parenthesized_p=*/NULL,
21272 /*member_p=*/false,
21273 /*friend_p=*/false);
21274 /* Check to see if there really was a declarator. */
21275 if (!cp_parser_parse_definitely (parser))
21276 abstract_declarator = NULL;
21277
21278 if (type_specifier_seq.type
21279 /* The concepts TS allows 'auto' as a type-id. */
21280 && (!flag_concepts || parser->in_type_id_in_expr_p)
21281 /* None of the valid uses of 'auto' in C++14 involve the type-id
21282 nonterminal, but it is valid in a trailing-return-type. */
21283 && !(cxx_dialect >= cxx14 && is_trailing_return))
21284 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21285 {
21286 /* A type-id with type 'auto' is only ok if the abstract declarator
21287 is a function declarator with a late-specified return type.
21288
21289 A type-id with 'auto' is also valid in a trailing-return-type
21290 in a compound-requirement. */
21291 if (abstract_declarator
21292 && abstract_declarator->kind == cdk_function
21293 && abstract_declarator->u.function.late_return_type)
21294 /* OK */;
21295 else if (parser->in_result_type_constraint_p)
21296 /* OK */;
21297 else
21298 {
21299 location_t loc = type_specifier_seq.locations[ds_type_spec];
21300 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21301 {
21302 error_at (loc, "missing template arguments after %qT",
21303 auto_node);
21304 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21305 tmpl);
21306 }
21307 else
21308 error_at (loc, "invalid use of %qT", auto_node);
21309 return error_mark_node;
21310 }
21311 }
21312
21313 return groktypename (&type_specifier_seq, abstract_declarator,
21314 is_template_arg);
21315 }
21316
21317 static tree
21318 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21319 {
21320 return cp_parser_type_id_1 (parser, false, false, type_location);
21321 }
21322
21323 static tree
21324 cp_parser_template_type_arg (cp_parser *parser)
21325 {
21326 tree r;
21327 const char *saved_message = parser->type_definition_forbidden_message;
21328 parser->type_definition_forbidden_message
21329 = G_("types may not be defined in template arguments");
21330 r = cp_parser_type_id_1 (parser, true, false, NULL);
21331 parser->type_definition_forbidden_message = saved_message;
21332 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21333 {
21334 error ("invalid use of %<auto%> in template argument");
21335 r = error_mark_node;
21336 }
21337 return r;
21338 }
21339
21340 static tree
21341 cp_parser_trailing_type_id (cp_parser *parser)
21342 {
21343 return cp_parser_type_id_1 (parser, false, true, NULL);
21344 }
21345
21346 /* Parse a type-specifier-seq.
21347
21348 type-specifier-seq:
21349 type-specifier type-specifier-seq [opt]
21350
21351 GNU extension:
21352
21353 type-specifier-seq:
21354 attributes type-specifier-seq [opt]
21355
21356 If IS_DECLARATION is true, we are at the start of a "condition" or
21357 exception-declaration, so we might be followed by a declarator-id.
21358
21359 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21360 i.e. we've just seen "->".
21361
21362 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21363
21364 static void
21365 cp_parser_type_specifier_seq (cp_parser* parser,
21366 bool is_declaration,
21367 bool is_trailing_return,
21368 cp_decl_specifier_seq *type_specifier_seq)
21369 {
21370 bool seen_type_specifier = false;
21371 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21372 cp_token *start_token = NULL;
21373
21374 /* Clear the TYPE_SPECIFIER_SEQ. */
21375 clear_decl_specs (type_specifier_seq);
21376
21377 /* In the context of a trailing return type, enum E { } is an
21378 elaborated-type-specifier followed by a function-body, not an
21379 enum-specifier. */
21380 if (is_trailing_return)
21381 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21382
21383 /* Parse the type-specifiers and attributes. */
21384 while (true)
21385 {
21386 tree type_specifier;
21387 bool is_cv_qualifier;
21388
21389 /* Check for attributes first. */
21390 if (cp_next_tokens_can_be_attribute_p (parser))
21391 {
21392 type_specifier_seq->attributes
21393 = attr_chainon (type_specifier_seq->attributes,
21394 cp_parser_attributes_opt (parser));
21395 continue;
21396 }
21397
21398 /* record the token of the beginning of the type specifier seq,
21399 for error reporting purposes*/
21400 if (!start_token)
21401 start_token = cp_lexer_peek_token (parser->lexer);
21402
21403 /* Look for the type-specifier. */
21404 type_specifier = cp_parser_type_specifier (parser,
21405 flags,
21406 type_specifier_seq,
21407 /*is_declaration=*/false,
21408 NULL,
21409 &is_cv_qualifier);
21410 if (!type_specifier)
21411 {
21412 /* If the first type-specifier could not be found, this is not a
21413 type-specifier-seq at all. */
21414 if (!seen_type_specifier)
21415 {
21416 /* Set in_declarator_p to avoid skipping to the semicolon. */
21417 int in_decl = parser->in_declarator_p;
21418 parser->in_declarator_p = true;
21419
21420 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21421 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21422 cp_parser_error (parser, "expected type-specifier");
21423
21424 parser->in_declarator_p = in_decl;
21425
21426 type_specifier_seq->type = error_mark_node;
21427 return;
21428 }
21429 /* If subsequent type-specifiers could not be found, the
21430 type-specifier-seq is complete. */
21431 break;
21432 }
21433
21434 seen_type_specifier = true;
21435 /* The standard says that a condition can be:
21436
21437 type-specifier-seq declarator = assignment-expression
21438
21439 However, given:
21440
21441 struct S {};
21442 if (int S = ...)
21443
21444 we should treat the "S" as a declarator, not as a
21445 type-specifier. The standard doesn't say that explicitly for
21446 type-specifier-seq, but it does say that for
21447 decl-specifier-seq in an ordinary declaration. Perhaps it
21448 would be clearer just to allow a decl-specifier-seq here, and
21449 then add a semantic restriction that if any decl-specifiers
21450 that are not type-specifiers appear, the program is invalid. */
21451 if (is_declaration && !is_cv_qualifier)
21452 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21453 }
21454 }
21455
21456 /* Return whether the function currently being declared has an associated
21457 template parameter list. */
21458
21459 static bool
21460 function_being_declared_is_template_p (cp_parser* parser)
21461 {
21462 if (!current_template_parms || processing_template_parmlist)
21463 return false;
21464
21465 if (parser->implicit_template_scope)
21466 return true;
21467
21468 if (at_class_scope_p ()
21469 && TYPE_BEING_DEFINED (current_class_type))
21470 return parser->num_template_parameter_lists != 0;
21471
21472 return ((int) parser->num_template_parameter_lists > template_class_depth
21473 (current_class_type));
21474 }
21475
21476 /* Parse a parameter-declaration-clause.
21477
21478 parameter-declaration-clause:
21479 parameter-declaration-list [opt] ... [opt]
21480 parameter-declaration-list , ...
21481
21482 Returns a representation for the parameter declarations. A return
21483 value of NULL indicates a parameter-declaration-clause consisting
21484 only of an ellipsis. */
21485
21486 static tree
21487 cp_parser_parameter_declaration_clause (cp_parser* parser)
21488 {
21489 tree parameters;
21490 cp_token *token;
21491 bool ellipsis_p;
21492
21493 temp_override<bool> cleanup
21494 (parser->auto_is_implicit_function_template_parm_p);
21495
21496 if (!processing_specialization
21497 && !processing_template_parmlist
21498 && !processing_explicit_instantiation
21499 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21500 actual function or a random abstract declarator. */
21501 && parser->default_arg_ok_p)
21502 if (!current_function_decl
21503 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21504 parser->auto_is_implicit_function_template_parm_p = true;
21505
21506 /* Peek at the next token. */
21507 token = cp_lexer_peek_token (parser->lexer);
21508 /* Check for trivial parameter-declaration-clauses. */
21509 if (token->type == CPP_ELLIPSIS)
21510 {
21511 /* Consume the `...' token. */
21512 cp_lexer_consume_token (parser->lexer);
21513 return NULL_TREE;
21514 }
21515 else if (token->type == CPP_CLOSE_PAREN)
21516 /* There are no parameters. */
21517 return void_list_node;
21518 /* Check for `(void)', too, which is a special case. */
21519 else if (token->keyword == RID_VOID
21520 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21521 == CPP_CLOSE_PAREN))
21522 {
21523 /* Consume the `void' token. */
21524 cp_lexer_consume_token (parser->lexer);
21525 /* There are no parameters. */
21526 return void_list_node;
21527 }
21528
21529 /* Parse the parameter-declaration-list. */
21530 parameters = cp_parser_parameter_declaration_list (parser);
21531 /* If a parse error occurred while parsing the
21532 parameter-declaration-list, then the entire
21533 parameter-declaration-clause is erroneous. */
21534 if (parameters == error_mark_node)
21535 return NULL_TREE;
21536
21537 /* Peek at the next token. */
21538 token = cp_lexer_peek_token (parser->lexer);
21539 /* If it's a `,', the clause should terminate with an ellipsis. */
21540 if (token->type == CPP_COMMA)
21541 {
21542 /* Consume the `,'. */
21543 cp_lexer_consume_token (parser->lexer);
21544 /* Expect an ellipsis. */
21545 ellipsis_p
21546 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21547 }
21548 /* It might also be `...' if the optional trailing `,' was
21549 omitted. */
21550 else if (token->type == CPP_ELLIPSIS)
21551 {
21552 /* Consume the `...' token. */
21553 cp_lexer_consume_token (parser->lexer);
21554 /* And remember that we saw it. */
21555 ellipsis_p = true;
21556 }
21557 else
21558 ellipsis_p = false;
21559
21560 /* Finish the parameter list. */
21561 if (!ellipsis_p)
21562 parameters = chainon (parameters, void_list_node);
21563
21564 return parameters;
21565 }
21566
21567 /* Parse a parameter-declaration-list.
21568
21569 parameter-declaration-list:
21570 parameter-declaration
21571 parameter-declaration-list , parameter-declaration
21572
21573 Returns a representation of the parameter-declaration-list, as for
21574 cp_parser_parameter_declaration_clause. However, the
21575 `void_list_node' is never appended to the list. */
21576
21577 static tree
21578 cp_parser_parameter_declaration_list (cp_parser* parser)
21579 {
21580 tree parameters = NULL_TREE;
21581 tree *tail = &parameters;
21582 bool saved_in_unbraced_linkage_specification_p;
21583 int index = 0;
21584
21585 /* The special considerations that apply to a function within an
21586 unbraced linkage specifications do not apply to the parameters
21587 to the function. */
21588 saved_in_unbraced_linkage_specification_p
21589 = parser->in_unbraced_linkage_specification_p;
21590 parser->in_unbraced_linkage_specification_p = false;
21591
21592 /* Look for more parameters. */
21593 while (true)
21594 {
21595 cp_parameter_declarator *parameter;
21596 tree decl = error_mark_node;
21597 bool parenthesized_p = false;
21598
21599 /* Parse the parameter. */
21600 parameter
21601 = cp_parser_parameter_declaration (parser,
21602 /*template_parm_p=*/false,
21603 &parenthesized_p);
21604
21605 /* We don't know yet if the enclosing context is deprecated, so wait
21606 and warn in grokparms if appropriate. */
21607 deprecated_state = DEPRECATED_SUPPRESS;
21608
21609 if (parameter)
21610 {
21611 decl = grokdeclarator (parameter->declarator,
21612 &parameter->decl_specifiers,
21613 PARM,
21614 parameter->default_argument != NULL_TREE,
21615 &parameter->decl_specifiers.attributes);
21616 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21617 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21618 }
21619
21620 deprecated_state = DEPRECATED_NORMAL;
21621
21622 /* If a parse error occurred parsing the parameter declaration,
21623 then the entire parameter-declaration-list is erroneous. */
21624 if (decl == error_mark_node)
21625 {
21626 parameters = error_mark_node;
21627 break;
21628 }
21629
21630 if (parameter->decl_specifiers.attributes)
21631 cplus_decl_attributes (&decl,
21632 parameter->decl_specifiers.attributes,
21633 0);
21634 if (DECL_NAME (decl))
21635 decl = pushdecl (decl);
21636
21637 if (decl != error_mark_node)
21638 {
21639 retrofit_lang_decl (decl);
21640 DECL_PARM_INDEX (decl) = ++index;
21641 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21642 }
21643
21644 /* Add the new parameter to the list. */
21645 *tail = build_tree_list (parameter->default_argument, decl);
21646 tail = &TREE_CHAIN (*tail);
21647
21648 /* Peek at the next token. */
21649 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21650 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21651 /* These are for Objective-C++ */
21652 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21653 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21654 /* The parameter-declaration-list is complete. */
21655 break;
21656 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21657 {
21658 cp_token *token;
21659
21660 /* Peek at the next token. */
21661 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21662 /* If it's an ellipsis, then the list is complete. */
21663 if (token->type == CPP_ELLIPSIS)
21664 break;
21665 /* Otherwise, there must be more parameters. Consume the
21666 `,'. */
21667 cp_lexer_consume_token (parser->lexer);
21668 /* When parsing something like:
21669
21670 int i(float f, double d)
21671
21672 we can tell after seeing the declaration for "f" that we
21673 are not looking at an initialization of a variable "i",
21674 but rather at the declaration of a function "i".
21675
21676 Due to the fact that the parsing of template arguments
21677 (as specified to a template-id) requires backtracking we
21678 cannot use this technique when inside a template argument
21679 list. */
21680 if (!parser->in_template_argument_list_p
21681 && !parser->in_type_id_in_expr_p
21682 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21683 /* However, a parameter-declaration of the form
21684 "float(f)" (which is a valid declaration of a
21685 parameter "f") can also be interpreted as an
21686 expression (the conversion of "f" to "float"). */
21687 && !parenthesized_p)
21688 cp_parser_commit_to_tentative_parse (parser);
21689 }
21690 else
21691 {
21692 cp_parser_error (parser, "expected %<,%> or %<...%>");
21693 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21694 cp_parser_skip_to_closing_parenthesis (parser,
21695 /*recovering=*/true,
21696 /*or_comma=*/false,
21697 /*consume_paren=*/false);
21698 break;
21699 }
21700 }
21701
21702 parser->in_unbraced_linkage_specification_p
21703 = saved_in_unbraced_linkage_specification_p;
21704
21705 /* Reset implicit_template_scope if we are about to leave the function
21706 parameter list that introduced it. Note that for out-of-line member
21707 definitions, there will be one or more class scopes before we get to
21708 the template parameter scope. */
21709
21710 if (cp_binding_level *its = parser->implicit_template_scope)
21711 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21712 {
21713 while (maybe_its->kind == sk_class)
21714 maybe_its = maybe_its->level_chain;
21715 if (maybe_its == its)
21716 {
21717 parser->implicit_template_parms = 0;
21718 parser->implicit_template_scope = 0;
21719 }
21720 }
21721
21722 return parameters;
21723 }
21724
21725 /* Parse a parameter declaration.
21726
21727 parameter-declaration:
21728 decl-specifier-seq ... [opt] declarator
21729 decl-specifier-seq declarator = assignment-expression
21730 decl-specifier-seq ... [opt] abstract-declarator [opt]
21731 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21732
21733 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21734 declares a template parameter. (In that case, a non-nested `>'
21735 token encountered during the parsing of the assignment-expression
21736 is not interpreted as a greater-than operator.)
21737
21738 Returns a representation of the parameter, or NULL if an error
21739 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21740 true iff the declarator is of the form "(p)". */
21741
21742 static cp_parameter_declarator *
21743 cp_parser_parameter_declaration (cp_parser *parser,
21744 bool template_parm_p,
21745 bool *parenthesized_p)
21746 {
21747 int declares_class_or_enum;
21748 cp_decl_specifier_seq decl_specifiers;
21749 cp_declarator *declarator;
21750 tree default_argument;
21751 cp_token *token = NULL, *declarator_token_start = NULL;
21752 const char *saved_message;
21753 bool template_parameter_pack_p = false;
21754
21755 /* In a template parameter, `>' is not an operator.
21756
21757 [temp.param]
21758
21759 When parsing a default template-argument for a non-type
21760 template-parameter, the first non-nested `>' is taken as the end
21761 of the template parameter-list rather than a greater-than
21762 operator. */
21763
21764 /* Type definitions may not appear in parameter types. */
21765 saved_message = parser->type_definition_forbidden_message;
21766 parser->type_definition_forbidden_message
21767 = G_("types may not be defined in parameter types");
21768
21769 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21770 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21771 (current_template_parms)) : 0);
21772
21773 /* Parse the declaration-specifiers. */
21774 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21775 cp_parser_decl_specifier_seq (parser,
21776 CP_PARSER_FLAGS_NONE,
21777 &decl_specifiers,
21778 &declares_class_or_enum);
21779
21780 /* Complain about missing 'typename' or other invalid type names. */
21781 if (!decl_specifiers.any_type_specifiers_p
21782 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21783 decl_specifiers.type = error_mark_node;
21784
21785 /* If an error occurred, there's no reason to attempt to parse the
21786 rest of the declaration. */
21787 if (cp_parser_error_occurred (parser))
21788 {
21789 parser->type_definition_forbidden_message = saved_message;
21790 return NULL;
21791 }
21792
21793 /* Peek at the next token. */
21794 token = cp_lexer_peek_token (parser->lexer);
21795
21796 /* If the next token is a `)', `,', `=', `>', or `...', then there
21797 is no declarator. However, when variadic templates are enabled,
21798 there may be a declarator following `...'. */
21799 if (token->type == CPP_CLOSE_PAREN
21800 || token->type == CPP_COMMA
21801 || token->type == CPP_EQ
21802 || token->type == CPP_GREATER)
21803 {
21804 declarator = NULL;
21805 if (parenthesized_p)
21806 *parenthesized_p = false;
21807 }
21808 /* Otherwise, there should be a declarator. */
21809 else
21810 {
21811 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21812 parser->default_arg_ok_p = false;
21813
21814 /* After seeing a decl-specifier-seq, if the next token is not a
21815 "(", there is no possibility that the code is a valid
21816 expression. Therefore, if parsing tentatively, we commit at
21817 this point. */
21818 if (!parser->in_template_argument_list_p
21819 /* In an expression context, having seen:
21820
21821 (int((char ...
21822
21823 we cannot be sure whether we are looking at a
21824 function-type (taking a "char" as a parameter) or a cast
21825 of some object of type "char" to "int". */
21826 && !parser->in_type_id_in_expr_p
21827 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21828 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21829 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21830 cp_parser_commit_to_tentative_parse (parser);
21831 /* Parse the declarator. */
21832 declarator_token_start = token;
21833 declarator = cp_parser_declarator (parser,
21834 CP_PARSER_DECLARATOR_EITHER,
21835 /*ctor_dtor_or_conv_p=*/NULL,
21836 parenthesized_p,
21837 /*member_p=*/false,
21838 /*friend_p=*/false);
21839 parser->default_arg_ok_p = saved_default_arg_ok_p;
21840 /* After the declarator, allow more attributes. */
21841 decl_specifiers.attributes
21842 = attr_chainon (decl_specifiers.attributes,
21843 cp_parser_attributes_opt (parser));
21844
21845 /* If the declarator is a template parameter pack, remember that and
21846 clear the flag in the declarator itself so we don't get errors
21847 from grokdeclarator. */
21848 if (template_parm_p && declarator && declarator->parameter_pack_p)
21849 {
21850 declarator->parameter_pack_p = false;
21851 template_parameter_pack_p = true;
21852 }
21853 }
21854
21855 /* If the next token is an ellipsis, and we have not seen a declarator
21856 name, and if either the type of the declarator contains parameter
21857 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21858 for, eg, abbreviated integral type names), then we actually have a
21859 parameter pack expansion expression. Otherwise, leave the ellipsis
21860 for a C-style variadic function. */
21861 token = cp_lexer_peek_token (parser->lexer);
21862
21863 /* If a function parameter pack was specified and an implicit template
21864 parameter was introduced during cp_parser_parameter_declaration,
21865 change any implicit parameters introduced into packs. */
21866 if (parser->implicit_template_parms
21867 && ((token->type == CPP_ELLIPSIS
21868 && declarator_can_be_parameter_pack (declarator))
21869 || (declarator && declarator->parameter_pack_p)))
21870 {
21871 int latest_template_parm_idx = TREE_VEC_LENGTH
21872 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21873
21874 if (latest_template_parm_idx != template_parm_idx)
21875 decl_specifiers.type = convert_generic_types_to_packs
21876 (decl_specifiers.type,
21877 template_parm_idx, latest_template_parm_idx);
21878 }
21879
21880 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21881 {
21882 tree type = decl_specifiers.type;
21883
21884 if (type && DECL_P (type))
21885 type = TREE_TYPE (type);
21886
21887 if (((type
21888 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21889 && (template_parm_p || uses_parameter_packs (type)))
21890 || (!type && template_parm_p))
21891 && declarator_can_be_parameter_pack (declarator))
21892 {
21893 /* Consume the `...'. */
21894 cp_lexer_consume_token (parser->lexer);
21895 maybe_warn_variadic_templates ();
21896
21897 /* Build a pack expansion type */
21898 if (template_parm_p)
21899 template_parameter_pack_p = true;
21900 else if (declarator)
21901 declarator->parameter_pack_p = true;
21902 else
21903 decl_specifiers.type = make_pack_expansion (type);
21904 }
21905 }
21906
21907 /* The restriction on defining new types applies only to the type
21908 of the parameter, not to the default argument. */
21909 parser->type_definition_forbidden_message = saved_message;
21910
21911 /* If the next token is `=', then process a default argument. */
21912 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21913 {
21914 tree type = decl_specifiers.type;
21915 token = cp_lexer_peek_token (parser->lexer);
21916 /* If we are defining a class, then the tokens that make up the
21917 default argument must be saved and processed later. */
21918 if (!template_parm_p && at_class_scope_p ()
21919 && TYPE_BEING_DEFINED (current_class_type)
21920 && !LAMBDA_TYPE_P (current_class_type))
21921 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21922
21923 // A constrained-type-specifier may declare a type template-parameter.
21924 else if (declares_constrained_type_template_parameter (type))
21925 default_argument
21926 = cp_parser_default_type_template_argument (parser);
21927
21928 // A constrained-type-specifier may declare a template-template-parameter.
21929 else if (declares_constrained_template_template_parameter (type))
21930 default_argument
21931 = cp_parser_default_template_template_argument (parser);
21932
21933 /* Outside of a class definition, we can just parse the
21934 assignment-expression. */
21935 else
21936 default_argument
21937 = cp_parser_default_argument (parser, template_parm_p);
21938
21939 if (!parser->default_arg_ok_p)
21940 {
21941 permerror (token->location,
21942 "default arguments are only "
21943 "permitted for function parameters");
21944 }
21945 else if ((declarator && declarator->parameter_pack_p)
21946 || template_parameter_pack_p
21947 || (decl_specifiers.type
21948 && PACK_EXPANSION_P (decl_specifiers.type)))
21949 {
21950 /* Find the name of the parameter pack. */
21951 cp_declarator *id_declarator = declarator;
21952 while (id_declarator && id_declarator->kind != cdk_id)
21953 id_declarator = id_declarator->declarator;
21954
21955 if (id_declarator && id_declarator->kind == cdk_id)
21956 error_at (declarator_token_start->location,
21957 template_parm_p
21958 ? G_("template parameter pack %qD "
21959 "cannot have a default argument")
21960 : G_("parameter pack %qD cannot have "
21961 "a default argument"),
21962 id_declarator->u.id.unqualified_name);
21963 else
21964 error_at (declarator_token_start->location,
21965 template_parm_p
21966 ? G_("template parameter pack cannot have "
21967 "a default argument")
21968 : G_("parameter pack cannot have a "
21969 "default argument"));
21970
21971 default_argument = NULL_TREE;
21972 }
21973 }
21974 else
21975 default_argument = NULL_TREE;
21976
21977 /* Generate a location for the parameter, ranging from the start of the
21978 initial token to the end of the final token (using input_location for
21979 the latter, set up by cp_lexer_set_source_position_from_token when
21980 consuming tokens).
21981
21982 If we have a identifier, then use it for the caret location, e.g.
21983
21984 extern int callee (int one, int (*two)(int, int), float three);
21985 ~~~~~~^~~~~~~~~~~~~~
21986
21987 otherwise, reuse the start location for the caret location e.g.:
21988
21989 extern int callee (int one, int (*)(int, int), float three);
21990 ^~~~~~~~~~~~~~~~~
21991
21992 */
21993 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21994 ? declarator->id_loc
21995 : decl_spec_token_start->location);
21996 location_t param_loc = make_location (caret_loc,
21997 decl_spec_token_start->location,
21998 input_location);
21999
22000 return make_parameter_declarator (&decl_specifiers,
22001 declarator,
22002 default_argument,
22003 param_loc,
22004 template_parameter_pack_p);
22005 }
22006
22007 /* Parse a default argument and return it.
22008
22009 TEMPLATE_PARM_P is true if this is a default argument for a
22010 non-type template parameter. */
22011 static tree
22012 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22013 {
22014 tree default_argument = NULL_TREE;
22015 bool saved_greater_than_is_operator_p;
22016 bool saved_local_variables_forbidden_p;
22017 bool non_constant_p, is_direct_init;
22018
22019 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22020 set correctly. */
22021 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22022 parser->greater_than_is_operator_p = !template_parm_p;
22023 /* Local variable names (and the `this' keyword) may not
22024 appear in a default argument. */
22025 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22026 parser->local_variables_forbidden_p = true;
22027 /* Parse the assignment-expression. */
22028 if (template_parm_p)
22029 push_deferring_access_checks (dk_no_deferred);
22030 tree saved_class_ptr = NULL_TREE;
22031 tree saved_class_ref = NULL_TREE;
22032 /* The "this" pointer is not valid in a default argument. */
22033 if (cfun)
22034 {
22035 saved_class_ptr = current_class_ptr;
22036 cp_function_chain->x_current_class_ptr = NULL_TREE;
22037 saved_class_ref = current_class_ref;
22038 cp_function_chain->x_current_class_ref = NULL_TREE;
22039 }
22040 default_argument
22041 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22042 /* Restore the "this" pointer. */
22043 if (cfun)
22044 {
22045 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22046 cp_function_chain->x_current_class_ref = saved_class_ref;
22047 }
22048 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22049 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22050 if (template_parm_p)
22051 pop_deferring_access_checks ();
22052 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22053 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22054
22055 return default_argument;
22056 }
22057
22058 /* Parse a function-body.
22059
22060 function-body:
22061 compound_statement */
22062
22063 static void
22064 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22065 {
22066 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22067 ? BCS_TRY_BLOCK : BCS_NORMAL),
22068 true);
22069 }
22070
22071 /* Parse a ctor-initializer-opt followed by a function-body. Return
22072 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22073 is true we are parsing a function-try-block. */
22074
22075 static void
22076 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22077 bool in_function_try_block)
22078 {
22079 tree body, list;
22080 const bool check_body_p =
22081 DECL_CONSTRUCTOR_P (current_function_decl)
22082 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22083 tree last = NULL;
22084
22085 /* Begin the function body. */
22086 body = begin_function_body ();
22087 /* Parse the optional ctor-initializer. */
22088 cp_parser_ctor_initializer_opt (parser);
22089
22090 /* If we're parsing a constexpr constructor definition, we need
22091 to check that the constructor body is indeed empty. However,
22092 before we get to cp_parser_function_body lot of junk has been
22093 generated, so we can't just check that we have an empty block.
22094 Rather we take a snapshot of the outermost block, and check whether
22095 cp_parser_function_body changed its state. */
22096 if (check_body_p)
22097 {
22098 list = cur_stmt_list;
22099 if (STATEMENT_LIST_TAIL (list))
22100 last = STATEMENT_LIST_TAIL (list)->stmt;
22101 }
22102 /* Parse the function-body. */
22103 cp_parser_function_body (parser, in_function_try_block);
22104 if (check_body_p)
22105 check_constexpr_ctor_body (last, list, /*complain=*/true);
22106 /* Finish the function body. */
22107 finish_function_body (body);
22108 }
22109
22110 /* Parse an initializer.
22111
22112 initializer:
22113 = initializer-clause
22114 ( expression-list )
22115
22116 Returns an expression representing the initializer. If no
22117 initializer is present, NULL_TREE is returned.
22118
22119 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22120 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22121 set to TRUE if there is no initializer present. If there is an
22122 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22123 is set to true; otherwise it is set to false. */
22124
22125 static tree
22126 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22127 bool* non_constant_p, bool subexpression_p)
22128 {
22129 cp_token *token;
22130 tree init;
22131
22132 /* Peek at the next token. */
22133 token = cp_lexer_peek_token (parser->lexer);
22134
22135 /* Let our caller know whether or not this initializer was
22136 parenthesized. */
22137 *is_direct_init = (token->type != CPP_EQ);
22138 /* Assume that the initializer is constant. */
22139 *non_constant_p = false;
22140
22141 if (token->type == CPP_EQ)
22142 {
22143 /* Consume the `='. */
22144 cp_lexer_consume_token (parser->lexer);
22145 /* Parse the initializer-clause. */
22146 init = cp_parser_initializer_clause (parser, non_constant_p);
22147 }
22148 else if (token->type == CPP_OPEN_PAREN)
22149 {
22150 vec<tree, va_gc> *vec;
22151 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22152 /*cast_p=*/false,
22153 /*allow_expansion_p=*/true,
22154 non_constant_p);
22155 if (vec == NULL)
22156 return error_mark_node;
22157 init = build_tree_list_vec (vec);
22158 release_tree_vector (vec);
22159 }
22160 else if (token->type == CPP_OPEN_BRACE)
22161 {
22162 cp_lexer_set_source_position (parser->lexer);
22163 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22164 init = cp_parser_braced_list (parser, non_constant_p);
22165 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22166 }
22167 else
22168 {
22169 /* Anything else is an error. */
22170 cp_parser_error (parser, "expected initializer");
22171 init = error_mark_node;
22172 }
22173
22174 if (!subexpression_p && check_for_bare_parameter_packs (init))
22175 init = error_mark_node;
22176
22177 return init;
22178 }
22179
22180 /* Parse an initializer-clause.
22181
22182 initializer-clause:
22183 assignment-expression
22184 braced-init-list
22185
22186 Returns an expression representing the initializer.
22187
22188 If the `assignment-expression' production is used the value
22189 returned is simply a representation for the expression.
22190
22191 Otherwise, calls cp_parser_braced_list. */
22192
22193 static cp_expr
22194 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22195 {
22196 cp_expr initializer;
22197
22198 /* Assume the expression is constant. */
22199 *non_constant_p = false;
22200
22201 /* If it is not a `{', then we are looking at an
22202 assignment-expression. */
22203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22204 {
22205 initializer
22206 = cp_parser_constant_expression (parser,
22207 /*allow_non_constant_p=*/true,
22208 non_constant_p);
22209 }
22210 else
22211 initializer = cp_parser_braced_list (parser, non_constant_p);
22212
22213 return initializer;
22214 }
22215
22216 /* Parse a brace-enclosed initializer list.
22217
22218 braced-init-list:
22219 { initializer-list , [opt] }
22220 { designated-initializer-list , [opt] }
22221 { }
22222
22223 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22224 the elements of the initializer-list (or NULL, if the last
22225 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22226 NULL_TREE. There is no way to detect whether or not the optional
22227 trailing `,' was provided. NON_CONSTANT_P is as for
22228 cp_parser_initializer. */
22229
22230 static cp_expr
22231 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22232 {
22233 tree initializer;
22234 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22235
22236 /* Consume the `{' token. */
22237 matching_braces braces;
22238 braces.require_open (parser);
22239 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22240 initializer = make_node (CONSTRUCTOR);
22241 /* If it's not a `}', then there is a non-trivial initializer. */
22242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22243 {
22244 /* Parse the initializer list. */
22245 CONSTRUCTOR_ELTS (initializer)
22246 = cp_parser_initializer_list (parser, non_constant_p);
22247 /* A trailing `,' token is allowed. */
22248 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22249 cp_lexer_consume_token (parser->lexer);
22250 }
22251 else
22252 *non_constant_p = false;
22253 /* Now, there should be a trailing `}'. */
22254 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22255 braces.require_close (parser);
22256 TREE_TYPE (initializer) = init_list_type_node;
22257
22258 cp_expr result (initializer);
22259 /* Build a location of the form:
22260 { ... }
22261 ^~~~~~~
22262 with caret==start at the open brace, finish at the close brace. */
22263 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22264 result.set_location (combined_loc);
22265 return result;
22266 }
22267
22268 /* Consume tokens up to, and including, the next non-nested closing `]'.
22269 Returns true iff we found a closing `]'. */
22270
22271 static bool
22272 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22273 {
22274 unsigned square_depth = 0;
22275
22276 while (true)
22277 {
22278 cp_token * token = cp_lexer_peek_token (parser->lexer);
22279
22280 switch (token->type)
22281 {
22282 case CPP_EOF:
22283 case CPP_PRAGMA_EOL:
22284 /* If we've run out of tokens, then there is no closing `]'. */
22285 return false;
22286
22287 case CPP_OPEN_SQUARE:
22288 ++square_depth;
22289 break;
22290
22291 case CPP_CLOSE_SQUARE:
22292 if (!square_depth--)
22293 {
22294 cp_lexer_consume_token (parser->lexer);
22295 return true;
22296 }
22297 break;
22298
22299 default:
22300 break;
22301 }
22302
22303 /* Consume the token. */
22304 cp_lexer_consume_token (parser->lexer);
22305 }
22306 }
22307
22308 /* Return true if we are looking at an array-designator, false otherwise. */
22309
22310 static bool
22311 cp_parser_array_designator_p (cp_parser *parser)
22312 {
22313 /* Consume the `['. */
22314 cp_lexer_consume_token (parser->lexer);
22315
22316 cp_lexer_save_tokens (parser->lexer);
22317
22318 /* Skip tokens until the next token is a closing square bracket.
22319 If we find the closing `]', and the next token is a `=', then
22320 we are looking at an array designator. */
22321 bool array_designator_p
22322 = (cp_parser_skip_to_closing_square_bracket (parser)
22323 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22324
22325 /* Roll back the tokens we skipped. */
22326 cp_lexer_rollback_tokens (parser->lexer);
22327
22328 return array_designator_p;
22329 }
22330
22331 /* Parse an initializer-list.
22332
22333 initializer-list:
22334 initializer-clause ... [opt]
22335 initializer-list , initializer-clause ... [opt]
22336
22337 C++2A Extension:
22338
22339 designated-initializer-list:
22340 designated-initializer-clause
22341 designated-initializer-list , designated-initializer-clause
22342
22343 designated-initializer-clause:
22344 designator brace-or-equal-initializer
22345
22346 designator:
22347 . identifier
22348
22349 GNU Extension:
22350
22351 initializer-list:
22352 designation initializer-clause ...[opt]
22353 initializer-list , designation initializer-clause ...[opt]
22354
22355 designation:
22356 . identifier =
22357 identifier :
22358 [ constant-expression ] =
22359
22360 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22361 for the initializer. If the INDEX of the elt is non-NULL, it is the
22362 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22363 as for cp_parser_initializer. */
22364
22365 static vec<constructor_elt, va_gc> *
22366 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22367 {
22368 vec<constructor_elt, va_gc> *v = NULL;
22369 bool first_p = true;
22370 tree first_designator = NULL_TREE;
22371
22372 /* Assume all of the expressions are constant. */
22373 *non_constant_p = false;
22374
22375 /* Parse the rest of the list. */
22376 while (true)
22377 {
22378 cp_token *token;
22379 tree designator;
22380 tree initializer;
22381 bool clause_non_constant_p;
22382 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22383
22384 /* Handle the C++2A syntax, '. id ='. */
22385 if ((cxx_dialect >= cxx2a
22386 || cp_parser_allow_gnu_extensions_p (parser))
22387 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22388 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22389 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22390 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22391 == CPP_OPEN_BRACE)))
22392 {
22393 if (cxx_dialect < cxx2a)
22394 pedwarn (loc, OPT_Wpedantic,
22395 "C++ designated initializers only available with "
22396 "-std=c++2a or -std=gnu++2a");
22397 /* Consume the `.'. */
22398 cp_lexer_consume_token (parser->lexer);
22399 /* Consume the identifier. */
22400 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22401 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22402 /* Consume the `='. */
22403 cp_lexer_consume_token (parser->lexer);
22404 }
22405 /* Also, if the next token is an identifier and the following one is a
22406 colon, we are looking at the GNU designated-initializer
22407 syntax. */
22408 else if (cp_parser_allow_gnu_extensions_p (parser)
22409 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22410 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22411 == CPP_COLON))
22412 {
22413 /* Warn the user that they are using an extension. */
22414 pedwarn (loc, OPT_Wpedantic,
22415 "ISO C++ does not allow GNU designated initializers");
22416 /* Consume the identifier. */
22417 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22418 /* Consume the `:'. */
22419 cp_lexer_consume_token (parser->lexer);
22420 }
22421 /* Also handle C99 array designators, '[ const ] ='. */
22422 else if (cp_parser_allow_gnu_extensions_p (parser)
22423 && !c_dialect_objc ()
22424 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22425 {
22426 /* In C++11, [ could start a lambda-introducer. */
22427 bool non_const = false;
22428
22429 cp_parser_parse_tentatively (parser);
22430
22431 if (!cp_parser_array_designator_p (parser))
22432 {
22433 cp_parser_simulate_error (parser);
22434 designator = NULL_TREE;
22435 }
22436 else
22437 {
22438 designator = cp_parser_constant_expression (parser, true,
22439 &non_const);
22440 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22441 cp_parser_require (parser, CPP_EQ, RT_EQ);
22442 }
22443
22444 if (!cp_parser_parse_definitely (parser))
22445 designator = NULL_TREE;
22446 else if (non_const
22447 && (!require_potential_rvalue_constant_expression
22448 (designator)))
22449 designator = NULL_TREE;
22450 if (designator)
22451 /* Warn the user that they are using an extension. */
22452 pedwarn (loc, OPT_Wpedantic,
22453 "ISO C++ does not allow C99 designated initializers");
22454 }
22455 else
22456 designator = NULL_TREE;
22457
22458 if (first_p)
22459 {
22460 first_designator = designator;
22461 first_p = false;
22462 }
22463 else if (cxx_dialect >= cxx2a
22464 && first_designator != error_mark_node
22465 && (!first_designator != !designator))
22466 {
22467 error_at (loc, "either all initializer clauses should be designated "
22468 "or none of them should be");
22469 first_designator = error_mark_node;
22470 }
22471 else if (cxx_dialect < cxx2a && !first_designator)
22472 first_designator = designator;
22473
22474 /* Parse the initializer. */
22475 initializer = cp_parser_initializer_clause (parser,
22476 &clause_non_constant_p);
22477 /* If any clause is non-constant, so is the entire initializer. */
22478 if (clause_non_constant_p)
22479 *non_constant_p = true;
22480
22481 /* If we have an ellipsis, this is an initializer pack
22482 expansion. */
22483 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22484 {
22485 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22486
22487 /* Consume the `...'. */
22488 cp_lexer_consume_token (parser->lexer);
22489
22490 if (designator && cxx_dialect >= cxx2a)
22491 error_at (loc,
22492 "%<...%> not allowed in designated initializer list");
22493
22494 /* Turn the initializer into an initializer expansion. */
22495 initializer = make_pack_expansion (initializer);
22496 }
22497
22498 /* Add it to the vector. */
22499 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22500
22501 /* If the next token is not a comma, we have reached the end of
22502 the list. */
22503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22504 break;
22505
22506 /* Peek at the next token. */
22507 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22508 /* If the next token is a `}', then we're still done. An
22509 initializer-clause can have a trailing `,' after the
22510 initializer-list and before the closing `}'. */
22511 if (token->type == CPP_CLOSE_BRACE)
22512 break;
22513
22514 /* Consume the `,' token. */
22515 cp_lexer_consume_token (parser->lexer);
22516 }
22517
22518 /* The same identifier shall not appear in multiple designators
22519 of a designated-initializer-list. */
22520 if (first_designator)
22521 {
22522 unsigned int i;
22523 tree designator, val;
22524 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22525 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22526 {
22527 if (IDENTIFIER_MARKED (designator))
22528 {
22529 error_at (cp_expr_loc_or_loc (val, input_location),
22530 "%<.%s%> designator used multiple times in "
22531 "the same initializer list",
22532 IDENTIFIER_POINTER (designator));
22533 (*v)[i].index = error_mark_node;
22534 }
22535 else
22536 IDENTIFIER_MARKED (designator) = 1;
22537 }
22538 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22539 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22540 IDENTIFIER_MARKED (designator) = 0;
22541 }
22542
22543 return v;
22544 }
22545
22546 /* Classes [gram.class] */
22547
22548 /* Parse a class-name.
22549
22550 class-name:
22551 identifier
22552 template-id
22553
22554 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22555 to indicate that names looked up in dependent types should be
22556 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22557 keyword has been used to indicate that the name that appears next
22558 is a template. TAG_TYPE indicates the explicit tag given before
22559 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22560 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22561 is the class being defined in a class-head. If ENUM_OK is TRUE,
22562 enum-names are also accepted.
22563
22564 Returns the TYPE_DECL representing the class. */
22565
22566 static tree
22567 cp_parser_class_name (cp_parser *parser,
22568 bool typename_keyword_p,
22569 bool template_keyword_p,
22570 enum tag_types tag_type,
22571 bool check_dependency_p,
22572 bool class_head_p,
22573 bool is_declaration,
22574 bool enum_ok)
22575 {
22576 tree decl;
22577 tree scope;
22578 bool typename_p;
22579 cp_token *token;
22580 tree identifier = NULL_TREE;
22581
22582 /* All class-names start with an identifier. */
22583 token = cp_lexer_peek_token (parser->lexer);
22584 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22585 {
22586 cp_parser_error (parser, "expected class-name");
22587 return error_mark_node;
22588 }
22589
22590 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22591 to a template-id, so we save it here. */
22592 scope = parser->scope;
22593 if (scope == error_mark_node)
22594 return error_mark_node;
22595
22596 /* Any name names a type if we're following the `typename' keyword
22597 in a qualified name where the enclosing scope is type-dependent. */
22598 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22599 && dependent_type_p (scope));
22600 /* Handle the common case (an identifier, but not a template-id)
22601 efficiently. */
22602 if (token->type == CPP_NAME
22603 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22604 {
22605 cp_token *identifier_token;
22606 bool ambiguous_p;
22607
22608 /* Look for the identifier. */
22609 identifier_token = cp_lexer_peek_token (parser->lexer);
22610 ambiguous_p = identifier_token->error_reported;
22611 identifier = cp_parser_identifier (parser);
22612 /* If the next token isn't an identifier, we are certainly not
22613 looking at a class-name. */
22614 if (identifier == error_mark_node)
22615 decl = error_mark_node;
22616 /* If we know this is a type-name, there's no need to look it
22617 up. */
22618 else if (typename_p)
22619 decl = identifier;
22620 else
22621 {
22622 tree ambiguous_decls;
22623 /* If we already know that this lookup is ambiguous, then
22624 we've already issued an error message; there's no reason
22625 to check again. */
22626 if (ambiguous_p)
22627 {
22628 cp_parser_simulate_error (parser);
22629 return error_mark_node;
22630 }
22631 /* If the next token is a `::', then the name must be a type
22632 name.
22633
22634 [basic.lookup.qual]
22635
22636 During the lookup for a name preceding the :: scope
22637 resolution operator, object, function, and enumerator
22638 names are ignored. */
22639 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22640 tag_type = scope_type;
22641 /* Look up the name. */
22642 decl = cp_parser_lookup_name (parser, identifier,
22643 tag_type,
22644 /*is_template=*/false,
22645 /*is_namespace=*/false,
22646 check_dependency_p,
22647 &ambiguous_decls,
22648 identifier_token->location);
22649 if (ambiguous_decls)
22650 {
22651 if (cp_parser_parsing_tentatively (parser))
22652 cp_parser_simulate_error (parser);
22653 return error_mark_node;
22654 }
22655 }
22656 }
22657 else
22658 {
22659 /* Try a template-id. */
22660 decl = cp_parser_template_id (parser, template_keyword_p,
22661 check_dependency_p,
22662 tag_type,
22663 is_declaration);
22664 if (decl == error_mark_node)
22665 return error_mark_node;
22666 }
22667
22668 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22669
22670 /* If this is a typename, create a TYPENAME_TYPE. */
22671 if (typename_p && decl != error_mark_node)
22672 {
22673 decl = make_typename_type (scope, decl, typename_type,
22674 /*complain=*/tf_error);
22675 if (decl != error_mark_node)
22676 decl = TYPE_NAME (decl);
22677 }
22678
22679 decl = strip_using_decl (decl);
22680
22681 /* Check to see that it is really the name of a class. */
22682 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22683 && identifier_p (TREE_OPERAND (decl, 0))
22684 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22685 /* Situations like this:
22686
22687 template <typename T> struct A {
22688 typename T::template X<int>::I i;
22689 };
22690
22691 are problematic. Is `T::template X<int>' a class-name? The
22692 standard does not seem to be definitive, but there is no other
22693 valid interpretation of the following `::'. Therefore, those
22694 names are considered class-names. */
22695 {
22696 decl = make_typename_type (scope, decl, tag_type, tf_error);
22697 if (decl != error_mark_node)
22698 decl = TYPE_NAME (decl);
22699 }
22700 else if (TREE_CODE (decl) != TYPE_DECL
22701 || TREE_TYPE (decl) == error_mark_node
22702 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22703 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22704 /* In Objective-C 2.0, a classname followed by '.' starts a
22705 dot-syntax expression, and it's not a type-name. */
22706 || (c_dialect_objc ()
22707 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22708 && objc_is_class_name (decl)))
22709 decl = error_mark_node;
22710
22711 if (decl == error_mark_node)
22712 cp_parser_error (parser, "expected class-name");
22713 else if (identifier && !parser->scope)
22714 maybe_note_name_used_in_class (identifier, decl);
22715
22716 return decl;
22717 }
22718
22719 /* Parse a class-specifier.
22720
22721 class-specifier:
22722 class-head { member-specification [opt] }
22723
22724 Returns the TREE_TYPE representing the class. */
22725
22726 static tree
22727 cp_parser_class_specifier_1 (cp_parser* parser)
22728 {
22729 tree type;
22730 tree attributes = NULL_TREE;
22731 bool nested_name_specifier_p;
22732 unsigned saved_num_template_parameter_lists;
22733 bool saved_in_function_body;
22734 unsigned char in_statement;
22735 bool in_switch_statement_p;
22736 bool saved_in_unbraced_linkage_specification_p;
22737 tree old_scope = NULL_TREE;
22738 tree scope = NULL_TREE;
22739 cp_token *closing_brace;
22740
22741 push_deferring_access_checks (dk_no_deferred);
22742
22743 /* Parse the class-head. */
22744 type = cp_parser_class_head (parser,
22745 &nested_name_specifier_p);
22746 /* If the class-head was a semantic disaster, skip the entire body
22747 of the class. */
22748 if (!type)
22749 {
22750 cp_parser_skip_to_end_of_block_or_statement (parser);
22751 pop_deferring_access_checks ();
22752 return error_mark_node;
22753 }
22754
22755 /* Look for the `{'. */
22756 matching_braces braces;
22757 if (!braces.require_open (parser))
22758 {
22759 pop_deferring_access_checks ();
22760 return error_mark_node;
22761 }
22762
22763 cp_ensure_no_omp_declare_simd (parser);
22764 cp_ensure_no_oacc_routine (parser);
22765
22766 /* Issue an error message if type-definitions are forbidden here. */
22767 cp_parser_check_type_definition (parser);
22768 /* Remember that we are defining one more class. */
22769 ++parser->num_classes_being_defined;
22770 /* Inside the class, surrounding template-parameter-lists do not
22771 apply. */
22772 saved_num_template_parameter_lists
22773 = parser->num_template_parameter_lists;
22774 parser->num_template_parameter_lists = 0;
22775 /* We are not in a function body. */
22776 saved_in_function_body = parser->in_function_body;
22777 parser->in_function_body = false;
22778 /* Or in a loop. */
22779 in_statement = parser->in_statement;
22780 parser->in_statement = 0;
22781 /* Or in a switch. */
22782 in_switch_statement_p = parser->in_switch_statement_p;
22783 parser->in_switch_statement_p = false;
22784 /* We are not immediately inside an extern "lang" block. */
22785 saved_in_unbraced_linkage_specification_p
22786 = parser->in_unbraced_linkage_specification_p;
22787 parser->in_unbraced_linkage_specification_p = false;
22788
22789 // Associate constraints with the type.
22790 if (flag_concepts)
22791 type = associate_classtype_constraints (type);
22792
22793 /* Start the class. */
22794 if (nested_name_specifier_p)
22795 {
22796 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22797 old_scope = push_inner_scope (scope);
22798 }
22799 type = begin_class_definition (type);
22800
22801 if (type == error_mark_node)
22802 /* If the type is erroneous, skip the entire body of the class. */
22803 cp_parser_skip_to_closing_brace (parser);
22804 else
22805 /* Parse the member-specification. */
22806 cp_parser_member_specification_opt (parser);
22807
22808 /* Look for the trailing `}'. */
22809 closing_brace = braces.require_close (parser);
22810 /* Look for trailing attributes to apply to this class. */
22811 if (cp_parser_allow_gnu_extensions_p (parser))
22812 attributes = cp_parser_gnu_attributes_opt (parser);
22813 if (type != error_mark_node)
22814 type = finish_struct (type, attributes);
22815 if (nested_name_specifier_p)
22816 pop_inner_scope (old_scope, scope);
22817
22818 /* We've finished a type definition. Check for the common syntax
22819 error of forgetting a semicolon after the definition. We need to
22820 be careful, as we can't just check for not-a-semicolon and be done
22821 with it; the user might have typed:
22822
22823 class X { } c = ...;
22824 class X { } *p = ...;
22825
22826 and so forth. Instead, enumerate all the possible tokens that
22827 might follow this production; if we don't see one of them, then
22828 complain and silently insert the semicolon. */
22829 {
22830 cp_token *token = cp_lexer_peek_token (parser->lexer);
22831 bool want_semicolon = true;
22832
22833 if (cp_next_tokens_can_be_std_attribute_p (parser))
22834 /* Don't try to parse c++11 attributes here. As per the
22835 grammar, that should be a task for
22836 cp_parser_decl_specifier_seq. */
22837 want_semicolon = false;
22838
22839 switch (token->type)
22840 {
22841 case CPP_NAME:
22842 case CPP_SEMICOLON:
22843 case CPP_MULT:
22844 case CPP_AND:
22845 case CPP_OPEN_PAREN:
22846 case CPP_CLOSE_PAREN:
22847 case CPP_COMMA:
22848 want_semicolon = false;
22849 break;
22850
22851 /* While it's legal for type qualifiers and storage class
22852 specifiers to follow type definitions in the grammar, only
22853 compiler testsuites contain code like that. Assume that if
22854 we see such code, then what we're really seeing is a case
22855 like:
22856
22857 class X { }
22858 const <type> var = ...;
22859
22860 or
22861
22862 class Y { }
22863 static <type> func (...) ...
22864
22865 i.e. the qualifier or specifier applies to the next
22866 declaration. To do so, however, we need to look ahead one
22867 more token to see if *that* token is a type specifier.
22868
22869 This code could be improved to handle:
22870
22871 class Z { }
22872 static const <type> var = ...; */
22873 case CPP_KEYWORD:
22874 if (keyword_is_decl_specifier (token->keyword))
22875 {
22876 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22877
22878 /* Handling user-defined types here would be nice, but very
22879 tricky. */
22880 want_semicolon
22881 = (lookahead->type == CPP_KEYWORD
22882 && keyword_begins_type_specifier (lookahead->keyword));
22883 }
22884 break;
22885 default:
22886 break;
22887 }
22888
22889 /* If we don't have a type, then something is very wrong and we
22890 shouldn't try to do anything clever. Likewise for not seeing the
22891 closing brace. */
22892 if (closing_brace && TYPE_P (type) && want_semicolon)
22893 {
22894 /* Locate the closing brace. */
22895 cp_token_position prev
22896 = cp_lexer_previous_token_position (parser->lexer);
22897 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22898 location_t loc = prev_token->location;
22899
22900 /* We want to suggest insertion of a ';' immediately *after* the
22901 closing brace, so, if we can, offset the location by 1 column. */
22902 location_t next_loc = loc;
22903 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22904 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22905
22906 rich_location richloc (line_table, next_loc);
22907
22908 /* If we successfully offset the location, suggest the fix-it. */
22909 if (next_loc != loc)
22910 richloc.add_fixit_insert_before (next_loc, ";");
22911
22912 if (CLASSTYPE_DECLARED_CLASS (type))
22913 error_at (&richloc,
22914 "expected %<;%> after class definition");
22915 else if (TREE_CODE (type) == RECORD_TYPE)
22916 error_at (&richloc,
22917 "expected %<;%> after struct definition");
22918 else if (TREE_CODE (type) == UNION_TYPE)
22919 error_at (&richloc,
22920 "expected %<;%> after union definition");
22921 else
22922 gcc_unreachable ();
22923
22924 /* Unget one token and smash it to look as though we encountered
22925 a semicolon in the input stream. */
22926 cp_lexer_set_token_position (parser->lexer, prev);
22927 token = cp_lexer_peek_token (parser->lexer);
22928 token->type = CPP_SEMICOLON;
22929 token->keyword = RID_MAX;
22930 }
22931 }
22932
22933 /* If this class is not itself within the scope of another class,
22934 then we need to parse the bodies of all of the queued function
22935 definitions. Note that the queued functions defined in a class
22936 are not always processed immediately following the
22937 class-specifier for that class. Consider:
22938
22939 struct A {
22940 struct B { void f() { sizeof (A); } };
22941 };
22942
22943 If `f' were processed before the processing of `A' were
22944 completed, there would be no way to compute the size of `A'.
22945 Note that the nesting we are interested in here is lexical --
22946 not the semantic nesting given by TYPE_CONTEXT. In particular,
22947 for:
22948
22949 struct A { struct B; };
22950 struct A::B { void f() { } };
22951
22952 there is no need to delay the parsing of `A::B::f'. */
22953 if (--parser->num_classes_being_defined == 0)
22954 {
22955 tree decl;
22956 tree class_type = NULL_TREE;
22957 tree pushed_scope = NULL_TREE;
22958 unsigned ix;
22959 cp_default_arg_entry *e;
22960 tree save_ccp, save_ccr;
22961
22962 if (any_erroneous_template_args_p (type))
22963 {
22964 /* Skip default arguments, NSDMIs, etc, in order to improve
22965 error recovery (c++/71169, c++/71832). */
22966 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22967 vec_safe_truncate (unparsed_nsdmis, 0);
22968 vec_safe_truncate (unparsed_classes, 0);
22969 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22970 }
22971
22972 /* In a first pass, parse default arguments to the functions.
22973 Then, in a second pass, parse the bodies of the functions.
22974 This two-phased approach handles cases like:
22975
22976 struct S {
22977 void f() { g(); }
22978 void g(int i = 3);
22979 };
22980
22981 */
22982 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22983 {
22984 decl = e->decl;
22985 /* If there are default arguments that have not yet been processed,
22986 take care of them now. */
22987 if (class_type != e->class_type)
22988 {
22989 if (pushed_scope)
22990 pop_scope (pushed_scope);
22991 class_type = e->class_type;
22992 pushed_scope = push_scope (class_type);
22993 }
22994 /* Make sure that any template parameters are in scope. */
22995 maybe_begin_member_template_processing (decl);
22996 /* Parse the default argument expressions. */
22997 cp_parser_late_parsing_default_args (parser, decl);
22998 /* Remove any template parameters from the symbol table. */
22999 maybe_end_member_template_processing ();
23000 }
23001 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23002 /* Now parse any NSDMIs. */
23003 save_ccp = current_class_ptr;
23004 save_ccr = current_class_ref;
23005 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23006 {
23007 if (class_type != DECL_CONTEXT (decl))
23008 {
23009 if (pushed_scope)
23010 pop_scope (pushed_scope);
23011 class_type = DECL_CONTEXT (decl);
23012 pushed_scope = push_scope (class_type);
23013 }
23014 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23015 cp_parser_late_parsing_nsdmi (parser, decl);
23016 }
23017 vec_safe_truncate (unparsed_nsdmis, 0);
23018 current_class_ptr = save_ccp;
23019 current_class_ref = save_ccr;
23020 if (pushed_scope)
23021 pop_scope (pushed_scope);
23022
23023 /* Now do some post-NSDMI bookkeeping. */
23024 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23025 after_nsdmi_defaulted_late_checks (class_type);
23026 vec_safe_truncate (unparsed_classes, 0);
23027 after_nsdmi_defaulted_late_checks (type);
23028
23029 /* Now parse the body of the functions. */
23030 if (flag_openmp)
23031 {
23032 /* OpenMP UDRs need to be parsed before all other functions. */
23033 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23034 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23035 cp_parser_late_parsing_for_member (parser, decl);
23036 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23037 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23038 cp_parser_late_parsing_for_member (parser, decl);
23039 }
23040 else
23041 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23042 cp_parser_late_parsing_for_member (parser, decl);
23043 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23044 }
23045 else
23046 vec_safe_push (unparsed_classes, type);
23047
23048 /* Put back any saved access checks. */
23049 pop_deferring_access_checks ();
23050
23051 /* Restore saved state. */
23052 parser->in_switch_statement_p = in_switch_statement_p;
23053 parser->in_statement = in_statement;
23054 parser->in_function_body = saved_in_function_body;
23055 parser->num_template_parameter_lists
23056 = saved_num_template_parameter_lists;
23057 parser->in_unbraced_linkage_specification_p
23058 = saved_in_unbraced_linkage_specification_p;
23059
23060 return type;
23061 }
23062
23063 static tree
23064 cp_parser_class_specifier (cp_parser* parser)
23065 {
23066 tree ret;
23067 timevar_push (TV_PARSE_STRUCT);
23068 ret = cp_parser_class_specifier_1 (parser);
23069 timevar_pop (TV_PARSE_STRUCT);
23070 return ret;
23071 }
23072
23073 /* Parse a class-head.
23074
23075 class-head:
23076 class-key identifier [opt] base-clause [opt]
23077 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23078 class-key nested-name-specifier [opt] template-id
23079 base-clause [opt]
23080
23081 class-virt-specifier:
23082 final
23083
23084 GNU Extensions:
23085 class-key attributes identifier [opt] base-clause [opt]
23086 class-key attributes nested-name-specifier identifier base-clause [opt]
23087 class-key attributes nested-name-specifier [opt] template-id
23088 base-clause [opt]
23089
23090 Upon return BASES is initialized to the list of base classes (or
23091 NULL, if there are none) in the same form returned by
23092 cp_parser_base_clause.
23093
23094 Returns the TYPE of the indicated class. Sets
23095 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23096 involving a nested-name-specifier was used, and FALSE otherwise.
23097
23098 Returns error_mark_node if this is not a class-head.
23099
23100 Returns NULL_TREE if the class-head is syntactically valid, but
23101 semantically invalid in a way that means we should skip the entire
23102 body of the class. */
23103
23104 static tree
23105 cp_parser_class_head (cp_parser* parser,
23106 bool* nested_name_specifier_p)
23107 {
23108 tree nested_name_specifier;
23109 enum tag_types class_key;
23110 tree id = NULL_TREE;
23111 tree type = NULL_TREE;
23112 tree attributes;
23113 tree bases;
23114 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23115 bool template_id_p = false;
23116 bool qualified_p = false;
23117 bool invalid_nested_name_p = false;
23118 bool invalid_explicit_specialization_p = false;
23119 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23120 tree pushed_scope = NULL_TREE;
23121 unsigned num_templates;
23122 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23123 /* Assume no nested-name-specifier will be present. */
23124 *nested_name_specifier_p = false;
23125 /* Assume no template parameter lists will be used in defining the
23126 type. */
23127 num_templates = 0;
23128 parser->colon_corrects_to_scope_p = false;
23129
23130 /* Look for the class-key. */
23131 class_key = cp_parser_class_key (parser);
23132 if (class_key == none_type)
23133 return error_mark_node;
23134
23135 location_t class_head_start_location = input_location;
23136
23137 /* Parse the attributes. */
23138 attributes = cp_parser_attributes_opt (parser);
23139
23140 /* If the next token is `::', that is invalid -- but sometimes
23141 people do try to write:
23142
23143 struct ::S {};
23144
23145 Handle this gracefully by accepting the extra qualifier, and then
23146 issuing an error about it later if this really is a
23147 class-head. If it turns out just to be an elaborated type
23148 specifier, remain silent. */
23149 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23150 qualified_p = true;
23151
23152 push_deferring_access_checks (dk_no_check);
23153
23154 /* Determine the name of the class. Begin by looking for an
23155 optional nested-name-specifier. */
23156 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23157 nested_name_specifier
23158 = cp_parser_nested_name_specifier_opt (parser,
23159 /*typename_keyword_p=*/false,
23160 /*check_dependency_p=*/false,
23161 /*type_p=*/true,
23162 /*is_declaration=*/false);
23163 /* If there was a nested-name-specifier, then there *must* be an
23164 identifier. */
23165
23166 cp_token *bad_template_keyword = NULL;
23167
23168 if (nested_name_specifier)
23169 {
23170 type_start_token = cp_lexer_peek_token (parser->lexer);
23171 /* Although the grammar says `identifier', it really means
23172 `class-name' or `template-name'. You are only allowed to
23173 define a class that has already been declared with this
23174 syntax.
23175
23176 The proposed resolution for Core Issue 180 says that wherever
23177 you see `class T::X' you should treat `X' as a type-name.
23178
23179 It is OK to define an inaccessible class; for example:
23180
23181 class A { class B; };
23182 class A::B {};
23183
23184 We do not know if we will see a class-name, or a
23185 template-name. We look for a class-name first, in case the
23186 class-name is a template-id; if we looked for the
23187 template-name first we would stop after the template-name. */
23188 cp_parser_parse_tentatively (parser);
23189 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23190 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23191 type = cp_parser_class_name (parser,
23192 /*typename_keyword_p=*/false,
23193 /*template_keyword_p=*/false,
23194 class_type,
23195 /*check_dependency_p=*/false,
23196 /*class_head_p=*/true,
23197 /*is_declaration=*/false);
23198 /* If that didn't work, ignore the nested-name-specifier. */
23199 if (!cp_parser_parse_definitely (parser))
23200 {
23201 invalid_nested_name_p = true;
23202 type_start_token = cp_lexer_peek_token (parser->lexer);
23203 id = cp_parser_identifier (parser);
23204 if (id == error_mark_node)
23205 id = NULL_TREE;
23206 }
23207 /* If we could not find a corresponding TYPE, treat this
23208 declaration like an unqualified declaration. */
23209 if (type == error_mark_node)
23210 nested_name_specifier = NULL_TREE;
23211 /* Otherwise, count the number of templates used in TYPE and its
23212 containing scopes. */
23213 else
23214 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23215 }
23216 /* Otherwise, the identifier is optional. */
23217 else
23218 {
23219 /* We don't know whether what comes next is a template-id,
23220 an identifier, or nothing at all. */
23221 cp_parser_parse_tentatively (parser);
23222 /* Check for a template-id. */
23223 type_start_token = cp_lexer_peek_token (parser->lexer);
23224 id = cp_parser_template_id (parser,
23225 /*template_keyword_p=*/false,
23226 /*check_dependency_p=*/true,
23227 class_key,
23228 /*is_declaration=*/true);
23229 /* If that didn't work, it could still be an identifier. */
23230 if (!cp_parser_parse_definitely (parser))
23231 {
23232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23233 {
23234 type_start_token = cp_lexer_peek_token (parser->lexer);
23235 id = cp_parser_identifier (parser);
23236 }
23237 else
23238 id = NULL_TREE;
23239 }
23240 else
23241 {
23242 template_id_p = true;
23243 ++num_templates;
23244 }
23245 }
23246
23247 pop_deferring_access_checks ();
23248
23249 if (id)
23250 {
23251 cp_parser_check_for_invalid_template_id (parser, id,
23252 class_key,
23253 type_start_token->location);
23254 }
23255 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23256
23257 /* If it's not a `:' or a `{' then we can't really be looking at a
23258 class-head, since a class-head only appears as part of a
23259 class-specifier. We have to detect this situation before calling
23260 xref_tag, since that has irreversible side-effects. */
23261 if (!cp_parser_next_token_starts_class_definition_p (parser))
23262 {
23263 cp_parser_error (parser, "expected %<{%> or %<:%>");
23264 type = error_mark_node;
23265 goto out;
23266 }
23267
23268 /* At this point, we're going ahead with the class-specifier, even
23269 if some other problem occurs. */
23270 cp_parser_commit_to_tentative_parse (parser);
23271 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23272 {
23273 cp_parser_error (parser,
23274 "cannot specify %<override%> for a class");
23275 type = error_mark_node;
23276 goto out;
23277 }
23278 /* Issue the error about the overly-qualified name now. */
23279 if (qualified_p)
23280 {
23281 cp_parser_error (parser,
23282 "global qualification of class name is invalid");
23283 type = error_mark_node;
23284 goto out;
23285 }
23286 else if (invalid_nested_name_p)
23287 {
23288 cp_parser_error (parser,
23289 "qualified name does not name a class");
23290 type = error_mark_node;
23291 goto out;
23292 }
23293 else if (nested_name_specifier)
23294 {
23295 tree scope;
23296
23297 if (bad_template_keyword)
23298 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23299 keyword template shall not appear at the top level. */
23300 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23301 "keyword %<template%> not allowed in class-head-name");
23302
23303 /* Reject typedef-names in class heads. */
23304 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23305 {
23306 error_at (type_start_token->location,
23307 "invalid class name in declaration of %qD",
23308 type);
23309 type = NULL_TREE;
23310 goto done;
23311 }
23312
23313 /* Figure out in what scope the declaration is being placed. */
23314 scope = current_scope ();
23315 /* If that scope does not contain the scope in which the
23316 class was originally declared, the program is invalid. */
23317 if (scope && !is_ancestor (scope, nested_name_specifier))
23318 {
23319 if (at_namespace_scope_p ())
23320 error_at (type_start_token->location,
23321 "declaration of %qD in namespace %qD which does not "
23322 "enclose %qD",
23323 type, scope, nested_name_specifier);
23324 else
23325 error_at (type_start_token->location,
23326 "declaration of %qD in %qD which does not enclose %qD",
23327 type, scope, nested_name_specifier);
23328 type = NULL_TREE;
23329 goto done;
23330 }
23331 /* [dcl.meaning]
23332
23333 A declarator-id shall not be qualified except for the
23334 definition of a ... nested class outside of its class
23335 ... [or] the definition or explicit instantiation of a
23336 class member of a namespace outside of its namespace. */
23337 if (scope == nested_name_specifier)
23338 {
23339 permerror (nested_name_specifier_token_start->location,
23340 "extra qualification not allowed");
23341 nested_name_specifier = NULL_TREE;
23342 num_templates = 0;
23343 }
23344 }
23345 /* An explicit-specialization must be preceded by "template <>". If
23346 it is not, try to recover gracefully. */
23347 if (at_namespace_scope_p ()
23348 && parser->num_template_parameter_lists == 0
23349 && !processing_template_parmlist
23350 && template_id_p)
23351 {
23352 /* Build a location of this form:
23353 struct typename <ARGS>
23354 ^~~~~~~~~~~~~~~~~~~~~~
23355 with caret==start at the start token, and
23356 finishing at the end of the type. */
23357 location_t reported_loc
23358 = make_location (class_head_start_location,
23359 class_head_start_location,
23360 get_finish (type_start_token->location));
23361 rich_location richloc (line_table, reported_loc);
23362 richloc.add_fixit_insert_before (class_head_start_location,
23363 "template <> ");
23364 error_at (&richloc,
23365 "an explicit specialization must be preceded by"
23366 " %<template <>%>");
23367 invalid_explicit_specialization_p = true;
23368 /* Take the same action that would have been taken by
23369 cp_parser_explicit_specialization. */
23370 ++parser->num_template_parameter_lists;
23371 begin_specialization ();
23372 }
23373 /* There must be no "return" statements between this point and the
23374 end of this function; set "type "to the correct return value and
23375 use "goto done;" to return. */
23376 /* Make sure that the right number of template parameters were
23377 present. */
23378 if (!cp_parser_check_template_parameters (parser, num_templates,
23379 template_id_p,
23380 type_start_token->location,
23381 /*declarator=*/NULL))
23382 {
23383 /* If something went wrong, there is no point in even trying to
23384 process the class-definition. */
23385 type = NULL_TREE;
23386 goto done;
23387 }
23388
23389 /* Look up the type. */
23390 if (template_id_p)
23391 {
23392 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23393 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23394 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23395 {
23396 error_at (type_start_token->location,
23397 "function template %qD redeclared as a class template", id);
23398 type = error_mark_node;
23399 }
23400 else
23401 {
23402 type = TREE_TYPE (id);
23403 type = maybe_process_partial_specialization (type);
23404
23405 /* Check the scope while we still know whether or not we had a
23406 nested-name-specifier. */
23407 if (type != error_mark_node)
23408 check_unqualified_spec_or_inst (type, type_start_token->location);
23409 }
23410 if (nested_name_specifier)
23411 pushed_scope = push_scope (nested_name_specifier);
23412 }
23413 else if (nested_name_specifier)
23414 {
23415 tree class_type;
23416
23417 /* Given:
23418
23419 template <typename T> struct S { struct T };
23420 template <typename T> struct S<T>::T { };
23421
23422 we will get a TYPENAME_TYPE when processing the definition of
23423 `S::T'. We need to resolve it to the actual type before we
23424 try to define it. */
23425 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23426 {
23427 class_type = resolve_typename_type (TREE_TYPE (type),
23428 /*only_current_p=*/false);
23429 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23430 type = TYPE_NAME (class_type);
23431 else
23432 {
23433 cp_parser_error (parser, "could not resolve typename type");
23434 type = error_mark_node;
23435 }
23436 }
23437
23438 if (maybe_process_partial_specialization (TREE_TYPE (type))
23439 == error_mark_node)
23440 {
23441 type = NULL_TREE;
23442 goto done;
23443 }
23444
23445 class_type = current_class_type;
23446 /* Enter the scope indicated by the nested-name-specifier. */
23447 pushed_scope = push_scope (nested_name_specifier);
23448 /* Get the canonical version of this type. */
23449 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23450 /* Call push_template_decl if it seems like we should be defining a
23451 template either from the template headers or the type we're
23452 defining, so that we diagnose both extra and missing headers. */
23453 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23454 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23455 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23456 {
23457 type = push_template_decl (type);
23458 if (type == error_mark_node)
23459 {
23460 type = NULL_TREE;
23461 goto done;
23462 }
23463 }
23464
23465 type = TREE_TYPE (type);
23466 *nested_name_specifier_p = true;
23467 }
23468 else /* The name is not a nested name. */
23469 {
23470 /* If the class was unnamed, create a dummy name. */
23471 if (!id)
23472 id = make_anon_name ();
23473 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23474 ? ts_within_enclosing_non_class
23475 : ts_current);
23476 type = xref_tag (class_key, id, tag_scope,
23477 parser->num_template_parameter_lists);
23478 }
23479
23480 /* Indicate whether this class was declared as a `class' or as a
23481 `struct'. */
23482 if (TREE_CODE (type) == RECORD_TYPE)
23483 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23484 cp_parser_check_class_key (class_key, type);
23485
23486 /* If this type was already complete, and we see another definition,
23487 that's an error. */
23488 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23489 {
23490 error_at (type_start_token->location, "redefinition of %q#T",
23491 type);
23492 inform (location_of (type), "previous definition of %q#T",
23493 type);
23494 type = NULL_TREE;
23495 goto done;
23496 }
23497 else if (type == error_mark_node)
23498 type = NULL_TREE;
23499
23500 if (type)
23501 {
23502 /* Apply attributes now, before any use of the class as a template
23503 argument in its base list. */
23504 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23505 fixup_attribute_variants (type);
23506 }
23507
23508 /* We will have entered the scope containing the class; the names of
23509 base classes should be looked up in that context. For example:
23510
23511 struct A { struct B {}; struct C; };
23512 struct A::C : B {};
23513
23514 is valid. */
23515
23516 /* Get the list of base-classes, if there is one. */
23517 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23518 {
23519 /* PR59482: enter the class scope so that base-specifiers are looked
23520 up correctly. */
23521 if (type)
23522 pushclass (type);
23523 bases = cp_parser_base_clause (parser);
23524 /* PR59482: get out of the previously pushed class scope so that the
23525 subsequent pops pop the right thing. */
23526 if (type)
23527 popclass ();
23528 }
23529 else
23530 bases = NULL_TREE;
23531
23532 /* If we're really defining a class, process the base classes.
23533 If they're invalid, fail. */
23534 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23535 xref_basetypes (type, bases);
23536
23537 done:
23538 /* Leave the scope given by the nested-name-specifier. We will
23539 enter the class scope itself while processing the members. */
23540 if (pushed_scope)
23541 pop_scope (pushed_scope);
23542
23543 if (invalid_explicit_specialization_p)
23544 {
23545 end_specialization ();
23546 --parser->num_template_parameter_lists;
23547 }
23548
23549 if (type)
23550 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23551 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23552 CLASSTYPE_FINAL (type) = 1;
23553 out:
23554 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23555 return type;
23556 }
23557
23558 /* Parse a class-key.
23559
23560 class-key:
23561 class
23562 struct
23563 union
23564
23565 Returns the kind of class-key specified, or none_type to indicate
23566 error. */
23567
23568 static enum tag_types
23569 cp_parser_class_key (cp_parser* parser)
23570 {
23571 cp_token *token;
23572 enum tag_types tag_type;
23573
23574 /* Look for the class-key. */
23575 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23576 if (!token)
23577 return none_type;
23578
23579 /* Check to see if the TOKEN is a class-key. */
23580 tag_type = cp_parser_token_is_class_key (token);
23581 if (!tag_type)
23582 cp_parser_error (parser, "expected class-key");
23583 return tag_type;
23584 }
23585
23586 /* Parse a type-parameter-key.
23587
23588 type-parameter-key:
23589 class
23590 typename
23591 */
23592
23593 static void
23594 cp_parser_type_parameter_key (cp_parser* parser)
23595 {
23596 /* Look for the type-parameter-key. */
23597 enum tag_types tag_type = none_type;
23598 cp_token *token = cp_lexer_peek_token (parser->lexer);
23599 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23600 {
23601 cp_lexer_consume_token (parser->lexer);
23602 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23603 /* typename is not allowed in a template template parameter
23604 by the standard until C++17. */
23605 pedwarn (token->location, OPT_Wpedantic,
23606 "ISO C++ forbids typename key in template template parameter;"
23607 " use -std=c++17 or -std=gnu++17");
23608 }
23609 else
23610 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23611
23612 return;
23613 }
23614
23615 /* Parse an (optional) member-specification.
23616
23617 member-specification:
23618 member-declaration member-specification [opt]
23619 access-specifier : member-specification [opt] */
23620
23621 static void
23622 cp_parser_member_specification_opt (cp_parser* parser)
23623 {
23624 while (true)
23625 {
23626 cp_token *token;
23627 enum rid keyword;
23628
23629 /* Peek at the next token. */
23630 token = cp_lexer_peek_token (parser->lexer);
23631 /* If it's a `}', or EOF then we've seen all the members. */
23632 if (token->type == CPP_CLOSE_BRACE
23633 || token->type == CPP_EOF
23634 || token->type == CPP_PRAGMA_EOL)
23635 break;
23636
23637 /* See if this token is a keyword. */
23638 keyword = token->keyword;
23639 switch (keyword)
23640 {
23641 case RID_PUBLIC:
23642 case RID_PROTECTED:
23643 case RID_PRIVATE:
23644 /* Consume the access-specifier. */
23645 cp_lexer_consume_token (parser->lexer);
23646 /* Remember which access-specifier is active. */
23647 current_access_specifier = token->u.value;
23648 /* Look for the `:'. */
23649 cp_parser_require (parser, CPP_COLON, RT_COLON);
23650 break;
23651
23652 default:
23653 /* Accept #pragmas at class scope. */
23654 if (token->type == CPP_PRAGMA)
23655 {
23656 cp_parser_pragma (parser, pragma_member, NULL);
23657 break;
23658 }
23659
23660 /* Otherwise, the next construction must be a
23661 member-declaration. */
23662 cp_parser_member_declaration (parser);
23663 }
23664 }
23665 }
23666
23667 /* Parse a member-declaration.
23668
23669 member-declaration:
23670 decl-specifier-seq [opt] member-declarator-list [opt] ;
23671 function-definition ; [opt]
23672 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23673 using-declaration
23674 template-declaration
23675 alias-declaration
23676
23677 member-declarator-list:
23678 member-declarator
23679 member-declarator-list , member-declarator
23680
23681 member-declarator:
23682 declarator pure-specifier [opt]
23683 declarator constant-initializer [opt]
23684 identifier [opt] : constant-expression
23685
23686 GNU Extensions:
23687
23688 member-declaration:
23689 __extension__ member-declaration
23690
23691 member-declarator:
23692 declarator attributes [opt] pure-specifier [opt]
23693 declarator attributes [opt] constant-initializer [opt]
23694 identifier [opt] attributes [opt] : constant-expression
23695
23696 C++0x Extensions:
23697
23698 member-declaration:
23699 static_assert-declaration */
23700
23701 static void
23702 cp_parser_member_declaration (cp_parser* parser)
23703 {
23704 cp_decl_specifier_seq decl_specifiers;
23705 tree prefix_attributes;
23706 tree decl;
23707 int declares_class_or_enum;
23708 bool friend_p;
23709 cp_token *token = NULL;
23710 cp_token *decl_spec_token_start = NULL;
23711 cp_token *initializer_token_start = NULL;
23712 int saved_pedantic;
23713 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23714
23715 /* Check for the `__extension__' keyword. */
23716 if (cp_parser_extension_opt (parser, &saved_pedantic))
23717 {
23718 /* Recurse. */
23719 cp_parser_member_declaration (parser);
23720 /* Restore the old value of the PEDANTIC flag. */
23721 pedantic = saved_pedantic;
23722
23723 return;
23724 }
23725
23726 /* Check for a template-declaration. */
23727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23728 {
23729 /* An explicit specialization here is an error condition, and we
23730 expect the specialization handler to detect and report this. */
23731 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23732 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23733 cp_parser_explicit_specialization (parser);
23734 else
23735 cp_parser_template_declaration (parser, /*member_p=*/true);
23736
23737 return;
23738 }
23739 /* Check for a template introduction. */
23740 else if (cp_parser_template_declaration_after_export (parser, true))
23741 return;
23742
23743 /* Check for a using-declaration. */
23744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23745 {
23746 if (cxx_dialect < cxx11)
23747 {
23748 /* Parse the using-declaration. */
23749 cp_parser_using_declaration (parser,
23750 /*access_declaration_p=*/false);
23751 return;
23752 }
23753 else
23754 {
23755 tree decl;
23756 bool alias_decl_expected;
23757 cp_parser_parse_tentatively (parser);
23758 decl = cp_parser_alias_declaration (parser);
23759 /* Note that if we actually see the '=' token after the
23760 identifier, cp_parser_alias_declaration commits the
23761 tentative parse. In that case, we really expect an
23762 alias-declaration. Otherwise, we expect a using
23763 declaration. */
23764 alias_decl_expected =
23765 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23766 cp_parser_parse_definitely (parser);
23767
23768 if (alias_decl_expected)
23769 finish_member_declaration (decl);
23770 else
23771 cp_parser_using_declaration (parser,
23772 /*access_declaration_p=*/false);
23773 return;
23774 }
23775 }
23776
23777 /* Check for @defs. */
23778 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23779 {
23780 tree ivar, member;
23781 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23782 ivar = ivar_chains;
23783 while (ivar)
23784 {
23785 member = ivar;
23786 ivar = TREE_CHAIN (member);
23787 TREE_CHAIN (member) = NULL_TREE;
23788 finish_member_declaration (member);
23789 }
23790 return;
23791 }
23792
23793 /* If the next token is `static_assert' we have a static assertion. */
23794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23795 {
23796 cp_parser_static_assert (parser, /*member_p=*/true);
23797 return;
23798 }
23799
23800 parser->colon_corrects_to_scope_p = false;
23801
23802 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23803 goto out;
23804
23805 /* Parse the decl-specifier-seq. */
23806 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23807 cp_parser_decl_specifier_seq (parser,
23808 CP_PARSER_FLAGS_OPTIONAL,
23809 &decl_specifiers,
23810 &declares_class_or_enum);
23811 /* Check for an invalid type-name. */
23812 if (!decl_specifiers.any_type_specifiers_p
23813 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23814 goto out;
23815 /* If there is no declarator, then the decl-specifier-seq should
23816 specify a type. */
23817 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23818 {
23819 /* If there was no decl-specifier-seq, and the next token is a
23820 `;', then we have something like:
23821
23822 struct S { ; };
23823
23824 [class.mem]
23825
23826 Each member-declaration shall declare at least one member
23827 name of the class. */
23828 if (!decl_specifiers.any_specifiers_p)
23829 {
23830 cp_token *token = cp_lexer_peek_token (parser->lexer);
23831 if (!in_system_header_at (token->location))
23832 {
23833 gcc_rich_location richloc (token->location);
23834 richloc.add_fixit_remove ();
23835 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23836 }
23837 }
23838 else
23839 {
23840 tree type;
23841
23842 /* See if this declaration is a friend. */
23843 friend_p = cp_parser_friend_p (&decl_specifiers);
23844 /* If there were decl-specifiers, check to see if there was
23845 a class-declaration. */
23846 type = check_tag_decl (&decl_specifiers,
23847 /*explicit_type_instantiation_p=*/false);
23848 /* Nested classes have already been added to the class, but
23849 a `friend' needs to be explicitly registered. */
23850 if (friend_p)
23851 {
23852 /* If the `friend' keyword was present, the friend must
23853 be introduced with a class-key. */
23854 if (!declares_class_or_enum && cxx_dialect < cxx11)
23855 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23856 "in C++03 a class-key must be used "
23857 "when declaring a friend");
23858 /* In this case:
23859
23860 template <typename T> struct A {
23861 friend struct A<T>::B;
23862 };
23863
23864 A<T>::B will be represented by a TYPENAME_TYPE, and
23865 therefore not recognized by check_tag_decl. */
23866 if (!type)
23867 {
23868 type = decl_specifiers.type;
23869 if (type && TREE_CODE (type) == TYPE_DECL)
23870 type = TREE_TYPE (type);
23871 }
23872 if (!type || !TYPE_P (type))
23873 error_at (decl_spec_token_start->location,
23874 "friend declaration does not name a class or "
23875 "function");
23876 else
23877 make_friend_class (current_class_type, type,
23878 /*complain=*/true);
23879 }
23880 /* If there is no TYPE, an error message will already have
23881 been issued. */
23882 else if (!type || type == error_mark_node)
23883 ;
23884 /* An anonymous aggregate has to be handled specially; such
23885 a declaration really declares a data member (with a
23886 particular type), as opposed to a nested class. */
23887 else if (ANON_AGGR_TYPE_P (type))
23888 {
23889 /* C++11 9.5/6. */
23890 if (decl_specifiers.storage_class != sc_none)
23891 error_at (decl_spec_token_start->location,
23892 "a storage class on an anonymous aggregate "
23893 "in class scope is not allowed");
23894
23895 /* Remove constructors and such from TYPE, now that we
23896 know it is an anonymous aggregate. */
23897 fixup_anonymous_aggr (type);
23898 /* And make the corresponding data member. */
23899 decl = build_decl (decl_spec_token_start->location,
23900 FIELD_DECL, NULL_TREE, type);
23901 /* Add it to the class. */
23902 finish_member_declaration (decl);
23903 }
23904 else
23905 cp_parser_check_access_in_redeclaration
23906 (TYPE_NAME (type),
23907 decl_spec_token_start->location);
23908 }
23909 }
23910 else
23911 {
23912 bool assume_semicolon = false;
23913
23914 /* Clear attributes from the decl_specifiers but keep them
23915 around as prefix attributes that apply them to the entity
23916 being declared. */
23917 prefix_attributes = decl_specifiers.attributes;
23918 decl_specifiers.attributes = NULL_TREE;
23919
23920 /* See if these declarations will be friends. */
23921 friend_p = cp_parser_friend_p (&decl_specifiers);
23922
23923 /* Keep going until we hit the `;' at the end of the
23924 declaration. */
23925 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23926 {
23927 tree attributes = NULL_TREE;
23928 tree first_attribute;
23929 tree initializer;
23930 bool named_bitfld = false;
23931
23932 /* Peek at the next token. */
23933 token = cp_lexer_peek_token (parser->lexer);
23934
23935 /* The following code wants to know early if it is a bit-field
23936 or some other declaration. Attributes can appear before
23937 the `:' token. Skip over them without consuming any tokens
23938 to peek if they are followed by `:'. */
23939 if (cp_next_tokens_can_be_attribute_p (parser)
23940 || (token->type == CPP_NAME
23941 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23942 && (named_bitfld = true)))
23943 {
23944 size_t n
23945 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23946 token = cp_lexer_peek_nth_token (parser->lexer, n);
23947 }
23948
23949 /* Check for a bitfield declaration. */
23950 if (token->type == CPP_COLON
23951 || (token->type == CPP_NAME
23952 && token == cp_lexer_peek_token (parser->lexer)
23953 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23954 && (named_bitfld = true)))
23955 {
23956 tree identifier;
23957 tree width;
23958 tree late_attributes = NULL_TREE;
23959
23960 if (named_bitfld)
23961 identifier = cp_parser_identifier (parser);
23962 else
23963 identifier = NULL_TREE;
23964
23965 /* Look for attributes that apply to the bitfield. */
23966 attributes = cp_parser_attributes_opt (parser);
23967
23968 /* Consume the `:' token. */
23969 cp_lexer_consume_token (parser->lexer);
23970
23971 /* Get the width of the bitfield. */
23972 width = cp_parser_constant_expression (parser, false, NULL,
23973 cxx_dialect >= cxx11);
23974
23975 /* In C++2A and as extension for C++11 and above we allow
23976 default member initializers for bit-fields. */
23977 initializer = NULL_TREE;
23978 if (cxx_dialect >= cxx11
23979 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23980 || cp_lexer_next_token_is (parser->lexer,
23981 CPP_OPEN_BRACE)))
23982 {
23983 location_t loc
23984 = cp_lexer_peek_token (parser->lexer)->location;
23985 if (cxx_dialect < cxx2a
23986 && !in_system_header_at (loc)
23987 && identifier != NULL_TREE)
23988 pedwarn (loc, 0,
23989 "default member initializers for bit-fields "
23990 "only available with -std=c++2a or "
23991 "-std=gnu++2a");
23992
23993 initializer = cp_parser_save_nsdmi (parser);
23994 if (identifier == NULL_TREE)
23995 {
23996 error_at (loc, "default member initializer for "
23997 "unnamed bit-field");
23998 initializer = NULL_TREE;
23999 }
24000 }
24001 else
24002 {
24003 /* Look for attributes that apply to the bitfield after
24004 the `:' token and width. This is where GCC used to
24005 parse attributes in the past, pedwarn if there is
24006 a std attribute. */
24007 if (cp_next_tokens_can_be_std_attribute_p (parser))
24008 pedwarn (input_location, OPT_Wpedantic,
24009 "ISO C++ allows bit-field attributes only "
24010 "before the %<:%> token");
24011
24012 late_attributes = cp_parser_attributes_opt (parser);
24013 }
24014
24015 attributes = attr_chainon (attributes, late_attributes);
24016
24017 /* Remember which attributes are prefix attributes and
24018 which are not. */
24019 first_attribute = attributes;
24020 /* Combine the attributes. */
24021 attributes = attr_chainon (prefix_attributes, attributes);
24022
24023 /* Create the bitfield declaration. */
24024 decl = grokbitfield (identifier
24025 ? make_id_declarator (NULL_TREE,
24026 identifier,
24027 sfk_none)
24028 : NULL,
24029 &decl_specifiers,
24030 width, initializer,
24031 attributes);
24032 }
24033 else
24034 {
24035 cp_declarator *declarator;
24036 tree asm_specification;
24037 int ctor_dtor_or_conv_p;
24038
24039 /* Parse the declarator. */
24040 declarator
24041 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24042 &ctor_dtor_or_conv_p,
24043 /*parenthesized_p=*/NULL,
24044 /*member_p=*/true,
24045 friend_p);
24046
24047 /* If something went wrong parsing the declarator, make sure
24048 that we at least consume some tokens. */
24049 if (declarator == cp_error_declarator)
24050 {
24051 /* Skip to the end of the statement. */
24052 cp_parser_skip_to_end_of_statement (parser);
24053 /* If the next token is not a semicolon, that is
24054 probably because we just skipped over the body of
24055 a function. So, we consume a semicolon if
24056 present, but do not issue an error message if it
24057 is not present. */
24058 if (cp_lexer_next_token_is (parser->lexer,
24059 CPP_SEMICOLON))
24060 cp_lexer_consume_token (parser->lexer);
24061 goto out;
24062 }
24063
24064 if (declares_class_or_enum & 2)
24065 cp_parser_check_for_definition_in_return_type
24066 (declarator, decl_specifiers.type,
24067 decl_specifiers.locations[ds_type_spec]);
24068
24069 /* Look for an asm-specification. */
24070 asm_specification = cp_parser_asm_specification_opt (parser);
24071 /* Look for attributes that apply to the declaration. */
24072 attributes = cp_parser_attributes_opt (parser);
24073 /* Remember which attributes are prefix attributes and
24074 which are not. */
24075 first_attribute = attributes;
24076 /* Combine the attributes. */
24077 attributes = attr_chainon (prefix_attributes, attributes);
24078
24079 /* If it's an `=', then we have a constant-initializer or a
24080 pure-specifier. It is not correct to parse the
24081 initializer before registering the member declaration
24082 since the member declaration should be in scope while
24083 its initializer is processed. However, the rest of the
24084 front end does not yet provide an interface that allows
24085 us to handle this correctly. */
24086 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24087 {
24088 /* In [class.mem]:
24089
24090 A pure-specifier shall be used only in the declaration of
24091 a virtual function.
24092
24093 A member-declarator can contain a constant-initializer
24094 only if it declares a static member of integral or
24095 enumeration type.
24096
24097 Therefore, if the DECLARATOR is for a function, we look
24098 for a pure-specifier; otherwise, we look for a
24099 constant-initializer. When we call `grokfield', it will
24100 perform more stringent semantics checks. */
24101 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24102 if (function_declarator_p (declarator)
24103 || (decl_specifiers.type
24104 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24105 && declarator->kind == cdk_id
24106 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24107 == FUNCTION_TYPE)))
24108 initializer = cp_parser_pure_specifier (parser);
24109 else if (decl_specifiers.storage_class != sc_static)
24110 initializer = cp_parser_save_nsdmi (parser);
24111 else if (cxx_dialect >= cxx11)
24112 {
24113 bool nonconst;
24114 /* Don't require a constant rvalue in C++11, since we
24115 might want a reference constant. We'll enforce
24116 constancy later. */
24117 cp_lexer_consume_token (parser->lexer);
24118 /* Parse the initializer. */
24119 initializer = cp_parser_initializer_clause (parser,
24120 &nonconst);
24121 }
24122 else
24123 /* Parse the initializer. */
24124 initializer = cp_parser_constant_initializer (parser);
24125 }
24126 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24127 && !function_declarator_p (declarator))
24128 {
24129 bool x;
24130 if (decl_specifiers.storage_class != sc_static)
24131 initializer = cp_parser_save_nsdmi (parser);
24132 else
24133 initializer = cp_parser_initializer (parser, &x, &x);
24134 }
24135 /* Otherwise, there is no initializer. */
24136 else
24137 initializer = NULL_TREE;
24138
24139 /* See if we are probably looking at a function
24140 definition. We are certainly not looking at a
24141 member-declarator. Calling `grokfield' has
24142 side-effects, so we must not do it unless we are sure
24143 that we are looking at a member-declarator. */
24144 if (cp_parser_token_starts_function_definition_p
24145 (cp_lexer_peek_token (parser->lexer)))
24146 {
24147 /* The grammar does not allow a pure-specifier to be
24148 used when a member function is defined. (It is
24149 possible that this fact is an oversight in the
24150 standard, since a pure function may be defined
24151 outside of the class-specifier. */
24152 if (initializer && initializer_token_start)
24153 error_at (initializer_token_start->location,
24154 "pure-specifier on function-definition");
24155 decl = cp_parser_save_member_function_body (parser,
24156 &decl_specifiers,
24157 declarator,
24158 attributes);
24159 if (parser->fully_implicit_function_template_p)
24160 decl = finish_fully_implicit_template (parser, decl);
24161 /* If the member was not a friend, declare it here. */
24162 if (!friend_p)
24163 finish_member_declaration (decl);
24164 /* Peek at the next token. */
24165 token = cp_lexer_peek_token (parser->lexer);
24166 /* If the next token is a semicolon, consume it. */
24167 if (token->type == CPP_SEMICOLON)
24168 {
24169 location_t semicolon_loc
24170 = cp_lexer_consume_token (parser->lexer)->location;
24171 gcc_rich_location richloc (semicolon_loc);
24172 richloc.add_fixit_remove ();
24173 warning_at (&richloc, OPT_Wextra_semi,
24174 "extra %<;%> after in-class "
24175 "function definition");
24176 }
24177 goto out;
24178 }
24179 else
24180 if (declarator->kind == cdk_function)
24181 declarator->id_loc = token->location;
24182 /* Create the declaration. */
24183 decl = grokfield (declarator, &decl_specifiers,
24184 initializer, /*init_const_expr_p=*/true,
24185 asm_specification, attributes);
24186 if (parser->fully_implicit_function_template_p)
24187 {
24188 if (friend_p)
24189 finish_fully_implicit_template (parser, 0);
24190 else
24191 decl = finish_fully_implicit_template (parser, decl);
24192 }
24193 }
24194
24195 cp_finalize_omp_declare_simd (parser, decl);
24196 cp_finalize_oacc_routine (parser, decl, false);
24197
24198 /* Reset PREFIX_ATTRIBUTES. */
24199 if (attributes != error_mark_node)
24200 {
24201 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24202 attributes = TREE_CHAIN (attributes);
24203 if (attributes)
24204 TREE_CHAIN (attributes) = NULL_TREE;
24205 }
24206
24207 /* If there is any qualification still in effect, clear it
24208 now; we will be starting fresh with the next declarator. */
24209 parser->scope = NULL_TREE;
24210 parser->qualifying_scope = NULL_TREE;
24211 parser->object_scope = NULL_TREE;
24212 /* If it's a `,', then there are more declarators. */
24213 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24214 {
24215 cp_lexer_consume_token (parser->lexer);
24216 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24217 {
24218 cp_token *token = cp_lexer_previous_token (parser->lexer);
24219 gcc_rich_location richloc (token->location);
24220 richloc.add_fixit_remove ();
24221 error_at (&richloc, "stray %<,%> at end of "
24222 "member declaration");
24223 }
24224 }
24225 /* If the next token isn't a `;', then we have a parse error. */
24226 else if (cp_lexer_next_token_is_not (parser->lexer,
24227 CPP_SEMICOLON))
24228 {
24229 /* The next token might be a ways away from where the
24230 actual semicolon is missing. Find the previous token
24231 and use that for our error position. */
24232 cp_token *token = cp_lexer_previous_token (parser->lexer);
24233 gcc_rich_location richloc (token->location);
24234 richloc.add_fixit_insert_after (";");
24235 error_at (&richloc, "expected %<;%> at end of "
24236 "member declaration");
24237
24238 /* Assume that the user meant to provide a semicolon. If
24239 we were to cp_parser_skip_to_end_of_statement, we might
24240 skip to a semicolon inside a member function definition
24241 and issue nonsensical error messages. */
24242 assume_semicolon = true;
24243 }
24244
24245 if (decl)
24246 {
24247 /* Add DECL to the list of members. */
24248 if (!friend_p
24249 /* Explicitly include, eg, NSDMIs, for better error
24250 recovery (c++/58650). */
24251 || !DECL_DECLARES_FUNCTION_P (decl))
24252 finish_member_declaration (decl);
24253
24254 if (TREE_CODE (decl) == FUNCTION_DECL)
24255 cp_parser_save_default_args (parser, decl);
24256 else if (TREE_CODE (decl) == FIELD_DECL
24257 && DECL_INITIAL (decl))
24258 /* Add DECL to the queue of NSDMI to be parsed later. */
24259 vec_safe_push (unparsed_nsdmis, decl);
24260 }
24261
24262 if (assume_semicolon)
24263 goto out;
24264 }
24265 }
24266
24267 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24268 out:
24269 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24270 }
24271
24272 /* Parse a pure-specifier.
24273
24274 pure-specifier:
24275 = 0
24276
24277 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24278 Otherwise, ERROR_MARK_NODE is returned. */
24279
24280 static tree
24281 cp_parser_pure_specifier (cp_parser* parser)
24282 {
24283 cp_token *token;
24284
24285 /* Look for the `=' token. */
24286 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24287 return error_mark_node;
24288 /* Look for the `0' token. */
24289 token = cp_lexer_peek_token (parser->lexer);
24290
24291 if (token->type == CPP_EOF
24292 || token->type == CPP_PRAGMA_EOL)
24293 return error_mark_node;
24294
24295 cp_lexer_consume_token (parser->lexer);
24296
24297 /* Accept = default or = delete in c++0x mode. */
24298 if (token->keyword == RID_DEFAULT
24299 || token->keyword == RID_DELETE)
24300 {
24301 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24302 return token->u.value;
24303 }
24304
24305 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24306 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24307 {
24308 cp_parser_error (parser,
24309 "invalid pure specifier (only %<= 0%> is allowed)");
24310 cp_parser_skip_to_end_of_statement (parser);
24311 return error_mark_node;
24312 }
24313 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24314 {
24315 error_at (token->location, "templates may not be %<virtual%>");
24316 return error_mark_node;
24317 }
24318
24319 return integer_zero_node;
24320 }
24321
24322 /* Parse a constant-initializer.
24323
24324 constant-initializer:
24325 = constant-expression
24326
24327 Returns a representation of the constant-expression. */
24328
24329 static tree
24330 cp_parser_constant_initializer (cp_parser* parser)
24331 {
24332 /* Look for the `=' token. */
24333 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24334 return error_mark_node;
24335
24336 /* It is invalid to write:
24337
24338 struct S { static const int i = { 7 }; };
24339
24340 */
24341 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24342 {
24343 cp_parser_error (parser,
24344 "a brace-enclosed initializer is not allowed here");
24345 /* Consume the opening brace. */
24346 matching_braces braces;
24347 braces.consume_open (parser);
24348 /* Skip the initializer. */
24349 cp_parser_skip_to_closing_brace (parser);
24350 /* Look for the trailing `}'. */
24351 braces.require_close (parser);
24352
24353 return error_mark_node;
24354 }
24355
24356 return cp_parser_constant_expression (parser);
24357 }
24358
24359 /* Derived classes [gram.class.derived] */
24360
24361 /* Parse a base-clause.
24362
24363 base-clause:
24364 : base-specifier-list
24365
24366 base-specifier-list:
24367 base-specifier ... [opt]
24368 base-specifier-list , base-specifier ... [opt]
24369
24370 Returns a TREE_LIST representing the base-classes, in the order in
24371 which they were declared. The representation of each node is as
24372 described by cp_parser_base_specifier.
24373
24374 In the case that no bases are specified, this function will return
24375 NULL_TREE, not ERROR_MARK_NODE. */
24376
24377 static tree
24378 cp_parser_base_clause (cp_parser* parser)
24379 {
24380 tree bases = NULL_TREE;
24381
24382 /* Look for the `:' that begins the list. */
24383 cp_parser_require (parser, CPP_COLON, RT_COLON);
24384
24385 /* Scan the base-specifier-list. */
24386 while (true)
24387 {
24388 cp_token *token;
24389 tree base;
24390 bool pack_expansion_p = false;
24391
24392 /* Look for the base-specifier. */
24393 base = cp_parser_base_specifier (parser);
24394 /* Look for the (optional) ellipsis. */
24395 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24396 {
24397 /* Consume the `...'. */
24398 cp_lexer_consume_token (parser->lexer);
24399
24400 pack_expansion_p = true;
24401 }
24402
24403 /* Add BASE to the front of the list. */
24404 if (base && base != error_mark_node)
24405 {
24406 if (pack_expansion_p)
24407 /* Make this a pack expansion type. */
24408 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24409
24410 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24411 {
24412 TREE_CHAIN (base) = bases;
24413 bases = base;
24414 }
24415 }
24416 /* Peek at the next token. */
24417 token = cp_lexer_peek_token (parser->lexer);
24418 /* If it's not a comma, then the list is complete. */
24419 if (token->type != CPP_COMMA)
24420 break;
24421 /* Consume the `,'. */
24422 cp_lexer_consume_token (parser->lexer);
24423 }
24424
24425 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24426 base class had a qualified name. However, the next name that
24427 appears is certainly not qualified. */
24428 parser->scope = NULL_TREE;
24429 parser->qualifying_scope = NULL_TREE;
24430 parser->object_scope = NULL_TREE;
24431
24432 return nreverse (bases);
24433 }
24434
24435 /* Parse a base-specifier.
24436
24437 base-specifier:
24438 :: [opt] nested-name-specifier [opt] class-name
24439 virtual access-specifier [opt] :: [opt] nested-name-specifier
24440 [opt] class-name
24441 access-specifier virtual [opt] :: [opt] nested-name-specifier
24442 [opt] class-name
24443
24444 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24445 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24446 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24447 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24448
24449 static tree
24450 cp_parser_base_specifier (cp_parser* parser)
24451 {
24452 cp_token *token;
24453 bool done = false;
24454 bool virtual_p = false;
24455 bool duplicate_virtual_error_issued_p = false;
24456 bool duplicate_access_error_issued_p = false;
24457 bool class_scope_p, template_p;
24458 tree access = access_default_node;
24459 tree type;
24460
24461 /* Process the optional `virtual' and `access-specifier'. */
24462 while (!done)
24463 {
24464 /* Peek at the next token. */
24465 token = cp_lexer_peek_token (parser->lexer);
24466 /* Process `virtual'. */
24467 switch (token->keyword)
24468 {
24469 case RID_VIRTUAL:
24470 /* If `virtual' appears more than once, issue an error. */
24471 if (virtual_p && !duplicate_virtual_error_issued_p)
24472 {
24473 cp_parser_error (parser,
24474 "%<virtual%> specified more than once in base-specifier");
24475 duplicate_virtual_error_issued_p = true;
24476 }
24477
24478 virtual_p = true;
24479
24480 /* Consume the `virtual' token. */
24481 cp_lexer_consume_token (parser->lexer);
24482
24483 break;
24484
24485 case RID_PUBLIC:
24486 case RID_PROTECTED:
24487 case RID_PRIVATE:
24488 /* If more than one access specifier appears, issue an
24489 error. */
24490 if (access != access_default_node
24491 && !duplicate_access_error_issued_p)
24492 {
24493 cp_parser_error (parser,
24494 "more than one access specifier in base-specifier");
24495 duplicate_access_error_issued_p = true;
24496 }
24497
24498 access = ridpointers[(int) token->keyword];
24499
24500 /* Consume the access-specifier. */
24501 cp_lexer_consume_token (parser->lexer);
24502
24503 break;
24504
24505 default:
24506 done = true;
24507 break;
24508 }
24509 }
24510 /* It is not uncommon to see programs mechanically, erroneously, use
24511 the 'typename' keyword to denote (dependent) qualified types
24512 as base classes. */
24513 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24514 {
24515 token = cp_lexer_peek_token (parser->lexer);
24516 if (!processing_template_decl)
24517 error_at (token->location,
24518 "keyword %<typename%> not allowed outside of templates");
24519 else
24520 error_at (token->location,
24521 "keyword %<typename%> not allowed in this context "
24522 "(the base class is implicitly a type)");
24523 cp_lexer_consume_token (parser->lexer);
24524 }
24525
24526 /* Look for the optional `::' operator. */
24527 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24528 /* Look for the nested-name-specifier. The simplest way to
24529 implement:
24530
24531 [temp.res]
24532
24533 The keyword `typename' is not permitted in a base-specifier or
24534 mem-initializer; in these contexts a qualified name that
24535 depends on a template-parameter is implicitly assumed to be a
24536 type name.
24537
24538 is to pretend that we have seen the `typename' keyword at this
24539 point. */
24540 cp_parser_nested_name_specifier_opt (parser,
24541 /*typename_keyword_p=*/true,
24542 /*check_dependency_p=*/true,
24543 /*type_p=*/true,
24544 /*is_declaration=*/true);
24545 /* If the base class is given by a qualified name, assume that names
24546 we see are type names or templates, as appropriate. */
24547 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24548 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24549
24550 if (!parser->scope
24551 && cp_lexer_next_token_is_decltype (parser->lexer))
24552 /* DR 950 allows decltype as a base-specifier. */
24553 type = cp_parser_decltype (parser);
24554 else
24555 {
24556 /* Otherwise, look for the class-name. */
24557 type = cp_parser_class_name (parser,
24558 class_scope_p,
24559 template_p,
24560 typename_type,
24561 /*check_dependency_p=*/true,
24562 /*class_head_p=*/false,
24563 /*is_declaration=*/true);
24564 type = TREE_TYPE (type);
24565 }
24566
24567 if (type == error_mark_node)
24568 return error_mark_node;
24569
24570 return finish_base_specifier (type, access, virtual_p);
24571 }
24572
24573 /* Exception handling [gram.exception] */
24574
24575 /* Parse an (optional) noexcept-specification.
24576
24577 noexcept-specification:
24578 noexcept ( constant-expression ) [opt]
24579
24580 If no noexcept-specification is present, returns NULL_TREE.
24581 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24582 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24583 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24584 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24585 in which case a boolean condition is returned instead. */
24586
24587 static tree
24588 cp_parser_noexcept_specification_opt (cp_parser* parser,
24589 bool require_constexpr,
24590 bool* consumed_expr,
24591 bool return_cond)
24592 {
24593 cp_token *token;
24594 const char *saved_message;
24595
24596 /* Peek at the next token. */
24597 token = cp_lexer_peek_token (parser->lexer);
24598
24599 /* Is it a noexcept-specification? */
24600 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24601 {
24602 tree expr;
24603 cp_lexer_consume_token (parser->lexer);
24604
24605 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24606 {
24607 matching_parens parens;
24608 parens.consume_open (parser);
24609
24610 if (require_constexpr)
24611 {
24612 /* Types may not be defined in an exception-specification. */
24613 saved_message = parser->type_definition_forbidden_message;
24614 parser->type_definition_forbidden_message
24615 = G_("types may not be defined in an exception-specification");
24616
24617 expr = cp_parser_constant_expression (parser);
24618
24619 /* Restore the saved message. */
24620 parser->type_definition_forbidden_message = saved_message;
24621 }
24622 else
24623 {
24624 expr = cp_parser_expression (parser);
24625 *consumed_expr = true;
24626 }
24627
24628 parens.require_close (parser);
24629 }
24630 else
24631 {
24632 expr = boolean_true_node;
24633 if (!require_constexpr)
24634 *consumed_expr = false;
24635 }
24636
24637 /* We cannot build a noexcept-spec right away because this will check
24638 that expr is a constexpr. */
24639 if (!return_cond)
24640 return build_noexcept_spec (expr, tf_warning_or_error);
24641 else
24642 return expr;
24643 }
24644 else
24645 return NULL_TREE;
24646 }
24647
24648 /* Parse an (optional) exception-specification.
24649
24650 exception-specification:
24651 throw ( type-id-list [opt] )
24652
24653 Returns a TREE_LIST representing the exception-specification. The
24654 TREE_VALUE of each node is a type. */
24655
24656 static tree
24657 cp_parser_exception_specification_opt (cp_parser* parser)
24658 {
24659 cp_token *token;
24660 tree type_id_list;
24661 const char *saved_message;
24662
24663 /* Peek at the next token. */
24664 token = cp_lexer_peek_token (parser->lexer);
24665
24666 /* Is it a noexcept-specification? */
24667 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24668 false);
24669 if (type_id_list != NULL_TREE)
24670 return type_id_list;
24671
24672 /* If it's not `throw', then there's no exception-specification. */
24673 if (!cp_parser_is_keyword (token, RID_THROW))
24674 return NULL_TREE;
24675
24676 location_t loc = token->location;
24677
24678 /* Consume the `throw'. */
24679 cp_lexer_consume_token (parser->lexer);
24680
24681 /* Look for the `('. */
24682 matching_parens parens;
24683 parens.require_open (parser);
24684
24685 /* Peek at the next token. */
24686 token = cp_lexer_peek_token (parser->lexer);
24687 /* If it's not a `)', then there is a type-id-list. */
24688 if (token->type != CPP_CLOSE_PAREN)
24689 {
24690 /* Types may not be defined in an exception-specification. */
24691 saved_message = parser->type_definition_forbidden_message;
24692 parser->type_definition_forbidden_message
24693 = G_("types may not be defined in an exception-specification");
24694 /* Parse the type-id-list. */
24695 type_id_list = cp_parser_type_id_list (parser);
24696 /* Restore the saved message. */
24697 parser->type_definition_forbidden_message = saved_message;
24698
24699 if (cxx_dialect >= cxx17)
24700 {
24701 error_at (loc, "ISO C++17 does not allow dynamic exception "
24702 "specifications");
24703 type_id_list = NULL_TREE;
24704 }
24705 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24706 warning_at (loc, OPT_Wdeprecated,
24707 "dynamic exception specifications are deprecated in "
24708 "C++11");
24709 }
24710 /* In C++17, throw() is equivalent to noexcept (true). throw()
24711 is deprecated in C++11 and above as well, but is still widely used,
24712 so don't warn about it yet. */
24713 else if (cxx_dialect >= cxx17)
24714 type_id_list = noexcept_true_spec;
24715 else
24716 type_id_list = empty_except_spec;
24717
24718 /* Look for the `)'. */
24719 parens.require_close (parser);
24720
24721 return type_id_list;
24722 }
24723
24724 /* Parse an (optional) type-id-list.
24725
24726 type-id-list:
24727 type-id ... [opt]
24728 type-id-list , type-id ... [opt]
24729
24730 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24731 in the order that the types were presented. */
24732
24733 static tree
24734 cp_parser_type_id_list (cp_parser* parser)
24735 {
24736 tree types = NULL_TREE;
24737
24738 while (true)
24739 {
24740 cp_token *token;
24741 tree type;
24742
24743 token = cp_lexer_peek_token (parser->lexer);
24744
24745 /* Get the next type-id. */
24746 type = cp_parser_type_id (parser);
24747 /* Check for invalid 'auto'. */
24748 if (flag_concepts && type_uses_auto (type))
24749 {
24750 error_at (token->location,
24751 "invalid use of %<auto%> in exception-specification");
24752 type = error_mark_node;
24753 }
24754 /* Parse the optional ellipsis. */
24755 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24756 {
24757 /* Consume the `...'. */
24758 cp_lexer_consume_token (parser->lexer);
24759
24760 /* Turn the type into a pack expansion expression. */
24761 type = make_pack_expansion (type);
24762 }
24763 /* Add it to the list. */
24764 types = add_exception_specifier (types, type, /*complain=*/1);
24765 /* Peek at the next token. */
24766 token = cp_lexer_peek_token (parser->lexer);
24767 /* If it is not a `,', we are done. */
24768 if (token->type != CPP_COMMA)
24769 break;
24770 /* Consume the `,'. */
24771 cp_lexer_consume_token (parser->lexer);
24772 }
24773
24774 return nreverse (types);
24775 }
24776
24777 /* Parse a try-block.
24778
24779 try-block:
24780 try compound-statement handler-seq */
24781
24782 static tree
24783 cp_parser_try_block (cp_parser* parser)
24784 {
24785 tree try_block;
24786
24787 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24788 if (parser->in_function_body
24789 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24790 error ("%<try%> in %<constexpr%> function");
24791
24792 try_block = begin_try_block ();
24793 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24794 finish_try_block (try_block);
24795 cp_parser_handler_seq (parser);
24796 finish_handler_sequence (try_block);
24797
24798 return try_block;
24799 }
24800
24801 /* Parse a function-try-block.
24802
24803 function-try-block:
24804 try ctor-initializer [opt] function-body handler-seq */
24805
24806 static void
24807 cp_parser_function_try_block (cp_parser* parser)
24808 {
24809 tree compound_stmt;
24810 tree try_block;
24811
24812 /* Look for the `try' keyword. */
24813 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24814 return;
24815 /* Let the rest of the front end know where we are. */
24816 try_block = begin_function_try_block (&compound_stmt);
24817 /* Parse the function-body. */
24818 cp_parser_ctor_initializer_opt_and_function_body
24819 (parser, /*in_function_try_block=*/true);
24820 /* We're done with the `try' part. */
24821 finish_function_try_block (try_block);
24822 /* Parse the handlers. */
24823 cp_parser_handler_seq (parser);
24824 /* We're done with the handlers. */
24825 finish_function_handler_sequence (try_block, compound_stmt);
24826 }
24827
24828 /* Parse a handler-seq.
24829
24830 handler-seq:
24831 handler handler-seq [opt] */
24832
24833 static void
24834 cp_parser_handler_seq (cp_parser* parser)
24835 {
24836 while (true)
24837 {
24838 cp_token *token;
24839
24840 /* Parse the handler. */
24841 cp_parser_handler (parser);
24842 /* Peek at the next token. */
24843 token = cp_lexer_peek_token (parser->lexer);
24844 /* If it's not `catch' then there are no more handlers. */
24845 if (!cp_parser_is_keyword (token, RID_CATCH))
24846 break;
24847 }
24848 }
24849
24850 /* Parse a handler.
24851
24852 handler:
24853 catch ( exception-declaration ) compound-statement */
24854
24855 static void
24856 cp_parser_handler (cp_parser* parser)
24857 {
24858 tree handler;
24859 tree declaration;
24860
24861 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24862 handler = begin_handler ();
24863 matching_parens parens;
24864 parens.require_open (parser);
24865 declaration = cp_parser_exception_declaration (parser);
24866 finish_handler_parms (declaration, handler);
24867 parens.require_close (parser);
24868 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24869 finish_handler (handler);
24870 }
24871
24872 /* Parse an exception-declaration.
24873
24874 exception-declaration:
24875 type-specifier-seq declarator
24876 type-specifier-seq abstract-declarator
24877 type-specifier-seq
24878 ...
24879
24880 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24881 ellipsis variant is used. */
24882
24883 static tree
24884 cp_parser_exception_declaration (cp_parser* parser)
24885 {
24886 cp_decl_specifier_seq type_specifiers;
24887 cp_declarator *declarator;
24888 const char *saved_message;
24889
24890 /* If it's an ellipsis, it's easy to handle. */
24891 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24892 {
24893 /* Consume the `...' token. */
24894 cp_lexer_consume_token (parser->lexer);
24895 return NULL_TREE;
24896 }
24897
24898 /* Types may not be defined in exception-declarations. */
24899 saved_message = parser->type_definition_forbidden_message;
24900 parser->type_definition_forbidden_message
24901 = G_("types may not be defined in exception-declarations");
24902
24903 /* Parse the type-specifier-seq. */
24904 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24905 /*is_trailing_return=*/false,
24906 &type_specifiers);
24907 /* If it's a `)', then there is no declarator. */
24908 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24909 declarator = NULL;
24910 else
24911 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24912 /*ctor_dtor_or_conv_p=*/NULL,
24913 /*parenthesized_p=*/NULL,
24914 /*member_p=*/false,
24915 /*friend_p=*/false);
24916
24917 /* Restore the saved message. */
24918 parser->type_definition_forbidden_message = saved_message;
24919
24920 if (!type_specifiers.any_specifiers_p)
24921 return error_mark_node;
24922
24923 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24924 }
24925
24926 /* Parse a throw-expression.
24927
24928 throw-expression:
24929 throw assignment-expression [opt]
24930
24931 Returns a THROW_EXPR representing the throw-expression. */
24932
24933 static tree
24934 cp_parser_throw_expression (cp_parser* parser)
24935 {
24936 tree expression;
24937 cp_token* token;
24938
24939 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24940 token = cp_lexer_peek_token (parser->lexer);
24941 /* Figure out whether or not there is an assignment-expression
24942 following the "throw" keyword. */
24943 if (token->type == CPP_COMMA
24944 || token->type == CPP_SEMICOLON
24945 || token->type == CPP_CLOSE_PAREN
24946 || token->type == CPP_CLOSE_SQUARE
24947 || token->type == CPP_CLOSE_BRACE
24948 || token->type == CPP_COLON)
24949 expression = NULL_TREE;
24950 else
24951 expression = cp_parser_assignment_expression (parser);
24952
24953 return build_throw (expression);
24954 }
24955
24956 /* GNU Extensions */
24957
24958 /* Parse an (optional) asm-specification.
24959
24960 asm-specification:
24961 asm ( string-literal )
24962
24963 If the asm-specification is present, returns a STRING_CST
24964 corresponding to the string-literal. Otherwise, returns
24965 NULL_TREE. */
24966
24967 static tree
24968 cp_parser_asm_specification_opt (cp_parser* parser)
24969 {
24970 cp_token *token;
24971 tree asm_specification;
24972
24973 /* Peek at the next token. */
24974 token = cp_lexer_peek_token (parser->lexer);
24975 /* If the next token isn't the `asm' keyword, then there's no
24976 asm-specification. */
24977 if (!cp_parser_is_keyword (token, RID_ASM))
24978 return NULL_TREE;
24979
24980 /* Consume the `asm' token. */
24981 cp_lexer_consume_token (parser->lexer);
24982 /* Look for the `('. */
24983 matching_parens parens;
24984 parens.require_open (parser);
24985
24986 /* Look for the string-literal. */
24987 asm_specification = cp_parser_string_literal (parser, false, false);
24988
24989 /* Look for the `)'. */
24990 parens.require_close (parser);
24991
24992 return asm_specification;
24993 }
24994
24995 /* Parse an asm-operand-list.
24996
24997 asm-operand-list:
24998 asm-operand
24999 asm-operand-list , asm-operand
25000
25001 asm-operand:
25002 string-literal ( expression )
25003 [ string-literal ] string-literal ( expression )
25004
25005 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25006 each node is the expression. The TREE_PURPOSE is itself a
25007 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25008 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25009 is a STRING_CST for the string literal before the parenthesis. Returns
25010 ERROR_MARK_NODE if any of the operands are invalid. */
25011
25012 static tree
25013 cp_parser_asm_operand_list (cp_parser* parser)
25014 {
25015 tree asm_operands = NULL_TREE;
25016 bool invalid_operands = false;
25017
25018 while (true)
25019 {
25020 tree string_literal;
25021 tree expression;
25022 tree name;
25023
25024 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25025 {
25026 /* Consume the `[' token. */
25027 cp_lexer_consume_token (parser->lexer);
25028 /* Read the operand name. */
25029 name = cp_parser_identifier (parser);
25030 if (name != error_mark_node)
25031 name = build_string (IDENTIFIER_LENGTH (name),
25032 IDENTIFIER_POINTER (name));
25033 /* Look for the closing `]'. */
25034 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25035 }
25036 else
25037 name = NULL_TREE;
25038 /* Look for the string-literal. */
25039 string_literal = cp_parser_string_literal (parser, false, false);
25040
25041 /* Look for the `('. */
25042 matching_parens parens;
25043 parens.require_open (parser);
25044 /* Parse the expression. */
25045 expression = cp_parser_expression (parser);
25046 /* Look for the `)'. */
25047 parens.require_close (parser);
25048
25049 if (name == error_mark_node
25050 || string_literal == error_mark_node
25051 || expression == error_mark_node)
25052 invalid_operands = true;
25053
25054 /* Add this operand to the list. */
25055 asm_operands = tree_cons (build_tree_list (name, string_literal),
25056 expression,
25057 asm_operands);
25058 /* If the next token is not a `,', there are no more
25059 operands. */
25060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25061 break;
25062 /* Consume the `,'. */
25063 cp_lexer_consume_token (parser->lexer);
25064 }
25065
25066 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25067 }
25068
25069 /* Parse an asm-clobber-list.
25070
25071 asm-clobber-list:
25072 string-literal
25073 asm-clobber-list , string-literal
25074
25075 Returns a TREE_LIST, indicating the clobbers in the order that they
25076 appeared. The TREE_VALUE of each node is a STRING_CST. */
25077
25078 static tree
25079 cp_parser_asm_clobber_list (cp_parser* parser)
25080 {
25081 tree clobbers = NULL_TREE;
25082
25083 while (true)
25084 {
25085 tree string_literal;
25086
25087 /* Look for the string literal. */
25088 string_literal = cp_parser_string_literal (parser, false, false);
25089 /* Add it to the list. */
25090 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25091 /* If the next token is not a `,', then the list is
25092 complete. */
25093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25094 break;
25095 /* Consume the `,' token. */
25096 cp_lexer_consume_token (parser->lexer);
25097 }
25098
25099 return clobbers;
25100 }
25101
25102 /* Parse an asm-label-list.
25103
25104 asm-label-list:
25105 identifier
25106 asm-label-list , identifier
25107
25108 Returns a TREE_LIST, indicating the labels in the order that they
25109 appeared. The TREE_VALUE of each node is a label. */
25110
25111 static tree
25112 cp_parser_asm_label_list (cp_parser* parser)
25113 {
25114 tree labels = NULL_TREE;
25115
25116 while (true)
25117 {
25118 tree identifier, label, name;
25119
25120 /* Look for the identifier. */
25121 identifier = cp_parser_identifier (parser);
25122 if (!error_operand_p (identifier))
25123 {
25124 label = lookup_label (identifier);
25125 if (TREE_CODE (label) == LABEL_DECL)
25126 {
25127 TREE_USED (label) = 1;
25128 check_goto (label);
25129 name = build_string (IDENTIFIER_LENGTH (identifier),
25130 IDENTIFIER_POINTER (identifier));
25131 labels = tree_cons (name, label, labels);
25132 }
25133 }
25134 /* If the next token is not a `,', then the list is
25135 complete. */
25136 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25137 break;
25138 /* Consume the `,' token. */
25139 cp_lexer_consume_token (parser->lexer);
25140 }
25141
25142 return nreverse (labels);
25143 }
25144
25145 /* Return TRUE iff the next tokens in the stream are possibly the
25146 beginning of a GNU extension attribute. */
25147
25148 static bool
25149 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25150 {
25151 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25152 }
25153
25154 /* Return TRUE iff the next tokens in the stream are possibly the
25155 beginning of a standard C++-11 attribute specifier. */
25156
25157 static bool
25158 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25159 {
25160 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25161 }
25162
25163 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25164 beginning of a standard C++-11 attribute specifier. */
25165
25166 static bool
25167 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25168 {
25169 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25170
25171 return (cxx_dialect >= cxx11
25172 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25173 || (token->type == CPP_OPEN_SQUARE
25174 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25175 && token->type == CPP_OPEN_SQUARE)));
25176 }
25177
25178 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25179 beginning of a GNU extension attribute. */
25180
25181 static bool
25182 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25183 {
25184 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25185
25186 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25187 }
25188
25189 /* Return true iff the next tokens can be the beginning of either a
25190 GNU attribute list, or a standard C++11 attribute sequence. */
25191
25192 static bool
25193 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25194 {
25195 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25196 || cp_next_tokens_can_be_std_attribute_p (parser));
25197 }
25198
25199 /* Return true iff the next Nth tokens can be the beginning of either
25200 a GNU attribute list, or a standard C++11 attribute sequence. */
25201
25202 static bool
25203 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25204 {
25205 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25206 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25207 }
25208
25209 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25210 of GNU attributes, or return NULL. */
25211
25212 static tree
25213 cp_parser_attributes_opt (cp_parser *parser)
25214 {
25215 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25216 return cp_parser_gnu_attributes_opt (parser);
25217 return cp_parser_std_attribute_spec_seq (parser);
25218 }
25219
25220 /* Parse an (optional) series of attributes.
25221
25222 attributes:
25223 attributes attribute
25224
25225 attribute:
25226 __attribute__ (( attribute-list [opt] ))
25227
25228 The return value is as for cp_parser_gnu_attribute_list. */
25229
25230 static tree
25231 cp_parser_gnu_attributes_opt (cp_parser* parser)
25232 {
25233 tree attributes = NULL_TREE;
25234
25235 temp_override<bool> cleanup
25236 (parser->auto_is_implicit_function_template_parm_p, false);
25237
25238 while (true)
25239 {
25240 cp_token *token;
25241 tree attribute_list;
25242 bool ok = true;
25243
25244 /* Peek at the next token. */
25245 token = cp_lexer_peek_token (parser->lexer);
25246 /* If it's not `__attribute__', then we're done. */
25247 if (token->keyword != RID_ATTRIBUTE)
25248 break;
25249
25250 /* Consume the `__attribute__' keyword. */
25251 cp_lexer_consume_token (parser->lexer);
25252 /* Look for the two `(' tokens. */
25253 matching_parens outer_parens;
25254 outer_parens.require_open (parser);
25255 matching_parens inner_parens;
25256 inner_parens.require_open (parser);
25257
25258 /* Peek at the next token. */
25259 token = cp_lexer_peek_token (parser->lexer);
25260 if (token->type != CPP_CLOSE_PAREN)
25261 /* Parse the attribute-list. */
25262 attribute_list = cp_parser_gnu_attribute_list (parser);
25263 else
25264 /* If the next token is a `)', then there is no attribute
25265 list. */
25266 attribute_list = NULL;
25267
25268 /* Look for the two `)' tokens. */
25269 if (!inner_parens.require_close (parser))
25270 ok = false;
25271 if (!outer_parens.require_close (parser))
25272 ok = false;
25273 if (!ok)
25274 cp_parser_skip_to_end_of_statement (parser);
25275
25276 /* Add these new attributes to the list. */
25277 attributes = attr_chainon (attributes, attribute_list);
25278 }
25279
25280 return attributes;
25281 }
25282
25283 /* Parse a GNU attribute-list.
25284
25285 attribute-list:
25286 attribute
25287 attribute-list , attribute
25288
25289 attribute:
25290 identifier
25291 identifier ( identifier )
25292 identifier ( identifier , expression-list )
25293 identifier ( expression-list )
25294
25295 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25296 to an attribute. The TREE_PURPOSE of each node is the identifier
25297 indicating which attribute is in use. The TREE_VALUE represents
25298 the arguments, if any. */
25299
25300 static tree
25301 cp_parser_gnu_attribute_list (cp_parser* parser)
25302 {
25303 tree attribute_list = NULL_TREE;
25304 bool save_translate_strings_p = parser->translate_strings_p;
25305
25306 parser->translate_strings_p = false;
25307 while (true)
25308 {
25309 cp_token *token;
25310 tree identifier;
25311 tree attribute;
25312
25313 /* Look for the identifier. We also allow keywords here; for
25314 example `__attribute__ ((const))' is legal. */
25315 token = cp_lexer_peek_token (parser->lexer);
25316 if (token->type == CPP_NAME
25317 || token->type == CPP_KEYWORD)
25318 {
25319 tree arguments = NULL_TREE;
25320
25321 /* Consume the token, but save it since we need it for the
25322 SIMD enabled function parsing. */
25323 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25324
25325 /* Save away the identifier that indicates which attribute
25326 this is. */
25327 identifier = (token->type == CPP_KEYWORD)
25328 /* For keywords, use the canonical spelling, not the
25329 parsed identifier. */
25330 ? ridpointers[(int) token->keyword]
25331 : id_token->u.value;
25332
25333 identifier = canonicalize_attr_name (identifier);
25334 attribute = build_tree_list (identifier, NULL_TREE);
25335
25336 /* Peek at the next token. */
25337 token = cp_lexer_peek_token (parser->lexer);
25338 /* If it's an `(', then parse the attribute arguments. */
25339 if (token->type == CPP_OPEN_PAREN)
25340 {
25341 vec<tree, va_gc> *vec;
25342 int attr_flag = (attribute_takes_identifier_p (identifier)
25343 ? id_attr : normal_attr);
25344 vec = cp_parser_parenthesized_expression_list
25345 (parser, attr_flag, /*cast_p=*/false,
25346 /*allow_expansion_p=*/false,
25347 /*non_constant_p=*/NULL);
25348 if (vec == NULL)
25349 arguments = error_mark_node;
25350 else
25351 {
25352 arguments = build_tree_list_vec (vec);
25353 release_tree_vector (vec);
25354 }
25355 /* Save the arguments away. */
25356 TREE_VALUE (attribute) = arguments;
25357 }
25358
25359 if (arguments != error_mark_node)
25360 {
25361 /* Add this attribute to the list. */
25362 TREE_CHAIN (attribute) = attribute_list;
25363 attribute_list = attribute;
25364 }
25365
25366 token = cp_lexer_peek_token (parser->lexer);
25367 }
25368 /* Now, look for more attributes. If the next token isn't a
25369 `,', we're done. */
25370 if (token->type != CPP_COMMA)
25371 break;
25372
25373 /* Consume the comma and keep going. */
25374 cp_lexer_consume_token (parser->lexer);
25375 }
25376 parser->translate_strings_p = save_translate_strings_p;
25377
25378 /* We built up the list in reverse order. */
25379 return nreverse (attribute_list);
25380 }
25381
25382 /* Parse a standard C++11 attribute.
25383
25384 The returned representation is a TREE_LIST which TREE_PURPOSE is
25385 the scoped name of the attribute, and the TREE_VALUE is its
25386 arguments list.
25387
25388 Note that the scoped name of the attribute is itself a TREE_LIST
25389 which TREE_PURPOSE is the namespace of the attribute, and
25390 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25391 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25392 and which TREE_PURPOSE is directly the attribute name.
25393
25394 Clients of the attribute code should use get_attribute_namespace
25395 and get_attribute_name to get the actual namespace and name of
25396 attributes, regardless of their being GNU or C++11 attributes.
25397
25398 attribute:
25399 attribute-token attribute-argument-clause [opt]
25400
25401 attribute-token:
25402 identifier
25403 attribute-scoped-token
25404
25405 attribute-scoped-token:
25406 attribute-namespace :: identifier
25407
25408 attribute-namespace:
25409 identifier
25410
25411 attribute-argument-clause:
25412 ( balanced-token-seq )
25413
25414 balanced-token-seq:
25415 balanced-token [opt]
25416 balanced-token-seq balanced-token
25417
25418 balanced-token:
25419 ( balanced-token-seq )
25420 [ balanced-token-seq ]
25421 { balanced-token-seq }. */
25422
25423 static tree
25424 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25425 {
25426 tree attribute, attr_id = NULL_TREE, arguments;
25427 cp_token *token;
25428
25429 temp_override<bool> cleanup
25430 (parser->auto_is_implicit_function_template_parm_p, false);
25431
25432 /* First, parse name of the attribute, a.k.a attribute-token. */
25433
25434 token = cp_lexer_peek_token (parser->lexer);
25435 if (token->type == CPP_NAME)
25436 attr_id = token->u.value;
25437 else if (token->type == CPP_KEYWORD)
25438 attr_id = ridpointers[(int) token->keyword];
25439 else if (token->flags & NAMED_OP)
25440 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25441
25442 if (attr_id == NULL_TREE)
25443 return NULL_TREE;
25444
25445 cp_lexer_consume_token (parser->lexer);
25446
25447 token = cp_lexer_peek_token (parser->lexer);
25448 if (token->type == CPP_SCOPE)
25449 {
25450 /* We are seeing a scoped attribute token. */
25451
25452 cp_lexer_consume_token (parser->lexer);
25453 if (attr_ns)
25454 error_at (token->location, "attribute using prefix used together "
25455 "with scoped attribute token");
25456 attr_ns = attr_id;
25457
25458 token = cp_lexer_consume_token (parser->lexer);
25459 if (token->type == CPP_NAME)
25460 attr_id = token->u.value;
25461 else if (token->type == CPP_KEYWORD)
25462 attr_id = ridpointers[(int) token->keyword];
25463 else if (token->flags & NAMED_OP)
25464 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25465 else
25466 {
25467 error_at (token->location,
25468 "expected an identifier for the attribute name");
25469 return error_mark_node;
25470 }
25471
25472 attr_ns = canonicalize_attr_name (attr_ns);
25473 attr_id = canonicalize_attr_name (attr_id);
25474 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25475 NULL_TREE);
25476 token = cp_lexer_peek_token (parser->lexer);
25477 }
25478 else if (attr_ns)
25479 {
25480 attr_ns = canonicalize_attr_name (attr_ns);
25481 attr_id = canonicalize_attr_name (attr_id);
25482 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25483 NULL_TREE);
25484 }
25485 else
25486 {
25487 attr_id = canonicalize_attr_name (attr_id);
25488 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25489 NULL_TREE);
25490 /* C++11 noreturn attribute is equivalent to GNU's. */
25491 if (is_attribute_p ("noreturn", attr_id))
25492 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25493 /* C++14 deprecated attribute is equivalent to GNU's. */
25494 else if (is_attribute_p ("deprecated", attr_id))
25495 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25496 /* C++17 fallthrough attribute is equivalent to GNU's. */
25497 else if (is_attribute_p ("fallthrough", attr_id))
25498 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25499 /* Transactional Memory TS optimize_for_synchronized attribute is
25500 equivalent to GNU transaction_callable. */
25501 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25502 TREE_PURPOSE (attribute)
25503 = get_identifier ("transaction_callable");
25504 /* Transactional Memory attributes are GNU attributes. */
25505 else if (tm_attr_to_mask (attr_id))
25506 TREE_PURPOSE (attribute) = attr_id;
25507 }
25508
25509 /* Now parse the optional argument clause of the attribute. */
25510
25511 if (token->type != CPP_OPEN_PAREN)
25512 return attribute;
25513
25514 {
25515 vec<tree, va_gc> *vec;
25516 int attr_flag = normal_attr;
25517
25518 if (attr_ns == gnu_identifier
25519 && attribute_takes_identifier_p (attr_id))
25520 /* A GNU attribute that takes an identifier in parameter. */
25521 attr_flag = id_attr;
25522
25523 vec = cp_parser_parenthesized_expression_list
25524 (parser, attr_flag, /*cast_p=*/false,
25525 /*allow_expansion_p=*/true,
25526 /*non_constant_p=*/NULL);
25527 if (vec == NULL)
25528 arguments = error_mark_node;
25529 else
25530 {
25531 arguments = build_tree_list_vec (vec);
25532 release_tree_vector (vec);
25533 }
25534
25535 if (arguments == error_mark_node)
25536 attribute = error_mark_node;
25537 else
25538 TREE_VALUE (attribute) = arguments;
25539 }
25540
25541 return attribute;
25542 }
25543
25544 /* Check that the attribute ATTRIBUTE appears at most once in the
25545 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25546 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25547 isn't implemented yet in GCC. */
25548
25549 static void
25550 cp_parser_check_std_attribute (tree attributes, tree attribute)
25551 {
25552 if (attributes)
25553 {
25554 tree name = get_attribute_name (attribute);
25555 if (is_attribute_p ("noreturn", name)
25556 && lookup_attribute ("noreturn", attributes))
25557 error ("attribute %<noreturn%> can appear at most once "
25558 "in an attribute-list");
25559 else if (is_attribute_p ("deprecated", name)
25560 && lookup_attribute ("deprecated", attributes))
25561 error ("attribute %<deprecated%> can appear at most once "
25562 "in an attribute-list");
25563 }
25564 }
25565
25566 /* Parse a list of standard C++-11 attributes.
25567
25568 attribute-list:
25569 attribute [opt]
25570 attribute-list , attribute[opt]
25571 attribute ...
25572 attribute-list , attribute ...
25573 */
25574
25575 static tree
25576 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25577 {
25578 tree attributes = NULL_TREE, attribute = NULL_TREE;
25579 cp_token *token = NULL;
25580
25581 while (true)
25582 {
25583 attribute = cp_parser_std_attribute (parser, attr_ns);
25584 if (attribute == error_mark_node)
25585 break;
25586 if (attribute != NULL_TREE)
25587 {
25588 cp_parser_check_std_attribute (attributes, attribute);
25589 TREE_CHAIN (attribute) = attributes;
25590 attributes = attribute;
25591 }
25592 token = cp_lexer_peek_token (parser->lexer);
25593 if (token->type == CPP_ELLIPSIS)
25594 {
25595 cp_lexer_consume_token (parser->lexer);
25596 if (attribute == NULL_TREE)
25597 error_at (token->location,
25598 "expected attribute before %<...%>");
25599 else
25600 {
25601 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25602 if (pack == error_mark_node)
25603 return error_mark_node;
25604 TREE_VALUE (attribute) = pack;
25605 }
25606 token = cp_lexer_peek_token (parser->lexer);
25607 }
25608 if (token->type != CPP_COMMA)
25609 break;
25610 cp_lexer_consume_token (parser->lexer);
25611 }
25612 attributes = nreverse (attributes);
25613 return attributes;
25614 }
25615
25616 /* Parse a standard C++-11 attribute specifier.
25617
25618 attribute-specifier:
25619 [ [ attribute-using-prefix [opt] attribute-list ] ]
25620 alignment-specifier
25621
25622 attribute-using-prefix:
25623 using attribute-namespace :
25624
25625 alignment-specifier:
25626 alignas ( type-id ... [opt] )
25627 alignas ( alignment-expression ... [opt] ). */
25628
25629 static tree
25630 cp_parser_std_attribute_spec (cp_parser *parser)
25631 {
25632 tree attributes = NULL_TREE;
25633 cp_token *token = cp_lexer_peek_token (parser->lexer);
25634
25635 if (token->type == CPP_OPEN_SQUARE
25636 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25637 {
25638 tree attr_ns = NULL_TREE;
25639
25640 cp_lexer_consume_token (parser->lexer);
25641 cp_lexer_consume_token (parser->lexer);
25642
25643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25644 {
25645 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25646 if (token->type == CPP_NAME)
25647 attr_ns = token->u.value;
25648 else if (token->type == CPP_KEYWORD)
25649 attr_ns = ridpointers[(int) token->keyword];
25650 else if (token->flags & NAMED_OP)
25651 attr_ns = get_identifier (cpp_type2name (token->type,
25652 token->flags));
25653 if (attr_ns
25654 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25655 {
25656 if (cxx_dialect < cxx17
25657 && !in_system_header_at (input_location))
25658 pedwarn (input_location, 0,
25659 "attribute using prefix only available "
25660 "with -std=c++17 or -std=gnu++17");
25661
25662 cp_lexer_consume_token (parser->lexer);
25663 cp_lexer_consume_token (parser->lexer);
25664 cp_lexer_consume_token (parser->lexer);
25665 }
25666 else
25667 attr_ns = NULL_TREE;
25668 }
25669
25670 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25671
25672 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25673 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25674 cp_parser_skip_to_end_of_statement (parser);
25675 else
25676 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25677 when we are sure that we have actually parsed them. */
25678 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25679 }
25680 else
25681 {
25682 tree alignas_expr;
25683
25684 /* Look for an alignment-specifier. */
25685
25686 token = cp_lexer_peek_token (parser->lexer);
25687
25688 if (token->type != CPP_KEYWORD
25689 || token->keyword != RID_ALIGNAS)
25690 return NULL_TREE;
25691
25692 cp_lexer_consume_token (parser->lexer);
25693 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25694
25695 matching_parens parens;
25696 if (!parens.require_open (parser))
25697 return error_mark_node;
25698
25699 cp_parser_parse_tentatively (parser);
25700 alignas_expr = cp_parser_type_id (parser);
25701
25702 if (!cp_parser_parse_definitely (parser))
25703 {
25704 alignas_expr = cp_parser_assignment_expression (parser);
25705 if (alignas_expr == error_mark_node)
25706 cp_parser_skip_to_end_of_statement (parser);
25707 if (alignas_expr == NULL_TREE
25708 || alignas_expr == error_mark_node)
25709 return alignas_expr;
25710 }
25711
25712 alignas_expr = cxx_alignas_expr (alignas_expr);
25713 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25714
25715 /* Handle alignas (pack...). */
25716 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25717 {
25718 cp_lexer_consume_token (parser->lexer);
25719 alignas_expr = make_pack_expansion (alignas_expr);
25720 }
25721
25722 /* Something went wrong, so don't build the attribute. */
25723 if (alignas_expr == error_mark_node)
25724 return error_mark_node;
25725
25726 if (!parens.require_close (parser))
25727 return error_mark_node;
25728
25729 /* Build the C++-11 representation of an 'aligned'
25730 attribute. */
25731 attributes
25732 = build_tree_list (build_tree_list (gnu_identifier,
25733 aligned_identifier), alignas_expr);
25734 }
25735
25736 return attributes;
25737 }
25738
25739 /* Parse a standard C++-11 attribute-specifier-seq.
25740
25741 attribute-specifier-seq:
25742 attribute-specifier-seq [opt] attribute-specifier
25743 */
25744
25745 static tree
25746 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25747 {
25748 tree attr_specs = NULL_TREE;
25749 tree attr_last = NULL_TREE;
25750
25751 while (true)
25752 {
25753 tree attr_spec = cp_parser_std_attribute_spec (parser);
25754 if (attr_spec == NULL_TREE)
25755 break;
25756 if (attr_spec == error_mark_node)
25757 return error_mark_node;
25758
25759 if (attr_last)
25760 TREE_CHAIN (attr_last) = attr_spec;
25761 else
25762 attr_specs = attr_last = attr_spec;
25763 attr_last = tree_last (attr_last);
25764 }
25765
25766 return attr_specs;
25767 }
25768
25769 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25770 return index of the first token after balanced-token, or N on failure. */
25771
25772 static size_t
25773 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25774 {
25775 size_t orig_n = n;
25776 int nparens = 0, nbraces = 0, nsquares = 0;
25777 do
25778 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25779 {
25780 case CPP_EOF:
25781 case CPP_PRAGMA_EOL:
25782 /* Ran out of tokens. */
25783 return orig_n;
25784 case CPP_OPEN_PAREN:
25785 ++nparens;
25786 break;
25787 case CPP_OPEN_BRACE:
25788 ++nbraces;
25789 break;
25790 case CPP_OPEN_SQUARE:
25791 ++nsquares;
25792 break;
25793 case CPP_CLOSE_PAREN:
25794 --nparens;
25795 break;
25796 case CPP_CLOSE_BRACE:
25797 --nbraces;
25798 break;
25799 case CPP_CLOSE_SQUARE:
25800 --nsquares;
25801 break;
25802 default:
25803 break;
25804 }
25805 while (nparens || nbraces || nsquares);
25806 return n;
25807 }
25808
25809 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25810 return index of the first token after the GNU attribute tokens, or N on
25811 failure. */
25812
25813 static size_t
25814 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25815 {
25816 while (true)
25817 {
25818 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25819 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25820 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25821 break;
25822
25823 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25824 if (n2 == n + 2)
25825 break;
25826 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25827 break;
25828 n = n2 + 1;
25829 }
25830 return n;
25831 }
25832
25833 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25834 next token), return index of the first token after the standard C++11
25835 attribute tokens, or N on failure. */
25836
25837 static size_t
25838 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25839 {
25840 while (true)
25841 {
25842 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25843 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25844 {
25845 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25846 if (n2 == n + 1)
25847 break;
25848 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25849 break;
25850 n = n2 + 1;
25851 }
25852 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25853 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25854 {
25855 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25856 if (n2 == n + 1)
25857 break;
25858 n = n2;
25859 }
25860 else
25861 break;
25862 }
25863 return n;
25864 }
25865
25866 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25867 as the next token), return index of the first token after the attribute
25868 tokens, or N on failure. */
25869
25870 static size_t
25871 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25872 {
25873 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25874 return cp_parser_skip_gnu_attributes_opt (parser, n);
25875 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25876 }
25877
25878 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25879 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25880 current value of the PEDANTIC flag, regardless of whether or not
25881 the `__extension__' keyword is present. The caller is responsible
25882 for restoring the value of the PEDANTIC flag. */
25883
25884 static bool
25885 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25886 {
25887 /* Save the old value of the PEDANTIC flag. */
25888 *saved_pedantic = pedantic;
25889
25890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25891 {
25892 /* Consume the `__extension__' token. */
25893 cp_lexer_consume_token (parser->lexer);
25894 /* We're not being pedantic while the `__extension__' keyword is
25895 in effect. */
25896 pedantic = 0;
25897
25898 return true;
25899 }
25900
25901 return false;
25902 }
25903
25904 /* Parse a label declaration.
25905
25906 label-declaration:
25907 __label__ label-declarator-seq ;
25908
25909 label-declarator-seq:
25910 identifier , label-declarator-seq
25911 identifier */
25912
25913 static void
25914 cp_parser_label_declaration (cp_parser* parser)
25915 {
25916 /* Look for the `__label__' keyword. */
25917 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25918
25919 while (true)
25920 {
25921 tree identifier;
25922
25923 /* Look for an identifier. */
25924 identifier = cp_parser_identifier (parser);
25925 /* If we failed, stop. */
25926 if (identifier == error_mark_node)
25927 break;
25928 /* Declare it as a label. */
25929 finish_label_decl (identifier);
25930 /* If the next token is a `;', stop. */
25931 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25932 break;
25933 /* Look for the `,' separating the label declarations. */
25934 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25935 }
25936
25937 /* Look for the final `;'. */
25938 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25939 }
25940
25941 // -------------------------------------------------------------------------- //
25942 // Requires Clause
25943
25944 // Parse a requires clause.
25945 //
25946 // requires-clause:
25947 // 'requires' logical-or-expression
25948 //
25949 // The required logical-or-expression must be a constant expression. Note
25950 // that we don't check that the expression is constepxr here. We defer until
25951 // we analyze constraints and then, we only check atomic constraints.
25952 static tree
25953 cp_parser_requires_clause (cp_parser *parser)
25954 {
25955 // Parse the requires clause so that it is not automatically folded.
25956 ++processing_template_decl;
25957 tree expr = cp_parser_binary_expression (parser, false, false,
25958 PREC_NOT_OPERATOR, NULL);
25959 if (check_for_bare_parameter_packs (expr))
25960 expr = error_mark_node;
25961 --processing_template_decl;
25962 return expr;
25963 }
25964
25965 // Optionally parse a requires clause:
25966 static tree
25967 cp_parser_requires_clause_opt (cp_parser *parser)
25968 {
25969 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25970 if (tok->keyword != RID_REQUIRES)
25971 {
25972 if (!flag_concepts && tok->type == CPP_NAME
25973 && tok->u.value == ridpointers[RID_REQUIRES])
25974 {
25975 error_at (cp_lexer_peek_token (parser->lexer)->location,
25976 "%<requires%> only available with -fconcepts");
25977 /* Parse and discard the requires-clause. */
25978 cp_lexer_consume_token (parser->lexer);
25979 cp_parser_requires_clause (parser);
25980 }
25981 return NULL_TREE;
25982 }
25983 cp_lexer_consume_token (parser->lexer);
25984 return cp_parser_requires_clause (parser);
25985 }
25986
25987
25988 /*---------------------------------------------------------------------------
25989 Requires expressions
25990 ---------------------------------------------------------------------------*/
25991
25992 /* Parse a requires expression
25993
25994 requirement-expression:
25995 'requires' requirement-parameter-list [opt] requirement-body */
25996 static tree
25997 cp_parser_requires_expression (cp_parser *parser)
25998 {
25999 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26000 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26001
26002 /* A requires-expression shall appear only within a concept
26003 definition or a requires-clause.
26004
26005 TODO: Implement this diagnostic correctly. */
26006 if (!processing_template_decl)
26007 {
26008 error_at (loc, "a requires expression cannot appear outside a template");
26009 cp_parser_skip_to_end_of_statement (parser);
26010 return error_mark_node;
26011 }
26012
26013 tree parms, reqs;
26014 {
26015 /* Local parameters are delared as variables within the scope
26016 of the expression. They are not visible past the end of
26017 the expression. Expressions within the requires-expression
26018 are unevaluated. */
26019 struct scope_sentinel
26020 {
26021 scope_sentinel ()
26022 {
26023 ++cp_unevaluated_operand;
26024 begin_scope (sk_block, NULL_TREE);
26025 }
26026
26027 ~scope_sentinel ()
26028 {
26029 pop_bindings_and_leave_scope ();
26030 --cp_unevaluated_operand;
26031 }
26032 } s;
26033
26034 /* Parse the optional parameter list. */
26035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26036 {
26037 parms = cp_parser_requirement_parameter_list (parser);
26038 if (parms == error_mark_node)
26039 return error_mark_node;
26040 }
26041 else
26042 parms = NULL_TREE;
26043
26044 /* Parse the requirement body. */
26045 reqs = cp_parser_requirement_body (parser);
26046 if (reqs == error_mark_node)
26047 return error_mark_node;
26048 }
26049
26050 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26051 the parm chain. */
26052 grokparms (parms, &parms);
26053 return finish_requires_expr (parms, reqs);
26054 }
26055
26056 /* Parse a parameterized requirement.
26057
26058 requirement-parameter-list:
26059 '(' parameter-declaration-clause ')' */
26060 static tree
26061 cp_parser_requirement_parameter_list (cp_parser *parser)
26062 {
26063 matching_parens parens;
26064 if (!parens.require_open (parser))
26065 return error_mark_node;
26066
26067 tree parms = cp_parser_parameter_declaration_clause (parser);
26068
26069 if (!parens.require_close (parser))
26070 return error_mark_node;
26071
26072 return parms;
26073 }
26074
26075 /* Parse the body of a requirement.
26076
26077 requirement-body:
26078 '{' requirement-list '}' */
26079 static tree
26080 cp_parser_requirement_body (cp_parser *parser)
26081 {
26082 matching_braces braces;
26083 if (!braces.require_open (parser))
26084 return error_mark_node;
26085
26086 tree reqs = cp_parser_requirement_list (parser);
26087
26088 if (!braces.require_close (parser))
26089 return error_mark_node;
26090
26091 return reqs;
26092 }
26093
26094 /* Parse a list of requirements.
26095
26096 requirement-list:
26097 requirement
26098 requirement-list ';' requirement[opt] */
26099 static tree
26100 cp_parser_requirement_list (cp_parser *parser)
26101 {
26102 tree result = NULL_TREE;
26103 while (true)
26104 {
26105 tree req = cp_parser_requirement (parser);
26106 if (req == error_mark_node)
26107 return error_mark_node;
26108
26109 result = tree_cons (NULL_TREE, req, result);
26110
26111 /* If we see a semi-colon, consume it. */
26112 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26113 cp_lexer_consume_token (parser->lexer);
26114
26115 /* Stop processing at the end of the list. */
26116 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26117 break;
26118 }
26119
26120 /* Reverse the order of requirements so they are analyzed in
26121 declaration order. */
26122 return nreverse (result);
26123 }
26124
26125 /* Parse a syntactic requirement or type requirement.
26126
26127 requirement:
26128 simple-requirement
26129 compound-requirement
26130 type-requirement
26131 nested-requirement */
26132 static tree
26133 cp_parser_requirement (cp_parser *parser)
26134 {
26135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26136 return cp_parser_compound_requirement (parser);
26137 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26138 return cp_parser_type_requirement (parser);
26139 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26140 return cp_parser_nested_requirement (parser);
26141 else
26142 return cp_parser_simple_requirement (parser);
26143 }
26144
26145 /* Parse a simple requirement.
26146
26147 simple-requirement:
26148 expression ';' */
26149 static tree
26150 cp_parser_simple_requirement (cp_parser *parser)
26151 {
26152 tree expr = cp_parser_expression (parser, NULL, false, false);
26153 if (!expr || expr == error_mark_node)
26154 return error_mark_node;
26155
26156 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26157 return error_mark_node;
26158
26159 return finish_simple_requirement (expr);
26160 }
26161
26162 /* Parse a type requirement
26163
26164 type-requirement
26165 nested-name-specifier [opt] required-type-name ';'
26166
26167 required-type-name:
26168 type-name
26169 'template' [opt] simple-template-id */
26170 static tree
26171 cp_parser_type_requirement (cp_parser *parser)
26172 {
26173 cp_lexer_consume_token (parser->lexer);
26174
26175 // Save the scope before parsing name specifiers.
26176 tree saved_scope = parser->scope;
26177 tree saved_object_scope = parser->object_scope;
26178 tree saved_qualifying_scope = parser->qualifying_scope;
26179 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26180 cp_parser_nested_name_specifier_opt (parser,
26181 /*typename_keyword_p=*/true,
26182 /*check_dependency_p=*/false,
26183 /*type_p=*/true,
26184 /*is_declaration=*/false);
26185
26186 tree type;
26187 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26188 {
26189 cp_lexer_consume_token (parser->lexer);
26190 type = cp_parser_template_id (parser,
26191 /*template_keyword_p=*/true,
26192 /*check_dependency=*/false,
26193 /*tag_type=*/none_type,
26194 /*is_declaration=*/false);
26195 type = make_typename_type (parser->scope, type, typename_type,
26196 /*complain=*/tf_error);
26197 }
26198 else
26199 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26200
26201 if (TREE_CODE (type) == TYPE_DECL)
26202 type = TREE_TYPE (type);
26203
26204 parser->scope = saved_scope;
26205 parser->object_scope = saved_object_scope;
26206 parser->qualifying_scope = saved_qualifying_scope;
26207
26208 if (type == error_mark_node)
26209 cp_parser_skip_to_end_of_statement (parser);
26210
26211 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26212 return error_mark_node;
26213 if (type == error_mark_node)
26214 return error_mark_node;
26215
26216 return finish_type_requirement (type);
26217 }
26218
26219 /* Parse a compound requirement
26220
26221 compound-requirement:
26222 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26223 static tree
26224 cp_parser_compound_requirement (cp_parser *parser)
26225 {
26226 /* Parse an expression enclosed in '{ }'s. */
26227 matching_braces braces;
26228 if (!braces.require_open (parser))
26229 return error_mark_node;
26230
26231 tree expr = cp_parser_expression (parser, NULL, false, false);
26232 if (!expr || expr == error_mark_node)
26233 return error_mark_node;
26234
26235 if (!braces.require_close (parser))
26236 return error_mark_node;
26237
26238 /* Parse the optional noexcept. */
26239 bool noexcept_p = false;
26240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26241 {
26242 cp_lexer_consume_token (parser->lexer);
26243 noexcept_p = true;
26244 }
26245
26246 /* Parse the optional trailing return type. */
26247 tree type = NULL_TREE;
26248 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26249 {
26250 cp_lexer_consume_token (parser->lexer);
26251 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26252 parser->in_result_type_constraint_p = true;
26253 type = cp_parser_trailing_type_id (parser);
26254 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26255 if (type == error_mark_node)
26256 return error_mark_node;
26257 }
26258
26259 return finish_compound_requirement (expr, type, noexcept_p);
26260 }
26261
26262 /* Parse a nested requirement. This is the same as a requires clause.
26263
26264 nested-requirement:
26265 requires-clause */
26266 static tree
26267 cp_parser_nested_requirement (cp_parser *parser)
26268 {
26269 cp_lexer_consume_token (parser->lexer);
26270 tree req = cp_parser_requires_clause (parser);
26271 if (req == error_mark_node)
26272 return error_mark_node;
26273 return finish_nested_requirement (req);
26274 }
26275
26276 /* Support Functions */
26277
26278 /* Return the appropriate prefer_type argument for lookup_name_real based on
26279 tag_type and template_mem_access. */
26280
26281 static inline int
26282 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26283 {
26284 /* DR 141: When looking in the current enclosing context for a template-name
26285 after -> or ., only consider class templates. */
26286 if (template_mem_access)
26287 return 2;
26288 switch (tag_type)
26289 {
26290 case none_type: return 0; // No preference.
26291 case scope_type: return 1; // Type or namespace.
26292 default: return 2; // Type only.
26293 }
26294 }
26295
26296 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26297 NAME should have one of the representations used for an
26298 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26299 is returned. If PARSER->SCOPE is a dependent type, then a
26300 SCOPE_REF is returned.
26301
26302 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26303 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26304 was formed. Abstractly, such entities should not be passed to this
26305 function, because they do not need to be looked up, but it is
26306 simpler to check for this special case here, rather than at the
26307 call-sites.
26308
26309 In cases not explicitly covered above, this function returns a
26310 DECL, OVERLOAD, or baselink representing the result of the lookup.
26311 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26312 is returned.
26313
26314 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26315 (e.g., "struct") that was used. In that case bindings that do not
26316 refer to types are ignored.
26317
26318 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26319 ignored.
26320
26321 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26322 are ignored.
26323
26324 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26325 types.
26326
26327 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26328 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26329 NULL_TREE otherwise. */
26330
26331 static cp_expr
26332 cp_parser_lookup_name (cp_parser *parser, tree name,
26333 enum tag_types tag_type,
26334 bool is_template,
26335 bool is_namespace,
26336 bool check_dependency,
26337 tree *ambiguous_decls,
26338 location_t name_location)
26339 {
26340 tree decl;
26341 tree object_type = parser->context->object_type;
26342
26343 /* Assume that the lookup will be unambiguous. */
26344 if (ambiguous_decls)
26345 *ambiguous_decls = NULL_TREE;
26346
26347 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26348 no longer valid. Note that if we are parsing tentatively, and
26349 the parse fails, OBJECT_TYPE will be automatically restored. */
26350 parser->context->object_type = NULL_TREE;
26351
26352 if (name == error_mark_node)
26353 return error_mark_node;
26354
26355 /* A template-id has already been resolved; there is no lookup to
26356 do. */
26357 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26358 return name;
26359 if (BASELINK_P (name))
26360 {
26361 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26362 == TEMPLATE_ID_EXPR);
26363 return name;
26364 }
26365
26366 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26367 it should already have been checked to make sure that the name
26368 used matches the type being destroyed. */
26369 if (TREE_CODE (name) == BIT_NOT_EXPR)
26370 {
26371 tree type;
26372
26373 /* Figure out to which type this destructor applies. */
26374 if (parser->scope)
26375 type = parser->scope;
26376 else if (object_type)
26377 type = object_type;
26378 else
26379 type = current_class_type;
26380 /* If that's not a class type, there is no destructor. */
26381 if (!type || !CLASS_TYPE_P (type))
26382 return error_mark_node;
26383
26384 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26385 lazily_declare_fn (sfk_destructor, type);
26386
26387 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26388 return dtor;
26389
26390 return error_mark_node;
26391 }
26392
26393 /* By this point, the NAME should be an ordinary identifier. If
26394 the id-expression was a qualified name, the qualifying scope is
26395 stored in PARSER->SCOPE at this point. */
26396 gcc_assert (identifier_p (name));
26397
26398 /* Perform the lookup. */
26399 if (parser->scope)
26400 {
26401 bool dependent_p;
26402
26403 if (parser->scope == error_mark_node)
26404 return error_mark_node;
26405
26406 /* If the SCOPE is dependent, the lookup must be deferred until
26407 the template is instantiated -- unless we are explicitly
26408 looking up names in uninstantiated templates. Even then, we
26409 cannot look up the name if the scope is not a class type; it
26410 might, for example, be a template type parameter. */
26411 dependent_p = (TYPE_P (parser->scope)
26412 && dependent_scope_p (parser->scope));
26413 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26414 && dependent_p)
26415 /* Defer lookup. */
26416 decl = error_mark_node;
26417 else
26418 {
26419 tree pushed_scope = NULL_TREE;
26420
26421 /* If PARSER->SCOPE is a dependent type, then it must be a
26422 class type, and we must not be checking dependencies;
26423 otherwise, we would have processed this lookup above. So
26424 that PARSER->SCOPE is not considered a dependent base by
26425 lookup_member, we must enter the scope here. */
26426 if (dependent_p)
26427 pushed_scope = push_scope (parser->scope);
26428
26429 /* If the PARSER->SCOPE is a template specialization, it
26430 may be instantiated during name lookup. In that case,
26431 errors may be issued. Even if we rollback the current
26432 tentative parse, those errors are valid. */
26433 decl = lookup_qualified_name (parser->scope, name,
26434 prefer_type_arg (tag_type),
26435 /*complain=*/true);
26436
26437 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26438 lookup result and the nested-name-specifier nominates a class C:
26439 * if the name specified after the nested-name-specifier, when
26440 looked up in C, is the injected-class-name of C (Clause 9), or
26441 * if the name specified after the nested-name-specifier is the
26442 same as the identifier or the simple-template-id's template-
26443 name in the last component of the nested-name-specifier,
26444 the name is instead considered to name the constructor of
26445 class C. [ Note: for example, the constructor is not an
26446 acceptable lookup result in an elaborated-type-specifier so
26447 the constructor would not be used in place of the
26448 injected-class-name. --end note ] Such a constructor name
26449 shall be used only in the declarator-id of a declaration that
26450 names a constructor or in a using-declaration. */
26451 if (tag_type == none_type
26452 && DECL_SELF_REFERENCE_P (decl)
26453 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26454 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26455 prefer_type_arg (tag_type),
26456 /*complain=*/true);
26457
26458 /* If we have a single function from a using decl, pull it out. */
26459 if (TREE_CODE (decl) == OVERLOAD
26460 && !really_overloaded_fn (decl))
26461 decl = OVL_FUNCTION (decl);
26462
26463 if (pushed_scope)
26464 pop_scope (pushed_scope);
26465 }
26466
26467 /* If the scope is a dependent type and either we deferred lookup or
26468 we did lookup but didn't find the name, rememeber the name. */
26469 if (decl == error_mark_node && TYPE_P (parser->scope)
26470 && dependent_type_p (parser->scope))
26471 {
26472 if (tag_type)
26473 {
26474 tree type;
26475
26476 /* The resolution to Core Issue 180 says that `struct
26477 A::B' should be considered a type-name, even if `A'
26478 is dependent. */
26479 type = make_typename_type (parser->scope, name, tag_type,
26480 /*complain=*/tf_error);
26481 if (type != error_mark_node)
26482 decl = TYPE_NAME (type);
26483 }
26484 else if (is_template
26485 && (cp_parser_next_token_ends_template_argument_p (parser)
26486 || cp_lexer_next_token_is (parser->lexer,
26487 CPP_CLOSE_PAREN)))
26488 decl = make_unbound_class_template (parser->scope,
26489 name, NULL_TREE,
26490 /*complain=*/tf_error);
26491 else
26492 decl = build_qualified_name (/*type=*/NULL_TREE,
26493 parser->scope, name,
26494 is_template);
26495 }
26496 parser->qualifying_scope = parser->scope;
26497 parser->object_scope = NULL_TREE;
26498 }
26499 else if (object_type)
26500 {
26501 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26502 OBJECT_TYPE is not a class. */
26503 if (CLASS_TYPE_P (object_type))
26504 /* If the OBJECT_TYPE is a template specialization, it may
26505 be instantiated during name lookup. In that case, errors
26506 may be issued. Even if we rollback the current tentative
26507 parse, those errors are valid. */
26508 decl = lookup_member (object_type,
26509 name,
26510 /*protect=*/0,
26511 prefer_type_arg (tag_type),
26512 tf_warning_or_error);
26513 else
26514 decl = NULL_TREE;
26515
26516 if (!decl)
26517 /* Look it up in the enclosing context. DR 141: When looking for a
26518 template-name after -> or ., only consider class templates. */
26519 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26520 /*nonclass=*/0,
26521 /*block_p=*/true, is_namespace, 0);
26522 if (object_type == unknown_type_node)
26523 /* The object is type-dependent, so we can't look anything up; we used
26524 this to get the DR 141 behavior. */
26525 object_type = NULL_TREE;
26526 parser->object_scope = object_type;
26527 parser->qualifying_scope = NULL_TREE;
26528 }
26529 else
26530 {
26531 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26532 /*nonclass=*/0,
26533 /*block_p=*/true, is_namespace, 0);
26534 parser->qualifying_scope = NULL_TREE;
26535 parser->object_scope = NULL_TREE;
26536 }
26537
26538 /* If the lookup failed, let our caller know. */
26539 if (!decl || decl == error_mark_node)
26540 return error_mark_node;
26541
26542 /* Pull out the template from an injected-class-name (or multiple). */
26543 if (is_template)
26544 decl = maybe_get_template_decl_from_type_decl (decl);
26545
26546 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26547 if (TREE_CODE (decl) == TREE_LIST)
26548 {
26549 if (ambiguous_decls)
26550 *ambiguous_decls = decl;
26551 /* The error message we have to print is too complicated for
26552 cp_parser_error, so we incorporate its actions directly. */
26553 if (!cp_parser_simulate_error (parser))
26554 {
26555 error_at (name_location, "reference to %qD is ambiguous",
26556 name);
26557 print_candidates (decl);
26558 }
26559 return error_mark_node;
26560 }
26561
26562 gcc_assert (DECL_P (decl)
26563 || TREE_CODE (decl) == OVERLOAD
26564 || TREE_CODE (decl) == SCOPE_REF
26565 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26566 || BASELINK_P (decl));
26567
26568 /* If we have resolved the name of a member declaration, check to
26569 see if the declaration is accessible. When the name resolves to
26570 set of overloaded functions, accessibility is checked when
26571 overload resolution is done.
26572
26573 During an explicit instantiation, access is not checked at all,
26574 as per [temp.explicit]. */
26575 if (DECL_P (decl))
26576 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26577
26578 maybe_record_typedef_use (decl);
26579
26580 return cp_expr (decl, name_location);
26581 }
26582
26583 /* Like cp_parser_lookup_name, but for use in the typical case where
26584 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26585 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26586
26587 static tree
26588 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26589 {
26590 return cp_parser_lookup_name (parser, name,
26591 none_type,
26592 /*is_template=*/false,
26593 /*is_namespace=*/false,
26594 /*check_dependency=*/true,
26595 /*ambiguous_decls=*/NULL,
26596 location);
26597 }
26598
26599 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26600 the current context, return the TYPE_DECL. If TAG_NAME_P is
26601 true, the DECL indicates the class being defined in a class-head,
26602 or declared in an elaborated-type-specifier.
26603
26604 Otherwise, return DECL. */
26605
26606 static tree
26607 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26608 {
26609 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26610 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26611
26612 struct A {
26613 template <typename T> struct B;
26614 };
26615
26616 template <typename T> struct A::B {};
26617
26618 Similarly, in an elaborated-type-specifier:
26619
26620 namespace N { struct X{}; }
26621
26622 struct A {
26623 template <typename T> friend struct N::X;
26624 };
26625
26626 However, if the DECL refers to a class type, and we are in
26627 the scope of the class, then the name lookup automatically
26628 finds the TYPE_DECL created by build_self_reference rather
26629 than a TEMPLATE_DECL. For example, in:
26630
26631 template <class T> struct S {
26632 S s;
26633 };
26634
26635 there is no need to handle such case. */
26636
26637 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26638 return DECL_TEMPLATE_RESULT (decl);
26639
26640 return decl;
26641 }
26642
26643 /* If too many, or too few, template-parameter lists apply to the
26644 declarator, issue an error message. Returns TRUE if all went well,
26645 and FALSE otherwise. */
26646
26647 static bool
26648 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26649 cp_declarator *declarator,
26650 location_t declarator_location)
26651 {
26652 switch (declarator->kind)
26653 {
26654 case cdk_id:
26655 {
26656 unsigned num_templates = 0;
26657 tree scope = declarator->u.id.qualifying_scope;
26658 bool template_id_p = false;
26659
26660 if (scope)
26661 num_templates = num_template_headers_for_class (scope);
26662 else if (TREE_CODE (declarator->u.id.unqualified_name)
26663 == TEMPLATE_ID_EXPR)
26664 {
26665 /* If the DECLARATOR has the form `X<y>' then it uses one
26666 additional level of template parameters. */
26667 ++num_templates;
26668 template_id_p = true;
26669 }
26670
26671 return cp_parser_check_template_parameters
26672 (parser, num_templates, template_id_p, declarator_location,
26673 declarator);
26674 }
26675
26676 case cdk_function:
26677 case cdk_array:
26678 case cdk_pointer:
26679 case cdk_reference:
26680 case cdk_ptrmem:
26681 return (cp_parser_check_declarator_template_parameters
26682 (parser, declarator->declarator, declarator_location));
26683
26684 case cdk_decomp:
26685 case cdk_error:
26686 return true;
26687
26688 default:
26689 gcc_unreachable ();
26690 }
26691 return false;
26692 }
26693
26694 /* NUM_TEMPLATES were used in the current declaration. If that is
26695 invalid, return FALSE and issue an error messages. Otherwise,
26696 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26697 declarator and we can print more accurate diagnostics. */
26698
26699 static bool
26700 cp_parser_check_template_parameters (cp_parser* parser,
26701 unsigned num_templates,
26702 bool template_id_p,
26703 location_t location,
26704 cp_declarator *declarator)
26705 {
26706 /* If there are the same number of template classes and parameter
26707 lists, that's OK. */
26708 if (parser->num_template_parameter_lists == num_templates)
26709 return true;
26710 /* If there are more, but only one more, and the name ends in an identifier,
26711 then we are declaring a primary template. That's OK too. */
26712 if (!template_id_p
26713 && parser->num_template_parameter_lists == num_templates + 1)
26714 return true;
26715 /* If there are more template classes than parameter lists, we have
26716 something like:
26717
26718 template <class T> void S<T>::R<T>::f (); */
26719 if (parser->num_template_parameter_lists < num_templates)
26720 {
26721 if (declarator && !current_function_decl)
26722 error_at (location, "specializing member %<%T::%E%> "
26723 "requires %<template<>%> syntax",
26724 declarator->u.id.qualifying_scope,
26725 declarator->u.id.unqualified_name);
26726 else if (declarator)
26727 error_at (location, "invalid declaration of %<%T::%E%>",
26728 declarator->u.id.qualifying_scope,
26729 declarator->u.id.unqualified_name);
26730 else
26731 error_at (location, "too few template-parameter-lists");
26732 return false;
26733 }
26734 /* Otherwise, there are too many template parameter lists. We have
26735 something like:
26736
26737 template <class T> template <class U> void S::f(); */
26738 error_at (location, "too many template-parameter-lists");
26739 return false;
26740 }
26741
26742 /* Parse an optional `::' token indicating that the following name is
26743 from the global namespace. If so, PARSER->SCOPE is set to the
26744 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26745 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26746 Returns the new value of PARSER->SCOPE, if the `::' token is
26747 present, and NULL_TREE otherwise. */
26748
26749 static tree
26750 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26751 {
26752 cp_token *token;
26753
26754 /* Peek at the next token. */
26755 token = cp_lexer_peek_token (parser->lexer);
26756 /* If we're looking at a `::' token then we're starting from the
26757 global namespace, not our current location. */
26758 if (token->type == CPP_SCOPE)
26759 {
26760 /* Consume the `::' token. */
26761 cp_lexer_consume_token (parser->lexer);
26762 /* Set the SCOPE so that we know where to start the lookup. */
26763 parser->scope = global_namespace;
26764 parser->qualifying_scope = global_namespace;
26765 parser->object_scope = NULL_TREE;
26766
26767 return parser->scope;
26768 }
26769 else if (!current_scope_valid_p)
26770 {
26771 parser->scope = NULL_TREE;
26772 parser->qualifying_scope = NULL_TREE;
26773 parser->object_scope = NULL_TREE;
26774 }
26775
26776 return NULL_TREE;
26777 }
26778
26779 /* Returns TRUE if the upcoming token sequence is the start of a
26780 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26781 declarator is preceded by the `friend' specifier. */
26782
26783 static bool
26784 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26785 {
26786 bool constructor_p;
26787 bool outside_class_specifier_p;
26788 tree nested_name_specifier;
26789 cp_token *next_token;
26790
26791 /* The common case is that this is not a constructor declarator, so
26792 try to avoid doing lots of work if at all possible. It's not
26793 valid declare a constructor at function scope. */
26794 if (parser->in_function_body)
26795 return false;
26796 /* And only certain tokens can begin a constructor declarator. */
26797 next_token = cp_lexer_peek_token (parser->lexer);
26798 if (next_token->type != CPP_NAME
26799 && next_token->type != CPP_SCOPE
26800 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26801 && next_token->type != CPP_TEMPLATE_ID)
26802 return false;
26803
26804 /* Parse tentatively; we are going to roll back all of the tokens
26805 consumed here. */
26806 cp_parser_parse_tentatively (parser);
26807 /* Assume that we are looking at a constructor declarator. */
26808 constructor_p = true;
26809
26810 /* Look for the optional `::' operator. */
26811 cp_parser_global_scope_opt (parser,
26812 /*current_scope_valid_p=*/false);
26813 /* Look for the nested-name-specifier. */
26814 nested_name_specifier
26815 = (cp_parser_nested_name_specifier_opt (parser,
26816 /*typename_keyword_p=*/false,
26817 /*check_dependency_p=*/false,
26818 /*type_p=*/false,
26819 /*is_declaration=*/false));
26820
26821 outside_class_specifier_p = (!at_class_scope_p ()
26822 || !TYPE_BEING_DEFINED (current_class_type)
26823 || friend_p);
26824
26825 /* Outside of a class-specifier, there must be a
26826 nested-name-specifier. Except in C++17 mode, where we
26827 might be declaring a guiding declaration. */
26828 if (!nested_name_specifier && outside_class_specifier_p
26829 && cxx_dialect < cxx17)
26830 constructor_p = false;
26831 else if (nested_name_specifier == error_mark_node)
26832 constructor_p = false;
26833
26834 /* If we have a class scope, this is easy; DR 147 says that S::S always
26835 names the constructor, and no other qualified name could. */
26836 if (constructor_p && nested_name_specifier
26837 && CLASS_TYPE_P (nested_name_specifier))
26838 {
26839 tree id = cp_parser_unqualified_id (parser,
26840 /*template_keyword_p=*/false,
26841 /*check_dependency_p=*/false,
26842 /*declarator_p=*/true,
26843 /*optional_p=*/false);
26844 if (is_overloaded_fn (id))
26845 id = DECL_NAME (get_first_fn (id));
26846 if (!constructor_name_p (id, nested_name_specifier))
26847 constructor_p = false;
26848 }
26849 /* If we still think that this might be a constructor-declarator,
26850 look for a class-name. */
26851 else if (constructor_p)
26852 {
26853 /* If we have:
26854
26855 template <typename T> struct S {
26856 S();
26857 };
26858
26859 we must recognize that the nested `S' names a class. */
26860 if (cxx_dialect >= cxx17)
26861 cp_parser_parse_tentatively (parser);
26862
26863 tree type_decl;
26864 type_decl = cp_parser_class_name (parser,
26865 /*typename_keyword_p=*/false,
26866 /*template_keyword_p=*/false,
26867 none_type,
26868 /*check_dependency_p=*/false,
26869 /*class_head_p=*/false,
26870 /*is_declaration=*/false);
26871
26872 if (cxx_dialect >= cxx17
26873 && !cp_parser_parse_definitely (parser))
26874 {
26875 type_decl = NULL_TREE;
26876 tree tmpl = cp_parser_template_name (parser,
26877 /*template_keyword*/false,
26878 /*check_dependency_p*/false,
26879 /*is_declaration*/false,
26880 none_type,
26881 /*is_identifier*/NULL);
26882 if (DECL_CLASS_TEMPLATE_P (tmpl)
26883 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26884 /* It's a deduction guide, return true. */;
26885 else
26886 cp_parser_simulate_error (parser);
26887 }
26888
26889 /* If there was no class-name, then this is not a constructor.
26890 Otherwise, if we are in a class-specifier and we aren't
26891 handling a friend declaration, check that its type matches
26892 current_class_type (c++/38313). Note: error_mark_node
26893 is left alone for error recovery purposes. */
26894 constructor_p = (!cp_parser_error_occurred (parser)
26895 && (outside_class_specifier_p
26896 || type_decl == NULL_TREE
26897 || type_decl == error_mark_node
26898 || same_type_p (current_class_type,
26899 TREE_TYPE (type_decl))));
26900
26901 /* If we're still considering a constructor, we have to see a `(',
26902 to begin the parameter-declaration-clause, followed by either a
26903 `)', an `...', or a decl-specifier. We need to check for a
26904 type-specifier to avoid being fooled into thinking that:
26905
26906 S (f) (int);
26907
26908 is a constructor. (It is actually a function named `f' that
26909 takes one parameter (of type `int') and returns a value of type
26910 `S'. */
26911 if (constructor_p
26912 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26913 constructor_p = false;
26914
26915 if (constructor_p
26916 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26917 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26918 /* A parameter declaration begins with a decl-specifier,
26919 which is either the "attribute" keyword, a storage class
26920 specifier, or (usually) a type-specifier. */
26921 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26922 {
26923 tree type;
26924 tree pushed_scope = NULL_TREE;
26925 unsigned saved_num_template_parameter_lists;
26926
26927 /* Names appearing in the type-specifier should be looked up
26928 in the scope of the class. */
26929 if (current_class_type)
26930 type = NULL_TREE;
26931 else if (type_decl)
26932 {
26933 type = TREE_TYPE (type_decl);
26934 if (TREE_CODE (type) == TYPENAME_TYPE)
26935 {
26936 type = resolve_typename_type (type,
26937 /*only_current_p=*/false);
26938 if (TREE_CODE (type) == TYPENAME_TYPE)
26939 {
26940 cp_parser_abort_tentative_parse (parser);
26941 return false;
26942 }
26943 }
26944 pushed_scope = push_scope (type);
26945 }
26946
26947 /* Inside the constructor parameter list, surrounding
26948 template-parameter-lists do not apply. */
26949 saved_num_template_parameter_lists
26950 = parser->num_template_parameter_lists;
26951 parser->num_template_parameter_lists = 0;
26952
26953 /* Look for the type-specifier. */
26954 cp_parser_type_specifier (parser,
26955 CP_PARSER_FLAGS_NONE,
26956 /*decl_specs=*/NULL,
26957 /*is_declarator=*/true,
26958 /*declares_class_or_enum=*/NULL,
26959 /*is_cv_qualifier=*/NULL);
26960
26961 parser->num_template_parameter_lists
26962 = saved_num_template_parameter_lists;
26963
26964 /* Leave the scope of the class. */
26965 if (pushed_scope)
26966 pop_scope (pushed_scope);
26967
26968 constructor_p = !cp_parser_error_occurred (parser);
26969 }
26970 }
26971
26972 /* We did not really want to consume any tokens. */
26973 cp_parser_abort_tentative_parse (parser);
26974
26975 return constructor_p;
26976 }
26977
26978 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26979 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26980 they must be performed once we are in the scope of the function.
26981
26982 Returns the function defined. */
26983
26984 static tree
26985 cp_parser_function_definition_from_specifiers_and_declarator
26986 (cp_parser* parser,
26987 cp_decl_specifier_seq *decl_specifiers,
26988 tree attributes,
26989 const cp_declarator *declarator)
26990 {
26991 tree fn;
26992 bool success_p;
26993
26994 /* Begin the function-definition. */
26995 success_p = start_function (decl_specifiers, declarator, attributes);
26996
26997 /* The things we're about to see are not directly qualified by any
26998 template headers we've seen thus far. */
26999 reset_specialization ();
27000
27001 /* If there were names looked up in the decl-specifier-seq that we
27002 did not check, check them now. We must wait until we are in the
27003 scope of the function to perform the checks, since the function
27004 might be a friend. */
27005 perform_deferred_access_checks (tf_warning_or_error);
27006
27007 if (success_p)
27008 {
27009 cp_finalize_omp_declare_simd (parser, current_function_decl);
27010 parser->omp_declare_simd = NULL;
27011 cp_finalize_oacc_routine (parser, current_function_decl, true);
27012 parser->oacc_routine = NULL;
27013 }
27014
27015 if (!success_p)
27016 {
27017 /* Skip the entire function. */
27018 cp_parser_skip_to_end_of_block_or_statement (parser);
27019 fn = error_mark_node;
27020 }
27021 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27022 {
27023 /* Seen already, skip it. An error message has already been output. */
27024 cp_parser_skip_to_end_of_block_or_statement (parser);
27025 fn = current_function_decl;
27026 current_function_decl = NULL_TREE;
27027 /* If this is a function from a class, pop the nested class. */
27028 if (current_class_name)
27029 pop_nested_class ();
27030 }
27031 else
27032 {
27033 timevar_id_t tv;
27034 if (DECL_DECLARED_INLINE_P (current_function_decl))
27035 tv = TV_PARSE_INLINE;
27036 else
27037 tv = TV_PARSE_FUNC;
27038 timevar_push (tv);
27039 fn = cp_parser_function_definition_after_declarator (parser,
27040 /*inline_p=*/false);
27041 timevar_pop (tv);
27042 }
27043
27044 return fn;
27045 }
27046
27047 /* Parse the part of a function-definition that follows the
27048 declarator. INLINE_P is TRUE iff this function is an inline
27049 function defined within a class-specifier.
27050
27051 Returns the function defined. */
27052
27053 static tree
27054 cp_parser_function_definition_after_declarator (cp_parser* parser,
27055 bool inline_p)
27056 {
27057 tree fn;
27058 bool saved_in_unbraced_linkage_specification_p;
27059 bool saved_in_function_body;
27060 unsigned saved_num_template_parameter_lists;
27061 cp_token *token;
27062 bool fully_implicit_function_template_p
27063 = parser->fully_implicit_function_template_p;
27064 parser->fully_implicit_function_template_p = false;
27065 tree implicit_template_parms
27066 = parser->implicit_template_parms;
27067 parser->implicit_template_parms = 0;
27068 cp_binding_level* implicit_template_scope
27069 = parser->implicit_template_scope;
27070 parser->implicit_template_scope = 0;
27071
27072 saved_in_function_body = parser->in_function_body;
27073 parser->in_function_body = true;
27074 /* If the next token is `return', then the code may be trying to
27075 make use of the "named return value" extension that G++ used to
27076 support. */
27077 token = cp_lexer_peek_token (parser->lexer);
27078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27079 {
27080 /* Consume the `return' keyword. */
27081 cp_lexer_consume_token (parser->lexer);
27082 /* Look for the identifier that indicates what value is to be
27083 returned. */
27084 cp_parser_identifier (parser);
27085 /* Issue an error message. */
27086 error_at (token->location,
27087 "named return values are no longer supported");
27088 /* Skip tokens until we reach the start of the function body. */
27089 while (true)
27090 {
27091 cp_token *token = cp_lexer_peek_token (parser->lexer);
27092 if (token->type == CPP_OPEN_BRACE
27093 || token->type == CPP_EOF
27094 || token->type == CPP_PRAGMA_EOL)
27095 break;
27096 cp_lexer_consume_token (parser->lexer);
27097 }
27098 }
27099 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27100 anything declared inside `f'. */
27101 saved_in_unbraced_linkage_specification_p
27102 = parser->in_unbraced_linkage_specification_p;
27103 parser->in_unbraced_linkage_specification_p = false;
27104 /* Inside the function, surrounding template-parameter-lists do not
27105 apply. */
27106 saved_num_template_parameter_lists
27107 = parser->num_template_parameter_lists;
27108 parser->num_template_parameter_lists = 0;
27109
27110 /* If the next token is `try', `__transaction_atomic', or
27111 `__transaction_relaxed`, then we are looking at either function-try-block
27112 or function-transaction-block. Note that all of these include the
27113 function-body. */
27114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27115 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27116 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27117 RID_TRANSACTION_RELAXED))
27118 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27119 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27120 cp_parser_function_try_block (parser);
27121 else
27122 cp_parser_ctor_initializer_opt_and_function_body
27123 (parser, /*in_function_try_block=*/false);
27124
27125 /* Finish the function. */
27126 fn = finish_function (inline_p);
27127 /* Generate code for it, if necessary. */
27128 expand_or_defer_fn (fn);
27129 /* Restore the saved values. */
27130 parser->in_unbraced_linkage_specification_p
27131 = saved_in_unbraced_linkage_specification_p;
27132 parser->num_template_parameter_lists
27133 = saved_num_template_parameter_lists;
27134 parser->in_function_body = saved_in_function_body;
27135
27136 parser->fully_implicit_function_template_p
27137 = fully_implicit_function_template_p;
27138 parser->implicit_template_parms
27139 = implicit_template_parms;
27140 parser->implicit_template_scope
27141 = implicit_template_scope;
27142
27143 if (parser->fully_implicit_function_template_p)
27144 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27145
27146 return fn;
27147 }
27148
27149 /* Parse a template-declaration body (following argument list). */
27150
27151 static void
27152 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27153 tree parameter_list,
27154 bool member_p)
27155 {
27156 tree decl = NULL_TREE;
27157 bool friend_p = false;
27158
27159 /* We just processed one more parameter list. */
27160 ++parser->num_template_parameter_lists;
27161
27162 /* Get the deferred access checks from the parameter list. These
27163 will be checked once we know what is being declared, as for a
27164 member template the checks must be performed in the scope of the
27165 class containing the member. */
27166 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27167
27168 /* Tentatively parse for a new template parameter list, which can either be
27169 the template keyword or a template introduction. */
27170 if (cp_parser_template_declaration_after_export (parser, member_p))
27171 /* OK */;
27172 else if (cxx_dialect >= cxx11
27173 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27174 decl = cp_parser_alias_declaration (parser);
27175 else
27176 {
27177 /* There are no access checks when parsing a template, as we do not
27178 know if a specialization will be a friend. */
27179 push_deferring_access_checks (dk_no_check);
27180 cp_token *token = cp_lexer_peek_token (parser->lexer);
27181 decl = cp_parser_single_declaration (parser,
27182 checks,
27183 member_p,
27184 /*explicit_specialization_p=*/false,
27185 &friend_p);
27186 pop_deferring_access_checks ();
27187
27188 /* If this is a member template declaration, let the front
27189 end know. */
27190 if (member_p && !friend_p && decl)
27191 {
27192 if (TREE_CODE (decl) == TYPE_DECL)
27193 cp_parser_check_access_in_redeclaration (decl, token->location);
27194
27195 decl = finish_member_template_decl (decl);
27196 }
27197 else if (friend_p && decl
27198 && DECL_DECLARES_TYPE_P (decl))
27199 make_friend_class (current_class_type, TREE_TYPE (decl),
27200 /*complain=*/true);
27201 }
27202 /* We are done with the current parameter list. */
27203 --parser->num_template_parameter_lists;
27204
27205 pop_deferring_access_checks ();
27206
27207 /* Finish up. */
27208 finish_template_decl (parameter_list);
27209
27210 /* Check the template arguments for a literal operator template. */
27211 if (decl
27212 && DECL_DECLARES_FUNCTION_P (decl)
27213 && UDLIT_OPER_P (DECL_NAME (decl)))
27214 {
27215 bool ok = true;
27216 if (parameter_list == NULL_TREE)
27217 ok = false;
27218 else
27219 {
27220 int num_parms = TREE_VEC_LENGTH (parameter_list);
27221 if (num_parms == 1)
27222 {
27223 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27224 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27225 if (TREE_TYPE (parm) != char_type_node
27226 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27227 ok = false;
27228 }
27229 else if (num_parms == 2 && cxx_dialect >= cxx14)
27230 {
27231 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27232 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27233 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27234 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27235 if (parm == error_mark_node
27236 || TREE_TYPE (parm) != TREE_TYPE (type)
27237 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27238 ok = false;
27239 }
27240 else
27241 ok = false;
27242 }
27243 if (!ok)
27244 {
27245 if (cxx_dialect >= cxx14)
27246 error ("literal operator template %qD has invalid parameter list."
27247 " Expected non-type template argument pack <char...>"
27248 " or <typename CharT, CharT...>",
27249 decl);
27250 else
27251 error ("literal operator template %qD has invalid parameter list."
27252 " Expected non-type template argument pack <char...>",
27253 decl);
27254 }
27255 }
27256
27257 /* Register member declarations. */
27258 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27259 finish_member_declaration (decl);
27260 /* If DECL is a function template, we must return to parse it later.
27261 (Even though there is no definition, there might be default
27262 arguments that need handling.) */
27263 if (member_p && decl
27264 && DECL_DECLARES_FUNCTION_P (decl))
27265 vec_safe_push (unparsed_funs_with_definitions, decl);
27266 }
27267
27268 /* Parse a template introduction header for a template-declaration. Returns
27269 false if tentative parse fails. */
27270
27271 static bool
27272 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27273 {
27274 cp_parser_parse_tentatively (parser);
27275
27276 tree saved_scope = parser->scope;
27277 tree saved_object_scope = parser->object_scope;
27278 tree saved_qualifying_scope = parser->qualifying_scope;
27279
27280 /* Look for the optional `::' operator. */
27281 cp_parser_global_scope_opt (parser,
27282 /*current_scope_valid_p=*/false);
27283 /* Look for the nested-name-specifier. */
27284 cp_parser_nested_name_specifier_opt (parser,
27285 /*typename_keyword_p=*/false,
27286 /*check_dependency_p=*/true,
27287 /*type_p=*/false,
27288 /*is_declaration=*/false);
27289
27290 cp_token *token = cp_lexer_peek_token (parser->lexer);
27291 tree concept_name = cp_parser_identifier (parser);
27292
27293 /* Look up the concept for which we will be matching
27294 template parameters. */
27295 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27296 token->location);
27297 parser->scope = saved_scope;
27298 parser->object_scope = saved_object_scope;
27299 parser->qualifying_scope = saved_qualifying_scope;
27300
27301 if (concept_name == error_mark_node)
27302 cp_parser_simulate_error (parser);
27303
27304 /* Look for opening brace for introduction. */
27305 matching_braces braces;
27306 braces.require_open (parser);
27307
27308 if (!cp_parser_parse_definitely (parser))
27309 return false;
27310
27311 push_deferring_access_checks (dk_deferred);
27312
27313 /* Build vector of placeholder parameters and grab
27314 matching identifiers. */
27315 tree introduction_list = cp_parser_introduction_list (parser);
27316
27317 /* Look for closing brace for introduction. */
27318 if (!braces.require_close (parser))
27319 return true;
27320
27321 /* The introduction-list shall not be empty. */
27322 int nargs = TREE_VEC_LENGTH (introduction_list);
27323 if (nargs == 0)
27324 {
27325 /* In cp_parser_introduction_list we have already issued an error. */
27326 return true;
27327 }
27328
27329 if (tmpl_decl == error_mark_node)
27330 {
27331 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27332 token->location);
27333 return true;
27334 }
27335
27336 /* Build and associate the constraint. */
27337 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27338 if (parms && parms != error_mark_node)
27339 {
27340 cp_parser_template_declaration_after_parameters (parser, parms,
27341 member_p);
27342 return true;
27343 }
27344
27345 error_at (token->location, "no matching concept for template-introduction");
27346 return true;
27347 }
27348
27349 /* Parse a normal template-declaration following the template keyword. */
27350
27351 static void
27352 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27353 {
27354 tree parameter_list;
27355 bool need_lang_pop;
27356 location_t location = input_location;
27357
27358 /* Look for the `<' token. */
27359 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27360 return;
27361 if (at_class_scope_p () && current_function_decl)
27362 {
27363 /* 14.5.2.2 [temp.mem]
27364
27365 A local class shall not have member templates. */
27366 error_at (location,
27367 "invalid declaration of member template in local class");
27368 cp_parser_skip_to_end_of_block_or_statement (parser);
27369 return;
27370 }
27371 /* [temp]
27372
27373 A template ... shall not have C linkage. */
27374 if (current_lang_name == lang_name_c)
27375 {
27376 error_at (location, "template with C linkage");
27377 maybe_show_extern_c_location ();
27378 /* Give it C++ linkage to avoid confusing other parts of the
27379 front end. */
27380 push_lang_context (lang_name_cplusplus);
27381 need_lang_pop = true;
27382 }
27383 else
27384 need_lang_pop = false;
27385
27386 /* We cannot perform access checks on the template parameter
27387 declarations until we know what is being declared, just as we
27388 cannot check the decl-specifier list. */
27389 push_deferring_access_checks (dk_deferred);
27390
27391 /* If the next token is `>', then we have an invalid
27392 specialization. Rather than complain about an invalid template
27393 parameter, issue an error message here. */
27394 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27395 {
27396 cp_parser_error (parser, "invalid explicit specialization");
27397 begin_specialization ();
27398 parameter_list = NULL_TREE;
27399 }
27400 else
27401 {
27402 /* Parse the template parameters. */
27403 parameter_list = cp_parser_template_parameter_list (parser);
27404 }
27405
27406 /* Look for the `>'. */
27407 cp_parser_skip_to_end_of_template_parameter_list (parser);
27408
27409 /* Manage template requirements */
27410 if (flag_concepts)
27411 {
27412 tree reqs = get_shorthand_constraints (current_template_parms);
27413 if (tree r = cp_parser_requires_clause_opt (parser))
27414 reqs = conjoin_constraints (reqs, normalize_expression (r));
27415 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27416 }
27417
27418 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27419 member_p);
27420
27421 /* For the erroneous case of a template with C linkage, we pushed an
27422 implicit C++ linkage scope; exit that scope now. */
27423 if (need_lang_pop)
27424 pop_lang_context ();
27425 }
27426
27427 /* Parse a template-declaration, assuming that the `export' (and
27428 `extern') keywords, if present, has already been scanned. MEMBER_P
27429 is as for cp_parser_template_declaration. */
27430
27431 static bool
27432 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27433 {
27434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27435 {
27436 cp_lexer_consume_token (parser->lexer);
27437 cp_parser_explicit_template_declaration (parser, member_p);
27438 return true;
27439 }
27440 else if (flag_concepts)
27441 return cp_parser_template_introduction (parser, member_p);
27442
27443 return false;
27444 }
27445
27446 /* Perform the deferred access checks from a template-parameter-list.
27447 CHECKS is a TREE_LIST of access checks, as returned by
27448 get_deferred_access_checks. */
27449
27450 static void
27451 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27452 {
27453 ++processing_template_parmlist;
27454 perform_access_checks (checks, tf_warning_or_error);
27455 --processing_template_parmlist;
27456 }
27457
27458 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27459 `function-definition' sequence that follows a template header.
27460 If MEMBER_P is true, this declaration appears in a class scope.
27461
27462 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27463 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27464
27465 static tree
27466 cp_parser_single_declaration (cp_parser* parser,
27467 vec<deferred_access_check, va_gc> *checks,
27468 bool member_p,
27469 bool explicit_specialization_p,
27470 bool* friend_p)
27471 {
27472 int declares_class_or_enum;
27473 tree decl = NULL_TREE;
27474 cp_decl_specifier_seq decl_specifiers;
27475 bool function_definition_p = false;
27476 cp_token *decl_spec_token_start;
27477
27478 /* This function is only used when processing a template
27479 declaration. */
27480 gcc_assert (innermost_scope_kind () == sk_template_parms
27481 || innermost_scope_kind () == sk_template_spec);
27482
27483 /* Defer access checks until we know what is being declared. */
27484 push_deferring_access_checks (dk_deferred);
27485
27486 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27487 alternative. */
27488 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27489 cp_parser_decl_specifier_seq (parser,
27490 CP_PARSER_FLAGS_OPTIONAL,
27491 &decl_specifiers,
27492 &declares_class_or_enum);
27493 if (friend_p)
27494 *friend_p = cp_parser_friend_p (&decl_specifiers);
27495
27496 /* There are no template typedefs. */
27497 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27498 {
27499 error_at (decl_spec_token_start->location,
27500 "template declaration of %<typedef%>");
27501 decl = error_mark_node;
27502 }
27503
27504 /* Gather up the access checks that occurred the
27505 decl-specifier-seq. */
27506 stop_deferring_access_checks ();
27507
27508 /* Check for the declaration of a template class. */
27509 if (declares_class_or_enum)
27510 {
27511 if (cp_parser_declares_only_class_p (parser)
27512 || (declares_class_or_enum & 2))
27513 {
27514 // If this is a declaration, but not a definition, associate
27515 // any constraints with the type declaration. Constraints
27516 // are associated with definitions in cp_parser_class_specifier.
27517 if (declares_class_or_enum == 1)
27518 associate_classtype_constraints (decl_specifiers.type);
27519
27520 decl = shadow_tag (&decl_specifiers);
27521
27522 /* In this case:
27523
27524 struct C {
27525 friend template <typename T> struct A<T>::B;
27526 };
27527
27528 A<T>::B will be represented by a TYPENAME_TYPE, and
27529 therefore not recognized by shadow_tag. */
27530 if (friend_p && *friend_p
27531 && !decl
27532 && decl_specifiers.type
27533 && TYPE_P (decl_specifiers.type))
27534 decl = decl_specifiers.type;
27535
27536 if (decl && decl != error_mark_node)
27537 decl = TYPE_NAME (decl);
27538 else
27539 decl = error_mark_node;
27540
27541 /* Perform access checks for template parameters. */
27542 cp_parser_perform_template_parameter_access_checks (checks);
27543
27544 /* Give a helpful diagnostic for
27545 template <class T> struct A { } a;
27546 if we aren't already recovering from an error. */
27547 if (!cp_parser_declares_only_class_p (parser)
27548 && !seen_error ())
27549 {
27550 error_at (cp_lexer_peek_token (parser->lexer)->location,
27551 "a class template declaration must not declare "
27552 "anything else");
27553 cp_parser_skip_to_end_of_block_or_statement (parser);
27554 goto out;
27555 }
27556 }
27557 }
27558
27559 /* Complain about missing 'typename' or other invalid type names. */
27560 if (!decl_specifiers.any_type_specifiers_p
27561 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27562 {
27563 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27564 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27565 the rest of this declaration. */
27566 decl = error_mark_node;
27567 goto out;
27568 }
27569
27570 /* If it's not a template class, try for a template function. If
27571 the next token is a `;', then this declaration does not declare
27572 anything. But, if there were errors in the decl-specifiers, then
27573 the error might well have come from an attempted class-specifier.
27574 In that case, there's no need to warn about a missing declarator. */
27575 if (!decl
27576 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27577 || decl_specifiers.type != error_mark_node))
27578 {
27579 decl = cp_parser_init_declarator (parser,
27580 &decl_specifiers,
27581 checks,
27582 /*function_definition_allowed_p=*/true,
27583 member_p,
27584 declares_class_or_enum,
27585 &function_definition_p,
27586 NULL, NULL, NULL);
27587
27588 /* 7.1.1-1 [dcl.stc]
27589
27590 A storage-class-specifier shall not be specified in an explicit
27591 specialization... */
27592 if (decl
27593 && explicit_specialization_p
27594 && decl_specifiers.storage_class != sc_none)
27595 {
27596 error_at (decl_spec_token_start->location,
27597 "explicit template specialization cannot have a storage class");
27598 decl = error_mark_node;
27599 }
27600
27601 if (decl && VAR_P (decl))
27602 check_template_variable (decl);
27603 }
27604
27605 /* Look for a trailing `;' after the declaration. */
27606 if (!function_definition_p
27607 && (decl == error_mark_node
27608 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27609 cp_parser_skip_to_end_of_block_or_statement (parser);
27610
27611 out:
27612 pop_deferring_access_checks ();
27613
27614 /* Clear any current qualification; whatever comes next is the start
27615 of something new. */
27616 parser->scope = NULL_TREE;
27617 parser->qualifying_scope = NULL_TREE;
27618 parser->object_scope = NULL_TREE;
27619
27620 return decl;
27621 }
27622
27623 /* Parse a cast-expression that is not the operand of a unary "&". */
27624
27625 static cp_expr
27626 cp_parser_simple_cast_expression (cp_parser *parser)
27627 {
27628 return cp_parser_cast_expression (parser, /*address_p=*/false,
27629 /*cast_p=*/false, /*decltype*/false, NULL);
27630 }
27631
27632 /* Parse a functional cast to TYPE. Returns an expression
27633 representing the cast. */
27634
27635 static cp_expr
27636 cp_parser_functional_cast (cp_parser* parser, tree type)
27637 {
27638 vec<tree, va_gc> *vec;
27639 tree expression_list;
27640 cp_expr cast;
27641 bool nonconst_p;
27642
27643 location_t start_loc = input_location;
27644
27645 if (!type)
27646 type = error_mark_node;
27647
27648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27649 {
27650 cp_lexer_set_source_position (parser->lexer);
27651 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27652 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27653 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27654 if (TREE_CODE (type) == TYPE_DECL)
27655 type = TREE_TYPE (type);
27656
27657 cast = finish_compound_literal (type, expression_list,
27658 tf_warning_or_error, fcl_functional);
27659 /* Create a location of the form:
27660 type_name{i, f}
27661 ^~~~~~~~~~~~~~~
27662 with caret == start at the start of the type name,
27663 finishing at the closing brace. */
27664 location_t finish_loc
27665 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27666 location_t combined_loc = make_location (start_loc, start_loc,
27667 finish_loc);
27668 cast.set_location (combined_loc);
27669 return cast;
27670 }
27671
27672
27673 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27674 /*cast_p=*/true,
27675 /*allow_expansion_p=*/true,
27676 /*non_constant_p=*/NULL);
27677 if (vec == NULL)
27678 expression_list = error_mark_node;
27679 else
27680 {
27681 expression_list = build_tree_list_vec (vec);
27682 release_tree_vector (vec);
27683 }
27684
27685 cast = build_functional_cast (type, expression_list,
27686 tf_warning_or_error);
27687 /* [expr.const]/1: In an integral constant expression "only type
27688 conversions to integral or enumeration type can be used". */
27689 if (TREE_CODE (type) == TYPE_DECL)
27690 type = TREE_TYPE (type);
27691 if (cast != error_mark_node
27692 && !cast_valid_in_integral_constant_expression_p (type)
27693 && cp_parser_non_integral_constant_expression (parser,
27694 NIC_CONSTRUCTOR))
27695 return error_mark_node;
27696
27697 /* Create a location of the form:
27698 float(i)
27699 ^~~~~~~~
27700 with caret == start at the start of the type name,
27701 finishing at the closing paren. */
27702 location_t finish_loc
27703 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27704 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27705 cast.set_location (combined_loc);
27706 return cast;
27707 }
27708
27709 /* Save the tokens that make up the body of a member function defined
27710 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27711 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27712 specifiers applied to the declaration. Returns the FUNCTION_DECL
27713 for the member function. */
27714
27715 static tree
27716 cp_parser_save_member_function_body (cp_parser* parser,
27717 cp_decl_specifier_seq *decl_specifiers,
27718 cp_declarator *declarator,
27719 tree attributes)
27720 {
27721 cp_token *first;
27722 cp_token *last;
27723 tree fn;
27724 bool function_try_block = false;
27725
27726 /* Create the FUNCTION_DECL. */
27727 fn = grokmethod (decl_specifiers, declarator, attributes);
27728 cp_finalize_omp_declare_simd (parser, fn);
27729 cp_finalize_oacc_routine (parser, fn, true);
27730 /* If something went badly wrong, bail out now. */
27731 if (fn == error_mark_node)
27732 {
27733 /* If there's a function-body, skip it. */
27734 if (cp_parser_token_starts_function_definition_p
27735 (cp_lexer_peek_token (parser->lexer)))
27736 cp_parser_skip_to_end_of_block_or_statement (parser);
27737 return error_mark_node;
27738 }
27739
27740 /* Remember it, if there default args to post process. */
27741 cp_parser_save_default_args (parser, fn);
27742
27743 /* Save away the tokens that make up the body of the
27744 function. */
27745 first = parser->lexer->next_token;
27746
27747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27748 cp_lexer_consume_token (parser->lexer);
27749 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27750 RID_TRANSACTION_ATOMIC))
27751 {
27752 cp_lexer_consume_token (parser->lexer);
27753 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27754 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27755 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27756 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27757 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27758 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27759 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27760 {
27761 cp_lexer_consume_token (parser->lexer);
27762 cp_lexer_consume_token (parser->lexer);
27763 cp_lexer_consume_token (parser->lexer);
27764 cp_lexer_consume_token (parser->lexer);
27765 cp_lexer_consume_token (parser->lexer);
27766 }
27767 else
27768 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27769 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27770 {
27771 cp_lexer_consume_token (parser->lexer);
27772 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27773 break;
27774 }
27775 }
27776
27777 /* Handle function try blocks. */
27778 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27779 {
27780 cp_lexer_consume_token (parser->lexer);
27781 function_try_block = true;
27782 }
27783 /* We can have braced-init-list mem-initializers before the fn body. */
27784 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27785 {
27786 cp_lexer_consume_token (parser->lexer);
27787 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27788 {
27789 /* cache_group will stop after an un-nested { } pair, too. */
27790 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27791 break;
27792
27793 /* variadic mem-inits have ... after the ')'. */
27794 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27795 cp_lexer_consume_token (parser->lexer);
27796 }
27797 }
27798 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27799 /* Handle function try blocks. */
27800 if (function_try_block)
27801 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27802 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27803 last = parser->lexer->next_token;
27804
27805 /* Save away the inline definition; we will process it when the
27806 class is complete. */
27807 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27808 DECL_PENDING_INLINE_P (fn) = 1;
27809
27810 /* We need to know that this was defined in the class, so that
27811 friend templates are handled correctly. */
27812 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27813
27814 /* Add FN to the queue of functions to be parsed later. */
27815 vec_safe_push (unparsed_funs_with_definitions, fn);
27816
27817 return fn;
27818 }
27819
27820 /* Save the tokens that make up the in-class initializer for a non-static
27821 data member. Returns a DEFAULT_ARG. */
27822
27823 static tree
27824 cp_parser_save_nsdmi (cp_parser* parser)
27825 {
27826 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27827 }
27828
27829 /* Parse a template-argument-list, as well as the trailing ">" (but
27830 not the opening "<"). See cp_parser_template_argument_list for the
27831 return value. */
27832
27833 static tree
27834 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27835 {
27836 tree arguments;
27837 tree saved_scope;
27838 tree saved_qualifying_scope;
27839 tree saved_object_scope;
27840 bool saved_greater_than_is_operator_p;
27841 int saved_unevaluated_operand;
27842 int saved_inhibit_evaluation_warnings;
27843
27844 /* [temp.names]
27845
27846 When parsing a template-id, the first non-nested `>' is taken as
27847 the end of the template-argument-list rather than a greater-than
27848 operator. */
27849 saved_greater_than_is_operator_p
27850 = parser->greater_than_is_operator_p;
27851 parser->greater_than_is_operator_p = false;
27852 /* Parsing the argument list may modify SCOPE, so we save it
27853 here. */
27854 saved_scope = parser->scope;
27855 saved_qualifying_scope = parser->qualifying_scope;
27856 saved_object_scope = parser->object_scope;
27857 /* We need to evaluate the template arguments, even though this
27858 template-id may be nested within a "sizeof". */
27859 saved_unevaluated_operand = cp_unevaluated_operand;
27860 cp_unevaluated_operand = 0;
27861 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27862 c_inhibit_evaluation_warnings = 0;
27863 /* Parse the template-argument-list itself. */
27864 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27865 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27866 arguments = NULL_TREE;
27867 else
27868 arguments = cp_parser_template_argument_list (parser);
27869 /* Look for the `>' that ends the template-argument-list. If we find
27870 a '>>' instead, it's probably just a typo. */
27871 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27872 {
27873 if (cxx_dialect != cxx98)
27874 {
27875 /* In C++0x, a `>>' in a template argument list or cast
27876 expression is considered to be two separate `>'
27877 tokens. So, change the current token to a `>', but don't
27878 consume it: it will be consumed later when the outer
27879 template argument list (or cast expression) is parsed.
27880 Note that this replacement of `>' for `>>' is necessary
27881 even if we are parsing tentatively: in the tentative
27882 case, after calling
27883 cp_parser_enclosed_template_argument_list we will always
27884 throw away all of the template arguments and the first
27885 closing `>', either because the template argument list
27886 was erroneous or because we are replacing those tokens
27887 with a CPP_TEMPLATE_ID token. The second `>' (which will
27888 not have been thrown away) is needed either to close an
27889 outer template argument list or to complete a new-style
27890 cast. */
27891 cp_token *token = cp_lexer_peek_token (parser->lexer);
27892 token->type = CPP_GREATER;
27893 }
27894 else if (!saved_greater_than_is_operator_p)
27895 {
27896 /* If we're in a nested template argument list, the '>>' has
27897 to be a typo for '> >'. We emit the error message, but we
27898 continue parsing and we push a '>' as next token, so that
27899 the argument list will be parsed correctly. Note that the
27900 global source location is still on the token before the
27901 '>>', so we need to say explicitly where we want it. */
27902 cp_token *token = cp_lexer_peek_token (parser->lexer);
27903 gcc_rich_location richloc (token->location);
27904 richloc.add_fixit_replace ("> >");
27905 error_at (&richloc, "%<>>%> should be %<> >%> "
27906 "within a nested template argument list");
27907
27908 token->type = CPP_GREATER;
27909 }
27910 else
27911 {
27912 /* If this is not a nested template argument list, the '>>'
27913 is a typo for '>'. Emit an error message and continue.
27914 Same deal about the token location, but here we can get it
27915 right by consuming the '>>' before issuing the diagnostic. */
27916 cp_token *token = cp_lexer_consume_token (parser->lexer);
27917 error_at (token->location,
27918 "spurious %<>>%>, use %<>%> to terminate "
27919 "a template argument list");
27920 }
27921 }
27922 else
27923 cp_parser_skip_to_end_of_template_parameter_list (parser);
27924 /* The `>' token might be a greater-than operator again now. */
27925 parser->greater_than_is_operator_p
27926 = saved_greater_than_is_operator_p;
27927 /* Restore the SAVED_SCOPE. */
27928 parser->scope = saved_scope;
27929 parser->qualifying_scope = saved_qualifying_scope;
27930 parser->object_scope = saved_object_scope;
27931 cp_unevaluated_operand = saved_unevaluated_operand;
27932 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27933
27934 return arguments;
27935 }
27936
27937 /* MEMBER_FUNCTION is a member function, or a friend. If default
27938 arguments, or the body of the function have not yet been parsed,
27939 parse them now. */
27940
27941 static void
27942 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27943 {
27944 timevar_push (TV_PARSE_INMETH);
27945 /* If this member is a template, get the underlying
27946 FUNCTION_DECL. */
27947 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27948 member_function = DECL_TEMPLATE_RESULT (member_function);
27949
27950 /* There should not be any class definitions in progress at this
27951 point; the bodies of members are only parsed outside of all class
27952 definitions. */
27953 gcc_assert (parser->num_classes_being_defined == 0);
27954 /* While we're parsing the member functions we might encounter more
27955 classes. We want to handle them right away, but we don't want
27956 them getting mixed up with functions that are currently in the
27957 queue. */
27958 push_unparsed_function_queues (parser);
27959
27960 /* Make sure that any template parameters are in scope. */
27961 maybe_begin_member_template_processing (member_function);
27962
27963 /* If the body of the function has not yet been parsed, parse it
27964 now. */
27965 if (DECL_PENDING_INLINE_P (member_function))
27966 {
27967 tree function_scope;
27968 cp_token_cache *tokens;
27969
27970 /* The function is no longer pending; we are processing it. */
27971 tokens = DECL_PENDING_INLINE_INFO (member_function);
27972 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27973 DECL_PENDING_INLINE_P (member_function) = 0;
27974
27975 /* If this is a local class, enter the scope of the containing
27976 function. */
27977 function_scope = current_function_decl;
27978 if (function_scope)
27979 push_function_context ();
27980
27981 /* Push the body of the function onto the lexer stack. */
27982 cp_parser_push_lexer_for_tokens (parser, tokens);
27983
27984 /* Let the front end know that we going to be defining this
27985 function. */
27986 start_preparsed_function (member_function, NULL_TREE,
27987 SF_PRE_PARSED | SF_INCLASS_INLINE);
27988
27989 /* Don't do access checking if it is a templated function. */
27990 if (processing_template_decl)
27991 push_deferring_access_checks (dk_no_check);
27992
27993 /* #pragma omp declare reduction needs special parsing. */
27994 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27995 {
27996 parser->lexer->in_pragma = true;
27997 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27998 finish_function (/*inline_p=*/true);
27999 cp_check_omp_declare_reduction (member_function);
28000 }
28001 else
28002 /* Now, parse the body of the function. */
28003 cp_parser_function_definition_after_declarator (parser,
28004 /*inline_p=*/true);
28005
28006 if (processing_template_decl)
28007 pop_deferring_access_checks ();
28008
28009 /* Leave the scope of the containing function. */
28010 if (function_scope)
28011 pop_function_context ();
28012 cp_parser_pop_lexer (parser);
28013 }
28014
28015 /* Remove any template parameters from the symbol table. */
28016 maybe_end_member_template_processing ();
28017
28018 /* Restore the queue. */
28019 pop_unparsed_function_queues (parser);
28020 timevar_pop (TV_PARSE_INMETH);
28021 }
28022
28023 /* If DECL contains any default args, remember it on the unparsed
28024 functions queue. */
28025
28026 static void
28027 cp_parser_save_default_args (cp_parser* parser, tree decl)
28028 {
28029 tree probe;
28030
28031 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28032 probe;
28033 probe = TREE_CHAIN (probe))
28034 if (TREE_PURPOSE (probe))
28035 {
28036 cp_default_arg_entry entry = {current_class_type, decl};
28037 vec_safe_push (unparsed_funs_with_default_args, entry);
28038 break;
28039 }
28040 }
28041
28042 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28043 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28044 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28045 from the parameter-type-list. */
28046
28047 static tree
28048 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28049 tree default_arg, tree parmtype)
28050 {
28051 cp_token_cache *tokens;
28052 tree parsed_arg;
28053 bool dummy;
28054
28055 if (default_arg == error_mark_node)
28056 return error_mark_node;
28057
28058 /* Push the saved tokens for the default argument onto the parser's
28059 lexer stack. */
28060 tokens = DEFARG_TOKENS (default_arg);
28061 cp_parser_push_lexer_for_tokens (parser, tokens);
28062
28063 start_lambda_scope (decl);
28064
28065 /* Parse the default argument. */
28066 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28067 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28068 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28069
28070 finish_lambda_scope ();
28071
28072 if (parsed_arg == error_mark_node)
28073 cp_parser_skip_to_end_of_statement (parser);
28074
28075 if (!processing_template_decl)
28076 {
28077 /* In a non-template class, check conversions now. In a template,
28078 we'll wait and instantiate these as needed. */
28079 if (TREE_CODE (decl) == PARM_DECL)
28080 parsed_arg = check_default_argument (parmtype, parsed_arg,
28081 tf_warning_or_error);
28082 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28083 parsed_arg = error_mark_node;
28084 else
28085 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28086 }
28087
28088 /* If the token stream has not been completely used up, then
28089 there was extra junk after the end of the default
28090 argument. */
28091 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28092 {
28093 if (TREE_CODE (decl) == PARM_DECL)
28094 cp_parser_error (parser, "expected %<,%>");
28095 else
28096 cp_parser_error (parser, "expected %<;%>");
28097 }
28098
28099 /* Revert to the main lexer. */
28100 cp_parser_pop_lexer (parser);
28101
28102 return parsed_arg;
28103 }
28104
28105 /* FIELD is a non-static data member with an initializer which we saved for
28106 later; parse it now. */
28107
28108 static void
28109 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28110 {
28111 tree def;
28112
28113 maybe_begin_member_template_processing (field);
28114
28115 push_unparsed_function_queues (parser);
28116 def = cp_parser_late_parse_one_default_arg (parser, field,
28117 DECL_INITIAL (field),
28118 NULL_TREE);
28119 pop_unparsed_function_queues (parser);
28120
28121 maybe_end_member_template_processing ();
28122
28123 DECL_INITIAL (field) = def;
28124 }
28125
28126 /* FN is a FUNCTION_DECL which may contains a parameter with an
28127 unparsed DEFAULT_ARG. Parse the default args now. This function
28128 assumes that the current scope is the scope in which the default
28129 argument should be processed. */
28130
28131 static void
28132 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28133 {
28134 bool saved_local_variables_forbidden_p;
28135 tree parm, parmdecl;
28136
28137 /* While we're parsing the default args, we might (due to the
28138 statement expression extension) encounter more classes. We want
28139 to handle them right away, but we don't want them getting mixed
28140 up with default args that are currently in the queue. */
28141 push_unparsed_function_queues (parser);
28142
28143 /* Local variable names (and the `this' keyword) may not appear
28144 in a default argument. */
28145 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28146 parser->local_variables_forbidden_p = true;
28147
28148 push_defarg_context (fn);
28149
28150 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28151 parmdecl = DECL_ARGUMENTS (fn);
28152 parm && parm != void_list_node;
28153 parm = TREE_CHAIN (parm),
28154 parmdecl = DECL_CHAIN (parmdecl))
28155 {
28156 tree default_arg = TREE_PURPOSE (parm);
28157 tree parsed_arg;
28158 vec<tree, va_gc> *insts;
28159 tree copy;
28160 unsigned ix;
28161
28162 if (!default_arg)
28163 continue;
28164
28165 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28166 /* This can happen for a friend declaration for a function
28167 already declared with default arguments. */
28168 continue;
28169
28170 parsed_arg
28171 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28172 default_arg,
28173 TREE_VALUE (parm));
28174 TREE_PURPOSE (parm) = parsed_arg;
28175
28176 /* Update any instantiations we've already created. */
28177 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28178 vec_safe_iterate (insts, ix, &copy); ix++)
28179 TREE_PURPOSE (copy) = parsed_arg;
28180 }
28181
28182 pop_defarg_context ();
28183
28184 /* Make sure no default arg is missing. */
28185 check_default_args (fn);
28186
28187 /* Restore the state of local_variables_forbidden_p. */
28188 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28189
28190 /* Restore the queue. */
28191 pop_unparsed_function_queues (parser);
28192 }
28193
28194 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28195
28196 sizeof ... ( identifier )
28197
28198 where the 'sizeof' token has already been consumed. */
28199
28200 static tree
28201 cp_parser_sizeof_pack (cp_parser *parser)
28202 {
28203 /* Consume the `...'. */
28204 cp_lexer_consume_token (parser->lexer);
28205 maybe_warn_variadic_templates ();
28206
28207 matching_parens parens;
28208 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28209 if (paren)
28210 parens.consume_open (parser);
28211 else
28212 permerror (cp_lexer_peek_token (parser->lexer)->location,
28213 "%<sizeof...%> argument must be surrounded by parentheses");
28214
28215 cp_token *token = cp_lexer_peek_token (parser->lexer);
28216 tree name = cp_parser_identifier (parser);
28217 if (name == error_mark_node)
28218 return error_mark_node;
28219 /* The name is not qualified. */
28220 parser->scope = NULL_TREE;
28221 parser->qualifying_scope = NULL_TREE;
28222 parser->object_scope = NULL_TREE;
28223 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28224 if (expr == error_mark_node)
28225 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28226 token->location);
28227 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28228 expr = TREE_TYPE (expr);
28229 else if (TREE_CODE (expr) == CONST_DECL)
28230 expr = DECL_INITIAL (expr);
28231 expr = make_pack_expansion (expr);
28232 PACK_EXPANSION_SIZEOF_P (expr) = true;
28233
28234 if (paren)
28235 parens.require_close (parser);
28236
28237 return expr;
28238 }
28239
28240 /* Parse the operand of `sizeof' (or a similar operator). Returns
28241 either a TYPE or an expression, depending on the form of the
28242 input. The KEYWORD indicates which kind of expression we have
28243 encountered. */
28244
28245 static tree
28246 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28247 {
28248 tree expr = NULL_TREE;
28249 const char *saved_message;
28250 char *tmp;
28251 bool saved_integral_constant_expression_p;
28252 bool saved_non_integral_constant_expression_p;
28253
28254 /* If it's a `...', then we are computing the length of a parameter
28255 pack. */
28256 if (keyword == RID_SIZEOF
28257 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28258 return cp_parser_sizeof_pack (parser);
28259
28260 /* Types cannot be defined in a `sizeof' expression. Save away the
28261 old message. */
28262 saved_message = parser->type_definition_forbidden_message;
28263 /* And create the new one. */
28264 tmp = concat ("types may not be defined in %<",
28265 IDENTIFIER_POINTER (ridpointers[keyword]),
28266 "%> expressions", NULL);
28267 parser->type_definition_forbidden_message = tmp;
28268
28269 /* The restrictions on constant-expressions do not apply inside
28270 sizeof expressions. */
28271 saved_integral_constant_expression_p
28272 = parser->integral_constant_expression_p;
28273 saved_non_integral_constant_expression_p
28274 = parser->non_integral_constant_expression_p;
28275 parser->integral_constant_expression_p = false;
28276
28277 /* Do not actually evaluate the expression. */
28278 ++cp_unevaluated_operand;
28279 ++c_inhibit_evaluation_warnings;
28280 /* If it's a `(', then we might be looking at the type-id
28281 construction. */
28282 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28283 {
28284 tree type = NULL_TREE;
28285
28286 /* We can't be sure yet whether we're looking at a type-id or an
28287 expression. */
28288 cp_parser_parse_tentatively (parser);
28289
28290 matching_parens parens;
28291 parens.consume_open (parser);
28292
28293 /* Note: as a GNU Extension, compound literals are considered
28294 postfix-expressions as they are in C99, so they are valid
28295 arguments to sizeof. See comment in cp_parser_cast_expression
28296 for details. */
28297 if (cp_parser_compound_literal_p (parser))
28298 cp_parser_simulate_error (parser);
28299 else
28300 {
28301 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28302 parser->in_type_id_in_expr_p = true;
28303 /* Look for the type-id. */
28304 type = cp_parser_type_id (parser);
28305 /* Look for the closing `)'. */
28306 parens.require_close (parser);
28307 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28308 }
28309
28310 /* If all went well, then we're done. */
28311 if (cp_parser_parse_definitely (parser))
28312 expr = type;
28313 }
28314
28315 /* If the type-id production did not work out, then we must be
28316 looking at the unary-expression production. */
28317 if (!expr)
28318 expr = cp_parser_unary_expression (parser);
28319
28320 /* Go back to evaluating expressions. */
28321 --cp_unevaluated_operand;
28322 --c_inhibit_evaluation_warnings;
28323
28324 /* Free the message we created. */
28325 free (tmp);
28326 /* And restore the old one. */
28327 parser->type_definition_forbidden_message = saved_message;
28328 parser->integral_constant_expression_p
28329 = saved_integral_constant_expression_p;
28330 parser->non_integral_constant_expression_p
28331 = saved_non_integral_constant_expression_p;
28332
28333 return expr;
28334 }
28335
28336 /* If the current declaration has no declarator, return true. */
28337
28338 static bool
28339 cp_parser_declares_only_class_p (cp_parser *parser)
28340 {
28341 /* If the next token is a `;' or a `,' then there is no
28342 declarator. */
28343 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28344 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28345 }
28346
28347 /* Update the DECL_SPECS to reflect the storage class indicated by
28348 KEYWORD. */
28349
28350 static void
28351 cp_parser_set_storage_class (cp_parser *parser,
28352 cp_decl_specifier_seq *decl_specs,
28353 enum rid keyword,
28354 cp_token *token)
28355 {
28356 cp_storage_class storage_class;
28357
28358 if (parser->in_unbraced_linkage_specification_p)
28359 {
28360 error_at (token->location, "invalid use of %qD in linkage specification",
28361 ridpointers[keyword]);
28362 return;
28363 }
28364 else if (decl_specs->storage_class != sc_none)
28365 {
28366 decl_specs->conflicting_specifiers_p = true;
28367 return;
28368 }
28369
28370 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28371 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28372 && decl_specs->gnu_thread_keyword_p)
28373 {
28374 pedwarn (decl_specs->locations[ds_thread], 0,
28375 "%<__thread%> before %qD", ridpointers[keyword]);
28376 }
28377
28378 switch (keyword)
28379 {
28380 case RID_AUTO:
28381 storage_class = sc_auto;
28382 break;
28383 case RID_REGISTER:
28384 storage_class = sc_register;
28385 break;
28386 case RID_STATIC:
28387 storage_class = sc_static;
28388 break;
28389 case RID_EXTERN:
28390 storage_class = sc_extern;
28391 break;
28392 case RID_MUTABLE:
28393 storage_class = sc_mutable;
28394 break;
28395 default:
28396 gcc_unreachable ();
28397 }
28398 decl_specs->storage_class = storage_class;
28399 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28400
28401 /* A storage class specifier cannot be applied alongside a typedef
28402 specifier. If there is a typedef specifier present then set
28403 conflicting_specifiers_p which will trigger an error later
28404 on in grokdeclarator. */
28405 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28406 decl_specs->conflicting_specifiers_p = true;
28407 }
28408
28409 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28410 is true, the type is a class or enum definition. */
28411
28412 static void
28413 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28414 tree type_spec,
28415 cp_token *token,
28416 bool type_definition_p)
28417 {
28418 decl_specs->any_specifiers_p = true;
28419
28420 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28421 (with, for example, in "typedef int wchar_t;") we remember that
28422 this is what happened. In system headers, we ignore these
28423 declarations so that G++ can work with system headers that are not
28424 C++-safe. */
28425 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28426 && !type_definition_p
28427 && (type_spec == boolean_type_node
28428 || type_spec == char16_type_node
28429 || type_spec == char32_type_node
28430 || type_spec == wchar_type_node)
28431 && (decl_specs->type
28432 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28433 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28434 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28435 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28436 {
28437 decl_specs->redefined_builtin_type = type_spec;
28438 set_and_check_decl_spec_loc (decl_specs,
28439 ds_redefined_builtin_type_spec,
28440 token);
28441 if (!decl_specs->type)
28442 {
28443 decl_specs->type = type_spec;
28444 decl_specs->type_definition_p = false;
28445 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28446 }
28447 }
28448 else if (decl_specs->type)
28449 decl_specs->multiple_types_p = true;
28450 else
28451 {
28452 decl_specs->type = type_spec;
28453 decl_specs->type_definition_p = type_definition_p;
28454 decl_specs->redefined_builtin_type = NULL_TREE;
28455 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28456 }
28457 }
28458
28459 /* True iff TOKEN is the GNU keyword __thread. */
28460
28461 static bool
28462 token_is__thread (cp_token *token)
28463 {
28464 gcc_assert (token->keyword == RID_THREAD);
28465 return id_equal (token->u.value, "__thread");
28466 }
28467
28468 /* Set the location for a declarator specifier and check if it is
28469 duplicated.
28470
28471 DECL_SPECS is the sequence of declarator specifiers onto which to
28472 set the location.
28473
28474 DS is the single declarator specifier to set which location is to
28475 be set onto the existing sequence of declarators.
28476
28477 LOCATION is the location for the declarator specifier to
28478 consider. */
28479
28480 static void
28481 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28482 cp_decl_spec ds, cp_token *token)
28483 {
28484 gcc_assert (ds < ds_last);
28485
28486 if (decl_specs == NULL)
28487 return;
28488
28489 source_location location = token->location;
28490
28491 if (decl_specs->locations[ds] == 0)
28492 {
28493 decl_specs->locations[ds] = location;
28494 if (ds == ds_thread)
28495 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28496 }
28497 else
28498 {
28499 if (ds == ds_long)
28500 {
28501 if (decl_specs->locations[ds_long_long] != 0)
28502 error_at (location,
28503 "%<long long long%> is too long for GCC");
28504 else
28505 {
28506 decl_specs->locations[ds_long_long] = location;
28507 pedwarn_cxx98 (location,
28508 OPT_Wlong_long,
28509 "ISO C++ 1998 does not support %<long long%>");
28510 }
28511 }
28512 else if (ds == ds_thread)
28513 {
28514 bool gnu = token_is__thread (token);
28515 gcc_rich_location richloc (location);
28516 if (gnu != decl_specs->gnu_thread_keyword_p)
28517 {
28518 richloc.add_range (decl_specs->locations[ds_thread]);
28519 error_at (&richloc,
28520 "both %<__thread%> and %<thread_local%> specified");
28521 }
28522 else
28523 {
28524 richloc.add_fixit_remove ();
28525 error_at (&richloc, "duplicate %qD", token->u.value);
28526 }
28527 }
28528 else
28529 {
28530 static const char *const decl_spec_names[] = {
28531 "signed",
28532 "unsigned",
28533 "short",
28534 "long",
28535 "const",
28536 "volatile",
28537 "restrict",
28538 "inline",
28539 "virtual",
28540 "explicit",
28541 "friend",
28542 "typedef",
28543 "using",
28544 "constexpr",
28545 "__complex"
28546 };
28547 gcc_rich_location richloc (location);
28548 richloc.add_fixit_remove ();
28549 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28550 }
28551 }
28552 }
28553
28554 /* Return true iff the declarator specifier DS is present in the
28555 sequence of declarator specifiers DECL_SPECS. */
28556
28557 bool
28558 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28559 cp_decl_spec ds)
28560 {
28561 gcc_assert (ds < ds_last);
28562
28563 if (decl_specs == NULL)
28564 return false;
28565
28566 return decl_specs->locations[ds] != 0;
28567 }
28568
28569 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28570 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28571
28572 static bool
28573 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28574 {
28575 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28576 }
28577
28578 /* Issue an error message indicating that TOKEN_DESC was expected.
28579 If KEYWORD is true, it indicated this function is called by
28580 cp_parser_require_keword and the required token can only be
28581 a indicated keyword.
28582
28583 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28584 within any error as the location of an "opening" token matching
28585 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28586 RT_CLOSE_PAREN). */
28587
28588 static void
28589 cp_parser_required_error (cp_parser *parser,
28590 required_token token_desc,
28591 bool keyword,
28592 location_t matching_location)
28593 {
28594 if (cp_parser_simulate_error (parser))
28595 return;
28596
28597 const char *gmsgid = NULL;
28598 switch (token_desc)
28599 {
28600 case RT_NEW:
28601 gmsgid = G_("expected %<new%>");
28602 break;
28603 case RT_DELETE:
28604 gmsgid = G_("expected %<delete%>");
28605 break;
28606 case RT_RETURN:
28607 gmsgid = G_("expected %<return%>");
28608 break;
28609 case RT_WHILE:
28610 gmsgid = G_("expected %<while%>");
28611 break;
28612 case RT_EXTERN:
28613 gmsgid = G_("expected %<extern%>");
28614 break;
28615 case RT_STATIC_ASSERT:
28616 gmsgid = G_("expected %<static_assert%>");
28617 break;
28618 case RT_DECLTYPE:
28619 gmsgid = G_("expected %<decltype%>");
28620 break;
28621 case RT_OPERATOR:
28622 gmsgid = G_("expected %<operator%>");
28623 break;
28624 case RT_CLASS:
28625 gmsgid = G_("expected %<class%>");
28626 break;
28627 case RT_TEMPLATE:
28628 gmsgid = G_("expected %<template%>");
28629 break;
28630 case RT_NAMESPACE:
28631 gmsgid = G_("expected %<namespace%>");
28632 break;
28633 case RT_USING:
28634 gmsgid = G_("expected %<using%>");
28635 break;
28636 case RT_ASM:
28637 gmsgid = G_("expected %<asm%>");
28638 break;
28639 case RT_TRY:
28640 gmsgid = G_("expected %<try%>");
28641 break;
28642 case RT_CATCH:
28643 gmsgid = G_("expected %<catch%>");
28644 break;
28645 case RT_THROW:
28646 gmsgid = G_("expected %<throw%>");
28647 break;
28648 case RT_LABEL:
28649 gmsgid = G_("expected %<__label__%>");
28650 break;
28651 case RT_AT_TRY:
28652 gmsgid = G_("expected %<@try%>");
28653 break;
28654 case RT_AT_SYNCHRONIZED:
28655 gmsgid = G_("expected %<@synchronized%>");
28656 break;
28657 case RT_AT_THROW:
28658 gmsgid = G_("expected %<@throw%>");
28659 break;
28660 case RT_TRANSACTION_ATOMIC:
28661 gmsgid = G_("expected %<__transaction_atomic%>");
28662 break;
28663 case RT_TRANSACTION_RELAXED:
28664 gmsgid = G_("expected %<__transaction_relaxed%>");
28665 break;
28666 default:
28667 break;
28668 }
28669
28670 if (!gmsgid && !keyword)
28671 {
28672 switch (token_desc)
28673 {
28674 case RT_SEMICOLON:
28675 gmsgid = G_("expected %<;%>");
28676 break;
28677 case RT_OPEN_PAREN:
28678 gmsgid = G_("expected %<(%>");
28679 break;
28680 case RT_CLOSE_BRACE:
28681 gmsgid = G_("expected %<}%>");
28682 break;
28683 case RT_OPEN_BRACE:
28684 gmsgid = G_("expected %<{%>");
28685 break;
28686 case RT_CLOSE_SQUARE:
28687 gmsgid = G_("expected %<]%>");
28688 break;
28689 case RT_OPEN_SQUARE:
28690 gmsgid = G_("expected %<[%>");
28691 break;
28692 case RT_COMMA:
28693 gmsgid = G_("expected %<,%>");
28694 break;
28695 case RT_SCOPE:
28696 gmsgid = G_("expected %<::%>");
28697 break;
28698 case RT_LESS:
28699 gmsgid = G_("expected %<<%>");
28700 break;
28701 case RT_GREATER:
28702 gmsgid = G_("expected %<>%>");
28703 break;
28704 case RT_EQ:
28705 gmsgid = G_("expected %<=%>");
28706 break;
28707 case RT_ELLIPSIS:
28708 gmsgid = G_("expected %<...%>");
28709 break;
28710 case RT_MULT:
28711 gmsgid = G_("expected %<*%>");
28712 break;
28713 case RT_COMPL:
28714 gmsgid = G_("expected %<~%>");
28715 break;
28716 case RT_COLON:
28717 gmsgid = G_("expected %<:%>");
28718 break;
28719 case RT_COLON_SCOPE:
28720 gmsgid = G_("expected %<:%> or %<::%>");
28721 break;
28722 case RT_CLOSE_PAREN:
28723 gmsgid = G_("expected %<)%>");
28724 break;
28725 case RT_COMMA_CLOSE_PAREN:
28726 gmsgid = G_("expected %<,%> or %<)%>");
28727 break;
28728 case RT_PRAGMA_EOL:
28729 gmsgid = G_("expected end of line");
28730 break;
28731 case RT_NAME:
28732 gmsgid = G_("expected identifier");
28733 break;
28734 case RT_SELECT:
28735 gmsgid = G_("expected selection-statement");
28736 break;
28737 case RT_ITERATION:
28738 gmsgid = G_("expected iteration-statement");
28739 break;
28740 case RT_JUMP:
28741 gmsgid = G_("expected jump-statement");
28742 break;
28743 case RT_CLASS_KEY:
28744 gmsgid = G_("expected class-key");
28745 break;
28746 case RT_CLASS_TYPENAME_TEMPLATE:
28747 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28748 break;
28749 default:
28750 gcc_unreachable ();
28751 }
28752 }
28753
28754 if (gmsgid)
28755 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28756 }
28757
28758
28759 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28760 issue an error message indicating that TOKEN_DESC was expected.
28761
28762 Returns the token consumed, if the token had the appropriate type.
28763 Otherwise, returns NULL.
28764
28765 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28766 within any error as the location of an "opening" token matching
28767 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28768 RT_CLOSE_PAREN). */
28769
28770 static cp_token *
28771 cp_parser_require (cp_parser* parser,
28772 enum cpp_ttype type,
28773 required_token token_desc,
28774 location_t matching_location)
28775 {
28776 if (cp_lexer_next_token_is (parser->lexer, type))
28777 return cp_lexer_consume_token (parser->lexer);
28778 else
28779 {
28780 /* Output the MESSAGE -- unless we're parsing tentatively. */
28781 if (!cp_parser_simulate_error (parser))
28782 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28783 matching_location);
28784 return NULL;
28785 }
28786 }
28787
28788 /* An error message is produced if the next token is not '>'.
28789 All further tokens are skipped until the desired token is
28790 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28791
28792 static void
28793 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28794 {
28795 /* Current level of '< ... >'. */
28796 unsigned level = 0;
28797 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28798 unsigned nesting_depth = 0;
28799
28800 /* Are we ready, yet? If not, issue error message. */
28801 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28802 return;
28803
28804 /* Skip tokens until the desired token is found. */
28805 while (true)
28806 {
28807 /* Peek at the next token. */
28808 switch (cp_lexer_peek_token (parser->lexer)->type)
28809 {
28810 case CPP_LESS:
28811 if (!nesting_depth)
28812 ++level;
28813 break;
28814
28815 case CPP_RSHIFT:
28816 if (cxx_dialect == cxx98)
28817 /* C++0x views the `>>' operator as two `>' tokens, but
28818 C++98 does not. */
28819 break;
28820 else if (!nesting_depth && level-- == 0)
28821 {
28822 /* We've hit a `>>' where the first `>' closes the
28823 template argument list, and the second `>' is
28824 spurious. Just consume the `>>' and stop; we've
28825 already produced at least one error. */
28826 cp_lexer_consume_token (parser->lexer);
28827 return;
28828 }
28829 /* Fall through for C++0x, so we handle the second `>' in
28830 the `>>'. */
28831 gcc_fallthrough ();
28832
28833 case CPP_GREATER:
28834 if (!nesting_depth && level-- == 0)
28835 {
28836 /* We've reached the token we want, consume it and stop. */
28837 cp_lexer_consume_token (parser->lexer);
28838 return;
28839 }
28840 break;
28841
28842 case CPP_OPEN_PAREN:
28843 case CPP_OPEN_SQUARE:
28844 ++nesting_depth;
28845 break;
28846
28847 case CPP_CLOSE_PAREN:
28848 case CPP_CLOSE_SQUARE:
28849 if (nesting_depth-- == 0)
28850 return;
28851 break;
28852
28853 case CPP_EOF:
28854 case CPP_PRAGMA_EOL:
28855 case CPP_SEMICOLON:
28856 case CPP_OPEN_BRACE:
28857 case CPP_CLOSE_BRACE:
28858 /* The '>' was probably forgotten, don't look further. */
28859 return;
28860
28861 default:
28862 break;
28863 }
28864
28865 /* Consume this token. */
28866 cp_lexer_consume_token (parser->lexer);
28867 }
28868 }
28869
28870 /* If the next token is the indicated keyword, consume it. Otherwise,
28871 issue an error message indicating that TOKEN_DESC was expected.
28872
28873 Returns the token consumed, if the token had the appropriate type.
28874 Otherwise, returns NULL. */
28875
28876 static cp_token *
28877 cp_parser_require_keyword (cp_parser* parser,
28878 enum rid keyword,
28879 required_token token_desc)
28880 {
28881 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28882
28883 if (token && token->keyword != keyword)
28884 {
28885 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28886 UNKNOWN_LOCATION);
28887 return NULL;
28888 }
28889
28890 return token;
28891 }
28892
28893 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28894 function-definition. */
28895
28896 static bool
28897 cp_parser_token_starts_function_definition_p (cp_token* token)
28898 {
28899 return (/* An ordinary function-body begins with an `{'. */
28900 token->type == CPP_OPEN_BRACE
28901 /* A ctor-initializer begins with a `:'. */
28902 || token->type == CPP_COLON
28903 /* A function-try-block begins with `try'. */
28904 || token->keyword == RID_TRY
28905 /* A function-transaction-block begins with `__transaction_atomic'
28906 or `__transaction_relaxed'. */
28907 || token->keyword == RID_TRANSACTION_ATOMIC
28908 || token->keyword == RID_TRANSACTION_RELAXED
28909 /* The named return value extension begins with `return'. */
28910 || token->keyword == RID_RETURN);
28911 }
28912
28913 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28914 definition. */
28915
28916 static bool
28917 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28918 {
28919 cp_token *token;
28920
28921 token = cp_lexer_peek_token (parser->lexer);
28922 return (token->type == CPP_OPEN_BRACE
28923 || (token->type == CPP_COLON
28924 && !parser->colon_doesnt_start_class_def_p));
28925 }
28926
28927 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28928 C++0x) ending a template-argument. */
28929
28930 static bool
28931 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28932 {
28933 cp_token *token;
28934
28935 token = cp_lexer_peek_token (parser->lexer);
28936 return (token->type == CPP_COMMA
28937 || token->type == CPP_GREATER
28938 || token->type == CPP_ELLIPSIS
28939 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28940 }
28941
28942 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28943 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28944
28945 static bool
28946 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28947 size_t n)
28948 {
28949 cp_token *token;
28950
28951 token = cp_lexer_peek_nth_token (parser->lexer, n);
28952 if (token->type == CPP_LESS)
28953 return true;
28954 /* Check for the sequence `<::' in the original code. It would be lexed as
28955 `[:', where `[' is a digraph, and there is no whitespace before
28956 `:'. */
28957 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28958 {
28959 cp_token *token2;
28960 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28961 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28962 return true;
28963 }
28964 return false;
28965 }
28966
28967 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28968 or none_type otherwise. */
28969
28970 static enum tag_types
28971 cp_parser_token_is_class_key (cp_token* token)
28972 {
28973 switch (token->keyword)
28974 {
28975 case RID_CLASS:
28976 return class_type;
28977 case RID_STRUCT:
28978 return record_type;
28979 case RID_UNION:
28980 return union_type;
28981
28982 default:
28983 return none_type;
28984 }
28985 }
28986
28987 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28988 or none_type otherwise or if the token is null. */
28989
28990 static enum tag_types
28991 cp_parser_token_is_type_parameter_key (cp_token* token)
28992 {
28993 if (!token)
28994 return none_type;
28995
28996 switch (token->keyword)
28997 {
28998 case RID_CLASS:
28999 return class_type;
29000 case RID_TYPENAME:
29001 return typename_type;
29002
29003 default:
29004 return none_type;
29005 }
29006 }
29007
29008 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29009
29010 static void
29011 cp_parser_check_class_key (enum tag_types class_key, tree type)
29012 {
29013 if (type == error_mark_node)
29014 return;
29015 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29016 {
29017 if (permerror (input_location, "%qs tag used in naming %q#T",
29018 class_key == union_type ? "union"
29019 : class_key == record_type ? "struct" : "class",
29020 type))
29021 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29022 "%q#T was previously declared here", type);
29023 }
29024 }
29025
29026 /* Issue an error message if DECL is redeclared with different
29027 access than its original declaration [class.access.spec/3].
29028 This applies to nested classes, nested class templates and
29029 enumerations [class.mem/1]. */
29030
29031 static void
29032 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29033 {
29034 if (!decl
29035 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29036 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29037 return;
29038
29039 if ((TREE_PRIVATE (decl)
29040 != (current_access_specifier == access_private_node))
29041 || (TREE_PROTECTED (decl)
29042 != (current_access_specifier == access_protected_node)))
29043 error_at (location, "%qD redeclared with different access", decl);
29044 }
29045
29046 /* Look for the `template' keyword, as a syntactic disambiguator.
29047 Return TRUE iff it is present, in which case it will be
29048 consumed. */
29049
29050 static bool
29051 cp_parser_optional_template_keyword (cp_parser *parser)
29052 {
29053 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29054 {
29055 /* In C++98 the `template' keyword can only be used within templates;
29056 outside templates the parser can always figure out what is a
29057 template and what is not. In C++11, per the resolution of DR 468,
29058 `template' is allowed in cases where it is not strictly necessary. */
29059 if (!processing_template_decl
29060 && pedantic && cxx_dialect == cxx98)
29061 {
29062 cp_token *token = cp_lexer_peek_token (parser->lexer);
29063 pedwarn (token->location, OPT_Wpedantic,
29064 "in C++98 %<template%> (as a disambiguator) is only "
29065 "allowed within templates");
29066 /* If this part of the token stream is rescanned, the same
29067 error message would be generated. So, we purge the token
29068 from the stream. */
29069 cp_lexer_purge_token (parser->lexer);
29070 return false;
29071 }
29072 else
29073 {
29074 /* Consume the `template' keyword. */
29075 cp_lexer_consume_token (parser->lexer);
29076 return true;
29077 }
29078 }
29079 return false;
29080 }
29081
29082 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29083 set PARSER->SCOPE, and perform other related actions. */
29084
29085 static void
29086 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29087 {
29088 struct tree_check *check_value;
29089
29090 /* Get the stored value. */
29091 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29092 /* Set the scope from the stored value. */
29093 parser->scope = saved_checks_value (check_value);
29094 parser->qualifying_scope = check_value->qualifying_scope;
29095 parser->object_scope = NULL_TREE;
29096 }
29097
29098 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29099 encounter the end of a block before what we were looking for. */
29100
29101 static bool
29102 cp_parser_cache_group (cp_parser *parser,
29103 enum cpp_ttype end,
29104 unsigned depth)
29105 {
29106 while (true)
29107 {
29108 cp_token *token = cp_lexer_peek_token (parser->lexer);
29109
29110 /* Abort a parenthesized expression if we encounter a semicolon. */
29111 if ((end == CPP_CLOSE_PAREN || depth == 0)
29112 && token->type == CPP_SEMICOLON)
29113 return true;
29114 /* If we've reached the end of the file, stop. */
29115 if (token->type == CPP_EOF
29116 || (end != CPP_PRAGMA_EOL
29117 && token->type == CPP_PRAGMA_EOL))
29118 return true;
29119 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29120 /* We've hit the end of an enclosing block, so there's been some
29121 kind of syntax error. */
29122 return true;
29123
29124 /* Consume the token. */
29125 cp_lexer_consume_token (parser->lexer);
29126 /* See if it starts a new group. */
29127 if (token->type == CPP_OPEN_BRACE)
29128 {
29129 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29130 /* In theory this should probably check end == '}', but
29131 cp_parser_save_member_function_body needs it to exit
29132 after either '}' or ')' when called with ')'. */
29133 if (depth == 0)
29134 return false;
29135 }
29136 else if (token->type == CPP_OPEN_PAREN)
29137 {
29138 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29139 if (depth == 0 && end == CPP_CLOSE_PAREN)
29140 return false;
29141 }
29142 else if (token->type == CPP_PRAGMA)
29143 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29144 else if (token->type == end)
29145 return false;
29146 }
29147 }
29148
29149 /* Like above, for caching a default argument or NSDMI. Both of these are
29150 terminated by a non-nested comma, but it can be unclear whether or not a
29151 comma is nested in a template argument list unless we do more parsing.
29152 In order to handle this ambiguity, when we encounter a ',' after a '<'
29153 we try to parse what follows as a parameter-declaration-list (in the
29154 case of a default argument) or a member-declarator (in the case of an
29155 NSDMI). If that succeeds, then we stop caching. */
29156
29157 static tree
29158 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29159 {
29160 unsigned depth = 0;
29161 int maybe_template_id = 0;
29162 cp_token *first_token;
29163 cp_token *token;
29164 tree default_argument;
29165
29166 /* Add tokens until we have processed the entire default
29167 argument. We add the range [first_token, token). */
29168 first_token = cp_lexer_peek_token (parser->lexer);
29169 if (first_token->type == CPP_OPEN_BRACE)
29170 {
29171 /* For list-initialization, this is straightforward. */
29172 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29173 token = cp_lexer_peek_token (parser->lexer);
29174 }
29175 else while (true)
29176 {
29177 bool done = false;
29178
29179 /* Peek at the next token. */
29180 token = cp_lexer_peek_token (parser->lexer);
29181 /* What we do depends on what token we have. */
29182 switch (token->type)
29183 {
29184 /* In valid code, a default argument must be
29185 immediately followed by a `,' `)', or `...'. */
29186 case CPP_COMMA:
29187 if (depth == 0 && maybe_template_id)
29188 {
29189 /* If we've seen a '<', we might be in a
29190 template-argument-list. Until Core issue 325 is
29191 resolved, we don't know how this situation ought
29192 to be handled, so try to DTRT. We check whether
29193 what comes after the comma is a valid parameter
29194 declaration list. If it is, then the comma ends
29195 the default argument; otherwise the default
29196 argument continues. */
29197 bool error = false;
29198 cp_token *peek;
29199
29200 /* Set ITALP so cp_parser_parameter_declaration_list
29201 doesn't decide to commit to this parse. */
29202 bool saved_italp = parser->in_template_argument_list_p;
29203 parser->in_template_argument_list_p = true;
29204
29205 cp_parser_parse_tentatively (parser);
29206
29207 if (nsdmi)
29208 {
29209 /* Parse declarators until we reach a non-comma or
29210 somthing that cannot be an initializer.
29211 Just checking whether we're looking at a single
29212 declarator is insufficient. Consider:
29213 int var = tuple<T,U>::x;
29214 The template parameter 'U' looks exactly like a
29215 declarator. */
29216 do
29217 {
29218 int ctor_dtor_or_conv_p;
29219 cp_lexer_consume_token (parser->lexer);
29220 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29221 &ctor_dtor_or_conv_p,
29222 /*parenthesized_p=*/NULL,
29223 /*member_p=*/true,
29224 /*friend_p=*/false);
29225 peek = cp_lexer_peek_token (parser->lexer);
29226 if (cp_parser_error_occurred (parser))
29227 break;
29228 }
29229 while (peek->type == CPP_COMMA);
29230 /* If we met an '=' or ';' then the original comma
29231 was the end of the NSDMI. Otherwise assume
29232 we're still in the NSDMI. */
29233 error = (peek->type != CPP_EQ
29234 && peek->type != CPP_SEMICOLON);
29235 }
29236 else
29237 {
29238 cp_lexer_consume_token (parser->lexer);
29239 begin_scope (sk_function_parms, NULL_TREE);
29240 if (cp_parser_parameter_declaration_list (parser)
29241 == error_mark_node)
29242 error = true;
29243 pop_bindings_and_leave_scope ();
29244 }
29245 if (!cp_parser_error_occurred (parser) && !error)
29246 done = true;
29247 cp_parser_abort_tentative_parse (parser);
29248
29249 parser->in_template_argument_list_p = saved_italp;
29250 break;
29251 }
29252 /* FALLTHRU */
29253 case CPP_CLOSE_PAREN:
29254 case CPP_ELLIPSIS:
29255 /* If we run into a non-nested `;', `}', or `]',
29256 then the code is invalid -- but the default
29257 argument is certainly over. */
29258 case CPP_SEMICOLON:
29259 case CPP_CLOSE_BRACE:
29260 case CPP_CLOSE_SQUARE:
29261 if (depth == 0
29262 /* Handle correctly int n = sizeof ... ( p ); */
29263 && token->type != CPP_ELLIPSIS)
29264 done = true;
29265 /* Update DEPTH, if necessary. */
29266 else if (token->type == CPP_CLOSE_PAREN
29267 || token->type == CPP_CLOSE_BRACE
29268 || token->type == CPP_CLOSE_SQUARE)
29269 --depth;
29270 break;
29271
29272 case CPP_OPEN_PAREN:
29273 case CPP_OPEN_SQUARE:
29274 case CPP_OPEN_BRACE:
29275 ++depth;
29276 break;
29277
29278 case CPP_LESS:
29279 if (depth == 0)
29280 /* This might be the comparison operator, or it might
29281 start a template argument list. */
29282 ++maybe_template_id;
29283 break;
29284
29285 case CPP_RSHIFT:
29286 if (cxx_dialect == cxx98)
29287 break;
29288 /* Fall through for C++0x, which treats the `>>'
29289 operator like two `>' tokens in certain
29290 cases. */
29291 gcc_fallthrough ();
29292
29293 case CPP_GREATER:
29294 if (depth == 0)
29295 {
29296 /* This might be an operator, or it might close a
29297 template argument list. But if a previous '<'
29298 started a template argument list, this will have
29299 closed it, so we can't be in one anymore. */
29300 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29301 if (maybe_template_id < 0)
29302 maybe_template_id = 0;
29303 }
29304 break;
29305
29306 /* If we run out of tokens, issue an error message. */
29307 case CPP_EOF:
29308 case CPP_PRAGMA_EOL:
29309 error_at (token->location, "file ends in default argument");
29310 return error_mark_node;
29311
29312 case CPP_NAME:
29313 case CPP_SCOPE:
29314 /* In these cases, we should look for template-ids.
29315 For example, if the default argument is
29316 `X<int, double>()', we need to do name lookup to
29317 figure out whether or not `X' is a template; if
29318 so, the `,' does not end the default argument.
29319
29320 That is not yet done. */
29321 break;
29322
29323 default:
29324 break;
29325 }
29326
29327 /* If we've reached the end, stop. */
29328 if (done)
29329 break;
29330
29331 /* Add the token to the token block. */
29332 token = cp_lexer_consume_token (parser->lexer);
29333 }
29334
29335 /* Create a DEFAULT_ARG to represent the unparsed default
29336 argument. */
29337 default_argument = make_node (DEFAULT_ARG);
29338 DEFARG_TOKENS (default_argument)
29339 = cp_token_cache_new (first_token, token);
29340 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29341
29342 return default_argument;
29343 }
29344
29345 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29346
29347 location_t
29348 defarg_location (tree default_argument)
29349 {
29350 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29351 location_t start = tokens->first->location;
29352 location_t end = tokens->last->location;
29353 return make_location (start, start, end);
29354 }
29355
29356 /* Begin parsing tentatively. We always save tokens while parsing
29357 tentatively so that if the tentative parsing fails we can restore the
29358 tokens. */
29359
29360 static void
29361 cp_parser_parse_tentatively (cp_parser* parser)
29362 {
29363 /* Enter a new parsing context. */
29364 parser->context = cp_parser_context_new (parser->context);
29365 /* Begin saving tokens. */
29366 cp_lexer_save_tokens (parser->lexer);
29367 /* In order to avoid repetitive access control error messages,
29368 access checks are queued up until we are no longer parsing
29369 tentatively. */
29370 push_deferring_access_checks (dk_deferred);
29371 }
29372
29373 /* Commit to the currently active tentative parse. */
29374
29375 static void
29376 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29377 {
29378 cp_parser_context *context;
29379 cp_lexer *lexer;
29380
29381 /* Mark all of the levels as committed. */
29382 lexer = parser->lexer;
29383 for (context = parser->context; context->next; context = context->next)
29384 {
29385 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29386 break;
29387 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29388 while (!cp_lexer_saving_tokens (lexer))
29389 lexer = lexer->next;
29390 cp_lexer_commit_tokens (lexer);
29391 }
29392 }
29393
29394 /* Commit to the topmost currently active tentative parse.
29395
29396 Note that this function shouldn't be called when there are
29397 irreversible side-effects while in a tentative state. For
29398 example, we shouldn't create a permanent entry in the symbol
29399 table, or issue an error message that might not apply if the
29400 tentative parse is aborted. */
29401
29402 static void
29403 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29404 {
29405 cp_parser_context *context = parser->context;
29406 cp_lexer *lexer = parser->lexer;
29407
29408 if (context)
29409 {
29410 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29411 return;
29412 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29413
29414 while (!cp_lexer_saving_tokens (lexer))
29415 lexer = lexer->next;
29416 cp_lexer_commit_tokens (lexer);
29417 }
29418 }
29419
29420 /* Abort the currently active tentative parse. All consumed tokens
29421 will be rolled back, and no diagnostics will be issued. */
29422
29423 static void
29424 cp_parser_abort_tentative_parse (cp_parser* parser)
29425 {
29426 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29427 || errorcount > 0);
29428 cp_parser_simulate_error (parser);
29429 /* Now, pretend that we want to see if the construct was
29430 successfully parsed. */
29431 cp_parser_parse_definitely (parser);
29432 }
29433
29434 /* Stop parsing tentatively. If a parse error has occurred, restore the
29435 token stream. Otherwise, commit to the tokens we have consumed.
29436 Returns true if no error occurred; false otherwise. */
29437
29438 static bool
29439 cp_parser_parse_definitely (cp_parser* parser)
29440 {
29441 bool error_occurred;
29442 cp_parser_context *context;
29443
29444 /* Remember whether or not an error occurred, since we are about to
29445 destroy that information. */
29446 error_occurred = cp_parser_error_occurred (parser);
29447 /* Remove the topmost context from the stack. */
29448 context = parser->context;
29449 parser->context = context->next;
29450 /* If no parse errors occurred, commit to the tentative parse. */
29451 if (!error_occurred)
29452 {
29453 /* Commit to the tokens read tentatively, unless that was
29454 already done. */
29455 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29456 cp_lexer_commit_tokens (parser->lexer);
29457
29458 pop_to_parent_deferring_access_checks ();
29459 }
29460 /* Otherwise, if errors occurred, roll back our state so that things
29461 are just as they were before we began the tentative parse. */
29462 else
29463 {
29464 cp_lexer_rollback_tokens (parser->lexer);
29465 pop_deferring_access_checks ();
29466 }
29467 /* Add the context to the front of the free list. */
29468 context->next = cp_parser_context_free_list;
29469 cp_parser_context_free_list = context;
29470
29471 return !error_occurred;
29472 }
29473
29474 /* Returns true if we are parsing tentatively and are not committed to
29475 this tentative parse. */
29476
29477 static bool
29478 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29479 {
29480 return (cp_parser_parsing_tentatively (parser)
29481 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29482 }
29483
29484 /* Returns nonzero iff an error has occurred during the most recent
29485 tentative parse. */
29486
29487 static bool
29488 cp_parser_error_occurred (cp_parser* parser)
29489 {
29490 return (cp_parser_parsing_tentatively (parser)
29491 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29492 }
29493
29494 /* Returns nonzero if GNU extensions are allowed. */
29495
29496 static bool
29497 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29498 {
29499 return parser->allow_gnu_extensions_p;
29500 }
29501 \f
29502 /* Objective-C++ Productions */
29503
29504
29505 /* Parse an Objective-C expression, which feeds into a primary-expression
29506 above.
29507
29508 objc-expression:
29509 objc-message-expression
29510 objc-string-literal
29511 objc-encode-expression
29512 objc-protocol-expression
29513 objc-selector-expression
29514
29515 Returns a tree representation of the expression. */
29516
29517 static cp_expr
29518 cp_parser_objc_expression (cp_parser* parser)
29519 {
29520 /* Try to figure out what kind of declaration is present. */
29521 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29522
29523 switch (kwd->type)
29524 {
29525 case CPP_OPEN_SQUARE:
29526 return cp_parser_objc_message_expression (parser);
29527
29528 case CPP_OBJC_STRING:
29529 kwd = cp_lexer_consume_token (parser->lexer);
29530 return objc_build_string_object (kwd->u.value);
29531
29532 case CPP_KEYWORD:
29533 switch (kwd->keyword)
29534 {
29535 case RID_AT_ENCODE:
29536 return cp_parser_objc_encode_expression (parser);
29537
29538 case RID_AT_PROTOCOL:
29539 return cp_parser_objc_protocol_expression (parser);
29540
29541 case RID_AT_SELECTOR:
29542 return cp_parser_objc_selector_expression (parser);
29543
29544 default:
29545 break;
29546 }
29547 /* FALLTHRU */
29548 default:
29549 error_at (kwd->location,
29550 "misplaced %<@%D%> Objective-C++ construct",
29551 kwd->u.value);
29552 cp_parser_skip_to_end_of_block_or_statement (parser);
29553 }
29554
29555 return error_mark_node;
29556 }
29557
29558 /* Parse an Objective-C message expression.
29559
29560 objc-message-expression:
29561 [ objc-message-receiver objc-message-args ]
29562
29563 Returns a representation of an Objective-C message. */
29564
29565 static tree
29566 cp_parser_objc_message_expression (cp_parser* parser)
29567 {
29568 tree receiver, messageargs;
29569
29570 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29571 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29572 receiver = cp_parser_objc_message_receiver (parser);
29573 messageargs = cp_parser_objc_message_args (parser);
29574 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29575 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29576
29577 tree result = objc_build_message_expr (receiver, messageargs);
29578
29579 /* Construct a location e.g.
29580 [self func1:5]
29581 ^~~~~~~~~~~~~~
29582 ranging from the '[' to the ']', with the caret at the start. */
29583 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29584 protected_set_expr_location (result, combined_loc);
29585
29586 return result;
29587 }
29588
29589 /* Parse an objc-message-receiver.
29590
29591 objc-message-receiver:
29592 expression
29593 simple-type-specifier
29594
29595 Returns a representation of the type or expression. */
29596
29597 static tree
29598 cp_parser_objc_message_receiver (cp_parser* parser)
29599 {
29600 tree rcv;
29601
29602 /* An Objective-C message receiver may be either (1) a type
29603 or (2) an expression. */
29604 cp_parser_parse_tentatively (parser);
29605 rcv = cp_parser_expression (parser);
29606
29607 /* If that worked out, fine. */
29608 if (cp_parser_parse_definitely (parser))
29609 return rcv;
29610
29611 cp_parser_parse_tentatively (parser);
29612 rcv = cp_parser_simple_type_specifier (parser,
29613 /*decl_specs=*/NULL,
29614 CP_PARSER_FLAGS_NONE);
29615
29616 if (cp_parser_parse_definitely (parser))
29617 return objc_get_class_reference (rcv);
29618
29619 cp_parser_error (parser, "objective-c++ message receiver expected");
29620 return error_mark_node;
29621 }
29622
29623 /* Parse the arguments and selectors comprising an Objective-C message.
29624
29625 objc-message-args:
29626 objc-selector
29627 objc-selector-args
29628 objc-selector-args , objc-comma-args
29629
29630 objc-selector-args:
29631 objc-selector [opt] : assignment-expression
29632 objc-selector-args objc-selector [opt] : assignment-expression
29633
29634 objc-comma-args:
29635 assignment-expression
29636 objc-comma-args , assignment-expression
29637
29638 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29639 selector arguments and TREE_VALUE containing a list of comma
29640 arguments. */
29641
29642 static tree
29643 cp_parser_objc_message_args (cp_parser* parser)
29644 {
29645 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29646 bool maybe_unary_selector_p = true;
29647 cp_token *token = cp_lexer_peek_token (parser->lexer);
29648
29649 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29650 {
29651 tree selector = NULL_TREE, arg;
29652
29653 if (token->type != CPP_COLON)
29654 selector = cp_parser_objc_selector (parser);
29655
29656 /* Detect if we have a unary selector. */
29657 if (maybe_unary_selector_p
29658 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29659 return build_tree_list (selector, NULL_TREE);
29660
29661 maybe_unary_selector_p = false;
29662 cp_parser_require (parser, CPP_COLON, RT_COLON);
29663 arg = cp_parser_assignment_expression (parser);
29664
29665 sel_args
29666 = chainon (sel_args,
29667 build_tree_list (selector, arg));
29668
29669 token = cp_lexer_peek_token (parser->lexer);
29670 }
29671
29672 /* Handle non-selector arguments, if any. */
29673 while (token->type == CPP_COMMA)
29674 {
29675 tree arg;
29676
29677 cp_lexer_consume_token (parser->lexer);
29678 arg = cp_parser_assignment_expression (parser);
29679
29680 addl_args
29681 = chainon (addl_args,
29682 build_tree_list (NULL_TREE, arg));
29683
29684 token = cp_lexer_peek_token (parser->lexer);
29685 }
29686
29687 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29688 {
29689 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29690 return build_tree_list (error_mark_node, error_mark_node);
29691 }
29692
29693 return build_tree_list (sel_args, addl_args);
29694 }
29695
29696 /* Parse an Objective-C encode expression.
29697
29698 objc-encode-expression:
29699 @encode objc-typename
29700
29701 Returns an encoded representation of the type argument. */
29702
29703 static cp_expr
29704 cp_parser_objc_encode_expression (cp_parser* parser)
29705 {
29706 tree type;
29707 cp_token *token;
29708 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29709
29710 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29711 matching_parens parens;
29712 parens.require_open (parser);
29713 token = cp_lexer_peek_token (parser->lexer);
29714 type = complete_type (cp_parser_type_id (parser));
29715 parens.require_close (parser);
29716
29717 if (!type)
29718 {
29719 error_at (token->location,
29720 "%<@encode%> must specify a type as an argument");
29721 return error_mark_node;
29722 }
29723
29724 /* This happens if we find @encode(T) (where T is a template
29725 typename or something dependent on a template typename) when
29726 parsing a template. In that case, we can't compile it
29727 immediately, but we rather create an AT_ENCODE_EXPR which will
29728 need to be instantiated when the template is used.
29729 */
29730 if (dependent_type_p (type))
29731 {
29732 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29733 TREE_READONLY (value) = 1;
29734 return value;
29735 }
29736
29737
29738 /* Build a location of the form:
29739 @encode(int)
29740 ^~~~~~~~~~~~
29741 with caret==start at the @ token, finishing at the close paren. */
29742 location_t combined_loc
29743 = make_location (start_loc, start_loc,
29744 cp_lexer_previous_token (parser->lexer)->location);
29745
29746 return cp_expr (objc_build_encode_expr (type), combined_loc);
29747 }
29748
29749 /* Parse an Objective-C @defs expression. */
29750
29751 static tree
29752 cp_parser_objc_defs_expression (cp_parser *parser)
29753 {
29754 tree name;
29755
29756 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29757 matching_parens parens;
29758 parens.require_open (parser);
29759 name = cp_parser_identifier (parser);
29760 parens.require_close (parser);
29761
29762 return objc_get_class_ivars (name);
29763 }
29764
29765 /* Parse an Objective-C protocol expression.
29766
29767 objc-protocol-expression:
29768 @protocol ( identifier )
29769
29770 Returns a representation of the protocol expression. */
29771
29772 static tree
29773 cp_parser_objc_protocol_expression (cp_parser* parser)
29774 {
29775 tree proto;
29776 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29777
29778 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29779 matching_parens parens;
29780 parens.require_open (parser);
29781 proto = cp_parser_identifier (parser);
29782 parens.require_close (parser);
29783
29784 /* Build a location of the form:
29785 @protocol(prot)
29786 ^~~~~~~~~~~~~~~
29787 with caret==start at the @ token, finishing at the close paren. */
29788 location_t combined_loc
29789 = make_location (start_loc, start_loc,
29790 cp_lexer_previous_token (parser->lexer)->location);
29791 tree result = objc_build_protocol_expr (proto);
29792 protected_set_expr_location (result, combined_loc);
29793 return result;
29794 }
29795
29796 /* Parse an Objective-C selector expression.
29797
29798 objc-selector-expression:
29799 @selector ( objc-method-signature )
29800
29801 objc-method-signature:
29802 objc-selector
29803 objc-selector-seq
29804
29805 objc-selector-seq:
29806 objc-selector :
29807 objc-selector-seq objc-selector :
29808
29809 Returns a representation of the method selector. */
29810
29811 static tree
29812 cp_parser_objc_selector_expression (cp_parser* parser)
29813 {
29814 tree sel_seq = NULL_TREE;
29815 bool maybe_unary_selector_p = true;
29816 cp_token *token;
29817 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29818
29819 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29820 matching_parens parens;
29821 parens.require_open (parser);
29822 token = cp_lexer_peek_token (parser->lexer);
29823
29824 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29825 || token->type == CPP_SCOPE)
29826 {
29827 tree selector = NULL_TREE;
29828
29829 if (token->type != CPP_COLON
29830 || token->type == CPP_SCOPE)
29831 selector = cp_parser_objc_selector (parser);
29832
29833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29834 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29835 {
29836 /* Detect if we have a unary selector. */
29837 if (maybe_unary_selector_p)
29838 {
29839 sel_seq = selector;
29840 goto finish_selector;
29841 }
29842 else
29843 {
29844 cp_parser_error (parser, "expected %<:%>");
29845 }
29846 }
29847 maybe_unary_selector_p = false;
29848 token = cp_lexer_consume_token (parser->lexer);
29849
29850 if (token->type == CPP_SCOPE)
29851 {
29852 sel_seq
29853 = chainon (sel_seq,
29854 build_tree_list (selector, NULL_TREE));
29855 sel_seq
29856 = chainon (sel_seq,
29857 build_tree_list (NULL_TREE, NULL_TREE));
29858 }
29859 else
29860 sel_seq
29861 = chainon (sel_seq,
29862 build_tree_list (selector, NULL_TREE));
29863
29864 token = cp_lexer_peek_token (parser->lexer);
29865 }
29866
29867 finish_selector:
29868 parens.require_close (parser);
29869
29870
29871 /* Build a location of the form:
29872 @selector(func)
29873 ^~~~~~~~~~~~~~~
29874 with caret==start at the @ token, finishing at the close paren. */
29875 location_t combined_loc
29876 = make_location (loc, loc,
29877 cp_lexer_previous_token (parser->lexer)->location);
29878 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29879 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29880 protected_set_expr_location (result, combined_loc);
29881 return result;
29882 }
29883
29884 /* Parse a list of identifiers.
29885
29886 objc-identifier-list:
29887 identifier
29888 objc-identifier-list , identifier
29889
29890 Returns a TREE_LIST of identifier nodes. */
29891
29892 static tree
29893 cp_parser_objc_identifier_list (cp_parser* parser)
29894 {
29895 tree identifier;
29896 tree list;
29897 cp_token *sep;
29898
29899 identifier = cp_parser_identifier (parser);
29900 if (identifier == error_mark_node)
29901 return error_mark_node;
29902
29903 list = build_tree_list (NULL_TREE, identifier);
29904 sep = cp_lexer_peek_token (parser->lexer);
29905
29906 while (sep->type == CPP_COMMA)
29907 {
29908 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29909 identifier = cp_parser_identifier (parser);
29910 if (identifier == error_mark_node)
29911 return list;
29912
29913 list = chainon (list, build_tree_list (NULL_TREE,
29914 identifier));
29915 sep = cp_lexer_peek_token (parser->lexer);
29916 }
29917
29918 return list;
29919 }
29920
29921 /* Parse an Objective-C alias declaration.
29922
29923 objc-alias-declaration:
29924 @compatibility_alias identifier identifier ;
29925
29926 This function registers the alias mapping with the Objective-C front end.
29927 It returns nothing. */
29928
29929 static void
29930 cp_parser_objc_alias_declaration (cp_parser* parser)
29931 {
29932 tree alias, orig;
29933
29934 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29935 alias = cp_parser_identifier (parser);
29936 orig = cp_parser_identifier (parser);
29937 objc_declare_alias (alias, orig);
29938 cp_parser_consume_semicolon_at_end_of_statement (parser);
29939 }
29940
29941 /* Parse an Objective-C class forward-declaration.
29942
29943 objc-class-declaration:
29944 @class objc-identifier-list ;
29945
29946 The function registers the forward declarations with the Objective-C
29947 front end. It returns nothing. */
29948
29949 static void
29950 cp_parser_objc_class_declaration (cp_parser* parser)
29951 {
29952 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29953 while (true)
29954 {
29955 tree id;
29956
29957 id = cp_parser_identifier (parser);
29958 if (id == error_mark_node)
29959 break;
29960
29961 objc_declare_class (id);
29962
29963 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29964 cp_lexer_consume_token (parser->lexer);
29965 else
29966 break;
29967 }
29968 cp_parser_consume_semicolon_at_end_of_statement (parser);
29969 }
29970
29971 /* Parse a list of Objective-C protocol references.
29972
29973 objc-protocol-refs-opt:
29974 objc-protocol-refs [opt]
29975
29976 objc-protocol-refs:
29977 < objc-identifier-list >
29978
29979 Returns a TREE_LIST of identifiers, if any. */
29980
29981 static tree
29982 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29983 {
29984 tree protorefs = NULL_TREE;
29985
29986 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29987 {
29988 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29989 protorefs = cp_parser_objc_identifier_list (parser);
29990 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29991 }
29992
29993 return protorefs;
29994 }
29995
29996 /* Parse a Objective-C visibility specification. */
29997
29998 static void
29999 cp_parser_objc_visibility_spec (cp_parser* parser)
30000 {
30001 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30002
30003 switch (vis->keyword)
30004 {
30005 case RID_AT_PRIVATE:
30006 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30007 break;
30008 case RID_AT_PROTECTED:
30009 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30010 break;
30011 case RID_AT_PUBLIC:
30012 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30013 break;
30014 case RID_AT_PACKAGE:
30015 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30016 break;
30017 default:
30018 return;
30019 }
30020
30021 /* Eat '@private'/'@protected'/'@public'. */
30022 cp_lexer_consume_token (parser->lexer);
30023 }
30024
30025 /* Parse an Objective-C method type. Return 'true' if it is a class
30026 (+) method, and 'false' if it is an instance (-) method. */
30027
30028 static inline bool
30029 cp_parser_objc_method_type (cp_parser* parser)
30030 {
30031 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30032 return true;
30033 else
30034 return false;
30035 }
30036
30037 /* Parse an Objective-C protocol qualifier. */
30038
30039 static tree
30040 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30041 {
30042 tree quals = NULL_TREE, node;
30043 cp_token *token = cp_lexer_peek_token (parser->lexer);
30044
30045 node = token->u.value;
30046
30047 while (node && identifier_p (node)
30048 && (node == ridpointers [(int) RID_IN]
30049 || node == ridpointers [(int) RID_OUT]
30050 || node == ridpointers [(int) RID_INOUT]
30051 || node == ridpointers [(int) RID_BYCOPY]
30052 || node == ridpointers [(int) RID_BYREF]
30053 || node == ridpointers [(int) RID_ONEWAY]))
30054 {
30055 quals = tree_cons (NULL_TREE, node, quals);
30056 cp_lexer_consume_token (parser->lexer);
30057 token = cp_lexer_peek_token (parser->lexer);
30058 node = token->u.value;
30059 }
30060
30061 return quals;
30062 }
30063
30064 /* Parse an Objective-C typename. */
30065
30066 static tree
30067 cp_parser_objc_typename (cp_parser* parser)
30068 {
30069 tree type_name = NULL_TREE;
30070
30071 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30072 {
30073 tree proto_quals, cp_type = NULL_TREE;
30074
30075 matching_parens parens;
30076 parens.consume_open (parser); /* Eat '('. */
30077 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30078
30079 /* An ObjC type name may consist of just protocol qualifiers, in which
30080 case the type shall default to 'id'. */
30081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30082 {
30083 cp_type = cp_parser_type_id (parser);
30084
30085 /* If the type could not be parsed, an error has already
30086 been produced. For error recovery, behave as if it had
30087 not been specified, which will use the default type
30088 'id'. */
30089 if (cp_type == error_mark_node)
30090 {
30091 cp_type = NULL_TREE;
30092 /* We need to skip to the closing parenthesis as
30093 cp_parser_type_id() does not seem to do it for
30094 us. */
30095 cp_parser_skip_to_closing_parenthesis (parser,
30096 /*recovering=*/true,
30097 /*or_comma=*/false,
30098 /*consume_paren=*/false);
30099 }
30100 }
30101
30102 parens.require_close (parser);
30103 type_name = build_tree_list (proto_quals, cp_type);
30104 }
30105
30106 return type_name;
30107 }
30108
30109 /* Check to see if TYPE refers to an Objective-C selector name. */
30110
30111 static bool
30112 cp_parser_objc_selector_p (enum cpp_ttype type)
30113 {
30114 return (type == CPP_NAME || type == CPP_KEYWORD
30115 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30116 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30117 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30118 || type == CPP_XOR || type == CPP_XOR_EQ);
30119 }
30120
30121 /* Parse an Objective-C selector. */
30122
30123 static tree
30124 cp_parser_objc_selector (cp_parser* parser)
30125 {
30126 cp_token *token = cp_lexer_consume_token (parser->lexer);
30127
30128 if (!cp_parser_objc_selector_p (token->type))
30129 {
30130 error_at (token->location, "invalid Objective-C++ selector name");
30131 return error_mark_node;
30132 }
30133
30134 /* C++ operator names are allowed to appear in ObjC selectors. */
30135 switch (token->type)
30136 {
30137 case CPP_AND_AND: return get_identifier ("and");
30138 case CPP_AND_EQ: return get_identifier ("and_eq");
30139 case CPP_AND: return get_identifier ("bitand");
30140 case CPP_OR: return get_identifier ("bitor");
30141 case CPP_COMPL: return get_identifier ("compl");
30142 case CPP_NOT: return get_identifier ("not");
30143 case CPP_NOT_EQ: return get_identifier ("not_eq");
30144 case CPP_OR_OR: return get_identifier ("or");
30145 case CPP_OR_EQ: return get_identifier ("or_eq");
30146 case CPP_XOR: return get_identifier ("xor");
30147 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30148 default: return token->u.value;
30149 }
30150 }
30151
30152 /* Parse an Objective-C params list. */
30153
30154 static tree
30155 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30156 {
30157 tree params = NULL_TREE;
30158 bool maybe_unary_selector_p = true;
30159 cp_token *token = cp_lexer_peek_token (parser->lexer);
30160
30161 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30162 {
30163 tree selector = NULL_TREE, type_name, identifier;
30164 tree parm_attr = NULL_TREE;
30165
30166 if (token->keyword == RID_ATTRIBUTE)
30167 break;
30168
30169 if (token->type != CPP_COLON)
30170 selector = cp_parser_objc_selector (parser);
30171
30172 /* Detect if we have a unary selector. */
30173 if (maybe_unary_selector_p
30174 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30175 {
30176 params = selector; /* Might be followed by attributes. */
30177 break;
30178 }
30179
30180 maybe_unary_selector_p = false;
30181 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30182 {
30183 /* Something went quite wrong. There should be a colon
30184 here, but there is not. Stop parsing parameters. */
30185 break;
30186 }
30187 type_name = cp_parser_objc_typename (parser);
30188 /* New ObjC allows attributes on parameters too. */
30189 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30190 parm_attr = cp_parser_attributes_opt (parser);
30191 identifier = cp_parser_identifier (parser);
30192
30193 params
30194 = chainon (params,
30195 objc_build_keyword_decl (selector,
30196 type_name,
30197 identifier,
30198 parm_attr));
30199
30200 token = cp_lexer_peek_token (parser->lexer);
30201 }
30202
30203 if (params == NULL_TREE)
30204 {
30205 cp_parser_error (parser, "objective-c++ method declaration is expected");
30206 return error_mark_node;
30207 }
30208
30209 /* We allow tail attributes for the method. */
30210 if (token->keyword == RID_ATTRIBUTE)
30211 {
30212 *attributes = cp_parser_attributes_opt (parser);
30213 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30214 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30215 return params;
30216 cp_parser_error (parser,
30217 "method attributes must be specified at the end");
30218 return error_mark_node;
30219 }
30220
30221 if (params == NULL_TREE)
30222 {
30223 cp_parser_error (parser, "objective-c++ method declaration is expected");
30224 return error_mark_node;
30225 }
30226 return params;
30227 }
30228
30229 /* Parse the non-keyword Objective-C params. */
30230
30231 static tree
30232 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30233 tree* attributes)
30234 {
30235 tree params = make_node (TREE_LIST);
30236 cp_token *token = cp_lexer_peek_token (parser->lexer);
30237 *ellipsisp = false; /* Initially, assume no ellipsis. */
30238
30239 while (token->type == CPP_COMMA)
30240 {
30241 cp_parameter_declarator *parmdecl;
30242 tree parm;
30243
30244 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30245 token = cp_lexer_peek_token (parser->lexer);
30246
30247 if (token->type == CPP_ELLIPSIS)
30248 {
30249 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30250 *ellipsisp = true;
30251 token = cp_lexer_peek_token (parser->lexer);
30252 break;
30253 }
30254
30255 /* TODO: parse attributes for tail parameters. */
30256 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30257 parm = grokdeclarator (parmdecl->declarator,
30258 &parmdecl->decl_specifiers,
30259 PARM, /*initialized=*/0,
30260 /*attrlist=*/NULL);
30261
30262 chainon (params, build_tree_list (NULL_TREE, parm));
30263 token = cp_lexer_peek_token (parser->lexer);
30264 }
30265
30266 /* We allow tail attributes for the method. */
30267 if (token->keyword == RID_ATTRIBUTE)
30268 {
30269 if (*attributes == NULL_TREE)
30270 {
30271 *attributes = cp_parser_attributes_opt (parser);
30272 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30273 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30274 return params;
30275 }
30276 else
30277 /* We have an error, but parse the attributes, so that we can
30278 carry on. */
30279 *attributes = cp_parser_attributes_opt (parser);
30280
30281 cp_parser_error (parser,
30282 "method attributes must be specified at the end");
30283 return error_mark_node;
30284 }
30285
30286 return params;
30287 }
30288
30289 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30290
30291 static void
30292 cp_parser_objc_interstitial_code (cp_parser* parser)
30293 {
30294 cp_token *token = cp_lexer_peek_token (parser->lexer);
30295
30296 /* If the next token is `extern' and the following token is a string
30297 literal, then we have a linkage specification. */
30298 if (token->keyword == RID_EXTERN
30299 && cp_parser_is_pure_string_literal
30300 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30301 cp_parser_linkage_specification (parser);
30302 /* Handle #pragma, if any. */
30303 else if (token->type == CPP_PRAGMA)
30304 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30305 /* Allow stray semicolons. */
30306 else if (token->type == CPP_SEMICOLON)
30307 cp_lexer_consume_token (parser->lexer);
30308 /* Mark methods as optional or required, when building protocols. */
30309 else if (token->keyword == RID_AT_OPTIONAL)
30310 {
30311 cp_lexer_consume_token (parser->lexer);
30312 objc_set_method_opt (true);
30313 }
30314 else if (token->keyword == RID_AT_REQUIRED)
30315 {
30316 cp_lexer_consume_token (parser->lexer);
30317 objc_set_method_opt (false);
30318 }
30319 else if (token->keyword == RID_NAMESPACE)
30320 cp_parser_namespace_definition (parser);
30321 /* Other stray characters must generate errors. */
30322 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30323 {
30324 cp_lexer_consume_token (parser->lexer);
30325 error ("stray %qs between Objective-C++ methods",
30326 token->type == CPP_OPEN_BRACE ? "{" : "}");
30327 }
30328 /* Finally, try to parse a block-declaration, or a function-definition. */
30329 else
30330 cp_parser_block_declaration (parser, /*statement_p=*/false);
30331 }
30332
30333 /* Parse a method signature. */
30334
30335 static tree
30336 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30337 {
30338 tree rettype, kwdparms, optparms;
30339 bool ellipsis = false;
30340 bool is_class_method;
30341
30342 is_class_method = cp_parser_objc_method_type (parser);
30343 rettype = cp_parser_objc_typename (parser);
30344 *attributes = NULL_TREE;
30345 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30346 if (kwdparms == error_mark_node)
30347 return error_mark_node;
30348 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30349 if (optparms == error_mark_node)
30350 return error_mark_node;
30351
30352 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30353 }
30354
30355 static bool
30356 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30357 {
30358 tree tattr;
30359 cp_lexer_save_tokens (parser->lexer);
30360 tattr = cp_parser_attributes_opt (parser);
30361 gcc_assert (tattr) ;
30362
30363 /* If the attributes are followed by a method introducer, this is not allowed.
30364 Dump the attributes and flag the situation. */
30365 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30366 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30367 return true;
30368
30369 /* Otherwise, the attributes introduce some interstitial code, possibly so
30370 rewind to allow that check. */
30371 cp_lexer_rollback_tokens (parser->lexer);
30372 return false;
30373 }
30374
30375 /* Parse an Objective-C method prototype list. */
30376
30377 static void
30378 cp_parser_objc_method_prototype_list (cp_parser* parser)
30379 {
30380 cp_token *token = cp_lexer_peek_token (parser->lexer);
30381
30382 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30383 {
30384 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30385 {
30386 tree attributes, sig;
30387 bool is_class_method;
30388 if (token->type == CPP_PLUS)
30389 is_class_method = true;
30390 else
30391 is_class_method = false;
30392 sig = cp_parser_objc_method_signature (parser, &attributes);
30393 if (sig == error_mark_node)
30394 {
30395 cp_parser_skip_to_end_of_block_or_statement (parser);
30396 token = cp_lexer_peek_token (parser->lexer);
30397 continue;
30398 }
30399 objc_add_method_declaration (is_class_method, sig, attributes);
30400 cp_parser_consume_semicolon_at_end_of_statement (parser);
30401 }
30402 else if (token->keyword == RID_AT_PROPERTY)
30403 cp_parser_objc_at_property_declaration (parser);
30404 else if (token->keyword == RID_ATTRIBUTE
30405 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30406 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30407 OPT_Wattributes,
30408 "prefix attributes are ignored for methods");
30409 else
30410 /* Allow for interspersed non-ObjC++ code. */
30411 cp_parser_objc_interstitial_code (parser);
30412
30413 token = cp_lexer_peek_token (parser->lexer);
30414 }
30415
30416 if (token->type != CPP_EOF)
30417 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30418 else
30419 cp_parser_error (parser, "expected %<@end%>");
30420
30421 objc_finish_interface ();
30422 }
30423
30424 /* Parse an Objective-C method definition list. */
30425
30426 static void
30427 cp_parser_objc_method_definition_list (cp_parser* parser)
30428 {
30429 cp_token *token = cp_lexer_peek_token (parser->lexer);
30430
30431 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30432 {
30433 tree meth;
30434
30435 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30436 {
30437 cp_token *ptk;
30438 tree sig, attribute;
30439 bool is_class_method;
30440 if (token->type == CPP_PLUS)
30441 is_class_method = true;
30442 else
30443 is_class_method = false;
30444 push_deferring_access_checks (dk_deferred);
30445 sig = cp_parser_objc_method_signature (parser, &attribute);
30446 if (sig == error_mark_node)
30447 {
30448 cp_parser_skip_to_end_of_block_or_statement (parser);
30449 token = cp_lexer_peek_token (parser->lexer);
30450 continue;
30451 }
30452 objc_start_method_definition (is_class_method, sig, attribute,
30453 NULL_TREE);
30454
30455 /* For historical reasons, we accept an optional semicolon. */
30456 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30457 cp_lexer_consume_token (parser->lexer);
30458
30459 ptk = cp_lexer_peek_token (parser->lexer);
30460 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30461 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30462 {
30463 perform_deferred_access_checks (tf_warning_or_error);
30464 stop_deferring_access_checks ();
30465 meth = cp_parser_function_definition_after_declarator (parser,
30466 false);
30467 pop_deferring_access_checks ();
30468 objc_finish_method_definition (meth);
30469 }
30470 }
30471 /* The following case will be removed once @synthesize is
30472 completely implemented. */
30473 else if (token->keyword == RID_AT_PROPERTY)
30474 cp_parser_objc_at_property_declaration (parser);
30475 else if (token->keyword == RID_AT_SYNTHESIZE)
30476 cp_parser_objc_at_synthesize_declaration (parser);
30477 else if (token->keyword == RID_AT_DYNAMIC)
30478 cp_parser_objc_at_dynamic_declaration (parser);
30479 else if (token->keyword == RID_ATTRIBUTE
30480 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30481 warning_at (token->location, OPT_Wattributes,
30482 "prefix attributes are ignored for methods");
30483 else
30484 /* Allow for interspersed non-ObjC++ code. */
30485 cp_parser_objc_interstitial_code (parser);
30486
30487 token = cp_lexer_peek_token (parser->lexer);
30488 }
30489
30490 if (token->type != CPP_EOF)
30491 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30492 else
30493 cp_parser_error (parser, "expected %<@end%>");
30494
30495 objc_finish_implementation ();
30496 }
30497
30498 /* Parse Objective-C ivars. */
30499
30500 static void
30501 cp_parser_objc_class_ivars (cp_parser* parser)
30502 {
30503 cp_token *token = cp_lexer_peek_token (parser->lexer);
30504
30505 if (token->type != CPP_OPEN_BRACE)
30506 return; /* No ivars specified. */
30507
30508 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30509 token = cp_lexer_peek_token (parser->lexer);
30510
30511 while (token->type != CPP_CLOSE_BRACE
30512 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30513 {
30514 cp_decl_specifier_seq declspecs;
30515 int decl_class_or_enum_p;
30516 tree prefix_attributes;
30517
30518 cp_parser_objc_visibility_spec (parser);
30519
30520 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30521 break;
30522
30523 cp_parser_decl_specifier_seq (parser,
30524 CP_PARSER_FLAGS_OPTIONAL,
30525 &declspecs,
30526 &decl_class_or_enum_p);
30527
30528 /* auto, register, static, extern, mutable. */
30529 if (declspecs.storage_class != sc_none)
30530 {
30531 cp_parser_error (parser, "invalid type for instance variable");
30532 declspecs.storage_class = sc_none;
30533 }
30534
30535 /* thread_local. */
30536 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30537 {
30538 cp_parser_error (parser, "invalid type for instance variable");
30539 declspecs.locations[ds_thread] = 0;
30540 }
30541
30542 /* typedef. */
30543 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30544 {
30545 cp_parser_error (parser, "invalid type for instance variable");
30546 declspecs.locations[ds_typedef] = 0;
30547 }
30548
30549 prefix_attributes = declspecs.attributes;
30550 declspecs.attributes = NULL_TREE;
30551
30552 /* Keep going until we hit the `;' at the end of the
30553 declaration. */
30554 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30555 {
30556 tree width = NULL_TREE, attributes, first_attribute, decl;
30557 cp_declarator *declarator = NULL;
30558 int ctor_dtor_or_conv_p;
30559
30560 /* Check for a (possibly unnamed) bitfield declaration. */
30561 token = cp_lexer_peek_token (parser->lexer);
30562 if (token->type == CPP_COLON)
30563 goto eat_colon;
30564
30565 if (token->type == CPP_NAME
30566 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30567 == CPP_COLON))
30568 {
30569 /* Get the name of the bitfield. */
30570 declarator = make_id_declarator (NULL_TREE,
30571 cp_parser_identifier (parser),
30572 sfk_none);
30573
30574 eat_colon:
30575 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30576 /* Get the width of the bitfield. */
30577 width
30578 = cp_parser_constant_expression (parser);
30579 }
30580 else
30581 {
30582 /* Parse the declarator. */
30583 declarator
30584 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30585 &ctor_dtor_or_conv_p,
30586 /*parenthesized_p=*/NULL,
30587 /*member_p=*/false,
30588 /*friend_p=*/false);
30589 }
30590
30591 /* Look for attributes that apply to the ivar. */
30592 attributes = cp_parser_attributes_opt (parser);
30593 /* Remember which attributes are prefix attributes and
30594 which are not. */
30595 first_attribute = attributes;
30596 /* Combine the attributes. */
30597 attributes = attr_chainon (prefix_attributes, attributes);
30598
30599 if (width)
30600 /* Create the bitfield declaration. */
30601 decl = grokbitfield (declarator, &declspecs,
30602 width, NULL_TREE, attributes);
30603 else
30604 decl = grokfield (declarator, &declspecs,
30605 NULL_TREE, /*init_const_expr_p=*/false,
30606 NULL_TREE, attributes);
30607
30608 /* Add the instance variable. */
30609 if (decl != error_mark_node && decl != NULL_TREE)
30610 objc_add_instance_variable (decl);
30611
30612 /* Reset PREFIX_ATTRIBUTES. */
30613 if (attributes != error_mark_node)
30614 {
30615 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30616 attributes = TREE_CHAIN (attributes);
30617 if (attributes)
30618 TREE_CHAIN (attributes) = NULL_TREE;
30619 }
30620
30621 token = cp_lexer_peek_token (parser->lexer);
30622
30623 if (token->type == CPP_COMMA)
30624 {
30625 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30626 continue;
30627 }
30628 break;
30629 }
30630
30631 cp_parser_consume_semicolon_at_end_of_statement (parser);
30632 token = cp_lexer_peek_token (parser->lexer);
30633 }
30634
30635 if (token->keyword == RID_AT_END)
30636 cp_parser_error (parser, "expected %<}%>");
30637
30638 /* Do not consume the RID_AT_END, so it will be read again as terminating
30639 the @interface of @implementation. */
30640 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30641 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30642
30643 /* For historical reasons, we accept an optional semicolon. */
30644 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30645 cp_lexer_consume_token (parser->lexer);
30646 }
30647
30648 /* Parse an Objective-C protocol declaration. */
30649
30650 static void
30651 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30652 {
30653 tree proto, protorefs;
30654 cp_token *tok;
30655
30656 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30658 {
30659 tok = cp_lexer_peek_token (parser->lexer);
30660 error_at (tok->location, "identifier expected after %<@protocol%>");
30661 cp_parser_consume_semicolon_at_end_of_statement (parser);
30662 return;
30663 }
30664
30665 /* See if we have a forward declaration or a definition. */
30666 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30667
30668 /* Try a forward declaration first. */
30669 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30670 {
30671 while (true)
30672 {
30673 tree id;
30674
30675 id = cp_parser_identifier (parser);
30676 if (id == error_mark_node)
30677 break;
30678
30679 objc_declare_protocol (id, attributes);
30680
30681 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30682 cp_lexer_consume_token (parser->lexer);
30683 else
30684 break;
30685 }
30686 cp_parser_consume_semicolon_at_end_of_statement (parser);
30687 }
30688
30689 /* Ok, we got a full-fledged definition (or at least should). */
30690 else
30691 {
30692 proto = cp_parser_identifier (parser);
30693 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30694 objc_start_protocol (proto, protorefs, attributes);
30695 cp_parser_objc_method_prototype_list (parser);
30696 }
30697 }
30698
30699 /* Parse an Objective-C superclass or category. */
30700
30701 static void
30702 cp_parser_objc_superclass_or_category (cp_parser *parser,
30703 bool iface_p,
30704 tree *super,
30705 tree *categ, bool *is_class_extension)
30706 {
30707 cp_token *next = cp_lexer_peek_token (parser->lexer);
30708
30709 *super = *categ = NULL_TREE;
30710 *is_class_extension = false;
30711 if (next->type == CPP_COLON)
30712 {
30713 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30714 *super = cp_parser_identifier (parser);
30715 }
30716 else if (next->type == CPP_OPEN_PAREN)
30717 {
30718 matching_parens parens;
30719 parens.consume_open (parser); /* Eat '('. */
30720
30721 /* If there is no category name, and this is an @interface, we
30722 have a class extension. */
30723 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30724 {
30725 *categ = NULL_TREE;
30726 *is_class_extension = true;
30727 }
30728 else
30729 *categ = cp_parser_identifier (parser);
30730
30731 parens.require_close (parser);
30732 }
30733 }
30734
30735 /* Parse an Objective-C class interface. */
30736
30737 static void
30738 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30739 {
30740 tree name, super, categ, protos;
30741 bool is_class_extension;
30742
30743 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30744 name = cp_parser_identifier (parser);
30745 if (name == error_mark_node)
30746 {
30747 /* It's hard to recover because even if valid @interface stuff
30748 is to follow, we can't compile it (or validate it) if we
30749 don't even know which class it refers to. Let's assume this
30750 was a stray '@interface' token in the stream and skip it.
30751 */
30752 return;
30753 }
30754 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30755 &is_class_extension);
30756 protos = cp_parser_objc_protocol_refs_opt (parser);
30757
30758 /* We have either a class or a category on our hands. */
30759 if (categ || is_class_extension)
30760 objc_start_category_interface (name, categ, protos, attributes);
30761 else
30762 {
30763 objc_start_class_interface (name, super, protos, attributes);
30764 /* Handle instance variable declarations, if any. */
30765 cp_parser_objc_class_ivars (parser);
30766 objc_continue_interface ();
30767 }
30768
30769 cp_parser_objc_method_prototype_list (parser);
30770 }
30771
30772 /* Parse an Objective-C class implementation. */
30773
30774 static void
30775 cp_parser_objc_class_implementation (cp_parser* parser)
30776 {
30777 tree name, super, categ;
30778 bool is_class_extension;
30779
30780 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30781 name = cp_parser_identifier (parser);
30782 if (name == error_mark_node)
30783 {
30784 /* It's hard to recover because even if valid @implementation
30785 stuff is to follow, we can't compile it (or validate it) if
30786 we don't even know which class it refers to. Let's assume
30787 this was a stray '@implementation' token in the stream and
30788 skip it.
30789 */
30790 return;
30791 }
30792 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30793 &is_class_extension);
30794
30795 /* We have either a class or a category on our hands. */
30796 if (categ)
30797 objc_start_category_implementation (name, categ);
30798 else
30799 {
30800 objc_start_class_implementation (name, super);
30801 /* Handle instance variable declarations, if any. */
30802 cp_parser_objc_class_ivars (parser);
30803 objc_continue_implementation ();
30804 }
30805
30806 cp_parser_objc_method_definition_list (parser);
30807 }
30808
30809 /* Consume the @end token and finish off the implementation. */
30810
30811 static void
30812 cp_parser_objc_end_implementation (cp_parser* parser)
30813 {
30814 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30815 objc_finish_implementation ();
30816 }
30817
30818 /* Parse an Objective-C declaration. */
30819
30820 static void
30821 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30822 {
30823 /* Try to figure out what kind of declaration is present. */
30824 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30825
30826 if (attributes)
30827 switch (kwd->keyword)
30828 {
30829 case RID_AT_ALIAS:
30830 case RID_AT_CLASS:
30831 case RID_AT_END:
30832 error_at (kwd->location, "attributes may not be specified before"
30833 " the %<@%D%> Objective-C++ keyword",
30834 kwd->u.value);
30835 attributes = NULL;
30836 break;
30837 case RID_AT_IMPLEMENTATION:
30838 warning_at (kwd->location, OPT_Wattributes,
30839 "prefix attributes are ignored before %<@%D%>",
30840 kwd->u.value);
30841 attributes = NULL;
30842 default:
30843 break;
30844 }
30845
30846 switch (kwd->keyword)
30847 {
30848 case RID_AT_ALIAS:
30849 cp_parser_objc_alias_declaration (parser);
30850 break;
30851 case RID_AT_CLASS:
30852 cp_parser_objc_class_declaration (parser);
30853 break;
30854 case RID_AT_PROTOCOL:
30855 cp_parser_objc_protocol_declaration (parser, attributes);
30856 break;
30857 case RID_AT_INTERFACE:
30858 cp_parser_objc_class_interface (parser, attributes);
30859 break;
30860 case RID_AT_IMPLEMENTATION:
30861 cp_parser_objc_class_implementation (parser);
30862 break;
30863 case RID_AT_END:
30864 cp_parser_objc_end_implementation (parser);
30865 break;
30866 default:
30867 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30868 kwd->u.value);
30869 cp_parser_skip_to_end_of_block_or_statement (parser);
30870 }
30871 }
30872
30873 /* Parse an Objective-C try-catch-finally statement.
30874
30875 objc-try-catch-finally-stmt:
30876 @try compound-statement objc-catch-clause-seq [opt]
30877 objc-finally-clause [opt]
30878
30879 objc-catch-clause-seq:
30880 objc-catch-clause objc-catch-clause-seq [opt]
30881
30882 objc-catch-clause:
30883 @catch ( objc-exception-declaration ) compound-statement
30884
30885 objc-finally-clause:
30886 @finally compound-statement
30887
30888 objc-exception-declaration:
30889 parameter-declaration
30890 '...'
30891
30892 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30893
30894 Returns NULL_TREE.
30895
30896 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30897 for C. Keep them in sync. */
30898
30899 static tree
30900 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30901 {
30902 location_t location;
30903 tree stmt;
30904
30905 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30906 location = cp_lexer_peek_token (parser->lexer)->location;
30907 objc_maybe_warn_exceptions (location);
30908 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30909 node, lest it get absorbed into the surrounding block. */
30910 stmt = push_stmt_list ();
30911 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30912 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30913
30914 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30915 {
30916 cp_parameter_declarator *parm;
30917 tree parameter_declaration = error_mark_node;
30918 bool seen_open_paren = false;
30919 matching_parens parens;
30920
30921 cp_lexer_consume_token (parser->lexer);
30922 if (parens.require_open (parser))
30923 seen_open_paren = true;
30924 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30925 {
30926 /* We have "@catch (...)" (where the '...' are literally
30927 what is in the code). Skip the '...'.
30928 parameter_declaration is set to NULL_TREE, and
30929 objc_being_catch_clauses() knows that that means
30930 '...'. */
30931 cp_lexer_consume_token (parser->lexer);
30932 parameter_declaration = NULL_TREE;
30933 }
30934 else
30935 {
30936 /* We have "@catch (NSException *exception)" or something
30937 like that. Parse the parameter declaration. */
30938 parm = cp_parser_parameter_declaration (parser, false, NULL);
30939 if (parm == NULL)
30940 parameter_declaration = error_mark_node;
30941 else
30942 parameter_declaration = grokdeclarator (parm->declarator,
30943 &parm->decl_specifiers,
30944 PARM, /*initialized=*/0,
30945 /*attrlist=*/NULL);
30946 }
30947 if (seen_open_paren)
30948 parens.require_close (parser);
30949 else
30950 {
30951 /* If there was no open parenthesis, we are recovering from
30952 an error, and we are trying to figure out what mistake
30953 the user has made. */
30954
30955 /* If there is an immediate closing parenthesis, the user
30956 probably forgot the opening one (ie, they typed "@catch
30957 NSException *e)". Parse the closing parenthesis and keep
30958 going. */
30959 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30960 cp_lexer_consume_token (parser->lexer);
30961
30962 /* If these is no immediate closing parenthesis, the user
30963 probably doesn't know that parenthesis are required at
30964 all (ie, they typed "@catch NSException *e"). So, just
30965 forget about the closing parenthesis and keep going. */
30966 }
30967 objc_begin_catch_clause (parameter_declaration);
30968 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30969 objc_finish_catch_clause ();
30970 }
30971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30972 {
30973 cp_lexer_consume_token (parser->lexer);
30974 location = cp_lexer_peek_token (parser->lexer)->location;
30975 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30976 node, lest it get absorbed into the surrounding block. */
30977 stmt = push_stmt_list ();
30978 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30979 objc_build_finally_clause (location, pop_stmt_list (stmt));
30980 }
30981
30982 return objc_finish_try_stmt ();
30983 }
30984
30985 /* Parse an Objective-C synchronized statement.
30986
30987 objc-synchronized-stmt:
30988 @synchronized ( expression ) compound-statement
30989
30990 Returns NULL_TREE. */
30991
30992 static tree
30993 cp_parser_objc_synchronized_statement (cp_parser *parser)
30994 {
30995 location_t location;
30996 tree lock, stmt;
30997
30998 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30999
31000 location = cp_lexer_peek_token (parser->lexer)->location;
31001 objc_maybe_warn_exceptions (location);
31002 matching_parens parens;
31003 parens.require_open (parser);
31004 lock = cp_parser_expression (parser);
31005 parens.require_close (parser);
31006
31007 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31008 node, lest it get absorbed into the surrounding block. */
31009 stmt = push_stmt_list ();
31010 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31011
31012 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31013 }
31014
31015 /* Parse an Objective-C throw statement.
31016
31017 objc-throw-stmt:
31018 @throw assignment-expression [opt] ;
31019
31020 Returns a constructed '@throw' statement. */
31021
31022 static tree
31023 cp_parser_objc_throw_statement (cp_parser *parser)
31024 {
31025 tree expr = NULL_TREE;
31026 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31027
31028 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31029
31030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31031 expr = cp_parser_expression (parser);
31032
31033 cp_parser_consume_semicolon_at_end_of_statement (parser);
31034
31035 return objc_build_throw_stmt (loc, expr);
31036 }
31037
31038 /* Parse an Objective-C statement. */
31039
31040 static tree
31041 cp_parser_objc_statement (cp_parser * parser)
31042 {
31043 /* Try to figure out what kind of declaration is present. */
31044 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31045
31046 switch (kwd->keyword)
31047 {
31048 case RID_AT_TRY:
31049 return cp_parser_objc_try_catch_finally_statement (parser);
31050 case RID_AT_SYNCHRONIZED:
31051 return cp_parser_objc_synchronized_statement (parser);
31052 case RID_AT_THROW:
31053 return cp_parser_objc_throw_statement (parser);
31054 default:
31055 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31056 kwd->u.value);
31057 cp_parser_skip_to_end_of_block_or_statement (parser);
31058 }
31059
31060 return error_mark_node;
31061 }
31062
31063 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31064 look ahead to see if an objc keyword follows the attributes. This
31065 is to detect the use of prefix attributes on ObjC @interface and
31066 @protocol. */
31067
31068 static bool
31069 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31070 {
31071 cp_lexer_save_tokens (parser->lexer);
31072 *attrib = cp_parser_attributes_opt (parser);
31073 gcc_assert (*attrib);
31074 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31075 {
31076 cp_lexer_commit_tokens (parser->lexer);
31077 return true;
31078 }
31079 cp_lexer_rollback_tokens (parser->lexer);
31080 return false;
31081 }
31082
31083 /* This routine is a minimal replacement for
31084 c_parser_struct_declaration () used when parsing the list of
31085 types/names or ObjC++ properties. For example, when parsing the
31086 code
31087
31088 @property (readonly) int a, b, c;
31089
31090 this function is responsible for parsing "int a, int b, int c" and
31091 returning the declarations as CHAIN of DECLs.
31092
31093 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31094 similar parsing. */
31095 static tree
31096 cp_parser_objc_struct_declaration (cp_parser *parser)
31097 {
31098 tree decls = NULL_TREE;
31099 cp_decl_specifier_seq declspecs;
31100 int decl_class_or_enum_p;
31101 tree prefix_attributes;
31102
31103 cp_parser_decl_specifier_seq (parser,
31104 CP_PARSER_FLAGS_NONE,
31105 &declspecs,
31106 &decl_class_or_enum_p);
31107
31108 if (declspecs.type == error_mark_node)
31109 return error_mark_node;
31110
31111 /* auto, register, static, extern, mutable. */
31112 if (declspecs.storage_class != sc_none)
31113 {
31114 cp_parser_error (parser, "invalid type for property");
31115 declspecs.storage_class = sc_none;
31116 }
31117
31118 /* thread_local. */
31119 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31120 {
31121 cp_parser_error (parser, "invalid type for property");
31122 declspecs.locations[ds_thread] = 0;
31123 }
31124
31125 /* typedef. */
31126 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31127 {
31128 cp_parser_error (parser, "invalid type for property");
31129 declspecs.locations[ds_typedef] = 0;
31130 }
31131
31132 prefix_attributes = declspecs.attributes;
31133 declspecs.attributes = NULL_TREE;
31134
31135 /* Keep going until we hit the `;' at the end of the declaration. */
31136 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31137 {
31138 tree attributes, first_attribute, decl;
31139 cp_declarator *declarator;
31140 cp_token *token;
31141
31142 /* Parse the declarator. */
31143 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31144 NULL, NULL, false, false);
31145
31146 /* Look for attributes that apply to the ivar. */
31147 attributes = cp_parser_attributes_opt (parser);
31148 /* Remember which attributes are prefix attributes and
31149 which are not. */
31150 first_attribute = attributes;
31151 /* Combine the attributes. */
31152 attributes = attr_chainon (prefix_attributes, attributes);
31153
31154 decl = grokfield (declarator, &declspecs,
31155 NULL_TREE, /*init_const_expr_p=*/false,
31156 NULL_TREE, attributes);
31157
31158 if (decl == error_mark_node || decl == NULL_TREE)
31159 return error_mark_node;
31160
31161 /* Reset PREFIX_ATTRIBUTES. */
31162 if (attributes != error_mark_node)
31163 {
31164 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31165 attributes = TREE_CHAIN (attributes);
31166 if (attributes)
31167 TREE_CHAIN (attributes) = NULL_TREE;
31168 }
31169
31170 DECL_CHAIN (decl) = decls;
31171 decls = decl;
31172
31173 token = cp_lexer_peek_token (parser->lexer);
31174 if (token->type == CPP_COMMA)
31175 {
31176 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31177 continue;
31178 }
31179 else
31180 break;
31181 }
31182 return decls;
31183 }
31184
31185 /* Parse an Objective-C @property declaration. The syntax is:
31186
31187 objc-property-declaration:
31188 '@property' objc-property-attributes[opt] struct-declaration ;
31189
31190 objc-property-attributes:
31191 '(' objc-property-attribute-list ')'
31192
31193 objc-property-attribute-list:
31194 objc-property-attribute
31195 objc-property-attribute-list, objc-property-attribute
31196
31197 objc-property-attribute
31198 'getter' = identifier
31199 'setter' = identifier
31200 'readonly'
31201 'readwrite'
31202 'assign'
31203 'retain'
31204 'copy'
31205 'nonatomic'
31206
31207 For example:
31208 @property NSString *name;
31209 @property (readonly) id object;
31210 @property (retain, nonatomic, getter=getTheName) id name;
31211 @property int a, b, c;
31212
31213 PS: This function is identical to
31214 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31215 static void
31216 cp_parser_objc_at_property_declaration (cp_parser *parser)
31217 {
31218 /* The following variables hold the attributes of the properties as
31219 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31220 seen. When we see an attribute, we set them to 'true' (if they
31221 are boolean properties) or to the identifier (if they have an
31222 argument, ie, for getter and setter). Note that here we only
31223 parse the list of attributes, check the syntax and accumulate the
31224 attributes that we find. objc_add_property_declaration() will
31225 then process the information. */
31226 bool property_assign = false;
31227 bool property_copy = false;
31228 tree property_getter_ident = NULL_TREE;
31229 bool property_nonatomic = false;
31230 bool property_readonly = false;
31231 bool property_readwrite = false;
31232 bool property_retain = false;
31233 tree property_setter_ident = NULL_TREE;
31234
31235 /* 'properties' is the list of properties that we read. Usually a
31236 single one, but maybe more (eg, in "@property int a, b, c;" there
31237 are three). */
31238 tree properties;
31239 location_t loc;
31240
31241 loc = cp_lexer_peek_token (parser->lexer)->location;
31242
31243 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31244
31245 /* Parse the optional attribute list... */
31246 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31247 {
31248 /* Eat the '('. */
31249 matching_parens parens;
31250 parens.consume_open (parser);
31251
31252 while (true)
31253 {
31254 bool syntax_error = false;
31255 cp_token *token = cp_lexer_peek_token (parser->lexer);
31256 enum rid keyword;
31257
31258 if (token->type != CPP_NAME)
31259 {
31260 cp_parser_error (parser, "expected identifier");
31261 break;
31262 }
31263 keyword = C_RID_CODE (token->u.value);
31264 cp_lexer_consume_token (parser->lexer);
31265 switch (keyword)
31266 {
31267 case RID_ASSIGN: property_assign = true; break;
31268 case RID_COPY: property_copy = true; break;
31269 case RID_NONATOMIC: property_nonatomic = true; break;
31270 case RID_READONLY: property_readonly = true; break;
31271 case RID_READWRITE: property_readwrite = true; break;
31272 case RID_RETAIN: property_retain = true; break;
31273
31274 case RID_GETTER:
31275 case RID_SETTER:
31276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31277 {
31278 if (keyword == RID_GETTER)
31279 cp_parser_error (parser,
31280 "missing %<=%> (after %<getter%> attribute)");
31281 else
31282 cp_parser_error (parser,
31283 "missing %<=%> (after %<setter%> attribute)");
31284 syntax_error = true;
31285 break;
31286 }
31287 cp_lexer_consume_token (parser->lexer); /* eat the = */
31288 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31289 {
31290 cp_parser_error (parser, "expected identifier");
31291 syntax_error = true;
31292 break;
31293 }
31294 if (keyword == RID_SETTER)
31295 {
31296 if (property_setter_ident != NULL_TREE)
31297 {
31298 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31299 cp_lexer_consume_token (parser->lexer);
31300 }
31301 else
31302 property_setter_ident = cp_parser_objc_selector (parser);
31303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31304 cp_parser_error (parser, "setter name must terminate with %<:%>");
31305 else
31306 cp_lexer_consume_token (parser->lexer);
31307 }
31308 else
31309 {
31310 if (property_getter_ident != NULL_TREE)
31311 {
31312 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31313 cp_lexer_consume_token (parser->lexer);
31314 }
31315 else
31316 property_getter_ident = cp_parser_objc_selector (parser);
31317 }
31318 break;
31319 default:
31320 cp_parser_error (parser, "unknown property attribute");
31321 syntax_error = true;
31322 break;
31323 }
31324
31325 if (syntax_error)
31326 break;
31327
31328 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31329 cp_lexer_consume_token (parser->lexer);
31330 else
31331 break;
31332 }
31333
31334 /* FIXME: "@property (setter, assign);" will generate a spurious
31335 "error: expected ‘)’ before ‘,’ token". This is because
31336 cp_parser_require, unlike the C counterpart, will produce an
31337 error even if we are in error recovery. */
31338 if (!parens.require_close (parser))
31339 {
31340 cp_parser_skip_to_closing_parenthesis (parser,
31341 /*recovering=*/true,
31342 /*or_comma=*/false,
31343 /*consume_paren=*/true);
31344 }
31345 }
31346
31347 /* ... and the property declaration(s). */
31348 properties = cp_parser_objc_struct_declaration (parser);
31349
31350 if (properties == error_mark_node)
31351 {
31352 cp_parser_skip_to_end_of_statement (parser);
31353 /* If the next token is now a `;', consume it. */
31354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31355 cp_lexer_consume_token (parser->lexer);
31356 return;
31357 }
31358
31359 if (properties == NULL_TREE)
31360 cp_parser_error (parser, "expected identifier");
31361 else
31362 {
31363 /* Comma-separated properties are chained together in
31364 reverse order; add them one by one. */
31365 properties = nreverse (properties);
31366
31367 for (; properties; properties = TREE_CHAIN (properties))
31368 objc_add_property_declaration (loc, copy_node (properties),
31369 property_readonly, property_readwrite,
31370 property_assign, property_retain,
31371 property_copy, property_nonatomic,
31372 property_getter_ident, property_setter_ident);
31373 }
31374
31375 cp_parser_consume_semicolon_at_end_of_statement (parser);
31376 }
31377
31378 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31379
31380 objc-synthesize-declaration:
31381 @synthesize objc-synthesize-identifier-list ;
31382
31383 objc-synthesize-identifier-list:
31384 objc-synthesize-identifier
31385 objc-synthesize-identifier-list, objc-synthesize-identifier
31386
31387 objc-synthesize-identifier
31388 identifier
31389 identifier = identifier
31390
31391 For example:
31392 @synthesize MyProperty;
31393 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31394
31395 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31396 for C. Keep them in sync.
31397 */
31398 static void
31399 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31400 {
31401 tree list = NULL_TREE;
31402 location_t loc;
31403 loc = cp_lexer_peek_token (parser->lexer)->location;
31404
31405 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31406 while (true)
31407 {
31408 tree property, ivar;
31409 property = cp_parser_identifier (parser);
31410 if (property == error_mark_node)
31411 {
31412 cp_parser_consume_semicolon_at_end_of_statement (parser);
31413 return;
31414 }
31415 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31416 {
31417 cp_lexer_consume_token (parser->lexer);
31418 ivar = cp_parser_identifier (parser);
31419 if (ivar == error_mark_node)
31420 {
31421 cp_parser_consume_semicolon_at_end_of_statement (parser);
31422 return;
31423 }
31424 }
31425 else
31426 ivar = NULL_TREE;
31427 list = chainon (list, build_tree_list (ivar, property));
31428 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31429 cp_lexer_consume_token (parser->lexer);
31430 else
31431 break;
31432 }
31433 cp_parser_consume_semicolon_at_end_of_statement (parser);
31434 objc_add_synthesize_declaration (loc, list);
31435 }
31436
31437 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31438
31439 objc-dynamic-declaration:
31440 @dynamic identifier-list ;
31441
31442 For example:
31443 @dynamic MyProperty;
31444 @dynamic MyProperty, AnotherProperty;
31445
31446 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31447 for C. Keep them in sync.
31448 */
31449 static void
31450 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31451 {
31452 tree list = NULL_TREE;
31453 location_t loc;
31454 loc = cp_lexer_peek_token (parser->lexer)->location;
31455
31456 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31457 while (true)
31458 {
31459 tree property;
31460 property = cp_parser_identifier (parser);
31461 if (property == error_mark_node)
31462 {
31463 cp_parser_consume_semicolon_at_end_of_statement (parser);
31464 return;
31465 }
31466 list = chainon (list, build_tree_list (NULL, property));
31467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31468 cp_lexer_consume_token (parser->lexer);
31469 else
31470 break;
31471 }
31472 cp_parser_consume_semicolon_at_end_of_statement (parser);
31473 objc_add_dynamic_declaration (loc, list);
31474 }
31475
31476 \f
31477 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31478
31479 /* Returns name of the next clause.
31480 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31481 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31482 returned and the token is consumed. */
31483
31484 static pragma_omp_clause
31485 cp_parser_omp_clause_name (cp_parser *parser)
31486 {
31487 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31488
31489 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31490 result = PRAGMA_OACC_CLAUSE_AUTO;
31491 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31492 result = PRAGMA_OMP_CLAUSE_IF;
31493 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31494 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31495 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31496 result = PRAGMA_OACC_CLAUSE_DELETE;
31497 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31498 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31499 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31500 result = PRAGMA_OMP_CLAUSE_FOR;
31501 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31502 {
31503 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31504 const char *p = IDENTIFIER_POINTER (id);
31505
31506 switch (p[0])
31507 {
31508 case 'a':
31509 if (!strcmp ("aligned", p))
31510 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31511 else if (!strcmp ("async", p))
31512 result = PRAGMA_OACC_CLAUSE_ASYNC;
31513 break;
31514 case 'c':
31515 if (!strcmp ("collapse", p))
31516 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31517 else if (!strcmp ("copy", p))
31518 result = PRAGMA_OACC_CLAUSE_COPY;
31519 else if (!strcmp ("copyin", p))
31520 result = PRAGMA_OMP_CLAUSE_COPYIN;
31521 else if (!strcmp ("copyout", p))
31522 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31523 else if (!strcmp ("copyprivate", p))
31524 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31525 else if (!strcmp ("create", p))
31526 result = PRAGMA_OACC_CLAUSE_CREATE;
31527 break;
31528 case 'd':
31529 if (!strcmp ("defaultmap", p))
31530 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31531 else if (!strcmp ("depend", p))
31532 result = PRAGMA_OMP_CLAUSE_DEPEND;
31533 else if (!strcmp ("device", p))
31534 result = PRAGMA_OMP_CLAUSE_DEVICE;
31535 else if (!strcmp ("deviceptr", p))
31536 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31537 else if (!strcmp ("device_resident", p))
31538 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31539 else if (!strcmp ("dist_schedule", p))
31540 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31541 break;
31542 case 'f':
31543 if (!strcmp ("final", p))
31544 result = PRAGMA_OMP_CLAUSE_FINAL;
31545 else if (!strcmp ("finalize", p))
31546 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31547 else if (!strcmp ("firstprivate", p))
31548 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31549 else if (!strcmp ("from", p))
31550 result = PRAGMA_OMP_CLAUSE_FROM;
31551 break;
31552 case 'g':
31553 if (!strcmp ("gang", p))
31554 result = PRAGMA_OACC_CLAUSE_GANG;
31555 else if (!strcmp ("grainsize", p))
31556 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31557 break;
31558 case 'h':
31559 if (!strcmp ("hint", p))
31560 result = PRAGMA_OMP_CLAUSE_HINT;
31561 else if (!strcmp ("host", p))
31562 result = PRAGMA_OACC_CLAUSE_HOST;
31563 break;
31564 case 'i':
31565 if (!strcmp ("if_present", p))
31566 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31567 else if (!strcmp ("inbranch", p))
31568 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31569 else if (!strcmp ("independent", p))
31570 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31571 else if (!strcmp ("is_device_ptr", p))
31572 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31573 break;
31574 case 'l':
31575 if (!strcmp ("lastprivate", p))
31576 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31577 else if (!strcmp ("linear", p))
31578 result = PRAGMA_OMP_CLAUSE_LINEAR;
31579 else if (!strcmp ("link", p))
31580 result = PRAGMA_OMP_CLAUSE_LINK;
31581 break;
31582 case 'm':
31583 if (!strcmp ("map", p))
31584 result = PRAGMA_OMP_CLAUSE_MAP;
31585 else if (!strcmp ("mergeable", p))
31586 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31587 break;
31588 case 'n':
31589 if (!strcmp ("nogroup", p))
31590 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31591 else if (!strcmp ("notinbranch", p))
31592 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31593 else if (!strcmp ("nowait", p))
31594 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31595 else if (!strcmp ("num_gangs", p))
31596 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31597 else if (!strcmp ("num_tasks", p))
31598 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31599 else if (!strcmp ("num_teams", p))
31600 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31601 else if (!strcmp ("num_threads", p))
31602 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31603 else if (!strcmp ("num_workers", p))
31604 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31605 break;
31606 case 'o':
31607 if (!strcmp ("ordered", p))
31608 result = PRAGMA_OMP_CLAUSE_ORDERED;
31609 break;
31610 case 'p':
31611 if (!strcmp ("parallel", p))
31612 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31613 else if (!strcmp ("present", p))
31614 result = PRAGMA_OACC_CLAUSE_PRESENT;
31615 else if (!strcmp ("present_or_copy", p)
31616 || !strcmp ("pcopy", p))
31617 result = PRAGMA_OACC_CLAUSE_COPY;
31618 else if (!strcmp ("present_or_copyin", p)
31619 || !strcmp ("pcopyin", p))
31620 result = PRAGMA_OACC_CLAUSE_COPYIN;
31621 else if (!strcmp ("present_or_copyout", p)
31622 || !strcmp ("pcopyout", p))
31623 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31624 else if (!strcmp ("present_or_create", p)
31625 || !strcmp ("pcreate", p))
31626 result = PRAGMA_OACC_CLAUSE_CREATE;
31627 else if (!strcmp ("priority", p))
31628 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31629 else if (!strcmp ("proc_bind", p))
31630 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31631 break;
31632 case 'r':
31633 if (!strcmp ("reduction", p))
31634 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31635 break;
31636 case 's':
31637 if (!strcmp ("safelen", p))
31638 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31639 else if (!strcmp ("schedule", p))
31640 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31641 else if (!strcmp ("sections", p))
31642 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31643 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31644 result = PRAGMA_OACC_CLAUSE_HOST;
31645 else if (!strcmp ("seq", p))
31646 result = PRAGMA_OACC_CLAUSE_SEQ;
31647 else if (!strcmp ("shared", p))
31648 result = PRAGMA_OMP_CLAUSE_SHARED;
31649 else if (!strcmp ("simd", p))
31650 result = PRAGMA_OMP_CLAUSE_SIMD;
31651 else if (!strcmp ("simdlen", p))
31652 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31653 break;
31654 case 't':
31655 if (!strcmp ("taskgroup", p))
31656 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31657 else if (!strcmp ("thread_limit", p))
31658 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31659 else if (!strcmp ("threads", p))
31660 result = PRAGMA_OMP_CLAUSE_THREADS;
31661 else if (!strcmp ("tile", p))
31662 result = PRAGMA_OACC_CLAUSE_TILE;
31663 else if (!strcmp ("to", p))
31664 result = PRAGMA_OMP_CLAUSE_TO;
31665 break;
31666 case 'u':
31667 if (!strcmp ("uniform", p))
31668 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31669 else if (!strcmp ("untied", p))
31670 result = PRAGMA_OMP_CLAUSE_UNTIED;
31671 else if (!strcmp ("use_device", p))
31672 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31673 else if (!strcmp ("use_device_ptr", p))
31674 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31675 break;
31676 case 'v':
31677 if (!strcmp ("vector", p))
31678 result = PRAGMA_OACC_CLAUSE_VECTOR;
31679 else if (!strcmp ("vector_length", p))
31680 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31681 break;
31682 case 'w':
31683 if (!strcmp ("wait", p))
31684 result = PRAGMA_OACC_CLAUSE_WAIT;
31685 else if (!strcmp ("worker", p))
31686 result = PRAGMA_OACC_CLAUSE_WORKER;
31687 break;
31688 }
31689 }
31690
31691 if (result != PRAGMA_OMP_CLAUSE_NONE)
31692 cp_lexer_consume_token (parser->lexer);
31693
31694 return result;
31695 }
31696
31697 /* Validate that a clause of the given type does not already exist. */
31698
31699 static void
31700 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31701 const char *name, location_t location)
31702 {
31703 tree c;
31704
31705 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31706 if (OMP_CLAUSE_CODE (c) == code)
31707 {
31708 error_at (location, "too many %qs clauses", name);
31709 break;
31710 }
31711 }
31712
31713 /* OpenMP 2.5:
31714 variable-list:
31715 identifier
31716 variable-list , identifier
31717
31718 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31719 colon). An opening parenthesis will have been consumed by the caller.
31720
31721 If KIND is nonzero, create the appropriate node and install the decl
31722 in OMP_CLAUSE_DECL and add the node to the head of the list.
31723
31724 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31725 return the list created.
31726
31727 COLON can be NULL if only closing parenthesis should end the list,
31728 or pointer to bool which will receive false if the list is terminated
31729 by closing parenthesis or true if the list is terminated by colon. */
31730
31731 static tree
31732 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31733 tree list, bool *colon)
31734 {
31735 cp_token *token;
31736 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31737 if (colon)
31738 {
31739 parser->colon_corrects_to_scope_p = false;
31740 *colon = false;
31741 }
31742 while (1)
31743 {
31744 tree name, decl;
31745
31746 token = cp_lexer_peek_token (parser->lexer);
31747 if (kind != 0
31748 && current_class_ptr
31749 && cp_parser_is_keyword (token, RID_THIS))
31750 {
31751 decl = finish_this_expr ();
31752 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31753 || CONVERT_EXPR_P (decl))
31754 decl = TREE_OPERAND (decl, 0);
31755 cp_lexer_consume_token (parser->lexer);
31756 }
31757 else
31758 {
31759 name = cp_parser_id_expression (parser, /*template_p=*/false,
31760 /*check_dependency_p=*/true,
31761 /*template_p=*/NULL,
31762 /*declarator_p=*/false,
31763 /*optional_p=*/false);
31764 if (name == error_mark_node)
31765 goto skip_comma;
31766
31767 if (identifier_p (name))
31768 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31769 else
31770 decl = name;
31771 if (decl == error_mark_node)
31772 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31773 token->location);
31774 }
31775 if (decl == error_mark_node)
31776 ;
31777 else if (kind != 0)
31778 {
31779 switch (kind)
31780 {
31781 case OMP_CLAUSE__CACHE_:
31782 /* The OpenACC cache directive explicitly only allows "array
31783 elements or subarrays". */
31784 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31785 {
31786 error_at (token->location, "expected %<[%>");
31787 decl = error_mark_node;
31788 break;
31789 }
31790 /* FALLTHROUGH. */
31791 case OMP_CLAUSE_MAP:
31792 case OMP_CLAUSE_FROM:
31793 case OMP_CLAUSE_TO:
31794 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31795 {
31796 location_t loc
31797 = cp_lexer_peek_token (parser->lexer)->location;
31798 cp_id_kind idk = CP_ID_KIND_NONE;
31799 cp_lexer_consume_token (parser->lexer);
31800 decl = convert_from_reference (decl);
31801 decl
31802 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31803 decl, false,
31804 &idk, loc);
31805 }
31806 /* FALLTHROUGH. */
31807 case OMP_CLAUSE_DEPEND:
31808 case OMP_CLAUSE_REDUCTION:
31809 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31810 {
31811 tree low_bound = NULL_TREE, length = NULL_TREE;
31812
31813 parser->colon_corrects_to_scope_p = false;
31814 cp_lexer_consume_token (parser->lexer);
31815 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31816 low_bound = cp_parser_expression (parser);
31817 if (!colon)
31818 parser->colon_corrects_to_scope_p
31819 = saved_colon_corrects_to_scope_p;
31820 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31821 length = integer_one_node;
31822 else
31823 {
31824 /* Look for `:'. */
31825 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31826 goto skip_comma;
31827 if (!cp_lexer_next_token_is (parser->lexer,
31828 CPP_CLOSE_SQUARE))
31829 length = cp_parser_expression (parser);
31830 }
31831 /* Look for the closing `]'. */
31832 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31833 RT_CLOSE_SQUARE))
31834 goto skip_comma;
31835
31836 decl = tree_cons (low_bound, length, decl);
31837 }
31838 break;
31839 default:
31840 break;
31841 }
31842
31843 tree u = build_omp_clause (token->location, kind);
31844 OMP_CLAUSE_DECL (u) = decl;
31845 OMP_CLAUSE_CHAIN (u) = list;
31846 list = u;
31847 }
31848 else
31849 list = tree_cons (decl, NULL_TREE, list);
31850
31851 get_comma:
31852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31853 break;
31854 cp_lexer_consume_token (parser->lexer);
31855 }
31856
31857 if (colon)
31858 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31859
31860 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31861 {
31862 *colon = true;
31863 cp_parser_require (parser, CPP_COLON, RT_COLON);
31864 return list;
31865 }
31866
31867 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31868 {
31869 int ending;
31870
31871 /* Try to resync to an unnested comma. Copied from
31872 cp_parser_parenthesized_expression_list. */
31873 skip_comma:
31874 if (colon)
31875 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31876 ending = cp_parser_skip_to_closing_parenthesis (parser,
31877 /*recovering=*/true,
31878 /*or_comma=*/true,
31879 /*consume_paren=*/true);
31880 if (ending < 0)
31881 goto get_comma;
31882 }
31883
31884 return list;
31885 }
31886
31887 /* Similarly, but expect leading and trailing parenthesis. This is a very
31888 common case for omp clauses. */
31889
31890 static tree
31891 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31892 {
31893 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31894 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31895 return list;
31896 }
31897
31898 /* OpenACC 2.0:
31899 copy ( variable-list )
31900 copyin ( variable-list )
31901 copyout ( variable-list )
31902 create ( variable-list )
31903 delete ( variable-list )
31904 present ( variable-list ) */
31905
31906 static tree
31907 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31908 tree list)
31909 {
31910 enum gomp_map_kind kind;
31911 switch (c_kind)
31912 {
31913 case PRAGMA_OACC_CLAUSE_COPY:
31914 kind = GOMP_MAP_TOFROM;
31915 break;
31916 case PRAGMA_OACC_CLAUSE_COPYIN:
31917 kind = GOMP_MAP_TO;
31918 break;
31919 case PRAGMA_OACC_CLAUSE_COPYOUT:
31920 kind = GOMP_MAP_FROM;
31921 break;
31922 case PRAGMA_OACC_CLAUSE_CREATE:
31923 kind = GOMP_MAP_ALLOC;
31924 break;
31925 case PRAGMA_OACC_CLAUSE_DELETE:
31926 kind = GOMP_MAP_RELEASE;
31927 break;
31928 case PRAGMA_OACC_CLAUSE_DEVICE:
31929 kind = GOMP_MAP_FORCE_TO;
31930 break;
31931 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31932 kind = GOMP_MAP_DEVICE_RESIDENT;
31933 break;
31934 case PRAGMA_OACC_CLAUSE_HOST:
31935 kind = GOMP_MAP_FORCE_FROM;
31936 break;
31937 case PRAGMA_OACC_CLAUSE_LINK:
31938 kind = GOMP_MAP_LINK;
31939 break;
31940 case PRAGMA_OACC_CLAUSE_PRESENT:
31941 kind = GOMP_MAP_FORCE_PRESENT;
31942 break;
31943 default:
31944 gcc_unreachable ();
31945 }
31946 tree nl, c;
31947 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31948
31949 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31950 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31951
31952 return nl;
31953 }
31954
31955 /* OpenACC 2.0:
31956 deviceptr ( variable-list ) */
31957
31958 static tree
31959 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31960 {
31961 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31962 tree vars, t;
31963
31964 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31965 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31966 variable-list must only allow for pointer variables. */
31967 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31968 for (t = vars; t; t = TREE_CHAIN (t))
31969 {
31970 tree v = TREE_PURPOSE (t);
31971 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31972 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31973 OMP_CLAUSE_DECL (u) = v;
31974 OMP_CLAUSE_CHAIN (u) = list;
31975 list = u;
31976 }
31977
31978 return list;
31979 }
31980
31981 /* OpenACC 2.5:
31982 auto
31983 finalize
31984 independent
31985 nohost
31986 seq */
31987
31988 static tree
31989 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31990 enum omp_clause_code code,
31991 tree list, location_t location)
31992 {
31993 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31994 tree c = build_omp_clause (location, code);
31995 OMP_CLAUSE_CHAIN (c) = list;
31996 return c;
31997 }
31998
31999 /* OpenACC:
32000 num_gangs ( expression )
32001 num_workers ( expression )
32002 vector_length ( expression ) */
32003
32004 static tree
32005 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32006 const char *str, tree list)
32007 {
32008 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32009
32010 matching_parens parens;
32011 if (!parens.require_open (parser))
32012 return list;
32013
32014 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32015
32016 if (t == error_mark_node
32017 || !parens.require_close (parser))
32018 {
32019 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32020 /*or_comma=*/false,
32021 /*consume_paren=*/true);
32022 return list;
32023 }
32024
32025 check_no_duplicate_clause (list, code, str, loc);
32026
32027 tree c = build_omp_clause (loc, code);
32028 OMP_CLAUSE_OPERAND (c, 0) = t;
32029 OMP_CLAUSE_CHAIN (c) = list;
32030 return c;
32031 }
32032
32033 /* OpenACC:
32034
32035 gang [( gang-arg-list )]
32036 worker [( [num:] int-expr )]
32037 vector [( [length:] int-expr )]
32038
32039 where gang-arg is one of:
32040
32041 [num:] int-expr
32042 static: size-expr
32043
32044 and size-expr may be:
32045
32046 *
32047 int-expr
32048 */
32049
32050 static tree
32051 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32052 const char *str, tree list)
32053 {
32054 const char *id = "num";
32055 cp_lexer *lexer = parser->lexer;
32056 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32057 location_t loc = cp_lexer_peek_token (lexer)->location;
32058
32059 if (kind == OMP_CLAUSE_VECTOR)
32060 id = "length";
32061
32062 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32063 {
32064 matching_parens parens;
32065 parens.consume_open (parser);
32066
32067 do
32068 {
32069 cp_token *next = cp_lexer_peek_token (lexer);
32070 int idx = 0;
32071
32072 /* Gang static argument. */
32073 if (kind == OMP_CLAUSE_GANG
32074 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32075 {
32076 cp_lexer_consume_token (lexer);
32077
32078 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32079 goto cleanup_error;
32080
32081 idx = 1;
32082 if (ops[idx] != NULL)
32083 {
32084 cp_parser_error (parser, "too many %<static%> arguments");
32085 goto cleanup_error;
32086 }
32087
32088 /* Check for the '*' argument. */
32089 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32090 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32091 || cp_lexer_nth_token_is (parser->lexer, 2,
32092 CPP_CLOSE_PAREN)))
32093 {
32094 cp_lexer_consume_token (lexer);
32095 ops[idx] = integer_minus_one_node;
32096
32097 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32098 {
32099 cp_lexer_consume_token (lexer);
32100 continue;
32101 }
32102 else break;
32103 }
32104 }
32105 /* Worker num: argument and vector length: arguments. */
32106 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32107 && id_equal (next->u.value, id)
32108 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32109 {
32110 cp_lexer_consume_token (lexer); /* id */
32111 cp_lexer_consume_token (lexer); /* ':' */
32112 }
32113
32114 /* Now collect the actual argument. */
32115 if (ops[idx] != NULL_TREE)
32116 {
32117 cp_parser_error (parser, "unexpected argument");
32118 goto cleanup_error;
32119 }
32120
32121 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32122 false);
32123 if (expr == error_mark_node)
32124 goto cleanup_error;
32125
32126 mark_exp_read (expr);
32127 ops[idx] = expr;
32128
32129 if (kind == OMP_CLAUSE_GANG
32130 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32131 {
32132 cp_lexer_consume_token (lexer);
32133 continue;
32134 }
32135 break;
32136 }
32137 while (1);
32138
32139 if (!parens.require_close (parser))
32140 goto cleanup_error;
32141 }
32142
32143 check_no_duplicate_clause (list, kind, str, loc);
32144
32145 c = build_omp_clause (loc, kind);
32146
32147 if (ops[1])
32148 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32149
32150 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32151 OMP_CLAUSE_CHAIN (c) = list;
32152
32153 return c;
32154
32155 cleanup_error:
32156 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32157 return list;
32158 }
32159
32160 /* OpenACC 2.0:
32161 tile ( size-expr-list ) */
32162
32163 static tree
32164 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32165 {
32166 tree c, expr = error_mark_node;
32167 tree tile = NULL_TREE;
32168
32169 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32170 so, but the spec authors never considered such a case and have
32171 differing opinions on what it might mean, including 'not
32172 allowed'.) */
32173 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32174 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32175 clause_loc);
32176
32177 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32178 return list;
32179
32180 do
32181 {
32182 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32183 return list;
32184
32185 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32186 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32187 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32188 {
32189 cp_lexer_consume_token (parser->lexer);
32190 expr = integer_zero_node;
32191 }
32192 else
32193 expr = cp_parser_constant_expression (parser);
32194
32195 tile = tree_cons (NULL_TREE, expr, tile);
32196 }
32197 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32198
32199 /* Consume the trailing ')'. */
32200 cp_lexer_consume_token (parser->lexer);
32201
32202 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32203 tile = nreverse (tile);
32204 OMP_CLAUSE_TILE_LIST (c) = tile;
32205 OMP_CLAUSE_CHAIN (c) = list;
32206 return c;
32207 }
32208
32209 /* OpenACC 2.0
32210 Parse wait clause or directive parameters. */
32211
32212 static tree
32213 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32214 {
32215 vec<tree, va_gc> *args;
32216 tree t, args_tree;
32217
32218 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32219 /*cast_p=*/false,
32220 /*allow_expansion_p=*/true,
32221 /*non_constant_p=*/NULL);
32222
32223 if (args == NULL || args->length () == 0)
32224 {
32225 cp_parser_error (parser, "expected integer expression before ')'");
32226 if (args != NULL)
32227 release_tree_vector (args);
32228 return list;
32229 }
32230
32231 args_tree = build_tree_list_vec (args);
32232
32233 release_tree_vector (args);
32234
32235 for (t = args_tree; t; t = TREE_CHAIN (t))
32236 {
32237 tree targ = TREE_VALUE (t);
32238
32239 if (targ != error_mark_node)
32240 {
32241 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32242 error ("%<wait%> expression must be integral");
32243 else
32244 {
32245 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32246
32247 targ = mark_rvalue_use (targ);
32248 OMP_CLAUSE_DECL (c) = targ;
32249 OMP_CLAUSE_CHAIN (c) = list;
32250 list = c;
32251 }
32252 }
32253 }
32254
32255 return list;
32256 }
32257
32258 /* OpenACC:
32259 wait ( int-expr-list ) */
32260
32261 static tree
32262 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32263 {
32264 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32265
32266 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32267 return list;
32268
32269 list = cp_parser_oacc_wait_list (parser, location, list);
32270
32271 return list;
32272 }
32273
32274 /* OpenMP 3.0:
32275 collapse ( constant-expression ) */
32276
32277 static tree
32278 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32279 {
32280 tree c, num;
32281 location_t loc;
32282 HOST_WIDE_INT n;
32283
32284 loc = cp_lexer_peek_token (parser->lexer)->location;
32285 matching_parens parens;
32286 if (!parens.require_open (parser))
32287 return list;
32288
32289 num = cp_parser_constant_expression (parser);
32290
32291 if (!parens.require_close (parser))
32292 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32293 /*or_comma=*/false,
32294 /*consume_paren=*/true);
32295
32296 if (num == error_mark_node)
32297 return list;
32298 num = fold_non_dependent_expr (num);
32299 if (!tree_fits_shwi_p (num)
32300 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32301 || (n = tree_to_shwi (num)) <= 0
32302 || (int) n != n)
32303 {
32304 error_at (loc, "collapse argument needs positive constant integer expression");
32305 return list;
32306 }
32307
32308 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32309 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32310 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32311 OMP_CLAUSE_CHAIN (c) = list;
32312 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32313
32314 return c;
32315 }
32316
32317 /* OpenMP 2.5:
32318 default ( none | shared )
32319
32320 OpenACC:
32321 default ( none | present ) */
32322
32323 static tree
32324 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32325 location_t location, bool is_oacc)
32326 {
32327 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32328 tree c;
32329
32330 matching_parens parens;
32331 if (!parens.require_open (parser))
32332 return list;
32333 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32334 {
32335 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32336 const char *p = IDENTIFIER_POINTER (id);
32337
32338 switch (p[0])
32339 {
32340 case 'n':
32341 if (strcmp ("none", p) != 0)
32342 goto invalid_kind;
32343 kind = OMP_CLAUSE_DEFAULT_NONE;
32344 break;
32345
32346 case 'p':
32347 if (strcmp ("present", p) != 0 || !is_oacc)
32348 goto invalid_kind;
32349 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32350 break;
32351
32352 case 's':
32353 if (strcmp ("shared", p) != 0 || is_oacc)
32354 goto invalid_kind;
32355 kind = OMP_CLAUSE_DEFAULT_SHARED;
32356 break;
32357
32358 default:
32359 goto invalid_kind;
32360 }
32361
32362 cp_lexer_consume_token (parser->lexer);
32363 }
32364 else
32365 {
32366 invalid_kind:
32367 if (is_oacc)
32368 cp_parser_error (parser, "expected %<none%> or %<present%>");
32369 else
32370 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32371 }
32372
32373 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32374 || !parens.require_close (parser))
32375 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32376 /*or_comma=*/false,
32377 /*consume_paren=*/true);
32378
32379 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32380 return list;
32381
32382 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32383 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32384 OMP_CLAUSE_CHAIN (c) = list;
32385 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32386
32387 return c;
32388 }
32389
32390 /* OpenMP 3.1:
32391 final ( expression ) */
32392
32393 static tree
32394 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32395 {
32396 tree t, c;
32397
32398 matching_parens parens;
32399 if (!parens.require_open (parser))
32400 return list;
32401
32402 t = cp_parser_condition (parser);
32403
32404 if (t == error_mark_node
32405 || !parens.require_close (parser))
32406 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32407 /*or_comma=*/false,
32408 /*consume_paren=*/true);
32409
32410 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32411
32412 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32413 OMP_CLAUSE_FINAL_EXPR (c) = t;
32414 OMP_CLAUSE_CHAIN (c) = list;
32415
32416 return c;
32417 }
32418
32419 /* OpenMP 2.5:
32420 if ( expression )
32421
32422 OpenMP 4.5:
32423 if ( directive-name-modifier : expression )
32424
32425 directive-name-modifier:
32426 parallel | task | taskloop | target data | target | target update
32427 | target enter data | target exit data */
32428
32429 static tree
32430 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32431 bool is_omp)
32432 {
32433 tree t, c;
32434 enum tree_code if_modifier = ERROR_MARK;
32435
32436 matching_parens parens;
32437 if (!parens.require_open (parser))
32438 return list;
32439
32440 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32441 {
32442 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32443 const char *p = IDENTIFIER_POINTER (id);
32444 int n = 2;
32445
32446 if (strcmp ("parallel", p) == 0)
32447 if_modifier = OMP_PARALLEL;
32448 else if (strcmp ("task", p) == 0)
32449 if_modifier = OMP_TASK;
32450 else if (strcmp ("taskloop", p) == 0)
32451 if_modifier = OMP_TASKLOOP;
32452 else if (strcmp ("target", p) == 0)
32453 {
32454 if_modifier = OMP_TARGET;
32455 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32456 {
32457 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32458 p = IDENTIFIER_POINTER (id);
32459 if (strcmp ("data", p) == 0)
32460 if_modifier = OMP_TARGET_DATA;
32461 else if (strcmp ("update", p) == 0)
32462 if_modifier = OMP_TARGET_UPDATE;
32463 else if (strcmp ("enter", p) == 0)
32464 if_modifier = OMP_TARGET_ENTER_DATA;
32465 else if (strcmp ("exit", p) == 0)
32466 if_modifier = OMP_TARGET_EXIT_DATA;
32467 if (if_modifier != OMP_TARGET)
32468 n = 3;
32469 else
32470 {
32471 location_t loc
32472 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32473 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32474 "or %<exit%>");
32475 if_modifier = ERROR_MARK;
32476 }
32477 if (if_modifier == OMP_TARGET_ENTER_DATA
32478 || if_modifier == OMP_TARGET_EXIT_DATA)
32479 {
32480 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32481 {
32482 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32483 p = IDENTIFIER_POINTER (id);
32484 if (strcmp ("data", p) == 0)
32485 n = 4;
32486 }
32487 if (n != 4)
32488 {
32489 location_t loc
32490 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32491 error_at (loc, "expected %<data%>");
32492 if_modifier = ERROR_MARK;
32493 }
32494 }
32495 }
32496 }
32497 if (if_modifier != ERROR_MARK)
32498 {
32499 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32500 {
32501 while (n-- > 0)
32502 cp_lexer_consume_token (parser->lexer);
32503 }
32504 else
32505 {
32506 if (n > 2)
32507 {
32508 location_t loc
32509 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32510 error_at (loc, "expected %<:%>");
32511 }
32512 if_modifier = ERROR_MARK;
32513 }
32514 }
32515 }
32516
32517 t = cp_parser_condition (parser);
32518
32519 if (t == error_mark_node
32520 || !parens.require_close (parser))
32521 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32522 /*or_comma=*/false,
32523 /*consume_paren=*/true);
32524
32525 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32526 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32527 {
32528 if (if_modifier != ERROR_MARK
32529 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32530 {
32531 const char *p = NULL;
32532 switch (if_modifier)
32533 {
32534 case OMP_PARALLEL: p = "parallel"; break;
32535 case OMP_TASK: p = "task"; break;
32536 case OMP_TASKLOOP: p = "taskloop"; break;
32537 case OMP_TARGET_DATA: p = "target data"; break;
32538 case OMP_TARGET: p = "target"; break;
32539 case OMP_TARGET_UPDATE: p = "target update"; break;
32540 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32541 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32542 default: gcc_unreachable ();
32543 }
32544 error_at (location, "too many %<if%> clauses with %qs modifier",
32545 p);
32546 return list;
32547 }
32548 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32549 {
32550 if (!is_omp)
32551 error_at (location, "too many %<if%> clauses");
32552 else
32553 error_at (location, "too many %<if%> clauses without modifier");
32554 return list;
32555 }
32556 else if (if_modifier == ERROR_MARK
32557 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32558 {
32559 error_at (location, "if any %<if%> clause has modifier, then all "
32560 "%<if%> clauses have to use modifier");
32561 return list;
32562 }
32563 }
32564
32565 c = build_omp_clause (location, OMP_CLAUSE_IF);
32566 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32567 OMP_CLAUSE_IF_EXPR (c) = t;
32568 OMP_CLAUSE_CHAIN (c) = list;
32569
32570 return c;
32571 }
32572
32573 /* OpenMP 3.1:
32574 mergeable */
32575
32576 static tree
32577 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32578 tree list, location_t location)
32579 {
32580 tree c;
32581
32582 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32583 location);
32584
32585 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32586 OMP_CLAUSE_CHAIN (c) = list;
32587 return c;
32588 }
32589
32590 /* OpenMP 2.5:
32591 nowait */
32592
32593 static tree
32594 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32595 tree list, location_t location)
32596 {
32597 tree c;
32598
32599 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32600
32601 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32602 OMP_CLAUSE_CHAIN (c) = list;
32603 return c;
32604 }
32605
32606 /* OpenMP 2.5:
32607 num_threads ( expression ) */
32608
32609 static tree
32610 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32611 location_t location)
32612 {
32613 tree t, c;
32614
32615 matching_parens parens;
32616 if (!parens.require_open (parser))
32617 return list;
32618
32619 t = cp_parser_expression (parser);
32620
32621 if (t == error_mark_node
32622 || !parens.require_close (parser))
32623 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32624 /*or_comma=*/false,
32625 /*consume_paren=*/true);
32626
32627 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32628 "num_threads", location);
32629
32630 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32631 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32632 OMP_CLAUSE_CHAIN (c) = list;
32633
32634 return c;
32635 }
32636
32637 /* OpenMP 4.5:
32638 num_tasks ( expression ) */
32639
32640 static tree
32641 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32642 location_t location)
32643 {
32644 tree t, c;
32645
32646 matching_parens parens;
32647 if (!parens.require_open (parser))
32648 return list;
32649
32650 t = cp_parser_expression (parser);
32651
32652 if (t == error_mark_node
32653 || !parens.require_close (parser))
32654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32655 /*or_comma=*/false,
32656 /*consume_paren=*/true);
32657
32658 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32659 "num_tasks", location);
32660
32661 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32662 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32663 OMP_CLAUSE_CHAIN (c) = list;
32664
32665 return c;
32666 }
32667
32668 /* OpenMP 4.5:
32669 grainsize ( expression ) */
32670
32671 static tree
32672 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32673 location_t location)
32674 {
32675 tree t, c;
32676
32677 matching_parens parens;
32678 if (!parens.require_open (parser))
32679 return list;
32680
32681 t = cp_parser_expression (parser);
32682
32683 if (t == error_mark_node
32684 || !parens.require_close (parser))
32685 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32686 /*or_comma=*/false,
32687 /*consume_paren=*/true);
32688
32689 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32690 "grainsize", location);
32691
32692 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32693 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32694 OMP_CLAUSE_CHAIN (c) = list;
32695
32696 return c;
32697 }
32698
32699 /* OpenMP 4.5:
32700 priority ( expression ) */
32701
32702 static tree
32703 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32704 location_t location)
32705 {
32706 tree t, c;
32707
32708 matching_parens parens;
32709 if (!parens.require_open (parser))
32710 return list;
32711
32712 t = cp_parser_expression (parser);
32713
32714 if (t == error_mark_node
32715 || !parens.require_close (parser))
32716 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32717 /*or_comma=*/false,
32718 /*consume_paren=*/true);
32719
32720 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32721 "priority", location);
32722
32723 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32724 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32725 OMP_CLAUSE_CHAIN (c) = list;
32726
32727 return c;
32728 }
32729
32730 /* OpenMP 4.5:
32731 hint ( expression ) */
32732
32733 static tree
32734 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32735 location_t location)
32736 {
32737 tree t, c;
32738
32739 matching_parens parens;
32740 if (!parens.require_open (parser))
32741 return list;
32742
32743 t = cp_parser_expression (parser);
32744
32745 if (t == error_mark_node
32746 || !parens.require_close (parser))
32747 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32748 /*or_comma=*/false,
32749 /*consume_paren=*/true);
32750
32751 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32752
32753 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32754 OMP_CLAUSE_HINT_EXPR (c) = t;
32755 OMP_CLAUSE_CHAIN (c) = list;
32756
32757 return c;
32758 }
32759
32760 /* OpenMP 4.5:
32761 defaultmap ( tofrom : scalar ) */
32762
32763 static tree
32764 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32765 location_t location)
32766 {
32767 tree c, id;
32768 const char *p;
32769
32770 matching_parens parens;
32771 if (!parens.require_open (parser))
32772 return list;
32773
32774 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32775 {
32776 cp_parser_error (parser, "expected %<tofrom%>");
32777 goto out_err;
32778 }
32779 id = cp_lexer_peek_token (parser->lexer)->u.value;
32780 p = IDENTIFIER_POINTER (id);
32781 if (strcmp (p, "tofrom") != 0)
32782 {
32783 cp_parser_error (parser, "expected %<tofrom%>");
32784 goto out_err;
32785 }
32786 cp_lexer_consume_token (parser->lexer);
32787 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32788 goto out_err;
32789
32790 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32791 {
32792 cp_parser_error (parser, "expected %<scalar%>");
32793 goto out_err;
32794 }
32795 id = cp_lexer_peek_token (parser->lexer)->u.value;
32796 p = IDENTIFIER_POINTER (id);
32797 if (strcmp (p, "scalar") != 0)
32798 {
32799 cp_parser_error (parser, "expected %<scalar%>");
32800 goto out_err;
32801 }
32802 cp_lexer_consume_token (parser->lexer);
32803 if (!parens.require_close (parser))
32804 goto out_err;
32805
32806 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32807 location);
32808
32809 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32810 OMP_CLAUSE_CHAIN (c) = list;
32811 return c;
32812
32813 out_err:
32814 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32815 /*or_comma=*/false,
32816 /*consume_paren=*/true);
32817 return list;
32818 }
32819
32820 /* OpenMP 2.5:
32821 ordered
32822
32823 OpenMP 4.5:
32824 ordered ( constant-expression ) */
32825
32826 static tree
32827 cp_parser_omp_clause_ordered (cp_parser *parser,
32828 tree list, location_t location)
32829 {
32830 tree c, num = NULL_TREE;
32831 HOST_WIDE_INT n;
32832
32833 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32834 "ordered", location);
32835
32836 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32837 {
32838 matching_parens parens;
32839 parens.consume_open (parser);
32840
32841 num = cp_parser_constant_expression (parser);
32842
32843 if (!parens.require_close (parser))
32844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32845 /*or_comma=*/false,
32846 /*consume_paren=*/true);
32847
32848 if (num == error_mark_node)
32849 return list;
32850 num = fold_non_dependent_expr (num);
32851 if (!tree_fits_shwi_p (num)
32852 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32853 || (n = tree_to_shwi (num)) <= 0
32854 || (int) n != n)
32855 {
32856 error_at (location,
32857 "ordered argument needs positive constant integer "
32858 "expression");
32859 return list;
32860 }
32861 }
32862
32863 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32864 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32865 OMP_CLAUSE_CHAIN (c) = list;
32866 return c;
32867 }
32868
32869 /* OpenMP 2.5:
32870 reduction ( reduction-operator : variable-list )
32871
32872 reduction-operator:
32873 One of: + * - & ^ | && ||
32874
32875 OpenMP 3.1:
32876
32877 reduction-operator:
32878 One of: + * - & ^ | && || min max
32879
32880 OpenMP 4.0:
32881
32882 reduction-operator:
32883 One of: + * - & ^ | && ||
32884 id-expression */
32885
32886 static tree
32887 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32888 {
32889 enum tree_code code = ERROR_MARK;
32890 tree nlist, c, id = NULL_TREE;
32891
32892 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32893 return list;
32894
32895 switch (cp_lexer_peek_token (parser->lexer)->type)
32896 {
32897 case CPP_PLUS: code = PLUS_EXPR; break;
32898 case CPP_MULT: code = MULT_EXPR; break;
32899 case CPP_MINUS: code = MINUS_EXPR; break;
32900 case CPP_AND: code = BIT_AND_EXPR; break;
32901 case CPP_XOR: code = BIT_XOR_EXPR; break;
32902 case CPP_OR: code = BIT_IOR_EXPR; break;
32903 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32904 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32905 default: break;
32906 }
32907
32908 if (code != ERROR_MARK)
32909 cp_lexer_consume_token (parser->lexer);
32910 else
32911 {
32912 bool saved_colon_corrects_to_scope_p;
32913 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32914 parser->colon_corrects_to_scope_p = false;
32915 id = cp_parser_id_expression (parser, /*template_p=*/false,
32916 /*check_dependency_p=*/true,
32917 /*template_p=*/NULL,
32918 /*declarator_p=*/false,
32919 /*optional_p=*/false);
32920 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32921 if (identifier_p (id))
32922 {
32923 const char *p = IDENTIFIER_POINTER (id);
32924
32925 if (strcmp (p, "min") == 0)
32926 code = MIN_EXPR;
32927 else if (strcmp (p, "max") == 0)
32928 code = MAX_EXPR;
32929 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32930 code = PLUS_EXPR;
32931 else if (id == ovl_op_identifier (false, MULT_EXPR))
32932 code = MULT_EXPR;
32933 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32934 code = MINUS_EXPR;
32935 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32936 code = BIT_AND_EXPR;
32937 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32938 code = BIT_IOR_EXPR;
32939 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32940 code = BIT_XOR_EXPR;
32941 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32942 code = TRUTH_ANDIF_EXPR;
32943 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32944 code = TRUTH_ORIF_EXPR;
32945 id = omp_reduction_id (code, id, NULL_TREE);
32946 tree scope = parser->scope;
32947 if (scope)
32948 id = build_qualified_name (NULL_TREE, scope, id, false);
32949 parser->scope = NULL_TREE;
32950 parser->qualifying_scope = NULL_TREE;
32951 parser->object_scope = NULL_TREE;
32952 }
32953 else
32954 {
32955 error ("invalid reduction-identifier");
32956 resync_fail:
32957 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32958 /*or_comma=*/false,
32959 /*consume_paren=*/true);
32960 return list;
32961 }
32962 }
32963
32964 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32965 goto resync_fail;
32966
32967 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32968 NULL);
32969 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32970 {
32971 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32972 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32973 }
32974
32975 return nlist;
32976 }
32977
32978 /* OpenMP 2.5:
32979 schedule ( schedule-kind )
32980 schedule ( schedule-kind , expression )
32981
32982 schedule-kind:
32983 static | dynamic | guided | runtime | auto
32984
32985 OpenMP 4.5:
32986 schedule ( schedule-modifier : schedule-kind )
32987 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32988
32989 schedule-modifier:
32990 simd
32991 monotonic
32992 nonmonotonic */
32993
32994 static tree
32995 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32996 {
32997 tree c, t;
32998 int modifiers = 0, nmodifiers = 0;
32999
33000 matching_parens parens;
33001 if (!parens.require_open (parser))
33002 return list;
33003
33004 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33005
33006 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33007 {
33008 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33009 const char *p = IDENTIFIER_POINTER (id);
33010 if (strcmp ("simd", p) == 0)
33011 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33012 else if (strcmp ("monotonic", p) == 0)
33013 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33014 else if (strcmp ("nonmonotonic", p) == 0)
33015 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33016 else
33017 break;
33018 cp_lexer_consume_token (parser->lexer);
33019 if (nmodifiers++ == 0
33020 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33021 cp_lexer_consume_token (parser->lexer);
33022 else
33023 {
33024 cp_parser_require (parser, CPP_COLON, RT_COLON);
33025 break;
33026 }
33027 }
33028
33029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33030 {
33031 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33032 const char *p = IDENTIFIER_POINTER (id);
33033
33034 switch (p[0])
33035 {
33036 case 'd':
33037 if (strcmp ("dynamic", p) != 0)
33038 goto invalid_kind;
33039 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33040 break;
33041
33042 case 'g':
33043 if (strcmp ("guided", p) != 0)
33044 goto invalid_kind;
33045 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33046 break;
33047
33048 case 'r':
33049 if (strcmp ("runtime", p) != 0)
33050 goto invalid_kind;
33051 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33052 break;
33053
33054 default:
33055 goto invalid_kind;
33056 }
33057 }
33058 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33059 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33060 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33061 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33062 else
33063 goto invalid_kind;
33064 cp_lexer_consume_token (parser->lexer);
33065
33066 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33067 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33068 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33069 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33070 {
33071 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33072 "specified");
33073 modifiers = 0;
33074 }
33075
33076 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33077 {
33078 cp_token *token;
33079 cp_lexer_consume_token (parser->lexer);
33080
33081 token = cp_lexer_peek_token (parser->lexer);
33082 t = cp_parser_assignment_expression (parser);
33083
33084 if (t == error_mark_node)
33085 goto resync_fail;
33086 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33087 error_at (token->location, "schedule %<runtime%> does not take "
33088 "a %<chunk_size%> parameter");
33089 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33090 error_at (token->location, "schedule %<auto%> does not take "
33091 "a %<chunk_size%> parameter");
33092 else
33093 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33094
33095 if (!parens.require_close (parser))
33096 goto resync_fail;
33097 }
33098 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33099 goto resync_fail;
33100
33101 OMP_CLAUSE_SCHEDULE_KIND (c)
33102 = (enum omp_clause_schedule_kind)
33103 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33104
33105 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33106 OMP_CLAUSE_CHAIN (c) = list;
33107 return c;
33108
33109 invalid_kind:
33110 cp_parser_error (parser, "invalid schedule kind");
33111 resync_fail:
33112 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33113 /*or_comma=*/false,
33114 /*consume_paren=*/true);
33115 return list;
33116 }
33117
33118 /* OpenMP 3.0:
33119 untied */
33120
33121 static tree
33122 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33123 tree list, location_t location)
33124 {
33125 tree c;
33126
33127 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33128
33129 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33130 OMP_CLAUSE_CHAIN (c) = list;
33131 return c;
33132 }
33133
33134 /* OpenMP 4.0:
33135 inbranch
33136 notinbranch */
33137
33138 static tree
33139 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33140 tree list, location_t location)
33141 {
33142 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33143 tree c = build_omp_clause (location, code);
33144 OMP_CLAUSE_CHAIN (c) = list;
33145 return c;
33146 }
33147
33148 /* OpenMP 4.0:
33149 parallel
33150 for
33151 sections
33152 taskgroup */
33153
33154 static tree
33155 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33156 enum omp_clause_code code,
33157 tree list, location_t location)
33158 {
33159 tree c = build_omp_clause (location, code);
33160 OMP_CLAUSE_CHAIN (c) = list;
33161 return c;
33162 }
33163
33164 /* OpenMP 4.5:
33165 nogroup */
33166
33167 static tree
33168 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33169 tree list, location_t location)
33170 {
33171 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33172 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33173 OMP_CLAUSE_CHAIN (c) = list;
33174 return c;
33175 }
33176
33177 /* OpenMP 4.5:
33178 simd
33179 threads */
33180
33181 static tree
33182 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33183 enum omp_clause_code code,
33184 tree list, location_t location)
33185 {
33186 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33187 tree c = build_omp_clause (location, code);
33188 OMP_CLAUSE_CHAIN (c) = list;
33189 return c;
33190 }
33191
33192 /* OpenMP 4.0:
33193 num_teams ( expression ) */
33194
33195 static tree
33196 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33197 location_t location)
33198 {
33199 tree t, c;
33200
33201 matching_parens parens;
33202 if (!parens.require_open (parser))
33203 return list;
33204
33205 t = cp_parser_expression (parser);
33206
33207 if (t == error_mark_node
33208 || !parens.require_close (parser))
33209 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33210 /*or_comma=*/false,
33211 /*consume_paren=*/true);
33212
33213 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33214 "num_teams", location);
33215
33216 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33217 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33218 OMP_CLAUSE_CHAIN (c) = list;
33219
33220 return c;
33221 }
33222
33223 /* OpenMP 4.0:
33224 thread_limit ( expression ) */
33225
33226 static tree
33227 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33228 location_t location)
33229 {
33230 tree t, c;
33231
33232 matching_parens parens;
33233 if (!parens.require_open (parser))
33234 return list;
33235
33236 t = cp_parser_expression (parser);
33237
33238 if (t == error_mark_node
33239 || !parens.require_close (parser))
33240 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33241 /*or_comma=*/false,
33242 /*consume_paren=*/true);
33243
33244 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33245 "thread_limit", location);
33246
33247 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33248 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33249 OMP_CLAUSE_CHAIN (c) = list;
33250
33251 return c;
33252 }
33253
33254 /* OpenMP 4.0:
33255 aligned ( variable-list )
33256 aligned ( variable-list : constant-expression ) */
33257
33258 static tree
33259 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33260 {
33261 tree nlist, c, alignment = NULL_TREE;
33262 bool colon;
33263
33264 matching_parens parens;
33265 if (!parens.require_open (parser))
33266 return list;
33267
33268 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33269 &colon);
33270
33271 if (colon)
33272 {
33273 alignment = cp_parser_constant_expression (parser);
33274
33275 if (!parens.require_close (parser))
33276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33279
33280 if (alignment == error_mark_node)
33281 alignment = NULL_TREE;
33282 }
33283
33284 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33285 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33286
33287 return nlist;
33288 }
33289
33290 /* OpenMP 4.0:
33291 linear ( variable-list )
33292 linear ( variable-list : expression )
33293
33294 OpenMP 4.5:
33295 linear ( modifier ( variable-list ) )
33296 linear ( modifier ( variable-list ) : expression ) */
33297
33298 static tree
33299 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33300 bool declare_simd)
33301 {
33302 tree nlist, c, step = integer_one_node;
33303 bool colon;
33304 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33305
33306 matching_parens parens;
33307 if (!parens.require_open (parser))
33308 return list;
33309
33310 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33311 {
33312 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33313 const char *p = IDENTIFIER_POINTER (id);
33314
33315 if (strcmp ("ref", p) == 0)
33316 kind = OMP_CLAUSE_LINEAR_REF;
33317 else if (strcmp ("val", p) == 0)
33318 kind = OMP_CLAUSE_LINEAR_VAL;
33319 else if (strcmp ("uval", p) == 0)
33320 kind = OMP_CLAUSE_LINEAR_UVAL;
33321 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33322 cp_lexer_consume_token (parser->lexer);
33323 else
33324 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33325 }
33326
33327 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33328 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33329 &colon);
33330 else
33331 {
33332 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33333 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33334 if (colon)
33335 cp_parser_require (parser, CPP_COLON, RT_COLON);
33336 else if (!parens.require_close (parser))
33337 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33338 /*or_comma=*/false,
33339 /*consume_paren=*/true);
33340 }
33341
33342 if (colon)
33343 {
33344 step = NULL_TREE;
33345 if (declare_simd
33346 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33347 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33348 {
33349 cp_token *token = cp_lexer_peek_token (parser->lexer);
33350 cp_parser_parse_tentatively (parser);
33351 step = cp_parser_id_expression (parser, /*template_p=*/false,
33352 /*check_dependency_p=*/true,
33353 /*template_p=*/NULL,
33354 /*declarator_p=*/false,
33355 /*optional_p=*/false);
33356 if (step != error_mark_node)
33357 step = cp_parser_lookup_name_simple (parser, step, token->location);
33358 if (step == error_mark_node)
33359 {
33360 step = NULL_TREE;
33361 cp_parser_abort_tentative_parse (parser);
33362 }
33363 else if (!cp_parser_parse_definitely (parser))
33364 step = NULL_TREE;
33365 }
33366 if (!step)
33367 step = cp_parser_expression (parser);
33368
33369 if (!parens.require_close (parser))
33370 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33371 /*or_comma=*/false,
33372 /*consume_paren=*/true);
33373
33374 if (step == error_mark_node)
33375 return list;
33376 }
33377
33378 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33379 {
33380 OMP_CLAUSE_LINEAR_STEP (c) = step;
33381 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33382 }
33383
33384 return nlist;
33385 }
33386
33387 /* OpenMP 4.0:
33388 safelen ( constant-expression ) */
33389
33390 static tree
33391 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33392 location_t location)
33393 {
33394 tree t, c;
33395
33396 matching_parens parens;
33397 if (!parens.require_open (parser))
33398 return list;
33399
33400 t = cp_parser_constant_expression (parser);
33401
33402 if (t == error_mark_node
33403 || !parens.require_close (parser))
33404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33405 /*or_comma=*/false,
33406 /*consume_paren=*/true);
33407
33408 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33409
33410 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33411 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33412 OMP_CLAUSE_CHAIN (c) = list;
33413
33414 return c;
33415 }
33416
33417 /* OpenMP 4.0:
33418 simdlen ( constant-expression ) */
33419
33420 static tree
33421 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33422 location_t location)
33423 {
33424 tree t, c;
33425
33426 matching_parens parens;
33427 if (!parens.require_open (parser))
33428 return list;
33429
33430 t = cp_parser_constant_expression (parser);
33431
33432 if (t == error_mark_node
33433 || !parens.require_close (parser))
33434 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33435 /*or_comma=*/false,
33436 /*consume_paren=*/true);
33437
33438 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33439
33440 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33441 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33442 OMP_CLAUSE_CHAIN (c) = list;
33443
33444 return c;
33445 }
33446
33447 /* OpenMP 4.5:
33448 vec:
33449 identifier [+/- integer]
33450 vec , identifier [+/- integer]
33451 */
33452
33453 static tree
33454 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33455 tree list)
33456 {
33457 tree vec = NULL;
33458
33459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33460 {
33461 cp_parser_error (parser, "expected identifier");
33462 return list;
33463 }
33464
33465 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33466 {
33467 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33468 tree t, identifier = cp_parser_identifier (parser);
33469 tree addend = NULL;
33470
33471 if (identifier == error_mark_node)
33472 t = error_mark_node;
33473 else
33474 {
33475 t = cp_parser_lookup_name_simple
33476 (parser, identifier,
33477 cp_lexer_peek_token (parser->lexer)->location);
33478 if (t == error_mark_node)
33479 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33480 id_loc);
33481 }
33482
33483 bool neg = false;
33484 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33485 neg = true;
33486 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33487 {
33488 addend = integer_zero_node;
33489 goto add_to_vector;
33490 }
33491 cp_lexer_consume_token (parser->lexer);
33492
33493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33494 {
33495 cp_parser_error (parser, "expected integer");
33496 return list;
33497 }
33498
33499 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33500 if (TREE_CODE (addend) != INTEGER_CST)
33501 {
33502 cp_parser_error (parser, "expected integer");
33503 return list;
33504 }
33505 cp_lexer_consume_token (parser->lexer);
33506
33507 add_to_vector:
33508 if (t != error_mark_node)
33509 {
33510 vec = tree_cons (addend, t, vec);
33511 if (neg)
33512 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33513 }
33514
33515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33516 break;
33517
33518 cp_lexer_consume_token (parser->lexer);
33519 }
33520
33521 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33522 {
33523 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33524 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33525 OMP_CLAUSE_DECL (u) = nreverse (vec);
33526 OMP_CLAUSE_CHAIN (u) = list;
33527 return u;
33528 }
33529 return list;
33530 }
33531
33532 /* OpenMP 4.0:
33533 depend ( depend-kind : variable-list )
33534
33535 depend-kind:
33536 in | out | inout
33537
33538 OpenMP 4.5:
33539 depend ( source )
33540
33541 depend ( sink : vec ) */
33542
33543 static tree
33544 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33545 {
33546 tree nlist, c;
33547 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33548
33549 matching_parens parens;
33550 if (!parens.require_open (parser))
33551 return list;
33552
33553 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33554 {
33555 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33556 const char *p = IDENTIFIER_POINTER (id);
33557
33558 if (strcmp ("in", p) == 0)
33559 kind = OMP_CLAUSE_DEPEND_IN;
33560 else if (strcmp ("inout", p) == 0)
33561 kind = OMP_CLAUSE_DEPEND_INOUT;
33562 else if (strcmp ("out", p) == 0)
33563 kind = OMP_CLAUSE_DEPEND_OUT;
33564 else if (strcmp ("source", p) == 0)
33565 kind = OMP_CLAUSE_DEPEND_SOURCE;
33566 else if (strcmp ("sink", p) == 0)
33567 kind = OMP_CLAUSE_DEPEND_SINK;
33568 else
33569 goto invalid_kind;
33570 }
33571 else
33572 goto invalid_kind;
33573
33574 cp_lexer_consume_token (parser->lexer);
33575
33576 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33577 {
33578 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33579 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33580 OMP_CLAUSE_DECL (c) = NULL_TREE;
33581 OMP_CLAUSE_CHAIN (c) = list;
33582 if (!parens.require_close (parser))
33583 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33584 /*or_comma=*/false,
33585 /*consume_paren=*/true);
33586 return c;
33587 }
33588
33589 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33590 goto resync_fail;
33591
33592 if (kind == OMP_CLAUSE_DEPEND_SINK)
33593 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33594 else
33595 {
33596 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33597 list, NULL);
33598
33599 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33600 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33601 }
33602 return nlist;
33603
33604 invalid_kind:
33605 cp_parser_error (parser, "invalid depend kind");
33606 resync_fail:
33607 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33608 /*or_comma=*/false,
33609 /*consume_paren=*/true);
33610 return list;
33611 }
33612
33613 /* OpenMP 4.0:
33614 map ( map-kind : variable-list )
33615 map ( variable-list )
33616
33617 map-kind:
33618 alloc | to | from | tofrom
33619
33620 OpenMP 4.5:
33621 map-kind:
33622 alloc | to | from | tofrom | release | delete
33623
33624 map ( always [,] map-kind: variable-list ) */
33625
33626 static tree
33627 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33628 {
33629 tree nlist, c;
33630 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33631 bool always = false;
33632
33633 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33634 return list;
33635
33636 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33637 {
33638 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33639 const char *p = IDENTIFIER_POINTER (id);
33640
33641 if (strcmp ("always", p) == 0)
33642 {
33643 int nth = 2;
33644 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33645 nth++;
33646 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33647 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33648 == RID_DELETE))
33649 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33650 == CPP_COLON))
33651 {
33652 always = true;
33653 cp_lexer_consume_token (parser->lexer);
33654 if (nth == 3)
33655 cp_lexer_consume_token (parser->lexer);
33656 }
33657 }
33658 }
33659
33660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33661 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33662 {
33663 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33664 const char *p = IDENTIFIER_POINTER (id);
33665
33666 if (strcmp ("alloc", p) == 0)
33667 kind = GOMP_MAP_ALLOC;
33668 else if (strcmp ("to", p) == 0)
33669 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33670 else if (strcmp ("from", p) == 0)
33671 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33672 else if (strcmp ("tofrom", p) == 0)
33673 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33674 else if (strcmp ("release", p) == 0)
33675 kind = GOMP_MAP_RELEASE;
33676 else
33677 {
33678 cp_parser_error (parser, "invalid map kind");
33679 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33680 /*or_comma=*/false,
33681 /*consume_paren=*/true);
33682 return list;
33683 }
33684 cp_lexer_consume_token (parser->lexer);
33685 cp_lexer_consume_token (parser->lexer);
33686 }
33687 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33688 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33689 {
33690 kind = GOMP_MAP_DELETE;
33691 cp_lexer_consume_token (parser->lexer);
33692 cp_lexer_consume_token (parser->lexer);
33693 }
33694
33695 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33696 NULL);
33697
33698 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33699 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33700
33701 return nlist;
33702 }
33703
33704 /* OpenMP 4.0:
33705 device ( expression ) */
33706
33707 static tree
33708 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33709 location_t location)
33710 {
33711 tree t, c;
33712
33713 matching_parens parens;
33714 if (!parens.require_open (parser))
33715 return list;
33716
33717 t = cp_parser_expression (parser);
33718
33719 if (t == error_mark_node
33720 || !parens.require_close (parser))
33721 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33722 /*or_comma=*/false,
33723 /*consume_paren=*/true);
33724
33725 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33726 "device", location);
33727
33728 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33729 OMP_CLAUSE_DEVICE_ID (c) = t;
33730 OMP_CLAUSE_CHAIN (c) = list;
33731
33732 return c;
33733 }
33734
33735 /* OpenMP 4.0:
33736 dist_schedule ( static )
33737 dist_schedule ( static , expression ) */
33738
33739 static tree
33740 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33741 location_t location)
33742 {
33743 tree c, t;
33744
33745 matching_parens parens;
33746 if (!parens.require_open (parser))
33747 return list;
33748
33749 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33750
33751 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33752 goto invalid_kind;
33753 cp_lexer_consume_token (parser->lexer);
33754
33755 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33756 {
33757 cp_lexer_consume_token (parser->lexer);
33758
33759 t = cp_parser_assignment_expression (parser);
33760
33761 if (t == error_mark_node)
33762 goto resync_fail;
33763 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33764
33765 if (!parens.require_close (parser))
33766 goto resync_fail;
33767 }
33768 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33769 goto resync_fail;
33770
33771 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33772 location);
33773 OMP_CLAUSE_CHAIN (c) = list;
33774 return c;
33775
33776 invalid_kind:
33777 cp_parser_error (parser, "invalid dist_schedule kind");
33778 resync_fail:
33779 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33780 /*or_comma=*/false,
33781 /*consume_paren=*/true);
33782 return list;
33783 }
33784
33785 /* OpenMP 4.0:
33786 proc_bind ( proc-bind-kind )
33787
33788 proc-bind-kind:
33789 master | close | spread */
33790
33791 static tree
33792 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33793 location_t location)
33794 {
33795 tree c;
33796 enum omp_clause_proc_bind_kind kind;
33797
33798 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33799 return list;
33800
33801 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33802 {
33803 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33804 const char *p = IDENTIFIER_POINTER (id);
33805
33806 if (strcmp ("master", p) == 0)
33807 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33808 else if (strcmp ("close", p) == 0)
33809 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33810 else if (strcmp ("spread", p) == 0)
33811 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33812 else
33813 goto invalid_kind;
33814 }
33815 else
33816 goto invalid_kind;
33817
33818 cp_lexer_consume_token (parser->lexer);
33819 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33820 goto resync_fail;
33821
33822 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33823 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33824 location);
33825 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33826 OMP_CLAUSE_CHAIN (c) = list;
33827 return c;
33828
33829 invalid_kind:
33830 cp_parser_error (parser, "invalid depend kind");
33831 resync_fail:
33832 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33833 /*or_comma=*/false,
33834 /*consume_paren=*/true);
33835 return list;
33836 }
33837
33838 /* OpenACC:
33839 async [( int-expr )] */
33840
33841 static tree
33842 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33843 {
33844 tree c, t;
33845 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33846
33847 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33848
33849 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33850 {
33851 matching_parens parens;
33852 parens.consume_open (parser);
33853
33854 t = cp_parser_expression (parser);
33855 if (t == error_mark_node
33856 || !parens.require_close (parser))
33857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33858 /*or_comma=*/false,
33859 /*consume_paren=*/true);
33860 }
33861
33862 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33863
33864 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33865 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33866 OMP_CLAUSE_CHAIN (c) = list;
33867 list = c;
33868
33869 return list;
33870 }
33871
33872 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33873 is a bitmask in MASK. Return the list of clauses found. */
33874
33875 static tree
33876 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33877 const char *where, cp_token *pragma_tok,
33878 bool finish_p = true)
33879 {
33880 tree clauses = NULL;
33881 bool first = true;
33882
33883 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33884 {
33885 location_t here;
33886 pragma_omp_clause c_kind;
33887 omp_clause_code code;
33888 const char *c_name;
33889 tree prev = clauses;
33890
33891 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33892 cp_lexer_consume_token (parser->lexer);
33893
33894 here = cp_lexer_peek_token (parser->lexer)->location;
33895 c_kind = cp_parser_omp_clause_name (parser);
33896
33897 switch (c_kind)
33898 {
33899 case PRAGMA_OACC_CLAUSE_ASYNC:
33900 clauses = cp_parser_oacc_clause_async (parser, clauses);
33901 c_name = "async";
33902 break;
33903 case PRAGMA_OACC_CLAUSE_AUTO:
33904 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33905 clauses, here);
33906 c_name = "auto";
33907 break;
33908 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33909 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33910 c_name = "collapse";
33911 break;
33912 case PRAGMA_OACC_CLAUSE_COPY:
33913 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33914 c_name = "copy";
33915 break;
33916 case PRAGMA_OACC_CLAUSE_COPYIN:
33917 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33918 c_name = "copyin";
33919 break;
33920 case PRAGMA_OACC_CLAUSE_COPYOUT:
33921 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33922 c_name = "copyout";
33923 break;
33924 case PRAGMA_OACC_CLAUSE_CREATE:
33925 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33926 c_name = "create";
33927 break;
33928 case PRAGMA_OACC_CLAUSE_DELETE:
33929 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33930 c_name = "delete";
33931 break;
33932 case PRAGMA_OMP_CLAUSE_DEFAULT:
33933 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33934 c_name = "default";
33935 break;
33936 case PRAGMA_OACC_CLAUSE_DEVICE:
33937 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33938 c_name = "device";
33939 break;
33940 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33941 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33942 c_name = "deviceptr";
33943 break;
33944 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33945 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33946 c_name = "device_resident";
33947 break;
33948 case PRAGMA_OACC_CLAUSE_FINALIZE:
33949 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
33950 clauses, here);
33951 c_name = "finalize";
33952 break;
33953 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33954 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33955 clauses);
33956 c_name = "firstprivate";
33957 break;
33958 case PRAGMA_OACC_CLAUSE_GANG:
33959 c_name = "gang";
33960 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33961 c_name, clauses);
33962 break;
33963 case PRAGMA_OACC_CLAUSE_HOST:
33964 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33965 c_name = "host";
33966 break;
33967 case PRAGMA_OACC_CLAUSE_IF:
33968 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33969 c_name = "if";
33970 break;
33971 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
33972 clauses = cp_parser_oacc_simple_clause (parser,
33973 OMP_CLAUSE_IF_PRESENT,
33974 clauses, here);
33975 c_name = "if_present";
33976 break;
33977 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33978 clauses = cp_parser_oacc_simple_clause (parser,
33979 OMP_CLAUSE_INDEPENDENT,
33980 clauses, here);
33981 c_name = "independent";
33982 break;
33983 case PRAGMA_OACC_CLAUSE_LINK:
33984 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33985 c_name = "link";
33986 break;
33987 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33988 code = OMP_CLAUSE_NUM_GANGS;
33989 c_name = "num_gangs";
33990 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33991 clauses);
33992 break;
33993 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33994 c_name = "num_workers";
33995 code = OMP_CLAUSE_NUM_WORKERS;
33996 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33997 clauses);
33998 break;
33999 case PRAGMA_OACC_CLAUSE_PRESENT:
34000 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34001 c_name = "present";
34002 break;
34003 case PRAGMA_OACC_CLAUSE_PRIVATE:
34004 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34005 clauses);
34006 c_name = "private";
34007 break;
34008 case PRAGMA_OACC_CLAUSE_REDUCTION:
34009 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34010 c_name = "reduction";
34011 break;
34012 case PRAGMA_OACC_CLAUSE_SEQ:
34013 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34014 clauses, here);
34015 c_name = "seq";
34016 break;
34017 case PRAGMA_OACC_CLAUSE_TILE:
34018 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34019 c_name = "tile";
34020 break;
34021 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34022 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34023 clauses);
34024 c_name = "use_device";
34025 break;
34026 case PRAGMA_OACC_CLAUSE_VECTOR:
34027 c_name = "vector";
34028 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34029 c_name, clauses);
34030 break;
34031 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34032 c_name = "vector_length";
34033 code = OMP_CLAUSE_VECTOR_LENGTH;
34034 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34035 clauses);
34036 break;
34037 case PRAGMA_OACC_CLAUSE_WAIT:
34038 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34039 c_name = "wait";
34040 break;
34041 case PRAGMA_OACC_CLAUSE_WORKER:
34042 c_name = "worker";
34043 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34044 c_name, clauses);
34045 break;
34046 default:
34047 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34048 goto saw_error;
34049 }
34050
34051 first = false;
34052
34053 if (((mask >> c_kind) & 1) == 0)
34054 {
34055 /* Remove the invalid clause(s) from the list to avoid
34056 confusing the rest of the compiler. */
34057 clauses = prev;
34058 error_at (here, "%qs is not valid for %qs", c_name, where);
34059 }
34060 }
34061
34062 saw_error:
34063 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34064
34065 if (finish_p)
34066 return finish_omp_clauses (clauses, C_ORT_ACC);
34067
34068 return clauses;
34069 }
34070
34071 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34072 is a bitmask in MASK. Return the list of clauses found; the result
34073 of clause default goes in *pdefault. */
34074
34075 static tree
34076 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34077 const char *where, cp_token *pragma_tok,
34078 bool finish_p = true)
34079 {
34080 tree clauses = NULL;
34081 bool first = true;
34082 cp_token *token = NULL;
34083
34084 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34085 {
34086 pragma_omp_clause c_kind;
34087 const char *c_name;
34088 tree prev = clauses;
34089
34090 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34091 cp_lexer_consume_token (parser->lexer);
34092
34093 token = cp_lexer_peek_token (parser->lexer);
34094 c_kind = cp_parser_omp_clause_name (parser);
34095
34096 switch (c_kind)
34097 {
34098 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34099 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34100 token->location);
34101 c_name = "collapse";
34102 break;
34103 case PRAGMA_OMP_CLAUSE_COPYIN:
34104 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34105 c_name = "copyin";
34106 break;
34107 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34108 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34109 clauses);
34110 c_name = "copyprivate";
34111 break;
34112 case PRAGMA_OMP_CLAUSE_DEFAULT:
34113 clauses = cp_parser_omp_clause_default (parser, clauses,
34114 token->location, false);
34115 c_name = "default";
34116 break;
34117 case PRAGMA_OMP_CLAUSE_FINAL:
34118 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34119 c_name = "final";
34120 break;
34121 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34122 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34123 clauses);
34124 c_name = "firstprivate";
34125 break;
34126 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34127 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34128 token->location);
34129 c_name = "grainsize";
34130 break;
34131 case PRAGMA_OMP_CLAUSE_HINT:
34132 clauses = cp_parser_omp_clause_hint (parser, clauses,
34133 token->location);
34134 c_name = "hint";
34135 break;
34136 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34137 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34138 token->location);
34139 c_name = "defaultmap";
34140 break;
34141 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34142 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34143 clauses);
34144 c_name = "use_device_ptr";
34145 break;
34146 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34147 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34148 clauses);
34149 c_name = "is_device_ptr";
34150 break;
34151 case PRAGMA_OMP_CLAUSE_IF:
34152 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34153 true);
34154 c_name = "if";
34155 break;
34156 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34157 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34158 clauses);
34159 c_name = "lastprivate";
34160 break;
34161 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34162 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34163 token->location);
34164 c_name = "mergeable";
34165 break;
34166 case PRAGMA_OMP_CLAUSE_NOWAIT:
34167 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34168 c_name = "nowait";
34169 break;
34170 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34171 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34172 token->location);
34173 c_name = "num_tasks";
34174 break;
34175 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34176 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34177 token->location);
34178 c_name = "num_threads";
34179 break;
34180 case PRAGMA_OMP_CLAUSE_ORDERED:
34181 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34182 token->location);
34183 c_name = "ordered";
34184 break;
34185 case PRAGMA_OMP_CLAUSE_PRIORITY:
34186 clauses = cp_parser_omp_clause_priority (parser, clauses,
34187 token->location);
34188 c_name = "priority";
34189 break;
34190 case PRAGMA_OMP_CLAUSE_PRIVATE:
34191 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34192 clauses);
34193 c_name = "private";
34194 break;
34195 case PRAGMA_OMP_CLAUSE_REDUCTION:
34196 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34197 c_name = "reduction";
34198 break;
34199 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34200 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34201 token->location);
34202 c_name = "schedule";
34203 break;
34204 case PRAGMA_OMP_CLAUSE_SHARED:
34205 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34206 clauses);
34207 c_name = "shared";
34208 break;
34209 case PRAGMA_OMP_CLAUSE_UNTIED:
34210 clauses = cp_parser_omp_clause_untied (parser, clauses,
34211 token->location);
34212 c_name = "untied";
34213 break;
34214 case PRAGMA_OMP_CLAUSE_INBRANCH:
34215 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34216 clauses, token->location);
34217 c_name = "inbranch";
34218 break;
34219 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34220 clauses = cp_parser_omp_clause_branch (parser,
34221 OMP_CLAUSE_NOTINBRANCH,
34222 clauses, token->location);
34223 c_name = "notinbranch";
34224 break;
34225 case PRAGMA_OMP_CLAUSE_PARALLEL:
34226 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34227 clauses, token->location);
34228 c_name = "parallel";
34229 if (!first)
34230 {
34231 clause_not_first:
34232 error_at (token->location, "%qs must be the first clause of %qs",
34233 c_name, where);
34234 clauses = prev;
34235 }
34236 break;
34237 case PRAGMA_OMP_CLAUSE_FOR:
34238 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34239 clauses, token->location);
34240 c_name = "for";
34241 if (!first)
34242 goto clause_not_first;
34243 break;
34244 case PRAGMA_OMP_CLAUSE_SECTIONS:
34245 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34246 clauses, token->location);
34247 c_name = "sections";
34248 if (!first)
34249 goto clause_not_first;
34250 break;
34251 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34252 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34253 clauses, token->location);
34254 c_name = "taskgroup";
34255 if (!first)
34256 goto clause_not_first;
34257 break;
34258 case PRAGMA_OMP_CLAUSE_LINK:
34259 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34260 c_name = "to";
34261 break;
34262 case PRAGMA_OMP_CLAUSE_TO:
34263 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34264 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34265 clauses);
34266 else
34267 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34268 c_name = "to";
34269 break;
34270 case PRAGMA_OMP_CLAUSE_FROM:
34271 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34272 c_name = "from";
34273 break;
34274 case PRAGMA_OMP_CLAUSE_UNIFORM:
34275 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34276 clauses);
34277 c_name = "uniform";
34278 break;
34279 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34280 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34281 token->location);
34282 c_name = "num_teams";
34283 break;
34284 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34285 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34286 token->location);
34287 c_name = "thread_limit";
34288 break;
34289 case PRAGMA_OMP_CLAUSE_ALIGNED:
34290 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34291 c_name = "aligned";
34292 break;
34293 case PRAGMA_OMP_CLAUSE_LINEAR:
34294 {
34295 bool declare_simd = false;
34296 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34297 declare_simd = true;
34298 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34299 }
34300 c_name = "linear";
34301 break;
34302 case PRAGMA_OMP_CLAUSE_DEPEND:
34303 clauses = cp_parser_omp_clause_depend (parser, clauses,
34304 token->location);
34305 c_name = "depend";
34306 break;
34307 case PRAGMA_OMP_CLAUSE_MAP:
34308 clauses = cp_parser_omp_clause_map (parser, clauses);
34309 c_name = "map";
34310 break;
34311 case PRAGMA_OMP_CLAUSE_DEVICE:
34312 clauses = cp_parser_omp_clause_device (parser, clauses,
34313 token->location);
34314 c_name = "device";
34315 break;
34316 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34317 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34318 token->location);
34319 c_name = "dist_schedule";
34320 break;
34321 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34322 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34323 token->location);
34324 c_name = "proc_bind";
34325 break;
34326 case PRAGMA_OMP_CLAUSE_SAFELEN:
34327 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34328 token->location);
34329 c_name = "safelen";
34330 break;
34331 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34332 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34333 token->location);
34334 c_name = "simdlen";
34335 break;
34336 case PRAGMA_OMP_CLAUSE_NOGROUP:
34337 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34338 token->location);
34339 c_name = "nogroup";
34340 break;
34341 case PRAGMA_OMP_CLAUSE_THREADS:
34342 clauses
34343 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34344 clauses, token->location);
34345 c_name = "threads";
34346 break;
34347 case PRAGMA_OMP_CLAUSE_SIMD:
34348 clauses
34349 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34350 clauses, token->location);
34351 c_name = "simd";
34352 break;
34353 default:
34354 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34355 goto saw_error;
34356 }
34357
34358 first = false;
34359
34360 if (((mask >> c_kind) & 1) == 0)
34361 {
34362 /* Remove the invalid clause(s) from the list to avoid
34363 confusing the rest of the compiler. */
34364 clauses = prev;
34365 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34366 }
34367 }
34368 saw_error:
34369 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34370 if (finish_p)
34371 {
34372 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34373 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34374 else
34375 return finish_omp_clauses (clauses, C_ORT_OMP);
34376 }
34377 return clauses;
34378 }
34379
34380 /* OpenMP 2.5:
34381 structured-block:
34382 statement
34383
34384 In practice, we're also interested in adding the statement to an
34385 outer node. So it is convenient if we work around the fact that
34386 cp_parser_statement calls add_stmt. */
34387
34388 static unsigned
34389 cp_parser_begin_omp_structured_block (cp_parser *parser)
34390 {
34391 unsigned save = parser->in_statement;
34392
34393 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34394 This preserves the "not within loop or switch" style error messages
34395 for nonsense cases like
34396 void foo() {
34397 #pragma omp single
34398 break;
34399 }
34400 */
34401 if (parser->in_statement)
34402 parser->in_statement = IN_OMP_BLOCK;
34403
34404 return save;
34405 }
34406
34407 static void
34408 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34409 {
34410 parser->in_statement = save;
34411 }
34412
34413 static tree
34414 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34415 {
34416 tree stmt = begin_omp_structured_block ();
34417 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34418
34419 cp_parser_statement (parser, NULL_TREE, false, if_p);
34420
34421 cp_parser_end_omp_structured_block (parser, save);
34422 return finish_omp_structured_block (stmt);
34423 }
34424
34425 /* OpenMP 2.5:
34426 # pragma omp atomic new-line
34427 expression-stmt
34428
34429 expression-stmt:
34430 x binop= expr | x++ | ++x | x-- | --x
34431 binop:
34432 +, *, -, /, &, ^, |, <<, >>
34433
34434 where x is an lvalue expression with scalar type.
34435
34436 OpenMP 3.1:
34437 # pragma omp atomic new-line
34438 update-stmt
34439
34440 # pragma omp atomic read new-line
34441 read-stmt
34442
34443 # pragma omp atomic write new-line
34444 write-stmt
34445
34446 # pragma omp atomic update new-line
34447 update-stmt
34448
34449 # pragma omp atomic capture new-line
34450 capture-stmt
34451
34452 # pragma omp atomic capture new-line
34453 capture-block
34454
34455 read-stmt:
34456 v = x
34457 write-stmt:
34458 x = expr
34459 update-stmt:
34460 expression-stmt | x = x binop expr
34461 capture-stmt:
34462 v = expression-stmt
34463 capture-block:
34464 { v = x; update-stmt; } | { update-stmt; v = x; }
34465
34466 OpenMP 4.0:
34467 update-stmt:
34468 expression-stmt | x = x binop expr | x = expr binop x
34469 capture-stmt:
34470 v = update-stmt
34471 capture-block:
34472 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34473
34474 where x and v are lvalue expressions with scalar type. */
34475
34476 static void
34477 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34478 {
34479 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34480 tree rhs1 = NULL_TREE, orig_lhs;
34481 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34482 bool structured_block = false;
34483 bool seq_cst = false;
34484
34485 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34486 {
34487 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34488 const char *p = IDENTIFIER_POINTER (id);
34489
34490 if (!strcmp (p, "seq_cst"))
34491 {
34492 seq_cst = true;
34493 cp_lexer_consume_token (parser->lexer);
34494 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34495 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34496 cp_lexer_consume_token (parser->lexer);
34497 }
34498 }
34499 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34500 {
34501 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34502 const char *p = IDENTIFIER_POINTER (id);
34503
34504 if (!strcmp (p, "read"))
34505 code = OMP_ATOMIC_READ;
34506 else if (!strcmp (p, "write"))
34507 code = NOP_EXPR;
34508 else if (!strcmp (p, "update"))
34509 code = OMP_ATOMIC;
34510 else if (!strcmp (p, "capture"))
34511 code = OMP_ATOMIC_CAPTURE_NEW;
34512 else
34513 p = NULL;
34514 if (p)
34515 cp_lexer_consume_token (parser->lexer);
34516 }
34517 if (!seq_cst)
34518 {
34519 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34520 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34521 cp_lexer_consume_token (parser->lexer);
34522
34523 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34524 {
34525 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34526 const char *p = IDENTIFIER_POINTER (id);
34527
34528 if (!strcmp (p, "seq_cst"))
34529 {
34530 seq_cst = true;
34531 cp_lexer_consume_token (parser->lexer);
34532 }
34533 }
34534 }
34535 cp_parser_require_pragma_eol (parser, pragma_tok);
34536
34537 switch (code)
34538 {
34539 case OMP_ATOMIC_READ:
34540 case NOP_EXPR: /* atomic write */
34541 v = cp_parser_unary_expression (parser);
34542 if (v == error_mark_node)
34543 goto saw_error;
34544 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34545 goto saw_error;
34546 if (code == NOP_EXPR)
34547 lhs = cp_parser_expression (parser);
34548 else
34549 lhs = cp_parser_unary_expression (parser);
34550 if (lhs == error_mark_node)
34551 goto saw_error;
34552 if (code == NOP_EXPR)
34553 {
34554 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34555 opcode. */
34556 code = OMP_ATOMIC;
34557 rhs = lhs;
34558 lhs = v;
34559 v = NULL_TREE;
34560 }
34561 goto done;
34562 case OMP_ATOMIC_CAPTURE_NEW:
34563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34564 {
34565 cp_lexer_consume_token (parser->lexer);
34566 structured_block = true;
34567 }
34568 else
34569 {
34570 v = cp_parser_unary_expression (parser);
34571 if (v == error_mark_node)
34572 goto saw_error;
34573 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34574 goto saw_error;
34575 }
34576 default:
34577 break;
34578 }
34579
34580 restart:
34581 lhs = cp_parser_unary_expression (parser);
34582 orig_lhs = lhs;
34583 switch (TREE_CODE (lhs))
34584 {
34585 case ERROR_MARK:
34586 goto saw_error;
34587
34588 case POSTINCREMENT_EXPR:
34589 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34590 code = OMP_ATOMIC_CAPTURE_OLD;
34591 /* FALLTHROUGH */
34592 case PREINCREMENT_EXPR:
34593 lhs = TREE_OPERAND (lhs, 0);
34594 opcode = PLUS_EXPR;
34595 rhs = integer_one_node;
34596 break;
34597
34598 case POSTDECREMENT_EXPR:
34599 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34600 code = OMP_ATOMIC_CAPTURE_OLD;
34601 /* FALLTHROUGH */
34602 case PREDECREMENT_EXPR:
34603 lhs = TREE_OPERAND (lhs, 0);
34604 opcode = MINUS_EXPR;
34605 rhs = integer_one_node;
34606 break;
34607
34608 case COMPOUND_EXPR:
34609 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34610 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34611 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34612 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34613 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34614 (TREE_OPERAND (lhs, 1), 0), 0)))
34615 == BOOLEAN_TYPE)
34616 /* Undo effects of boolean_increment for post {in,de}crement. */
34617 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34618 /* FALLTHRU */
34619 case MODIFY_EXPR:
34620 if (TREE_CODE (lhs) == MODIFY_EXPR
34621 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34622 {
34623 /* Undo effects of boolean_increment. */
34624 if (integer_onep (TREE_OPERAND (lhs, 1)))
34625 {
34626 /* This is pre or post increment. */
34627 rhs = TREE_OPERAND (lhs, 1);
34628 lhs = TREE_OPERAND (lhs, 0);
34629 opcode = NOP_EXPR;
34630 if (code == OMP_ATOMIC_CAPTURE_NEW
34631 && !structured_block
34632 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34633 code = OMP_ATOMIC_CAPTURE_OLD;
34634 break;
34635 }
34636 }
34637 /* FALLTHRU */
34638 default:
34639 switch (cp_lexer_peek_token (parser->lexer)->type)
34640 {
34641 case CPP_MULT_EQ:
34642 opcode = MULT_EXPR;
34643 break;
34644 case CPP_DIV_EQ:
34645 opcode = TRUNC_DIV_EXPR;
34646 break;
34647 case CPP_PLUS_EQ:
34648 opcode = PLUS_EXPR;
34649 break;
34650 case CPP_MINUS_EQ:
34651 opcode = MINUS_EXPR;
34652 break;
34653 case CPP_LSHIFT_EQ:
34654 opcode = LSHIFT_EXPR;
34655 break;
34656 case CPP_RSHIFT_EQ:
34657 opcode = RSHIFT_EXPR;
34658 break;
34659 case CPP_AND_EQ:
34660 opcode = BIT_AND_EXPR;
34661 break;
34662 case CPP_OR_EQ:
34663 opcode = BIT_IOR_EXPR;
34664 break;
34665 case CPP_XOR_EQ:
34666 opcode = BIT_XOR_EXPR;
34667 break;
34668 case CPP_EQ:
34669 enum cp_parser_prec oprec;
34670 cp_token *token;
34671 cp_lexer_consume_token (parser->lexer);
34672 cp_parser_parse_tentatively (parser);
34673 rhs1 = cp_parser_simple_cast_expression (parser);
34674 if (rhs1 == error_mark_node)
34675 {
34676 cp_parser_abort_tentative_parse (parser);
34677 cp_parser_simple_cast_expression (parser);
34678 goto saw_error;
34679 }
34680 token = cp_lexer_peek_token (parser->lexer);
34681 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34682 {
34683 cp_parser_abort_tentative_parse (parser);
34684 cp_parser_parse_tentatively (parser);
34685 rhs = cp_parser_binary_expression (parser, false, true,
34686 PREC_NOT_OPERATOR, NULL);
34687 if (rhs == error_mark_node)
34688 {
34689 cp_parser_abort_tentative_parse (parser);
34690 cp_parser_binary_expression (parser, false, true,
34691 PREC_NOT_OPERATOR, NULL);
34692 goto saw_error;
34693 }
34694 switch (TREE_CODE (rhs))
34695 {
34696 case MULT_EXPR:
34697 case TRUNC_DIV_EXPR:
34698 case RDIV_EXPR:
34699 case PLUS_EXPR:
34700 case MINUS_EXPR:
34701 case LSHIFT_EXPR:
34702 case RSHIFT_EXPR:
34703 case BIT_AND_EXPR:
34704 case BIT_IOR_EXPR:
34705 case BIT_XOR_EXPR:
34706 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34707 {
34708 if (cp_parser_parse_definitely (parser))
34709 {
34710 opcode = TREE_CODE (rhs);
34711 rhs1 = TREE_OPERAND (rhs, 0);
34712 rhs = TREE_OPERAND (rhs, 1);
34713 goto stmt_done;
34714 }
34715 else
34716 goto saw_error;
34717 }
34718 break;
34719 default:
34720 break;
34721 }
34722 cp_parser_abort_tentative_parse (parser);
34723 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34724 {
34725 rhs = cp_parser_expression (parser);
34726 if (rhs == error_mark_node)
34727 goto saw_error;
34728 opcode = NOP_EXPR;
34729 rhs1 = NULL_TREE;
34730 goto stmt_done;
34731 }
34732 cp_parser_error (parser,
34733 "invalid form of %<#pragma omp atomic%>");
34734 goto saw_error;
34735 }
34736 if (!cp_parser_parse_definitely (parser))
34737 goto saw_error;
34738 switch (token->type)
34739 {
34740 case CPP_SEMICOLON:
34741 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34742 {
34743 code = OMP_ATOMIC_CAPTURE_OLD;
34744 v = lhs;
34745 lhs = NULL_TREE;
34746 lhs1 = rhs1;
34747 rhs1 = NULL_TREE;
34748 cp_lexer_consume_token (parser->lexer);
34749 goto restart;
34750 }
34751 else if (structured_block)
34752 {
34753 opcode = NOP_EXPR;
34754 rhs = rhs1;
34755 rhs1 = NULL_TREE;
34756 goto stmt_done;
34757 }
34758 cp_parser_error (parser,
34759 "invalid form of %<#pragma omp atomic%>");
34760 goto saw_error;
34761 case CPP_MULT:
34762 opcode = MULT_EXPR;
34763 break;
34764 case CPP_DIV:
34765 opcode = TRUNC_DIV_EXPR;
34766 break;
34767 case CPP_PLUS:
34768 opcode = PLUS_EXPR;
34769 break;
34770 case CPP_MINUS:
34771 opcode = MINUS_EXPR;
34772 break;
34773 case CPP_LSHIFT:
34774 opcode = LSHIFT_EXPR;
34775 break;
34776 case CPP_RSHIFT:
34777 opcode = RSHIFT_EXPR;
34778 break;
34779 case CPP_AND:
34780 opcode = BIT_AND_EXPR;
34781 break;
34782 case CPP_OR:
34783 opcode = BIT_IOR_EXPR;
34784 break;
34785 case CPP_XOR:
34786 opcode = BIT_XOR_EXPR;
34787 break;
34788 default:
34789 cp_parser_error (parser,
34790 "invalid operator for %<#pragma omp atomic%>");
34791 goto saw_error;
34792 }
34793 oprec = TOKEN_PRECEDENCE (token);
34794 gcc_assert (oprec != PREC_NOT_OPERATOR);
34795 if (commutative_tree_code (opcode))
34796 oprec = (enum cp_parser_prec) (oprec - 1);
34797 cp_lexer_consume_token (parser->lexer);
34798 rhs = cp_parser_binary_expression (parser, false, false,
34799 oprec, NULL);
34800 if (rhs == error_mark_node)
34801 goto saw_error;
34802 goto stmt_done;
34803 /* FALLTHROUGH */
34804 default:
34805 cp_parser_error (parser,
34806 "invalid operator for %<#pragma omp atomic%>");
34807 goto saw_error;
34808 }
34809 cp_lexer_consume_token (parser->lexer);
34810
34811 rhs = cp_parser_expression (parser);
34812 if (rhs == error_mark_node)
34813 goto saw_error;
34814 break;
34815 }
34816 stmt_done:
34817 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34818 {
34819 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34820 goto saw_error;
34821 v = cp_parser_unary_expression (parser);
34822 if (v == error_mark_node)
34823 goto saw_error;
34824 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34825 goto saw_error;
34826 lhs1 = cp_parser_unary_expression (parser);
34827 if (lhs1 == error_mark_node)
34828 goto saw_error;
34829 }
34830 if (structured_block)
34831 {
34832 cp_parser_consume_semicolon_at_end_of_statement (parser);
34833 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34834 }
34835 done:
34836 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34837 if (!structured_block)
34838 cp_parser_consume_semicolon_at_end_of_statement (parser);
34839 return;
34840
34841 saw_error:
34842 cp_parser_skip_to_end_of_block_or_statement (parser);
34843 if (structured_block)
34844 {
34845 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34846 cp_lexer_consume_token (parser->lexer);
34847 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34848 {
34849 cp_parser_skip_to_end_of_block_or_statement (parser);
34850 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34851 cp_lexer_consume_token (parser->lexer);
34852 }
34853 }
34854 }
34855
34856
34857 /* OpenMP 2.5:
34858 # pragma omp barrier new-line */
34859
34860 static void
34861 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34862 {
34863 cp_parser_require_pragma_eol (parser, pragma_tok);
34864 finish_omp_barrier ();
34865 }
34866
34867 /* OpenMP 2.5:
34868 # pragma omp critical [(name)] new-line
34869 structured-block
34870
34871 OpenMP 4.5:
34872 # pragma omp critical [(name) [hint(expression)]] new-line
34873 structured-block */
34874
34875 #define OMP_CRITICAL_CLAUSE_MASK \
34876 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34877
34878 static tree
34879 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34880 {
34881 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34882
34883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34884 {
34885 matching_parens parens;
34886 parens.consume_open (parser);
34887
34888 name = cp_parser_identifier (parser);
34889
34890 if (name == error_mark_node
34891 || !parens.require_close (parser))
34892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34893 /*or_comma=*/false,
34894 /*consume_paren=*/true);
34895 if (name == error_mark_node)
34896 name = NULL;
34897
34898 clauses = cp_parser_omp_all_clauses (parser,
34899 OMP_CRITICAL_CLAUSE_MASK,
34900 "#pragma omp critical", pragma_tok);
34901 }
34902 else
34903 cp_parser_require_pragma_eol (parser, pragma_tok);
34904
34905 stmt = cp_parser_omp_structured_block (parser, if_p);
34906 return c_finish_omp_critical (input_location, stmt, name, clauses);
34907 }
34908
34909 /* OpenMP 2.5:
34910 # pragma omp flush flush-vars[opt] new-line
34911
34912 flush-vars:
34913 ( variable-list ) */
34914
34915 static void
34916 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34917 {
34918 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34919 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34920 cp_parser_require_pragma_eol (parser, pragma_tok);
34921
34922 finish_omp_flush ();
34923 }
34924
34925 /* Helper function, to parse omp for increment expression. */
34926
34927 static tree
34928 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34929 {
34930 tree cond = cp_parser_binary_expression (parser, false, true,
34931 PREC_NOT_OPERATOR, NULL);
34932 if (cond == error_mark_node
34933 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34934 {
34935 cp_parser_skip_to_end_of_statement (parser);
34936 return error_mark_node;
34937 }
34938
34939 switch (TREE_CODE (cond))
34940 {
34941 case GT_EXPR:
34942 case GE_EXPR:
34943 case LT_EXPR:
34944 case LE_EXPR:
34945 break;
34946 case NE_EXPR:
34947 /* Fall through: OpenMP disallows NE_EXPR. */
34948 gcc_fallthrough ();
34949 default:
34950 return error_mark_node;
34951 }
34952
34953 /* If decl is an iterator, preserve LHS and RHS of the relational
34954 expr until finish_omp_for. */
34955 if (decl
34956 && (type_dependent_expression_p (decl)
34957 || CLASS_TYPE_P (TREE_TYPE (decl))))
34958 return cond;
34959
34960 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
34961 TREE_CODE (cond),
34962 TREE_OPERAND (cond, 0), ERROR_MARK,
34963 TREE_OPERAND (cond, 1), ERROR_MARK,
34964 /*overload=*/NULL, tf_warning_or_error);
34965 }
34966
34967 /* Helper function, to parse omp for increment expression. */
34968
34969 static tree
34970 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34971 {
34972 cp_token *token = cp_lexer_peek_token (parser->lexer);
34973 enum tree_code op;
34974 tree lhs, rhs;
34975 cp_id_kind idk;
34976 bool decl_first;
34977
34978 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34979 {
34980 op = (token->type == CPP_PLUS_PLUS
34981 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34982 cp_lexer_consume_token (parser->lexer);
34983 lhs = cp_parser_simple_cast_expression (parser);
34984 if (lhs != decl
34985 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34986 return error_mark_node;
34987 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34988 }
34989
34990 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34991 if (lhs != decl
34992 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34993 return error_mark_node;
34994
34995 token = cp_lexer_peek_token (parser->lexer);
34996 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34997 {
34998 op = (token->type == CPP_PLUS_PLUS
34999 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35000 cp_lexer_consume_token (parser->lexer);
35001 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35002 }
35003
35004 op = cp_parser_assignment_operator_opt (parser);
35005 if (op == ERROR_MARK)
35006 return error_mark_node;
35007
35008 if (op != NOP_EXPR)
35009 {
35010 rhs = cp_parser_assignment_expression (parser);
35011 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35012 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35013 }
35014
35015 lhs = cp_parser_binary_expression (parser, false, false,
35016 PREC_ADDITIVE_EXPRESSION, NULL);
35017 token = cp_lexer_peek_token (parser->lexer);
35018 decl_first = (lhs == decl
35019 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35020 if (decl_first)
35021 lhs = NULL_TREE;
35022 if (token->type != CPP_PLUS
35023 && token->type != CPP_MINUS)
35024 return error_mark_node;
35025
35026 do
35027 {
35028 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35029 cp_lexer_consume_token (parser->lexer);
35030 rhs = cp_parser_binary_expression (parser, false, false,
35031 PREC_ADDITIVE_EXPRESSION, NULL);
35032 token = cp_lexer_peek_token (parser->lexer);
35033 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35034 {
35035 if (lhs == NULL_TREE)
35036 {
35037 if (op == PLUS_EXPR)
35038 lhs = rhs;
35039 else
35040 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35041 tf_warning_or_error);
35042 }
35043 else
35044 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35045 ERROR_MARK, NULL, tf_warning_or_error);
35046 }
35047 }
35048 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35049
35050 if (!decl_first)
35051 {
35052 if ((rhs != decl
35053 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35054 || op == MINUS_EXPR)
35055 return error_mark_node;
35056 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35057 }
35058 else
35059 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35060
35061 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35062 }
35063
35064 /* Parse the initialization statement of an OpenMP for loop.
35065
35066 Return true if the resulting construct should have an
35067 OMP_CLAUSE_PRIVATE added to it. */
35068
35069 static tree
35070 cp_parser_omp_for_loop_init (cp_parser *parser,
35071 tree &this_pre_body,
35072 vec<tree, va_gc> *&for_block,
35073 tree &init,
35074 tree &orig_init,
35075 tree &decl,
35076 tree &real_decl)
35077 {
35078 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35079 return NULL_TREE;
35080
35081 tree add_private_clause = NULL_TREE;
35082
35083 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35084
35085 init-expr:
35086 var = lb
35087 integer-type var = lb
35088 random-access-iterator-type var = lb
35089 pointer-type var = lb
35090 */
35091 cp_decl_specifier_seq type_specifiers;
35092
35093 /* First, try to parse as an initialized declaration. See
35094 cp_parser_condition, from whence the bulk of this is copied. */
35095
35096 cp_parser_parse_tentatively (parser);
35097 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35098 /*is_trailing_return=*/false,
35099 &type_specifiers);
35100 if (cp_parser_parse_definitely (parser))
35101 {
35102 /* If parsing a type specifier seq succeeded, then this
35103 MUST be a initialized declaration. */
35104 tree asm_specification, attributes;
35105 cp_declarator *declarator;
35106
35107 declarator = cp_parser_declarator (parser,
35108 CP_PARSER_DECLARATOR_NAMED,
35109 /*ctor_dtor_or_conv_p=*/NULL,
35110 /*parenthesized_p=*/NULL,
35111 /*member_p=*/false,
35112 /*friend_p=*/false);
35113 attributes = cp_parser_attributes_opt (parser);
35114 asm_specification = cp_parser_asm_specification_opt (parser);
35115
35116 if (declarator == cp_error_declarator)
35117 cp_parser_skip_to_end_of_statement (parser);
35118
35119 else
35120 {
35121 tree pushed_scope, auto_node;
35122
35123 decl = start_decl (declarator, &type_specifiers,
35124 SD_INITIALIZED, attributes,
35125 /*prefix_attributes=*/NULL_TREE,
35126 &pushed_scope);
35127
35128 auto_node = type_uses_auto (TREE_TYPE (decl));
35129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35130 {
35131 if (cp_lexer_next_token_is (parser->lexer,
35132 CPP_OPEN_PAREN))
35133 error ("parenthesized initialization is not allowed in "
35134 "OpenMP %<for%> loop");
35135 else
35136 /* Trigger an error. */
35137 cp_parser_require (parser, CPP_EQ, RT_EQ);
35138
35139 init = error_mark_node;
35140 cp_parser_skip_to_end_of_statement (parser);
35141 }
35142 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35143 || type_dependent_expression_p (decl)
35144 || auto_node)
35145 {
35146 bool is_direct_init, is_non_constant_init;
35147
35148 init = cp_parser_initializer (parser,
35149 &is_direct_init,
35150 &is_non_constant_init);
35151
35152 if (auto_node)
35153 {
35154 TREE_TYPE (decl)
35155 = do_auto_deduction (TREE_TYPE (decl), init,
35156 auto_node);
35157
35158 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35159 && !type_dependent_expression_p (decl))
35160 goto non_class;
35161 }
35162
35163 cp_finish_decl (decl, init, !is_non_constant_init,
35164 asm_specification,
35165 LOOKUP_ONLYCONVERTING);
35166 orig_init = init;
35167 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35168 {
35169 vec_safe_push (for_block, this_pre_body);
35170 init = NULL_TREE;
35171 }
35172 else
35173 {
35174 init = pop_stmt_list (this_pre_body);
35175 if (init && TREE_CODE (init) == STATEMENT_LIST)
35176 {
35177 tree_stmt_iterator i = tsi_start (init);
35178 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35179 while (!tsi_end_p (i))
35180 {
35181 tree t = tsi_stmt (i);
35182 if (TREE_CODE (t) == DECL_EXPR
35183 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35184 {
35185 tsi_delink (&i);
35186 vec_safe_push (for_block, t);
35187 continue;
35188 }
35189 break;
35190 }
35191 if (tsi_one_before_end_p (i))
35192 {
35193 tree t = tsi_stmt (i);
35194 tsi_delink (&i);
35195 free_stmt_list (init);
35196 init = t;
35197 }
35198 }
35199 }
35200 this_pre_body = NULL_TREE;
35201 }
35202 else
35203 {
35204 /* Consume '='. */
35205 cp_lexer_consume_token (parser->lexer);
35206 init = cp_parser_assignment_expression (parser);
35207
35208 non_class:
35209 if (TYPE_REF_P (TREE_TYPE (decl)))
35210 init = error_mark_node;
35211 else
35212 cp_finish_decl (decl, NULL_TREE,
35213 /*init_const_expr_p=*/false,
35214 asm_specification,
35215 LOOKUP_ONLYCONVERTING);
35216 }
35217
35218 if (pushed_scope)
35219 pop_scope (pushed_scope);
35220 }
35221 }
35222 else
35223 {
35224 cp_id_kind idk;
35225 /* If parsing a type specifier sequence failed, then
35226 this MUST be a simple expression. */
35227 cp_parser_parse_tentatively (parser);
35228 decl = cp_parser_primary_expression (parser, false, false,
35229 false, &idk);
35230 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35231 if (!cp_parser_error_occurred (parser)
35232 && decl
35233 && (TREE_CODE (decl) == COMPONENT_REF
35234 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35235 {
35236 cp_parser_abort_tentative_parse (parser);
35237 cp_parser_parse_tentatively (parser);
35238 cp_token *token = cp_lexer_peek_token (parser->lexer);
35239 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35240 /*check_dependency_p=*/true,
35241 /*template_p=*/NULL,
35242 /*declarator_p=*/false,
35243 /*optional_p=*/false);
35244 if (name != error_mark_node
35245 && last_tok == cp_lexer_peek_token (parser->lexer))
35246 {
35247 decl = cp_parser_lookup_name_simple (parser, name,
35248 token->location);
35249 if (TREE_CODE (decl) == FIELD_DECL)
35250 add_private_clause = omp_privatize_field (decl, false);
35251 }
35252 cp_parser_abort_tentative_parse (parser);
35253 cp_parser_parse_tentatively (parser);
35254 decl = cp_parser_primary_expression (parser, false, false,
35255 false, &idk);
35256 }
35257 if (!cp_parser_error_occurred (parser)
35258 && decl
35259 && DECL_P (decl)
35260 && CLASS_TYPE_P (TREE_TYPE (decl)))
35261 {
35262 tree rhs;
35263
35264 cp_parser_parse_definitely (parser);
35265 cp_parser_require (parser, CPP_EQ, RT_EQ);
35266 rhs = cp_parser_assignment_expression (parser);
35267 orig_init = rhs;
35268 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35269 decl, NOP_EXPR,
35270 rhs,
35271 tf_warning_or_error));
35272 if (!add_private_clause)
35273 add_private_clause = decl;
35274 }
35275 else
35276 {
35277 decl = NULL;
35278 cp_parser_abort_tentative_parse (parser);
35279 init = cp_parser_expression (parser);
35280 if (init)
35281 {
35282 if (TREE_CODE (init) == MODIFY_EXPR
35283 || TREE_CODE (init) == MODOP_EXPR)
35284 real_decl = TREE_OPERAND (init, 0);
35285 }
35286 }
35287 }
35288 return add_private_clause;
35289 }
35290
35291 /* Parse the restricted form of the for statement allowed by OpenMP. */
35292
35293 static tree
35294 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35295 tree *cclauses, bool *if_p)
35296 {
35297 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35298 tree real_decl, initv, condv, incrv, declv;
35299 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35300 location_t loc_first;
35301 bool collapse_err = false;
35302 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35303 vec<tree, va_gc> *for_block = make_tree_vector ();
35304 auto_vec<tree, 4> orig_inits;
35305 bool tiling = false;
35306
35307 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35308 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35309 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35310 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35311 {
35312 tiling = true;
35313 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35314 }
35315 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35316 && OMP_CLAUSE_ORDERED_EXPR (cl))
35317 {
35318 ordered_cl = cl;
35319 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35320 }
35321
35322 if (ordered && ordered < collapse)
35323 {
35324 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35325 "%<ordered%> clause parameter is less than %<collapse%>");
35326 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35327 = build_int_cst (NULL_TREE, collapse);
35328 ordered = collapse;
35329 }
35330 if (ordered)
35331 {
35332 for (tree *pc = &clauses; *pc; )
35333 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35334 {
35335 error_at (OMP_CLAUSE_LOCATION (*pc),
35336 "%<linear%> clause may not be specified together "
35337 "with %<ordered%> clause with a parameter");
35338 *pc = OMP_CLAUSE_CHAIN (*pc);
35339 }
35340 else
35341 pc = &OMP_CLAUSE_CHAIN (*pc);
35342 }
35343
35344 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35345 count = ordered ? ordered : collapse;
35346
35347 declv = make_tree_vec (count);
35348 initv = make_tree_vec (count);
35349 condv = make_tree_vec (count);
35350 incrv = make_tree_vec (count);
35351
35352 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35353
35354 for (i = 0; i < count; i++)
35355 {
35356 int bracecount = 0;
35357 tree add_private_clause = NULL_TREE;
35358 location_t loc;
35359
35360 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35361 {
35362 if (!collapse_err)
35363 cp_parser_error (parser, "for statement expected");
35364 return NULL;
35365 }
35366 loc = cp_lexer_consume_token (parser->lexer)->location;
35367
35368 matching_parens parens;
35369 if (!parens.require_open (parser))
35370 return NULL;
35371
35372 init = orig_init = decl = real_decl = NULL;
35373 this_pre_body = push_stmt_list ();
35374
35375 add_private_clause
35376 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35377 init, orig_init, decl, real_decl);
35378
35379 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35380 if (this_pre_body)
35381 {
35382 this_pre_body = pop_stmt_list (this_pre_body);
35383 if (pre_body)
35384 {
35385 tree t = pre_body;
35386 pre_body = push_stmt_list ();
35387 add_stmt (t);
35388 add_stmt (this_pre_body);
35389 pre_body = pop_stmt_list (pre_body);
35390 }
35391 else
35392 pre_body = this_pre_body;
35393 }
35394
35395 if (decl)
35396 real_decl = decl;
35397 if (cclauses != NULL
35398 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35399 && real_decl != NULL_TREE)
35400 {
35401 tree *c;
35402 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35403 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35404 && OMP_CLAUSE_DECL (*c) == real_decl)
35405 {
35406 error_at (loc, "iteration variable %qD"
35407 " should not be firstprivate", real_decl);
35408 *c = OMP_CLAUSE_CHAIN (*c);
35409 }
35410 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35411 && OMP_CLAUSE_DECL (*c) == real_decl)
35412 {
35413 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35414 tree l = *c;
35415 *c = OMP_CLAUSE_CHAIN (*c);
35416 if (code == OMP_SIMD)
35417 {
35418 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35419 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35420 }
35421 else
35422 {
35423 OMP_CLAUSE_CHAIN (l) = clauses;
35424 clauses = l;
35425 }
35426 add_private_clause = NULL_TREE;
35427 }
35428 else
35429 {
35430 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35431 && OMP_CLAUSE_DECL (*c) == real_decl)
35432 add_private_clause = NULL_TREE;
35433 c = &OMP_CLAUSE_CHAIN (*c);
35434 }
35435 }
35436
35437 if (add_private_clause)
35438 {
35439 tree c;
35440 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35441 {
35442 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35443 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35444 && OMP_CLAUSE_DECL (c) == decl)
35445 break;
35446 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35447 && OMP_CLAUSE_DECL (c) == decl)
35448 error_at (loc, "iteration variable %qD "
35449 "should not be firstprivate",
35450 decl);
35451 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35452 && OMP_CLAUSE_DECL (c) == decl)
35453 error_at (loc, "iteration variable %qD should not be reduction",
35454 decl);
35455 }
35456 if (c == NULL)
35457 {
35458 if (code != OMP_SIMD)
35459 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35460 else if (collapse == 1)
35461 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35462 else
35463 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35464 OMP_CLAUSE_DECL (c) = add_private_clause;
35465 c = finish_omp_clauses (c, C_ORT_OMP);
35466 if (c)
35467 {
35468 OMP_CLAUSE_CHAIN (c) = clauses;
35469 clauses = c;
35470 /* For linear, signal that we need to fill up
35471 the so far unknown linear step. */
35472 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35473 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35474 }
35475 }
35476 }
35477
35478 cond = NULL;
35479 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35480 cond = cp_parser_omp_for_cond (parser, decl);
35481 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35482
35483 incr = NULL;
35484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35485 {
35486 /* If decl is an iterator, preserve the operator on decl
35487 until finish_omp_for. */
35488 if (real_decl
35489 && ((processing_template_decl
35490 && (TREE_TYPE (real_decl) == NULL_TREE
35491 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35492 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35493 incr = cp_parser_omp_for_incr (parser, real_decl);
35494 else
35495 incr = cp_parser_expression (parser);
35496 if (!EXPR_HAS_LOCATION (incr))
35497 protected_set_expr_location (incr, input_location);
35498 }
35499
35500 if (!parens.require_close (parser))
35501 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35502 /*or_comma=*/false,
35503 /*consume_paren=*/true);
35504
35505 TREE_VEC_ELT (declv, i) = decl;
35506 TREE_VEC_ELT (initv, i) = init;
35507 TREE_VEC_ELT (condv, i) = cond;
35508 TREE_VEC_ELT (incrv, i) = incr;
35509 if (orig_init)
35510 {
35511 orig_inits.safe_grow_cleared (i + 1);
35512 orig_inits[i] = orig_init;
35513 }
35514
35515 if (i == count - 1)
35516 break;
35517
35518 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35519 in between the collapsed for loops to be still considered perfectly
35520 nested. Hopefully the final version clarifies this.
35521 For now handle (multiple) {'s and empty statements. */
35522 cp_parser_parse_tentatively (parser);
35523 for (;;)
35524 {
35525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35526 break;
35527 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35528 {
35529 cp_lexer_consume_token (parser->lexer);
35530 bracecount++;
35531 }
35532 else if (bracecount
35533 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35534 cp_lexer_consume_token (parser->lexer);
35535 else
35536 {
35537 loc = cp_lexer_peek_token (parser->lexer)->location;
35538 error_at (loc, "not enough for loops to collapse");
35539 collapse_err = true;
35540 cp_parser_abort_tentative_parse (parser);
35541 declv = NULL_TREE;
35542 break;
35543 }
35544 }
35545
35546 if (declv)
35547 {
35548 cp_parser_parse_definitely (parser);
35549 nbraces += bracecount;
35550 }
35551 }
35552
35553 if (nbraces)
35554 if_p = NULL;
35555
35556 /* Note that we saved the original contents of this flag when we entered
35557 the structured block, and so we don't need to re-save it here. */
35558 parser->in_statement = IN_OMP_FOR;
35559
35560 /* Note that the grammar doesn't call for a structured block here,
35561 though the loop as a whole is a structured block. */
35562 body = push_stmt_list ();
35563 cp_parser_statement (parser, NULL_TREE, false, if_p);
35564 body = pop_stmt_list (body);
35565
35566 if (declv == NULL_TREE)
35567 ret = NULL_TREE;
35568 else
35569 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35570 body, pre_body, &orig_inits, clauses);
35571
35572 while (nbraces)
35573 {
35574 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35575 {
35576 cp_lexer_consume_token (parser->lexer);
35577 nbraces--;
35578 }
35579 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35580 cp_lexer_consume_token (parser->lexer);
35581 else
35582 {
35583 if (!collapse_err)
35584 {
35585 error_at (cp_lexer_peek_token (parser->lexer)->location,
35586 "collapsed loops not perfectly nested");
35587 }
35588 collapse_err = true;
35589 cp_parser_statement_seq_opt (parser, NULL);
35590 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35591 break;
35592 }
35593 }
35594
35595 while (!for_block->is_empty ())
35596 {
35597 tree t = for_block->pop ();
35598 if (TREE_CODE (t) == STATEMENT_LIST)
35599 add_stmt (pop_stmt_list (t));
35600 else
35601 add_stmt (t);
35602 }
35603 release_tree_vector (for_block);
35604
35605 return ret;
35606 }
35607
35608 /* Helper function for OpenMP parsing, split clauses and call
35609 finish_omp_clauses on each of the set of clauses afterwards. */
35610
35611 static void
35612 cp_omp_split_clauses (location_t loc, enum tree_code code,
35613 omp_clause_mask mask, tree clauses, tree *cclauses)
35614 {
35615 int i;
35616 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35617 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35618 if (cclauses[i])
35619 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35620 }
35621
35622 /* OpenMP 4.0:
35623 #pragma omp simd simd-clause[optseq] new-line
35624 for-loop */
35625
35626 #define OMP_SIMD_CLAUSE_MASK \
35627 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35635
35636 static tree
35637 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35638 char *p_name, omp_clause_mask mask, tree *cclauses,
35639 bool *if_p)
35640 {
35641 tree clauses, sb, ret;
35642 unsigned int save;
35643 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35644
35645 strcat (p_name, " simd");
35646 mask |= OMP_SIMD_CLAUSE_MASK;
35647
35648 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35649 cclauses == NULL);
35650 if (cclauses)
35651 {
35652 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35653 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35654 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35655 OMP_CLAUSE_ORDERED);
35656 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35657 {
35658 error_at (OMP_CLAUSE_LOCATION (c),
35659 "%<ordered%> clause with parameter may not be specified "
35660 "on %qs construct", p_name);
35661 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35662 }
35663 }
35664
35665 sb = begin_omp_structured_block ();
35666 save = cp_parser_begin_omp_structured_block (parser);
35667
35668 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35669
35670 cp_parser_end_omp_structured_block (parser, save);
35671 add_stmt (finish_omp_structured_block (sb));
35672
35673 return ret;
35674 }
35675
35676 /* OpenMP 2.5:
35677 #pragma omp for for-clause[optseq] new-line
35678 for-loop
35679
35680 OpenMP 4.0:
35681 #pragma omp for simd for-simd-clause[optseq] new-line
35682 for-loop */
35683
35684 #define OMP_FOR_CLAUSE_MASK \
35685 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35694
35695 static tree
35696 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35697 char *p_name, omp_clause_mask mask, tree *cclauses,
35698 bool *if_p)
35699 {
35700 tree clauses, sb, ret;
35701 unsigned int save;
35702 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35703
35704 strcat (p_name, " for");
35705 mask |= OMP_FOR_CLAUSE_MASK;
35706 /* parallel for{, simd} disallows nowait clause, but for
35707 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35708 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35709 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35710 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35711 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35712 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35713
35714 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35715 {
35716 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35717 const char *p = IDENTIFIER_POINTER (id);
35718
35719 if (strcmp (p, "simd") == 0)
35720 {
35721 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35722 if (cclauses == NULL)
35723 cclauses = cclauses_buf;
35724
35725 cp_lexer_consume_token (parser->lexer);
35726 if (!flag_openmp) /* flag_openmp_simd */
35727 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35728 cclauses, if_p);
35729 sb = begin_omp_structured_block ();
35730 save = cp_parser_begin_omp_structured_block (parser);
35731 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35732 cclauses, if_p);
35733 cp_parser_end_omp_structured_block (parser, save);
35734 tree body = finish_omp_structured_block (sb);
35735 if (ret == NULL)
35736 return ret;
35737 ret = make_node (OMP_FOR);
35738 TREE_TYPE (ret) = void_type_node;
35739 OMP_FOR_BODY (ret) = body;
35740 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35741 SET_EXPR_LOCATION (ret, loc);
35742 add_stmt (ret);
35743 return ret;
35744 }
35745 }
35746 if (!flag_openmp) /* flag_openmp_simd */
35747 {
35748 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35749 return NULL_TREE;
35750 }
35751
35752 /* Composite distribute parallel for disallows linear clause. */
35753 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35754 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35755
35756 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35757 cclauses == NULL);
35758 if (cclauses)
35759 {
35760 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35761 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35762 }
35763
35764 sb = begin_omp_structured_block ();
35765 save = cp_parser_begin_omp_structured_block (parser);
35766
35767 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35768
35769 cp_parser_end_omp_structured_block (parser, save);
35770 add_stmt (finish_omp_structured_block (sb));
35771
35772 return ret;
35773 }
35774
35775 /* OpenMP 2.5:
35776 # pragma omp master new-line
35777 structured-block */
35778
35779 static tree
35780 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35781 {
35782 cp_parser_require_pragma_eol (parser, pragma_tok);
35783 return c_finish_omp_master (input_location,
35784 cp_parser_omp_structured_block (parser, if_p));
35785 }
35786
35787 /* OpenMP 2.5:
35788 # pragma omp ordered new-line
35789 structured-block
35790
35791 OpenMP 4.5:
35792 # pragma omp ordered ordered-clauses new-line
35793 structured-block */
35794
35795 #define OMP_ORDERED_CLAUSE_MASK \
35796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35798
35799 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35800 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35801
35802 static bool
35803 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35804 enum pragma_context context, bool *if_p)
35805 {
35806 location_t loc = pragma_tok->location;
35807
35808 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35809 {
35810 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35811 const char *p = IDENTIFIER_POINTER (id);
35812
35813 if (strcmp (p, "depend") == 0)
35814 {
35815 if (!flag_openmp) /* flag_openmp_simd */
35816 {
35817 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35818 return false;
35819 }
35820 if (context == pragma_stmt)
35821 {
35822 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35823 "%<depend%> clause may only be used in compound "
35824 "statements");
35825 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35826 return false;
35827 }
35828 tree clauses
35829 = cp_parser_omp_all_clauses (parser,
35830 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35831 "#pragma omp ordered", pragma_tok);
35832 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35833 return false;
35834 }
35835 }
35836
35837 tree clauses
35838 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35839 "#pragma omp ordered", pragma_tok);
35840
35841 if (!flag_openmp /* flag_openmp_simd */
35842 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35843 return false;
35844
35845 c_finish_omp_ordered (loc, clauses,
35846 cp_parser_omp_structured_block (parser, if_p));
35847 return true;
35848 }
35849
35850 /* OpenMP 2.5:
35851
35852 section-scope:
35853 { section-sequence }
35854
35855 section-sequence:
35856 section-directive[opt] structured-block
35857 section-sequence section-directive structured-block */
35858
35859 static tree
35860 cp_parser_omp_sections_scope (cp_parser *parser)
35861 {
35862 tree stmt, substmt;
35863 bool error_suppress = false;
35864 cp_token *tok;
35865
35866 matching_braces braces;
35867 if (!braces.require_open (parser))
35868 return NULL_TREE;
35869
35870 stmt = push_stmt_list ();
35871
35872 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35873 != PRAGMA_OMP_SECTION)
35874 {
35875 substmt = cp_parser_omp_structured_block (parser, NULL);
35876 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35877 add_stmt (substmt);
35878 }
35879
35880 while (1)
35881 {
35882 tok = cp_lexer_peek_token (parser->lexer);
35883 if (tok->type == CPP_CLOSE_BRACE)
35884 break;
35885 if (tok->type == CPP_EOF)
35886 break;
35887
35888 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35889 {
35890 cp_lexer_consume_token (parser->lexer);
35891 cp_parser_require_pragma_eol (parser, tok);
35892 error_suppress = false;
35893 }
35894 else if (!error_suppress)
35895 {
35896 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35897 error_suppress = true;
35898 }
35899
35900 substmt = cp_parser_omp_structured_block (parser, NULL);
35901 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35902 add_stmt (substmt);
35903 }
35904 braces.require_close (parser);
35905
35906 substmt = pop_stmt_list (stmt);
35907
35908 stmt = make_node (OMP_SECTIONS);
35909 TREE_TYPE (stmt) = void_type_node;
35910 OMP_SECTIONS_BODY (stmt) = substmt;
35911
35912 add_stmt (stmt);
35913 return stmt;
35914 }
35915
35916 /* OpenMP 2.5:
35917 # pragma omp sections sections-clause[optseq] newline
35918 sections-scope */
35919
35920 #define OMP_SECTIONS_CLAUSE_MASK \
35921 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35926
35927 static tree
35928 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35929 char *p_name, omp_clause_mask mask, tree *cclauses)
35930 {
35931 tree clauses, ret;
35932 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35933
35934 strcat (p_name, " sections");
35935 mask |= OMP_SECTIONS_CLAUSE_MASK;
35936 if (cclauses)
35937 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35938
35939 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35940 cclauses == NULL);
35941 if (cclauses)
35942 {
35943 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35944 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35945 }
35946
35947 ret = cp_parser_omp_sections_scope (parser);
35948 if (ret)
35949 OMP_SECTIONS_CLAUSES (ret) = clauses;
35950
35951 return ret;
35952 }
35953
35954 /* OpenMP 2.5:
35955 # pragma omp parallel parallel-clause[optseq] new-line
35956 structured-block
35957 # pragma omp parallel for parallel-for-clause[optseq] new-line
35958 structured-block
35959 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35960 structured-block
35961
35962 OpenMP 4.0:
35963 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35964 structured-block */
35965
35966 #define OMP_PARALLEL_CLAUSE_MASK \
35967 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35976
35977 static tree
35978 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35979 char *p_name, omp_clause_mask mask, tree *cclauses,
35980 bool *if_p)
35981 {
35982 tree stmt, clauses, block;
35983 unsigned int save;
35984 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35985
35986 strcat (p_name, " parallel");
35987 mask |= OMP_PARALLEL_CLAUSE_MASK;
35988 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35989 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35990 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35991 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35992
35993 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35994 {
35995 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35996 if (cclauses == NULL)
35997 cclauses = cclauses_buf;
35998
35999 cp_lexer_consume_token (parser->lexer);
36000 if (!flag_openmp) /* flag_openmp_simd */
36001 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
36002 if_p);
36003 block = begin_omp_parallel ();
36004 save = cp_parser_begin_omp_structured_block (parser);
36005 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
36006 if_p);
36007 cp_parser_end_omp_structured_block (parser, save);
36008 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
36009 block);
36010 if (ret == NULL_TREE)
36011 return ret;
36012 OMP_PARALLEL_COMBINED (stmt) = 1;
36013 return stmt;
36014 }
36015 /* When combined with distribute, parallel has to be followed by for.
36016 #pragma omp target parallel is allowed though. */
36017 else if (cclauses
36018 && (mask & (OMP_CLAUSE_MASK_1
36019 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36020 {
36021 error_at (loc, "expected %<for%> after %qs", p_name);
36022 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36023 return NULL_TREE;
36024 }
36025 else if (!flag_openmp) /* flag_openmp_simd */
36026 {
36027 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36028 return NULL_TREE;
36029 }
36030 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36031 {
36032 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36033 const char *p = IDENTIFIER_POINTER (id);
36034 if (strcmp (p, "sections") == 0)
36035 {
36036 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36037 cclauses = cclauses_buf;
36038
36039 cp_lexer_consume_token (parser->lexer);
36040 block = begin_omp_parallel ();
36041 save = cp_parser_begin_omp_structured_block (parser);
36042 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
36043 cp_parser_end_omp_structured_block (parser, save);
36044 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
36045 block);
36046 OMP_PARALLEL_COMBINED (stmt) = 1;
36047 return stmt;
36048 }
36049 }
36050
36051 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36052 cclauses == NULL);
36053 if (cclauses)
36054 {
36055 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
36056 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
36057 }
36058
36059 block = begin_omp_parallel ();
36060 save = cp_parser_begin_omp_structured_block (parser);
36061 cp_parser_statement (parser, NULL_TREE, false, if_p);
36062 cp_parser_end_omp_structured_block (parser, save);
36063 stmt = finish_omp_parallel (clauses, block);
36064 return stmt;
36065 }
36066
36067 /* OpenMP 2.5:
36068 # pragma omp single single-clause[optseq] new-line
36069 structured-block */
36070
36071 #define OMP_SINGLE_CLAUSE_MASK \
36072 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
36075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36076
36077 static tree
36078 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36079 {
36080 tree stmt = make_node (OMP_SINGLE);
36081 TREE_TYPE (stmt) = void_type_node;
36082
36083 OMP_SINGLE_CLAUSES (stmt)
36084 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
36085 "#pragma omp single", pragma_tok);
36086 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36087
36088 return add_stmt (stmt);
36089 }
36090
36091 /* OpenMP 3.0:
36092 # pragma omp task task-clause[optseq] new-line
36093 structured-block */
36094
36095 #define OMP_TASK_CLAUSE_MASK \
36096 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36106
36107 static tree
36108 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36109 {
36110 tree clauses, block;
36111 unsigned int save;
36112
36113 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
36114 "#pragma omp task", pragma_tok);
36115 block = begin_omp_task ();
36116 save = cp_parser_begin_omp_structured_block (parser);
36117 cp_parser_statement (parser, NULL_TREE, false, if_p);
36118 cp_parser_end_omp_structured_block (parser, save);
36119 return finish_omp_task (clauses, block);
36120 }
36121
36122 /* OpenMP 3.0:
36123 # pragma omp taskwait new-line */
36124
36125 static void
36126 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
36127 {
36128 cp_parser_require_pragma_eol (parser, pragma_tok);
36129 finish_omp_taskwait ();
36130 }
36131
36132 /* OpenMP 3.1:
36133 # pragma omp taskyield new-line */
36134
36135 static void
36136 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36137 {
36138 cp_parser_require_pragma_eol (parser, pragma_tok);
36139 finish_omp_taskyield ();
36140 }
36141
36142 /* OpenMP 4.0:
36143 # pragma omp taskgroup new-line
36144 structured-block */
36145
36146 static tree
36147 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36148 {
36149 cp_parser_require_pragma_eol (parser, pragma_tok);
36150 return c_finish_omp_taskgroup (input_location,
36151 cp_parser_omp_structured_block (parser,
36152 if_p));
36153 }
36154
36155
36156 /* OpenMP 2.5:
36157 # pragma omp threadprivate (variable-list) */
36158
36159 static void
36160 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36161 {
36162 tree vars;
36163
36164 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36165 cp_parser_require_pragma_eol (parser, pragma_tok);
36166
36167 finish_omp_threadprivate (vars);
36168 }
36169
36170 /* OpenMP 4.0:
36171 # pragma omp cancel cancel-clause[optseq] new-line */
36172
36173 #define OMP_CANCEL_CLAUSE_MASK \
36174 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36179
36180 static void
36181 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36182 {
36183 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36184 "#pragma omp cancel", pragma_tok);
36185 finish_omp_cancel (clauses);
36186 }
36187
36188 /* OpenMP 4.0:
36189 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36190
36191 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36196
36197 static void
36198 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36199 enum pragma_context context)
36200 {
36201 tree clauses;
36202 bool point_seen = false;
36203
36204 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36205 {
36206 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36207 const char *p = IDENTIFIER_POINTER (id);
36208
36209 if (strcmp (p, "point") == 0)
36210 {
36211 cp_lexer_consume_token (parser->lexer);
36212 point_seen = true;
36213 }
36214 }
36215 if (!point_seen)
36216 {
36217 cp_parser_error (parser, "expected %<point%>");
36218 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36219 return;
36220 }
36221
36222 if (context != pragma_compound)
36223 {
36224 if (context == pragma_stmt)
36225 error_at (pragma_tok->location,
36226 "%<#pragma %s%> may only be used in compound statements",
36227 "omp cancellation point");
36228 else
36229 cp_parser_error (parser, "expected declaration specifiers");
36230 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36231 return;
36232 }
36233
36234 clauses = cp_parser_omp_all_clauses (parser,
36235 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36236 "#pragma omp cancellation point",
36237 pragma_tok);
36238 finish_omp_cancellation_point (clauses);
36239 }
36240
36241 /* OpenMP 4.0:
36242 #pragma omp distribute distribute-clause[optseq] new-line
36243 for-loop */
36244
36245 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36251
36252 static tree
36253 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36254 char *p_name, omp_clause_mask mask, tree *cclauses,
36255 bool *if_p)
36256 {
36257 tree clauses, sb, ret;
36258 unsigned int save;
36259 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36260
36261 strcat (p_name, " distribute");
36262 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36263
36264 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36265 {
36266 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36267 const char *p = IDENTIFIER_POINTER (id);
36268 bool simd = false;
36269 bool parallel = false;
36270
36271 if (strcmp (p, "simd") == 0)
36272 simd = true;
36273 else
36274 parallel = strcmp (p, "parallel") == 0;
36275 if (parallel || simd)
36276 {
36277 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36278 if (cclauses == NULL)
36279 cclauses = cclauses_buf;
36280 cp_lexer_consume_token (parser->lexer);
36281 if (!flag_openmp) /* flag_openmp_simd */
36282 {
36283 if (simd)
36284 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36285 cclauses, if_p);
36286 else
36287 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36288 cclauses, if_p);
36289 }
36290 sb = begin_omp_structured_block ();
36291 save = cp_parser_begin_omp_structured_block (parser);
36292 if (simd)
36293 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36294 cclauses, if_p);
36295 else
36296 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36297 cclauses, if_p);
36298 cp_parser_end_omp_structured_block (parser, save);
36299 tree body = finish_omp_structured_block (sb);
36300 if (ret == NULL)
36301 return ret;
36302 ret = make_node (OMP_DISTRIBUTE);
36303 TREE_TYPE (ret) = void_type_node;
36304 OMP_FOR_BODY (ret) = body;
36305 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36306 SET_EXPR_LOCATION (ret, loc);
36307 add_stmt (ret);
36308 return ret;
36309 }
36310 }
36311 if (!flag_openmp) /* flag_openmp_simd */
36312 {
36313 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36314 return NULL_TREE;
36315 }
36316
36317 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36318 cclauses == NULL);
36319 if (cclauses)
36320 {
36321 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36322 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36323 }
36324
36325 sb = begin_omp_structured_block ();
36326 save = cp_parser_begin_omp_structured_block (parser);
36327
36328 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36329
36330 cp_parser_end_omp_structured_block (parser, save);
36331 add_stmt (finish_omp_structured_block (sb));
36332
36333 return ret;
36334 }
36335
36336 /* OpenMP 4.0:
36337 # pragma omp teams teams-clause[optseq] new-line
36338 structured-block */
36339
36340 #define OMP_TEAMS_CLAUSE_MASK \
36341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36348
36349 static tree
36350 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36351 char *p_name, omp_clause_mask mask, tree *cclauses,
36352 bool *if_p)
36353 {
36354 tree clauses, sb, ret;
36355 unsigned int save;
36356 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36357
36358 strcat (p_name, " teams");
36359 mask |= OMP_TEAMS_CLAUSE_MASK;
36360
36361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36362 {
36363 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36364 const char *p = IDENTIFIER_POINTER (id);
36365 if (strcmp (p, "distribute") == 0)
36366 {
36367 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36368 if (cclauses == NULL)
36369 cclauses = cclauses_buf;
36370
36371 cp_lexer_consume_token (parser->lexer);
36372 if (!flag_openmp) /* flag_openmp_simd */
36373 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36374 cclauses, if_p);
36375 sb = begin_omp_structured_block ();
36376 save = cp_parser_begin_omp_structured_block (parser);
36377 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36378 cclauses, if_p);
36379 cp_parser_end_omp_structured_block (parser, save);
36380 tree body = finish_omp_structured_block (sb);
36381 if (ret == NULL)
36382 return ret;
36383 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36384 ret = make_node (OMP_TEAMS);
36385 TREE_TYPE (ret) = void_type_node;
36386 OMP_TEAMS_CLAUSES (ret) = clauses;
36387 OMP_TEAMS_BODY (ret) = body;
36388 OMP_TEAMS_COMBINED (ret) = 1;
36389 SET_EXPR_LOCATION (ret, loc);
36390 return add_stmt (ret);
36391 }
36392 }
36393 if (!flag_openmp) /* flag_openmp_simd */
36394 {
36395 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36396 return NULL_TREE;
36397 }
36398
36399 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36400 cclauses == NULL);
36401 if (cclauses)
36402 {
36403 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36404 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36405 }
36406
36407 tree stmt = make_node (OMP_TEAMS);
36408 TREE_TYPE (stmt) = void_type_node;
36409 OMP_TEAMS_CLAUSES (stmt) = clauses;
36410 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36411 SET_EXPR_LOCATION (stmt, loc);
36412
36413 return add_stmt (stmt);
36414 }
36415
36416 /* OpenMP 4.0:
36417 # pragma omp target data target-data-clause[optseq] new-line
36418 structured-block */
36419
36420 #define OMP_TARGET_DATA_CLAUSE_MASK \
36421 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36425
36426 static tree
36427 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36428 {
36429 tree clauses
36430 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36431 "#pragma omp target data", pragma_tok);
36432 int map_seen = 0;
36433 for (tree *pc = &clauses; *pc;)
36434 {
36435 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36436 switch (OMP_CLAUSE_MAP_KIND (*pc))
36437 {
36438 case GOMP_MAP_TO:
36439 case GOMP_MAP_ALWAYS_TO:
36440 case GOMP_MAP_FROM:
36441 case GOMP_MAP_ALWAYS_FROM:
36442 case GOMP_MAP_TOFROM:
36443 case GOMP_MAP_ALWAYS_TOFROM:
36444 case GOMP_MAP_ALLOC:
36445 map_seen = 3;
36446 break;
36447 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36448 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36449 case GOMP_MAP_ALWAYS_POINTER:
36450 break;
36451 default:
36452 map_seen |= 1;
36453 error_at (OMP_CLAUSE_LOCATION (*pc),
36454 "%<#pragma omp target data%> with map-type other "
36455 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36456 "on %<map%> clause");
36457 *pc = OMP_CLAUSE_CHAIN (*pc);
36458 continue;
36459 }
36460 pc = &OMP_CLAUSE_CHAIN (*pc);
36461 }
36462
36463 if (map_seen != 3)
36464 {
36465 if (map_seen == 0)
36466 error_at (pragma_tok->location,
36467 "%<#pragma omp target data%> must contain at least "
36468 "one %<map%> clause");
36469 return NULL_TREE;
36470 }
36471
36472 tree stmt = make_node (OMP_TARGET_DATA);
36473 TREE_TYPE (stmt) = void_type_node;
36474 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36475
36476 keep_next_level (true);
36477 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36478
36479 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36480 return add_stmt (stmt);
36481 }
36482
36483 /* OpenMP 4.5:
36484 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36485 structured-block */
36486
36487 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36493
36494 static tree
36495 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36496 enum pragma_context context)
36497 {
36498 bool data_seen = false;
36499 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36500 {
36501 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36502 const char *p = IDENTIFIER_POINTER (id);
36503
36504 if (strcmp (p, "data") == 0)
36505 {
36506 cp_lexer_consume_token (parser->lexer);
36507 data_seen = true;
36508 }
36509 }
36510 if (!data_seen)
36511 {
36512 cp_parser_error (parser, "expected %<data%>");
36513 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36514 return NULL_TREE;
36515 }
36516
36517 if (context == pragma_stmt)
36518 {
36519 error_at (pragma_tok->location,
36520 "%<#pragma %s%> may only be used in compound statements",
36521 "omp target enter data");
36522 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36523 return NULL_TREE;
36524 }
36525
36526 tree clauses
36527 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36528 "#pragma omp target enter data", pragma_tok);
36529 int map_seen = 0;
36530 for (tree *pc = &clauses; *pc;)
36531 {
36532 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36533 switch (OMP_CLAUSE_MAP_KIND (*pc))
36534 {
36535 case GOMP_MAP_TO:
36536 case GOMP_MAP_ALWAYS_TO:
36537 case GOMP_MAP_ALLOC:
36538 map_seen = 3;
36539 break;
36540 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36541 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36542 case GOMP_MAP_ALWAYS_POINTER:
36543 break;
36544 default:
36545 map_seen |= 1;
36546 error_at (OMP_CLAUSE_LOCATION (*pc),
36547 "%<#pragma omp target enter data%> with map-type other "
36548 "than %<to%> or %<alloc%> on %<map%> clause");
36549 *pc = OMP_CLAUSE_CHAIN (*pc);
36550 continue;
36551 }
36552 pc = &OMP_CLAUSE_CHAIN (*pc);
36553 }
36554
36555 if (map_seen != 3)
36556 {
36557 if (map_seen == 0)
36558 error_at (pragma_tok->location,
36559 "%<#pragma omp target enter data%> must contain at least "
36560 "one %<map%> clause");
36561 return NULL_TREE;
36562 }
36563
36564 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36565 TREE_TYPE (stmt) = void_type_node;
36566 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36567 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36568 return add_stmt (stmt);
36569 }
36570
36571 /* OpenMP 4.5:
36572 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36573 structured-block */
36574
36575 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36576 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36581
36582 static tree
36583 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36584 enum pragma_context context)
36585 {
36586 bool data_seen = false;
36587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36588 {
36589 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36590 const char *p = IDENTIFIER_POINTER (id);
36591
36592 if (strcmp (p, "data") == 0)
36593 {
36594 cp_lexer_consume_token (parser->lexer);
36595 data_seen = true;
36596 }
36597 }
36598 if (!data_seen)
36599 {
36600 cp_parser_error (parser, "expected %<data%>");
36601 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36602 return NULL_TREE;
36603 }
36604
36605 if (context == pragma_stmt)
36606 {
36607 error_at (pragma_tok->location,
36608 "%<#pragma %s%> may only be used in compound statements",
36609 "omp target exit data");
36610 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36611 return NULL_TREE;
36612 }
36613
36614 tree clauses
36615 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36616 "#pragma omp target exit data", pragma_tok);
36617 int map_seen = 0;
36618 for (tree *pc = &clauses; *pc;)
36619 {
36620 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36621 switch (OMP_CLAUSE_MAP_KIND (*pc))
36622 {
36623 case GOMP_MAP_FROM:
36624 case GOMP_MAP_ALWAYS_FROM:
36625 case GOMP_MAP_RELEASE:
36626 case GOMP_MAP_DELETE:
36627 map_seen = 3;
36628 break;
36629 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36630 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36631 case GOMP_MAP_ALWAYS_POINTER:
36632 break;
36633 default:
36634 map_seen |= 1;
36635 error_at (OMP_CLAUSE_LOCATION (*pc),
36636 "%<#pragma omp target exit data%> with map-type other "
36637 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36638 " clause");
36639 *pc = OMP_CLAUSE_CHAIN (*pc);
36640 continue;
36641 }
36642 pc = &OMP_CLAUSE_CHAIN (*pc);
36643 }
36644
36645 if (map_seen != 3)
36646 {
36647 if (map_seen == 0)
36648 error_at (pragma_tok->location,
36649 "%<#pragma omp target exit data%> must contain at least "
36650 "one %<map%> clause");
36651 return NULL_TREE;
36652 }
36653
36654 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36655 TREE_TYPE (stmt) = void_type_node;
36656 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36657 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36658 return add_stmt (stmt);
36659 }
36660
36661 /* OpenMP 4.0:
36662 # pragma omp target update target-update-clause[optseq] new-line */
36663
36664 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36665 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36671
36672 static bool
36673 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36674 enum pragma_context context)
36675 {
36676 if (context == pragma_stmt)
36677 {
36678 error_at (pragma_tok->location,
36679 "%<#pragma %s%> may only be used in compound statements",
36680 "omp target update");
36681 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36682 return false;
36683 }
36684
36685 tree clauses
36686 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36687 "#pragma omp target update", pragma_tok);
36688 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36689 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36690 {
36691 error_at (pragma_tok->location,
36692 "%<#pragma omp target update%> must contain at least one "
36693 "%<from%> or %<to%> clauses");
36694 return false;
36695 }
36696
36697 tree stmt = make_node (OMP_TARGET_UPDATE);
36698 TREE_TYPE (stmt) = void_type_node;
36699 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36700 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36701 add_stmt (stmt);
36702 return false;
36703 }
36704
36705 /* OpenMP 4.0:
36706 # pragma omp target target-clause[optseq] new-line
36707 structured-block */
36708
36709 #define OMP_TARGET_CLAUSE_MASK \
36710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36719
36720 static bool
36721 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36722 enum pragma_context context, bool *if_p)
36723 {
36724 tree *pc = NULL, stmt;
36725
36726 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36727 {
36728 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36729 const char *p = IDENTIFIER_POINTER (id);
36730 enum tree_code ccode = ERROR_MARK;
36731
36732 if (strcmp (p, "teams") == 0)
36733 ccode = OMP_TEAMS;
36734 else if (strcmp (p, "parallel") == 0)
36735 ccode = OMP_PARALLEL;
36736 else if (strcmp (p, "simd") == 0)
36737 ccode = OMP_SIMD;
36738 if (ccode != ERROR_MARK)
36739 {
36740 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36741 char p_name[sizeof ("#pragma omp target teams distribute "
36742 "parallel for simd")];
36743
36744 cp_lexer_consume_token (parser->lexer);
36745 strcpy (p_name, "#pragma omp target");
36746 if (!flag_openmp) /* flag_openmp_simd */
36747 {
36748 tree stmt;
36749 switch (ccode)
36750 {
36751 case OMP_TEAMS:
36752 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36753 OMP_TARGET_CLAUSE_MASK,
36754 cclauses, if_p);
36755 break;
36756 case OMP_PARALLEL:
36757 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36758 OMP_TARGET_CLAUSE_MASK,
36759 cclauses, if_p);
36760 break;
36761 case OMP_SIMD:
36762 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36763 OMP_TARGET_CLAUSE_MASK,
36764 cclauses, if_p);
36765 break;
36766 default:
36767 gcc_unreachable ();
36768 }
36769 return stmt != NULL_TREE;
36770 }
36771 keep_next_level (true);
36772 tree sb = begin_omp_structured_block (), ret;
36773 unsigned save = cp_parser_begin_omp_structured_block (parser);
36774 switch (ccode)
36775 {
36776 case OMP_TEAMS:
36777 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36778 OMP_TARGET_CLAUSE_MASK, cclauses,
36779 if_p);
36780 break;
36781 case OMP_PARALLEL:
36782 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36783 OMP_TARGET_CLAUSE_MASK, cclauses,
36784 if_p);
36785 break;
36786 case OMP_SIMD:
36787 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36788 OMP_TARGET_CLAUSE_MASK, cclauses,
36789 if_p);
36790 break;
36791 default:
36792 gcc_unreachable ();
36793 }
36794 cp_parser_end_omp_structured_block (parser, save);
36795 tree body = finish_omp_structured_block (sb);
36796 if (ret == NULL_TREE)
36797 return false;
36798 if (ccode == OMP_TEAMS && !processing_template_decl)
36799 {
36800 /* For combined target teams, ensure the num_teams and
36801 thread_limit clause expressions are evaluated on the host,
36802 before entering the target construct. */
36803 tree c;
36804 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36805 c; c = OMP_CLAUSE_CHAIN (c))
36806 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36807 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36808 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36809 {
36810 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36811 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36812 if (expr == error_mark_node)
36813 continue;
36814 tree tmp = TARGET_EXPR_SLOT (expr);
36815 add_stmt (expr);
36816 OMP_CLAUSE_OPERAND (c, 0) = expr;
36817 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36818 OMP_CLAUSE_FIRSTPRIVATE);
36819 OMP_CLAUSE_DECL (tc) = tmp;
36820 OMP_CLAUSE_CHAIN (tc)
36821 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36822 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36823 }
36824 }
36825 tree stmt = make_node (OMP_TARGET);
36826 TREE_TYPE (stmt) = void_type_node;
36827 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36828 OMP_TARGET_BODY (stmt) = body;
36829 OMP_TARGET_COMBINED (stmt) = 1;
36830 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36831 add_stmt (stmt);
36832 pc = &OMP_TARGET_CLAUSES (stmt);
36833 goto check_clauses;
36834 }
36835 else if (!flag_openmp) /* flag_openmp_simd */
36836 {
36837 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36838 return false;
36839 }
36840 else if (strcmp (p, "data") == 0)
36841 {
36842 cp_lexer_consume_token (parser->lexer);
36843 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36844 return true;
36845 }
36846 else if (strcmp (p, "enter") == 0)
36847 {
36848 cp_lexer_consume_token (parser->lexer);
36849 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36850 return false;
36851 }
36852 else if (strcmp (p, "exit") == 0)
36853 {
36854 cp_lexer_consume_token (parser->lexer);
36855 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36856 return false;
36857 }
36858 else if (strcmp (p, "update") == 0)
36859 {
36860 cp_lexer_consume_token (parser->lexer);
36861 return cp_parser_omp_target_update (parser, pragma_tok, context);
36862 }
36863 }
36864 if (!flag_openmp) /* flag_openmp_simd */
36865 {
36866 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36867 return false;
36868 }
36869
36870 stmt = make_node (OMP_TARGET);
36871 TREE_TYPE (stmt) = void_type_node;
36872
36873 OMP_TARGET_CLAUSES (stmt)
36874 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36875 "#pragma omp target", pragma_tok);
36876 pc = &OMP_TARGET_CLAUSES (stmt);
36877 keep_next_level (true);
36878 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36879
36880 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36881 add_stmt (stmt);
36882
36883 check_clauses:
36884 while (*pc)
36885 {
36886 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36887 switch (OMP_CLAUSE_MAP_KIND (*pc))
36888 {
36889 case GOMP_MAP_TO:
36890 case GOMP_MAP_ALWAYS_TO:
36891 case GOMP_MAP_FROM:
36892 case GOMP_MAP_ALWAYS_FROM:
36893 case GOMP_MAP_TOFROM:
36894 case GOMP_MAP_ALWAYS_TOFROM:
36895 case GOMP_MAP_ALLOC:
36896 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36897 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36898 case GOMP_MAP_ALWAYS_POINTER:
36899 break;
36900 default:
36901 error_at (OMP_CLAUSE_LOCATION (*pc),
36902 "%<#pragma omp target%> with map-type other "
36903 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36904 "on %<map%> clause");
36905 *pc = OMP_CLAUSE_CHAIN (*pc);
36906 continue;
36907 }
36908 pc = &OMP_CLAUSE_CHAIN (*pc);
36909 }
36910 return true;
36911 }
36912
36913 /* OpenACC 2.0:
36914 # pragma acc cache (variable-list) new-line
36915 */
36916
36917 static tree
36918 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36919 {
36920 tree stmt, clauses;
36921
36922 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36923 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36924
36925 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36926
36927 stmt = make_node (OACC_CACHE);
36928 TREE_TYPE (stmt) = void_type_node;
36929 OACC_CACHE_CLAUSES (stmt) = clauses;
36930 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36931 add_stmt (stmt);
36932
36933 return stmt;
36934 }
36935
36936 /* OpenACC 2.0:
36937 # pragma acc data oacc-data-clause[optseq] new-line
36938 structured-block */
36939
36940 #define OACC_DATA_CLAUSE_MASK \
36941 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36948
36949 static tree
36950 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36951 {
36952 tree stmt, clauses, block;
36953 unsigned int save;
36954
36955 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36956 "#pragma acc data", pragma_tok);
36957
36958 block = begin_omp_parallel ();
36959 save = cp_parser_begin_omp_structured_block (parser);
36960 cp_parser_statement (parser, NULL_TREE, false, if_p);
36961 cp_parser_end_omp_structured_block (parser, save);
36962 stmt = finish_oacc_data (clauses, block);
36963 return stmt;
36964 }
36965
36966 /* OpenACC 2.0:
36967 # pragma acc host_data <clauses> new-line
36968 structured-block */
36969
36970 #define OACC_HOST_DATA_CLAUSE_MASK \
36971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36972
36973 static tree
36974 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36975 {
36976 tree stmt, clauses, block;
36977 unsigned int save;
36978
36979 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36980 "#pragma acc host_data", pragma_tok);
36981
36982 block = begin_omp_parallel ();
36983 save = cp_parser_begin_omp_structured_block (parser);
36984 cp_parser_statement (parser, NULL_TREE, false, if_p);
36985 cp_parser_end_omp_structured_block (parser, save);
36986 stmt = finish_oacc_host_data (clauses, block);
36987 return stmt;
36988 }
36989
36990 /* OpenACC 2.0:
36991 # pragma acc declare oacc-data-clause[optseq] new-line
36992 */
36993
36994 #define OACC_DECLARE_CLAUSE_MASK \
36995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
37003
37004 static tree
37005 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
37006 {
37007 tree clauses, stmt;
37008 bool error = false;
37009
37010 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
37011 "#pragma acc declare", pragma_tok, true);
37012
37013
37014 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37015 {
37016 error_at (pragma_tok->location,
37017 "no valid clauses specified in %<#pragma acc declare%>");
37018 return NULL_TREE;
37019 }
37020
37021 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
37022 {
37023 location_t loc = OMP_CLAUSE_LOCATION (t);
37024 tree decl = OMP_CLAUSE_DECL (t);
37025 if (!DECL_P (decl))
37026 {
37027 error_at (loc, "array section in %<#pragma acc declare%>");
37028 error = true;
37029 continue;
37030 }
37031 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
37032 switch (OMP_CLAUSE_MAP_KIND (t))
37033 {
37034 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37035 case GOMP_MAP_ALLOC:
37036 case GOMP_MAP_TO:
37037 case GOMP_MAP_FORCE_DEVICEPTR:
37038 case GOMP_MAP_DEVICE_RESIDENT:
37039 break;
37040
37041 case GOMP_MAP_LINK:
37042 if (!global_bindings_p ()
37043 && (TREE_STATIC (decl)
37044 || !DECL_EXTERNAL (decl)))
37045 {
37046 error_at (loc,
37047 "%qD must be a global variable in "
37048 "%<#pragma acc declare link%>",
37049 decl);
37050 error = true;
37051 continue;
37052 }
37053 break;
37054
37055 default:
37056 if (global_bindings_p ())
37057 {
37058 error_at (loc, "invalid OpenACC clause at file scope");
37059 error = true;
37060 continue;
37061 }
37062 if (DECL_EXTERNAL (decl))
37063 {
37064 error_at (loc,
37065 "invalid use of %<extern%> variable %qD "
37066 "in %<#pragma acc declare%>", decl);
37067 error = true;
37068 continue;
37069 }
37070 else if (TREE_PUBLIC (decl))
37071 {
37072 error_at (loc,
37073 "invalid use of %<global%> variable %qD "
37074 "in %<#pragma acc declare%>", decl);
37075 error = true;
37076 continue;
37077 }
37078 break;
37079 }
37080
37081 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
37082 || lookup_attribute ("omp declare target link",
37083 DECL_ATTRIBUTES (decl)))
37084 {
37085 error_at (loc, "variable %qD used more than once with "
37086 "%<#pragma acc declare%>", decl);
37087 error = true;
37088 continue;
37089 }
37090
37091 if (!error)
37092 {
37093 tree id;
37094
37095 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
37096 id = get_identifier ("omp declare target link");
37097 else
37098 id = get_identifier ("omp declare target");
37099
37100 DECL_ATTRIBUTES (decl)
37101 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
37102 if (global_bindings_p ())
37103 {
37104 symtab_node *node = symtab_node::get (decl);
37105 if (node != NULL)
37106 {
37107 node->offloadable = 1;
37108 if (ENABLE_OFFLOADING)
37109 {
37110 g->have_offload = true;
37111 if (is_a <varpool_node *> (node))
37112 vec_safe_push (offload_vars, decl);
37113 }
37114 }
37115 }
37116 }
37117 }
37118
37119 if (error || global_bindings_p ())
37120 return NULL_TREE;
37121
37122 stmt = make_node (OACC_DECLARE);
37123 TREE_TYPE (stmt) = void_type_node;
37124 OACC_DECLARE_CLAUSES (stmt) = clauses;
37125 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37126
37127 add_stmt (stmt);
37128
37129 return NULL_TREE;
37130 }
37131
37132 /* OpenACC 2.0:
37133 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37134
37135 or
37136
37137 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37138
37139 LOC is the location of the #pragma token.
37140 */
37141
37142 #define OACC_ENTER_DATA_CLAUSE_MASK \
37143 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37148
37149 #define OACC_EXIT_DATA_CLAUSE_MASK \
37150 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37156
37157 static tree
37158 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37159 bool enter)
37160 {
37161 location_t loc = pragma_tok->location;
37162 tree stmt, clauses;
37163 const char *p = "";
37164
37165 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37166 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37167
37168 if (strcmp (p, "data") != 0)
37169 {
37170 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37171 enter ? "enter" : "exit");
37172 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37173 return NULL_TREE;
37174 }
37175
37176 cp_lexer_consume_token (parser->lexer);
37177
37178 if (enter)
37179 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37180 "#pragma acc enter data", pragma_tok);
37181 else
37182 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37183 "#pragma acc exit data", pragma_tok);
37184
37185 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37186 {
37187 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37188 enter ? "enter" : "exit");
37189 return NULL_TREE;
37190 }
37191
37192 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37193 TREE_TYPE (stmt) = void_type_node;
37194 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37195 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37196 add_stmt (stmt);
37197 return stmt;
37198 }
37199
37200 /* OpenACC 2.0:
37201 # pragma acc loop oacc-loop-clause[optseq] new-line
37202 structured-block */
37203
37204 #define OACC_LOOP_CLAUSE_MASK \
37205 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37215
37216 static tree
37217 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37218 omp_clause_mask mask, tree *cclauses, bool *if_p)
37219 {
37220 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37221
37222 strcat (p_name, " loop");
37223 mask |= OACC_LOOP_CLAUSE_MASK;
37224
37225 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37226 cclauses == NULL);
37227 if (cclauses)
37228 {
37229 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37230 if (*cclauses)
37231 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37232 if (clauses)
37233 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37234 }
37235
37236 tree block = begin_omp_structured_block ();
37237 int save = cp_parser_begin_omp_structured_block (parser);
37238 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37239 cp_parser_end_omp_structured_block (parser, save);
37240 add_stmt (finish_omp_structured_block (block));
37241
37242 return stmt;
37243 }
37244
37245 /* OpenACC 2.0:
37246 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37247 structured-block
37248
37249 or
37250
37251 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37252 structured-block
37253 */
37254
37255 #define OACC_KERNELS_CLAUSE_MASK \
37256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37269
37270 #define OACC_PARALLEL_CLAUSE_MASK \
37271 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37287
37288 static tree
37289 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37290 char *p_name, bool *if_p)
37291 {
37292 omp_clause_mask mask;
37293 enum tree_code code;
37294 switch (cp_parser_pragma_kind (pragma_tok))
37295 {
37296 case PRAGMA_OACC_KERNELS:
37297 strcat (p_name, " kernels");
37298 mask = OACC_KERNELS_CLAUSE_MASK;
37299 code = OACC_KERNELS;
37300 break;
37301 case PRAGMA_OACC_PARALLEL:
37302 strcat (p_name, " parallel");
37303 mask = OACC_PARALLEL_CLAUSE_MASK;
37304 code = OACC_PARALLEL;
37305 break;
37306 default:
37307 gcc_unreachable ();
37308 }
37309
37310 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37311 {
37312 const char *p
37313 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37314 if (strcmp (p, "loop") == 0)
37315 {
37316 cp_lexer_consume_token (parser->lexer);
37317 tree block = begin_omp_parallel ();
37318 tree clauses;
37319 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37320 if_p);
37321 return finish_omp_construct (code, block, clauses);
37322 }
37323 }
37324
37325 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37326
37327 tree block = begin_omp_parallel ();
37328 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37329 cp_parser_statement (parser, NULL_TREE, false, if_p);
37330 cp_parser_end_omp_structured_block (parser, save);
37331 return finish_omp_construct (code, block, clauses);
37332 }
37333
37334 /* OpenACC 2.0:
37335 # pragma acc update oacc-update-clause[optseq] new-line
37336 */
37337
37338 #define OACC_UPDATE_CLAUSE_MASK \
37339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37345
37346 static tree
37347 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37348 {
37349 tree stmt, clauses;
37350
37351 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37352 "#pragma acc update", pragma_tok);
37353
37354 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37355 {
37356 error_at (pragma_tok->location,
37357 "%<#pragma acc update%> must contain at least one "
37358 "%<device%> or %<host%> or %<self%> clause");
37359 return NULL_TREE;
37360 }
37361
37362 stmt = make_node (OACC_UPDATE);
37363 TREE_TYPE (stmt) = void_type_node;
37364 OACC_UPDATE_CLAUSES (stmt) = clauses;
37365 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37366 add_stmt (stmt);
37367 return stmt;
37368 }
37369
37370 /* OpenACC 2.0:
37371 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37372
37373 LOC is the location of the #pragma token.
37374 */
37375
37376 #define OACC_WAIT_CLAUSE_MASK \
37377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37378
37379 static tree
37380 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37381 {
37382 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37383 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37384
37385 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37386 list = cp_parser_oacc_wait_list (parser, loc, list);
37387
37388 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37389 "#pragma acc wait", pragma_tok);
37390
37391 stmt = c_finish_oacc_wait (loc, list, clauses);
37392 stmt = finish_expr_stmt (stmt);
37393
37394 return stmt;
37395 }
37396
37397 /* OpenMP 4.0:
37398 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37399
37400 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37401 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37407
37408 static void
37409 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37410 enum pragma_context context)
37411 {
37412 bool first_p = parser->omp_declare_simd == NULL;
37413 cp_omp_declare_simd_data data;
37414 if (first_p)
37415 {
37416 data.error_seen = false;
37417 data.fndecl_seen = false;
37418 data.tokens = vNULL;
37419 data.clauses = NULL_TREE;
37420 /* It is safe to take the address of a local variable; it will only be
37421 used while this scope is live. */
37422 parser->omp_declare_simd = &data;
37423 }
37424
37425 /* Store away all pragma tokens. */
37426 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37427 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37428 cp_lexer_consume_token (parser->lexer);
37429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37430 parser->omp_declare_simd->error_seen = true;
37431 cp_parser_require_pragma_eol (parser, pragma_tok);
37432 struct cp_token_cache *cp
37433 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37434 parser->omp_declare_simd->tokens.safe_push (cp);
37435
37436 if (first_p)
37437 {
37438 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37439 cp_parser_pragma (parser, context, NULL);
37440 switch (context)
37441 {
37442 case pragma_external:
37443 cp_parser_declaration (parser);
37444 break;
37445 case pragma_member:
37446 cp_parser_member_declaration (parser);
37447 break;
37448 case pragma_objc_icode:
37449 cp_parser_block_declaration (parser, /*statement_p=*/false);
37450 break;
37451 default:
37452 cp_parser_declaration_statement (parser);
37453 break;
37454 }
37455 if (parser->omp_declare_simd
37456 && !parser->omp_declare_simd->error_seen
37457 && !parser->omp_declare_simd->fndecl_seen)
37458 error_at (pragma_tok->location,
37459 "%<#pragma omp declare simd%> not immediately followed by "
37460 "function declaration or definition");
37461 data.tokens.release ();
37462 parser->omp_declare_simd = NULL;
37463 }
37464 }
37465
37466 /* Finalize #pragma omp declare simd clauses after direct declarator has
37467 been parsed, and put that into "omp declare simd" attribute. */
37468
37469 static tree
37470 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37471 {
37472 struct cp_token_cache *ce;
37473 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37474 int i;
37475
37476 if (!data->error_seen && data->fndecl_seen)
37477 {
37478 error ("%<#pragma omp declare simd%> not immediately followed by "
37479 "a single function declaration or definition");
37480 data->error_seen = true;
37481 }
37482 if (data->error_seen)
37483 return attrs;
37484
37485 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37486 {
37487 tree c, cl;
37488
37489 cp_parser_push_lexer_for_tokens (parser, ce);
37490 parser->lexer->in_pragma = true;
37491 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37492 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37493 cp_lexer_consume_token (parser->lexer);
37494 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37495 "#pragma omp declare simd", pragma_tok);
37496 cp_parser_pop_lexer (parser);
37497 if (cl)
37498 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37499 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37500 TREE_CHAIN (c) = attrs;
37501 if (processing_template_decl)
37502 ATTR_IS_DEPENDENT (c) = 1;
37503 attrs = c;
37504 }
37505
37506 data->fndecl_seen = true;
37507 return attrs;
37508 }
37509
37510
37511 /* OpenMP 4.0:
37512 # pragma omp declare target new-line
37513 declarations and definitions
37514 # pragma omp end declare target new-line
37515
37516 OpenMP 4.5:
37517 # pragma omp declare target ( extended-list ) new-line
37518
37519 # pragma omp declare target declare-target-clauses[seq] new-line */
37520
37521 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37524
37525 static void
37526 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37527 {
37528 tree clauses = NULL_TREE;
37529 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37530 clauses
37531 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37532 "#pragma omp declare target", pragma_tok);
37533 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37534 {
37535 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37536 clauses);
37537 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37538 cp_parser_require_pragma_eol (parser, pragma_tok);
37539 }
37540 else
37541 {
37542 cp_parser_require_pragma_eol (parser, pragma_tok);
37543 scope_chain->omp_declare_target_attribute++;
37544 return;
37545 }
37546 if (scope_chain->omp_declare_target_attribute)
37547 error_at (pragma_tok->location,
37548 "%<#pragma omp declare target%> with clauses in between "
37549 "%<#pragma omp declare target%> without clauses and "
37550 "%<#pragma omp end declare target%>");
37551 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37552 {
37553 tree t = OMP_CLAUSE_DECL (c), id;
37554 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37555 tree at2 = lookup_attribute ("omp declare target link",
37556 DECL_ATTRIBUTES (t));
37557 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37558 {
37559 id = get_identifier ("omp declare target link");
37560 std::swap (at1, at2);
37561 }
37562 else
37563 id = get_identifier ("omp declare target");
37564 if (at2)
37565 {
37566 error_at (OMP_CLAUSE_LOCATION (c),
37567 "%qD specified both in declare target %<link%> and %<to%>"
37568 " clauses", t);
37569 continue;
37570 }
37571 if (!at1)
37572 {
37573 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37574 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37575 continue;
37576
37577 symtab_node *node = symtab_node::get (t);
37578 if (node != NULL)
37579 {
37580 node->offloadable = 1;
37581 if (ENABLE_OFFLOADING)
37582 {
37583 g->have_offload = true;
37584 if (is_a <varpool_node *> (node))
37585 vec_safe_push (offload_vars, t);
37586 }
37587 }
37588 }
37589 }
37590 }
37591
37592 static void
37593 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37594 {
37595 const char *p = "";
37596 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37597 {
37598 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37599 p = IDENTIFIER_POINTER (id);
37600 }
37601 if (strcmp (p, "declare") == 0)
37602 {
37603 cp_lexer_consume_token (parser->lexer);
37604 p = "";
37605 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37606 {
37607 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37608 p = IDENTIFIER_POINTER (id);
37609 }
37610 if (strcmp (p, "target") == 0)
37611 cp_lexer_consume_token (parser->lexer);
37612 else
37613 {
37614 cp_parser_error (parser, "expected %<target%>");
37615 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37616 return;
37617 }
37618 }
37619 else
37620 {
37621 cp_parser_error (parser, "expected %<declare%>");
37622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37623 return;
37624 }
37625 cp_parser_require_pragma_eol (parser, pragma_tok);
37626 if (!scope_chain->omp_declare_target_attribute)
37627 error_at (pragma_tok->location,
37628 "%<#pragma omp end declare target%> without corresponding "
37629 "%<#pragma omp declare target%>");
37630 else
37631 scope_chain->omp_declare_target_attribute--;
37632 }
37633
37634 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37635 expression and optional initializer clause of
37636 #pragma omp declare reduction. We store the expression(s) as
37637 either 3, 6 or 7 special statements inside of the artificial function's
37638 body. The first two statements are DECL_EXPRs for the artificial
37639 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37640 expression that uses those variables.
37641 If there was any INITIALIZER clause, this is followed by further statements,
37642 the fourth and fifth statements are DECL_EXPRs for the artificial
37643 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37644 constructor variant (first token after open paren is not omp_priv),
37645 then the sixth statement is a statement with the function call expression
37646 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37647 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37648 to initialize the OMP_PRIV artificial variable and there is seventh
37649 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37650
37651 static bool
37652 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37653 {
37654 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37655 gcc_assert (TYPE_REF_P (type));
37656 type = TREE_TYPE (type);
37657 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37658 DECL_ARTIFICIAL (omp_out) = 1;
37659 pushdecl (omp_out);
37660 add_decl_expr (omp_out);
37661 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37662 DECL_ARTIFICIAL (omp_in) = 1;
37663 pushdecl (omp_in);
37664 add_decl_expr (omp_in);
37665 tree combiner;
37666 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37667
37668 keep_next_level (true);
37669 tree block = begin_omp_structured_block ();
37670 combiner = cp_parser_expression (parser);
37671 finish_expr_stmt (combiner);
37672 block = finish_omp_structured_block (block);
37673 add_stmt (block);
37674
37675 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37676 return false;
37677
37678 const char *p = "";
37679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37680 {
37681 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37682 p = IDENTIFIER_POINTER (id);
37683 }
37684
37685 if (strcmp (p, "initializer") == 0)
37686 {
37687 cp_lexer_consume_token (parser->lexer);
37688 matching_parens parens;
37689 if (!parens.require_open (parser))
37690 return false;
37691
37692 p = "";
37693 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37694 {
37695 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37696 p = IDENTIFIER_POINTER (id);
37697 }
37698
37699 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37700 DECL_ARTIFICIAL (omp_priv) = 1;
37701 pushdecl (omp_priv);
37702 add_decl_expr (omp_priv);
37703 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37704 DECL_ARTIFICIAL (omp_orig) = 1;
37705 pushdecl (omp_orig);
37706 add_decl_expr (omp_orig);
37707
37708 keep_next_level (true);
37709 block = begin_omp_structured_block ();
37710
37711 bool ctor = false;
37712 if (strcmp (p, "omp_priv") == 0)
37713 {
37714 bool is_direct_init, is_non_constant_init;
37715 ctor = true;
37716 cp_lexer_consume_token (parser->lexer);
37717 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37718 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37719 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37720 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37721 == CPP_CLOSE_PAREN
37722 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37723 == CPP_CLOSE_PAREN))
37724 {
37725 finish_omp_structured_block (block);
37726 error ("invalid initializer clause");
37727 return false;
37728 }
37729 initializer = cp_parser_initializer (parser, &is_direct_init,
37730 &is_non_constant_init);
37731 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37732 NULL_TREE, LOOKUP_ONLYCONVERTING);
37733 }
37734 else
37735 {
37736 cp_parser_parse_tentatively (parser);
37737 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37738 /*check_dependency_p=*/true,
37739 /*template_p=*/NULL,
37740 /*declarator_p=*/false,
37741 /*optional_p=*/false);
37742 vec<tree, va_gc> *args;
37743 if (fn_name == error_mark_node
37744 || cp_parser_error_occurred (parser)
37745 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37746 || ((args = cp_parser_parenthesized_expression_list
37747 (parser, non_attr, /*cast_p=*/false,
37748 /*allow_expansion_p=*/true,
37749 /*non_constant_p=*/NULL)),
37750 cp_parser_error_occurred (parser)))
37751 {
37752 finish_omp_structured_block (block);
37753 cp_parser_abort_tentative_parse (parser);
37754 cp_parser_error (parser, "expected id-expression (arguments)");
37755 return false;
37756 }
37757 unsigned int i;
37758 tree arg;
37759 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37760 if (arg == omp_priv
37761 || (TREE_CODE (arg) == ADDR_EXPR
37762 && TREE_OPERAND (arg, 0) == omp_priv))
37763 break;
37764 cp_parser_abort_tentative_parse (parser);
37765 if (arg == NULL_TREE)
37766 error ("one of the initializer call arguments should be %<omp_priv%>"
37767 " or %<&omp_priv%>");
37768 initializer = cp_parser_postfix_expression (parser, false, false, false,
37769 false, NULL);
37770 finish_expr_stmt (initializer);
37771 }
37772
37773 block = finish_omp_structured_block (block);
37774 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37775 add_stmt (block);
37776
37777 if (ctor)
37778 add_decl_expr (omp_orig);
37779
37780 if (!parens.require_close (parser))
37781 return false;
37782 }
37783
37784 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37785 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37786 UNKNOWN_LOCATION);
37787
37788 return true;
37789 }
37790
37791 /* OpenMP 4.0
37792 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37793 initializer-clause[opt] new-line
37794
37795 initializer-clause:
37796 initializer (omp_priv initializer)
37797 initializer (function-name (argument-list)) */
37798
37799 static void
37800 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37801 enum pragma_context)
37802 {
37803 auto_vec<tree> types;
37804 enum tree_code reduc_code = ERROR_MARK;
37805 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37806 unsigned int i;
37807 cp_token *first_token;
37808 cp_token_cache *cp;
37809 int errs;
37810 void *p;
37811
37812 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37813 p = obstack_alloc (&declarator_obstack, 0);
37814
37815 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37816 goto fail;
37817
37818 switch (cp_lexer_peek_token (parser->lexer)->type)
37819 {
37820 case CPP_PLUS:
37821 reduc_code = PLUS_EXPR;
37822 break;
37823 case CPP_MULT:
37824 reduc_code = MULT_EXPR;
37825 break;
37826 case CPP_MINUS:
37827 reduc_code = MINUS_EXPR;
37828 break;
37829 case CPP_AND:
37830 reduc_code = BIT_AND_EXPR;
37831 break;
37832 case CPP_XOR:
37833 reduc_code = BIT_XOR_EXPR;
37834 break;
37835 case CPP_OR:
37836 reduc_code = BIT_IOR_EXPR;
37837 break;
37838 case CPP_AND_AND:
37839 reduc_code = TRUTH_ANDIF_EXPR;
37840 break;
37841 case CPP_OR_OR:
37842 reduc_code = TRUTH_ORIF_EXPR;
37843 break;
37844 case CPP_NAME:
37845 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37846 break;
37847 default:
37848 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37849 "%<|%>, %<&&%>, %<||%> or identifier");
37850 goto fail;
37851 }
37852
37853 if (reduc_code != ERROR_MARK)
37854 cp_lexer_consume_token (parser->lexer);
37855
37856 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37857 if (reduc_id == error_mark_node)
37858 goto fail;
37859
37860 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37861 goto fail;
37862
37863 /* Types may not be defined in declare reduction type list. */
37864 const char *saved_message;
37865 saved_message = parser->type_definition_forbidden_message;
37866 parser->type_definition_forbidden_message
37867 = G_("types may not be defined in declare reduction type list");
37868 bool saved_colon_corrects_to_scope_p;
37869 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37870 parser->colon_corrects_to_scope_p = false;
37871 bool saved_colon_doesnt_start_class_def_p;
37872 saved_colon_doesnt_start_class_def_p
37873 = parser->colon_doesnt_start_class_def_p;
37874 parser->colon_doesnt_start_class_def_p = true;
37875
37876 while (true)
37877 {
37878 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37879 type = cp_parser_type_id (parser);
37880 if (type == error_mark_node)
37881 ;
37882 else if (ARITHMETIC_TYPE_P (type)
37883 && (orig_reduc_id == NULL_TREE
37884 || (TREE_CODE (type) != COMPLEX_TYPE
37885 && (id_equal (orig_reduc_id, "min")
37886 || id_equal (orig_reduc_id, "max")))))
37887 error_at (loc, "predeclared arithmetic type %qT in "
37888 "%<#pragma omp declare reduction%>", type);
37889 else if (TREE_CODE (type) == FUNCTION_TYPE
37890 || TREE_CODE (type) == METHOD_TYPE
37891 || TREE_CODE (type) == ARRAY_TYPE)
37892 error_at (loc, "function or array type %qT in "
37893 "%<#pragma omp declare reduction%>", type);
37894 else if (TYPE_REF_P (type))
37895 error_at (loc, "reference type %qT in "
37896 "%<#pragma omp declare reduction%>", type);
37897 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37898 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37899 "%<#pragma omp declare reduction%>", type);
37900 else
37901 types.safe_push (type);
37902
37903 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37904 cp_lexer_consume_token (parser->lexer);
37905 else
37906 break;
37907 }
37908
37909 /* Restore the saved message. */
37910 parser->type_definition_forbidden_message = saved_message;
37911 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37912 parser->colon_doesnt_start_class_def_p
37913 = saved_colon_doesnt_start_class_def_p;
37914
37915 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37916 || types.is_empty ())
37917 {
37918 fail:
37919 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37920 goto done;
37921 }
37922
37923 first_token = cp_lexer_peek_token (parser->lexer);
37924 cp = NULL;
37925 errs = errorcount;
37926 FOR_EACH_VEC_ELT (types, i, type)
37927 {
37928 tree fntype
37929 = build_function_type_list (void_type_node,
37930 cp_build_reference_type (type, false),
37931 NULL_TREE);
37932 tree this_reduc_id = reduc_id;
37933 if (!dependent_type_p (type))
37934 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37935 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37936 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37937 DECL_ARTIFICIAL (fndecl) = 1;
37938 DECL_EXTERNAL (fndecl) = 1;
37939 DECL_DECLARED_INLINE_P (fndecl) = 1;
37940 DECL_IGNORED_P (fndecl) = 1;
37941 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37942 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37943 DECL_ATTRIBUTES (fndecl)
37944 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37945 DECL_ATTRIBUTES (fndecl));
37946 if (processing_template_decl)
37947 fndecl = push_template_decl (fndecl);
37948 bool block_scope = false;
37949 tree block = NULL_TREE;
37950 if (current_function_decl)
37951 {
37952 block_scope = true;
37953 DECL_CONTEXT (fndecl) = global_namespace;
37954 if (!processing_template_decl)
37955 pushdecl (fndecl);
37956 }
37957 else if (current_class_type)
37958 {
37959 if (cp == NULL)
37960 {
37961 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37962 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37963 cp_lexer_consume_token (parser->lexer);
37964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37965 goto fail;
37966 cp = cp_token_cache_new (first_token,
37967 cp_lexer_peek_nth_token (parser->lexer,
37968 2));
37969 }
37970 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37971 finish_member_declaration (fndecl);
37972 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37973 DECL_PENDING_INLINE_P (fndecl) = 1;
37974 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37975 continue;
37976 }
37977 else
37978 {
37979 DECL_CONTEXT (fndecl) = current_namespace;
37980 pushdecl (fndecl);
37981 }
37982 if (!block_scope)
37983 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37984 else
37985 block = begin_omp_structured_block ();
37986 if (cp)
37987 {
37988 cp_parser_push_lexer_for_tokens (parser, cp);
37989 parser->lexer->in_pragma = true;
37990 }
37991 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37992 {
37993 if (!block_scope)
37994 finish_function (/*inline_p=*/false);
37995 else
37996 DECL_CONTEXT (fndecl) = current_function_decl;
37997 if (cp)
37998 cp_parser_pop_lexer (parser);
37999 goto fail;
38000 }
38001 if (cp)
38002 cp_parser_pop_lexer (parser);
38003 if (!block_scope)
38004 finish_function (/*inline_p=*/false);
38005 else
38006 {
38007 DECL_CONTEXT (fndecl) = current_function_decl;
38008 block = finish_omp_structured_block (block);
38009 if (TREE_CODE (block) == BIND_EXPR)
38010 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
38011 else if (TREE_CODE (block) == STATEMENT_LIST)
38012 DECL_SAVED_TREE (fndecl) = block;
38013 if (processing_template_decl)
38014 add_decl_expr (fndecl);
38015 }
38016 cp_check_omp_declare_reduction (fndecl);
38017 if (cp == NULL && types.length () > 1)
38018 cp = cp_token_cache_new (first_token,
38019 cp_lexer_peek_nth_token (parser->lexer, 2));
38020 if (errs != errorcount)
38021 break;
38022 }
38023
38024 cp_parser_require_pragma_eol (parser, pragma_tok);
38025
38026 done:
38027 /* Free any declarators allocated. */
38028 obstack_free (&declarator_obstack, p);
38029 }
38030
38031 /* OpenMP 4.0
38032 #pragma omp declare simd declare-simd-clauses[optseq] new-line
38033 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38034 initializer-clause[opt] new-line
38035 #pragma omp declare target new-line */
38036
38037 static bool
38038 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
38039 enum pragma_context context)
38040 {
38041 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38042 {
38043 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38044 const char *p = IDENTIFIER_POINTER (id);
38045
38046 if (strcmp (p, "simd") == 0)
38047 {
38048 cp_lexer_consume_token (parser->lexer);
38049 cp_parser_omp_declare_simd (parser, pragma_tok,
38050 context);
38051 return true;
38052 }
38053 cp_ensure_no_omp_declare_simd (parser);
38054 if (strcmp (p, "reduction") == 0)
38055 {
38056 cp_lexer_consume_token (parser->lexer);
38057 cp_parser_omp_declare_reduction (parser, pragma_tok,
38058 context);
38059 return false;
38060 }
38061 if (!flag_openmp) /* flag_openmp_simd */
38062 {
38063 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38064 return false;
38065 }
38066 if (strcmp (p, "target") == 0)
38067 {
38068 cp_lexer_consume_token (parser->lexer);
38069 cp_parser_omp_declare_target (parser, pragma_tok);
38070 return false;
38071 }
38072 }
38073 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
38074 "or %<target%>");
38075 cp_parser_require_pragma_eol (parser, pragma_tok);
38076 return false;
38077 }
38078
38079 /* OpenMP 4.5:
38080 #pragma omp taskloop taskloop-clause[optseq] new-line
38081 for-loop
38082
38083 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
38084 for-loop */
38085
38086 #define OMP_TASKLOOP_CLAUSE_MASK \
38087 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
38093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
38094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
38096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
38098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
38099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
38100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
38101
38102 static tree
38103 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
38104 char *p_name, omp_clause_mask mask, tree *cclauses,
38105 bool *if_p)
38106 {
38107 tree clauses, sb, ret;
38108 unsigned int save;
38109 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38110
38111 strcat (p_name, " taskloop");
38112 mask |= OMP_TASKLOOP_CLAUSE_MASK;
38113
38114 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38115 {
38116 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38117 const char *p = IDENTIFIER_POINTER (id);
38118
38119 if (strcmp (p, "simd") == 0)
38120 {
38121 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38122 if (cclauses == NULL)
38123 cclauses = cclauses_buf;
38124
38125 cp_lexer_consume_token (parser->lexer);
38126 if (!flag_openmp) /* flag_openmp_simd */
38127 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38128 cclauses, if_p);
38129 sb = begin_omp_structured_block ();
38130 save = cp_parser_begin_omp_structured_block (parser);
38131 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38132 cclauses, if_p);
38133 cp_parser_end_omp_structured_block (parser, save);
38134 tree body = finish_omp_structured_block (sb);
38135 if (ret == NULL)
38136 return ret;
38137 ret = make_node (OMP_TASKLOOP);
38138 TREE_TYPE (ret) = void_type_node;
38139 OMP_FOR_BODY (ret) = body;
38140 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38141 SET_EXPR_LOCATION (ret, loc);
38142 add_stmt (ret);
38143 return ret;
38144 }
38145 }
38146 if (!flag_openmp) /* flag_openmp_simd */
38147 {
38148 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38149 return NULL_TREE;
38150 }
38151
38152 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38153 cclauses == NULL);
38154 if (cclauses)
38155 {
38156 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38157 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38158 }
38159
38160 sb = begin_omp_structured_block ();
38161 save = cp_parser_begin_omp_structured_block (parser);
38162
38163 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38164 if_p);
38165
38166 cp_parser_end_omp_structured_block (parser, save);
38167 add_stmt (finish_omp_structured_block (sb));
38168
38169 return ret;
38170 }
38171
38172
38173 /* OpenACC 2.0:
38174 # pragma acc routine oacc-routine-clause[optseq] new-line
38175 function-definition
38176
38177 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38178 */
38179
38180 #define OACC_ROUTINE_CLAUSE_MASK \
38181 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38185
38186
38187 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38188 component, which must resolve to a declared namespace-scope
38189 function. The clauses are either processed directly (for a named
38190 function), or defered until the immediatley following declaration
38191 is parsed. */
38192
38193 static void
38194 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38195 enum pragma_context context)
38196 {
38197 gcc_checking_assert (context == pragma_external);
38198 /* The checking for "another pragma following this one" in the "no optional
38199 '( name )'" case makes sure that we dont re-enter. */
38200 gcc_checking_assert (parser->oacc_routine == NULL);
38201
38202 cp_oacc_routine_data data;
38203 data.error_seen = false;
38204 data.fndecl_seen = false;
38205 data.tokens = vNULL;
38206 data.clauses = NULL_TREE;
38207 data.loc = pragma_tok->location;
38208 /* It is safe to take the address of a local variable; it will only be
38209 used while this scope is live. */
38210 parser->oacc_routine = &data;
38211
38212 /* Look for optional '( name )'. */
38213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38214 {
38215 matching_parens parens;
38216 parens.consume_open (parser); /* '(' */
38217
38218 /* We parse the name as an id-expression. If it resolves to
38219 anything other than a non-overloaded function at namespace
38220 scope, it's an error. */
38221 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38222 tree name = cp_parser_id_expression (parser,
38223 /*template_keyword_p=*/false,
38224 /*check_dependency_p=*/false,
38225 /*template_p=*/NULL,
38226 /*declarator_p=*/false,
38227 /*optional_p=*/false);
38228 tree decl = (identifier_p (name)
38229 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38230 : name);
38231 if (name != error_mark_node && decl == error_mark_node)
38232 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38233
38234 if (decl == error_mark_node
38235 || !parens.require_close (parser))
38236 {
38237 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38238 parser->oacc_routine = NULL;
38239 return;
38240 }
38241
38242 data.clauses
38243 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38244 "#pragma acc routine",
38245 cp_lexer_peek_token (parser->lexer));
38246
38247 if (decl && is_overloaded_fn (decl)
38248 && (TREE_CODE (decl) != FUNCTION_DECL
38249 || DECL_FUNCTION_TEMPLATE_P (decl)))
38250 {
38251 error_at (name_loc,
38252 "%<#pragma acc routine%> names a set of overloads");
38253 parser->oacc_routine = NULL;
38254 return;
38255 }
38256
38257 /* Perhaps we should use the same rule as declarations in different
38258 namespaces? */
38259 if (!DECL_NAMESPACE_SCOPE_P (decl))
38260 {
38261 error_at (name_loc,
38262 "%qD does not refer to a namespace scope function", decl);
38263 parser->oacc_routine = NULL;
38264 return;
38265 }
38266
38267 if (TREE_CODE (decl) != FUNCTION_DECL)
38268 {
38269 error_at (name_loc, "%qD does not refer to a function", decl);
38270 parser->oacc_routine = NULL;
38271 return;
38272 }
38273
38274 cp_finalize_oacc_routine (parser, decl, false);
38275 parser->oacc_routine = NULL;
38276 }
38277 else /* No optional '( name )'. */
38278 {
38279 /* Store away all pragma tokens. */
38280 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38281 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38282 cp_lexer_consume_token (parser->lexer);
38283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38284 parser->oacc_routine->error_seen = true;
38285 cp_parser_require_pragma_eol (parser, pragma_tok);
38286 struct cp_token_cache *cp
38287 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38288 parser->oacc_routine->tokens.safe_push (cp);
38289
38290 /* Emit a helpful diagnostic if there's another pragma following this
38291 one. */
38292 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38293 {
38294 cp_ensure_no_oacc_routine (parser);
38295 data.tokens.release ();
38296 /* ..., and then just keep going. */
38297 return;
38298 }
38299
38300 /* We only have to consider the pragma_external case here. */
38301 cp_parser_declaration (parser);
38302 if (parser->oacc_routine
38303 && !parser->oacc_routine->fndecl_seen)
38304 cp_ensure_no_oacc_routine (parser);
38305 else
38306 parser->oacc_routine = NULL;
38307 data.tokens.release ();
38308 }
38309 }
38310
38311 /* Finalize #pragma acc routine clauses after direct declarator has
38312 been parsed. */
38313
38314 static tree
38315 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38316 {
38317 struct cp_token_cache *ce;
38318 cp_oacc_routine_data *data = parser->oacc_routine;
38319
38320 if (!data->error_seen && data->fndecl_seen)
38321 {
38322 error_at (data->loc,
38323 "%<#pragma acc routine%> not immediately followed by "
38324 "a single function declaration or definition");
38325 data->error_seen = true;
38326 }
38327 if (data->error_seen)
38328 return attrs;
38329
38330 gcc_checking_assert (data->tokens.length () == 1);
38331 ce = data->tokens[0];
38332
38333 cp_parser_push_lexer_for_tokens (parser, ce);
38334 parser->lexer->in_pragma = true;
38335 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38336
38337 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38338 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38339 parser->oacc_routine->clauses
38340 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38341 "#pragma acc routine", pragma_tok);
38342 cp_parser_pop_lexer (parser);
38343 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38344 fndecl_seen. */
38345
38346 return attrs;
38347 }
38348
38349 /* Apply any saved OpenACC routine clauses to a just-parsed
38350 declaration. */
38351
38352 static void
38353 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38354 {
38355 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38356 {
38357 /* Keep going if we're in error reporting mode. */
38358 if (parser->oacc_routine->error_seen
38359 || fndecl == error_mark_node)
38360 return;
38361
38362 if (parser->oacc_routine->fndecl_seen)
38363 {
38364 error_at (parser->oacc_routine->loc,
38365 "%<#pragma acc routine%> not immediately followed by"
38366 " a single function declaration or definition");
38367 parser->oacc_routine = NULL;
38368 return;
38369 }
38370 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38371 {
38372 cp_ensure_no_oacc_routine (parser);
38373 return;
38374 }
38375
38376 if (oacc_get_fn_attrib (fndecl))
38377 {
38378 error_at (parser->oacc_routine->loc,
38379 "%<#pragma acc routine%> already applied to %qD", fndecl);
38380 parser->oacc_routine = NULL;
38381 return;
38382 }
38383
38384 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38385 {
38386 error_at (parser->oacc_routine->loc,
38387 TREE_USED (fndecl)
38388 ? G_("%<#pragma acc routine%> must be applied before use")
38389 : G_("%<#pragma acc routine%> must be applied before "
38390 "definition"));
38391 parser->oacc_routine = NULL;
38392 return;
38393 }
38394
38395 /* Process the routine's dimension clauses. */
38396 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38397 oacc_replace_fn_attrib (fndecl, dims);
38398
38399 /* Add an "omp declare target" attribute. */
38400 DECL_ATTRIBUTES (fndecl)
38401 = tree_cons (get_identifier ("omp declare target"),
38402 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38403
38404 /* Don't unset parser->oacc_routine here: we may still need it to
38405 diagnose wrong usage. But, remember that we've used this "#pragma acc
38406 routine". */
38407 parser->oacc_routine->fndecl_seen = true;
38408 }
38409 }
38410
38411 /* Main entry point to OpenMP statement pragmas. */
38412
38413 static void
38414 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38415 {
38416 tree stmt;
38417 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38418 omp_clause_mask mask (0);
38419
38420 switch (cp_parser_pragma_kind (pragma_tok))
38421 {
38422 case PRAGMA_OACC_ATOMIC:
38423 cp_parser_omp_atomic (parser, pragma_tok);
38424 return;
38425 case PRAGMA_OACC_CACHE:
38426 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38427 break;
38428 case PRAGMA_OACC_DATA:
38429 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38430 break;
38431 case PRAGMA_OACC_ENTER_DATA:
38432 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38433 break;
38434 case PRAGMA_OACC_EXIT_DATA:
38435 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38436 break;
38437 case PRAGMA_OACC_HOST_DATA:
38438 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38439 break;
38440 case PRAGMA_OACC_KERNELS:
38441 case PRAGMA_OACC_PARALLEL:
38442 strcpy (p_name, "#pragma acc");
38443 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38444 if_p);
38445 break;
38446 case PRAGMA_OACC_LOOP:
38447 strcpy (p_name, "#pragma acc");
38448 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38449 if_p);
38450 break;
38451 case PRAGMA_OACC_UPDATE:
38452 stmt = cp_parser_oacc_update (parser, pragma_tok);
38453 break;
38454 case PRAGMA_OACC_WAIT:
38455 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38456 break;
38457 case PRAGMA_OMP_ATOMIC:
38458 cp_parser_omp_atomic (parser, pragma_tok);
38459 return;
38460 case PRAGMA_OMP_CRITICAL:
38461 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38462 break;
38463 case PRAGMA_OMP_DISTRIBUTE:
38464 strcpy (p_name, "#pragma omp");
38465 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38466 if_p);
38467 break;
38468 case PRAGMA_OMP_FOR:
38469 strcpy (p_name, "#pragma omp");
38470 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38471 if_p);
38472 break;
38473 case PRAGMA_OMP_MASTER:
38474 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38475 break;
38476 case PRAGMA_OMP_PARALLEL:
38477 strcpy (p_name, "#pragma omp");
38478 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38479 if_p);
38480 break;
38481 case PRAGMA_OMP_SECTIONS:
38482 strcpy (p_name, "#pragma omp");
38483 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38484 break;
38485 case PRAGMA_OMP_SIMD:
38486 strcpy (p_name, "#pragma omp");
38487 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38488 if_p);
38489 break;
38490 case PRAGMA_OMP_SINGLE:
38491 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38492 break;
38493 case PRAGMA_OMP_TASK:
38494 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38495 break;
38496 case PRAGMA_OMP_TASKGROUP:
38497 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38498 break;
38499 case PRAGMA_OMP_TASKLOOP:
38500 strcpy (p_name, "#pragma omp");
38501 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38502 if_p);
38503 break;
38504 case PRAGMA_OMP_TEAMS:
38505 strcpy (p_name, "#pragma omp");
38506 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38507 if_p);
38508 break;
38509 default:
38510 gcc_unreachable ();
38511 }
38512
38513 protected_set_expr_location (stmt, pragma_tok->location);
38514 }
38515 \f
38516 /* Transactional Memory parsing routines. */
38517
38518 /* Parse a transaction attribute.
38519
38520 txn-attribute:
38521 attribute
38522 [ [ identifier ] ]
38523
38524 We use this instead of cp_parser_attributes_opt for transactions to avoid
38525 the pedwarn in C++98 mode. */
38526
38527 static tree
38528 cp_parser_txn_attribute_opt (cp_parser *parser)
38529 {
38530 cp_token *token;
38531 tree attr_name, attr = NULL;
38532
38533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38534 return cp_parser_attributes_opt (parser);
38535
38536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38537 return NULL_TREE;
38538 cp_lexer_consume_token (parser->lexer);
38539 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38540 goto error1;
38541
38542 token = cp_lexer_peek_token (parser->lexer);
38543 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38544 {
38545 token = cp_lexer_consume_token (parser->lexer);
38546
38547 attr_name = (token->type == CPP_KEYWORD
38548 /* For keywords, use the canonical spelling,
38549 not the parsed identifier. */
38550 ? ridpointers[(int) token->keyword]
38551 : token->u.value);
38552 attr = build_tree_list (attr_name, NULL_TREE);
38553 }
38554 else
38555 cp_parser_error (parser, "expected identifier");
38556
38557 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38558 error1:
38559 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38560 return attr;
38561 }
38562
38563 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38564
38565 transaction-statement:
38566 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38567 compound-statement
38568 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38569 */
38570
38571 static tree
38572 cp_parser_transaction (cp_parser *parser, cp_token *token)
38573 {
38574 unsigned char old_in = parser->in_transaction;
38575 unsigned char this_in = 1, new_in;
38576 enum rid keyword = token->keyword;
38577 tree stmt, attrs, noex;
38578
38579 cp_lexer_consume_token (parser->lexer);
38580
38581 if (keyword == RID_TRANSACTION_RELAXED
38582 || keyword == RID_SYNCHRONIZED)
38583 this_in |= TM_STMT_ATTR_RELAXED;
38584 else
38585 {
38586 attrs = cp_parser_txn_attribute_opt (parser);
38587 if (attrs)
38588 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38589 }
38590
38591 /* Parse a noexcept specification. */
38592 if (keyword == RID_ATOMIC_NOEXCEPT)
38593 noex = boolean_true_node;
38594 else if (keyword == RID_ATOMIC_CANCEL)
38595 {
38596 /* cancel-and-throw is unimplemented. */
38597 sorry ("atomic_cancel");
38598 noex = NULL_TREE;
38599 }
38600 else
38601 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38602
38603 /* Keep track if we're in the lexical scope of an outer transaction. */
38604 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38605
38606 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38607
38608 parser->in_transaction = new_in;
38609 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38610 parser->in_transaction = old_in;
38611
38612 finish_transaction_stmt (stmt, NULL, this_in, noex);
38613
38614 return stmt;
38615 }
38616
38617 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38618
38619 transaction-expression:
38620 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38621 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38622 */
38623
38624 static tree
38625 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38626 {
38627 unsigned char old_in = parser->in_transaction;
38628 unsigned char this_in = 1;
38629 cp_token *token;
38630 tree expr, noex;
38631 bool noex_expr;
38632 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38633
38634 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38635 || keyword == RID_TRANSACTION_RELAXED);
38636
38637 if (!flag_tm)
38638 error_at (loc,
38639 keyword == RID_TRANSACTION_RELAXED
38640 ? G_("%<__transaction_relaxed%> without transactional memory "
38641 "support enabled")
38642 : G_("%<__transaction_atomic%> without transactional memory "
38643 "support enabled"));
38644
38645 token = cp_parser_require_keyword (parser, keyword,
38646 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38647 : RT_TRANSACTION_RELAXED));
38648 gcc_assert (token != NULL);
38649
38650 if (keyword == RID_TRANSACTION_RELAXED)
38651 this_in |= TM_STMT_ATTR_RELAXED;
38652
38653 /* Set this early. This might mean that we allow transaction_cancel in
38654 an expression that we find out later actually has to be a constexpr.
38655 However, we expect that cxx_constant_value will be able to deal with
38656 this; also, if the noexcept has no constexpr, then what we parse next
38657 really is a transaction's body. */
38658 parser->in_transaction = this_in;
38659
38660 /* Parse a noexcept specification. */
38661 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38662 true);
38663
38664 if (!noex || !noex_expr
38665 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38666 {
38667 matching_parens parens;
38668 parens.require_open (parser);
38669
38670 expr = cp_parser_expression (parser);
38671 expr = finish_parenthesized_expr (expr);
38672
38673 parens.require_close (parser);
38674 }
38675 else
38676 {
38677 /* The only expression that is available got parsed for the noexcept
38678 already. noexcept is true then. */
38679 expr = noex;
38680 noex = boolean_true_node;
38681 }
38682
38683 expr = build_transaction_expr (token->location, expr, this_in, noex);
38684 parser->in_transaction = old_in;
38685
38686 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38687 return error_mark_node;
38688
38689 return (flag_tm ? expr : error_mark_node);
38690 }
38691
38692 /* Parse a function-transaction-block.
38693
38694 function-transaction-block:
38695 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38696 function-body
38697 __transaction_atomic txn-attribute[opt] function-try-block
38698 __transaction_relaxed ctor-initializer[opt] function-body
38699 __transaction_relaxed function-try-block
38700 */
38701
38702 static void
38703 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38704 {
38705 unsigned char old_in = parser->in_transaction;
38706 unsigned char new_in = 1;
38707 tree compound_stmt, stmt, attrs;
38708 cp_token *token;
38709
38710 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38711 || keyword == RID_TRANSACTION_RELAXED);
38712 token = cp_parser_require_keyword (parser, keyword,
38713 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38714 : RT_TRANSACTION_RELAXED));
38715 gcc_assert (token != NULL);
38716
38717 if (keyword == RID_TRANSACTION_RELAXED)
38718 new_in |= TM_STMT_ATTR_RELAXED;
38719 else
38720 {
38721 attrs = cp_parser_txn_attribute_opt (parser);
38722 if (attrs)
38723 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38724 }
38725
38726 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38727
38728 parser->in_transaction = new_in;
38729
38730 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38731 cp_parser_function_try_block (parser);
38732 else
38733 cp_parser_ctor_initializer_opt_and_function_body
38734 (parser, /*in_function_try_block=*/false);
38735
38736 parser->in_transaction = old_in;
38737
38738 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38739 }
38740
38741 /* Parse a __transaction_cancel statement.
38742
38743 cancel-statement:
38744 __transaction_cancel txn-attribute[opt] ;
38745 __transaction_cancel txn-attribute[opt] throw-expression ;
38746
38747 ??? Cancel and throw is not yet implemented. */
38748
38749 static tree
38750 cp_parser_transaction_cancel (cp_parser *parser)
38751 {
38752 cp_token *token;
38753 bool is_outer = false;
38754 tree stmt, attrs;
38755
38756 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38757 RT_TRANSACTION_CANCEL);
38758 gcc_assert (token != NULL);
38759
38760 attrs = cp_parser_txn_attribute_opt (parser);
38761 if (attrs)
38762 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38763
38764 /* ??? Parse cancel-and-throw here. */
38765
38766 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38767
38768 if (!flag_tm)
38769 {
38770 error_at (token->location, "%<__transaction_cancel%> without "
38771 "transactional memory support enabled");
38772 return error_mark_node;
38773 }
38774 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38775 {
38776 error_at (token->location, "%<__transaction_cancel%> within a "
38777 "%<__transaction_relaxed%>");
38778 return error_mark_node;
38779 }
38780 else if (is_outer)
38781 {
38782 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38783 && !is_tm_may_cancel_outer (current_function_decl))
38784 {
38785 error_at (token->location, "outer %<__transaction_cancel%> not "
38786 "within outer %<__transaction_atomic%>");
38787 error_at (token->location,
38788 " or a %<transaction_may_cancel_outer%> function");
38789 return error_mark_node;
38790 }
38791 }
38792 else if (parser->in_transaction == 0)
38793 {
38794 error_at (token->location, "%<__transaction_cancel%> not within "
38795 "%<__transaction_atomic%>");
38796 return error_mark_node;
38797 }
38798
38799 stmt = build_tm_abort_call (token->location, is_outer);
38800 add_stmt (stmt);
38801
38802 return stmt;
38803 }
38804 \f
38805 /* The parser. */
38806
38807 static GTY (()) cp_parser *the_parser;
38808
38809 \f
38810 /* Special handling for the first token or line in the file. The first
38811 thing in the file might be #pragma GCC pch_preprocess, which loads a
38812 PCH file, which is a GC collection point. So we need to handle this
38813 first pragma without benefit of an existing lexer structure.
38814
38815 Always returns one token to the caller in *FIRST_TOKEN. This is
38816 either the true first token of the file, or the first token after
38817 the initial pragma. */
38818
38819 static void
38820 cp_parser_initial_pragma (cp_token *first_token)
38821 {
38822 tree name = NULL;
38823
38824 cp_lexer_get_preprocessor_token (NULL, first_token);
38825 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38826 return;
38827
38828 cp_lexer_get_preprocessor_token (NULL, first_token);
38829 if (first_token->type == CPP_STRING)
38830 {
38831 name = first_token->u.value;
38832
38833 cp_lexer_get_preprocessor_token (NULL, first_token);
38834 if (first_token->type != CPP_PRAGMA_EOL)
38835 error_at (first_token->location,
38836 "junk at end of %<#pragma GCC pch_preprocess%>");
38837 }
38838 else
38839 error_at (first_token->location, "expected string literal");
38840
38841 /* Skip to the end of the pragma. */
38842 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38843 cp_lexer_get_preprocessor_token (NULL, first_token);
38844
38845 /* Now actually load the PCH file. */
38846 if (name)
38847 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38848
38849 /* Read one more token to return to our caller. We have to do this
38850 after reading the PCH file in, since its pointers have to be
38851 live. */
38852 cp_lexer_get_preprocessor_token (NULL, first_token);
38853 }
38854
38855 /* Parse a pragma GCC ivdep. */
38856
38857 static bool
38858 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38859 {
38860 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38861 return true;
38862 }
38863
38864 /* Parse a pragma GCC unroll. */
38865
38866 static unsigned short
38867 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38868 {
38869 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38870 tree expr = cp_parser_constant_expression (parser);
38871 unsigned short unroll;
38872 expr = maybe_constant_value (expr);
38873 HOST_WIDE_INT lunroll = 0;
38874 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38875 || TREE_CODE (expr) != INTEGER_CST
38876 || (lunroll = tree_to_shwi (expr)) < 0
38877 || lunroll >= USHRT_MAX)
38878 {
38879 error_at (location, "%<#pragma GCC unroll%> requires an"
38880 " assignment-expression that evaluates to a non-negative"
38881 " integral constant less than %u", USHRT_MAX);
38882 unroll = 0;
38883 }
38884 else
38885 {
38886 unroll = (unsigned short)lunroll;
38887 if (unroll == 0)
38888 unroll = 1;
38889 }
38890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38891 return unroll;
38892 }
38893
38894 /* Normal parsing of a pragma token. Here we can (and must) use the
38895 regular lexer. */
38896
38897 static bool
38898 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38899 {
38900 cp_token *pragma_tok;
38901 unsigned int id;
38902 tree stmt;
38903 bool ret;
38904
38905 pragma_tok = cp_lexer_consume_token (parser->lexer);
38906 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38907 parser->lexer->in_pragma = true;
38908
38909 id = cp_parser_pragma_kind (pragma_tok);
38910 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38911 cp_ensure_no_omp_declare_simd (parser);
38912 switch (id)
38913 {
38914 case PRAGMA_GCC_PCH_PREPROCESS:
38915 error_at (pragma_tok->location,
38916 "%<#pragma GCC pch_preprocess%> must be first");
38917 break;
38918
38919 case PRAGMA_OMP_BARRIER:
38920 switch (context)
38921 {
38922 case pragma_compound:
38923 cp_parser_omp_barrier (parser, pragma_tok);
38924 return false;
38925 case pragma_stmt:
38926 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38927 "used in compound statements", "omp barrier");
38928 break;
38929 default:
38930 goto bad_stmt;
38931 }
38932 break;
38933
38934 case PRAGMA_OMP_FLUSH:
38935 switch (context)
38936 {
38937 case pragma_compound:
38938 cp_parser_omp_flush (parser, pragma_tok);
38939 return false;
38940 case pragma_stmt:
38941 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38942 "used in compound statements", "omp flush");
38943 break;
38944 default:
38945 goto bad_stmt;
38946 }
38947 break;
38948
38949 case PRAGMA_OMP_TASKWAIT:
38950 switch (context)
38951 {
38952 case pragma_compound:
38953 cp_parser_omp_taskwait (parser, pragma_tok);
38954 return false;
38955 case pragma_stmt:
38956 error_at (pragma_tok->location,
38957 "%<#pragma %s%> may only be used in compound statements",
38958 "omp taskwait");
38959 break;
38960 default:
38961 goto bad_stmt;
38962 }
38963 break;
38964
38965 case PRAGMA_OMP_TASKYIELD:
38966 switch (context)
38967 {
38968 case pragma_compound:
38969 cp_parser_omp_taskyield (parser, pragma_tok);
38970 return false;
38971 case pragma_stmt:
38972 error_at (pragma_tok->location,
38973 "%<#pragma %s%> may only be used in compound statements",
38974 "omp taskyield");
38975 break;
38976 default:
38977 goto bad_stmt;
38978 }
38979 break;
38980
38981 case PRAGMA_OMP_CANCEL:
38982 switch (context)
38983 {
38984 case pragma_compound:
38985 cp_parser_omp_cancel (parser, pragma_tok);
38986 return false;
38987 case pragma_stmt:
38988 error_at (pragma_tok->location,
38989 "%<#pragma %s%> may only be used in compound statements",
38990 "omp cancel");
38991 break;
38992 default:
38993 goto bad_stmt;
38994 }
38995 break;
38996
38997 case PRAGMA_OMP_CANCELLATION_POINT:
38998 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38999 return false;
39000
39001 case PRAGMA_OMP_THREADPRIVATE:
39002 cp_parser_omp_threadprivate (parser, pragma_tok);
39003 return false;
39004
39005 case PRAGMA_OMP_DECLARE:
39006 return cp_parser_omp_declare (parser, pragma_tok, context);
39007
39008 case PRAGMA_OACC_DECLARE:
39009 cp_parser_oacc_declare (parser, pragma_tok);
39010 return false;
39011
39012 case PRAGMA_OACC_ENTER_DATA:
39013 if (context == pragma_stmt)
39014 {
39015 error_at (pragma_tok->location,
39016 "%<#pragma %s%> may only be used in compound statements",
39017 "acc enter data");
39018 break;
39019 }
39020 else if (context != pragma_compound)
39021 goto bad_stmt;
39022 cp_parser_omp_construct (parser, pragma_tok, if_p);
39023 return true;
39024
39025 case PRAGMA_OACC_EXIT_DATA:
39026 if (context == pragma_stmt)
39027 {
39028 error_at (pragma_tok->location,
39029 "%<#pragma %s%> may only be used in compound statements",
39030 "acc exit data");
39031 break;
39032 }
39033 else if (context != pragma_compound)
39034 goto bad_stmt;
39035 cp_parser_omp_construct (parser, pragma_tok, if_p);
39036 return true;
39037
39038 case PRAGMA_OACC_ROUTINE:
39039 if (context != pragma_external)
39040 {
39041 error_at (pragma_tok->location,
39042 "%<#pragma acc routine%> must be at file scope");
39043 break;
39044 }
39045 cp_parser_oacc_routine (parser, pragma_tok, context);
39046 return false;
39047
39048 case PRAGMA_OACC_UPDATE:
39049 if (context == pragma_stmt)
39050 {
39051 error_at (pragma_tok->location,
39052 "%<#pragma %s%> may only be used in compound statements",
39053 "acc update");
39054 break;
39055 }
39056 else if (context != pragma_compound)
39057 goto bad_stmt;
39058 cp_parser_omp_construct (parser, pragma_tok, if_p);
39059 return true;
39060
39061 case PRAGMA_OACC_WAIT:
39062 if (context == pragma_stmt)
39063 {
39064 error_at (pragma_tok->location,
39065 "%<#pragma %s%> may only be used in compound statements",
39066 "acc wait");
39067 break;
39068 }
39069 else if (context != pragma_compound)
39070 goto bad_stmt;
39071 cp_parser_omp_construct (parser, pragma_tok, if_p);
39072 return true;
39073
39074 case PRAGMA_OACC_ATOMIC:
39075 case PRAGMA_OACC_CACHE:
39076 case PRAGMA_OACC_DATA:
39077 case PRAGMA_OACC_HOST_DATA:
39078 case PRAGMA_OACC_KERNELS:
39079 case PRAGMA_OACC_PARALLEL:
39080 case PRAGMA_OACC_LOOP:
39081 case PRAGMA_OMP_ATOMIC:
39082 case PRAGMA_OMP_CRITICAL:
39083 case PRAGMA_OMP_DISTRIBUTE:
39084 case PRAGMA_OMP_FOR:
39085 case PRAGMA_OMP_MASTER:
39086 case PRAGMA_OMP_PARALLEL:
39087 case PRAGMA_OMP_SECTIONS:
39088 case PRAGMA_OMP_SIMD:
39089 case PRAGMA_OMP_SINGLE:
39090 case PRAGMA_OMP_TASK:
39091 case PRAGMA_OMP_TASKGROUP:
39092 case PRAGMA_OMP_TASKLOOP:
39093 case PRAGMA_OMP_TEAMS:
39094 if (context != pragma_stmt && context != pragma_compound)
39095 goto bad_stmt;
39096 stmt = push_omp_privatization_clauses (false);
39097 cp_parser_omp_construct (parser, pragma_tok, if_p);
39098 pop_omp_privatization_clauses (stmt);
39099 return true;
39100
39101 case PRAGMA_OMP_ORDERED:
39102 if (context != pragma_stmt && context != pragma_compound)
39103 goto bad_stmt;
39104 stmt = push_omp_privatization_clauses (false);
39105 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
39106 pop_omp_privatization_clauses (stmt);
39107 return ret;
39108
39109 case PRAGMA_OMP_TARGET:
39110 if (context != pragma_stmt && context != pragma_compound)
39111 goto bad_stmt;
39112 stmt = push_omp_privatization_clauses (false);
39113 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
39114 pop_omp_privatization_clauses (stmt);
39115 return ret;
39116
39117 case PRAGMA_OMP_END_DECLARE_TARGET:
39118 cp_parser_omp_end_declare_target (parser, pragma_tok);
39119 return false;
39120
39121 case PRAGMA_OMP_SECTION:
39122 error_at (pragma_tok->location,
39123 "%<#pragma omp section%> may only be used in "
39124 "%<#pragma omp sections%> construct");
39125 break;
39126
39127 case PRAGMA_IVDEP:
39128 {
39129 if (context == pragma_external)
39130 {
39131 error_at (pragma_tok->location,
39132 "%<#pragma GCC ivdep%> must be inside a function");
39133 break;
39134 }
39135 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39136 unsigned short unroll;
39137 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39138 if (tok->type == CPP_PRAGMA
39139 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39140 {
39141 tok = cp_lexer_consume_token (parser->lexer);
39142 unroll = cp_parser_pragma_unroll (parser, tok);
39143 tok = cp_lexer_peek_token (the_parser->lexer);
39144 }
39145 else
39146 unroll = 0;
39147 if (tok->type != CPP_KEYWORD
39148 || (tok->keyword != RID_FOR
39149 && tok->keyword != RID_WHILE
39150 && tok->keyword != RID_DO))
39151 {
39152 cp_parser_error (parser, "for, while or do statement expected");
39153 return false;
39154 }
39155 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39156 return true;
39157 }
39158
39159 case PRAGMA_UNROLL:
39160 {
39161 if (context == pragma_external)
39162 {
39163 error_at (pragma_tok->location,
39164 "%<#pragma GCC unroll%> must be inside a function");
39165 break;
39166 }
39167 const unsigned short unroll
39168 = cp_parser_pragma_unroll (parser, pragma_tok);
39169 bool ivdep;
39170 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39171 if (tok->type == CPP_PRAGMA
39172 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39173 {
39174 tok = cp_lexer_consume_token (parser->lexer);
39175 ivdep = cp_parser_pragma_ivdep (parser, tok);
39176 tok = cp_lexer_peek_token (the_parser->lexer);
39177 }
39178 else
39179 ivdep = false;
39180 if (tok->type != CPP_KEYWORD
39181 || (tok->keyword != RID_FOR
39182 && tok->keyword != RID_WHILE
39183 && tok->keyword != RID_DO))
39184 {
39185 cp_parser_error (parser, "for, while or do statement expected");
39186 return false;
39187 }
39188 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39189 return true;
39190 }
39191
39192 default:
39193 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39194 c_invoke_pragma_handler (id);
39195 break;
39196
39197 bad_stmt:
39198 cp_parser_error (parser, "expected declaration specifiers");
39199 break;
39200 }
39201
39202 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39203 return false;
39204 }
39205
39206 /* The interface the pragma parsers have to the lexer. */
39207
39208 enum cpp_ttype
39209 pragma_lex (tree *value, location_t *loc)
39210 {
39211 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39212 enum cpp_ttype ret = tok->type;
39213
39214 *value = tok->u.value;
39215 if (loc)
39216 *loc = tok->location;
39217
39218 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39219 ret = CPP_EOF;
39220 else if (ret == CPP_STRING)
39221 *value = cp_parser_string_literal (the_parser, false, false);
39222 else
39223 {
39224 if (ret == CPP_KEYWORD)
39225 ret = CPP_NAME;
39226 cp_lexer_consume_token (the_parser->lexer);
39227 }
39228
39229 return ret;
39230 }
39231
39232 \f
39233 /* External interface. */
39234
39235 /* Parse one entire translation unit. */
39236
39237 void
39238 c_parse_file (void)
39239 {
39240 static bool already_called = false;
39241
39242 if (already_called)
39243 fatal_error (input_location,
39244 "inter-module optimizations not implemented for C++");
39245 already_called = true;
39246
39247 the_parser = cp_parser_new ();
39248 push_deferring_access_checks (flag_access_control
39249 ? dk_no_deferred : dk_no_check);
39250 cp_parser_translation_unit (the_parser);
39251 the_parser = NULL;
39252
39253 finish_translation_unit ();
39254 }
39255
39256 /* Create an identifier for a generic parameter type (a synthesized
39257 template parameter implied by `auto' or a concept identifier). */
39258
39259 static GTY(()) int generic_parm_count;
39260 static tree
39261 make_generic_type_name ()
39262 {
39263 char buf[32];
39264 sprintf (buf, "auto:%d", ++generic_parm_count);
39265 return get_identifier (buf);
39266 }
39267
39268 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39269 (creating a new template parameter list if necessary). Returns the newly
39270 created template type parm. */
39271
39272 static tree
39273 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39274 {
39275 gcc_assert (current_binding_level->kind == sk_function_parms);
39276
39277 /* Before committing to modifying any scope, if we're in an
39278 implicit template scope, and we're trying to synthesize a
39279 constrained parameter, try to find a previous parameter with
39280 the same name. This is the same-type rule for abbreviated
39281 function templates.
39282
39283 NOTE: We can generate implicit parameters when tentatively
39284 parsing a nested name specifier, only to reject that parse
39285 later. However, matching the same template-id as part of a
39286 direct-declarator should generate an identical template
39287 parameter, so this rule will merge them. */
39288 if (parser->implicit_template_scope && constr)
39289 {
39290 tree t = parser->implicit_template_parms;
39291 while (t)
39292 {
39293 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39294 {
39295 tree d = TREE_VALUE (t);
39296 if (TREE_CODE (d) == PARM_DECL)
39297 /* Return the TEMPLATE_PARM_INDEX. */
39298 d = DECL_INITIAL (d);
39299 return d;
39300 }
39301 t = TREE_CHAIN (t);
39302 }
39303 }
39304
39305 /* We are either continuing a function template that already contains implicit
39306 template parameters, creating a new fully-implicit function template, or
39307 extending an existing explicit function template with implicit template
39308 parameters. */
39309
39310 cp_binding_level *const entry_scope = current_binding_level;
39311
39312 bool become_template = false;
39313 cp_binding_level *parent_scope = 0;
39314
39315 if (parser->implicit_template_scope)
39316 {
39317 gcc_assert (parser->implicit_template_parms);
39318
39319 current_binding_level = parser->implicit_template_scope;
39320 }
39321 else
39322 {
39323 /* Roll back to the existing template parameter scope (in the case of
39324 extending an explicit function template) or introduce a new template
39325 parameter scope ahead of the function parameter scope (or class scope
39326 in the case of out-of-line member definitions). The function scope is
39327 added back after template parameter synthesis below. */
39328
39329 cp_binding_level *scope = entry_scope;
39330
39331 while (scope->kind == sk_function_parms)
39332 {
39333 parent_scope = scope;
39334 scope = scope->level_chain;
39335 }
39336 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39337 {
39338 /* If not defining a class, then any class scope is a scope level in
39339 an out-of-line member definition. In this case simply wind back
39340 beyond the first such scope to inject the template parameter list.
39341 Otherwise wind back to the class being defined. The latter can
39342 occur in class member friend declarations such as:
39343
39344 class A {
39345 void foo (auto);
39346 };
39347 class B {
39348 friend void A::foo (auto);
39349 };
39350
39351 The template parameter list synthesized for the friend declaration
39352 must be injected in the scope of 'B'. This can also occur in
39353 erroneous cases such as:
39354
39355 struct A {
39356 struct B {
39357 void foo (auto);
39358 };
39359 void B::foo (auto) {}
39360 };
39361
39362 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39363 but, nevertheless, the template parameter list synthesized for the
39364 declarator should be injected into the scope of 'A' as if the
39365 ill-formed template was specified explicitly. */
39366
39367 while (scope->kind == sk_class && !scope->defining_class_p)
39368 {
39369 parent_scope = scope;
39370 scope = scope->level_chain;
39371 }
39372 }
39373
39374 current_binding_level = scope;
39375
39376 if (scope->kind != sk_template_parms
39377 || !function_being_declared_is_template_p (parser))
39378 {
39379 /* Introduce a new template parameter list for implicit template
39380 parameters. */
39381
39382 become_template = true;
39383
39384 parser->implicit_template_scope
39385 = begin_scope (sk_template_parms, NULL);
39386
39387 ++processing_template_decl;
39388
39389 parser->fully_implicit_function_template_p = true;
39390 ++parser->num_template_parameter_lists;
39391 }
39392 else
39393 {
39394 /* Synthesize implicit template parameters at the end of the explicit
39395 template parameter list. */
39396
39397 gcc_assert (current_template_parms);
39398
39399 parser->implicit_template_scope = scope;
39400
39401 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39402 parser->implicit_template_parms
39403 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39404 }
39405 }
39406
39407 /* Synthesize a new template parameter and track the current template
39408 parameter chain with implicit_template_parms. */
39409
39410 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39411 tree synth_id = make_generic_type_name ();
39412 tree synth_tmpl_parm;
39413 bool non_type = false;
39414
39415 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39416 synth_tmpl_parm
39417 = finish_template_type_parm (class_type_node, synth_id);
39418 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39419 synth_tmpl_parm
39420 = finish_constrained_template_template_parm (proto, synth_id);
39421 else
39422 {
39423 synth_tmpl_parm = copy_decl (proto);
39424 DECL_NAME (synth_tmpl_parm) = synth_id;
39425 non_type = true;
39426 }
39427
39428 // Attach the constraint to the parm before processing.
39429 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39430 TREE_TYPE (node) = constr;
39431 tree new_parm
39432 = process_template_parm (parser->implicit_template_parms,
39433 input_location,
39434 node,
39435 /*non_type=*/non_type,
39436 /*param_pack=*/false);
39437
39438 // Chain the new parameter to the list of implicit parameters.
39439 if (parser->implicit_template_parms)
39440 parser->implicit_template_parms
39441 = TREE_CHAIN (parser->implicit_template_parms);
39442 else
39443 parser->implicit_template_parms = new_parm;
39444
39445 tree new_decl = get_local_decls ();
39446 if (non_type)
39447 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39448 new_decl = DECL_INITIAL (new_decl);
39449
39450 /* If creating a fully implicit function template, start the new implicit
39451 template parameter list with this synthesized type, otherwise grow the
39452 current template parameter list. */
39453
39454 if (become_template)
39455 {
39456 parent_scope->level_chain = current_binding_level;
39457
39458 tree new_parms = make_tree_vec (1);
39459 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39460 current_template_parms = tree_cons (size_int (processing_template_decl),
39461 new_parms, current_template_parms);
39462 }
39463 else
39464 {
39465 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39466 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39467 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39468 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39469 }
39470
39471 // If the new parameter was constrained, we need to add that to the
39472 // constraints in the template parameter list.
39473 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39474 {
39475 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39476 reqs = conjoin_constraints (reqs, req);
39477 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39478 }
39479
39480 current_binding_level = entry_scope;
39481
39482 return new_decl;
39483 }
39484
39485 /* Finish the declaration of a fully implicit function template. Such a
39486 template has no explicit template parameter list so has not been through the
39487 normal template head and tail processing. synthesize_implicit_template_parm
39488 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39489 provided if the declaration is a class member such that its template
39490 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39491 form is returned. Otherwise NULL_TREE is returned. */
39492
39493 static tree
39494 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39495 {
39496 gcc_assert (parser->fully_implicit_function_template_p);
39497
39498 if (member_decl_opt && member_decl_opt != error_mark_node
39499 && DECL_VIRTUAL_P (member_decl_opt))
39500 {
39501 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39502 "implicit templates may not be %<virtual%>");
39503 DECL_VIRTUAL_P (member_decl_opt) = false;
39504 }
39505
39506 if (member_decl_opt)
39507 member_decl_opt = finish_member_template_decl (member_decl_opt);
39508 end_template_decl ();
39509
39510 parser->fully_implicit_function_template_p = false;
39511 parser->implicit_template_parms = 0;
39512 parser->implicit_template_scope = 0;
39513 --parser->num_template_parameter_lists;
39514
39515 return member_decl_opt;
39516 }
39517
39518 /* Like finish_fully_implicit_template, but to be used in error
39519 recovery, rearranging scopes so that we restore the state we had
39520 before synthesize_implicit_template_parm inserted the implement
39521 template parms scope. */
39522
39523 static void
39524 abort_fully_implicit_template (cp_parser *parser)
39525 {
39526 cp_binding_level *return_to_scope = current_binding_level;
39527
39528 if (parser->implicit_template_scope
39529 && return_to_scope != parser->implicit_template_scope)
39530 {
39531 cp_binding_level *child = return_to_scope;
39532 for (cp_binding_level *scope = child->level_chain;
39533 scope != parser->implicit_template_scope;
39534 scope = child->level_chain)
39535 child = scope;
39536 child->level_chain = parser->implicit_template_scope->level_chain;
39537 parser->implicit_template_scope->level_chain = return_to_scope;
39538 current_binding_level = parser->implicit_template_scope;
39539 }
39540 else
39541 return_to_scope = return_to_scope->level_chain;
39542
39543 finish_fully_implicit_template (parser, NULL);
39544
39545 gcc_assert (current_binding_level == return_to_scope);
39546 }
39547
39548 /* Helper function for diagnostics that have complained about things
39549 being used with 'extern "C"' linkage.
39550
39551 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39552
39553 void
39554 maybe_show_extern_c_location (void)
39555 {
39556 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39557 inform (the_parser->innermost_linkage_specification_location,
39558 "%<extern \"C\"%> linkage started here");
39559 }
39560
39561 #include "gt-cp-parser.h"