]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
Fix numerous typos in comments
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 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 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46
47 \f
48 /* The lexer. */
49
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
52
53 static cp_token eof_token =
54 {
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
56 };
57
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
115 };
116
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
127 };
128
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
152
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
174
175 RT_SELECT, /* selection-statement */
176 RT_INTERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
183 };
184
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
187
188 class type_id_in_expr_sentinel
189 {
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
199 };
200
201 /* Prototypes. */
202
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
248
249 static void cp_parser_initial_pragma
250 (cp_token *);
251
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
262
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
266
267 /* Variables. */
268
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
271
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
275
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
281
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
286 {
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
290
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292
293 if (buffer == NULL)
294 return;
295
296 if (num == 0)
297 num = buffer->length ();
298
299 if (start_token == NULL)
300 start_token = buffer->address ();
301
302 if (start_token > buffer->address ())
303 {
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
306 }
307
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 {
312 if (token == start_token)
313 do_print = true;
314
315 if (!do_print)
316 continue;
317
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
321
322 cp_lexer_print_token (file, token);
323
324 if (token == curr_token)
325 fprintf (file, "]]");
326
327 switch (token->type)
328 {
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
335
336 default:
337 fputc (' ', file);
338 }
339 }
340
341 if (i == num && i < buffer->length ())
342 {
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
345 }
346
347 fprintf (file, "\n");
348 }
349
350
351 /* Dump all tokens in BUFFER to stderr. */
352
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 {
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
357 }
358
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
361 {
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
363 }
364
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
367 {
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
372 }
373
374
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
377
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380 {
381 if (t)
382 {
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
385 }
386 }
387
388
389 /* Dump parser context C to FILE. */
390
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 {
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
398 }
399
400
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
402
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405 {
406 unsigned i;
407 cp_parser_context *c;
408
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
411 {
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
414 }
415 }
416
417
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422 {
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
425 }
426
427
428 /* Print an unparsed function entry UF to FILE. */
429
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432 {
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
436
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
441 {
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
447 }
448
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 {
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
455 }
456 fprintf (file, "\n");
457
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 {
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
464 }
465 fprintf (file, "\n");
466 }
467
468
469 /* Print the stack of unparsed member functions S to FILE. */
470
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
474 {
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
477
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 {
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
483 }
484 }
485
486
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 {
493 cp_token *next_token, *first_token, *start_token;
494
495 if (file == NULL)
496 file = stderr;
497
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
505 }
506
507
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
510
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
513 {
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
517
518 if (file == NULL)
519 file = stderr;
520
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
594 }
595
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
598 {
599 cp_debug_parser (stderr, &ref);
600 }
601
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
604 {
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
609 }
610
611 /* Allocate memory for a new lexer object and return it. */
612
613 static cp_lexer *
614 cp_lexer_alloc (void)
615 {
616 cp_lexer *lexer;
617
618 c_common_no_more_pch ();
619
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
622
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
625
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630
631 return lexer;
632 }
633
634
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
637
638 static cp_lexer *
639 cp_lexer_new_main (void)
640 {
641 cp_lexer *lexer;
642 cp_token token;
643
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
648
649 lexer = cp_lexer_alloc ();
650
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
653
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
656 {
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
659 }
660
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
667
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
671
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
674 }
675
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
678
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
681 {
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
685
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
690
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
695
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
698 }
699
700 /* Frees all resources associated with LEXER. */
701
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
704 {
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
708 }
709
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
714
715 #define LEXER_DEBUGGING_ENABLED_P false
716
717 /* Returns nonzero if debugging information should be output. */
718
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
721 {
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
724
725 return lexer->debugging_p;
726 }
727
728
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
731 {
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
733
734 return lexer->next_token - previous_p;
735 }
736
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
739 {
740 return pos;
741 }
742
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
745 {
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
747 }
748
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
751 {
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
756 }
757
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
760 {
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
762
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
765 {
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
768 }
769
770 return cp_lexer_token_at (lexer, tp);
771 }
772
773 /* nonzero if we are presently saving tokens. */
774
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
777 {
778 return lexer->saved_tokens.length () != 0;
779 }
780
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
785
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
788 {
789 static int is_extern_c = 0;
790
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
798
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
805
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
808 {
809 if (C_IS_RESERVED_WORD (token->u.value))
810 {
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
815 }
816 else
817 {
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
821 {
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
827
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
831 }
832
833 token->keyword = RID_MAX;
834 }
835 }
836 else if (token->type == CPP_AT_NAME)
837 {
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
841 {
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
849 */
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
859 }
860 }
861 }
862
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
866 {
867 if (token->type != CPP_EOF)
868 {
869 input_location = token->location;
870 }
871 }
872
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
876 {
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
879 }
880
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
883
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
886 {
887 if (cp_lexer_debugging_p (lexer))
888 {
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
892 }
893 return lexer->next_token;
894 }
895
896 /* Return true if the next token has the indicated TYPE. */
897
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
900 {
901 return cp_lexer_peek_token (lexer)->type == type;
902 }
903
904 /* Return true if the next token does not have the indicated TYPE. */
905
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
908 {
909 return !cp_lexer_next_token_is (lexer, type);
910 }
911
912 /* Return true if the next token is the indicated KEYWORD. */
913
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
916 {
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
918 }
919
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
922 {
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
924 }
925
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
928 {
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
930 }
931
932 /* Return true if the next token is not the indicated KEYWORD. */
933
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
936 {
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
938 }
939
940 /* Return true if KEYWORD can start a decl-specifier. */
941
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
944 {
945 switch (keyword)
946 {
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
984
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
991 }
992 }
993
994 /* Return true if the next token is a keyword for a decl-specifier. */
995
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
998 {
999 cp_token *token;
1000
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1003 }
1004
1005 /* Returns TRUE iff the token T begins a decltype type. */
1006
1007 static bool
1008 token_is_decltype (cp_token *t)
1009 {
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1012 }
1013
1014 /* Returns TRUE iff the next token begins a decltype type. */
1015
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1018 {
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1021 }
1022
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1025
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1028 {
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1034 {
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1040 }
1041 /* Return the stored value. */
1042 return check_value->value;
1043 }
1044
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1050
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1053 {
1054 cp_token *token;
1055
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1058
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1062
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1067 {
1068 ++token;
1069 if (token == lexer->last_token)
1070 {
1071 token = &eof_token;
1072 break;
1073 }
1074
1075 if (!token->purged_p)
1076 --n;
1077 }
1078
1079 if (cp_lexer_debugging_p (lexer))
1080 {
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1083 }
1084
1085 return token;
1086 }
1087
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1090
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1093 {
1094 cp_token *token = lexer->next_token;
1095
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1098
1099 do
1100 {
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1103 {
1104 lexer->next_token = &eof_token;
1105 break;
1106 }
1107
1108 }
1109 while (lexer->next_token->purged_p);
1110
1111 cp_lexer_set_source_position_from_token (token);
1112
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1115 {
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1119 }
1120
1121 return token;
1122 }
1123
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1127
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1130 {
1131 cp_token *tok = lexer->next_token;
1132
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1138
1139 do
1140 {
1141 tok++;
1142 if (tok == lexer->last_token)
1143 {
1144 tok = &eof_token;
1145 break;
1146 }
1147 }
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1150 }
1151
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1155
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1158 {
1159 cp_token *peek = lexer->next_token;
1160
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1163
1164 gcc_assert (tok < peek);
1165
1166 for ( tok += 1; tok != peek; tok += 1)
1167 {
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1172 }
1173 }
1174
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1177
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1180 {
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1184
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1186 }
1187
1188 /* Commit to the portion of the token stream most recently saved. */
1189
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1192 {
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1196
1197 lexer->saved_tokens.pop ();
1198 }
1199
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1202
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1205 {
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1209
1210 lexer->next_token = lexer->saved_tokens.pop ();
1211 }
1212
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1217
1218 struct saved_token_sentinel
1219 {
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1224 {
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1227 }
1228 void rollback ()
1229 {
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1232 }
1233 ~saved_token_sentinel()
1234 {
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1238 }
1239 };
1240
1241 /* Print a representation of the TOKEN on the STREAM. */
1242
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1245 {
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1259 };
1260
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1263 {
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1273
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1281
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value, 0);
1284 break;
1285
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1294 }
1295 }
1296
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1299 {
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1302 }
1303
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1306 {
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1311 }
1312
1313
1314 /* Start emitting debugging information. */
1315
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1318 {
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1322
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1325 }
1326
1327 /* Stop emitting debugging information. */
1328
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1331 {
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1335
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1338 }
1339
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1341
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1344 {
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1349 }
1350
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1353
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1356 {
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1358 {
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1362 }
1363 }
1364
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1367
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1370 {
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1372 {
1373 if (fndecl == error_mark_node)
1374 {
1375 parser->omp_declare_simd = NULL;
1376 return;
1377 }
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1379 {
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1382 }
1383 }
1384 }
1385
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1388
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1391 {
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1393 {
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1398 }
1399 }
1400 \f
1401 /* Decl-specifiers. */
1402
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1404
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1407 {
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1409 }
1410
1411 /* Declarators. */
1412
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1417
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1428
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1431
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1435
1436 /* Alloc BYTES from the declarator memory pool. */
1437
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1440 {
1441 return obstack_alloc (&declarator_obstack, bytes);
1442 }
1443
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1446
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1449 {
1450 cp_declarator *declarator;
1451
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1459
1460 return declarator;
1461 }
1462
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1468
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1472 {
1473 cp_declarator *declarator;
1474
1475 /* It is valid to write:
1476
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1480
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1487
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1491
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1496
1497 return declarator;
1498 }
1499
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1504
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1508 {
1509 cp_declarator *declarator;
1510
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1516 {
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1520 }
1521 else
1522 declarator->parameter_pack_p = false;
1523
1524 declarator->std_attributes = attributes;
1525
1526 return declarator;
1527 }
1528
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1532
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1536 {
1537 cp_declarator *declarator;
1538
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1544 {
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1548 }
1549 else
1550 declarator->parameter_pack_p = false;
1551
1552 declarator->std_attributes = attributes;
1553
1554 return declarator;
1555 }
1556
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1560
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1565 {
1566 cp_declarator *declarator;
1567
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1572
1573 if (pointee)
1574 {
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1577 }
1578 else
1579 declarator->parameter_pack_p = false;
1580
1581 declarator->std_attributes = attributes;
1582
1583 return declarator;
1584 }
1585
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1590
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1601 {
1602 cp_declarator *declarator;
1603
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1615 {
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1619 }
1620 else
1621 declarator->parameter_pack_p = false;
1622
1623 return declarator;
1624 }
1625
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1628
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1631 {
1632 cp_declarator *declarator;
1633
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1638 {
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1642 }
1643 else
1644 declarator->parameter_pack_p = false;
1645
1646 return declarator;
1647 }
1648
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1653 {
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1657
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1664 {
1665 switch ((int)declarator->kind)
1666 {
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1672
1673 case cdk_error:
1674 return true;
1675
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1679 }
1680 }
1681
1682 return !found;
1683 }
1684
1685 cp_parameter_declarator *no_parameters;
1686
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1689
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1695 {
1696 cp_parameter_declarator *parameter;
1697
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1708
1709 return parameter;
1710 }
1711
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1713
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1716 {
1717 while (declarator)
1718 {
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1727 }
1728 return false;
1729 }
1730
1731 /* The parser. */
1732
1733 /* Overview
1734 --------
1735
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1743
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1747
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1756
1757 Methodology
1758 -----------
1759
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1776
1777 Future Improvements
1778 -------------------
1779
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1785
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1788
1789 enum
1790 {
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1807 };
1808
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1812
1813 /* The different kinds of declarators we want to parse. */
1814
1815 enum cp_parser_declarator_kind
1816 {
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1823 };
1824
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1828
1829 enum cp_parser_prec
1830 {
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1844 };
1845
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1848
1849 struct cp_parser_binary_operations_map_node
1850 {
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1857 };
1858
1859 struct cp_parser_expression_stack_entry
1860 {
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1873 };
1874
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1880
1881 /* Prototypes. */
1882
1883 /* Constructors and destructors. */
1884
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1887
1888 /* Class variables. */
1889
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1891
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1895
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1899
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1903
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1906
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1909
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1914
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1917
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1919
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1921
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1923
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1925
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1927 };
1928
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1933
1934 /* Constructors and destructors. */
1935
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1938
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1941 {
1942 cp_parser_context *context;
1943
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1946 {
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1951 }
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1954
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1960 {
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1966 }
1967
1968 return context;
1969 }
1970
1971 /* Managing the unparsed function queues. */
1972
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1981
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1984 {
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1987 }
1988
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1991 {
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1994 }
1995
1996 /* Prototypes. */
1997
1998 /* Constructors and destructors. */
1999
2000 static cp_parser *cp_parser_new
2001 (void);
2002
2003 /* Routines to parse various constructs.
2004
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2014
2015 /* Lexical conventions [gram.lex] */
2016
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2027
2028 /* Basic concepts [gram.basic] */
2029
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2032
2033 /* Expressions [gram.expr] */
2034
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2101
2102 /* Statements [gram.stmt.stmt] */
2103
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2138
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2143
2144 /* Declarations [gram.dcl.dcl] */
2145
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2205
2206 /* Declarators [gram.dcl.decl] */
2207
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2256
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2259
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2262
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2265
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2268
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2273
2274 /* Classes [gram.class] */
2275
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2294
2295 /* Derived classes [gram.class.derived] */
2296
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2301
2302 /* Special member functions [gram.special] */
2303
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2318
2319 /* Overloading [gram.over] */
2320
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2325
2326 /* Templates [gram.temp] */
2327
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2348
2349 /* Exception handling [gram.exception] */
2350
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2367
2368 /* GNU Extensions */
2369
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2406
2407 /* Concept Extensions */
2408
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2431
2432 /* Transactional Memory Extensions */
2433
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2442
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2449 };
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2452
2453 /* Objective-C++ Productions */
2454
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2491
2492 /* Utility Routines */
2493
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2651
2652 /* Concept-related syntactic transformations */
2653
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2656
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2659 //
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2662 {
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2665 }
2666
2667 cp_unevaluated::~cp_unevaluated ()
2668 {
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2671 }
2672
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2675
2676 /* Returns nonzero if we are parsing tentatively. */
2677
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2680 {
2681 return parser->context->next != NULL;
2682 }
2683
2684 /* Returns nonzero if TOKEN is a string literal. */
2685
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2688 {
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2694 }
2695
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2698
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2701 {
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2708 }
2709
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2714 {
2715 return token->keyword == keyword;
2716 }
2717
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2720
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2723 {
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 }
2729
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2740
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2744 {
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2754
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2759
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2766
2767 return true;
2768 }
2769
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2775
2776 static void
2777 cp_parser_error (cp_parser* parser, const char* gmsgid)
2778 {
2779 if (!cp_parser_simulate_error (parser))
2780 {
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2785
2786 if (token->type == CPP_PRAGMA)
2787 {
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2792 }
2793
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2798 {
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2801 {
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2804 }
2805 }
2806
2807 c_parse_error (gmsgid,
2808 /* Because c_parser_error does not understand
2809 CPP_KEYWORD, keywords are treated like
2810 identifiers. */
2811 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2812 token->u.value, token->flags);
2813 }
2814 }
2815
2816 /* Issue an error about name-lookup failing. NAME is the
2817 IDENTIFIER_NODE DECL is the result of
2818 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2819 the thing that we hoped to find. */
2820
2821 static void
2822 cp_parser_name_lookup_error (cp_parser* parser,
2823 tree name,
2824 tree decl,
2825 name_lookup_error desired,
2826 location_t location)
2827 {
2828 /* If name lookup completely failed, tell the user that NAME was not
2829 declared. */
2830 if (decl == error_mark_node)
2831 {
2832 if (parser->scope && parser->scope != global_namespace)
2833 error_at (location, "%<%E::%E%> has not been declared",
2834 parser->scope, name);
2835 else if (parser->scope == global_namespace)
2836 error_at (location, "%<::%E%> has not been declared", name);
2837 else if (parser->object_scope
2838 && !CLASS_TYPE_P (parser->object_scope))
2839 error_at (location, "request for member %qE in non-class type %qT",
2840 name, parser->object_scope);
2841 else if (parser->object_scope)
2842 error_at (location, "%<%T::%E%> has not been declared",
2843 parser->object_scope, name);
2844 else
2845 error_at (location, "%qE has not been declared", name);
2846 }
2847 else if (parser->scope && parser->scope != global_namespace)
2848 {
2849 switch (desired)
2850 {
2851 case NLE_TYPE:
2852 error_at (location, "%<%E::%E%> is not a type",
2853 parser->scope, name);
2854 break;
2855 case NLE_CXX98:
2856 error_at (location, "%<%E::%E%> is not a class or namespace",
2857 parser->scope, name);
2858 break;
2859 case NLE_NOT_CXX98:
2860 error_at (location,
2861 "%<%E::%E%> is not a class, namespace, or enumeration",
2862 parser->scope, name);
2863 break;
2864 default:
2865 gcc_unreachable ();
2866
2867 }
2868 }
2869 else if (parser->scope == global_namespace)
2870 {
2871 switch (desired)
2872 {
2873 case NLE_TYPE:
2874 error_at (location, "%<::%E%> is not a type", name);
2875 break;
2876 case NLE_CXX98:
2877 error_at (location, "%<::%E%> is not a class or namespace", name);
2878 break;
2879 case NLE_NOT_CXX98:
2880 error_at (location,
2881 "%<::%E%> is not a class, namespace, or enumeration",
2882 name);
2883 break;
2884 default:
2885 gcc_unreachable ();
2886 }
2887 }
2888 else
2889 {
2890 switch (desired)
2891 {
2892 case NLE_TYPE:
2893 error_at (location, "%qE is not a type", name);
2894 break;
2895 case NLE_CXX98:
2896 error_at (location, "%qE is not a class or namespace", name);
2897 break;
2898 case NLE_NOT_CXX98:
2899 error_at (location,
2900 "%qE is not a class, namespace, or enumeration", name);
2901 break;
2902 default:
2903 gcc_unreachable ();
2904 }
2905 }
2906 }
2907
2908 /* If we are parsing tentatively, remember that an error has occurred
2909 during this tentative parse. Returns true if the error was
2910 simulated; false if a message should be issued by the caller. */
2911
2912 static bool
2913 cp_parser_simulate_error (cp_parser* parser)
2914 {
2915 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2916 {
2917 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2918 return true;
2919 }
2920 return false;
2921 }
2922
2923 /* This function is called when a type is defined. If type
2924 definitions are forbidden at this point, an error message is
2925 issued. */
2926
2927 static bool
2928 cp_parser_check_type_definition (cp_parser* parser)
2929 {
2930 /* If types are forbidden here, issue a message. */
2931 if (parser->type_definition_forbidden_message)
2932 {
2933 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2934 in the message need to be interpreted. */
2935 error (parser->type_definition_forbidden_message);
2936 return false;
2937 }
2938 return true;
2939 }
2940
2941 /* This function is called when the DECLARATOR is processed. The TYPE
2942 was a type defined in the decl-specifiers. If it is invalid to
2943 define a type in the decl-specifiers for DECLARATOR, an error is
2944 issued. TYPE_LOCATION is the location of TYPE and is used
2945 for error reporting. */
2946
2947 static void
2948 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2949 tree type, location_t type_location)
2950 {
2951 /* [dcl.fct] forbids type definitions in return types.
2952 Unfortunately, it's not easy to know whether or not we are
2953 processing a return type until after the fact. */
2954 while (declarator
2955 && (declarator->kind == cdk_pointer
2956 || declarator->kind == cdk_reference
2957 || declarator->kind == cdk_ptrmem))
2958 declarator = declarator->declarator;
2959 if (declarator
2960 && declarator->kind == cdk_function)
2961 {
2962 error_at (type_location,
2963 "new types may not be defined in a return type");
2964 inform (type_location,
2965 "(perhaps a semicolon is missing after the definition of %qT)",
2966 type);
2967 }
2968 }
2969
2970 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2971 "<" in any valid C++ program. If the next token is indeed "<",
2972 issue a message warning the user about what appears to be an
2973 invalid attempt to form a template-id. LOCATION is the location
2974 of the type-specifier (TYPE) */
2975
2976 static void
2977 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2978 tree type,
2979 enum tag_types tag_type,
2980 location_t location)
2981 {
2982 cp_token_position start = 0;
2983
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2985 {
2986 if (TYPE_P (type))
2987 error_at (location, "%qT is not a template", type);
2988 else if (identifier_p (type))
2989 {
2990 if (tag_type != none_type)
2991 error_at (location, "%qE is not a class template", type);
2992 else
2993 error_at (location, "%qE is not a template", type);
2994 }
2995 else
2996 error_at (location, "invalid template-id");
2997 /* Remember the location of the invalid "<". */
2998 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2999 start = cp_lexer_token_position (parser->lexer, true);
3000 /* Consume the "<". */
3001 cp_lexer_consume_token (parser->lexer);
3002 /* Parse the template arguments. */
3003 cp_parser_enclosed_template_argument_list (parser);
3004 /* Permanently remove the invalid template arguments so that
3005 this error message is not issued again. */
3006 if (start)
3007 cp_lexer_purge_tokens_after (parser->lexer, start);
3008 }
3009 }
3010
3011 /* If parsing an integral constant-expression, issue an error message
3012 about the fact that THING appeared and return true. Otherwise,
3013 return false. In either case, set
3014 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3015
3016 static bool
3017 cp_parser_non_integral_constant_expression (cp_parser *parser,
3018 non_integral_constant thing)
3019 {
3020 parser->non_integral_constant_expression_p = true;
3021 if (parser->integral_constant_expression_p)
3022 {
3023 if (!parser->allow_non_integral_constant_expression_p)
3024 {
3025 const char *msg = NULL;
3026 switch (thing)
3027 {
3028 case NIC_FLOAT:
3029 pedwarn (input_location, OPT_Wpedantic,
3030 "ISO C++ forbids using a floating-point literal "
3031 "in a constant-expression");
3032 return true;
3033 case NIC_CAST:
3034 error ("a cast to a type other than an integral or "
3035 "enumeration type cannot appear in a "
3036 "constant-expression");
3037 return true;
3038 case NIC_TYPEID:
3039 error ("%<typeid%> operator "
3040 "cannot appear in a constant-expression");
3041 return true;
3042 case NIC_NCC:
3043 error ("non-constant compound literals "
3044 "cannot appear in a constant-expression");
3045 return true;
3046 case NIC_FUNC_CALL:
3047 error ("a function call "
3048 "cannot appear in a constant-expression");
3049 return true;
3050 case NIC_INC:
3051 error ("an increment "
3052 "cannot appear in a constant-expression");
3053 return true;
3054 case NIC_DEC:
3055 error ("an decrement "
3056 "cannot appear in a constant-expression");
3057 return true;
3058 case NIC_ARRAY_REF:
3059 error ("an array reference "
3060 "cannot appear in a constant-expression");
3061 return true;
3062 case NIC_ADDR_LABEL:
3063 error ("the address of a label "
3064 "cannot appear in a constant-expression");
3065 return true;
3066 case NIC_OVERLOADED:
3067 error ("calls to overloaded operators "
3068 "cannot appear in a constant-expression");
3069 return true;
3070 case NIC_ASSIGNMENT:
3071 error ("an assignment cannot appear in a constant-expression");
3072 return true;
3073 case NIC_COMMA:
3074 error ("a comma operator "
3075 "cannot appear in a constant-expression");
3076 return true;
3077 case NIC_CONSTRUCTOR:
3078 error ("a call to a constructor "
3079 "cannot appear in a constant-expression");
3080 return true;
3081 case NIC_TRANSACTION:
3082 error ("a transaction expression "
3083 "cannot appear in a constant-expression");
3084 return true;
3085 case NIC_THIS:
3086 msg = "this";
3087 break;
3088 case NIC_FUNC_NAME:
3089 msg = "__FUNCTION__";
3090 break;
3091 case NIC_PRETTY_FUNC:
3092 msg = "__PRETTY_FUNCTION__";
3093 break;
3094 case NIC_C99_FUNC:
3095 msg = "__func__";
3096 break;
3097 case NIC_VA_ARG:
3098 msg = "va_arg";
3099 break;
3100 case NIC_ARROW:
3101 msg = "->";
3102 break;
3103 case NIC_POINT:
3104 msg = ".";
3105 break;
3106 case NIC_STAR:
3107 msg = "*";
3108 break;
3109 case NIC_ADDR:
3110 msg = "&";
3111 break;
3112 case NIC_PREINCREMENT:
3113 msg = "++";
3114 break;
3115 case NIC_PREDECREMENT:
3116 msg = "--";
3117 break;
3118 case NIC_NEW:
3119 msg = "new";
3120 break;
3121 case NIC_DEL:
3122 msg = "delete";
3123 break;
3124 default:
3125 gcc_unreachable ();
3126 }
3127 if (msg)
3128 error ("%qs cannot appear in a constant-expression", msg);
3129 return true;
3130 }
3131 }
3132 return false;
3133 }
3134
3135 /* Emit a diagnostic for an invalid type name. This function commits
3136 to the current active tentative parse, if any. (Otherwise, the
3137 problematic construct might be encountered again later, resulting
3138 in duplicate error messages.) LOCATION is the location of ID. */
3139
3140 static void
3141 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3142 location_t location)
3143 {
3144 tree decl, ambiguous_decls;
3145 cp_parser_commit_to_tentative_parse (parser);
3146 /* Try to lookup the identifier. */
3147 decl = cp_parser_lookup_name (parser, id, none_type,
3148 /*is_template=*/false,
3149 /*is_namespace=*/false,
3150 /*check_dependency=*/true,
3151 &ambiguous_decls, location);
3152 if (ambiguous_decls)
3153 /* If the lookup was ambiguous, an error will already have
3154 been issued. */
3155 return;
3156 /* If the lookup found a template-name, it means that the user forgot
3157 to specify an argument list. Emit a useful error message. */
3158 if (DECL_TYPE_TEMPLATE_P (decl))
3159 {
3160 error_at (location,
3161 "invalid use of template-name %qE without an argument list",
3162 decl);
3163 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3164 inform (location, "class template argument deduction is only available "
3165 "with -std=c++1z or -std=gnu++1z");
3166 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3167 }
3168 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3169 error_at (location, "invalid use of destructor %qD as a type", id);
3170 else if (TREE_CODE (decl) == TYPE_DECL)
3171 /* Something like 'unsigned A a;' */
3172 error_at (location, "invalid combination of multiple type-specifiers");
3173 else if (!parser->scope)
3174 {
3175 /* Issue an error message. */
3176 const char *suggestion = NULL;
3177 if (TREE_CODE (id) == IDENTIFIER_NODE)
3178 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3179 if (suggestion)
3180 {
3181 gcc_rich_location richloc (location);
3182 richloc.add_fixit_replace (suggestion);
3183 error_at_rich_loc (&richloc,
3184 "%qE does not name a type; did you mean %qs?",
3185 id, suggestion);
3186 }
3187 else
3188 error_at (location, "%qE does not name a type", id);
3189 /* If we're in a template class, it's possible that the user was
3190 referring to a type from a base class. For example:
3191
3192 template <typename T> struct A { typedef T X; };
3193 template <typename T> struct B : public A<T> { X x; };
3194
3195 The user should have said "typename A<T>::X". */
3196 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3197 inform (location, "C++11 %<constexpr%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3200 inform (location, "C++11 %<noexcept%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11
3203 && TREE_CODE (id) == IDENTIFIER_NODE
3204 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3205 inform (location, "C++11 %<thread_local%> only available with "
3206 "-std=c++11 or -std=gnu++11");
3207 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3208 inform (location, "%<concept%> only available with -fconcepts");
3209 else if (processing_template_decl && current_class_type
3210 && TYPE_BINFO (current_class_type))
3211 {
3212 tree b;
3213
3214 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3215 b;
3216 b = TREE_CHAIN (b))
3217 {
3218 tree base_type = BINFO_TYPE (b);
3219 if (CLASS_TYPE_P (base_type)
3220 && dependent_type_p (base_type))
3221 {
3222 tree field;
3223 /* Go from a particular instantiation of the
3224 template (which will have an empty TYPE_FIELDs),
3225 to the main version. */
3226 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3227 for (field = TYPE_FIELDS (base_type);
3228 field;
3229 field = DECL_CHAIN (field))
3230 if (TREE_CODE (field) == TYPE_DECL
3231 && DECL_NAME (field) == id)
3232 {
3233 inform (location,
3234 "(perhaps %<typename %T::%E%> was intended)",
3235 BINFO_TYPE (b), id);
3236 break;
3237 }
3238 if (field)
3239 break;
3240 }
3241 }
3242 }
3243 }
3244 /* Here we diagnose qualified-ids where the scope is actually correct,
3245 but the identifier does not resolve to a valid type name. */
3246 else if (parser->scope != error_mark_node)
3247 {
3248 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3249 {
3250 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3251 error_at (location_of (id),
3252 "%qE in namespace %qE does not name a template type",
3253 id, parser->scope);
3254 else
3255 error_at (location_of (id),
3256 "%qE in namespace %qE does not name a type",
3257 id, parser->scope);
3258 if (DECL_P (decl))
3259 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3260 }
3261 else if (CLASS_TYPE_P (parser->scope)
3262 && constructor_name_p (id, parser->scope))
3263 {
3264 /* A<T>::A<T>() */
3265 error_at (location, "%<%T::%E%> names the constructor, not"
3266 " the type", parser->scope, id);
3267 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3268 error_at (location, "and %qT has no template constructors",
3269 parser->scope);
3270 }
3271 else if (TYPE_P (parser->scope)
3272 && dependent_scope_p (parser->scope))
3273 error_at (location, "need %<typename%> before %<%T::%E%> because "
3274 "%qT is a dependent scope",
3275 parser->scope, id, parser->scope);
3276 else if (TYPE_P (parser->scope))
3277 {
3278 if (!COMPLETE_TYPE_P (parser->scope))
3279 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3280 parser->scope);
3281 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3282 error_at (location_of (id),
3283 "%qE in %q#T does not name a template type",
3284 id, parser->scope);
3285 else
3286 error_at (location_of (id),
3287 "%qE in %q#T does not name a type",
3288 id, parser->scope);
3289 if (DECL_P (decl))
3290 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3291 }
3292 else
3293 gcc_unreachable ();
3294 }
3295 }
3296
3297 /* Check for a common situation where a type-name should be present,
3298 but is not, and issue a sensible error message. Returns true if an
3299 invalid type-name was detected.
3300
3301 The situation handled by this function are variable declarations of the
3302 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3303 Usually, `ID' should name a type, but if we got here it means that it
3304 does not. We try to emit the best possible error message depending on
3305 how exactly the id-expression looks like. */
3306
3307 static bool
3308 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3309 {
3310 tree id;
3311 cp_token *token = cp_lexer_peek_token (parser->lexer);
3312
3313 /* Avoid duplicate error about ambiguous lookup. */
3314 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3315 {
3316 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3317 if (next->type == CPP_NAME && next->error_reported)
3318 goto out;
3319 }
3320
3321 cp_parser_parse_tentatively (parser);
3322 id = cp_parser_id_expression (parser,
3323 /*template_keyword_p=*/false,
3324 /*check_dependency_p=*/true,
3325 /*template_p=*/NULL,
3326 /*declarator_p=*/true,
3327 /*optional_p=*/false);
3328 /* If the next token is a (, this is a function with no explicit return
3329 type, i.e. constructor, destructor or conversion op. */
3330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3331 || TREE_CODE (id) == TYPE_DECL)
3332 {
3333 cp_parser_abort_tentative_parse (parser);
3334 return false;
3335 }
3336 if (!cp_parser_parse_definitely (parser))
3337 return false;
3338
3339 /* Emit a diagnostic for the invalid type. */
3340 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3341 out:
3342 /* If we aren't in the middle of a declarator (i.e. in a
3343 parameter-declaration-clause), skip to the end of the declaration;
3344 there's no point in trying to process it. */
3345 if (!parser->in_declarator_p)
3346 cp_parser_skip_to_end_of_block_or_statement (parser);
3347 return true;
3348 }
3349
3350 /* Consume tokens up to, and including, the next non-nested closing `)'.
3351 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3352 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3353 found an unnested token of that type. */
3354
3355 static int
3356 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3357 bool recovering,
3358 cpp_ttype or_ttype,
3359 bool consume_paren)
3360 {
3361 unsigned paren_depth = 0;
3362 unsigned brace_depth = 0;
3363 unsigned square_depth = 0;
3364
3365 if (recovering && or_ttype == CPP_EOF
3366 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3367 return 0;
3368
3369 while (true)
3370 {
3371 cp_token * token = cp_lexer_peek_token (parser->lexer);
3372
3373 /* Have we found what we're looking for before the closing paren? */
3374 if (token->type == or_ttype && or_ttype != CPP_EOF
3375 && !brace_depth && !paren_depth && !square_depth)
3376 return -1;
3377
3378 switch (token->type)
3379 {
3380 case CPP_EOF:
3381 case CPP_PRAGMA_EOL:
3382 /* If we've run out of tokens, then there is no closing `)'. */
3383 return 0;
3384
3385 /* This is good for lambda expression capture-lists. */
3386 case CPP_OPEN_SQUARE:
3387 ++square_depth;
3388 break;
3389 case CPP_CLOSE_SQUARE:
3390 if (!square_depth--)
3391 return 0;
3392 break;
3393
3394 case CPP_SEMICOLON:
3395 /* This matches the processing in skip_to_end_of_statement. */
3396 if (!brace_depth)
3397 return 0;
3398 break;
3399
3400 case CPP_OPEN_BRACE:
3401 ++brace_depth;
3402 break;
3403 case CPP_CLOSE_BRACE:
3404 if (!brace_depth--)
3405 return 0;
3406 break;
3407
3408 case CPP_OPEN_PAREN:
3409 if (!brace_depth)
3410 ++paren_depth;
3411 break;
3412
3413 case CPP_CLOSE_PAREN:
3414 if (!brace_depth && !paren_depth--)
3415 {
3416 if (consume_paren)
3417 cp_lexer_consume_token (parser->lexer);
3418 return 1;
3419 }
3420 break;
3421
3422 default:
3423 break;
3424 }
3425
3426 /* Consume the token. */
3427 cp_lexer_consume_token (parser->lexer);
3428 }
3429 }
3430
3431 /* Consume tokens up to, and including, the next non-nested closing `)'.
3432 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3433 are doing error recovery. Returns -1 if OR_COMMA is true and we
3434 found an unnested token of that type. */
3435
3436 static int
3437 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3438 bool recovering,
3439 bool or_comma,
3440 bool consume_paren)
3441 {
3442 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3443 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3444 ttype, consume_paren);
3445 }
3446
3447 /* Consume tokens until we reach the end of the current statement.
3448 Normally, that will be just before consuming a `;'. However, if a
3449 non-nested `}' comes first, then we stop before consuming that. */
3450
3451 static void
3452 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3453 {
3454 unsigned nesting_depth = 0;
3455
3456 /* Unwind generic function template scope if necessary. */
3457 if (parser->fully_implicit_function_template_p)
3458 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3459
3460 while (true)
3461 {
3462 cp_token *token = cp_lexer_peek_token (parser->lexer);
3463
3464 switch (token->type)
3465 {
3466 case CPP_EOF:
3467 case CPP_PRAGMA_EOL:
3468 /* If we've run out of tokens, stop. */
3469 return;
3470
3471 case CPP_SEMICOLON:
3472 /* If the next token is a `;', we have reached the end of the
3473 statement. */
3474 if (!nesting_depth)
3475 return;
3476 break;
3477
3478 case CPP_CLOSE_BRACE:
3479 /* If this is a non-nested '}', stop before consuming it.
3480 That way, when confronted with something like:
3481
3482 { 3 + }
3483
3484 we stop before consuming the closing '}', even though we
3485 have not yet reached a `;'. */
3486 if (nesting_depth == 0)
3487 return;
3488
3489 /* If it is the closing '}' for a block that we have
3490 scanned, stop -- but only after consuming the token.
3491 That way given:
3492
3493 void f g () { ... }
3494 typedef int I;
3495
3496 we will stop after the body of the erroneously declared
3497 function, but before consuming the following `typedef'
3498 declaration. */
3499 if (--nesting_depth == 0)
3500 {
3501 cp_lexer_consume_token (parser->lexer);
3502 return;
3503 }
3504 break;
3505
3506 case CPP_OPEN_BRACE:
3507 ++nesting_depth;
3508 break;
3509
3510 default:
3511 break;
3512 }
3513
3514 /* Consume the token. */
3515 cp_lexer_consume_token (parser->lexer);
3516 }
3517 }
3518
3519 /* This function is called at the end of a statement or declaration.
3520 If the next token is a semicolon, it is consumed; otherwise, error
3521 recovery is attempted. */
3522
3523 static void
3524 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3525 {
3526 /* Look for the trailing `;'. */
3527 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3528 {
3529 /* If there is additional (erroneous) input, skip to the end of
3530 the statement. */
3531 cp_parser_skip_to_end_of_statement (parser);
3532 /* If the next token is now a `;', consume it. */
3533 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3534 cp_lexer_consume_token (parser->lexer);
3535 }
3536 }
3537
3538 /* Skip tokens until we have consumed an entire block, or until we
3539 have consumed a non-nested `;'. */
3540
3541 static void
3542 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3543 {
3544 int nesting_depth = 0;
3545
3546 /* Unwind generic function template scope if necessary. */
3547 if (parser->fully_implicit_function_template_p)
3548 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3549
3550 while (nesting_depth >= 0)
3551 {
3552 cp_token *token = cp_lexer_peek_token (parser->lexer);
3553
3554 switch (token->type)
3555 {
3556 case CPP_EOF:
3557 case CPP_PRAGMA_EOL:
3558 /* If we've run out of tokens, stop. */
3559 return;
3560
3561 case CPP_SEMICOLON:
3562 /* Stop if this is an unnested ';'. */
3563 if (!nesting_depth)
3564 nesting_depth = -1;
3565 break;
3566
3567 case CPP_CLOSE_BRACE:
3568 /* Stop if this is an unnested '}', or closes the outermost
3569 nesting level. */
3570 nesting_depth--;
3571 if (nesting_depth < 0)
3572 return;
3573 if (!nesting_depth)
3574 nesting_depth = -1;
3575 break;
3576
3577 case CPP_OPEN_BRACE:
3578 /* Nest. */
3579 nesting_depth++;
3580 break;
3581
3582 default:
3583 break;
3584 }
3585
3586 /* Consume the token. */
3587 cp_lexer_consume_token (parser->lexer);
3588 }
3589 }
3590
3591 /* Skip tokens until a non-nested closing curly brace is the next
3592 token, or there are no more tokens. Return true in the first case,
3593 false otherwise. */
3594
3595 static bool
3596 cp_parser_skip_to_closing_brace (cp_parser *parser)
3597 {
3598 unsigned nesting_depth = 0;
3599
3600 while (true)
3601 {
3602 cp_token *token = cp_lexer_peek_token (parser->lexer);
3603
3604 switch (token->type)
3605 {
3606 case CPP_EOF:
3607 case CPP_PRAGMA_EOL:
3608 /* If we've run out of tokens, stop. */
3609 return false;
3610
3611 case CPP_CLOSE_BRACE:
3612 /* If the next token is a non-nested `}', then we have reached
3613 the end of the current block. */
3614 if (nesting_depth-- == 0)
3615 return true;
3616 break;
3617
3618 case CPP_OPEN_BRACE:
3619 /* If it the next token is a `{', then we are entering a new
3620 block. Consume the entire block. */
3621 ++nesting_depth;
3622 break;
3623
3624 default:
3625 break;
3626 }
3627
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser->lexer);
3630 }
3631 }
3632
3633 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3634 parameter is the PRAGMA token, allowing us to purge the entire pragma
3635 sequence. */
3636
3637 static void
3638 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3639 {
3640 cp_token *token;
3641
3642 parser->lexer->in_pragma = false;
3643
3644 do
3645 token = cp_lexer_consume_token (parser->lexer);
3646 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3647
3648 /* Ensure that the pragma is not parsed again. */
3649 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3650 }
3651
3652 /* Require pragma end of line, resyncing with it as necessary. The
3653 arguments are as for cp_parser_skip_to_pragma_eol. */
3654
3655 static void
3656 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3657 {
3658 parser->lexer->in_pragma = false;
3659 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3661 }
3662
3663 /* This is a simple wrapper around make_typename_type. When the id is
3664 an unresolved identifier node, we can provide a superior diagnostic
3665 using cp_parser_diagnose_invalid_type_name. */
3666
3667 static tree
3668 cp_parser_make_typename_type (cp_parser *parser, tree id,
3669 location_t id_location)
3670 {
3671 tree result;
3672 if (identifier_p (id))
3673 {
3674 result = make_typename_type (parser->scope, id, typename_type,
3675 /*complain=*/tf_none);
3676 if (result == error_mark_node)
3677 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3678 return result;
3679 }
3680 return make_typename_type (parser->scope, id, typename_type, tf_error);
3681 }
3682
3683 /* This is a wrapper around the
3684 make_{pointer,ptrmem,reference}_declarator functions that decides
3685 which one to call based on the CODE and CLASS_TYPE arguments. The
3686 CODE argument should be one of the values returned by
3687 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3688 appertain to the pointer or reference. */
3689
3690 static cp_declarator *
3691 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3692 cp_cv_quals cv_qualifiers,
3693 cp_declarator *target,
3694 tree attributes)
3695 {
3696 if (code == ERROR_MARK)
3697 return cp_error_declarator;
3698
3699 if (code == INDIRECT_REF)
3700 if (class_type == NULL_TREE)
3701 return make_pointer_declarator (cv_qualifiers, target, attributes);
3702 else
3703 return make_ptrmem_declarator (cv_qualifiers, class_type,
3704 target, attributes);
3705 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3706 return make_reference_declarator (cv_qualifiers, target,
3707 false, attributes);
3708 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3709 return make_reference_declarator (cv_qualifiers, target,
3710 true, attributes);
3711 gcc_unreachable ();
3712 }
3713
3714 /* Create a new C++ parser. */
3715
3716 static cp_parser *
3717 cp_parser_new (void)
3718 {
3719 cp_parser *parser;
3720 cp_lexer *lexer;
3721 unsigned i;
3722
3723 /* cp_lexer_new_main is called before doing GC allocation because
3724 cp_lexer_new_main might load a PCH file. */
3725 lexer = cp_lexer_new_main ();
3726
3727 /* Initialize the binops_by_token so that we can get the tree
3728 directly from the token. */
3729 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3730 binops_by_token[binops[i].token_type] = binops[i];
3731
3732 parser = ggc_cleared_alloc<cp_parser> ();
3733 parser->lexer = lexer;
3734 parser->context = cp_parser_context_new (NULL);
3735
3736 /* For now, we always accept GNU extensions. */
3737 parser->allow_gnu_extensions_p = 1;
3738
3739 /* The `>' token is a greater-than operator, not the end of a
3740 template-id. */
3741 parser->greater_than_is_operator_p = true;
3742
3743 parser->default_arg_ok_p = true;
3744
3745 /* We are not parsing a constant-expression. */
3746 parser->integral_constant_expression_p = false;
3747 parser->allow_non_integral_constant_expression_p = false;
3748 parser->non_integral_constant_expression_p = false;
3749
3750 /* Local variable names are not forbidden. */
3751 parser->local_variables_forbidden_p = false;
3752
3753 /* We are not processing an `extern "C"' declaration. */
3754 parser->in_unbraced_linkage_specification_p = false;
3755
3756 /* We are not processing a declarator. */
3757 parser->in_declarator_p = false;
3758
3759 /* We are not processing a template-argument-list. */
3760 parser->in_template_argument_list_p = false;
3761
3762 /* We are not in an iteration statement. */
3763 parser->in_statement = 0;
3764
3765 /* We are not in a switch statement. */
3766 parser->in_switch_statement_p = false;
3767
3768 /* We are not parsing a type-id inside an expression. */
3769 parser->in_type_id_in_expr_p = false;
3770
3771 /* Declarations aren't implicitly extern "C". */
3772 parser->implicit_extern_c = false;
3773
3774 /* String literals should be translated to the execution character set. */
3775 parser->translate_strings_p = true;
3776
3777 /* We are not parsing a function body. */
3778 parser->in_function_body = false;
3779
3780 /* We can correct until told otherwise. */
3781 parser->colon_corrects_to_scope_p = true;
3782
3783 /* The unparsed function queue is empty. */
3784 push_unparsed_function_queues (parser);
3785
3786 /* There are no classes being defined. */
3787 parser->num_classes_being_defined = 0;
3788
3789 /* No template parameters apply. */
3790 parser->num_template_parameter_lists = 0;
3791
3792 /* Special parsing data structures. */
3793 parser->omp_declare_simd = NULL;
3794 parser->cilk_simd_fn_info = NULL;
3795 parser->oacc_routine = NULL;
3796
3797 /* Not declaring an implicit function template. */
3798 parser->auto_is_implicit_function_template_parm_p = false;
3799 parser->fully_implicit_function_template_p = false;
3800 parser->implicit_template_parms = 0;
3801 parser->implicit_template_scope = 0;
3802
3803 /* Allow constrained-type-specifiers. */
3804 parser->prevent_constrained_type_specifiers = 0;
3805
3806 return parser;
3807 }
3808
3809 /* Create a cp_lexer structure which will emit the tokens in CACHE
3810 and push it onto the parser's lexer stack. This is used for delayed
3811 parsing of in-class method bodies and default arguments, and should
3812 not be confused with tentative parsing. */
3813 static void
3814 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3815 {
3816 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3817 lexer->next = parser->lexer;
3818 parser->lexer = lexer;
3819
3820 /* Move the current source position to that of the first token in the
3821 new lexer. */
3822 cp_lexer_set_source_position_from_token (lexer->next_token);
3823 }
3824
3825 /* Pop the top lexer off the parser stack. This is never used for the
3826 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3827 static void
3828 cp_parser_pop_lexer (cp_parser *parser)
3829 {
3830 cp_lexer *lexer = parser->lexer;
3831 parser->lexer = lexer->next;
3832 cp_lexer_destroy (lexer);
3833
3834 /* Put the current source position back where it was before this
3835 lexer was pushed. */
3836 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3837 }
3838
3839 /* Lexical conventions [gram.lex] */
3840
3841 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3842 identifier. */
3843
3844 static cp_expr
3845 cp_parser_identifier (cp_parser* parser)
3846 {
3847 cp_token *token;
3848
3849 /* Look for the identifier. */
3850 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3851 /* Return the value. */
3852 if (token)
3853 return cp_expr (token->u.value, token->location);
3854 else
3855 return error_mark_node;
3856 }
3857
3858 /* Parse a sequence of adjacent string constants. Returns a
3859 TREE_STRING representing the combined, nul-terminated string
3860 constant. If TRANSLATE is true, translate the string to the
3861 execution character set. If WIDE_OK is true, a wide string is
3862 invalid here.
3863
3864 C++98 [lex.string] says that if a narrow string literal token is
3865 adjacent to a wide string literal token, the behavior is undefined.
3866 However, C99 6.4.5p4 says that this results in a wide string literal.
3867 We follow C99 here, for consistency with the C front end.
3868
3869 This code is largely lifted from lex_string() in c-lex.c.
3870
3871 FUTURE: ObjC++ will need to handle @-strings here. */
3872 static cp_expr
3873 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3874 bool lookup_udlit = true)
3875 {
3876 tree value;
3877 size_t count;
3878 struct obstack str_ob;
3879 cpp_string str, istr, *strs;
3880 cp_token *tok;
3881 enum cpp_ttype type, curr_type;
3882 int have_suffix_p = 0;
3883 tree string_tree;
3884 tree suffix_id = NULL_TREE;
3885 bool curr_tok_is_userdef_p = false;
3886
3887 tok = cp_lexer_peek_token (parser->lexer);
3888 if (!cp_parser_is_string_literal (tok))
3889 {
3890 cp_parser_error (parser, "expected string-literal");
3891 return error_mark_node;
3892 }
3893
3894 location_t loc = tok->location;
3895
3896 if (cpp_userdef_string_p (tok->type))
3897 {
3898 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3899 curr_type = cpp_userdef_string_remove_type (tok->type);
3900 curr_tok_is_userdef_p = true;
3901 }
3902 else
3903 {
3904 string_tree = tok->u.value;
3905 curr_type = tok->type;
3906 }
3907 type = curr_type;
3908
3909 /* Try to avoid the overhead of creating and destroying an obstack
3910 for the common case of just one string. */
3911 if (!cp_parser_is_string_literal
3912 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3913 {
3914 cp_lexer_consume_token (parser->lexer);
3915
3916 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3917 str.len = TREE_STRING_LENGTH (string_tree);
3918 count = 1;
3919
3920 if (curr_tok_is_userdef_p)
3921 {
3922 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3923 have_suffix_p = 1;
3924 curr_type = cpp_userdef_string_remove_type (tok->type);
3925 }
3926 else
3927 curr_type = tok->type;
3928
3929 strs = &str;
3930 }
3931 else
3932 {
3933 location_t last_tok_loc = tok->location;
3934 gcc_obstack_init (&str_ob);
3935 count = 0;
3936
3937 do
3938 {
3939 cp_lexer_consume_token (parser->lexer);
3940 count++;
3941 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3942 str.len = TREE_STRING_LENGTH (string_tree);
3943
3944 if (curr_tok_is_userdef_p)
3945 {
3946 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3947 if (have_suffix_p == 0)
3948 {
3949 suffix_id = curr_suffix_id;
3950 have_suffix_p = 1;
3951 }
3952 else if (have_suffix_p == 1
3953 && curr_suffix_id != suffix_id)
3954 {
3955 error ("inconsistent user-defined literal suffixes"
3956 " %qD and %qD in string literal",
3957 suffix_id, curr_suffix_id);
3958 have_suffix_p = -1;
3959 }
3960 curr_type = cpp_userdef_string_remove_type (tok->type);
3961 }
3962 else
3963 curr_type = tok->type;
3964
3965 if (type != curr_type)
3966 {
3967 if (type == CPP_STRING)
3968 type = curr_type;
3969 else if (curr_type != CPP_STRING)
3970 {
3971 rich_location rich_loc (line_table, tok->location);
3972 rich_loc.add_range (last_tok_loc, false);
3973 error_at_rich_loc (&rich_loc,
3974 "unsupported non-standard concatenation "
3975 "of string literals");
3976 }
3977 }
3978
3979 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3980
3981 last_tok_loc = tok->location;
3982
3983 tok = cp_lexer_peek_token (parser->lexer);
3984 if (cpp_userdef_string_p (tok->type))
3985 {
3986 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3987 curr_type = cpp_userdef_string_remove_type (tok->type);
3988 curr_tok_is_userdef_p = true;
3989 }
3990 else
3991 {
3992 string_tree = tok->u.value;
3993 curr_type = tok->type;
3994 curr_tok_is_userdef_p = false;
3995 }
3996 }
3997 while (cp_parser_is_string_literal (tok));
3998
3999 /* A string literal built by concatenation has its caret=start at
4000 the start of the initial string, and its finish at the finish of
4001 the final string literal. */
4002 loc = make_location (loc, loc, get_finish (last_tok_loc));
4003
4004 strs = (cpp_string *) obstack_finish (&str_ob);
4005 }
4006
4007 if (type != CPP_STRING && !wide_ok)
4008 {
4009 cp_parser_error (parser, "a wide string is invalid in this context");
4010 type = CPP_STRING;
4011 }
4012
4013 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4014 (parse_in, strs, count, &istr, type))
4015 {
4016 value = build_string (istr.len, (const char *)istr.text);
4017 free (CONST_CAST (unsigned char *, istr.text));
4018
4019 switch (type)
4020 {
4021 default:
4022 case CPP_STRING:
4023 case CPP_UTF8STRING:
4024 TREE_TYPE (value) = char_array_type_node;
4025 break;
4026 case CPP_STRING16:
4027 TREE_TYPE (value) = char16_array_type_node;
4028 break;
4029 case CPP_STRING32:
4030 TREE_TYPE (value) = char32_array_type_node;
4031 break;
4032 case CPP_WSTRING:
4033 TREE_TYPE (value) = wchar_array_type_node;
4034 break;
4035 }
4036
4037 value = fix_string_type (value);
4038
4039 if (have_suffix_p)
4040 {
4041 tree literal = build_userdef_literal (suffix_id, value,
4042 OT_NONE, NULL_TREE);
4043 if (lookup_udlit)
4044 value = cp_parser_userdef_string_literal (literal);
4045 else
4046 value = literal;
4047 }
4048 }
4049 else
4050 /* cpp_interpret_string has issued an error. */
4051 value = error_mark_node;
4052
4053 if (count > 1)
4054 obstack_free (&str_ob, 0);
4055
4056 return cp_expr (value, loc);
4057 }
4058
4059 /* Look up a literal operator with the name and the exact arguments. */
4060
4061 static tree
4062 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4063 {
4064 tree decl, fns;
4065 decl = lookup_name (name);
4066 if (!decl || !is_overloaded_fn (decl))
4067 return error_mark_node;
4068
4069 for (fns = decl; fns; fns = OVL_NEXT (fns))
4070 {
4071 unsigned int ix;
4072 bool found = true;
4073 tree fn = OVL_CURRENT (fns);
4074 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4075 if (parmtypes != NULL_TREE)
4076 {
4077 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4078 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4079 {
4080 tree tparm = TREE_VALUE (parmtypes);
4081 tree targ = TREE_TYPE ((*args)[ix]);
4082 bool ptr = TYPE_PTR_P (tparm);
4083 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4084 if ((ptr || arr || !same_type_p (tparm, targ))
4085 && (!ptr || !arr
4086 || !same_type_p (TREE_TYPE (tparm),
4087 TREE_TYPE (targ))))
4088 found = false;
4089 }
4090 if (found
4091 && ix == vec_safe_length (args)
4092 /* May be this should be sufficient_parms_p instead,
4093 depending on how exactly should user-defined literals
4094 work in presence of default arguments on the literal
4095 operator parameters. */
4096 && parmtypes == void_list_node)
4097 return decl;
4098 }
4099 }
4100
4101 return error_mark_node;
4102 }
4103
4104 /* Parse a user-defined char constant. Returns a call to a user-defined
4105 literal operator taking the character as an argument. */
4106
4107 static cp_expr
4108 cp_parser_userdef_char_literal (cp_parser *parser)
4109 {
4110 cp_token *token = cp_lexer_consume_token (parser->lexer);
4111 tree literal = token->u.value;
4112 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4113 tree value = USERDEF_LITERAL_VALUE (literal);
4114 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4115 tree decl, result;
4116
4117 /* Build up a call to the user-defined operator */
4118 /* Lookup the name we got back from the id-expression. */
4119 vec<tree, va_gc> *args = make_tree_vector ();
4120 vec_safe_push (args, value);
4121 decl = lookup_literal_operator (name, args);
4122 if (!decl || decl == error_mark_node)
4123 {
4124 error ("unable to find character literal operator %qD with %qT argument",
4125 name, TREE_TYPE (value));
4126 release_tree_vector (args);
4127 return error_mark_node;
4128 }
4129 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4130 release_tree_vector (args);
4131 return result;
4132 }
4133
4134 /* A subroutine of cp_parser_userdef_numeric_literal to
4135 create a char... template parameter pack from a string node. */
4136
4137 static tree
4138 make_char_string_pack (tree value)
4139 {
4140 tree charvec;
4141 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4142 const char *str = TREE_STRING_POINTER (value);
4143 int i, len = TREE_STRING_LENGTH (value) - 1;
4144 tree argvec = make_tree_vec (1);
4145
4146 /* Fill in CHARVEC with all of the parameters. */
4147 charvec = make_tree_vec (len);
4148 for (i = 0; i < len; ++i)
4149 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4150
4151 /* Build the argument packs. */
4152 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4153 TREE_TYPE (argpack) = char_type_node;
4154
4155 TREE_VEC_ELT (argvec, 0) = argpack;
4156
4157 return argvec;
4158 }
4159
4160 /* A subroutine of cp_parser_userdef_numeric_literal to
4161 create a char... template parameter pack from a string node. */
4162
4163 static tree
4164 make_string_pack (tree value)
4165 {
4166 tree charvec;
4167 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4168 const unsigned char *str
4169 = (const unsigned char *) TREE_STRING_POINTER (value);
4170 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4171 int len = TREE_STRING_LENGTH (value) / sz - 1;
4172 tree argvec = make_tree_vec (2);
4173
4174 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4175 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4176
4177 /* First template parm is character type. */
4178 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4179
4180 /* Fill in CHARVEC with all of the parameters. */
4181 charvec = make_tree_vec (len);
4182 for (int i = 0; i < len; ++i)
4183 TREE_VEC_ELT (charvec, i)
4184 = double_int_to_tree (str_char_type_node,
4185 double_int::from_buffer (str + i * sz, sz));
4186
4187 /* Build the argument packs. */
4188 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4189 TREE_TYPE (argpack) = str_char_type_node;
4190
4191 TREE_VEC_ELT (argvec, 1) = argpack;
4192
4193 return argvec;
4194 }
4195
4196 /* Parse a user-defined numeric constant. returns a call to a user-defined
4197 literal operator. */
4198
4199 static cp_expr
4200 cp_parser_userdef_numeric_literal (cp_parser *parser)
4201 {
4202 cp_token *token = cp_lexer_consume_token (parser->lexer);
4203 tree literal = token->u.value;
4204 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4205 tree value = USERDEF_LITERAL_VALUE (literal);
4206 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4207 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4208 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4209 tree decl, result;
4210 vec<tree, va_gc> *args;
4211
4212 /* Look for a literal operator taking the exact type of numeric argument
4213 as the literal value. */
4214 args = make_tree_vector ();
4215 vec_safe_push (args, value);
4216 decl = lookup_literal_operator (name, args);
4217 if (decl && decl != error_mark_node)
4218 {
4219 result = finish_call_expr (decl, &args, false, true,
4220 tf_warning_or_error);
4221
4222 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4223 {
4224 warning_at (token->location, OPT_Woverflow,
4225 "integer literal exceeds range of %qT type",
4226 long_long_unsigned_type_node);
4227 }
4228 else
4229 {
4230 if (overflow > 0)
4231 warning_at (token->location, OPT_Woverflow,
4232 "floating literal exceeds range of %qT type",
4233 long_double_type_node);
4234 else if (overflow < 0)
4235 warning_at (token->location, OPT_Woverflow,
4236 "floating literal truncated to zero");
4237 }
4238
4239 release_tree_vector (args);
4240 return result;
4241 }
4242 release_tree_vector (args);
4243
4244 /* If the numeric argument didn't work, look for a raw literal
4245 operator taking a const char* argument consisting of the number
4246 in string format. */
4247 args = make_tree_vector ();
4248 vec_safe_push (args, num_string);
4249 decl = lookup_literal_operator (name, args);
4250 if (decl && decl != error_mark_node)
4251 {
4252 result = finish_call_expr (decl, &args, false, true,
4253 tf_warning_or_error);
4254 release_tree_vector (args);
4255 return result;
4256 }
4257 release_tree_vector (args);
4258
4259 /* If the raw literal didn't work, look for a non-type template
4260 function with parameter pack char.... Call the function with
4261 template parameter characters representing the number. */
4262 args = make_tree_vector ();
4263 decl = lookup_literal_operator (name, args);
4264 if (decl && decl != error_mark_node)
4265 {
4266 tree tmpl_args = make_char_string_pack (num_string);
4267 decl = lookup_template_function (decl, tmpl_args);
4268 result = finish_call_expr (decl, &args, false, true,
4269 tf_warning_or_error);
4270 release_tree_vector (args);
4271 return result;
4272 }
4273
4274 release_tree_vector (args);
4275
4276 error ("unable to find numeric literal operator %qD", name);
4277 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4278 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4279 "to enable more built-in suffixes");
4280 return error_mark_node;
4281 }
4282
4283 /* Parse a user-defined string constant. Returns a call to a user-defined
4284 literal operator taking a character pointer and the length of the string
4285 as arguments. */
4286
4287 static tree
4288 cp_parser_userdef_string_literal (tree literal)
4289 {
4290 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4291 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4292 tree value = USERDEF_LITERAL_VALUE (literal);
4293 int len = TREE_STRING_LENGTH (value)
4294 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4295 tree decl, result;
4296 vec<tree, va_gc> *args;
4297
4298 /* Build up a call to the user-defined operator. */
4299 /* Lookup the name we got back from the id-expression. */
4300 args = make_tree_vector ();
4301 vec_safe_push (args, value);
4302 vec_safe_push (args, build_int_cst (size_type_node, len));
4303 decl = lookup_literal_operator (name, args);
4304
4305 if (decl && decl != error_mark_node)
4306 {
4307 result = finish_call_expr (decl, &args, false, true,
4308 tf_warning_or_error);
4309 release_tree_vector (args);
4310 return result;
4311 }
4312 release_tree_vector (args);
4313
4314 /* Look for a template function with typename parameter CharT
4315 and parameter pack CharT... Call the function with
4316 template parameter characters representing the string. */
4317 args = make_tree_vector ();
4318 decl = lookup_literal_operator (name, args);
4319 if (decl && decl != error_mark_node)
4320 {
4321 tree tmpl_args = make_string_pack (value);
4322 decl = lookup_template_function (decl, tmpl_args);
4323 result = finish_call_expr (decl, &args, false, true,
4324 tf_warning_or_error);
4325 release_tree_vector (args);
4326 return result;
4327 }
4328 release_tree_vector (args);
4329
4330 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4331 name, TREE_TYPE (value), size_type_node);
4332 return error_mark_node;
4333 }
4334
4335
4336 /* Basic concepts [gram.basic] */
4337
4338 /* Parse a translation-unit.
4339
4340 translation-unit:
4341 declaration-seq [opt]
4342
4343 Returns TRUE if all went well. */
4344
4345 static bool
4346 cp_parser_translation_unit (cp_parser* parser)
4347 {
4348 /* The address of the first non-permanent object on the declarator
4349 obstack. */
4350 static void *declarator_obstack_base;
4351
4352 bool success;
4353
4354 /* Create the declarator obstack, if necessary. */
4355 if (!cp_error_declarator)
4356 {
4357 gcc_obstack_init (&declarator_obstack);
4358 /* Create the error declarator. */
4359 cp_error_declarator = make_declarator (cdk_error);
4360 /* Create the empty parameter list. */
4361 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4362 /* Remember where the base of the declarator obstack lies. */
4363 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4364 }
4365
4366 cp_parser_declaration_seq_opt (parser);
4367
4368 /* If there are no tokens left then all went well. */
4369 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4370 {
4371 /* Get rid of the token array; we don't need it any more. */
4372 cp_lexer_destroy (parser->lexer);
4373 parser->lexer = NULL;
4374
4375 /* This file might have been a context that's implicitly extern
4376 "C". If so, pop the lang context. (Only relevant for PCH.) */
4377 if (parser->implicit_extern_c)
4378 {
4379 pop_lang_context ();
4380 parser->implicit_extern_c = false;
4381 }
4382
4383 /* Finish up. */
4384 finish_translation_unit ();
4385
4386 success = true;
4387 }
4388 else
4389 {
4390 cp_parser_error (parser, "expected declaration");
4391 success = false;
4392 }
4393
4394 /* Make sure the declarator obstack was fully cleaned up. */
4395 gcc_assert (obstack_next_free (&declarator_obstack)
4396 == declarator_obstack_base);
4397
4398 /* All went well. */
4399 return success;
4400 }
4401
4402 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4403 decltype context. */
4404
4405 static inline tsubst_flags_t
4406 complain_flags (bool decltype_p)
4407 {
4408 tsubst_flags_t complain = tf_warning_or_error;
4409 if (decltype_p)
4410 complain |= tf_decltype;
4411 return complain;
4412 }
4413
4414 /* We're about to parse a collection of statements. If we're currently
4415 parsing tentatively, set up a firewall so that any nested
4416 cp_parser_commit_to_tentative_parse won't affect the current context. */
4417
4418 static cp_token_position
4419 cp_parser_start_tentative_firewall (cp_parser *parser)
4420 {
4421 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4422 return 0;
4423
4424 cp_parser_parse_tentatively (parser);
4425 cp_parser_commit_to_topmost_tentative_parse (parser);
4426 return cp_lexer_token_position (parser->lexer, false);
4427 }
4428
4429 /* We've finished parsing the collection of statements. Wrap up the
4430 firewall and replace the relevant tokens with the parsed form. */
4431
4432 static void
4433 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4434 tree expr)
4435 {
4436 if (!start)
4437 return;
4438
4439 /* Finish the firewall level. */
4440 cp_parser_parse_definitely (parser);
4441 /* And remember the result of the parse for when we try again. */
4442 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4443 token->type = CPP_PREPARSED_EXPR;
4444 token->u.value = expr;
4445 token->keyword = RID_MAX;
4446 cp_lexer_purge_tokens_after (parser->lexer, start);
4447 }
4448
4449 /* Like the above functions, but let the user modify the tokens. Used by
4450 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4451 later parses, so it makes sense to localize the effects of
4452 cp_parser_commit_to_tentative_parse. */
4453
4454 struct tentative_firewall
4455 {
4456 cp_parser *parser;
4457 bool set;
4458
4459 tentative_firewall (cp_parser *p): parser(p)
4460 {
4461 /* If we're currently parsing tentatively, start a committed level as a
4462 firewall and then an inner tentative parse. */
4463 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4464 {
4465 cp_parser_parse_tentatively (parser);
4466 cp_parser_commit_to_topmost_tentative_parse (parser);
4467 cp_parser_parse_tentatively (parser);
4468 }
4469 }
4470
4471 ~tentative_firewall()
4472 {
4473 if (set)
4474 {
4475 /* Finish the inner tentative parse and the firewall, propagating any
4476 uncommitted error state to the outer tentative parse. */
4477 bool err = cp_parser_error_occurred (parser);
4478 cp_parser_parse_definitely (parser);
4479 cp_parser_parse_definitely (parser);
4480 if (err)
4481 cp_parser_simulate_error (parser);
4482 }
4483 }
4484 };
4485
4486 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4487 enclosing parentheses. */
4488
4489 static cp_expr
4490 cp_parser_statement_expr (cp_parser *parser)
4491 {
4492 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4493
4494 /* Consume the '('. */
4495 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4496 cp_lexer_consume_token (parser->lexer);
4497 /* Start the statement-expression. */
4498 tree expr = begin_stmt_expr ();
4499 /* Parse the compound-statement. */
4500 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4501 /* Finish up. */
4502 expr = finish_stmt_expr (expr, false);
4503 /* Consume the ')'. */
4504 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4505 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4506 cp_parser_skip_to_end_of_statement (parser);
4507
4508 cp_parser_end_tentative_firewall (parser, start, expr);
4509 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4510 return cp_expr (expr, combined_loc);
4511 }
4512
4513 /* Expressions [gram.expr] */
4514
4515 /* Parse a fold-operator.
4516
4517 fold-operator:
4518 - * / % ^ & | = < > << >>
4519 = -= *= /= %= ^= &= |= <<= >>=
4520 == != <= >= && || , .* ->*
4521
4522 This returns the tree code corresponding to the matched operator
4523 as an int. When the current token matches a compound assignment
4524 opertor, the resulting tree code is the negative value of the
4525 non-assignment operator. */
4526
4527 static int
4528 cp_parser_fold_operator (cp_token *token)
4529 {
4530 switch (token->type)
4531 {
4532 case CPP_PLUS: return PLUS_EXPR;
4533 case CPP_MINUS: return MINUS_EXPR;
4534 case CPP_MULT: return MULT_EXPR;
4535 case CPP_DIV: return TRUNC_DIV_EXPR;
4536 case CPP_MOD: return TRUNC_MOD_EXPR;
4537 case CPP_XOR: return BIT_XOR_EXPR;
4538 case CPP_AND: return BIT_AND_EXPR;
4539 case CPP_OR: return BIT_IOR_EXPR;
4540 case CPP_LSHIFT: return LSHIFT_EXPR;
4541 case CPP_RSHIFT: return RSHIFT_EXPR;
4542
4543 case CPP_EQ: return -NOP_EXPR;
4544 case CPP_PLUS_EQ: return -PLUS_EXPR;
4545 case CPP_MINUS_EQ: return -MINUS_EXPR;
4546 case CPP_MULT_EQ: return -MULT_EXPR;
4547 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4548 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4549 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4550 case CPP_AND_EQ: return -BIT_AND_EXPR;
4551 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4552 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4553 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4554
4555 case CPP_EQ_EQ: return EQ_EXPR;
4556 case CPP_NOT_EQ: return NE_EXPR;
4557 case CPP_LESS: return LT_EXPR;
4558 case CPP_GREATER: return GT_EXPR;
4559 case CPP_LESS_EQ: return LE_EXPR;
4560 case CPP_GREATER_EQ: return GE_EXPR;
4561
4562 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4563 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4564
4565 case CPP_COMMA: return COMPOUND_EXPR;
4566
4567 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4568 case CPP_DEREF_STAR: return MEMBER_REF;
4569
4570 default: return ERROR_MARK;
4571 }
4572 }
4573
4574 /* Returns true if CODE indicates a binary expression, which is not allowed in
4575 the LHS of a fold-expression. More codes will need to be added to use this
4576 function in other contexts. */
4577
4578 static bool
4579 is_binary_op (tree_code code)
4580 {
4581 switch (code)
4582 {
4583 case PLUS_EXPR:
4584 case POINTER_PLUS_EXPR:
4585 case MINUS_EXPR:
4586 case MULT_EXPR:
4587 case TRUNC_DIV_EXPR:
4588 case TRUNC_MOD_EXPR:
4589 case BIT_XOR_EXPR:
4590 case BIT_AND_EXPR:
4591 case BIT_IOR_EXPR:
4592 case LSHIFT_EXPR:
4593 case RSHIFT_EXPR:
4594
4595 case MODOP_EXPR:
4596
4597 case EQ_EXPR:
4598 case NE_EXPR:
4599 case LE_EXPR:
4600 case GE_EXPR:
4601 case LT_EXPR:
4602 case GT_EXPR:
4603
4604 case TRUTH_ANDIF_EXPR:
4605 case TRUTH_ORIF_EXPR:
4606
4607 case COMPOUND_EXPR:
4608
4609 case DOTSTAR_EXPR:
4610 case MEMBER_REF:
4611 return true;
4612
4613 default:
4614 return false;
4615 }
4616 }
4617
4618 /* If the next token is a suitable fold operator, consume it and return as
4619 the function above. */
4620
4621 static int
4622 cp_parser_fold_operator (cp_parser *parser)
4623 {
4624 cp_token* token = cp_lexer_peek_token (parser->lexer);
4625 int code = cp_parser_fold_operator (token);
4626 if (code != ERROR_MARK)
4627 cp_lexer_consume_token (parser->lexer);
4628 return code;
4629 }
4630
4631 /* Parse a fold-expression.
4632
4633 fold-expression:
4634 ( ... folding-operator cast-expression)
4635 ( cast-expression folding-operator ... )
4636 ( cast-expression folding operator ... folding-operator cast-expression)
4637
4638 Note that the '(' and ')' are matched in primary expression. */
4639
4640 static cp_expr
4641 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4642 {
4643 cp_id_kind pidk;
4644
4645 // Left fold.
4646 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4647 {
4648 cp_lexer_consume_token (parser->lexer);
4649 int op = cp_parser_fold_operator (parser);
4650 if (op == ERROR_MARK)
4651 {
4652 cp_parser_error (parser, "expected binary operator");
4653 return error_mark_node;
4654 }
4655
4656 tree expr = cp_parser_cast_expression (parser, false, false,
4657 false, &pidk);
4658 if (expr == error_mark_node)
4659 return error_mark_node;
4660 return finish_left_unary_fold_expr (expr, op);
4661 }
4662
4663 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4664 int op = cp_parser_fold_operator (parser);
4665 if (op == ERROR_MARK)
4666 {
4667 cp_parser_error (parser, "expected binary operator");
4668 return error_mark_node;
4669 }
4670
4671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4672 {
4673 cp_parser_error (parser, "expected ...");
4674 return error_mark_node;
4675 }
4676 cp_lexer_consume_token (parser->lexer);
4677
4678 /* The operands of a fold-expression are cast-expressions, so binary or
4679 conditional expressions are not allowed. We check this here to avoid
4680 tentative parsing. */
4681 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4682 /* OK, the expression was parenthesized. */;
4683 else if (is_binary_op (TREE_CODE (expr1)))
4684 error_at (location_of (expr1),
4685 "binary expression in operand of fold-expression");
4686 else if (TREE_CODE (expr1) == COND_EXPR)
4687 error_at (location_of (expr1),
4688 "conditional expression in operand of fold-expression");
4689
4690 // Right fold.
4691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4692 return finish_right_unary_fold_expr (expr1, op);
4693
4694 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4695 {
4696 cp_parser_error (parser, "mismatched operator in fold-expression");
4697 return error_mark_node;
4698 }
4699 cp_lexer_consume_token (parser->lexer);
4700
4701 // Binary left or right fold.
4702 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4703 if (expr2 == error_mark_node)
4704 return error_mark_node;
4705 return finish_binary_fold_expr (expr1, expr2, op);
4706 }
4707
4708 /* Parse a primary-expression.
4709
4710 primary-expression:
4711 literal
4712 this
4713 ( expression )
4714 id-expression
4715 lambda-expression (C++11)
4716
4717 GNU Extensions:
4718
4719 primary-expression:
4720 ( compound-statement )
4721 __builtin_va_arg ( assignment-expression , type-id )
4722 __builtin_offsetof ( type-id , offsetof-expression )
4723
4724 C++ Extensions:
4725 __has_nothrow_assign ( type-id )
4726 __has_nothrow_constructor ( type-id )
4727 __has_nothrow_copy ( type-id )
4728 __has_trivial_assign ( type-id )
4729 __has_trivial_constructor ( type-id )
4730 __has_trivial_copy ( type-id )
4731 __has_trivial_destructor ( type-id )
4732 __has_virtual_destructor ( type-id )
4733 __is_abstract ( type-id )
4734 __is_base_of ( type-id , type-id )
4735 __is_class ( type-id )
4736 __is_empty ( type-id )
4737 __is_enum ( type-id )
4738 __is_final ( type-id )
4739 __is_literal_type ( type-id )
4740 __is_pod ( type-id )
4741 __is_polymorphic ( type-id )
4742 __is_std_layout ( type-id )
4743 __is_trivial ( type-id )
4744 __is_union ( type-id )
4745
4746 Objective-C++ Extension:
4747
4748 primary-expression:
4749 objc-expression
4750
4751 literal:
4752 __null
4753
4754 ADDRESS_P is true iff this expression was immediately preceded by
4755 "&" and therefore might denote a pointer-to-member. CAST_P is true
4756 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4757 true iff this expression is a template argument.
4758
4759 Returns a representation of the expression. Upon return, *IDK
4760 indicates what kind of id-expression (if any) was present. */
4761
4762 static cp_expr
4763 cp_parser_primary_expression (cp_parser *parser,
4764 bool address_p,
4765 bool cast_p,
4766 bool template_arg_p,
4767 bool decltype_p,
4768 cp_id_kind *idk)
4769 {
4770 cp_token *token = NULL;
4771
4772 /* Assume the primary expression is not an id-expression. */
4773 *idk = CP_ID_KIND_NONE;
4774
4775 /* Peek at the next token. */
4776 token = cp_lexer_peek_token (parser->lexer);
4777 switch ((int) token->type)
4778 {
4779 /* literal:
4780 integer-literal
4781 character-literal
4782 floating-literal
4783 string-literal
4784 boolean-literal
4785 pointer-literal
4786 user-defined-literal */
4787 case CPP_CHAR:
4788 case CPP_CHAR16:
4789 case CPP_CHAR32:
4790 case CPP_WCHAR:
4791 case CPP_UTF8CHAR:
4792 case CPP_NUMBER:
4793 case CPP_PREPARSED_EXPR:
4794 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4795 return cp_parser_userdef_numeric_literal (parser);
4796 token = cp_lexer_consume_token (parser->lexer);
4797 if (TREE_CODE (token->u.value) == FIXED_CST)
4798 {
4799 error_at (token->location,
4800 "fixed-point types not supported in C++");
4801 return error_mark_node;
4802 }
4803 /* Floating-point literals are only allowed in an integral
4804 constant expression if they are cast to an integral or
4805 enumeration type. */
4806 if (TREE_CODE (token->u.value) == REAL_CST
4807 && parser->integral_constant_expression_p
4808 && pedantic)
4809 {
4810 /* CAST_P will be set even in invalid code like "int(2.7 +
4811 ...)". Therefore, we have to check that the next token
4812 is sure to end the cast. */
4813 if (cast_p)
4814 {
4815 cp_token *next_token;
4816
4817 next_token = cp_lexer_peek_token (parser->lexer);
4818 if (/* The comma at the end of an
4819 enumerator-definition. */
4820 next_token->type != CPP_COMMA
4821 /* The curly brace at the end of an enum-specifier. */
4822 && next_token->type != CPP_CLOSE_BRACE
4823 /* The end of a statement. */
4824 && next_token->type != CPP_SEMICOLON
4825 /* The end of the cast-expression. */
4826 && next_token->type != CPP_CLOSE_PAREN
4827 /* The end of an array bound. */
4828 && next_token->type != CPP_CLOSE_SQUARE
4829 /* The closing ">" in a template-argument-list. */
4830 && (next_token->type != CPP_GREATER
4831 || parser->greater_than_is_operator_p)
4832 /* C++0x only: A ">>" treated like two ">" tokens,
4833 in a template-argument-list. */
4834 && (next_token->type != CPP_RSHIFT
4835 || (cxx_dialect == cxx98)
4836 || parser->greater_than_is_operator_p))
4837 cast_p = false;
4838 }
4839
4840 /* If we are within a cast, then the constraint that the
4841 cast is to an integral or enumeration type will be
4842 checked at that point. If we are not within a cast, then
4843 this code is invalid. */
4844 if (!cast_p)
4845 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4846 }
4847 return cp_expr (token->u.value, token->location);
4848
4849 case CPP_CHAR_USERDEF:
4850 case CPP_CHAR16_USERDEF:
4851 case CPP_CHAR32_USERDEF:
4852 case CPP_WCHAR_USERDEF:
4853 case CPP_UTF8CHAR_USERDEF:
4854 return cp_parser_userdef_char_literal (parser);
4855
4856 case CPP_STRING:
4857 case CPP_STRING16:
4858 case CPP_STRING32:
4859 case CPP_WSTRING:
4860 case CPP_UTF8STRING:
4861 case CPP_STRING_USERDEF:
4862 case CPP_STRING16_USERDEF:
4863 case CPP_STRING32_USERDEF:
4864 case CPP_WSTRING_USERDEF:
4865 case CPP_UTF8STRING_USERDEF:
4866 /* ??? Should wide strings be allowed when parser->translate_strings_p
4867 is false (i.e. in attributes)? If not, we can kill the third
4868 argument to cp_parser_string_literal. */
4869 return cp_parser_string_literal (parser,
4870 parser->translate_strings_p,
4871 true);
4872
4873 case CPP_OPEN_PAREN:
4874 /* If we see `( { ' then we are looking at the beginning of
4875 a GNU statement-expression. */
4876 if (cp_parser_allow_gnu_extensions_p (parser)
4877 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4878 {
4879 /* Statement-expressions are not allowed by the standard. */
4880 pedwarn (token->location, OPT_Wpedantic,
4881 "ISO C++ forbids braced-groups within expressions");
4882
4883 /* And they're not allowed outside of a function-body; you
4884 cannot, for example, write:
4885
4886 int i = ({ int j = 3; j + 1; });
4887
4888 at class or namespace scope. */
4889 if (!parser->in_function_body
4890 || parser->in_template_argument_list_p)
4891 {
4892 error_at (token->location,
4893 "statement-expressions are not allowed outside "
4894 "functions nor in template-argument lists");
4895 cp_parser_skip_to_end_of_block_or_statement (parser);
4896 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4897 cp_lexer_consume_token (parser->lexer);
4898 return error_mark_node;
4899 }
4900 else
4901 return cp_parser_statement_expr (parser);
4902 }
4903 /* Otherwise it's a normal parenthesized expression. */
4904 {
4905 cp_expr expr;
4906 bool saved_greater_than_is_operator_p;
4907
4908 location_t open_paren_loc = token->location;
4909
4910 /* Consume the `('. */
4911 cp_lexer_consume_token (parser->lexer);
4912 /* Within a parenthesized expression, a `>' token is always
4913 the greater-than operator. */
4914 saved_greater_than_is_operator_p
4915 = parser->greater_than_is_operator_p;
4916 parser->greater_than_is_operator_p = true;
4917
4918 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4919 /* Left fold expression. */
4920 expr = NULL_TREE;
4921 else
4922 /* Parse the parenthesized expression. */
4923 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4924
4925 token = cp_lexer_peek_token (parser->lexer);
4926 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4927 {
4928 expr = cp_parser_fold_expression (parser, expr);
4929 if (expr != error_mark_node
4930 && cxx_dialect < cxx1z
4931 && !in_system_header_at (input_location))
4932 pedwarn (input_location, 0, "fold-expressions only available "
4933 "with -std=c++1z or -std=gnu++1z");
4934 }
4935 else
4936 /* Let the front end know that this expression was
4937 enclosed in parentheses. This matters in case, for
4938 example, the expression is of the form `A::B', since
4939 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4940 not. */
4941 expr = finish_parenthesized_expr (expr);
4942
4943 /* DR 705: Wrapping an unqualified name in parentheses
4944 suppresses arg-dependent lookup. We want to pass back
4945 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4946 (c++/37862), but none of the others. */
4947 if (*idk != CP_ID_KIND_QUALIFIED)
4948 *idk = CP_ID_KIND_NONE;
4949
4950 /* The `>' token might be the end of a template-id or
4951 template-parameter-list now. */
4952 parser->greater_than_is_operator_p
4953 = saved_greater_than_is_operator_p;
4954
4955 /* Consume the `)'. */
4956 token = cp_lexer_peek_token (parser->lexer);
4957 location_t close_paren_loc = token->location;
4958 expr.set_range (open_paren_loc, close_paren_loc);
4959 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4960 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4961 cp_parser_skip_to_end_of_statement (parser);
4962
4963 return expr;
4964 }
4965
4966 case CPP_OPEN_SQUARE:
4967 {
4968 if (c_dialect_objc ())
4969 {
4970 /* We might have an Objective-C++ message. */
4971 cp_parser_parse_tentatively (parser);
4972 tree msg = cp_parser_objc_message_expression (parser);
4973 /* If that works out, we're done ... */
4974 if (cp_parser_parse_definitely (parser))
4975 return msg;
4976 /* ... else, fall though to see if it's a lambda. */
4977 }
4978 cp_expr lam = cp_parser_lambda_expression (parser);
4979 /* Don't warn about a failed tentative parse. */
4980 if (cp_parser_error_occurred (parser))
4981 return error_mark_node;
4982 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4983 return lam;
4984 }
4985
4986 case CPP_OBJC_STRING:
4987 if (c_dialect_objc ())
4988 /* We have an Objective-C++ string literal. */
4989 return cp_parser_objc_expression (parser);
4990 cp_parser_error (parser, "expected primary-expression");
4991 return error_mark_node;
4992
4993 case CPP_KEYWORD:
4994 switch (token->keyword)
4995 {
4996 /* These two are the boolean literals. */
4997 case RID_TRUE:
4998 cp_lexer_consume_token (parser->lexer);
4999 return cp_expr (boolean_true_node, token->location);
5000 case RID_FALSE:
5001 cp_lexer_consume_token (parser->lexer);
5002 return cp_expr (boolean_false_node, token->location);
5003
5004 /* The `__null' literal. */
5005 case RID_NULL:
5006 cp_lexer_consume_token (parser->lexer);
5007 return cp_expr (null_node, token->location);
5008
5009 /* The `nullptr' literal. */
5010 case RID_NULLPTR:
5011 cp_lexer_consume_token (parser->lexer);
5012 return cp_expr (nullptr_node, token->location);
5013
5014 /* Recognize the `this' keyword. */
5015 case RID_THIS:
5016 cp_lexer_consume_token (parser->lexer);
5017 if (parser->local_variables_forbidden_p)
5018 {
5019 error_at (token->location,
5020 "%<this%> may not be used in this context");
5021 return error_mark_node;
5022 }
5023 /* Pointers cannot appear in constant-expressions. */
5024 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5025 return error_mark_node;
5026 return cp_expr (finish_this_expr (), token->location);
5027
5028 /* The `operator' keyword can be the beginning of an
5029 id-expression. */
5030 case RID_OPERATOR:
5031 goto id_expression;
5032
5033 case RID_FUNCTION_NAME:
5034 case RID_PRETTY_FUNCTION_NAME:
5035 case RID_C99_FUNCTION_NAME:
5036 {
5037 non_integral_constant name;
5038
5039 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5040 __func__ are the names of variables -- but they are
5041 treated specially. Therefore, they are handled here,
5042 rather than relying on the generic id-expression logic
5043 below. Grammatically, these names are id-expressions.
5044
5045 Consume the token. */
5046 token = cp_lexer_consume_token (parser->lexer);
5047
5048 switch (token->keyword)
5049 {
5050 case RID_FUNCTION_NAME:
5051 name = NIC_FUNC_NAME;
5052 break;
5053 case RID_PRETTY_FUNCTION_NAME:
5054 name = NIC_PRETTY_FUNC;
5055 break;
5056 case RID_C99_FUNCTION_NAME:
5057 name = NIC_C99_FUNC;
5058 break;
5059 default:
5060 gcc_unreachable ();
5061 }
5062
5063 if (cp_parser_non_integral_constant_expression (parser, name))
5064 return error_mark_node;
5065
5066 /* Look up the name. */
5067 return finish_fname (token->u.value);
5068 }
5069
5070 case RID_VA_ARG:
5071 {
5072 tree expression;
5073 tree type;
5074 source_location type_location;
5075 location_t start_loc
5076 = cp_lexer_peek_token (parser->lexer)->location;
5077 /* The `__builtin_va_arg' construct is used to handle
5078 `va_arg'. Consume the `__builtin_va_arg' token. */
5079 cp_lexer_consume_token (parser->lexer);
5080 /* Look for the opening `('. */
5081 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5082 /* Now, parse the assignment-expression. */
5083 expression = cp_parser_assignment_expression (parser);
5084 /* Look for the `,'. */
5085 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5086 type_location = cp_lexer_peek_token (parser->lexer)->location;
5087 /* Parse the type-id. */
5088 {
5089 type_id_in_expr_sentinel s (parser);
5090 type = cp_parser_type_id (parser);
5091 }
5092 /* Look for the closing `)'. */
5093 location_t finish_loc
5094 = cp_lexer_peek_token (parser->lexer)->location;
5095 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5096 /* Using `va_arg' in a constant-expression is not
5097 allowed. */
5098 if (cp_parser_non_integral_constant_expression (parser,
5099 NIC_VA_ARG))
5100 return error_mark_node;
5101 /* Construct a location of the form:
5102 __builtin_va_arg (v, int)
5103 ~~~~~~~~~~~~~~~~~~~~~^~~~
5104 with the caret at the type, ranging from the start of the
5105 "__builtin_va_arg" token to the close paren. */
5106 location_t combined_loc
5107 = make_location (type_location, start_loc, finish_loc);
5108 return build_x_va_arg (combined_loc, expression, type);
5109 }
5110
5111 case RID_OFFSETOF:
5112 return cp_parser_builtin_offsetof (parser);
5113
5114 case RID_HAS_NOTHROW_ASSIGN:
5115 case RID_HAS_NOTHROW_CONSTRUCTOR:
5116 case RID_HAS_NOTHROW_COPY:
5117 case RID_HAS_TRIVIAL_ASSIGN:
5118 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5119 case RID_HAS_TRIVIAL_COPY:
5120 case RID_HAS_TRIVIAL_DESTRUCTOR:
5121 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5122 case RID_HAS_VIRTUAL_DESTRUCTOR:
5123 case RID_IS_ABSTRACT:
5124 case RID_IS_AGGREGATE:
5125 case RID_IS_BASE_OF:
5126 case RID_IS_CLASS:
5127 case RID_IS_EMPTY:
5128 case RID_IS_ENUM:
5129 case RID_IS_FINAL:
5130 case RID_IS_LITERAL_TYPE:
5131 case RID_IS_POD:
5132 case RID_IS_POLYMORPHIC:
5133 case RID_IS_SAME_AS:
5134 case RID_IS_STD_LAYOUT:
5135 case RID_IS_TRIVIAL:
5136 case RID_IS_TRIVIALLY_ASSIGNABLE:
5137 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5138 case RID_IS_TRIVIALLY_COPYABLE:
5139 case RID_IS_UNION:
5140 return cp_parser_trait_expr (parser, token->keyword);
5141
5142 // C++ concepts
5143 case RID_REQUIRES:
5144 return cp_parser_requires_expression (parser);
5145
5146 /* Objective-C++ expressions. */
5147 case RID_AT_ENCODE:
5148 case RID_AT_PROTOCOL:
5149 case RID_AT_SELECTOR:
5150 return cp_parser_objc_expression (parser);
5151
5152 case RID_TEMPLATE:
5153 if (parser->in_function_body
5154 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5155 == CPP_LESS))
5156 {
5157 error_at (token->location,
5158 "a template declaration cannot appear at block scope");
5159 cp_parser_skip_to_end_of_block_or_statement (parser);
5160 return error_mark_node;
5161 }
5162 /* FALLTHRU */
5163 default:
5164 cp_parser_error (parser, "expected primary-expression");
5165 return error_mark_node;
5166 }
5167
5168 /* An id-expression can start with either an identifier, a
5169 `::' as the beginning of a qualified-id, or the "operator"
5170 keyword. */
5171 case CPP_NAME:
5172 case CPP_SCOPE:
5173 case CPP_TEMPLATE_ID:
5174 case CPP_NESTED_NAME_SPECIFIER:
5175 {
5176 id_expression:
5177 cp_expr id_expression;
5178 cp_expr decl;
5179 const char *error_msg;
5180 bool template_p;
5181 bool done;
5182 cp_token *id_expr_token;
5183
5184 /* Parse the id-expression. */
5185 id_expression
5186 = cp_parser_id_expression (parser,
5187 /*template_keyword_p=*/false,
5188 /*check_dependency_p=*/true,
5189 &template_p,
5190 /*declarator_p=*/false,
5191 /*optional_p=*/false);
5192 if (id_expression == error_mark_node)
5193 return error_mark_node;
5194 id_expr_token = token;
5195 token = cp_lexer_peek_token (parser->lexer);
5196 done = (token->type != CPP_OPEN_SQUARE
5197 && token->type != CPP_OPEN_PAREN
5198 && token->type != CPP_DOT
5199 && token->type != CPP_DEREF
5200 && token->type != CPP_PLUS_PLUS
5201 && token->type != CPP_MINUS_MINUS);
5202 /* If we have a template-id, then no further lookup is
5203 required. If the template-id was for a template-class, we
5204 will sometimes have a TYPE_DECL at this point. */
5205 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5206 || TREE_CODE (id_expression) == TYPE_DECL)
5207 decl = id_expression;
5208 /* Look up the name. */
5209 else
5210 {
5211 tree ambiguous_decls;
5212
5213 /* If we already know that this lookup is ambiguous, then
5214 we've already issued an error message; there's no reason
5215 to check again. */
5216 if (id_expr_token->type == CPP_NAME
5217 && id_expr_token->error_reported)
5218 {
5219 cp_parser_simulate_error (parser);
5220 return error_mark_node;
5221 }
5222
5223 decl = cp_parser_lookup_name (parser, id_expression,
5224 none_type,
5225 template_p,
5226 /*is_namespace=*/false,
5227 /*check_dependency=*/true,
5228 &ambiguous_decls,
5229 id_expr_token->location);
5230 /* If the lookup was ambiguous, an error will already have
5231 been issued. */
5232 if (ambiguous_decls)
5233 return error_mark_node;
5234
5235 /* In Objective-C++, we may have an Objective-C 2.0
5236 dot-syntax for classes here. */
5237 if (c_dialect_objc ()
5238 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5239 && TREE_CODE (decl) == TYPE_DECL
5240 && objc_is_class_name (decl))
5241 {
5242 tree component;
5243 cp_lexer_consume_token (parser->lexer);
5244 component = cp_parser_identifier (parser);
5245 if (component == error_mark_node)
5246 return error_mark_node;
5247
5248 tree result = objc_build_class_component_ref (id_expression,
5249 component);
5250 /* Build a location of the form:
5251 expr.component
5252 ~~~~~^~~~~~~~~
5253 with caret at the start of the component name (at
5254 input_location), ranging from the start of the id_expression
5255 to the end of the component name. */
5256 location_t combined_loc
5257 = make_location (input_location, id_expression.get_start (),
5258 get_finish (input_location));
5259 protected_set_expr_location (result, combined_loc);
5260 return result;
5261 }
5262
5263 /* In Objective-C++, an instance variable (ivar) may be preferred
5264 to whatever cp_parser_lookup_name() found.
5265 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5266 rest of c-family, we have to do a little extra work to preserve
5267 any location information in cp_expr "decl". Given that
5268 objc_lookup_ivar is implemented in "c-family" and "objc", we
5269 have a trip through the pure "tree" type, rather than cp_expr.
5270 Naively copying it back to "decl" would implicitly give the
5271 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5272 store an EXPR_LOCATION. Hence we only update "decl" (and
5273 hence its location_t) if we get back a different tree node. */
5274 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5275 id_expression);
5276 if (decl_tree != decl.get_value ())
5277 decl = cp_expr (decl_tree);
5278
5279 /* If name lookup gives us a SCOPE_REF, then the
5280 qualifying scope was dependent. */
5281 if (TREE_CODE (decl) == SCOPE_REF)
5282 {
5283 /* At this point, we do not know if DECL is a valid
5284 integral constant expression. We assume that it is
5285 in fact such an expression, so that code like:
5286
5287 template <int N> struct A {
5288 int a[B<N>::i];
5289 };
5290
5291 is accepted. At template-instantiation time, we
5292 will check that B<N>::i is actually a constant. */
5293 return decl;
5294 }
5295 /* Check to see if DECL is a local variable in a context
5296 where that is forbidden. */
5297 if (parser->local_variables_forbidden_p
5298 && local_variable_p (decl))
5299 {
5300 /* It might be that we only found DECL because we are
5301 trying to be generous with pre-ISO scoping rules.
5302 For example, consider:
5303
5304 int i;
5305 void g() {
5306 for (int i = 0; i < 10; ++i) {}
5307 extern void f(int j = i);
5308 }
5309
5310 Here, name look up will originally find the out
5311 of scope `i'. We need to issue a warning message,
5312 but then use the global `i'. */
5313 decl = check_for_out_of_scope_variable (decl);
5314 if (local_variable_p (decl))
5315 {
5316 error_at (id_expr_token->location,
5317 "local variable %qD may not appear in this context",
5318 decl.get_value ());
5319 return error_mark_node;
5320 }
5321 }
5322 }
5323
5324 decl = (finish_id_expression
5325 (id_expression, decl, parser->scope,
5326 idk,
5327 parser->integral_constant_expression_p,
5328 parser->allow_non_integral_constant_expression_p,
5329 &parser->non_integral_constant_expression_p,
5330 template_p, done, address_p,
5331 template_arg_p,
5332 &error_msg,
5333 id_expression.get_location ()));
5334 if (error_msg)
5335 cp_parser_error (parser, error_msg);
5336 decl.set_location (id_expr_token->location);
5337 return decl;
5338 }
5339
5340 /* Anything else is an error. */
5341 default:
5342 cp_parser_error (parser, "expected primary-expression");
5343 return error_mark_node;
5344 }
5345 }
5346
5347 static inline cp_expr
5348 cp_parser_primary_expression (cp_parser *parser,
5349 bool address_p,
5350 bool cast_p,
5351 bool template_arg_p,
5352 cp_id_kind *idk)
5353 {
5354 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5355 /*decltype*/false, idk);
5356 }
5357
5358 /* Parse an id-expression.
5359
5360 id-expression:
5361 unqualified-id
5362 qualified-id
5363
5364 qualified-id:
5365 :: [opt] nested-name-specifier template [opt] unqualified-id
5366 :: identifier
5367 :: operator-function-id
5368 :: template-id
5369
5370 Return a representation of the unqualified portion of the
5371 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5372 a `::' or nested-name-specifier.
5373
5374 Often, if the id-expression was a qualified-id, the caller will
5375 want to make a SCOPE_REF to represent the qualified-id. This
5376 function does not do this in order to avoid wastefully creating
5377 SCOPE_REFs when they are not required.
5378
5379 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5380 `template' keyword.
5381
5382 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5383 uninstantiated templates.
5384
5385 If *TEMPLATE_P is non-NULL, it is set to true iff the
5386 `template' keyword is used to explicitly indicate that the entity
5387 named is a template.
5388
5389 If DECLARATOR_P is true, the id-expression is appearing as part of
5390 a declarator, rather than as part of an expression. */
5391
5392 static cp_expr
5393 cp_parser_id_expression (cp_parser *parser,
5394 bool template_keyword_p,
5395 bool check_dependency_p,
5396 bool *template_p,
5397 bool declarator_p,
5398 bool optional_p)
5399 {
5400 bool global_scope_p;
5401 bool nested_name_specifier_p;
5402
5403 /* Assume the `template' keyword was not used. */
5404 if (template_p)
5405 *template_p = template_keyword_p;
5406
5407 /* Look for the optional `::' operator. */
5408 global_scope_p
5409 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5410 != NULL_TREE);
5411 /* Look for the optional nested-name-specifier. */
5412 nested_name_specifier_p
5413 = (cp_parser_nested_name_specifier_opt (parser,
5414 /*typename_keyword_p=*/false,
5415 check_dependency_p,
5416 /*type_p=*/false,
5417 declarator_p)
5418 != NULL_TREE);
5419 /* If there is a nested-name-specifier, then we are looking at
5420 the first qualified-id production. */
5421 if (nested_name_specifier_p)
5422 {
5423 tree saved_scope;
5424 tree saved_object_scope;
5425 tree saved_qualifying_scope;
5426 cp_expr unqualified_id;
5427 bool is_template;
5428
5429 /* See if the next token is the `template' keyword. */
5430 if (!template_p)
5431 template_p = &is_template;
5432 *template_p = cp_parser_optional_template_keyword (parser);
5433 /* Name lookup we do during the processing of the
5434 unqualified-id might obliterate SCOPE. */
5435 saved_scope = parser->scope;
5436 saved_object_scope = parser->object_scope;
5437 saved_qualifying_scope = parser->qualifying_scope;
5438 /* Process the final unqualified-id. */
5439 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5440 check_dependency_p,
5441 declarator_p,
5442 /*optional_p=*/false);
5443 /* Restore the SAVED_SCOPE for our caller. */
5444 parser->scope = saved_scope;
5445 parser->object_scope = saved_object_scope;
5446 parser->qualifying_scope = saved_qualifying_scope;
5447
5448 return unqualified_id;
5449 }
5450 /* Otherwise, if we are in global scope, then we are looking at one
5451 of the other qualified-id productions. */
5452 else if (global_scope_p)
5453 {
5454 cp_token *token;
5455 tree id;
5456
5457 /* Peek at the next token. */
5458 token = cp_lexer_peek_token (parser->lexer);
5459
5460 /* If it's an identifier, and the next token is not a "<", then
5461 we can avoid the template-id case. This is an optimization
5462 for this common case. */
5463 if (token->type == CPP_NAME
5464 && !cp_parser_nth_token_starts_template_argument_list_p
5465 (parser, 2))
5466 return cp_parser_identifier (parser);
5467
5468 cp_parser_parse_tentatively (parser);
5469 /* Try a template-id. */
5470 id = cp_parser_template_id (parser,
5471 /*template_keyword_p=*/false,
5472 /*check_dependency_p=*/true,
5473 none_type,
5474 declarator_p);
5475 /* If that worked, we're done. */
5476 if (cp_parser_parse_definitely (parser))
5477 return id;
5478
5479 /* Peek at the next token. (Changes in the token buffer may
5480 have invalidated the pointer obtained above.) */
5481 token = cp_lexer_peek_token (parser->lexer);
5482
5483 switch (token->type)
5484 {
5485 case CPP_NAME:
5486 return cp_parser_identifier (parser);
5487
5488 case CPP_KEYWORD:
5489 if (token->keyword == RID_OPERATOR)
5490 return cp_parser_operator_function_id (parser);
5491 /* Fall through. */
5492
5493 default:
5494 cp_parser_error (parser, "expected id-expression");
5495 return error_mark_node;
5496 }
5497 }
5498 else
5499 return cp_parser_unqualified_id (parser, template_keyword_p,
5500 /*check_dependency_p=*/true,
5501 declarator_p,
5502 optional_p);
5503 }
5504
5505 /* Parse an unqualified-id.
5506
5507 unqualified-id:
5508 identifier
5509 operator-function-id
5510 conversion-function-id
5511 ~ class-name
5512 template-id
5513
5514 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5515 keyword, in a construct like `A::template ...'.
5516
5517 Returns a representation of unqualified-id. For the `identifier'
5518 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5519 production a BIT_NOT_EXPR is returned; the operand of the
5520 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5521 other productions, see the documentation accompanying the
5522 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5523 names are looked up in uninstantiated templates. If DECLARATOR_P
5524 is true, the unqualified-id is appearing as part of a declarator,
5525 rather than as part of an expression. */
5526
5527 static cp_expr
5528 cp_parser_unqualified_id (cp_parser* parser,
5529 bool template_keyword_p,
5530 bool check_dependency_p,
5531 bool declarator_p,
5532 bool optional_p)
5533 {
5534 cp_token *token;
5535
5536 /* Peek at the next token. */
5537 token = cp_lexer_peek_token (parser->lexer);
5538
5539 switch ((int) token->type)
5540 {
5541 case CPP_NAME:
5542 {
5543 tree id;
5544
5545 /* We don't know yet whether or not this will be a
5546 template-id. */
5547 cp_parser_parse_tentatively (parser);
5548 /* Try a template-id. */
5549 id = cp_parser_template_id (parser, template_keyword_p,
5550 check_dependency_p,
5551 none_type,
5552 declarator_p);
5553 /* If it worked, we're done. */
5554 if (cp_parser_parse_definitely (parser))
5555 return id;
5556 /* Otherwise, it's an ordinary identifier. */
5557 return cp_parser_identifier (parser);
5558 }
5559
5560 case CPP_TEMPLATE_ID:
5561 return cp_parser_template_id (parser, template_keyword_p,
5562 check_dependency_p,
5563 none_type,
5564 declarator_p);
5565
5566 case CPP_COMPL:
5567 {
5568 tree type_decl;
5569 tree qualifying_scope;
5570 tree object_scope;
5571 tree scope;
5572 bool done;
5573
5574 /* Consume the `~' token. */
5575 cp_lexer_consume_token (parser->lexer);
5576 /* Parse the class-name. The standard, as written, seems to
5577 say that:
5578
5579 template <typename T> struct S { ~S (); };
5580 template <typename T> S<T>::~S() {}
5581
5582 is invalid, since `~' must be followed by a class-name, but
5583 `S<T>' is dependent, and so not known to be a class.
5584 That's not right; we need to look in uninstantiated
5585 templates. A further complication arises from:
5586
5587 template <typename T> void f(T t) {
5588 t.T::~T();
5589 }
5590
5591 Here, it is not possible to look up `T' in the scope of `T'
5592 itself. We must look in both the current scope, and the
5593 scope of the containing complete expression.
5594
5595 Yet another issue is:
5596
5597 struct S {
5598 int S;
5599 ~S();
5600 };
5601
5602 S::~S() {}
5603
5604 The standard does not seem to say that the `S' in `~S'
5605 should refer to the type `S' and not the data member
5606 `S::S'. */
5607
5608 /* DR 244 says that we look up the name after the "~" in the
5609 same scope as we looked up the qualifying name. That idea
5610 isn't fully worked out; it's more complicated than that. */
5611 scope = parser->scope;
5612 object_scope = parser->object_scope;
5613 qualifying_scope = parser->qualifying_scope;
5614
5615 /* Check for invalid scopes. */
5616 if (scope == error_mark_node)
5617 {
5618 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5619 cp_lexer_consume_token (parser->lexer);
5620 return error_mark_node;
5621 }
5622 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5623 {
5624 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5625 error_at (token->location,
5626 "scope %qT before %<~%> is not a class-name",
5627 scope);
5628 cp_parser_simulate_error (parser);
5629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5630 cp_lexer_consume_token (parser->lexer);
5631 return error_mark_node;
5632 }
5633 gcc_assert (!scope || TYPE_P (scope));
5634
5635 /* If the name is of the form "X::~X" it's OK even if X is a
5636 typedef. */
5637 token = cp_lexer_peek_token (parser->lexer);
5638 if (scope
5639 && token->type == CPP_NAME
5640 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5641 != CPP_LESS)
5642 && (token->u.value == TYPE_IDENTIFIER (scope)
5643 || (CLASS_TYPE_P (scope)
5644 && constructor_name_p (token->u.value, scope))))
5645 {
5646 cp_lexer_consume_token (parser->lexer);
5647 return build_nt (BIT_NOT_EXPR, scope);
5648 }
5649
5650 /* ~auto means the destructor of whatever the object is. */
5651 if (cp_parser_is_keyword (token, RID_AUTO))
5652 {
5653 if (cxx_dialect < cxx14)
5654 pedwarn (input_location, 0,
5655 "%<~auto%> only available with "
5656 "-std=c++14 or -std=gnu++14");
5657 cp_lexer_consume_token (parser->lexer);
5658 return build_nt (BIT_NOT_EXPR, make_auto ());
5659 }
5660
5661 /* If there was an explicit qualification (S::~T), first look
5662 in the scope given by the qualification (i.e., S).
5663
5664 Note: in the calls to cp_parser_class_name below we pass
5665 typename_type so that lookup finds the injected-class-name
5666 rather than the constructor. */
5667 done = false;
5668 type_decl = NULL_TREE;
5669 if (scope)
5670 {
5671 cp_parser_parse_tentatively (parser);
5672 type_decl = cp_parser_class_name (parser,
5673 /*typename_keyword_p=*/false,
5674 /*template_keyword_p=*/false,
5675 typename_type,
5676 /*check_dependency=*/false,
5677 /*class_head_p=*/false,
5678 declarator_p);
5679 if (cp_parser_parse_definitely (parser))
5680 done = true;
5681 }
5682 /* In "N::S::~S", look in "N" as well. */
5683 if (!done && scope && qualifying_scope)
5684 {
5685 cp_parser_parse_tentatively (parser);
5686 parser->scope = qualifying_scope;
5687 parser->object_scope = NULL_TREE;
5688 parser->qualifying_scope = NULL_TREE;
5689 type_decl
5690 = cp_parser_class_name (parser,
5691 /*typename_keyword_p=*/false,
5692 /*template_keyword_p=*/false,
5693 typename_type,
5694 /*check_dependency=*/false,
5695 /*class_head_p=*/false,
5696 declarator_p);
5697 if (cp_parser_parse_definitely (parser))
5698 done = true;
5699 }
5700 /* In "p->S::~T", look in the scope given by "*p" as well. */
5701 else if (!done && object_scope)
5702 {
5703 cp_parser_parse_tentatively (parser);
5704 parser->scope = object_scope;
5705 parser->object_scope = NULL_TREE;
5706 parser->qualifying_scope = NULL_TREE;
5707 type_decl
5708 = cp_parser_class_name (parser,
5709 /*typename_keyword_p=*/false,
5710 /*template_keyword_p=*/false,
5711 typename_type,
5712 /*check_dependency=*/false,
5713 /*class_head_p=*/false,
5714 declarator_p);
5715 if (cp_parser_parse_definitely (parser))
5716 done = true;
5717 }
5718 /* Look in the surrounding context. */
5719 if (!done)
5720 {
5721 parser->scope = NULL_TREE;
5722 parser->object_scope = NULL_TREE;
5723 parser->qualifying_scope = NULL_TREE;
5724 if (processing_template_decl)
5725 cp_parser_parse_tentatively (parser);
5726 type_decl
5727 = cp_parser_class_name (parser,
5728 /*typename_keyword_p=*/false,
5729 /*template_keyword_p=*/false,
5730 typename_type,
5731 /*check_dependency=*/false,
5732 /*class_head_p=*/false,
5733 declarator_p);
5734 if (processing_template_decl
5735 && ! cp_parser_parse_definitely (parser))
5736 {
5737 /* We couldn't find a type with this name. If we're parsing
5738 tentatively, fail and try something else. */
5739 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5740 {
5741 cp_parser_simulate_error (parser);
5742 return error_mark_node;
5743 }
5744 /* Otherwise, accept it and check for a match at instantiation
5745 time. */
5746 type_decl = cp_parser_identifier (parser);
5747 if (type_decl != error_mark_node)
5748 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5749 return type_decl;
5750 }
5751 }
5752 /* If an error occurred, assume that the name of the
5753 destructor is the same as the name of the qualifying
5754 class. That allows us to keep parsing after running
5755 into ill-formed destructor names. */
5756 if (type_decl == error_mark_node && scope)
5757 return build_nt (BIT_NOT_EXPR, scope);
5758 else if (type_decl == error_mark_node)
5759 return error_mark_node;
5760
5761 /* Check that destructor name and scope match. */
5762 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5763 {
5764 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5765 error_at (token->location,
5766 "declaration of %<~%T%> as member of %qT",
5767 type_decl, scope);
5768 cp_parser_simulate_error (parser);
5769 return error_mark_node;
5770 }
5771
5772 /* [class.dtor]
5773
5774 A typedef-name that names a class shall not be used as the
5775 identifier in the declarator for a destructor declaration. */
5776 if (declarator_p
5777 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5778 && !DECL_SELF_REFERENCE_P (type_decl)
5779 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5780 error_at (token->location,
5781 "typedef-name %qD used as destructor declarator",
5782 type_decl);
5783
5784 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5785 }
5786
5787 case CPP_KEYWORD:
5788 if (token->keyword == RID_OPERATOR)
5789 {
5790 cp_expr id;
5791
5792 /* This could be a template-id, so we try that first. */
5793 cp_parser_parse_tentatively (parser);
5794 /* Try a template-id. */
5795 id = cp_parser_template_id (parser, template_keyword_p,
5796 /*check_dependency_p=*/true,
5797 none_type,
5798 declarator_p);
5799 /* If that worked, we're done. */
5800 if (cp_parser_parse_definitely (parser))
5801 return id;
5802 /* We still don't know whether we're looking at an
5803 operator-function-id or a conversion-function-id. */
5804 cp_parser_parse_tentatively (parser);
5805 /* Try an operator-function-id. */
5806 id = cp_parser_operator_function_id (parser);
5807 /* If that didn't work, try a conversion-function-id. */
5808 if (!cp_parser_parse_definitely (parser))
5809 id = cp_parser_conversion_function_id (parser);
5810 else if (UDLIT_OPER_P (id))
5811 {
5812 /* 17.6.3.3.5 */
5813 const char *name = UDLIT_OP_SUFFIX (id);
5814 if (name[0] != '_' && !in_system_header_at (input_location)
5815 && declarator_p)
5816 warning (OPT_Wliteral_suffix,
5817 "literal operator suffixes not preceded by %<_%>"
5818 " are reserved for future standardization");
5819 }
5820
5821 return id;
5822 }
5823 /* Fall through. */
5824
5825 default:
5826 if (optional_p)
5827 return NULL_TREE;
5828 cp_parser_error (parser, "expected unqualified-id");
5829 return error_mark_node;
5830 }
5831 }
5832
5833 /* Parse an (optional) nested-name-specifier.
5834
5835 nested-name-specifier: [C++98]
5836 class-or-namespace-name :: nested-name-specifier [opt]
5837 class-or-namespace-name :: template nested-name-specifier [opt]
5838
5839 nested-name-specifier: [C++0x]
5840 type-name ::
5841 namespace-name ::
5842 nested-name-specifier identifier ::
5843 nested-name-specifier template [opt] simple-template-id ::
5844
5845 PARSER->SCOPE should be set appropriately before this function is
5846 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5847 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5848 in name lookups.
5849
5850 Sets PARSER->SCOPE to the class (TYPE) or namespace
5851 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5852 it unchanged if there is no nested-name-specifier. Returns the new
5853 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5854
5855 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5856 part of a declaration and/or decl-specifier. */
5857
5858 static tree
5859 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5860 bool typename_keyword_p,
5861 bool check_dependency_p,
5862 bool type_p,
5863 bool is_declaration)
5864 {
5865 bool success = false;
5866 cp_token_position start = 0;
5867 cp_token *token;
5868
5869 /* Remember where the nested-name-specifier starts. */
5870 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5871 {
5872 start = cp_lexer_token_position (parser->lexer, false);
5873 push_deferring_access_checks (dk_deferred);
5874 }
5875
5876 while (true)
5877 {
5878 tree new_scope;
5879 tree old_scope;
5880 tree saved_qualifying_scope;
5881 bool template_keyword_p;
5882
5883 /* Spot cases that cannot be the beginning of a
5884 nested-name-specifier. */
5885 token = cp_lexer_peek_token (parser->lexer);
5886
5887 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5888 the already parsed nested-name-specifier. */
5889 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5890 {
5891 /* Grab the nested-name-specifier and continue the loop. */
5892 cp_parser_pre_parsed_nested_name_specifier (parser);
5893 /* If we originally encountered this nested-name-specifier
5894 with IS_DECLARATION set to false, we will not have
5895 resolved TYPENAME_TYPEs, so we must do so here. */
5896 if (is_declaration
5897 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5898 {
5899 new_scope = resolve_typename_type (parser->scope,
5900 /*only_current_p=*/false);
5901 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5902 parser->scope = new_scope;
5903 }
5904 success = true;
5905 continue;
5906 }
5907
5908 /* Spot cases that cannot be the beginning of a
5909 nested-name-specifier. On the second and subsequent times
5910 through the loop, we look for the `template' keyword. */
5911 if (success && token->keyword == RID_TEMPLATE)
5912 ;
5913 /* A template-id can start a nested-name-specifier. */
5914 else if (token->type == CPP_TEMPLATE_ID)
5915 ;
5916 /* DR 743: decltype can be used in a nested-name-specifier. */
5917 else if (token_is_decltype (token))
5918 ;
5919 else
5920 {
5921 /* If the next token is not an identifier, then it is
5922 definitely not a type-name or namespace-name. */
5923 if (token->type != CPP_NAME)
5924 break;
5925 /* If the following token is neither a `<' (to begin a
5926 template-id), nor a `::', then we are not looking at a
5927 nested-name-specifier. */
5928 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5929
5930 if (token->type == CPP_COLON
5931 && parser->colon_corrects_to_scope_p
5932 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5933 {
5934 error_at (token->location,
5935 "found %<:%> in nested-name-specifier, expected %<::%>");
5936 token->type = CPP_SCOPE;
5937 }
5938
5939 if (token->type != CPP_SCOPE
5940 && !cp_parser_nth_token_starts_template_argument_list_p
5941 (parser, 2))
5942 break;
5943 }
5944
5945 /* The nested-name-specifier is optional, so we parse
5946 tentatively. */
5947 cp_parser_parse_tentatively (parser);
5948
5949 /* Look for the optional `template' keyword, if this isn't the
5950 first time through the loop. */
5951 if (success)
5952 template_keyword_p = cp_parser_optional_template_keyword (parser);
5953 else
5954 template_keyword_p = false;
5955
5956 /* Save the old scope since the name lookup we are about to do
5957 might destroy it. */
5958 old_scope = parser->scope;
5959 saved_qualifying_scope = parser->qualifying_scope;
5960 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5961 look up names in "X<T>::I" in order to determine that "Y" is
5962 a template. So, if we have a typename at this point, we make
5963 an effort to look through it. */
5964 if (is_declaration
5965 && !typename_keyword_p
5966 && parser->scope
5967 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5968 parser->scope = resolve_typename_type (parser->scope,
5969 /*only_current_p=*/false);
5970 /* Parse the qualifying entity. */
5971 new_scope
5972 = cp_parser_qualifying_entity (parser,
5973 typename_keyword_p,
5974 template_keyword_p,
5975 check_dependency_p,
5976 type_p,
5977 is_declaration);
5978 /* Look for the `::' token. */
5979 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5980
5981 /* If we found what we wanted, we keep going; otherwise, we're
5982 done. */
5983 if (!cp_parser_parse_definitely (parser))
5984 {
5985 bool error_p = false;
5986
5987 /* Restore the OLD_SCOPE since it was valid before the
5988 failed attempt at finding the last
5989 class-or-namespace-name. */
5990 parser->scope = old_scope;
5991 parser->qualifying_scope = saved_qualifying_scope;
5992
5993 /* If the next token is a decltype, and the one after that is a
5994 `::', then the decltype has failed to resolve to a class or
5995 enumeration type. Give this error even when parsing
5996 tentatively since it can't possibly be valid--and we're going
5997 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5998 won't get another chance.*/
5999 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6000 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6001 == CPP_SCOPE))
6002 {
6003 token = cp_lexer_consume_token (parser->lexer);
6004 error_at (token->location, "decltype evaluates to %qT, "
6005 "which is not a class or enumeration type",
6006 token->u.tree_check_value->value);
6007 parser->scope = error_mark_node;
6008 error_p = true;
6009 /* As below. */
6010 success = true;
6011 cp_lexer_consume_token (parser->lexer);
6012 }
6013
6014 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6015 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6016 {
6017 /* If we have a non-type template-id followed by ::, it can't
6018 possibly be valid. */
6019 token = cp_lexer_peek_token (parser->lexer);
6020 tree tid = token->u.tree_check_value->value;
6021 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6022 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6023 {
6024 tree tmpl = NULL_TREE;
6025 if (is_overloaded_fn (tid))
6026 {
6027 tree fns = get_fns (tid);
6028 if (!OVL_CHAIN (fns))
6029 tmpl = OVL_CURRENT (fns);
6030 error_at (token->location, "function template-id %qD "
6031 "in nested-name-specifier", tid);
6032 }
6033 else
6034 {
6035 /* Variable template. */
6036 tmpl = TREE_OPERAND (tid, 0);
6037 gcc_assert (variable_template_p (tmpl));
6038 error_at (token->location, "variable template-id %qD "
6039 "in nested-name-specifier", tid);
6040 }
6041 if (tmpl)
6042 inform (DECL_SOURCE_LOCATION (tmpl),
6043 "%qD declared here", tmpl);
6044
6045 parser->scope = error_mark_node;
6046 error_p = true;
6047 /* As below. */
6048 success = true;
6049 cp_lexer_consume_token (parser->lexer);
6050 cp_lexer_consume_token (parser->lexer);
6051 }
6052 }
6053
6054 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6055 break;
6056 /* If the next token is an identifier, and the one after
6057 that is a `::', then any valid interpretation would have
6058 found a class-or-namespace-name. */
6059 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6060 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6061 == CPP_SCOPE)
6062 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6063 != CPP_COMPL))
6064 {
6065 token = cp_lexer_consume_token (parser->lexer);
6066 if (!error_p)
6067 {
6068 if (!token->error_reported)
6069 {
6070 tree decl;
6071 tree ambiguous_decls;
6072
6073 decl = cp_parser_lookup_name (parser, token->u.value,
6074 none_type,
6075 /*is_template=*/false,
6076 /*is_namespace=*/false,
6077 /*check_dependency=*/true,
6078 &ambiguous_decls,
6079 token->location);
6080 if (TREE_CODE (decl) == TEMPLATE_DECL)
6081 error_at (token->location,
6082 "%qD used without template parameters",
6083 decl);
6084 else if (ambiguous_decls)
6085 {
6086 // cp_parser_lookup_name has the same diagnostic,
6087 // thus make sure to emit it at most once.
6088 if (cp_parser_uncommitted_to_tentative_parse_p
6089 (parser))
6090 {
6091 error_at (token->location,
6092 "reference to %qD is ambiguous",
6093 token->u.value);
6094 print_candidates (ambiguous_decls);
6095 }
6096 decl = error_mark_node;
6097 }
6098 else
6099 {
6100 if (cxx_dialect != cxx98)
6101 cp_parser_name_lookup_error
6102 (parser, token->u.value, decl, NLE_NOT_CXX98,
6103 token->location);
6104 else
6105 cp_parser_name_lookup_error
6106 (parser, token->u.value, decl, NLE_CXX98,
6107 token->location);
6108 }
6109 }
6110 parser->scope = error_mark_node;
6111 error_p = true;
6112 /* Treat this as a successful nested-name-specifier
6113 due to:
6114
6115 [basic.lookup.qual]
6116
6117 If the name found is not a class-name (clause
6118 _class_) or namespace-name (_namespace.def_), the
6119 program is ill-formed. */
6120 success = true;
6121 }
6122 cp_lexer_consume_token (parser->lexer);
6123 }
6124 break;
6125 }
6126 /* We've found one valid nested-name-specifier. */
6127 success = true;
6128 /* Name lookup always gives us a DECL. */
6129 if (TREE_CODE (new_scope) == TYPE_DECL)
6130 new_scope = TREE_TYPE (new_scope);
6131 /* Uses of "template" must be followed by actual templates. */
6132 if (template_keyword_p
6133 && !(CLASS_TYPE_P (new_scope)
6134 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6135 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6136 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6137 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6138 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6139 == TEMPLATE_ID_EXPR)))
6140 permerror (input_location, TYPE_P (new_scope)
6141 ? G_("%qT is not a template")
6142 : G_("%qD is not a template"),
6143 new_scope);
6144 /* If it is a class scope, try to complete it; we are about to
6145 be looking up names inside the class. */
6146 if (TYPE_P (new_scope)
6147 /* Since checking types for dependency can be expensive,
6148 avoid doing it if the type is already complete. */
6149 && !COMPLETE_TYPE_P (new_scope)
6150 /* Do not try to complete dependent types. */
6151 && !dependent_type_p (new_scope))
6152 {
6153 new_scope = complete_type (new_scope);
6154 /* If it is a typedef to current class, use the current
6155 class instead, as the typedef won't have any names inside
6156 it yet. */
6157 if (!COMPLETE_TYPE_P (new_scope)
6158 && currently_open_class (new_scope))
6159 new_scope = TYPE_MAIN_VARIANT (new_scope);
6160 }
6161 /* Make sure we look in the right scope the next time through
6162 the loop. */
6163 parser->scope = new_scope;
6164 }
6165
6166 /* If parsing tentatively, replace the sequence of tokens that makes
6167 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6168 token. That way, should we re-parse the token stream, we will
6169 not have to repeat the effort required to do the parse, nor will
6170 we issue duplicate error messages. */
6171 if (success && start)
6172 {
6173 cp_token *token;
6174
6175 token = cp_lexer_token_at (parser->lexer, start);
6176 /* Reset the contents of the START token. */
6177 token->type = CPP_NESTED_NAME_SPECIFIER;
6178 /* Retrieve any deferred checks. Do not pop this access checks yet
6179 so the memory will not be reclaimed during token replacing below. */
6180 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6181 token->u.tree_check_value->value = parser->scope;
6182 token->u.tree_check_value->checks = get_deferred_access_checks ();
6183 token->u.tree_check_value->qualifying_scope =
6184 parser->qualifying_scope;
6185 token->keyword = RID_MAX;
6186
6187 /* Purge all subsequent tokens. */
6188 cp_lexer_purge_tokens_after (parser->lexer, start);
6189 }
6190
6191 if (start)
6192 pop_to_parent_deferring_access_checks ();
6193
6194 return success ? parser->scope : NULL_TREE;
6195 }
6196
6197 /* Parse a nested-name-specifier. See
6198 cp_parser_nested_name_specifier_opt for details. This function
6199 behaves identically, except that it will an issue an error if no
6200 nested-name-specifier is present. */
6201
6202 static tree
6203 cp_parser_nested_name_specifier (cp_parser *parser,
6204 bool typename_keyword_p,
6205 bool check_dependency_p,
6206 bool type_p,
6207 bool is_declaration)
6208 {
6209 tree scope;
6210
6211 /* Look for the nested-name-specifier. */
6212 scope = cp_parser_nested_name_specifier_opt (parser,
6213 typename_keyword_p,
6214 check_dependency_p,
6215 type_p,
6216 is_declaration);
6217 /* If it was not present, issue an error message. */
6218 if (!scope)
6219 {
6220 cp_parser_error (parser, "expected nested-name-specifier");
6221 parser->scope = NULL_TREE;
6222 }
6223
6224 return scope;
6225 }
6226
6227 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6228 this is either a class-name or a namespace-name (which corresponds
6229 to the class-or-namespace-name production in the grammar). For
6230 C++0x, it can also be a type-name that refers to an enumeration
6231 type or a simple-template-id.
6232
6233 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6234 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6235 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6236 TYPE_P is TRUE iff the next name should be taken as a class-name,
6237 even the same name is declared to be another entity in the same
6238 scope.
6239
6240 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6241 specified by the class-or-namespace-name. If neither is found the
6242 ERROR_MARK_NODE is returned. */
6243
6244 static tree
6245 cp_parser_qualifying_entity (cp_parser *parser,
6246 bool typename_keyword_p,
6247 bool template_keyword_p,
6248 bool check_dependency_p,
6249 bool type_p,
6250 bool is_declaration)
6251 {
6252 tree saved_scope;
6253 tree saved_qualifying_scope;
6254 tree saved_object_scope;
6255 tree scope;
6256 bool only_class_p;
6257 bool successful_parse_p;
6258
6259 /* DR 743: decltype can appear in a nested-name-specifier. */
6260 if (cp_lexer_next_token_is_decltype (parser->lexer))
6261 {
6262 scope = cp_parser_decltype (parser);
6263 if (TREE_CODE (scope) != ENUMERAL_TYPE
6264 && !MAYBE_CLASS_TYPE_P (scope))
6265 {
6266 cp_parser_simulate_error (parser);
6267 return error_mark_node;
6268 }
6269 if (TYPE_NAME (scope))
6270 scope = TYPE_NAME (scope);
6271 return scope;
6272 }
6273
6274 /* Before we try to parse the class-name, we must save away the
6275 current PARSER->SCOPE since cp_parser_class_name will destroy
6276 it. */
6277 saved_scope = parser->scope;
6278 saved_qualifying_scope = parser->qualifying_scope;
6279 saved_object_scope = parser->object_scope;
6280 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6281 there is no need to look for a namespace-name. */
6282 only_class_p = template_keyword_p
6283 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6284 if (!only_class_p)
6285 cp_parser_parse_tentatively (parser);
6286 scope = cp_parser_class_name (parser,
6287 typename_keyword_p,
6288 template_keyword_p,
6289 type_p ? class_type : none_type,
6290 check_dependency_p,
6291 /*class_head_p=*/false,
6292 is_declaration,
6293 /*enum_ok=*/cxx_dialect > cxx98);
6294 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6295 /* If that didn't work, try for a namespace-name. */
6296 if (!only_class_p && !successful_parse_p)
6297 {
6298 /* Restore the saved scope. */
6299 parser->scope = saved_scope;
6300 parser->qualifying_scope = saved_qualifying_scope;
6301 parser->object_scope = saved_object_scope;
6302 /* If we are not looking at an identifier followed by the scope
6303 resolution operator, then this is not part of a
6304 nested-name-specifier. (Note that this function is only used
6305 to parse the components of a nested-name-specifier.) */
6306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6307 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6308 return error_mark_node;
6309 scope = cp_parser_namespace_name (parser);
6310 }
6311
6312 return scope;
6313 }
6314
6315 /* Return true if we are looking at a compound-literal, false otherwise. */
6316
6317 static bool
6318 cp_parser_compound_literal_p (cp_parser *parser)
6319 {
6320 /* Consume the `('. */
6321 cp_lexer_consume_token (parser->lexer);
6322
6323 cp_lexer_save_tokens (parser->lexer);
6324
6325 /* Skip tokens until the next token is a closing parenthesis.
6326 If we find the closing `)', and the next token is a `{', then
6327 we are looking at a compound-literal. */
6328 bool compound_literal_p
6329 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6330 /*consume_paren=*/true)
6331 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6332
6333 /* Roll back the tokens we skipped. */
6334 cp_lexer_rollback_tokens (parser->lexer);
6335
6336 return compound_literal_p;
6337 }
6338
6339 /* Parse a postfix-expression.
6340
6341 postfix-expression:
6342 primary-expression
6343 postfix-expression [ expression ]
6344 postfix-expression ( expression-list [opt] )
6345 simple-type-specifier ( expression-list [opt] )
6346 typename :: [opt] nested-name-specifier identifier
6347 ( expression-list [opt] )
6348 typename :: [opt] nested-name-specifier template [opt] template-id
6349 ( expression-list [opt] )
6350 postfix-expression . template [opt] id-expression
6351 postfix-expression -> template [opt] id-expression
6352 postfix-expression . pseudo-destructor-name
6353 postfix-expression -> pseudo-destructor-name
6354 postfix-expression ++
6355 postfix-expression --
6356 dynamic_cast < type-id > ( expression )
6357 static_cast < type-id > ( expression )
6358 reinterpret_cast < type-id > ( expression )
6359 const_cast < type-id > ( expression )
6360 typeid ( expression )
6361 typeid ( type-id )
6362
6363 GNU Extension:
6364
6365 postfix-expression:
6366 ( type-id ) { initializer-list , [opt] }
6367
6368 This extension is a GNU version of the C99 compound-literal
6369 construct. (The C99 grammar uses `type-name' instead of `type-id',
6370 but they are essentially the same concept.)
6371
6372 If ADDRESS_P is true, the postfix expression is the operand of the
6373 `&' operator. CAST_P is true if this expression is the target of a
6374 cast.
6375
6376 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6377 class member access expressions [expr.ref].
6378
6379 Returns a representation of the expression. */
6380
6381 static cp_expr
6382 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6383 bool member_access_only_p, bool decltype_p,
6384 cp_id_kind * pidk_return)
6385 {
6386 cp_token *token;
6387 location_t loc;
6388 enum rid keyword;
6389 cp_id_kind idk = CP_ID_KIND_NONE;
6390 cp_expr postfix_expression = NULL_TREE;
6391 bool is_member_access = false;
6392 int saved_in_statement = -1;
6393
6394 /* Peek at the next token. */
6395 token = cp_lexer_peek_token (parser->lexer);
6396 loc = token->location;
6397 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6398
6399 /* Some of the productions are determined by keywords. */
6400 keyword = token->keyword;
6401 switch (keyword)
6402 {
6403 case RID_DYNCAST:
6404 case RID_STATCAST:
6405 case RID_REINTCAST:
6406 case RID_CONSTCAST:
6407 {
6408 tree type;
6409 cp_expr expression;
6410 const char *saved_message;
6411 bool saved_in_type_id_in_expr_p;
6412
6413 /* All of these can be handled in the same way from the point
6414 of view of parsing. Begin by consuming the token
6415 identifying the cast. */
6416 cp_lexer_consume_token (parser->lexer);
6417
6418 /* New types cannot be defined in the cast. */
6419 saved_message = parser->type_definition_forbidden_message;
6420 parser->type_definition_forbidden_message
6421 = G_("types may not be defined in casts");
6422
6423 /* Look for the opening `<'. */
6424 cp_parser_require (parser, CPP_LESS, RT_LESS);
6425 /* Parse the type to which we are casting. */
6426 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6427 parser->in_type_id_in_expr_p = true;
6428 type = cp_parser_type_id (parser);
6429 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6430 /* Look for the closing `>'. */
6431 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6432 /* Restore the old message. */
6433 parser->type_definition_forbidden_message = saved_message;
6434
6435 bool saved_greater_than_is_operator_p
6436 = parser->greater_than_is_operator_p;
6437 parser->greater_than_is_operator_p = true;
6438
6439 /* And the expression which is being cast. */
6440 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6441 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6442 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6443 RT_CLOSE_PAREN);
6444 location_t end_loc = close_paren ?
6445 close_paren->location : UNKNOWN_LOCATION;
6446
6447 parser->greater_than_is_operator_p
6448 = saved_greater_than_is_operator_p;
6449
6450 /* Only type conversions to integral or enumeration types
6451 can be used in constant-expressions. */
6452 if (!cast_valid_in_integral_constant_expression_p (type)
6453 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6454 {
6455 postfix_expression = error_mark_node;
6456 break;
6457 }
6458
6459 switch (keyword)
6460 {
6461 case RID_DYNCAST:
6462 postfix_expression
6463 = build_dynamic_cast (type, expression, tf_warning_or_error);
6464 break;
6465 case RID_STATCAST:
6466 postfix_expression
6467 = build_static_cast (type, expression, tf_warning_or_error);
6468 break;
6469 case RID_REINTCAST:
6470 postfix_expression
6471 = build_reinterpret_cast (type, expression,
6472 tf_warning_or_error);
6473 break;
6474 case RID_CONSTCAST:
6475 postfix_expression
6476 = build_const_cast (type, expression, tf_warning_or_error);
6477 break;
6478 default:
6479 gcc_unreachable ();
6480 }
6481
6482 /* Construct a location e.g. :
6483 reinterpret_cast <int *> (expr)
6484 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6485 ranging from the start of the "*_cast" token to the final closing
6486 paren, with the caret at the start. */
6487 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6488 postfix_expression.set_location (cp_cast_loc);
6489 }
6490 break;
6491
6492 case RID_TYPEID:
6493 {
6494 tree type;
6495 const char *saved_message;
6496 bool saved_in_type_id_in_expr_p;
6497
6498 /* Consume the `typeid' token. */
6499 cp_lexer_consume_token (parser->lexer);
6500 /* Look for the `(' token. */
6501 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6502 /* Types cannot be defined in a `typeid' expression. */
6503 saved_message = parser->type_definition_forbidden_message;
6504 parser->type_definition_forbidden_message
6505 = G_("types may not be defined in a %<typeid%> expression");
6506 /* We can't be sure yet whether we're looking at a type-id or an
6507 expression. */
6508 cp_parser_parse_tentatively (parser);
6509 /* Try a type-id first. */
6510 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6511 parser->in_type_id_in_expr_p = true;
6512 type = cp_parser_type_id (parser);
6513 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6514 /* Look for the `)' token. Otherwise, we can't be sure that
6515 we're not looking at an expression: consider `typeid (int
6516 (3))', for example. */
6517 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6518 /* If all went well, simply lookup the type-id. */
6519 if (cp_parser_parse_definitely (parser))
6520 postfix_expression = get_typeid (type, tf_warning_or_error);
6521 /* Otherwise, fall back to the expression variant. */
6522 else
6523 {
6524 tree expression;
6525
6526 /* Look for an expression. */
6527 expression = cp_parser_expression (parser, & idk);
6528 /* Compute its typeid. */
6529 postfix_expression = build_typeid (expression, tf_warning_or_error);
6530 /* Look for the `)' token. */
6531 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6532 }
6533 /* Restore the saved message. */
6534 parser->type_definition_forbidden_message = saved_message;
6535 /* `typeid' may not appear in an integral constant expression. */
6536 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6537 postfix_expression = error_mark_node;
6538 }
6539 break;
6540
6541 case RID_TYPENAME:
6542 {
6543 tree type;
6544 /* The syntax permitted here is the same permitted for an
6545 elaborated-type-specifier. */
6546 ++parser->prevent_constrained_type_specifiers;
6547 type = cp_parser_elaborated_type_specifier (parser,
6548 /*is_friend=*/false,
6549 /*is_declaration=*/false);
6550 --parser->prevent_constrained_type_specifiers;
6551 postfix_expression = cp_parser_functional_cast (parser, type);
6552 }
6553 break;
6554
6555 case RID_CILK_SPAWN:
6556 {
6557 location_t cilk_spawn_loc
6558 = cp_lexer_peek_token (parser->lexer)->location;
6559 cp_lexer_consume_token (parser->lexer);
6560 token = cp_lexer_peek_token (parser->lexer);
6561 if (token->type == CPP_SEMICOLON)
6562 {
6563 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6564 "an expression");
6565 postfix_expression = error_mark_node;
6566 break;
6567 }
6568 else if (!current_function_decl)
6569 {
6570 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6571 "inside a function");
6572 postfix_expression = error_mark_node;
6573 break;
6574 }
6575 else
6576 {
6577 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6578 saved_in_statement = parser->in_statement;
6579 parser->in_statement |= IN_CILK_SPAWN;
6580 }
6581 cfun->calls_cilk_spawn = 1;
6582 postfix_expression =
6583 cp_parser_postfix_expression (parser, false, false,
6584 false, false, &idk);
6585 if (!flag_cilkplus)
6586 {
6587 error_at (token->location, "-fcilkplus must be enabled to use"
6588 " %<_Cilk_spawn%>");
6589 cfun->calls_cilk_spawn = 0;
6590 }
6591 else if (saved_in_statement & IN_CILK_SPAWN)
6592 {
6593 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6594 "are not permitted");
6595 postfix_expression = error_mark_node;
6596 cfun->calls_cilk_spawn = 0;
6597 }
6598 else
6599 {
6600 location_t loc = postfix_expression.get_location ();
6601 postfix_expression = build_cilk_spawn (token->location,
6602 postfix_expression);
6603 /* Build a location of the form:
6604 _Cilk_spawn expr
6605 ~~~~~~~~~~~~^~~~
6606 with caret at the expr, ranging from the start of the
6607 _Cilk_spawn token to the end of the expression. */
6608 location_t combined_loc =
6609 make_location (loc, cilk_spawn_loc, get_finish (loc));
6610 postfix_expression.set_location (combined_loc);
6611 if (postfix_expression != error_mark_node)
6612 SET_EXPR_LOCATION (postfix_expression, input_location);
6613 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6614 }
6615 break;
6616 }
6617
6618 case RID_ADDRESSOF:
6619 case RID_BUILTIN_SHUFFLE:
6620 case RID_BUILTIN_LAUNDER:
6621 {
6622 vec<tree, va_gc> *vec;
6623 unsigned int i;
6624 tree p;
6625
6626 cp_lexer_consume_token (parser->lexer);
6627 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6628 /*cast_p=*/false, /*allow_expansion_p=*/true,
6629 /*non_constant_p=*/NULL);
6630 if (vec == NULL)
6631 {
6632 postfix_expression = error_mark_node;
6633 break;
6634 }
6635
6636 FOR_EACH_VEC_ELT (*vec, i, p)
6637 mark_exp_read (p);
6638
6639 switch (keyword)
6640 {
6641 case RID_ADDRESSOF:
6642 if (vec->length () == 1)
6643 postfix_expression
6644 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6645 else
6646 {
6647 error_at (loc, "wrong number of arguments to "
6648 "%<__builtin_addressof%>");
6649 postfix_expression = error_mark_node;
6650 }
6651 break;
6652
6653 case RID_BUILTIN_LAUNDER:
6654 if (vec->length () == 1)
6655 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6656 tf_warning_or_error);
6657 else
6658 {
6659 error_at (loc, "wrong number of arguments to "
6660 "%<__builtin_launder%>");
6661 postfix_expression = error_mark_node;
6662 }
6663 break;
6664
6665 case RID_BUILTIN_SHUFFLE:
6666 if (vec->length () == 2)
6667 postfix_expression
6668 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6669 (*vec)[1], tf_warning_or_error);
6670 else if (vec->length () == 3)
6671 postfix_expression
6672 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6673 (*vec)[2], tf_warning_or_error);
6674 else
6675 {
6676 error_at (loc, "wrong number of arguments to "
6677 "%<__builtin_shuffle%>");
6678 postfix_expression = error_mark_node;
6679 }
6680 break;
6681
6682 default:
6683 gcc_unreachable ();
6684 }
6685 break;
6686 }
6687
6688 default:
6689 {
6690 tree type;
6691
6692 /* If the next thing is a simple-type-specifier, we may be
6693 looking at a functional cast. We could also be looking at
6694 an id-expression. So, we try the functional cast, and if
6695 that doesn't work we fall back to the primary-expression. */
6696 cp_parser_parse_tentatively (parser);
6697 /* Look for the simple-type-specifier. */
6698 ++parser->prevent_constrained_type_specifiers;
6699 type = cp_parser_simple_type_specifier (parser,
6700 /*decl_specs=*/NULL,
6701 CP_PARSER_FLAGS_NONE);
6702 --parser->prevent_constrained_type_specifiers;
6703 /* Parse the cast itself. */
6704 if (!cp_parser_error_occurred (parser))
6705 postfix_expression
6706 = cp_parser_functional_cast (parser, type);
6707 /* If that worked, we're done. */
6708 if (cp_parser_parse_definitely (parser))
6709 break;
6710
6711 /* If the functional-cast didn't work out, try a
6712 compound-literal. */
6713 if (cp_parser_allow_gnu_extensions_p (parser)
6714 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6715 {
6716 cp_expr initializer = NULL_TREE;
6717
6718 cp_parser_parse_tentatively (parser);
6719
6720 /* Avoid calling cp_parser_type_id pointlessly, see comment
6721 in cp_parser_cast_expression about c++/29234. */
6722 if (!cp_parser_compound_literal_p (parser))
6723 cp_parser_simulate_error (parser);
6724 else
6725 {
6726 /* Parse the type. */
6727 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6728 parser->in_type_id_in_expr_p = true;
6729 type = cp_parser_type_id (parser);
6730 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6731 /* Look for the `)'. */
6732 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6733 }
6734
6735 /* If things aren't going well, there's no need to
6736 keep going. */
6737 if (!cp_parser_error_occurred (parser))
6738 {
6739 bool non_constant_p;
6740 /* Parse the brace-enclosed initializer list. */
6741 initializer = cp_parser_braced_list (parser,
6742 &non_constant_p);
6743 }
6744 /* If that worked, we're definitely looking at a
6745 compound-literal expression. */
6746 if (cp_parser_parse_definitely (parser))
6747 {
6748 /* Warn the user that a compound literal is not
6749 allowed in standard C++. */
6750 pedwarn (input_location, OPT_Wpedantic,
6751 "ISO C++ forbids compound-literals");
6752 /* For simplicity, we disallow compound literals in
6753 constant-expressions. We could
6754 allow compound literals of integer type, whose
6755 initializer was a constant, in constant
6756 expressions. Permitting that usage, as a further
6757 extension, would not change the meaning of any
6758 currently accepted programs. (Of course, as
6759 compound literals are not part of ISO C++, the
6760 standard has nothing to say.) */
6761 if (cp_parser_non_integral_constant_expression (parser,
6762 NIC_NCC))
6763 {
6764 postfix_expression = error_mark_node;
6765 break;
6766 }
6767 /* Form the representation of the compound-literal. */
6768 postfix_expression
6769 = finish_compound_literal (type, initializer,
6770 tf_warning_or_error);
6771 postfix_expression.set_location (initializer.get_location ());
6772 break;
6773 }
6774 }
6775
6776 /* It must be a primary-expression. */
6777 postfix_expression
6778 = cp_parser_primary_expression (parser, address_p, cast_p,
6779 /*template_arg_p=*/false,
6780 decltype_p,
6781 &idk);
6782 }
6783 break;
6784 }
6785
6786 /* Note that we don't need to worry about calling build_cplus_new on a
6787 class-valued CALL_EXPR in decltype when it isn't the end of the
6788 postfix-expression; unary_complex_lvalue will take care of that for
6789 all these cases. */
6790
6791 /* Keep looping until the postfix-expression is complete. */
6792 while (true)
6793 {
6794 if (idk == CP_ID_KIND_UNQUALIFIED
6795 && identifier_p (postfix_expression)
6796 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6797 /* It is not a Koenig lookup function call. */
6798 postfix_expression
6799 = unqualified_name_lookup_error (postfix_expression);
6800
6801 /* Peek at the next token. */
6802 token = cp_lexer_peek_token (parser->lexer);
6803
6804 switch (token->type)
6805 {
6806 case CPP_OPEN_SQUARE:
6807 if (cp_next_tokens_can_be_std_attribute_p (parser))
6808 {
6809 cp_parser_error (parser,
6810 "two consecutive %<[%> shall "
6811 "only introduce an attribute");
6812 return error_mark_node;
6813 }
6814 postfix_expression
6815 = cp_parser_postfix_open_square_expression (parser,
6816 postfix_expression,
6817 false,
6818 decltype_p);
6819 postfix_expression.set_range (start_loc,
6820 postfix_expression.get_location ());
6821
6822 idk = CP_ID_KIND_NONE;
6823 is_member_access = false;
6824 break;
6825
6826 case CPP_OPEN_PAREN:
6827 /* postfix-expression ( expression-list [opt] ) */
6828 {
6829 bool koenig_p;
6830 bool is_builtin_constant_p;
6831 bool saved_integral_constant_expression_p = false;
6832 bool saved_non_integral_constant_expression_p = false;
6833 tsubst_flags_t complain = complain_flags (decltype_p);
6834 vec<tree, va_gc> *args;
6835 location_t close_paren_loc = UNKNOWN_LOCATION;
6836
6837 is_member_access = false;
6838
6839 is_builtin_constant_p
6840 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6841 if (is_builtin_constant_p)
6842 {
6843 /* The whole point of __builtin_constant_p is to allow
6844 non-constant expressions to appear as arguments. */
6845 saved_integral_constant_expression_p
6846 = parser->integral_constant_expression_p;
6847 saved_non_integral_constant_expression_p
6848 = parser->non_integral_constant_expression_p;
6849 parser->integral_constant_expression_p = false;
6850 }
6851 args = (cp_parser_parenthesized_expression_list
6852 (parser, non_attr,
6853 /*cast_p=*/false, /*allow_expansion_p=*/true,
6854 /*non_constant_p=*/NULL,
6855 /*close_paren_loc=*/&close_paren_loc));
6856 if (is_builtin_constant_p)
6857 {
6858 parser->integral_constant_expression_p
6859 = saved_integral_constant_expression_p;
6860 parser->non_integral_constant_expression_p
6861 = saved_non_integral_constant_expression_p;
6862 }
6863
6864 if (args == NULL)
6865 {
6866 postfix_expression = error_mark_node;
6867 break;
6868 }
6869
6870 /* Function calls are not permitted in
6871 constant-expressions. */
6872 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6873 && cp_parser_non_integral_constant_expression (parser,
6874 NIC_FUNC_CALL))
6875 {
6876 postfix_expression = error_mark_node;
6877 release_tree_vector (args);
6878 break;
6879 }
6880
6881 koenig_p = false;
6882 if (idk == CP_ID_KIND_UNQUALIFIED
6883 || idk == CP_ID_KIND_TEMPLATE_ID)
6884 {
6885 if (identifier_p (postfix_expression))
6886 {
6887 if (!args->is_empty ())
6888 {
6889 koenig_p = true;
6890 if (!any_type_dependent_arguments_p (args))
6891 postfix_expression
6892 = perform_koenig_lookup (postfix_expression, args,
6893 complain);
6894 }
6895 else
6896 postfix_expression
6897 = unqualified_fn_lookup_error (postfix_expression);
6898 }
6899 /* We do not perform argument-dependent lookup if
6900 normal lookup finds a non-function, in accordance
6901 with the expected resolution of DR 218. */
6902 else if (!args->is_empty ()
6903 && is_overloaded_fn (postfix_expression))
6904 {
6905 tree fn = get_first_fn (postfix_expression);
6906 fn = STRIP_TEMPLATE (fn);
6907
6908 /* Do not do argument dependent lookup if regular
6909 lookup finds a member function or a block-scope
6910 function declaration. [basic.lookup.argdep]/3 */
6911 if (!DECL_FUNCTION_MEMBER_P (fn)
6912 && !DECL_LOCAL_FUNCTION_P (fn))
6913 {
6914 koenig_p = true;
6915 if (!any_type_dependent_arguments_p (args))
6916 postfix_expression
6917 = perform_koenig_lookup (postfix_expression, args,
6918 complain);
6919 }
6920 }
6921 }
6922
6923 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6924 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6925 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6926 && vec_safe_length (args) == 3)
6927 {
6928 tree arg0 = (*args)[0];
6929 tree arg1 = (*args)[1];
6930 tree arg2 = (*args)[2];
6931 int literal_mask = ((!!integer_zerop (arg1) << 1)
6932 | (!!integer_zerop (arg2) << 2));
6933 if (TREE_CODE (arg2) == CONST_DECL)
6934 arg2 = DECL_INITIAL (arg2);
6935 warn_for_memset (input_location, arg0, arg2, literal_mask);
6936 }
6937
6938 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6939 {
6940 tree instance = TREE_OPERAND (postfix_expression, 0);
6941 tree fn = TREE_OPERAND (postfix_expression, 1);
6942
6943 if (processing_template_decl
6944 && (type_dependent_object_expression_p (instance)
6945 || (!BASELINK_P (fn)
6946 && TREE_CODE (fn) != FIELD_DECL)
6947 || type_dependent_expression_p (fn)
6948 || any_type_dependent_arguments_p (args)))
6949 {
6950 maybe_generic_this_capture (instance, fn);
6951 postfix_expression
6952 = build_nt_call_vec (postfix_expression, args);
6953 release_tree_vector (args);
6954 break;
6955 }
6956
6957 if (BASELINK_P (fn))
6958 {
6959 postfix_expression
6960 = (build_new_method_call
6961 (instance, fn, &args, NULL_TREE,
6962 (idk == CP_ID_KIND_QUALIFIED
6963 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6964 : LOOKUP_NORMAL),
6965 /*fn_p=*/NULL,
6966 complain));
6967 }
6968 else
6969 postfix_expression
6970 = finish_call_expr (postfix_expression, &args,
6971 /*disallow_virtual=*/false,
6972 /*koenig_p=*/false,
6973 complain);
6974 }
6975 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6976 || TREE_CODE (postfix_expression) == MEMBER_REF
6977 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6978 postfix_expression = (build_offset_ref_call_from_tree
6979 (postfix_expression, &args,
6980 complain));
6981 else if (idk == CP_ID_KIND_QUALIFIED)
6982 /* A call to a static class member, or a namespace-scope
6983 function. */
6984 postfix_expression
6985 = finish_call_expr (postfix_expression, &args,
6986 /*disallow_virtual=*/true,
6987 koenig_p,
6988 complain);
6989 else
6990 /* All other function calls. */
6991 postfix_expression
6992 = finish_call_expr (postfix_expression, &args,
6993 /*disallow_virtual=*/false,
6994 koenig_p,
6995 complain);
6996
6997 if (close_paren_loc != UNKNOWN_LOCATION)
6998 {
6999 location_t combined_loc = make_location (token->location,
7000 start_loc,
7001 close_paren_loc);
7002 postfix_expression.set_location (combined_loc);
7003 }
7004
7005 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7006 idk = CP_ID_KIND_NONE;
7007
7008 release_tree_vector (args);
7009 }
7010 break;
7011
7012 case CPP_DOT:
7013 case CPP_DEREF:
7014 /* postfix-expression . template [opt] id-expression
7015 postfix-expression . pseudo-destructor-name
7016 postfix-expression -> template [opt] id-expression
7017 postfix-expression -> pseudo-destructor-name */
7018
7019 /* Consume the `.' or `->' operator. */
7020 cp_lexer_consume_token (parser->lexer);
7021
7022 postfix_expression
7023 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7024 postfix_expression,
7025 false, &idk, loc);
7026
7027 is_member_access = true;
7028 break;
7029
7030 case CPP_PLUS_PLUS:
7031 /* postfix-expression ++ */
7032 /* Consume the `++' token. */
7033 cp_lexer_consume_token (parser->lexer);
7034 /* Generate a representation for the complete expression. */
7035 postfix_expression
7036 = finish_increment_expr (postfix_expression,
7037 POSTINCREMENT_EXPR);
7038 /* Increments may not appear in constant-expressions. */
7039 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7040 postfix_expression = error_mark_node;
7041 idk = CP_ID_KIND_NONE;
7042 is_member_access = false;
7043 break;
7044
7045 case CPP_MINUS_MINUS:
7046 /* postfix-expression -- */
7047 /* Consume the `--' token. */
7048 cp_lexer_consume_token (parser->lexer);
7049 /* Generate a representation for the complete expression. */
7050 postfix_expression
7051 = finish_increment_expr (postfix_expression,
7052 POSTDECREMENT_EXPR);
7053 /* Decrements may not appear in constant-expressions. */
7054 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7055 postfix_expression = error_mark_node;
7056 idk = CP_ID_KIND_NONE;
7057 is_member_access = false;
7058 break;
7059
7060 default:
7061 if (pidk_return != NULL)
7062 * pidk_return = idk;
7063 if (member_access_only_p)
7064 return is_member_access
7065 ? postfix_expression
7066 : cp_expr (error_mark_node);
7067 else
7068 return postfix_expression;
7069 }
7070 }
7071
7072 /* We should never get here. */
7073 gcc_unreachable ();
7074 return error_mark_node;
7075 }
7076
7077 /* This function parses Cilk Plus array notations. If a normal array expr. is
7078 parsed then the array index is passed back to the caller through *INIT_INDEX
7079 and the function returns a NULL_TREE. If array notation expr. is parsed,
7080 then *INIT_INDEX is ignored by the caller and the function returns
7081 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7082 error_mark_node. */
7083
7084 static tree
7085 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7086 tree array_value)
7087 {
7088 cp_token *token = NULL;
7089 tree length_index, stride = NULL_TREE, value_tree, array_type;
7090 if (!array_value || array_value == error_mark_node)
7091 {
7092 cp_parser_skip_to_end_of_statement (parser);
7093 return error_mark_node;
7094 }
7095
7096 array_type = TREE_TYPE (array_value);
7097
7098 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7099 parser->colon_corrects_to_scope_p = false;
7100 token = cp_lexer_peek_token (parser->lexer);
7101
7102 if (!token)
7103 {
7104 cp_parser_error (parser, "expected %<:%> or numeral");
7105 return error_mark_node;
7106 }
7107 else if (token->type == CPP_COLON)
7108 {
7109 /* Consume the ':'. */
7110 cp_lexer_consume_token (parser->lexer);
7111
7112 /* If we are here, then we have a case like this A[:]. */
7113 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7114 {
7115 cp_parser_error (parser, "expected %<]%>");
7116 cp_parser_skip_to_end_of_statement (parser);
7117 return error_mark_node;
7118 }
7119 *init_index = NULL_TREE;
7120 stride = NULL_TREE;
7121 length_index = NULL_TREE;
7122 }
7123 else
7124 {
7125 /* If we are here, then there are three valid possibilities:
7126 1. ARRAY [ EXP ]
7127 2. ARRAY [ EXP : EXP ]
7128 3. ARRAY [ EXP : EXP : EXP ] */
7129
7130 *init_index = cp_parser_expression (parser);
7131 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7132 {
7133 /* This indicates that we have a normal array expression. */
7134 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7135 return NULL_TREE;
7136 }
7137
7138 /* Consume the ':'. */
7139 cp_lexer_consume_token (parser->lexer);
7140 length_index = cp_parser_expression (parser);
7141 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7142 {
7143 cp_lexer_consume_token (parser->lexer);
7144 stride = cp_parser_expression (parser);
7145 }
7146 }
7147 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7148
7149 if (*init_index == error_mark_node || length_index == error_mark_node
7150 || stride == error_mark_node || array_type == error_mark_node)
7151 {
7152 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7153 cp_lexer_consume_token (parser->lexer);
7154 return error_mark_node;
7155 }
7156 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7157
7158 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7159 length_index, stride, array_type);
7160 return value_tree;
7161 }
7162
7163 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7164 by cp_parser_builtin_offsetof. We're looking for
7165
7166 postfix-expression [ expression ]
7167 postfix-expression [ braced-init-list ] (C++11)
7168
7169 FOR_OFFSETOF is set if we're being called in that context, which
7170 changes how we deal with integer constant expressions. */
7171
7172 static tree
7173 cp_parser_postfix_open_square_expression (cp_parser *parser,
7174 tree postfix_expression,
7175 bool for_offsetof,
7176 bool decltype_p)
7177 {
7178 tree index = NULL_TREE;
7179 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7180 bool saved_greater_than_is_operator_p;
7181
7182 /* Consume the `[' token. */
7183 cp_lexer_consume_token (parser->lexer);
7184
7185 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7186 parser->greater_than_is_operator_p = true;
7187
7188 /* Parse the index expression. */
7189 /* ??? For offsetof, there is a question of what to allow here. If
7190 offsetof is not being used in an integral constant expression context,
7191 then we *could* get the right answer by computing the value at runtime.
7192 If we are in an integral constant expression context, then we might
7193 could accept any constant expression; hard to say without analysis.
7194 Rather than open the barn door too wide right away, allow only integer
7195 constant expressions here. */
7196 if (for_offsetof)
7197 index = cp_parser_constant_expression (parser);
7198 else
7199 {
7200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7201 {
7202 bool expr_nonconst_p;
7203 cp_lexer_set_source_position (parser->lexer);
7204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7205 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7206 if (flag_cilkplus
7207 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7208 {
7209 error_at (cp_lexer_peek_token (parser->lexer)->location,
7210 "braced list index is not allowed with array "
7211 "notation");
7212 cp_parser_skip_to_end_of_statement (parser);
7213 return error_mark_node;
7214 }
7215 }
7216 else if (flag_cilkplus)
7217 {
7218 /* Here are have these two options:
7219 ARRAY[EXP : EXP] - Array notation expr with default
7220 stride of 1.
7221 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7222 stride. */
7223 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7224 postfix_expression);
7225 if (an_exp)
7226 return an_exp;
7227 }
7228 else
7229 index = cp_parser_expression (parser);
7230 }
7231
7232 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7233
7234 /* Look for the closing `]'. */
7235 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7236
7237 /* Build the ARRAY_REF. */
7238 postfix_expression = grok_array_decl (loc, postfix_expression,
7239 index, decltype_p);
7240
7241 /* When not doing offsetof, array references are not permitted in
7242 constant-expressions. */
7243 if (!for_offsetof
7244 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7245 postfix_expression = error_mark_node;
7246
7247 return postfix_expression;
7248 }
7249
7250 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7251 by cp_parser_builtin_offsetof. We're looking for
7252
7253 postfix-expression . template [opt] id-expression
7254 postfix-expression . pseudo-destructor-name
7255 postfix-expression -> template [opt] id-expression
7256 postfix-expression -> pseudo-destructor-name
7257
7258 FOR_OFFSETOF is set if we're being called in that context. That sorta
7259 limits what of the above we'll actually accept, but nevermind.
7260 TOKEN_TYPE is the "." or "->" token, which will already have been
7261 removed from the stream. */
7262
7263 static tree
7264 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7265 enum cpp_ttype token_type,
7266 cp_expr postfix_expression,
7267 bool for_offsetof, cp_id_kind *idk,
7268 location_t location)
7269 {
7270 tree name;
7271 bool dependent_p;
7272 bool pseudo_destructor_p;
7273 tree scope = NULL_TREE;
7274 location_t start_loc = postfix_expression.get_start ();
7275
7276 /* If this is a `->' operator, dereference the pointer. */
7277 if (token_type == CPP_DEREF)
7278 postfix_expression = build_x_arrow (location, postfix_expression,
7279 tf_warning_or_error);
7280 /* Check to see whether or not the expression is type-dependent and
7281 not the current instantiation. */
7282 dependent_p = type_dependent_object_expression_p (postfix_expression);
7283 /* The identifier following the `->' or `.' is not qualified. */
7284 parser->scope = NULL_TREE;
7285 parser->qualifying_scope = NULL_TREE;
7286 parser->object_scope = NULL_TREE;
7287 *idk = CP_ID_KIND_NONE;
7288
7289 /* Enter the scope corresponding to the type of the object
7290 given by the POSTFIX_EXPRESSION. */
7291 if (!dependent_p)
7292 {
7293 scope = TREE_TYPE (postfix_expression);
7294 /* According to the standard, no expression should ever have
7295 reference type. Unfortunately, we do not currently match
7296 the standard in this respect in that our internal representation
7297 of an expression may have reference type even when the standard
7298 says it does not. Therefore, we have to manually obtain the
7299 underlying type here. */
7300 scope = non_reference (scope);
7301 /* The type of the POSTFIX_EXPRESSION must be complete. */
7302 /* Unlike the object expression in other contexts, *this is not
7303 required to be of complete type for purposes of class member
7304 access (5.2.5) outside the member function body. */
7305 if (postfix_expression != current_class_ref
7306 && scope != error_mark_node
7307 && !(processing_template_decl
7308 && current_class_type
7309 && (same_type_ignoring_top_level_qualifiers_p
7310 (scope, current_class_type))))
7311 {
7312 scope = complete_type (scope);
7313 if (!COMPLETE_TYPE_P (scope)
7314 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7315 && EXPR_P (postfix_expression))
7316 {
7317 /* In a template, be permissive by treating an object expression
7318 of incomplete type as dependent (after a pedwarn). */
7319 diagnostic_t kind = (processing_template_decl
7320 ? DK_PEDWARN
7321 : DK_ERROR);
7322 cxx_incomplete_type_diagnostic
7323 (location_of (postfix_expression),
7324 postfix_expression, scope, kind);
7325 if (processing_template_decl)
7326 {
7327 dependent_p = true;
7328 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7329 }
7330 }
7331 }
7332
7333 if (!dependent_p)
7334 {
7335 /* Let the name lookup machinery know that we are processing a
7336 class member access expression. */
7337 parser->context->object_type = scope;
7338 /* If something went wrong, we want to be able to discern that case,
7339 as opposed to the case where there was no SCOPE due to the type
7340 of expression being dependent. */
7341 if (!scope)
7342 scope = error_mark_node;
7343 /* If the SCOPE was erroneous, make the various semantic analysis
7344 functions exit quickly -- and without issuing additional error
7345 messages. */
7346 if (scope == error_mark_node)
7347 postfix_expression = error_mark_node;
7348 }
7349 }
7350
7351 if (dependent_p)
7352 /* Tell cp_parser_lookup_name that there was an object, even though it's
7353 type-dependent. */
7354 parser->context->object_type = unknown_type_node;
7355
7356 /* Assume this expression is not a pseudo-destructor access. */
7357 pseudo_destructor_p = false;
7358
7359 /* If the SCOPE is a scalar type, then, if this is a valid program,
7360 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7361 is type dependent, it can be pseudo-destructor-name or something else.
7362 Try to parse it as pseudo-destructor-name first. */
7363 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7364 {
7365 tree s;
7366 tree type;
7367
7368 cp_parser_parse_tentatively (parser);
7369 /* Parse the pseudo-destructor-name. */
7370 s = NULL_TREE;
7371 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7372 &s, &type);
7373 if (dependent_p
7374 && (cp_parser_error_occurred (parser)
7375 || !SCALAR_TYPE_P (type)))
7376 cp_parser_abort_tentative_parse (parser);
7377 else if (cp_parser_parse_definitely (parser))
7378 {
7379 pseudo_destructor_p = true;
7380 postfix_expression
7381 = finish_pseudo_destructor_expr (postfix_expression,
7382 s, type, location);
7383 }
7384 }
7385
7386 if (!pseudo_destructor_p)
7387 {
7388 /* If the SCOPE is not a scalar type, we are looking at an
7389 ordinary class member access expression, rather than a
7390 pseudo-destructor-name. */
7391 bool template_p;
7392 cp_token *token = cp_lexer_peek_token (parser->lexer);
7393 /* Parse the id-expression. */
7394 name = (cp_parser_id_expression
7395 (parser,
7396 cp_parser_optional_template_keyword (parser),
7397 /*check_dependency_p=*/true,
7398 &template_p,
7399 /*declarator_p=*/false,
7400 /*optional_p=*/false));
7401 /* In general, build a SCOPE_REF if the member name is qualified.
7402 However, if the name was not dependent and has already been
7403 resolved; there is no need to build the SCOPE_REF. For example;
7404
7405 struct X { void f(); };
7406 template <typename T> void f(T* t) { t->X::f(); }
7407
7408 Even though "t" is dependent, "X::f" is not and has been resolved
7409 to a BASELINK; there is no need to include scope information. */
7410
7411 /* But we do need to remember that there was an explicit scope for
7412 virtual function calls. */
7413 if (parser->scope)
7414 *idk = CP_ID_KIND_QUALIFIED;
7415
7416 /* If the name is a template-id that names a type, we will get a
7417 TYPE_DECL here. That is invalid code. */
7418 if (TREE_CODE (name) == TYPE_DECL)
7419 {
7420 error_at (token->location, "invalid use of %qD", name);
7421 postfix_expression = error_mark_node;
7422 }
7423 else
7424 {
7425 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7426 {
7427 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7428 {
7429 error_at (token->location, "%<%D::%D%> is not a class member",
7430 parser->scope, name);
7431 postfix_expression = error_mark_node;
7432 }
7433 else
7434 name = build_qualified_name (/*type=*/NULL_TREE,
7435 parser->scope,
7436 name,
7437 template_p);
7438 parser->scope = NULL_TREE;
7439 parser->qualifying_scope = NULL_TREE;
7440 parser->object_scope = NULL_TREE;
7441 }
7442 if (parser->scope && name && BASELINK_P (name))
7443 adjust_result_of_qualified_name_lookup
7444 (name, parser->scope, scope);
7445 postfix_expression
7446 = finish_class_member_access_expr (postfix_expression, name,
7447 template_p,
7448 tf_warning_or_error);
7449 /* Build a location e.g.:
7450 ptr->access_expr
7451 ~~~^~~~~~~~~~~~~
7452 where the caret is at the deref token, ranging from
7453 the start of postfix_expression to the end of the access expr. */
7454 location_t end_loc
7455 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7456 location_t combined_loc
7457 = make_location (input_location, start_loc, end_loc);
7458 protected_set_expr_location (postfix_expression, combined_loc);
7459 }
7460 }
7461
7462 /* We no longer need to look up names in the scope of the object on
7463 the left-hand side of the `.' or `->' operator. */
7464 parser->context->object_type = NULL_TREE;
7465
7466 /* Outside of offsetof, these operators may not appear in
7467 constant-expressions. */
7468 if (!for_offsetof
7469 && (cp_parser_non_integral_constant_expression
7470 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7471 postfix_expression = error_mark_node;
7472
7473 return postfix_expression;
7474 }
7475
7476 /* Parse a parenthesized expression-list.
7477
7478 expression-list:
7479 assignment-expression
7480 expression-list, assignment-expression
7481
7482 attribute-list:
7483 expression-list
7484 identifier
7485 identifier, expression-list
7486
7487 CAST_P is true if this expression is the target of a cast.
7488
7489 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7490 argument pack.
7491
7492 Returns a vector of trees. Each element is a representation of an
7493 assignment-expression. NULL is returned if the ( and or ) are
7494 missing. An empty, but allocated, vector is returned on no
7495 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7496 if we are parsing an attribute list for an attribute that wants a
7497 plain identifier argument, normal_attr for an attribute that wants
7498 an expression, or non_attr if we aren't parsing an attribute list. If
7499 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7500 not all of the expressions in the list were constant.
7501 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7502 will be written to with the location of the closing parenthesis. If
7503 an error occurs, it may or may not be written to. */
7504
7505 static vec<tree, va_gc> *
7506 cp_parser_parenthesized_expression_list (cp_parser* parser,
7507 int is_attribute_list,
7508 bool cast_p,
7509 bool allow_expansion_p,
7510 bool *non_constant_p,
7511 location_t *close_paren_loc)
7512 {
7513 vec<tree, va_gc> *expression_list;
7514 bool fold_expr_p = is_attribute_list != non_attr;
7515 tree identifier = NULL_TREE;
7516 bool saved_greater_than_is_operator_p;
7517
7518 /* Assume all the expressions will be constant. */
7519 if (non_constant_p)
7520 *non_constant_p = false;
7521
7522 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7523 return NULL;
7524
7525 expression_list = make_tree_vector ();
7526
7527 /* Within a parenthesized expression, a `>' token is always
7528 the greater-than operator. */
7529 saved_greater_than_is_operator_p
7530 = parser->greater_than_is_operator_p;
7531 parser->greater_than_is_operator_p = true;
7532
7533 /* Consume expressions until there are no more. */
7534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7535 while (true)
7536 {
7537 tree expr;
7538
7539 /* At the beginning of attribute lists, check to see if the
7540 next token is an identifier. */
7541 if (is_attribute_list == id_attr
7542 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7543 {
7544 cp_token *token;
7545
7546 /* Consume the identifier. */
7547 token = cp_lexer_consume_token (parser->lexer);
7548 /* Save the identifier. */
7549 identifier = token->u.value;
7550 }
7551 else
7552 {
7553 bool expr_non_constant_p;
7554
7555 /* Parse the next assignment-expression. */
7556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7557 {
7558 /* A braced-init-list. */
7559 cp_lexer_set_source_position (parser->lexer);
7560 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7561 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7562 if (non_constant_p && expr_non_constant_p)
7563 *non_constant_p = true;
7564 }
7565 else if (non_constant_p)
7566 {
7567 expr = (cp_parser_constant_expression
7568 (parser, /*allow_non_constant_p=*/true,
7569 &expr_non_constant_p));
7570 if (expr_non_constant_p)
7571 *non_constant_p = true;
7572 }
7573 else
7574 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7575 cast_p);
7576
7577 if (fold_expr_p)
7578 expr = instantiate_non_dependent_expr (expr);
7579
7580 /* If we have an ellipsis, then this is an expression
7581 expansion. */
7582 if (allow_expansion_p
7583 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7584 {
7585 /* Consume the `...'. */
7586 cp_lexer_consume_token (parser->lexer);
7587
7588 /* Build the argument pack. */
7589 expr = make_pack_expansion (expr);
7590 }
7591
7592 /* Add it to the list. We add error_mark_node
7593 expressions to the list, so that we can still tell if
7594 the correct form for a parenthesized expression-list
7595 is found. That gives better errors. */
7596 vec_safe_push (expression_list, expr);
7597
7598 if (expr == error_mark_node)
7599 goto skip_comma;
7600 }
7601
7602 /* After the first item, attribute lists look the same as
7603 expression lists. */
7604 is_attribute_list = non_attr;
7605
7606 get_comma:;
7607 /* If the next token isn't a `,', then we are done. */
7608 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7609 break;
7610
7611 /* Otherwise, consume the `,' and keep going. */
7612 cp_lexer_consume_token (parser->lexer);
7613 }
7614
7615 if (close_paren_loc)
7616 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7617
7618 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7619 {
7620 int ending;
7621
7622 skip_comma:;
7623 /* We try and resync to an unnested comma, as that will give the
7624 user better diagnostics. */
7625 ending = cp_parser_skip_to_closing_parenthesis (parser,
7626 /*recovering=*/true,
7627 /*or_comma=*/true,
7628 /*consume_paren=*/true);
7629 if (ending < 0)
7630 goto get_comma;
7631 if (!ending)
7632 {
7633 parser->greater_than_is_operator_p
7634 = saved_greater_than_is_operator_p;
7635 return NULL;
7636 }
7637 }
7638
7639 parser->greater_than_is_operator_p
7640 = saved_greater_than_is_operator_p;
7641
7642 if (identifier)
7643 vec_safe_insert (expression_list, 0, identifier);
7644
7645 return expression_list;
7646 }
7647
7648 /* Parse a pseudo-destructor-name.
7649
7650 pseudo-destructor-name:
7651 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7652 :: [opt] nested-name-specifier template template-id :: ~ type-name
7653 :: [opt] nested-name-specifier [opt] ~ type-name
7654
7655 If either of the first two productions is used, sets *SCOPE to the
7656 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7657 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7658 or ERROR_MARK_NODE if the parse fails. */
7659
7660 static void
7661 cp_parser_pseudo_destructor_name (cp_parser* parser,
7662 tree object,
7663 tree* scope,
7664 tree* type)
7665 {
7666 bool nested_name_specifier_p;
7667
7668 /* Handle ~auto. */
7669 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7670 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7671 && !type_dependent_expression_p (object))
7672 {
7673 if (cxx_dialect < cxx14)
7674 pedwarn (input_location, 0,
7675 "%<~auto%> only available with "
7676 "-std=c++14 or -std=gnu++14");
7677 cp_lexer_consume_token (parser->lexer);
7678 cp_lexer_consume_token (parser->lexer);
7679 *scope = NULL_TREE;
7680 *type = TREE_TYPE (object);
7681 return;
7682 }
7683
7684 /* Assume that things will not work out. */
7685 *type = error_mark_node;
7686
7687 /* Look for the optional `::' operator. */
7688 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7689 /* Look for the optional nested-name-specifier. */
7690 nested_name_specifier_p
7691 = (cp_parser_nested_name_specifier_opt (parser,
7692 /*typename_keyword_p=*/false,
7693 /*check_dependency_p=*/true,
7694 /*type_p=*/false,
7695 /*is_declaration=*/false)
7696 != NULL_TREE);
7697 /* Now, if we saw a nested-name-specifier, we might be doing the
7698 second production. */
7699 if (nested_name_specifier_p
7700 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7701 {
7702 /* Consume the `template' keyword. */
7703 cp_lexer_consume_token (parser->lexer);
7704 /* Parse the template-id. */
7705 cp_parser_template_id (parser,
7706 /*template_keyword_p=*/true,
7707 /*check_dependency_p=*/false,
7708 class_type,
7709 /*is_declaration=*/true);
7710 /* Look for the `::' token. */
7711 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7712 }
7713 /* If the next token is not a `~', then there might be some
7714 additional qualification. */
7715 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7716 {
7717 /* At this point, we're looking for "type-name :: ~". The type-name
7718 must not be a class-name, since this is a pseudo-destructor. So,
7719 it must be either an enum-name, or a typedef-name -- both of which
7720 are just identifiers. So, we peek ahead to check that the "::"
7721 and "~" tokens are present; if they are not, then we can avoid
7722 calling type_name. */
7723 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7724 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7725 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7726 {
7727 cp_parser_error (parser, "non-scalar type");
7728 return;
7729 }
7730
7731 /* Look for the type-name. */
7732 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7733 if (*scope == error_mark_node)
7734 return;
7735
7736 /* Look for the `::' token. */
7737 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7738 }
7739 else
7740 *scope = NULL_TREE;
7741
7742 /* Look for the `~'. */
7743 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7744
7745 /* Once we see the ~, this has to be a pseudo-destructor. */
7746 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7747 cp_parser_commit_to_topmost_tentative_parse (parser);
7748
7749 /* Look for the type-name again. We are not responsible for
7750 checking that it matches the first type-name. */
7751 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7752 }
7753
7754 /* Parse a unary-expression.
7755
7756 unary-expression:
7757 postfix-expression
7758 ++ cast-expression
7759 -- cast-expression
7760 unary-operator cast-expression
7761 sizeof unary-expression
7762 sizeof ( type-id )
7763 alignof ( type-id ) [C++0x]
7764 new-expression
7765 delete-expression
7766
7767 GNU Extensions:
7768
7769 unary-expression:
7770 __extension__ cast-expression
7771 __alignof__ unary-expression
7772 __alignof__ ( type-id )
7773 alignof unary-expression [C++0x]
7774 __real__ cast-expression
7775 __imag__ cast-expression
7776 && identifier
7777 sizeof ( type-id ) { initializer-list , [opt] }
7778 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7779 __alignof__ ( type-id ) { initializer-list , [opt] }
7780
7781 ADDRESS_P is true iff the unary-expression is appearing as the
7782 operand of the `&' operator. CAST_P is true if this expression is
7783 the target of a cast.
7784
7785 Returns a representation of the expression. */
7786
7787 static cp_expr
7788 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7789 bool address_p, bool cast_p, bool decltype_p)
7790 {
7791 cp_token *token;
7792 enum tree_code unary_operator;
7793
7794 /* Peek at the next token. */
7795 token = cp_lexer_peek_token (parser->lexer);
7796 /* Some keywords give away the kind of expression. */
7797 if (token->type == CPP_KEYWORD)
7798 {
7799 enum rid keyword = token->keyword;
7800
7801 switch (keyword)
7802 {
7803 case RID_ALIGNOF:
7804 case RID_SIZEOF:
7805 {
7806 tree operand, ret;
7807 enum tree_code op;
7808 location_t first_loc;
7809
7810 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7811 /* Consume the token. */
7812 cp_lexer_consume_token (parser->lexer);
7813 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7814 /* Parse the operand. */
7815 operand = cp_parser_sizeof_operand (parser, keyword);
7816
7817 if (TYPE_P (operand))
7818 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7819 else
7820 {
7821 /* ISO C++ defines alignof only with types, not with
7822 expressions. So pedwarn if alignof is used with a non-
7823 type expression. However, __alignof__ is ok. */
7824 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7825 pedwarn (token->location, OPT_Wpedantic,
7826 "ISO C++ does not allow %<alignof%> "
7827 "with a non-type");
7828
7829 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7830 }
7831 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7832 SIZEOF_EXPR with the original operand. */
7833 if (op == SIZEOF_EXPR && ret != error_mark_node)
7834 {
7835 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7836 {
7837 if (!processing_template_decl && TYPE_P (operand))
7838 {
7839 ret = build_min (SIZEOF_EXPR, size_type_node,
7840 build1 (NOP_EXPR, operand,
7841 error_mark_node));
7842 SIZEOF_EXPR_TYPE_P (ret) = 1;
7843 }
7844 else
7845 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7846 TREE_SIDE_EFFECTS (ret) = 0;
7847 TREE_READONLY (ret) = 1;
7848 }
7849 SET_EXPR_LOCATION (ret, first_loc);
7850 }
7851 return ret;
7852 }
7853
7854 case RID_NEW:
7855 return cp_parser_new_expression (parser);
7856
7857 case RID_DELETE:
7858 return cp_parser_delete_expression (parser);
7859
7860 case RID_EXTENSION:
7861 {
7862 /* The saved value of the PEDANTIC flag. */
7863 int saved_pedantic;
7864 tree expr;
7865
7866 /* Save away the PEDANTIC flag. */
7867 cp_parser_extension_opt (parser, &saved_pedantic);
7868 /* Parse the cast-expression. */
7869 expr = cp_parser_simple_cast_expression (parser);
7870 /* Restore the PEDANTIC flag. */
7871 pedantic = saved_pedantic;
7872
7873 return expr;
7874 }
7875
7876 case RID_REALPART:
7877 case RID_IMAGPART:
7878 {
7879 tree expression;
7880
7881 /* Consume the `__real__' or `__imag__' token. */
7882 cp_lexer_consume_token (parser->lexer);
7883 /* Parse the cast-expression. */
7884 expression = cp_parser_simple_cast_expression (parser);
7885 /* Create the complete representation. */
7886 return build_x_unary_op (token->location,
7887 (keyword == RID_REALPART
7888 ? REALPART_EXPR : IMAGPART_EXPR),
7889 expression,
7890 tf_warning_or_error);
7891 }
7892 break;
7893
7894 case RID_TRANSACTION_ATOMIC:
7895 case RID_TRANSACTION_RELAXED:
7896 return cp_parser_transaction_expression (parser, keyword);
7897
7898 case RID_NOEXCEPT:
7899 {
7900 tree expr;
7901 const char *saved_message;
7902 bool saved_integral_constant_expression_p;
7903 bool saved_non_integral_constant_expression_p;
7904 bool saved_greater_than_is_operator_p;
7905
7906 cp_lexer_consume_token (parser->lexer);
7907 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7908
7909 saved_message = parser->type_definition_forbidden_message;
7910 parser->type_definition_forbidden_message
7911 = G_("types may not be defined in %<noexcept%> expressions");
7912
7913 saved_integral_constant_expression_p
7914 = parser->integral_constant_expression_p;
7915 saved_non_integral_constant_expression_p
7916 = parser->non_integral_constant_expression_p;
7917 parser->integral_constant_expression_p = false;
7918
7919 saved_greater_than_is_operator_p
7920 = parser->greater_than_is_operator_p;
7921 parser->greater_than_is_operator_p = true;
7922
7923 ++cp_unevaluated_operand;
7924 ++c_inhibit_evaluation_warnings;
7925 ++cp_noexcept_operand;
7926 expr = cp_parser_expression (parser);
7927 --cp_noexcept_operand;
7928 --c_inhibit_evaluation_warnings;
7929 --cp_unevaluated_operand;
7930
7931 parser->greater_than_is_operator_p
7932 = saved_greater_than_is_operator_p;
7933
7934 parser->integral_constant_expression_p
7935 = saved_integral_constant_expression_p;
7936 parser->non_integral_constant_expression_p
7937 = saved_non_integral_constant_expression_p;
7938
7939 parser->type_definition_forbidden_message = saved_message;
7940
7941 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7942 return finish_noexcept_expr (expr, tf_warning_or_error);
7943 }
7944
7945 default:
7946 break;
7947 }
7948 }
7949
7950 /* Look for the `:: new' and `:: delete', which also signal the
7951 beginning of a new-expression, or delete-expression,
7952 respectively. If the next token is `::', then it might be one of
7953 these. */
7954 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7955 {
7956 enum rid keyword;
7957
7958 /* See if the token after the `::' is one of the keywords in
7959 which we're interested. */
7960 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7961 /* If it's `new', we have a new-expression. */
7962 if (keyword == RID_NEW)
7963 return cp_parser_new_expression (parser);
7964 /* Similarly, for `delete'. */
7965 else if (keyword == RID_DELETE)
7966 return cp_parser_delete_expression (parser);
7967 }
7968
7969 /* Look for a unary operator. */
7970 unary_operator = cp_parser_unary_operator (token);
7971 /* The `++' and `--' operators can be handled similarly, even though
7972 they are not technically unary-operators in the grammar. */
7973 if (unary_operator == ERROR_MARK)
7974 {
7975 if (token->type == CPP_PLUS_PLUS)
7976 unary_operator = PREINCREMENT_EXPR;
7977 else if (token->type == CPP_MINUS_MINUS)
7978 unary_operator = PREDECREMENT_EXPR;
7979 /* Handle the GNU address-of-label extension. */
7980 else if (cp_parser_allow_gnu_extensions_p (parser)
7981 && token->type == CPP_AND_AND)
7982 {
7983 tree identifier;
7984 tree expression;
7985 location_t start_loc = token->location;
7986
7987 /* Consume the '&&' token. */
7988 cp_lexer_consume_token (parser->lexer);
7989 /* Look for the identifier. */
7990 location_t finish_loc
7991 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7992 identifier = cp_parser_identifier (parser);
7993 /* Construct a location of the form:
7994 &&label
7995 ^~~~~~~
7996 with caret==start at the "&&", finish at the end of the label. */
7997 location_t combined_loc
7998 = make_location (start_loc, start_loc, finish_loc);
7999 /* Create an expression representing the address. */
8000 expression = finish_label_address_expr (identifier, combined_loc);
8001 if (cp_parser_non_integral_constant_expression (parser,
8002 NIC_ADDR_LABEL))
8003 expression = error_mark_node;
8004 return expression;
8005 }
8006 }
8007 if (unary_operator != ERROR_MARK)
8008 {
8009 cp_expr cast_expression;
8010 cp_expr expression = error_mark_node;
8011 non_integral_constant non_constant_p = NIC_NONE;
8012 location_t loc = token->location;
8013 tsubst_flags_t complain = complain_flags (decltype_p);
8014
8015 /* Consume the operator token. */
8016 token = cp_lexer_consume_token (parser->lexer);
8017 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8018
8019 /* Parse the cast-expression. */
8020 cast_expression
8021 = cp_parser_cast_expression (parser,
8022 unary_operator == ADDR_EXPR,
8023 /*cast_p=*/false,
8024 /*decltype*/false,
8025 pidk);
8026
8027 /* Make a location:
8028 OP_TOKEN CAST_EXPRESSION
8029 ^~~~~~~~~~~~~~~~~~~~~~~~~
8030 with start==caret at the operator token, and
8031 extending to the end of the cast_expression. */
8032 loc = make_location (loc, loc, cast_expression.get_finish ());
8033
8034 /* Now, build an appropriate representation. */
8035 switch (unary_operator)
8036 {
8037 case INDIRECT_REF:
8038 non_constant_p = NIC_STAR;
8039 expression = build_x_indirect_ref (loc, cast_expression,
8040 RO_UNARY_STAR,
8041 complain);
8042 /* TODO: build_x_indirect_ref does not always honor the
8043 location, so ensure it is set. */
8044 expression.set_location (loc);
8045 break;
8046
8047 case ADDR_EXPR:
8048 non_constant_p = NIC_ADDR;
8049 /* Fall through. */
8050 case BIT_NOT_EXPR:
8051 expression = build_x_unary_op (loc, unary_operator,
8052 cast_expression,
8053 complain);
8054 /* TODO: build_x_unary_op does not always honor the location,
8055 so ensure it is set. */
8056 expression.set_location (loc);
8057 break;
8058
8059 case PREINCREMENT_EXPR:
8060 case PREDECREMENT_EXPR:
8061 non_constant_p = unary_operator == PREINCREMENT_EXPR
8062 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8063 /* Fall through. */
8064 case NEGATE_EXPR:
8065 /* Immediately fold negation of a constant, unless the constant is 0
8066 (since -0 == 0) or it would overflow. */
8067 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8068 && CONSTANT_CLASS_P (cast_expression)
8069 && !integer_zerop (cast_expression)
8070 && !TREE_OVERFLOW (cast_expression))
8071 {
8072 tree folded = fold_build1 (unary_operator,
8073 TREE_TYPE (cast_expression),
8074 cast_expression);
8075 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8076 {
8077 expression = cp_expr (folded, loc);
8078 break;
8079 }
8080 }
8081 /* Fall through. */
8082 case UNARY_PLUS_EXPR:
8083 case TRUTH_NOT_EXPR:
8084 expression = finish_unary_op_expr (loc, unary_operator,
8085 cast_expression, complain);
8086 break;
8087
8088 default:
8089 gcc_unreachable ();
8090 }
8091
8092 if (non_constant_p != NIC_NONE
8093 && cp_parser_non_integral_constant_expression (parser,
8094 non_constant_p))
8095 expression = error_mark_node;
8096
8097 return expression;
8098 }
8099
8100 return cp_parser_postfix_expression (parser, address_p, cast_p,
8101 /*member_access_only_p=*/false,
8102 decltype_p,
8103 pidk);
8104 }
8105
8106 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8107 unary-operator, the corresponding tree code is returned. */
8108
8109 static enum tree_code
8110 cp_parser_unary_operator (cp_token* token)
8111 {
8112 switch (token->type)
8113 {
8114 case CPP_MULT:
8115 return INDIRECT_REF;
8116
8117 case CPP_AND:
8118 return ADDR_EXPR;
8119
8120 case CPP_PLUS:
8121 return UNARY_PLUS_EXPR;
8122
8123 case CPP_MINUS:
8124 return NEGATE_EXPR;
8125
8126 case CPP_NOT:
8127 return TRUTH_NOT_EXPR;
8128
8129 case CPP_COMPL:
8130 return BIT_NOT_EXPR;
8131
8132 default:
8133 return ERROR_MARK;
8134 }
8135 }
8136
8137 /* Parse a new-expression.
8138
8139 new-expression:
8140 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8141 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8142
8143 Returns a representation of the expression. */
8144
8145 static tree
8146 cp_parser_new_expression (cp_parser* parser)
8147 {
8148 bool global_scope_p;
8149 vec<tree, va_gc> *placement;
8150 tree type;
8151 vec<tree, va_gc> *initializer;
8152 tree nelts = NULL_TREE;
8153 tree ret;
8154
8155 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8156
8157 /* Look for the optional `::' operator. */
8158 global_scope_p
8159 = (cp_parser_global_scope_opt (parser,
8160 /*current_scope_valid_p=*/false)
8161 != NULL_TREE);
8162 /* Look for the `new' operator. */
8163 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8164 /* There's no easy way to tell a new-placement from the
8165 `( type-id )' construct. */
8166 cp_parser_parse_tentatively (parser);
8167 /* Look for a new-placement. */
8168 placement = cp_parser_new_placement (parser);
8169 /* If that didn't work out, there's no new-placement. */
8170 if (!cp_parser_parse_definitely (parser))
8171 {
8172 if (placement != NULL)
8173 release_tree_vector (placement);
8174 placement = NULL;
8175 }
8176
8177 /* If the next token is a `(', then we have a parenthesized
8178 type-id. */
8179 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8180 {
8181 cp_token *token;
8182 const char *saved_message = parser->type_definition_forbidden_message;
8183
8184 /* Consume the `('. */
8185 cp_lexer_consume_token (parser->lexer);
8186
8187 /* Parse the type-id. */
8188 parser->type_definition_forbidden_message
8189 = G_("types may not be defined in a new-expression");
8190 {
8191 type_id_in_expr_sentinel s (parser);
8192 type = cp_parser_type_id (parser);
8193 }
8194 parser->type_definition_forbidden_message = saved_message;
8195
8196 /* Look for the closing `)'. */
8197 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8198 token = cp_lexer_peek_token (parser->lexer);
8199 /* There should not be a direct-new-declarator in this production,
8200 but GCC used to allowed this, so we check and emit a sensible error
8201 message for this case. */
8202 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8203 {
8204 error_at (token->location,
8205 "array bound forbidden after parenthesized type-id");
8206 inform (token->location,
8207 "try removing the parentheses around the type-id");
8208 cp_parser_direct_new_declarator (parser);
8209 }
8210 }
8211 /* Otherwise, there must be a new-type-id. */
8212 else
8213 type = cp_parser_new_type_id (parser, &nelts);
8214
8215 /* If the next token is a `(' or '{', then we have a new-initializer. */
8216 cp_token *token = cp_lexer_peek_token (parser->lexer);
8217 if (token->type == CPP_OPEN_PAREN
8218 || token->type == CPP_OPEN_BRACE)
8219 initializer = cp_parser_new_initializer (parser);
8220 else
8221 initializer = NULL;
8222
8223 /* A new-expression may not appear in an integral constant
8224 expression. */
8225 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8226 ret = error_mark_node;
8227 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8228 of a new-type-id or type-id of a new-expression, the new-expression shall
8229 contain a new-initializer of the form ( assignment-expression )".
8230 Additionally, consistently with the spirit of DR 1467, we want to accept
8231 'new auto { 2 }' too. */
8232 else if ((ret = type_uses_auto (type))
8233 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8234 && (vec_safe_length (initializer) != 1
8235 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8236 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8237 {
8238 error_at (token->location,
8239 "initialization of new-expression for type %<auto%> "
8240 "requires exactly one element");
8241 ret = error_mark_node;
8242 }
8243 else
8244 {
8245 /* Construct a location e.g.:
8246 ptr = new int[100]
8247 ^~~~~~~~~~~~
8248 with caret == start at the start of the "new" token, and the end
8249 at the end of the final token we consumed. */
8250 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8251 location_t end_loc = get_finish (end_tok->location);
8252 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8253
8254 /* Create a representation of the new-expression. */
8255 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8256 tf_warning_or_error);
8257 protected_set_expr_location (ret, combined_loc);
8258 }
8259
8260 if (placement != NULL)
8261 release_tree_vector (placement);
8262 if (initializer != NULL)
8263 release_tree_vector (initializer);
8264
8265 return ret;
8266 }
8267
8268 /* Parse a new-placement.
8269
8270 new-placement:
8271 ( expression-list )
8272
8273 Returns the same representation as for an expression-list. */
8274
8275 static vec<tree, va_gc> *
8276 cp_parser_new_placement (cp_parser* parser)
8277 {
8278 vec<tree, va_gc> *expression_list;
8279
8280 /* Parse the expression-list. */
8281 expression_list = (cp_parser_parenthesized_expression_list
8282 (parser, non_attr, /*cast_p=*/false,
8283 /*allow_expansion_p=*/true,
8284 /*non_constant_p=*/NULL));
8285
8286 if (expression_list && expression_list->is_empty ())
8287 error ("expected expression-list or type-id");
8288
8289 return expression_list;
8290 }
8291
8292 /* Parse a new-type-id.
8293
8294 new-type-id:
8295 type-specifier-seq new-declarator [opt]
8296
8297 Returns the TYPE allocated. If the new-type-id indicates an array
8298 type, *NELTS is set to the number of elements in the last array
8299 bound; the TYPE will not include the last array bound. */
8300
8301 static tree
8302 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8303 {
8304 cp_decl_specifier_seq type_specifier_seq;
8305 cp_declarator *new_declarator;
8306 cp_declarator *declarator;
8307 cp_declarator *outer_declarator;
8308 const char *saved_message;
8309
8310 /* The type-specifier sequence must not contain type definitions.
8311 (It cannot contain declarations of new types either, but if they
8312 are not definitions we will catch that because they are not
8313 complete.) */
8314 saved_message = parser->type_definition_forbidden_message;
8315 parser->type_definition_forbidden_message
8316 = G_("types may not be defined in a new-type-id");
8317 /* Parse the type-specifier-seq. */
8318 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8319 /*is_trailing_return=*/false,
8320 &type_specifier_seq);
8321 /* Restore the old message. */
8322 parser->type_definition_forbidden_message = saved_message;
8323
8324 if (type_specifier_seq.type == error_mark_node)
8325 return error_mark_node;
8326
8327 /* Parse the new-declarator. */
8328 new_declarator = cp_parser_new_declarator_opt (parser);
8329
8330 /* Determine the number of elements in the last array dimension, if
8331 any. */
8332 *nelts = NULL_TREE;
8333 /* Skip down to the last array dimension. */
8334 declarator = new_declarator;
8335 outer_declarator = NULL;
8336 while (declarator && (declarator->kind == cdk_pointer
8337 || declarator->kind == cdk_ptrmem))
8338 {
8339 outer_declarator = declarator;
8340 declarator = declarator->declarator;
8341 }
8342 while (declarator
8343 && declarator->kind == cdk_array
8344 && declarator->declarator
8345 && declarator->declarator->kind == cdk_array)
8346 {
8347 outer_declarator = declarator;
8348 declarator = declarator->declarator;
8349 }
8350
8351 if (declarator && declarator->kind == cdk_array)
8352 {
8353 *nelts = declarator->u.array.bounds;
8354 if (*nelts == error_mark_node)
8355 *nelts = integer_one_node;
8356
8357 if (outer_declarator)
8358 outer_declarator->declarator = declarator->declarator;
8359 else
8360 new_declarator = NULL;
8361 }
8362
8363 return groktypename (&type_specifier_seq, new_declarator, false);
8364 }
8365
8366 /* Parse an (optional) new-declarator.
8367
8368 new-declarator:
8369 ptr-operator new-declarator [opt]
8370 direct-new-declarator
8371
8372 Returns the declarator. */
8373
8374 static cp_declarator *
8375 cp_parser_new_declarator_opt (cp_parser* parser)
8376 {
8377 enum tree_code code;
8378 tree type, std_attributes = NULL_TREE;
8379 cp_cv_quals cv_quals;
8380
8381 /* We don't know if there's a ptr-operator next, or not. */
8382 cp_parser_parse_tentatively (parser);
8383 /* Look for a ptr-operator. */
8384 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8385 /* If that worked, look for more new-declarators. */
8386 if (cp_parser_parse_definitely (parser))
8387 {
8388 cp_declarator *declarator;
8389
8390 /* Parse another optional declarator. */
8391 declarator = cp_parser_new_declarator_opt (parser);
8392
8393 declarator = cp_parser_make_indirect_declarator
8394 (code, type, cv_quals, declarator, std_attributes);
8395
8396 return declarator;
8397 }
8398
8399 /* If the next token is a `[', there is a direct-new-declarator. */
8400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8401 return cp_parser_direct_new_declarator (parser);
8402
8403 return NULL;
8404 }
8405
8406 /* Parse a direct-new-declarator.
8407
8408 direct-new-declarator:
8409 [ expression ]
8410 direct-new-declarator [constant-expression]
8411
8412 */
8413
8414 static cp_declarator *
8415 cp_parser_direct_new_declarator (cp_parser* parser)
8416 {
8417 cp_declarator *declarator = NULL;
8418
8419 while (true)
8420 {
8421 tree expression;
8422 cp_token *token;
8423
8424 /* Look for the opening `['. */
8425 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8426
8427 token = cp_lexer_peek_token (parser->lexer);
8428 expression = cp_parser_expression (parser);
8429 /* The standard requires that the expression have integral
8430 type. DR 74 adds enumeration types. We believe that the
8431 real intent is that these expressions be handled like the
8432 expression in a `switch' condition, which also allows
8433 classes with a single conversion to integral or
8434 enumeration type. */
8435 if (!processing_template_decl)
8436 {
8437 expression
8438 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8439 expression,
8440 /*complain=*/true);
8441 if (!expression)
8442 {
8443 error_at (token->location,
8444 "expression in new-declarator must have integral "
8445 "or enumeration type");
8446 expression = error_mark_node;
8447 }
8448 }
8449
8450 /* Look for the closing `]'. */
8451 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8452
8453 /* Add this bound to the declarator. */
8454 declarator = make_array_declarator (declarator, expression);
8455
8456 /* If the next token is not a `[', then there are no more
8457 bounds. */
8458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8459 break;
8460 }
8461
8462 return declarator;
8463 }
8464
8465 /* Parse a new-initializer.
8466
8467 new-initializer:
8468 ( expression-list [opt] )
8469 braced-init-list
8470
8471 Returns a representation of the expression-list. */
8472
8473 static vec<tree, va_gc> *
8474 cp_parser_new_initializer (cp_parser* parser)
8475 {
8476 vec<tree, va_gc> *expression_list;
8477
8478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8479 {
8480 tree t;
8481 bool expr_non_constant_p;
8482 cp_lexer_set_source_position (parser->lexer);
8483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8484 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8485 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8486 expression_list = make_tree_vector_single (t);
8487 }
8488 else
8489 expression_list = (cp_parser_parenthesized_expression_list
8490 (parser, non_attr, /*cast_p=*/false,
8491 /*allow_expansion_p=*/true,
8492 /*non_constant_p=*/NULL));
8493
8494 return expression_list;
8495 }
8496
8497 /* Parse a delete-expression.
8498
8499 delete-expression:
8500 :: [opt] delete cast-expression
8501 :: [opt] delete [ ] cast-expression
8502
8503 Returns a representation of the expression. */
8504
8505 static tree
8506 cp_parser_delete_expression (cp_parser* parser)
8507 {
8508 bool global_scope_p;
8509 bool array_p;
8510 tree expression;
8511
8512 /* Look for the optional `::' operator. */
8513 global_scope_p
8514 = (cp_parser_global_scope_opt (parser,
8515 /*current_scope_valid_p=*/false)
8516 != NULL_TREE);
8517 /* Look for the `delete' keyword. */
8518 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8519 /* See if the array syntax is in use. */
8520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8521 {
8522 /* Consume the `[' token. */
8523 cp_lexer_consume_token (parser->lexer);
8524 /* Look for the `]' token. */
8525 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8526 /* Remember that this is the `[]' construct. */
8527 array_p = true;
8528 }
8529 else
8530 array_p = false;
8531
8532 /* Parse the cast-expression. */
8533 expression = cp_parser_simple_cast_expression (parser);
8534
8535 /* A delete-expression may not appear in an integral constant
8536 expression. */
8537 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8538 return error_mark_node;
8539
8540 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8541 tf_warning_or_error);
8542 }
8543
8544 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8545 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8546 0 otherwise. */
8547
8548 static int
8549 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8550 {
8551 cp_token *token = cp_lexer_peek_token (parser->lexer);
8552 switch (token->type)
8553 {
8554 case CPP_COMMA:
8555 case CPP_SEMICOLON:
8556 case CPP_QUERY:
8557 case CPP_COLON:
8558 case CPP_CLOSE_SQUARE:
8559 case CPP_CLOSE_PAREN:
8560 case CPP_CLOSE_BRACE:
8561 case CPP_OPEN_BRACE:
8562 case CPP_DOT:
8563 case CPP_DOT_STAR:
8564 case CPP_DEREF:
8565 case CPP_DEREF_STAR:
8566 case CPP_DIV:
8567 case CPP_MOD:
8568 case CPP_LSHIFT:
8569 case CPP_RSHIFT:
8570 case CPP_LESS:
8571 case CPP_GREATER:
8572 case CPP_LESS_EQ:
8573 case CPP_GREATER_EQ:
8574 case CPP_EQ_EQ:
8575 case CPP_NOT_EQ:
8576 case CPP_EQ:
8577 case CPP_MULT_EQ:
8578 case CPP_DIV_EQ:
8579 case CPP_MOD_EQ:
8580 case CPP_PLUS_EQ:
8581 case CPP_MINUS_EQ:
8582 case CPP_RSHIFT_EQ:
8583 case CPP_LSHIFT_EQ:
8584 case CPP_AND_EQ:
8585 case CPP_XOR_EQ:
8586 case CPP_OR_EQ:
8587 case CPP_XOR:
8588 case CPP_OR:
8589 case CPP_OR_OR:
8590 case CPP_EOF:
8591 case CPP_ELLIPSIS:
8592 return 0;
8593
8594 case CPP_OPEN_PAREN:
8595 /* In ((type ()) () the last () isn't a valid cast-expression,
8596 so the whole must be parsed as postfix-expression. */
8597 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8598 != CPP_CLOSE_PAREN;
8599
8600 case CPP_OPEN_SQUARE:
8601 /* '[' may start a primary-expression in obj-c++ and in C++11,
8602 as a lambda-expression, eg, '(void)[]{}'. */
8603 if (cxx_dialect >= cxx11)
8604 return -1;
8605 return c_dialect_objc ();
8606
8607 case CPP_PLUS_PLUS:
8608 case CPP_MINUS_MINUS:
8609 /* '++' and '--' may or may not start a cast-expression:
8610
8611 struct T { void operator++(int); };
8612 void f() { (T())++; }
8613
8614 vs
8615
8616 int a;
8617 (int)++a; */
8618 return -1;
8619
8620 default:
8621 return 1;
8622 }
8623 }
8624
8625 /* Parse a cast-expression.
8626
8627 cast-expression:
8628 unary-expression
8629 ( type-id ) cast-expression
8630
8631 ADDRESS_P is true iff the unary-expression is appearing as the
8632 operand of the `&' operator. CAST_P is true if this expression is
8633 the target of a cast.
8634
8635 Returns a representation of the expression. */
8636
8637 static cp_expr
8638 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8639 bool decltype_p, cp_id_kind * pidk)
8640 {
8641 /* If it's a `(', then we might be looking at a cast. */
8642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8643 {
8644 tree type = NULL_TREE;
8645 cp_expr expr (NULL_TREE);
8646 int cast_expression = 0;
8647 const char *saved_message;
8648
8649 /* There's no way to know yet whether or not this is a cast.
8650 For example, `(int (3))' is a unary-expression, while `(int)
8651 3' is a cast. So, we resort to parsing tentatively. */
8652 cp_parser_parse_tentatively (parser);
8653 /* Types may not be defined in a cast. */
8654 saved_message = parser->type_definition_forbidden_message;
8655 parser->type_definition_forbidden_message
8656 = G_("types may not be defined in casts");
8657 /* Consume the `('. */
8658 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8659 location_t open_paren_loc = open_paren->location;
8660
8661 /* A very tricky bit is that `(struct S) { 3 }' is a
8662 compound-literal (which we permit in C++ as an extension).
8663 But, that construct is not a cast-expression -- it is a
8664 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8665 is legal; if the compound-literal were a cast-expression,
8666 you'd need an extra set of parentheses.) But, if we parse
8667 the type-id, and it happens to be a class-specifier, then we
8668 will commit to the parse at that point, because we cannot
8669 undo the action that is done when creating a new class. So,
8670 then we cannot back up and do a postfix-expression.
8671
8672 Another tricky case is the following (c++/29234):
8673
8674 struct S { void operator () (); };
8675
8676 void foo ()
8677 {
8678 ( S()() );
8679 }
8680
8681 As a type-id we parse the parenthesized S()() as a function
8682 returning a function, groktypename complains and we cannot
8683 back up in this case either.
8684
8685 Therefore, we scan ahead to the closing `)', and check to see
8686 if the tokens after the `)' can start a cast-expression. Otherwise
8687 we are dealing with an unary-expression, a postfix-expression
8688 or something else.
8689
8690 Yet another tricky case, in C++11, is the following (c++/54891):
8691
8692 (void)[]{};
8693
8694 The issue is that usually, besides the case of lambda-expressions,
8695 the parenthesized type-id cannot be followed by '[', and, eg, we
8696 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8697 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8698 we don't commit, we try a cast-expression, then an unary-expression.
8699
8700 Save tokens so that we can put them back. */
8701 cp_lexer_save_tokens (parser->lexer);
8702
8703 /* We may be looking at a cast-expression. */
8704 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8705 /*consume_paren=*/true))
8706 cast_expression
8707 = cp_parser_tokens_start_cast_expression (parser);
8708
8709 /* Roll back the tokens we skipped. */
8710 cp_lexer_rollback_tokens (parser->lexer);
8711 /* If we aren't looking at a cast-expression, simulate an error so
8712 that the call to cp_parser_error_occurred below returns true. */
8713 if (!cast_expression)
8714 cp_parser_simulate_error (parser);
8715 else
8716 {
8717 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8718 parser->in_type_id_in_expr_p = true;
8719 /* Look for the type-id. */
8720 type = cp_parser_type_id (parser);
8721 /* Look for the closing `)'. */
8722 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8723 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8724 }
8725
8726 /* Restore the saved message. */
8727 parser->type_definition_forbidden_message = saved_message;
8728
8729 /* At this point this can only be either a cast or a
8730 parenthesized ctor such as `(T ())' that looks like a cast to
8731 function returning T. */
8732 if (!cp_parser_error_occurred (parser))
8733 {
8734 /* Only commit if the cast-expression doesn't start with
8735 '++', '--', or '[' in C++11. */
8736 if (cast_expression > 0)
8737 cp_parser_commit_to_topmost_tentative_parse (parser);
8738
8739 expr = cp_parser_cast_expression (parser,
8740 /*address_p=*/false,
8741 /*cast_p=*/true,
8742 /*decltype_p=*/false,
8743 pidk);
8744
8745 if (cp_parser_parse_definitely (parser))
8746 {
8747 /* Warn about old-style casts, if so requested. */
8748 if (warn_old_style_cast
8749 && !in_system_header_at (input_location)
8750 && !VOID_TYPE_P (type)
8751 && current_lang_name != lang_name_c)
8752 warning (OPT_Wold_style_cast, "use of old-style cast");
8753
8754 /* Only type conversions to integral or enumeration types
8755 can be used in constant-expressions. */
8756 if (!cast_valid_in_integral_constant_expression_p (type)
8757 && cp_parser_non_integral_constant_expression (parser,
8758 NIC_CAST))
8759 return error_mark_node;
8760
8761 /* Perform the cast. */
8762 /* Make a location:
8763 (TYPE) EXPR
8764 ^~~~~~~~~~~
8765 with start==caret at the open paren, extending to the
8766 end of "expr". */
8767 location_t cast_loc = make_location (open_paren_loc,
8768 open_paren_loc,
8769 expr.get_finish ());
8770 expr = build_c_cast (cast_loc, type, expr);
8771 return expr;
8772 }
8773 }
8774 else
8775 cp_parser_abort_tentative_parse (parser);
8776 }
8777
8778 /* If we get here, then it's not a cast, so it must be a
8779 unary-expression. */
8780 return cp_parser_unary_expression (parser, pidk, address_p,
8781 cast_p, decltype_p);
8782 }
8783
8784 /* Parse a binary expression of the general form:
8785
8786 pm-expression:
8787 cast-expression
8788 pm-expression .* cast-expression
8789 pm-expression ->* cast-expression
8790
8791 multiplicative-expression:
8792 pm-expression
8793 multiplicative-expression * pm-expression
8794 multiplicative-expression / pm-expression
8795 multiplicative-expression % pm-expression
8796
8797 additive-expression:
8798 multiplicative-expression
8799 additive-expression + multiplicative-expression
8800 additive-expression - multiplicative-expression
8801
8802 shift-expression:
8803 additive-expression
8804 shift-expression << additive-expression
8805 shift-expression >> additive-expression
8806
8807 relational-expression:
8808 shift-expression
8809 relational-expression < shift-expression
8810 relational-expression > shift-expression
8811 relational-expression <= shift-expression
8812 relational-expression >= shift-expression
8813
8814 GNU Extension:
8815
8816 relational-expression:
8817 relational-expression <? shift-expression
8818 relational-expression >? shift-expression
8819
8820 equality-expression:
8821 relational-expression
8822 equality-expression == relational-expression
8823 equality-expression != relational-expression
8824
8825 and-expression:
8826 equality-expression
8827 and-expression & equality-expression
8828
8829 exclusive-or-expression:
8830 and-expression
8831 exclusive-or-expression ^ and-expression
8832
8833 inclusive-or-expression:
8834 exclusive-or-expression
8835 inclusive-or-expression | exclusive-or-expression
8836
8837 logical-and-expression:
8838 inclusive-or-expression
8839 logical-and-expression && inclusive-or-expression
8840
8841 logical-or-expression:
8842 logical-and-expression
8843 logical-or-expression || logical-and-expression
8844
8845 All these are implemented with a single function like:
8846
8847 binary-expression:
8848 simple-cast-expression
8849 binary-expression <token> binary-expression
8850
8851 CAST_P is true if this expression is the target of a cast.
8852
8853 The binops_by_token map is used to get the tree codes for each <token> type.
8854 binary-expressions are associated according to a precedence table. */
8855
8856 #define TOKEN_PRECEDENCE(token) \
8857 (((token->type == CPP_GREATER \
8858 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8859 && !parser->greater_than_is_operator_p) \
8860 ? PREC_NOT_OPERATOR \
8861 : binops_by_token[token->type].prec)
8862
8863 static cp_expr
8864 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8865 bool no_toplevel_fold_p,
8866 bool decltype_p,
8867 enum cp_parser_prec prec,
8868 cp_id_kind * pidk)
8869 {
8870 cp_parser_expression_stack stack;
8871 cp_parser_expression_stack_entry *sp = &stack[0];
8872 cp_parser_expression_stack_entry current;
8873 cp_expr rhs;
8874 cp_token *token;
8875 enum tree_code rhs_type;
8876 enum cp_parser_prec new_prec, lookahead_prec;
8877 tree overload;
8878
8879 /* Parse the first expression. */
8880 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8881 ? TRUTH_NOT_EXPR : ERROR_MARK);
8882 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8883 cast_p, decltype_p, pidk);
8884 current.prec = prec;
8885
8886 if (cp_parser_error_occurred (parser))
8887 return error_mark_node;
8888
8889 for (;;)
8890 {
8891 /* Get an operator token. */
8892 token = cp_lexer_peek_token (parser->lexer);
8893
8894 if (warn_cxx11_compat
8895 && token->type == CPP_RSHIFT
8896 && !parser->greater_than_is_operator_p)
8897 {
8898 if (warning_at (token->location, OPT_Wc__11_compat,
8899 "%<>>%> operator is treated"
8900 " as two right angle brackets in C++11"))
8901 inform (token->location,
8902 "suggest parentheses around %<>>%> expression");
8903 }
8904
8905 new_prec = TOKEN_PRECEDENCE (token);
8906 if (new_prec != PREC_NOT_OPERATOR
8907 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8908 /* This is a fold-expression; handle it later. */
8909 new_prec = PREC_NOT_OPERATOR;
8910
8911 /* Popping an entry off the stack means we completed a subexpression:
8912 - either we found a token which is not an operator (`>' where it is not
8913 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8914 will happen repeatedly;
8915 - or, we found an operator which has lower priority. This is the case
8916 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8917 parsing `3 * 4'. */
8918 if (new_prec <= current.prec)
8919 {
8920 if (sp == stack)
8921 break;
8922 else
8923 goto pop;
8924 }
8925
8926 get_rhs:
8927 current.tree_type = binops_by_token[token->type].tree_type;
8928 current.loc = token->location;
8929
8930 /* We used the operator token. */
8931 cp_lexer_consume_token (parser->lexer);
8932
8933 /* For "false && x" or "true || x", x will never be executed;
8934 disable warnings while evaluating it. */
8935 if (current.tree_type == TRUTH_ANDIF_EXPR)
8936 c_inhibit_evaluation_warnings +=
8937 cp_fully_fold (current.lhs) == truthvalue_false_node;
8938 else if (current.tree_type == TRUTH_ORIF_EXPR)
8939 c_inhibit_evaluation_warnings +=
8940 cp_fully_fold (current.lhs) == truthvalue_true_node;
8941
8942 /* Extract another operand. It may be the RHS of this expression
8943 or the LHS of a new, higher priority expression. */
8944 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8945 ? TRUTH_NOT_EXPR : ERROR_MARK);
8946 rhs = cp_parser_simple_cast_expression (parser);
8947
8948 /* Get another operator token. Look up its precedence to avoid
8949 building a useless (immediately popped) stack entry for common
8950 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8951 token = cp_lexer_peek_token (parser->lexer);
8952 lookahead_prec = TOKEN_PRECEDENCE (token);
8953 if (lookahead_prec != PREC_NOT_OPERATOR
8954 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8955 lookahead_prec = PREC_NOT_OPERATOR;
8956 if (lookahead_prec > new_prec)
8957 {
8958 /* ... and prepare to parse the RHS of the new, higher priority
8959 expression. Since precedence levels on the stack are
8960 monotonically increasing, we do not have to care about
8961 stack overflows. */
8962 *sp = current;
8963 ++sp;
8964 current.lhs = rhs;
8965 current.lhs_type = rhs_type;
8966 current.prec = new_prec;
8967 new_prec = lookahead_prec;
8968 goto get_rhs;
8969
8970 pop:
8971 lookahead_prec = new_prec;
8972 /* If the stack is not empty, we have parsed into LHS the right side
8973 (`4' in the example above) of an expression we had suspended.
8974 We can use the information on the stack to recover the LHS (`3')
8975 from the stack together with the tree code (`MULT_EXPR'), and
8976 the precedence of the higher level subexpression
8977 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8978 which will be used to actually build the additive expression. */
8979 rhs = current.lhs;
8980 rhs_type = current.lhs_type;
8981 --sp;
8982 current = *sp;
8983 }
8984
8985 /* Undo the disabling of warnings done above. */
8986 if (current.tree_type == TRUTH_ANDIF_EXPR)
8987 c_inhibit_evaluation_warnings -=
8988 cp_fully_fold (current.lhs) == truthvalue_false_node;
8989 else if (current.tree_type == TRUTH_ORIF_EXPR)
8990 c_inhibit_evaluation_warnings -=
8991 cp_fully_fold (current.lhs) == truthvalue_true_node;
8992
8993 if (warn_logical_not_paren
8994 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8995 && current.lhs_type == TRUTH_NOT_EXPR
8996 /* Avoid warning for !!x == y. */
8997 && (TREE_CODE (current.lhs) != NE_EXPR
8998 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8999 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9000 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9001 /* Avoid warning for !b == y where b is boolean. */
9002 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9003 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9004 != BOOLEAN_TYPE))))
9005 /* Avoid warning for !!b == y where b is boolean. */
9006 && (!DECL_P (current.lhs)
9007 || TREE_TYPE (current.lhs) == NULL_TREE
9008 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9009 warn_logical_not_parentheses (current.loc, current.tree_type,
9010 current.lhs, maybe_constant_value (rhs));
9011
9012 overload = NULL;
9013
9014 location_t combined_loc = make_location (current.loc,
9015 current.lhs.get_start (),
9016 rhs.get_finish ());
9017
9018 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9019 ERROR_MARK for everything that is not a binary expression.
9020 This makes warn_about_parentheses miss some warnings that
9021 involve unary operators. For unary expressions we should
9022 pass the correct tree_code unless the unary expression was
9023 surrounded by parentheses.
9024 */
9025 if (no_toplevel_fold_p
9026 && lookahead_prec <= current.prec
9027 && sp == stack)
9028 current.lhs = build2_loc (combined_loc,
9029 current.tree_type,
9030 TREE_CODE_CLASS (current.tree_type)
9031 == tcc_comparison
9032 ? boolean_type_node : TREE_TYPE (current.lhs),
9033 current.lhs, rhs);
9034 else
9035 {
9036 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9037 current.lhs, current.lhs_type,
9038 rhs, rhs_type, &overload,
9039 complain_flags (decltype_p));
9040 /* TODO: build_x_binary_op doesn't always honor the location. */
9041 current.lhs.set_location (combined_loc);
9042 }
9043 current.lhs_type = current.tree_type;
9044
9045 /* If the binary operator required the use of an overloaded operator,
9046 then this expression cannot be an integral constant-expression.
9047 An overloaded operator can be used even if both operands are
9048 otherwise permissible in an integral constant-expression if at
9049 least one of the operands is of enumeration type. */
9050
9051 if (overload
9052 && cp_parser_non_integral_constant_expression (parser,
9053 NIC_OVERLOADED))
9054 return error_mark_node;
9055 }
9056
9057 return current.lhs;
9058 }
9059
9060 static cp_expr
9061 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9062 bool no_toplevel_fold_p,
9063 enum cp_parser_prec prec,
9064 cp_id_kind * pidk)
9065 {
9066 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9067 /*decltype*/false, prec, pidk);
9068 }
9069
9070 /* Parse the `? expression : assignment-expression' part of a
9071 conditional-expression. The LOGICAL_OR_EXPR is the
9072 logical-or-expression that started the conditional-expression.
9073 Returns a representation of the entire conditional-expression.
9074
9075 This routine is used by cp_parser_assignment_expression.
9076
9077 ? expression : assignment-expression
9078
9079 GNU Extensions:
9080
9081 ? : assignment-expression */
9082
9083 static tree
9084 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9085 {
9086 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9087 cp_expr assignment_expr;
9088 struct cp_token *token;
9089 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9090
9091 /* Consume the `?' token. */
9092 cp_lexer_consume_token (parser->lexer);
9093 token = cp_lexer_peek_token (parser->lexer);
9094 if (cp_parser_allow_gnu_extensions_p (parser)
9095 && token->type == CPP_COLON)
9096 {
9097 pedwarn (token->location, OPT_Wpedantic,
9098 "ISO C++ does not allow ?: with omitted middle operand");
9099 /* Implicit true clause. */
9100 expr = NULL_TREE;
9101 c_inhibit_evaluation_warnings +=
9102 folded_logical_or_expr == truthvalue_true_node;
9103 warn_for_omitted_condop (token->location, logical_or_expr);
9104 }
9105 else
9106 {
9107 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9108 parser->colon_corrects_to_scope_p = false;
9109 /* Parse the expression. */
9110 c_inhibit_evaluation_warnings +=
9111 folded_logical_or_expr == truthvalue_false_node;
9112 expr = cp_parser_expression (parser);
9113 c_inhibit_evaluation_warnings +=
9114 ((folded_logical_or_expr == truthvalue_true_node)
9115 - (folded_logical_or_expr == truthvalue_false_node));
9116 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9117 }
9118
9119 /* The next token should be a `:'. */
9120 cp_parser_require (parser, CPP_COLON, RT_COLON);
9121 /* Parse the assignment-expression. */
9122 assignment_expr = cp_parser_assignment_expression (parser);
9123 c_inhibit_evaluation_warnings -=
9124 folded_logical_or_expr == truthvalue_true_node;
9125
9126 /* Make a location:
9127 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9128 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9129 with the caret at the "?", ranging from the start of
9130 the logical_or_expr to the end of the assignment_expr. */
9131 loc = make_location (loc,
9132 logical_or_expr.get_start (),
9133 assignment_expr.get_finish ());
9134
9135 /* Build the conditional-expression. */
9136 return build_x_conditional_expr (loc, logical_or_expr,
9137 expr,
9138 assignment_expr,
9139 tf_warning_or_error);
9140 }
9141
9142 /* Parse an assignment-expression.
9143
9144 assignment-expression:
9145 conditional-expression
9146 logical-or-expression assignment-operator assignment_expression
9147 throw-expression
9148
9149 CAST_P is true if this expression is the target of a cast.
9150 DECLTYPE_P is true if this expression is the operand of decltype.
9151
9152 Returns a representation for the expression. */
9153
9154 static cp_expr
9155 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9156 bool cast_p, bool decltype_p)
9157 {
9158 cp_expr expr;
9159
9160 /* If the next token is the `throw' keyword, then we're looking at
9161 a throw-expression. */
9162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9163 expr = cp_parser_throw_expression (parser);
9164 /* Otherwise, it must be that we are looking at a
9165 logical-or-expression. */
9166 else
9167 {
9168 /* Parse the binary expressions (logical-or-expression). */
9169 expr = cp_parser_binary_expression (parser, cast_p, false,
9170 decltype_p,
9171 PREC_NOT_OPERATOR, pidk);
9172 /* If the next token is a `?' then we're actually looking at a
9173 conditional-expression. */
9174 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9175 return cp_parser_question_colon_clause (parser, expr);
9176 else
9177 {
9178 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9179
9180 /* If it's an assignment-operator, we're using the second
9181 production. */
9182 enum tree_code assignment_operator
9183 = cp_parser_assignment_operator_opt (parser);
9184 if (assignment_operator != ERROR_MARK)
9185 {
9186 bool non_constant_p;
9187
9188 /* Parse the right-hand side of the assignment. */
9189 cp_expr rhs = cp_parser_initializer_clause (parser,
9190 &non_constant_p);
9191
9192 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9193 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9194
9195 /* An assignment may not appear in a
9196 constant-expression. */
9197 if (cp_parser_non_integral_constant_expression (parser,
9198 NIC_ASSIGNMENT))
9199 return error_mark_node;
9200 /* Build the assignment expression. Its default
9201 location:
9202 LHS = RHS
9203 ~~~~^~~~~
9204 is the location of the '=' token as the
9205 caret, ranging from the start of the lhs to the
9206 end of the rhs. */
9207 loc = make_location (loc,
9208 expr.get_start (),
9209 rhs.get_finish ());
9210 expr = build_x_modify_expr (loc, expr,
9211 assignment_operator,
9212 rhs,
9213 complain_flags (decltype_p));
9214 /* TODO: build_x_modify_expr doesn't honor the location,
9215 so we must set it here. */
9216 expr.set_location (loc);
9217 }
9218 }
9219 }
9220
9221 return expr;
9222 }
9223
9224 /* Parse an (optional) assignment-operator.
9225
9226 assignment-operator: one of
9227 = *= /= %= += -= >>= <<= &= ^= |=
9228
9229 GNU Extension:
9230
9231 assignment-operator: one of
9232 <?= >?=
9233
9234 If the next token is an assignment operator, the corresponding tree
9235 code is returned, and the token is consumed. For example, for
9236 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9237 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9238 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9239 operator, ERROR_MARK is returned. */
9240
9241 static enum tree_code
9242 cp_parser_assignment_operator_opt (cp_parser* parser)
9243 {
9244 enum tree_code op;
9245 cp_token *token;
9246
9247 /* Peek at the next token. */
9248 token = cp_lexer_peek_token (parser->lexer);
9249
9250 switch (token->type)
9251 {
9252 case CPP_EQ:
9253 op = NOP_EXPR;
9254 break;
9255
9256 case CPP_MULT_EQ:
9257 op = MULT_EXPR;
9258 break;
9259
9260 case CPP_DIV_EQ:
9261 op = TRUNC_DIV_EXPR;
9262 break;
9263
9264 case CPP_MOD_EQ:
9265 op = TRUNC_MOD_EXPR;
9266 break;
9267
9268 case CPP_PLUS_EQ:
9269 op = PLUS_EXPR;
9270 break;
9271
9272 case CPP_MINUS_EQ:
9273 op = MINUS_EXPR;
9274 break;
9275
9276 case CPP_RSHIFT_EQ:
9277 op = RSHIFT_EXPR;
9278 break;
9279
9280 case CPP_LSHIFT_EQ:
9281 op = LSHIFT_EXPR;
9282 break;
9283
9284 case CPP_AND_EQ:
9285 op = BIT_AND_EXPR;
9286 break;
9287
9288 case CPP_XOR_EQ:
9289 op = BIT_XOR_EXPR;
9290 break;
9291
9292 case CPP_OR_EQ:
9293 op = BIT_IOR_EXPR;
9294 break;
9295
9296 default:
9297 /* Nothing else is an assignment operator. */
9298 op = ERROR_MARK;
9299 }
9300
9301 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9302 if (op != ERROR_MARK
9303 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9304 op = ERROR_MARK;
9305
9306 /* If it was an assignment operator, consume it. */
9307 if (op != ERROR_MARK)
9308 cp_lexer_consume_token (parser->lexer);
9309
9310 return op;
9311 }
9312
9313 /* Parse an expression.
9314
9315 expression:
9316 assignment-expression
9317 expression , assignment-expression
9318
9319 CAST_P is true if this expression is the target of a cast.
9320 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9321 except possibly parenthesized or on the RHS of a comma (N3276).
9322
9323 Returns a representation of the expression. */
9324
9325 static cp_expr
9326 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9327 bool cast_p, bool decltype_p)
9328 {
9329 cp_expr expression = NULL_TREE;
9330 location_t loc = UNKNOWN_LOCATION;
9331
9332 while (true)
9333 {
9334 cp_expr assignment_expression;
9335
9336 /* Parse the next assignment-expression. */
9337 assignment_expression
9338 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9339
9340 /* We don't create a temporary for a call that is the immediate operand
9341 of decltype or on the RHS of a comma. But when we see a comma, we
9342 need to create a temporary for a call on the LHS. */
9343 if (decltype_p && !processing_template_decl
9344 && TREE_CODE (assignment_expression) == CALL_EXPR
9345 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9346 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9347 assignment_expression
9348 = build_cplus_new (TREE_TYPE (assignment_expression),
9349 assignment_expression, tf_warning_or_error);
9350
9351 /* If this is the first assignment-expression, we can just
9352 save it away. */
9353 if (!expression)
9354 expression = assignment_expression;
9355 else
9356 {
9357 /* Create a location with caret at the comma, ranging
9358 from the start of the LHS to the end of the RHS. */
9359 loc = make_location (loc,
9360 expression.get_start (),
9361 assignment_expression.get_finish ());
9362 expression = build_x_compound_expr (loc, expression,
9363 assignment_expression,
9364 complain_flags (decltype_p));
9365 expression.set_location (loc);
9366 }
9367 /* If the next token is not a comma, or we're in a fold-expression, then
9368 we are done with the expression. */
9369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9370 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9371 break;
9372 /* Consume the `,'. */
9373 loc = cp_lexer_peek_token (parser->lexer)->location;
9374 cp_lexer_consume_token (parser->lexer);
9375 /* A comma operator cannot appear in a constant-expression. */
9376 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9377 expression = error_mark_node;
9378 }
9379
9380 return expression;
9381 }
9382
9383 /* Parse a constant-expression.
9384
9385 constant-expression:
9386 conditional-expression
9387
9388 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9389 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9390 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9391 is false, NON_CONSTANT_P should be NULL. */
9392
9393 static cp_expr
9394 cp_parser_constant_expression (cp_parser* parser,
9395 bool allow_non_constant_p,
9396 bool *non_constant_p)
9397 {
9398 bool saved_integral_constant_expression_p;
9399 bool saved_allow_non_integral_constant_expression_p;
9400 bool saved_non_integral_constant_expression_p;
9401 cp_expr expression;
9402
9403 /* It might seem that we could simply parse the
9404 conditional-expression, and then check to see if it were
9405 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9406 one that the compiler can figure out is constant, possibly after
9407 doing some simplifications or optimizations. The standard has a
9408 precise definition of constant-expression, and we must honor
9409 that, even though it is somewhat more restrictive.
9410
9411 For example:
9412
9413 int i[(2, 3)];
9414
9415 is not a legal declaration, because `(2, 3)' is not a
9416 constant-expression. The `,' operator is forbidden in a
9417 constant-expression. However, GCC's constant-folding machinery
9418 will fold this operation to an INTEGER_CST for `3'. */
9419
9420 /* Save the old settings. */
9421 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9422 saved_allow_non_integral_constant_expression_p
9423 = parser->allow_non_integral_constant_expression_p;
9424 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9425 /* We are now parsing a constant-expression. */
9426 parser->integral_constant_expression_p = true;
9427 parser->allow_non_integral_constant_expression_p
9428 = (allow_non_constant_p || cxx_dialect >= cxx11);
9429 parser->non_integral_constant_expression_p = false;
9430 /* Although the grammar says "conditional-expression", we parse an
9431 "assignment-expression", which also permits "throw-expression"
9432 and the use of assignment operators. In the case that
9433 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9434 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9435 actually essential that we look for an assignment-expression.
9436 For example, cp_parser_initializer_clauses uses this function to
9437 determine whether a particular assignment-expression is in fact
9438 constant. */
9439 expression = cp_parser_assignment_expression (parser);
9440 /* Restore the old settings. */
9441 parser->integral_constant_expression_p
9442 = saved_integral_constant_expression_p;
9443 parser->allow_non_integral_constant_expression_p
9444 = saved_allow_non_integral_constant_expression_p;
9445 if (cxx_dialect >= cxx11)
9446 {
9447 /* Require an rvalue constant expression here; that's what our
9448 callers expect. Reference constant expressions are handled
9449 separately in e.g. cp_parser_template_argument. */
9450 bool is_const = potential_rvalue_constant_expression (expression);
9451 parser->non_integral_constant_expression_p = !is_const;
9452 if (!is_const && !allow_non_constant_p)
9453 require_potential_rvalue_constant_expression (expression);
9454 }
9455 if (allow_non_constant_p)
9456 *non_constant_p = parser->non_integral_constant_expression_p;
9457 parser->non_integral_constant_expression_p
9458 = saved_non_integral_constant_expression_p;
9459
9460 return expression;
9461 }
9462
9463 /* Parse __builtin_offsetof.
9464
9465 offsetof-expression:
9466 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9467
9468 offsetof-member-designator:
9469 id-expression
9470 | offsetof-member-designator "." id-expression
9471 | offsetof-member-designator "[" expression "]"
9472 | offsetof-member-designator "->" id-expression */
9473
9474 static cp_expr
9475 cp_parser_builtin_offsetof (cp_parser *parser)
9476 {
9477 int save_ice_p, save_non_ice_p;
9478 tree type;
9479 cp_expr expr;
9480 cp_id_kind dummy;
9481 cp_token *token;
9482 location_t finish_loc;
9483
9484 /* We're about to accept non-integral-constant things, but will
9485 definitely yield an integral constant expression. Save and
9486 restore these values around our local parsing. */
9487 save_ice_p = parser->integral_constant_expression_p;
9488 save_non_ice_p = parser->non_integral_constant_expression_p;
9489
9490 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9491
9492 /* Consume the "__builtin_offsetof" token. */
9493 cp_lexer_consume_token (parser->lexer);
9494 /* Consume the opening `('. */
9495 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9496 /* Parse the type-id. */
9497 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9498 type = cp_parser_type_id (parser);
9499 /* Look for the `,'. */
9500 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9501 token = cp_lexer_peek_token (parser->lexer);
9502
9503 /* Build the (type *)null that begins the traditional offsetof macro. */
9504 tree object_ptr
9505 = build_static_cast (build_pointer_type (type), null_pointer_node,
9506 tf_warning_or_error);
9507
9508 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9509 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9510 true, &dummy, token->location);
9511 while (true)
9512 {
9513 token = cp_lexer_peek_token (parser->lexer);
9514 switch (token->type)
9515 {
9516 case CPP_OPEN_SQUARE:
9517 /* offsetof-member-designator "[" expression "]" */
9518 expr = cp_parser_postfix_open_square_expression (parser, expr,
9519 true, false);
9520 break;
9521
9522 case CPP_DEREF:
9523 /* offsetof-member-designator "->" identifier */
9524 expr = grok_array_decl (token->location, expr,
9525 integer_zero_node, false);
9526 /* FALLTHRU */
9527
9528 case CPP_DOT:
9529 /* offsetof-member-designator "." identifier */
9530 cp_lexer_consume_token (parser->lexer);
9531 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9532 expr, true, &dummy,
9533 token->location);
9534 break;
9535
9536 case CPP_CLOSE_PAREN:
9537 /* Consume the ")" token. */
9538 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9539 cp_lexer_consume_token (parser->lexer);
9540 goto success;
9541
9542 default:
9543 /* Error. We know the following require will fail, but
9544 that gives the proper error message. */
9545 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9546 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9547 expr = error_mark_node;
9548 goto failure;
9549 }
9550 }
9551
9552 success:
9553 /* Make a location of the form:
9554 __builtin_offsetof (struct s, f)
9555 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9556 with caret at the type-id, ranging from the start of the
9557 "_builtin_offsetof" token to the close paren. */
9558 loc = make_location (loc, start_loc, finish_loc);
9559 /* The result will be an INTEGER_CST, so we need to explicitly
9560 preserve the location. */
9561 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9562
9563 failure:
9564 parser->integral_constant_expression_p = save_ice_p;
9565 parser->non_integral_constant_expression_p = save_non_ice_p;
9566
9567 return expr;
9568 }
9569
9570 /* Parse a trait expression.
9571
9572 Returns a representation of the expression, the underlying type
9573 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9574
9575 static tree
9576 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9577 {
9578 cp_trait_kind kind;
9579 tree type1, type2 = NULL_TREE;
9580 bool binary = false;
9581 bool variadic = false;
9582
9583 switch (keyword)
9584 {
9585 case RID_HAS_NOTHROW_ASSIGN:
9586 kind = CPTK_HAS_NOTHROW_ASSIGN;
9587 break;
9588 case RID_HAS_NOTHROW_CONSTRUCTOR:
9589 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9590 break;
9591 case RID_HAS_NOTHROW_COPY:
9592 kind = CPTK_HAS_NOTHROW_COPY;
9593 break;
9594 case RID_HAS_TRIVIAL_ASSIGN:
9595 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9596 break;
9597 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9598 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9599 break;
9600 case RID_HAS_TRIVIAL_COPY:
9601 kind = CPTK_HAS_TRIVIAL_COPY;
9602 break;
9603 case RID_HAS_TRIVIAL_DESTRUCTOR:
9604 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9605 break;
9606 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9607 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9608 break;
9609 case RID_HAS_VIRTUAL_DESTRUCTOR:
9610 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9611 break;
9612 case RID_IS_ABSTRACT:
9613 kind = CPTK_IS_ABSTRACT;
9614 break;
9615 case RID_IS_AGGREGATE:
9616 kind = CPTK_IS_AGGREGATE;
9617 break;
9618 case RID_IS_BASE_OF:
9619 kind = CPTK_IS_BASE_OF;
9620 binary = true;
9621 break;
9622 case RID_IS_CLASS:
9623 kind = CPTK_IS_CLASS;
9624 break;
9625 case RID_IS_EMPTY:
9626 kind = CPTK_IS_EMPTY;
9627 break;
9628 case RID_IS_ENUM:
9629 kind = CPTK_IS_ENUM;
9630 break;
9631 case RID_IS_FINAL:
9632 kind = CPTK_IS_FINAL;
9633 break;
9634 case RID_IS_LITERAL_TYPE:
9635 kind = CPTK_IS_LITERAL_TYPE;
9636 break;
9637 case RID_IS_POD:
9638 kind = CPTK_IS_POD;
9639 break;
9640 case RID_IS_POLYMORPHIC:
9641 kind = CPTK_IS_POLYMORPHIC;
9642 break;
9643 case RID_IS_SAME_AS:
9644 kind = CPTK_IS_SAME_AS;
9645 binary = true;
9646 break;
9647 case RID_IS_STD_LAYOUT:
9648 kind = CPTK_IS_STD_LAYOUT;
9649 break;
9650 case RID_IS_TRIVIAL:
9651 kind = CPTK_IS_TRIVIAL;
9652 break;
9653 case RID_IS_TRIVIALLY_ASSIGNABLE:
9654 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9655 binary = true;
9656 break;
9657 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9658 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9659 variadic = true;
9660 break;
9661 case RID_IS_TRIVIALLY_COPYABLE:
9662 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9663 break;
9664 case RID_IS_UNION:
9665 kind = CPTK_IS_UNION;
9666 break;
9667 case RID_UNDERLYING_TYPE:
9668 kind = CPTK_UNDERLYING_TYPE;
9669 break;
9670 case RID_BASES:
9671 kind = CPTK_BASES;
9672 break;
9673 case RID_DIRECT_BASES:
9674 kind = CPTK_DIRECT_BASES;
9675 break;
9676 default:
9677 gcc_unreachable ();
9678 }
9679
9680 /* Consume the token. */
9681 cp_lexer_consume_token (parser->lexer);
9682
9683 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9684
9685 {
9686 type_id_in_expr_sentinel s (parser);
9687 type1 = cp_parser_type_id (parser);
9688 }
9689
9690 if (type1 == error_mark_node)
9691 return error_mark_node;
9692
9693 if (binary)
9694 {
9695 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9696
9697 {
9698 type_id_in_expr_sentinel s (parser);
9699 type2 = cp_parser_type_id (parser);
9700 }
9701
9702 if (type2 == error_mark_node)
9703 return error_mark_node;
9704 }
9705 else if (variadic)
9706 {
9707 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9708 {
9709 cp_lexer_consume_token (parser->lexer);
9710 tree elt = cp_parser_type_id (parser);
9711 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9712 {
9713 cp_lexer_consume_token (parser->lexer);
9714 elt = make_pack_expansion (elt);
9715 }
9716 if (elt == error_mark_node)
9717 return error_mark_node;
9718 type2 = tree_cons (NULL_TREE, elt, type2);
9719 }
9720 }
9721
9722 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9723
9724 /* Complete the trait expression, which may mean either processing
9725 the trait expr now or saving it for template instantiation. */
9726 switch (kind)
9727 {
9728 case CPTK_UNDERLYING_TYPE:
9729 return finish_underlying_type (type1);
9730 case CPTK_BASES:
9731 return finish_bases (type1, false);
9732 case CPTK_DIRECT_BASES:
9733 return finish_bases (type1, true);
9734 default:
9735 return finish_trait_expr (kind, type1, type2);
9736 }
9737 }
9738
9739 /* Lambdas that appear in variable initializer or default argument scope
9740 get that in their mangling, so we need to record it. We might as well
9741 use the count for function and namespace scopes as well. */
9742 static GTY(()) tree lambda_scope;
9743 static GTY(()) int lambda_count;
9744 struct GTY(()) tree_int
9745 {
9746 tree t;
9747 int i;
9748 };
9749 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9750
9751 static void
9752 start_lambda_scope (tree decl)
9753 {
9754 tree_int ti;
9755 gcc_assert (decl);
9756 /* Once we're inside a function, we ignore other scopes and just push
9757 the function again so that popping works properly. */
9758 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9759 decl = current_function_decl;
9760 ti.t = lambda_scope;
9761 ti.i = lambda_count;
9762 vec_safe_push (lambda_scope_stack, ti);
9763 if (lambda_scope != decl)
9764 {
9765 /* Don't reset the count if we're still in the same function. */
9766 lambda_scope = decl;
9767 lambda_count = 0;
9768 }
9769 }
9770
9771 static void
9772 record_lambda_scope (tree lambda)
9773 {
9774 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9775 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9776 }
9777
9778 static void
9779 finish_lambda_scope (void)
9780 {
9781 tree_int *p = &lambda_scope_stack->last ();
9782 if (lambda_scope != p->t)
9783 {
9784 lambda_scope = p->t;
9785 lambda_count = p->i;
9786 }
9787 lambda_scope_stack->pop ();
9788 }
9789
9790 /* Parse a lambda expression.
9791
9792 lambda-expression:
9793 lambda-introducer lambda-declarator [opt] compound-statement
9794
9795 Returns a representation of the expression. */
9796
9797 static cp_expr
9798 cp_parser_lambda_expression (cp_parser* parser)
9799 {
9800 tree lambda_expr = build_lambda_expr ();
9801 tree type;
9802 bool ok = true;
9803 cp_token *token = cp_lexer_peek_token (parser->lexer);
9804 cp_token_position start = 0;
9805
9806 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9807
9808 if (cp_unevaluated_operand)
9809 {
9810 if (!token->error_reported)
9811 {
9812 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9813 "lambda-expression in unevaluated context");
9814 token->error_reported = true;
9815 }
9816 ok = false;
9817 }
9818 else if (parser->in_template_argument_list_p)
9819 {
9820 if (!token->error_reported)
9821 {
9822 error_at (token->location, "lambda-expression in template-argument");
9823 token->error_reported = true;
9824 }
9825 ok = false;
9826 }
9827
9828 /* We may be in the middle of deferred access check. Disable
9829 it now. */
9830 push_deferring_access_checks (dk_no_deferred);
9831
9832 cp_parser_lambda_introducer (parser, lambda_expr);
9833
9834 type = begin_lambda_type (lambda_expr);
9835 if (type == error_mark_node)
9836 return error_mark_node;
9837
9838 record_lambda_scope (lambda_expr);
9839
9840 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9841 determine_visibility (TYPE_NAME (type));
9842
9843 /* Now that we've started the type, add the capture fields for any
9844 explicit captures. */
9845 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9846
9847 {
9848 /* Inside the class, surrounding template-parameter-lists do not apply. */
9849 unsigned int saved_num_template_parameter_lists
9850 = parser->num_template_parameter_lists;
9851 unsigned char in_statement = parser->in_statement;
9852 bool in_switch_statement_p = parser->in_switch_statement_p;
9853 bool fully_implicit_function_template_p
9854 = parser->fully_implicit_function_template_p;
9855 tree implicit_template_parms = parser->implicit_template_parms;
9856 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9857 bool auto_is_implicit_function_template_parm_p
9858 = parser->auto_is_implicit_function_template_parm_p;
9859
9860 parser->num_template_parameter_lists = 0;
9861 parser->in_statement = 0;
9862 parser->in_switch_statement_p = false;
9863 parser->fully_implicit_function_template_p = false;
9864 parser->implicit_template_parms = 0;
9865 parser->implicit_template_scope = 0;
9866 parser->auto_is_implicit_function_template_parm_p = false;
9867
9868 /* By virtue of defining a local class, a lambda expression has access to
9869 the private variables of enclosing classes. */
9870
9871 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9872
9873 if (ok && cp_parser_error_occurred (parser))
9874 ok = false;
9875
9876 if (ok)
9877 {
9878 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9879 && cp_parser_start_tentative_firewall (parser))
9880 start = token;
9881 cp_parser_lambda_body (parser, lambda_expr);
9882 }
9883 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9884 {
9885 if (cp_parser_skip_to_closing_brace (parser))
9886 cp_lexer_consume_token (parser->lexer);
9887 }
9888
9889 /* The capture list was built up in reverse order; fix that now. */
9890 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9891 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9892
9893 if (ok)
9894 maybe_add_lambda_conv_op (type);
9895
9896 type = finish_struct (type, /*attributes=*/NULL_TREE);
9897
9898 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9899 parser->in_statement = in_statement;
9900 parser->in_switch_statement_p = in_switch_statement_p;
9901 parser->fully_implicit_function_template_p
9902 = fully_implicit_function_template_p;
9903 parser->implicit_template_parms = implicit_template_parms;
9904 parser->implicit_template_scope = implicit_template_scope;
9905 parser->auto_is_implicit_function_template_parm_p
9906 = auto_is_implicit_function_template_parm_p;
9907 }
9908
9909 /* This field is only used during parsing of the lambda. */
9910 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9911
9912 /* This lambda shouldn't have any proxies left at this point. */
9913 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9914 /* And now that we're done, push proxies for an enclosing lambda. */
9915 insert_pending_capture_proxies ();
9916
9917 if (ok)
9918 lambda_expr = build_lambda_object (lambda_expr);
9919 else
9920 lambda_expr = error_mark_node;
9921
9922 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9923
9924 pop_deferring_access_checks ();
9925
9926 return lambda_expr;
9927 }
9928
9929 /* Parse the beginning of a lambda expression.
9930
9931 lambda-introducer:
9932 [ lambda-capture [opt] ]
9933
9934 LAMBDA_EXPR is the current representation of the lambda expression. */
9935
9936 static void
9937 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9938 {
9939 /* Need commas after the first capture. */
9940 bool first = true;
9941
9942 /* Eat the leading `['. */
9943 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9944
9945 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9946 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9947 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9948 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9949 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9950 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9951
9952 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9953 {
9954 cp_lexer_consume_token (parser->lexer);
9955 first = false;
9956 }
9957
9958 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9959 {
9960 cp_token* capture_token;
9961 tree capture_id;
9962 tree capture_init_expr;
9963 cp_id_kind idk = CP_ID_KIND_NONE;
9964 bool explicit_init_p = false;
9965
9966 enum capture_kind_type
9967 {
9968 BY_COPY,
9969 BY_REFERENCE
9970 };
9971 enum capture_kind_type capture_kind = BY_COPY;
9972
9973 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9974 {
9975 error ("expected end of capture-list");
9976 return;
9977 }
9978
9979 if (first)
9980 first = false;
9981 else
9982 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9983
9984 /* Possibly capture `this'. */
9985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9986 {
9987 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9988 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9989 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9990 "with by-copy capture default");
9991 cp_lexer_consume_token (parser->lexer);
9992 add_capture (lambda_expr,
9993 /*id=*/this_identifier,
9994 /*initializer=*/finish_this_expr (),
9995 /*by_reference_p=*/true,
9996 explicit_init_p);
9997 continue;
9998 }
9999
10000 /* Possibly capture `*this'. */
10001 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10002 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10003 {
10004 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10005 if (cxx_dialect < cxx1z)
10006 pedwarn (loc, 0, "%<*this%> capture only available with "
10007 "-std=c++1z or -std=gnu++1z");
10008 cp_lexer_consume_token (parser->lexer);
10009 cp_lexer_consume_token (parser->lexer);
10010 add_capture (lambda_expr,
10011 /*id=*/this_identifier,
10012 /*initializer=*/finish_this_expr (),
10013 /*by_reference_p=*/false,
10014 explicit_init_p);
10015 continue;
10016 }
10017
10018 /* Remember whether we want to capture as a reference or not. */
10019 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10020 {
10021 capture_kind = BY_REFERENCE;
10022 cp_lexer_consume_token (parser->lexer);
10023 }
10024
10025 /* Get the identifier. */
10026 capture_token = cp_lexer_peek_token (parser->lexer);
10027 capture_id = cp_parser_identifier (parser);
10028
10029 if (capture_id == error_mark_node)
10030 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10031 delimiters, but I modified this to stop on unnested ']' as well. It
10032 was already changed to stop on unnested '}', so the
10033 "closing_parenthesis" name is no more misleading with my change. */
10034 {
10035 cp_parser_skip_to_closing_parenthesis (parser,
10036 /*recovering=*/true,
10037 /*or_comma=*/true,
10038 /*consume_paren=*/true);
10039 break;
10040 }
10041
10042 /* Find the initializer for this capture. */
10043 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10044 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10045 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10046 {
10047 bool direct, non_constant;
10048 /* An explicit initializer exists. */
10049 if (cxx_dialect < cxx14)
10050 pedwarn (input_location, 0,
10051 "lambda capture initializers "
10052 "only available with -std=c++14 or -std=gnu++14");
10053 capture_init_expr = cp_parser_initializer (parser, &direct,
10054 &non_constant);
10055 explicit_init_p = true;
10056 if (capture_init_expr == NULL_TREE)
10057 {
10058 error ("empty initializer for lambda init-capture");
10059 capture_init_expr = error_mark_node;
10060 }
10061 }
10062 else
10063 {
10064 const char* error_msg;
10065
10066 /* Turn the identifier into an id-expression. */
10067 capture_init_expr
10068 = cp_parser_lookup_name_simple (parser, capture_id,
10069 capture_token->location);
10070
10071 if (capture_init_expr == error_mark_node)
10072 {
10073 unqualified_name_lookup_error (capture_id);
10074 continue;
10075 }
10076 else if (DECL_P (capture_init_expr)
10077 && (!VAR_P (capture_init_expr)
10078 && TREE_CODE (capture_init_expr) != PARM_DECL))
10079 {
10080 error_at (capture_token->location,
10081 "capture of non-variable %qD ",
10082 capture_init_expr);
10083 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10084 "%q#D declared here", capture_init_expr);
10085 continue;
10086 }
10087 if (VAR_P (capture_init_expr)
10088 && decl_storage_duration (capture_init_expr) != dk_auto)
10089 {
10090 if (pedwarn (capture_token->location, 0, "capture of variable "
10091 "%qD with non-automatic storage duration",
10092 capture_init_expr))
10093 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10094 "%q#D declared here", capture_init_expr);
10095 continue;
10096 }
10097
10098 capture_init_expr
10099 = finish_id_expression
10100 (capture_id,
10101 capture_init_expr,
10102 parser->scope,
10103 &idk,
10104 /*integral_constant_expression_p=*/false,
10105 /*allow_non_integral_constant_expression_p=*/false,
10106 /*non_integral_constant_expression_p=*/NULL,
10107 /*template_p=*/false,
10108 /*done=*/true,
10109 /*address_p=*/false,
10110 /*template_arg_p=*/false,
10111 &error_msg,
10112 capture_token->location);
10113
10114 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10115 {
10116 cp_lexer_consume_token (parser->lexer);
10117 capture_init_expr = make_pack_expansion (capture_init_expr);
10118 }
10119 else
10120 check_for_bare_parameter_packs (capture_init_expr);
10121 }
10122
10123 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10124 && !explicit_init_p)
10125 {
10126 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10127 && capture_kind == BY_COPY)
10128 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10129 "of %qD redundant with by-copy capture default",
10130 capture_id);
10131 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10132 && capture_kind == BY_REFERENCE)
10133 pedwarn (capture_token->location, 0, "explicit by-reference "
10134 "capture of %qD redundant with by-reference capture "
10135 "default", capture_id);
10136 }
10137
10138 add_capture (lambda_expr,
10139 capture_id,
10140 capture_init_expr,
10141 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10142 explicit_init_p);
10143 }
10144
10145 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10146 }
10147
10148 /* Parse the (optional) middle of a lambda expression.
10149
10150 lambda-declarator:
10151 < template-parameter-list [opt] >
10152 ( parameter-declaration-clause [opt] )
10153 attribute-specifier [opt]
10154 decl-specifier-seq [opt]
10155 exception-specification [opt]
10156 lambda-return-type-clause [opt]
10157
10158 LAMBDA_EXPR is the current representation of the lambda expression. */
10159
10160 static bool
10161 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10162 {
10163 /* 5.1.1.4 of the standard says:
10164 If a lambda-expression does not include a lambda-declarator, it is as if
10165 the lambda-declarator were ().
10166 This means an empty parameter list, no attributes, and no exception
10167 specification. */
10168 tree param_list = void_list_node;
10169 tree attributes = NULL_TREE;
10170 tree exception_spec = NULL_TREE;
10171 tree template_param_list = NULL_TREE;
10172 tree tx_qual = NULL_TREE;
10173 cp_decl_specifier_seq lambda_specs;
10174 clear_decl_specs (&lambda_specs);
10175
10176 /* The template-parameter-list is optional, but must begin with
10177 an opening angle if present. */
10178 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10179 {
10180 if (cxx_dialect < cxx14)
10181 pedwarn (parser->lexer->next_token->location, 0,
10182 "lambda templates are only available with "
10183 "-std=c++14 or -std=gnu++14");
10184 else
10185 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10186 "ISO C++ does not support lambda templates");
10187
10188 cp_lexer_consume_token (parser->lexer);
10189
10190 template_param_list = cp_parser_template_parameter_list (parser);
10191
10192 cp_parser_skip_to_end_of_template_parameter_list (parser);
10193
10194 /* We just processed one more parameter list. */
10195 ++parser->num_template_parameter_lists;
10196 }
10197
10198 /* The parameter-declaration-clause is optional (unless
10199 template-parameter-list was given), but must begin with an
10200 opening parenthesis if present. */
10201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10202 {
10203 cp_lexer_consume_token (parser->lexer);
10204
10205 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10206
10207 /* Parse parameters. */
10208 param_list = cp_parser_parameter_declaration_clause (parser);
10209
10210 /* Default arguments shall not be specified in the
10211 parameter-declaration-clause of a lambda-declarator. */
10212 if (cxx_dialect < cxx14)
10213 for (tree t = param_list; t; t = TREE_CHAIN (t))
10214 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10215 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10216 "default argument specified for lambda parameter");
10217
10218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10219
10220 attributes = cp_parser_attributes_opt (parser);
10221
10222 /* In the decl-specifier-seq of the lambda-declarator, each
10223 decl-specifier shall either be mutable or constexpr. */
10224 int declares_class_or_enum;
10225 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10226 cp_parser_decl_specifier_seq (parser,
10227 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10228 &lambda_specs, &declares_class_or_enum);
10229 if (lambda_specs.storage_class == sc_mutable)
10230 {
10231 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10232 if (lambda_specs.conflicting_specifiers_p)
10233 error_at (lambda_specs.locations[ds_storage_class],
10234 "duplicate %<mutable%>");
10235 }
10236
10237 tx_qual = cp_parser_tx_qualifier_opt (parser);
10238
10239 /* Parse optional exception specification. */
10240 exception_spec = cp_parser_exception_specification_opt (parser);
10241
10242 /* Parse optional trailing return type. */
10243 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10244 {
10245 cp_lexer_consume_token (parser->lexer);
10246 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10247 = cp_parser_trailing_type_id (parser);
10248 }
10249
10250 /* The function parameters must be in scope all the way until after the
10251 trailing-return-type in case of decltype. */
10252 pop_bindings_and_leave_scope ();
10253 }
10254 else if (template_param_list != NULL_TREE) // generate diagnostic
10255 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10256
10257 /* Create the function call operator.
10258
10259 Messing with declarators like this is no uglier than building up the
10260 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10261 other code. */
10262 {
10263 cp_decl_specifier_seq return_type_specs;
10264 cp_declarator* declarator;
10265 tree fco;
10266 int quals;
10267 void *p;
10268
10269 clear_decl_specs (&return_type_specs);
10270 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10271 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10272 else
10273 /* Maybe we will deduce the return type later. */
10274 return_type_specs.type = make_auto ();
10275
10276 if (lambda_specs.locations[ds_constexpr])
10277 {
10278 if (cxx_dialect >= cxx1z)
10279 return_type_specs.locations[ds_constexpr]
10280 = lambda_specs.locations[ds_constexpr];
10281 else
10282 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10283 "lambda only available with -std=c++1z or -std=gnu++1z");
10284 }
10285
10286 p = obstack_alloc (&declarator_obstack, 0);
10287
10288 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10289 sfk_none);
10290
10291 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10292 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10293 declarator = make_call_declarator (declarator, param_list, quals,
10294 VIRT_SPEC_UNSPECIFIED,
10295 REF_QUAL_NONE,
10296 tx_qual,
10297 exception_spec,
10298 /*late_return_type=*/NULL_TREE,
10299 /*requires_clause*/NULL_TREE);
10300 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10301
10302 fco = grokmethod (&return_type_specs,
10303 declarator,
10304 attributes);
10305 if (fco != error_mark_node)
10306 {
10307 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10308 DECL_ARTIFICIAL (fco) = 1;
10309 /* Give the object parameter a different name. */
10310 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10311 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10312 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10313 }
10314 if (template_param_list)
10315 {
10316 fco = finish_member_template_decl (fco);
10317 finish_template_decl (template_param_list);
10318 --parser->num_template_parameter_lists;
10319 }
10320 else if (parser->fully_implicit_function_template_p)
10321 fco = finish_fully_implicit_template (parser, fco);
10322
10323 finish_member_declaration (fco);
10324
10325 obstack_free (&declarator_obstack, p);
10326
10327 return (fco != error_mark_node);
10328 }
10329 }
10330
10331 /* Parse the body of a lambda expression, which is simply
10332
10333 compound-statement
10334
10335 but which requires special handling.
10336 LAMBDA_EXPR is the current representation of the lambda expression. */
10337
10338 static void
10339 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10340 {
10341 bool nested = (current_function_decl != NULL_TREE);
10342 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10343 if (nested)
10344 push_function_context ();
10345 else
10346 /* Still increment function_depth so that we don't GC in the
10347 middle of an expression. */
10348 ++function_depth;
10349 vec<tree> omp_privatization_save;
10350 save_omp_privatization_clauses (omp_privatization_save);
10351 /* Clear this in case we're in the middle of a default argument. */
10352 parser->local_variables_forbidden_p = false;
10353
10354 /* Finish the function call operator
10355 - class_specifier
10356 + late_parsing_for_member
10357 + function_definition_after_declarator
10358 + ctor_initializer_opt_and_function_body */
10359 {
10360 tree fco = lambda_function (lambda_expr);
10361 tree body;
10362 bool done = false;
10363 tree compound_stmt;
10364 tree cap;
10365
10366 /* Let the front end know that we are going to be defining this
10367 function. */
10368 start_preparsed_function (fco,
10369 NULL_TREE,
10370 SF_PRE_PARSED | SF_INCLASS_INLINE);
10371
10372 start_lambda_scope (fco);
10373 body = begin_function_body ();
10374
10375 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10376 goto out;
10377
10378 /* Push the proxies for any explicit captures. */
10379 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10380 cap = TREE_CHAIN (cap))
10381 build_capture_proxy (TREE_PURPOSE (cap));
10382
10383 compound_stmt = begin_compound_stmt (0);
10384
10385 /* 5.1.1.4 of the standard says:
10386 If a lambda-expression does not include a trailing-return-type, it
10387 is as if the trailing-return-type denotes the following type:
10388 * if the compound-statement is of the form
10389 { return attribute-specifier [opt] expression ; }
10390 the type of the returned expression after lvalue-to-rvalue
10391 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10392 (_conv.array_ 4.2), and function-to-pointer conversion
10393 (_conv.func_ 4.3);
10394 * otherwise, void. */
10395
10396 /* In a lambda that has neither a lambda-return-type-clause
10397 nor a deducible form, errors should be reported for return statements
10398 in the body. Since we used void as the placeholder return type, parsing
10399 the body as usual will give such desired behavior. */
10400 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10401 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10402 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10403 {
10404 tree expr = NULL_TREE;
10405 cp_id_kind idk = CP_ID_KIND_NONE;
10406
10407 /* Parse tentatively in case there's more after the initial return
10408 statement. */
10409 cp_parser_parse_tentatively (parser);
10410
10411 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10412
10413 expr = cp_parser_expression (parser, &idk);
10414
10415 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10416 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10417
10418 if (cp_parser_parse_definitely (parser))
10419 {
10420 if (!processing_template_decl)
10421 {
10422 tree type = lambda_return_type (expr);
10423 apply_deduced_return_type (fco, type);
10424 if (type == error_mark_node)
10425 expr = error_mark_node;
10426 }
10427
10428 /* Will get error here if type not deduced yet. */
10429 finish_return_stmt (expr);
10430
10431 done = true;
10432 }
10433 }
10434
10435 if (!done)
10436 {
10437 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10438 cp_parser_label_declaration (parser);
10439 cp_parser_statement_seq_opt (parser, NULL_TREE);
10440 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10441 }
10442
10443 finish_compound_stmt (compound_stmt);
10444
10445 out:
10446 finish_function_body (body);
10447 finish_lambda_scope ();
10448
10449 /* Finish the function and generate code for it if necessary. */
10450 tree fn = finish_function (/*inline*/2);
10451
10452 /* Only expand if the call op is not a template. */
10453 if (!DECL_TEMPLATE_INFO (fco))
10454 expand_or_defer_fn (fn);
10455 }
10456
10457 restore_omp_privatization_clauses (omp_privatization_save);
10458 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10459 if (nested)
10460 pop_function_context();
10461 else
10462 --function_depth;
10463 }
10464
10465 /* Statements [gram.stmt.stmt] */
10466
10467 /* Parse a statement.
10468
10469 statement:
10470 labeled-statement
10471 expression-statement
10472 compound-statement
10473 selection-statement
10474 iteration-statement
10475 jump-statement
10476 declaration-statement
10477 try-block
10478
10479 C++11:
10480
10481 statement:
10482 labeled-statement
10483 attribute-specifier-seq (opt) expression-statement
10484 attribute-specifier-seq (opt) compound-statement
10485 attribute-specifier-seq (opt) selection-statement
10486 attribute-specifier-seq (opt) iteration-statement
10487 attribute-specifier-seq (opt) jump-statement
10488 declaration-statement
10489 attribute-specifier-seq (opt) try-block
10490
10491 init-statement:
10492 expression-statement
10493 simple-declaration
10494
10495 TM Extension:
10496
10497 statement:
10498 atomic-statement
10499
10500 IN_COMPOUND is true when the statement is nested inside a
10501 cp_parser_compound_statement; this matters for certain pragmas.
10502
10503 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10504 is a (possibly labeled) if statement which is not enclosed in braces
10505 and has an else clause. This is used to implement -Wparentheses.
10506
10507 CHAIN is a vector of if-else-if conditions. */
10508
10509 static void
10510 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10511 bool in_compound, bool *if_p, vec<tree> *chain)
10512 {
10513 tree statement, std_attrs = NULL_TREE;
10514 cp_token *token;
10515 location_t statement_location, attrs_location;
10516
10517 restart:
10518 if (if_p != NULL)
10519 *if_p = false;
10520 /* There is no statement yet. */
10521 statement = NULL_TREE;
10522
10523 saved_token_sentinel saved_tokens (parser->lexer);
10524 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10525 if (c_dialect_objc ())
10526 /* In obj-c++, seeing '[[' might be the either the beginning of
10527 c++11 attributes, or a nested objc-message-expression. So
10528 let's parse the c++11 attributes tentatively. */
10529 cp_parser_parse_tentatively (parser);
10530 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10531 if (c_dialect_objc ())
10532 {
10533 if (!cp_parser_parse_definitely (parser))
10534 std_attrs = NULL_TREE;
10535 }
10536
10537 /* Peek at the next token. */
10538 token = cp_lexer_peek_token (parser->lexer);
10539 /* Remember the location of the first token in the statement. */
10540 statement_location = token->location;
10541 /* If this is a keyword, then that will often determine what kind of
10542 statement we have. */
10543 if (token->type == CPP_KEYWORD)
10544 {
10545 enum rid keyword = token->keyword;
10546
10547 switch (keyword)
10548 {
10549 case RID_CASE:
10550 case RID_DEFAULT:
10551 /* Looks like a labeled-statement with a case label.
10552 Parse the label, and then use tail recursion to parse
10553 the statement. */
10554 cp_parser_label_for_labeled_statement (parser, std_attrs);
10555 in_compound = false;
10556 goto restart;
10557
10558 case RID_IF:
10559 case RID_SWITCH:
10560 statement = cp_parser_selection_statement (parser, if_p, chain);
10561 break;
10562
10563 case RID_WHILE:
10564 case RID_DO:
10565 case RID_FOR:
10566 statement = cp_parser_iteration_statement (parser, if_p, false);
10567 break;
10568
10569 case RID_CILK_FOR:
10570 if (!flag_cilkplus)
10571 {
10572 error_at (cp_lexer_peek_token (parser->lexer)->location,
10573 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10574 cp_lexer_consume_token (parser->lexer);
10575 statement = error_mark_node;
10576 }
10577 else
10578 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10579 break;
10580
10581 case RID_BREAK:
10582 case RID_CONTINUE:
10583 case RID_RETURN:
10584 case RID_GOTO:
10585 statement = cp_parser_jump_statement (parser);
10586 break;
10587
10588 case RID_CILK_SYNC:
10589 cp_lexer_consume_token (parser->lexer);
10590 if (flag_cilkplus)
10591 {
10592 tree sync_expr = build_cilk_sync ();
10593 SET_EXPR_LOCATION (sync_expr,
10594 token->location);
10595 statement = finish_expr_stmt (sync_expr);
10596 }
10597 else
10598 {
10599 error_at (token->location, "-fcilkplus must be enabled to use"
10600 " %<_Cilk_sync%>");
10601 statement = error_mark_node;
10602 }
10603 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10604 break;
10605
10606 /* Objective-C++ exception-handling constructs. */
10607 case RID_AT_TRY:
10608 case RID_AT_CATCH:
10609 case RID_AT_FINALLY:
10610 case RID_AT_SYNCHRONIZED:
10611 case RID_AT_THROW:
10612 statement = cp_parser_objc_statement (parser);
10613 break;
10614
10615 case RID_TRY:
10616 statement = cp_parser_try_block (parser);
10617 break;
10618
10619 case RID_NAMESPACE:
10620 /* This must be a namespace alias definition. */
10621 cp_parser_declaration_statement (parser);
10622 return;
10623
10624 case RID_TRANSACTION_ATOMIC:
10625 case RID_TRANSACTION_RELAXED:
10626 case RID_SYNCHRONIZED:
10627 case RID_ATOMIC_NOEXCEPT:
10628 case RID_ATOMIC_CANCEL:
10629 statement = cp_parser_transaction (parser, token);
10630 break;
10631 case RID_TRANSACTION_CANCEL:
10632 statement = cp_parser_transaction_cancel (parser);
10633 break;
10634
10635 default:
10636 /* It might be a keyword like `int' that can start a
10637 declaration-statement. */
10638 break;
10639 }
10640 }
10641 else if (token->type == CPP_NAME)
10642 {
10643 /* If the next token is a `:', then we are looking at a
10644 labeled-statement. */
10645 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10646 if (token->type == CPP_COLON)
10647 {
10648 /* Looks like a labeled-statement with an ordinary label.
10649 Parse the label, and then use tail recursion to parse
10650 the statement. */
10651
10652 cp_parser_label_for_labeled_statement (parser, std_attrs);
10653 in_compound = false;
10654 goto restart;
10655 }
10656 }
10657 /* Anything that starts with a `{' must be a compound-statement. */
10658 else if (token->type == CPP_OPEN_BRACE)
10659 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10660 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10661 a statement all its own. */
10662 else if (token->type == CPP_PRAGMA)
10663 {
10664 /* Only certain OpenMP pragmas are attached to statements, and thus
10665 are considered statements themselves. All others are not. In
10666 the context of a compound, accept the pragma as a "statement" and
10667 return so that we can check for a close brace. Otherwise we
10668 require a real statement and must go back and read one. */
10669 if (in_compound)
10670 cp_parser_pragma (parser, pragma_compound, if_p);
10671 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10672 goto restart;
10673 return;
10674 }
10675 else if (token->type == CPP_EOF)
10676 {
10677 cp_parser_error (parser, "expected statement");
10678 return;
10679 }
10680
10681 /* Everything else must be a declaration-statement or an
10682 expression-statement. Try for the declaration-statement
10683 first, unless we are looking at a `;', in which case we know that
10684 we have an expression-statement. */
10685 if (!statement)
10686 {
10687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10688 {
10689 if (std_attrs != NULL_TREE)
10690 {
10691 /* Attributes should be parsed as part of the the
10692 declaration, so let's un-parse them. */
10693 saved_tokens.rollback();
10694 std_attrs = NULL_TREE;
10695 }
10696
10697 cp_parser_parse_tentatively (parser);
10698 /* Try to parse the declaration-statement. */
10699 cp_parser_declaration_statement (parser);
10700 /* If that worked, we're done. */
10701 if (cp_parser_parse_definitely (parser))
10702 return;
10703 }
10704 /* Look for an expression-statement instead. */
10705 statement = cp_parser_expression_statement (parser, in_statement_expr);
10706
10707 /* Handle [[fallthrough]];. */
10708 if (attribute_fallthrough_p (std_attrs))
10709 {
10710 /* The next token after the fallthrough attribute is ';'. */
10711 if (statement == NULL_TREE)
10712 {
10713 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10714 statement = build_call_expr_internal_loc (statement_location,
10715 IFN_FALLTHROUGH,
10716 void_type_node, 0);
10717 finish_expr_stmt (statement);
10718 }
10719 else
10720 warning_at (statement_location, OPT_Wattributes,
10721 "%<fallthrough%> attribute not followed by %<;%>");
10722 std_attrs = NULL_TREE;
10723 }
10724 }
10725
10726 /* Set the line number for the statement. */
10727 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10728 SET_EXPR_LOCATION (statement, statement_location);
10729
10730 /* Allow "[[fallthrough]];", but warn otherwise. */
10731 if (std_attrs != NULL_TREE)
10732 warning_at (attrs_location,
10733 OPT_Wattributes,
10734 "attributes at the beginning of statement are ignored");
10735 }
10736
10737 /* Parse the label for a labeled-statement, i.e.
10738
10739 identifier :
10740 case constant-expression :
10741 default :
10742
10743 GNU Extension:
10744 case constant-expression ... constant-expression : statement
10745
10746 When a label is parsed without errors, the label is added to the
10747 parse tree by the finish_* functions, so this function doesn't
10748 have to return the label. */
10749
10750 static void
10751 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10752 {
10753 cp_token *token;
10754 tree label = NULL_TREE;
10755 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10756
10757 /* The next token should be an identifier. */
10758 token = cp_lexer_peek_token (parser->lexer);
10759 if (token->type != CPP_NAME
10760 && token->type != CPP_KEYWORD)
10761 {
10762 cp_parser_error (parser, "expected labeled-statement");
10763 return;
10764 }
10765
10766 /* Remember whether this case or a user-defined label is allowed to fall
10767 through to. */
10768 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10769
10770 parser->colon_corrects_to_scope_p = false;
10771 switch (token->keyword)
10772 {
10773 case RID_CASE:
10774 {
10775 tree expr, expr_hi;
10776 cp_token *ellipsis;
10777
10778 /* Consume the `case' token. */
10779 cp_lexer_consume_token (parser->lexer);
10780 /* Parse the constant-expression. */
10781 expr = cp_parser_constant_expression (parser);
10782 if (check_for_bare_parameter_packs (expr))
10783 expr = error_mark_node;
10784
10785 ellipsis = cp_lexer_peek_token (parser->lexer);
10786 if (ellipsis->type == CPP_ELLIPSIS)
10787 {
10788 /* Consume the `...' token. */
10789 cp_lexer_consume_token (parser->lexer);
10790 expr_hi = cp_parser_constant_expression (parser);
10791 if (check_for_bare_parameter_packs (expr_hi))
10792 expr_hi = error_mark_node;
10793
10794 /* We don't need to emit warnings here, as the common code
10795 will do this for us. */
10796 }
10797 else
10798 expr_hi = NULL_TREE;
10799
10800 if (parser->in_switch_statement_p)
10801 {
10802 tree l = finish_case_label (token->location, expr, expr_hi);
10803 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10804 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10805 }
10806 else
10807 error_at (token->location,
10808 "case label %qE not within a switch statement",
10809 expr);
10810 }
10811 break;
10812
10813 case RID_DEFAULT:
10814 /* Consume the `default' token. */
10815 cp_lexer_consume_token (parser->lexer);
10816
10817 if (parser->in_switch_statement_p)
10818 {
10819 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10820 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10821 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10822 }
10823 else
10824 error_at (token->location, "case label not within a switch statement");
10825 break;
10826
10827 default:
10828 /* Anything else must be an ordinary label. */
10829 label = finish_label_stmt (cp_parser_identifier (parser));
10830 if (label && TREE_CODE (label) == LABEL_DECL)
10831 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10832 break;
10833 }
10834
10835 /* Require the `:' token. */
10836 cp_parser_require (parser, CPP_COLON, RT_COLON);
10837
10838 /* An ordinary label may optionally be followed by attributes.
10839 However, this is only permitted if the attributes are then
10840 followed by a semicolon. This is because, for backward
10841 compatibility, when parsing
10842 lab: __attribute__ ((unused)) int i;
10843 we want the attribute to attach to "i", not "lab". */
10844 if (label != NULL_TREE
10845 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10846 {
10847 tree attrs;
10848 cp_parser_parse_tentatively (parser);
10849 attrs = cp_parser_gnu_attributes_opt (parser);
10850 if (attrs == NULL_TREE
10851 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10852 cp_parser_abort_tentative_parse (parser);
10853 else if (!cp_parser_parse_definitely (parser))
10854 ;
10855 else
10856 attributes = chainon (attributes, attrs);
10857 }
10858
10859 if (attributes != NULL_TREE)
10860 cplus_decl_attributes (&label, attributes, 0);
10861
10862 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10863 }
10864
10865 /* Parse an expression-statement.
10866
10867 expression-statement:
10868 expression [opt] ;
10869
10870 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10871 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10872 indicates whether this expression-statement is part of an
10873 expression statement. */
10874
10875 static tree
10876 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10877 {
10878 tree statement = NULL_TREE;
10879 cp_token *token = cp_lexer_peek_token (parser->lexer);
10880 location_t loc = token->location;
10881
10882 /* There might be attribute fallthrough. */
10883 tree attr = cp_parser_gnu_attributes_opt (parser);
10884
10885 /* If the next token is a ';', then there is no expression
10886 statement. */
10887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10888 {
10889 statement = cp_parser_expression (parser);
10890 if (statement == error_mark_node
10891 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10892 {
10893 cp_parser_skip_to_end_of_block_or_statement (parser);
10894 return error_mark_node;
10895 }
10896 }
10897
10898 /* Handle [[fallthrough]];. */
10899 if (attribute_fallthrough_p (attr))
10900 {
10901 /* The next token after the fallthrough attribute is ';'. */
10902 if (statement == NULL_TREE)
10903 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10904 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10905 void_type_node, 0);
10906 else
10907 warning_at (loc, OPT_Wattributes,
10908 "%<fallthrough%> attribute not followed by %<;%>");
10909 attr = NULL_TREE;
10910 }
10911
10912 /* Allow "[[fallthrough]];", but warn otherwise. */
10913 if (attr != NULL_TREE)
10914 warning_at (loc, OPT_Wattributes,
10915 "attributes at the beginning of statement are ignored");
10916
10917 /* Give a helpful message for "A<T>::type t;" and the like. */
10918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10919 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10920 {
10921 if (TREE_CODE (statement) == SCOPE_REF)
10922 error_at (token->location, "need %<typename%> before %qE because "
10923 "%qT is a dependent scope",
10924 statement, TREE_OPERAND (statement, 0));
10925 else if (is_overloaded_fn (statement)
10926 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10927 {
10928 /* A::A a; */
10929 tree fn = get_first_fn (statement);
10930 error_at (token->location,
10931 "%<%T::%D%> names the constructor, not the type",
10932 DECL_CONTEXT (fn), DECL_NAME (fn));
10933 }
10934 }
10935
10936 /* Consume the final `;'. */
10937 cp_parser_consume_semicolon_at_end_of_statement (parser);
10938
10939 if (in_statement_expr
10940 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10941 /* This is the final expression statement of a statement
10942 expression. */
10943 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10944 else if (statement)
10945 statement = finish_expr_stmt (statement);
10946
10947 return statement;
10948 }
10949
10950 /* Parse a compound-statement.
10951
10952 compound-statement:
10953 { statement-seq [opt] }
10954
10955 GNU extension:
10956
10957 compound-statement:
10958 { label-declaration-seq [opt] statement-seq [opt] }
10959
10960 label-declaration-seq:
10961 label-declaration
10962 label-declaration-seq label-declaration
10963
10964 Returns a tree representing the statement. */
10965
10966 static tree
10967 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10968 int bcs_flags, bool function_body)
10969 {
10970 tree compound_stmt;
10971
10972 /* Consume the `{'. */
10973 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10974 return error_mark_node;
10975 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10976 && !function_body && cxx_dialect < cxx14)
10977 pedwarn (input_location, OPT_Wpedantic,
10978 "compound-statement in constexpr function");
10979 /* Begin the compound-statement. */
10980 compound_stmt = begin_compound_stmt (bcs_flags);
10981 /* If the next keyword is `__label__' we have a label declaration. */
10982 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10983 cp_parser_label_declaration (parser);
10984 /* Parse an (optional) statement-seq. */
10985 cp_parser_statement_seq_opt (parser, in_statement_expr);
10986 /* Finish the compound-statement. */
10987 finish_compound_stmt (compound_stmt);
10988 /* Consume the `}'. */
10989 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10990
10991 return compound_stmt;
10992 }
10993
10994 /* Parse an (optional) statement-seq.
10995
10996 statement-seq:
10997 statement
10998 statement-seq [opt] statement */
10999
11000 static void
11001 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11002 {
11003 /* Scan statements until there aren't any more. */
11004 while (true)
11005 {
11006 cp_token *token = cp_lexer_peek_token (parser->lexer);
11007
11008 /* If we are looking at a `}', then we have run out of
11009 statements; the same is true if we have reached the end
11010 of file, or have stumbled upon a stray '@end'. */
11011 if (token->type == CPP_CLOSE_BRACE
11012 || token->type == CPP_EOF
11013 || token->type == CPP_PRAGMA_EOL
11014 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11015 break;
11016
11017 /* If we are in a compound statement and find 'else' then
11018 something went wrong. */
11019 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11020 {
11021 if (parser->in_statement & IN_IF_STMT)
11022 break;
11023 else
11024 {
11025 token = cp_lexer_consume_token (parser->lexer);
11026 error_at (token->location, "%<else%> without a previous %<if%>");
11027 }
11028 }
11029
11030 /* Parse the statement. */
11031 cp_parser_statement (parser, in_statement_expr, true, NULL);
11032 }
11033 }
11034
11035 /* Return true if we're looking at (init; cond), false otherwise. */
11036
11037 static bool
11038 cp_parser_init_statement_p (cp_parser *parser)
11039 {
11040 /* Save tokens so that we can put them back. */
11041 cp_lexer_save_tokens (parser->lexer);
11042
11043 /* Look for ';' that is not nested in () or {}. */
11044 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11045 /*recovering=*/false,
11046 CPP_SEMICOLON,
11047 /*consume_paren=*/false);
11048
11049 /* Roll back the tokens we skipped. */
11050 cp_lexer_rollback_tokens (parser->lexer);
11051
11052 return ret == -1;
11053 }
11054
11055 /* Parse a selection-statement.
11056
11057 selection-statement:
11058 if ( init-statement [opt] condition ) statement
11059 if ( init-statement [opt] condition ) statement else statement
11060 switch ( init-statement [opt] condition ) statement
11061
11062 Returns the new IF_STMT or SWITCH_STMT.
11063
11064 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11065 is a (possibly labeled) if statement which is not enclosed in
11066 braces and has an else clause. This is used to implement
11067 -Wparentheses.
11068
11069 CHAIN is a vector of if-else-if conditions. This is used to implement
11070 -Wduplicated-cond. */
11071
11072 static tree
11073 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11074 vec<tree> *chain)
11075 {
11076 cp_token *token;
11077 enum rid keyword;
11078 token_indent_info guard_tinfo;
11079
11080 if (if_p != NULL)
11081 *if_p = false;
11082
11083 /* Peek at the next token. */
11084 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11085 guard_tinfo = get_token_indent_info (token);
11086
11087 /* See what kind of keyword it is. */
11088 keyword = token->keyword;
11089 switch (keyword)
11090 {
11091 case RID_IF:
11092 case RID_SWITCH:
11093 {
11094 tree statement;
11095 tree condition;
11096
11097 bool cx = false;
11098 if (keyword == RID_IF
11099 && cp_lexer_next_token_is_keyword (parser->lexer,
11100 RID_CONSTEXPR))
11101 {
11102 cx = true;
11103 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11104 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11105 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11106 "with -std=c++1z or -std=gnu++1z");
11107 }
11108
11109 /* Look for the `('. */
11110 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11111 {
11112 cp_parser_skip_to_end_of_statement (parser);
11113 return error_mark_node;
11114 }
11115
11116 /* Begin the selection-statement. */
11117 if (keyword == RID_IF)
11118 {
11119 statement = begin_if_stmt ();
11120 IF_STMT_CONSTEXPR_P (statement) = cx;
11121 }
11122 else
11123 statement = begin_switch_stmt ();
11124
11125 /* Parse the optional init-statement. */
11126 if (cp_parser_init_statement_p (parser))
11127 {
11128 tree decl;
11129 if (cxx_dialect < cxx1z)
11130 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11131 "init-statement in selection statements only available "
11132 "with -std=c++1z or -std=gnu++1z");
11133 cp_parser_init_statement (parser, &decl);
11134 }
11135
11136 /* Parse the condition. */
11137 condition = cp_parser_condition (parser);
11138 /* Look for the `)'. */
11139 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11140 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11141 /*consume_paren=*/true);
11142
11143 if (keyword == RID_IF)
11144 {
11145 bool nested_if;
11146 unsigned char in_statement;
11147
11148 /* Add the condition. */
11149 condition = finish_if_stmt_cond (condition, statement);
11150
11151 if (warn_duplicated_cond)
11152 warn_duplicated_cond_add_or_warn (token->location, condition,
11153 &chain);
11154
11155 /* Parse the then-clause. */
11156 in_statement = parser->in_statement;
11157 parser->in_statement |= IN_IF_STMT;
11158
11159 /* Outside a template, the non-selected branch of a constexpr
11160 if is a 'discarded statement', i.e. unevaluated. */
11161 bool was_discarded = in_discarded_stmt;
11162 bool discard_then = (cx && !processing_template_decl
11163 && integer_zerop (condition));
11164 if (discard_then)
11165 {
11166 in_discarded_stmt = true;
11167 ++c_inhibit_evaluation_warnings;
11168 }
11169
11170 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11171 guard_tinfo);
11172
11173 parser->in_statement = in_statement;
11174
11175 finish_then_clause (statement);
11176
11177 if (discard_then)
11178 {
11179 THEN_CLAUSE (statement) = NULL_TREE;
11180 in_discarded_stmt = was_discarded;
11181 --c_inhibit_evaluation_warnings;
11182 }
11183
11184 /* If the next token is `else', parse the else-clause. */
11185 if (cp_lexer_next_token_is_keyword (parser->lexer,
11186 RID_ELSE))
11187 {
11188 bool discard_else = (cx && !processing_template_decl
11189 && integer_nonzerop (condition));
11190 if (discard_else)
11191 {
11192 in_discarded_stmt = true;
11193 ++c_inhibit_evaluation_warnings;
11194 }
11195
11196 guard_tinfo
11197 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11198 /* Consume the `else' keyword. */
11199 cp_lexer_consume_token (parser->lexer);
11200 if (warn_duplicated_cond)
11201 {
11202 if (cp_lexer_next_token_is_keyword (parser->lexer,
11203 RID_IF)
11204 && chain == NULL)
11205 {
11206 /* We've got "if (COND) else if (COND2)". Start
11207 the condition chain and add COND as the first
11208 element. */
11209 chain = new vec<tree> ();
11210 if (!CONSTANT_CLASS_P (condition)
11211 && !TREE_SIDE_EFFECTS (condition))
11212 {
11213 /* Wrap it in a NOP_EXPR so that we can set the
11214 location of the condition. */
11215 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11216 condition);
11217 SET_EXPR_LOCATION (e, token->location);
11218 chain->safe_push (e);
11219 }
11220 }
11221 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11222 RID_IF))
11223 {
11224 /* This is if-else without subsequent if. Zap the
11225 condition chain; we would have already warned at
11226 this point. */
11227 delete chain;
11228 chain = NULL;
11229 }
11230 }
11231 begin_else_clause (statement);
11232 /* Parse the else-clause. */
11233 cp_parser_implicitly_scoped_statement (parser, NULL,
11234 guard_tinfo, chain);
11235
11236 finish_else_clause (statement);
11237
11238 /* If we are currently parsing a then-clause, then
11239 IF_P will not be NULL. We set it to true to
11240 indicate that this if statement has an else clause.
11241 This may trigger the Wparentheses warning below
11242 when we get back up to the parent if statement. */
11243 if (if_p != NULL)
11244 *if_p = true;
11245
11246 if (discard_else)
11247 {
11248 ELSE_CLAUSE (statement) = NULL_TREE;
11249 in_discarded_stmt = was_discarded;
11250 --c_inhibit_evaluation_warnings;
11251 }
11252 }
11253 else
11254 {
11255 /* This if statement does not have an else clause. If
11256 NESTED_IF is true, then the then-clause has an if
11257 statement which does have an else clause. We warn
11258 about the potential ambiguity. */
11259 if (nested_if)
11260 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11261 "suggest explicit braces to avoid ambiguous"
11262 " %<else%>");
11263 if (warn_duplicated_cond)
11264 {
11265 /* We don't need the condition chain anymore. */
11266 delete chain;
11267 chain = NULL;
11268 }
11269 }
11270
11271 /* Now we're all done with the if-statement. */
11272 finish_if_stmt (statement);
11273 }
11274 else
11275 {
11276 bool in_switch_statement_p;
11277 unsigned char in_statement;
11278
11279 /* Add the condition. */
11280 finish_switch_cond (condition, statement);
11281
11282 /* Parse the body of the switch-statement. */
11283 in_switch_statement_p = parser->in_switch_statement_p;
11284 in_statement = parser->in_statement;
11285 parser->in_switch_statement_p = true;
11286 parser->in_statement |= IN_SWITCH_STMT;
11287 cp_parser_implicitly_scoped_statement (parser, if_p,
11288 guard_tinfo);
11289 parser->in_switch_statement_p = in_switch_statement_p;
11290 parser->in_statement = in_statement;
11291
11292 /* Now we're all done with the switch-statement. */
11293 finish_switch_stmt (statement);
11294 }
11295
11296 return statement;
11297 }
11298 break;
11299
11300 default:
11301 cp_parser_error (parser, "expected selection-statement");
11302 return error_mark_node;
11303 }
11304 }
11305
11306 /* Parse a condition.
11307
11308 condition:
11309 expression
11310 type-specifier-seq declarator = initializer-clause
11311 type-specifier-seq declarator braced-init-list
11312
11313 GNU Extension:
11314
11315 condition:
11316 type-specifier-seq declarator asm-specification [opt]
11317 attributes [opt] = assignment-expression
11318
11319 Returns the expression that should be tested. */
11320
11321 static tree
11322 cp_parser_condition (cp_parser* parser)
11323 {
11324 cp_decl_specifier_seq type_specifiers;
11325 const char *saved_message;
11326 int declares_class_or_enum;
11327
11328 /* Try the declaration first. */
11329 cp_parser_parse_tentatively (parser);
11330 /* New types are not allowed in the type-specifier-seq for a
11331 condition. */
11332 saved_message = parser->type_definition_forbidden_message;
11333 parser->type_definition_forbidden_message
11334 = G_("types may not be defined in conditions");
11335 /* Parse the type-specifier-seq. */
11336 cp_parser_decl_specifier_seq (parser,
11337 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11338 &type_specifiers,
11339 &declares_class_or_enum);
11340 /* Restore the saved message. */
11341 parser->type_definition_forbidden_message = saved_message;
11342 /* If all is well, we might be looking at a declaration. */
11343 if (!cp_parser_error_occurred (parser))
11344 {
11345 tree decl;
11346 tree asm_specification;
11347 tree attributes;
11348 cp_declarator *declarator;
11349 tree initializer = NULL_TREE;
11350
11351 /* Parse the declarator. */
11352 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11353 /*ctor_dtor_or_conv_p=*/NULL,
11354 /*parenthesized_p=*/NULL,
11355 /*member_p=*/false,
11356 /*friend_p=*/false);
11357 /* Parse the attributes. */
11358 attributes = cp_parser_attributes_opt (parser);
11359 /* Parse the asm-specification. */
11360 asm_specification = cp_parser_asm_specification_opt (parser);
11361 /* If the next token is not an `=' or '{', then we might still be
11362 looking at an expression. For example:
11363
11364 if (A(a).x)
11365
11366 looks like a decl-specifier-seq and a declarator -- but then
11367 there is no `=', so this is an expression. */
11368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11369 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11370 cp_parser_simulate_error (parser);
11371
11372 /* If we did see an `=' or '{', then we are looking at a declaration
11373 for sure. */
11374 if (cp_parser_parse_definitely (parser))
11375 {
11376 tree pushed_scope;
11377 bool non_constant_p;
11378 int flags = LOOKUP_ONLYCONVERTING;
11379
11380 /* Create the declaration. */
11381 decl = start_decl (declarator, &type_specifiers,
11382 /*initialized_p=*/true,
11383 attributes, /*prefix_attributes=*/NULL_TREE,
11384 &pushed_scope);
11385
11386 /* Parse the initializer. */
11387 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11388 {
11389 initializer = cp_parser_braced_list (parser, &non_constant_p);
11390 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11391 flags = 0;
11392 }
11393 else
11394 {
11395 /* Consume the `='. */
11396 cp_parser_require (parser, CPP_EQ, RT_EQ);
11397 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11398 }
11399 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11400 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11401
11402 /* Process the initializer. */
11403 cp_finish_decl (decl,
11404 initializer, !non_constant_p,
11405 asm_specification,
11406 flags);
11407
11408 if (pushed_scope)
11409 pop_scope (pushed_scope);
11410
11411 return convert_from_reference (decl);
11412 }
11413 }
11414 /* If we didn't even get past the declarator successfully, we are
11415 definitely not looking at a declaration. */
11416 else
11417 cp_parser_abort_tentative_parse (parser);
11418
11419 /* Otherwise, we are looking at an expression. */
11420 return cp_parser_expression (parser);
11421 }
11422
11423 /* Parses a for-statement or range-for-statement until the closing ')',
11424 not included. */
11425
11426 static tree
11427 cp_parser_for (cp_parser *parser, bool ivdep)
11428 {
11429 tree init, scope, decl;
11430 bool is_range_for;
11431
11432 /* Begin the for-statement. */
11433 scope = begin_for_scope (&init);
11434
11435 /* Parse the initialization. */
11436 is_range_for = cp_parser_init_statement (parser, &decl);
11437
11438 if (is_range_for)
11439 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11440 else
11441 return cp_parser_c_for (parser, scope, init, ivdep);
11442 }
11443
11444 static tree
11445 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11446 {
11447 /* Normal for loop */
11448 tree condition = NULL_TREE;
11449 tree expression = NULL_TREE;
11450 tree stmt;
11451
11452 stmt = begin_for_stmt (scope, init);
11453 /* The init-statement has already been parsed in
11454 cp_parser_init_statement, so no work is needed here. */
11455 finish_init_stmt (stmt);
11456
11457 /* If there's a condition, process it. */
11458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11459 condition = cp_parser_condition (parser);
11460 else if (ivdep)
11461 {
11462 cp_parser_error (parser, "missing loop condition in loop with "
11463 "%<GCC ivdep%> pragma");
11464 condition = error_mark_node;
11465 }
11466 finish_for_cond (condition, stmt, ivdep);
11467 /* Look for the `;'. */
11468 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11469
11470 /* If there's an expression, process it. */
11471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11472 expression = cp_parser_expression (parser);
11473 finish_for_expr (expression, stmt);
11474
11475 return stmt;
11476 }
11477
11478 /* Tries to parse a range-based for-statement:
11479
11480 range-based-for:
11481 decl-specifier-seq declarator : expression
11482
11483 The decl-specifier-seq declarator and the `:' are already parsed by
11484 cp_parser_init_statement. If processing_template_decl it returns a
11485 newly created RANGE_FOR_STMT; if not, it is converted to a
11486 regular FOR_STMT. */
11487
11488 static tree
11489 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11490 bool ivdep)
11491 {
11492 tree stmt, range_expr;
11493 auto_vec <cxx_binding *, 16> bindings;
11494 auto_vec <tree, 16> names;
11495 tree decomp_first_name = NULL_TREE;
11496 unsigned int decomp_cnt = 0;
11497
11498 /* Get the range declaration momentarily out of the way so that
11499 the range expression doesn't clash with it. */
11500 if (range_decl != error_mark_node)
11501 {
11502 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11503 {
11504 tree v = DECL_VALUE_EXPR (range_decl);
11505 /* For decomposition declaration get all of the corresponding
11506 declarations out of the way. */
11507 if (TREE_CODE (v) == ARRAY_REF
11508 && VAR_P (TREE_OPERAND (v, 0))
11509 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11510 {
11511 tree d = range_decl;
11512 range_decl = TREE_OPERAND (v, 0);
11513 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11514 decomp_first_name = d;
11515 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11516 {
11517 tree name = DECL_NAME (d);
11518 names.safe_push (name);
11519 bindings.safe_push (IDENTIFIER_BINDING (name));
11520 IDENTIFIER_BINDING (name)
11521 = IDENTIFIER_BINDING (name)->previous;
11522 }
11523 }
11524 }
11525 if (names.is_empty ())
11526 {
11527 tree name = DECL_NAME (range_decl);
11528 names.safe_push (name);
11529 bindings.safe_push (IDENTIFIER_BINDING (name));
11530 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11531 }
11532 }
11533
11534 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11535 {
11536 bool expr_non_constant_p;
11537 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11538 }
11539 else
11540 range_expr = cp_parser_expression (parser);
11541
11542 /* Put the range declaration(s) back into scope. */
11543 for (unsigned int i = 0; i < names.length (); i++)
11544 {
11545 cxx_binding *binding = bindings[i];
11546 binding->previous = IDENTIFIER_BINDING (names[i]);
11547 IDENTIFIER_BINDING (names[i]) = binding;
11548 }
11549
11550 /* If in template, STMT is converted to a normal for-statement
11551 at instantiation. If not, it is done just ahead. */
11552 if (processing_template_decl)
11553 {
11554 if (check_for_bare_parameter_packs (range_expr))
11555 range_expr = error_mark_node;
11556 stmt = begin_range_for_stmt (scope, init);
11557 if (ivdep)
11558 RANGE_FOR_IVDEP (stmt) = 1;
11559 finish_range_for_decl (stmt, range_decl, range_expr);
11560 if (!type_dependent_expression_p (range_expr)
11561 /* do_auto_deduction doesn't mess with template init-lists. */
11562 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11563 do_range_for_auto_deduction (range_decl, range_expr);
11564 }
11565 else
11566 {
11567 stmt = begin_for_stmt (scope, init);
11568 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11569 decomp_first_name, decomp_cnt, ivdep);
11570 }
11571 return stmt;
11572 }
11573
11574 /* Subroutine of cp_convert_range_for: given the initializer expression,
11575 builds up the range temporary. */
11576
11577 static tree
11578 build_range_temp (tree range_expr)
11579 {
11580 tree range_type, range_temp;
11581
11582 /* Find out the type deduced by the declaration
11583 `auto &&__range = range_expr'. */
11584 range_type = cp_build_reference_type (make_auto (), true);
11585 range_type = do_auto_deduction (range_type, range_expr,
11586 type_uses_auto (range_type));
11587
11588 /* Create the __range variable. */
11589 range_temp = build_decl (input_location, VAR_DECL,
11590 get_identifier ("__for_range"), range_type);
11591 TREE_USED (range_temp) = 1;
11592 DECL_ARTIFICIAL (range_temp) = 1;
11593
11594 return range_temp;
11595 }
11596
11597 /* Used by cp_parser_range_for in template context: we aren't going to
11598 do a full conversion yet, but we still need to resolve auto in the
11599 type of the for-range-declaration if present. This is basically
11600 a shortcut version of cp_convert_range_for. */
11601
11602 static void
11603 do_range_for_auto_deduction (tree decl, tree range_expr)
11604 {
11605 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11606 if (auto_node)
11607 {
11608 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11609 range_temp = convert_from_reference (build_range_temp (range_expr));
11610 iter_type = (cp_parser_perform_range_for_lookup
11611 (range_temp, &begin_dummy, &end_dummy));
11612 if (iter_type)
11613 {
11614 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11615 iter_type);
11616 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11617 tf_warning_or_error);
11618 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11619 iter_decl, auto_node);
11620 }
11621 }
11622 }
11623
11624 /* Converts a range-based for-statement into a normal
11625 for-statement, as per the definition.
11626
11627 for (RANGE_DECL : RANGE_EXPR)
11628 BLOCK
11629
11630 should be equivalent to:
11631
11632 {
11633 auto &&__range = RANGE_EXPR;
11634 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11635 __begin != __end;
11636 ++__begin)
11637 {
11638 RANGE_DECL = *__begin;
11639 BLOCK
11640 }
11641 }
11642
11643 If RANGE_EXPR is an array:
11644 BEGIN_EXPR = __range
11645 END_EXPR = __range + ARRAY_SIZE(__range)
11646 Else if RANGE_EXPR has a member 'begin' or 'end':
11647 BEGIN_EXPR = __range.begin()
11648 END_EXPR = __range.end()
11649 Else:
11650 BEGIN_EXPR = begin(__range)
11651 END_EXPR = end(__range);
11652
11653 If __range has a member 'begin' but not 'end', or vice versa, we must
11654 still use the second alternative (it will surely fail, however).
11655 When calling begin()/end() in the third alternative we must use
11656 argument dependent lookup, but always considering 'std' as an associated
11657 namespace. */
11658
11659 tree
11660 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11661 tree decomp_first_name, unsigned int decomp_cnt,
11662 bool ivdep)
11663 {
11664 tree begin, end;
11665 tree iter_type, begin_expr, end_expr;
11666 tree condition, expression;
11667
11668 if (range_decl == error_mark_node || range_expr == error_mark_node)
11669 /* If an error happened previously do nothing or else a lot of
11670 unhelpful errors would be issued. */
11671 begin_expr = end_expr = iter_type = error_mark_node;
11672 else
11673 {
11674 tree range_temp;
11675
11676 if (VAR_P (range_expr)
11677 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11678 /* Can't bind a reference to an array of runtime bound. */
11679 range_temp = range_expr;
11680 else
11681 {
11682 range_temp = build_range_temp (range_expr);
11683 pushdecl (range_temp);
11684 cp_finish_decl (range_temp, range_expr,
11685 /*is_constant_init*/false, NULL_TREE,
11686 LOOKUP_ONLYCONVERTING);
11687 range_temp = convert_from_reference (range_temp);
11688 }
11689 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11690 &begin_expr, &end_expr);
11691 }
11692
11693 /* The new for initialization statement. */
11694 begin = build_decl (input_location, VAR_DECL,
11695 get_identifier ("__for_begin"), iter_type);
11696 TREE_USED (begin) = 1;
11697 DECL_ARTIFICIAL (begin) = 1;
11698 pushdecl (begin);
11699 cp_finish_decl (begin, begin_expr,
11700 /*is_constant_init*/false, NULL_TREE,
11701 LOOKUP_ONLYCONVERTING);
11702
11703 if (cxx_dialect >= cxx1z)
11704 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11705 end = build_decl (input_location, VAR_DECL,
11706 get_identifier ("__for_end"), iter_type);
11707 TREE_USED (end) = 1;
11708 DECL_ARTIFICIAL (end) = 1;
11709 pushdecl (end);
11710 cp_finish_decl (end, end_expr,
11711 /*is_constant_init*/false, NULL_TREE,
11712 LOOKUP_ONLYCONVERTING);
11713
11714 finish_init_stmt (statement);
11715
11716 /* The new for condition. */
11717 condition = build_x_binary_op (input_location, NE_EXPR,
11718 begin, ERROR_MARK,
11719 end, ERROR_MARK,
11720 NULL, tf_warning_or_error);
11721 finish_for_cond (condition, statement, ivdep);
11722
11723 /* The new increment expression. */
11724 expression = finish_unary_op_expr (input_location,
11725 PREINCREMENT_EXPR, begin,
11726 tf_warning_or_error);
11727 finish_for_expr (expression, statement);
11728
11729 /* The declaration is initialized with *__begin inside the loop body. */
11730 cp_finish_decl (range_decl,
11731 build_x_indirect_ref (input_location, begin, RO_NULL,
11732 tf_warning_or_error),
11733 /*is_constant_init*/false, NULL_TREE,
11734 LOOKUP_ONLYCONVERTING);
11735 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11736 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11737
11738 return statement;
11739 }
11740
11741 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11742 We need to solve both at the same time because the method used
11743 depends on the existence of members begin or end.
11744 Returns the type deduced for the iterator expression. */
11745
11746 static tree
11747 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11748 {
11749 if (error_operand_p (range))
11750 {
11751 *begin = *end = error_mark_node;
11752 return error_mark_node;
11753 }
11754
11755 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11756 {
11757 error ("range-based %<for%> expression of type %qT "
11758 "has incomplete type", TREE_TYPE (range));
11759 *begin = *end = error_mark_node;
11760 return error_mark_node;
11761 }
11762 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11763 {
11764 /* If RANGE is an array, we will use pointer arithmetic. */
11765 *begin = decay_conversion (range, tf_warning_or_error);
11766 *end = build_binary_op (input_location, PLUS_EXPR,
11767 range,
11768 array_type_nelts_top (TREE_TYPE (range)),
11769 0);
11770 return TREE_TYPE (*begin);
11771 }
11772 else
11773 {
11774 /* If it is not an array, we must do a bit of magic. */
11775 tree id_begin, id_end;
11776 tree member_begin, member_end;
11777
11778 *begin = *end = error_mark_node;
11779
11780 id_begin = get_identifier ("begin");
11781 id_end = get_identifier ("end");
11782 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11783 /*protect=*/2, /*want_type=*/false,
11784 tf_warning_or_error);
11785 member_end = lookup_member (TREE_TYPE (range), id_end,
11786 /*protect=*/2, /*want_type=*/false,
11787 tf_warning_or_error);
11788
11789 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11790 {
11791 /* Use the member functions. */
11792 if (member_begin != NULL_TREE)
11793 *begin = cp_parser_range_for_member_function (range, id_begin);
11794 else
11795 error ("range-based %<for%> expression of type %qT has an "
11796 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11797
11798 if (member_end != NULL_TREE)
11799 *end = cp_parser_range_for_member_function (range, id_end);
11800 else
11801 error ("range-based %<for%> expression of type %qT has a "
11802 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11803 }
11804 else
11805 {
11806 /* Use global functions with ADL. */
11807 vec<tree, va_gc> *vec;
11808 vec = make_tree_vector ();
11809
11810 vec_safe_push (vec, range);
11811
11812 member_begin = perform_koenig_lookup (id_begin, vec,
11813 tf_warning_or_error);
11814 *begin = finish_call_expr (member_begin, &vec, false, true,
11815 tf_warning_or_error);
11816 member_end = perform_koenig_lookup (id_end, vec,
11817 tf_warning_or_error);
11818 *end = finish_call_expr (member_end, &vec, false, true,
11819 tf_warning_or_error);
11820
11821 release_tree_vector (vec);
11822 }
11823
11824 /* Last common checks. */
11825 if (*begin == error_mark_node || *end == error_mark_node)
11826 {
11827 /* If one of the expressions is an error do no more checks. */
11828 *begin = *end = error_mark_node;
11829 return error_mark_node;
11830 }
11831 else if (type_dependent_expression_p (*begin)
11832 || type_dependent_expression_p (*end))
11833 /* Can happen, when, eg, in a template context, Koenig lookup
11834 can't resolve begin/end (c++/58503). */
11835 return NULL_TREE;
11836 else
11837 {
11838 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11839 /* The unqualified type of the __begin and __end temporaries should
11840 be the same, as required by the multiple auto declaration. */
11841 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11842 {
11843 if (cxx_dialect >= cxx1z
11844 && (build_x_binary_op (input_location, NE_EXPR,
11845 *begin, ERROR_MARK,
11846 *end, ERROR_MARK,
11847 NULL, tf_none)
11848 != error_mark_node))
11849 /* P0184R0 allows __begin and __end to have different types,
11850 but make sure they are comparable so we can give a better
11851 diagnostic. */;
11852 else
11853 error ("inconsistent begin/end types in range-based %<for%> "
11854 "statement: %qT and %qT",
11855 TREE_TYPE (*begin), TREE_TYPE (*end));
11856 }
11857 return iter_type;
11858 }
11859 }
11860 }
11861
11862 /* Helper function for cp_parser_perform_range_for_lookup.
11863 Builds a tree for RANGE.IDENTIFIER(). */
11864
11865 static tree
11866 cp_parser_range_for_member_function (tree range, tree identifier)
11867 {
11868 tree member, res;
11869 vec<tree, va_gc> *vec;
11870
11871 member = finish_class_member_access_expr (range, identifier,
11872 false, tf_warning_or_error);
11873 if (member == error_mark_node)
11874 return error_mark_node;
11875
11876 vec = make_tree_vector ();
11877 res = finish_call_expr (member, &vec,
11878 /*disallow_virtual=*/false,
11879 /*koenig_p=*/false,
11880 tf_warning_or_error);
11881 release_tree_vector (vec);
11882 return res;
11883 }
11884
11885 /* Parse an iteration-statement.
11886
11887 iteration-statement:
11888 while ( condition ) statement
11889 do statement while ( expression ) ;
11890 for ( init-statement condition [opt] ; expression [opt] )
11891 statement
11892
11893 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11894
11895 static tree
11896 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11897 {
11898 cp_token *token;
11899 enum rid keyword;
11900 tree statement;
11901 unsigned char in_statement;
11902 token_indent_info guard_tinfo;
11903
11904 /* Peek at the next token. */
11905 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11906 if (!token)
11907 return error_mark_node;
11908
11909 guard_tinfo = get_token_indent_info (token);
11910
11911 /* Remember whether or not we are already within an iteration
11912 statement. */
11913 in_statement = parser->in_statement;
11914
11915 /* See what kind of keyword it is. */
11916 keyword = token->keyword;
11917 switch (keyword)
11918 {
11919 case RID_WHILE:
11920 {
11921 tree condition;
11922
11923 /* Begin the while-statement. */
11924 statement = begin_while_stmt ();
11925 /* Look for the `('. */
11926 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11927 /* Parse the condition. */
11928 condition = cp_parser_condition (parser);
11929 finish_while_stmt_cond (condition, statement, ivdep);
11930 /* Look for the `)'. */
11931 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11932 /* Parse the dependent statement. */
11933 parser->in_statement = IN_ITERATION_STMT;
11934 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11935 parser->in_statement = in_statement;
11936 /* We're done with the while-statement. */
11937 finish_while_stmt (statement);
11938 }
11939 break;
11940
11941 case RID_DO:
11942 {
11943 tree expression;
11944
11945 /* Begin the do-statement. */
11946 statement = begin_do_stmt ();
11947 /* Parse the body of the do-statement. */
11948 parser->in_statement = IN_ITERATION_STMT;
11949 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11950 parser->in_statement = in_statement;
11951 finish_do_body (statement);
11952 /* Look for the `while' keyword. */
11953 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11954 /* Look for the `('. */
11955 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11956 /* Parse the expression. */
11957 expression = cp_parser_expression (parser);
11958 /* We're done with the do-statement. */
11959 finish_do_stmt (expression, statement, ivdep);
11960 /* Look for the `)'. */
11961 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11962 /* Look for the `;'. */
11963 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11964 }
11965 break;
11966
11967 case RID_FOR:
11968 {
11969 /* Look for the `('. */
11970 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11971
11972 statement = cp_parser_for (parser, ivdep);
11973
11974 /* Look for the `)'. */
11975 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11976
11977 /* Parse the body of the for-statement. */
11978 parser->in_statement = IN_ITERATION_STMT;
11979 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11980 parser->in_statement = in_statement;
11981
11982 /* We're done with the for-statement. */
11983 finish_for_stmt (statement);
11984 }
11985 break;
11986
11987 default:
11988 cp_parser_error (parser, "expected iteration-statement");
11989 statement = error_mark_node;
11990 break;
11991 }
11992
11993 return statement;
11994 }
11995
11996 /* Parse a init-statement or the declarator of a range-based-for.
11997 Returns true if a range-based-for declaration is seen.
11998
11999 init-statement:
12000 expression-statement
12001 simple-declaration */
12002
12003 static bool
12004 cp_parser_init_statement (cp_parser* parser, tree *decl)
12005 {
12006 /* If the next token is a `;', then we have an empty
12007 expression-statement. Grammatically, this is also a
12008 simple-declaration, but an invalid one, because it does not
12009 declare anything. Therefore, if we did not handle this case
12010 specially, we would issue an error message about an invalid
12011 declaration. */
12012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12013 {
12014 bool is_range_for = false;
12015 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12016
12017 /* A colon is used in range-based for. */
12018 parser->colon_corrects_to_scope_p = false;
12019
12020 /* We're going to speculatively look for a declaration, falling back
12021 to an expression, if necessary. */
12022 cp_parser_parse_tentatively (parser);
12023 /* Parse the declaration. */
12024 cp_parser_simple_declaration (parser,
12025 /*function_definition_allowed_p=*/false,
12026 decl);
12027 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12028 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12029 {
12030 /* It is a range-for, consume the ':' */
12031 cp_lexer_consume_token (parser->lexer);
12032 is_range_for = true;
12033 if (cxx_dialect < cxx11)
12034 {
12035 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12036 "range-based %<for%> loops only available with "
12037 "-std=c++11 or -std=gnu++11");
12038 *decl = error_mark_node;
12039 }
12040 }
12041 else
12042 /* The ';' is not consumed yet because we told
12043 cp_parser_simple_declaration not to. */
12044 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12045
12046 if (cp_parser_parse_definitely (parser))
12047 return is_range_for;
12048 /* If the tentative parse failed, then we shall need to look for an
12049 expression-statement. */
12050 }
12051 /* If we are here, it is an expression-statement. */
12052 cp_parser_expression_statement (parser, NULL_TREE);
12053 return false;
12054 }
12055
12056 /* Parse a jump-statement.
12057
12058 jump-statement:
12059 break ;
12060 continue ;
12061 return expression [opt] ;
12062 return braced-init-list ;
12063 goto identifier ;
12064
12065 GNU extension:
12066
12067 jump-statement:
12068 goto * expression ;
12069
12070 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12071
12072 static tree
12073 cp_parser_jump_statement (cp_parser* parser)
12074 {
12075 tree statement = error_mark_node;
12076 cp_token *token;
12077 enum rid keyword;
12078 unsigned char in_statement;
12079
12080 /* Peek at the next token. */
12081 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12082 if (!token)
12083 return error_mark_node;
12084
12085 /* See what kind of keyword it is. */
12086 keyword = token->keyword;
12087 switch (keyword)
12088 {
12089 case RID_BREAK:
12090 in_statement = parser->in_statement & ~IN_IF_STMT;
12091 switch (in_statement)
12092 {
12093 case 0:
12094 error_at (token->location, "break statement not within loop or switch");
12095 break;
12096 default:
12097 gcc_assert ((in_statement & IN_SWITCH_STMT)
12098 || in_statement == IN_ITERATION_STMT);
12099 statement = finish_break_stmt ();
12100 if (in_statement == IN_ITERATION_STMT)
12101 break_maybe_infinite_loop ();
12102 break;
12103 case IN_OMP_BLOCK:
12104 error_at (token->location, "invalid exit from OpenMP structured block");
12105 break;
12106 case IN_OMP_FOR:
12107 error_at (token->location, "break statement used with OpenMP for loop");
12108 break;
12109 case IN_CILK_SIMD_FOR:
12110 error_at (token->location, "break statement used with Cilk Plus for loop");
12111 break;
12112 }
12113 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12114 break;
12115
12116 case RID_CONTINUE:
12117 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12118 {
12119 case 0:
12120 error_at (token->location, "continue statement not within a loop");
12121 break;
12122 case IN_CILK_SIMD_FOR:
12123 error_at (token->location,
12124 "continue statement within %<#pragma simd%> loop body");
12125 /* Fall through. */
12126 case IN_ITERATION_STMT:
12127 case IN_OMP_FOR:
12128 statement = finish_continue_stmt ();
12129 break;
12130 case IN_OMP_BLOCK:
12131 error_at (token->location, "invalid exit from OpenMP structured block");
12132 break;
12133 default:
12134 gcc_unreachable ();
12135 }
12136 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12137 break;
12138
12139 case RID_RETURN:
12140 {
12141 tree expr;
12142 bool expr_non_constant_p;
12143
12144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12145 {
12146 cp_lexer_set_source_position (parser->lexer);
12147 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12148 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12149 }
12150 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12151 expr = cp_parser_expression (parser);
12152 else
12153 /* If the next token is a `;', then there is no
12154 expression. */
12155 expr = NULL_TREE;
12156 /* Build the return-statement. */
12157 if (current_function_auto_return_pattern && in_discarded_stmt)
12158 /* Don't deduce from a discarded return statement. */;
12159 else
12160 statement = finish_return_stmt (expr);
12161 /* Look for the final `;'. */
12162 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12163 }
12164 break;
12165
12166 case RID_GOTO:
12167 if (parser->in_function_body
12168 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12169 {
12170 error ("%<goto%> in %<constexpr%> function");
12171 cp_function_chain->invalid_constexpr = true;
12172 }
12173
12174 /* Create the goto-statement. */
12175 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12176 {
12177 /* Issue a warning about this use of a GNU extension. */
12178 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12179 /* Consume the '*' token. */
12180 cp_lexer_consume_token (parser->lexer);
12181 /* Parse the dependent expression. */
12182 finish_goto_stmt (cp_parser_expression (parser));
12183 }
12184 else
12185 finish_goto_stmt (cp_parser_identifier (parser));
12186 /* Look for the final `;'. */
12187 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12188 break;
12189
12190 default:
12191 cp_parser_error (parser, "expected jump-statement");
12192 break;
12193 }
12194
12195 return statement;
12196 }
12197
12198 /* Parse a declaration-statement.
12199
12200 declaration-statement:
12201 block-declaration */
12202
12203 static void
12204 cp_parser_declaration_statement (cp_parser* parser)
12205 {
12206 void *p;
12207
12208 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12209 p = obstack_alloc (&declarator_obstack, 0);
12210
12211 /* Parse the block-declaration. */
12212 cp_parser_block_declaration (parser, /*statement_p=*/true);
12213
12214 /* Free any declarators allocated. */
12215 obstack_free (&declarator_obstack, p);
12216 }
12217
12218 /* Some dependent statements (like `if (cond) statement'), are
12219 implicitly in their own scope. In other words, if the statement is
12220 a single statement (as opposed to a compound-statement), it is
12221 none-the-less treated as if it were enclosed in braces. Any
12222 declarations appearing in the dependent statement are out of scope
12223 after control passes that point. This function parses a statement,
12224 but ensures that is in its own scope, even if it is not a
12225 compound-statement.
12226
12227 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12228 is a (possibly labeled) if statement which is not enclosed in
12229 braces and has an else clause. This is used to implement
12230 -Wparentheses.
12231
12232 CHAIN is a vector of if-else-if conditions. This is used to implement
12233 -Wduplicated-cond.
12234
12235 Returns the new statement. */
12236
12237 static tree
12238 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12239 const token_indent_info &guard_tinfo,
12240 vec<tree> *chain)
12241 {
12242 tree statement;
12243 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12244 token_indent_info body_tinfo
12245 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12246
12247 if (if_p != NULL)
12248 *if_p = false;
12249
12250 /* Mark if () ; with a special NOP_EXPR. */
12251 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12252 {
12253 cp_lexer_consume_token (parser->lexer);
12254 statement = add_stmt (build_empty_stmt (body_loc));
12255
12256 if (guard_tinfo.keyword == RID_IF
12257 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12258 warning_at (body_loc, OPT_Wempty_body,
12259 "suggest braces around empty body in an %<if%> statement");
12260 else if (guard_tinfo.keyword == RID_ELSE)
12261 warning_at (body_loc, OPT_Wempty_body,
12262 "suggest braces around empty body in an %<else%> statement");
12263 }
12264 /* if a compound is opened, we simply parse the statement directly. */
12265 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12266 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12267 /* If the token is not a `{', then we must take special action. */
12268 else
12269 {
12270 /* Create a compound-statement. */
12271 statement = begin_compound_stmt (0);
12272 /* Parse the dependent-statement. */
12273 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12274 /* Finish the dummy compound-statement. */
12275 finish_compound_stmt (statement);
12276 }
12277
12278 token_indent_info next_tinfo
12279 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12280 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12281
12282 /* Return the statement. */
12283 return statement;
12284 }
12285
12286 /* For some dependent statements (like `while (cond) statement'), we
12287 have already created a scope. Therefore, even if the dependent
12288 statement is a compound-statement, we do not want to create another
12289 scope. */
12290
12291 static void
12292 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12293 const token_indent_info &guard_tinfo)
12294 {
12295 /* If the token is a `{', then we must take special action. */
12296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12297 {
12298 token_indent_info body_tinfo
12299 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12300
12301 cp_parser_statement (parser, NULL_TREE, false, if_p);
12302 token_indent_info next_tinfo
12303 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12304 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12305 }
12306 else
12307 {
12308 /* Avoid calling cp_parser_compound_statement, so that we
12309 don't create a new scope. Do everything else by hand. */
12310 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12311 /* If the next keyword is `__label__' we have a label declaration. */
12312 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12313 cp_parser_label_declaration (parser);
12314 /* Parse an (optional) statement-seq. */
12315 cp_parser_statement_seq_opt (parser, NULL_TREE);
12316 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12317 }
12318 }
12319
12320 /* Declarations [gram.dcl.dcl] */
12321
12322 /* Parse an optional declaration-sequence.
12323
12324 declaration-seq:
12325 declaration
12326 declaration-seq declaration */
12327
12328 static void
12329 cp_parser_declaration_seq_opt (cp_parser* parser)
12330 {
12331 while (true)
12332 {
12333 cp_token *token;
12334
12335 token = cp_lexer_peek_token (parser->lexer);
12336
12337 if (token->type == CPP_CLOSE_BRACE
12338 || token->type == CPP_EOF
12339 || token->type == CPP_PRAGMA_EOL)
12340 break;
12341
12342 if (token->type == CPP_SEMICOLON)
12343 {
12344 /* A declaration consisting of a single semicolon is
12345 invalid. Allow it unless we're being pedantic. */
12346 cp_lexer_consume_token (parser->lexer);
12347 if (!in_system_header_at (input_location))
12348 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12349 continue;
12350 }
12351
12352 /* If we're entering or exiting a region that's implicitly
12353 extern "C", modify the lang context appropriately. */
12354 if (!parser->implicit_extern_c && token->implicit_extern_c)
12355 {
12356 push_lang_context (lang_name_c);
12357 parser->implicit_extern_c = true;
12358 }
12359 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12360 {
12361 pop_lang_context ();
12362 parser->implicit_extern_c = false;
12363 }
12364
12365 if (token->type == CPP_PRAGMA)
12366 {
12367 /* A top-level declaration can consist solely of a #pragma.
12368 A nested declaration cannot, so this is done here and not
12369 in cp_parser_declaration. (A #pragma at block scope is
12370 handled in cp_parser_statement.) */
12371 cp_parser_pragma (parser, pragma_external, NULL);
12372 continue;
12373 }
12374
12375 /* Parse the declaration itself. */
12376 cp_parser_declaration (parser);
12377 }
12378 }
12379
12380 /* Parse a declaration.
12381
12382 declaration:
12383 block-declaration
12384 function-definition
12385 template-declaration
12386 explicit-instantiation
12387 explicit-specialization
12388 linkage-specification
12389 namespace-definition
12390
12391 C++17:
12392 deduction-guide
12393
12394 GNU extension:
12395
12396 declaration:
12397 __extension__ declaration */
12398
12399 static void
12400 cp_parser_declaration (cp_parser* parser)
12401 {
12402 cp_token token1;
12403 cp_token token2;
12404 int saved_pedantic;
12405 void *p;
12406 tree attributes = NULL_TREE;
12407
12408 /* Check for the `__extension__' keyword. */
12409 if (cp_parser_extension_opt (parser, &saved_pedantic))
12410 {
12411 /* Parse the qualified declaration. */
12412 cp_parser_declaration (parser);
12413 /* Restore the PEDANTIC flag. */
12414 pedantic = saved_pedantic;
12415
12416 return;
12417 }
12418
12419 /* Try to figure out what kind of declaration is present. */
12420 token1 = *cp_lexer_peek_token (parser->lexer);
12421
12422 if (token1.type != CPP_EOF)
12423 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12424 else
12425 {
12426 token2.type = CPP_EOF;
12427 token2.keyword = RID_MAX;
12428 }
12429
12430 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12431 p = obstack_alloc (&declarator_obstack, 0);
12432
12433 /* If the next token is `extern' and the following token is a string
12434 literal, then we have a linkage specification. */
12435 if (token1.keyword == RID_EXTERN
12436 && cp_parser_is_pure_string_literal (&token2))
12437 cp_parser_linkage_specification (parser);
12438 /* If the next token is `template', then we have either a template
12439 declaration, an explicit instantiation, or an explicit
12440 specialization. */
12441 else if (token1.keyword == RID_TEMPLATE)
12442 {
12443 /* `template <>' indicates a template specialization. */
12444 if (token2.type == CPP_LESS
12445 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12446 cp_parser_explicit_specialization (parser);
12447 /* `template <' indicates a template declaration. */
12448 else if (token2.type == CPP_LESS)
12449 cp_parser_template_declaration (parser, /*member_p=*/false);
12450 /* Anything else must be an explicit instantiation. */
12451 else
12452 cp_parser_explicit_instantiation (parser);
12453 }
12454 /* If the next token is `export', then we have a template
12455 declaration. */
12456 else if (token1.keyword == RID_EXPORT)
12457 cp_parser_template_declaration (parser, /*member_p=*/false);
12458 /* If the next token is `extern', 'static' or 'inline' and the one
12459 after that is `template', we have a GNU extended explicit
12460 instantiation directive. */
12461 else if (cp_parser_allow_gnu_extensions_p (parser)
12462 && (token1.keyword == RID_EXTERN
12463 || token1.keyword == RID_STATIC
12464 || token1.keyword == RID_INLINE)
12465 && token2.keyword == RID_TEMPLATE)
12466 cp_parser_explicit_instantiation (parser);
12467 /* If the next token is `namespace', check for a named or unnamed
12468 namespace definition. */
12469 else if (token1.keyword == RID_NAMESPACE
12470 && (/* A named namespace definition. */
12471 (token2.type == CPP_NAME
12472 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12473 != CPP_EQ))
12474 || (token2.type == CPP_OPEN_SQUARE
12475 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12476 == CPP_OPEN_SQUARE)
12477 /* An unnamed namespace definition. */
12478 || token2.type == CPP_OPEN_BRACE
12479 || token2.keyword == RID_ATTRIBUTE))
12480 cp_parser_namespace_definition (parser);
12481 /* An inline (associated) namespace definition. */
12482 else if (token1.keyword == RID_INLINE
12483 && token2.keyword == RID_NAMESPACE)
12484 cp_parser_namespace_definition (parser);
12485 /* Objective-C++ declaration/definition. */
12486 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12487 cp_parser_objc_declaration (parser, NULL_TREE);
12488 else if (c_dialect_objc ()
12489 && token1.keyword == RID_ATTRIBUTE
12490 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12491 cp_parser_objc_declaration (parser, attributes);
12492 /* At this point we may have a template declared by a concept
12493 introduction. */
12494 else if (flag_concepts
12495 && cp_parser_template_declaration_after_export (parser,
12496 /*member_p=*/false))
12497 /* We did. */;
12498 else
12499 /* Try to parse a block-declaration, or a function-definition. */
12500 cp_parser_block_declaration (parser, /*statement_p=*/false);
12501
12502 /* Free any declarators allocated. */
12503 obstack_free (&declarator_obstack, p);
12504 }
12505
12506 /* Parse a block-declaration.
12507
12508 block-declaration:
12509 simple-declaration
12510 asm-definition
12511 namespace-alias-definition
12512 using-declaration
12513 using-directive
12514
12515 GNU Extension:
12516
12517 block-declaration:
12518 __extension__ block-declaration
12519
12520 C++0x Extension:
12521
12522 block-declaration:
12523 static_assert-declaration
12524
12525 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12526 part of a declaration-statement. */
12527
12528 static void
12529 cp_parser_block_declaration (cp_parser *parser,
12530 bool statement_p)
12531 {
12532 cp_token *token1;
12533 int saved_pedantic;
12534
12535 /* Check for the `__extension__' keyword. */
12536 if (cp_parser_extension_opt (parser, &saved_pedantic))
12537 {
12538 /* Parse the qualified declaration. */
12539 cp_parser_block_declaration (parser, statement_p);
12540 /* Restore the PEDANTIC flag. */
12541 pedantic = saved_pedantic;
12542
12543 return;
12544 }
12545
12546 /* Peek at the next token to figure out which kind of declaration is
12547 present. */
12548 token1 = cp_lexer_peek_token (parser->lexer);
12549
12550 /* If the next keyword is `asm', we have an asm-definition. */
12551 if (token1->keyword == RID_ASM)
12552 {
12553 if (statement_p)
12554 cp_parser_commit_to_tentative_parse (parser);
12555 cp_parser_asm_definition (parser);
12556 }
12557 /* If the next keyword is `namespace', we have a
12558 namespace-alias-definition. */
12559 else if (token1->keyword == RID_NAMESPACE)
12560 cp_parser_namespace_alias_definition (parser);
12561 /* If the next keyword is `using', we have a
12562 using-declaration, a using-directive, or an alias-declaration. */
12563 else if (token1->keyword == RID_USING)
12564 {
12565 cp_token *token2;
12566
12567 if (statement_p)
12568 cp_parser_commit_to_tentative_parse (parser);
12569 /* If the token after `using' is `namespace', then we have a
12570 using-directive. */
12571 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12572 if (token2->keyword == RID_NAMESPACE)
12573 cp_parser_using_directive (parser);
12574 /* If the second token after 'using' is '=', then we have an
12575 alias-declaration. */
12576 else if (cxx_dialect >= cxx11
12577 && token2->type == CPP_NAME
12578 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12579 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12580 cp_parser_alias_declaration (parser);
12581 /* Otherwise, it's a using-declaration. */
12582 else
12583 cp_parser_using_declaration (parser,
12584 /*access_declaration_p=*/false);
12585 }
12586 /* If the next keyword is `__label__' we have a misplaced label
12587 declaration. */
12588 else if (token1->keyword == RID_LABEL)
12589 {
12590 cp_lexer_consume_token (parser->lexer);
12591 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12592 cp_parser_skip_to_end_of_statement (parser);
12593 /* If the next token is now a `;', consume it. */
12594 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12595 cp_lexer_consume_token (parser->lexer);
12596 }
12597 /* If the next token is `static_assert' we have a static assertion. */
12598 else if (token1->keyword == RID_STATIC_ASSERT)
12599 cp_parser_static_assert (parser, /*member_p=*/false);
12600 /* Anything else must be a simple-declaration. */
12601 else
12602 cp_parser_simple_declaration (parser, !statement_p,
12603 /*maybe_range_for_decl*/NULL);
12604 }
12605
12606 /* Parse a simple-declaration.
12607
12608 simple-declaration:
12609 decl-specifier-seq [opt] init-declarator-list [opt] ;
12610 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12611 brace-or-equal-initializer ;
12612
12613 init-declarator-list:
12614 init-declarator
12615 init-declarator-list , init-declarator
12616
12617 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12618 function-definition as a simple-declaration.
12619
12620 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12621 parsed declaration if it is an uninitialized single declarator not followed
12622 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12623 if present, will not be consumed. */
12624
12625 static void
12626 cp_parser_simple_declaration (cp_parser* parser,
12627 bool function_definition_allowed_p,
12628 tree *maybe_range_for_decl)
12629 {
12630 cp_decl_specifier_seq decl_specifiers;
12631 int declares_class_or_enum;
12632 bool saw_declarator;
12633 location_t comma_loc = UNKNOWN_LOCATION;
12634 location_t init_loc = UNKNOWN_LOCATION;
12635
12636 if (maybe_range_for_decl)
12637 *maybe_range_for_decl = NULL_TREE;
12638
12639 /* Defer access checks until we know what is being declared; the
12640 checks for names appearing in the decl-specifier-seq should be
12641 done as if we were in the scope of the thing being declared. */
12642 push_deferring_access_checks (dk_deferred);
12643
12644 /* Parse the decl-specifier-seq. We have to keep track of whether
12645 or not the decl-specifier-seq declares a named class or
12646 enumeration type, since that is the only case in which the
12647 init-declarator-list is allowed to be empty.
12648
12649 [dcl.dcl]
12650
12651 In a simple-declaration, the optional init-declarator-list can be
12652 omitted only when declaring a class or enumeration, that is when
12653 the decl-specifier-seq contains either a class-specifier, an
12654 elaborated-type-specifier, or an enum-specifier. */
12655 cp_parser_decl_specifier_seq (parser,
12656 CP_PARSER_FLAGS_OPTIONAL,
12657 &decl_specifiers,
12658 &declares_class_or_enum);
12659 /* We no longer need to defer access checks. */
12660 stop_deferring_access_checks ();
12661
12662 /* In a block scope, a valid declaration must always have a
12663 decl-specifier-seq. By not trying to parse declarators, we can
12664 resolve the declaration/expression ambiguity more quickly. */
12665 if (!function_definition_allowed_p
12666 && !decl_specifiers.any_specifiers_p)
12667 {
12668 cp_parser_error (parser, "expected declaration");
12669 goto done;
12670 }
12671
12672 /* If the next two tokens are both identifiers, the code is
12673 erroneous. The usual cause of this situation is code like:
12674
12675 T t;
12676
12677 where "T" should name a type -- but does not. */
12678 if (!decl_specifiers.any_type_specifiers_p
12679 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12680 {
12681 /* If parsing tentatively, we should commit; we really are
12682 looking at a declaration. */
12683 cp_parser_commit_to_tentative_parse (parser);
12684 /* Give up. */
12685 goto done;
12686 }
12687
12688 /* If we have seen at least one decl-specifier, and the next token
12689 is not a parenthesis, then we must be looking at a declaration.
12690 (After "int (" we might be looking at a functional cast.) */
12691 if (decl_specifiers.any_specifiers_p
12692 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12693 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12694 && !cp_parser_error_occurred (parser))
12695 cp_parser_commit_to_tentative_parse (parser);
12696
12697 /* Look for C++17 decomposition declaration. */
12698 for (size_t n = 1; ; n++)
12699 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12700 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12701 continue;
12702 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12703 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12704 && decl_specifiers.any_specifiers_p)
12705 {
12706 tree decl
12707 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12708 maybe_range_for_decl,
12709 &init_loc);
12710
12711 /* The next token should be either a `,' or a `;'. */
12712 cp_token *token = cp_lexer_peek_token (parser->lexer);
12713 /* If it's a `;', we are done. */
12714 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12715 goto finish;
12716 /* Anything else is an error. */
12717 else
12718 {
12719 /* If we have already issued an error message we don't need
12720 to issue another one. */
12721 if ((decl != error_mark_node
12722 && DECL_INITIAL (decl) != error_mark_node)
12723 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12724 cp_parser_error (parser, "expected %<,%> or %<;%>");
12725 /* Skip tokens until we reach the end of the statement. */
12726 cp_parser_skip_to_end_of_statement (parser);
12727 /* If the next token is now a `;', consume it. */
12728 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12729 cp_lexer_consume_token (parser->lexer);
12730 goto done;
12731 }
12732 }
12733 else
12734 break;
12735
12736 tree last_type;
12737 bool auto_specifier_p;
12738 /* NULL_TREE if both variable and function declaration are allowed,
12739 error_mark_node if function declaration are not allowed and
12740 a FUNCTION_DECL that should be diagnosed if it is followed by
12741 variable declarations. */
12742 tree auto_function_declaration;
12743
12744 last_type = NULL_TREE;
12745 auto_specifier_p
12746 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12747 auto_function_declaration = NULL_TREE;
12748
12749 /* Keep going until we hit the `;' at the end of the simple
12750 declaration. */
12751 saw_declarator = false;
12752 while (cp_lexer_next_token_is_not (parser->lexer,
12753 CPP_SEMICOLON))
12754 {
12755 cp_token *token;
12756 bool function_definition_p;
12757 tree decl;
12758 tree auto_result = NULL_TREE;
12759
12760 if (saw_declarator)
12761 {
12762 /* If we are processing next declarator, comma is expected */
12763 token = cp_lexer_peek_token (parser->lexer);
12764 gcc_assert (token->type == CPP_COMMA);
12765 cp_lexer_consume_token (parser->lexer);
12766 if (maybe_range_for_decl)
12767 {
12768 *maybe_range_for_decl = error_mark_node;
12769 if (comma_loc == UNKNOWN_LOCATION)
12770 comma_loc = token->location;
12771 }
12772 }
12773 else
12774 saw_declarator = true;
12775
12776 /* Parse the init-declarator. */
12777 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12778 /*checks=*/NULL,
12779 function_definition_allowed_p,
12780 /*member_p=*/false,
12781 declares_class_or_enum,
12782 &function_definition_p,
12783 maybe_range_for_decl,
12784 &init_loc,
12785 &auto_result);
12786 /* If an error occurred while parsing tentatively, exit quickly.
12787 (That usually happens when in the body of a function; each
12788 statement is treated as a declaration-statement until proven
12789 otherwise.) */
12790 if (cp_parser_error_occurred (parser))
12791 goto done;
12792
12793 if (auto_specifier_p && cxx_dialect >= cxx14)
12794 {
12795 /* If the init-declarator-list contains more than one
12796 init-declarator, they shall all form declarations of
12797 variables. */
12798 if (auto_function_declaration == NULL_TREE)
12799 auto_function_declaration
12800 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12801 else if (TREE_CODE (decl) == FUNCTION_DECL
12802 || auto_function_declaration != error_mark_node)
12803 {
12804 error_at (decl_specifiers.locations[ds_type_spec],
12805 "non-variable %qD in declaration with more than one "
12806 "declarator with placeholder type",
12807 TREE_CODE (decl) == FUNCTION_DECL
12808 ? decl : auto_function_declaration);
12809 auto_function_declaration = error_mark_node;
12810 }
12811 }
12812
12813 if (auto_result
12814 && (!processing_template_decl || !type_uses_auto (auto_result)))
12815 {
12816 if (last_type
12817 && last_type != error_mark_node
12818 && !same_type_p (auto_result, last_type))
12819 {
12820 /* If the list of declarators contains more than one declarator,
12821 the type of each declared variable is determined as described
12822 above. If the type deduced for the template parameter U is not
12823 the same in each deduction, the program is ill-formed. */
12824 error_at (decl_specifiers.locations[ds_type_spec],
12825 "inconsistent deduction for %qT: %qT and then %qT",
12826 decl_specifiers.type, last_type, auto_result);
12827 last_type = error_mark_node;
12828 }
12829 else
12830 last_type = auto_result;
12831 }
12832
12833 /* Handle function definitions specially. */
12834 if (function_definition_p)
12835 {
12836 /* If the next token is a `,', then we are probably
12837 processing something like:
12838
12839 void f() {}, *p;
12840
12841 which is erroneous. */
12842 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12843 {
12844 cp_token *token = cp_lexer_peek_token (parser->lexer);
12845 error_at (token->location,
12846 "mixing"
12847 " declarations and function-definitions is forbidden");
12848 }
12849 /* Otherwise, we're done with the list of declarators. */
12850 else
12851 {
12852 pop_deferring_access_checks ();
12853 return;
12854 }
12855 }
12856 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12857 *maybe_range_for_decl = decl;
12858 /* The next token should be either a `,' or a `;'. */
12859 token = cp_lexer_peek_token (parser->lexer);
12860 /* If it's a `,', there are more declarators to come. */
12861 if (token->type == CPP_COMMA)
12862 /* will be consumed next time around */;
12863 /* If it's a `;', we are done. */
12864 else if (token->type == CPP_SEMICOLON)
12865 break;
12866 else if (maybe_range_for_decl)
12867 {
12868 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
12869 permerror (decl_specifiers.locations[ds_type_spec],
12870 "types may not be defined in a for-range-declaration");
12871 break;
12872 }
12873 /* Anything else is an error. */
12874 else
12875 {
12876 /* If we have already issued an error message we don't need
12877 to issue another one. */
12878 if ((decl != error_mark_node
12879 && DECL_INITIAL (decl) != error_mark_node)
12880 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12881 cp_parser_error (parser, "expected %<,%> or %<;%>");
12882 /* Skip tokens until we reach the end of the statement. */
12883 cp_parser_skip_to_end_of_statement (parser);
12884 /* If the next token is now a `;', consume it. */
12885 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12886 cp_lexer_consume_token (parser->lexer);
12887 goto done;
12888 }
12889 /* After the first time around, a function-definition is not
12890 allowed -- even if it was OK at first. For example:
12891
12892 int i, f() {}
12893
12894 is not valid. */
12895 function_definition_allowed_p = false;
12896 }
12897
12898 /* Issue an error message if no declarators are present, and the
12899 decl-specifier-seq does not itself declare a class or
12900 enumeration: [dcl.dcl]/3. */
12901 if (!saw_declarator)
12902 {
12903 if (cp_parser_declares_only_class_p (parser))
12904 {
12905 if (!declares_class_or_enum
12906 && decl_specifiers.type
12907 && OVERLOAD_TYPE_P (decl_specifiers.type))
12908 /* Ensure an error is issued anyway when finish_decltype_type,
12909 called via cp_parser_decl_specifier_seq, returns a class or
12910 an enumeration (c++/51786). */
12911 decl_specifiers.type = NULL_TREE;
12912 shadow_tag (&decl_specifiers);
12913 }
12914 /* Perform any deferred access checks. */
12915 perform_deferred_access_checks (tf_warning_or_error);
12916 }
12917
12918 /* Consume the `;'. */
12919 finish:
12920 if (!maybe_range_for_decl)
12921 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12922 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12923 {
12924 if (init_loc != UNKNOWN_LOCATION)
12925 error_at (init_loc, "initializer in range-based %<for%> loop");
12926 if (comma_loc != UNKNOWN_LOCATION)
12927 error_at (comma_loc,
12928 "multiple declarations in range-based %<for%> loop");
12929 }
12930
12931 done:
12932 pop_deferring_access_checks ();
12933 }
12934
12935 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12936 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12937 initializer ; */
12938
12939 static tree
12940 cp_parser_decomposition_declaration (cp_parser *parser,
12941 cp_decl_specifier_seq *decl_specifiers,
12942 tree *maybe_range_for_decl,
12943 location_t *init_loc)
12944 {
12945 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12946 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12947 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12948
12949 /* Parse the identifier-list. */
12950 auto_vec<cp_expr, 10> v;
12951 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12952 while (true)
12953 {
12954 cp_expr e = cp_parser_identifier (parser);
12955 if (e.get_value () == error_mark_node)
12956 break;
12957 v.safe_push (e);
12958 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12959 break;
12960 cp_lexer_consume_token (parser->lexer);
12961 }
12962
12963 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12964 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12965 {
12966 end_loc = UNKNOWN_LOCATION;
12967 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12968 false);
12969 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12970 cp_lexer_consume_token (parser->lexer);
12971 else
12972 {
12973 cp_parser_skip_to_end_of_statement (parser);
12974 return error_mark_node;
12975 }
12976 }
12977
12978 if (cxx_dialect < cxx1z)
12979 pedwarn (loc, 0, "decomposition declaration only available with "
12980 "-std=c++1z or -std=gnu++1z");
12981
12982 tree pushed_scope;
12983 cp_declarator *declarator = make_declarator (cdk_decomp);
12984 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
12985 declarator->id_loc = loc;
12986 if (ref_qual != REF_QUAL_NONE)
12987 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
12988 ref_qual == REF_QUAL_RVALUE,
12989 NULL_TREE);
12990 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
12991 NULL_TREE, decl_specifiers->attributes,
12992 &pushed_scope);
12993 tree orig_decl = decl;
12994
12995 unsigned int i;
12996 cp_expr e;
12997 cp_decl_specifier_seq decl_specs;
12998 clear_decl_specs (&decl_specs);
12999 decl_specs.type = make_auto ();
13000 tree prev = decl;
13001 FOR_EACH_VEC_ELT (v, i, e)
13002 {
13003 if (i == 0)
13004 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13005 else
13006 declarator->u.id.unqualified_name = e.get_value ();
13007 declarator->id_loc = e.get_location ();
13008 tree elt_pushed_scope;
13009 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13010 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13011 if (decl2 == error_mark_node)
13012 decl = error_mark_node;
13013 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13014 {
13015 /* Ensure we've diagnosed redeclaration if we aren't creating
13016 a new VAR_DECL. */
13017 gcc_assert (errorcount);
13018 decl = error_mark_node;
13019 }
13020 else
13021 prev = decl2;
13022 if (elt_pushed_scope)
13023 pop_scope (elt_pushed_scope);
13024 }
13025
13026 if (v.is_empty ())
13027 {
13028 error_at (loc, "empty decomposition declaration");
13029 decl = error_mark_node;
13030 }
13031
13032 if (maybe_range_for_decl == NULL
13033 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13034 {
13035 bool non_constant_p = false, is_direct_init = false;
13036 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13037 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13038 &non_constant_p);
13039
13040 if (decl != error_mark_node)
13041 {
13042 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13043 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13044 cp_finish_decomp (decl, prev, v.length ());
13045 }
13046 }
13047 else if (decl != error_mark_node)
13048 {
13049 *maybe_range_for_decl = prev;
13050 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13051 the underlying DECL. */
13052 cp_finish_decomp (decl, prev, v.length ());
13053 }
13054
13055 if (pushed_scope)
13056 pop_scope (pushed_scope);
13057
13058 if (decl == error_mark_node && DECL_P (orig_decl))
13059 {
13060 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13061 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13062 }
13063
13064 return decl;
13065 }
13066
13067 /* Parse a decl-specifier-seq.
13068
13069 decl-specifier-seq:
13070 decl-specifier-seq [opt] decl-specifier
13071 decl-specifier attribute-specifier-seq [opt] (C++11)
13072
13073 decl-specifier:
13074 storage-class-specifier
13075 type-specifier
13076 function-specifier
13077 friend
13078 typedef
13079
13080 GNU Extension:
13081
13082 decl-specifier:
13083 attributes
13084
13085 Concepts Extension:
13086
13087 decl-specifier:
13088 concept
13089
13090 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13091
13092 The parser flags FLAGS is used to control type-specifier parsing.
13093
13094 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13095 flags:
13096
13097 1: one of the decl-specifiers is an elaborated-type-specifier
13098 (i.e., a type declaration)
13099 2: one of the decl-specifiers is an enum-specifier or a
13100 class-specifier (i.e., a type definition)
13101
13102 */
13103
13104 static void
13105 cp_parser_decl_specifier_seq (cp_parser* parser,
13106 cp_parser_flags flags,
13107 cp_decl_specifier_seq *decl_specs,
13108 int* declares_class_or_enum)
13109 {
13110 bool constructor_possible_p = !parser->in_declarator_p;
13111 bool found_decl_spec = false;
13112 cp_token *start_token = NULL;
13113 cp_decl_spec ds;
13114
13115 /* Clear DECL_SPECS. */
13116 clear_decl_specs (decl_specs);
13117
13118 /* Assume no class or enumeration type is declared. */
13119 *declares_class_or_enum = 0;
13120
13121 /* Keep reading specifiers until there are no more to read. */
13122 while (true)
13123 {
13124 bool constructor_p;
13125 cp_token *token;
13126 ds = ds_last;
13127
13128 /* Peek at the next token. */
13129 token = cp_lexer_peek_token (parser->lexer);
13130
13131 /* Save the first token of the decl spec list for error
13132 reporting. */
13133 if (!start_token)
13134 start_token = token;
13135 /* Handle attributes. */
13136 if (cp_next_tokens_can_be_attribute_p (parser))
13137 {
13138 /* Parse the attributes. */
13139 tree attrs = cp_parser_attributes_opt (parser);
13140
13141 /* In a sequence of declaration specifiers, c++11 attributes
13142 appertain to the type that precede them. In that case
13143 [dcl.spec]/1 says:
13144
13145 The attribute-specifier-seq affects the type only for
13146 the declaration it appears in, not other declarations
13147 involving the same type.
13148
13149 But for now let's force the user to position the
13150 attribute either at the beginning of the declaration or
13151 after the declarator-id, which would clearly mean that it
13152 applies to the declarator. */
13153 if (cxx11_attribute_p (attrs))
13154 {
13155 if (!found_decl_spec)
13156 /* The c++11 attribute is at the beginning of the
13157 declaration. It appertains to the entity being
13158 declared. */;
13159 else
13160 {
13161 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13162 {
13163 /* This is an attribute following a
13164 class-specifier. */
13165 if (decl_specs->type_definition_p)
13166 warn_misplaced_attr_for_class_type (token->location,
13167 decl_specs->type);
13168 attrs = NULL_TREE;
13169 }
13170 else
13171 {
13172 decl_specs->std_attributes
13173 = chainon (decl_specs->std_attributes,
13174 attrs);
13175 if (decl_specs->locations[ds_std_attribute] == 0)
13176 decl_specs->locations[ds_std_attribute] = token->location;
13177 }
13178 continue;
13179 }
13180 }
13181
13182 decl_specs->attributes
13183 = chainon (decl_specs->attributes,
13184 attrs);
13185 if (decl_specs->locations[ds_attribute] == 0)
13186 decl_specs->locations[ds_attribute] = token->location;
13187 continue;
13188 }
13189 /* Assume we will find a decl-specifier keyword. */
13190 found_decl_spec = true;
13191 /* If the next token is an appropriate keyword, we can simply
13192 add it to the list. */
13193 switch (token->keyword)
13194 {
13195 /* decl-specifier:
13196 friend
13197 constexpr */
13198 case RID_FRIEND:
13199 if (!at_class_scope_p ())
13200 {
13201 error_at (token->location, "%<friend%> used outside of class");
13202 cp_lexer_purge_token (parser->lexer);
13203 }
13204 else
13205 {
13206 ds = ds_friend;
13207 /* Consume the token. */
13208 cp_lexer_consume_token (parser->lexer);
13209 }
13210 break;
13211
13212 case RID_CONSTEXPR:
13213 ds = ds_constexpr;
13214 cp_lexer_consume_token (parser->lexer);
13215 break;
13216
13217 case RID_CONCEPT:
13218 ds = ds_concept;
13219 cp_lexer_consume_token (parser->lexer);
13220 break;
13221
13222 /* function-specifier:
13223 inline
13224 virtual
13225 explicit */
13226 case RID_INLINE:
13227 case RID_VIRTUAL:
13228 case RID_EXPLICIT:
13229 cp_parser_function_specifier_opt (parser, decl_specs);
13230 break;
13231
13232 /* decl-specifier:
13233 typedef */
13234 case RID_TYPEDEF:
13235 ds = ds_typedef;
13236 /* Consume the token. */
13237 cp_lexer_consume_token (parser->lexer);
13238 /* A constructor declarator cannot appear in a typedef. */
13239 constructor_possible_p = false;
13240 /* The "typedef" keyword can only occur in a declaration; we
13241 may as well commit at this point. */
13242 cp_parser_commit_to_tentative_parse (parser);
13243
13244 if (decl_specs->storage_class != sc_none)
13245 decl_specs->conflicting_specifiers_p = true;
13246 break;
13247
13248 /* storage-class-specifier:
13249 auto
13250 register
13251 static
13252 extern
13253 mutable
13254
13255 GNU Extension:
13256 thread */
13257 case RID_AUTO:
13258 if (cxx_dialect == cxx98)
13259 {
13260 /* Consume the token. */
13261 cp_lexer_consume_token (parser->lexer);
13262
13263 /* Complain about `auto' as a storage specifier, if
13264 we're complaining about C++0x compatibility. */
13265 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13266 " changes meaning in C++11; please remove it");
13267
13268 /* Set the storage class anyway. */
13269 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13270 token);
13271 }
13272 else
13273 /* C++0x auto type-specifier. */
13274 found_decl_spec = false;
13275 break;
13276
13277 case RID_REGISTER:
13278 case RID_STATIC:
13279 case RID_EXTERN:
13280 case RID_MUTABLE:
13281 /* Consume the token. */
13282 cp_lexer_consume_token (parser->lexer);
13283 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13284 token);
13285 break;
13286 case RID_THREAD:
13287 /* Consume the token. */
13288 ds = ds_thread;
13289 cp_lexer_consume_token (parser->lexer);
13290 break;
13291
13292 default:
13293 /* We did not yet find a decl-specifier yet. */
13294 found_decl_spec = false;
13295 break;
13296 }
13297
13298 if (found_decl_spec
13299 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13300 && token->keyword != RID_CONSTEXPR)
13301 error ("decl-specifier invalid in condition");
13302
13303 if (found_decl_spec
13304 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13305 && token->keyword != RID_MUTABLE
13306 && token->keyword != RID_CONSTEXPR)
13307 error_at (token->location, "%qD invalid in lambda",
13308 ridpointers[token->keyword]);
13309
13310 if (ds != ds_last)
13311 set_and_check_decl_spec_loc (decl_specs, ds, token);
13312
13313 /* Constructors are a special case. The `S' in `S()' is not a
13314 decl-specifier; it is the beginning of the declarator. */
13315 constructor_p
13316 = (!found_decl_spec
13317 && constructor_possible_p
13318 && (cp_parser_constructor_declarator_p
13319 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13320
13321 /* If we don't have a DECL_SPEC yet, then we must be looking at
13322 a type-specifier. */
13323 if (!found_decl_spec && !constructor_p)
13324 {
13325 int decl_spec_declares_class_or_enum;
13326 bool is_cv_qualifier;
13327 tree type_spec;
13328
13329 type_spec
13330 = cp_parser_type_specifier (parser, flags,
13331 decl_specs,
13332 /*is_declaration=*/true,
13333 &decl_spec_declares_class_or_enum,
13334 &is_cv_qualifier);
13335 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13336
13337 /* If this type-specifier referenced a user-defined type
13338 (a typedef, class-name, etc.), then we can't allow any
13339 more such type-specifiers henceforth.
13340
13341 [dcl.spec]
13342
13343 The longest sequence of decl-specifiers that could
13344 possibly be a type name is taken as the
13345 decl-specifier-seq of a declaration. The sequence shall
13346 be self-consistent as described below.
13347
13348 [dcl.type]
13349
13350 As a general rule, at most one type-specifier is allowed
13351 in the complete decl-specifier-seq of a declaration. The
13352 only exceptions are the following:
13353
13354 -- const or volatile can be combined with any other
13355 type-specifier.
13356
13357 -- signed or unsigned can be combined with char, long,
13358 short, or int.
13359
13360 -- ..
13361
13362 Example:
13363
13364 typedef char* Pc;
13365 void g (const int Pc);
13366
13367 Here, Pc is *not* part of the decl-specifier seq; it's
13368 the declarator. Therefore, once we see a type-specifier
13369 (other than a cv-qualifier), we forbid any additional
13370 user-defined types. We *do* still allow things like `int
13371 int' to be considered a decl-specifier-seq, and issue the
13372 error message later. */
13373 if (type_spec && !is_cv_qualifier)
13374 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13375 /* A constructor declarator cannot follow a type-specifier. */
13376 if (type_spec)
13377 {
13378 constructor_possible_p = false;
13379 found_decl_spec = true;
13380 if (!is_cv_qualifier)
13381 decl_specs->any_type_specifiers_p = true;
13382 }
13383 }
13384
13385 /* If we still do not have a DECL_SPEC, then there are no more
13386 decl-specifiers. */
13387 if (!found_decl_spec)
13388 break;
13389
13390 decl_specs->any_specifiers_p = true;
13391 /* After we see one decl-specifier, further decl-specifiers are
13392 always optional. */
13393 flags |= CP_PARSER_FLAGS_OPTIONAL;
13394 }
13395
13396 /* Don't allow a friend specifier with a class definition. */
13397 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13398 && (*declares_class_or_enum & 2))
13399 error_at (decl_specs->locations[ds_friend],
13400 "class definition may not be declared a friend");
13401 }
13402
13403 /* Parse an (optional) storage-class-specifier.
13404
13405 storage-class-specifier:
13406 auto
13407 register
13408 static
13409 extern
13410 mutable
13411
13412 GNU Extension:
13413
13414 storage-class-specifier:
13415 thread
13416
13417 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13418
13419 static tree
13420 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13421 {
13422 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13423 {
13424 case RID_AUTO:
13425 if (cxx_dialect != cxx98)
13426 return NULL_TREE;
13427 /* Fall through for C++98. */
13428 gcc_fallthrough ();
13429
13430 case RID_REGISTER:
13431 case RID_STATIC:
13432 case RID_EXTERN:
13433 case RID_MUTABLE:
13434 case RID_THREAD:
13435 /* Consume the token. */
13436 return cp_lexer_consume_token (parser->lexer)->u.value;
13437
13438 default:
13439 return NULL_TREE;
13440 }
13441 }
13442
13443 /* Parse an (optional) function-specifier.
13444
13445 function-specifier:
13446 inline
13447 virtual
13448 explicit
13449
13450 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13451 Updates DECL_SPECS, if it is non-NULL. */
13452
13453 static tree
13454 cp_parser_function_specifier_opt (cp_parser* parser,
13455 cp_decl_specifier_seq *decl_specs)
13456 {
13457 cp_token *token = cp_lexer_peek_token (parser->lexer);
13458 switch (token->keyword)
13459 {
13460 case RID_INLINE:
13461 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13462 break;
13463
13464 case RID_VIRTUAL:
13465 /* 14.5.2.3 [temp.mem]
13466
13467 A member function template shall not be virtual. */
13468 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13469 && current_class_type)
13470 error_at (token->location, "templates may not be %<virtual%>");
13471 else
13472 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13473 break;
13474
13475 case RID_EXPLICIT:
13476 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13477 break;
13478
13479 default:
13480 return NULL_TREE;
13481 }
13482
13483 /* Consume the token. */
13484 return cp_lexer_consume_token (parser->lexer)->u.value;
13485 }
13486
13487 /* Parse a linkage-specification.
13488
13489 linkage-specification:
13490 extern string-literal { declaration-seq [opt] }
13491 extern string-literal declaration */
13492
13493 static void
13494 cp_parser_linkage_specification (cp_parser* parser)
13495 {
13496 tree linkage;
13497
13498 /* Look for the `extern' keyword. */
13499 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13500
13501 /* Look for the string-literal. */
13502 linkage = cp_parser_string_literal (parser, false, false);
13503
13504 /* Transform the literal into an identifier. If the literal is a
13505 wide-character string, or contains embedded NULs, then we can't
13506 handle it as the user wants. */
13507 if (strlen (TREE_STRING_POINTER (linkage))
13508 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13509 {
13510 cp_parser_error (parser, "invalid linkage-specification");
13511 /* Assume C++ linkage. */
13512 linkage = lang_name_cplusplus;
13513 }
13514 else
13515 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13516
13517 /* We're now using the new linkage. */
13518 push_lang_context (linkage);
13519
13520 /* If the next token is a `{', then we're using the first
13521 production. */
13522 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13523 {
13524 cp_ensure_no_omp_declare_simd (parser);
13525 cp_ensure_no_oacc_routine (parser);
13526
13527 /* Consume the `{' token. */
13528 cp_lexer_consume_token (parser->lexer);
13529 /* Parse the declarations. */
13530 cp_parser_declaration_seq_opt (parser);
13531 /* Look for the closing `}'. */
13532 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13533 }
13534 /* Otherwise, there's just one declaration. */
13535 else
13536 {
13537 bool saved_in_unbraced_linkage_specification_p;
13538
13539 saved_in_unbraced_linkage_specification_p
13540 = parser->in_unbraced_linkage_specification_p;
13541 parser->in_unbraced_linkage_specification_p = true;
13542 cp_parser_declaration (parser);
13543 parser->in_unbraced_linkage_specification_p
13544 = saved_in_unbraced_linkage_specification_p;
13545 }
13546
13547 /* We're done with the linkage-specification. */
13548 pop_lang_context ();
13549 }
13550
13551 /* Parse a static_assert-declaration.
13552
13553 static_assert-declaration:
13554 static_assert ( constant-expression , string-literal ) ;
13555 static_assert ( constant-expression ) ; (C++1Z)
13556
13557 If MEMBER_P, this static_assert is a class member. */
13558
13559 static void
13560 cp_parser_static_assert(cp_parser *parser, bool member_p)
13561 {
13562 tree condition;
13563 tree message;
13564 cp_token *token;
13565 location_t saved_loc;
13566 bool dummy;
13567
13568 /* Peek at the `static_assert' token so we can keep track of exactly
13569 where the static assertion started. */
13570 token = cp_lexer_peek_token (parser->lexer);
13571 saved_loc = token->location;
13572
13573 /* Look for the `static_assert' keyword. */
13574 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13575 RT_STATIC_ASSERT))
13576 return;
13577
13578 /* We know we are in a static assertion; commit to any tentative
13579 parse. */
13580 if (cp_parser_parsing_tentatively (parser))
13581 cp_parser_commit_to_tentative_parse (parser);
13582
13583 /* Parse the `(' starting the static assertion condition. */
13584 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13585
13586 /* Parse the constant-expression. Allow a non-constant expression
13587 here in order to give better diagnostics in finish_static_assert. */
13588 condition =
13589 cp_parser_constant_expression (parser,
13590 /*allow_non_constant_p=*/true,
13591 /*non_constant_p=*/&dummy);
13592
13593 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13594 {
13595 if (cxx_dialect < cxx1z)
13596 pedwarn (input_location, OPT_Wpedantic,
13597 "static_assert without a message "
13598 "only available with -std=c++1z or -std=gnu++1z");
13599 /* Eat the ')' */
13600 cp_lexer_consume_token (parser->lexer);
13601 message = build_string (1, "");
13602 TREE_TYPE (message) = char_array_type_node;
13603 fix_string_type (message);
13604 }
13605 else
13606 {
13607 /* Parse the separating `,'. */
13608 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13609
13610 /* Parse the string-literal message. */
13611 message = cp_parser_string_literal (parser,
13612 /*translate=*/false,
13613 /*wide_ok=*/true);
13614
13615 /* A `)' completes the static assertion. */
13616 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13617 cp_parser_skip_to_closing_parenthesis (parser,
13618 /*recovering=*/true,
13619 /*or_comma=*/false,
13620 /*consume_paren=*/true);
13621 }
13622
13623 /* A semicolon terminates the declaration. */
13624 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13625
13626 /* Complete the static assertion, which may mean either processing
13627 the static assert now or saving it for template instantiation. */
13628 finish_static_assert (condition, message, saved_loc, member_p);
13629 }
13630
13631 /* Parse the expression in decltype ( expression ). */
13632
13633 static tree
13634 cp_parser_decltype_expr (cp_parser *parser,
13635 bool &id_expression_or_member_access_p)
13636 {
13637 cp_token *id_expr_start_token;
13638 tree expr;
13639
13640 /* Since we're going to preserve any side-effects from this parse, set up a
13641 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13642 in the expression. */
13643 tentative_firewall firewall (parser);
13644
13645 /* First, try parsing an id-expression. */
13646 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13647 cp_parser_parse_tentatively (parser);
13648 expr = cp_parser_id_expression (parser,
13649 /*template_keyword_p=*/false,
13650 /*check_dependency_p=*/true,
13651 /*template_p=*/NULL,
13652 /*declarator_p=*/false,
13653 /*optional_p=*/false);
13654
13655 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13656 {
13657 bool non_integral_constant_expression_p = false;
13658 tree id_expression = expr;
13659 cp_id_kind idk;
13660 const char *error_msg;
13661
13662 if (identifier_p (expr))
13663 /* Lookup the name we got back from the id-expression. */
13664 expr = cp_parser_lookup_name_simple (parser, expr,
13665 id_expr_start_token->location);
13666
13667 if (expr
13668 && expr != error_mark_node
13669 && TREE_CODE (expr) != TYPE_DECL
13670 && (TREE_CODE (expr) != BIT_NOT_EXPR
13671 || !TYPE_P (TREE_OPERAND (expr, 0)))
13672 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13673 {
13674 /* Complete lookup of the id-expression. */
13675 expr = (finish_id_expression
13676 (id_expression, expr, parser->scope, &idk,
13677 /*integral_constant_expression_p=*/false,
13678 /*allow_non_integral_constant_expression_p=*/true,
13679 &non_integral_constant_expression_p,
13680 /*template_p=*/false,
13681 /*done=*/true,
13682 /*address_p=*/false,
13683 /*template_arg_p=*/false,
13684 &error_msg,
13685 id_expr_start_token->location));
13686
13687 if (expr == error_mark_node)
13688 /* We found an id-expression, but it was something that we
13689 should not have found. This is an error, not something
13690 we can recover from, so note that we found an
13691 id-expression and we'll recover as gracefully as
13692 possible. */
13693 id_expression_or_member_access_p = true;
13694 }
13695
13696 if (expr
13697 && expr != error_mark_node
13698 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13699 /* We have an id-expression. */
13700 id_expression_or_member_access_p = true;
13701 }
13702
13703 if (!id_expression_or_member_access_p)
13704 {
13705 /* Abort the id-expression parse. */
13706 cp_parser_abort_tentative_parse (parser);
13707
13708 /* Parsing tentatively, again. */
13709 cp_parser_parse_tentatively (parser);
13710
13711 /* Parse a class member access. */
13712 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13713 /*cast_p=*/false, /*decltype*/true,
13714 /*member_access_only_p=*/true, NULL);
13715
13716 if (expr
13717 && expr != error_mark_node
13718 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13719 /* We have an id-expression. */
13720 id_expression_or_member_access_p = true;
13721 }
13722
13723 if (id_expression_or_member_access_p)
13724 /* We have parsed the complete id-expression or member access. */
13725 cp_parser_parse_definitely (parser);
13726 else
13727 {
13728 /* Abort our attempt to parse an id-expression or member access
13729 expression. */
13730 cp_parser_abort_tentative_parse (parser);
13731
13732 /* Parse a full expression. */
13733 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13734 /*decltype_p=*/true);
13735 }
13736
13737 return expr;
13738 }
13739
13740 /* Parse a `decltype' type. Returns the type.
13741
13742 simple-type-specifier:
13743 decltype ( expression )
13744 C++14 proposal:
13745 decltype ( auto ) */
13746
13747 static tree
13748 cp_parser_decltype (cp_parser *parser)
13749 {
13750 tree expr;
13751 bool id_expression_or_member_access_p = false;
13752 const char *saved_message;
13753 bool saved_integral_constant_expression_p;
13754 bool saved_non_integral_constant_expression_p;
13755 bool saved_greater_than_is_operator_p;
13756 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13757
13758 if (start_token->type == CPP_DECLTYPE)
13759 {
13760 /* Already parsed. */
13761 cp_lexer_consume_token (parser->lexer);
13762 return saved_checks_value (start_token->u.tree_check_value);
13763 }
13764
13765 /* Look for the `decltype' token. */
13766 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13767 return error_mark_node;
13768
13769 /* Parse the opening `('. */
13770 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13771 return error_mark_node;
13772
13773 /* decltype (auto) */
13774 if (cxx_dialect >= cxx14
13775 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13776 {
13777 cp_lexer_consume_token (parser->lexer);
13778 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13779 return error_mark_node;
13780 expr = make_decltype_auto ();
13781 AUTO_IS_DECLTYPE (expr) = true;
13782 goto rewrite;
13783 }
13784
13785 /* Types cannot be defined in a `decltype' expression. Save away the
13786 old message. */
13787 saved_message = parser->type_definition_forbidden_message;
13788
13789 /* And create the new one. */
13790 parser->type_definition_forbidden_message
13791 = G_("types may not be defined in %<decltype%> expressions");
13792
13793 /* The restrictions on constant-expressions do not apply inside
13794 decltype expressions. */
13795 saved_integral_constant_expression_p
13796 = parser->integral_constant_expression_p;
13797 saved_non_integral_constant_expression_p
13798 = parser->non_integral_constant_expression_p;
13799 parser->integral_constant_expression_p = false;
13800
13801 /* Within a parenthesized expression, a `>' token is always
13802 the greater-than operator. */
13803 saved_greater_than_is_operator_p
13804 = parser->greater_than_is_operator_p;
13805 parser->greater_than_is_operator_p = true;
13806
13807 /* Do not actually evaluate the expression. */
13808 ++cp_unevaluated_operand;
13809
13810 /* Do not warn about problems with the expression. */
13811 ++c_inhibit_evaluation_warnings;
13812
13813 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13814
13815 /* Go back to evaluating expressions. */
13816 --cp_unevaluated_operand;
13817 --c_inhibit_evaluation_warnings;
13818
13819 /* The `>' token might be the end of a template-id or
13820 template-parameter-list now. */
13821 parser->greater_than_is_operator_p
13822 = saved_greater_than_is_operator_p;
13823
13824 /* Restore the old message and the integral constant expression
13825 flags. */
13826 parser->type_definition_forbidden_message = saved_message;
13827 parser->integral_constant_expression_p
13828 = saved_integral_constant_expression_p;
13829 parser->non_integral_constant_expression_p
13830 = saved_non_integral_constant_expression_p;
13831
13832 /* Parse to the closing `)'. */
13833 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13834 {
13835 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13836 /*consume_paren=*/true);
13837 return error_mark_node;
13838 }
13839
13840 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13841 tf_warning_or_error);
13842
13843 rewrite:
13844 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13845 it again. */
13846 start_token->type = CPP_DECLTYPE;
13847 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13848 start_token->u.tree_check_value->value = expr;
13849 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13850 start_token->keyword = RID_MAX;
13851 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13852
13853 return expr;
13854 }
13855
13856 /* Special member functions [gram.special] */
13857
13858 /* Parse a conversion-function-id.
13859
13860 conversion-function-id:
13861 operator conversion-type-id
13862
13863 Returns an IDENTIFIER_NODE representing the operator. */
13864
13865 static tree
13866 cp_parser_conversion_function_id (cp_parser* parser)
13867 {
13868 tree type;
13869 tree saved_scope;
13870 tree saved_qualifying_scope;
13871 tree saved_object_scope;
13872 tree pushed_scope = NULL_TREE;
13873
13874 /* Look for the `operator' token. */
13875 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13876 return error_mark_node;
13877 /* When we parse the conversion-type-id, the current scope will be
13878 reset. However, we need that information in able to look up the
13879 conversion function later, so we save it here. */
13880 saved_scope = parser->scope;
13881 saved_qualifying_scope = parser->qualifying_scope;
13882 saved_object_scope = parser->object_scope;
13883 /* We must enter the scope of the class so that the names of
13884 entities declared within the class are available in the
13885 conversion-type-id. For example, consider:
13886
13887 struct S {
13888 typedef int I;
13889 operator I();
13890 };
13891
13892 S::operator I() { ... }
13893
13894 In order to see that `I' is a type-name in the definition, we
13895 must be in the scope of `S'. */
13896 if (saved_scope)
13897 pushed_scope = push_scope (saved_scope);
13898 /* Parse the conversion-type-id. */
13899 type = cp_parser_conversion_type_id (parser);
13900 /* Leave the scope of the class, if any. */
13901 if (pushed_scope)
13902 pop_scope (pushed_scope);
13903 /* Restore the saved scope. */
13904 parser->scope = saved_scope;
13905 parser->qualifying_scope = saved_qualifying_scope;
13906 parser->object_scope = saved_object_scope;
13907 /* If the TYPE is invalid, indicate failure. */
13908 if (type == error_mark_node)
13909 return error_mark_node;
13910 return mangle_conv_op_name_for_type (type);
13911 }
13912
13913 /* Parse a conversion-type-id:
13914
13915 conversion-type-id:
13916 type-specifier-seq conversion-declarator [opt]
13917
13918 Returns the TYPE specified. */
13919
13920 static tree
13921 cp_parser_conversion_type_id (cp_parser* parser)
13922 {
13923 tree attributes;
13924 cp_decl_specifier_seq type_specifiers;
13925 cp_declarator *declarator;
13926 tree type_specified;
13927 const char *saved_message;
13928
13929 /* Parse the attributes. */
13930 attributes = cp_parser_attributes_opt (parser);
13931
13932 saved_message = parser->type_definition_forbidden_message;
13933 parser->type_definition_forbidden_message
13934 = G_("types may not be defined in a conversion-type-id");
13935
13936 /* Parse the type-specifiers. */
13937 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13938 /*is_trailing_return=*/false,
13939 &type_specifiers);
13940
13941 parser->type_definition_forbidden_message = saved_message;
13942
13943 /* If that didn't work, stop. */
13944 if (type_specifiers.type == error_mark_node)
13945 return error_mark_node;
13946 /* Parse the conversion-declarator. */
13947 declarator = cp_parser_conversion_declarator_opt (parser);
13948
13949 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13950 /*initialized=*/0, &attributes);
13951 if (attributes)
13952 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13953
13954 /* Don't give this error when parsing tentatively. This happens to
13955 work because we always parse this definitively once. */
13956 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13957 && type_uses_auto (type_specified))
13958 {
13959 if (cxx_dialect < cxx14)
13960 {
13961 error ("invalid use of %<auto%> in conversion operator");
13962 return error_mark_node;
13963 }
13964 else if (template_parm_scope_p ())
13965 warning (0, "use of %<auto%> in member template "
13966 "conversion operator can never be deduced");
13967 }
13968
13969 return type_specified;
13970 }
13971
13972 /* Parse an (optional) conversion-declarator.
13973
13974 conversion-declarator:
13975 ptr-operator conversion-declarator [opt]
13976
13977 */
13978
13979 static cp_declarator *
13980 cp_parser_conversion_declarator_opt (cp_parser* parser)
13981 {
13982 enum tree_code code;
13983 tree class_type, std_attributes = NULL_TREE;
13984 cp_cv_quals cv_quals;
13985
13986 /* We don't know if there's a ptr-operator next, or not. */
13987 cp_parser_parse_tentatively (parser);
13988 /* Try the ptr-operator. */
13989 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13990 &std_attributes);
13991 /* If it worked, look for more conversion-declarators. */
13992 if (cp_parser_parse_definitely (parser))
13993 {
13994 cp_declarator *declarator;
13995
13996 /* Parse another optional declarator. */
13997 declarator = cp_parser_conversion_declarator_opt (parser);
13998
13999 declarator = cp_parser_make_indirect_declarator
14000 (code, class_type, cv_quals, declarator, std_attributes);
14001
14002 return declarator;
14003 }
14004
14005 return NULL;
14006 }
14007
14008 /* Parse an (optional) ctor-initializer.
14009
14010 ctor-initializer:
14011 : mem-initializer-list
14012
14013 Returns TRUE iff the ctor-initializer was actually present. */
14014
14015 static bool
14016 cp_parser_ctor_initializer_opt (cp_parser* parser)
14017 {
14018 /* If the next token is not a `:', then there is no
14019 ctor-initializer. */
14020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14021 {
14022 /* Do default initialization of any bases and members. */
14023 if (DECL_CONSTRUCTOR_P (current_function_decl))
14024 finish_mem_initializers (NULL_TREE);
14025
14026 return false;
14027 }
14028
14029 /* Consume the `:' token. */
14030 cp_lexer_consume_token (parser->lexer);
14031 /* And the mem-initializer-list. */
14032 cp_parser_mem_initializer_list (parser);
14033
14034 return true;
14035 }
14036
14037 /* Parse a mem-initializer-list.
14038
14039 mem-initializer-list:
14040 mem-initializer ... [opt]
14041 mem-initializer ... [opt] , mem-initializer-list */
14042
14043 static void
14044 cp_parser_mem_initializer_list (cp_parser* parser)
14045 {
14046 tree mem_initializer_list = NULL_TREE;
14047 tree target_ctor = error_mark_node;
14048 cp_token *token = cp_lexer_peek_token (parser->lexer);
14049
14050 /* Let the semantic analysis code know that we are starting the
14051 mem-initializer-list. */
14052 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14053 error_at (token->location,
14054 "only constructors take member initializers");
14055
14056 /* Loop through the list. */
14057 while (true)
14058 {
14059 tree mem_initializer;
14060
14061 token = cp_lexer_peek_token (parser->lexer);
14062 /* Parse the mem-initializer. */
14063 mem_initializer = cp_parser_mem_initializer (parser);
14064 /* If the next token is a `...', we're expanding member initializers. */
14065 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14066 {
14067 /* Consume the `...'. */
14068 cp_lexer_consume_token (parser->lexer);
14069
14070 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14071 can be expanded but members cannot. */
14072 if (mem_initializer != error_mark_node
14073 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14074 {
14075 error_at (token->location,
14076 "cannot expand initializer for member %<%D%>",
14077 TREE_PURPOSE (mem_initializer));
14078 mem_initializer = error_mark_node;
14079 }
14080
14081 /* Construct the pack expansion type. */
14082 if (mem_initializer != error_mark_node)
14083 mem_initializer = make_pack_expansion (mem_initializer);
14084 }
14085 if (target_ctor != error_mark_node
14086 && mem_initializer != error_mark_node)
14087 {
14088 error ("mem-initializer for %qD follows constructor delegation",
14089 TREE_PURPOSE (mem_initializer));
14090 mem_initializer = error_mark_node;
14091 }
14092 /* Look for a target constructor. */
14093 if (mem_initializer != error_mark_node
14094 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14095 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14096 {
14097 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14098 if (mem_initializer_list)
14099 {
14100 error ("constructor delegation follows mem-initializer for %qD",
14101 TREE_PURPOSE (mem_initializer_list));
14102 mem_initializer = error_mark_node;
14103 }
14104 target_ctor = mem_initializer;
14105 }
14106 /* Add it to the list, unless it was erroneous. */
14107 if (mem_initializer != error_mark_node)
14108 {
14109 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14110 mem_initializer_list = mem_initializer;
14111 }
14112 /* If the next token is not a `,', we're done. */
14113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14114 break;
14115 /* Consume the `,' token. */
14116 cp_lexer_consume_token (parser->lexer);
14117 }
14118
14119 /* Perform semantic analysis. */
14120 if (DECL_CONSTRUCTOR_P (current_function_decl))
14121 finish_mem_initializers (mem_initializer_list);
14122 }
14123
14124 /* Parse a mem-initializer.
14125
14126 mem-initializer:
14127 mem-initializer-id ( expression-list [opt] )
14128 mem-initializer-id braced-init-list
14129
14130 GNU extension:
14131
14132 mem-initializer:
14133 ( expression-list [opt] )
14134
14135 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14136 class) or FIELD_DECL (for a non-static data member) to initialize;
14137 the TREE_VALUE is the expression-list. An empty initialization
14138 list is represented by void_list_node. */
14139
14140 static tree
14141 cp_parser_mem_initializer (cp_parser* parser)
14142 {
14143 tree mem_initializer_id;
14144 tree expression_list;
14145 tree member;
14146 cp_token *token = cp_lexer_peek_token (parser->lexer);
14147
14148 /* Find out what is being initialized. */
14149 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14150 {
14151 permerror (token->location,
14152 "anachronistic old-style base class initializer");
14153 mem_initializer_id = NULL_TREE;
14154 }
14155 else
14156 {
14157 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14158 if (mem_initializer_id == error_mark_node)
14159 return mem_initializer_id;
14160 }
14161 member = expand_member_init (mem_initializer_id);
14162 if (member && !DECL_P (member))
14163 in_base_initializer = 1;
14164
14165 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14166 {
14167 bool expr_non_constant_p;
14168 cp_lexer_set_source_position (parser->lexer);
14169 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14170 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14171 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14172 expression_list = build_tree_list (NULL_TREE, expression_list);
14173 }
14174 else
14175 {
14176 vec<tree, va_gc> *vec;
14177 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14178 /*cast_p=*/false,
14179 /*allow_expansion_p=*/true,
14180 /*non_constant_p=*/NULL);
14181 if (vec == NULL)
14182 return error_mark_node;
14183 expression_list = build_tree_list_vec (vec);
14184 release_tree_vector (vec);
14185 }
14186
14187 if (expression_list == error_mark_node)
14188 return error_mark_node;
14189 if (!expression_list)
14190 expression_list = void_type_node;
14191
14192 in_base_initializer = 0;
14193
14194 return member ? build_tree_list (member, expression_list) : error_mark_node;
14195 }
14196
14197 /* Parse a mem-initializer-id.
14198
14199 mem-initializer-id:
14200 :: [opt] nested-name-specifier [opt] class-name
14201 decltype-specifier (C++11)
14202 identifier
14203
14204 Returns a TYPE indicating the class to be initialized for the first
14205 production (and the second in C++11). Returns an IDENTIFIER_NODE
14206 indicating the data member to be initialized for the last production. */
14207
14208 static tree
14209 cp_parser_mem_initializer_id (cp_parser* parser)
14210 {
14211 bool global_scope_p;
14212 bool nested_name_specifier_p;
14213 bool template_p = false;
14214 tree id;
14215
14216 cp_token *token = cp_lexer_peek_token (parser->lexer);
14217
14218 /* `typename' is not allowed in this context ([temp.res]). */
14219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14220 {
14221 error_at (token->location,
14222 "keyword %<typename%> not allowed in this context (a qualified "
14223 "member initializer is implicitly a type)");
14224 cp_lexer_consume_token (parser->lexer);
14225 }
14226 /* Look for the optional `::' operator. */
14227 global_scope_p
14228 = (cp_parser_global_scope_opt (parser,
14229 /*current_scope_valid_p=*/false)
14230 != NULL_TREE);
14231 /* Look for the optional nested-name-specifier. The simplest way to
14232 implement:
14233
14234 [temp.res]
14235
14236 The keyword `typename' is not permitted in a base-specifier or
14237 mem-initializer; in these contexts a qualified name that
14238 depends on a template-parameter is implicitly assumed to be a
14239 type name.
14240
14241 is to assume that we have seen the `typename' keyword at this
14242 point. */
14243 nested_name_specifier_p
14244 = (cp_parser_nested_name_specifier_opt (parser,
14245 /*typename_keyword_p=*/true,
14246 /*check_dependency_p=*/true,
14247 /*type_p=*/true,
14248 /*is_declaration=*/true)
14249 != NULL_TREE);
14250 if (nested_name_specifier_p)
14251 template_p = cp_parser_optional_template_keyword (parser);
14252 /* If there is a `::' operator or a nested-name-specifier, then we
14253 are definitely looking for a class-name. */
14254 if (global_scope_p || nested_name_specifier_p)
14255 return cp_parser_class_name (parser,
14256 /*typename_keyword_p=*/true,
14257 /*template_keyword_p=*/template_p,
14258 typename_type,
14259 /*check_dependency_p=*/true,
14260 /*class_head_p=*/false,
14261 /*is_declaration=*/true);
14262 /* Otherwise, we could also be looking for an ordinary identifier. */
14263 cp_parser_parse_tentatively (parser);
14264 if (cp_lexer_next_token_is_decltype (parser->lexer))
14265 /* Try a decltype-specifier. */
14266 id = cp_parser_decltype (parser);
14267 else
14268 /* Otherwise, try a class-name. */
14269 id = cp_parser_class_name (parser,
14270 /*typename_keyword_p=*/true,
14271 /*template_keyword_p=*/false,
14272 none_type,
14273 /*check_dependency_p=*/true,
14274 /*class_head_p=*/false,
14275 /*is_declaration=*/true);
14276 /* If we found one, we're done. */
14277 if (cp_parser_parse_definitely (parser))
14278 return id;
14279 /* Otherwise, look for an ordinary identifier. */
14280 return cp_parser_identifier (parser);
14281 }
14282
14283 /* Overloading [gram.over] */
14284
14285 /* Parse an operator-function-id.
14286
14287 operator-function-id:
14288 operator operator
14289
14290 Returns an IDENTIFIER_NODE for the operator which is a
14291 human-readable spelling of the identifier, e.g., `operator +'. */
14292
14293 static cp_expr
14294 cp_parser_operator_function_id (cp_parser* parser)
14295 {
14296 /* Look for the `operator' keyword. */
14297 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14298 return error_mark_node;
14299 /* And then the name of the operator itself. */
14300 return cp_parser_operator (parser);
14301 }
14302
14303 /* Return an identifier node for a user-defined literal operator.
14304 The suffix identifier is chained to the operator name identifier. */
14305
14306 tree
14307 cp_literal_operator_id (const char* name)
14308 {
14309 tree identifier;
14310 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14311 + strlen (name) + 10);
14312 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14313 identifier = get_identifier (buffer);
14314
14315 return identifier;
14316 }
14317
14318 /* Parse an operator.
14319
14320 operator:
14321 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14322 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14323 || ++ -- , ->* -> () []
14324
14325 GNU Extensions:
14326
14327 operator:
14328 <? >? <?= >?=
14329
14330 Returns an IDENTIFIER_NODE for the operator which is a
14331 human-readable spelling of the identifier, e.g., `operator +'. */
14332
14333 static cp_expr
14334 cp_parser_operator (cp_parser* parser)
14335 {
14336 tree id = NULL_TREE;
14337 cp_token *token;
14338 bool utf8 = false;
14339
14340 /* Peek at the next token. */
14341 token = cp_lexer_peek_token (parser->lexer);
14342
14343 location_t start_loc = token->location;
14344
14345 /* Figure out which operator we have. */
14346 switch (token->type)
14347 {
14348 case CPP_KEYWORD:
14349 {
14350 enum tree_code op;
14351
14352 /* The keyword should be either `new' or `delete'. */
14353 if (token->keyword == RID_NEW)
14354 op = NEW_EXPR;
14355 else if (token->keyword == RID_DELETE)
14356 op = DELETE_EXPR;
14357 else
14358 break;
14359
14360 /* Consume the `new' or `delete' token. */
14361 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14362
14363 /* Peek at the next token. */
14364 token = cp_lexer_peek_token (parser->lexer);
14365 /* If it's a `[' token then this is the array variant of the
14366 operator. */
14367 if (token->type == CPP_OPEN_SQUARE)
14368 {
14369 /* Consume the `[' token. */
14370 cp_lexer_consume_token (parser->lexer);
14371 /* Look for the `]' token. */
14372 if (cp_token *close_token
14373 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14374 end_loc = close_token->location;
14375 id = cp_operator_id (op == NEW_EXPR
14376 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14377 }
14378 /* Otherwise, we have the non-array variant. */
14379 else
14380 id = cp_operator_id (op);
14381
14382 location_t loc = make_location (start_loc, start_loc, end_loc);
14383
14384 return cp_expr (id, loc);
14385 }
14386
14387 case CPP_PLUS:
14388 id = cp_operator_id (PLUS_EXPR);
14389 break;
14390
14391 case CPP_MINUS:
14392 id = cp_operator_id (MINUS_EXPR);
14393 break;
14394
14395 case CPP_MULT:
14396 id = cp_operator_id (MULT_EXPR);
14397 break;
14398
14399 case CPP_DIV:
14400 id = cp_operator_id (TRUNC_DIV_EXPR);
14401 break;
14402
14403 case CPP_MOD:
14404 id = cp_operator_id (TRUNC_MOD_EXPR);
14405 break;
14406
14407 case CPP_XOR:
14408 id = cp_operator_id (BIT_XOR_EXPR);
14409 break;
14410
14411 case CPP_AND:
14412 id = cp_operator_id (BIT_AND_EXPR);
14413 break;
14414
14415 case CPP_OR:
14416 id = cp_operator_id (BIT_IOR_EXPR);
14417 break;
14418
14419 case CPP_COMPL:
14420 id = cp_operator_id (BIT_NOT_EXPR);
14421 break;
14422
14423 case CPP_NOT:
14424 id = cp_operator_id (TRUTH_NOT_EXPR);
14425 break;
14426
14427 case CPP_EQ:
14428 id = cp_assignment_operator_id (NOP_EXPR);
14429 break;
14430
14431 case CPP_LESS:
14432 id = cp_operator_id (LT_EXPR);
14433 break;
14434
14435 case CPP_GREATER:
14436 id = cp_operator_id (GT_EXPR);
14437 break;
14438
14439 case CPP_PLUS_EQ:
14440 id = cp_assignment_operator_id (PLUS_EXPR);
14441 break;
14442
14443 case CPP_MINUS_EQ:
14444 id = cp_assignment_operator_id (MINUS_EXPR);
14445 break;
14446
14447 case CPP_MULT_EQ:
14448 id = cp_assignment_operator_id (MULT_EXPR);
14449 break;
14450
14451 case CPP_DIV_EQ:
14452 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14453 break;
14454
14455 case CPP_MOD_EQ:
14456 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14457 break;
14458
14459 case CPP_XOR_EQ:
14460 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14461 break;
14462
14463 case CPP_AND_EQ:
14464 id = cp_assignment_operator_id (BIT_AND_EXPR);
14465 break;
14466
14467 case CPP_OR_EQ:
14468 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14469 break;
14470
14471 case CPP_LSHIFT:
14472 id = cp_operator_id (LSHIFT_EXPR);
14473 break;
14474
14475 case CPP_RSHIFT:
14476 id = cp_operator_id (RSHIFT_EXPR);
14477 break;
14478
14479 case CPP_LSHIFT_EQ:
14480 id = cp_assignment_operator_id (LSHIFT_EXPR);
14481 break;
14482
14483 case CPP_RSHIFT_EQ:
14484 id = cp_assignment_operator_id (RSHIFT_EXPR);
14485 break;
14486
14487 case CPP_EQ_EQ:
14488 id = cp_operator_id (EQ_EXPR);
14489 break;
14490
14491 case CPP_NOT_EQ:
14492 id = cp_operator_id (NE_EXPR);
14493 break;
14494
14495 case CPP_LESS_EQ:
14496 id = cp_operator_id (LE_EXPR);
14497 break;
14498
14499 case CPP_GREATER_EQ:
14500 id = cp_operator_id (GE_EXPR);
14501 break;
14502
14503 case CPP_AND_AND:
14504 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14505 break;
14506
14507 case CPP_OR_OR:
14508 id = cp_operator_id (TRUTH_ORIF_EXPR);
14509 break;
14510
14511 case CPP_PLUS_PLUS:
14512 id = cp_operator_id (POSTINCREMENT_EXPR);
14513 break;
14514
14515 case CPP_MINUS_MINUS:
14516 id = cp_operator_id (PREDECREMENT_EXPR);
14517 break;
14518
14519 case CPP_COMMA:
14520 id = cp_operator_id (COMPOUND_EXPR);
14521 break;
14522
14523 case CPP_DEREF_STAR:
14524 id = cp_operator_id (MEMBER_REF);
14525 break;
14526
14527 case CPP_DEREF:
14528 id = cp_operator_id (COMPONENT_REF);
14529 break;
14530
14531 case CPP_OPEN_PAREN:
14532 /* Consume the `('. */
14533 cp_lexer_consume_token (parser->lexer);
14534 /* Look for the matching `)'. */
14535 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14536 return cp_operator_id (CALL_EXPR);
14537
14538 case CPP_OPEN_SQUARE:
14539 /* Consume the `['. */
14540 cp_lexer_consume_token (parser->lexer);
14541 /* Look for the matching `]'. */
14542 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14543 return cp_operator_id (ARRAY_REF);
14544
14545 case CPP_UTF8STRING:
14546 case CPP_UTF8STRING_USERDEF:
14547 utf8 = true;
14548 /* FALLTHRU */
14549 case CPP_STRING:
14550 case CPP_WSTRING:
14551 case CPP_STRING16:
14552 case CPP_STRING32:
14553 case CPP_STRING_USERDEF:
14554 case CPP_WSTRING_USERDEF:
14555 case CPP_STRING16_USERDEF:
14556 case CPP_STRING32_USERDEF:
14557 {
14558 tree str, string_tree;
14559 int sz, len;
14560
14561 if (cxx_dialect == cxx98)
14562 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14563
14564 /* Consume the string. */
14565 str = cp_parser_string_literal (parser, /*translate=*/true,
14566 /*wide_ok=*/true, /*lookup_udlit=*/false);
14567 if (str == error_mark_node)
14568 return error_mark_node;
14569 else if (TREE_CODE (str) == USERDEF_LITERAL)
14570 {
14571 string_tree = USERDEF_LITERAL_VALUE (str);
14572 id = USERDEF_LITERAL_SUFFIX_ID (str);
14573 }
14574 else
14575 {
14576 string_tree = str;
14577 /* Look for the suffix identifier. */
14578 token = cp_lexer_peek_token (parser->lexer);
14579 if (token->type == CPP_NAME)
14580 id = cp_parser_identifier (parser);
14581 else if (token->type == CPP_KEYWORD)
14582 {
14583 error ("unexpected keyword;"
14584 " remove space between quotes and suffix identifier");
14585 return error_mark_node;
14586 }
14587 else
14588 {
14589 error ("expected suffix identifier");
14590 return error_mark_node;
14591 }
14592 }
14593 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14594 (TREE_TYPE (TREE_TYPE (string_tree))));
14595 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14596 if (len != 0)
14597 {
14598 error ("expected empty string after %<operator%> keyword");
14599 return error_mark_node;
14600 }
14601 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14602 != char_type_node)
14603 {
14604 error ("invalid encoding prefix in literal operator");
14605 return error_mark_node;
14606 }
14607 if (id != error_mark_node)
14608 {
14609 const char *name = IDENTIFIER_POINTER (id);
14610 id = cp_literal_operator_id (name);
14611 }
14612 return id;
14613 }
14614
14615 default:
14616 /* Anything else is an error. */
14617 break;
14618 }
14619
14620 /* If we have selected an identifier, we need to consume the
14621 operator token. */
14622 if (id)
14623 cp_lexer_consume_token (parser->lexer);
14624 /* Otherwise, no valid operator name was present. */
14625 else
14626 {
14627 cp_parser_error (parser, "expected operator");
14628 id = error_mark_node;
14629 }
14630
14631 return cp_expr (id, start_loc);
14632 }
14633
14634 /* Parse a template-declaration.
14635
14636 template-declaration:
14637 export [opt] template < template-parameter-list > declaration
14638
14639 If MEMBER_P is TRUE, this template-declaration occurs within a
14640 class-specifier.
14641
14642 The grammar rule given by the standard isn't correct. What
14643 is really meant is:
14644
14645 template-declaration:
14646 export [opt] template-parameter-list-seq
14647 decl-specifier-seq [opt] init-declarator [opt] ;
14648 export [opt] template-parameter-list-seq
14649 function-definition
14650
14651 template-parameter-list-seq:
14652 template-parameter-list-seq [opt]
14653 template < template-parameter-list >
14654
14655 Concept Extensions:
14656
14657 template-parameter-list-seq:
14658 template < template-parameter-list > requires-clause [opt]
14659
14660 requires-clause:
14661 requires logical-or-expression */
14662
14663 static void
14664 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14665 {
14666 /* Check for `export'. */
14667 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14668 {
14669 /* Consume the `export' token. */
14670 cp_lexer_consume_token (parser->lexer);
14671 /* Warn that we do not support `export'. */
14672 warning (0, "keyword %<export%> not implemented, and will be ignored");
14673 }
14674
14675 cp_parser_template_declaration_after_export (parser, member_p);
14676 }
14677
14678 /* Parse a template-parameter-list.
14679
14680 template-parameter-list:
14681 template-parameter
14682 template-parameter-list , template-parameter
14683
14684 Returns a TREE_LIST. Each node represents a template parameter.
14685 The nodes are connected via their TREE_CHAINs. */
14686
14687 static tree
14688 cp_parser_template_parameter_list (cp_parser* parser)
14689 {
14690 tree parameter_list = NULL_TREE;
14691
14692 begin_template_parm_list ();
14693
14694 /* The loop below parses the template parms. We first need to know
14695 the total number of template parms to be able to compute proper
14696 canonical types of each dependent type. So after the loop, when
14697 we know the total number of template parms,
14698 end_template_parm_list computes the proper canonical types and
14699 fixes up the dependent types accordingly. */
14700 while (true)
14701 {
14702 tree parameter;
14703 bool is_non_type;
14704 bool is_parameter_pack;
14705 location_t parm_loc;
14706
14707 /* Parse the template-parameter. */
14708 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14709 parameter = cp_parser_template_parameter (parser,
14710 &is_non_type,
14711 &is_parameter_pack);
14712 /* Add it to the list. */
14713 if (parameter != error_mark_node)
14714 parameter_list = process_template_parm (parameter_list,
14715 parm_loc,
14716 parameter,
14717 is_non_type,
14718 is_parameter_pack);
14719 else
14720 {
14721 tree err_parm = build_tree_list (parameter, parameter);
14722 parameter_list = chainon (parameter_list, err_parm);
14723 }
14724
14725 /* If the next token is not a `,', we're done. */
14726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14727 break;
14728 /* Otherwise, consume the `,' token. */
14729 cp_lexer_consume_token (parser->lexer);
14730 }
14731
14732 return end_template_parm_list (parameter_list);
14733 }
14734
14735 /* Parse a introduction-list.
14736
14737 introduction-list:
14738 introduced-parameter
14739 introduction-list , introduced-parameter
14740
14741 introduced-parameter:
14742 ...[opt] identifier
14743
14744 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14745 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14746 WILDCARD_DECL will also have DECL_NAME set and token location in
14747 DECL_SOURCE_LOCATION. */
14748
14749 static tree
14750 cp_parser_introduction_list (cp_parser *parser)
14751 {
14752 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14753
14754 while (true)
14755 {
14756 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14757 if (is_pack)
14758 cp_lexer_consume_token (parser->lexer);
14759
14760 /* Build placeholder. */
14761 tree parm = build_nt (WILDCARD_DECL);
14762 DECL_SOURCE_LOCATION (parm)
14763 = cp_lexer_peek_token (parser->lexer)->location;
14764 DECL_NAME (parm) = cp_parser_identifier (parser);
14765 WILDCARD_PACK_P (parm) = is_pack;
14766 vec_safe_push (introduction_vec, parm);
14767
14768 /* If the next token is not a `,', we're done. */
14769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14770 break;
14771 /* Otherwise, consume the `,' token. */
14772 cp_lexer_consume_token (parser->lexer);
14773 }
14774
14775 /* Convert the vec into a TREE_VEC. */
14776 tree introduction_list = make_tree_vec (introduction_vec->length ());
14777 unsigned int n;
14778 tree parm;
14779 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14780 TREE_VEC_ELT (introduction_list, n) = parm;
14781
14782 release_tree_vector (introduction_vec);
14783 return introduction_list;
14784 }
14785
14786 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14787 is an abstract declarator. */
14788
14789 static inline cp_declarator*
14790 get_id_declarator (cp_declarator *declarator)
14791 {
14792 cp_declarator *d = declarator;
14793 while (d && d->kind != cdk_id)
14794 d = d->declarator;
14795 return d;
14796 }
14797
14798 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14799 is an abstract declarator. */
14800
14801 static inline tree
14802 get_unqualified_id (cp_declarator *declarator)
14803 {
14804 declarator = get_id_declarator (declarator);
14805 if (declarator)
14806 return declarator->u.id.unqualified_name;
14807 else
14808 return NULL_TREE;
14809 }
14810
14811 /* Returns true if DECL represents a constrained-parameter. */
14812
14813 static inline bool
14814 is_constrained_parameter (tree decl)
14815 {
14816 return (decl
14817 && TREE_CODE (decl) == TYPE_DECL
14818 && CONSTRAINED_PARM_CONCEPT (decl)
14819 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14820 }
14821
14822 /* Returns true if PARM declares a constrained-parameter. */
14823
14824 static inline bool
14825 is_constrained_parameter (cp_parameter_declarator *parm)
14826 {
14827 return is_constrained_parameter (parm->decl_specifiers.type);
14828 }
14829
14830 /* Check that the type parameter is only a declarator-id, and that its
14831 type is not cv-qualified. */
14832
14833 bool
14834 cp_parser_check_constrained_type_parm (cp_parser *parser,
14835 cp_parameter_declarator *parm)
14836 {
14837 if (!parm->declarator)
14838 return true;
14839
14840 if (parm->declarator->kind != cdk_id)
14841 {
14842 cp_parser_error (parser, "invalid constrained type parameter");
14843 return false;
14844 }
14845
14846 /* Don't allow cv-qualified type parameters. */
14847 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14848 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14849 {
14850 cp_parser_error (parser, "cv-qualified type parameter");
14851 return false;
14852 }
14853
14854 return true;
14855 }
14856
14857 /* Finish parsing/processing a template type parameter and checking
14858 various restrictions. */
14859
14860 static inline tree
14861 cp_parser_constrained_type_template_parm (cp_parser *parser,
14862 tree id,
14863 cp_parameter_declarator* parmdecl)
14864 {
14865 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14866 return finish_template_type_parm (class_type_node, id);
14867 else
14868 return error_mark_node;
14869 }
14870
14871 static tree
14872 finish_constrained_template_template_parm (tree proto, tree id)
14873 {
14874 /* FIXME: This should probably be copied, and we may need to adjust
14875 the template parameter depths. */
14876 tree saved_parms = current_template_parms;
14877 begin_template_parm_list ();
14878 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14879 end_template_parm_list ();
14880
14881 tree parm = finish_template_template_parm (class_type_node, id);
14882 current_template_parms = saved_parms;
14883
14884 return parm;
14885 }
14886
14887 /* Finish parsing/processing a template template parameter by borrowing
14888 the template parameter list from the prototype parameter. */
14889
14890 static tree
14891 cp_parser_constrained_template_template_parm (cp_parser *parser,
14892 tree proto,
14893 tree id,
14894 cp_parameter_declarator *parmdecl)
14895 {
14896 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14897 return error_mark_node;
14898 return finish_constrained_template_template_parm (proto, id);
14899 }
14900
14901 /* Create a new non-type template parameter from the given PARM
14902 declarator. */
14903
14904 static tree
14905 constrained_non_type_template_parm (bool *is_non_type,
14906 cp_parameter_declarator *parm)
14907 {
14908 *is_non_type = true;
14909 cp_declarator *decl = parm->declarator;
14910 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14911 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14912 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14913 }
14914
14915 /* Build a constrained template parameter based on the PARMDECL
14916 declarator. The type of PARMDECL is the constrained type, which
14917 refers to the prototype template parameter that ultimately
14918 specifies the type of the declared parameter. */
14919
14920 static tree
14921 finish_constrained_parameter (cp_parser *parser,
14922 cp_parameter_declarator *parmdecl,
14923 bool *is_non_type,
14924 bool *is_parameter_pack)
14925 {
14926 tree decl = parmdecl->decl_specifiers.type;
14927 tree id = get_unqualified_id (parmdecl->declarator);
14928 tree def = parmdecl->default_argument;
14929 tree proto = DECL_INITIAL (decl);
14930
14931 /* A template parameter constrained by a variadic concept shall also
14932 be declared as a template parameter pack. */
14933 bool is_variadic = template_parameter_pack_p (proto);
14934 if (is_variadic && !*is_parameter_pack)
14935 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14936
14937 /* Build the parameter. Return an error if the declarator was invalid. */
14938 tree parm;
14939 if (TREE_CODE (proto) == TYPE_DECL)
14940 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14941 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14942 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14943 parmdecl);
14944 else
14945 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14946 if (parm == error_mark_node)
14947 return error_mark_node;
14948
14949 /* Finish the parameter decl and create a node attaching the
14950 default argument and constraint. */
14951 parm = build_tree_list (def, parm);
14952 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14953
14954 return parm;
14955 }
14956
14957 /* Returns true if the parsed type actually represents the declaration
14958 of a type template-parameter. */
14959
14960 static inline bool
14961 declares_constrained_type_template_parameter (tree type)
14962 {
14963 return (is_constrained_parameter (type)
14964 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14965 }
14966
14967
14968 /* Returns true if the parsed type actually represents the declaration of
14969 a template template-parameter. */
14970
14971 static bool
14972 declares_constrained_template_template_parameter (tree type)
14973 {
14974 return (is_constrained_parameter (type)
14975 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14976 }
14977
14978 /* Parse a default argument for a type template-parameter.
14979 Note that diagnostics are handled in cp_parser_template_parameter. */
14980
14981 static tree
14982 cp_parser_default_type_template_argument (cp_parser *parser)
14983 {
14984 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14985
14986 /* Consume the `=' token. */
14987 cp_lexer_consume_token (parser->lexer);
14988
14989 cp_token *token = cp_lexer_peek_token (parser->lexer);
14990
14991 /* Parse the default-argument. */
14992 push_deferring_access_checks (dk_no_deferred);
14993 tree default_argument = cp_parser_type_id (parser);
14994 pop_deferring_access_checks ();
14995
14996 if (flag_concepts && type_uses_auto (default_argument))
14997 {
14998 error_at (token->location,
14999 "invalid use of %<auto%> in default template argument");
15000 return error_mark_node;
15001 }
15002
15003 return default_argument;
15004 }
15005
15006 /* Parse a default argument for a template template-parameter. */
15007
15008 static tree
15009 cp_parser_default_template_template_argument (cp_parser *parser)
15010 {
15011 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15012
15013 bool is_template;
15014
15015 /* Consume the `='. */
15016 cp_lexer_consume_token (parser->lexer);
15017 /* Parse the id-expression. */
15018 push_deferring_access_checks (dk_no_deferred);
15019 /* save token before parsing the id-expression, for error
15020 reporting */
15021 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15022 tree default_argument
15023 = cp_parser_id_expression (parser,
15024 /*template_keyword_p=*/false,
15025 /*check_dependency_p=*/true,
15026 /*template_p=*/&is_template,
15027 /*declarator_p=*/false,
15028 /*optional_p=*/false);
15029 if (TREE_CODE (default_argument) == TYPE_DECL)
15030 /* If the id-expression was a template-id that refers to
15031 a template-class, we already have the declaration here,
15032 so no further lookup is needed. */
15033 ;
15034 else
15035 /* Look up the name. */
15036 default_argument
15037 = cp_parser_lookup_name (parser, default_argument,
15038 none_type,
15039 /*is_template=*/is_template,
15040 /*is_namespace=*/false,
15041 /*check_dependency=*/true,
15042 /*ambiguous_decls=*/NULL,
15043 token->location);
15044 /* See if the default argument is valid. */
15045 default_argument = check_template_template_default_arg (default_argument);
15046 pop_deferring_access_checks ();
15047 return default_argument;
15048 }
15049
15050 /* Parse a template-parameter.
15051
15052 template-parameter:
15053 type-parameter
15054 parameter-declaration
15055
15056 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15057 the parameter. The TREE_PURPOSE is the default value, if any.
15058 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15059 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15060 set to true iff this parameter is a parameter pack. */
15061
15062 static tree
15063 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15064 bool *is_parameter_pack)
15065 {
15066 cp_token *token;
15067 cp_parameter_declarator *parameter_declarator;
15068 tree parm;
15069
15070 /* Assume it is a type parameter or a template parameter. */
15071 *is_non_type = false;
15072 /* Assume it not a parameter pack. */
15073 *is_parameter_pack = false;
15074 /* Peek at the next token. */
15075 token = cp_lexer_peek_token (parser->lexer);
15076 /* If it is `class' or `template', we have a type-parameter. */
15077 if (token->keyword == RID_TEMPLATE)
15078 return cp_parser_type_parameter (parser, is_parameter_pack);
15079 /* If it is `class' or `typename' we do not know yet whether it is a
15080 type parameter or a non-type parameter. Consider:
15081
15082 template <typename T, typename T::X X> ...
15083
15084 or:
15085
15086 template <class C, class D*> ...
15087
15088 Here, the first parameter is a type parameter, and the second is
15089 a non-type parameter. We can tell by looking at the token after
15090 the identifier -- if it is a `,', `=', or `>' then we have a type
15091 parameter. */
15092 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15093 {
15094 /* Peek at the token after `class' or `typename'. */
15095 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15096 /* If it's an ellipsis, we have a template type parameter
15097 pack. */
15098 if (token->type == CPP_ELLIPSIS)
15099 return cp_parser_type_parameter (parser, is_parameter_pack);
15100 /* If it's an identifier, skip it. */
15101 if (token->type == CPP_NAME)
15102 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15103 /* Now, see if the token looks like the end of a template
15104 parameter. */
15105 if (token->type == CPP_COMMA
15106 || token->type == CPP_EQ
15107 || token->type == CPP_GREATER)
15108 return cp_parser_type_parameter (parser, is_parameter_pack);
15109 }
15110
15111 /* Otherwise, it is a non-type parameter or a constrained parameter.
15112
15113 [temp.param]
15114
15115 When parsing a default template-argument for a non-type
15116 template-parameter, the first non-nested `>' is taken as the end
15117 of the template parameter-list rather than a greater-than
15118 operator. */
15119 parameter_declarator
15120 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15121 /*parenthesized_p=*/NULL);
15122
15123 if (!parameter_declarator)
15124 return error_mark_node;
15125
15126 /* If the parameter declaration is marked as a parameter pack, set
15127 *IS_PARAMETER_PACK to notify the caller. */
15128 if (parameter_declarator->template_parameter_pack_p)
15129 *is_parameter_pack = true;
15130
15131 if (parameter_declarator->default_argument)
15132 {
15133 /* Can happen in some cases of erroneous input (c++/34892). */
15134 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15135 /* Consume the `...' for better error recovery. */
15136 cp_lexer_consume_token (parser->lexer);
15137 }
15138
15139 // The parameter may have been constrained.
15140 if (is_constrained_parameter (parameter_declarator))
15141 return finish_constrained_parameter (parser,
15142 parameter_declarator,
15143 is_non_type,
15144 is_parameter_pack);
15145
15146 // Now we're sure that the parameter is a non-type parameter.
15147 *is_non_type = true;
15148
15149 parm = grokdeclarator (parameter_declarator->declarator,
15150 &parameter_declarator->decl_specifiers,
15151 TPARM, /*initialized=*/0,
15152 /*attrlist=*/NULL);
15153 if (parm == error_mark_node)
15154 return error_mark_node;
15155
15156 return build_tree_list (parameter_declarator->default_argument, parm);
15157 }
15158
15159 /* Parse a type-parameter.
15160
15161 type-parameter:
15162 class identifier [opt]
15163 class identifier [opt] = type-id
15164 typename identifier [opt]
15165 typename identifier [opt] = type-id
15166 template < template-parameter-list > class identifier [opt]
15167 template < template-parameter-list > class identifier [opt]
15168 = id-expression
15169
15170 GNU Extension (variadic templates):
15171
15172 type-parameter:
15173 class ... identifier [opt]
15174 typename ... identifier [opt]
15175
15176 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15177 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15178 the declaration of the parameter.
15179
15180 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15181
15182 static tree
15183 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15184 {
15185 cp_token *token;
15186 tree parameter;
15187
15188 /* Look for a keyword to tell us what kind of parameter this is. */
15189 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15190 if (!token)
15191 return error_mark_node;
15192
15193 switch (token->keyword)
15194 {
15195 case RID_CLASS:
15196 case RID_TYPENAME:
15197 {
15198 tree identifier;
15199 tree default_argument;
15200
15201 /* If the next token is an ellipsis, we have a template
15202 argument pack. */
15203 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15204 {
15205 /* Consume the `...' token. */
15206 cp_lexer_consume_token (parser->lexer);
15207 maybe_warn_variadic_templates ();
15208
15209 *is_parameter_pack = true;
15210 }
15211
15212 /* If the next token is an identifier, then it names the
15213 parameter. */
15214 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15215 identifier = cp_parser_identifier (parser);
15216 else
15217 identifier = NULL_TREE;
15218
15219 /* Create the parameter. */
15220 parameter = finish_template_type_parm (class_type_node, identifier);
15221
15222 /* If the next token is an `=', we have a default argument. */
15223 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15224 {
15225 default_argument
15226 = cp_parser_default_type_template_argument (parser);
15227
15228 /* Template parameter packs cannot have default
15229 arguments. */
15230 if (*is_parameter_pack)
15231 {
15232 if (identifier)
15233 error_at (token->location,
15234 "template parameter pack %qD cannot have a "
15235 "default argument", identifier);
15236 else
15237 error_at (token->location,
15238 "template parameter packs cannot have "
15239 "default arguments");
15240 default_argument = NULL_TREE;
15241 }
15242 else if (check_for_bare_parameter_packs (default_argument))
15243 default_argument = error_mark_node;
15244 }
15245 else
15246 default_argument = NULL_TREE;
15247
15248 /* Create the combined representation of the parameter and the
15249 default argument. */
15250 parameter = build_tree_list (default_argument, parameter);
15251 }
15252 break;
15253
15254 case RID_TEMPLATE:
15255 {
15256 tree identifier;
15257 tree default_argument;
15258
15259 /* Look for the `<'. */
15260 cp_parser_require (parser, CPP_LESS, RT_LESS);
15261 /* Parse the template-parameter-list. */
15262 cp_parser_template_parameter_list (parser);
15263 /* Look for the `>'. */
15264 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15265
15266 // If template requirements are present, parse them.
15267 if (flag_concepts)
15268 {
15269 tree reqs = get_shorthand_constraints (current_template_parms);
15270 if (tree r = cp_parser_requires_clause_opt (parser))
15271 reqs = conjoin_constraints (reqs, normalize_expression (r));
15272 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15273 }
15274
15275 /* Look for the `class' or 'typename' keywords. */
15276 cp_parser_type_parameter_key (parser);
15277 /* If the next token is an ellipsis, we have a template
15278 argument pack. */
15279 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15280 {
15281 /* Consume the `...' token. */
15282 cp_lexer_consume_token (parser->lexer);
15283 maybe_warn_variadic_templates ();
15284
15285 *is_parameter_pack = true;
15286 }
15287 /* If the next token is an `=', then there is a
15288 default-argument. If the next token is a `>', we are at
15289 the end of the parameter-list. If the next token is a `,',
15290 then we are at the end of this parameter. */
15291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15292 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15293 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15294 {
15295 identifier = cp_parser_identifier (parser);
15296 /* Treat invalid names as if the parameter were nameless. */
15297 if (identifier == error_mark_node)
15298 identifier = NULL_TREE;
15299 }
15300 else
15301 identifier = NULL_TREE;
15302
15303 /* Create the template parameter. */
15304 parameter = finish_template_template_parm (class_type_node,
15305 identifier);
15306
15307 /* If the next token is an `=', then there is a
15308 default-argument. */
15309 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15310 {
15311 default_argument
15312 = cp_parser_default_template_template_argument (parser);
15313
15314 /* Template parameter packs cannot have default
15315 arguments. */
15316 if (*is_parameter_pack)
15317 {
15318 if (identifier)
15319 error_at (token->location,
15320 "template parameter pack %qD cannot "
15321 "have a default argument",
15322 identifier);
15323 else
15324 error_at (token->location, "template parameter packs cannot "
15325 "have default arguments");
15326 default_argument = NULL_TREE;
15327 }
15328 }
15329 else
15330 default_argument = NULL_TREE;
15331
15332 /* Create the combined representation of the parameter and the
15333 default argument. */
15334 parameter = build_tree_list (default_argument, parameter);
15335 }
15336 break;
15337
15338 default:
15339 gcc_unreachable ();
15340 break;
15341 }
15342
15343 return parameter;
15344 }
15345
15346 /* Parse a template-id.
15347
15348 template-id:
15349 template-name < template-argument-list [opt] >
15350
15351 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15352 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15353 returned. Otherwise, if the template-name names a function, or set
15354 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15355 names a class, returns a TYPE_DECL for the specialization.
15356
15357 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15358 uninstantiated templates. */
15359
15360 static tree
15361 cp_parser_template_id (cp_parser *parser,
15362 bool template_keyword_p,
15363 bool check_dependency_p,
15364 enum tag_types tag_type,
15365 bool is_declaration)
15366 {
15367 tree templ;
15368 tree arguments;
15369 tree template_id;
15370 cp_token_position start_of_id = 0;
15371 cp_token *next_token = NULL, *next_token_2 = NULL;
15372 bool is_identifier;
15373
15374 /* If the next token corresponds to a template-id, there is no need
15375 to reparse it. */
15376 next_token = cp_lexer_peek_token (parser->lexer);
15377 if (next_token->type == CPP_TEMPLATE_ID)
15378 {
15379 cp_lexer_consume_token (parser->lexer);
15380 return saved_checks_value (next_token->u.tree_check_value);
15381 }
15382
15383 /* Avoid performing name lookup if there is no possibility of
15384 finding a template-id. */
15385 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15386 || (next_token->type == CPP_NAME
15387 && !cp_parser_nth_token_starts_template_argument_list_p
15388 (parser, 2)))
15389 {
15390 cp_parser_error (parser, "expected template-id");
15391 return error_mark_node;
15392 }
15393
15394 /* Remember where the template-id starts. */
15395 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15396 start_of_id = cp_lexer_token_position (parser->lexer, false);
15397
15398 push_deferring_access_checks (dk_deferred);
15399
15400 /* Parse the template-name. */
15401 is_identifier = false;
15402 templ = cp_parser_template_name (parser, template_keyword_p,
15403 check_dependency_p,
15404 is_declaration,
15405 tag_type,
15406 &is_identifier);
15407 if (templ == error_mark_node || is_identifier)
15408 {
15409 pop_deferring_access_checks ();
15410 return templ;
15411 }
15412
15413 /* Since we're going to preserve any side-effects from this parse, set up a
15414 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15415 in the template arguments. */
15416 tentative_firewall firewall (parser);
15417
15418 /* If we find the sequence `[:' after a template-name, it's probably
15419 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15420 parse correctly the argument list. */
15421 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15422 == CPP_OPEN_SQUARE)
15423 && next_token->flags & DIGRAPH
15424 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15425 == CPP_COLON)
15426 && !(next_token_2->flags & PREV_WHITE))
15427 {
15428 cp_parser_parse_tentatively (parser);
15429 /* Change `:' into `::'. */
15430 next_token_2->type = CPP_SCOPE;
15431 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15432 CPP_LESS. */
15433 cp_lexer_consume_token (parser->lexer);
15434
15435 /* Parse the arguments. */
15436 arguments = cp_parser_enclosed_template_argument_list (parser);
15437 if (!cp_parser_parse_definitely (parser))
15438 {
15439 /* If we couldn't parse an argument list, then we revert our changes
15440 and return simply an error. Maybe this is not a template-id
15441 after all. */
15442 next_token_2->type = CPP_COLON;
15443 cp_parser_error (parser, "expected %<<%>");
15444 pop_deferring_access_checks ();
15445 return error_mark_node;
15446 }
15447 /* Otherwise, emit an error about the invalid digraph, but continue
15448 parsing because we got our argument list. */
15449 if (permerror (next_token->location,
15450 "%<<::%> cannot begin a template-argument list"))
15451 {
15452 static bool hint = false;
15453 inform (next_token->location,
15454 "%<<:%> is an alternate spelling for %<[%>."
15455 " Insert whitespace between %<<%> and %<::%>");
15456 if (!hint && !flag_permissive)
15457 {
15458 inform (next_token->location, "(if you use %<-fpermissive%> "
15459 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15460 "accept your code)");
15461 hint = true;
15462 }
15463 }
15464 }
15465 else
15466 {
15467 /* Look for the `<' that starts the template-argument-list. */
15468 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15469 {
15470 pop_deferring_access_checks ();
15471 return error_mark_node;
15472 }
15473 /* Parse the arguments. */
15474 arguments = cp_parser_enclosed_template_argument_list (parser);
15475 }
15476
15477 /* Build a representation of the specialization. */
15478 if (identifier_p (templ))
15479 template_id = build_min_nt_loc (next_token->location,
15480 TEMPLATE_ID_EXPR,
15481 templ, arguments);
15482 else if (DECL_TYPE_TEMPLATE_P (templ)
15483 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15484 {
15485 bool entering_scope;
15486 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15487 template (rather than some instantiation thereof) only if
15488 is not nested within some other construct. For example, in
15489 "template <typename T> void f(T) { A<T>::", A<T> is just an
15490 instantiation of A. */
15491 entering_scope = (template_parm_scope_p ()
15492 && cp_lexer_next_token_is (parser->lexer,
15493 CPP_SCOPE));
15494 template_id
15495 = finish_template_type (templ, arguments, entering_scope);
15496 }
15497 /* A template-like identifier may be a partial concept id. */
15498 else if (flag_concepts
15499 && (template_id = (cp_parser_maybe_partial_concept_id
15500 (parser, templ, arguments))))
15501 return template_id;
15502 else if (variable_template_p (templ))
15503 {
15504 template_id = lookup_template_variable (templ, arguments);
15505 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15506 SET_EXPR_LOCATION (template_id, next_token->location);
15507 }
15508 else
15509 {
15510 /* If it's not a class-template or a template-template, it should be
15511 a function-template. */
15512 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15513 || TREE_CODE (templ) == OVERLOAD
15514 || BASELINK_P (templ)));
15515
15516 template_id = lookup_template_function (templ, arguments);
15517 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15518 SET_EXPR_LOCATION (template_id, next_token->location);
15519 }
15520
15521 /* If parsing tentatively, replace the sequence of tokens that makes
15522 up the template-id with a CPP_TEMPLATE_ID token. That way,
15523 should we re-parse the token stream, we will not have to repeat
15524 the effort required to do the parse, nor will we issue duplicate
15525 error messages about problems during instantiation of the
15526 template. */
15527 if (start_of_id
15528 /* Don't do this if we had a parse error in a declarator; re-parsing
15529 might succeed if a name changes meaning (60361). */
15530 && !(cp_parser_error_occurred (parser)
15531 && cp_parser_parsing_tentatively (parser)
15532 && parser->in_declarator_p))
15533 {
15534 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15535
15536 /* Reset the contents of the START_OF_ID token. */
15537 token->type = CPP_TEMPLATE_ID;
15538
15539 /* Update the location to be of the form:
15540 template-name < template-argument-list [opt] >
15541 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15542 with caret == start at the start of the template-name,
15543 ranging until the closing '>'. */
15544 location_t finish_loc
15545 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15546 location_t combined_loc
15547 = make_location (token->location, token->location, finish_loc);
15548 token->location = combined_loc;
15549
15550 /* Retrieve any deferred checks. Do not pop this access checks yet
15551 so the memory will not be reclaimed during token replacing below. */
15552 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15553 token->u.tree_check_value->value = template_id;
15554 token->u.tree_check_value->checks = get_deferred_access_checks ();
15555 token->keyword = RID_MAX;
15556
15557 /* Purge all subsequent tokens. */
15558 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15559
15560 /* ??? Can we actually assume that, if template_id ==
15561 error_mark_node, we will have issued a diagnostic to the
15562 user, as opposed to simply marking the tentative parse as
15563 failed? */
15564 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15565 error_at (token->location, "parse error in template argument list");
15566 }
15567
15568 pop_to_parent_deferring_access_checks ();
15569 return template_id;
15570 }
15571
15572 /* Parse a template-name.
15573
15574 template-name:
15575 identifier
15576
15577 The standard should actually say:
15578
15579 template-name:
15580 identifier
15581 operator-function-id
15582
15583 A defect report has been filed about this issue.
15584
15585 A conversion-function-id cannot be a template name because they cannot
15586 be part of a template-id. In fact, looking at this code:
15587
15588 a.operator K<int>()
15589
15590 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15591 It is impossible to call a templated conversion-function-id with an
15592 explicit argument list, since the only allowed template parameter is
15593 the type to which it is converting.
15594
15595 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15596 `template' keyword, in a construction like:
15597
15598 T::template f<3>()
15599
15600 In that case `f' is taken to be a template-name, even though there
15601 is no way of knowing for sure.
15602
15603 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15604 name refers to a set of overloaded functions, at least one of which
15605 is a template, or an IDENTIFIER_NODE with the name of the template,
15606 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15607 names are looked up inside uninstantiated templates. */
15608
15609 static tree
15610 cp_parser_template_name (cp_parser* parser,
15611 bool template_keyword_p,
15612 bool check_dependency_p,
15613 bool is_declaration,
15614 enum tag_types tag_type,
15615 bool *is_identifier)
15616 {
15617 tree identifier;
15618 tree decl;
15619 tree fns;
15620 cp_token *token = cp_lexer_peek_token (parser->lexer);
15621
15622 /* If the next token is `operator', then we have either an
15623 operator-function-id or a conversion-function-id. */
15624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15625 {
15626 /* We don't know whether we're looking at an
15627 operator-function-id or a conversion-function-id. */
15628 cp_parser_parse_tentatively (parser);
15629 /* Try an operator-function-id. */
15630 identifier = cp_parser_operator_function_id (parser);
15631 /* If that didn't work, try a conversion-function-id. */
15632 if (!cp_parser_parse_definitely (parser))
15633 {
15634 cp_parser_error (parser, "expected template-name");
15635 return error_mark_node;
15636 }
15637 }
15638 /* Look for the identifier. */
15639 else
15640 identifier = cp_parser_identifier (parser);
15641
15642 /* If we didn't find an identifier, we don't have a template-id. */
15643 if (identifier == error_mark_node)
15644 return error_mark_node;
15645
15646 /* If the name immediately followed the `template' keyword, then it
15647 is a template-name. However, if the next token is not `<', then
15648 we do not treat it as a template-name, since it is not being used
15649 as part of a template-id. This enables us to handle constructs
15650 like:
15651
15652 template <typename T> struct S { S(); };
15653 template <typename T> S<T>::S();
15654
15655 correctly. We would treat `S' as a template -- if it were `S<T>'
15656 -- but we do not if there is no `<'. */
15657
15658 if (processing_template_decl
15659 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15660 {
15661 /* In a declaration, in a dependent context, we pretend that the
15662 "template" keyword was present in order to improve error
15663 recovery. For example, given:
15664
15665 template <typename T> void f(T::X<int>);
15666
15667 we want to treat "X<int>" as a template-id. */
15668 if (is_declaration
15669 && !template_keyword_p
15670 && parser->scope && TYPE_P (parser->scope)
15671 && check_dependency_p
15672 && dependent_scope_p (parser->scope)
15673 /* Do not do this for dtors (or ctors), since they never
15674 need the template keyword before their name. */
15675 && !constructor_name_p (identifier, parser->scope))
15676 {
15677 cp_token_position start = 0;
15678
15679 /* Explain what went wrong. */
15680 error_at (token->location, "non-template %qD used as template",
15681 identifier);
15682 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15683 parser->scope, identifier);
15684 /* If parsing tentatively, find the location of the "<" token. */
15685 if (cp_parser_simulate_error (parser))
15686 start = cp_lexer_token_position (parser->lexer, true);
15687 /* Parse the template arguments so that we can issue error
15688 messages about them. */
15689 cp_lexer_consume_token (parser->lexer);
15690 cp_parser_enclosed_template_argument_list (parser);
15691 /* Skip tokens until we find a good place from which to
15692 continue parsing. */
15693 cp_parser_skip_to_closing_parenthesis (parser,
15694 /*recovering=*/true,
15695 /*or_comma=*/true,
15696 /*consume_paren=*/false);
15697 /* If parsing tentatively, permanently remove the
15698 template argument list. That will prevent duplicate
15699 error messages from being issued about the missing
15700 "template" keyword. */
15701 if (start)
15702 cp_lexer_purge_tokens_after (parser->lexer, start);
15703 if (is_identifier)
15704 *is_identifier = true;
15705 parser->context->object_type = NULL_TREE;
15706 return identifier;
15707 }
15708
15709 /* If the "template" keyword is present, then there is generally
15710 no point in doing name-lookup, so we just return IDENTIFIER.
15711 But, if the qualifying scope is non-dependent then we can
15712 (and must) do name-lookup normally. */
15713 if (template_keyword_p
15714 && (!parser->scope
15715 || (TYPE_P (parser->scope)
15716 && dependent_type_p (parser->scope))))
15717 {
15718 /* We're optimizing away the call to cp_parser_lookup_name, but we
15719 still need to do this. */
15720 parser->context->object_type = NULL_TREE;
15721 return identifier;
15722 }
15723 }
15724
15725 /* Look up the name. */
15726 decl = cp_parser_lookup_name (parser, identifier,
15727 tag_type,
15728 /*is_template=*/true,
15729 /*is_namespace=*/false,
15730 check_dependency_p,
15731 /*ambiguous_decls=*/NULL,
15732 token->location);
15733
15734 decl = strip_using_decl (decl);
15735
15736 /* If DECL is a template, then the name was a template-name. */
15737 if (TREE_CODE (decl) == TEMPLATE_DECL)
15738 {
15739 if (TREE_DEPRECATED (decl)
15740 && deprecated_state != DEPRECATED_SUPPRESS)
15741 warn_deprecated_use (decl, NULL_TREE);
15742 }
15743 else
15744 {
15745 tree fn = NULL_TREE;
15746
15747 /* The standard does not explicitly indicate whether a name that
15748 names a set of overloaded declarations, some of which are
15749 templates, is a template-name. However, such a name should
15750 be a template-name; otherwise, there is no way to form a
15751 template-id for the overloaded templates. */
15752 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15753 if (TREE_CODE (fns) == OVERLOAD)
15754 for (fn = fns; fn; fn = OVL_NEXT (fn))
15755 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15756 break;
15757
15758 if (!fn)
15759 {
15760 /* The name does not name a template. */
15761 cp_parser_error (parser, "expected template-name");
15762 return error_mark_node;
15763 }
15764 }
15765
15766 /* If DECL is dependent, and refers to a function, then just return
15767 its name; we will look it up again during template instantiation. */
15768 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15769 {
15770 tree scope = ovl_scope (decl);
15771 if (TYPE_P (scope) && dependent_type_p (scope))
15772 return identifier;
15773 }
15774
15775 return decl;
15776 }
15777
15778 /* Parse a template-argument-list.
15779
15780 template-argument-list:
15781 template-argument ... [opt]
15782 template-argument-list , template-argument ... [opt]
15783
15784 Returns a TREE_VEC containing the arguments. */
15785
15786 static tree
15787 cp_parser_template_argument_list (cp_parser* parser)
15788 {
15789 tree fixed_args[10];
15790 unsigned n_args = 0;
15791 unsigned alloced = 10;
15792 tree *arg_ary = fixed_args;
15793 tree vec;
15794 bool saved_in_template_argument_list_p;
15795 bool saved_ice_p;
15796 bool saved_non_ice_p;
15797
15798 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15799 parser->in_template_argument_list_p = true;
15800 /* Even if the template-id appears in an integral
15801 constant-expression, the contents of the argument list do
15802 not. */
15803 saved_ice_p = parser->integral_constant_expression_p;
15804 parser->integral_constant_expression_p = false;
15805 saved_non_ice_p = parser->non_integral_constant_expression_p;
15806 parser->non_integral_constant_expression_p = false;
15807
15808 /* Parse the arguments. */
15809 do
15810 {
15811 tree argument;
15812
15813 if (n_args)
15814 /* Consume the comma. */
15815 cp_lexer_consume_token (parser->lexer);
15816
15817 /* Parse the template-argument. */
15818 argument = cp_parser_template_argument (parser);
15819
15820 /* If the next token is an ellipsis, we're expanding a template
15821 argument pack. */
15822 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15823 {
15824 if (argument == error_mark_node)
15825 {
15826 cp_token *token = cp_lexer_peek_token (parser->lexer);
15827 error_at (token->location,
15828 "expected parameter pack before %<...%>");
15829 }
15830 /* Consume the `...' token. */
15831 cp_lexer_consume_token (parser->lexer);
15832
15833 /* Make the argument into a TYPE_PACK_EXPANSION or
15834 EXPR_PACK_EXPANSION. */
15835 argument = make_pack_expansion (argument);
15836 }
15837
15838 if (n_args == alloced)
15839 {
15840 alloced *= 2;
15841
15842 if (arg_ary == fixed_args)
15843 {
15844 arg_ary = XNEWVEC (tree, alloced);
15845 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15846 }
15847 else
15848 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15849 }
15850 arg_ary[n_args++] = argument;
15851 }
15852 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15853
15854 vec = make_tree_vec (n_args);
15855
15856 while (n_args--)
15857 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15858
15859 if (arg_ary != fixed_args)
15860 free (arg_ary);
15861 parser->non_integral_constant_expression_p = saved_non_ice_p;
15862 parser->integral_constant_expression_p = saved_ice_p;
15863 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15864 if (CHECKING_P)
15865 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15866 return vec;
15867 }
15868
15869 /* Parse a template-argument.
15870
15871 template-argument:
15872 assignment-expression
15873 type-id
15874 id-expression
15875
15876 The representation is that of an assignment-expression, type-id, or
15877 id-expression -- except that the qualified id-expression is
15878 evaluated, so that the value returned is either a DECL or an
15879 OVERLOAD.
15880
15881 Although the standard says "assignment-expression", it forbids
15882 throw-expressions or assignments in the template argument.
15883 Therefore, we use "conditional-expression" instead. */
15884
15885 static tree
15886 cp_parser_template_argument (cp_parser* parser)
15887 {
15888 tree argument;
15889 bool template_p;
15890 bool address_p;
15891 bool maybe_type_id = false;
15892 cp_token *token = NULL, *argument_start_token = NULL;
15893 location_t loc = 0;
15894 cp_id_kind idk;
15895
15896 /* There's really no way to know what we're looking at, so we just
15897 try each alternative in order.
15898
15899 [temp.arg]
15900
15901 In a template-argument, an ambiguity between a type-id and an
15902 expression is resolved to a type-id, regardless of the form of
15903 the corresponding template-parameter.
15904
15905 Therefore, we try a type-id first. */
15906 cp_parser_parse_tentatively (parser);
15907 argument = cp_parser_template_type_arg (parser);
15908 /* If there was no error parsing the type-id but the next token is a
15909 '>>', our behavior depends on which dialect of C++ we're
15910 parsing. In C++98, we probably found a typo for '> >'. But there
15911 are type-id which are also valid expressions. For instance:
15912
15913 struct X { int operator >> (int); };
15914 template <int V> struct Foo {};
15915 Foo<X () >> 5> r;
15916
15917 Here 'X()' is a valid type-id of a function type, but the user just
15918 wanted to write the expression "X() >> 5". Thus, we remember that we
15919 found a valid type-id, but we still try to parse the argument as an
15920 expression to see what happens.
15921
15922 In C++0x, the '>>' will be considered two separate '>'
15923 tokens. */
15924 if (!cp_parser_error_occurred (parser)
15925 && cxx_dialect == cxx98
15926 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15927 {
15928 maybe_type_id = true;
15929 cp_parser_abort_tentative_parse (parser);
15930 }
15931 else
15932 {
15933 /* If the next token isn't a `,' or a `>', then this argument wasn't
15934 really finished. This means that the argument is not a valid
15935 type-id. */
15936 if (!cp_parser_next_token_ends_template_argument_p (parser))
15937 cp_parser_error (parser, "expected template-argument");
15938 /* If that worked, we're done. */
15939 if (cp_parser_parse_definitely (parser))
15940 return argument;
15941 }
15942 /* We're still not sure what the argument will be. */
15943 cp_parser_parse_tentatively (parser);
15944 /* Try a template. */
15945 argument_start_token = cp_lexer_peek_token (parser->lexer);
15946 argument = cp_parser_id_expression (parser,
15947 /*template_keyword_p=*/false,
15948 /*check_dependency_p=*/true,
15949 &template_p,
15950 /*declarator_p=*/false,
15951 /*optional_p=*/false);
15952 /* If the next token isn't a `,' or a `>', then this argument wasn't
15953 really finished. */
15954 if (!cp_parser_next_token_ends_template_argument_p (parser))
15955 cp_parser_error (parser, "expected template-argument");
15956 if (!cp_parser_error_occurred (parser))
15957 {
15958 /* Figure out what is being referred to. If the id-expression
15959 was for a class template specialization, then we will have a
15960 TYPE_DECL at this point. There is no need to do name lookup
15961 at this point in that case. */
15962 if (TREE_CODE (argument) != TYPE_DECL)
15963 argument = cp_parser_lookup_name (parser, argument,
15964 none_type,
15965 /*is_template=*/template_p,
15966 /*is_namespace=*/false,
15967 /*check_dependency=*/true,
15968 /*ambiguous_decls=*/NULL,
15969 argument_start_token->location);
15970 /* Handle a constrained-type-specifier for a non-type template
15971 parameter. */
15972 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15973 argument = decl;
15974 else if (TREE_CODE (argument) != TEMPLATE_DECL
15975 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15976 cp_parser_error (parser, "expected template-name");
15977 }
15978 if (cp_parser_parse_definitely (parser))
15979 {
15980 if (TREE_DEPRECATED (argument))
15981 warn_deprecated_use (argument, NULL_TREE);
15982 return argument;
15983 }
15984 /* It must be a non-type argument. In C++17 any constant-expression is
15985 allowed. */
15986 if (cxx_dialect > cxx14)
15987 goto general_expr;
15988
15989 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15990
15991 -- an integral constant-expression of integral or enumeration
15992 type; or
15993
15994 -- the name of a non-type template-parameter; or
15995
15996 -- the name of an object or function with external linkage...
15997
15998 -- the address of an object or function with external linkage...
15999
16000 -- a pointer to member... */
16001 /* Look for a non-type template parameter. */
16002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16003 {
16004 cp_parser_parse_tentatively (parser);
16005 argument = cp_parser_primary_expression (parser,
16006 /*address_p=*/false,
16007 /*cast_p=*/false,
16008 /*template_arg_p=*/true,
16009 &idk);
16010 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16011 || !cp_parser_next_token_ends_template_argument_p (parser))
16012 cp_parser_simulate_error (parser);
16013 if (cp_parser_parse_definitely (parser))
16014 return argument;
16015 }
16016
16017 /* If the next token is "&", the argument must be the address of an
16018 object or function with external linkage. */
16019 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16020 if (address_p)
16021 {
16022 loc = cp_lexer_peek_token (parser->lexer)->location;
16023 cp_lexer_consume_token (parser->lexer);
16024 }
16025 /* See if we might have an id-expression. */
16026 token = cp_lexer_peek_token (parser->lexer);
16027 if (token->type == CPP_NAME
16028 || token->keyword == RID_OPERATOR
16029 || token->type == CPP_SCOPE
16030 || token->type == CPP_TEMPLATE_ID
16031 || token->type == CPP_NESTED_NAME_SPECIFIER)
16032 {
16033 cp_parser_parse_tentatively (parser);
16034 argument = cp_parser_primary_expression (parser,
16035 address_p,
16036 /*cast_p=*/false,
16037 /*template_arg_p=*/true,
16038 &idk);
16039 if (cp_parser_error_occurred (parser)
16040 || !cp_parser_next_token_ends_template_argument_p (parser))
16041 cp_parser_abort_tentative_parse (parser);
16042 else
16043 {
16044 tree probe;
16045
16046 if (INDIRECT_REF_P (argument))
16047 {
16048 /* Strip the dereference temporarily. */
16049 gcc_assert (REFERENCE_REF_P (argument));
16050 argument = TREE_OPERAND (argument, 0);
16051 }
16052
16053 /* If we're in a template, we represent a qualified-id referring
16054 to a static data member as a SCOPE_REF even if the scope isn't
16055 dependent so that we can check access control later. */
16056 probe = argument;
16057 if (TREE_CODE (probe) == SCOPE_REF)
16058 probe = TREE_OPERAND (probe, 1);
16059 if (VAR_P (probe))
16060 {
16061 /* A variable without external linkage might still be a
16062 valid constant-expression, so no error is issued here
16063 if the external-linkage check fails. */
16064 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16065 cp_parser_simulate_error (parser);
16066 }
16067 else if (is_overloaded_fn (argument))
16068 /* All overloaded functions are allowed; if the external
16069 linkage test does not pass, an error will be issued
16070 later. */
16071 ;
16072 else if (address_p
16073 && (TREE_CODE (argument) == OFFSET_REF
16074 || TREE_CODE (argument) == SCOPE_REF))
16075 /* A pointer-to-member. */
16076 ;
16077 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16078 ;
16079 else
16080 cp_parser_simulate_error (parser);
16081
16082 if (cp_parser_parse_definitely (parser))
16083 {
16084 if (address_p)
16085 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16086 tf_warning_or_error);
16087 else
16088 argument = convert_from_reference (argument);
16089 return argument;
16090 }
16091 }
16092 }
16093 /* If the argument started with "&", there are no other valid
16094 alternatives at this point. */
16095 if (address_p)
16096 {
16097 cp_parser_error (parser, "invalid non-type template argument");
16098 return error_mark_node;
16099 }
16100
16101 general_expr:
16102 /* If the argument wasn't successfully parsed as a type-id followed
16103 by '>>', the argument can only be a constant expression now.
16104 Otherwise, we try parsing the constant-expression tentatively,
16105 because the argument could really be a type-id. */
16106 if (maybe_type_id)
16107 cp_parser_parse_tentatively (parser);
16108
16109 if (cxx_dialect <= cxx14)
16110 argument = cp_parser_constant_expression (parser);
16111 else
16112 {
16113 /* With C++17 generalized non-type template arguments we need to handle
16114 lvalue constant expressions, too. */
16115 argument = cp_parser_assignment_expression (parser);
16116 require_potential_constant_expression (argument);
16117 }
16118
16119 if (!maybe_type_id)
16120 return argument;
16121 if (!cp_parser_next_token_ends_template_argument_p (parser))
16122 cp_parser_error (parser, "expected template-argument");
16123 if (cp_parser_parse_definitely (parser))
16124 return argument;
16125 /* We did our best to parse the argument as a non type-id, but that
16126 was the only alternative that matched (albeit with a '>' after
16127 it). We can assume it's just a typo from the user, and a
16128 diagnostic will then be issued. */
16129 return cp_parser_template_type_arg (parser);
16130 }
16131
16132 /* Parse an explicit-instantiation.
16133
16134 explicit-instantiation:
16135 template declaration
16136
16137 Although the standard says `declaration', what it really means is:
16138
16139 explicit-instantiation:
16140 template decl-specifier-seq [opt] declarator [opt] ;
16141
16142 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16143 supposed to be allowed. A defect report has been filed about this
16144 issue.
16145
16146 GNU Extension:
16147
16148 explicit-instantiation:
16149 storage-class-specifier template
16150 decl-specifier-seq [opt] declarator [opt] ;
16151 function-specifier template
16152 decl-specifier-seq [opt] declarator [opt] ; */
16153
16154 static void
16155 cp_parser_explicit_instantiation (cp_parser* parser)
16156 {
16157 int declares_class_or_enum;
16158 cp_decl_specifier_seq decl_specifiers;
16159 tree extension_specifier = NULL_TREE;
16160
16161 timevar_push (TV_TEMPLATE_INST);
16162
16163 /* Look for an (optional) storage-class-specifier or
16164 function-specifier. */
16165 if (cp_parser_allow_gnu_extensions_p (parser))
16166 {
16167 extension_specifier
16168 = cp_parser_storage_class_specifier_opt (parser);
16169 if (!extension_specifier)
16170 extension_specifier
16171 = cp_parser_function_specifier_opt (parser,
16172 /*decl_specs=*/NULL);
16173 }
16174
16175 /* Look for the `template' keyword. */
16176 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16177 /* Let the front end know that we are processing an explicit
16178 instantiation. */
16179 begin_explicit_instantiation ();
16180 /* [temp.explicit] says that we are supposed to ignore access
16181 control while processing explicit instantiation directives. */
16182 push_deferring_access_checks (dk_no_check);
16183 /* Parse a decl-specifier-seq. */
16184 cp_parser_decl_specifier_seq (parser,
16185 CP_PARSER_FLAGS_OPTIONAL,
16186 &decl_specifiers,
16187 &declares_class_or_enum);
16188 /* If there was exactly one decl-specifier, and it declared a class,
16189 and there's no declarator, then we have an explicit type
16190 instantiation. */
16191 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16192 {
16193 tree type;
16194
16195 type = check_tag_decl (&decl_specifiers,
16196 /*explicit_type_instantiation_p=*/true);
16197 /* Turn access control back on for names used during
16198 template instantiation. */
16199 pop_deferring_access_checks ();
16200 if (type)
16201 do_type_instantiation (type, extension_specifier,
16202 /*complain=*/tf_error);
16203 }
16204 else
16205 {
16206 cp_declarator *declarator;
16207 tree decl;
16208
16209 /* Parse the declarator. */
16210 declarator
16211 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16212 /*ctor_dtor_or_conv_p=*/NULL,
16213 /*parenthesized_p=*/NULL,
16214 /*member_p=*/false,
16215 /*friend_p=*/false);
16216 if (declares_class_or_enum & 2)
16217 cp_parser_check_for_definition_in_return_type (declarator,
16218 decl_specifiers.type,
16219 decl_specifiers.locations[ds_type_spec]);
16220 if (declarator != cp_error_declarator)
16221 {
16222 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16223 permerror (decl_specifiers.locations[ds_inline],
16224 "explicit instantiation shall not use"
16225 " %<inline%> specifier");
16226 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16227 permerror (decl_specifiers.locations[ds_constexpr],
16228 "explicit instantiation shall not use"
16229 " %<constexpr%> specifier");
16230
16231 decl = grokdeclarator (declarator, &decl_specifiers,
16232 NORMAL, 0, &decl_specifiers.attributes);
16233 /* Turn access control back on for names used during
16234 template instantiation. */
16235 pop_deferring_access_checks ();
16236 /* Do the explicit instantiation. */
16237 do_decl_instantiation (decl, extension_specifier);
16238 }
16239 else
16240 {
16241 pop_deferring_access_checks ();
16242 /* Skip the body of the explicit instantiation. */
16243 cp_parser_skip_to_end_of_statement (parser);
16244 }
16245 }
16246 /* We're done with the instantiation. */
16247 end_explicit_instantiation ();
16248
16249 cp_parser_consume_semicolon_at_end_of_statement (parser);
16250
16251 timevar_pop (TV_TEMPLATE_INST);
16252 }
16253
16254 /* Parse an explicit-specialization.
16255
16256 explicit-specialization:
16257 template < > declaration
16258
16259 Although the standard says `declaration', what it really means is:
16260
16261 explicit-specialization:
16262 template <> decl-specifier [opt] init-declarator [opt] ;
16263 template <> function-definition
16264 template <> explicit-specialization
16265 template <> template-declaration */
16266
16267 static void
16268 cp_parser_explicit_specialization (cp_parser* parser)
16269 {
16270 bool need_lang_pop;
16271 cp_token *token = cp_lexer_peek_token (parser->lexer);
16272
16273 /* Look for the `template' keyword. */
16274 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16275 /* Look for the `<'. */
16276 cp_parser_require (parser, CPP_LESS, RT_LESS);
16277 /* Look for the `>'. */
16278 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16279 /* We have processed another parameter list. */
16280 ++parser->num_template_parameter_lists;
16281 /* [temp]
16282
16283 A template ... explicit specialization ... shall not have C
16284 linkage. */
16285 if (current_lang_name == lang_name_c)
16286 {
16287 error_at (token->location, "template specialization with C linkage");
16288 /* Give it C++ linkage to avoid confusing other parts of the
16289 front end. */
16290 push_lang_context (lang_name_cplusplus);
16291 need_lang_pop = true;
16292 }
16293 else
16294 need_lang_pop = false;
16295 /* Let the front end know that we are beginning a specialization. */
16296 if (!begin_specialization ())
16297 {
16298 end_specialization ();
16299 return;
16300 }
16301
16302 /* If the next keyword is `template', we need to figure out whether
16303 or not we're looking a template-declaration. */
16304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16305 {
16306 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16307 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16308 cp_parser_template_declaration_after_export (parser,
16309 /*member_p=*/false);
16310 else
16311 cp_parser_explicit_specialization (parser);
16312 }
16313 else
16314 /* Parse the dependent declaration. */
16315 cp_parser_single_declaration (parser,
16316 /*checks=*/NULL,
16317 /*member_p=*/false,
16318 /*explicit_specialization_p=*/true,
16319 /*friend_p=*/NULL);
16320 /* We're done with the specialization. */
16321 end_specialization ();
16322 /* For the erroneous case of a template with C linkage, we pushed an
16323 implicit C++ linkage scope; exit that scope now. */
16324 if (need_lang_pop)
16325 pop_lang_context ();
16326 /* We're done with this parameter list. */
16327 --parser->num_template_parameter_lists;
16328 }
16329
16330 /* Parse a type-specifier.
16331
16332 type-specifier:
16333 simple-type-specifier
16334 class-specifier
16335 enum-specifier
16336 elaborated-type-specifier
16337 cv-qualifier
16338
16339 GNU Extension:
16340
16341 type-specifier:
16342 __complex__
16343
16344 Returns a representation of the type-specifier. For a
16345 class-specifier, enum-specifier, or elaborated-type-specifier, a
16346 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16347
16348 The parser flags FLAGS is used to control type-specifier parsing.
16349
16350 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16351 in a decl-specifier-seq.
16352
16353 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16354 class-specifier, enum-specifier, or elaborated-type-specifier, then
16355 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16356 if a type is declared; 2 if it is defined. Otherwise, it is set to
16357 zero.
16358
16359 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16360 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16361 is set to FALSE. */
16362
16363 static tree
16364 cp_parser_type_specifier (cp_parser* parser,
16365 cp_parser_flags flags,
16366 cp_decl_specifier_seq *decl_specs,
16367 bool is_declaration,
16368 int* declares_class_or_enum,
16369 bool* is_cv_qualifier)
16370 {
16371 tree type_spec = NULL_TREE;
16372 cp_token *token;
16373 enum rid keyword;
16374 cp_decl_spec ds = ds_last;
16375
16376 /* Assume this type-specifier does not declare a new type. */
16377 if (declares_class_or_enum)
16378 *declares_class_or_enum = 0;
16379 /* And that it does not specify a cv-qualifier. */
16380 if (is_cv_qualifier)
16381 *is_cv_qualifier = false;
16382 /* Peek at the next token. */
16383 token = cp_lexer_peek_token (parser->lexer);
16384
16385 /* If we're looking at a keyword, we can use that to guide the
16386 production we choose. */
16387 keyword = token->keyword;
16388 switch (keyword)
16389 {
16390 case RID_ENUM:
16391 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16392 goto elaborated_type_specifier;
16393
16394 /* Look for the enum-specifier. */
16395 type_spec = cp_parser_enum_specifier (parser);
16396 /* If that worked, we're done. */
16397 if (type_spec)
16398 {
16399 if (declares_class_or_enum)
16400 *declares_class_or_enum = 2;
16401 if (decl_specs)
16402 cp_parser_set_decl_spec_type (decl_specs,
16403 type_spec,
16404 token,
16405 /*type_definition_p=*/true);
16406 return type_spec;
16407 }
16408 else
16409 goto elaborated_type_specifier;
16410
16411 /* Any of these indicate either a class-specifier, or an
16412 elaborated-type-specifier. */
16413 case RID_CLASS:
16414 case RID_STRUCT:
16415 case RID_UNION:
16416 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16417 goto elaborated_type_specifier;
16418
16419 /* Parse tentatively so that we can back up if we don't find a
16420 class-specifier. */
16421 cp_parser_parse_tentatively (parser);
16422 /* Look for the class-specifier. */
16423 type_spec = cp_parser_class_specifier (parser);
16424 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16425 /* If that worked, we're done. */
16426 if (cp_parser_parse_definitely (parser))
16427 {
16428 if (declares_class_or_enum)
16429 *declares_class_or_enum = 2;
16430 if (decl_specs)
16431 cp_parser_set_decl_spec_type (decl_specs,
16432 type_spec,
16433 token,
16434 /*type_definition_p=*/true);
16435 return type_spec;
16436 }
16437
16438 /* Fall through. */
16439 elaborated_type_specifier:
16440 /* We're declaring (not defining) a class or enum. */
16441 if (declares_class_or_enum)
16442 *declares_class_or_enum = 1;
16443
16444 /* Fall through. */
16445 case RID_TYPENAME:
16446 /* Look for an elaborated-type-specifier. */
16447 type_spec
16448 = (cp_parser_elaborated_type_specifier
16449 (parser,
16450 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16451 is_declaration));
16452 if (decl_specs)
16453 cp_parser_set_decl_spec_type (decl_specs,
16454 type_spec,
16455 token,
16456 /*type_definition_p=*/false);
16457 return type_spec;
16458
16459 case RID_CONST:
16460 ds = ds_const;
16461 if (is_cv_qualifier)
16462 *is_cv_qualifier = true;
16463 break;
16464
16465 case RID_VOLATILE:
16466 ds = ds_volatile;
16467 if (is_cv_qualifier)
16468 *is_cv_qualifier = true;
16469 break;
16470
16471 case RID_RESTRICT:
16472 ds = ds_restrict;
16473 if (is_cv_qualifier)
16474 *is_cv_qualifier = true;
16475 break;
16476
16477 case RID_COMPLEX:
16478 /* The `__complex__' keyword is a GNU extension. */
16479 ds = ds_complex;
16480 break;
16481
16482 default:
16483 break;
16484 }
16485
16486 /* Handle simple keywords. */
16487 if (ds != ds_last)
16488 {
16489 if (decl_specs)
16490 {
16491 set_and_check_decl_spec_loc (decl_specs, ds, token);
16492 decl_specs->any_specifiers_p = true;
16493 }
16494 return cp_lexer_consume_token (parser->lexer)->u.value;
16495 }
16496
16497 /* If we do not already have a type-specifier, assume we are looking
16498 at a simple-type-specifier. */
16499 type_spec = cp_parser_simple_type_specifier (parser,
16500 decl_specs,
16501 flags);
16502
16503 /* If we didn't find a type-specifier, and a type-specifier was not
16504 optional in this context, issue an error message. */
16505 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16506 {
16507 cp_parser_error (parser, "expected type specifier");
16508 return error_mark_node;
16509 }
16510
16511 return type_spec;
16512 }
16513
16514 /* Parse a simple-type-specifier.
16515
16516 simple-type-specifier:
16517 :: [opt] nested-name-specifier [opt] type-name
16518 :: [opt] nested-name-specifier template template-id
16519 char
16520 wchar_t
16521 bool
16522 short
16523 int
16524 long
16525 signed
16526 unsigned
16527 float
16528 double
16529 void
16530
16531 C++11 Extension:
16532
16533 simple-type-specifier:
16534 auto
16535 decltype ( expression )
16536 char16_t
16537 char32_t
16538 __underlying_type ( type-id )
16539
16540 C++17 extension:
16541
16542 nested-name-specifier(opt) template-name
16543
16544 GNU Extension:
16545
16546 simple-type-specifier:
16547 __int128
16548 __typeof__ unary-expression
16549 __typeof__ ( type-id )
16550 __typeof__ ( type-id ) { initializer-list , [opt] }
16551
16552 Concepts Extension:
16553
16554 simple-type-specifier:
16555 constrained-type-specifier
16556
16557 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16558 appropriately updated. */
16559
16560 static tree
16561 cp_parser_simple_type_specifier (cp_parser* parser,
16562 cp_decl_specifier_seq *decl_specs,
16563 cp_parser_flags flags)
16564 {
16565 tree type = NULL_TREE;
16566 cp_token *token;
16567 int idx;
16568
16569 /* Peek at the next token. */
16570 token = cp_lexer_peek_token (parser->lexer);
16571
16572 /* If we're looking at a keyword, things are easy. */
16573 switch (token->keyword)
16574 {
16575 case RID_CHAR:
16576 if (decl_specs)
16577 decl_specs->explicit_char_p = true;
16578 type = char_type_node;
16579 break;
16580 case RID_CHAR16:
16581 type = char16_type_node;
16582 break;
16583 case RID_CHAR32:
16584 type = char32_type_node;
16585 break;
16586 case RID_WCHAR:
16587 type = wchar_type_node;
16588 break;
16589 case RID_BOOL:
16590 type = boolean_type_node;
16591 break;
16592 case RID_SHORT:
16593 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16594 type = short_integer_type_node;
16595 break;
16596 case RID_INT:
16597 if (decl_specs)
16598 decl_specs->explicit_int_p = true;
16599 type = integer_type_node;
16600 break;
16601 case RID_INT_N_0:
16602 case RID_INT_N_1:
16603 case RID_INT_N_2:
16604 case RID_INT_N_3:
16605 idx = token->keyword - RID_INT_N_0;
16606 if (! int_n_enabled_p [idx])
16607 break;
16608 if (decl_specs)
16609 {
16610 decl_specs->explicit_intN_p = true;
16611 decl_specs->int_n_idx = idx;
16612 }
16613 type = int_n_trees [idx].signed_type;
16614 break;
16615 case RID_LONG:
16616 if (decl_specs)
16617 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16618 type = long_integer_type_node;
16619 break;
16620 case RID_SIGNED:
16621 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16622 type = integer_type_node;
16623 break;
16624 case RID_UNSIGNED:
16625 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16626 type = unsigned_type_node;
16627 break;
16628 case RID_FLOAT:
16629 type = float_type_node;
16630 break;
16631 case RID_DOUBLE:
16632 type = double_type_node;
16633 break;
16634 case RID_VOID:
16635 type = void_type_node;
16636 break;
16637
16638 case RID_AUTO:
16639 maybe_warn_cpp0x (CPP0X_AUTO);
16640 if (parser->auto_is_implicit_function_template_parm_p)
16641 {
16642 /* The 'auto' might be the placeholder return type for a function decl
16643 with trailing return type. */
16644 bool have_trailing_return_fn_decl = false;
16645
16646 cp_parser_parse_tentatively (parser);
16647 cp_lexer_consume_token (parser->lexer);
16648 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16649 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16650 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16651 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16652 {
16653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16654 {
16655 cp_lexer_consume_token (parser->lexer);
16656 cp_parser_skip_to_closing_parenthesis (parser,
16657 /*recovering*/false,
16658 /*or_comma*/false,
16659 /*consume_paren*/true);
16660 continue;
16661 }
16662
16663 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16664 {
16665 have_trailing_return_fn_decl = true;
16666 break;
16667 }
16668
16669 cp_lexer_consume_token (parser->lexer);
16670 }
16671 cp_parser_abort_tentative_parse (parser);
16672
16673 if (have_trailing_return_fn_decl)
16674 {
16675 type = make_auto ();
16676 break;
16677 }
16678
16679 if (cxx_dialect >= cxx14)
16680 {
16681 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16682 type = TREE_TYPE (type);
16683 }
16684 else
16685 type = error_mark_node;
16686
16687 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16688 {
16689 if (cxx_dialect < cxx14)
16690 error_at (token->location,
16691 "use of %<auto%> in lambda parameter declaration "
16692 "only available with "
16693 "-std=c++14 or -std=gnu++14");
16694 }
16695 else if (cxx_dialect < cxx14)
16696 error_at (token->location,
16697 "use of %<auto%> in parameter declaration "
16698 "only available with "
16699 "-std=c++14 or -std=gnu++14");
16700 else if (!flag_concepts)
16701 pedwarn (token->location, OPT_Wpedantic,
16702 "ISO C++ forbids use of %<auto%> in parameter "
16703 "declaration");
16704 }
16705 else
16706 type = make_auto ();
16707 break;
16708
16709 case RID_DECLTYPE:
16710 /* Since DR 743, decltype can either be a simple-type-specifier by
16711 itself or begin a nested-name-specifier. Parsing it will replace
16712 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16713 handling below decide what to do. */
16714 cp_parser_decltype (parser);
16715 cp_lexer_set_token_position (parser->lexer, token);
16716 break;
16717
16718 case RID_TYPEOF:
16719 /* Consume the `typeof' token. */
16720 cp_lexer_consume_token (parser->lexer);
16721 /* Parse the operand to `typeof'. */
16722 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16723 /* If it is not already a TYPE, take its type. */
16724 if (!TYPE_P (type))
16725 type = finish_typeof (type);
16726
16727 if (decl_specs)
16728 cp_parser_set_decl_spec_type (decl_specs, type,
16729 token,
16730 /*type_definition_p=*/false);
16731
16732 return type;
16733
16734 case RID_UNDERLYING_TYPE:
16735 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16736 if (decl_specs)
16737 cp_parser_set_decl_spec_type (decl_specs, type,
16738 token,
16739 /*type_definition_p=*/false);
16740
16741 return type;
16742
16743 case RID_BASES:
16744 case RID_DIRECT_BASES:
16745 type = cp_parser_trait_expr (parser, token->keyword);
16746 if (decl_specs)
16747 cp_parser_set_decl_spec_type (decl_specs, type,
16748 token,
16749 /*type_definition_p=*/false);
16750 return type;
16751 default:
16752 break;
16753 }
16754
16755 /* If token is an already-parsed decltype not followed by ::,
16756 it's a simple-type-specifier. */
16757 if (token->type == CPP_DECLTYPE
16758 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16759 {
16760 type = saved_checks_value (token->u.tree_check_value);
16761 if (decl_specs)
16762 {
16763 cp_parser_set_decl_spec_type (decl_specs, type,
16764 token,
16765 /*type_definition_p=*/false);
16766 /* Remember that we are handling a decltype in order to
16767 implement the resolution of DR 1510 when the argument
16768 isn't instantiation dependent. */
16769 decl_specs->decltype_p = true;
16770 }
16771 cp_lexer_consume_token (parser->lexer);
16772 return type;
16773 }
16774
16775 /* If the type-specifier was for a built-in type, we're done. */
16776 if (type)
16777 {
16778 /* Record the type. */
16779 if (decl_specs
16780 && (token->keyword != RID_SIGNED
16781 && token->keyword != RID_UNSIGNED
16782 && token->keyword != RID_SHORT
16783 && token->keyword != RID_LONG))
16784 cp_parser_set_decl_spec_type (decl_specs,
16785 type,
16786 token,
16787 /*type_definition_p=*/false);
16788 if (decl_specs)
16789 decl_specs->any_specifiers_p = true;
16790
16791 /* Consume the token. */
16792 cp_lexer_consume_token (parser->lexer);
16793
16794 if (type == error_mark_node)
16795 return error_mark_node;
16796
16797 /* There is no valid C++ program where a non-template type is
16798 followed by a "<". That usually indicates that the user thought
16799 that the type was a template. */
16800 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16801 token->location);
16802
16803 return TYPE_NAME (type);
16804 }
16805
16806 /* The type-specifier must be a user-defined type. */
16807 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16808 {
16809 bool qualified_p;
16810 bool global_p;
16811
16812 /* Don't gobble tokens or issue error messages if this is an
16813 optional type-specifier. */
16814 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16815 cp_parser_parse_tentatively (parser);
16816
16817 token = cp_lexer_peek_token (parser->lexer);
16818
16819 /* Look for the optional `::' operator. */
16820 global_p
16821 = (cp_parser_global_scope_opt (parser,
16822 /*current_scope_valid_p=*/false)
16823 != NULL_TREE);
16824 /* Look for the nested-name specifier. */
16825 qualified_p
16826 = (cp_parser_nested_name_specifier_opt (parser,
16827 /*typename_keyword_p=*/false,
16828 /*check_dependency_p=*/true,
16829 /*type_p=*/false,
16830 /*is_declaration=*/false)
16831 != NULL_TREE);
16832 /* If we have seen a nested-name-specifier, and the next token
16833 is `template', then we are using the template-id production. */
16834 if (parser->scope
16835 && cp_parser_optional_template_keyword (parser))
16836 {
16837 /* Look for the template-id. */
16838 type = cp_parser_template_id (parser,
16839 /*template_keyword_p=*/true,
16840 /*check_dependency_p=*/true,
16841 none_type,
16842 /*is_declaration=*/false);
16843 /* If the template-id did not name a type, we are out of
16844 luck. */
16845 if (TREE_CODE (type) != TYPE_DECL)
16846 {
16847 cp_parser_error (parser, "expected template-id for type");
16848 type = NULL_TREE;
16849 }
16850 }
16851 /* Otherwise, look for a type-name. */
16852 else
16853 type = cp_parser_type_name (parser);
16854 /* Keep track of all name-lookups performed in class scopes. */
16855 if (type
16856 && !global_p
16857 && !qualified_p
16858 && TREE_CODE (type) == TYPE_DECL
16859 && identifier_p (DECL_NAME (type)))
16860 maybe_note_name_used_in_class (DECL_NAME (type), type);
16861 /* If it didn't work out, we don't have a TYPE. */
16862 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16863 && !cp_parser_parse_definitely (parser))
16864 type = NULL_TREE;
16865 if (!type && cxx_dialect >= cxx1z)
16866 {
16867 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16868 cp_parser_parse_tentatively (parser);
16869
16870 cp_parser_global_scope_opt (parser,
16871 /*current_scope_valid_p=*/false);
16872 cp_parser_nested_name_specifier_opt (parser,
16873 /*typename_keyword_p=*/false,
16874 /*check_dependency_p=*/true,
16875 /*type_p=*/false,
16876 /*is_declaration=*/false);
16877 tree name = cp_parser_identifier (parser);
16878 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16879 && parser->scope != error_mark_node)
16880 {
16881 tree tmpl = cp_parser_lookup_name (parser, name,
16882 none_type,
16883 /*is_template=*/false,
16884 /*is_namespace=*/false,
16885 /*check_dependency=*/true,
16886 /*ambiguous_decls=*/NULL,
16887 token->location);
16888 if (tmpl && tmpl != error_mark_node
16889 && (DECL_CLASS_TEMPLATE_P (tmpl)
16890 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16891 type = make_template_placeholder (tmpl);
16892 else
16893 {
16894 type = error_mark_node;
16895 if (!cp_parser_simulate_error (parser))
16896 cp_parser_name_lookup_error (parser, name, tmpl,
16897 NLE_TYPE, token->location);
16898 }
16899 }
16900 else
16901 type = error_mark_node;
16902
16903 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16904 && !cp_parser_parse_definitely (parser))
16905 type = NULL_TREE;
16906 }
16907 if (type && decl_specs)
16908 cp_parser_set_decl_spec_type (decl_specs, type,
16909 token,
16910 /*type_definition_p=*/false);
16911 }
16912
16913 /* If we didn't get a type-name, issue an error message. */
16914 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16915 {
16916 cp_parser_error (parser, "expected type-name");
16917 return error_mark_node;
16918 }
16919
16920 if (type && type != error_mark_node)
16921 {
16922 /* See if TYPE is an Objective-C type, and if so, parse and
16923 accept any protocol references following it. Do this before
16924 the cp_parser_check_for_invalid_template_id() call, because
16925 Objective-C types can be followed by '<...>' which would
16926 enclose protocol names rather than template arguments, and so
16927 everything is fine. */
16928 if (c_dialect_objc () && !parser->scope
16929 && (objc_is_id (type) || objc_is_class_name (type)))
16930 {
16931 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16932 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16933
16934 /* Clobber the "unqualified" type previously entered into
16935 DECL_SPECS with the new, improved protocol-qualified version. */
16936 if (decl_specs)
16937 decl_specs->type = qual_type;
16938
16939 return qual_type;
16940 }
16941
16942 /* There is no valid C++ program where a non-template type is
16943 followed by a "<". That usually indicates that the user
16944 thought that the type was a template. */
16945 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16946 none_type,
16947 token->location);
16948 }
16949
16950 return type;
16951 }
16952
16953 /* Parse a type-name.
16954
16955 type-name:
16956 class-name
16957 enum-name
16958 typedef-name
16959 simple-template-id [in c++0x]
16960
16961 enum-name:
16962 identifier
16963
16964 typedef-name:
16965 identifier
16966
16967 Concepts:
16968
16969 type-name:
16970 concept-name
16971 partial-concept-id
16972
16973 concept-name:
16974 identifier
16975
16976 Returns a TYPE_DECL for the type. */
16977
16978 static tree
16979 cp_parser_type_name (cp_parser* parser)
16980 {
16981 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16982 }
16983
16984 /* See above. */
16985 static tree
16986 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16987 {
16988 tree type_decl;
16989
16990 /* We can't know yet whether it is a class-name or not. */
16991 cp_parser_parse_tentatively (parser);
16992 /* Try a class-name. */
16993 type_decl = cp_parser_class_name (parser,
16994 typename_keyword_p,
16995 /*template_keyword_p=*/false,
16996 none_type,
16997 /*check_dependency_p=*/true,
16998 /*class_head_p=*/false,
16999 /*is_declaration=*/false);
17000 /* If it's not a class-name, keep looking. */
17001 if (!cp_parser_parse_definitely (parser))
17002 {
17003 if (cxx_dialect < cxx11)
17004 /* It must be a typedef-name or an enum-name. */
17005 return cp_parser_nonclass_name (parser);
17006
17007 cp_parser_parse_tentatively (parser);
17008 /* It is either a simple-template-id representing an
17009 instantiation of an alias template... */
17010 type_decl = cp_parser_template_id (parser,
17011 /*template_keyword_p=*/false,
17012 /*check_dependency_p=*/true,
17013 none_type,
17014 /*is_declaration=*/false);
17015 /* Note that this must be an instantiation of an alias template
17016 because [temp.names]/6 says:
17017
17018 A template-id that names an alias template specialization
17019 is a type-name.
17020
17021 Whereas [temp.names]/7 says:
17022
17023 A simple-template-id that names a class template
17024 specialization is a class-name.
17025
17026 With concepts, this could also be a partial-concept-id that
17027 declares a non-type template parameter. */
17028 if (type_decl != NULL_TREE
17029 && TREE_CODE (type_decl) == TYPE_DECL
17030 && TYPE_DECL_ALIAS_P (type_decl))
17031 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17032 else if (is_constrained_parameter (type_decl))
17033 /* Don't do anything. */ ;
17034 else
17035 cp_parser_simulate_error (parser);
17036
17037 if (!cp_parser_parse_definitely (parser))
17038 /* ... Or a typedef-name or an enum-name. */
17039 return cp_parser_nonclass_name (parser);
17040 }
17041
17042 return type_decl;
17043 }
17044
17045 /* Check if DECL and ARGS can form a constrained-type-specifier.
17046 If ARGS is non-null, we try to form a concept check of the
17047 form DECL<?, ARGS> where ? is a wildcard that matches any
17048 kind of template argument. If ARGS is NULL, then we try to
17049 form a concept check of the form DECL<?>. */
17050
17051 static tree
17052 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17053 tree decl, tree args)
17054 {
17055 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17056
17057 /* If we a constrained-type-specifier cannot be deduced. */
17058 if (parser->prevent_constrained_type_specifiers)
17059 return NULL_TREE;
17060
17061 /* A constrained type specifier can only be found in an
17062 overload set or as a reference to a template declaration.
17063
17064 FIXME: This might be masking a bug. It's possible that
17065 that the deduction below is causing template specializations
17066 to be formed with the wildcard as an argument. */
17067 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17068 return NULL_TREE;
17069
17070 /* Try to build a call expression that evaluates the
17071 concept. This can fail if the overload set refers
17072 only to non-templates. */
17073 tree placeholder = build_nt (WILDCARD_DECL);
17074 tree check = build_concept_check (decl, placeholder, args);
17075 if (check == error_mark_node)
17076 return NULL_TREE;
17077
17078 /* Deduce the checked constraint and the prototype parameter.
17079
17080 FIXME: In certain cases, failure to deduce should be a
17081 diagnosable error. */
17082 tree conc;
17083 tree proto;
17084 if (!deduce_constrained_parameter (check, conc, proto))
17085 return NULL_TREE;
17086
17087 /* In template parameter scope, this results in a constrained
17088 parameter. Return a descriptor of that parm. */
17089 if (processing_template_parmlist)
17090 return build_constrained_parameter (conc, proto, args);
17091
17092 /* In a parameter-declaration-clause, constrained-type
17093 specifiers result in invented template parameters. */
17094 if (parser->auto_is_implicit_function_template_parm_p)
17095 {
17096 tree x = build_constrained_parameter (conc, proto, args);
17097 return synthesize_implicit_template_parm (parser, x);
17098 }
17099 else
17100 {
17101 /* Otherwise, we're in a context where the constrained
17102 type name is deduced and the constraint applies
17103 after deduction. */
17104 return make_constrained_auto (conc, args);
17105 }
17106
17107 return NULL_TREE;
17108 }
17109
17110 /* If DECL refers to a concept, return a TYPE_DECL representing
17111 the result of using the constrained type specifier in the
17112 current context. DECL refers to a concept if
17113
17114 - it is an overload set containing a function concept taking a single
17115 type argument, or
17116
17117 - it is a variable concept taking a single type argument. */
17118
17119 static tree
17120 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17121 {
17122 if (flag_concepts
17123 && (TREE_CODE (decl) == OVERLOAD
17124 || BASELINK_P (decl)
17125 || variable_concept_p (decl)))
17126 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17127 else
17128 return NULL_TREE;
17129 }
17130
17131 /* Check if DECL and ARGS form a partial-concept-id. If so,
17132 assign ID to the resulting constrained placeholder.
17133
17134 Returns true if the partial-concept-id designates a placeholder
17135 and false otherwise. Note that *id is set to NULL_TREE in
17136 this case. */
17137
17138 static tree
17139 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17140 {
17141 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17142 }
17143
17144 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17145 or a concept-name.
17146
17147 enum-name:
17148 identifier
17149
17150 typedef-name:
17151 identifier
17152
17153 concept-name:
17154 identifier
17155
17156 Returns a TYPE_DECL for the type. */
17157
17158 static tree
17159 cp_parser_nonclass_name (cp_parser* parser)
17160 {
17161 tree type_decl;
17162 tree identifier;
17163
17164 cp_token *token = cp_lexer_peek_token (parser->lexer);
17165 identifier = cp_parser_identifier (parser);
17166 if (identifier == error_mark_node)
17167 return error_mark_node;
17168
17169 /* Look up the type-name. */
17170 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17171
17172 type_decl = strip_using_decl (type_decl);
17173
17174 /* If we found an overload set, then it may refer to a concept-name. */
17175 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17176 type_decl = decl;
17177
17178 if (TREE_CODE (type_decl) != TYPE_DECL
17179 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17180 {
17181 /* See if this is an Objective-C type. */
17182 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17183 tree type = objc_get_protocol_qualified_type (identifier, protos);
17184 if (type)
17185 type_decl = TYPE_NAME (type);
17186 }
17187
17188 /* Issue an error if we did not find a type-name. */
17189 if (TREE_CODE (type_decl) != TYPE_DECL
17190 /* In Objective-C, we have the complication that class names are
17191 normally type names and start declarations (eg, the
17192 "NSObject" in "NSObject *object;"), but can be used in an
17193 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17194 is an expression. So, a classname followed by a dot is not a
17195 valid type-name. */
17196 || (objc_is_class_name (TREE_TYPE (type_decl))
17197 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17198 {
17199 if (!cp_parser_simulate_error (parser))
17200 cp_parser_name_lookup_error (parser, identifier, type_decl,
17201 NLE_TYPE, token->location);
17202 return error_mark_node;
17203 }
17204 /* Remember that the name was used in the definition of the
17205 current class so that we can check later to see if the
17206 meaning would have been different after the class was
17207 entirely defined. */
17208 else if (type_decl != error_mark_node
17209 && !parser->scope)
17210 maybe_note_name_used_in_class (identifier, type_decl);
17211
17212 return type_decl;
17213 }
17214
17215 /* Parse an elaborated-type-specifier. Note that the grammar given
17216 here incorporates the resolution to DR68.
17217
17218 elaborated-type-specifier:
17219 class-key :: [opt] nested-name-specifier [opt] identifier
17220 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17221 enum-key :: [opt] nested-name-specifier [opt] identifier
17222 typename :: [opt] nested-name-specifier identifier
17223 typename :: [opt] nested-name-specifier template [opt]
17224 template-id
17225
17226 GNU extension:
17227
17228 elaborated-type-specifier:
17229 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17230 class-key attributes :: [opt] nested-name-specifier [opt]
17231 template [opt] template-id
17232 enum attributes :: [opt] nested-name-specifier [opt] identifier
17233
17234 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17235 declared `friend'. If IS_DECLARATION is TRUE, then this
17236 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17237 something is being declared.
17238
17239 Returns the TYPE specified. */
17240
17241 static tree
17242 cp_parser_elaborated_type_specifier (cp_parser* parser,
17243 bool is_friend,
17244 bool is_declaration)
17245 {
17246 enum tag_types tag_type;
17247 tree identifier;
17248 tree type = NULL_TREE;
17249 tree attributes = NULL_TREE;
17250 tree globalscope;
17251 cp_token *token = NULL;
17252
17253 /* See if we're looking at the `enum' keyword. */
17254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17255 {
17256 /* Consume the `enum' token. */
17257 cp_lexer_consume_token (parser->lexer);
17258 /* Remember that it's an enumeration type. */
17259 tag_type = enum_type;
17260 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17261 enums) is used here. */
17262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17263 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17264 {
17265 pedwarn (input_location, 0, "elaborated-type-specifier "
17266 "for a scoped enum must not use the %<%D%> keyword",
17267 cp_lexer_peek_token (parser->lexer)->u.value);
17268 /* Consume the `struct' or `class' and parse it anyway. */
17269 cp_lexer_consume_token (parser->lexer);
17270 }
17271 /* Parse the attributes. */
17272 attributes = cp_parser_attributes_opt (parser);
17273 }
17274 /* Or, it might be `typename'. */
17275 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17276 RID_TYPENAME))
17277 {
17278 /* Consume the `typename' token. */
17279 cp_lexer_consume_token (parser->lexer);
17280 /* Remember that it's a `typename' type. */
17281 tag_type = typename_type;
17282 }
17283 /* Otherwise it must be a class-key. */
17284 else
17285 {
17286 tag_type = cp_parser_class_key (parser);
17287 if (tag_type == none_type)
17288 return error_mark_node;
17289 /* Parse the attributes. */
17290 attributes = cp_parser_attributes_opt (parser);
17291 }
17292
17293 /* Look for the `::' operator. */
17294 globalscope = cp_parser_global_scope_opt (parser,
17295 /*current_scope_valid_p=*/false);
17296 /* Look for the nested-name-specifier. */
17297 tree nested_name_specifier;
17298 if (tag_type == typename_type && !globalscope)
17299 {
17300 nested_name_specifier
17301 = cp_parser_nested_name_specifier (parser,
17302 /*typename_keyword_p=*/true,
17303 /*check_dependency_p=*/true,
17304 /*type_p=*/true,
17305 is_declaration);
17306 if (!nested_name_specifier)
17307 return error_mark_node;
17308 }
17309 else
17310 /* Even though `typename' is not present, the proposed resolution
17311 to Core Issue 180 says that in `class A<T>::B', `B' should be
17312 considered a type-name, even if `A<T>' is dependent. */
17313 nested_name_specifier
17314 = cp_parser_nested_name_specifier_opt (parser,
17315 /*typename_keyword_p=*/true,
17316 /*check_dependency_p=*/true,
17317 /*type_p=*/true,
17318 is_declaration);
17319 /* For everything but enumeration types, consider a template-id.
17320 For an enumeration type, consider only a plain identifier. */
17321 if (tag_type != enum_type)
17322 {
17323 bool template_p = false;
17324 tree decl;
17325
17326 /* Allow the `template' keyword. */
17327 template_p = cp_parser_optional_template_keyword (parser);
17328 /* If we didn't see `template', we don't know if there's a
17329 template-id or not. */
17330 if (!template_p)
17331 cp_parser_parse_tentatively (parser);
17332 /* Parse the template-id. */
17333 token = cp_lexer_peek_token (parser->lexer);
17334 decl = cp_parser_template_id (parser, template_p,
17335 /*check_dependency_p=*/true,
17336 tag_type,
17337 is_declaration);
17338 /* If we didn't find a template-id, look for an ordinary
17339 identifier. */
17340 if (!template_p && !cp_parser_parse_definitely (parser))
17341 ;
17342 /* We can get here when cp_parser_template_id, called by
17343 cp_parser_class_name with tag_type == none_type, succeeds
17344 and caches a BASELINK. Then, when called again here,
17345 instead of failing and returning an error_mark_node
17346 returns it (see template/typename17.C in C++11).
17347 ??? Could we diagnose this earlier? */
17348 else if (tag_type == typename_type && BASELINK_P (decl))
17349 {
17350 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17351 type = error_mark_node;
17352 }
17353 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17354 in effect, then we must assume that, upon instantiation, the
17355 template will correspond to a class. */
17356 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17357 && tag_type == typename_type)
17358 type = make_typename_type (parser->scope, decl,
17359 typename_type,
17360 /*complain=*/tf_error);
17361 /* If the `typename' keyword is in effect and DECL is not a type
17362 decl, then type is non existent. */
17363 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17364 ;
17365 else if (TREE_CODE (decl) == TYPE_DECL)
17366 {
17367 type = check_elaborated_type_specifier (tag_type, decl,
17368 /*allow_template_p=*/true);
17369
17370 /* If the next token is a semicolon, this must be a specialization,
17371 instantiation, or friend declaration. Check the scope while we
17372 still know whether or not we had a nested-name-specifier. */
17373 if (type != error_mark_node
17374 && !nested_name_specifier && !is_friend
17375 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17376 check_unqualified_spec_or_inst (type, token->location);
17377 }
17378 else if (decl == error_mark_node)
17379 type = error_mark_node;
17380 }
17381
17382 if (!type)
17383 {
17384 token = cp_lexer_peek_token (parser->lexer);
17385 identifier = cp_parser_identifier (parser);
17386
17387 if (identifier == error_mark_node)
17388 {
17389 parser->scope = NULL_TREE;
17390 return error_mark_node;
17391 }
17392
17393 /* For a `typename', we needn't call xref_tag. */
17394 if (tag_type == typename_type
17395 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17396 return cp_parser_make_typename_type (parser, identifier,
17397 token->location);
17398
17399 /* Template parameter lists apply only if we are not within a
17400 function parameter list. */
17401 bool template_parm_lists_apply
17402 = parser->num_template_parameter_lists;
17403 if (template_parm_lists_apply)
17404 for (cp_binding_level *s = current_binding_level;
17405 s && s->kind != sk_template_parms;
17406 s = s->level_chain)
17407 if (s->kind == sk_function_parms)
17408 template_parm_lists_apply = false;
17409
17410 /* Look up a qualified name in the usual way. */
17411 if (parser->scope)
17412 {
17413 tree decl;
17414 tree ambiguous_decls;
17415
17416 decl = cp_parser_lookup_name (parser, identifier,
17417 tag_type,
17418 /*is_template=*/false,
17419 /*is_namespace=*/false,
17420 /*check_dependency=*/true,
17421 &ambiguous_decls,
17422 token->location);
17423
17424 /* If the lookup was ambiguous, an error will already have been
17425 issued. */
17426 if (ambiguous_decls)
17427 return error_mark_node;
17428
17429 /* If we are parsing friend declaration, DECL may be a
17430 TEMPLATE_DECL tree node here. However, we need to check
17431 whether this TEMPLATE_DECL results in valid code. Consider
17432 the following example:
17433
17434 namespace N {
17435 template <class T> class C {};
17436 }
17437 class X {
17438 template <class T> friend class N::C; // #1, valid code
17439 };
17440 template <class T> class Y {
17441 friend class N::C; // #2, invalid code
17442 };
17443
17444 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17445 name lookup of `N::C'. We see that friend declaration must
17446 be template for the code to be valid. Note that
17447 processing_template_decl does not work here since it is
17448 always 1 for the above two cases. */
17449
17450 decl = (cp_parser_maybe_treat_template_as_class
17451 (decl, /*tag_name_p=*/is_friend
17452 && template_parm_lists_apply));
17453
17454 if (TREE_CODE (decl) != TYPE_DECL)
17455 {
17456 cp_parser_diagnose_invalid_type_name (parser,
17457 identifier,
17458 token->location);
17459 return error_mark_node;
17460 }
17461
17462 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17463 {
17464 bool allow_template = (template_parm_lists_apply
17465 || DECL_SELF_REFERENCE_P (decl));
17466 type = check_elaborated_type_specifier (tag_type, decl,
17467 allow_template);
17468
17469 if (type == error_mark_node)
17470 return error_mark_node;
17471 }
17472
17473 /* Forward declarations of nested types, such as
17474
17475 class C1::C2;
17476 class C1::C2::C3;
17477
17478 are invalid unless all components preceding the final '::'
17479 are complete. If all enclosing types are complete, these
17480 declarations become merely pointless.
17481
17482 Invalid forward declarations of nested types are errors
17483 caught elsewhere in parsing. Those that are pointless arrive
17484 here. */
17485
17486 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17487 && !is_friend && !processing_explicit_instantiation)
17488 warning (0, "declaration %qD does not declare anything", decl);
17489
17490 type = TREE_TYPE (decl);
17491 }
17492 else
17493 {
17494 /* An elaborated-type-specifier sometimes introduces a new type and
17495 sometimes names an existing type. Normally, the rule is that it
17496 introduces a new type only if there is not an existing type of
17497 the same name already in scope. For example, given:
17498
17499 struct S {};
17500 void f() { struct S s; }
17501
17502 the `struct S' in the body of `f' is the same `struct S' as in
17503 the global scope; the existing definition is used. However, if
17504 there were no global declaration, this would introduce a new
17505 local class named `S'.
17506
17507 An exception to this rule applies to the following code:
17508
17509 namespace N { struct S; }
17510
17511 Here, the elaborated-type-specifier names a new type
17512 unconditionally; even if there is already an `S' in the
17513 containing scope this declaration names a new type.
17514 This exception only applies if the elaborated-type-specifier
17515 forms the complete declaration:
17516
17517 [class.name]
17518
17519 A declaration consisting solely of `class-key identifier ;' is
17520 either a redeclaration of the name in the current scope or a
17521 forward declaration of the identifier as a class name. It
17522 introduces the name into the current scope.
17523
17524 We are in this situation precisely when the next token is a `;'.
17525
17526 An exception to the exception is that a `friend' declaration does
17527 *not* name a new type; i.e., given:
17528
17529 struct S { friend struct T; };
17530
17531 `T' is not a new type in the scope of `S'.
17532
17533 Also, `new struct S' or `sizeof (struct S)' never results in the
17534 definition of a new type; a new type can only be declared in a
17535 declaration context. */
17536
17537 tag_scope ts;
17538 bool template_p;
17539
17540 if (is_friend)
17541 /* Friends have special name lookup rules. */
17542 ts = ts_within_enclosing_non_class;
17543 else if (is_declaration
17544 && cp_lexer_next_token_is (parser->lexer,
17545 CPP_SEMICOLON))
17546 /* This is a `class-key identifier ;' */
17547 ts = ts_current;
17548 else
17549 ts = ts_global;
17550
17551 template_p =
17552 (template_parm_lists_apply
17553 && (cp_parser_next_token_starts_class_definition_p (parser)
17554 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17555 /* An unqualified name was used to reference this type, so
17556 there were no qualifying templates. */
17557 if (template_parm_lists_apply
17558 && !cp_parser_check_template_parameters (parser,
17559 /*num_templates=*/0,
17560 token->location,
17561 /*declarator=*/NULL))
17562 return error_mark_node;
17563 type = xref_tag (tag_type, identifier, ts, template_p);
17564 }
17565 }
17566
17567 if (type == error_mark_node)
17568 return error_mark_node;
17569
17570 /* Allow attributes on forward declarations of classes. */
17571 if (attributes)
17572 {
17573 if (TREE_CODE (type) == TYPENAME_TYPE)
17574 warning (OPT_Wattributes,
17575 "attributes ignored on uninstantiated type");
17576 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17577 && ! processing_explicit_instantiation)
17578 warning (OPT_Wattributes,
17579 "attributes ignored on template instantiation");
17580 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17581 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17582 else
17583 warning (OPT_Wattributes,
17584 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17585 }
17586
17587 if (tag_type != enum_type)
17588 {
17589 /* Indicate whether this class was declared as a `class' or as a
17590 `struct'. */
17591 if (CLASS_TYPE_P (type))
17592 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17593 cp_parser_check_class_key (tag_type, type);
17594 }
17595
17596 /* A "<" cannot follow an elaborated type specifier. If that
17597 happens, the user was probably trying to form a template-id. */
17598 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17599 token->location);
17600
17601 return type;
17602 }
17603
17604 /* Parse an enum-specifier.
17605
17606 enum-specifier:
17607 enum-head { enumerator-list [opt] }
17608 enum-head { enumerator-list , } [C++0x]
17609
17610 enum-head:
17611 enum-key identifier [opt] enum-base [opt]
17612 enum-key nested-name-specifier identifier enum-base [opt]
17613
17614 enum-key:
17615 enum
17616 enum class [C++0x]
17617 enum struct [C++0x]
17618
17619 enum-base: [C++0x]
17620 : type-specifier-seq
17621
17622 opaque-enum-specifier:
17623 enum-key identifier enum-base [opt] ;
17624
17625 GNU Extensions:
17626 enum-key attributes[opt] identifier [opt] enum-base [opt]
17627 { enumerator-list [opt] }attributes[opt]
17628 enum-key attributes[opt] identifier [opt] enum-base [opt]
17629 { enumerator-list, }attributes[opt] [C++0x]
17630
17631 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17632 if the token stream isn't an enum-specifier after all. */
17633
17634 static tree
17635 cp_parser_enum_specifier (cp_parser* parser)
17636 {
17637 tree identifier;
17638 tree type = NULL_TREE;
17639 tree prev_scope;
17640 tree nested_name_specifier = NULL_TREE;
17641 tree attributes;
17642 bool scoped_enum_p = false;
17643 bool has_underlying_type = false;
17644 bool nested_being_defined = false;
17645 bool new_value_list = false;
17646 bool is_new_type = false;
17647 bool is_unnamed = false;
17648 tree underlying_type = NULL_TREE;
17649 cp_token *type_start_token = NULL;
17650 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17651
17652 parser->colon_corrects_to_scope_p = false;
17653
17654 /* Parse tentatively so that we can back up if we don't find a
17655 enum-specifier. */
17656 cp_parser_parse_tentatively (parser);
17657
17658 /* Caller guarantees that the current token is 'enum', an identifier
17659 possibly follows, and the token after that is an opening brace.
17660 If we don't have an identifier, fabricate an anonymous name for
17661 the enumeration being defined. */
17662 cp_lexer_consume_token (parser->lexer);
17663
17664 /* Parse the "class" or "struct", which indicates a scoped
17665 enumeration type in C++0x. */
17666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17667 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17668 {
17669 if (cxx_dialect < cxx11)
17670 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17671
17672 /* Consume the `struct' or `class' token. */
17673 cp_lexer_consume_token (parser->lexer);
17674
17675 scoped_enum_p = true;
17676 }
17677
17678 attributes = cp_parser_attributes_opt (parser);
17679
17680 /* Clear the qualification. */
17681 parser->scope = NULL_TREE;
17682 parser->qualifying_scope = NULL_TREE;
17683 parser->object_scope = NULL_TREE;
17684
17685 /* Figure out in what scope the declaration is being placed. */
17686 prev_scope = current_scope ();
17687
17688 type_start_token = cp_lexer_peek_token (parser->lexer);
17689
17690 push_deferring_access_checks (dk_no_check);
17691 nested_name_specifier
17692 = cp_parser_nested_name_specifier_opt (parser,
17693 /*typename_keyword_p=*/true,
17694 /*check_dependency_p=*/false,
17695 /*type_p=*/false,
17696 /*is_declaration=*/false);
17697
17698 if (nested_name_specifier)
17699 {
17700 tree name;
17701
17702 identifier = cp_parser_identifier (parser);
17703 name = cp_parser_lookup_name (parser, identifier,
17704 enum_type,
17705 /*is_template=*/false,
17706 /*is_namespace=*/false,
17707 /*check_dependency=*/true,
17708 /*ambiguous_decls=*/NULL,
17709 input_location);
17710 if (name && name != error_mark_node)
17711 {
17712 type = TREE_TYPE (name);
17713 if (TREE_CODE (type) == TYPENAME_TYPE)
17714 {
17715 /* Are template enums allowed in ISO? */
17716 if (template_parm_scope_p ())
17717 pedwarn (type_start_token->location, OPT_Wpedantic,
17718 "%qD is an enumeration template", name);
17719 /* ignore a typename reference, for it will be solved by name
17720 in start_enum. */
17721 type = NULL_TREE;
17722 }
17723 }
17724 else if (nested_name_specifier == error_mark_node)
17725 /* We already issued an error. */;
17726 else
17727 {
17728 error_at (type_start_token->location,
17729 "%qD does not name an enumeration in %qT",
17730 identifier, nested_name_specifier);
17731 nested_name_specifier = error_mark_node;
17732 }
17733 }
17734 else
17735 {
17736 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17737 identifier = cp_parser_identifier (parser);
17738 else
17739 {
17740 identifier = make_anon_name ();
17741 is_unnamed = true;
17742 if (scoped_enum_p)
17743 error_at (type_start_token->location,
17744 "unnamed scoped enum is not allowed");
17745 }
17746 }
17747 pop_deferring_access_checks ();
17748
17749 /* Check for the `:' that denotes a specified underlying type in C++0x.
17750 Note that a ':' could also indicate a bitfield width, however. */
17751 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17752 {
17753 cp_decl_specifier_seq type_specifiers;
17754
17755 /* Consume the `:'. */
17756 cp_lexer_consume_token (parser->lexer);
17757
17758 /* Parse the type-specifier-seq. */
17759 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17760 /*is_trailing_return=*/false,
17761 &type_specifiers);
17762
17763 /* At this point this is surely not elaborated type specifier. */
17764 if (!cp_parser_parse_definitely (parser))
17765 return NULL_TREE;
17766
17767 if (cxx_dialect < cxx11)
17768 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17769
17770 has_underlying_type = true;
17771
17772 /* If that didn't work, stop. */
17773 if (type_specifiers.type != error_mark_node)
17774 {
17775 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17776 /*initialized=*/0, NULL);
17777 if (underlying_type == error_mark_node
17778 || check_for_bare_parameter_packs (underlying_type))
17779 underlying_type = NULL_TREE;
17780 }
17781 }
17782
17783 /* Look for the `{' but don't consume it yet. */
17784 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17785 {
17786 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17787 {
17788 cp_parser_error (parser, "expected %<{%>");
17789 if (has_underlying_type)
17790 {
17791 type = NULL_TREE;
17792 goto out;
17793 }
17794 }
17795 /* An opaque-enum-specifier must have a ';' here. */
17796 if ((scoped_enum_p || underlying_type)
17797 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17798 {
17799 cp_parser_error (parser, "expected %<;%> or %<{%>");
17800 if (has_underlying_type)
17801 {
17802 type = NULL_TREE;
17803 goto out;
17804 }
17805 }
17806 }
17807
17808 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17809 return NULL_TREE;
17810
17811 if (nested_name_specifier)
17812 {
17813 if (CLASS_TYPE_P (nested_name_specifier))
17814 {
17815 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17816 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17817 push_scope (nested_name_specifier);
17818 }
17819 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17820 {
17821 push_nested_namespace (nested_name_specifier);
17822 }
17823 }
17824
17825 /* Issue an error message if type-definitions are forbidden here. */
17826 if (!cp_parser_check_type_definition (parser))
17827 type = error_mark_node;
17828 else
17829 /* Create the new type. We do this before consuming the opening
17830 brace so the enum will be recorded as being on the line of its
17831 tag (or the 'enum' keyword, if there is no tag). */
17832 type = start_enum (identifier, type, underlying_type,
17833 attributes, scoped_enum_p, &is_new_type);
17834
17835 /* If the next token is not '{' it is an opaque-enum-specifier or an
17836 elaborated-type-specifier. */
17837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17838 {
17839 timevar_push (TV_PARSE_ENUM);
17840 if (nested_name_specifier
17841 && nested_name_specifier != error_mark_node)
17842 {
17843 /* The following catches invalid code such as:
17844 enum class S<int>::E { A, B, C }; */
17845 if (!processing_specialization
17846 && CLASS_TYPE_P (nested_name_specifier)
17847 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17848 error_at (type_start_token->location, "cannot add an enumerator "
17849 "list to a template instantiation");
17850
17851 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17852 {
17853 error_at (type_start_token->location,
17854 "%<%T::%E%> has not been declared",
17855 TYPE_CONTEXT (nested_name_specifier),
17856 nested_name_specifier);
17857 type = error_mark_node;
17858 }
17859 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17860 && !CLASS_TYPE_P (nested_name_specifier))
17861 {
17862 error_at (type_start_token->location, "nested name specifier "
17863 "%qT for enum declaration does not name a class "
17864 "or namespace", nested_name_specifier);
17865 type = error_mark_node;
17866 }
17867 /* If that scope does not contain the scope in which the
17868 class was originally declared, the program is invalid. */
17869 else if (prev_scope && !is_ancestor (prev_scope,
17870 nested_name_specifier))
17871 {
17872 if (at_namespace_scope_p ())
17873 error_at (type_start_token->location,
17874 "declaration of %qD in namespace %qD which does not "
17875 "enclose %qD",
17876 type, prev_scope, nested_name_specifier);
17877 else
17878 error_at (type_start_token->location,
17879 "declaration of %qD in %qD which does not "
17880 "enclose %qD",
17881 type, prev_scope, nested_name_specifier);
17882 type = error_mark_node;
17883 }
17884 /* If that scope is the scope where the declaration is being placed
17885 the program is invalid. */
17886 else if (CLASS_TYPE_P (nested_name_specifier)
17887 && CLASS_TYPE_P (prev_scope)
17888 && same_type_p (nested_name_specifier, prev_scope))
17889 {
17890 permerror (type_start_token->location,
17891 "extra qualification not allowed");
17892 nested_name_specifier = NULL_TREE;
17893 }
17894 }
17895
17896 if (scoped_enum_p)
17897 begin_scope (sk_scoped_enum, type);
17898
17899 /* Consume the opening brace. */
17900 cp_lexer_consume_token (parser->lexer);
17901
17902 if (type == error_mark_node)
17903 ; /* Nothing to add */
17904 else if (OPAQUE_ENUM_P (type)
17905 || (cxx_dialect > cxx98 && processing_specialization))
17906 {
17907 new_value_list = true;
17908 SET_OPAQUE_ENUM_P (type, false);
17909 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17910 }
17911 else
17912 {
17913 error_at (type_start_token->location,
17914 "multiple definition of %q#T", type);
17915 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17916 "previous definition here");
17917 type = error_mark_node;
17918 }
17919
17920 if (type == error_mark_node)
17921 cp_parser_skip_to_end_of_block_or_statement (parser);
17922 /* If the next token is not '}', then there are some enumerators. */
17923 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17924 {
17925 if (is_unnamed && !scoped_enum_p)
17926 pedwarn (type_start_token->location, OPT_Wpedantic,
17927 "ISO C++ forbids empty unnamed enum");
17928 }
17929 else
17930 cp_parser_enumerator_list (parser, type);
17931
17932 /* Consume the final '}'. */
17933 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17934
17935 if (scoped_enum_p)
17936 finish_scope ();
17937 timevar_pop (TV_PARSE_ENUM);
17938 }
17939 else
17940 {
17941 /* If a ';' follows, then it is an opaque-enum-specifier
17942 and additional restrictions apply. */
17943 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17944 {
17945 if (is_unnamed)
17946 error_at (type_start_token->location,
17947 "opaque-enum-specifier without name");
17948 else if (nested_name_specifier)
17949 error_at (type_start_token->location,
17950 "opaque-enum-specifier must use a simple identifier");
17951 }
17952 }
17953
17954 /* Look for trailing attributes to apply to this enumeration, and
17955 apply them if appropriate. */
17956 if (cp_parser_allow_gnu_extensions_p (parser))
17957 {
17958 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17959 cplus_decl_attributes (&type,
17960 trailing_attr,
17961 (int) ATTR_FLAG_TYPE_IN_PLACE);
17962 }
17963
17964 /* Finish up the enumeration. */
17965 if (type != error_mark_node)
17966 {
17967 if (new_value_list)
17968 finish_enum_value_list (type);
17969 if (is_new_type)
17970 finish_enum (type);
17971 }
17972
17973 if (nested_name_specifier)
17974 {
17975 if (CLASS_TYPE_P (nested_name_specifier))
17976 {
17977 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17978 pop_scope (nested_name_specifier);
17979 }
17980 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17981 {
17982 pop_nested_namespace (nested_name_specifier);
17983 }
17984 }
17985 out:
17986 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17987 return type;
17988 }
17989
17990 /* Parse an enumerator-list. The enumerators all have the indicated
17991 TYPE.
17992
17993 enumerator-list:
17994 enumerator-definition
17995 enumerator-list , enumerator-definition */
17996
17997 static void
17998 cp_parser_enumerator_list (cp_parser* parser, tree type)
17999 {
18000 while (true)
18001 {
18002 /* Parse an enumerator-definition. */
18003 cp_parser_enumerator_definition (parser, type);
18004
18005 /* If the next token is not a ',', we've reached the end of
18006 the list. */
18007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18008 break;
18009 /* Otherwise, consume the `,' and keep going. */
18010 cp_lexer_consume_token (parser->lexer);
18011 /* If the next token is a `}', there is a trailing comma. */
18012 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18013 {
18014 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18015 pedwarn (input_location, OPT_Wpedantic,
18016 "comma at end of enumerator list");
18017 break;
18018 }
18019 }
18020 }
18021
18022 /* Parse an enumerator-definition. The enumerator has the indicated
18023 TYPE.
18024
18025 enumerator-definition:
18026 enumerator
18027 enumerator = constant-expression
18028
18029 enumerator:
18030 identifier
18031
18032 GNU Extensions:
18033
18034 enumerator-definition:
18035 enumerator attributes [opt]
18036 enumerator attributes [opt] = constant-expression */
18037
18038 static void
18039 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18040 {
18041 tree identifier;
18042 tree value;
18043 location_t loc;
18044
18045 /* Save the input location because we are interested in the location
18046 of the identifier and not the location of the explicit value. */
18047 loc = cp_lexer_peek_token (parser->lexer)->location;
18048
18049 /* Look for the identifier. */
18050 identifier = cp_parser_identifier (parser);
18051 if (identifier == error_mark_node)
18052 return;
18053
18054 /* Parse any specified attributes. */
18055 tree attrs = cp_parser_attributes_opt (parser);
18056
18057 /* If the next token is an '=', then there is an explicit value. */
18058 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18059 {
18060 /* Consume the `=' token. */
18061 cp_lexer_consume_token (parser->lexer);
18062 /* Parse the value. */
18063 value = cp_parser_constant_expression (parser);
18064 }
18065 else
18066 value = NULL_TREE;
18067
18068 /* If we are processing a template, make sure the initializer of the
18069 enumerator doesn't contain any bare template parameter pack. */
18070 if (check_for_bare_parameter_packs (value))
18071 value = error_mark_node;
18072
18073 /* Create the enumerator. */
18074 build_enumerator (identifier, value, type, attrs, loc);
18075 }
18076
18077 /* Parse a namespace-name.
18078
18079 namespace-name:
18080 original-namespace-name
18081 namespace-alias
18082
18083 Returns the NAMESPACE_DECL for the namespace. */
18084
18085 static tree
18086 cp_parser_namespace_name (cp_parser* parser)
18087 {
18088 tree identifier;
18089 tree namespace_decl;
18090
18091 cp_token *token = cp_lexer_peek_token (parser->lexer);
18092
18093 /* Get the name of the namespace. */
18094 identifier = cp_parser_identifier (parser);
18095 if (identifier == error_mark_node)
18096 return error_mark_node;
18097
18098 /* Look up the identifier in the currently active scope. Look only
18099 for namespaces, due to:
18100
18101 [basic.lookup.udir]
18102
18103 When looking up a namespace-name in a using-directive or alias
18104 definition, only namespace names are considered.
18105
18106 And:
18107
18108 [basic.lookup.qual]
18109
18110 During the lookup of a name preceding the :: scope resolution
18111 operator, object, function, and enumerator names are ignored.
18112
18113 (Note that cp_parser_qualifying_entity only calls this
18114 function if the token after the name is the scope resolution
18115 operator.) */
18116 namespace_decl = cp_parser_lookup_name (parser, identifier,
18117 none_type,
18118 /*is_template=*/false,
18119 /*is_namespace=*/true,
18120 /*check_dependency=*/true,
18121 /*ambiguous_decls=*/NULL,
18122 token->location);
18123 /* If it's not a namespace, issue an error. */
18124 if (namespace_decl == error_mark_node
18125 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18126 {
18127 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18128 error_at (token->location, "%qD is not a namespace-name", identifier);
18129 cp_parser_error (parser, "expected namespace-name");
18130 namespace_decl = error_mark_node;
18131 }
18132
18133 return namespace_decl;
18134 }
18135
18136 /* Parse a namespace-definition.
18137
18138 namespace-definition:
18139 named-namespace-definition
18140 unnamed-namespace-definition
18141
18142 named-namespace-definition:
18143 original-namespace-definition
18144 extension-namespace-definition
18145
18146 original-namespace-definition:
18147 namespace identifier { namespace-body }
18148
18149 extension-namespace-definition:
18150 namespace original-namespace-name { namespace-body }
18151
18152 unnamed-namespace-definition:
18153 namespace { namespace-body } */
18154
18155 static void
18156 cp_parser_namespace_definition (cp_parser* parser)
18157 {
18158 tree identifier, attribs;
18159 bool has_visibility;
18160 bool is_inline;
18161 cp_token* token;
18162 int nested_definition_count = 0;
18163
18164 cp_ensure_no_omp_declare_simd (parser);
18165 cp_ensure_no_oacc_routine (parser);
18166 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18167 {
18168 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18169 is_inline = true;
18170 cp_lexer_consume_token (parser->lexer);
18171 }
18172 else
18173 is_inline = false;
18174
18175 /* Look for the `namespace' keyword. */
18176 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18177
18178 /* Parse any specified attributes before the identifier. */
18179 attribs = cp_parser_attributes_opt (parser);
18180
18181 /* Get the name of the namespace. We do not attempt to distinguish
18182 between an original-namespace-definition and an
18183 extension-namespace-definition at this point. The semantic
18184 analysis routines are responsible for that. */
18185 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18186 identifier = cp_parser_identifier (parser);
18187 else
18188 identifier = NULL_TREE;
18189
18190 /* Parse any specified attributes after the identifier. */
18191 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18192 if (post_ident_attribs)
18193 {
18194 if (attribs)
18195 attribs = chainon (attribs, post_ident_attribs);
18196 else
18197 attribs = post_ident_attribs;
18198 }
18199
18200 /* Start the namespace. */
18201 bool ok = push_namespace (identifier);
18202
18203 /* Parse any nested namespace definition. */
18204 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18205 {
18206 if (attribs)
18207 error_at (token->location, "a nested namespace definition cannot have attributes");
18208 if (cxx_dialect < cxx1z)
18209 pedwarn (input_location, OPT_Wpedantic,
18210 "nested namespace definitions only available with "
18211 "-std=c++1z or -std=gnu++1z");
18212 if (is_inline)
18213 error_at (token->location, "a nested namespace definition cannot be inline");
18214 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18215 {
18216 cp_lexer_consume_token (parser->lexer);
18217 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18218 identifier = cp_parser_identifier (parser);
18219 else
18220 {
18221 cp_parser_error (parser, "nested identifier required");
18222 break;
18223 }
18224 if (push_namespace (identifier))
18225 ++nested_definition_count;
18226 }
18227 }
18228
18229 /* Look for the `{' to validate starting the namespace. */
18230 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18231
18232 /* "inline namespace" is equivalent to a stub namespace definition
18233 followed by a strong using directive. */
18234 if (is_inline && ok)
18235 {
18236 tree name_space = current_namespace;
18237 /* Set up namespace association. */
18238 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18239 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18240 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18241 /* Import the contents of the inline namespace. */
18242 pop_namespace ();
18243 do_using_directive (name_space);
18244 push_namespace (identifier);
18245 }
18246
18247 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18248
18249 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18250
18251 /* Parse the body of the namespace. */
18252 cp_parser_namespace_body (parser);
18253
18254 if (has_visibility)
18255 pop_visibility (1);
18256
18257 /* Finish the nested namespace definitions. */
18258 while (nested_definition_count--)
18259 pop_namespace ();
18260
18261 /* Finish the namespace. */
18262 if (ok)
18263 pop_namespace ();
18264 /* Look for the final `}'. */
18265 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18266 }
18267
18268 /* Parse a namespace-body.
18269
18270 namespace-body:
18271 declaration-seq [opt] */
18272
18273 static void
18274 cp_parser_namespace_body (cp_parser* parser)
18275 {
18276 cp_parser_declaration_seq_opt (parser);
18277 }
18278
18279 /* Parse a namespace-alias-definition.
18280
18281 namespace-alias-definition:
18282 namespace identifier = qualified-namespace-specifier ; */
18283
18284 static void
18285 cp_parser_namespace_alias_definition (cp_parser* parser)
18286 {
18287 tree identifier;
18288 tree namespace_specifier;
18289
18290 cp_token *token = cp_lexer_peek_token (parser->lexer);
18291
18292 /* Look for the `namespace' keyword. */
18293 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18294 /* Look for the identifier. */
18295 identifier = cp_parser_identifier (parser);
18296 if (identifier == error_mark_node)
18297 return;
18298 /* Look for the `=' token. */
18299 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18300 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18301 {
18302 error_at (token->location, "%<namespace%> definition is not allowed here");
18303 /* Skip the definition. */
18304 cp_lexer_consume_token (parser->lexer);
18305 if (cp_parser_skip_to_closing_brace (parser))
18306 cp_lexer_consume_token (parser->lexer);
18307 return;
18308 }
18309 cp_parser_require (parser, CPP_EQ, RT_EQ);
18310 /* Look for the qualified-namespace-specifier. */
18311 namespace_specifier
18312 = cp_parser_qualified_namespace_specifier (parser);
18313 /* Look for the `;' token. */
18314 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18315
18316 /* Register the alias in the symbol table. */
18317 do_namespace_alias (identifier, namespace_specifier);
18318 }
18319
18320 /* Parse a qualified-namespace-specifier.
18321
18322 qualified-namespace-specifier:
18323 :: [opt] nested-name-specifier [opt] namespace-name
18324
18325 Returns a NAMESPACE_DECL corresponding to the specified
18326 namespace. */
18327
18328 static tree
18329 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18330 {
18331 /* Look for the optional `::'. */
18332 cp_parser_global_scope_opt (parser,
18333 /*current_scope_valid_p=*/false);
18334
18335 /* Look for the optional nested-name-specifier. */
18336 cp_parser_nested_name_specifier_opt (parser,
18337 /*typename_keyword_p=*/false,
18338 /*check_dependency_p=*/true,
18339 /*type_p=*/false,
18340 /*is_declaration=*/true);
18341
18342 return cp_parser_namespace_name (parser);
18343 }
18344
18345 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18346 access declaration.
18347
18348 using-declaration:
18349 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18350 using :: unqualified-id ;
18351
18352 access-declaration:
18353 qualified-id ;
18354
18355 */
18356
18357 static bool
18358 cp_parser_using_declaration (cp_parser* parser,
18359 bool access_declaration_p)
18360 {
18361 cp_token *token;
18362 bool typename_p = false;
18363 bool global_scope_p;
18364 tree decl;
18365 tree identifier;
18366 tree qscope;
18367 int oldcount = errorcount;
18368 cp_token *diag_token = NULL;
18369
18370 if (access_declaration_p)
18371 {
18372 diag_token = cp_lexer_peek_token (parser->lexer);
18373 cp_parser_parse_tentatively (parser);
18374 }
18375 else
18376 {
18377 /* Look for the `using' keyword. */
18378 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18379
18380 again:
18381 /* Peek at the next token. */
18382 token = cp_lexer_peek_token (parser->lexer);
18383 /* See if it's `typename'. */
18384 if (token->keyword == RID_TYPENAME)
18385 {
18386 /* Remember that we've seen it. */
18387 typename_p = true;
18388 /* Consume the `typename' token. */
18389 cp_lexer_consume_token (parser->lexer);
18390 }
18391 }
18392
18393 /* Look for the optional global scope qualification. */
18394 global_scope_p
18395 = (cp_parser_global_scope_opt (parser,
18396 /*current_scope_valid_p=*/false)
18397 != NULL_TREE);
18398
18399 /* If we saw `typename', or didn't see `::', then there must be a
18400 nested-name-specifier present. */
18401 if (typename_p || !global_scope_p)
18402 {
18403 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18404 /*check_dependency_p=*/true,
18405 /*type_p=*/false,
18406 /*is_declaration=*/true);
18407 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18408 {
18409 cp_parser_skip_to_end_of_block_or_statement (parser);
18410 return false;
18411 }
18412 }
18413 /* Otherwise, we could be in either of the two productions. In that
18414 case, treat the nested-name-specifier as optional. */
18415 else
18416 qscope = cp_parser_nested_name_specifier_opt (parser,
18417 /*typename_keyword_p=*/false,
18418 /*check_dependency_p=*/true,
18419 /*type_p=*/false,
18420 /*is_declaration=*/true);
18421 if (!qscope)
18422 qscope = global_namespace;
18423 else if (UNSCOPED_ENUM_P (qscope))
18424 qscope = CP_TYPE_CONTEXT (qscope);
18425
18426 if (access_declaration_p && cp_parser_error_occurred (parser))
18427 /* Something has already gone wrong; there's no need to parse
18428 further. Since an error has occurred, the return value of
18429 cp_parser_parse_definitely will be false, as required. */
18430 return cp_parser_parse_definitely (parser);
18431
18432 token = cp_lexer_peek_token (parser->lexer);
18433 /* Parse the unqualified-id. */
18434 identifier = cp_parser_unqualified_id (parser,
18435 /*template_keyword_p=*/false,
18436 /*check_dependency_p=*/true,
18437 /*declarator_p=*/true,
18438 /*optional_p=*/false);
18439
18440 if (access_declaration_p)
18441 {
18442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18443 cp_parser_simulate_error (parser);
18444 if (!cp_parser_parse_definitely (parser))
18445 return false;
18446 }
18447 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18448 {
18449 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18450 if (cxx_dialect < cxx1z
18451 && !in_system_header_at (ell->location))
18452 pedwarn (ell->location, 0,
18453 "pack expansion in using-declaration only available "
18454 "with -std=c++1z or -std=gnu++1z");
18455 qscope = make_pack_expansion (qscope);
18456 }
18457
18458 /* The function we call to handle a using-declaration is different
18459 depending on what scope we are in. */
18460 if (qscope == error_mark_node || identifier == error_mark_node)
18461 ;
18462 else if (!identifier_p (identifier)
18463 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18464 /* [namespace.udecl]
18465
18466 A using declaration shall not name a template-id. */
18467 error_at (token->location,
18468 "a template-id may not appear in a using-declaration");
18469 else
18470 {
18471 if (at_class_scope_p ())
18472 {
18473 /* Create the USING_DECL. */
18474 decl = do_class_using_decl (qscope, identifier);
18475
18476 if (decl && typename_p)
18477 USING_DECL_TYPENAME_P (decl) = 1;
18478
18479 if (check_for_bare_parameter_packs (decl))
18480 {
18481 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18482 return false;
18483 }
18484 else
18485 /* Add it to the list of members in this class. */
18486 finish_member_declaration (decl);
18487 }
18488 else
18489 {
18490 decl = cp_parser_lookup_name_simple (parser,
18491 identifier,
18492 token->location);
18493 if (decl == error_mark_node)
18494 cp_parser_name_lookup_error (parser, identifier,
18495 decl, NLE_NULL,
18496 token->location);
18497 else if (check_for_bare_parameter_packs (decl))
18498 {
18499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18500 return false;
18501 }
18502 else if (!at_namespace_scope_p ())
18503 do_local_using_decl (decl, qscope, identifier);
18504 else
18505 do_toplevel_using_decl (decl, qscope, identifier);
18506 }
18507 }
18508
18509 if (!access_declaration_p
18510 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18511 {
18512 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18513 if (cxx_dialect < cxx1z)
18514 pedwarn (comma->location, 0,
18515 "comma-separated list in using-declaration only available "
18516 "with -std=c++1z or -std=gnu++1z");
18517 goto again;
18518 }
18519
18520 /* Look for the final `;'. */
18521 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18522
18523 if (access_declaration_p && errorcount == oldcount)
18524 warning_at (diag_token->location, OPT_Wdeprecated,
18525 "access declarations are deprecated "
18526 "in favour of using-declarations; "
18527 "suggestion: add the %<using%> keyword");
18528
18529 return true;
18530 }
18531
18532 /* Parse an alias-declaration.
18533
18534 alias-declaration:
18535 using identifier attribute-specifier-seq [opt] = type-id */
18536
18537 static tree
18538 cp_parser_alias_declaration (cp_parser* parser)
18539 {
18540 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18541 location_t id_location;
18542 cp_declarator *declarator;
18543 cp_decl_specifier_seq decl_specs;
18544 bool member_p;
18545 const char *saved_message = NULL;
18546
18547 /* Look for the `using' keyword. */
18548 cp_token *using_token
18549 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18550 if (using_token == NULL)
18551 return error_mark_node;
18552
18553 id_location = cp_lexer_peek_token (parser->lexer)->location;
18554 id = cp_parser_identifier (parser);
18555 if (id == error_mark_node)
18556 return error_mark_node;
18557
18558 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18559 attributes = cp_parser_attributes_opt (parser);
18560 if (attributes == error_mark_node)
18561 return error_mark_node;
18562
18563 cp_parser_require (parser, CPP_EQ, RT_EQ);
18564
18565 if (cp_parser_error_occurred (parser))
18566 return error_mark_node;
18567
18568 cp_parser_commit_to_tentative_parse (parser);
18569
18570 /* Now we are going to parse the type-id of the declaration. */
18571
18572 /*
18573 [dcl.type]/3 says:
18574
18575 "A type-specifier-seq shall not define a class or enumeration
18576 unless it appears in the type-id of an alias-declaration (7.1.3) that
18577 is not the declaration of a template-declaration."
18578
18579 In other words, if we currently are in an alias template, the
18580 type-id should not define a type.
18581
18582 So let's set parser->type_definition_forbidden_message in that
18583 case; cp_parser_check_type_definition (called by
18584 cp_parser_class_specifier) will then emit an error if a type is
18585 defined in the type-id. */
18586 if (parser->num_template_parameter_lists)
18587 {
18588 saved_message = parser->type_definition_forbidden_message;
18589 parser->type_definition_forbidden_message =
18590 G_("types may not be defined in alias template declarations");
18591 }
18592
18593 type = cp_parser_type_id (parser);
18594
18595 /* Restore the error message if need be. */
18596 if (parser->num_template_parameter_lists)
18597 parser->type_definition_forbidden_message = saved_message;
18598
18599 if (type == error_mark_node
18600 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18601 {
18602 cp_parser_skip_to_end_of_block_or_statement (parser);
18603 return error_mark_node;
18604 }
18605
18606 /* A typedef-name can also be introduced by an alias-declaration. The
18607 identifier following the using keyword becomes a typedef-name. It has
18608 the same semantics as if it were introduced by the typedef
18609 specifier. In particular, it does not define a new type and it shall
18610 not appear in the type-id. */
18611
18612 clear_decl_specs (&decl_specs);
18613 decl_specs.type = type;
18614 if (attributes != NULL_TREE)
18615 {
18616 decl_specs.attributes = attributes;
18617 set_and_check_decl_spec_loc (&decl_specs,
18618 ds_attribute,
18619 attrs_token);
18620 }
18621 set_and_check_decl_spec_loc (&decl_specs,
18622 ds_typedef,
18623 using_token);
18624 set_and_check_decl_spec_loc (&decl_specs,
18625 ds_alias,
18626 using_token);
18627
18628 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18629 declarator->id_loc = id_location;
18630
18631 member_p = at_class_scope_p ();
18632 if (member_p)
18633 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18634 NULL_TREE, attributes);
18635 else
18636 decl = start_decl (declarator, &decl_specs, 0,
18637 attributes, NULL_TREE, &pushed_scope);
18638 if (decl == error_mark_node)
18639 return decl;
18640
18641 // Attach constraints to the alias declaration.
18642 if (flag_concepts && current_template_parms)
18643 {
18644 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18645 tree constr = build_constraints (reqs, NULL_TREE);
18646 set_constraints (decl, constr);
18647 }
18648
18649 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18650
18651 if (pushed_scope)
18652 pop_scope (pushed_scope);
18653
18654 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18655 added into the symbol table; otherwise, return the TYPE_DECL. */
18656 if (DECL_LANG_SPECIFIC (decl)
18657 && DECL_TEMPLATE_INFO (decl)
18658 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18659 {
18660 decl = DECL_TI_TEMPLATE (decl);
18661 if (member_p)
18662 check_member_template (decl);
18663 }
18664
18665 return decl;
18666 }
18667
18668 /* Parse a using-directive.
18669
18670 using-directive:
18671 using namespace :: [opt] nested-name-specifier [opt]
18672 namespace-name ; */
18673
18674 static void
18675 cp_parser_using_directive (cp_parser* parser)
18676 {
18677 tree namespace_decl;
18678 tree attribs;
18679
18680 /* Look for the `using' keyword. */
18681 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18682 /* And the `namespace' keyword. */
18683 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18684 /* Look for the optional `::' operator. */
18685 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18686 /* And the optional nested-name-specifier. */
18687 cp_parser_nested_name_specifier_opt (parser,
18688 /*typename_keyword_p=*/false,
18689 /*check_dependency_p=*/true,
18690 /*type_p=*/false,
18691 /*is_declaration=*/true);
18692 /* Get the namespace being used. */
18693 namespace_decl = cp_parser_namespace_name (parser);
18694 /* And any specified attributes. */
18695 attribs = cp_parser_attributes_opt (parser);
18696 /* Update the symbol table. */
18697 parse_using_directive (namespace_decl, attribs);
18698 /* Look for the final `;'. */
18699 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18700 }
18701
18702 /* Parse an asm-definition.
18703
18704 asm-definition:
18705 asm ( string-literal ) ;
18706
18707 GNU Extension:
18708
18709 asm-definition:
18710 asm volatile [opt] ( string-literal ) ;
18711 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18712 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18713 : asm-operand-list [opt] ) ;
18714 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18715 : asm-operand-list [opt]
18716 : asm-clobber-list [opt] ) ;
18717 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18718 : asm-clobber-list [opt]
18719 : asm-goto-list ) ; */
18720
18721 static void
18722 cp_parser_asm_definition (cp_parser* parser)
18723 {
18724 tree string;
18725 tree outputs = NULL_TREE;
18726 tree inputs = NULL_TREE;
18727 tree clobbers = NULL_TREE;
18728 tree labels = NULL_TREE;
18729 tree asm_stmt;
18730 bool volatile_p = false;
18731 bool extended_p = false;
18732 bool invalid_inputs_p = false;
18733 bool invalid_outputs_p = false;
18734 bool goto_p = false;
18735 required_token missing = RT_NONE;
18736
18737 /* Look for the `asm' keyword. */
18738 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18739
18740 if (parser->in_function_body
18741 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18742 {
18743 error ("%<asm%> in %<constexpr%> function");
18744 cp_function_chain->invalid_constexpr = true;
18745 }
18746
18747 /* See if the next token is `volatile'. */
18748 if (cp_parser_allow_gnu_extensions_p (parser)
18749 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18750 {
18751 /* Remember that we saw the `volatile' keyword. */
18752 volatile_p = true;
18753 /* Consume the token. */
18754 cp_lexer_consume_token (parser->lexer);
18755 }
18756 if (cp_parser_allow_gnu_extensions_p (parser)
18757 && parser->in_function_body
18758 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18759 {
18760 /* Remember that we saw the `goto' keyword. */
18761 goto_p = true;
18762 /* Consume the token. */
18763 cp_lexer_consume_token (parser->lexer);
18764 }
18765 /* Look for the opening `('. */
18766 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18767 return;
18768 /* Look for the string. */
18769 string = cp_parser_string_literal (parser, false, false);
18770 if (string == error_mark_node)
18771 {
18772 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18773 /*consume_paren=*/true);
18774 return;
18775 }
18776
18777 /* If we're allowing GNU extensions, check for the extended assembly
18778 syntax. Unfortunately, the `:' tokens need not be separated by
18779 a space in C, and so, for compatibility, we tolerate that here
18780 too. Doing that means that we have to treat the `::' operator as
18781 two `:' tokens. */
18782 if (cp_parser_allow_gnu_extensions_p (parser)
18783 && parser->in_function_body
18784 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18785 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18786 {
18787 bool inputs_p = false;
18788 bool clobbers_p = false;
18789 bool labels_p = false;
18790
18791 /* The extended syntax was used. */
18792 extended_p = true;
18793
18794 /* Look for outputs. */
18795 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18796 {
18797 /* Consume the `:'. */
18798 cp_lexer_consume_token (parser->lexer);
18799 /* Parse the output-operands. */
18800 if (cp_lexer_next_token_is_not (parser->lexer,
18801 CPP_COLON)
18802 && cp_lexer_next_token_is_not (parser->lexer,
18803 CPP_SCOPE)
18804 && cp_lexer_next_token_is_not (parser->lexer,
18805 CPP_CLOSE_PAREN)
18806 && !goto_p)
18807 {
18808 outputs = cp_parser_asm_operand_list (parser);
18809 if (outputs == error_mark_node)
18810 invalid_outputs_p = true;
18811 }
18812 }
18813 /* If the next token is `::', there are no outputs, and the
18814 next token is the beginning of the inputs. */
18815 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18816 /* The inputs are coming next. */
18817 inputs_p = true;
18818
18819 /* Look for inputs. */
18820 if (inputs_p
18821 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18822 {
18823 /* Consume the `:' or `::'. */
18824 cp_lexer_consume_token (parser->lexer);
18825 /* Parse the output-operands. */
18826 if (cp_lexer_next_token_is_not (parser->lexer,
18827 CPP_COLON)
18828 && cp_lexer_next_token_is_not (parser->lexer,
18829 CPP_SCOPE)
18830 && cp_lexer_next_token_is_not (parser->lexer,
18831 CPP_CLOSE_PAREN))
18832 {
18833 inputs = cp_parser_asm_operand_list (parser);
18834 if (inputs == error_mark_node)
18835 invalid_inputs_p = true;
18836 }
18837 }
18838 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18839 /* The clobbers are coming next. */
18840 clobbers_p = true;
18841
18842 /* Look for clobbers. */
18843 if (clobbers_p
18844 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18845 {
18846 clobbers_p = true;
18847 /* Consume the `:' or `::'. */
18848 cp_lexer_consume_token (parser->lexer);
18849 /* Parse the clobbers. */
18850 if (cp_lexer_next_token_is_not (parser->lexer,
18851 CPP_COLON)
18852 && cp_lexer_next_token_is_not (parser->lexer,
18853 CPP_CLOSE_PAREN))
18854 clobbers = cp_parser_asm_clobber_list (parser);
18855 }
18856 else if (goto_p
18857 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18858 /* The labels are coming next. */
18859 labels_p = true;
18860
18861 /* Look for labels. */
18862 if (labels_p
18863 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18864 {
18865 labels_p = true;
18866 /* Consume the `:' or `::'. */
18867 cp_lexer_consume_token (parser->lexer);
18868 /* Parse the labels. */
18869 labels = cp_parser_asm_label_list (parser);
18870 }
18871
18872 if (goto_p && !labels_p)
18873 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18874 }
18875 else if (goto_p)
18876 missing = RT_COLON_SCOPE;
18877
18878 /* Look for the closing `)'. */
18879 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18880 missing ? missing : RT_CLOSE_PAREN))
18881 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18882 /*consume_paren=*/true);
18883 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18884
18885 if (!invalid_inputs_p && !invalid_outputs_p)
18886 {
18887 /* Create the ASM_EXPR. */
18888 if (parser->in_function_body)
18889 {
18890 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18891 inputs, clobbers, labels);
18892 /* If the extended syntax was not used, mark the ASM_EXPR. */
18893 if (!extended_p)
18894 {
18895 tree temp = asm_stmt;
18896 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18897 temp = TREE_OPERAND (temp, 0);
18898
18899 ASM_INPUT_P (temp) = 1;
18900 }
18901 }
18902 else
18903 symtab->finalize_toplevel_asm (string);
18904 }
18905 }
18906
18907 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18908 type that comes from the decl-specifier-seq. */
18909
18910 static tree
18911 strip_declarator_types (tree type, cp_declarator *declarator)
18912 {
18913 for (cp_declarator *d = declarator; d;)
18914 switch (d->kind)
18915 {
18916 case cdk_id:
18917 case cdk_decomp:
18918 case cdk_error:
18919 d = NULL;
18920 break;
18921
18922 default:
18923 if (TYPE_PTRMEMFUNC_P (type))
18924 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18925 type = TREE_TYPE (type);
18926 d = d->declarator;
18927 break;
18928 }
18929
18930 return type;
18931 }
18932
18933 /* Declarators [gram.dcl.decl] */
18934
18935 /* Parse an init-declarator.
18936
18937 init-declarator:
18938 declarator initializer [opt]
18939
18940 GNU Extension:
18941
18942 init-declarator:
18943 declarator asm-specification [opt] attributes [opt] initializer [opt]
18944
18945 function-definition:
18946 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18947 function-body
18948 decl-specifier-seq [opt] declarator function-try-block
18949
18950 GNU Extension:
18951
18952 function-definition:
18953 __extension__ function-definition
18954
18955 TM Extension:
18956
18957 function-definition:
18958 decl-specifier-seq [opt] declarator function-transaction-block
18959
18960 The DECL_SPECIFIERS apply to this declarator. Returns a
18961 representation of the entity declared. If MEMBER_P is TRUE, then
18962 this declarator appears in a class scope. The new DECL created by
18963 this declarator is returned.
18964
18965 The CHECKS are access checks that should be performed once we know
18966 what entity is being declared (and, therefore, what classes have
18967 befriended it).
18968
18969 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18970 for a function-definition here as well. If the declarator is a
18971 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18972 be TRUE upon return. By that point, the function-definition will
18973 have been completely parsed.
18974
18975 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18976 is FALSE.
18977
18978 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18979 parsed declaration if it is an uninitialized single declarator not followed
18980 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18981 if present, will not be consumed. If returned, this declarator will be
18982 created with SD_INITIALIZED but will not call cp_finish_decl.
18983
18984 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18985 and there is an initializer, the pointed location_t is set to the
18986 location of the '=' or `(', or '{' in C++11 token introducing the
18987 initializer. */
18988
18989 static tree
18990 cp_parser_init_declarator (cp_parser* parser,
18991 cp_decl_specifier_seq *decl_specifiers,
18992 vec<deferred_access_check, va_gc> *checks,
18993 bool function_definition_allowed_p,
18994 bool member_p,
18995 int declares_class_or_enum,
18996 bool* function_definition_p,
18997 tree* maybe_range_for_decl,
18998 location_t* init_loc,
18999 tree* auto_result)
19000 {
19001 cp_token *token = NULL, *asm_spec_start_token = NULL,
19002 *attributes_start_token = NULL;
19003 cp_declarator *declarator;
19004 tree prefix_attributes;
19005 tree attributes = NULL;
19006 tree asm_specification;
19007 tree initializer;
19008 tree decl = NULL_TREE;
19009 tree scope;
19010 int is_initialized;
19011 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19012 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19013 "(...)". */
19014 enum cpp_ttype initialization_kind;
19015 bool is_direct_init = false;
19016 bool is_non_constant_init;
19017 int ctor_dtor_or_conv_p;
19018 bool friend_p = cp_parser_friend_p (decl_specifiers);
19019 tree pushed_scope = NULL_TREE;
19020 bool range_for_decl_p = false;
19021 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19022 location_t tmp_init_loc = UNKNOWN_LOCATION;
19023
19024 /* Gather the attributes that were provided with the
19025 decl-specifiers. */
19026 prefix_attributes = decl_specifiers->attributes;
19027
19028 /* Assume that this is not the declarator for a function
19029 definition. */
19030 if (function_definition_p)
19031 *function_definition_p = false;
19032
19033 /* Default arguments are only permitted for function parameters. */
19034 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19035 parser->default_arg_ok_p = false;
19036
19037 /* Defer access checks while parsing the declarator; we cannot know
19038 what names are accessible until we know what is being
19039 declared. */
19040 resume_deferring_access_checks ();
19041
19042 token = cp_lexer_peek_token (parser->lexer);
19043
19044 /* Parse the declarator. */
19045 declarator
19046 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19047 &ctor_dtor_or_conv_p,
19048 /*parenthesized_p=*/NULL,
19049 member_p, friend_p);
19050 /* Gather up the deferred checks. */
19051 stop_deferring_access_checks ();
19052
19053 parser->default_arg_ok_p = saved_default_arg_ok_p;
19054
19055 /* If the DECLARATOR was erroneous, there's no need to go
19056 further. */
19057 if (declarator == cp_error_declarator)
19058 return error_mark_node;
19059
19060 /* Check that the number of template-parameter-lists is OK. */
19061 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19062 token->location))
19063 return error_mark_node;
19064
19065 if (declares_class_or_enum & 2)
19066 cp_parser_check_for_definition_in_return_type (declarator,
19067 decl_specifiers->type,
19068 decl_specifiers->locations[ds_type_spec]);
19069
19070 /* Figure out what scope the entity declared by the DECLARATOR is
19071 located in. `grokdeclarator' sometimes changes the scope, so
19072 we compute it now. */
19073 scope = get_scope_of_declarator (declarator);
19074
19075 /* Perform any lookups in the declared type which were thought to be
19076 dependent, but are not in the scope of the declarator. */
19077 decl_specifiers->type
19078 = maybe_update_decl_type (decl_specifiers->type, scope);
19079
19080 /* If we're allowing GNU extensions, look for an
19081 asm-specification. */
19082 if (cp_parser_allow_gnu_extensions_p (parser))
19083 {
19084 /* Look for an asm-specification. */
19085 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19086 asm_specification = cp_parser_asm_specification_opt (parser);
19087 }
19088 else
19089 asm_specification = NULL_TREE;
19090
19091 /* Look for attributes. */
19092 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19093 attributes = cp_parser_attributes_opt (parser);
19094
19095 /* Peek at the next token. */
19096 token = cp_lexer_peek_token (parser->lexer);
19097
19098 bool bogus_implicit_tmpl = false;
19099
19100 if (function_declarator_p (declarator))
19101 {
19102 /* Handle C++17 deduction guides. */
19103 if (!decl_specifiers->type
19104 && ctor_dtor_or_conv_p <= 0
19105 && cxx_dialect >= cxx1z)
19106 {
19107 cp_declarator *id = get_id_declarator (declarator);
19108 tree name = id->u.id.unqualified_name;
19109 parser->scope = id->u.id.qualifying_scope;
19110 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19111 if (tmpl
19112 && (DECL_CLASS_TEMPLATE_P (tmpl)
19113 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19114 {
19115 id->u.id.unqualified_name = dguide_name (tmpl);
19116 id->u.id.sfk = sfk_deduction_guide;
19117 ctor_dtor_or_conv_p = 1;
19118 }
19119 }
19120
19121 /* Check to see if the token indicates the start of a
19122 function-definition. */
19123 if (cp_parser_token_starts_function_definition_p (token))
19124 {
19125 if (!function_definition_allowed_p)
19126 {
19127 /* If a function-definition should not appear here, issue an
19128 error message. */
19129 cp_parser_error (parser,
19130 "a function-definition is not allowed here");
19131 return error_mark_node;
19132 }
19133
19134 location_t func_brace_location
19135 = cp_lexer_peek_token (parser->lexer)->location;
19136
19137 /* Neither attributes nor an asm-specification are allowed
19138 on a function-definition. */
19139 if (asm_specification)
19140 error_at (asm_spec_start_token->location,
19141 "an asm-specification is not allowed "
19142 "on a function-definition");
19143 if (attributes)
19144 error_at (attributes_start_token->location,
19145 "attributes are not allowed "
19146 "on a function-definition");
19147 /* This is a function-definition. */
19148 *function_definition_p = true;
19149
19150 /* Parse the function definition. */
19151 if (member_p)
19152 decl = cp_parser_save_member_function_body (parser,
19153 decl_specifiers,
19154 declarator,
19155 prefix_attributes);
19156 else
19157 decl =
19158 (cp_parser_function_definition_from_specifiers_and_declarator
19159 (parser, decl_specifiers, prefix_attributes, declarator));
19160
19161 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19162 {
19163 /* This is where the prologue starts... */
19164 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19165 = func_brace_location;
19166 }
19167
19168 return decl;
19169 }
19170 }
19171 else if (parser->fully_implicit_function_template_p)
19172 {
19173 /* A non-template declaration involving a function parameter list
19174 containing an implicit template parameter will be made into a
19175 template. If the resulting declaration is not going to be an
19176 actual function then finish the template scope here to prevent it.
19177 An error message will be issued once we have a decl to talk about.
19178
19179 FIXME probably we should do type deduction rather than create an
19180 implicit template, but the standard currently doesn't allow it. */
19181 bogus_implicit_tmpl = true;
19182 finish_fully_implicit_template (parser, NULL_TREE);
19183 }
19184
19185 /* [dcl.dcl]
19186
19187 Only in function declarations for constructors, destructors, type
19188 conversions, and deduction guides can the decl-specifier-seq be omitted.
19189
19190 We explicitly postpone this check past the point where we handle
19191 function-definitions because we tolerate function-definitions
19192 that are missing their return types in some modes. */
19193 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19194 {
19195 cp_parser_error (parser,
19196 "expected constructor, destructor, or type conversion");
19197 return error_mark_node;
19198 }
19199
19200 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19201 if (token->type == CPP_EQ
19202 || token->type == CPP_OPEN_PAREN
19203 || token->type == CPP_OPEN_BRACE)
19204 {
19205 is_initialized = SD_INITIALIZED;
19206 initialization_kind = token->type;
19207 if (maybe_range_for_decl)
19208 *maybe_range_for_decl = error_mark_node;
19209 tmp_init_loc = token->location;
19210 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19211 *init_loc = tmp_init_loc;
19212
19213 if (token->type == CPP_EQ
19214 && function_declarator_p (declarator))
19215 {
19216 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19217 if (t2->keyword == RID_DEFAULT)
19218 is_initialized = SD_DEFAULTED;
19219 else if (t2->keyword == RID_DELETE)
19220 is_initialized = SD_DELETED;
19221 }
19222 }
19223 else
19224 {
19225 /* If the init-declarator isn't initialized and isn't followed by a
19226 `,' or `;', it's not a valid init-declarator. */
19227 if (token->type != CPP_COMMA
19228 && token->type != CPP_SEMICOLON)
19229 {
19230 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19231 range_for_decl_p = true;
19232 else
19233 {
19234 if (!maybe_range_for_decl)
19235 cp_parser_error (parser, "expected initializer");
19236 return error_mark_node;
19237 }
19238 }
19239 is_initialized = SD_UNINITIALIZED;
19240 initialization_kind = CPP_EOF;
19241 }
19242
19243 /* Because start_decl has side-effects, we should only call it if we
19244 know we're going ahead. By this point, we know that we cannot
19245 possibly be looking at any other construct. */
19246 cp_parser_commit_to_tentative_parse (parser);
19247
19248 /* Enter the newly declared entry in the symbol table. If we're
19249 processing a declaration in a class-specifier, we wait until
19250 after processing the initializer. */
19251 if (!member_p)
19252 {
19253 if (parser->in_unbraced_linkage_specification_p)
19254 decl_specifiers->storage_class = sc_extern;
19255 decl = start_decl (declarator, decl_specifiers,
19256 range_for_decl_p? SD_INITIALIZED : is_initialized,
19257 attributes, prefix_attributes, &pushed_scope);
19258 cp_finalize_omp_declare_simd (parser, decl);
19259 cp_finalize_oacc_routine (parser, decl, false);
19260 /* Adjust location of decl if declarator->id_loc is more appropriate:
19261 set, and decl wasn't merged with another decl, in which case its
19262 location would be different from input_location, and more accurate. */
19263 if (DECL_P (decl)
19264 && declarator->id_loc != UNKNOWN_LOCATION
19265 && DECL_SOURCE_LOCATION (decl) == input_location)
19266 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19267 }
19268 else if (scope)
19269 /* Enter the SCOPE. That way unqualified names appearing in the
19270 initializer will be looked up in SCOPE. */
19271 pushed_scope = push_scope (scope);
19272
19273 /* Perform deferred access control checks, now that we know in which
19274 SCOPE the declared entity resides. */
19275 if (!member_p && decl)
19276 {
19277 tree saved_current_function_decl = NULL_TREE;
19278
19279 /* If the entity being declared is a function, pretend that we
19280 are in its scope. If it is a `friend', it may have access to
19281 things that would not otherwise be accessible. */
19282 if (TREE_CODE (decl) == FUNCTION_DECL)
19283 {
19284 saved_current_function_decl = current_function_decl;
19285 current_function_decl = decl;
19286 }
19287
19288 /* Perform access checks for template parameters. */
19289 cp_parser_perform_template_parameter_access_checks (checks);
19290
19291 /* Perform the access control checks for the declarator and the
19292 decl-specifiers. */
19293 perform_deferred_access_checks (tf_warning_or_error);
19294
19295 /* Restore the saved value. */
19296 if (TREE_CODE (decl) == FUNCTION_DECL)
19297 current_function_decl = saved_current_function_decl;
19298 }
19299
19300 /* Parse the initializer. */
19301 initializer = NULL_TREE;
19302 is_direct_init = false;
19303 is_non_constant_init = true;
19304 if (is_initialized)
19305 {
19306 if (function_declarator_p (declarator))
19307 {
19308 if (initialization_kind == CPP_EQ)
19309 initializer = cp_parser_pure_specifier (parser);
19310 else
19311 {
19312 /* If the declaration was erroneous, we don't really
19313 know what the user intended, so just silently
19314 consume the initializer. */
19315 if (decl != error_mark_node)
19316 error_at (tmp_init_loc, "initializer provided for function");
19317 cp_parser_skip_to_closing_parenthesis (parser,
19318 /*recovering=*/true,
19319 /*or_comma=*/false,
19320 /*consume_paren=*/true);
19321 }
19322 }
19323 else
19324 {
19325 /* We want to record the extra mangling scope for in-class
19326 initializers of class members and initializers of static data
19327 member templates. The former involves deferring
19328 parsing of the initializer until end of class as with default
19329 arguments. So right here we only handle the latter. */
19330 if (!member_p && processing_template_decl)
19331 start_lambda_scope (decl);
19332 initializer = cp_parser_initializer (parser,
19333 &is_direct_init,
19334 &is_non_constant_init);
19335 if (!member_p && processing_template_decl)
19336 finish_lambda_scope ();
19337 if (initializer == error_mark_node)
19338 cp_parser_skip_to_end_of_statement (parser);
19339 }
19340 }
19341
19342 /* The old parser allows attributes to appear after a parenthesized
19343 initializer. Mark Mitchell proposed removing this functionality
19344 on the GCC mailing lists on 2002-08-13. This parser accepts the
19345 attributes -- but ignores them. */
19346 if (cp_parser_allow_gnu_extensions_p (parser)
19347 && initialization_kind == CPP_OPEN_PAREN)
19348 if (cp_parser_attributes_opt (parser))
19349 warning (OPT_Wattributes,
19350 "attributes after parenthesized initializer ignored");
19351
19352 /* And now complain about a non-function implicit template. */
19353 if (bogus_implicit_tmpl && decl != error_mark_node)
19354 error_at (DECL_SOURCE_LOCATION (decl),
19355 "non-function %qD declared as implicit template", decl);
19356
19357 /* For an in-class declaration, use `grokfield' to create the
19358 declaration. */
19359 if (member_p)
19360 {
19361 if (pushed_scope)
19362 {
19363 pop_scope (pushed_scope);
19364 pushed_scope = NULL_TREE;
19365 }
19366 decl = grokfield (declarator, decl_specifiers,
19367 initializer, !is_non_constant_init,
19368 /*asmspec=*/NULL_TREE,
19369 chainon (attributes, prefix_attributes));
19370 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19371 cp_parser_save_default_args (parser, decl);
19372 cp_finalize_omp_declare_simd (parser, decl);
19373 cp_finalize_oacc_routine (parser, decl, false);
19374 }
19375
19376 /* Finish processing the declaration. But, skip member
19377 declarations. */
19378 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19379 {
19380 cp_finish_decl (decl,
19381 initializer, !is_non_constant_init,
19382 asm_specification,
19383 /* If the initializer is in parentheses, then this is
19384 a direct-initialization, which means that an
19385 `explicit' constructor is OK. Otherwise, an
19386 `explicit' constructor cannot be used. */
19387 ((is_direct_init || !is_initialized)
19388 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19389 }
19390 else if ((cxx_dialect != cxx98) && friend_p
19391 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19392 /* Core issue #226 (C++0x only): A default template-argument
19393 shall not be specified in a friend class template
19394 declaration. */
19395 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19396 /*is_partial=*/false, /*is_friend_decl=*/1);
19397
19398 if (!friend_p && pushed_scope)
19399 pop_scope (pushed_scope);
19400
19401 if (function_declarator_p (declarator)
19402 && parser->fully_implicit_function_template_p)
19403 {
19404 if (member_p)
19405 decl = finish_fully_implicit_template (parser, decl);
19406 else
19407 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19408 }
19409
19410 if (auto_result && is_initialized && decl_specifiers->type
19411 && type_uses_auto (decl_specifiers->type))
19412 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19413
19414 return decl;
19415 }
19416
19417 /* Parse a declarator.
19418
19419 declarator:
19420 direct-declarator
19421 ptr-operator declarator
19422
19423 abstract-declarator:
19424 ptr-operator abstract-declarator [opt]
19425 direct-abstract-declarator
19426
19427 GNU Extensions:
19428
19429 declarator:
19430 attributes [opt] direct-declarator
19431 attributes [opt] ptr-operator declarator
19432
19433 abstract-declarator:
19434 attributes [opt] ptr-operator abstract-declarator [opt]
19435 attributes [opt] direct-abstract-declarator
19436
19437 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19438 detect constructors, destructors, deduction guides, or conversion operators.
19439 It is set to -1 if the declarator is a name, and +1 if it is a
19440 function. Otherwise it is set to zero. Usually you just want to
19441 test for >0, but internally the negative value is used.
19442
19443 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19444 a decl-specifier-seq unless it declares a constructor, destructor,
19445 or conversion. It might seem that we could check this condition in
19446 semantic analysis, rather than parsing, but that makes it difficult
19447 to handle something like `f()'. We want to notice that there are
19448 no decl-specifiers, and therefore realize that this is an
19449 expression, not a declaration.)
19450
19451 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19452 the declarator is a direct-declarator of the form "(...)".
19453
19454 MEMBER_P is true iff this declarator is a member-declarator.
19455
19456 FRIEND_P is true iff this declarator is a friend. */
19457
19458 static cp_declarator *
19459 cp_parser_declarator (cp_parser* parser,
19460 cp_parser_declarator_kind dcl_kind,
19461 int* ctor_dtor_or_conv_p,
19462 bool* parenthesized_p,
19463 bool member_p, bool friend_p)
19464 {
19465 cp_declarator *declarator;
19466 enum tree_code code;
19467 cp_cv_quals cv_quals;
19468 tree class_type;
19469 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19470
19471 /* Assume this is not a constructor, destructor, or type-conversion
19472 operator. */
19473 if (ctor_dtor_or_conv_p)
19474 *ctor_dtor_or_conv_p = 0;
19475
19476 if (cp_parser_allow_gnu_extensions_p (parser))
19477 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19478
19479 /* Check for the ptr-operator production. */
19480 cp_parser_parse_tentatively (parser);
19481 /* Parse the ptr-operator. */
19482 code = cp_parser_ptr_operator (parser,
19483 &class_type,
19484 &cv_quals,
19485 &std_attributes);
19486
19487 /* If that worked, then we have a ptr-operator. */
19488 if (cp_parser_parse_definitely (parser))
19489 {
19490 /* If a ptr-operator was found, then this declarator was not
19491 parenthesized. */
19492 if (parenthesized_p)
19493 *parenthesized_p = true;
19494 /* The dependent declarator is optional if we are parsing an
19495 abstract-declarator. */
19496 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19497 cp_parser_parse_tentatively (parser);
19498
19499 /* Parse the dependent declarator. */
19500 declarator = cp_parser_declarator (parser, dcl_kind,
19501 /*ctor_dtor_or_conv_p=*/NULL,
19502 /*parenthesized_p=*/NULL,
19503 /*member_p=*/false,
19504 friend_p);
19505
19506 /* If we are parsing an abstract-declarator, we must handle the
19507 case where the dependent declarator is absent. */
19508 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19509 && !cp_parser_parse_definitely (parser))
19510 declarator = NULL;
19511
19512 declarator = cp_parser_make_indirect_declarator
19513 (code, class_type, cv_quals, declarator, std_attributes);
19514 }
19515 /* Everything else is a direct-declarator. */
19516 else
19517 {
19518 if (parenthesized_p)
19519 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19520 CPP_OPEN_PAREN);
19521 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19522 ctor_dtor_or_conv_p,
19523 member_p, friend_p);
19524 }
19525
19526 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19527 declarator->attributes = gnu_attributes;
19528 return declarator;
19529 }
19530
19531 /* Parse a direct-declarator or direct-abstract-declarator.
19532
19533 direct-declarator:
19534 declarator-id
19535 direct-declarator ( parameter-declaration-clause )
19536 cv-qualifier-seq [opt]
19537 ref-qualifier [opt]
19538 exception-specification [opt]
19539 direct-declarator [ constant-expression [opt] ]
19540 ( declarator )
19541
19542 direct-abstract-declarator:
19543 direct-abstract-declarator [opt]
19544 ( parameter-declaration-clause )
19545 cv-qualifier-seq [opt]
19546 ref-qualifier [opt]
19547 exception-specification [opt]
19548 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19549 ( abstract-declarator )
19550
19551 Returns a representation of the declarator. DCL_KIND is
19552 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19553 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19554 we are parsing a direct-declarator. It is
19555 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19556 of ambiguity we prefer an abstract declarator, as per
19557 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19558 as for cp_parser_declarator. */
19559
19560 static cp_declarator *
19561 cp_parser_direct_declarator (cp_parser* parser,
19562 cp_parser_declarator_kind dcl_kind,
19563 int* ctor_dtor_or_conv_p,
19564 bool member_p, bool friend_p)
19565 {
19566 cp_token *token;
19567 cp_declarator *declarator = NULL;
19568 tree scope = NULL_TREE;
19569 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19570 bool saved_in_declarator_p = parser->in_declarator_p;
19571 bool first = true;
19572 tree pushed_scope = NULL_TREE;
19573
19574 while (true)
19575 {
19576 /* Peek at the next token. */
19577 token = cp_lexer_peek_token (parser->lexer);
19578 if (token->type == CPP_OPEN_PAREN)
19579 {
19580 /* This is either a parameter-declaration-clause, or a
19581 parenthesized declarator. When we know we are parsing a
19582 named declarator, it must be a parenthesized declarator
19583 if FIRST is true. For instance, `(int)' is a
19584 parameter-declaration-clause, with an omitted
19585 direct-abstract-declarator. But `((*))', is a
19586 parenthesized abstract declarator. Finally, when T is a
19587 template parameter `(T)' is a
19588 parameter-declaration-clause, and not a parenthesized
19589 named declarator.
19590
19591 We first try and parse a parameter-declaration-clause,
19592 and then try a nested declarator (if FIRST is true).
19593
19594 It is not an error for it not to be a
19595 parameter-declaration-clause, even when FIRST is
19596 false. Consider,
19597
19598 int i (int);
19599 int i (3);
19600
19601 The first is the declaration of a function while the
19602 second is the definition of a variable, including its
19603 initializer.
19604
19605 Having seen only the parenthesis, we cannot know which of
19606 these two alternatives should be selected. Even more
19607 complex are examples like:
19608
19609 int i (int (a));
19610 int i (int (3));
19611
19612 The former is a function-declaration; the latter is a
19613 variable initialization.
19614
19615 Thus again, we try a parameter-declaration-clause, and if
19616 that fails, we back out and return. */
19617
19618 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19619 {
19620 tree params;
19621 bool is_declarator = false;
19622
19623 /* In a member-declarator, the only valid interpretation
19624 of a parenthesis is the start of a
19625 parameter-declaration-clause. (It is invalid to
19626 initialize a static data member with a parenthesized
19627 initializer; only the "=" form of initialization is
19628 permitted.) */
19629 if (!member_p)
19630 cp_parser_parse_tentatively (parser);
19631
19632 /* Consume the `('. */
19633 cp_lexer_consume_token (parser->lexer);
19634 if (first)
19635 {
19636 /* If this is going to be an abstract declarator, we're
19637 in a declarator and we can't have default args. */
19638 parser->default_arg_ok_p = false;
19639 parser->in_declarator_p = true;
19640 }
19641
19642 begin_scope (sk_function_parms, NULL_TREE);
19643
19644 /* Parse the parameter-declaration-clause. */
19645 params = cp_parser_parameter_declaration_clause (parser);
19646
19647 /* Consume the `)'. */
19648 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19649
19650 /* If all went well, parse the cv-qualifier-seq,
19651 ref-qualifier and the exception-specification. */
19652 if (member_p || cp_parser_parse_definitely (parser))
19653 {
19654 cp_cv_quals cv_quals;
19655 cp_virt_specifiers virt_specifiers;
19656 cp_ref_qualifier ref_qual;
19657 tree exception_specification;
19658 tree late_return;
19659 tree attrs;
19660 bool memfn = (member_p || (pushed_scope
19661 && CLASS_TYPE_P (pushed_scope)));
19662
19663 is_declarator = true;
19664
19665 if (ctor_dtor_or_conv_p)
19666 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19667 first = false;
19668
19669 /* Parse the cv-qualifier-seq. */
19670 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19671 /* Parse the ref-qualifier. */
19672 ref_qual = cp_parser_ref_qualifier_opt (parser);
19673 /* Parse the tx-qualifier. */
19674 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19675 /* And the exception-specification. */
19676 exception_specification
19677 = cp_parser_exception_specification_opt (parser);
19678
19679 attrs = cp_parser_std_attribute_spec_seq (parser);
19680
19681 /* In here, we handle cases where attribute is used after
19682 the function declaration. For example:
19683 void func (int x) __attribute__((vector(..))); */
19684 tree gnu_attrs = NULL_TREE;
19685 if (flag_cilkplus
19686 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19687 {
19688 cp_parser_parse_tentatively (parser);
19689 tree attr = cp_parser_gnu_attributes_opt (parser);
19690 if (cp_lexer_next_token_is_not (parser->lexer,
19691 CPP_SEMICOLON)
19692 && cp_lexer_next_token_is_not (parser->lexer,
19693 CPP_OPEN_BRACE))
19694 cp_parser_abort_tentative_parse (parser);
19695 else if (!cp_parser_parse_definitely (parser))
19696 ;
19697 else
19698 gnu_attrs = attr;
19699 }
19700 tree requires_clause = NULL_TREE;
19701 late_return = (cp_parser_late_return_type_opt
19702 (parser, declarator, requires_clause,
19703 memfn ? cv_quals : -1));
19704
19705 /* Parse the virt-specifier-seq. */
19706 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19707
19708 /* Create the function-declarator. */
19709 declarator = make_call_declarator (declarator,
19710 params,
19711 cv_quals,
19712 virt_specifiers,
19713 ref_qual,
19714 tx_qual,
19715 exception_specification,
19716 late_return,
19717 requires_clause);
19718 declarator->std_attributes = attrs;
19719 declarator->attributes = gnu_attrs;
19720 /* Any subsequent parameter lists are to do with
19721 return type, so are not those of the declared
19722 function. */
19723 parser->default_arg_ok_p = false;
19724 }
19725
19726 /* Remove the function parms from scope. */
19727 pop_bindings_and_leave_scope ();
19728
19729 if (is_declarator)
19730 /* Repeat the main loop. */
19731 continue;
19732 }
19733
19734 /* If this is the first, we can try a parenthesized
19735 declarator. */
19736 if (first)
19737 {
19738 bool saved_in_type_id_in_expr_p;
19739
19740 parser->default_arg_ok_p = saved_default_arg_ok_p;
19741 parser->in_declarator_p = saved_in_declarator_p;
19742
19743 /* Consume the `('. */
19744 cp_lexer_consume_token (parser->lexer);
19745 /* Parse the nested declarator. */
19746 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19747 parser->in_type_id_in_expr_p = true;
19748 declarator
19749 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19750 /*parenthesized_p=*/NULL,
19751 member_p, friend_p);
19752 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19753 first = false;
19754 /* Expect a `)'. */
19755 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19756 declarator = cp_error_declarator;
19757 if (declarator == cp_error_declarator)
19758 break;
19759
19760 goto handle_declarator;
19761 }
19762 /* Otherwise, we must be done. */
19763 else
19764 break;
19765 }
19766 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19767 && token->type == CPP_OPEN_SQUARE
19768 && !cp_next_tokens_can_be_attribute_p (parser))
19769 {
19770 /* Parse an array-declarator. */
19771 tree bounds, attrs;
19772
19773 if (ctor_dtor_or_conv_p)
19774 *ctor_dtor_or_conv_p = 0;
19775
19776 first = false;
19777 parser->default_arg_ok_p = false;
19778 parser->in_declarator_p = true;
19779 /* Consume the `['. */
19780 cp_lexer_consume_token (parser->lexer);
19781 /* Peek at the next token. */
19782 token = cp_lexer_peek_token (parser->lexer);
19783 /* If the next token is `]', then there is no
19784 constant-expression. */
19785 if (token->type != CPP_CLOSE_SQUARE)
19786 {
19787 bool non_constant_p;
19788 bounds
19789 = cp_parser_constant_expression (parser,
19790 /*allow_non_constant=*/true,
19791 &non_constant_p);
19792 if (!non_constant_p)
19793 /* OK */;
19794 else if (error_operand_p (bounds))
19795 /* Already gave an error. */;
19796 else if (!parser->in_function_body
19797 || current_binding_level->kind == sk_function_parms)
19798 {
19799 /* Normally, the array bound must be an integral constant
19800 expression. However, as an extension, we allow VLAs
19801 in function scopes as long as they aren't part of a
19802 parameter declaration. */
19803 cp_parser_error (parser,
19804 "array bound is not an integer constant");
19805 bounds = error_mark_node;
19806 }
19807 else if (processing_template_decl
19808 && !type_dependent_expression_p (bounds))
19809 {
19810 /* Remember this wasn't a constant-expression. */
19811 bounds = build_nop (TREE_TYPE (bounds), bounds);
19812 TREE_SIDE_EFFECTS (bounds) = 1;
19813 }
19814 }
19815 else
19816 bounds = NULL_TREE;
19817 /* Look for the closing `]'. */
19818 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19819 {
19820 declarator = cp_error_declarator;
19821 break;
19822 }
19823
19824 attrs = cp_parser_std_attribute_spec_seq (parser);
19825 declarator = make_array_declarator (declarator, bounds);
19826 declarator->std_attributes = attrs;
19827 }
19828 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19829 {
19830 {
19831 tree qualifying_scope;
19832 tree unqualified_name;
19833 tree attrs;
19834 special_function_kind sfk;
19835 bool abstract_ok;
19836 bool pack_expansion_p = false;
19837 cp_token *declarator_id_start_token;
19838
19839 /* Parse a declarator-id */
19840 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19841 if (abstract_ok)
19842 {
19843 cp_parser_parse_tentatively (parser);
19844
19845 /* If we see an ellipsis, we should be looking at a
19846 parameter pack. */
19847 if (token->type == CPP_ELLIPSIS)
19848 {
19849 /* Consume the `...' */
19850 cp_lexer_consume_token (parser->lexer);
19851
19852 pack_expansion_p = true;
19853 }
19854 }
19855
19856 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19857 unqualified_name
19858 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19859 qualifying_scope = parser->scope;
19860 if (abstract_ok)
19861 {
19862 bool okay = false;
19863
19864 if (!unqualified_name && pack_expansion_p)
19865 {
19866 /* Check whether an error occurred. */
19867 okay = !cp_parser_error_occurred (parser);
19868
19869 /* We already consumed the ellipsis to mark a
19870 parameter pack, but we have no way to report it,
19871 so abort the tentative parse. We will be exiting
19872 immediately anyway. */
19873 cp_parser_abort_tentative_parse (parser);
19874 }
19875 else
19876 okay = cp_parser_parse_definitely (parser);
19877
19878 if (!okay)
19879 unqualified_name = error_mark_node;
19880 else if (unqualified_name
19881 && (qualifying_scope
19882 || (!identifier_p (unqualified_name))))
19883 {
19884 cp_parser_error (parser, "expected unqualified-id");
19885 unqualified_name = error_mark_node;
19886 }
19887 }
19888
19889 if (!unqualified_name)
19890 return NULL;
19891 if (unqualified_name == error_mark_node)
19892 {
19893 declarator = cp_error_declarator;
19894 pack_expansion_p = false;
19895 declarator->parameter_pack_p = false;
19896 break;
19897 }
19898
19899 attrs = cp_parser_std_attribute_spec_seq (parser);
19900
19901 if (qualifying_scope && at_namespace_scope_p ()
19902 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19903 {
19904 /* In the declaration of a member of a template class
19905 outside of the class itself, the SCOPE will sometimes
19906 be a TYPENAME_TYPE. For example, given:
19907
19908 template <typename T>
19909 int S<T>::R::i = 3;
19910
19911 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19912 this context, we must resolve S<T>::R to an ordinary
19913 type, rather than a typename type.
19914
19915 The reason we normally avoid resolving TYPENAME_TYPEs
19916 is that a specialization of `S' might render
19917 `S<T>::R' not a type. However, if `S' is
19918 specialized, then this `i' will not be used, so there
19919 is no harm in resolving the types here. */
19920 tree type;
19921
19922 /* Resolve the TYPENAME_TYPE. */
19923 type = resolve_typename_type (qualifying_scope,
19924 /*only_current_p=*/false);
19925 /* If that failed, the declarator is invalid. */
19926 if (TREE_CODE (type) == TYPENAME_TYPE)
19927 {
19928 if (typedef_variant_p (type))
19929 error_at (declarator_id_start_token->location,
19930 "cannot define member of dependent typedef "
19931 "%qT", type);
19932 else
19933 error_at (declarator_id_start_token->location,
19934 "%<%T::%E%> is not a type",
19935 TYPE_CONTEXT (qualifying_scope),
19936 TYPE_IDENTIFIER (qualifying_scope));
19937 }
19938 qualifying_scope = type;
19939 }
19940
19941 sfk = sfk_none;
19942
19943 if (unqualified_name)
19944 {
19945 tree class_type;
19946
19947 if (qualifying_scope
19948 && CLASS_TYPE_P (qualifying_scope))
19949 class_type = qualifying_scope;
19950 else
19951 class_type = current_class_type;
19952
19953 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19954 {
19955 tree name_type = TREE_TYPE (unqualified_name);
19956 if (class_type && same_type_p (name_type, class_type))
19957 {
19958 if (qualifying_scope
19959 && CLASSTYPE_USE_TEMPLATE (name_type))
19960 {
19961 error_at (declarator_id_start_token->location,
19962 "invalid use of constructor as a template");
19963 inform (declarator_id_start_token->location,
19964 "use %<%T::%D%> instead of %<%T::%D%> to "
19965 "name the constructor in a qualified name",
19966 class_type,
19967 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19968 class_type, name_type);
19969 declarator = cp_error_declarator;
19970 break;
19971 }
19972 else
19973 unqualified_name = constructor_name (class_type);
19974 }
19975 else
19976 {
19977 /* We do not attempt to print the declarator
19978 here because we do not have enough
19979 information about its original syntactic
19980 form. */
19981 cp_parser_error (parser, "invalid declarator");
19982 declarator = cp_error_declarator;
19983 break;
19984 }
19985 }
19986
19987 if (class_type)
19988 {
19989 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19990 sfk = sfk_destructor;
19991 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19992 sfk = sfk_conversion;
19993 else if (/* There's no way to declare a constructor
19994 for an unnamed type, even if the type
19995 got a name for linkage purposes. */
19996 !TYPE_WAS_UNNAMED (class_type)
19997 /* Handle correctly (c++/19200):
19998
19999 struct S {
20000 struct T{};
20001 friend void S(T);
20002 };
20003
20004 and also:
20005
20006 namespace N {
20007 void S();
20008 }
20009
20010 struct S {
20011 friend void N::S();
20012 }; */
20013 && !(friend_p
20014 && class_type != qualifying_scope)
20015 && constructor_name_p (unqualified_name,
20016 class_type))
20017 {
20018 unqualified_name = constructor_name (class_type);
20019 sfk = sfk_constructor;
20020 }
20021 else if (is_overloaded_fn (unqualified_name)
20022 && DECL_CONSTRUCTOR_P (get_first_fn
20023 (unqualified_name)))
20024 sfk = sfk_constructor;
20025
20026 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20027 *ctor_dtor_or_conv_p = -1;
20028 }
20029 }
20030 declarator = make_id_declarator (qualifying_scope,
20031 unqualified_name,
20032 sfk);
20033 declarator->std_attributes = attrs;
20034 declarator->id_loc = token->location;
20035 declarator->parameter_pack_p = pack_expansion_p;
20036
20037 if (pack_expansion_p)
20038 maybe_warn_variadic_templates ();
20039 }
20040
20041 handle_declarator:;
20042 scope = get_scope_of_declarator (declarator);
20043 if (scope)
20044 {
20045 /* Any names that appear after the declarator-id for a
20046 member are looked up in the containing scope. */
20047 if (at_function_scope_p ())
20048 {
20049 /* But declarations with qualified-ids can't appear in a
20050 function. */
20051 cp_parser_error (parser, "qualified-id in declaration");
20052 declarator = cp_error_declarator;
20053 break;
20054 }
20055 pushed_scope = push_scope (scope);
20056 }
20057 parser->in_declarator_p = true;
20058 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20059 || (declarator && declarator->kind == cdk_id))
20060 /* Default args are only allowed on function
20061 declarations. */
20062 parser->default_arg_ok_p = saved_default_arg_ok_p;
20063 else
20064 parser->default_arg_ok_p = false;
20065
20066 first = false;
20067 }
20068 /* We're done. */
20069 else
20070 break;
20071 }
20072
20073 /* For an abstract declarator, we might wind up with nothing at this
20074 point. That's an error; the declarator is not optional. */
20075 if (!declarator)
20076 cp_parser_error (parser, "expected declarator");
20077
20078 /* If we entered a scope, we must exit it now. */
20079 if (pushed_scope)
20080 pop_scope (pushed_scope);
20081
20082 parser->default_arg_ok_p = saved_default_arg_ok_p;
20083 parser->in_declarator_p = saved_in_declarator_p;
20084
20085 return declarator;
20086 }
20087
20088 /* Parse a ptr-operator.
20089
20090 ptr-operator:
20091 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20092 * cv-qualifier-seq [opt]
20093 &
20094 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20095 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20096
20097 GNU Extension:
20098
20099 ptr-operator:
20100 & cv-qualifier-seq [opt]
20101
20102 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20103 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20104 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20105 filled in with the TYPE containing the member. *CV_QUALS is
20106 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20107 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20108 Note that the tree codes returned by this function have nothing
20109 to do with the types of trees that will be eventually be created
20110 to represent the pointer or reference type being parsed. They are
20111 just constants with suggestive names. */
20112 static enum tree_code
20113 cp_parser_ptr_operator (cp_parser* parser,
20114 tree* type,
20115 cp_cv_quals *cv_quals,
20116 tree *attributes)
20117 {
20118 enum tree_code code = ERROR_MARK;
20119 cp_token *token;
20120 tree attrs = NULL_TREE;
20121
20122 /* Assume that it's not a pointer-to-member. */
20123 *type = NULL_TREE;
20124 /* And that there are no cv-qualifiers. */
20125 *cv_quals = TYPE_UNQUALIFIED;
20126
20127 /* Peek at the next token. */
20128 token = cp_lexer_peek_token (parser->lexer);
20129
20130 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20131 if (token->type == CPP_MULT)
20132 code = INDIRECT_REF;
20133 else if (token->type == CPP_AND)
20134 code = ADDR_EXPR;
20135 else if ((cxx_dialect != cxx98) &&
20136 token->type == CPP_AND_AND) /* C++0x only */
20137 code = NON_LVALUE_EXPR;
20138
20139 if (code != ERROR_MARK)
20140 {
20141 /* Consume the `*', `&' or `&&'. */
20142 cp_lexer_consume_token (parser->lexer);
20143
20144 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20145 `&', if we are allowing GNU extensions. (The only qualifier
20146 that can legally appear after `&' is `restrict', but that is
20147 enforced during semantic analysis. */
20148 if (code == INDIRECT_REF
20149 || cp_parser_allow_gnu_extensions_p (parser))
20150 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20151
20152 attrs = cp_parser_std_attribute_spec_seq (parser);
20153 if (attributes != NULL)
20154 *attributes = attrs;
20155 }
20156 else
20157 {
20158 /* Try the pointer-to-member case. */
20159 cp_parser_parse_tentatively (parser);
20160 /* Look for the optional `::' operator. */
20161 cp_parser_global_scope_opt (parser,
20162 /*current_scope_valid_p=*/false);
20163 /* Look for the nested-name specifier. */
20164 token = cp_lexer_peek_token (parser->lexer);
20165 cp_parser_nested_name_specifier (parser,
20166 /*typename_keyword_p=*/false,
20167 /*check_dependency_p=*/true,
20168 /*type_p=*/false,
20169 /*is_declaration=*/false);
20170 /* If we found it, and the next token is a `*', then we are
20171 indeed looking at a pointer-to-member operator. */
20172 if (!cp_parser_error_occurred (parser)
20173 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20174 {
20175 /* Indicate that the `*' operator was used. */
20176 code = INDIRECT_REF;
20177
20178 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20179 error_at (token->location, "%qD is a namespace", parser->scope);
20180 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20181 error_at (token->location, "cannot form pointer to member of "
20182 "non-class %q#T", parser->scope);
20183 else
20184 {
20185 /* The type of which the member is a member is given by the
20186 current SCOPE. */
20187 *type = parser->scope;
20188 /* The next name will not be qualified. */
20189 parser->scope = NULL_TREE;
20190 parser->qualifying_scope = NULL_TREE;
20191 parser->object_scope = NULL_TREE;
20192 /* Look for optional c++11 attributes. */
20193 attrs = cp_parser_std_attribute_spec_seq (parser);
20194 if (attributes != NULL)
20195 *attributes = attrs;
20196 /* Look for the optional cv-qualifier-seq. */
20197 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20198 }
20199 }
20200 /* If that didn't work we don't have a ptr-operator. */
20201 if (!cp_parser_parse_definitely (parser))
20202 cp_parser_error (parser, "expected ptr-operator");
20203 }
20204
20205 return code;
20206 }
20207
20208 /* Parse an (optional) cv-qualifier-seq.
20209
20210 cv-qualifier-seq:
20211 cv-qualifier cv-qualifier-seq [opt]
20212
20213 cv-qualifier:
20214 const
20215 volatile
20216
20217 GNU Extension:
20218
20219 cv-qualifier:
20220 __restrict__
20221
20222 Returns a bitmask representing the cv-qualifiers. */
20223
20224 static cp_cv_quals
20225 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20226 {
20227 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20228
20229 while (true)
20230 {
20231 cp_token *token;
20232 cp_cv_quals cv_qualifier;
20233
20234 /* Peek at the next token. */
20235 token = cp_lexer_peek_token (parser->lexer);
20236 /* See if it's a cv-qualifier. */
20237 switch (token->keyword)
20238 {
20239 case RID_CONST:
20240 cv_qualifier = TYPE_QUAL_CONST;
20241 break;
20242
20243 case RID_VOLATILE:
20244 cv_qualifier = TYPE_QUAL_VOLATILE;
20245 break;
20246
20247 case RID_RESTRICT:
20248 cv_qualifier = TYPE_QUAL_RESTRICT;
20249 break;
20250
20251 default:
20252 cv_qualifier = TYPE_UNQUALIFIED;
20253 break;
20254 }
20255
20256 if (!cv_qualifier)
20257 break;
20258
20259 if (cv_quals & cv_qualifier)
20260 {
20261 error_at (token->location, "duplicate cv-qualifier");
20262 cp_lexer_purge_token (parser->lexer);
20263 }
20264 else
20265 {
20266 cp_lexer_consume_token (parser->lexer);
20267 cv_quals |= cv_qualifier;
20268 }
20269 }
20270
20271 return cv_quals;
20272 }
20273
20274 /* Parse an (optional) ref-qualifier
20275
20276 ref-qualifier:
20277 &
20278 &&
20279
20280 Returns cp_ref_qualifier representing ref-qualifier. */
20281
20282 static cp_ref_qualifier
20283 cp_parser_ref_qualifier_opt (cp_parser* parser)
20284 {
20285 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20286
20287 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20288 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20289 return ref_qual;
20290
20291 while (true)
20292 {
20293 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20294 cp_token *token = cp_lexer_peek_token (parser->lexer);
20295
20296 switch (token->type)
20297 {
20298 case CPP_AND:
20299 curr_ref_qual = REF_QUAL_LVALUE;
20300 break;
20301
20302 case CPP_AND_AND:
20303 curr_ref_qual = REF_QUAL_RVALUE;
20304 break;
20305
20306 default:
20307 curr_ref_qual = REF_QUAL_NONE;
20308 break;
20309 }
20310
20311 if (!curr_ref_qual)
20312 break;
20313 else if (ref_qual)
20314 {
20315 error_at (token->location, "multiple ref-qualifiers");
20316 cp_lexer_purge_token (parser->lexer);
20317 }
20318 else
20319 {
20320 ref_qual = curr_ref_qual;
20321 cp_lexer_consume_token (parser->lexer);
20322 }
20323 }
20324
20325 return ref_qual;
20326 }
20327
20328 /* Parse an optional tx-qualifier.
20329
20330 tx-qualifier:
20331 transaction_safe
20332 transaction_safe_dynamic */
20333
20334 static tree
20335 cp_parser_tx_qualifier_opt (cp_parser *parser)
20336 {
20337 cp_token *token = cp_lexer_peek_token (parser->lexer);
20338 if (token->type == CPP_NAME)
20339 {
20340 tree name = token->u.value;
20341 const char *p = IDENTIFIER_POINTER (name);
20342 const int len = strlen ("transaction_safe");
20343 if (!strncmp (p, "transaction_safe", len))
20344 {
20345 p += len;
20346 if (*p == '\0'
20347 || !strcmp (p, "_dynamic"))
20348 {
20349 cp_lexer_consume_token (parser->lexer);
20350 if (!flag_tm)
20351 {
20352 error ("%E requires %<-fgnu-tm%>", name);
20353 return NULL_TREE;
20354 }
20355 else
20356 return name;
20357 }
20358 }
20359 }
20360 return NULL_TREE;
20361 }
20362
20363 /* Parse an (optional) virt-specifier-seq.
20364
20365 virt-specifier-seq:
20366 virt-specifier virt-specifier-seq [opt]
20367
20368 virt-specifier:
20369 override
20370 final
20371
20372 Returns a bitmask representing the virt-specifiers. */
20373
20374 static cp_virt_specifiers
20375 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20376 {
20377 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20378
20379 while (true)
20380 {
20381 cp_token *token;
20382 cp_virt_specifiers virt_specifier;
20383
20384 /* Peek at the next token. */
20385 token = cp_lexer_peek_token (parser->lexer);
20386 /* See if it's a virt-specifier-qualifier. */
20387 if (token->type != CPP_NAME)
20388 break;
20389 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20390 {
20391 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20392 virt_specifier = VIRT_SPEC_OVERRIDE;
20393 }
20394 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20395 {
20396 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20397 virt_specifier = VIRT_SPEC_FINAL;
20398 }
20399 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20400 {
20401 virt_specifier = VIRT_SPEC_FINAL;
20402 }
20403 else
20404 break;
20405
20406 if (virt_specifiers & virt_specifier)
20407 {
20408 error_at (token->location, "duplicate virt-specifier");
20409 cp_lexer_purge_token (parser->lexer);
20410 }
20411 else
20412 {
20413 cp_lexer_consume_token (parser->lexer);
20414 virt_specifiers |= virt_specifier;
20415 }
20416 }
20417 return virt_specifiers;
20418 }
20419
20420 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20421 is in scope even though it isn't real. */
20422
20423 void
20424 inject_this_parameter (tree ctype, cp_cv_quals quals)
20425 {
20426 tree this_parm;
20427
20428 if (current_class_ptr)
20429 {
20430 /* We don't clear this between NSDMIs. Is it already what we want? */
20431 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20432 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20433 && cp_type_quals (type) == quals)
20434 return;
20435 }
20436
20437 this_parm = build_this_parm (ctype, quals);
20438 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20439 current_class_ptr = NULL_TREE;
20440 current_class_ref
20441 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20442 current_class_ptr = this_parm;
20443 }
20444
20445 /* Return true iff our current scope is a non-static data member
20446 initializer. */
20447
20448 bool
20449 parsing_nsdmi (void)
20450 {
20451 /* We recognize NSDMI context by the context-less 'this' pointer set up
20452 by the function above. */
20453 if (current_class_ptr
20454 && TREE_CODE (current_class_ptr) == PARM_DECL
20455 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20456 return true;
20457 return false;
20458 }
20459
20460 /* Return true iff our current scope is a default capturing generic lambda
20461 defined within a template. FIXME: This is part of a workaround (see
20462 semantics.c) to handle building lambda closure types correctly in templates
20463 which we ultimately want to defer to instantiation time. */
20464
20465 bool
20466 parsing_default_capturing_generic_lambda_in_template (void)
20467 {
20468 if (!processing_template_decl || !current_class_type)
20469 return false;
20470
20471 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20472 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20473 return false;
20474
20475 tree callop = lambda_function (lam);
20476 if (!callop)
20477 return false;
20478
20479 return (DECL_TEMPLATE_INFO (callop)
20480 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20481 && ((current_nonlambda_class_type ()
20482 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20483 || ((current_nonlambda_function ()
20484 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20485 }
20486
20487 /* Parse a late-specified return type, if any. This is not a separate
20488 non-terminal, but part of a function declarator, which looks like
20489
20490 -> trailing-type-specifier-seq abstract-declarator(opt)
20491
20492 Returns the type indicated by the type-id.
20493
20494 In addition to this, parse any queued up #pragma omp declare simd
20495 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20496 #pragma acc routine clauses.
20497
20498 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20499 function. */
20500
20501 static tree
20502 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20503 tree& requires_clause, cp_cv_quals quals)
20504 {
20505 cp_token *token;
20506 tree type = NULL_TREE;
20507 bool declare_simd_p = (parser->omp_declare_simd
20508 && declarator
20509 && declarator->kind == cdk_id);
20510
20511 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20512 && declarator && declarator->kind == cdk_id);
20513
20514 bool oacc_routine_p = (parser->oacc_routine
20515 && declarator
20516 && declarator->kind == cdk_id);
20517
20518 /* Peek at the next token. */
20519 token = cp_lexer_peek_token (parser->lexer);
20520 /* A late-specified return type is indicated by an initial '->'. */
20521 if (token->type != CPP_DEREF
20522 && token->keyword != RID_REQUIRES
20523 && !(token->type == CPP_NAME
20524 && token->u.value == ridpointers[RID_REQUIRES])
20525 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20526 return NULL_TREE;
20527
20528 tree save_ccp = current_class_ptr;
20529 tree save_ccr = current_class_ref;
20530 if (quals >= 0)
20531 {
20532 /* DR 1207: 'this' is in scope in the trailing return type. */
20533 inject_this_parameter (current_class_type, quals);
20534 }
20535
20536 if (token->type == CPP_DEREF)
20537 {
20538 /* Consume the ->. */
20539 cp_lexer_consume_token (parser->lexer);
20540
20541 type = cp_parser_trailing_type_id (parser);
20542 }
20543
20544 /* Function declarations may be followed by a trailing
20545 requires-clause. */
20546 requires_clause = cp_parser_requires_clause_opt (parser);
20547
20548 if (cilk_simd_fn_vector_p)
20549 declarator->attributes
20550 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20551 declarator->attributes);
20552 if (declare_simd_p)
20553 declarator->attributes
20554 = cp_parser_late_parsing_omp_declare_simd (parser,
20555 declarator->attributes);
20556 if (oacc_routine_p)
20557 declarator->attributes
20558 = cp_parser_late_parsing_oacc_routine (parser,
20559 declarator->attributes);
20560
20561 if (quals >= 0)
20562 {
20563 current_class_ptr = save_ccp;
20564 current_class_ref = save_ccr;
20565 }
20566
20567 return type;
20568 }
20569
20570 /* Parse a declarator-id.
20571
20572 declarator-id:
20573 id-expression
20574 :: [opt] nested-name-specifier [opt] type-name
20575
20576 In the `id-expression' case, the value returned is as for
20577 cp_parser_id_expression if the id-expression was an unqualified-id.
20578 If the id-expression was a qualified-id, then a SCOPE_REF is
20579 returned. The first operand is the scope (either a NAMESPACE_DECL
20580 or TREE_TYPE), but the second is still just a representation of an
20581 unqualified-id. */
20582
20583 static tree
20584 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20585 {
20586 tree id;
20587 /* The expression must be an id-expression. Assume that qualified
20588 names are the names of types so that:
20589
20590 template <class T>
20591 int S<T>::R::i = 3;
20592
20593 will work; we must treat `S<T>::R' as the name of a type.
20594 Similarly, assume that qualified names are templates, where
20595 required, so that:
20596
20597 template <class T>
20598 int S<T>::R<T>::i = 3;
20599
20600 will work, too. */
20601 id = cp_parser_id_expression (parser,
20602 /*template_keyword_p=*/false,
20603 /*check_dependency_p=*/false,
20604 /*template_p=*/NULL,
20605 /*declarator_p=*/true,
20606 optional_p);
20607 if (id && BASELINK_P (id))
20608 id = BASELINK_FUNCTIONS (id);
20609 return id;
20610 }
20611
20612 /* Parse a type-id.
20613
20614 type-id:
20615 type-specifier-seq abstract-declarator [opt]
20616
20617 Returns the TYPE specified. */
20618
20619 static tree
20620 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20621 bool is_trailing_return)
20622 {
20623 cp_decl_specifier_seq type_specifier_seq;
20624 cp_declarator *abstract_declarator;
20625
20626 /* Parse the type-specifier-seq. */
20627 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20628 is_trailing_return,
20629 &type_specifier_seq);
20630 if (is_template_arg && type_specifier_seq.type
20631 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20632 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20633 /* A bare template name as a template argument is a template template
20634 argument, not a placeholder, so fail parsing it as a type argument. */
20635 {
20636 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20637 cp_parser_simulate_error (parser);
20638 return error_mark_node;
20639 }
20640 if (type_specifier_seq.type == error_mark_node)
20641 return error_mark_node;
20642
20643 /* There might or might not be an abstract declarator. */
20644 cp_parser_parse_tentatively (parser);
20645 /* Look for the declarator. */
20646 abstract_declarator
20647 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20648 /*parenthesized_p=*/NULL,
20649 /*member_p=*/false,
20650 /*friend_p=*/false);
20651 /* Check to see if there really was a declarator. */
20652 if (!cp_parser_parse_definitely (parser))
20653 abstract_declarator = NULL;
20654
20655 if (type_specifier_seq.type
20656 /* The concepts TS allows 'auto' as a type-id. */
20657 && (!flag_concepts || parser->in_type_id_in_expr_p)
20658 /* None of the valid uses of 'auto' in C++14 involve the type-id
20659 nonterminal, but it is valid in a trailing-return-type. */
20660 && !(cxx_dialect >= cxx14 && is_trailing_return))
20661 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20662 {
20663 /* A type-id with type 'auto' is only ok if the abstract declarator
20664 is a function declarator with a late-specified return type.
20665
20666 A type-id with 'auto' is also valid in a trailing-return-type
20667 in a compound-requirement. */
20668 if (abstract_declarator
20669 && abstract_declarator->kind == cdk_function
20670 && abstract_declarator->u.function.late_return_type)
20671 /* OK */;
20672 else if (parser->in_result_type_constraint_p)
20673 /* OK */;
20674 else
20675 {
20676 location_t loc = type_specifier_seq.locations[ds_type_spec];
20677 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20678 {
20679 error_at (loc, "missing template arguments after %qT",
20680 auto_node);
20681 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20682 tmpl);
20683 }
20684 else
20685 error_at (loc, "invalid use of %qT", auto_node);
20686 return error_mark_node;
20687 }
20688 }
20689
20690 return groktypename (&type_specifier_seq, abstract_declarator,
20691 is_template_arg);
20692 }
20693
20694 static tree
20695 cp_parser_type_id (cp_parser *parser)
20696 {
20697 return cp_parser_type_id_1 (parser, false, false);
20698 }
20699
20700 static tree
20701 cp_parser_template_type_arg (cp_parser *parser)
20702 {
20703 tree r;
20704 const char *saved_message = parser->type_definition_forbidden_message;
20705 parser->type_definition_forbidden_message
20706 = G_("types may not be defined in template arguments");
20707 r = cp_parser_type_id_1 (parser, true, false);
20708 parser->type_definition_forbidden_message = saved_message;
20709 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20710 {
20711 error ("invalid use of %<auto%> in template argument");
20712 r = error_mark_node;
20713 }
20714 return r;
20715 }
20716
20717 static tree
20718 cp_parser_trailing_type_id (cp_parser *parser)
20719 {
20720 return cp_parser_type_id_1 (parser, false, true);
20721 }
20722
20723 /* Parse a type-specifier-seq.
20724
20725 type-specifier-seq:
20726 type-specifier type-specifier-seq [opt]
20727
20728 GNU extension:
20729
20730 type-specifier-seq:
20731 attributes type-specifier-seq [opt]
20732
20733 If IS_DECLARATION is true, we are at the start of a "condition" or
20734 exception-declaration, so we might be followed by a declarator-id.
20735
20736 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20737 i.e. we've just seen "->".
20738
20739 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20740
20741 static void
20742 cp_parser_type_specifier_seq (cp_parser* parser,
20743 bool is_declaration,
20744 bool is_trailing_return,
20745 cp_decl_specifier_seq *type_specifier_seq)
20746 {
20747 bool seen_type_specifier = false;
20748 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20749 cp_token *start_token = NULL;
20750
20751 /* Clear the TYPE_SPECIFIER_SEQ. */
20752 clear_decl_specs (type_specifier_seq);
20753
20754 /* In the context of a trailing return type, enum E { } is an
20755 elaborated-type-specifier followed by a function-body, not an
20756 enum-specifier. */
20757 if (is_trailing_return)
20758 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20759
20760 /* Parse the type-specifiers and attributes. */
20761 while (true)
20762 {
20763 tree type_specifier;
20764 bool is_cv_qualifier;
20765
20766 /* Check for attributes first. */
20767 if (cp_next_tokens_can_be_attribute_p (parser))
20768 {
20769 type_specifier_seq->attributes =
20770 chainon (type_specifier_seq->attributes,
20771 cp_parser_attributes_opt (parser));
20772 continue;
20773 }
20774
20775 /* record the token of the beginning of the type specifier seq,
20776 for error reporting purposes*/
20777 if (!start_token)
20778 start_token = cp_lexer_peek_token (parser->lexer);
20779
20780 /* Look for the type-specifier. */
20781 type_specifier = cp_parser_type_specifier (parser,
20782 flags,
20783 type_specifier_seq,
20784 /*is_declaration=*/false,
20785 NULL,
20786 &is_cv_qualifier);
20787 if (!type_specifier)
20788 {
20789 /* If the first type-specifier could not be found, this is not a
20790 type-specifier-seq at all. */
20791 if (!seen_type_specifier)
20792 {
20793 /* Set in_declarator_p to avoid skipping to the semicolon. */
20794 int in_decl = parser->in_declarator_p;
20795 parser->in_declarator_p = true;
20796
20797 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20798 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20799 cp_parser_error (parser, "expected type-specifier");
20800
20801 parser->in_declarator_p = in_decl;
20802
20803 type_specifier_seq->type = error_mark_node;
20804 return;
20805 }
20806 /* If subsequent type-specifiers could not be found, the
20807 type-specifier-seq is complete. */
20808 break;
20809 }
20810
20811 seen_type_specifier = true;
20812 /* The standard says that a condition can be:
20813
20814 type-specifier-seq declarator = assignment-expression
20815
20816 However, given:
20817
20818 struct S {};
20819 if (int S = ...)
20820
20821 we should treat the "S" as a declarator, not as a
20822 type-specifier. The standard doesn't say that explicitly for
20823 type-specifier-seq, but it does say that for
20824 decl-specifier-seq in an ordinary declaration. Perhaps it
20825 would be clearer just to allow a decl-specifier-seq here, and
20826 then add a semantic restriction that if any decl-specifiers
20827 that are not type-specifiers appear, the program is invalid. */
20828 if (is_declaration && !is_cv_qualifier)
20829 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20830 }
20831 }
20832
20833 /* Return whether the function currently being declared has an associated
20834 template parameter list. */
20835
20836 static bool
20837 function_being_declared_is_template_p (cp_parser* parser)
20838 {
20839 if (!current_template_parms || processing_template_parmlist)
20840 return false;
20841
20842 if (parser->implicit_template_scope)
20843 return true;
20844
20845 if (at_class_scope_p ()
20846 && TYPE_BEING_DEFINED (current_class_type))
20847 return parser->num_template_parameter_lists != 0;
20848
20849 return ((int) parser->num_template_parameter_lists > template_class_depth
20850 (current_class_type));
20851 }
20852
20853 /* Parse a parameter-declaration-clause.
20854
20855 parameter-declaration-clause:
20856 parameter-declaration-list [opt] ... [opt]
20857 parameter-declaration-list , ...
20858
20859 Returns a representation for the parameter declarations. A return
20860 value of NULL indicates a parameter-declaration-clause consisting
20861 only of an ellipsis. */
20862
20863 static tree
20864 cp_parser_parameter_declaration_clause (cp_parser* parser)
20865 {
20866 tree parameters;
20867 cp_token *token;
20868 bool ellipsis_p;
20869 bool is_error;
20870
20871 struct cleanup {
20872 cp_parser* parser;
20873 int auto_is_implicit_function_template_parm_p;
20874 ~cleanup() {
20875 parser->auto_is_implicit_function_template_parm_p
20876 = auto_is_implicit_function_template_parm_p;
20877 }
20878 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20879
20880 (void) cleanup;
20881
20882 if (!processing_specialization
20883 && !processing_template_parmlist
20884 && !processing_explicit_instantiation)
20885 if (!current_function_decl
20886 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20887 parser->auto_is_implicit_function_template_parm_p = true;
20888
20889 /* Peek at the next token. */
20890 token = cp_lexer_peek_token (parser->lexer);
20891 /* Check for trivial parameter-declaration-clauses. */
20892 if (token->type == CPP_ELLIPSIS)
20893 {
20894 /* Consume the `...' token. */
20895 cp_lexer_consume_token (parser->lexer);
20896 return NULL_TREE;
20897 }
20898 else if (token->type == CPP_CLOSE_PAREN)
20899 /* There are no parameters. */
20900 {
20901 #ifndef NO_IMPLICIT_EXTERN_C
20902 if (in_system_header_at (input_location)
20903 && current_class_type == NULL
20904 && current_lang_name == lang_name_c)
20905 return NULL_TREE;
20906 else
20907 #endif
20908 return void_list_node;
20909 }
20910 /* Check for `(void)', too, which is a special case. */
20911 else if (token->keyword == RID_VOID
20912 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20913 == CPP_CLOSE_PAREN))
20914 {
20915 /* Consume the `void' token. */
20916 cp_lexer_consume_token (parser->lexer);
20917 /* There are no parameters. */
20918 return void_list_node;
20919 }
20920
20921 /* Parse the parameter-declaration-list. */
20922 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20923 /* If a parse error occurred while parsing the
20924 parameter-declaration-list, then the entire
20925 parameter-declaration-clause is erroneous. */
20926 if (is_error)
20927 return NULL;
20928
20929 /* Peek at the next token. */
20930 token = cp_lexer_peek_token (parser->lexer);
20931 /* If it's a `,', the clause should terminate with an ellipsis. */
20932 if (token->type == CPP_COMMA)
20933 {
20934 /* Consume the `,'. */
20935 cp_lexer_consume_token (parser->lexer);
20936 /* Expect an ellipsis. */
20937 ellipsis_p
20938 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20939 }
20940 /* It might also be `...' if the optional trailing `,' was
20941 omitted. */
20942 else if (token->type == CPP_ELLIPSIS)
20943 {
20944 /* Consume the `...' token. */
20945 cp_lexer_consume_token (parser->lexer);
20946 /* And remember that we saw it. */
20947 ellipsis_p = true;
20948 }
20949 else
20950 ellipsis_p = false;
20951
20952 /* Finish the parameter list. */
20953 if (!ellipsis_p)
20954 parameters = chainon (parameters, void_list_node);
20955
20956 return parameters;
20957 }
20958
20959 /* Parse a parameter-declaration-list.
20960
20961 parameter-declaration-list:
20962 parameter-declaration
20963 parameter-declaration-list , parameter-declaration
20964
20965 Returns a representation of the parameter-declaration-list, as for
20966 cp_parser_parameter_declaration_clause. However, the
20967 `void_list_node' is never appended to the list. Upon return,
20968 *IS_ERROR will be true iff an error occurred. */
20969
20970 static tree
20971 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20972 {
20973 tree parameters = NULL_TREE;
20974 tree *tail = &parameters;
20975 bool saved_in_unbraced_linkage_specification_p;
20976 int index = 0;
20977
20978 /* Assume all will go well. */
20979 *is_error = false;
20980 /* The special considerations that apply to a function within an
20981 unbraced linkage specifications do not apply to the parameters
20982 to the function. */
20983 saved_in_unbraced_linkage_specification_p
20984 = parser->in_unbraced_linkage_specification_p;
20985 parser->in_unbraced_linkage_specification_p = false;
20986
20987 /* Look for more parameters. */
20988 while (true)
20989 {
20990 cp_parameter_declarator *parameter;
20991 tree decl = error_mark_node;
20992 bool parenthesized_p = false;
20993 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20994 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20995 (current_template_parms)) : 0);
20996
20997 /* Parse the parameter. */
20998 parameter
20999 = cp_parser_parameter_declaration (parser,
21000 /*template_parm_p=*/false,
21001 &parenthesized_p);
21002
21003 /* We don't know yet if the enclosing context is deprecated, so wait
21004 and warn in grokparms if appropriate. */
21005 deprecated_state = DEPRECATED_SUPPRESS;
21006
21007 if (parameter)
21008 {
21009 /* If a function parameter pack was specified and an implicit template
21010 parameter was introduced during cp_parser_parameter_declaration,
21011 change any implicit parameters introduced into packs. */
21012 if (parser->implicit_template_parms
21013 && parameter->declarator
21014 && parameter->declarator->parameter_pack_p)
21015 {
21016 int latest_template_parm_idx = TREE_VEC_LENGTH
21017 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21018
21019 if (latest_template_parm_idx != template_parm_idx)
21020 parameter->decl_specifiers.type = convert_generic_types_to_packs
21021 (parameter->decl_specifiers.type,
21022 template_parm_idx, latest_template_parm_idx);
21023 }
21024
21025 decl = grokdeclarator (parameter->declarator,
21026 &parameter->decl_specifiers,
21027 PARM,
21028 parameter->default_argument != NULL_TREE,
21029 &parameter->decl_specifiers.attributes);
21030 }
21031
21032 deprecated_state = DEPRECATED_NORMAL;
21033
21034 /* If a parse error occurred parsing the parameter declaration,
21035 then the entire parameter-declaration-list is erroneous. */
21036 if (decl == error_mark_node)
21037 {
21038 *is_error = true;
21039 parameters = error_mark_node;
21040 break;
21041 }
21042
21043 if (parameter->decl_specifiers.attributes)
21044 cplus_decl_attributes (&decl,
21045 parameter->decl_specifiers.attributes,
21046 0);
21047 if (DECL_NAME (decl))
21048 decl = pushdecl (decl);
21049
21050 if (decl != error_mark_node)
21051 {
21052 retrofit_lang_decl (decl);
21053 DECL_PARM_INDEX (decl) = ++index;
21054 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21055 }
21056
21057 /* Add the new parameter to the list. */
21058 *tail = build_tree_list (parameter->default_argument, decl);
21059 tail = &TREE_CHAIN (*tail);
21060
21061 /* Peek at the next token. */
21062 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21063 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21064 /* These are for Objective-C++ */
21065 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21066 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21067 /* The parameter-declaration-list is complete. */
21068 break;
21069 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21070 {
21071 cp_token *token;
21072
21073 /* Peek at the next token. */
21074 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21075 /* If it's an ellipsis, then the list is complete. */
21076 if (token->type == CPP_ELLIPSIS)
21077 break;
21078 /* Otherwise, there must be more parameters. Consume the
21079 `,'. */
21080 cp_lexer_consume_token (parser->lexer);
21081 /* When parsing something like:
21082
21083 int i(float f, double d)
21084
21085 we can tell after seeing the declaration for "f" that we
21086 are not looking at an initialization of a variable "i",
21087 but rather at the declaration of a function "i".
21088
21089 Due to the fact that the parsing of template arguments
21090 (as specified to a template-id) requires backtracking we
21091 cannot use this technique when inside a template argument
21092 list. */
21093 if (!parser->in_template_argument_list_p
21094 && !parser->in_type_id_in_expr_p
21095 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21096 /* However, a parameter-declaration of the form
21097 "float(f)" (which is a valid declaration of a
21098 parameter "f") can also be interpreted as an
21099 expression (the conversion of "f" to "float"). */
21100 && !parenthesized_p)
21101 cp_parser_commit_to_tentative_parse (parser);
21102 }
21103 else
21104 {
21105 cp_parser_error (parser, "expected %<,%> or %<...%>");
21106 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21107 cp_parser_skip_to_closing_parenthesis (parser,
21108 /*recovering=*/true,
21109 /*or_comma=*/false,
21110 /*consume_paren=*/false);
21111 break;
21112 }
21113 }
21114
21115 parser->in_unbraced_linkage_specification_p
21116 = saved_in_unbraced_linkage_specification_p;
21117
21118 /* Reset implicit_template_scope if we are about to leave the function
21119 parameter list that introduced it. Note that for out-of-line member
21120 definitions, there will be one or more class scopes before we get to
21121 the template parameter scope. */
21122
21123 if (cp_binding_level *its = parser->implicit_template_scope)
21124 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21125 {
21126 while (maybe_its->kind == sk_class)
21127 maybe_its = maybe_its->level_chain;
21128 if (maybe_its == its)
21129 {
21130 parser->implicit_template_parms = 0;
21131 parser->implicit_template_scope = 0;
21132 }
21133 }
21134
21135 return parameters;
21136 }
21137
21138 /* Parse a parameter declaration.
21139
21140 parameter-declaration:
21141 decl-specifier-seq ... [opt] declarator
21142 decl-specifier-seq declarator = assignment-expression
21143 decl-specifier-seq ... [opt] abstract-declarator [opt]
21144 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21145
21146 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21147 declares a template parameter. (In that case, a non-nested `>'
21148 token encountered during the parsing of the assignment-expression
21149 is not interpreted as a greater-than operator.)
21150
21151 Returns a representation of the parameter, or NULL if an error
21152 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21153 true iff the declarator is of the form "(p)". */
21154
21155 static cp_parameter_declarator *
21156 cp_parser_parameter_declaration (cp_parser *parser,
21157 bool template_parm_p,
21158 bool *parenthesized_p)
21159 {
21160 int declares_class_or_enum;
21161 cp_decl_specifier_seq decl_specifiers;
21162 cp_declarator *declarator;
21163 tree default_argument;
21164 cp_token *token = NULL, *declarator_token_start = NULL;
21165 const char *saved_message;
21166 bool template_parameter_pack_p = false;
21167
21168 /* In a template parameter, `>' is not an operator.
21169
21170 [temp.param]
21171
21172 When parsing a default template-argument for a non-type
21173 template-parameter, the first non-nested `>' is taken as the end
21174 of the template parameter-list rather than a greater-than
21175 operator. */
21176
21177 /* Type definitions may not appear in parameter types. */
21178 saved_message = parser->type_definition_forbidden_message;
21179 parser->type_definition_forbidden_message
21180 = G_("types may not be defined in parameter types");
21181
21182 /* Parse the declaration-specifiers. */
21183 cp_parser_decl_specifier_seq (parser,
21184 CP_PARSER_FLAGS_NONE,
21185 &decl_specifiers,
21186 &declares_class_or_enum);
21187
21188 /* Complain about missing 'typename' or other invalid type names. */
21189 if (!decl_specifiers.any_type_specifiers_p
21190 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21191 decl_specifiers.type = error_mark_node;
21192
21193 /* If an error occurred, there's no reason to attempt to parse the
21194 rest of the declaration. */
21195 if (cp_parser_error_occurred (parser))
21196 {
21197 parser->type_definition_forbidden_message = saved_message;
21198 return NULL;
21199 }
21200
21201 /* Peek at the next token. */
21202 token = cp_lexer_peek_token (parser->lexer);
21203
21204 /* If the next token is a `)', `,', `=', `>', or `...', then there
21205 is no declarator. However, when variadic templates are enabled,
21206 there may be a declarator following `...'. */
21207 if (token->type == CPP_CLOSE_PAREN
21208 || token->type == CPP_COMMA
21209 || token->type == CPP_EQ
21210 || token->type == CPP_GREATER)
21211 {
21212 declarator = NULL;
21213 if (parenthesized_p)
21214 *parenthesized_p = false;
21215 }
21216 /* Otherwise, there should be a declarator. */
21217 else
21218 {
21219 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21220 parser->default_arg_ok_p = false;
21221
21222 /* After seeing a decl-specifier-seq, if the next token is not a
21223 "(", there is no possibility that the code is a valid
21224 expression. Therefore, if parsing tentatively, we commit at
21225 this point. */
21226 if (!parser->in_template_argument_list_p
21227 /* In an expression context, having seen:
21228
21229 (int((char ...
21230
21231 we cannot be sure whether we are looking at a
21232 function-type (taking a "char" as a parameter) or a cast
21233 of some object of type "char" to "int". */
21234 && !parser->in_type_id_in_expr_p
21235 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21236 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21237 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21238 cp_parser_commit_to_tentative_parse (parser);
21239 /* Parse the declarator. */
21240 declarator_token_start = token;
21241 declarator = cp_parser_declarator (parser,
21242 CP_PARSER_DECLARATOR_EITHER,
21243 /*ctor_dtor_or_conv_p=*/NULL,
21244 parenthesized_p,
21245 /*member_p=*/false,
21246 /*friend_p=*/false);
21247 parser->default_arg_ok_p = saved_default_arg_ok_p;
21248 /* After the declarator, allow more attributes. */
21249 decl_specifiers.attributes
21250 = chainon (decl_specifiers.attributes,
21251 cp_parser_attributes_opt (parser));
21252
21253 /* If the declarator is a template parameter pack, remember that and
21254 clear the flag in the declarator itself so we don't get errors
21255 from grokdeclarator. */
21256 if (template_parm_p && declarator && declarator->parameter_pack_p)
21257 {
21258 declarator->parameter_pack_p = false;
21259 template_parameter_pack_p = true;
21260 }
21261 }
21262
21263 /* If the next token is an ellipsis, and we have not seen a declarator
21264 name, and if either the type of the declarator contains parameter
21265 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21266 for, eg, abbreviated integral type names), then we actually have a
21267 parameter pack expansion expression. Otherwise, leave the ellipsis
21268 for a C-style variadic function. */
21269 token = cp_lexer_peek_token (parser->lexer);
21270 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21271 {
21272 tree type = decl_specifiers.type;
21273
21274 if (type && DECL_P (type))
21275 type = TREE_TYPE (type);
21276
21277 if (((type
21278 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21279 && (template_parm_p || uses_parameter_packs (type)))
21280 || (!type && template_parm_p))
21281 && declarator_can_be_parameter_pack (declarator))
21282 {
21283 /* Consume the `...'. */
21284 cp_lexer_consume_token (parser->lexer);
21285 maybe_warn_variadic_templates ();
21286
21287 /* Build a pack expansion type */
21288 if (template_parm_p)
21289 template_parameter_pack_p = true;
21290 else if (declarator)
21291 declarator->parameter_pack_p = true;
21292 else
21293 decl_specifiers.type = make_pack_expansion (type);
21294 }
21295 }
21296
21297 /* The restriction on defining new types applies only to the type
21298 of the parameter, not to the default argument. */
21299 parser->type_definition_forbidden_message = saved_message;
21300
21301 /* If the next token is `=', then process a default argument. */
21302 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21303 {
21304 tree type = decl_specifiers.type;
21305 token = cp_lexer_peek_token (parser->lexer);
21306 /* If we are defining a class, then the tokens that make up the
21307 default argument must be saved and processed later. */
21308 if (!template_parm_p && at_class_scope_p ()
21309 && TYPE_BEING_DEFINED (current_class_type)
21310 && !LAMBDA_TYPE_P (current_class_type))
21311 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21312
21313 // A constrained-type-specifier may declare a type template-parameter.
21314 else if (declares_constrained_type_template_parameter (type))
21315 default_argument
21316 = cp_parser_default_type_template_argument (parser);
21317
21318 // A constrained-type-specifier may declare a template-template-parameter.
21319 else if (declares_constrained_template_template_parameter (type))
21320 default_argument
21321 = cp_parser_default_template_template_argument (parser);
21322
21323 /* Outside of a class definition, we can just parse the
21324 assignment-expression. */
21325 else
21326 default_argument
21327 = cp_parser_default_argument (parser, template_parm_p);
21328
21329 if (!parser->default_arg_ok_p)
21330 {
21331 permerror (token->location,
21332 "default arguments are only "
21333 "permitted for function parameters");
21334 }
21335 else if ((declarator && declarator->parameter_pack_p)
21336 || template_parameter_pack_p
21337 || (decl_specifiers.type
21338 && PACK_EXPANSION_P (decl_specifiers.type)))
21339 {
21340 /* Find the name of the parameter pack. */
21341 cp_declarator *id_declarator = declarator;
21342 while (id_declarator && id_declarator->kind != cdk_id)
21343 id_declarator = id_declarator->declarator;
21344
21345 if (id_declarator && id_declarator->kind == cdk_id)
21346 error_at (declarator_token_start->location,
21347 template_parm_p
21348 ? G_("template parameter pack %qD "
21349 "cannot have a default argument")
21350 : G_("parameter pack %qD cannot have "
21351 "a default argument"),
21352 id_declarator->u.id.unqualified_name);
21353 else
21354 error_at (declarator_token_start->location,
21355 template_parm_p
21356 ? G_("template parameter pack cannot have "
21357 "a default argument")
21358 : G_("parameter pack cannot have a "
21359 "default argument"));
21360
21361 default_argument = NULL_TREE;
21362 }
21363 }
21364 else
21365 default_argument = NULL_TREE;
21366
21367 return make_parameter_declarator (&decl_specifiers,
21368 declarator,
21369 default_argument,
21370 template_parameter_pack_p);
21371 }
21372
21373 /* Parse a default argument and return it.
21374
21375 TEMPLATE_PARM_P is true if this is a default argument for a
21376 non-type template parameter. */
21377 static tree
21378 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21379 {
21380 tree default_argument = NULL_TREE;
21381 bool saved_greater_than_is_operator_p;
21382 bool saved_local_variables_forbidden_p;
21383 bool non_constant_p, is_direct_init;
21384
21385 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21386 set correctly. */
21387 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21388 parser->greater_than_is_operator_p = !template_parm_p;
21389 /* Local variable names (and the `this' keyword) may not
21390 appear in a default argument. */
21391 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21392 parser->local_variables_forbidden_p = true;
21393 /* Parse the assignment-expression. */
21394 if (template_parm_p)
21395 push_deferring_access_checks (dk_no_deferred);
21396 tree saved_class_ptr = NULL_TREE;
21397 tree saved_class_ref = NULL_TREE;
21398 /* The "this" pointer is not valid in a default argument. */
21399 if (cfun)
21400 {
21401 saved_class_ptr = current_class_ptr;
21402 cp_function_chain->x_current_class_ptr = NULL_TREE;
21403 saved_class_ref = current_class_ref;
21404 cp_function_chain->x_current_class_ref = NULL_TREE;
21405 }
21406 default_argument
21407 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21408 /* Restore the "this" pointer. */
21409 if (cfun)
21410 {
21411 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21412 cp_function_chain->x_current_class_ref = saved_class_ref;
21413 }
21414 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21415 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21416 if (template_parm_p)
21417 pop_deferring_access_checks ();
21418 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21419 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21420
21421 return default_argument;
21422 }
21423
21424 /* Parse a function-body.
21425
21426 function-body:
21427 compound_statement */
21428
21429 static void
21430 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21431 {
21432 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21433 ? BCS_TRY_BLOCK : BCS_NORMAL),
21434 true);
21435 }
21436
21437 /* Parse a ctor-initializer-opt followed by a function-body. Return
21438 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21439 is true we are parsing a function-try-block. */
21440
21441 static bool
21442 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21443 bool in_function_try_block)
21444 {
21445 tree body, list;
21446 bool ctor_initializer_p;
21447 const bool check_body_p =
21448 DECL_CONSTRUCTOR_P (current_function_decl)
21449 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21450 tree last = NULL;
21451
21452 /* Begin the function body. */
21453 body = begin_function_body ();
21454 /* Parse the optional ctor-initializer. */
21455 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21456
21457 /* If we're parsing a constexpr constructor definition, we need
21458 to check that the constructor body is indeed empty. However,
21459 before we get to cp_parser_function_body lot of junk has been
21460 generated, so we can't just check that we have an empty block.
21461 Rather we take a snapshot of the outermost block, and check whether
21462 cp_parser_function_body changed its state. */
21463 if (check_body_p)
21464 {
21465 list = cur_stmt_list;
21466 if (STATEMENT_LIST_TAIL (list))
21467 last = STATEMENT_LIST_TAIL (list)->stmt;
21468 }
21469 /* Parse the function-body. */
21470 cp_parser_function_body (parser, in_function_try_block);
21471 if (check_body_p)
21472 check_constexpr_ctor_body (last, list, /*complain=*/true);
21473 /* Finish the function body. */
21474 finish_function_body (body);
21475
21476 return ctor_initializer_p;
21477 }
21478
21479 /* Parse an initializer.
21480
21481 initializer:
21482 = initializer-clause
21483 ( expression-list )
21484
21485 Returns an expression representing the initializer. If no
21486 initializer is present, NULL_TREE is returned.
21487
21488 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21489 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21490 set to TRUE if there is no initializer present. If there is an
21491 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21492 is set to true; otherwise it is set to false. */
21493
21494 static tree
21495 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21496 bool* non_constant_p)
21497 {
21498 cp_token *token;
21499 tree init;
21500
21501 /* Peek at the next token. */
21502 token = cp_lexer_peek_token (parser->lexer);
21503
21504 /* Let our caller know whether or not this initializer was
21505 parenthesized. */
21506 *is_direct_init = (token->type != CPP_EQ);
21507 /* Assume that the initializer is constant. */
21508 *non_constant_p = false;
21509
21510 if (token->type == CPP_EQ)
21511 {
21512 /* Consume the `='. */
21513 cp_lexer_consume_token (parser->lexer);
21514 /* Parse the initializer-clause. */
21515 init = cp_parser_initializer_clause (parser, non_constant_p);
21516 }
21517 else if (token->type == CPP_OPEN_PAREN)
21518 {
21519 vec<tree, va_gc> *vec;
21520 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21521 /*cast_p=*/false,
21522 /*allow_expansion_p=*/true,
21523 non_constant_p);
21524 if (vec == NULL)
21525 return error_mark_node;
21526 init = build_tree_list_vec (vec);
21527 release_tree_vector (vec);
21528 }
21529 else if (token->type == CPP_OPEN_BRACE)
21530 {
21531 cp_lexer_set_source_position (parser->lexer);
21532 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21533 init = cp_parser_braced_list (parser, non_constant_p);
21534 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21535 }
21536 else
21537 {
21538 /* Anything else is an error. */
21539 cp_parser_error (parser, "expected initializer");
21540 init = error_mark_node;
21541 }
21542
21543 if (check_for_bare_parameter_packs (init))
21544 init = error_mark_node;
21545
21546 return init;
21547 }
21548
21549 /* Parse an initializer-clause.
21550
21551 initializer-clause:
21552 assignment-expression
21553 braced-init-list
21554
21555 Returns an expression representing the initializer.
21556
21557 If the `assignment-expression' production is used the value
21558 returned is simply a representation for the expression.
21559
21560 Otherwise, calls cp_parser_braced_list. */
21561
21562 static cp_expr
21563 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21564 {
21565 cp_expr initializer;
21566
21567 /* Assume the expression is constant. */
21568 *non_constant_p = false;
21569
21570 /* If it is not a `{', then we are looking at an
21571 assignment-expression. */
21572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21573 {
21574 initializer
21575 = cp_parser_constant_expression (parser,
21576 /*allow_non_constant_p=*/true,
21577 non_constant_p);
21578 }
21579 else
21580 initializer = cp_parser_braced_list (parser, non_constant_p);
21581
21582 return initializer;
21583 }
21584
21585 /* Parse a brace-enclosed initializer list.
21586
21587 braced-init-list:
21588 { initializer-list , [opt] }
21589 { }
21590
21591 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21592 the elements of the initializer-list (or NULL, if the last
21593 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21594 NULL_TREE. There is no way to detect whether or not the optional
21595 trailing `,' was provided. NON_CONSTANT_P is as for
21596 cp_parser_initializer. */
21597
21598 static cp_expr
21599 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21600 {
21601 tree initializer;
21602 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21603
21604 /* Consume the `{' token. */
21605 cp_lexer_consume_token (parser->lexer);
21606 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21607 initializer = make_node (CONSTRUCTOR);
21608 /* If it's not a `}', then there is a non-trivial initializer. */
21609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21610 {
21611 /* Parse the initializer list. */
21612 CONSTRUCTOR_ELTS (initializer)
21613 = cp_parser_initializer_list (parser, non_constant_p);
21614 /* A trailing `,' token is allowed. */
21615 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21616 cp_lexer_consume_token (parser->lexer);
21617 }
21618 else
21619 *non_constant_p = false;
21620 /* Now, there should be a trailing `}'. */
21621 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21622 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21623 TREE_TYPE (initializer) = init_list_type_node;
21624
21625 cp_expr result (initializer);
21626 /* Build a location of the form:
21627 { ... }
21628 ^~~~~~~
21629 with caret==start at the open brace, finish at the close brace. */
21630 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21631 result.set_location (combined_loc);
21632 return result;
21633 }
21634
21635 /* Consume tokens up to, and including, the next non-nested closing `]'.
21636 Returns true iff we found a closing `]'. */
21637
21638 static bool
21639 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21640 {
21641 unsigned square_depth = 0;
21642
21643 while (true)
21644 {
21645 cp_token * token = cp_lexer_peek_token (parser->lexer);
21646
21647 switch (token->type)
21648 {
21649 case CPP_EOF:
21650 case CPP_PRAGMA_EOL:
21651 /* If we've run out of tokens, then there is no closing `]'. */
21652 return false;
21653
21654 case CPP_OPEN_SQUARE:
21655 ++square_depth;
21656 break;
21657
21658 case CPP_CLOSE_SQUARE:
21659 if (!square_depth--)
21660 {
21661 cp_lexer_consume_token (parser->lexer);
21662 return true;
21663 }
21664 break;
21665
21666 default:
21667 break;
21668 }
21669
21670 /* Consume the token. */
21671 cp_lexer_consume_token (parser->lexer);
21672 }
21673 }
21674
21675 /* Return true if we are looking at an array-designator, false otherwise. */
21676
21677 static bool
21678 cp_parser_array_designator_p (cp_parser *parser)
21679 {
21680 /* Consume the `['. */
21681 cp_lexer_consume_token (parser->lexer);
21682
21683 cp_lexer_save_tokens (parser->lexer);
21684
21685 /* Skip tokens until the next token is a closing square bracket.
21686 If we find the closing `]', and the next token is a `=', then
21687 we are looking at an array designator. */
21688 bool array_designator_p
21689 = (cp_parser_skip_to_closing_square_bracket (parser)
21690 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21691
21692 /* Roll back the tokens we skipped. */
21693 cp_lexer_rollback_tokens (parser->lexer);
21694
21695 return array_designator_p;
21696 }
21697
21698 /* Parse an initializer-list.
21699
21700 initializer-list:
21701 initializer-clause ... [opt]
21702 initializer-list , initializer-clause ... [opt]
21703
21704 GNU Extension:
21705
21706 initializer-list:
21707 designation initializer-clause ...[opt]
21708 initializer-list , designation initializer-clause ...[opt]
21709
21710 designation:
21711 . identifier =
21712 identifier :
21713 [ constant-expression ] =
21714
21715 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21716 for the initializer. If the INDEX of the elt is non-NULL, it is the
21717 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21718 as for cp_parser_initializer. */
21719
21720 static vec<constructor_elt, va_gc> *
21721 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21722 {
21723 vec<constructor_elt, va_gc> *v = NULL;
21724
21725 /* Assume all of the expressions are constant. */
21726 *non_constant_p = false;
21727
21728 /* Parse the rest of the list. */
21729 while (true)
21730 {
21731 cp_token *token;
21732 tree designator;
21733 tree initializer;
21734 bool clause_non_constant_p;
21735
21736 /* If the next token is an identifier and the following one is a
21737 colon, we are looking at the GNU designated-initializer
21738 syntax. */
21739 if (cp_parser_allow_gnu_extensions_p (parser)
21740 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21741 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21742 {
21743 /* Warn the user that they are using an extension. */
21744 pedwarn (input_location, OPT_Wpedantic,
21745 "ISO C++ does not allow designated initializers");
21746 /* Consume the identifier. */
21747 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21748 /* Consume the `:'. */
21749 cp_lexer_consume_token (parser->lexer);
21750 }
21751 /* Also handle the C99 syntax, '. id ='. */
21752 else if (cp_parser_allow_gnu_extensions_p (parser)
21753 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21754 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21755 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21756 {
21757 /* Warn the user that they are using an extension. */
21758 pedwarn (input_location, OPT_Wpedantic,
21759 "ISO C++ does not allow C99 designated initializers");
21760 /* Consume the `.'. */
21761 cp_lexer_consume_token (parser->lexer);
21762 /* Consume the identifier. */
21763 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21764 /* Consume the `='. */
21765 cp_lexer_consume_token (parser->lexer);
21766 }
21767 /* Also handle C99 array designators, '[ const ] ='. */
21768 else if (cp_parser_allow_gnu_extensions_p (parser)
21769 && !c_dialect_objc ()
21770 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21771 {
21772 /* In C++11, [ could start a lambda-introducer. */
21773 bool non_const = false;
21774
21775 cp_parser_parse_tentatively (parser);
21776
21777 if (!cp_parser_array_designator_p (parser))
21778 {
21779 cp_parser_simulate_error (parser);
21780 designator = NULL_TREE;
21781 }
21782 else
21783 {
21784 designator = cp_parser_constant_expression (parser, true,
21785 &non_const);
21786 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21787 cp_parser_require (parser, CPP_EQ, RT_EQ);
21788 }
21789
21790 if (!cp_parser_parse_definitely (parser))
21791 designator = NULL_TREE;
21792 else if (non_const)
21793 require_potential_rvalue_constant_expression (designator);
21794 }
21795 else
21796 designator = NULL_TREE;
21797
21798 /* Parse the initializer. */
21799 initializer = cp_parser_initializer_clause (parser,
21800 &clause_non_constant_p);
21801 /* If any clause is non-constant, so is the entire initializer. */
21802 if (clause_non_constant_p)
21803 *non_constant_p = true;
21804
21805 /* If we have an ellipsis, this is an initializer pack
21806 expansion. */
21807 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21808 {
21809 /* Consume the `...'. */
21810 cp_lexer_consume_token (parser->lexer);
21811
21812 /* Turn the initializer into an initializer expansion. */
21813 initializer = make_pack_expansion (initializer);
21814 }
21815
21816 /* Add it to the vector. */
21817 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21818
21819 /* If the next token is not a comma, we have reached the end of
21820 the list. */
21821 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21822 break;
21823
21824 /* Peek at the next token. */
21825 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21826 /* If the next token is a `}', then we're still done. An
21827 initializer-clause can have a trailing `,' after the
21828 initializer-list and before the closing `}'. */
21829 if (token->type == CPP_CLOSE_BRACE)
21830 break;
21831
21832 /* Consume the `,' token. */
21833 cp_lexer_consume_token (parser->lexer);
21834 }
21835
21836 return v;
21837 }
21838
21839 /* Classes [gram.class] */
21840
21841 /* Parse a class-name.
21842
21843 class-name:
21844 identifier
21845 template-id
21846
21847 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21848 to indicate that names looked up in dependent types should be
21849 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21850 keyword has been used to indicate that the name that appears next
21851 is a template. TAG_TYPE indicates the explicit tag given before
21852 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21853 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21854 is the class being defined in a class-head. If ENUM_OK is TRUE,
21855 enum-names are also accepted.
21856
21857 Returns the TYPE_DECL representing the class. */
21858
21859 static tree
21860 cp_parser_class_name (cp_parser *parser,
21861 bool typename_keyword_p,
21862 bool template_keyword_p,
21863 enum tag_types tag_type,
21864 bool check_dependency_p,
21865 bool class_head_p,
21866 bool is_declaration,
21867 bool enum_ok)
21868 {
21869 tree decl;
21870 tree scope;
21871 bool typename_p;
21872 cp_token *token;
21873 tree identifier = NULL_TREE;
21874
21875 /* All class-names start with an identifier. */
21876 token = cp_lexer_peek_token (parser->lexer);
21877 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21878 {
21879 cp_parser_error (parser, "expected class-name");
21880 return error_mark_node;
21881 }
21882
21883 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21884 to a template-id, so we save it here. */
21885 scope = parser->scope;
21886 if (scope == error_mark_node)
21887 return error_mark_node;
21888
21889 /* Any name names a type if we're following the `typename' keyword
21890 in a qualified name where the enclosing scope is type-dependent. */
21891 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21892 && dependent_type_p (scope));
21893 /* Handle the common case (an identifier, but not a template-id)
21894 efficiently. */
21895 if (token->type == CPP_NAME
21896 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21897 {
21898 cp_token *identifier_token;
21899 bool ambiguous_p;
21900
21901 /* Look for the identifier. */
21902 identifier_token = cp_lexer_peek_token (parser->lexer);
21903 ambiguous_p = identifier_token->error_reported;
21904 identifier = cp_parser_identifier (parser);
21905 /* If the next token isn't an identifier, we are certainly not
21906 looking at a class-name. */
21907 if (identifier == error_mark_node)
21908 decl = error_mark_node;
21909 /* If we know this is a type-name, there's no need to look it
21910 up. */
21911 else if (typename_p)
21912 decl = identifier;
21913 else
21914 {
21915 tree ambiguous_decls;
21916 /* If we already know that this lookup is ambiguous, then
21917 we've already issued an error message; there's no reason
21918 to check again. */
21919 if (ambiguous_p)
21920 {
21921 cp_parser_simulate_error (parser);
21922 return error_mark_node;
21923 }
21924 /* If the next token is a `::', then the name must be a type
21925 name.
21926
21927 [basic.lookup.qual]
21928
21929 During the lookup for a name preceding the :: scope
21930 resolution operator, object, function, and enumerator
21931 names are ignored. */
21932 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21933 tag_type = scope_type;
21934 /* Look up the name. */
21935 decl = cp_parser_lookup_name (parser, identifier,
21936 tag_type,
21937 /*is_template=*/false,
21938 /*is_namespace=*/false,
21939 check_dependency_p,
21940 &ambiguous_decls,
21941 identifier_token->location);
21942 if (ambiguous_decls)
21943 {
21944 if (cp_parser_parsing_tentatively (parser))
21945 cp_parser_simulate_error (parser);
21946 return error_mark_node;
21947 }
21948 }
21949 }
21950 else
21951 {
21952 /* Try a template-id. */
21953 decl = cp_parser_template_id (parser, template_keyword_p,
21954 check_dependency_p,
21955 tag_type,
21956 is_declaration);
21957 if (decl == error_mark_node)
21958 return error_mark_node;
21959 }
21960
21961 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21962
21963 /* If this is a typename, create a TYPENAME_TYPE. */
21964 if (typename_p && decl != error_mark_node)
21965 {
21966 decl = make_typename_type (scope, decl, typename_type,
21967 /*complain=*/tf_error);
21968 if (decl != error_mark_node)
21969 decl = TYPE_NAME (decl);
21970 }
21971
21972 decl = strip_using_decl (decl);
21973
21974 /* Check to see that it is really the name of a class. */
21975 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21976 && identifier_p (TREE_OPERAND (decl, 0))
21977 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21978 /* Situations like this:
21979
21980 template <typename T> struct A {
21981 typename T::template X<int>::I i;
21982 };
21983
21984 are problematic. Is `T::template X<int>' a class-name? The
21985 standard does not seem to be definitive, but there is no other
21986 valid interpretation of the following `::'. Therefore, those
21987 names are considered class-names. */
21988 {
21989 decl = make_typename_type (scope, decl, tag_type, tf_error);
21990 if (decl != error_mark_node)
21991 decl = TYPE_NAME (decl);
21992 }
21993 else if (TREE_CODE (decl) != TYPE_DECL
21994 || TREE_TYPE (decl) == error_mark_node
21995 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21996 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21997 /* In Objective-C 2.0, a classname followed by '.' starts a
21998 dot-syntax expression, and it's not a type-name. */
21999 || (c_dialect_objc ()
22000 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22001 && objc_is_class_name (decl)))
22002 decl = error_mark_node;
22003
22004 if (decl == error_mark_node)
22005 cp_parser_error (parser, "expected class-name");
22006 else if (identifier && !parser->scope)
22007 maybe_note_name_used_in_class (identifier, decl);
22008
22009 return decl;
22010 }
22011
22012 /* Parse a class-specifier.
22013
22014 class-specifier:
22015 class-head { member-specification [opt] }
22016
22017 Returns the TREE_TYPE representing the class. */
22018
22019 static tree
22020 cp_parser_class_specifier_1 (cp_parser* parser)
22021 {
22022 tree type;
22023 tree attributes = NULL_TREE;
22024 bool nested_name_specifier_p;
22025 unsigned saved_num_template_parameter_lists;
22026 bool saved_in_function_body;
22027 unsigned char in_statement;
22028 bool in_switch_statement_p;
22029 bool saved_in_unbraced_linkage_specification_p;
22030 tree old_scope = NULL_TREE;
22031 tree scope = NULL_TREE;
22032 cp_token *closing_brace;
22033
22034 push_deferring_access_checks (dk_no_deferred);
22035
22036 /* Parse the class-head. */
22037 type = cp_parser_class_head (parser,
22038 &nested_name_specifier_p);
22039 /* If the class-head was a semantic disaster, skip the entire body
22040 of the class. */
22041 if (!type)
22042 {
22043 cp_parser_skip_to_end_of_block_or_statement (parser);
22044 pop_deferring_access_checks ();
22045 return error_mark_node;
22046 }
22047
22048 /* Look for the `{'. */
22049 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22050 {
22051 pop_deferring_access_checks ();
22052 return error_mark_node;
22053 }
22054
22055 cp_ensure_no_omp_declare_simd (parser);
22056 cp_ensure_no_oacc_routine (parser);
22057
22058 /* Issue an error message if type-definitions are forbidden here. */
22059 cp_parser_check_type_definition (parser);
22060 /* Remember that we are defining one more class. */
22061 ++parser->num_classes_being_defined;
22062 /* Inside the class, surrounding template-parameter-lists do not
22063 apply. */
22064 saved_num_template_parameter_lists
22065 = parser->num_template_parameter_lists;
22066 parser->num_template_parameter_lists = 0;
22067 /* We are not in a function body. */
22068 saved_in_function_body = parser->in_function_body;
22069 parser->in_function_body = false;
22070 /* Or in a loop. */
22071 in_statement = parser->in_statement;
22072 parser->in_statement = 0;
22073 /* Or in a switch. */
22074 in_switch_statement_p = parser->in_switch_statement_p;
22075 parser->in_switch_statement_p = false;
22076 /* We are not immediately inside an extern "lang" block. */
22077 saved_in_unbraced_linkage_specification_p
22078 = parser->in_unbraced_linkage_specification_p;
22079 parser->in_unbraced_linkage_specification_p = false;
22080
22081 // Associate constraints with the type.
22082 if (flag_concepts)
22083 type = associate_classtype_constraints (type);
22084
22085 /* Start the class. */
22086 if (nested_name_specifier_p)
22087 {
22088 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22089 old_scope = push_inner_scope (scope);
22090 }
22091 type = begin_class_definition (type);
22092
22093 if (type == error_mark_node)
22094 /* If the type is erroneous, skip the entire body of the class. */
22095 cp_parser_skip_to_closing_brace (parser);
22096 else
22097 /* Parse the member-specification. */
22098 cp_parser_member_specification_opt (parser);
22099
22100 /* Look for the trailing `}'. */
22101 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22102 /* Look for trailing attributes to apply to this class. */
22103 if (cp_parser_allow_gnu_extensions_p (parser))
22104 attributes = cp_parser_gnu_attributes_opt (parser);
22105 if (type != error_mark_node)
22106 type = finish_struct (type, attributes);
22107 if (nested_name_specifier_p)
22108 pop_inner_scope (old_scope, scope);
22109
22110 /* We've finished a type definition. Check for the common syntax
22111 error of forgetting a semicolon after the definition. We need to
22112 be careful, as we can't just check for not-a-semicolon and be done
22113 with it; the user might have typed:
22114
22115 class X { } c = ...;
22116 class X { } *p = ...;
22117
22118 and so forth. Instead, enumerate all the possible tokens that
22119 might follow this production; if we don't see one of them, then
22120 complain and silently insert the semicolon. */
22121 {
22122 cp_token *token = cp_lexer_peek_token (parser->lexer);
22123 bool want_semicolon = true;
22124
22125 if (cp_next_tokens_can_be_std_attribute_p (parser))
22126 /* Don't try to parse c++11 attributes here. As per the
22127 grammar, that should be a task for
22128 cp_parser_decl_specifier_seq. */
22129 want_semicolon = false;
22130
22131 switch (token->type)
22132 {
22133 case CPP_NAME:
22134 case CPP_SEMICOLON:
22135 case CPP_MULT:
22136 case CPP_AND:
22137 case CPP_OPEN_PAREN:
22138 case CPP_CLOSE_PAREN:
22139 case CPP_COMMA:
22140 want_semicolon = false;
22141 break;
22142
22143 /* While it's legal for type qualifiers and storage class
22144 specifiers to follow type definitions in the grammar, only
22145 compiler testsuites contain code like that. Assume that if
22146 we see such code, then what we're really seeing is a case
22147 like:
22148
22149 class X { }
22150 const <type> var = ...;
22151
22152 or
22153
22154 class Y { }
22155 static <type> func (...) ...
22156
22157 i.e. the qualifier or specifier applies to the next
22158 declaration. To do so, however, we need to look ahead one
22159 more token to see if *that* token is a type specifier.
22160
22161 This code could be improved to handle:
22162
22163 class Z { }
22164 static const <type> var = ...; */
22165 case CPP_KEYWORD:
22166 if (keyword_is_decl_specifier (token->keyword))
22167 {
22168 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22169
22170 /* Handling user-defined types here would be nice, but very
22171 tricky. */
22172 want_semicolon
22173 = (lookahead->type == CPP_KEYWORD
22174 && keyword_begins_type_specifier (lookahead->keyword));
22175 }
22176 break;
22177 default:
22178 break;
22179 }
22180
22181 /* If we don't have a type, then something is very wrong and we
22182 shouldn't try to do anything clever. Likewise for not seeing the
22183 closing brace. */
22184 if (closing_brace && TYPE_P (type) && want_semicolon)
22185 {
22186 /* Locate the closing brace. */
22187 cp_token_position prev
22188 = cp_lexer_previous_token_position (parser->lexer);
22189 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22190 location_t loc = prev_token->location;
22191
22192 /* We want to suggest insertion of a ';' immediately *after* the
22193 closing brace, so, if we can, offset the location by 1 column. */
22194 location_t next_loc = loc;
22195 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22196 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22197
22198 rich_location richloc (line_table, next_loc);
22199
22200 /* If we successfully offset the location, suggest the fix-it. */
22201 if (next_loc != loc)
22202 richloc.add_fixit_insert_before (next_loc, ";");
22203
22204 if (CLASSTYPE_DECLARED_CLASS (type))
22205 error_at_rich_loc (&richloc,
22206 "expected %<;%> after class definition");
22207 else if (TREE_CODE (type) == RECORD_TYPE)
22208 error_at_rich_loc (&richloc,
22209 "expected %<;%> after struct definition");
22210 else if (TREE_CODE (type) == UNION_TYPE)
22211 error_at_rich_loc (&richloc,
22212 "expected %<;%> after union definition");
22213 else
22214 gcc_unreachable ();
22215
22216 /* Unget one token and smash it to look as though we encountered
22217 a semicolon in the input stream. */
22218 cp_lexer_set_token_position (parser->lexer, prev);
22219 token = cp_lexer_peek_token (parser->lexer);
22220 token->type = CPP_SEMICOLON;
22221 token->keyword = RID_MAX;
22222 }
22223 }
22224
22225 /* If this class is not itself within the scope of another class,
22226 then we need to parse the bodies of all of the queued function
22227 definitions. Note that the queued functions defined in a class
22228 are not always processed immediately following the
22229 class-specifier for that class. Consider:
22230
22231 struct A {
22232 struct B { void f() { sizeof (A); } };
22233 };
22234
22235 If `f' were processed before the processing of `A' were
22236 completed, there would be no way to compute the size of `A'.
22237 Note that the nesting we are interested in here is lexical --
22238 not the semantic nesting given by TYPE_CONTEXT. In particular,
22239 for:
22240
22241 struct A { struct B; };
22242 struct A::B { void f() { } };
22243
22244 there is no need to delay the parsing of `A::B::f'. */
22245 if (--parser->num_classes_being_defined == 0)
22246 {
22247 tree decl;
22248 tree class_type = NULL_TREE;
22249 tree pushed_scope = NULL_TREE;
22250 unsigned ix;
22251 cp_default_arg_entry *e;
22252 tree save_ccp, save_ccr;
22253
22254 /* In a first pass, parse default arguments to the functions.
22255 Then, in a second pass, parse the bodies of the functions.
22256 This two-phased approach handles cases like:
22257
22258 struct S {
22259 void f() { g(); }
22260 void g(int i = 3);
22261 };
22262
22263 */
22264 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22265 {
22266 decl = e->decl;
22267 /* If there are default arguments that have not yet been processed,
22268 take care of them now. */
22269 if (class_type != e->class_type)
22270 {
22271 if (pushed_scope)
22272 pop_scope (pushed_scope);
22273 class_type = e->class_type;
22274 pushed_scope = push_scope (class_type);
22275 }
22276 /* Make sure that any template parameters are in scope. */
22277 maybe_begin_member_template_processing (decl);
22278 /* Parse the default argument expressions. */
22279 cp_parser_late_parsing_default_args (parser, decl);
22280 /* Remove any template parameters from the symbol table. */
22281 maybe_end_member_template_processing ();
22282 }
22283 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22284 /* Now parse any NSDMIs. */
22285 save_ccp = current_class_ptr;
22286 save_ccr = current_class_ref;
22287 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22288 {
22289 if (class_type != DECL_CONTEXT (decl))
22290 {
22291 if (pushed_scope)
22292 pop_scope (pushed_scope);
22293 class_type = DECL_CONTEXT (decl);
22294 pushed_scope = push_scope (class_type);
22295 }
22296 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22297 cp_parser_late_parsing_nsdmi (parser, decl);
22298 }
22299 vec_safe_truncate (unparsed_nsdmis, 0);
22300 current_class_ptr = save_ccp;
22301 current_class_ref = save_ccr;
22302 if (pushed_scope)
22303 pop_scope (pushed_scope);
22304
22305 /* Now do some post-NSDMI bookkeeping. */
22306 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22307 after_nsdmi_defaulted_late_checks (class_type);
22308 vec_safe_truncate (unparsed_classes, 0);
22309 after_nsdmi_defaulted_late_checks (type);
22310
22311 /* Now parse the body of the functions. */
22312 if (flag_openmp)
22313 {
22314 /* OpenMP UDRs need to be parsed before all other functions. */
22315 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22316 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22317 cp_parser_late_parsing_for_member (parser, decl);
22318 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22319 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22320 cp_parser_late_parsing_for_member (parser, decl);
22321 }
22322 else
22323 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22324 cp_parser_late_parsing_for_member (parser, decl);
22325 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22326 }
22327 else
22328 vec_safe_push (unparsed_classes, type);
22329
22330 /* Put back any saved access checks. */
22331 pop_deferring_access_checks ();
22332
22333 /* Restore saved state. */
22334 parser->in_switch_statement_p = in_switch_statement_p;
22335 parser->in_statement = in_statement;
22336 parser->in_function_body = saved_in_function_body;
22337 parser->num_template_parameter_lists
22338 = saved_num_template_parameter_lists;
22339 parser->in_unbraced_linkage_specification_p
22340 = saved_in_unbraced_linkage_specification_p;
22341
22342 return type;
22343 }
22344
22345 static tree
22346 cp_parser_class_specifier (cp_parser* parser)
22347 {
22348 tree ret;
22349 timevar_push (TV_PARSE_STRUCT);
22350 ret = cp_parser_class_specifier_1 (parser);
22351 timevar_pop (TV_PARSE_STRUCT);
22352 return ret;
22353 }
22354
22355 /* Parse a class-head.
22356
22357 class-head:
22358 class-key identifier [opt] base-clause [opt]
22359 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22360 class-key nested-name-specifier [opt] template-id
22361 base-clause [opt]
22362
22363 class-virt-specifier:
22364 final
22365
22366 GNU Extensions:
22367 class-key attributes identifier [opt] base-clause [opt]
22368 class-key attributes nested-name-specifier identifier base-clause [opt]
22369 class-key attributes nested-name-specifier [opt] template-id
22370 base-clause [opt]
22371
22372 Upon return BASES is initialized to the list of base classes (or
22373 NULL, if there are none) in the same form returned by
22374 cp_parser_base_clause.
22375
22376 Returns the TYPE of the indicated class. Sets
22377 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22378 involving a nested-name-specifier was used, and FALSE otherwise.
22379
22380 Returns error_mark_node if this is not a class-head.
22381
22382 Returns NULL_TREE if the class-head is syntactically valid, but
22383 semantically invalid in a way that means we should skip the entire
22384 body of the class. */
22385
22386 static tree
22387 cp_parser_class_head (cp_parser* parser,
22388 bool* nested_name_specifier_p)
22389 {
22390 tree nested_name_specifier;
22391 enum tag_types class_key;
22392 tree id = NULL_TREE;
22393 tree type = NULL_TREE;
22394 tree attributes;
22395 tree bases;
22396 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22397 bool template_id_p = false;
22398 bool qualified_p = false;
22399 bool invalid_nested_name_p = false;
22400 bool invalid_explicit_specialization_p = false;
22401 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22402 tree pushed_scope = NULL_TREE;
22403 unsigned num_templates;
22404 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22405 /* Assume no nested-name-specifier will be present. */
22406 *nested_name_specifier_p = false;
22407 /* Assume no template parameter lists will be used in defining the
22408 type. */
22409 num_templates = 0;
22410 parser->colon_corrects_to_scope_p = false;
22411
22412 /* Look for the class-key. */
22413 class_key = cp_parser_class_key (parser);
22414 if (class_key == none_type)
22415 return error_mark_node;
22416
22417 location_t class_head_start_location = input_location;
22418
22419 /* Parse the attributes. */
22420 attributes = cp_parser_attributes_opt (parser);
22421
22422 /* If the next token is `::', that is invalid -- but sometimes
22423 people do try to write:
22424
22425 struct ::S {};
22426
22427 Handle this gracefully by accepting the extra qualifier, and then
22428 issuing an error about it later if this really is a
22429 class-head. If it turns out just to be an elaborated type
22430 specifier, remain silent. */
22431 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22432 qualified_p = true;
22433
22434 push_deferring_access_checks (dk_no_check);
22435
22436 /* Determine the name of the class. Begin by looking for an
22437 optional nested-name-specifier. */
22438 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22439 nested_name_specifier
22440 = cp_parser_nested_name_specifier_opt (parser,
22441 /*typename_keyword_p=*/false,
22442 /*check_dependency_p=*/false,
22443 /*type_p=*/true,
22444 /*is_declaration=*/false);
22445 /* If there was a nested-name-specifier, then there *must* be an
22446 identifier. */
22447 if (nested_name_specifier)
22448 {
22449 type_start_token = cp_lexer_peek_token (parser->lexer);
22450 /* Although the grammar says `identifier', it really means
22451 `class-name' or `template-name'. You are only allowed to
22452 define a class that has already been declared with this
22453 syntax.
22454
22455 The proposed resolution for Core Issue 180 says that wherever
22456 you see `class T::X' you should treat `X' as a type-name.
22457
22458 It is OK to define an inaccessible class; for example:
22459
22460 class A { class B; };
22461 class A::B {};
22462
22463 We do not know if we will see a class-name, or a
22464 template-name. We look for a class-name first, in case the
22465 class-name is a template-id; if we looked for the
22466 template-name first we would stop after the template-name. */
22467 cp_parser_parse_tentatively (parser);
22468 type = cp_parser_class_name (parser,
22469 /*typename_keyword_p=*/false,
22470 /*template_keyword_p=*/false,
22471 class_type,
22472 /*check_dependency_p=*/false,
22473 /*class_head_p=*/true,
22474 /*is_declaration=*/false);
22475 /* If that didn't work, ignore the nested-name-specifier. */
22476 if (!cp_parser_parse_definitely (parser))
22477 {
22478 invalid_nested_name_p = true;
22479 type_start_token = cp_lexer_peek_token (parser->lexer);
22480 id = cp_parser_identifier (parser);
22481 if (id == error_mark_node)
22482 id = NULL_TREE;
22483 }
22484 /* If we could not find a corresponding TYPE, treat this
22485 declaration like an unqualified declaration. */
22486 if (type == error_mark_node)
22487 nested_name_specifier = NULL_TREE;
22488 /* Otherwise, count the number of templates used in TYPE and its
22489 containing scopes. */
22490 else
22491 {
22492 tree scope;
22493
22494 for (scope = TREE_TYPE (type);
22495 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22496 scope = get_containing_scope (scope))
22497 if (TYPE_P (scope)
22498 && CLASS_TYPE_P (scope)
22499 && CLASSTYPE_TEMPLATE_INFO (scope)
22500 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22501 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22502 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22503 ++num_templates;
22504 }
22505 }
22506 /* Otherwise, the identifier is optional. */
22507 else
22508 {
22509 /* We don't know whether what comes next is a template-id,
22510 an identifier, or nothing at all. */
22511 cp_parser_parse_tentatively (parser);
22512 /* Check for a template-id. */
22513 type_start_token = cp_lexer_peek_token (parser->lexer);
22514 id = cp_parser_template_id (parser,
22515 /*template_keyword_p=*/false,
22516 /*check_dependency_p=*/true,
22517 class_key,
22518 /*is_declaration=*/true);
22519 /* If that didn't work, it could still be an identifier. */
22520 if (!cp_parser_parse_definitely (parser))
22521 {
22522 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22523 {
22524 type_start_token = cp_lexer_peek_token (parser->lexer);
22525 id = cp_parser_identifier (parser);
22526 }
22527 else
22528 id = NULL_TREE;
22529 }
22530 else
22531 {
22532 template_id_p = true;
22533 ++num_templates;
22534 }
22535 }
22536
22537 pop_deferring_access_checks ();
22538
22539 if (id)
22540 {
22541 cp_parser_check_for_invalid_template_id (parser, id,
22542 class_key,
22543 type_start_token->location);
22544 }
22545 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22546
22547 /* If it's not a `:' or a `{' then we can't really be looking at a
22548 class-head, since a class-head only appears as part of a
22549 class-specifier. We have to detect this situation before calling
22550 xref_tag, since that has irreversible side-effects. */
22551 if (!cp_parser_next_token_starts_class_definition_p (parser))
22552 {
22553 cp_parser_error (parser, "expected %<{%> or %<:%>");
22554 type = error_mark_node;
22555 goto out;
22556 }
22557
22558 /* At this point, we're going ahead with the class-specifier, even
22559 if some other problem occurs. */
22560 cp_parser_commit_to_tentative_parse (parser);
22561 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22562 {
22563 cp_parser_error (parser,
22564 "cannot specify %<override%> for a class");
22565 type = error_mark_node;
22566 goto out;
22567 }
22568 /* Issue the error about the overly-qualified name now. */
22569 if (qualified_p)
22570 {
22571 cp_parser_error (parser,
22572 "global qualification of class name is invalid");
22573 type = error_mark_node;
22574 goto out;
22575 }
22576 else if (invalid_nested_name_p)
22577 {
22578 cp_parser_error (parser,
22579 "qualified name does not name a class");
22580 type = error_mark_node;
22581 goto out;
22582 }
22583 else if (nested_name_specifier)
22584 {
22585 tree scope;
22586
22587 /* Reject typedef-names in class heads. */
22588 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22589 {
22590 error_at (type_start_token->location,
22591 "invalid class name in declaration of %qD",
22592 type);
22593 type = NULL_TREE;
22594 goto done;
22595 }
22596
22597 /* Figure out in what scope the declaration is being placed. */
22598 scope = current_scope ();
22599 /* If that scope does not contain the scope in which the
22600 class was originally declared, the program is invalid. */
22601 if (scope && !is_ancestor (scope, nested_name_specifier))
22602 {
22603 if (at_namespace_scope_p ())
22604 error_at (type_start_token->location,
22605 "declaration of %qD in namespace %qD which does not "
22606 "enclose %qD",
22607 type, scope, nested_name_specifier);
22608 else
22609 error_at (type_start_token->location,
22610 "declaration of %qD in %qD which does not enclose %qD",
22611 type, scope, nested_name_specifier);
22612 type = NULL_TREE;
22613 goto done;
22614 }
22615 /* [dcl.meaning]
22616
22617 A declarator-id shall not be qualified except for the
22618 definition of a ... nested class outside of its class
22619 ... [or] the definition or explicit instantiation of a
22620 class member of a namespace outside of its namespace. */
22621 if (scope == nested_name_specifier)
22622 {
22623 permerror (nested_name_specifier_token_start->location,
22624 "extra qualification not allowed");
22625 nested_name_specifier = NULL_TREE;
22626 num_templates = 0;
22627 }
22628 }
22629 /* An explicit-specialization must be preceded by "template <>". If
22630 it is not, try to recover gracefully. */
22631 if (at_namespace_scope_p ()
22632 && parser->num_template_parameter_lists == 0
22633 && !processing_template_parmlist
22634 && template_id_p)
22635 {
22636 /* Build a location of this form:
22637 struct typename <ARGS>
22638 ^~~~~~~~~~~~~~~~~~~~~~
22639 with caret==start at the start token, and
22640 finishing at the end of the type. */
22641 location_t reported_loc
22642 = make_location (class_head_start_location,
22643 class_head_start_location,
22644 get_finish (type_start_token->location));
22645 rich_location richloc (line_table, reported_loc);
22646 richloc.add_fixit_insert_before (class_head_start_location,
22647 "template <> ");
22648 error_at_rich_loc
22649 (&richloc,
22650 "an explicit specialization must be preceded by %<template <>%>");
22651 invalid_explicit_specialization_p = true;
22652 /* Take the same action that would have been taken by
22653 cp_parser_explicit_specialization. */
22654 ++parser->num_template_parameter_lists;
22655 begin_specialization ();
22656 }
22657 /* There must be no "return" statements between this point and the
22658 end of this function; set "type "to the correct return value and
22659 use "goto done;" to return. */
22660 /* Make sure that the right number of template parameters were
22661 present. */
22662 if (!cp_parser_check_template_parameters (parser, num_templates,
22663 type_start_token->location,
22664 /*declarator=*/NULL))
22665 {
22666 /* If something went wrong, there is no point in even trying to
22667 process the class-definition. */
22668 type = NULL_TREE;
22669 goto done;
22670 }
22671
22672 /* Look up the type. */
22673 if (template_id_p)
22674 {
22675 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22676 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22677 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22678 {
22679 error_at (type_start_token->location,
22680 "function template %qD redeclared as a class template", id);
22681 type = error_mark_node;
22682 }
22683 else
22684 {
22685 type = TREE_TYPE (id);
22686 type = maybe_process_partial_specialization (type);
22687
22688 /* Check the scope while we still know whether or not we had a
22689 nested-name-specifier. */
22690 if (type != error_mark_node)
22691 check_unqualified_spec_or_inst (type, type_start_token->location);
22692 }
22693 if (nested_name_specifier)
22694 pushed_scope = push_scope (nested_name_specifier);
22695 }
22696 else if (nested_name_specifier)
22697 {
22698 tree class_type;
22699
22700 /* Given:
22701
22702 template <typename T> struct S { struct T };
22703 template <typename T> struct S<T>::T { };
22704
22705 we will get a TYPENAME_TYPE when processing the definition of
22706 `S::T'. We need to resolve it to the actual type before we
22707 try to define it. */
22708 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22709 {
22710 class_type = resolve_typename_type (TREE_TYPE (type),
22711 /*only_current_p=*/false);
22712 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22713 type = TYPE_NAME (class_type);
22714 else
22715 {
22716 cp_parser_error (parser, "could not resolve typename type");
22717 type = error_mark_node;
22718 }
22719 }
22720
22721 if (maybe_process_partial_specialization (TREE_TYPE (type))
22722 == error_mark_node)
22723 {
22724 type = NULL_TREE;
22725 goto done;
22726 }
22727
22728 class_type = current_class_type;
22729 /* Enter the scope indicated by the nested-name-specifier. */
22730 pushed_scope = push_scope (nested_name_specifier);
22731 /* Get the canonical version of this type. */
22732 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22733 /* Call push_template_decl if it seems like we should be defining a
22734 template either from the template headers or the type we're
22735 defining, so that we diagnose both extra and missing headers. */
22736 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22737 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22738 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22739 {
22740 type = push_template_decl (type);
22741 if (type == error_mark_node)
22742 {
22743 type = NULL_TREE;
22744 goto done;
22745 }
22746 }
22747
22748 type = TREE_TYPE (type);
22749 *nested_name_specifier_p = true;
22750 }
22751 else /* The name is not a nested name. */
22752 {
22753 /* If the class was unnamed, create a dummy name. */
22754 if (!id)
22755 id = make_anon_name ();
22756 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22757 ? ts_within_enclosing_non_class
22758 : ts_current);
22759 type = xref_tag (class_key, id, tag_scope,
22760 parser->num_template_parameter_lists);
22761 }
22762
22763 /* Indicate whether this class was declared as a `class' or as a
22764 `struct'. */
22765 if (TREE_CODE (type) == RECORD_TYPE)
22766 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22767 cp_parser_check_class_key (class_key, type);
22768
22769 /* If this type was already complete, and we see another definition,
22770 that's an error. */
22771 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22772 {
22773 error_at (type_start_token->location, "redefinition of %q#T",
22774 type);
22775 inform (location_of (type), "previous definition of %q#T",
22776 type);
22777 type = NULL_TREE;
22778 goto done;
22779 }
22780 else if (type == error_mark_node)
22781 type = NULL_TREE;
22782
22783 if (type)
22784 {
22785 /* Apply attributes now, before any use of the class as a template
22786 argument in its base list. */
22787 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22788 fixup_attribute_variants (type);
22789 }
22790
22791 /* We will have entered the scope containing the class; the names of
22792 base classes should be looked up in that context. For example:
22793
22794 struct A { struct B {}; struct C; };
22795 struct A::C : B {};
22796
22797 is valid. */
22798
22799 /* Get the list of base-classes, if there is one. */
22800 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22801 {
22802 /* PR59482: enter the class scope so that base-specifiers are looked
22803 up correctly. */
22804 if (type)
22805 pushclass (type);
22806 bases = cp_parser_base_clause (parser);
22807 /* PR59482: get out of the previously pushed class scope so that the
22808 subsequent pops pop the right thing. */
22809 if (type)
22810 popclass ();
22811 }
22812 else
22813 bases = NULL_TREE;
22814
22815 /* If we're really defining a class, process the base classes.
22816 If they're invalid, fail. */
22817 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22818 xref_basetypes (type, bases);
22819
22820 done:
22821 /* Leave the scope given by the nested-name-specifier. We will
22822 enter the class scope itself while processing the members. */
22823 if (pushed_scope)
22824 pop_scope (pushed_scope);
22825
22826 if (invalid_explicit_specialization_p)
22827 {
22828 end_specialization ();
22829 --parser->num_template_parameter_lists;
22830 }
22831
22832 if (type)
22833 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22834 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22835 CLASSTYPE_FINAL (type) = 1;
22836 out:
22837 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22838 return type;
22839 }
22840
22841 /* Parse a class-key.
22842
22843 class-key:
22844 class
22845 struct
22846 union
22847
22848 Returns the kind of class-key specified, or none_type to indicate
22849 error. */
22850
22851 static enum tag_types
22852 cp_parser_class_key (cp_parser* parser)
22853 {
22854 cp_token *token;
22855 enum tag_types tag_type;
22856
22857 /* Look for the class-key. */
22858 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22859 if (!token)
22860 return none_type;
22861
22862 /* Check to see if the TOKEN is a class-key. */
22863 tag_type = cp_parser_token_is_class_key (token);
22864 if (!tag_type)
22865 cp_parser_error (parser, "expected class-key");
22866 return tag_type;
22867 }
22868
22869 /* Parse a type-parameter-key.
22870
22871 type-parameter-key:
22872 class
22873 typename
22874 */
22875
22876 static void
22877 cp_parser_type_parameter_key (cp_parser* parser)
22878 {
22879 /* Look for the type-parameter-key. */
22880 enum tag_types tag_type = none_type;
22881 cp_token *token = cp_lexer_peek_token (parser->lexer);
22882 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22883 {
22884 cp_lexer_consume_token (parser->lexer);
22885 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22886 /* typename is not allowed in a template template parameter
22887 by the standard until C++1Z. */
22888 pedwarn (token->location, OPT_Wpedantic,
22889 "ISO C++ forbids typename key in template template parameter;"
22890 " use -std=c++1z or -std=gnu++1z");
22891 }
22892 else
22893 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22894
22895 return;
22896 }
22897
22898 /* Parse an (optional) member-specification.
22899
22900 member-specification:
22901 member-declaration member-specification [opt]
22902 access-specifier : member-specification [opt] */
22903
22904 static void
22905 cp_parser_member_specification_opt (cp_parser* parser)
22906 {
22907 while (true)
22908 {
22909 cp_token *token;
22910 enum rid keyword;
22911
22912 /* Peek at the next token. */
22913 token = cp_lexer_peek_token (parser->lexer);
22914 /* If it's a `}', or EOF then we've seen all the members. */
22915 if (token->type == CPP_CLOSE_BRACE
22916 || token->type == CPP_EOF
22917 || token->type == CPP_PRAGMA_EOL)
22918 break;
22919
22920 /* See if this token is a keyword. */
22921 keyword = token->keyword;
22922 switch (keyword)
22923 {
22924 case RID_PUBLIC:
22925 case RID_PROTECTED:
22926 case RID_PRIVATE:
22927 /* Consume the access-specifier. */
22928 cp_lexer_consume_token (parser->lexer);
22929 /* Remember which access-specifier is active. */
22930 current_access_specifier = token->u.value;
22931 /* Look for the `:'. */
22932 cp_parser_require (parser, CPP_COLON, RT_COLON);
22933 break;
22934
22935 default:
22936 /* Accept #pragmas at class scope. */
22937 if (token->type == CPP_PRAGMA)
22938 {
22939 cp_parser_pragma (parser, pragma_member, NULL);
22940 break;
22941 }
22942
22943 /* Otherwise, the next construction must be a
22944 member-declaration. */
22945 cp_parser_member_declaration (parser);
22946 }
22947 }
22948 }
22949
22950 /* Parse a member-declaration.
22951
22952 member-declaration:
22953 decl-specifier-seq [opt] member-declarator-list [opt] ;
22954 function-definition ; [opt]
22955 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22956 using-declaration
22957 template-declaration
22958 alias-declaration
22959
22960 member-declarator-list:
22961 member-declarator
22962 member-declarator-list , member-declarator
22963
22964 member-declarator:
22965 declarator pure-specifier [opt]
22966 declarator constant-initializer [opt]
22967 identifier [opt] : constant-expression
22968
22969 GNU Extensions:
22970
22971 member-declaration:
22972 __extension__ member-declaration
22973
22974 member-declarator:
22975 declarator attributes [opt] pure-specifier [opt]
22976 declarator attributes [opt] constant-initializer [opt]
22977 identifier [opt] attributes [opt] : constant-expression
22978
22979 C++0x Extensions:
22980
22981 member-declaration:
22982 static_assert-declaration */
22983
22984 static void
22985 cp_parser_member_declaration (cp_parser* parser)
22986 {
22987 cp_decl_specifier_seq decl_specifiers;
22988 tree prefix_attributes;
22989 tree decl;
22990 int declares_class_or_enum;
22991 bool friend_p;
22992 cp_token *token = NULL;
22993 cp_token *decl_spec_token_start = NULL;
22994 cp_token *initializer_token_start = NULL;
22995 int saved_pedantic;
22996 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22997
22998 /* Check for the `__extension__' keyword. */
22999 if (cp_parser_extension_opt (parser, &saved_pedantic))
23000 {
23001 /* Recurse. */
23002 cp_parser_member_declaration (parser);
23003 /* Restore the old value of the PEDANTIC flag. */
23004 pedantic = saved_pedantic;
23005
23006 return;
23007 }
23008
23009 /* Check for a template-declaration. */
23010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23011 {
23012 /* An explicit specialization here is an error condition, and we
23013 expect the specialization handler to detect and report this. */
23014 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23015 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23016 cp_parser_explicit_specialization (parser);
23017 else
23018 cp_parser_template_declaration (parser, /*member_p=*/true);
23019
23020 return;
23021 }
23022 /* Check for a template introduction. */
23023 else if (cp_parser_template_declaration_after_export (parser, true))
23024 return;
23025
23026 /* Check for a using-declaration. */
23027 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23028 {
23029 if (cxx_dialect < cxx11)
23030 {
23031 /* Parse the using-declaration. */
23032 cp_parser_using_declaration (parser,
23033 /*access_declaration_p=*/false);
23034 return;
23035 }
23036 else
23037 {
23038 tree decl;
23039 bool alias_decl_expected;
23040 cp_parser_parse_tentatively (parser);
23041 decl = cp_parser_alias_declaration (parser);
23042 /* Note that if we actually see the '=' token after the
23043 identifier, cp_parser_alias_declaration commits the
23044 tentative parse. In that case, we really expect an
23045 alias-declaration. Otherwise, we expect a using
23046 declaration. */
23047 alias_decl_expected =
23048 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23049 cp_parser_parse_definitely (parser);
23050
23051 if (alias_decl_expected)
23052 finish_member_declaration (decl);
23053 else
23054 cp_parser_using_declaration (parser,
23055 /*access_declaration_p=*/false);
23056 return;
23057 }
23058 }
23059
23060 /* Check for @defs. */
23061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23062 {
23063 tree ivar, member;
23064 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23065 ivar = ivar_chains;
23066 while (ivar)
23067 {
23068 member = ivar;
23069 ivar = TREE_CHAIN (member);
23070 TREE_CHAIN (member) = NULL_TREE;
23071 finish_member_declaration (member);
23072 }
23073 return;
23074 }
23075
23076 /* If the next token is `static_assert' we have a static assertion. */
23077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23078 {
23079 cp_parser_static_assert (parser, /*member_p=*/true);
23080 return;
23081 }
23082
23083 parser->colon_corrects_to_scope_p = false;
23084
23085 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23086 goto out;
23087
23088 /* Parse the decl-specifier-seq. */
23089 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23090 cp_parser_decl_specifier_seq (parser,
23091 CP_PARSER_FLAGS_OPTIONAL,
23092 &decl_specifiers,
23093 &declares_class_or_enum);
23094 /* Check for an invalid type-name. */
23095 if (!decl_specifiers.any_type_specifiers_p
23096 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23097 goto out;
23098 /* If there is no declarator, then the decl-specifier-seq should
23099 specify a type. */
23100 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23101 {
23102 /* If there was no decl-specifier-seq, and the next token is a
23103 `;', then we have something like:
23104
23105 struct S { ; };
23106
23107 [class.mem]
23108
23109 Each member-declaration shall declare at least one member
23110 name of the class. */
23111 if (!decl_specifiers.any_specifiers_p)
23112 {
23113 cp_token *token = cp_lexer_peek_token (parser->lexer);
23114 if (!in_system_header_at (token->location))
23115 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23116 }
23117 else
23118 {
23119 tree type;
23120
23121 /* See if this declaration is a friend. */
23122 friend_p = cp_parser_friend_p (&decl_specifiers);
23123 /* If there were decl-specifiers, check to see if there was
23124 a class-declaration. */
23125 type = check_tag_decl (&decl_specifiers,
23126 /*explicit_type_instantiation_p=*/false);
23127 /* Nested classes have already been added to the class, but
23128 a `friend' needs to be explicitly registered. */
23129 if (friend_p)
23130 {
23131 /* If the `friend' keyword was present, the friend must
23132 be introduced with a class-key. */
23133 if (!declares_class_or_enum && cxx_dialect < cxx11)
23134 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23135 "in C++03 a class-key must be used "
23136 "when declaring a friend");
23137 /* In this case:
23138
23139 template <typename T> struct A {
23140 friend struct A<T>::B;
23141 };
23142
23143 A<T>::B will be represented by a TYPENAME_TYPE, and
23144 therefore not recognized by check_tag_decl. */
23145 if (!type)
23146 {
23147 type = decl_specifiers.type;
23148 if (type && TREE_CODE (type) == TYPE_DECL)
23149 type = TREE_TYPE (type);
23150 }
23151 if (!type || !TYPE_P (type))
23152 error_at (decl_spec_token_start->location,
23153 "friend declaration does not name a class or "
23154 "function");
23155 else
23156 make_friend_class (current_class_type, type,
23157 /*complain=*/true);
23158 }
23159 /* If there is no TYPE, an error message will already have
23160 been issued. */
23161 else if (!type || type == error_mark_node)
23162 ;
23163 /* An anonymous aggregate has to be handled specially; such
23164 a declaration really declares a data member (with a
23165 particular type), as opposed to a nested class. */
23166 else if (ANON_AGGR_TYPE_P (type))
23167 {
23168 /* C++11 9.5/6. */
23169 if (decl_specifiers.storage_class != sc_none)
23170 error_at (decl_spec_token_start->location,
23171 "a storage class on an anonymous aggregate "
23172 "in class scope is not allowed");
23173
23174 /* Remove constructors and such from TYPE, now that we
23175 know it is an anonymous aggregate. */
23176 fixup_anonymous_aggr (type);
23177 /* And make the corresponding data member. */
23178 decl = build_decl (decl_spec_token_start->location,
23179 FIELD_DECL, NULL_TREE, type);
23180 /* Add it to the class. */
23181 finish_member_declaration (decl);
23182 }
23183 else
23184 cp_parser_check_access_in_redeclaration
23185 (TYPE_NAME (type),
23186 decl_spec_token_start->location);
23187 }
23188 }
23189 else
23190 {
23191 bool assume_semicolon = false;
23192
23193 /* Clear attributes from the decl_specifiers but keep them
23194 around as prefix attributes that apply them to the entity
23195 being declared. */
23196 prefix_attributes = decl_specifiers.attributes;
23197 decl_specifiers.attributes = NULL_TREE;
23198
23199 /* See if these declarations will be friends. */
23200 friend_p = cp_parser_friend_p (&decl_specifiers);
23201
23202 /* Keep going until we hit the `;' at the end of the
23203 declaration. */
23204 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23205 {
23206 tree attributes = NULL_TREE;
23207 tree first_attribute;
23208
23209 /* Peek at the next token. */
23210 token = cp_lexer_peek_token (parser->lexer);
23211
23212 /* Check for a bitfield declaration. */
23213 if (token->type == CPP_COLON
23214 || (token->type == CPP_NAME
23215 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23216 == CPP_COLON))
23217 {
23218 tree identifier;
23219 tree width;
23220
23221 /* Get the name of the bitfield. Note that we cannot just
23222 check TOKEN here because it may have been invalidated by
23223 the call to cp_lexer_peek_nth_token above. */
23224 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23225 identifier = cp_parser_identifier (parser);
23226 else
23227 identifier = NULL_TREE;
23228
23229 /* Consume the `:' token. */
23230 cp_lexer_consume_token (parser->lexer);
23231 /* Get the width of the bitfield. */
23232 width
23233 = cp_parser_constant_expression (parser);
23234
23235 /* Look for attributes that apply to the bitfield. */
23236 attributes = cp_parser_attributes_opt (parser);
23237 /* Remember which attributes are prefix attributes and
23238 which are not. */
23239 first_attribute = attributes;
23240 /* Combine the attributes. */
23241 attributes = chainon (prefix_attributes, attributes);
23242
23243 /* Create the bitfield declaration. */
23244 decl = grokbitfield (identifier
23245 ? make_id_declarator (NULL_TREE,
23246 identifier,
23247 sfk_none)
23248 : NULL,
23249 &decl_specifiers,
23250 width,
23251 attributes);
23252 }
23253 else
23254 {
23255 cp_declarator *declarator;
23256 tree initializer;
23257 tree asm_specification;
23258 int ctor_dtor_or_conv_p;
23259
23260 /* Parse the declarator. */
23261 declarator
23262 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23263 &ctor_dtor_or_conv_p,
23264 /*parenthesized_p=*/NULL,
23265 /*member_p=*/true,
23266 friend_p);
23267
23268 /* If something went wrong parsing the declarator, make sure
23269 that we at least consume some tokens. */
23270 if (declarator == cp_error_declarator)
23271 {
23272 /* Skip to the end of the statement. */
23273 cp_parser_skip_to_end_of_statement (parser);
23274 /* If the next token is not a semicolon, that is
23275 probably because we just skipped over the body of
23276 a function. So, we consume a semicolon if
23277 present, but do not issue an error message if it
23278 is not present. */
23279 if (cp_lexer_next_token_is (parser->lexer,
23280 CPP_SEMICOLON))
23281 cp_lexer_consume_token (parser->lexer);
23282 goto out;
23283 }
23284
23285 if (declares_class_or_enum & 2)
23286 cp_parser_check_for_definition_in_return_type
23287 (declarator, decl_specifiers.type,
23288 decl_specifiers.locations[ds_type_spec]);
23289
23290 /* Look for an asm-specification. */
23291 asm_specification = cp_parser_asm_specification_opt (parser);
23292 /* Look for attributes that apply to the declaration. */
23293 attributes = cp_parser_attributes_opt (parser);
23294 /* Remember which attributes are prefix attributes and
23295 which are not. */
23296 first_attribute = attributes;
23297 /* Combine the attributes. */
23298 attributes = chainon (prefix_attributes, attributes);
23299
23300 /* If it's an `=', then we have a constant-initializer or a
23301 pure-specifier. It is not correct to parse the
23302 initializer before registering the member declaration
23303 since the member declaration should be in scope while
23304 its initializer is processed. However, the rest of the
23305 front end does not yet provide an interface that allows
23306 us to handle this correctly. */
23307 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23308 {
23309 /* In [class.mem]:
23310
23311 A pure-specifier shall be used only in the declaration of
23312 a virtual function.
23313
23314 A member-declarator can contain a constant-initializer
23315 only if it declares a static member of integral or
23316 enumeration type.
23317
23318 Therefore, if the DECLARATOR is for a function, we look
23319 for a pure-specifier; otherwise, we look for a
23320 constant-initializer. When we call `grokfield', it will
23321 perform more stringent semantics checks. */
23322 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23323 if (function_declarator_p (declarator)
23324 || (decl_specifiers.type
23325 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23326 && declarator->kind == cdk_id
23327 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23328 == FUNCTION_TYPE)))
23329 initializer = cp_parser_pure_specifier (parser);
23330 else if (decl_specifiers.storage_class != sc_static)
23331 initializer = cp_parser_save_nsdmi (parser);
23332 else if (cxx_dialect >= cxx11)
23333 {
23334 bool nonconst;
23335 /* Don't require a constant rvalue in C++11, since we
23336 might want a reference constant. We'll enforce
23337 constancy later. */
23338 cp_lexer_consume_token (parser->lexer);
23339 /* Parse the initializer. */
23340 initializer = cp_parser_initializer_clause (parser,
23341 &nonconst);
23342 }
23343 else
23344 /* Parse the initializer. */
23345 initializer = cp_parser_constant_initializer (parser);
23346 }
23347 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23348 && !function_declarator_p (declarator))
23349 {
23350 bool x;
23351 if (decl_specifiers.storage_class != sc_static)
23352 initializer = cp_parser_save_nsdmi (parser);
23353 else
23354 initializer = cp_parser_initializer (parser, &x, &x);
23355 }
23356 /* Otherwise, there is no initializer. */
23357 else
23358 initializer = NULL_TREE;
23359
23360 /* See if we are probably looking at a function
23361 definition. We are certainly not looking at a
23362 member-declarator. Calling `grokfield' has
23363 side-effects, so we must not do it unless we are sure
23364 that we are looking at a member-declarator. */
23365 if (cp_parser_token_starts_function_definition_p
23366 (cp_lexer_peek_token (parser->lexer)))
23367 {
23368 /* The grammar does not allow a pure-specifier to be
23369 used when a member function is defined. (It is
23370 possible that this fact is an oversight in the
23371 standard, since a pure function may be defined
23372 outside of the class-specifier. */
23373 if (initializer && initializer_token_start)
23374 error_at (initializer_token_start->location,
23375 "pure-specifier on function-definition");
23376 decl = cp_parser_save_member_function_body (parser,
23377 &decl_specifiers,
23378 declarator,
23379 attributes);
23380 if (parser->fully_implicit_function_template_p)
23381 decl = finish_fully_implicit_template (parser, decl);
23382 /* If the member was not a friend, declare it here. */
23383 if (!friend_p)
23384 finish_member_declaration (decl);
23385 /* Peek at the next token. */
23386 token = cp_lexer_peek_token (parser->lexer);
23387 /* If the next token is a semicolon, consume it. */
23388 if (token->type == CPP_SEMICOLON)
23389 cp_lexer_consume_token (parser->lexer);
23390 goto out;
23391 }
23392 else
23393 if (declarator->kind == cdk_function)
23394 declarator->id_loc = token->location;
23395 /* Create the declaration. */
23396 decl = grokfield (declarator, &decl_specifiers,
23397 initializer, /*init_const_expr_p=*/true,
23398 asm_specification, attributes);
23399 if (parser->fully_implicit_function_template_p)
23400 {
23401 if (friend_p)
23402 finish_fully_implicit_template (parser, 0);
23403 else
23404 decl = finish_fully_implicit_template (parser, decl);
23405 }
23406 }
23407
23408 cp_finalize_omp_declare_simd (parser, decl);
23409 cp_finalize_oacc_routine (parser, decl, false);
23410
23411 /* Reset PREFIX_ATTRIBUTES. */
23412 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23413 attributes = TREE_CHAIN (attributes);
23414 if (attributes)
23415 TREE_CHAIN (attributes) = NULL_TREE;
23416
23417 /* If there is any qualification still in effect, clear it
23418 now; we will be starting fresh with the next declarator. */
23419 parser->scope = NULL_TREE;
23420 parser->qualifying_scope = NULL_TREE;
23421 parser->object_scope = NULL_TREE;
23422 /* If it's a `,', then there are more declarators. */
23423 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23424 {
23425 cp_lexer_consume_token (parser->lexer);
23426 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23427 {
23428 cp_token *token = cp_lexer_previous_token (parser->lexer);
23429 error_at (token->location,
23430 "stray %<,%> at end of member declaration");
23431 }
23432 }
23433 /* If the next token isn't a `;', then we have a parse error. */
23434 else if (cp_lexer_next_token_is_not (parser->lexer,
23435 CPP_SEMICOLON))
23436 {
23437 /* The next token might be a ways away from where the
23438 actual semicolon is missing. Find the previous token
23439 and use that for our error position. */
23440 cp_token *token = cp_lexer_previous_token (parser->lexer);
23441 error_at (token->location,
23442 "expected %<;%> at end of member declaration");
23443
23444 /* Assume that the user meant to provide a semicolon. If
23445 we were to cp_parser_skip_to_end_of_statement, we might
23446 skip to a semicolon inside a member function definition
23447 and issue nonsensical error messages. */
23448 assume_semicolon = true;
23449 }
23450
23451 if (decl)
23452 {
23453 /* Add DECL to the list of members. */
23454 if (!friend_p
23455 /* Explicitly include, eg, NSDMIs, for better error
23456 recovery (c++/58650). */
23457 || !DECL_DECLARES_FUNCTION_P (decl))
23458 finish_member_declaration (decl);
23459
23460 if (TREE_CODE (decl) == FUNCTION_DECL)
23461 cp_parser_save_default_args (parser, decl);
23462 else if (TREE_CODE (decl) == FIELD_DECL
23463 && !DECL_C_BIT_FIELD (decl)
23464 && DECL_INITIAL (decl))
23465 /* Add DECL to the queue of NSDMI to be parsed later. */
23466 vec_safe_push (unparsed_nsdmis, decl);
23467 }
23468
23469 if (assume_semicolon)
23470 goto out;
23471 }
23472 }
23473
23474 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23475 out:
23476 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23477 }
23478
23479 /* Parse a pure-specifier.
23480
23481 pure-specifier:
23482 = 0
23483
23484 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23485 Otherwise, ERROR_MARK_NODE is returned. */
23486
23487 static tree
23488 cp_parser_pure_specifier (cp_parser* parser)
23489 {
23490 cp_token *token;
23491
23492 /* Look for the `=' token. */
23493 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23494 return error_mark_node;
23495 /* Look for the `0' token. */
23496 token = cp_lexer_peek_token (parser->lexer);
23497
23498 if (token->type == CPP_EOF
23499 || token->type == CPP_PRAGMA_EOL)
23500 return error_mark_node;
23501
23502 cp_lexer_consume_token (parser->lexer);
23503
23504 /* Accept = default or = delete in c++0x mode. */
23505 if (token->keyword == RID_DEFAULT
23506 || token->keyword == RID_DELETE)
23507 {
23508 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23509 return token->u.value;
23510 }
23511
23512 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23513 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23514 {
23515 cp_parser_error (parser,
23516 "invalid pure specifier (only %<= 0%> is allowed)");
23517 cp_parser_skip_to_end_of_statement (parser);
23518 return error_mark_node;
23519 }
23520 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23521 {
23522 error_at (token->location, "templates may not be %<virtual%>");
23523 return error_mark_node;
23524 }
23525
23526 return integer_zero_node;
23527 }
23528
23529 /* Parse a constant-initializer.
23530
23531 constant-initializer:
23532 = constant-expression
23533
23534 Returns a representation of the constant-expression. */
23535
23536 static tree
23537 cp_parser_constant_initializer (cp_parser* parser)
23538 {
23539 /* Look for the `=' token. */
23540 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23541 return error_mark_node;
23542
23543 /* It is invalid to write:
23544
23545 struct S { static const int i = { 7 }; };
23546
23547 */
23548 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23549 {
23550 cp_parser_error (parser,
23551 "a brace-enclosed initializer is not allowed here");
23552 /* Consume the opening brace. */
23553 cp_lexer_consume_token (parser->lexer);
23554 /* Skip the initializer. */
23555 cp_parser_skip_to_closing_brace (parser);
23556 /* Look for the trailing `}'. */
23557 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23558
23559 return error_mark_node;
23560 }
23561
23562 return cp_parser_constant_expression (parser);
23563 }
23564
23565 /* Derived classes [gram.class.derived] */
23566
23567 /* Parse a base-clause.
23568
23569 base-clause:
23570 : base-specifier-list
23571
23572 base-specifier-list:
23573 base-specifier ... [opt]
23574 base-specifier-list , base-specifier ... [opt]
23575
23576 Returns a TREE_LIST representing the base-classes, in the order in
23577 which they were declared. The representation of each node is as
23578 described by cp_parser_base_specifier.
23579
23580 In the case that no bases are specified, this function will return
23581 NULL_TREE, not ERROR_MARK_NODE. */
23582
23583 static tree
23584 cp_parser_base_clause (cp_parser* parser)
23585 {
23586 tree bases = NULL_TREE;
23587
23588 /* Look for the `:' that begins the list. */
23589 cp_parser_require (parser, CPP_COLON, RT_COLON);
23590
23591 /* Scan the base-specifier-list. */
23592 while (true)
23593 {
23594 cp_token *token;
23595 tree base;
23596 bool pack_expansion_p = false;
23597
23598 /* Look for the base-specifier. */
23599 base = cp_parser_base_specifier (parser);
23600 /* Look for the (optional) ellipsis. */
23601 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23602 {
23603 /* Consume the `...'. */
23604 cp_lexer_consume_token (parser->lexer);
23605
23606 pack_expansion_p = true;
23607 }
23608
23609 /* Add BASE to the front of the list. */
23610 if (base && base != error_mark_node)
23611 {
23612 if (pack_expansion_p)
23613 /* Make this a pack expansion type. */
23614 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23615
23616 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23617 {
23618 TREE_CHAIN (base) = bases;
23619 bases = base;
23620 }
23621 }
23622 /* Peek at the next token. */
23623 token = cp_lexer_peek_token (parser->lexer);
23624 /* If it's not a comma, then the list is complete. */
23625 if (token->type != CPP_COMMA)
23626 break;
23627 /* Consume the `,'. */
23628 cp_lexer_consume_token (parser->lexer);
23629 }
23630
23631 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23632 base class had a qualified name. However, the next name that
23633 appears is certainly not qualified. */
23634 parser->scope = NULL_TREE;
23635 parser->qualifying_scope = NULL_TREE;
23636 parser->object_scope = NULL_TREE;
23637
23638 return nreverse (bases);
23639 }
23640
23641 /* Parse a base-specifier.
23642
23643 base-specifier:
23644 :: [opt] nested-name-specifier [opt] class-name
23645 virtual access-specifier [opt] :: [opt] nested-name-specifier
23646 [opt] class-name
23647 access-specifier virtual [opt] :: [opt] nested-name-specifier
23648 [opt] class-name
23649
23650 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23651 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23652 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23653 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23654
23655 static tree
23656 cp_parser_base_specifier (cp_parser* parser)
23657 {
23658 cp_token *token;
23659 bool done = false;
23660 bool virtual_p = false;
23661 bool duplicate_virtual_error_issued_p = false;
23662 bool duplicate_access_error_issued_p = false;
23663 bool class_scope_p, template_p;
23664 tree access = access_default_node;
23665 tree type;
23666
23667 /* Process the optional `virtual' and `access-specifier'. */
23668 while (!done)
23669 {
23670 /* Peek at the next token. */
23671 token = cp_lexer_peek_token (parser->lexer);
23672 /* Process `virtual'. */
23673 switch (token->keyword)
23674 {
23675 case RID_VIRTUAL:
23676 /* If `virtual' appears more than once, issue an error. */
23677 if (virtual_p && !duplicate_virtual_error_issued_p)
23678 {
23679 cp_parser_error (parser,
23680 "%<virtual%> specified more than once in base-specified");
23681 duplicate_virtual_error_issued_p = true;
23682 }
23683
23684 virtual_p = true;
23685
23686 /* Consume the `virtual' token. */
23687 cp_lexer_consume_token (parser->lexer);
23688
23689 break;
23690
23691 case RID_PUBLIC:
23692 case RID_PROTECTED:
23693 case RID_PRIVATE:
23694 /* If more than one access specifier appears, issue an
23695 error. */
23696 if (access != access_default_node
23697 && !duplicate_access_error_issued_p)
23698 {
23699 cp_parser_error (parser,
23700 "more than one access specifier in base-specified");
23701 duplicate_access_error_issued_p = true;
23702 }
23703
23704 access = ridpointers[(int) token->keyword];
23705
23706 /* Consume the access-specifier. */
23707 cp_lexer_consume_token (parser->lexer);
23708
23709 break;
23710
23711 default:
23712 done = true;
23713 break;
23714 }
23715 }
23716 /* It is not uncommon to see programs mechanically, erroneously, use
23717 the 'typename' keyword to denote (dependent) qualified types
23718 as base classes. */
23719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23720 {
23721 token = cp_lexer_peek_token (parser->lexer);
23722 if (!processing_template_decl)
23723 error_at (token->location,
23724 "keyword %<typename%> not allowed outside of templates");
23725 else
23726 error_at (token->location,
23727 "keyword %<typename%> not allowed in this context "
23728 "(the base class is implicitly a type)");
23729 cp_lexer_consume_token (parser->lexer);
23730 }
23731
23732 /* Look for the optional `::' operator. */
23733 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23734 /* Look for the nested-name-specifier. The simplest way to
23735 implement:
23736
23737 [temp.res]
23738
23739 The keyword `typename' is not permitted in a base-specifier or
23740 mem-initializer; in these contexts a qualified name that
23741 depends on a template-parameter is implicitly assumed to be a
23742 type name.
23743
23744 is to pretend that we have seen the `typename' keyword at this
23745 point. */
23746 cp_parser_nested_name_specifier_opt (parser,
23747 /*typename_keyword_p=*/true,
23748 /*check_dependency_p=*/true,
23749 /*type_p=*/true,
23750 /*is_declaration=*/true);
23751 /* If the base class is given by a qualified name, assume that names
23752 we see are type names or templates, as appropriate. */
23753 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23754 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23755
23756 if (!parser->scope
23757 && cp_lexer_next_token_is_decltype (parser->lexer))
23758 /* DR 950 allows decltype as a base-specifier. */
23759 type = cp_parser_decltype (parser);
23760 else
23761 {
23762 /* Otherwise, look for the class-name. */
23763 type = cp_parser_class_name (parser,
23764 class_scope_p,
23765 template_p,
23766 typename_type,
23767 /*check_dependency_p=*/true,
23768 /*class_head_p=*/false,
23769 /*is_declaration=*/true);
23770 type = TREE_TYPE (type);
23771 }
23772
23773 if (type == error_mark_node)
23774 return error_mark_node;
23775
23776 return finish_base_specifier (type, access, virtual_p);
23777 }
23778
23779 /* Exception handling [gram.exception] */
23780
23781 /* Parse an (optional) noexcept-specification.
23782
23783 noexcept-specification:
23784 noexcept ( constant-expression ) [opt]
23785
23786 If no noexcept-specification is present, returns NULL_TREE.
23787 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23788 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23789 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23790 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23791 in which case a boolean condition is returned instead. */
23792
23793 static tree
23794 cp_parser_noexcept_specification_opt (cp_parser* parser,
23795 bool require_constexpr,
23796 bool* consumed_expr,
23797 bool return_cond)
23798 {
23799 cp_token *token;
23800 const char *saved_message;
23801
23802 /* Peek at the next token. */
23803 token = cp_lexer_peek_token (parser->lexer);
23804
23805 /* Is it a noexcept-specification? */
23806 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23807 {
23808 tree expr;
23809 cp_lexer_consume_token (parser->lexer);
23810
23811 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23812 {
23813 cp_lexer_consume_token (parser->lexer);
23814
23815 if (require_constexpr)
23816 {
23817 /* Types may not be defined in an exception-specification. */
23818 saved_message = parser->type_definition_forbidden_message;
23819 parser->type_definition_forbidden_message
23820 = G_("types may not be defined in an exception-specification");
23821
23822 expr = cp_parser_constant_expression (parser);
23823
23824 /* Restore the saved message. */
23825 parser->type_definition_forbidden_message = saved_message;
23826 }
23827 else
23828 {
23829 expr = cp_parser_expression (parser);
23830 *consumed_expr = true;
23831 }
23832
23833 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23834 }
23835 else
23836 {
23837 expr = boolean_true_node;
23838 if (!require_constexpr)
23839 *consumed_expr = false;
23840 }
23841
23842 /* We cannot build a noexcept-spec right away because this will check
23843 that expr is a constexpr. */
23844 if (!return_cond)
23845 return build_noexcept_spec (expr, tf_warning_or_error);
23846 else
23847 return expr;
23848 }
23849 else
23850 return NULL_TREE;
23851 }
23852
23853 /* Parse an (optional) exception-specification.
23854
23855 exception-specification:
23856 throw ( type-id-list [opt] )
23857
23858 Returns a TREE_LIST representing the exception-specification. The
23859 TREE_VALUE of each node is a type. */
23860
23861 static tree
23862 cp_parser_exception_specification_opt (cp_parser* parser)
23863 {
23864 cp_token *token;
23865 tree type_id_list;
23866 const char *saved_message;
23867
23868 /* Peek at the next token. */
23869 token = cp_lexer_peek_token (parser->lexer);
23870
23871 /* Is it a noexcept-specification? */
23872 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23873 false);
23874 if (type_id_list != NULL_TREE)
23875 return type_id_list;
23876
23877 /* If it's not `throw', then there's no exception-specification. */
23878 if (!cp_parser_is_keyword (token, RID_THROW))
23879 return NULL_TREE;
23880
23881 location_t loc = token->location;
23882
23883 /* Consume the `throw'. */
23884 cp_lexer_consume_token (parser->lexer);
23885
23886 /* Look for the `('. */
23887 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23888
23889 /* Peek at the next token. */
23890 token = cp_lexer_peek_token (parser->lexer);
23891 /* If it's not a `)', then there is a type-id-list. */
23892 if (token->type != CPP_CLOSE_PAREN)
23893 {
23894 /* Types may not be defined in an exception-specification. */
23895 saved_message = parser->type_definition_forbidden_message;
23896 parser->type_definition_forbidden_message
23897 = G_("types may not be defined in an exception-specification");
23898 /* Parse the type-id-list. */
23899 type_id_list = cp_parser_type_id_list (parser);
23900 /* Restore the saved message. */
23901 parser->type_definition_forbidden_message = saved_message;
23902
23903 if (cxx_dialect >= cxx1z)
23904 {
23905 error_at (loc, "ISO C++1z does not allow dynamic exception "
23906 "specifications");
23907 type_id_list = NULL_TREE;
23908 }
23909 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23910 warning_at (loc, OPT_Wdeprecated,
23911 "dynamic exception specifications are deprecated in "
23912 "C++11");
23913 }
23914 /* In C++17, throw() is equivalent to noexcept (true). throw()
23915 is deprecated in C++11 and above as well, but is still widely used,
23916 so don't warn about it yet. */
23917 else if (cxx_dialect >= cxx1z)
23918 type_id_list = noexcept_true_spec;
23919 else
23920 type_id_list = empty_except_spec;
23921
23922 /* Look for the `)'. */
23923 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23924
23925 return type_id_list;
23926 }
23927
23928 /* Parse an (optional) type-id-list.
23929
23930 type-id-list:
23931 type-id ... [opt]
23932 type-id-list , type-id ... [opt]
23933
23934 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23935 in the order that the types were presented. */
23936
23937 static tree
23938 cp_parser_type_id_list (cp_parser* parser)
23939 {
23940 tree types = NULL_TREE;
23941
23942 while (true)
23943 {
23944 cp_token *token;
23945 tree type;
23946
23947 token = cp_lexer_peek_token (parser->lexer);
23948
23949 /* Get the next type-id. */
23950 type = cp_parser_type_id (parser);
23951 /* Check for invalid 'auto'. */
23952 if (flag_concepts && type_uses_auto (type))
23953 {
23954 error_at (token->location,
23955 "invalid use of %<auto%> in exception-specification");
23956 type = error_mark_node;
23957 }
23958 /* Parse the optional ellipsis. */
23959 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23960 {
23961 /* Consume the `...'. */
23962 cp_lexer_consume_token (parser->lexer);
23963
23964 /* Turn the type into a pack expansion expression. */
23965 type = make_pack_expansion (type);
23966 }
23967 /* Add it to the list. */
23968 types = add_exception_specifier (types, type, /*complain=*/1);
23969 /* Peek at the next token. */
23970 token = cp_lexer_peek_token (parser->lexer);
23971 /* If it is not a `,', we are done. */
23972 if (token->type != CPP_COMMA)
23973 break;
23974 /* Consume the `,'. */
23975 cp_lexer_consume_token (parser->lexer);
23976 }
23977
23978 return nreverse (types);
23979 }
23980
23981 /* Parse a try-block.
23982
23983 try-block:
23984 try compound-statement handler-seq */
23985
23986 static tree
23987 cp_parser_try_block (cp_parser* parser)
23988 {
23989 tree try_block;
23990
23991 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23992 if (parser->in_function_body
23993 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23994 error ("%<try%> in %<constexpr%> function");
23995
23996 try_block = begin_try_block ();
23997 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23998 finish_try_block (try_block);
23999 cp_parser_handler_seq (parser);
24000 finish_handler_sequence (try_block);
24001
24002 return try_block;
24003 }
24004
24005 /* Parse a function-try-block.
24006
24007 function-try-block:
24008 try ctor-initializer [opt] function-body handler-seq */
24009
24010 static bool
24011 cp_parser_function_try_block (cp_parser* parser)
24012 {
24013 tree compound_stmt;
24014 tree try_block;
24015 bool ctor_initializer_p;
24016
24017 /* Look for the `try' keyword. */
24018 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24019 return false;
24020 /* Let the rest of the front end know where we are. */
24021 try_block = begin_function_try_block (&compound_stmt);
24022 /* Parse the function-body. */
24023 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24024 (parser, /*in_function_try_block=*/true);
24025 /* We're done with the `try' part. */
24026 finish_function_try_block (try_block);
24027 /* Parse the handlers. */
24028 cp_parser_handler_seq (parser);
24029 /* We're done with the handlers. */
24030 finish_function_handler_sequence (try_block, compound_stmt);
24031
24032 return ctor_initializer_p;
24033 }
24034
24035 /* Parse a handler-seq.
24036
24037 handler-seq:
24038 handler handler-seq [opt] */
24039
24040 static void
24041 cp_parser_handler_seq (cp_parser* parser)
24042 {
24043 while (true)
24044 {
24045 cp_token *token;
24046
24047 /* Parse the handler. */
24048 cp_parser_handler (parser);
24049 /* Peek at the next token. */
24050 token = cp_lexer_peek_token (parser->lexer);
24051 /* If it's not `catch' then there are no more handlers. */
24052 if (!cp_parser_is_keyword (token, RID_CATCH))
24053 break;
24054 }
24055 }
24056
24057 /* Parse a handler.
24058
24059 handler:
24060 catch ( exception-declaration ) compound-statement */
24061
24062 static void
24063 cp_parser_handler (cp_parser* parser)
24064 {
24065 tree handler;
24066 tree declaration;
24067
24068 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24069 handler = begin_handler ();
24070 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24071 declaration = cp_parser_exception_declaration (parser);
24072 finish_handler_parms (declaration, handler);
24073 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24074 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24075 finish_handler (handler);
24076 }
24077
24078 /* Parse an exception-declaration.
24079
24080 exception-declaration:
24081 type-specifier-seq declarator
24082 type-specifier-seq abstract-declarator
24083 type-specifier-seq
24084 ...
24085
24086 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24087 ellipsis variant is used. */
24088
24089 static tree
24090 cp_parser_exception_declaration (cp_parser* parser)
24091 {
24092 cp_decl_specifier_seq type_specifiers;
24093 cp_declarator *declarator;
24094 const char *saved_message;
24095
24096 /* If it's an ellipsis, it's easy to handle. */
24097 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24098 {
24099 /* Consume the `...' token. */
24100 cp_lexer_consume_token (parser->lexer);
24101 return NULL_TREE;
24102 }
24103
24104 /* Types may not be defined in exception-declarations. */
24105 saved_message = parser->type_definition_forbidden_message;
24106 parser->type_definition_forbidden_message
24107 = G_("types may not be defined in exception-declarations");
24108
24109 /* Parse the type-specifier-seq. */
24110 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24111 /*is_trailing_return=*/false,
24112 &type_specifiers);
24113 /* If it's a `)', then there is no declarator. */
24114 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24115 declarator = NULL;
24116 else
24117 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24118 /*ctor_dtor_or_conv_p=*/NULL,
24119 /*parenthesized_p=*/NULL,
24120 /*member_p=*/false,
24121 /*friend_p=*/false);
24122
24123 /* Restore the saved message. */
24124 parser->type_definition_forbidden_message = saved_message;
24125
24126 if (!type_specifiers.any_specifiers_p)
24127 return error_mark_node;
24128
24129 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24130 }
24131
24132 /* Parse a throw-expression.
24133
24134 throw-expression:
24135 throw assignment-expression [opt]
24136
24137 Returns a THROW_EXPR representing the throw-expression. */
24138
24139 static tree
24140 cp_parser_throw_expression (cp_parser* parser)
24141 {
24142 tree expression;
24143 cp_token* token;
24144
24145 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24146 token = cp_lexer_peek_token (parser->lexer);
24147 /* Figure out whether or not there is an assignment-expression
24148 following the "throw" keyword. */
24149 if (token->type == CPP_COMMA
24150 || token->type == CPP_SEMICOLON
24151 || token->type == CPP_CLOSE_PAREN
24152 || token->type == CPP_CLOSE_SQUARE
24153 || token->type == CPP_CLOSE_BRACE
24154 || token->type == CPP_COLON)
24155 expression = NULL_TREE;
24156 else
24157 expression = cp_parser_assignment_expression (parser);
24158
24159 return build_throw (expression);
24160 }
24161
24162 /* GNU Extensions */
24163
24164 /* Parse an (optional) asm-specification.
24165
24166 asm-specification:
24167 asm ( string-literal )
24168
24169 If the asm-specification is present, returns a STRING_CST
24170 corresponding to the string-literal. Otherwise, returns
24171 NULL_TREE. */
24172
24173 static tree
24174 cp_parser_asm_specification_opt (cp_parser* parser)
24175 {
24176 cp_token *token;
24177 tree asm_specification;
24178
24179 /* Peek at the next token. */
24180 token = cp_lexer_peek_token (parser->lexer);
24181 /* If the next token isn't the `asm' keyword, then there's no
24182 asm-specification. */
24183 if (!cp_parser_is_keyword (token, RID_ASM))
24184 return NULL_TREE;
24185
24186 /* Consume the `asm' token. */
24187 cp_lexer_consume_token (parser->lexer);
24188 /* Look for the `('. */
24189 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24190
24191 /* Look for the string-literal. */
24192 asm_specification = cp_parser_string_literal (parser, false, false);
24193
24194 /* Look for the `)'. */
24195 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24196
24197 return asm_specification;
24198 }
24199
24200 /* Parse an asm-operand-list.
24201
24202 asm-operand-list:
24203 asm-operand
24204 asm-operand-list , asm-operand
24205
24206 asm-operand:
24207 string-literal ( expression )
24208 [ string-literal ] string-literal ( expression )
24209
24210 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24211 each node is the expression. The TREE_PURPOSE is itself a
24212 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24213 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24214 is a STRING_CST for the string literal before the parenthesis. Returns
24215 ERROR_MARK_NODE if any of the operands are invalid. */
24216
24217 static tree
24218 cp_parser_asm_operand_list (cp_parser* parser)
24219 {
24220 tree asm_operands = NULL_TREE;
24221 bool invalid_operands = false;
24222
24223 while (true)
24224 {
24225 tree string_literal;
24226 tree expression;
24227 tree name;
24228
24229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24230 {
24231 /* Consume the `[' token. */
24232 cp_lexer_consume_token (parser->lexer);
24233 /* Read the operand name. */
24234 name = cp_parser_identifier (parser);
24235 if (name != error_mark_node)
24236 name = build_string (IDENTIFIER_LENGTH (name),
24237 IDENTIFIER_POINTER (name));
24238 /* Look for the closing `]'. */
24239 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24240 }
24241 else
24242 name = NULL_TREE;
24243 /* Look for the string-literal. */
24244 string_literal = cp_parser_string_literal (parser, false, false);
24245
24246 /* Look for the `('. */
24247 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24248 /* Parse the expression. */
24249 expression = cp_parser_expression (parser);
24250 /* Look for the `)'. */
24251 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24252
24253 if (name == error_mark_node
24254 || string_literal == error_mark_node
24255 || expression == error_mark_node)
24256 invalid_operands = true;
24257
24258 /* Add this operand to the list. */
24259 asm_operands = tree_cons (build_tree_list (name, string_literal),
24260 expression,
24261 asm_operands);
24262 /* If the next token is not a `,', there are no more
24263 operands. */
24264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24265 break;
24266 /* Consume the `,'. */
24267 cp_lexer_consume_token (parser->lexer);
24268 }
24269
24270 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24271 }
24272
24273 /* Parse an asm-clobber-list.
24274
24275 asm-clobber-list:
24276 string-literal
24277 asm-clobber-list , string-literal
24278
24279 Returns a TREE_LIST, indicating the clobbers in the order that they
24280 appeared. The TREE_VALUE of each node is a STRING_CST. */
24281
24282 static tree
24283 cp_parser_asm_clobber_list (cp_parser* parser)
24284 {
24285 tree clobbers = NULL_TREE;
24286
24287 while (true)
24288 {
24289 tree string_literal;
24290
24291 /* Look for the string literal. */
24292 string_literal = cp_parser_string_literal (parser, false, false);
24293 /* Add it to the list. */
24294 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24295 /* If the next token is not a `,', then the list is
24296 complete. */
24297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24298 break;
24299 /* Consume the `,' token. */
24300 cp_lexer_consume_token (parser->lexer);
24301 }
24302
24303 return clobbers;
24304 }
24305
24306 /* Parse an asm-label-list.
24307
24308 asm-label-list:
24309 identifier
24310 asm-label-list , identifier
24311
24312 Returns a TREE_LIST, indicating the labels in the order that they
24313 appeared. The TREE_VALUE of each node is a label. */
24314
24315 static tree
24316 cp_parser_asm_label_list (cp_parser* parser)
24317 {
24318 tree labels = NULL_TREE;
24319
24320 while (true)
24321 {
24322 tree identifier, label, name;
24323
24324 /* Look for the identifier. */
24325 identifier = cp_parser_identifier (parser);
24326 if (!error_operand_p (identifier))
24327 {
24328 label = lookup_label (identifier);
24329 if (TREE_CODE (label) == LABEL_DECL)
24330 {
24331 TREE_USED (label) = 1;
24332 check_goto (label);
24333 name = build_string (IDENTIFIER_LENGTH (identifier),
24334 IDENTIFIER_POINTER (identifier));
24335 labels = tree_cons (name, label, labels);
24336 }
24337 }
24338 /* If the next token is not a `,', then the list is
24339 complete. */
24340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24341 break;
24342 /* Consume the `,' token. */
24343 cp_lexer_consume_token (parser->lexer);
24344 }
24345
24346 return nreverse (labels);
24347 }
24348
24349 /* Return TRUE iff the next tokens in the stream are possibly the
24350 beginning of a GNU extension attribute. */
24351
24352 static bool
24353 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24354 {
24355 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24356 }
24357
24358 /* Return TRUE iff the next tokens in the stream are possibly the
24359 beginning of a standard C++-11 attribute specifier. */
24360
24361 static bool
24362 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24363 {
24364 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24365 }
24366
24367 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24368 beginning of a standard C++-11 attribute specifier. */
24369
24370 static bool
24371 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24372 {
24373 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24374
24375 return (cxx_dialect >= cxx11
24376 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24377 || (token->type == CPP_OPEN_SQUARE
24378 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24379 && token->type == CPP_OPEN_SQUARE)));
24380 }
24381
24382 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24383 beginning of a GNU extension attribute. */
24384
24385 static bool
24386 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24387 {
24388 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24389
24390 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24391 }
24392
24393 /* Return true iff the next tokens can be the beginning of either a
24394 GNU attribute list, or a standard C++11 attribute sequence. */
24395
24396 static bool
24397 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24398 {
24399 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24400 || cp_next_tokens_can_be_std_attribute_p (parser));
24401 }
24402
24403 /* Return true iff the next Nth tokens can be the beginning of either
24404 a GNU attribute list, or a standard C++11 attribute sequence. */
24405
24406 static bool
24407 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24408 {
24409 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24410 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24411 }
24412
24413 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24414 of GNU attributes, or return NULL. */
24415
24416 static tree
24417 cp_parser_attributes_opt (cp_parser *parser)
24418 {
24419 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24420 return cp_parser_gnu_attributes_opt (parser);
24421 return cp_parser_std_attribute_spec_seq (parser);
24422 }
24423
24424 #define CILK_SIMD_FN_CLAUSE_MASK \
24425 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24426 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24427 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24428 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24429 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24430
24431 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24432 vector [(<clauses>)] */
24433
24434 static void
24435 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24436 {
24437 bool first_p = parser->cilk_simd_fn_info == NULL;
24438 cp_token *token = v_token;
24439 if (first_p)
24440 {
24441 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24442 parser->cilk_simd_fn_info->error_seen = false;
24443 parser->cilk_simd_fn_info->fndecl_seen = false;
24444 parser->cilk_simd_fn_info->tokens = vNULL;
24445 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24446 }
24447 int paren_scope = 0;
24448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24449 {
24450 cp_lexer_consume_token (parser->lexer);
24451 v_token = cp_lexer_peek_token (parser->lexer);
24452 paren_scope++;
24453 }
24454 while (paren_scope > 0)
24455 {
24456 token = cp_lexer_peek_token (parser->lexer);
24457 if (token->type == CPP_OPEN_PAREN)
24458 paren_scope++;
24459 else if (token->type == CPP_CLOSE_PAREN)
24460 paren_scope--;
24461 /* Do not push the last ')' */
24462 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24463 cp_lexer_consume_token (parser->lexer);
24464 }
24465
24466 token->type = CPP_PRAGMA_EOL;
24467 parser->lexer->next_token = token;
24468 cp_lexer_consume_token (parser->lexer);
24469
24470 struct cp_token_cache *cp
24471 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24472 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24473 }
24474
24475 /* Parse an (optional) series of attributes.
24476
24477 attributes:
24478 attributes attribute
24479
24480 attribute:
24481 __attribute__ (( attribute-list [opt] ))
24482
24483 The return value is as for cp_parser_gnu_attribute_list. */
24484
24485 static tree
24486 cp_parser_gnu_attributes_opt (cp_parser* parser)
24487 {
24488 tree attributes = NULL_TREE;
24489
24490 while (true)
24491 {
24492 cp_token *token;
24493 tree attribute_list;
24494 bool ok = true;
24495
24496 /* Peek at the next token. */
24497 token = cp_lexer_peek_token (parser->lexer);
24498 /* If it's not `__attribute__', then we're done. */
24499 if (token->keyword != RID_ATTRIBUTE)
24500 break;
24501
24502 /* Consume the `__attribute__' keyword. */
24503 cp_lexer_consume_token (parser->lexer);
24504 /* Look for the two `(' tokens. */
24505 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24506 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24507
24508 /* Peek at the next token. */
24509 token = cp_lexer_peek_token (parser->lexer);
24510 if (token->type != CPP_CLOSE_PAREN)
24511 /* Parse the attribute-list. */
24512 attribute_list = cp_parser_gnu_attribute_list (parser);
24513 else
24514 /* If the next token is a `)', then there is no attribute
24515 list. */
24516 attribute_list = NULL;
24517
24518 /* Look for the two `)' tokens. */
24519 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24520 ok = false;
24521 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24522 ok = false;
24523 if (!ok)
24524 cp_parser_skip_to_end_of_statement (parser);
24525
24526 /* Add these new attributes to the list. */
24527 attributes = chainon (attributes, attribute_list);
24528 }
24529
24530 return attributes;
24531 }
24532
24533 /* Parse a GNU attribute-list.
24534
24535 attribute-list:
24536 attribute
24537 attribute-list , attribute
24538
24539 attribute:
24540 identifier
24541 identifier ( identifier )
24542 identifier ( identifier , expression-list )
24543 identifier ( expression-list )
24544
24545 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24546 to an attribute. The TREE_PURPOSE of each node is the identifier
24547 indicating which attribute is in use. The TREE_VALUE represents
24548 the arguments, if any. */
24549
24550 static tree
24551 cp_parser_gnu_attribute_list (cp_parser* parser)
24552 {
24553 tree attribute_list = NULL_TREE;
24554 bool save_translate_strings_p = parser->translate_strings_p;
24555
24556 parser->translate_strings_p = false;
24557 while (true)
24558 {
24559 cp_token *token;
24560 tree identifier;
24561 tree attribute;
24562
24563 /* Look for the identifier. We also allow keywords here; for
24564 example `__attribute__ ((const))' is legal. */
24565 token = cp_lexer_peek_token (parser->lexer);
24566 if (token->type == CPP_NAME
24567 || token->type == CPP_KEYWORD)
24568 {
24569 tree arguments = NULL_TREE;
24570
24571 /* Consume the token, but save it since we need it for the
24572 SIMD enabled function parsing. */
24573 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24574
24575 /* Save away the identifier that indicates which attribute
24576 this is. */
24577 identifier = (token->type == CPP_KEYWORD)
24578 /* For keywords, use the canonical spelling, not the
24579 parsed identifier. */
24580 ? ridpointers[(int) token->keyword]
24581 : id_token->u.value;
24582
24583 attribute = build_tree_list (identifier, NULL_TREE);
24584
24585 /* Peek at the next token. */
24586 token = cp_lexer_peek_token (parser->lexer);
24587 /* If it's an `(', then parse the attribute arguments. */
24588 if (token->type == CPP_OPEN_PAREN)
24589 {
24590 vec<tree, va_gc> *vec;
24591 int attr_flag = (attribute_takes_identifier_p (identifier)
24592 ? id_attr : normal_attr);
24593 if (is_cilkplus_vector_p (identifier))
24594 {
24595 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24596 continue;
24597 }
24598 else
24599 vec = cp_parser_parenthesized_expression_list
24600 (parser, attr_flag, /*cast_p=*/false,
24601 /*allow_expansion_p=*/false,
24602 /*non_constant_p=*/NULL);
24603 if (vec == NULL)
24604 arguments = error_mark_node;
24605 else
24606 {
24607 arguments = build_tree_list_vec (vec);
24608 release_tree_vector (vec);
24609 }
24610 /* Save the arguments away. */
24611 TREE_VALUE (attribute) = arguments;
24612 }
24613 else if (is_cilkplus_vector_p (identifier))
24614 {
24615 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24616 continue;
24617 }
24618
24619 if (arguments != error_mark_node)
24620 {
24621 /* Add this attribute to the list. */
24622 TREE_CHAIN (attribute) = attribute_list;
24623 attribute_list = attribute;
24624 }
24625
24626 token = cp_lexer_peek_token (parser->lexer);
24627 }
24628 /* Now, look for more attributes. If the next token isn't a
24629 `,', we're done. */
24630 if (token->type != CPP_COMMA)
24631 break;
24632
24633 /* Consume the comma and keep going. */
24634 cp_lexer_consume_token (parser->lexer);
24635 }
24636 parser->translate_strings_p = save_translate_strings_p;
24637
24638 /* We built up the list in reverse order. */
24639 return nreverse (attribute_list);
24640 }
24641
24642 /* Parse a standard C++11 attribute.
24643
24644 The returned representation is a TREE_LIST which TREE_PURPOSE is
24645 the scoped name of the attribute, and the TREE_VALUE is its
24646 arguments list.
24647
24648 Note that the scoped name of the attribute is itself a TREE_LIST
24649 which TREE_PURPOSE is the namespace of the attribute, and
24650 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24651 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24652 and which TREE_PURPOSE is directly the attribute name.
24653
24654 Clients of the attribute code should use get_attribute_namespace
24655 and get_attribute_name to get the actual namespace and name of
24656 attributes, regardless of their being GNU or C++11 attributes.
24657
24658 attribute:
24659 attribute-token attribute-argument-clause [opt]
24660
24661 attribute-token:
24662 identifier
24663 attribute-scoped-token
24664
24665 attribute-scoped-token:
24666 attribute-namespace :: identifier
24667
24668 attribute-namespace:
24669 identifier
24670
24671 attribute-argument-clause:
24672 ( balanced-token-seq )
24673
24674 balanced-token-seq:
24675 balanced-token [opt]
24676 balanced-token-seq balanced-token
24677
24678 balanced-token:
24679 ( balanced-token-seq )
24680 [ balanced-token-seq ]
24681 { balanced-token-seq }. */
24682
24683 static tree
24684 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24685 {
24686 tree attribute, attr_id = NULL_TREE, arguments;
24687 cp_token *token;
24688
24689 /* First, parse name of the attribute, a.k.a attribute-token. */
24690
24691 token = cp_lexer_peek_token (parser->lexer);
24692 if (token->type == CPP_NAME)
24693 attr_id = token->u.value;
24694 else if (token->type == CPP_KEYWORD)
24695 attr_id = ridpointers[(int) token->keyword];
24696 else if (token->flags & NAMED_OP)
24697 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24698
24699 if (attr_id == NULL_TREE)
24700 return NULL_TREE;
24701
24702 cp_lexer_consume_token (parser->lexer);
24703
24704 token = cp_lexer_peek_token (parser->lexer);
24705 if (token->type == CPP_SCOPE)
24706 {
24707 /* We are seeing a scoped attribute token. */
24708
24709 cp_lexer_consume_token (parser->lexer);
24710 if (attr_ns)
24711 error_at (token->location, "attribute using prefix used together "
24712 "with scoped attribute token");
24713 attr_ns = attr_id;
24714
24715 token = cp_lexer_consume_token (parser->lexer);
24716 if (token->type == CPP_NAME)
24717 attr_id = token->u.value;
24718 else if (token->type == CPP_KEYWORD)
24719 attr_id = ridpointers[(int) token->keyword];
24720 else if (token->flags & NAMED_OP)
24721 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24722 else
24723 {
24724 error_at (token->location,
24725 "expected an identifier for the attribute name");
24726 return error_mark_node;
24727 }
24728 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24729 NULL_TREE);
24730 token = cp_lexer_peek_token (parser->lexer);
24731 }
24732 else if (attr_ns)
24733 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24734 NULL_TREE);
24735 else
24736 {
24737 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24738 NULL_TREE);
24739 /* C++11 noreturn attribute is equivalent to GNU's. */
24740 if (is_attribute_p ("noreturn", attr_id))
24741 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24742 /* C++14 deprecated attribute is equivalent to GNU's. */
24743 else if (is_attribute_p ("deprecated", attr_id))
24744 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24745 /* C++17 fallthrough attribute is equivalent to GNU's. */
24746 else if (is_attribute_p ("fallthrough", attr_id))
24747 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24748 /* Transactional Memory TS optimize_for_synchronized attribute is
24749 equivalent to GNU transaction_callable. */
24750 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24751 TREE_PURPOSE (attribute)
24752 = get_identifier ("transaction_callable");
24753 /* Transactional Memory attributes are GNU attributes. */
24754 else if (tm_attr_to_mask (attr_id))
24755 TREE_PURPOSE (attribute) = attr_id;
24756 }
24757
24758 /* Now parse the optional argument clause of the attribute. */
24759
24760 if (token->type != CPP_OPEN_PAREN)
24761 return attribute;
24762
24763 {
24764 vec<tree, va_gc> *vec;
24765 int attr_flag = normal_attr;
24766
24767 if (attr_ns == get_identifier ("gnu")
24768 && attribute_takes_identifier_p (attr_id))
24769 /* A GNU attribute that takes an identifier in parameter. */
24770 attr_flag = id_attr;
24771
24772 vec = cp_parser_parenthesized_expression_list
24773 (parser, attr_flag, /*cast_p=*/false,
24774 /*allow_expansion_p=*/true,
24775 /*non_constant_p=*/NULL);
24776 if (vec == NULL)
24777 arguments = error_mark_node;
24778 else
24779 {
24780 arguments = build_tree_list_vec (vec);
24781 release_tree_vector (vec);
24782 }
24783
24784 if (arguments == error_mark_node)
24785 attribute = error_mark_node;
24786 else
24787 TREE_VALUE (attribute) = arguments;
24788 }
24789
24790 return attribute;
24791 }
24792
24793 /* Check that the attribute ATTRIBUTE appears at most once in the
24794 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24795 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24796 isn't implemented yet in GCC. */
24797
24798 static void
24799 cp_parser_check_std_attribute (tree attributes, tree attribute)
24800 {
24801 if (attributes)
24802 {
24803 tree name = get_attribute_name (attribute);
24804 if (is_attribute_p ("noreturn", name)
24805 && lookup_attribute ("noreturn", attributes))
24806 error ("attribute %<noreturn%> can appear at most once "
24807 "in an attribute-list");
24808 else if (is_attribute_p ("deprecated", name)
24809 && lookup_attribute ("deprecated", attributes))
24810 error ("attribute %<deprecated%> can appear at most once "
24811 "in an attribute-list");
24812 }
24813 }
24814
24815 /* Parse a list of standard C++-11 attributes.
24816
24817 attribute-list:
24818 attribute [opt]
24819 attribute-list , attribute[opt]
24820 attribute ...
24821 attribute-list , attribute ...
24822 */
24823
24824 static tree
24825 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24826 {
24827 tree attributes = NULL_TREE, attribute = NULL_TREE;
24828 cp_token *token = NULL;
24829
24830 while (true)
24831 {
24832 attribute = cp_parser_std_attribute (parser, attr_ns);
24833 if (attribute == error_mark_node)
24834 break;
24835 if (attribute != NULL_TREE)
24836 {
24837 cp_parser_check_std_attribute (attributes, attribute);
24838 TREE_CHAIN (attribute) = attributes;
24839 attributes = attribute;
24840 }
24841 token = cp_lexer_peek_token (parser->lexer);
24842 if (token->type == CPP_ELLIPSIS)
24843 {
24844 cp_lexer_consume_token (parser->lexer);
24845 if (attribute == NULL_TREE)
24846 error_at (token->location,
24847 "expected attribute before %<...%>");
24848 else
24849 TREE_VALUE (attribute)
24850 = make_pack_expansion (TREE_VALUE (attribute));
24851 token = cp_lexer_peek_token (parser->lexer);
24852 }
24853 if (token->type != CPP_COMMA)
24854 break;
24855 cp_lexer_consume_token (parser->lexer);
24856 }
24857 attributes = nreverse (attributes);
24858 return attributes;
24859 }
24860
24861 /* Parse a standard C++-11 attribute specifier.
24862
24863 attribute-specifier:
24864 [ [ attribute-using-prefix [opt] attribute-list ] ]
24865 alignment-specifier
24866
24867 attribute-using-prefix:
24868 using attribute-namespace :
24869
24870 alignment-specifier:
24871 alignas ( type-id ... [opt] )
24872 alignas ( alignment-expression ... [opt] ). */
24873
24874 static tree
24875 cp_parser_std_attribute_spec (cp_parser *parser)
24876 {
24877 tree attributes = NULL_TREE;
24878 cp_token *token = cp_lexer_peek_token (parser->lexer);
24879
24880 if (token->type == CPP_OPEN_SQUARE
24881 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24882 {
24883 tree attr_ns = NULL_TREE;
24884
24885 cp_lexer_consume_token (parser->lexer);
24886 cp_lexer_consume_token (parser->lexer);
24887
24888 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24889 {
24890 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24891 if (token->type == CPP_NAME)
24892 attr_ns = token->u.value;
24893 else if (token->type == CPP_KEYWORD)
24894 attr_ns = ridpointers[(int) token->keyword];
24895 else if (token->flags & NAMED_OP)
24896 attr_ns = get_identifier (cpp_type2name (token->type,
24897 token->flags));
24898 if (attr_ns
24899 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24900 {
24901 if (cxx_dialect < cxx1z
24902 && !in_system_header_at (input_location))
24903 pedwarn (input_location, 0,
24904 "attribute using prefix only available "
24905 "with -std=c++1z or -std=gnu++1z");
24906
24907 cp_lexer_consume_token (parser->lexer);
24908 cp_lexer_consume_token (parser->lexer);
24909 cp_lexer_consume_token (parser->lexer);
24910 }
24911 else
24912 attr_ns = NULL_TREE;
24913 }
24914
24915 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24916
24917 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24918 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24919 cp_parser_skip_to_end_of_statement (parser);
24920 else
24921 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24922 when we are sure that we have actually parsed them. */
24923 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24924 }
24925 else
24926 {
24927 tree alignas_expr;
24928
24929 /* Look for an alignment-specifier. */
24930
24931 token = cp_lexer_peek_token (parser->lexer);
24932
24933 if (token->type != CPP_KEYWORD
24934 || token->keyword != RID_ALIGNAS)
24935 return NULL_TREE;
24936
24937 cp_lexer_consume_token (parser->lexer);
24938 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24939
24940 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24941 {
24942 cp_parser_error (parser, "expected %<(%>");
24943 return error_mark_node;
24944 }
24945
24946 cp_parser_parse_tentatively (parser);
24947 alignas_expr = cp_parser_type_id (parser);
24948
24949 if (!cp_parser_parse_definitely (parser))
24950 {
24951 alignas_expr = cp_parser_assignment_expression (parser);
24952 if (alignas_expr == error_mark_node)
24953 cp_parser_skip_to_end_of_statement (parser);
24954 if (alignas_expr == NULL_TREE
24955 || alignas_expr == error_mark_node)
24956 return alignas_expr;
24957 }
24958
24959 alignas_expr = cxx_alignas_expr (alignas_expr);
24960 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24961
24962 /* Handle alignas (pack...). */
24963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24964 {
24965 cp_lexer_consume_token (parser->lexer);
24966 alignas_expr = make_pack_expansion (alignas_expr);
24967 }
24968
24969 /* Something went wrong, so don't build the attribute. */
24970 if (alignas_expr == error_mark_node)
24971 return error_mark_node;
24972
24973 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24974 {
24975 cp_parser_error (parser, "expected %<)%>");
24976 return error_mark_node;
24977 }
24978
24979 /* Build the C++-11 representation of an 'aligned'
24980 attribute. */
24981 attributes =
24982 build_tree_list (build_tree_list (get_identifier ("gnu"),
24983 get_identifier ("aligned")),
24984 alignas_expr);
24985 }
24986
24987 return attributes;
24988 }
24989
24990 /* Parse a standard C++-11 attribute-specifier-seq.
24991
24992 attribute-specifier-seq:
24993 attribute-specifier-seq [opt] attribute-specifier
24994 */
24995
24996 static tree
24997 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24998 {
24999 tree attr_specs = NULL_TREE;
25000 tree attr_last = NULL_TREE;
25001
25002 while (true)
25003 {
25004 tree attr_spec = cp_parser_std_attribute_spec (parser);
25005 if (attr_spec == NULL_TREE)
25006 break;
25007 if (attr_spec == error_mark_node)
25008 return error_mark_node;
25009
25010 if (attr_last)
25011 TREE_CHAIN (attr_last) = attr_spec;
25012 else
25013 attr_specs = attr_last = attr_spec;
25014 attr_last = tree_last (attr_last);
25015 }
25016
25017 return attr_specs;
25018 }
25019
25020 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25021 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25022 current value of the PEDANTIC flag, regardless of whether or not
25023 the `__extension__' keyword is present. The caller is responsible
25024 for restoring the value of the PEDANTIC flag. */
25025
25026 static bool
25027 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25028 {
25029 /* Save the old value of the PEDANTIC flag. */
25030 *saved_pedantic = pedantic;
25031
25032 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25033 {
25034 /* Consume the `__extension__' token. */
25035 cp_lexer_consume_token (parser->lexer);
25036 /* We're not being pedantic while the `__extension__' keyword is
25037 in effect. */
25038 pedantic = 0;
25039
25040 return true;
25041 }
25042
25043 return false;
25044 }
25045
25046 /* Parse a label declaration.
25047
25048 label-declaration:
25049 __label__ label-declarator-seq ;
25050
25051 label-declarator-seq:
25052 identifier , label-declarator-seq
25053 identifier */
25054
25055 static void
25056 cp_parser_label_declaration (cp_parser* parser)
25057 {
25058 /* Look for the `__label__' keyword. */
25059 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25060
25061 while (true)
25062 {
25063 tree identifier;
25064
25065 /* Look for an identifier. */
25066 identifier = cp_parser_identifier (parser);
25067 /* If we failed, stop. */
25068 if (identifier == error_mark_node)
25069 break;
25070 /* Declare it as a label. */
25071 finish_label_decl (identifier);
25072 /* If the next token is a `;', stop. */
25073 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25074 break;
25075 /* Look for the `,' separating the label declarations. */
25076 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25077 }
25078
25079 /* Look for the final `;'. */
25080 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25081 }
25082
25083 // -------------------------------------------------------------------------- //
25084 // Requires Clause
25085
25086 // Parse a requires clause.
25087 //
25088 // requires-clause:
25089 // 'requires' logical-or-expression
25090 //
25091 // The required logical-or-expression must be a constant expression. Note
25092 // that we don't check that the expression is constepxr here. We defer until
25093 // we analyze constraints and then, we only check atomic constraints.
25094 static tree
25095 cp_parser_requires_clause (cp_parser *parser)
25096 {
25097 // Parse the requires clause so that it is not automatically folded.
25098 ++processing_template_decl;
25099 tree expr = cp_parser_binary_expression (parser, false, false,
25100 PREC_NOT_OPERATOR, NULL);
25101 if (check_for_bare_parameter_packs (expr))
25102 expr = error_mark_node;
25103 --processing_template_decl;
25104 return expr;
25105 }
25106
25107 // Optionally parse a requires clause:
25108 static tree
25109 cp_parser_requires_clause_opt (cp_parser *parser)
25110 {
25111 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25112 if (tok->keyword != RID_REQUIRES)
25113 {
25114 if (!flag_concepts && tok->type == CPP_NAME
25115 && tok->u.value == ridpointers[RID_REQUIRES])
25116 {
25117 error_at (cp_lexer_peek_token (parser->lexer)->location,
25118 "%<requires%> only available with -fconcepts");
25119 /* Parse and discard the requires-clause. */
25120 cp_lexer_consume_token (parser->lexer);
25121 cp_parser_requires_clause (parser);
25122 }
25123 return NULL_TREE;
25124 }
25125 cp_lexer_consume_token (parser->lexer);
25126 return cp_parser_requires_clause (parser);
25127 }
25128
25129
25130 /*---------------------------------------------------------------------------
25131 Requires expressions
25132 ---------------------------------------------------------------------------*/
25133
25134 /* Parse a requires expression
25135
25136 requirement-expression:
25137 'requires' requirement-parameter-list [opt] requirement-body */
25138 static tree
25139 cp_parser_requires_expression (cp_parser *parser)
25140 {
25141 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25142 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25143
25144 /* A requires-expression shall appear only within a concept
25145 definition or a requires-clause.
25146
25147 TODO: Implement this diagnostic correctly. */
25148 if (!processing_template_decl)
25149 {
25150 error_at (loc, "a requires expression cannot appear outside a template");
25151 cp_parser_skip_to_end_of_statement (parser);
25152 return error_mark_node;
25153 }
25154
25155 tree parms, reqs;
25156 {
25157 /* Local parameters are delared as variables within the scope
25158 of the expression. They are not visible past the end of
25159 the expression. Expressions within the requires-expression
25160 are unevaluated. */
25161 struct scope_sentinel
25162 {
25163 scope_sentinel ()
25164 {
25165 ++cp_unevaluated_operand;
25166 begin_scope (sk_block, NULL_TREE);
25167 }
25168
25169 ~scope_sentinel ()
25170 {
25171 pop_bindings_and_leave_scope ();
25172 --cp_unevaluated_operand;
25173 }
25174 } s;
25175
25176 /* Parse the optional parameter list. */
25177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25178 {
25179 parms = cp_parser_requirement_parameter_list (parser);
25180 if (parms == error_mark_node)
25181 return error_mark_node;
25182 }
25183 else
25184 parms = NULL_TREE;
25185
25186 /* Parse the requirement body. */
25187 reqs = cp_parser_requirement_body (parser);
25188 if (reqs == error_mark_node)
25189 return error_mark_node;
25190 }
25191
25192 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25193 the parm chain. */
25194 grokparms (parms, &parms);
25195 return finish_requires_expr (parms, reqs);
25196 }
25197
25198 /* Parse a parameterized requirement.
25199
25200 requirement-parameter-list:
25201 '(' parameter-declaration-clause ')' */
25202 static tree
25203 cp_parser_requirement_parameter_list (cp_parser *parser)
25204 {
25205 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25206 return error_mark_node;
25207
25208 tree parms = cp_parser_parameter_declaration_clause (parser);
25209
25210 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25211 return error_mark_node;
25212
25213 return parms;
25214 }
25215
25216 /* Parse the body of a requirement.
25217
25218 requirement-body:
25219 '{' requirement-list '}' */
25220 static tree
25221 cp_parser_requirement_body (cp_parser *parser)
25222 {
25223 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25224 return error_mark_node;
25225
25226 tree reqs = cp_parser_requirement_list (parser);
25227
25228 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25229 return error_mark_node;
25230
25231 return reqs;
25232 }
25233
25234 /* Parse a list of requirements.
25235
25236 requirement-list:
25237 requirement
25238 requirement-list ';' requirement[opt] */
25239 static tree
25240 cp_parser_requirement_list (cp_parser *parser)
25241 {
25242 tree result = NULL_TREE;
25243 while (true)
25244 {
25245 tree req = cp_parser_requirement (parser);
25246 if (req == error_mark_node)
25247 return error_mark_node;
25248
25249 result = tree_cons (NULL_TREE, req, result);
25250
25251 /* If we see a semi-colon, consume it. */
25252 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25253 cp_lexer_consume_token (parser->lexer);
25254
25255 /* Stop processing at the end of the list. */
25256 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25257 break;
25258 }
25259
25260 /* Reverse the order of requirements so they are analyzed in
25261 declaration order. */
25262 return nreverse (result);
25263 }
25264
25265 /* Parse a syntactic requirement or type requirement.
25266
25267 requirement:
25268 simple-requirement
25269 compound-requirement
25270 type-requirement
25271 nested-requirement */
25272 static tree
25273 cp_parser_requirement (cp_parser *parser)
25274 {
25275 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25276 return cp_parser_compound_requirement (parser);
25277 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25278 return cp_parser_type_requirement (parser);
25279 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25280 return cp_parser_nested_requirement (parser);
25281 else
25282 return cp_parser_simple_requirement (parser);
25283 }
25284
25285 /* Parse a simple requirement.
25286
25287 simple-requirement:
25288 expression ';' */
25289 static tree
25290 cp_parser_simple_requirement (cp_parser *parser)
25291 {
25292 tree expr = cp_parser_expression (parser, NULL, false, false);
25293 if (!expr || expr == error_mark_node)
25294 return error_mark_node;
25295
25296 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25297 return error_mark_node;
25298
25299 return finish_simple_requirement (expr);
25300 }
25301
25302 /* Parse a type requirement
25303
25304 type-requirement
25305 nested-name-specifier [opt] required-type-name ';'
25306
25307 required-type-name:
25308 type-name
25309 'template' [opt] simple-template-id */
25310 static tree
25311 cp_parser_type_requirement (cp_parser *parser)
25312 {
25313 cp_lexer_consume_token (parser->lexer);
25314
25315 // Save the scope before parsing name specifiers.
25316 tree saved_scope = parser->scope;
25317 tree saved_object_scope = parser->object_scope;
25318 tree saved_qualifying_scope = parser->qualifying_scope;
25319 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25320 cp_parser_nested_name_specifier_opt (parser,
25321 /*typename_keyword_p=*/true,
25322 /*check_dependency_p=*/false,
25323 /*type_p=*/true,
25324 /*is_declaration=*/false);
25325
25326 tree type;
25327 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25328 {
25329 cp_lexer_consume_token (parser->lexer);
25330 type = cp_parser_template_id (parser,
25331 /*template_keyword_p=*/true,
25332 /*check_dependency=*/false,
25333 /*tag_type=*/none_type,
25334 /*is_declaration=*/false);
25335 type = make_typename_type (parser->scope, type, typename_type,
25336 /*complain=*/tf_error);
25337 }
25338 else
25339 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25340
25341 if (TREE_CODE (type) == TYPE_DECL)
25342 type = TREE_TYPE (type);
25343
25344 parser->scope = saved_scope;
25345 parser->object_scope = saved_object_scope;
25346 parser->qualifying_scope = saved_qualifying_scope;
25347
25348 if (type == error_mark_node)
25349 cp_parser_skip_to_end_of_statement (parser);
25350
25351 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25352 return error_mark_node;
25353 if (type == error_mark_node)
25354 return error_mark_node;
25355
25356 return finish_type_requirement (type);
25357 }
25358
25359 /* Parse a compound requirement
25360
25361 compound-requirement:
25362 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25363 static tree
25364 cp_parser_compound_requirement (cp_parser *parser)
25365 {
25366 /* Parse an expression enclosed in '{ }'s. */
25367 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25368 return error_mark_node;
25369
25370 tree expr = cp_parser_expression (parser, NULL, false, false);
25371 if (!expr || expr == error_mark_node)
25372 return error_mark_node;
25373
25374 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25375 return error_mark_node;
25376
25377 /* Parse the optional noexcept. */
25378 bool noexcept_p = false;
25379 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25380 {
25381 cp_lexer_consume_token (parser->lexer);
25382 noexcept_p = true;
25383 }
25384
25385 /* Parse the optional trailing return type. */
25386 tree type = NULL_TREE;
25387 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25388 {
25389 cp_lexer_consume_token (parser->lexer);
25390 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25391 parser->in_result_type_constraint_p = true;
25392 type = cp_parser_trailing_type_id (parser);
25393 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25394 if (type == error_mark_node)
25395 return error_mark_node;
25396 }
25397
25398 return finish_compound_requirement (expr, type, noexcept_p);
25399 }
25400
25401 /* Parse a nested requirement. This is the same as a requires clause.
25402
25403 nested-requirement:
25404 requires-clause */
25405 static tree
25406 cp_parser_nested_requirement (cp_parser *parser)
25407 {
25408 cp_lexer_consume_token (parser->lexer);
25409 tree req = cp_parser_requires_clause (parser);
25410 if (req == error_mark_node)
25411 return error_mark_node;
25412 return finish_nested_requirement (req);
25413 }
25414
25415 /* Support Functions */
25416
25417 /* Return the appropriate prefer_type argument for lookup_name_real based on
25418 tag_type and template_mem_access. */
25419
25420 static inline int
25421 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25422 {
25423 /* DR 141: When looking in the current enclosing context for a template-name
25424 after -> or ., only consider class templates. */
25425 if (template_mem_access)
25426 return 2;
25427 switch (tag_type)
25428 {
25429 case none_type: return 0; // No preference.
25430 case scope_type: return 1; // Type or namespace.
25431 default: return 2; // Type only.
25432 }
25433 }
25434
25435 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25436 NAME should have one of the representations used for an
25437 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25438 is returned. If PARSER->SCOPE is a dependent type, then a
25439 SCOPE_REF is returned.
25440
25441 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25442 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25443 was formed. Abstractly, such entities should not be passed to this
25444 function, because they do not need to be looked up, but it is
25445 simpler to check for this special case here, rather than at the
25446 call-sites.
25447
25448 In cases not explicitly covered above, this function returns a
25449 DECL, OVERLOAD, or baselink representing the result of the lookup.
25450 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25451 is returned.
25452
25453 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25454 (e.g., "struct") that was used. In that case bindings that do not
25455 refer to types are ignored.
25456
25457 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25458 ignored.
25459
25460 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25461 are ignored.
25462
25463 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25464 types.
25465
25466 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25467 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25468 NULL_TREE otherwise. */
25469
25470 static cp_expr
25471 cp_parser_lookup_name (cp_parser *parser, tree name,
25472 enum tag_types tag_type,
25473 bool is_template,
25474 bool is_namespace,
25475 bool check_dependency,
25476 tree *ambiguous_decls,
25477 location_t name_location)
25478 {
25479 tree decl;
25480 tree object_type = parser->context->object_type;
25481
25482 /* Assume that the lookup will be unambiguous. */
25483 if (ambiguous_decls)
25484 *ambiguous_decls = NULL_TREE;
25485
25486 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25487 no longer valid. Note that if we are parsing tentatively, and
25488 the parse fails, OBJECT_TYPE will be automatically restored. */
25489 parser->context->object_type = NULL_TREE;
25490
25491 if (name == error_mark_node)
25492 return error_mark_node;
25493
25494 /* A template-id has already been resolved; there is no lookup to
25495 do. */
25496 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25497 return name;
25498 if (BASELINK_P (name))
25499 {
25500 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25501 == TEMPLATE_ID_EXPR);
25502 return name;
25503 }
25504
25505 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25506 it should already have been checked to make sure that the name
25507 used matches the type being destroyed. */
25508 if (TREE_CODE (name) == BIT_NOT_EXPR)
25509 {
25510 tree type;
25511
25512 /* Figure out to which type this destructor applies. */
25513 if (parser->scope)
25514 type = parser->scope;
25515 else if (object_type)
25516 type = object_type;
25517 else
25518 type = current_class_type;
25519 /* If that's not a class type, there is no destructor. */
25520 if (!type || !CLASS_TYPE_P (type))
25521 return error_mark_node;
25522 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25523 lazily_declare_fn (sfk_destructor, type);
25524 if (!CLASSTYPE_DESTRUCTORS (type))
25525 return error_mark_node;
25526 /* If it was a class type, return the destructor. */
25527 return CLASSTYPE_DESTRUCTORS (type);
25528 }
25529
25530 /* By this point, the NAME should be an ordinary identifier. If
25531 the id-expression was a qualified name, the qualifying scope is
25532 stored in PARSER->SCOPE at this point. */
25533 gcc_assert (identifier_p (name));
25534
25535 /* Perform the lookup. */
25536 if (parser->scope)
25537 {
25538 bool dependent_p;
25539
25540 if (parser->scope == error_mark_node)
25541 return error_mark_node;
25542
25543 /* If the SCOPE is dependent, the lookup must be deferred until
25544 the template is instantiated -- unless we are explicitly
25545 looking up names in uninstantiated templates. Even then, we
25546 cannot look up the name if the scope is not a class type; it
25547 might, for example, be a template type parameter. */
25548 dependent_p = (TYPE_P (parser->scope)
25549 && dependent_scope_p (parser->scope));
25550 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25551 && dependent_p)
25552 /* Defer lookup. */
25553 decl = error_mark_node;
25554 else
25555 {
25556 tree pushed_scope = NULL_TREE;
25557
25558 /* If PARSER->SCOPE is a dependent type, then it must be a
25559 class type, and we must not be checking dependencies;
25560 otherwise, we would have processed this lookup above. So
25561 that PARSER->SCOPE is not considered a dependent base by
25562 lookup_member, we must enter the scope here. */
25563 if (dependent_p)
25564 pushed_scope = push_scope (parser->scope);
25565
25566 /* If the PARSER->SCOPE is a template specialization, it
25567 may be instantiated during name lookup. In that case,
25568 errors may be issued. Even if we rollback the current
25569 tentative parse, those errors are valid. */
25570 decl = lookup_qualified_name (parser->scope, name,
25571 prefer_type_arg (tag_type),
25572 /*complain=*/true);
25573
25574 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25575 lookup result and the nested-name-specifier nominates a class C:
25576 * if the name specified after the nested-name-specifier, when
25577 looked up in C, is the injected-class-name of C (Clause 9), or
25578 * if the name specified after the nested-name-specifier is the
25579 same as the identifier or the simple-template-id's template-
25580 name in the last component of the nested-name-specifier,
25581 the name is instead considered to name the constructor of
25582 class C. [ Note: for example, the constructor is not an
25583 acceptable lookup result in an elaborated-type-specifier so
25584 the constructor would not be used in place of the
25585 injected-class-name. --end note ] Such a constructor name
25586 shall be used only in the declarator-id of a declaration that
25587 names a constructor or in a using-declaration. */
25588 if (tag_type == none_type
25589 && DECL_SELF_REFERENCE_P (decl)
25590 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25591 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25592 prefer_type_arg (tag_type),
25593 /*complain=*/true);
25594
25595 /* If we have a single function from a using decl, pull it out. */
25596 if (TREE_CODE (decl) == OVERLOAD
25597 && !really_overloaded_fn (decl))
25598 decl = OVL_FUNCTION (decl);
25599
25600 if (pushed_scope)
25601 pop_scope (pushed_scope);
25602 }
25603
25604 /* If the scope is a dependent type and either we deferred lookup or
25605 we did lookup but didn't find the name, rememeber the name. */
25606 if (decl == error_mark_node && TYPE_P (parser->scope)
25607 && dependent_type_p (parser->scope))
25608 {
25609 if (tag_type)
25610 {
25611 tree type;
25612
25613 /* The resolution to Core Issue 180 says that `struct
25614 A::B' should be considered a type-name, even if `A'
25615 is dependent. */
25616 type = make_typename_type (parser->scope, name, tag_type,
25617 /*complain=*/tf_error);
25618 if (type != error_mark_node)
25619 decl = TYPE_NAME (type);
25620 }
25621 else if (is_template
25622 && (cp_parser_next_token_ends_template_argument_p (parser)
25623 || cp_lexer_next_token_is (parser->lexer,
25624 CPP_CLOSE_PAREN)))
25625 decl = make_unbound_class_template (parser->scope,
25626 name, NULL_TREE,
25627 /*complain=*/tf_error);
25628 else
25629 decl = build_qualified_name (/*type=*/NULL_TREE,
25630 parser->scope, name,
25631 is_template);
25632 }
25633 parser->qualifying_scope = parser->scope;
25634 parser->object_scope = NULL_TREE;
25635 }
25636 else if (object_type)
25637 {
25638 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25639 OBJECT_TYPE is not a class. */
25640 if (CLASS_TYPE_P (object_type))
25641 /* If the OBJECT_TYPE is a template specialization, it may
25642 be instantiated during name lookup. In that case, errors
25643 may be issued. Even if we rollback the current tentative
25644 parse, those errors are valid. */
25645 decl = lookup_member (object_type,
25646 name,
25647 /*protect=*/0,
25648 prefer_type_arg (tag_type),
25649 tf_warning_or_error);
25650 else
25651 decl = NULL_TREE;
25652
25653 if (!decl)
25654 /* Look it up in the enclosing context. DR 141: When looking for a
25655 template-name after -> or ., only consider class templates. */
25656 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25657 /*nonclass=*/0,
25658 /*block_p=*/true, is_namespace, 0);
25659 if (object_type == unknown_type_node)
25660 /* The object is type-dependent, so we can't look anything up; we used
25661 this to get the DR 141 behavior. */
25662 object_type = NULL_TREE;
25663 parser->object_scope = object_type;
25664 parser->qualifying_scope = NULL_TREE;
25665 }
25666 else
25667 {
25668 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25669 /*nonclass=*/0,
25670 /*block_p=*/true, is_namespace, 0);
25671 parser->qualifying_scope = NULL_TREE;
25672 parser->object_scope = NULL_TREE;
25673 }
25674
25675 /* If the lookup failed, let our caller know. */
25676 if (!decl || decl == error_mark_node)
25677 return error_mark_node;
25678
25679 /* Pull out the template from an injected-class-name (or multiple). */
25680 if (is_template)
25681 decl = maybe_get_template_decl_from_type_decl (decl);
25682
25683 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25684 if (TREE_CODE (decl) == TREE_LIST)
25685 {
25686 if (ambiguous_decls)
25687 *ambiguous_decls = decl;
25688 /* The error message we have to print is too complicated for
25689 cp_parser_error, so we incorporate its actions directly. */
25690 if (!cp_parser_simulate_error (parser))
25691 {
25692 error_at (name_location, "reference to %qD is ambiguous",
25693 name);
25694 print_candidates (decl);
25695 }
25696 return error_mark_node;
25697 }
25698
25699 gcc_assert (DECL_P (decl)
25700 || TREE_CODE (decl) == OVERLOAD
25701 || TREE_CODE (decl) == SCOPE_REF
25702 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25703 || BASELINK_P (decl));
25704
25705 /* If we have resolved the name of a member declaration, check to
25706 see if the declaration is accessible. When the name resolves to
25707 set of overloaded functions, accessibility is checked when
25708 overload resolution is done.
25709
25710 During an explicit instantiation, access is not checked at all,
25711 as per [temp.explicit]. */
25712 if (DECL_P (decl))
25713 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25714
25715 maybe_record_typedef_use (decl);
25716
25717 return cp_expr (decl, name_location);
25718 }
25719
25720 /* Like cp_parser_lookup_name, but for use in the typical case where
25721 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25722 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25723
25724 static tree
25725 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25726 {
25727 return cp_parser_lookup_name (parser, name,
25728 none_type,
25729 /*is_template=*/false,
25730 /*is_namespace=*/false,
25731 /*check_dependency=*/true,
25732 /*ambiguous_decls=*/NULL,
25733 location);
25734 }
25735
25736 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25737 the current context, return the TYPE_DECL. If TAG_NAME_P is
25738 true, the DECL indicates the class being defined in a class-head,
25739 or declared in an elaborated-type-specifier.
25740
25741 Otherwise, return DECL. */
25742
25743 static tree
25744 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25745 {
25746 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25747 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25748
25749 struct A {
25750 template <typename T> struct B;
25751 };
25752
25753 template <typename T> struct A::B {};
25754
25755 Similarly, in an elaborated-type-specifier:
25756
25757 namespace N { struct X{}; }
25758
25759 struct A {
25760 template <typename T> friend struct N::X;
25761 };
25762
25763 However, if the DECL refers to a class type, and we are in
25764 the scope of the class, then the name lookup automatically
25765 finds the TYPE_DECL created by build_self_reference rather
25766 than a TEMPLATE_DECL. For example, in:
25767
25768 template <class T> struct S {
25769 S s;
25770 };
25771
25772 there is no need to handle such case. */
25773
25774 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25775 return DECL_TEMPLATE_RESULT (decl);
25776
25777 return decl;
25778 }
25779
25780 /* If too many, or too few, template-parameter lists apply to the
25781 declarator, issue an error message. Returns TRUE if all went well,
25782 and FALSE otherwise. */
25783
25784 static bool
25785 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25786 cp_declarator *declarator,
25787 location_t declarator_location)
25788 {
25789 switch (declarator->kind)
25790 {
25791 case cdk_id:
25792 {
25793 unsigned num_templates = 0;
25794 tree scope = declarator->u.id.qualifying_scope;
25795
25796 if (scope)
25797 num_templates = num_template_headers_for_class (scope);
25798 else if (TREE_CODE (declarator->u.id.unqualified_name)
25799 == TEMPLATE_ID_EXPR)
25800 /* If the DECLARATOR has the form `X<y>' then it uses one
25801 additional level of template parameters. */
25802 ++num_templates;
25803
25804 return cp_parser_check_template_parameters
25805 (parser, num_templates, declarator_location, declarator);
25806 }
25807
25808 case cdk_function:
25809 case cdk_array:
25810 case cdk_pointer:
25811 case cdk_reference:
25812 case cdk_ptrmem:
25813 return (cp_parser_check_declarator_template_parameters
25814 (parser, declarator->declarator, declarator_location));
25815
25816 case cdk_decomp:
25817 case cdk_error:
25818 return true;
25819
25820 default:
25821 gcc_unreachable ();
25822 }
25823 return false;
25824 }
25825
25826 /* NUM_TEMPLATES were used in the current declaration. If that is
25827 invalid, return FALSE and issue an error messages. Otherwise,
25828 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25829 declarator and we can print more accurate diagnostics. */
25830
25831 static bool
25832 cp_parser_check_template_parameters (cp_parser* parser,
25833 unsigned num_templates,
25834 location_t location,
25835 cp_declarator *declarator)
25836 {
25837 /* If there are the same number of template classes and parameter
25838 lists, that's OK. */
25839 if (parser->num_template_parameter_lists == num_templates)
25840 return true;
25841 /* If there are more, but only one more, then we are referring to a
25842 member template. That's OK too. */
25843 if (parser->num_template_parameter_lists == num_templates + 1)
25844 return true;
25845 /* If there are more template classes than parameter lists, we have
25846 something like:
25847
25848 template <class T> void S<T>::R<T>::f (); */
25849 if (parser->num_template_parameter_lists < num_templates)
25850 {
25851 if (declarator && !current_function_decl)
25852 error_at (location, "specializing member %<%T::%E%> "
25853 "requires %<template<>%> syntax",
25854 declarator->u.id.qualifying_scope,
25855 declarator->u.id.unqualified_name);
25856 else if (declarator)
25857 error_at (location, "invalid declaration of %<%T::%E%>",
25858 declarator->u.id.qualifying_scope,
25859 declarator->u.id.unqualified_name);
25860 else
25861 error_at (location, "too few template-parameter-lists");
25862 return false;
25863 }
25864 /* Otherwise, there are too many template parameter lists. We have
25865 something like:
25866
25867 template <class T> template <class U> void S::f(); */
25868 error_at (location, "too many template-parameter-lists");
25869 return false;
25870 }
25871
25872 /* Parse an optional `::' token indicating that the following name is
25873 from the global namespace. If so, PARSER->SCOPE is set to the
25874 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25875 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25876 Returns the new value of PARSER->SCOPE, if the `::' token is
25877 present, and NULL_TREE otherwise. */
25878
25879 static tree
25880 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25881 {
25882 cp_token *token;
25883
25884 /* Peek at the next token. */
25885 token = cp_lexer_peek_token (parser->lexer);
25886 /* If we're looking at a `::' token then we're starting from the
25887 global namespace, not our current location. */
25888 if (token->type == CPP_SCOPE)
25889 {
25890 /* Consume the `::' token. */
25891 cp_lexer_consume_token (parser->lexer);
25892 /* Set the SCOPE so that we know where to start the lookup. */
25893 parser->scope = global_namespace;
25894 parser->qualifying_scope = global_namespace;
25895 parser->object_scope = NULL_TREE;
25896
25897 return parser->scope;
25898 }
25899 else if (!current_scope_valid_p)
25900 {
25901 parser->scope = NULL_TREE;
25902 parser->qualifying_scope = NULL_TREE;
25903 parser->object_scope = NULL_TREE;
25904 }
25905
25906 return NULL_TREE;
25907 }
25908
25909 /* Returns TRUE if the upcoming token sequence is the start of a
25910 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25911 declarator is preceded by the `friend' specifier. */
25912
25913 static bool
25914 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25915 {
25916 bool constructor_p;
25917 bool outside_class_specifier_p;
25918 tree nested_name_specifier;
25919 cp_token *next_token;
25920
25921 /* The common case is that this is not a constructor declarator, so
25922 try to avoid doing lots of work if at all possible. It's not
25923 valid declare a constructor at function scope. */
25924 if (parser->in_function_body)
25925 return false;
25926 /* And only certain tokens can begin a constructor declarator. */
25927 next_token = cp_lexer_peek_token (parser->lexer);
25928 if (next_token->type != CPP_NAME
25929 && next_token->type != CPP_SCOPE
25930 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25931 && next_token->type != CPP_TEMPLATE_ID)
25932 return false;
25933
25934 /* Parse tentatively; we are going to roll back all of the tokens
25935 consumed here. */
25936 cp_parser_parse_tentatively (parser);
25937 /* Assume that we are looking at a constructor declarator. */
25938 constructor_p = true;
25939
25940 /* Look for the optional `::' operator. */
25941 cp_parser_global_scope_opt (parser,
25942 /*current_scope_valid_p=*/false);
25943 /* Look for the nested-name-specifier. */
25944 nested_name_specifier
25945 = (cp_parser_nested_name_specifier_opt (parser,
25946 /*typename_keyword_p=*/false,
25947 /*check_dependency_p=*/false,
25948 /*type_p=*/false,
25949 /*is_declaration=*/false));
25950
25951 outside_class_specifier_p = (!at_class_scope_p ()
25952 || !TYPE_BEING_DEFINED (current_class_type)
25953 || friend_p);
25954
25955 /* Outside of a class-specifier, there must be a
25956 nested-name-specifier. Except in C++17 mode, where we
25957 might be declaring a guiding declaration. */
25958 if (!nested_name_specifier && outside_class_specifier_p
25959 && cxx_dialect < cxx1z)
25960 constructor_p = false;
25961 else if (nested_name_specifier == error_mark_node)
25962 constructor_p = false;
25963
25964 /* If we have a class scope, this is easy; DR 147 says that S::S always
25965 names the constructor, and no other qualified name could. */
25966 if (constructor_p && nested_name_specifier
25967 && CLASS_TYPE_P (nested_name_specifier))
25968 {
25969 tree id = cp_parser_unqualified_id (parser,
25970 /*template_keyword_p=*/false,
25971 /*check_dependency_p=*/false,
25972 /*declarator_p=*/true,
25973 /*optional_p=*/false);
25974 if (is_overloaded_fn (id))
25975 id = DECL_NAME (get_first_fn (id));
25976 if (!constructor_name_p (id, nested_name_specifier))
25977 constructor_p = false;
25978 }
25979 /* If we still think that this might be a constructor-declarator,
25980 look for a class-name. */
25981 else if (constructor_p)
25982 {
25983 /* If we have:
25984
25985 template <typename T> struct S {
25986 S();
25987 };
25988
25989 we must recognize that the nested `S' names a class. */
25990 if (cxx_dialect >= cxx1z)
25991 cp_parser_parse_tentatively (parser);
25992
25993 tree type_decl;
25994 type_decl = cp_parser_class_name (parser,
25995 /*typename_keyword_p=*/false,
25996 /*template_keyword_p=*/false,
25997 none_type,
25998 /*check_dependency_p=*/false,
25999 /*class_head_p=*/false,
26000 /*is_declaration=*/false);
26001
26002 if (cxx_dialect >= cxx1z
26003 && !cp_parser_parse_definitely (parser))
26004 {
26005 type_decl = NULL_TREE;
26006 tree tmpl = cp_parser_template_name (parser,
26007 /*template_keyword*/false,
26008 /*check_dependency_p*/false,
26009 /*is_declaration*/false,
26010 none_type,
26011 /*is_identifier*/NULL);
26012 if (DECL_CLASS_TEMPLATE_P (tmpl)
26013 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26014 /* It's a deduction guide, return true. */;
26015 else
26016 cp_parser_simulate_error (parser);
26017 }
26018
26019 /* If there was no class-name, then this is not a constructor.
26020 Otherwise, if we are in a class-specifier and we aren't
26021 handling a friend declaration, check that its type matches
26022 current_class_type (c++/38313). Note: error_mark_node
26023 is left alone for error recovery purposes. */
26024 constructor_p = (!cp_parser_error_occurred (parser)
26025 && (outside_class_specifier_p
26026 || type_decl == NULL_TREE
26027 || type_decl == error_mark_node
26028 || same_type_p (current_class_type,
26029 TREE_TYPE (type_decl))));
26030
26031 /* If we're still considering a constructor, we have to see a `(',
26032 to begin the parameter-declaration-clause, followed by either a
26033 `)', an `...', or a decl-specifier. We need to check for a
26034 type-specifier to avoid being fooled into thinking that:
26035
26036 S (f) (int);
26037
26038 is a constructor. (It is actually a function named `f' that
26039 takes one parameter (of type `int') and returns a value of type
26040 `S'. */
26041 if (constructor_p
26042 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26043 constructor_p = false;
26044
26045 if (constructor_p
26046 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26047 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26048 /* A parameter declaration begins with a decl-specifier,
26049 which is either the "attribute" keyword, a storage class
26050 specifier, or (usually) a type-specifier. */
26051 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26052 {
26053 tree type;
26054 tree pushed_scope = NULL_TREE;
26055 unsigned saved_num_template_parameter_lists;
26056
26057 /* Names appearing in the type-specifier should be looked up
26058 in the scope of the class. */
26059 if (current_class_type)
26060 type = NULL_TREE;
26061 else if (type_decl)
26062 {
26063 type = TREE_TYPE (type_decl);
26064 if (TREE_CODE (type) == TYPENAME_TYPE)
26065 {
26066 type = resolve_typename_type (type,
26067 /*only_current_p=*/false);
26068 if (TREE_CODE (type) == TYPENAME_TYPE)
26069 {
26070 cp_parser_abort_tentative_parse (parser);
26071 return false;
26072 }
26073 }
26074 pushed_scope = push_scope (type);
26075 }
26076
26077 /* Inside the constructor parameter list, surrounding
26078 template-parameter-lists do not apply. */
26079 saved_num_template_parameter_lists
26080 = parser->num_template_parameter_lists;
26081 parser->num_template_parameter_lists = 0;
26082
26083 /* Look for the type-specifier. */
26084 cp_parser_type_specifier (parser,
26085 CP_PARSER_FLAGS_NONE,
26086 /*decl_specs=*/NULL,
26087 /*is_declarator=*/true,
26088 /*declares_class_or_enum=*/NULL,
26089 /*is_cv_qualifier=*/NULL);
26090
26091 parser->num_template_parameter_lists
26092 = saved_num_template_parameter_lists;
26093
26094 /* Leave the scope of the class. */
26095 if (pushed_scope)
26096 pop_scope (pushed_scope);
26097
26098 constructor_p = !cp_parser_error_occurred (parser);
26099 }
26100 }
26101
26102 /* We did not really want to consume any tokens. */
26103 cp_parser_abort_tentative_parse (parser);
26104
26105 return constructor_p;
26106 }
26107
26108 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26109 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26110 they must be performed once we are in the scope of the function.
26111
26112 Returns the function defined. */
26113
26114 static tree
26115 cp_parser_function_definition_from_specifiers_and_declarator
26116 (cp_parser* parser,
26117 cp_decl_specifier_seq *decl_specifiers,
26118 tree attributes,
26119 const cp_declarator *declarator)
26120 {
26121 tree fn;
26122 bool success_p;
26123
26124 /* Begin the function-definition. */
26125 success_p = start_function (decl_specifiers, declarator, attributes);
26126
26127 /* The things we're about to see are not directly qualified by any
26128 template headers we've seen thus far. */
26129 reset_specialization ();
26130
26131 /* If there were names looked up in the decl-specifier-seq that we
26132 did not check, check them now. We must wait until we are in the
26133 scope of the function to perform the checks, since the function
26134 might be a friend. */
26135 perform_deferred_access_checks (tf_warning_or_error);
26136
26137 if (success_p)
26138 {
26139 cp_finalize_omp_declare_simd (parser, current_function_decl);
26140 parser->omp_declare_simd = NULL;
26141 cp_finalize_oacc_routine (parser, current_function_decl, true);
26142 parser->oacc_routine = NULL;
26143 }
26144
26145 if (!success_p)
26146 {
26147 /* Skip the entire function. */
26148 cp_parser_skip_to_end_of_block_or_statement (parser);
26149 fn = error_mark_node;
26150 }
26151 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26152 {
26153 /* Seen already, skip it. An error message has already been output. */
26154 cp_parser_skip_to_end_of_block_or_statement (parser);
26155 fn = current_function_decl;
26156 current_function_decl = NULL_TREE;
26157 /* If this is a function from a class, pop the nested class. */
26158 if (current_class_name)
26159 pop_nested_class ();
26160 }
26161 else
26162 {
26163 timevar_id_t tv;
26164 if (DECL_DECLARED_INLINE_P (current_function_decl))
26165 tv = TV_PARSE_INLINE;
26166 else
26167 tv = TV_PARSE_FUNC;
26168 timevar_push (tv);
26169 fn = cp_parser_function_definition_after_declarator (parser,
26170 /*inline_p=*/false);
26171 timevar_pop (tv);
26172 }
26173
26174 return fn;
26175 }
26176
26177 /* Parse the part of a function-definition that follows the
26178 declarator. INLINE_P is TRUE iff this function is an inline
26179 function defined within a class-specifier.
26180
26181 Returns the function defined. */
26182
26183 static tree
26184 cp_parser_function_definition_after_declarator (cp_parser* parser,
26185 bool inline_p)
26186 {
26187 tree fn;
26188 bool ctor_initializer_p = false;
26189 bool saved_in_unbraced_linkage_specification_p;
26190 bool saved_in_function_body;
26191 unsigned saved_num_template_parameter_lists;
26192 cp_token *token;
26193 bool fully_implicit_function_template_p
26194 = parser->fully_implicit_function_template_p;
26195 parser->fully_implicit_function_template_p = false;
26196 tree implicit_template_parms
26197 = parser->implicit_template_parms;
26198 parser->implicit_template_parms = 0;
26199 cp_binding_level* implicit_template_scope
26200 = parser->implicit_template_scope;
26201 parser->implicit_template_scope = 0;
26202
26203 saved_in_function_body = parser->in_function_body;
26204 parser->in_function_body = true;
26205 /* If the next token is `return', then the code may be trying to
26206 make use of the "named return value" extension that G++ used to
26207 support. */
26208 token = cp_lexer_peek_token (parser->lexer);
26209 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26210 {
26211 /* Consume the `return' keyword. */
26212 cp_lexer_consume_token (parser->lexer);
26213 /* Look for the identifier that indicates what value is to be
26214 returned. */
26215 cp_parser_identifier (parser);
26216 /* Issue an error message. */
26217 error_at (token->location,
26218 "named return values are no longer supported");
26219 /* Skip tokens until we reach the start of the function body. */
26220 while (true)
26221 {
26222 cp_token *token = cp_lexer_peek_token (parser->lexer);
26223 if (token->type == CPP_OPEN_BRACE
26224 || token->type == CPP_EOF
26225 || token->type == CPP_PRAGMA_EOL)
26226 break;
26227 cp_lexer_consume_token (parser->lexer);
26228 }
26229 }
26230 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26231 anything declared inside `f'. */
26232 saved_in_unbraced_linkage_specification_p
26233 = parser->in_unbraced_linkage_specification_p;
26234 parser->in_unbraced_linkage_specification_p = false;
26235 /* Inside the function, surrounding template-parameter-lists do not
26236 apply. */
26237 saved_num_template_parameter_lists
26238 = parser->num_template_parameter_lists;
26239 parser->num_template_parameter_lists = 0;
26240
26241 start_lambda_scope (current_function_decl);
26242
26243 /* If the next token is `try', `__transaction_atomic', or
26244 `__transaction_relaxed`, then we are looking at either function-try-block
26245 or function-transaction-block. Note that all of these include the
26246 function-body. */
26247 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26248 ctor_initializer_p = cp_parser_function_transaction (parser,
26249 RID_TRANSACTION_ATOMIC);
26250 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26251 RID_TRANSACTION_RELAXED))
26252 ctor_initializer_p = cp_parser_function_transaction (parser,
26253 RID_TRANSACTION_RELAXED);
26254 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26255 ctor_initializer_p = cp_parser_function_try_block (parser);
26256 else
26257 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26258 (parser, /*in_function_try_block=*/false);
26259
26260 finish_lambda_scope ();
26261
26262 /* Finish the function. */
26263 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26264 (inline_p ? 2 : 0));
26265 /* Generate code for it, if necessary. */
26266 expand_or_defer_fn (fn);
26267 /* Restore the saved values. */
26268 parser->in_unbraced_linkage_specification_p
26269 = saved_in_unbraced_linkage_specification_p;
26270 parser->num_template_parameter_lists
26271 = saved_num_template_parameter_lists;
26272 parser->in_function_body = saved_in_function_body;
26273
26274 parser->fully_implicit_function_template_p
26275 = fully_implicit_function_template_p;
26276 parser->implicit_template_parms
26277 = implicit_template_parms;
26278 parser->implicit_template_scope
26279 = implicit_template_scope;
26280
26281 if (parser->fully_implicit_function_template_p)
26282 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26283
26284 return fn;
26285 }
26286
26287 /* Parse a template-declaration body (following argument list). */
26288
26289 static void
26290 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26291 tree parameter_list,
26292 bool member_p)
26293 {
26294 tree decl = NULL_TREE;
26295 bool friend_p = false;
26296
26297 /* We just processed one more parameter list. */
26298 ++parser->num_template_parameter_lists;
26299
26300 /* Get the deferred access checks from the parameter list. These
26301 will be checked once we know what is being declared, as for a
26302 member template the checks must be performed in the scope of the
26303 class containing the member. */
26304 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26305
26306 /* Tentatively parse for a new template parameter list, which can either be
26307 the template keyword or a template introduction. */
26308 if (cp_parser_template_declaration_after_export (parser, member_p))
26309 /* OK */;
26310 else if (cxx_dialect >= cxx11
26311 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26312 decl = cp_parser_alias_declaration (parser);
26313 else
26314 {
26315 /* There are no access checks when parsing a template, as we do not
26316 know if a specialization will be a friend. */
26317 push_deferring_access_checks (dk_no_check);
26318 cp_token *token = cp_lexer_peek_token (parser->lexer);
26319 decl = cp_parser_single_declaration (parser,
26320 checks,
26321 member_p,
26322 /*explicit_specialization_p=*/false,
26323 &friend_p);
26324 pop_deferring_access_checks ();
26325
26326 /* If this is a member template declaration, let the front
26327 end know. */
26328 if (member_p && !friend_p && decl)
26329 {
26330 if (TREE_CODE (decl) == TYPE_DECL)
26331 cp_parser_check_access_in_redeclaration (decl, token->location);
26332
26333 decl = finish_member_template_decl (decl);
26334 }
26335 else if (friend_p && decl
26336 && DECL_DECLARES_TYPE_P (decl))
26337 make_friend_class (current_class_type, TREE_TYPE (decl),
26338 /*complain=*/true);
26339 }
26340 /* We are done with the current parameter list. */
26341 --parser->num_template_parameter_lists;
26342
26343 pop_deferring_access_checks ();
26344
26345 /* Finish up. */
26346 finish_template_decl (parameter_list);
26347
26348 /* Check the template arguments for a literal operator template. */
26349 if (decl
26350 && DECL_DECLARES_FUNCTION_P (decl)
26351 && UDLIT_OPER_P (DECL_NAME (decl)))
26352 {
26353 bool ok = true;
26354 if (parameter_list == NULL_TREE)
26355 ok = false;
26356 else
26357 {
26358 int num_parms = TREE_VEC_LENGTH (parameter_list);
26359 if (num_parms == 1)
26360 {
26361 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26362 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26363 if (TREE_TYPE (parm) != char_type_node
26364 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26365 ok = false;
26366 }
26367 else if (num_parms == 2 && cxx_dialect >= cxx14)
26368 {
26369 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26370 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26371 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26372 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26373 if (parm == error_mark_node
26374 || TREE_TYPE (parm) != TREE_TYPE (type)
26375 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26376 ok = false;
26377 }
26378 else
26379 ok = false;
26380 }
26381 if (!ok)
26382 {
26383 if (cxx_dialect >= cxx14)
26384 error ("literal operator template %qD has invalid parameter list."
26385 " Expected non-type template argument pack <char...>"
26386 " or <typename CharT, CharT...>",
26387 decl);
26388 else
26389 error ("literal operator template %qD has invalid parameter list."
26390 " Expected non-type template argument pack <char...>",
26391 decl);
26392 }
26393 }
26394
26395 /* Register member declarations. */
26396 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26397 finish_member_declaration (decl);
26398 /* If DECL is a function template, we must return to parse it later.
26399 (Even though there is no definition, there might be default
26400 arguments that need handling.) */
26401 if (member_p && decl
26402 && DECL_DECLARES_FUNCTION_P (decl))
26403 vec_safe_push (unparsed_funs_with_definitions, decl);
26404 }
26405
26406 /* Parse a template introduction header for a template-declaration. Returns
26407 false if tentative parse fails. */
26408
26409 static bool
26410 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26411 {
26412 cp_parser_parse_tentatively (parser);
26413
26414 tree saved_scope = parser->scope;
26415 tree saved_object_scope = parser->object_scope;
26416 tree saved_qualifying_scope = parser->qualifying_scope;
26417
26418 /* Look for the optional `::' operator. */
26419 cp_parser_global_scope_opt (parser,
26420 /*current_scope_valid_p=*/false);
26421 /* Look for the nested-name-specifier. */
26422 cp_parser_nested_name_specifier_opt (parser,
26423 /*typename_keyword_p=*/false,
26424 /*check_dependency_p=*/true,
26425 /*type_p=*/false,
26426 /*is_declaration=*/false);
26427
26428 cp_token *token = cp_lexer_peek_token (parser->lexer);
26429 tree concept_name = cp_parser_identifier (parser);
26430
26431 /* Look up the concept for which we will be matching
26432 template parameters. */
26433 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26434 token->location);
26435 parser->scope = saved_scope;
26436 parser->object_scope = saved_object_scope;
26437 parser->qualifying_scope = saved_qualifying_scope;
26438
26439 if (concept_name == error_mark_node)
26440 cp_parser_simulate_error (parser);
26441
26442 /* Look for opening brace for introduction. */
26443 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26444
26445 if (!cp_parser_parse_definitely (parser))
26446 return false;
26447
26448 push_deferring_access_checks (dk_deferred);
26449
26450 /* Build vector of placeholder parameters and grab
26451 matching identifiers. */
26452 tree introduction_list = cp_parser_introduction_list (parser);
26453
26454 /* The introduction-list shall not be empty. */
26455 int nargs = TREE_VEC_LENGTH (introduction_list);
26456 if (nargs == 0)
26457 {
26458 error ("empty introduction-list");
26459 return true;
26460 }
26461
26462 /* Look for closing brace for introduction. */
26463 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26464 return true;
26465
26466 if (tmpl_decl == error_mark_node)
26467 {
26468 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26469 token->location);
26470 return true;
26471 }
26472
26473 /* Build and associate the constraint. */
26474 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26475 if (parms && parms != error_mark_node)
26476 {
26477 cp_parser_template_declaration_after_parameters (parser, parms,
26478 member_p);
26479 return true;
26480 }
26481
26482 error_at (token->location, "no matching concept for template-introduction");
26483 return true;
26484 }
26485
26486 /* Parse a normal template-declaration following the template keyword. */
26487
26488 static void
26489 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26490 {
26491 tree parameter_list;
26492 bool need_lang_pop;
26493 location_t location = input_location;
26494
26495 /* Look for the `<' token. */
26496 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26497 return;
26498 if (at_class_scope_p () && current_function_decl)
26499 {
26500 /* 14.5.2.2 [temp.mem]
26501
26502 A local class shall not have member templates. */
26503 error_at (location,
26504 "invalid declaration of member template in local class");
26505 cp_parser_skip_to_end_of_block_or_statement (parser);
26506 return;
26507 }
26508 /* [temp]
26509
26510 A template ... shall not have C linkage. */
26511 if (current_lang_name == lang_name_c)
26512 {
26513 error_at (location, "template with C linkage");
26514 /* Give it C++ linkage to avoid confusing other parts of the
26515 front end. */
26516 push_lang_context (lang_name_cplusplus);
26517 need_lang_pop = true;
26518 }
26519 else
26520 need_lang_pop = false;
26521
26522 /* We cannot perform access checks on the template parameter
26523 declarations until we know what is being declared, just as we
26524 cannot check the decl-specifier list. */
26525 push_deferring_access_checks (dk_deferred);
26526
26527 /* If the next token is `>', then we have an invalid
26528 specialization. Rather than complain about an invalid template
26529 parameter, issue an error message here. */
26530 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26531 {
26532 cp_parser_error (parser, "invalid explicit specialization");
26533 begin_specialization ();
26534 parameter_list = NULL_TREE;
26535 }
26536 else
26537 {
26538 /* Parse the template parameters. */
26539 parameter_list = cp_parser_template_parameter_list (parser);
26540 }
26541
26542 /* Look for the `>'. */
26543 cp_parser_skip_to_end_of_template_parameter_list (parser);
26544
26545 /* Manage template requirements */
26546 if (flag_concepts)
26547 {
26548 tree reqs = get_shorthand_constraints (current_template_parms);
26549 if (tree r = cp_parser_requires_clause_opt (parser))
26550 reqs = conjoin_constraints (reqs, normalize_expression (r));
26551 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26552 }
26553
26554 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26555 member_p);
26556
26557 /* For the erroneous case of a template with C linkage, we pushed an
26558 implicit C++ linkage scope; exit that scope now. */
26559 if (need_lang_pop)
26560 pop_lang_context ();
26561 }
26562
26563 /* Parse a template-declaration, assuming that the `export' (and
26564 `extern') keywords, if present, has already been scanned. MEMBER_P
26565 is as for cp_parser_template_declaration. */
26566
26567 static bool
26568 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26569 {
26570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26571 {
26572 cp_lexer_consume_token (parser->lexer);
26573 cp_parser_explicit_template_declaration (parser, member_p);
26574 return true;
26575 }
26576 else if (flag_concepts)
26577 return cp_parser_template_introduction (parser, member_p);
26578
26579 return false;
26580 }
26581
26582 /* Perform the deferred access checks from a template-parameter-list.
26583 CHECKS is a TREE_LIST of access checks, as returned by
26584 get_deferred_access_checks. */
26585
26586 static void
26587 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26588 {
26589 ++processing_template_parmlist;
26590 perform_access_checks (checks, tf_warning_or_error);
26591 --processing_template_parmlist;
26592 }
26593
26594 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26595 `function-definition' sequence that follows a template header.
26596 If MEMBER_P is true, this declaration appears in a class scope.
26597
26598 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26599 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26600
26601 static tree
26602 cp_parser_single_declaration (cp_parser* parser,
26603 vec<deferred_access_check, va_gc> *checks,
26604 bool member_p,
26605 bool explicit_specialization_p,
26606 bool* friend_p)
26607 {
26608 int declares_class_or_enum;
26609 tree decl = NULL_TREE;
26610 cp_decl_specifier_seq decl_specifiers;
26611 bool function_definition_p = false;
26612 cp_token *decl_spec_token_start;
26613
26614 /* This function is only used when processing a template
26615 declaration. */
26616 gcc_assert (innermost_scope_kind () == sk_template_parms
26617 || innermost_scope_kind () == sk_template_spec);
26618
26619 /* Defer access checks until we know what is being declared. */
26620 push_deferring_access_checks (dk_deferred);
26621
26622 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26623 alternative. */
26624 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26625 cp_parser_decl_specifier_seq (parser,
26626 CP_PARSER_FLAGS_OPTIONAL,
26627 &decl_specifiers,
26628 &declares_class_or_enum);
26629 if (friend_p)
26630 *friend_p = cp_parser_friend_p (&decl_specifiers);
26631
26632 /* There are no template typedefs. */
26633 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26634 {
26635 error_at (decl_spec_token_start->location,
26636 "template declaration of %<typedef%>");
26637 decl = error_mark_node;
26638 }
26639
26640 /* Gather up the access checks that occurred the
26641 decl-specifier-seq. */
26642 stop_deferring_access_checks ();
26643
26644 /* Check for the declaration of a template class. */
26645 if (declares_class_or_enum)
26646 {
26647 if (cp_parser_declares_only_class_p (parser)
26648 || (declares_class_or_enum & 2))
26649 {
26650 // If this is a declaration, but not a definition, associate
26651 // any constraints with the type declaration. Constraints
26652 // are associated with definitions in cp_parser_class_specifier.
26653 if (declares_class_or_enum == 1)
26654 associate_classtype_constraints (decl_specifiers.type);
26655
26656 decl = shadow_tag (&decl_specifiers);
26657
26658 /* In this case:
26659
26660 struct C {
26661 friend template <typename T> struct A<T>::B;
26662 };
26663
26664 A<T>::B will be represented by a TYPENAME_TYPE, and
26665 therefore not recognized by shadow_tag. */
26666 if (friend_p && *friend_p
26667 && !decl
26668 && decl_specifiers.type
26669 && TYPE_P (decl_specifiers.type))
26670 decl = decl_specifiers.type;
26671
26672 if (decl && decl != error_mark_node)
26673 decl = TYPE_NAME (decl);
26674 else
26675 decl = error_mark_node;
26676
26677 /* Perform access checks for template parameters. */
26678 cp_parser_perform_template_parameter_access_checks (checks);
26679
26680 /* Give a helpful diagnostic for
26681 template <class T> struct A { } a;
26682 if we aren't already recovering from an error. */
26683 if (!cp_parser_declares_only_class_p (parser)
26684 && !seen_error ())
26685 {
26686 error_at (cp_lexer_peek_token (parser->lexer)->location,
26687 "a class template declaration must not declare "
26688 "anything else");
26689 cp_parser_skip_to_end_of_block_or_statement (parser);
26690 goto out;
26691 }
26692 }
26693 }
26694
26695 /* Complain about missing 'typename' or other invalid type names. */
26696 if (!decl_specifiers.any_type_specifiers_p
26697 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26698 {
26699 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26700 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26701 the rest of this declaration. */
26702 decl = error_mark_node;
26703 goto out;
26704 }
26705
26706 /* If it's not a template class, try for a template function. If
26707 the next token is a `;', then this declaration does not declare
26708 anything. But, if there were errors in the decl-specifiers, then
26709 the error might well have come from an attempted class-specifier.
26710 In that case, there's no need to warn about a missing declarator. */
26711 if (!decl
26712 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26713 || decl_specifiers.type != error_mark_node))
26714 {
26715 decl = cp_parser_init_declarator (parser,
26716 &decl_specifiers,
26717 checks,
26718 /*function_definition_allowed_p=*/true,
26719 member_p,
26720 declares_class_or_enum,
26721 &function_definition_p,
26722 NULL, NULL, NULL);
26723
26724 /* 7.1.1-1 [dcl.stc]
26725
26726 A storage-class-specifier shall not be specified in an explicit
26727 specialization... */
26728 if (decl
26729 && explicit_specialization_p
26730 && decl_specifiers.storage_class != sc_none)
26731 {
26732 error_at (decl_spec_token_start->location,
26733 "explicit template specialization cannot have a storage class");
26734 decl = error_mark_node;
26735 }
26736
26737 if (decl && VAR_P (decl))
26738 check_template_variable (decl);
26739 }
26740
26741 /* Look for a trailing `;' after the declaration. */
26742 if (!function_definition_p
26743 && (decl == error_mark_node
26744 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26745 cp_parser_skip_to_end_of_block_or_statement (parser);
26746
26747 out:
26748 pop_deferring_access_checks ();
26749
26750 /* Clear any current qualification; whatever comes next is the start
26751 of something new. */
26752 parser->scope = NULL_TREE;
26753 parser->qualifying_scope = NULL_TREE;
26754 parser->object_scope = NULL_TREE;
26755
26756 return decl;
26757 }
26758
26759 /* Parse a cast-expression that is not the operand of a unary "&". */
26760
26761 static cp_expr
26762 cp_parser_simple_cast_expression (cp_parser *parser)
26763 {
26764 return cp_parser_cast_expression (parser, /*address_p=*/false,
26765 /*cast_p=*/false, /*decltype*/false, NULL);
26766 }
26767
26768 /* Parse a functional cast to TYPE. Returns an expression
26769 representing the cast. */
26770
26771 static cp_expr
26772 cp_parser_functional_cast (cp_parser* parser, tree type)
26773 {
26774 vec<tree, va_gc> *vec;
26775 tree expression_list;
26776 cp_expr cast;
26777 bool nonconst_p;
26778
26779 location_t start_loc = input_location;
26780
26781 if (!type)
26782 type = error_mark_node;
26783
26784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26785 {
26786 cp_lexer_set_source_position (parser->lexer);
26787 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26788 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26789 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26790 if (TREE_CODE (type) == TYPE_DECL)
26791 type = TREE_TYPE (type);
26792
26793 cast = finish_compound_literal (type, expression_list,
26794 tf_warning_or_error);
26795 /* Create a location of the form:
26796 type_name{i, f}
26797 ^~~~~~~~~~~~~~~
26798 with caret == start at the start of the type name,
26799 finishing at the closing brace. */
26800 location_t finish_loc
26801 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26802 location_t combined_loc = make_location (start_loc, start_loc,
26803 finish_loc);
26804 cast.set_location (combined_loc);
26805 return cast;
26806 }
26807
26808
26809 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26810 /*cast_p=*/true,
26811 /*allow_expansion_p=*/true,
26812 /*non_constant_p=*/NULL);
26813 if (vec == NULL)
26814 expression_list = error_mark_node;
26815 else
26816 {
26817 expression_list = build_tree_list_vec (vec);
26818 release_tree_vector (vec);
26819 }
26820
26821 cast = build_functional_cast (type, expression_list,
26822 tf_warning_or_error);
26823 /* [expr.const]/1: In an integral constant expression "only type
26824 conversions to integral or enumeration type can be used". */
26825 if (TREE_CODE (type) == TYPE_DECL)
26826 type = TREE_TYPE (type);
26827 if (cast != error_mark_node
26828 && !cast_valid_in_integral_constant_expression_p (type)
26829 && cp_parser_non_integral_constant_expression (parser,
26830 NIC_CONSTRUCTOR))
26831 return error_mark_node;
26832
26833 /* Create a location of the form:
26834 float(i)
26835 ^~~~~~~~
26836 with caret == start at the start of the type name,
26837 finishing at the closing paren. */
26838 location_t finish_loc
26839 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26840 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26841 cast.set_location (combined_loc);
26842 return cast;
26843 }
26844
26845 /* Save the tokens that make up the body of a member function defined
26846 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26847 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26848 specifiers applied to the declaration. Returns the FUNCTION_DECL
26849 for the member function. */
26850
26851 static tree
26852 cp_parser_save_member_function_body (cp_parser* parser,
26853 cp_decl_specifier_seq *decl_specifiers,
26854 cp_declarator *declarator,
26855 tree attributes)
26856 {
26857 cp_token *first;
26858 cp_token *last;
26859 tree fn;
26860 bool function_try_block = false;
26861
26862 /* Create the FUNCTION_DECL. */
26863 fn = grokmethod (decl_specifiers, declarator, attributes);
26864 cp_finalize_omp_declare_simd (parser, fn);
26865 cp_finalize_oacc_routine (parser, fn, true);
26866 /* If something went badly wrong, bail out now. */
26867 if (fn == error_mark_node)
26868 {
26869 /* If there's a function-body, skip it. */
26870 if (cp_parser_token_starts_function_definition_p
26871 (cp_lexer_peek_token (parser->lexer)))
26872 cp_parser_skip_to_end_of_block_or_statement (parser);
26873 return error_mark_node;
26874 }
26875
26876 /* Remember it, if there default args to post process. */
26877 cp_parser_save_default_args (parser, fn);
26878
26879 /* Save away the tokens that make up the body of the
26880 function. */
26881 first = parser->lexer->next_token;
26882
26883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26884 cp_lexer_consume_token (parser->lexer);
26885 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26886 RID_TRANSACTION_ATOMIC))
26887 {
26888 cp_lexer_consume_token (parser->lexer);
26889 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26891 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26892 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26893 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26894 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26895 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26896 {
26897 cp_lexer_consume_token (parser->lexer);
26898 cp_lexer_consume_token (parser->lexer);
26899 cp_lexer_consume_token (parser->lexer);
26900 cp_lexer_consume_token (parser->lexer);
26901 cp_lexer_consume_token (parser->lexer);
26902 }
26903 else
26904 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26905 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26906 {
26907 cp_lexer_consume_token (parser->lexer);
26908 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26909 break;
26910 }
26911 }
26912
26913 /* Handle function try blocks. */
26914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26915 {
26916 cp_lexer_consume_token (parser->lexer);
26917 function_try_block = true;
26918 }
26919 /* We can have braced-init-list mem-initializers before the fn body. */
26920 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26921 {
26922 cp_lexer_consume_token (parser->lexer);
26923 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26924 {
26925 /* cache_group will stop after an un-nested { } pair, too. */
26926 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26927 break;
26928
26929 /* variadic mem-inits have ... after the ')'. */
26930 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26931 cp_lexer_consume_token (parser->lexer);
26932 }
26933 }
26934 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26935 /* Handle function try blocks. */
26936 if (function_try_block)
26937 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26938 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26939 last = parser->lexer->next_token;
26940
26941 /* Save away the inline definition; we will process it when the
26942 class is complete. */
26943 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26944 DECL_PENDING_INLINE_P (fn) = 1;
26945
26946 /* We need to know that this was defined in the class, so that
26947 friend templates are handled correctly. */
26948 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26949
26950 /* Add FN to the queue of functions to be parsed later. */
26951 vec_safe_push (unparsed_funs_with_definitions, fn);
26952
26953 return fn;
26954 }
26955
26956 /* Save the tokens that make up the in-class initializer for a non-static
26957 data member. Returns a DEFAULT_ARG. */
26958
26959 static tree
26960 cp_parser_save_nsdmi (cp_parser* parser)
26961 {
26962 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26963 }
26964
26965 /* Parse a template-argument-list, as well as the trailing ">" (but
26966 not the opening "<"). See cp_parser_template_argument_list for the
26967 return value. */
26968
26969 static tree
26970 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26971 {
26972 tree arguments;
26973 tree saved_scope;
26974 tree saved_qualifying_scope;
26975 tree saved_object_scope;
26976 bool saved_greater_than_is_operator_p;
26977 int saved_unevaluated_operand;
26978 int saved_inhibit_evaluation_warnings;
26979
26980 /* [temp.names]
26981
26982 When parsing a template-id, the first non-nested `>' is taken as
26983 the end of the template-argument-list rather than a greater-than
26984 operator. */
26985 saved_greater_than_is_operator_p
26986 = parser->greater_than_is_operator_p;
26987 parser->greater_than_is_operator_p = false;
26988 /* Parsing the argument list may modify SCOPE, so we save it
26989 here. */
26990 saved_scope = parser->scope;
26991 saved_qualifying_scope = parser->qualifying_scope;
26992 saved_object_scope = parser->object_scope;
26993 /* We need to evaluate the template arguments, even though this
26994 template-id may be nested within a "sizeof". */
26995 saved_unevaluated_operand = cp_unevaluated_operand;
26996 cp_unevaluated_operand = 0;
26997 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26998 c_inhibit_evaluation_warnings = 0;
26999 /* Parse the template-argument-list itself. */
27000 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27001 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27002 arguments = NULL_TREE;
27003 else
27004 arguments = cp_parser_template_argument_list (parser);
27005 /* Look for the `>' that ends the template-argument-list. If we find
27006 a '>>' instead, it's probably just a typo. */
27007 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27008 {
27009 if (cxx_dialect != cxx98)
27010 {
27011 /* In C++0x, a `>>' in a template argument list or cast
27012 expression is considered to be two separate `>'
27013 tokens. So, change the current token to a `>', but don't
27014 consume it: it will be consumed later when the outer
27015 template argument list (or cast expression) is parsed.
27016 Note that this replacement of `>' for `>>' is necessary
27017 even if we are parsing tentatively: in the tentative
27018 case, after calling
27019 cp_parser_enclosed_template_argument_list we will always
27020 throw away all of the template arguments and the first
27021 closing `>', either because the template argument list
27022 was erroneous or because we are replacing those tokens
27023 with a CPP_TEMPLATE_ID token. The second `>' (which will
27024 not have been thrown away) is needed either to close an
27025 outer template argument list or to complete a new-style
27026 cast. */
27027 cp_token *token = cp_lexer_peek_token (parser->lexer);
27028 token->type = CPP_GREATER;
27029 }
27030 else if (!saved_greater_than_is_operator_p)
27031 {
27032 /* If we're in a nested template argument list, the '>>' has
27033 to be a typo for '> >'. We emit the error message, but we
27034 continue parsing and we push a '>' as next token, so that
27035 the argument list will be parsed correctly. Note that the
27036 global source location is still on the token before the
27037 '>>', so we need to say explicitly where we want it. */
27038 cp_token *token = cp_lexer_peek_token (parser->lexer);
27039 gcc_rich_location richloc (token->location);
27040 richloc.add_fixit_replace ("> >");
27041 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27042 "within a nested template argument list");
27043
27044 token->type = CPP_GREATER;
27045 }
27046 else
27047 {
27048 /* If this is not a nested template argument list, the '>>'
27049 is a typo for '>'. Emit an error message and continue.
27050 Same deal about the token location, but here we can get it
27051 right by consuming the '>>' before issuing the diagnostic. */
27052 cp_token *token = cp_lexer_consume_token (parser->lexer);
27053 error_at (token->location,
27054 "spurious %<>>%>, use %<>%> to terminate "
27055 "a template argument list");
27056 }
27057 }
27058 else
27059 cp_parser_skip_to_end_of_template_parameter_list (parser);
27060 /* The `>' token might be a greater-than operator again now. */
27061 parser->greater_than_is_operator_p
27062 = saved_greater_than_is_operator_p;
27063 /* Restore the SAVED_SCOPE. */
27064 parser->scope = saved_scope;
27065 parser->qualifying_scope = saved_qualifying_scope;
27066 parser->object_scope = saved_object_scope;
27067 cp_unevaluated_operand = saved_unevaluated_operand;
27068 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27069
27070 return arguments;
27071 }
27072
27073 /* MEMBER_FUNCTION is a member function, or a friend. If default
27074 arguments, or the body of the function have not yet been parsed,
27075 parse them now. */
27076
27077 static void
27078 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27079 {
27080 timevar_push (TV_PARSE_INMETH);
27081 /* If this member is a template, get the underlying
27082 FUNCTION_DECL. */
27083 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27084 member_function = DECL_TEMPLATE_RESULT (member_function);
27085
27086 /* There should not be any class definitions in progress at this
27087 point; the bodies of members are only parsed outside of all class
27088 definitions. */
27089 gcc_assert (parser->num_classes_being_defined == 0);
27090 /* While we're parsing the member functions we might encounter more
27091 classes. We want to handle them right away, but we don't want
27092 them getting mixed up with functions that are currently in the
27093 queue. */
27094 push_unparsed_function_queues (parser);
27095
27096 /* Make sure that any template parameters are in scope. */
27097 maybe_begin_member_template_processing (member_function);
27098
27099 /* If the body of the function has not yet been parsed, parse it
27100 now. */
27101 if (DECL_PENDING_INLINE_P (member_function))
27102 {
27103 tree function_scope;
27104 cp_token_cache *tokens;
27105
27106 /* The function is no longer pending; we are processing it. */
27107 tokens = DECL_PENDING_INLINE_INFO (member_function);
27108 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27109 DECL_PENDING_INLINE_P (member_function) = 0;
27110
27111 /* If this is a local class, enter the scope of the containing
27112 function. */
27113 function_scope = current_function_decl;
27114 if (function_scope)
27115 push_function_context ();
27116
27117 /* Push the body of the function onto the lexer stack. */
27118 cp_parser_push_lexer_for_tokens (parser, tokens);
27119
27120 /* Let the front end know that we going to be defining this
27121 function. */
27122 start_preparsed_function (member_function, NULL_TREE,
27123 SF_PRE_PARSED | SF_INCLASS_INLINE);
27124
27125 /* Don't do access checking if it is a templated function. */
27126 if (processing_template_decl)
27127 push_deferring_access_checks (dk_no_check);
27128
27129 /* #pragma omp declare reduction needs special parsing. */
27130 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27131 {
27132 parser->lexer->in_pragma = true;
27133 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27134 finish_function (/*inline*/2);
27135 cp_check_omp_declare_reduction (member_function);
27136 }
27137 else
27138 /* Now, parse the body of the function. */
27139 cp_parser_function_definition_after_declarator (parser,
27140 /*inline_p=*/true);
27141
27142 if (processing_template_decl)
27143 pop_deferring_access_checks ();
27144
27145 /* Leave the scope of the containing function. */
27146 if (function_scope)
27147 pop_function_context ();
27148 cp_parser_pop_lexer (parser);
27149 }
27150
27151 /* Remove any template parameters from the symbol table. */
27152 maybe_end_member_template_processing ();
27153
27154 /* Restore the queue. */
27155 pop_unparsed_function_queues (parser);
27156 timevar_pop (TV_PARSE_INMETH);
27157 }
27158
27159 /* If DECL contains any default args, remember it on the unparsed
27160 functions queue. */
27161
27162 static void
27163 cp_parser_save_default_args (cp_parser* parser, tree decl)
27164 {
27165 tree probe;
27166
27167 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27168 probe;
27169 probe = TREE_CHAIN (probe))
27170 if (TREE_PURPOSE (probe))
27171 {
27172 cp_default_arg_entry entry = {current_class_type, decl};
27173 vec_safe_push (unparsed_funs_with_default_args, entry);
27174 break;
27175 }
27176 }
27177
27178 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27179 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27180 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27181 from the parameter-type-list. */
27182
27183 static tree
27184 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27185 tree default_arg, tree parmtype)
27186 {
27187 cp_token_cache *tokens;
27188 tree parsed_arg;
27189 bool dummy;
27190
27191 if (default_arg == error_mark_node)
27192 return error_mark_node;
27193
27194 /* Push the saved tokens for the default argument onto the parser's
27195 lexer stack. */
27196 tokens = DEFARG_TOKENS (default_arg);
27197 cp_parser_push_lexer_for_tokens (parser, tokens);
27198
27199 start_lambda_scope (decl);
27200
27201 /* Parse the default argument. */
27202 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27203 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27205
27206 finish_lambda_scope ();
27207
27208 if (parsed_arg == error_mark_node)
27209 cp_parser_skip_to_end_of_statement (parser);
27210
27211 if (!processing_template_decl)
27212 {
27213 /* In a non-template class, check conversions now. In a template,
27214 we'll wait and instantiate these as needed. */
27215 if (TREE_CODE (decl) == PARM_DECL)
27216 parsed_arg = check_default_argument (parmtype, parsed_arg,
27217 tf_warning_or_error);
27218 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27219 parsed_arg = error_mark_node;
27220 else
27221 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27222 }
27223
27224 /* If the token stream has not been completely used up, then
27225 there was extra junk after the end of the default
27226 argument. */
27227 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27228 {
27229 if (TREE_CODE (decl) == PARM_DECL)
27230 cp_parser_error (parser, "expected %<,%>");
27231 else
27232 cp_parser_error (parser, "expected %<;%>");
27233 }
27234
27235 /* Revert to the main lexer. */
27236 cp_parser_pop_lexer (parser);
27237
27238 return parsed_arg;
27239 }
27240
27241 /* FIELD is a non-static data member with an initializer which we saved for
27242 later; parse it now. */
27243
27244 static void
27245 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27246 {
27247 tree def;
27248
27249 maybe_begin_member_template_processing (field);
27250
27251 push_unparsed_function_queues (parser);
27252 def = cp_parser_late_parse_one_default_arg (parser, field,
27253 DECL_INITIAL (field),
27254 NULL_TREE);
27255 pop_unparsed_function_queues (parser);
27256
27257 maybe_end_member_template_processing ();
27258
27259 DECL_INITIAL (field) = def;
27260 }
27261
27262 /* FN is a FUNCTION_DECL which may contains a parameter with an
27263 unparsed DEFAULT_ARG. Parse the default args now. This function
27264 assumes that the current scope is the scope in which the default
27265 argument should be processed. */
27266
27267 static void
27268 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27269 {
27270 bool saved_local_variables_forbidden_p;
27271 tree parm, parmdecl;
27272
27273 /* While we're parsing the default args, we might (due to the
27274 statement expression extension) encounter more classes. We want
27275 to handle them right away, but we don't want them getting mixed
27276 up with default args that are currently in the queue. */
27277 push_unparsed_function_queues (parser);
27278
27279 /* Local variable names (and the `this' keyword) may not appear
27280 in a default argument. */
27281 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27282 parser->local_variables_forbidden_p = true;
27283
27284 push_defarg_context (fn);
27285
27286 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27287 parmdecl = DECL_ARGUMENTS (fn);
27288 parm && parm != void_list_node;
27289 parm = TREE_CHAIN (parm),
27290 parmdecl = DECL_CHAIN (parmdecl))
27291 {
27292 tree default_arg = TREE_PURPOSE (parm);
27293 tree parsed_arg;
27294 vec<tree, va_gc> *insts;
27295 tree copy;
27296 unsigned ix;
27297
27298 if (!default_arg)
27299 continue;
27300
27301 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27302 /* This can happen for a friend declaration for a function
27303 already declared with default arguments. */
27304 continue;
27305
27306 parsed_arg
27307 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27308 default_arg,
27309 TREE_VALUE (parm));
27310 if (parsed_arg == error_mark_node)
27311 {
27312 continue;
27313 }
27314
27315 TREE_PURPOSE (parm) = parsed_arg;
27316
27317 /* Update any instantiations we've already created. */
27318 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27319 vec_safe_iterate (insts, ix, &copy); ix++)
27320 TREE_PURPOSE (copy) = parsed_arg;
27321 }
27322
27323 pop_defarg_context ();
27324
27325 /* Make sure no default arg is missing. */
27326 check_default_args (fn);
27327
27328 /* Restore the state of local_variables_forbidden_p. */
27329 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27330
27331 /* Restore the queue. */
27332 pop_unparsed_function_queues (parser);
27333 }
27334
27335 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27336
27337 sizeof ... ( identifier )
27338
27339 where the 'sizeof' token has already been consumed. */
27340
27341 static tree
27342 cp_parser_sizeof_pack (cp_parser *parser)
27343 {
27344 /* Consume the `...'. */
27345 cp_lexer_consume_token (parser->lexer);
27346 maybe_warn_variadic_templates ();
27347
27348 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27349 if (paren)
27350 cp_lexer_consume_token (parser->lexer);
27351 else
27352 permerror (cp_lexer_peek_token (parser->lexer)->location,
27353 "%<sizeof...%> argument must be surrounded by parentheses");
27354
27355 cp_token *token = cp_lexer_peek_token (parser->lexer);
27356 tree name = cp_parser_identifier (parser);
27357 if (name == error_mark_node)
27358 return error_mark_node;
27359 /* The name is not qualified. */
27360 parser->scope = NULL_TREE;
27361 parser->qualifying_scope = NULL_TREE;
27362 parser->object_scope = NULL_TREE;
27363 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27364 if (expr == error_mark_node)
27365 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27366 token->location);
27367 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27368 expr = TREE_TYPE (expr);
27369 else if (TREE_CODE (expr) == CONST_DECL)
27370 expr = DECL_INITIAL (expr);
27371 expr = make_pack_expansion (expr);
27372 PACK_EXPANSION_SIZEOF_P (expr) = true;
27373
27374 if (paren)
27375 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27376
27377 return expr;
27378 }
27379
27380 /* Parse the operand of `sizeof' (or a similar operator). Returns
27381 either a TYPE or an expression, depending on the form of the
27382 input. The KEYWORD indicates which kind of expression we have
27383 encountered. */
27384
27385 static tree
27386 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27387 {
27388 tree expr = NULL_TREE;
27389 const char *saved_message;
27390 char *tmp;
27391 bool saved_integral_constant_expression_p;
27392 bool saved_non_integral_constant_expression_p;
27393
27394 /* If it's a `...', then we are computing the length of a parameter
27395 pack. */
27396 if (keyword == RID_SIZEOF
27397 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27398 return cp_parser_sizeof_pack (parser);
27399
27400 /* Types cannot be defined in a `sizeof' expression. Save away the
27401 old message. */
27402 saved_message = parser->type_definition_forbidden_message;
27403 /* And create the new one. */
27404 tmp = concat ("types may not be defined in %<",
27405 IDENTIFIER_POINTER (ridpointers[keyword]),
27406 "%> expressions", NULL);
27407 parser->type_definition_forbidden_message = tmp;
27408
27409 /* The restrictions on constant-expressions do not apply inside
27410 sizeof expressions. */
27411 saved_integral_constant_expression_p
27412 = parser->integral_constant_expression_p;
27413 saved_non_integral_constant_expression_p
27414 = parser->non_integral_constant_expression_p;
27415 parser->integral_constant_expression_p = false;
27416
27417 /* Do not actually evaluate the expression. */
27418 ++cp_unevaluated_operand;
27419 ++c_inhibit_evaluation_warnings;
27420 /* If it's a `(', then we might be looking at the type-id
27421 construction. */
27422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27423 {
27424 tree type = NULL_TREE;
27425
27426 /* We can't be sure yet whether we're looking at a type-id or an
27427 expression. */
27428 cp_parser_parse_tentatively (parser);
27429 /* Note: as a GNU Extension, compound literals are considered
27430 postfix-expressions as they are in C99, so they are valid
27431 arguments to sizeof. See comment in cp_parser_cast_expression
27432 for details. */
27433 if (cp_parser_compound_literal_p (parser))
27434 cp_parser_simulate_error (parser);
27435 else
27436 {
27437 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27438 parser->in_type_id_in_expr_p = true;
27439 /* Look for the type-id. */
27440 type = cp_parser_type_id (parser);
27441 /* Look for the closing `)'. */
27442 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27443 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27444 }
27445
27446 /* If all went well, then we're done. */
27447 if (cp_parser_parse_definitely (parser))
27448 {
27449 cp_decl_specifier_seq decl_specs;
27450
27451 /* Build a trivial decl-specifier-seq. */
27452 clear_decl_specs (&decl_specs);
27453 decl_specs.type = type;
27454
27455 /* Call grokdeclarator to figure out what type this is. */
27456 expr = grokdeclarator (NULL,
27457 &decl_specs,
27458 TYPENAME,
27459 /*initialized=*/0,
27460 /*attrlist=*/NULL);
27461 }
27462 }
27463
27464 /* If the type-id production did not work out, then we must be
27465 looking at the unary-expression production. */
27466 if (!expr)
27467 expr = cp_parser_unary_expression (parser);
27468
27469 /* Go back to evaluating expressions. */
27470 --cp_unevaluated_operand;
27471 --c_inhibit_evaluation_warnings;
27472
27473 /* Free the message we created. */
27474 free (tmp);
27475 /* And restore the old one. */
27476 parser->type_definition_forbidden_message = saved_message;
27477 parser->integral_constant_expression_p
27478 = saved_integral_constant_expression_p;
27479 parser->non_integral_constant_expression_p
27480 = saved_non_integral_constant_expression_p;
27481
27482 return expr;
27483 }
27484
27485 /* If the current declaration has no declarator, return true. */
27486
27487 static bool
27488 cp_parser_declares_only_class_p (cp_parser *parser)
27489 {
27490 /* If the next token is a `;' or a `,' then there is no
27491 declarator. */
27492 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27493 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27494 }
27495
27496 /* Update the DECL_SPECS to reflect the storage class indicated by
27497 KEYWORD. */
27498
27499 static void
27500 cp_parser_set_storage_class (cp_parser *parser,
27501 cp_decl_specifier_seq *decl_specs,
27502 enum rid keyword,
27503 cp_token *token)
27504 {
27505 cp_storage_class storage_class;
27506
27507 if (parser->in_unbraced_linkage_specification_p)
27508 {
27509 error_at (token->location, "invalid use of %qD in linkage specification",
27510 ridpointers[keyword]);
27511 return;
27512 }
27513 else if (decl_specs->storage_class != sc_none)
27514 {
27515 decl_specs->conflicting_specifiers_p = true;
27516 return;
27517 }
27518
27519 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27520 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27521 && decl_specs->gnu_thread_keyword_p)
27522 {
27523 pedwarn (decl_specs->locations[ds_thread], 0,
27524 "%<__thread%> before %qD", ridpointers[keyword]);
27525 }
27526
27527 switch (keyword)
27528 {
27529 case RID_AUTO:
27530 storage_class = sc_auto;
27531 break;
27532 case RID_REGISTER:
27533 storage_class = sc_register;
27534 break;
27535 case RID_STATIC:
27536 storage_class = sc_static;
27537 break;
27538 case RID_EXTERN:
27539 storage_class = sc_extern;
27540 break;
27541 case RID_MUTABLE:
27542 storage_class = sc_mutable;
27543 break;
27544 default:
27545 gcc_unreachable ();
27546 }
27547 decl_specs->storage_class = storage_class;
27548 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27549
27550 /* A storage class specifier cannot be applied alongside a typedef
27551 specifier. If there is a typedef specifier present then set
27552 conflicting_specifiers_p which will trigger an error later
27553 on in grokdeclarator. */
27554 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27555 decl_specs->conflicting_specifiers_p = true;
27556 }
27557
27558 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27559 is true, the type is a class or enum definition. */
27560
27561 static void
27562 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27563 tree type_spec,
27564 cp_token *token,
27565 bool type_definition_p)
27566 {
27567 decl_specs->any_specifiers_p = true;
27568
27569 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27570 (with, for example, in "typedef int wchar_t;") we remember that
27571 this is what happened. In system headers, we ignore these
27572 declarations so that G++ can work with system headers that are not
27573 C++-safe. */
27574 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27575 && !type_definition_p
27576 && (type_spec == boolean_type_node
27577 || type_spec == char16_type_node
27578 || type_spec == char32_type_node
27579 || type_spec == wchar_type_node)
27580 && (decl_specs->type
27581 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27582 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27583 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27584 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27585 {
27586 decl_specs->redefined_builtin_type = type_spec;
27587 set_and_check_decl_spec_loc (decl_specs,
27588 ds_redefined_builtin_type_spec,
27589 token);
27590 if (!decl_specs->type)
27591 {
27592 decl_specs->type = type_spec;
27593 decl_specs->type_definition_p = false;
27594 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27595 }
27596 }
27597 else if (decl_specs->type)
27598 decl_specs->multiple_types_p = true;
27599 else
27600 {
27601 decl_specs->type = type_spec;
27602 decl_specs->type_definition_p = type_definition_p;
27603 decl_specs->redefined_builtin_type = NULL_TREE;
27604 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27605 }
27606 }
27607
27608 /* True iff TOKEN is the GNU keyword __thread. */
27609
27610 static bool
27611 token_is__thread (cp_token *token)
27612 {
27613 gcc_assert (token->keyword == RID_THREAD);
27614 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27615 }
27616
27617 /* Set the location for a declarator specifier and check if it is
27618 duplicated.
27619
27620 DECL_SPECS is the sequence of declarator specifiers onto which to
27621 set the location.
27622
27623 DS is the single declarator specifier to set which location is to
27624 be set onto the existing sequence of declarators.
27625
27626 LOCATION is the location for the declarator specifier to
27627 consider. */
27628
27629 static void
27630 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27631 cp_decl_spec ds, cp_token *token)
27632 {
27633 gcc_assert (ds < ds_last);
27634
27635 if (decl_specs == NULL)
27636 return;
27637
27638 source_location location = token->location;
27639
27640 if (decl_specs->locations[ds] == 0)
27641 {
27642 decl_specs->locations[ds] = location;
27643 if (ds == ds_thread)
27644 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27645 }
27646 else
27647 {
27648 if (ds == ds_long)
27649 {
27650 if (decl_specs->locations[ds_long_long] != 0)
27651 error_at (location,
27652 "%<long long long%> is too long for GCC");
27653 else
27654 {
27655 decl_specs->locations[ds_long_long] = location;
27656 pedwarn_cxx98 (location,
27657 OPT_Wlong_long,
27658 "ISO C++ 1998 does not support %<long long%>");
27659 }
27660 }
27661 else if (ds == ds_thread)
27662 {
27663 bool gnu = token_is__thread (token);
27664 if (gnu != decl_specs->gnu_thread_keyword_p)
27665 error_at (location,
27666 "both %<__thread%> and %<thread_local%> specified");
27667 else
27668 error_at (location, "duplicate %qD", token->u.value);
27669 }
27670 else
27671 {
27672 static const char *const decl_spec_names[] = {
27673 "signed",
27674 "unsigned",
27675 "short",
27676 "long",
27677 "const",
27678 "volatile",
27679 "restrict",
27680 "inline",
27681 "virtual",
27682 "explicit",
27683 "friend",
27684 "typedef",
27685 "using",
27686 "constexpr",
27687 "__complex"
27688 };
27689 error_at (location,
27690 "duplicate %qs", decl_spec_names[ds]);
27691 }
27692 }
27693 }
27694
27695 /* Return true iff the declarator specifier DS is present in the
27696 sequence of declarator specifiers DECL_SPECS. */
27697
27698 bool
27699 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27700 cp_decl_spec ds)
27701 {
27702 gcc_assert (ds < ds_last);
27703
27704 if (decl_specs == NULL)
27705 return false;
27706
27707 return decl_specs->locations[ds] != 0;
27708 }
27709
27710 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27711 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27712
27713 static bool
27714 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27715 {
27716 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27717 }
27718
27719 /* Issue an error message indicating that TOKEN_DESC was expected.
27720 If KEYWORD is true, it indicated this function is called by
27721 cp_parser_require_keword and the required token can only be
27722 a indicated keyword. */
27723
27724 static void
27725 cp_parser_required_error (cp_parser *parser,
27726 required_token token_desc,
27727 bool keyword)
27728 {
27729 switch (token_desc)
27730 {
27731 case RT_NEW:
27732 cp_parser_error (parser, "expected %<new%>");
27733 return;
27734 case RT_DELETE:
27735 cp_parser_error (parser, "expected %<delete%>");
27736 return;
27737 case RT_RETURN:
27738 cp_parser_error (parser, "expected %<return%>");
27739 return;
27740 case RT_WHILE:
27741 cp_parser_error (parser, "expected %<while%>");
27742 return;
27743 case RT_EXTERN:
27744 cp_parser_error (parser, "expected %<extern%>");
27745 return;
27746 case RT_STATIC_ASSERT:
27747 cp_parser_error (parser, "expected %<static_assert%>");
27748 return;
27749 case RT_DECLTYPE:
27750 cp_parser_error (parser, "expected %<decltype%>");
27751 return;
27752 case RT_OPERATOR:
27753 cp_parser_error (parser, "expected %<operator%>");
27754 return;
27755 case RT_CLASS:
27756 cp_parser_error (parser, "expected %<class%>");
27757 return;
27758 case RT_TEMPLATE:
27759 cp_parser_error (parser, "expected %<template%>");
27760 return;
27761 case RT_NAMESPACE:
27762 cp_parser_error (parser, "expected %<namespace%>");
27763 return;
27764 case RT_USING:
27765 cp_parser_error (parser, "expected %<using%>");
27766 return;
27767 case RT_ASM:
27768 cp_parser_error (parser, "expected %<asm%>");
27769 return;
27770 case RT_TRY:
27771 cp_parser_error (parser, "expected %<try%>");
27772 return;
27773 case RT_CATCH:
27774 cp_parser_error (parser, "expected %<catch%>");
27775 return;
27776 case RT_THROW:
27777 cp_parser_error (parser, "expected %<throw%>");
27778 return;
27779 case RT_LABEL:
27780 cp_parser_error (parser, "expected %<__label__%>");
27781 return;
27782 case RT_AT_TRY:
27783 cp_parser_error (parser, "expected %<@try%>");
27784 return;
27785 case RT_AT_SYNCHRONIZED:
27786 cp_parser_error (parser, "expected %<@synchronized%>");
27787 return;
27788 case RT_AT_THROW:
27789 cp_parser_error (parser, "expected %<@throw%>");
27790 return;
27791 case RT_TRANSACTION_ATOMIC:
27792 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27793 return;
27794 case RT_TRANSACTION_RELAXED:
27795 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27796 return;
27797 default:
27798 break;
27799 }
27800 if (!keyword)
27801 {
27802 switch (token_desc)
27803 {
27804 case RT_SEMICOLON:
27805 cp_parser_error (parser, "expected %<;%>");
27806 return;
27807 case RT_OPEN_PAREN:
27808 cp_parser_error (parser, "expected %<(%>");
27809 return;
27810 case RT_CLOSE_BRACE:
27811 cp_parser_error (parser, "expected %<}%>");
27812 return;
27813 case RT_OPEN_BRACE:
27814 cp_parser_error (parser, "expected %<{%>");
27815 return;
27816 case RT_CLOSE_SQUARE:
27817 cp_parser_error (parser, "expected %<]%>");
27818 return;
27819 case RT_OPEN_SQUARE:
27820 cp_parser_error (parser, "expected %<[%>");
27821 return;
27822 case RT_COMMA:
27823 cp_parser_error (parser, "expected %<,%>");
27824 return;
27825 case RT_SCOPE:
27826 cp_parser_error (parser, "expected %<::%>");
27827 return;
27828 case RT_LESS:
27829 cp_parser_error (parser, "expected %<<%>");
27830 return;
27831 case RT_GREATER:
27832 cp_parser_error (parser, "expected %<>%>");
27833 return;
27834 case RT_EQ:
27835 cp_parser_error (parser, "expected %<=%>");
27836 return;
27837 case RT_ELLIPSIS:
27838 cp_parser_error (parser, "expected %<...%>");
27839 return;
27840 case RT_MULT:
27841 cp_parser_error (parser, "expected %<*%>");
27842 return;
27843 case RT_COMPL:
27844 cp_parser_error (parser, "expected %<~%>");
27845 return;
27846 case RT_COLON:
27847 cp_parser_error (parser, "expected %<:%>");
27848 return;
27849 case RT_COLON_SCOPE:
27850 cp_parser_error (parser, "expected %<:%> or %<::%>");
27851 return;
27852 case RT_CLOSE_PAREN:
27853 cp_parser_error (parser, "expected %<)%>");
27854 return;
27855 case RT_COMMA_CLOSE_PAREN:
27856 cp_parser_error (parser, "expected %<,%> or %<)%>");
27857 return;
27858 case RT_PRAGMA_EOL:
27859 cp_parser_error (parser, "expected end of line");
27860 return;
27861 case RT_NAME:
27862 cp_parser_error (parser, "expected identifier");
27863 return;
27864 case RT_SELECT:
27865 cp_parser_error (parser, "expected selection-statement");
27866 return;
27867 case RT_INTERATION:
27868 cp_parser_error (parser, "expected iteration-statement");
27869 return;
27870 case RT_JUMP:
27871 cp_parser_error (parser, "expected jump-statement");
27872 return;
27873 case RT_CLASS_KEY:
27874 cp_parser_error (parser, "expected class-key");
27875 return;
27876 case RT_CLASS_TYPENAME_TEMPLATE:
27877 cp_parser_error (parser,
27878 "expected %<class%>, %<typename%>, or %<template%>");
27879 return;
27880 default:
27881 gcc_unreachable ();
27882 }
27883 }
27884 else
27885 gcc_unreachable ();
27886 }
27887
27888
27889
27890 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27891 issue an error message indicating that TOKEN_DESC was expected.
27892
27893 Returns the token consumed, if the token had the appropriate type.
27894 Otherwise, returns NULL. */
27895
27896 static cp_token *
27897 cp_parser_require (cp_parser* parser,
27898 enum cpp_ttype type,
27899 required_token token_desc)
27900 {
27901 if (cp_lexer_next_token_is (parser->lexer, type))
27902 return cp_lexer_consume_token (parser->lexer);
27903 else
27904 {
27905 /* Output the MESSAGE -- unless we're parsing tentatively. */
27906 if (!cp_parser_simulate_error (parser))
27907 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27908 return NULL;
27909 }
27910 }
27911
27912 /* An error message is produced if the next token is not '>'.
27913 All further tokens are skipped until the desired token is
27914 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27915
27916 static void
27917 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27918 {
27919 /* Current level of '< ... >'. */
27920 unsigned level = 0;
27921 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27922 unsigned nesting_depth = 0;
27923
27924 /* Are we ready, yet? If not, issue error message. */
27925 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27926 return;
27927
27928 /* Skip tokens until the desired token is found. */
27929 while (true)
27930 {
27931 /* Peek at the next token. */
27932 switch (cp_lexer_peek_token (parser->lexer)->type)
27933 {
27934 case CPP_LESS:
27935 if (!nesting_depth)
27936 ++level;
27937 break;
27938
27939 case CPP_RSHIFT:
27940 if (cxx_dialect == cxx98)
27941 /* C++0x views the `>>' operator as two `>' tokens, but
27942 C++98 does not. */
27943 break;
27944 else if (!nesting_depth && level-- == 0)
27945 {
27946 /* We've hit a `>>' where the first `>' closes the
27947 template argument list, and the second `>' is
27948 spurious. Just consume the `>>' and stop; we've
27949 already produced at least one error. */
27950 cp_lexer_consume_token (parser->lexer);
27951 return;
27952 }
27953 /* Fall through for C++0x, so we handle the second `>' in
27954 the `>>'. */
27955 gcc_fallthrough ();
27956
27957 case CPP_GREATER:
27958 if (!nesting_depth && level-- == 0)
27959 {
27960 /* We've reached the token we want, consume it and stop. */
27961 cp_lexer_consume_token (parser->lexer);
27962 return;
27963 }
27964 break;
27965
27966 case CPP_OPEN_PAREN:
27967 case CPP_OPEN_SQUARE:
27968 ++nesting_depth;
27969 break;
27970
27971 case CPP_CLOSE_PAREN:
27972 case CPP_CLOSE_SQUARE:
27973 if (nesting_depth-- == 0)
27974 return;
27975 break;
27976
27977 case CPP_EOF:
27978 case CPP_PRAGMA_EOL:
27979 case CPP_SEMICOLON:
27980 case CPP_OPEN_BRACE:
27981 case CPP_CLOSE_BRACE:
27982 /* The '>' was probably forgotten, don't look further. */
27983 return;
27984
27985 default:
27986 break;
27987 }
27988
27989 /* Consume this token. */
27990 cp_lexer_consume_token (parser->lexer);
27991 }
27992 }
27993
27994 /* If the next token is the indicated keyword, consume it. Otherwise,
27995 issue an error message indicating that TOKEN_DESC was expected.
27996
27997 Returns the token consumed, if the token had the appropriate type.
27998 Otherwise, returns NULL. */
27999
28000 static cp_token *
28001 cp_parser_require_keyword (cp_parser* parser,
28002 enum rid keyword,
28003 required_token token_desc)
28004 {
28005 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28006
28007 if (token && token->keyword != keyword)
28008 {
28009 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28010 return NULL;
28011 }
28012
28013 return token;
28014 }
28015
28016 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28017 function-definition. */
28018
28019 static bool
28020 cp_parser_token_starts_function_definition_p (cp_token* token)
28021 {
28022 return (/* An ordinary function-body begins with an `{'. */
28023 token->type == CPP_OPEN_BRACE
28024 /* A ctor-initializer begins with a `:'. */
28025 || token->type == CPP_COLON
28026 /* A function-try-block begins with `try'. */
28027 || token->keyword == RID_TRY
28028 /* A function-transaction-block begins with `__transaction_atomic'
28029 or `__transaction_relaxed'. */
28030 || token->keyword == RID_TRANSACTION_ATOMIC
28031 || token->keyword == RID_TRANSACTION_RELAXED
28032 /* The named return value extension begins with `return'. */
28033 || token->keyword == RID_RETURN);
28034 }
28035
28036 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28037 definition. */
28038
28039 static bool
28040 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28041 {
28042 cp_token *token;
28043
28044 token = cp_lexer_peek_token (parser->lexer);
28045 return (token->type == CPP_OPEN_BRACE
28046 || (token->type == CPP_COLON
28047 && !parser->colon_doesnt_start_class_def_p));
28048 }
28049
28050 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28051 C++0x) ending a template-argument. */
28052
28053 static bool
28054 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28055 {
28056 cp_token *token;
28057
28058 token = cp_lexer_peek_token (parser->lexer);
28059 return (token->type == CPP_COMMA
28060 || token->type == CPP_GREATER
28061 || token->type == CPP_ELLIPSIS
28062 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28063 }
28064
28065 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28066 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28067
28068 static bool
28069 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28070 size_t n)
28071 {
28072 cp_token *token;
28073
28074 token = cp_lexer_peek_nth_token (parser->lexer, n);
28075 if (token->type == CPP_LESS)
28076 return true;
28077 /* Check for the sequence `<::' in the original code. It would be lexed as
28078 `[:', where `[' is a digraph, and there is no whitespace before
28079 `:'. */
28080 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28081 {
28082 cp_token *token2;
28083 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28084 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28085 return true;
28086 }
28087 return false;
28088 }
28089
28090 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28091 or none_type otherwise. */
28092
28093 static enum tag_types
28094 cp_parser_token_is_class_key (cp_token* token)
28095 {
28096 switch (token->keyword)
28097 {
28098 case RID_CLASS:
28099 return class_type;
28100 case RID_STRUCT:
28101 return record_type;
28102 case RID_UNION:
28103 return union_type;
28104
28105 default:
28106 return none_type;
28107 }
28108 }
28109
28110 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28111 or none_type otherwise or if the token is null. */
28112
28113 static enum tag_types
28114 cp_parser_token_is_type_parameter_key (cp_token* token)
28115 {
28116 if (!token)
28117 return none_type;
28118
28119 switch (token->keyword)
28120 {
28121 case RID_CLASS:
28122 return class_type;
28123 case RID_TYPENAME:
28124 return typename_type;
28125
28126 default:
28127 return none_type;
28128 }
28129 }
28130
28131 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28132
28133 static void
28134 cp_parser_check_class_key (enum tag_types class_key, tree type)
28135 {
28136 if (type == error_mark_node)
28137 return;
28138 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28139 {
28140 if (permerror (input_location, "%qs tag used in naming %q#T",
28141 class_key == union_type ? "union"
28142 : class_key == record_type ? "struct" : "class",
28143 type))
28144 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28145 "%q#T was previously declared here", type);
28146 }
28147 }
28148
28149 /* Issue an error message if DECL is redeclared with different
28150 access than its original declaration [class.access.spec/3].
28151 This applies to nested classes, nested class templates and
28152 enumerations [class.mem/1]. */
28153
28154 static void
28155 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28156 {
28157 if (!decl
28158 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28159 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28160 return;
28161
28162 if ((TREE_PRIVATE (decl)
28163 != (current_access_specifier == access_private_node))
28164 || (TREE_PROTECTED (decl)
28165 != (current_access_specifier == access_protected_node)))
28166 error_at (location, "%qD redeclared with different access", decl);
28167 }
28168
28169 /* Look for the `template' keyword, as a syntactic disambiguator.
28170 Return TRUE iff it is present, in which case it will be
28171 consumed. */
28172
28173 static bool
28174 cp_parser_optional_template_keyword (cp_parser *parser)
28175 {
28176 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28177 {
28178 /* In C++98 the `template' keyword can only be used within templates;
28179 outside templates the parser can always figure out what is a
28180 template and what is not. In C++11, per the resolution of DR 468,
28181 `template' is allowed in cases where it is not strictly necessary. */
28182 if (!processing_template_decl
28183 && pedantic && cxx_dialect == cxx98)
28184 {
28185 cp_token *token = cp_lexer_peek_token (parser->lexer);
28186 pedwarn (token->location, OPT_Wpedantic,
28187 "in C++98 %<template%> (as a disambiguator) is only "
28188 "allowed within templates");
28189 /* If this part of the token stream is rescanned, the same
28190 error message would be generated. So, we purge the token
28191 from the stream. */
28192 cp_lexer_purge_token (parser->lexer);
28193 return false;
28194 }
28195 else
28196 {
28197 /* Consume the `template' keyword. */
28198 cp_lexer_consume_token (parser->lexer);
28199 return true;
28200 }
28201 }
28202 return false;
28203 }
28204
28205 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28206 set PARSER->SCOPE, and perform other related actions. */
28207
28208 static void
28209 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28210 {
28211 struct tree_check *check_value;
28212
28213 /* Get the stored value. */
28214 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28215 /* Set the scope from the stored value. */
28216 parser->scope = saved_checks_value (check_value);
28217 parser->qualifying_scope = check_value->qualifying_scope;
28218 parser->object_scope = NULL_TREE;
28219 }
28220
28221 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28222 encounter the end of a block before what we were looking for. */
28223
28224 static bool
28225 cp_parser_cache_group (cp_parser *parser,
28226 enum cpp_ttype end,
28227 unsigned depth)
28228 {
28229 while (true)
28230 {
28231 cp_token *token = cp_lexer_peek_token (parser->lexer);
28232
28233 /* Abort a parenthesized expression if we encounter a semicolon. */
28234 if ((end == CPP_CLOSE_PAREN || depth == 0)
28235 && token->type == CPP_SEMICOLON)
28236 return true;
28237 /* If we've reached the end of the file, stop. */
28238 if (token->type == CPP_EOF
28239 || (end != CPP_PRAGMA_EOL
28240 && token->type == CPP_PRAGMA_EOL))
28241 return true;
28242 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28243 /* We've hit the end of an enclosing block, so there's been some
28244 kind of syntax error. */
28245 return true;
28246
28247 /* Consume the token. */
28248 cp_lexer_consume_token (parser->lexer);
28249 /* See if it starts a new group. */
28250 if (token->type == CPP_OPEN_BRACE)
28251 {
28252 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28253 /* In theory this should probably check end == '}', but
28254 cp_parser_save_member_function_body needs it to exit
28255 after either '}' or ')' when called with ')'. */
28256 if (depth == 0)
28257 return false;
28258 }
28259 else if (token->type == CPP_OPEN_PAREN)
28260 {
28261 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28262 if (depth == 0 && end == CPP_CLOSE_PAREN)
28263 return false;
28264 }
28265 else if (token->type == CPP_PRAGMA)
28266 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28267 else if (token->type == end)
28268 return false;
28269 }
28270 }
28271
28272 /* Like above, for caching a default argument or NSDMI. Both of these are
28273 terminated by a non-nested comma, but it can be unclear whether or not a
28274 comma is nested in a template argument list unless we do more parsing.
28275 In order to handle this ambiguity, when we encounter a ',' after a '<'
28276 we try to parse what follows as a parameter-declaration-list (in the
28277 case of a default argument) or a member-declarator (in the case of an
28278 NSDMI). If that succeeds, then we stop caching. */
28279
28280 static tree
28281 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28282 {
28283 unsigned depth = 0;
28284 int maybe_template_id = 0;
28285 cp_token *first_token;
28286 cp_token *token;
28287 tree default_argument;
28288
28289 /* Add tokens until we have processed the entire default
28290 argument. We add the range [first_token, token). */
28291 first_token = cp_lexer_peek_token (parser->lexer);
28292 if (first_token->type == CPP_OPEN_BRACE)
28293 {
28294 /* For list-initialization, this is straightforward. */
28295 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28296 token = cp_lexer_peek_token (parser->lexer);
28297 }
28298 else while (true)
28299 {
28300 bool done = false;
28301
28302 /* Peek at the next token. */
28303 token = cp_lexer_peek_token (parser->lexer);
28304 /* What we do depends on what token we have. */
28305 switch (token->type)
28306 {
28307 /* In valid code, a default argument must be
28308 immediately followed by a `,' `)', or `...'. */
28309 case CPP_COMMA:
28310 if (depth == 0 && maybe_template_id)
28311 {
28312 /* If we've seen a '<', we might be in a
28313 template-argument-list. Until Core issue 325 is
28314 resolved, we don't know how this situation ought
28315 to be handled, so try to DTRT. We check whether
28316 what comes after the comma is a valid parameter
28317 declaration list. If it is, then the comma ends
28318 the default argument; otherwise the default
28319 argument continues. */
28320 bool error = false;
28321 cp_token *peek;
28322
28323 /* Set ITALP so cp_parser_parameter_declaration_list
28324 doesn't decide to commit to this parse. */
28325 bool saved_italp = parser->in_template_argument_list_p;
28326 parser->in_template_argument_list_p = true;
28327
28328 cp_parser_parse_tentatively (parser);
28329
28330 if (nsdmi)
28331 {
28332 /* Parse declarators until we reach a non-comma or
28333 somthing that cannot be an initializer.
28334 Just checking whether we're looking at a single
28335 declarator is insufficient. Consider:
28336 int var = tuple<T,U>::x;
28337 The template parameter 'U' looks exactly like a
28338 declarator. */
28339 do
28340 {
28341 int ctor_dtor_or_conv_p;
28342 cp_lexer_consume_token (parser->lexer);
28343 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28344 &ctor_dtor_or_conv_p,
28345 /*parenthesized_p=*/NULL,
28346 /*member_p=*/true,
28347 /*friend_p=*/false);
28348 peek = cp_lexer_peek_token (parser->lexer);
28349 if (cp_parser_error_occurred (parser))
28350 break;
28351 }
28352 while (peek->type == CPP_COMMA);
28353 /* If we met an '=' or ';' then the original comma
28354 was the end of the NSDMI. Otherwise assume
28355 we're still in the NSDMI. */
28356 error = (peek->type != CPP_EQ
28357 && peek->type != CPP_SEMICOLON);
28358 }
28359 else
28360 {
28361 cp_lexer_consume_token (parser->lexer);
28362 begin_scope (sk_function_parms, NULL_TREE);
28363 cp_parser_parameter_declaration_list (parser, &error);
28364 pop_bindings_and_leave_scope ();
28365 }
28366 if (!cp_parser_error_occurred (parser) && !error)
28367 done = true;
28368 cp_parser_abort_tentative_parse (parser);
28369
28370 parser->in_template_argument_list_p = saved_italp;
28371 break;
28372 }
28373 /* FALLTHRU */
28374 case CPP_CLOSE_PAREN:
28375 case CPP_ELLIPSIS:
28376 /* If we run into a non-nested `;', `}', or `]',
28377 then the code is invalid -- but the default
28378 argument is certainly over. */
28379 case CPP_SEMICOLON:
28380 case CPP_CLOSE_BRACE:
28381 case CPP_CLOSE_SQUARE:
28382 if (depth == 0
28383 /* Handle correctly int n = sizeof ... ( p ); */
28384 && token->type != CPP_ELLIPSIS)
28385 done = true;
28386 /* Update DEPTH, if necessary. */
28387 else if (token->type == CPP_CLOSE_PAREN
28388 || token->type == CPP_CLOSE_BRACE
28389 || token->type == CPP_CLOSE_SQUARE)
28390 --depth;
28391 break;
28392
28393 case CPP_OPEN_PAREN:
28394 case CPP_OPEN_SQUARE:
28395 case CPP_OPEN_BRACE:
28396 ++depth;
28397 break;
28398
28399 case CPP_LESS:
28400 if (depth == 0)
28401 /* This might be the comparison operator, or it might
28402 start a template argument list. */
28403 ++maybe_template_id;
28404 break;
28405
28406 case CPP_RSHIFT:
28407 if (cxx_dialect == cxx98)
28408 break;
28409 /* Fall through for C++0x, which treats the `>>'
28410 operator like two `>' tokens in certain
28411 cases. */
28412 gcc_fallthrough ();
28413
28414 case CPP_GREATER:
28415 if (depth == 0)
28416 {
28417 /* This might be an operator, or it might close a
28418 template argument list. But if a previous '<'
28419 started a template argument list, this will have
28420 closed it, so we can't be in one anymore. */
28421 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28422 if (maybe_template_id < 0)
28423 maybe_template_id = 0;
28424 }
28425 break;
28426
28427 /* If we run out of tokens, issue an error message. */
28428 case CPP_EOF:
28429 case CPP_PRAGMA_EOL:
28430 error_at (token->location, "file ends in default argument");
28431 return error_mark_node;
28432
28433 case CPP_NAME:
28434 case CPP_SCOPE:
28435 /* In these cases, we should look for template-ids.
28436 For example, if the default argument is
28437 `X<int, double>()', we need to do name lookup to
28438 figure out whether or not `X' is a template; if
28439 so, the `,' does not end the default argument.
28440
28441 That is not yet done. */
28442 break;
28443
28444 default:
28445 break;
28446 }
28447
28448 /* If we've reached the end, stop. */
28449 if (done)
28450 break;
28451
28452 /* Add the token to the token block. */
28453 token = cp_lexer_consume_token (parser->lexer);
28454 }
28455
28456 /* Create a DEFAULT_ARG to represent the unparsed default
28457 argument. */
28458 default_argument = make_node (DEFAULT_ARG);
28459 DEFARG_TOKENS (default_argument)
28460 = cp_token_cache_new (first_token, token);
28461 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28462
28463 return default_argument;
28464 }
28465
28466 /* Begin parsing tentatively. We always save tokens while parsing
28467 tentatively so that if the tentative parsing fails we can restore the
28468 tokens. */
28469
28470 static void
28471 cp_parser_parse_tentatively (cp_parser* parser)
28472 {
28473 /* Enter a new parsing context. */
28474 parser->context = cp_parser_context_new (parser->context);
28475 /* Begin saving tokens. */
28476 cp_lexer_save_tokens (parser->lexer);
28477 /* In order to avoid repetitive access control error messages,
28478 access checks are queued up until we are no longer parsing
28479 tentatively. */
28480 push_deferring_access_checks (dk_deferred);
28481 }
28482
28483 /* Commit to the currently active tentative parse. */
28484
28485 static void
28486 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28487 {
28488 cp_parser_context *context;
28489 cp_lexer *lexer;
28490
28491 /* Mark all of the levels as committed. */
28492 lexer = parser->lexer;
28493 for (context = parser->context; context->next; context = context->next)
28494 {
28495 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28496 break;
28497 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28498 while (!cp_lexer_saving_tokens (lexer))
28499 lexer = lexer->next;
28500 cp_lexer_commit_tokens (lexer);
28501 }
28502 }
28503
28504 /* Commit to the topmost currently active tentative parse.
28505
28506 Note that this function shouldn't be called when there are
28507 irreversible side-effects while in a tentative state. For
28508 example, we shouldn't create a permanent entry in the symbol
28509 table, or issue an error message that might not apply if the
28510 tentative parse is aborted. */
28511
28512 static void
28513 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28514 {
28515 cp_parser_context *context = parser->context;
28516 cp_lexer *lexer = parser->lexer;
28517
28518 if (context)
28519 {
28520 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28521 return;
28522 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28523
28524 while (!cp_lexer_saving_tokens (lexer))
28525 lexer = lexer->next;
28526 cp_lexer_commit_tokens (lexer);
28527 }
28528 }
28529
28530 /* Abort the currently active tentative parse. All consumed tokens
28531 will be rolled back, and no diagnostics will be issued. */
28532
28533 static void
28534 cp_parser_abort_tentative_parse (cp_parser* parser)
28535 {
28536 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28537 || errorcount > 0);
28538 cp_parser_simulate_error (parser);
28539 /* Now, pretend that we want to see if the construct was
28540 successfully parsed. */
28541 cp_parser_parse_definitely (parser);
28542 }
28543
28544 /* Stop parsing tentatively. If a parse error has occurred, restore the
28545 token stream. Otherwise, commit to the tokens we have consumed.
28546 Returns true if no error occurred; false otherwise. */
28547
28548 static bool
28549 cp_parser_parse_definitely (cp_parser* parser)
28550 {
28551 bool error_occurred;
28552 cp_parser_context *context;
28553
28554 /* Remember whether or not an error occurred, since we are about to
28555 destroy that information. */
28556 error_occurred = cp_parser_error_occurred (parser);
28557 /* Remove the topmost context from the stack. */
28558 context = parser->context;
28559 parser->context = context->next;
28560 /* If no parse errors occurred, commit to the tentative parse. */
28561 if (!error_occurred)
28562 {
28563 /* Commit to the tokens read tentatively, unless that was
28564 already done. */
28565 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28566 cp_lexer_commit_tokens (parser->lexer);
28567
28568 pop_to_parent_deferring_access_checks ();
28569 }
28570 /* Otherwise, if errors occurred, roll back our state so that things
28571 are just as they were before we began the tentative parse. */
28572 else
28573 {
28574 cp_lexer_rollback_tokens (parser->lexer);
28575 pop_deferring_access_checks ();
28576 }
28577 /* Add the context to the front of the free list. */
28578 context->next = cp_parser_context_free_list;
28579 cp_parser_context_free_list = context;
28580
28581 return !error_occurred;
28582 }
28583
28584 /* Returns true if we are parsing tentatively and are not committed to
28585 this tentative parse. */
28586
28587 static bool
28588 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28589 {
28590 return (cp_parser_parsing_tentatively (parser)
28591 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28592 }
28593
28594 /* Returns nonzero iff an error has occurred during the most recent
28595 tentative parse. */
28596
28597 static bool
28598 cp_parser_error_occurred (cp_parser* parser)
28599 {
28600 return (cp_parser_parsing_tentatively (parser)
28601 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28602 }
28603
28604 /* Returns nonzero if GNU extensions are allowed. */
28605
28606 static bool
28607 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28608 {
28609 return parser->allow_gnu_extensions_p;
28610 }
28611 \f
28612 /* Objective-C++ Productions */
28613
28614
28615 /* Parse an Objective-C expression, which feeds into a primary-expression
28616 above.
28617
28618 objc-expression:
28619 objc-message-expression
28620 objc-string-literal
28621 objc-encode-expression
28622 objc-protocol-expression
28623 objc-selector-expression
28624
28625 Returns a tree representation of the expression. */
28626
28627 static cp_expr
28628 cp_parser_objc_expression (cp_parser* parser)
28629 {
28630 /* Try to figure out what kind of declaration is present. */
28631 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28632
28633 switch (kwd->type)
28634 {
28635 case CPP_OPEN_SQUARE:
28636 return cp_parser_objc_message_expression (parser);
28637
28638 case CPP_OBJC_STRING:
28639 kwd = cp_lexer_consume_token (parser->lexer);
28640 return objc_build_string_object (kwd->u.value);
28641
28642 case CPP_KEYWORD:
28643 switch (kwd->keyword)
28644 {
28645 case RID_AT_ENCODE:
28646 return cp_parser_objc_encode_expression (parser);
28647
28648 case RID_AT_PROTOCOL:
28649 return cp_parser_objc_protocol_expression (parser);
28650
28651 case RID_AT_SELECTOR:
28652 return cp_parser_objc_selector_expression (parser);
28653
28654 default:
28655 break;
28656 }
28657 default:
28658 error_at (kwd->location,
28659 "misplaced %<@%D%> Objective-C++ construct",
28660 kwd->u.value);
28661 cp_parser_skip_to_end_of_block_or_statement (parser);
28662 }
28663
28664 return error_mark_node;
28665 }
28666
28667 /* Parse an Objective-C message expression.
28668
28669 objc-message-expression:
28670 [ objc-message-receiver objc-message-args ]
28671
28672 Returns a representation of an Objective-C message. */
28673
28674 static tree
28675 cp_parser_objc_message_expression (cp_parser* parser)
28676 {
28677 tree receiver, messageargs;
28678
28679 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28680 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28681 receiver = cp_parser_objc_message_receiver (parser);
28682 messageargs = cp_parser_objc_message_args (parser);
28683 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28685
28686 tree result = objc_build_message_expr (receiver, messageargs);
28687
28688 /* Construct a location e.g.
28689 [self func1:5]
28690 ^~~~~~~~~~~~~~
28691 ranging from the '[' to the ']', with the caret at the start. */
28692 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28693 protected_set_expr_location (result, combined_loc);
28694
28695 return result;
28696 }
28697
28698 /* Parse an objc-message-receiver.
28699
28700 objc-message-receiver:
28701 expression
28702 simple-type-specifier
28703
28704 Returns a representation of the type or expression. */
28705
28706 static tree
28707 cp_parser_objc_message_receiver (cp_parser* parser)
28708 {
28709 tree rcv;
28710
28711 /* An Objective-C message receiver may be either (1) a type
28712 or (2) an expression. */
28713 cp_parser_parse_tentatively (parser);
28714 rcv = cp_parser_expression (parser);
28715
28716 /* If that worked out, fine. */
28717 if (cp_parser_parse_definitely (parser))
28718 return rcv;
28719
28720 cp_parser_parse_tentatively (parser);
28721 rcv = cp_parser_simple_type_specifier (parser,
28722 /*decl_specs=*/NULL,
28723 CP_PARSER_FLAGS_NONE);
28724
28725 if (cp_parser_parse_definitely (parser))
28726 return objc_get_class_reference (rcv);
28727
28728 cp_parser_error (parser, "objective-c++ message receiver expected");
28729 return error_mark_node;
28730 }
28731
28732 /* Parse the arguments and selectors comprising an Objective-C message.
28733
28734 objc-message-args:
28735 objc-selector
28736 objc-selector-args
28737 objc-selector-args , objc-comma-args
28738
28739 objc-selector-args:
28740 objc-selector [opt] : assignment-expression
28741 objc-selector-args objc-selector [opt] : assignment-expression
28742
28743 objc-comma-args:
28744 assignment-expression
28745 objc-comma-args , assignment-expression
28746
28747 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28748 selector arguments and TREE_VALUE containing a list of comma
28749 arguments. */
28750
28751 static tree
28752 cp_parser_objc_message_args (cp_parser* parser)
28753 {
28754 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28755 bool maybe_unary_selector_p = true;
28756 cp_token *token = cp_lexer_peek_token (parser->lexer);
28757
28758 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28759 {
28760 tree selector = NULL_TREE, arg;
28761
28762 if (token->type != CPP_COLON)
28763 selector = cp_parser_objc_selector (parser);
28764
28765 /* Detect if we have a unary selector. */
28766 if (maybe_unary_selector_p
28767 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28768 return build_tree_list (selector, NULL_TREE);
28769
28770 maybe_unary_selector_p = false;
28771 cp_parser_require (parser, CPP_COLON, RT_COLON);
28772 arg = cp_parser_assignment_expression (parser);
28773
28774 sel_args
28775 = chainon (sel_args,
28776 build_tree_list (selector, arg));
28777
28778 token = cp_lexer_peek_token (parser->lexer);
28779 }
28780
28781 /* Handle non-selector arguments, if any. */
28782 while (token->type == CPP_COMMA)
28783 {
28784 tree arg;
28785
28786 cp_lexer_consume_token (parser->lexer);
28787 arg = cp_parser_assignment_expression (parser);
28788
28789 addl_args
28790 = chainon (addl_args,
28791 build_tree_list (NULL_TREE, arg));
28792
28793 token = cp_lexer_peek_token (parser->lexer);
28794 }
28795
28796 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28797 {
28798 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28799 return build_tree_list (error_mark_node, error_mark_node);
28800 }
28801
28802 return build_tree_list (sel_args, addl_args);
28803 }
28804
28805 /* Parse an Objective-C encode expression.
28806
28807 objc-encode-expression:
28808 @encode objc-typename
28809
28810 Returns an encoded representation of the type argument. */
28811
28812 static cp_expr
28813 cp_parser_objc_encode_expression (cp_parser* parser)
28814 {
28815 tree type;
28816 cp_token *token;
28817 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28818
28819 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28820 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28821 token = cp_lexer_peek_token (parser->lexer);
28822 type = complete_type (cp_parser_type_id (parser));
28823 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28824
28825 if (!type)
28826 {
28827 error_at (token->location,
28828 "%<@encode%> must specify a type as an argument");
28829 return error_mark_node;
28830 }
28831
28832 /* This happens if we find @encode(T) (where T is a template
28833 typename or something dependent on a template typename) when
28834 parsing a template. In that case, we can't compile it
28835 immediately, but we rather create an AT_ENCODE_EXPR which will
28836 need to be instantiated when the template is used.
28837 */
28838 if (dependent_type_p (type))
28839 {
28840 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28841 TREE_READONLY (value) = 1;
28842 return value;
28843 }
28844
28845
28846 /* Build a location of the form:
28847 @encode(int)
28848 ^~~~~~~~~~~~
28849 with caret==start at the @ token, finishing at the close paren. */
28850 location_t combined_loc
28851 = make_location (start_loc, start_loc,
28852 cp_lexer_previous_token (parser->lexer)->location);
28853
28854 return cp_expr (objc_build_encode_expr (type), combined_loc);
28855 }
28856
28857 /* Parse an Objective-C @defs expression. */
28858
28859 static tree
28860 cp_parser_objc_defs_expression (cp_parser *parser)
28861 {
28862 tree name;
28863
28864 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28865 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28866 name = cp_parser_identifier (parser);
28867 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28868
28869 return objc_get_class_ivars (name);
28870 }
28871
28872 /* Parse an Objective-C protocol expression.
28873
28874 objc-protocol-expression:
28875 @protocol ( identifier )
28876
28877 Returns a representation of the protocol expression. */
28878
28879 static tree
28880 cp_parser_objc_protocol_expression (cp_parser* parser)
28881 {
28882 tree proto;
28883 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28884
28885 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28886 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28887 proto = cp_parser_identifier (parser);
28888 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28889
28890 /* Build a location of the form:
28891 @protocol(prot)
28892 ^~~~~~~~~~~~~~~
28893 with caret==start at the @ token, finishing at the close paren. */
28894 location_t combined_loc
28895 = make_location (start_loc, start_loc,
28896 cp_lexer_previous_token (parser->lexer)->location);
28897 tree result = objc_build_protocol_expr (proto);
28898 protected_set_expr_location (result, combined_loc);
28899 return result;
28900 }
28901
28902 /* Parse an Objective-C selector expression.
28903
28904 objc-selector-expression:
28905 @selector ( objc-method-signature )
28906
28907 objc-method-signature:
28908 objc-selector
28909 objc-selector-seq
28910
28911 objc-selector-seq:
28912 objc-selector :
28913 objc-selector-seq objc-selector :
28914
28915 Returns a representation of the method selector. */
28916
28917 static tree
28918 cp_parser_objc_selector_expression (cp_parser* parser)
28919 {
28920 tree sel_seq = NULL_TREE;
28921 bool maybe_unary_selector_p = true;
28922 cp_token *token;
28923 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28924
28925 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28926 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28927 token = cp_lexer_peek_token (parser->lexer);
28928
28929 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28930 || token->type == CPP_SCOPE)
28931 {
28932 tree selector = NULL_TREE;
28933
28934 if (token->type != CPP_COLON
28935 || token->type == CPP_SCOPE)
28936 selector = cp_parser_objc_selector (parser);
28937
28938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28939 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28940 {
28941 /* Detect if we have a unary selector. */
28942 if (maybe_unary_selector_p)
28943 {
28944 sel_seq = selector;
28945 goto finish_selector;
28946 }
28947 else
28948 {
28949 cp_parser_error (parser, "expected %<:%>");
28950 }
28951 }
28952 maybe_unary_selector_p = false;
28953 token = cp_lexer_consume_token (parser->lexer);
28954
28955 if (token->type == CPP_SCOPE)
28956 {
28957 sel_seq
28958 = chainon (sel_seq,
28959 build_tree_list (selector, NULL_TREE));
28960 sel_seq
28961 = chainon (sel_seq,
28962 build_tree_list (NULL_TREE, NULL_TREE));
28963 }
28964 else
28965 sel_seq
28966 = chainon (sel_seq,
28967 build_tree_list (selector, NULL_TREE));
28968
28969 token = cp_lexer_peek_token (parser->lexer);
28970 }
28971
28972 finish_selector:
28973 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28974
28975
28976 /* Build a location of the form:
28977 @selector(func)
28978 ^~~~~~~~~~~~~~~
28979 with caret==start at the @ token, finishing at the close paren. */
28980 location_t combined_loc
28981 = make_location (loc, loc,
28982 cp_lexer_previous_token (parser->lexer)->location);
28983 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28984 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28985 protected_set_expr_location (result, combined_loc);
28986 return result;
28987 }
28988
28989 /* Parse a list of identifiers.
28990
28991 objc-identifier-list:
28992 identifier
28993 objc-identifier-list , identifier
28994
28995 Returns a TREE_LIST of identifier nodes. */
28996
28997 static tree
28998 cp_parser_objc_identifier_list (cp_parser* parser)
28999 {
29000 tree identifier;
29001 tree list;
29002 cp_token *sep;
29003
29004 identifier = cp_parser_identifier (parser);
29005 if (identifier == error_mark_node)
29006 return error_mark_node;
29007
29008 list = build_tree_list (NULL_TREE, identifier);
29009 sep = cp_lexer_peek_token (parser->lexer);
29010
29011 while (sep->type == CPP_COMMA)
29012 {
29013 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29014 identifier = cp_parser_identifier (parser);
29015 if (identifier == error_mark_node)
29016 return list;
29017
29018 list = chainon (list, build_tree_list (NULL_TREE,
29019 identifier));
29020 sep = cp_lexer_peek_token (parser->lexer);
29021 }
29022
29023 return list;
29024 }
29025
29026 /* Parse an Objective-C alias declaration.
29027
29028 objc-alias-declaration:
29029 @compatibility_alias identifier identifier ;
29030
29031 This function registers the alias mapping with the Objective-C front end.
29032 It returns nothing. */
29033
29034 static void
29035 cp_parser_objc_alias_declaration (cp_parser* parser)
29036 {
29037 tree alias, orig;
29038
29039 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29040 alias = cp_parser_identifier (parser);
29041 orig = cp_parser_identifier (parser);
29042 objc_declare_alias (alias, orig);
29043 cp_parser_consume_semicolon_at_end_of_statement (parser);
29044 }
29045
29046 /* Parse an Objective-C class forward-declaration.
29047
29048 objc-class-declaration:
29049 @class objc-identifier-list ;
29050
29051 The function registers the forward declarations with the Objective-C
29052 front end. It returns nothing. */
29053
29054 static void
29055 cp_parser_objc_class_declaration (cp_parser* parser)
29056 {
29057 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29058 while (true)
29059 {
29060 tree id;
29061
29062 id = cp_parser_identifier (parser);
29063 if (id == error_mark_node)
29064 break;
29065
29066 objc_declare_class (id);
29067
29068 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29069 cp_lexer_consume_token (parser->lexer);
29070 else
29071 break;
29072 }
29073 cp_parser_consume_semicolon_at_end_of_statement (parser);
29074 }
29075
29076 /* Parse a list of Objective-C protocol references.
29077
29078 objc-protocol-refs-opt:
29079 objc-protocol-refs [opt]
29080
29081 objc-protocol-refs:
29082 < objc-identifier-list >
29083
29084 Returns a TREE_LIST of identifiers, if any. */
29085
29086 static tree
29087 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29088 {
29089 tree protorefs = NULL_TREE;
29090
29091 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29092 {
29093 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29094 protorefs = cp_parser_objc_identifier_list (parser);
29095 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29096 }
29097
29098 return protorefs;
29099 }
29100
29101 /* Parse a Objective-C visibility specification. */
29102
29103 static void
29104 cp_parser_objc_visibility_spec (cp_parser* parser)
29105 {
29106 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29107
29108 switch (vis->keyword)
29109 {
29110 case RID_AT_PRIVATE:
29111 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29112 break;
29113 case RID_AT_PROTECTED:
29114 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29115 break;
29116 case RID_AT_PUBLIC:
29117 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29118 break;
29119 case RID_AT_PACKAGE:
29120 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29121 break;
29122 default:
29123 return;
29124 }
29125
29126 /* Eat '@private'/'@protected'/'@public'. */
29127 cp_lexer_consume_token (parser->lexer);
29128 }
29129
29130 /* Parse an Objective-C method type. Return 'true' if it is a class
29131 (+) method, and 'false' if it is an instance (-) method. */
29132
29133 static inline bool
29134 cp_parser_objc_method_type (cp_parser* parser)
29135 {
29136 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29137 return true;
29138 else
29139 return false;
29140 }
29141
29142 /* Parse an Objective-C protocol qualifier. */
29143
29144 static tree
29145 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29146 {
29147 tree quals = NULL_TREE, node;
29148 cp_token *token = cp_lexer_peek_token (parser->lexer);
29149
29150 node = token->u.value;
29151
29152 while (node && identifier_p (node)
29153 && (node == ridpointers [(int) RID_IN]
29154 || node == ridpointers [(int) RID_OUT]
29155 || node == ridpointers [(int) RID_INOUT]
29156 || node == ridpointers [(int) RID_BYCOPY]
29157 || node == ridpointers [(int) RID_BYREF]
29158 || node == ridpointers [(int) RID_ONEWAY]))
29159 {
29160 quals = tree_cons (NULL_TREE, node, quals);
29161 cp_lexer_consume_token (parser->lexer);
29162 token = cp_lexer_peek_token (parser->lexer);
29163 node = token->u.value;
29164 }
29165
29166 return quals;
29167 }
29168
29169 /* Parse an Objective-C typename. */
29170
29171 static tree
29172 cp_parser_objc_typename (cp_parser* parser)
29173 {
29174 tree type_name = NULL_TREE;
29175
29176 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29177 {
29178 tree proto_quals, cp_type = NULL_TREE;
29179
29180 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29181 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29182
29183 /* An ObjC type name may consist of just protocol qualifiers, in which
29184 case the type shall default to 'id'. */
29185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29186 {
29187 cp_type = cp_parser_type_id (parser);
29188
29189 /* If the type could not be parsed, an error has already
29190 been produced. For error recovery, behave as if it had
29191 not been specified, which will use the default type
29192 'id'. */
29193 if (cp_type == error_mark_node)
29194 {
29195 cp_type = NULL_TREE;
29196 /* We need to skip to the closing parenthesis as
29197 cp_parser_type_id() does not seem to do it for
29198 us. */
29199 cp_parser_skip_to_closing_parenthesis (parser,
29200 /*recovering=*/true,
29201 /*or_comma=*/false,
29202 /*consume_paren=*/false);
29203 }
29204 }
29205
29206 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29207 type_name = build_tree_list (proto_quals, cp_type);
29208 }
29209
29210 return type_name;
29211 }
29212
29213 /* Check to see if TYPE refers to an Objective-C selector name. */
29214
29215 static bool
29216 cp_parser_objc_selector_p (enum cpp_ttype type)
29217 {
29218 return (type == CPP_NAME || type == CPP_KEYWORD
29219 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29220 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29221 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29222 || type == CPP_XOR || type == CPP_XOR_EQ);
29223 }
29224
29225 /* Parse an Objective-C selector. */
29226
29227 static tree
29228 cp_parser_objc_selector (cp_parser* parser)
29229 {
29230 cp_token *token = cp_lexer_consume_token (parser->lexer);
29231
29232 if (!cp_parser_objc_selector_p (token->type))
29233 {
29234 error_at (token->location, "invalid Objective-C++ selector name");
29235 return error_mark_node;
29236 }
29237
29238 /* C++ operator names are allowed to appear in ObjC selectors. */
29239 switch (token->type)
29240 {
29241 case CPP_AND_AND: return get_identifier ("and");
29242 case CPP_AND_EQ: return get_identifier ("and_eq");
29243 case CPP_AND: return get_identifier ("bitand");
29244 case CPP_OR: return get_identifier ("bitor");
29245 case CPP_COMPL: return get_identifier ("compl");
29246 case CPP_NOT: return get_identifier ("not");
29247 case CPP_NOT_EQ: return get_identifier ("not_eq");
29248 case CPP_OR_OR: return get_identifier ("or");
29249 case CPP_OR_EQ: return get_identifier ("or_eq");
29250 case CPP_XOR: return get_identifier ("xor");
29251 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29252 default: return token->u.value;
29253 }
29254 }
29255
29256 /* Parse an Objective-C params list. */
29257
29258 static tree
29259 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29260 {
29261 tree params = NULL_TREE;
29262 bool maybe_unary_selector_p = true;
29263 cp_token *token = cp_lexer_peek_token (parser->lexer);
29264
29265 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29266 {
29267 tree selector = NULL_TREE, type_name, identifier;
29268 tree parm_attr = NULL_TREE;
29269
29270 if (token->keyword == RID_ATTRIBUTE)
29271 break;
29272
29273 if (token->type != CPP_COLON)
29274 selector = cp_parser_objc_selector (parser);
29275
29276 /* Detect if we have a unary selector. */
29277 if (maybe_unary_selector_p
29278 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29279 {
29280 params = selector; /* Might be followed by attributes. */
29281 break;
29282 }
29283
29284 maybe_unary_selector_p = false;
29285 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29286 {
29287 /* Something went quite wrong. There should be a colon
29288 here, but there is not. Stop parsing parameters. */
29289 break;
29290 }
29291 type_name = cp_parser_objc_typename (parser);
29292 /* New ObjC allows attributes on parameters too. */
29293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29294 parm_attr = cp_parser_attributes_opt (parser);
29295 identifier = cp_parser_identifier (parser);
29296
29297 params
29298 = chainon (params,
29299 objc_build_keyword_decl (selector,
29300 type_name,
29301 identifier,
29302 parm_attr));
29303
29304 token = cp_lexer_peek_token (parser->lexer);
29305 }
29306
29307 if (params == NULL_TREE)
29308 {
29309 cp_parser_error (parser, "objective-c++ method declaration is expected");
29310 return error_mark_node;
29311 }
29312
29313 /* We allow tail attributes for the method. */
29314 if (token->keyword == RID_ATTRIBUTE)
29315 {
29316 *attributes = cp_parser_attributes_opt (parser);
29317 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29318 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29319 return params;
29320 cp_parser_error (parser,
29321 "method attributes must be specified at the end");
29322 return error_mark_node;
29323 }
29324
29325 if (params == NULL_TREE)
29326 {
29327 cp_parser_error (parser, "objective-c++ method declaration is expected");
29328 return error_mark_node;
29329 }
29330 return params;
29331 }
29332
29333 /* Parse the non-keyword Objective-C params. */
29334
29335 static tree
29336 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29337 tree* attributes)
29338 {
29339 tree params = make_node (TREE_LIST);
29340 cp_token *token = cp_lexer_peek_token (parser->lexer);
29341 *ellipsisp = false; /* Initially, assume no ellipsis. */
29342
29343 while (token->type == CPP_COMMA)
29344 {
29345 cp_parameter_declarator *parmdecl;
29346 tree parm;
29347
29348 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29349 token = cp_lexer_peek_token (parser->lexer);
29350
29351 if (token->type == CPP_ELLIPSIS)
29352 {
29353 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29354 *ellipsisp = true;
29355 token = cp_lexer_peek_token (parser->lexer);
29356 break;
29357 }
29358
29359 /* TODO: parse attributes for tail parameters. */
29360 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29361 parm = grokdeclarator (parmdecl->declarator,
29362 &parmdecl->decl_specifiers,
29363 PARM, /*initialized=*/0,
29364 /*attrlist=*/NULL);
29365
29366 chainon (params, build_tree_list (NULL_TREE, parm));
29367 token = cp_lexer_peek_token (parser->lexer);
29368 }
29369
29370 /* We allow tail attributes for the method. */
29371 if (token->keyword == RID_ATTRIBUTE)
29372 {
29373 if (*attributes == NULL_TREE)
29374 {
29375 *attributes = cp_parser_attributes_opt (parser);
29376 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29377 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29378 return params;
29379 }
29380 else
29381 /* We have an error, but parse the attributes, so that we can
29382 carry on. */
29383 *attributes = cp_parser_attributes_opt (parser);
29384
29385 cp_parser_error (parser,
29386 "method attributes must be specified at the end");
29387 return error_mark_node;
29388 }
29389
29390 return params;
29391 }
29392
29393 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29394
29395 static void
29396 cp_parser_objc_interstitial_code (cp_parser* parser)
29397 {
29398 cp_token *token = cp_lexer_peek_token (parser->lexer);
29399
29400 /* If the next token is `extern' and the following token is a string
29401 literal, then we have a linkage specification. */
29402 if (token->keyword == RID_EXTERN
29403 && cp_parser_is_pure_string_literal
29404 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29405 cp_parser_linkage_specification (parser);
29406 /* Handle #pragma, if any. */
29407 else if (token->type == CPP_PRAGMA)
29408 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29409 /* Allow stray semicolons. */
29410 else if (token->type == CPP_SEMICOLON)
29411 cp_lexer_consume_token (parser->lexer);
29412 /* Mark methods as optional or required, when building protocols. */
29413 else if (token->keyword == RID_AT_OPTIONAL)
29414 {
29415 cp_lexer_consume_token (parser->lexer);
29416 objc_set_method_opt (true);
29417 }
29418 else if (token->keyword == RID_AT_REQUIRED)
29419 {
29420 cp_lexer_consume_token (parser->lexer);
29421 objc_set_method_opt (false);
29422 }
29423 else if (token->keyword == RID_NAMESPACE)
29424 cp_parser_namespace_definition (parser);
29425 /* Other stray characters must generate errors. */
29426 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29427 {
29428 cp_lexer_consume_token (parser->lexer);
29429 error ("stray %qs between Objective-C++ methods",
29430 token->type == CPP_OPEN_BRACE ? "{" : "}");
29431 }
29432 /* Finally, try to parse a block-declaration, or a function-definition. */
29433 else
29434 cp_parser_block_declaration (parser, /*statement_p=*/false);
29435 }
29436
29437 /* Parse a method signature. */
29438
29439 static tree
29440 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29441 {
29442 tree rettype, kwdparms, optparms;
29443 bool ellipsis = false;
29444 bool is_class_method;
29445
29446 is_class_method = cp_parser_objc_method_type (parser);
29447 rettype = cp_parser_objc_typename (parser);
29448 *attributes = NULL_TREE;
29449 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29450 if (kwdparms == error_mark_node)
29451 return error_mark_node;
29452 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29453 if (optparms == error_mark_node)
29454 return error_mark_node;
29455
29456 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29457 }
29458
29459 static bool
29460 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29461 {
29462 tree tattr;
29463 cp_lexer_save_tokens (parser->lexer);
29464 tattr = cp_parser_attributes_opt (parser);
29465 gcc_assert (tattr) ;
29466
29467 /* If the attributes are followed by a method introducer, this is not allowed.
29468 Dump the attributes and flag the situation. */
29469 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29470 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29471 return true;
29472
29473 /* Otherwise, the attributes introduce some interstitial code, possibly so
29474 rewind to allow that check. */
29475 cp_lexer_rollback_tokens (parser->lexer);
29476 return false;
29477 }
29478
29479 /* Parse an Objective-C method prototype list. */
29480
29481 static void
29482 cp_parser_objc_method_prototype_list (cp_parser* parser)
29483 {
29484 cp_token *token = cp_lexer_peek_token (parser->lexer);
29485
29486 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29487 {
29488 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29489 {
29490 tree attributes, sig;
29491 bool is_class_method;
29492 if (token->type == CPP_PLUS)
29493 is_class_method = true;
29494 else
29495 is_class_method = false;
29496 sig = cp_parser_objc_method_signature (parser, &attributes);
29497 if (sig == error_mark_node)
29498 {
29499 cp_parser_skip_to_end_of_block_or_statement (parser);
29500 token = cp_lexer_peek_token (parser->lexer);
29501 continue;
29502 }
29503 objc_add_method_declaration (is_class_method, sig, attributes);
29504 cp_parser_consume_semicolon_at_end_of_statement (parser);
29505 }
29506 else if (token->keyword == RID_AT_PROPERTY)
29507 cp_parser_objc_at_property_declaration (parser);
29508 else if (token->keyword == RID_ATTRIBUTE
29509 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29510 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29511 OPT_Wattributes,
29512 "prefix attributes are ignored for methods");
29513 else
29514 /* Allow for interspersed non-ObjC++ code. */
29515 cp_parser_objc_interstitial_code (parser);
29516
29517 token = cp_lexer_peek_token (parser->lexer);
29518 }
29519
29520 if (token->type != CPP_EOF)
29521 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29522 else
29523 cp_parser_error (parser, "expected %<@end%>");
29524
29525 objc_finish_interface ();
29526 }
29527
29528 /* Parse an Objective-C method definition list. */
29529
29530 static void
29531 cp_parser_objc_method_definition_list (cp_parser* parser)
29532 {
29533 cp_token *token = cp_lexer_peek_token (parser->lexer);
29534
29535 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29536 {
29537 tree meth;
29538
29539 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29540 {
29541 cp_token *ptk;
29542 tree sig, attribute;
29543 bool is_class_method;
29544 if (token->type == CPP_PLUS)
29545 is_class_method = true;
29546 else
29547 is_class_method = false;
29548 push_deferring_access_checks (dk_deferred);
29549 sig = cp_parser_objc_method_signature (parser, &attribute);
29550 if (sig == error_mark_node)
29551 {
29552 cp_parser_skip_to_end_of_block_or_statement (parser);
29553 token = cp_lexer_peek_token (parser->lexer);
29554 continue;
29555 }
29556 objc_start_method_definition (is_class_method, sig, attribute,
29557 NULL_TREE);
29558
29559 /* For historical reasons, we accept an optional semicolon. */
29560 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29561 cp_lexer_consume_token (parser->lexer);
29562
29563 ptk = cp_lexer_peek_token (parser->lexer);
29564 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29565 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29566 {
29567 perform_deferred_access_checks (tf_warning_or_error);
29568 stop_deferring_access_checks ();
29569 meth = cp_parser_function_definition_after_declarator (parser,
29570 false);
29571 pop_deferring_access_checks ();
29572 objc_finish_method_definition (meth);
29573 }
29574 }
29575 /* The following case will be removed once @synthesize is
29576 completely implemented. */
29577 else if (token->keyword == RID_AT_PROPERTY)
29578 cp_parser_objc_at_property_declaration (parser);
29579 else if (token->keyword == RID_AT_SYNTHESIZE)
29580 cp_parser_objc_at_synthesize_declaration (parser);
29581 else if (token->keyword == RID_AT_DYNAMIC)
29582 cp_parser_objc_at_dynamic_declaration (parser);
29583 else if (token->keyword == RID_ATTRIBUTE
29584 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29585 warning_at (token->location, OPT_Wattributes,
29586 "prefix attributes are ignored for methods");
29587 else
29588 /* Allow for interspersed non-ObjC++ code. */
29589 cp_parser_objc_interstitial_code (parser);
29590
29591 token = cp_lexer_peek_token (parser->lexer);
29592 }
29593
29594 if (token->type != CPP_EOF)
29595 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29596 else
29597 cp_parser_error (parser, "expected %<@end%>");
29598
29599 objc_finish_implementation ();
29600 }
29601
29602 /* Parse Objective-C ivars. */
29603
29604 static void
29605 cp_parser_objc_class_ivars (cp_parser* parser)
29606 {
29607 cp_token *token = cp_lexer_peek_token (parser->lexer);
29608
29609 if (token->type != CPP_OPEN_BRACE)
29610 return; /* No ivars specified. */
29611
29612 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29613 token = cp_lexer_peek_token (parser->lexer);
29614
29615 while (token->type != CPP_CLOSE_BRACE
29616 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29617 {
29618 cp_decl_specifier_seq declspecs;
29619 int decl_class_or_enum_p;
29620 tree prefix_attributes;
29621
29622 cp_parser_objc_visibility_spec (parser);
29623
29624 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29625 break;
29626
29627 cp_parser_decl_specifier_seq (parser,
29628 CP_PARSER_FLAGS_OPTIONAL,
29629 &declspecs,
29630 &decl_class_or_enum_p);
29631
29632 /* auto, register, static, extern, mutable. */
29633 if (declspecs.storage_class != sc_none)
29634 {
29635 cp_parser_error (parser, "invalid type for instance variable");
29636 declspecs.storage_class = sc_none;
29637 }
29638
29639 /* thread_local. */
29640 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29641 {
29642 cp_parser_error (parser, "invalid type for instance variable");
29643 declspecs.locations[ds_thread] = 0;
29644 }
29645
29646 /* typedef. */
29647 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29648 {
29649 cp_parser_error (parser, "invalid type for instance variable");
29650 declspecs.locations[ds_typedef] = 0;
29651 }
29652
29653 prefix_attributes = declspecs.attributes;
29654 declspecs.attributes = NULL_TREE;
29655
29656 /* Keep going until we hit the `;' at the end of the
29657 declaration. */
29658 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29659 {
29660 tree width = NULL_TREE, attributes, first_attribute, decl;
29661 cp_declarator *declarator = NULL;
29662 int ctor_dtor_or_conv_p;
29663
29664 /* Check for a (possibly unnamed) bitfield declaration. */
29665 token = cp_lexer_peek_token (parser->lexer);
29666 if (token->type == CPP_COLON)
29667 goto eat_colon;
29668
29669 if (token->type == CPP_NAME
29670 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29671 == CPP_COLON))
29672 {
29673 /* Get the name of the bitfield. */
29674 declarator = make_id_declarator (NULL_TREE,
29675 cp_parser_identifier (parser),
29676 sfk_none);
29677
29678 eat_colon:
29679 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29680 /* Get the width of the bitfield. */
29681 width
29682 = cp_parser_constant_expression (parser);
29683 }
29684 else
29685 {
29686 /* Parse the declarator. */
29687 declarator
29688 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29689 &ctor_dtor_or_conv_p,
29690 /*parenthesized_p=*/NULL,
29691 /*member_p=*/false,
29692 /*friend_p=*/false);
29693 }
29694
29695 /* Look for attributes that apply to the ivar. */
29696 attributes = cp_parser_attributes_opt (parser);
29697 /* Remember which attributes are prefix attributes and
29698 which are not. */
29699 first_attribute = attributes;
29700 /* Combine the attributes. */
29701 attributes = chainon (prefix_attributes, attributes);
29702
29703 if (width)
29704 /* Create the bitfield declaration. */
29705 decl = grokbitfield (declarator, &declspecs,
29706 width,
29707 attributes);
29708 else
29709 decl = grokfield (declarator, &declspecs,
29710 NULL_TREE, /*init_const_expr_p=*/false,
29711 NULL_TREE, attributes);
29712
29713 /* Add the instance variable. */
29714 if (decl != error_mark_node && decl != NULL_TREE)
29715 objc_add_instance_variable (decl);
29716
29717 /* Reset PREFIX_ATTRIBUTES. */
29718 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29719 attributes = TREE_CHAIN (attributes);
29720 if (attributes)
29721 TREE_CHAIN (attributes) = NULL_TREE;
29722
29723 token = cp_lexer_peek_token (parser->lexer);
29724
29725 if (token->type == CPP_COMMA)
29726 {
29727 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29728 continue;
29729 }
29730 break;
29731 }
29732
29733 cp_parser_consume_semicolon_at_end_of_statement (parser);
29734 token = cp_lexer_peek_token (parser->lexer);
29735 }
29736
29737 if (token->keyword == RID_AT_END)
29738 cp_parser_error (parser, "expected %<}%>");
29739
29740 /* Do not consume the RID_AT_END, so it will be read again as terminating
29741 the @interface of @implementation. */
29742 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29743 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29744
29745 /* For historical reasons, we accept an optional semicolon. */
29746 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29747 cp_lexer_consume_token (parser->lexer);
29748 }
29749
29750 /* Parse an Objective-C protocol declaration. */
29751
29752 static void
29753 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29754 {
29755 tree proto, protorefs;
29756 cp_token *tok;
29757
29758 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29759 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29760 {
29761 tok = cp_lexer_peek_token (parser->lexer);
29762 error_at (tok->location, "identifier expected after %<@protocol%>");
29763 cp_parser_consume_semicolon_at_end_of_statement (parser);
29764 return;
29765 }
29766
29767 /* See if we have a forward declaration or a definition. */
29768 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29769
29770 /* Try a forward declaration first. */
29771 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29772 {
29773 while (true)
29774 {
29775 tree id;
29776
29777 id = cp_parser_identifier (parser);
29778 if (id == error_mark_node)
29779 break;
29780
29781 objc_declare_protocol (id, attributes);
29782
29783 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29784 cp_lexer_consume_token (parser->lexer);
29785 else
29786 break;
29787 }
29788 cp_parser_consume_semicolon_at_end_of_statement (parser);
29789 }
29790
29791 /* Ok, we got a full-fledged definition (or at least should). */
29792 else
29793 {
29794 proto = cp_parser_identifier (parser);
29795 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29796 objc_start_protocol (proto, protorefs, attributes);
29797 cp_parser_objc_method_prototype_list (parser);
29798 }
29799 }
29800
29801 /* Parse an Objective-C superclass or category. */
29802
29803 static void
29804 cp_parser_objc_superclass_or_category (cp_parser *parser,
29805 bool iface_p,
29806 tree *super,
29807 tree *categ, bool *is_class_extension)
29808 {
29809 cp_token *next = cp_lexer_peek_token (parser->lexer);
29810
29811 *super = *categ = NULL_TREE;
29812 *is_class_extension = false;
29813 if (next->type == CPP_COLON)
29814 {
29815 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29816 *super = cp_parser_identifier (parser);
29817 }
29818 else if (next->type == CPP_OPEN_PAREN)
29819 {
29820 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29821
29822 /* If there is no category name, and this is an @interface, we
29823 have a class extension. */
29824 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29825 {
29826 *categ = NULL_TREE;
29827 *is_class_extension = true;
29828 }
29829 else
29830 *categ = cp_parser_identifier (parser);
29831
29832 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29833 }
29834 }
29835
29836 /* Parse an Objective-C class interface. */
29837
29838 static void
29839 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29840 {
29841 tree name, super, categ, protos;
29842 bool is_class_extension;
29843
29844 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29845 name = cp_parser_identifier (parser);
29846 if (name == error_mark_node)
29847 {
29848 /* It's hard to recover because even if valid @interface stuff
29849 is to follow, we can't compile it (or validate it) if we
29850 don't even know which class it refers to. Let's assume this
29851 was a stray '@interface' token in the stream and skip it.
29852 */
29853 return;
29854 }
29855 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29856 &is_class_extension);
29857 protos = cp_parser_objc_protocol_refs_opt (parser);
29858
29859 /* We have either a class or a category on our hands. */
29860 if (categ || is_class_extension)
29861 objc_start_category_interface (name, categ, protos, attributes);
29862 else
29863 {
29864 objc_start_class_interface (name, super, protos, attributes);
29865 /* Handle instance variable declarations, if any. */
29866 cp_parser_objc_class_ivars (parser);
29867 objc_continue_interface ();
29868 }
29869
29870 cp_parser_objc_method_prototype_list (parser);
29871 }
29872
29873 /* Parse an Objective-C class implementation. */
29874
29875 static void
29876 cp_parser_objc_class_implementation (cp_parser* parser)
29877 {
29878 tree name, super, categ;
29879 bool is_class_extension;
29880
29881 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29882 name = cp_parser_identifier (parser);
29883 if (name == error_mark_node)
29884 {
29885 /* It's hard to recover because even if valid @implementation
29886 stuff is to follow, we can't compile it (or validate it) if
29887 we don't even know which class it refers to. Let's assume
29888 this was a stray '@implementation' token in the stream and
29889 skip it.
29890 */
29891 return;
29892 }
29893 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29894 &is_class_extension);
29895
29896 /* We have either a class or a category on our hands. */
29897 if (categ)
29898 objc_start_category_implementation (name, categ);
29899 else
29900 {
29901 objc_start_class_implementation (name, super);
29902 /* Handle instance variable declarations, if any. */
29903 cp_parser_objc_class_ivars (parser);
29904 objc_continue_implementation ();
29905 }
29906
29907 cp_parser_objc_method_definition_list (parser);
29908 }
29909
29910 /* Consume the @end token and finish off the implementation. */
29911
29912 static void
29913 cp_parser_objc_end_implementation (cp_parser* parser)
29914 {
29915 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29916 objc_finish_implementation ();
29917 }
29918
29919 /* Parse an Objective-C declaration. */
29920
29921 static void
29922 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29923 {
29924 /* Try to figure out what kind of declaration is present. */
29925 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29926
29927 if (attributes)
29928 switch (kwd->keyword)
29929 {
29930 case RID_AT_ALIAS:
29931 case RID_AT_CLASS:
29932 case RID_AT_END:
29933 error_at (kwd->location, "attributes may not be specified before"
29934 " the %<@%D%> Objective-C++ keyword",
29935 kwd->u.value);
29936 attributes = NULL;
29937 break;
29938 case RID_AT_IMPLEMENTATION:
29939 warning_at (kwd->location, OPT_Wattributes,
29940 "prefix attributes are ignored before %<@%D%>",
29941 kwd->u.value);
29942 attributes = NULL;
29943 default:
29944 break;
29945 }
29946
29947 switch (kwd->keyword)
29948 {
29949 case RID_AT_ALIAS:
29950 cp_parser_objc_alias_declaration (parser);
29951 break;
29952 case RID_AT_CLASS:
29953 cp_parser_objc_class_declaration (parser);
29954 break;
29955 case RID_AT_PROTOCOL:
29956 cp_parser_objc_protocol_declaration (parser, attributes);
29957 break;
29958 case RID_AT_INTERFACE:
29959 cp_parser_objc_class_interface (parser, attributes);
29960 break;
29961 case RID_AT_IMPLEMENTATION:
29962 cp_parser_objc_class_implementation (parser);
29963 break;
29964 case RID_AT_END:
29965 cp_parser_objc_end_implementation (parser);
29966 break;
29967 default:
29968 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29969 kwd->u.value);
29970 cp_parser_skip_to_end_of_block_or_statement (parser);
29971 }
29972 }
29973
29974 /* Parse an Objective-C try-catch-finally statement.
29975
29976 objc-try-catch-finally-stmt:
29977 @try compound-statement objc-catch-clause-seq [opt]
29978 objc-finally-clause [opt]
29979
29980 objc-catch-clause-seq:
29981 objc-catch-clause objc-catch-clause-seq [opt]
29982
29983 objc-catch-clause:
29984 @catch ( objc-exception-declaration ) compound-statement
29985
29986 objc-finally-clause:
29987 @finally compound-statement
29988
29989 objc-exception-declaration:
29990 parameter-declaration
29991 '...'
29992
29993 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29994
29995 Returns NULL_TREE.
29996
29997 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29998 for C. Keep them in sync. */
29999
30000 static tree
30001 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30002 {
30003 location_t location;
30004 tree stmt;
30005
30006 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30007 location = cp_lexer_peek_token (parser->lexer)->location;
30008 objc_maybe_warn_exceptions (location);
30009 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30010 node, lest it get absorbed into the surrounding block. */
30011 stmt = push_stmt_list ();
30012 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30013 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30014
30015 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30016 {
30017 cp_parameter_declarator *parm;
30018 tree parameter_declaration = error_mark_node;
30019 bool seen_open_paren = false;
30020
30021 cp_lexer_consume_token (parser->lexer);
30022 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30023 seen_open_paren = true;
30024 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30025 {
30026 /* We have "@catch (...)" (where the '...' are literally
30027 what is in the code). Skip the '...'.
30028 parameter_declaration is set to NULL_TREE, and
30029 objc_being_catch_clauses() knows that that means
30030 '...'. */
30031 cp_lexer_consume_token (parser->lexer);
30032 parameter_declaration = NULL_TREE;
30033 }
30034 else
30035 {
30036 /* We have "@catch (NSException *exception)" or something
30037 like that. Parse the parameter declaration. */
30038 parm = cp_parser_parameter_declaration (parser, false, NULL);
30039 if (parm == NULL)
30040 parameter_declaration = error_mark_node;
30041 else
30042 parameter_declaration = grokdeclarator (parm->declarator,
30043 &parm->decl_specifiers,
30044 PARM, /*initialized=*/0,
30045 /*attrlist=*/NULL);
30046 }
30047 if (seen_open_paren)
30048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30049 else
30050 {
30051 /* If there was no open parenthesis, we are recovering from
30052 an error, and we are trying to figure out what mistake
30053 the user has made. */
30054
30055 /* If there is an immediate closing parenthesis, the user
30056 probably forgot the opening one (ie, they typed "@catch
30057 NSException *e)". Parse the closing parenthesis and keep
30058 going. */
30059 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30060 cp_lexer_consume_token (parser->lexer);
30061
30062 /* If these is no immediate closing parenthesis, the user
30063 probably doesn't know that parenthesis are required at
30064 all (ie, they typed "@catch NSException *e"). So, just
30065 forget about the closing parenthesis and keep going. */
30066 }
30067 objc_begin_catch_clause (parameter_declaration);
30068 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30069 objc_finish_catch_clause ();
30070 }
30071 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30072 {
30073 cp_lexer_consume_token (parser->lexer);
30074 location = cp_lexer_peek_token (parser->lexer)->location;
30075 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30076 node, lest it get absorbed into the surrounding block. */
30077 stmt = push_stmt_list ();
30078 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30079 objc_build_finally_clause (location, pop_stmt_list (stmt));
30080 }
30081
30082 return objc_finish_try_stmt ();
30083 }
30084
30085 /* Parse an Objective-C synchronized statement.
30086
30087 objc-synchronized-stmt:
30088 @synchronized ( expression ) compound-statement
30089
30090 Returns NULL_TREE. */
30091
30092 static tree
30093 cp_parser_objc_synchronized_statement (cp_parser *parser)
30094 {
30095 location_t location;
30096 tree lock, stmt;
30097
30098 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30099
30100 location = cp_lexer_peek_token (parser->lexer)->location;
30101 objc_maybe_warn_exceptions (location);
30102 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30103 lock = cp_parser_expression (parser);
30104 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30105
30106 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30107 node, lest it get absorbed into the surrounding block. */
30108 stmt = push_stmt_list ();
30109 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30110
30111 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30112 }
30113
30114 /* Parse an Objective-C throw statement.
30115
30116 objc-throw-stmt:
30117 @throw assignment-expression [opt] ;
30118
30119 Returns a constructed '@throw' statement. */
30120
30121 static tree
30122 cp_parser_objc_throw_statement (cp_parser *parser)
30123 {
30124 tree expr = NULL_TREE;
30125 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30126
30127 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30128
30129 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30130 expr = cp_parser_expression (parser);
30131
30132 cp_parser_consume_semicolon_at_end_of_statement (parser);
30133
30134 return objc_build_throw_stmt (loc, expr);
30135 }
30136
30137 /* Parse an Objective-C statement. */
30138
30139 static tree
30140 cp_parser_objc_statement (cp_parser * parser)
30141 {
30142 /* Try to figure out what kind of declaration is present. */
30143 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30144
30145 switch (kwd->keyword)
30146 {
30147 case RID_AT_TRY:
30148 return cp_parser_objc_try_catch_finally_statement (parser);
30149 case RID_AT_SYNCHRONIZED:
30150 return cp_parser_objc_synchronized_statement (parser);
30151 case RID_AT_THROW:
30152 return cp_parser_objc_throw_statement (parser);
30153 default:
30154 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30155 kwd->u.value);
30156 cp_parser_skip_to_end_of_block_or_statement (parser);
30157 }
30158
30159 return error_mark_node;
30160 }
30161
30162 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30163 look ahead to see if an objc keyword follows the attributes. This
30164 is to detect the use of prefix attributes on ObjC @interface and
30165 @protocol. */
30166
30167 static bool
30168 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30169 {
30170 cp_lexer_save_tokens (parser->lexer);
30171 *attrib = cp_parser_attributes_opt (parser);
30172 gcc_assert (*attrib);
30173 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30174 {
30175 cp_lexer_commit_tokens (parser->lexer);
30176 return true;
30177 }
30178 cp_lexer_rollback_tokens (parser->lexer);
30179 return false;
30180 }
30181
30182 /* This routine is a minimal replacement for
30183 c_parser_struct_declaration () used when parsing the list of
30184 types/names or ObjC++ properties. For example, when parsing the
30185 code
30186
30187 @property (readonly) int a, b, c;
30188
30189 this function is responsible for parsing "int a, int b, int c" and
30190 returning the declarations as CHAIN of DECLs.
30191
30192 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30193 similar parsing. */
30194 static tree
30195 cp_parser_objc_struct_declaration (cp_parser *parser)
30196 {
30197 tree decls = NULL_TREE;
30198 cp_decl_specifier_seq declspecs;
30199 int decl_class_or_enum_p;
30200 tree prefix_attributes;
30201
30202 cp_parser_decl_specifier_seq (parser,
30203 CP_PARSER_FLAGS_NONE,
30204 &declspecs,
30205 &decl_class_or_enum_p);
30206
30207 if (declspecs.type == error_mark_node)
30208 return error_mark_node;
30209
30210 /* auto, register, static, extern, mutable. */
30211 if (declspecs.storage_class != sc_none)
30212 {
30213 cp_parser_error (parser, "invalid type for property");
30214 declspecs.storage_class = sc_none;
30215 }
30216
30217 /* thread_local. */
30218 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30219 {
30220 cp_parser_error (parser, "invalid type for property");
30221 declspecs.locations[ds_thread] = 0;
30222 }
30223
30224 /* typedef. */
30225 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30226 {
30227 cp_parser_error (parser, "invalid type for property");
30228 declspecs.locations[ds_typedef] = 0;
30229 }
30230
30231 prefix_attributes = declspecs.attributes;
30232 declspecs.attributes = NULL_TREE;
30233
30234 /* Keep going until we hit the `;' at the end of the declaration. */
30235 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30236 {
30237 tree attributes, first_attribute, decl;
30238 cp_declarator *declarator;
30239 cp_token *token;
30240
30241 /* Parse the declarator. */
30242 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30243 NULL, NULL, false, false);
30244
30245 /* Look for attributes that apply to the ivar. */
30246 attributes = cp_parser_attributes_opt (parser);
30247 /* Remember which attributes are prefix attributes and
30248 which are not. */
30249 first_attribute = attributes;
30250 /* Combine the attributes. */
30251 attributes = chainon (prefix_attributes, attributes);
30252
30253 decl = grokfield (declarator, &declspecs,
30254 NULL_TREE, /*init_const_expr_p=*/false,
30255 NULL_TREE, attributes);
30256
30257 if (decl == error_mark_node || decl == NULL_TREE)
30258 return error_mark_node;
30259
30260 /* Reset PREFIX_ATTRIBUTES. */
30261 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30262 attributes = TREE_CHAIN (attributes);
30263 if (attributes)
30264 TREE_CHAIN (attributes) = NULL_TREE;
30265
30266 DECL_CHAIN (decl) = decls;
30267 decls = decl;
30268
30269 token = cp_lexer_peek_token (parser->lexer);
30270 if (token->type == CPP_COMMA)
30271 {
30272 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30273 continue;
30274 }
30275 else
30276 break;
30277 }
30278 return decls;
30279 }
30280
30281 /* Parse an Objective-C @property declaration. The syntax is:
30282
30283 objc-property-declaration:
30284 '@property' objc-property-attributes[opt] struct-declaration ;
30285
30286 objc-property-attributes:
30287 '(' objc-property-attribute-list ')'
30288
30289 objc-property-attribute-list:
30290 objc-property-attribute
30291 objc-property-attribute-list, objc-property-attribute
30292
30293 objc-property-attribute
30294 'getter' = identifier
30295 'setter' = identifier
30296 'readonly'
30297 'readwrite'
30298 'assign'
30299 'retain'
30300 'copy'
30301 'nonatomic'
30302
30303 For example:
30304 @property NSString *name;
30305 @property (readonly) id object;
30306 @property (retain, nonatomic, getter=getTheName) id name;
30307 @property int a, b, c;
30308
30309 PS: This function is identical to
30310 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30311 static void
30312 cp_parser_objc_at_property_declaration (cp_parser *parser)
30313 {
30314 /* The following variables hold the attributes of the properties as
30315 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30316 seen. When we see an attribute, we set them to 'true' (if they
30317 are boolean properties) or to the identifier (if they have an
30318 argument, ie, for getter and setter). Note that here we only
30319 parse the list of attributes, check the syntax and accumulate the
30320 attributes that we find. objc_add_property_declaration() will
30321 then process the information. */
30322 bool property_assign = false;
30323 bool property_copy = false;
30324 tree property_getter_ident = NULL_TREE;
30325 bool property_nonatomic = false;
30326 bool property_readonly = false;
30327 bool property_readwrite = false;
30328 bool property_retain = false;
30329 tree property_setter_ident = NULL_TREE;
30330
30331 /* 'properties' is the list of properties that we read. Usually a
30332 single one, but maybe more (eg, in "@property int a, b, c;" there
30333 are three). */
30334 tree properties;
30335 location_t loc;
30336
30337 loc = cp_lexer_peek_token (parser->lexer)->location;
30338
30339 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30340
30341 /* Parse the optional attribute list... */
30342 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30343 {
30344 /* Eat the '('. */
30345 cp_lexer_consume_token (parser->lexer);
30346
30347 while (true)
30348 {
30349 bool syntax_error = false;
30350 cp_token *token = cp_lexer_peek_token (parser->lexer);
30351 enum rid keyword;
30352
30353 if (token->type != CPP_NAME)
30354 {
30355 cp_parser_error (parser, "expected identifier");
30356 break;
30357 }
30358 keyword = C_RID_CODE (token->u.value);
30359 cp_lexer_consume_token (parser->lexer);
30360 switch (keyword)
30361 {
30362 case RID_ASSIGN: property_assign = true; break;
30363 case RID_COPY: property_copy = true; break;
30364 case RID_NONATOMIC: property_nonatomic = true; break;
30365 case RID_READONLY: property_readonly = true; break;
30366 case RID_READWRITE: property_readwrite = true; break;
30367 case RID_RETAIN: property_retain = true; break;
30368
30369 case RID_GETTER:
30370 case RID_SETTER:
30371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30372 {
30373 if (keyword == RID_GETTER)
30374 cp_parser_error (parser,
30375 "missing %<=%> (after %<getter%> attribute)");
30376 else
30377 cp_parser_error (parser,
30378 "missing %<=%> (after %<setter%> attribute)");
30379 syntax_error = true;
30380 break;
30381 }
30382 cp_lexer_consume_token (parser->lexer); /* eat the = */
30383 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30384 {
30385 cp_parser_error (parser, "expected identifier");
30386 syntax_error = true;
30387 break;
30388 }
30389 if (keyword == RID_SETTER)
30390 {
30391 if (property_setter_ident != NULL_TREE)
30392 {
30393 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30394 cp_lexer_consume_token (parser->lexer);
30395 }
30396 else
30397 property_setter_ident = cp_parser_objc_selector (parser);
30398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30399 cp_parser_error (parser, "setter name must terminate with %<:%>");
30400 else
30401 cp_lexer_consume_token (parser->lexer);
30402 }
30403 else
30404 {
30405 if (property_getter_ident != NULL_TREE)
30406 {
30407 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30408 cp_lexer_consume_token (parser->lexer);
30409 }
30410 else
30411 property_getter_ident = cp_parser_objc_selector (parser);
30412 }
30413 break;
30414 default:
30415 cp_parser_error (parser, "unknown property attribute");
30416 syntax_error = true;
30417 break;
30418 }
30419
30420 if (syntax_error)
30421 break;
30422
30423 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30424 cp_lexer_consume_token (parser->lexer);
30425 else
30426 break;
30427 }
30428
30429 /* FIXME: "@property (setter, assign);" will generate a spurious
30430 "error: expected ‘)’ before ‘,’ token". This is because
30431 cp_parser_require, unlike the C counterpart, will produce an
30432 error even if we are in error recovery. */
30433 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30434 {
30435 cp_parser_skip_to_closing_parenthesis (parser,
30436 /*recovering=*/true,
30437 /*or_comma=*/false,
30438 /*consume_paren=*/true);
30439 }
30440 }
30441
30442 /* ... and the property declaration(s). */
30443 properties = cp_parser_objc_struct_declaration (parser);
30444
30445 if (properties == error_mark_node)
30446 {
30447 cp_parser_skip_to_end_of_statement (parser);
30448 /* If the next token is now a `;', consume it. */
30449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30450 cp_lexer_consume_token (parser->lexer);
30451 return;
30452 }
30453
30454 if (properties == NULL_TREE)
30455 cp_parser_error (parser, "expected identifier");
30456 else
30457 {
30458 /* Comma-separated properties are chained together in
30459 reverse order; add them one by one. */
30460 properties = nreverse (properties);
30461
30462 for (; properties; properties = TREE_CHAIN (properties))
30463 objc_add_property_declaration (loc, copy_node (properties),
30464 property_readonly, property_readwrite,
30465 property_assign, property_retain,
30466 property_copy, property_nonatomic,
30467 property_getter_ident, property_setter_ident);
30468 }
30469
30470 cp_parser_consume_semicolon_at_end_of_statement (parser);
30471 }
30472
30473 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30474
30475 objc-synthesize-declaration:
30476 @synthesize objc-synthesize-identifier-list ;
30477
30478 objc-synthesize-identifier-list:
30479 objc-synthesize-identifier
30480 objc-synthesize-identifier-list, objc-synthesize-identifier
30481
30482 objc-synthesize-identifier
30483 identifier
30484 identifier = identifier
30485
30486 For example:
30487 @synthesize MyProperty;
30488 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30489
30490 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30491 for C. Keep them in sync.
30492 */
30493 static void
30494 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30495 {
30496 tree list = NULL_TREE;
30497 location_t loc;
30498 loc = cp_lexer_peek_token (parser->lexer)->location;
30499
30500 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30501 while (true)
30502 {
30503 tree property, ivar;
30504 property = cp_parser_identifier (parser);
30505 if (property == error_mark_node)
30506 {
30507 cp_parser_consume_semicolon_at_end_of_statement (parser);
30508 return;
30509 }
30510 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30511 {
30512 cp_lexer_consume_token (parser->lexer);
30513 ivar = cp_parser_identifier (parser);
30514 if (ivar == error_mark_node)
30515 {
30516 cp_parser_consume_semicolon_at_end_of_statement (parser);
30517 return;
30518 }
30519 }
30520 else
30521 ivar = NULL_TREE;
30522 list = chainon (list, build_tree_list (ivar, property));
30523 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30524 cp_lexer_consume_token (parser->lexer);
30525 else
30526 break;
30527 }
30528 cp_parser_consume_semicolon_at_end_of_statement (parser);
30529 objc_add_synthesize_declaration (loc, list);
30530 }
30531
30532 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30533
30534 objc-dynamic-declaration:
30535 @dynamic identifier-list ;
30536
30537 For example:
30538 @dynamic MyProperty;
30539 @dynamic MyProperty, AnotherProperty;
30540
30541 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30542 for C. Keep them in sync.
30543 */
30544 static void
30545 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30546 {
30547 tree list = NULL_TREE;
30548 location_t loc;
30549 loc = cp_lexer_peek_token (parser->lexer)->location;
30550
30551 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30552 while (true)
30553 {
30554 tree property;
30555 property = cp_parser_identifier (parser);
30556 if (property == error_mark_node)
30557 {
30558 cp_parser_consume_semicolon_at_end_of_statement (parser);
30559 return;
30560 }
30561 list = chainon (list, build_tree_list (NULL, property));
30562 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30563 cp_lexer_consume_token (parser->lexer);
30564 else
30565 break;
30566 }
30567 cp_parser_consume_semicolon_at_end_of_statement (parser);
30568 objc_add_dynamic_declaration (loc, list);
30569 }
30570
30571 \f
30572 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30573
30574 /* Returns name of the next clause.
30575 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30576 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30577 returned and the token is consumed. */
30578
30579 static pragma_omp_clause
30580 cp_parser_omp_clause_name (cp_parser *parser)
30581 {
30582 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30583
30584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30585 result = PRAGMA_OACC_CLAUSE_AUTO;
30586 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30587 result = PRAGMA_OMP_CLAUSE_IF;
30588 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30589 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30590 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30591 result = PRAGMA_OACC_CLAUSE_DELETE;
30592 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30593 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30594 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30595 result = PRAGMA_OMP_CLAUSE_FOR;
30596 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30597 {
30598 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30599 const char *p = IDENTIFIER_POINTER (id);
30600
30601 switch (p[0])
30602 {
30603 case 'a':
30604 if (!strcmp ("aligned", p))
30605 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30606 else if (!strcmp ("async", p))
30607 result = PRAGMA_OACC_CLAUSE_ASYNC;
30608 break;
30609 case 'c':
30610 if (!strcmp ("collapse", p))
30611 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30612 else if (!strcmp ("copy", p))
30613 result = PRAGMA_OACC_CLAUSE_COPY;
30614 else if (!strcmp ("copyin", p))
30615 result = PRAGMA_OMP_CLAUSE_COPYIN;
30616 else if (!strcmp ("copyout", p))
30617 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30618 else if (!strcmp ("copyprivate", p))
30619 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30620 else if (!strcmp ("create", p))
30621 result = PRAGMA_OACC_CLAUSE_CREATE;
30622 break;
30623 case 'd':
30624 if (!strcmp ("defaultmap", p))
30625 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30626 else if (!strcmp ("depend", p))
30627 result = PRAGMA_OMP_CLAUSE_DEPEND;
30628 else if (!strcmp ("device", p))
30629 result = PRAGMA_OMP_CLAUSE_DEVICE;
30630 else if (!strcmp ("deviceptr", p))
30631 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30632 else if (!strcmp ("device_resident", p))
30633 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30634 else if (!strcmp ("dist_schedule", p))
30635 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30636 break;
30637 case 'f':
30638 if (!strcmp ("final", p))
30639 result = PRAGMA_OMP_CLAUSE_FINAL;
30640 else if (!strcmp ("firstprivate", p))
30641 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30642 else if (!strcmp ("from", p))
30643 result = PRAGMA_OMP_CLAUSE_FROM;
30644 break;
30645 case 'g':
30646 if (!strcmp ("gang", p))
30647 result = PRAGMA_OACC_CLAUSE_GANG;
30648 else if (!strcmp ("grainsize", p))
30649 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30650 break;
30651 case 'h':
30652 if (!strcmp ("hint", p))
30653 result = PRAGMA_OMP_CLAUSE_HINT;
30654 else if (!strcmp ("host", p))
30655 result = PRAGMA_OACC_CLAUSE_HOST;
30656 break;
30657 case 'i':
30658 if (!strcmp ("inbranch", p))
30659 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30660 else if (!strcmp ("independent", p))
30661 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30662 else if (!strcmp ("is_device_ptr", p))
30663 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30664 break;
30665 case 'l':
30666 if (!strcmp ("lastprivate", p))
30667 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30668 else if (!strcmp ("linear", p))
30669 result = PRAGMA_OMP_CLAUSE_LINEAR;
30670 else if (!strcmp ("link", p))
30671 result = PRAGMA_OMP_CLAUSE_LINK;
30672 break;
30673 case 'm':
30674 if (!strcmp ("map", p))
30675 result = PRAGMA_OMP_CLAUSE_MAP;
30676 else if (!strcmp ("mergeable", p))
30677 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30678 else if (flag_cilkplus && !strcmp ("mask", p))
30679 result = PRAGMA_CILK_CLAUSE_MASK;
30680 break;
30681 case 'n':
30682 if (!strcmp ("nogroup", p))
30683 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30684 else if (!strcmp ("notinbranch", p))
30685 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30686 else if (!strcmp ("nowait", p))
30687 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30688 else if (flag_cilkplus && !strcmp ("nomask", p))
30689 result = PRAGMA_CILK_CLAUSE_NOMASK;
30690 else if (!strcmp ("num_gangs", p))
30691 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30692 else if (!strcmp ("num_tasks", p))
30693 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30694 else if (!strcmp ("num_teams", p))
30695 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30696 else if (!strcmp ("num_threads", p))
30697 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30698 else if (!strcmp ("num_workers", p))
30699 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30700 break;
30701 case 'o':
30702 if (!strcmp ("ordered", p))
30703 result = PRAGMA_OMP_CLAUSE_ORDERED;
30704 break;
30705 case 'p':
30706 if (!strcmp ("parallel", p))
30707 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30708 else if (!strcmp ("present", p))
30709 result = PRAGMA_OACC_CLAUSE_PRESENT;
30710 else if (!strcmp ("present_or_copy", p)
30711 || !strcmp ("pcopy", p))
30712 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30713 else if (!strcmp ("present_or_copyin", p)
30714 || !strcmp ("pcopyin", p))
30715 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30716 else if (!strcmp ("present_or_copyout", p)
30717 || !strcmp ("pcopyout", p))
30718 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30719 else if (!strcmp ("present_or_create", p)
30720 || !strcmp ("pcreate", p))
30721 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30722 else if (!strcmp ("priority", p))
30723 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30724 else if (!strcmp ("proc_bind", p))
30725 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30726 break;
30727 case 'r':
30728 if (!strcmp ("reduction", p))
30729 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30730 break;
30731 case 's':
30732 if (!strcmp ("safelen", p))
30733 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30734 else if (!strcmp ("schedule", p))
30735 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30736 else if (!strcmp ("sections", p))
30737 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30738 else if (!strcmp ("self", p))
30739 result = PRAGMA_OACC_CLAUSE_SELF;
30740 else if (!strcmp ("seq", p))
30741 result = PRAGMA_OACC_CLAUSE_SEQ;
30742 else if (!strcmp ("shared", p))
30743 result = PRAGMA_OMP_CLAUSE_SHARED;
30744 else if (!strcmp ("simd", p))
30745 result = PRAGMA_OMP_CLAUSE_SIMD;
30746 else if (!strcmp ("simdlen", p))
30747 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30748 break;
30749 case 't':
30750 if (!strcmp ("taskgroup", p))
30751 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30752 else if (!strcmp ("thread_limit", p))
30753 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30754 else if (!strcmp ("threads", p))
30755 result = PRAGMA_OMP_CLAUSE_THREADS;
30756 else if (!strcmp ("tile", p))
30757 result = PRAGMA_OACC_CLAUSE_TILE;
30758 else if (!strcmp ("to", p))
30759 result = PRAGMA_OMP_CLAUSE_TO;
30760 break;
30761 case 'u':
30762 if (!strcmp ("uniform", p))
30763 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30764 else if (!strcmp ("untied", p))
30765 result = PRAGMA_OMP_CLAUSE_UNTIED;
30766 else if (!strcmp ("use_device", p))
30767 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30768 else if (!strcmp ("use_device_ptr", p))
30769 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30770 break;
30771 case 'v':
30772 if (!strcmp ("vector", p))
30773 result = PRAGMA_OACC_CLAUSE_VECTOR;
30774 else if (!strcmp ("vector_length", p))
30775 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30776 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30777 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30778 break;
30779 case 'w':
30780 if (!strcmp ("wait", p))
30781 result = PRAGMA_OACC_CLAUSE_WAIT;
30782 else if (!strcmp ("worker", p))
30783 result = PRAGMA_OACC_CLAUSE_WORKER;
30784 break;
30785 }
30786 }
30787
30788 if (result != PRAGMA_OMP_CLAUSE_NONE)
30789 cp_lexer_consume_token (parser->lexer);
30790
30791 return result;
30792 }
30793
30794 /* Validate that a clause of the given type does not already exist. */
30795
30796 static void
30797 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30798 const char *name, location_t location)
30799 {
30800 tree c;
30801
30802 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30803 if (OMP_CLAUSE_CODE (c) == code)
30804 {
30805 error_at (location, "too many %qs clauses", name);
30806 break;
30807 }
30808 }
30809
30810 /* OpenMP 2.5:
30811 variable-list:
30812 identifier
30813 variable-list , identifier
30814
30815 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30816 colon). An opening parenthesis will have been consumed by the caller.
30817
30818 If KIND is nonzero, create the appropriate node and install the decl
30819 in OMP_CLAUSE_DECL and add the node to the head of the list.
30820
30821 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30822 return the list created.
30823
30824 COLON can be NULL if only closing parenthesis should end the list,
30825 or pointer to bool which will receive false if the list is terminated
30826 by closing parenthesis or true if the list is terminated by colon. */
30827
30828 static tree
30829 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30830 tree list, bool *colon)
30831 {
30832 cp_token *token;
30833 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30834 if (colon)
30835 {
30836 parser->colon_corrects_to_scope_p = false;
30837 *colon = false;
30838 }
30839 while (1)
30840 {
30841 tree name, decl;
30842
30843 token = cp_lexer_peek_token (parser->lexer);
30844 if (kind != 0
30845 && current_class_ptr
30846 && cp_parser_is_keyword (token, RID_THIS))
30847 {
30848 decl = finish_this_expr ();
30849 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30850 || CONVERT_EXPR_P (decl))
30851 decl = TREE_OPERAND (decl, 0);
30852 cp_lexer_consume_token (parser->lexer);
30853 }
30854 else
30855 {
30856 name = cp_parser_id_expression (parser, /*template_p=*/false,
30857 /*check_dependency_p=*/true,
30858 /*template_p=*/NULL,
30859 /*declarator_p=*/false,
30860 /*optional_p=*/false);
30861 if (name == error_mark_node)
30862 goto skip_comma;
30863
30864 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30865 if (decl == error_mark_node)
30866 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30867 token->location);
30868 }
30869 if (decl == error_mark_node)
30870 ;
30871 else if (kind != 0)
30872 {
30873 switch (kind)
30874 {
30875 case OMP_CLAUSE__CACHE_:
30876 /* The OpenACC cache directive explicitly only allows "array
30877 elements or subarrays". */
30878 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30879 {
30880 error_at (token->location, "expected %<[%>");
30881 decl = error_mark_node;
30882 break;
30883 }
30884 /* FALLTHROUGH. */
30885 case OMP_CLAUSE_MAP:
30886 case OMP_CLAUSE_FROM:
30887 case OMP_CLAUSE_TO:
30888 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30889 {
30890 location_t loc
30891 = cp_lexer_peek_token (parser->lexer)->location;
30892 cp_id_kind idk = CP_ID_KIND_NONE;
30893 cp_lexer_consume_token (parser->lexer);
30894 decl = convert_from_reference (decl);
30895 decl
30896 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30897 decl, false,
30898 &idk, loc);
30899 }
30900 /* FALLTHROUGH. */
30901 case OMP_CLAUSE_DEPEND:
30902 case OMP_CLAUSE_REDUCTION:
30903 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30904 {
30905 tree low_bound = NULL_TREE, length = NULL_TREE;
30906
30907 parser->colon_corrects_to_scope_p = false;
30908 cp_lexer_consume_token (parser->lexer);
30909 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30910 low_bound = cp_parser_expression (parser);
30911 if (!colon)
30912 parser->colon_corrects_to_scope_p
30913 = saved_colon_corrects_to_scope_p;
30914 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30915 length = integer_one_node;
30916 else
30917 {
30918 /* Look for `:'. */
30919 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30920 goto skip_comma;
30921 if (!cp_lexer_next_token_is (parser->lexer,
30922 CPP_CLOSE_SQUARE))
30923 length = cp_parser_expression (parser);
30924 }
30925 /* Look for the closing `]'. */
30926 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30927 RT_CLOSE_SQUARE))
30928 goto skip_comma;
30929
30930 decl = tree_cons (low_bound, length, decl);
30931 }
30932 break;
30933 default:
30934 break;
30935 }
30936
30937 tree u = build_omp_clause (token->location, kind);
30938 OMP_CLAUSE_DECL (u) = decl;
30939 OMP_CLAUSE_CHAIN (u) = list;
30940 list = u;
30941 }
30942 else
30943 list = tree_cons (decl, NULL_TREE, list);
30944
30945 get_comma:
30946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30947 break;
30948 cp_lexer_consume_token (parser->lexer);
30949 }
30950
30951 if (colon)
30952 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30953
30954 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30955 {
30956 *colon = true;
30957 cp_parser_require (parser, CPP_COLON, RT_COLON);
30958 return list;
30959 }
30960
30961 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30962 {
30963 int ending;
30964
30965 /* Try to resync to an unnested comma. Copied from
30966 cp_parser_parenthesized_expression_list. */
30967 skip_comma:
30968 if (colon)
30969 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30970 ending = cp_parser_skip_to_closing_parenthesis (parser,
30971 /*recovering=*/true,
30972 /*or_comma=*/true,
30973 /*consume_paren=*/true);
30974 if (ending < 0)
30975 goto get_comma;
30976 }
30977
30978 return list;
30979 }
30980
30981 /* Similarly, but expect leading and trailing parenthesis. This is a very
30982 common case for omp clauses. */
30983
30984 static tree
30985 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30986 {
30987 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30988 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30989 return list;
30990 }
30991
30992 /* OpenACC 2.0:
30993 copy ( variable-list )
30994 copyin ( variable-list )
30995 copyout ( variable-list )
30996 create ( variable-list )
30997 delete ( variable-list )
30998 present ( variable-list )
30999 present_or_copy ( variable-list )
31000 pcopy ( variable-list )
31001 present_or_copyin ( variable-list )
31002 pcopyin ( variable-list )
31003 present_or_copyout ( variable-list )
31004 pcopyout ( variable-list )
31005 present_or_create ( variable-list )
31006 pcreate ( variable-list ) */
31007
31008 static tree
31009 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31010 tree list)
31011 {
31012 enum gomp_map_kind kind;
31013 switch (c_kind)
31014 {
31015 case PRAGMA_OACC_CLAUSE_COPY:
31016 kind = GOMP_MAP_FORCE_TOFROM;
31017 break;
31018 case PRAGMA_OACC_CLAUSE_COPYIN:
31019 kind = GOMP_MAP_FORCE_TO;
31020 break;
31021 case PRAGMA_OACC_CLAUSE_COPYOUT:
31022 kind = GOMP_MAP_FORCE_FROM;
31023 break;
31024 case PRAGMA_OACC_CLAUSE_CREATE:
31025 kind = GOMP_MAP_FORCE_ALLOC;
31026 break;
31027 case PRAGMA_OACC_CLAUSE_DELETE:
31028 kind = GOMP_MAP_DELETE;
31029 break;
31030 case PRAGMA_OACC_CLAUSE_DEVICE:
31031 kind = GOMP_MAP_FORCE_TO;
31032 break;
31033 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31034 kind = GOMP_MAP_DEVICE_RESIDENT;
31035 break;
31036 case PRAGMA_OACC_CLAUSE_HOST:
31037 case PRAGMA_OACC_CLAUSE_SELF:
31038 kind = GOMP_MAP_FORCE_FROM;
31039 break;
31040 case PRAGMA_OACC_CLAUSE_LINK:
31041 kind = GOMP_MAP_LINK;
31042 break;
31043 case PRAGMA_OACC_CLAUSE_PRESENT:
31044 kind = GOMP_MAP_FORCE_PRESENT;
31045 break;
31046 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31047 kind = GOMP_MAP_TOFROM;
31048 break;
31049 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31050 kind = GOMP_MAP_TO;
31051 break;
31052 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31053 kind = GOMP_MAP_FROM;
31054 break;
31055 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31056 kind = GOMP_MAP_ALLOC;
31057 break;
31058 default:
31059 gcc_unreachable ();
31060 }
31061 tree nl, c;
31062 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31063
31064 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31065 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31066
31067 return nl;
31068 }
31069
31070 /* OpenACC 2.0:
31071 deviceptr ( variable-list ) */
31072
31073 static tree
31074 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31075 {
31076 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31077 tree vars, t;
31078
31079 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31080 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31081 variable-list must only allow for pointer variables. */
31082 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31083 for (t = vars; t; t = TREE_CHAIN (t))
31084 {
31085 tree v = TREE_PURPOSE (t);
31086 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31087 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31088 OMP_CLAUSE_DECL (u) = v;
31089 OMP_CLAUSE_CHAIN (u) = list;
31090 list = u;
31091 }
31092
31093 return list;
31094 }
31095
31096 /* OpenACC 2.0:
31097 auto
31098 independent
31099 nohost
31100 seq */
31101
31102 static tree
31103 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31104 enum omp_clause_code code,
31105 tree list, location_t location)
31106 {
31107 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31108 tree c = build_omp_clause (location, code);
31109 OMP_CLAUSE_CHAIN (c) = list;
31110 return c;
31111 }
31112
31113 /* OpenACC:
31114 num_gangs ( expression )
31115 num_workers ( expression )
31116 vector_length ( expression ) */
31117
31118 static tree
31119 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31120 const char *str, tree list)
31121 {
31122 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31123
31124 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31125 return list;
31126
31127 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31128
31129 if (t == error_mark_node
31130 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31131 {
31132 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31133 /*or_comma=*/false,
31134 /*consume_paren=*/true);
31135 return list;
31136 }
31137
31138 check_no_duplicate_clause (list, code, str, loc);
31139
31140 tree c = build_omp_clause (loc, code);
31141 OMP_CLAUSE_OPERAND (c, 0) = t;
31142 OMP_CLAUSE_CHAIN (c) = list;
31143 return c;
31144 }
31145
31146 /* OpenACC:
31147
31148 gang [( gang-arg-list )]
31149 worker [( [num:] int-expr )]
31150 vector [( [length:] int-expr )]
31151
31152 where gang-arg is one of:
31153
31154 [num:] int-expr
31155 static: size-expr
31156
31157 and size-expr may be:
31158
31159 *
31160 int-expr
31161 */
31162
31163 static tree
31164 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31165 const char *str, tree list)
31166 {
31167 const char *id = "num";
31168 cp_lexer *lexer = parser->lexer;
31169 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31170 location_t loc = cp_lexer_peek_token (lexer)->location;
31171
31172 if (kind == OMP_CLAUSE_VECTOR)
31173 id = "length";
31174
31175 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31176 {
31177 cp_lexer_consume_token (lexer);
31178
31179 do
31180 {
31181 cp_token *next = cp_lexer_peek_token (lexer);
31182 int idx = 0;
31183
31184 /* Gang static argument. */
31185 if (kind == OMP_CLAUSE_GANG
31186 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31187 {
31188 cp_lexer_consume_token (lexer);
31189
31190 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31191 goto cleanup_error;
31192
31193 idx = 1;
31194 if (ops[idx] != NULL)
31195 {
31196 cp_parser_error (parser, "too many %<static%> arguments");
31197 goto cleanup_error;
31198 }
31199
31200 /* Check for the '*' argument. */
31201 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31202 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31203 || cp_lexer_nth_token_is (parser->lexer, 2,
31204 CPP_CLOSE_PAREN)))
31205 {
31206 cp_lexer_consume_token (lexer);
31207 ops[idx] = integer_minus_one_node;
31208
31209 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31210 {
31211 cp_lexer_consume_token (lexer);
31212 continue;
31213 }
31214 else break;
31215 }
31216 }
31217 /* Worker num: argument and vector length: arguments. */
31218 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31219 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31220 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31221 {
31222 cp_lexer_consume_token (lexer); /* id */
31223 cp_lexer_consume_token (lexer); /* ':' */
31224 }
31225
31226 /* Now collect the actual argument. */
31227 if (ops[idx] != NULL_TREE)
31228 {
31229 cp_parser_error (parser, "unexpected argument");
31230 goto cleanup_error;
31231 }
31232
31233 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31234 false);
31235 if (expr == error_mark_node)
31236 goto cleanup_error;
31237
31238 mark_exp_read (expr);
31239 ops[idx] = expr;
31240
31241 if (kind == OMP_CLAUSE_GANG
31242 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31243 {
31244 cp_lexer_consume_token (lexer);
31245 continue;
31246 }
31247 break;
31248 }
31249 while (1);
31250
31251 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31252 goto cleanup_error;
31253 }
31254
31255 check_no_duplicate_clause (list, kind, str, loc);
31256
31257 c = build_omp_clause (loc, kind);
31258
31259 if (ops[1])
31260 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31261
31262 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31263 OMP_CLAUSE_CHAIN (c) = list;
31264
31265 return c;
31266
31267 cleanup_error:
31268 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31269 return list;
31270 }
31271
31272 /* OpenACC 2.0:
31273 tile ( size-expr-list ) */
31274
31275 static tree
31276 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31277 {
31278 tree c, expr = error_mark_node;
31279 tree tile = NULL_TREE;
31280
31281 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31282 so, but the spec authors never considered such a case and have
31283 differing opinions on what it might mean, including 'not
31284 allowed'.) */
31285 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31286 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31287 clause_loc);
31288
31289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31290 return list;
31291
31292 do
31293 {
31294 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31295 return list;
31296
31297 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31298 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31299 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31300 {
31301 cp_lexer_consume_token (parser->lexer);
31302 expr = integer_zero_node;
31303 }
31304 else
31305 expr = cp_parser_constant_expression (parser);
31306
31307 tile = tree_cons (NULL_TREE, expr, tile);
31308 }
31309 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31310
31311 /* Consume the trailing ')'. */
31312 cp_lexer_consume_token (parser->lexer);
31313
31314 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31315 tile = nreverse (tile);
31316 OMP_CLAUSE_TILE_LIST (c) = tile;
31317 OMP_CLAUSE_CHAIN (c) = list;
31318 return c;
31319 }
31320
31321 /* OpenACC 2.0
31322 Parse wait clause or directive parameters. */
31323
31324 static tree
31325 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31326 {
31327 vec<tree, va_gc> *args;
31328 tree t, args_tree;
31329
31330 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31331 /*cast_p=*/false,
31332 /*allow_expansion_p=*/true,
31333 /*non_constant_p=*/NULL);
31334
31335 if (args == NULL || args->length () == 0)
31336 {
31337 cp_parser_error (parser, "expected integer expression before ')'");
31338 if (args != NULL)
31339 release_tree_vector (args);
31340 return list;
31341 }
31342
31343 args_tree = build_tree_list_vec (args);
31344
31345 release_tree_vector (args);
31346
31347 for (t = args_tree; t; t = TREE_CHAIN (t))
31348 {
31349 tree targ = TREE_VALUE (t);
31350
31351 if (targ != error_mark_node)
31352 {
31353 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31354 error ("%<wait%> expression must be integral");
31355 else
31356 {
31357 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31358
31359 mark_rvalue_use (targ);
31360 OMP_CLAUSE_DECL (c) = targ;
31361 OMP_CLAUSE_CHAIN (c) = list;
31362 list = c;
31363 }
31364 }
31365 }
31366
31367 return list;
31368 }
31369
31370 /* OpenACC:
31371 wait ( int-expr-list ) */
31372
31373 static tree
31374 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31375 {
31376 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31377
31378 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31379 return list;
31380
31381 list = cp_parser_oacc_wait_list (parser, location, list);
31382
31383 return list;
31384 }
31385
31386 /* OpenMP 3.0:
31387 collapse ( constant-expression ) */
31388
31389 static tree
31390 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31391 {
31392 tree c, num;
31393 location_t loc;
31394 HOST_WIDE_INT n;
31395
31396 loc = cp_lexer_peek_token (parser->lexer)->location;
31397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31398 return list;
31399
31400 num = cp_parser_constant_expression (parser);
31401
31402 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31403 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31404 /*or_comma=*/false,
31405 /*consume_paren=*/true);
31406
31407 if (num == error_mark_node)
31408 return list;
31409 num = fold_non_dependent_expr (num);
31410 if (!tree_fits_shwi_p (num)
31411 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31412 || (n = tree_to_shwi (num)) <= 0
31413 || (int) n != n)
31414 {
31415 error_at (loc, "collapse argument needs positive constant integer expression");
31416 return list;
31417 }
31418
31419 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31420 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31421 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31422 OMP_CLAUSE_CHAIN (c) = list;
31423 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31424
31425 return c;
31426 }
31427
31428 /* OpenMP 2.5:
31429 default ( shared | none )
31430
31431 OpenACC 2.0
31432 default (none) */
31433
31434 static tree
31435 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31436 location_t location, bool is_oacc)
31437 {
31438 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31439 tree c;
31440
31441 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31442 return list;
31443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31444 {
31445 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31446 const char *p = IDENTIFIER_POINTER (id);
31447
31448 switch (p[0])
31449 {
31450 case 'n':
31451 if (strcmp ("none", p) != 0)
31452 goto invalid_kind;
31453 kind = OMP_CLAUSE_DEFAULT_NONE;
31454 break;
31455
31456 case 's':
31457 if (strcmp ("shared", p) != 0 || is_oacc)
31458 goto invalid_kind;
31459 kind = OMP_CLAUSE_DEFAULT_SHARED;
31460 break;
31461
31462 default:
31463 goto invalid_kind;
31464 }
31465
31466 cp_lexer_consume_token (parser->lexer);
31467 }
31468 else
31469 {
31470 invalid_kind:
31471 if (is_oacc)
31472 cp_parser_error (parser, "expected %<none%>");
31473 else
31474 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31475 }
31476
31477 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31478 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31479 /*or_comma=*/false,
31480 /*consume_paren=*/true);
31481
31482 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31483 return list;
31484
31485 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31486 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31487 OMP_CLAUSE_CHAIN (c) = list;
31488 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31489
31490 return c;
31491 }
31492
31493 /* OpenMP 3.1:
31494 final ( expression ) */
31495
31496 static tree
31497 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31498 {
31499 tree t, c;
31500
31501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31502 return list;
31503
31504 t = cp_parser_condition (parser);
31505
31506 if (t == error_mark_node
31507 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31509 /*or_comma=*/false,
31510 /*consume_paren=*/true);
31511
31512 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31513
31514 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31515 OMP_CLAUSE_FINAL_EXPR (c) = t;
31516 OMP_CLAUSE_CHAIN (c) = list;
31517
31518 return c;
31519 }
31520
31521 /* OpenMP 2.5:
31522 if ( expression )
31523
31524 OpenMP 4.5:
31525 if ( directive-name-modifier : expression )
31526
31527 directive-name-modifier:
31528 parallel | task | taskloop | target data | target | target update
31529 | target enter data | target exit data */
31530
31531 static tree
31532 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31533 bool is_omp)
31534 {
31535 tree t, c;
31536 enum tree_code if_modifier = ERROR_MARK;
31537
31538 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31539 return list;
31540
31541 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31542 {
31543 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31544 const char *p = IDENTIFIER_POINTER (id);
31545 int n = 2;
31546
31547 if (strcmp ("parallel", p) == 0)
31548 if_modifier = OMP_PARALLEL;
31549 else if (strcmp ("task", p) == 0)
31550 if_modifier = OMP_TASK;
31551 else if (strcmp ("taskloop", p) == 0)
31552 if_modifier = OMP_TASKLOOP;
31553 else if (strcmp ("target", p) == 0)
31554 {
31555 if_modifier = OMP_TARGET;
31556 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31557 {
31558 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31559 p = IDENTIFIER_POINTER (id);
31560 if (strcmp ("data", p) == 0)
31561 if_modifier = OMP_TARGET_DATA;
31562 else if (strcmp ("update", p) == 0)
31563 if_modifier = OMP_TARGET_UPDATE;
31564 else if (strcmp ("enter", p) == 0)
31565 if_modifier = OMP_TARGET_ENTER_DATA;
31566 else if (strcmp ("exit", p) == 0)
31567 if_modifier = OMP_TARGET_EXIT_DATA;
31568 if (if_modifier != OMP_TARGET)
31569 n = 3;
31570 else
31571 {
31572 location_t loc
31573 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31574 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31575 "or %<exit%>");
31576 if_modifier = ERROR_MARK;
31577 }
31578 if (if_modifier == OMP_TARGET_ENTER_DATA
31579 || if_modifier == OMP_TARGET_EXIT_DATA)
31580 {
31581 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31582 {
31583 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31584 p = IDENTIFIER_POINTER (id);
31585 if (strcmp ("data", p) == 0)
31586 n = 4;
31587 }
31588 if (n != 4)
31589 {
31590 location_t loc
31591 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31592 error_at (loc, "expected %<data%>");
31593 if_modifier = ERROR_MARK;
31594 }
31595 }
31596 }
31597 }
31598 if (if_modifier != ERROR_MARK)
31599 {
31600 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31601 {
31602 while (n-- > 0)
31603 cp_lexer_consume_token (parser->lexer);
31604 }
31605 else
31606 {
31607 if (n > 2)
31608 {
31609 location_t loc
31610 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31611 error_at (loc, "expected %<:%>");
31612 }
31613 if_modifier = ERROR_MARK;
31614 }
31615 }
31616 }
31617
31618 t = cp_parser_condition (parser);
31619
31620 if (t == error_mark_node
31621 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31622 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31623 /*or_comma=*/false,
31624 /*consume_paren=*/true);
31625
31626 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31627 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31628 {
31629 if (if_modifier != ERROR_MARK
31630 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31631 {
31632 const char *p = NULL;
31633 switch (if_modifier)
31634 {
31635 case OMP_PARALLEL: p = "parallel"; break;
31636 case OMP_TASK: p = "task"; break;
31637 case OMP_TASKLOOP: p = "taskloop"; break;
31638 case OMP_TARGET_DATA: p = "target data"; break;
31639 case OMP_TARGET: p = "target"; break;
31640 case OMP_TARGET_UPDATE: p = "target update"; break;
31641 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31642 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31643 default: gcc_unreachable ();
31644 }
31645 error_at (location, "too many %<if%> clauses with %qs modifier",
31646 p);
31647 return list;
31648 }
31649 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31650 {
31651 if (!is_omp)
31652 error_at (location, "too many %<if%> clauses");
31653 else
31654 error_at (location, "too many %<if%> clauses without modifier");
31655 return list;
31656 }
31657 else if (if_modifier == ERROR_MARK
31658 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31659 {
31660 error_at (location, "if any %<if%> clause has modifier, then all "
31661 "%<if%> clauses have to use modifier");
31662 return list;
31663 }
31664 }
31665
31666 c = build_omp_clause (location, OMP_CLAUSE_IF);
31667 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31668 OMP_CLAUSE_IF_EXPR (c) = t;
31669 OMP_CLAUSE_CHAIN (c) = list;
31670
31671 return c;
31672 }
31673
31674 /* OpenMP 3.1:
31675 mergeable */
31676
31677 static tree
31678 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31679 tree list, location_t location)
31680 {
31681 tree c;
31682
31683 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31684 location);
31685
31686 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31687 OMP_CLAUSE_CHAIN (c) = list;
31688 return c;
31689 }
31690
31691 /* OpenMP 2.5:
31692 nowait */
31693
31694 static tree
31695 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31696 tree list, location_t location)
31697 {
31698 tree c;
31699
31700 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31701
31702 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31703 OMP_CLAUSE_CHAIN (c) = list;
31704 return c;
31705 }
31706
31707 /* OpenMP 2.5:
31708 num_threads ( expression ) */
31709
31710 static tree
31711 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31712 location_t location)
31713 {
31714 tree t, c;
31715
31716 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31717 return list;
31718
31719 t = cp_parser_expression (parser);
31720
31721 if (t == error_mark_node
31722 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31723 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31724 /*or_comma=*/false,
31725 /*consume_paren=*/true);
31726
31727 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31728 "num_threads", location);
31729
31730 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31731 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31732 OMP_CLAUSE_CHAIN (c) = list;
31733
31734 return c;
31735 }
31736
31737 /* OpenMP 4.5:
31738 num_tasks ( expression ) */
31739
31740 static tree
31741 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31742 location_t location)
31743 {
31744 tree t, c;
31745
31746 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31747 return list;
31748
31749 t = cp_parser_expression (parser);
31750
31751 if (t == error_mark_node
31752 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31753 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31754 /*or_comma=*/false,
31755 /*consume_paren=*/true);
31756
31757 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31758 "num_tasks", location);
31759
31760 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31761 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31762 OMP_CLAUSE_CHAIN (c) = list;
31763
31764 return c;
31765 }
31766
31767 /* OpenMP 4.5:
31768 grainsize ( expression ) */
31769
31770 static tree
31771 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31772 location_t location)
31773 {
31774 tree t, c;
31775
31776 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31777 return list;
31778
31779 t = cp_parser_expression (parser);
31780
31781 if (t == error_mark_node
31782 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31783 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31784 /*or_comma=*/false,
31785 /*consume_paren=*/true);
31786
31787 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31788 "grainsize", location);
31789
31790 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31791 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31792 OMP_CLAUSE_CHAIN (c) = list;
31793
31794 return c;
31795 }
31796
31797 /* OpenMP 4.5:
31798 priority ( expression ) */
31799
31800 static tree
31801 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31802 location_t location)
31803 {
31804 tree t, c;
31805
31806 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31807 return list;
31808
31809 t = cp_parser_expression (parser);
31810
31811 if (t == error_mark_node
31812 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31813 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31814 /*or_comma=*/false,
31815 /*consume_paren=*/true);
31816
31817 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31818 "priority", location);
31819
31820 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31821 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31822 OMP_CLAUSE_CHAIN (c) = list;
31823
31824 return c;
31825 }
31826
31827 /* OpenMP 4.5:
31828 hint ( expression ) */
31829
31830 static tree
31831 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31832 location_t location)
31833 {
31834 tree t, c;
31835
31836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31837 return list;
31838
31839 t = cp_parser_expression (parser);
31840
31841 if (t == error_mark_node
31842 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31843 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31844 /*or_comma=*/false,
31845 /*consume_paren=*/true);
31846
31847 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31848
31849 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31850 OMP_CLAUSE_HINT_EXPR (c) = t;
31851 OMP_CLAUSE_CHAIN (c) = list;
31852
31853 return c;
31854 }
31855
31856 /* OpenMP 4.5:
31857 defaultmap ( tofrom : scalar ) */
31858
31859 static tree
31860 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31861 location_t location)
31862 {
31863 tree c, id;
31864 const char *p;
31865
31866 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31867 return list;
31868
31869 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31870 {
31871 cp_parser_error (parser, "expected %<tofrom%>");
31872 goto out_err;
31873 }
31874 id = cp_lexer_peek_token (parser->lexer)->u.value;
31875 p = IDENTIFIER_POINTER (id);
31876 if (strcmp (p, "tofrom") != 0)
31877 {
31878 cp_parser_error (parser, "expected %<tofrom%>");
31879 goto out_err;
31880 }
31881 cp_lexer_consume_token (parser->lexer);
31882 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31883 goto out_err;
31884
31885 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31886 {
31887 cp_parser_error (parser, "expected %<scalar%>");
31888 goto out_err;
31889 }
31890 id = cp_lexer_peek_token (parser->lexer)->u.value;
31891 p = IDENTIFIER_POINTER (id);
31892 if (strcmp (p, "scalar") != 0)
31893 {
31894 cp_parser_error (parser, "expected %<scalar%>");
31895 goto out_err;
31896 }
31897 cp_lexer_consume_token (parser->lexer);
31898 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31899 goto out_err;
31900
31901 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31902 location);
31903
31904 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31905 OMP_CLAUSE_CHAIN (c) = list;
31906 return c;
31907
31908 out_err:
31909 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31910 /*or_comma=*/false,
31911 /*consume_paren=*/true);
31912 return list;
31913 }
31914
31915 /* OpenMP 2.5:
31916 ordered
31917
31918 OpenMP 4.5:
31919 ordered ( constant-expression ) */
31920
31921 static tree
31922 cp_parser_omp_clause_ordered (cp_parser *parser,
31923 tree list, location_t location)
31924 {
31925 tree c, num = NULL_TREE;
31926 HOST_WIDE_INT n;
31927
31928 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31929 "ordered", location);
31930
31931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31932 {
31933 cp_lexer_consume_token (parser->lexer);
31934
31935 num = cp_parser_constant_expression (parser);
31936
31937 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31938 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31939 /*or_comma=*/false,
31940 /*consume_paren=*/true);
31941
31942 if (num == error_mark_node)
31943 return list;
31944 num = fold_non_dependent_expr (num);
31945 if (!tree_fits_shwi_p (num)
31946 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31947 || (n = tree_to_shwi (num)) <= 0
31948 || (int) n != n)
31949 {
31950 error_at (location,
31951 "ordered argument needs positive constant integer "
31952 "expression");
31953 return list;
31954 }
31955 }
31956
31957 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31958 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31959 OMP_CLAUSE_CHAIN (c) = list;
31960 return c;
31961 }
31962
31963 /* OpenMP 2.5:
31964 reduction ( reduction-operator : variable-list )
31965
31966 reduction-operator:
31967 One of: + * - & ^ | && ||
31968
31969 OpenMP 3.1:
31970
31971 reduction-operator:
31972 One of: + * - & ^ | && || min max
31973
31974 OpenMP 4.0:
31975
31976 reduction-operator:
31977 One of: + * - & ^ | && ||
31978 id-expression */
31979
31980 static tree
31981 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31982 {
31983 enum tree_code code = ERROR_MARK;
31984 tree nlist, c, id = NULL_TREE;
31985
31986 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31987 return list;
31988
31989 switch (cp_lexer_peek_token (parser->lexer)->type)
31990 {
31991 case CPP_PLUS: code = PLUS_EXPR; break;
31992 case CPP_MULT: code = MULT_EXPR; break;
31993 case CPP_MINUS: code = MINUS_EXPR; break;
31994 case CPP_AND: code = BIT_AND_EXPR; break;
31995 case CPP_XOR: code = BIT_XOR_EXPR; break;
31996 case CPP_OR: code = BIT_IOR_EXPR; break;
31997 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31998 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31999 default: break;
32000 }
32001
32002 if (code != ERROR_MARK)
32003 cp_lexer_consume_token (parser->lexer);
32004 else
32005 {
32006 bool saved_colon_corrects_to_scope_p;
32007 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32008 parser->colon_corrects_to_scope_p = false;
32009 id = cp_parser_id_expression (parser, /*template_p=*/false,
32010 /*check_dependency_p=*/true,
32011 /*template_p=*/NULL,
32012 /*declarator_p=*/false,
32013 /*optional_p=*/false);
32014 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32015 if (identifier_p (id))
32016 {
32017 const char *p = IDENTIFIER_POINTER (id);
32018
32019 if (strcmp (p, "min") == 0)
32020 code = MIN_EXPR;
32021 else if (strcmp (p, "max") == 0)
32022 code = MAX_EXPR;
32023 else if (id == cp_operator_id (PLUS_EXPR))
32024 code = PLUS_EXPR;
32025 else if (id == cp_operator_id (MULT_EXPR))
32026 code = MULT_EXPR;
32027 else if (id == cp_operator_id (MINUS_EXPR))
32028 code = MINUS_EXPR;
32029 else if (id == cp_operator_id (BIT_AND_EXPR))
32030 code = BIT_AND_EXPR;
32031 else if (id == cp_operator_id (BIT_IOR_EXPR))
32032 code = BIT_IOR_EXPR;
32033 else if (id == cp_operator_id (BIT_XOR_EXPR))
32034 code = BIT_XOR_EXPR;
32035 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32036 code = TRUTH_ANDIF_EXPR;
32037 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32038 code = TRUTH_ORIF_EXPR;
32039 id = omp_reduction_id (code, id, NULL_TREE);
32040 tree scope = parser->scope;
32041 if (scope)
32042 id = build_qualified_name (NULL_TREE, scope, id, false);
32043 parser->scope = NULL_TREE;
32044 parser->qualifying_scope = NULL_TREE;
32045 parser->object_scope = NULL_TREE;
32046 }
32047 else
32048 {
32049 error ("invalid reduction-identifier");
32050 resync_fail:
32051 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32052 /*or_comma=*/false,
32053 /*consume_paren=*/true);
32054 return list;
32055 }
32056 }
32057
32058 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32059 goto resync_fail;
32060
32061 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32062 NULL);
32063 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32064 {
32065 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32066 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32067 }
32068
32069 return nlist;
32070 }
32071
32072 /* OpenMP 2.5:
32073 schedule ( schedule-kind )
32074 schedule ( schedule-kind , expression )
32075
32076 schedule-kind:
32077 static | dynamic | guided | runtime | auto
32078
32079 OpenMP 4.5:
32080 schedule ( schedule-modifier : schedule-kind )
32081 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32082
32083 schedule-modifier:
32084 simd
32085 monotonic
32086 nonmonotonic */
32087
32088 static tree
32089 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32090 {
32091 tree c, t;
32092 int modifiers = 0, nmodifiers = 0;
32093
32094 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32095 return list;
32096
32097 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32098
32099 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32100 {
32101 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32102 const char *p = IDENTIFIER_POINTER (id);
32103 if (strcmp ("simd", p) == 0)
32104 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32105 else if (strcmp ("monotonic", p) == 0)
32106 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32107 else if (strcmp ("nonmonotonic", p) == 0)
32108 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32109 else
32110 break;
32111 cp_lexer_consume_token (parser->lexer);
32112 if (nmodifiers++ == 0
32113 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32114 cp_lexer_consume_token (parser->lexer);
32115 else
32116 {
32117 cp_parser_require (parser, CPP_COLON, RT_COLON);
32118 break;
32119 }
32120 }
32121
32122 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32123 {
32124 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32125 const char *p = IDENTIFIER_POINTER (id);
32126
32127 switch (p[0])
32128 {
32129 case 'd':
32130 if (strcmp ("dynamic", p) != 0)
32131 goto invalid_kind;
32132 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32133 break;
32134
32135 case 'g':
32136 if (strcmp ("guided", p) != 0)
32137 goto invalid_kind;
32138 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32139 break;
32140
32141 case 'r':
32142 if (strcmp ("runtime", p) != 0)
32143 goto invalid_kind;
32144 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32145 break;
32146
32147 default:
32148 goto invalid_kind;
32149 }
32150 }
32151 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32152 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32153 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32154 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32155 else
32156 goto invalid_kind;
32157 cp_lexer_consume_token (parser->lexer);
32158
32159 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32160 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32161 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32162 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32163 {
32164 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32165 "specified");
32166 modifiers = 0;
32167 }
32168
32169 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32170 {
32171 cp_token *token;
32172 cp_lexer_consume_token (parser->lexer);
32173
32174 token = cp_lexer_peek_token (parser->lexer);
32175 t = cp_parser_assignment_expression (parser);
32176
32177 if (t == error_mark_node)
32178 goto resync_fail;
32179 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32180 error_at (token->location, "schedule %<runtime%> does not take "
32181 "a %<chunk_size%> parameter");
32182 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32183 error_at (token->location, "schedule %<auto%> does not take "
32184 "a %<chunk_size%> parameter");
32185 else
32186 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32187
32188 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32189 goto resync_fail;
32190 }
32191 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32192 goto resync_fail;
32193
32194 OMP_CLAUSE_SCHEDULE_KIND (c)
32195 = (enum omp_clause_schedule_kind)
32196 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32197
32198 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32199 OMP_CLAUSE_CHAIN (c) = list;
32200 return c;
32201
32202 invalid_kind:
32203 cp_parser_error (parser, "invalid schedule kind");
32204 resync_fail:
32205 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32206 /*or_comma=*/false,
32207 /*consume_paren=*/true);
32208 return list;
32209 }
32210
32211 /* OpenMP 3.0:
32212 untied */
32213
32214 static tree
32215 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32216 tree list, location_t location)
32217 {
32218 tree c;
32219
32220 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32221
32222 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32223 OMP_CLAUSE_CHAIN (c) = list;
32224 return c;
32225 }
32226
32227 /* OpenMP 4.0:
32228 inbranch
32229 notinbranch */
32230
32231 static tree
32232 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32233 tree list, location_t location)
32234 {
32235 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32236 tree c = build_omp_clause (location, code);
32237 OMP_CLAUSE_CHAIN (c) = list;
32238 return c;
32239 }
32240
32241 /* OpenMP 4.0:
32242 parallel
32243 for
32244 sections
32245 taskgroup */
32246
32247 static tree
32248 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32249 enum omp_clause_code code,
32250 tree list, location_t location)
32251 {
32252 tree c = build_omp_clause (location, code);
32253 OMP_CLAUSE_CHAIN (c) = list;
32254 return c;
32255 }
32256
32257 /* OpenMP 4.5:
32258 nogroup */
32259
32260 static tree
32261 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32262 tree list, location_t location)
32263 {
32264 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32265 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32266 OMP_CLAUSE_CHAIN (c) = list;
32267 return c;
32268 }
32269
32270 /* OpenMP 4.5:
32271 simd
32272 threads */
32273
32274 static tree
32275 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32276 enum omp_clause_code code,
32277 tree list, location_t location)
32278 {
32279 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32280 tree c = build_omp_clause (location, code);
32281 OMP_CLAUSE_CHAIN (c) = list;
32282 return c;
32283 }
32284
32285 /* OpenMP 4.0:
32286 num_teams ( expression ) */
32287
32288 static tree
32289 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32290 location_t location)
32291 {
32292 tree t, c;
32293
32294 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32295 return list;
32296
32297 t = cp_parser_expression (parser);
32298
32299 if (t == error_mark_node
32300 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32301 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32302 /*or_comma=*/false,
32303 /*consume_paren=*/true);
32304
32305 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32306 "num_teams", location);
32307
32308 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32309 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32310 OMP_CLAUSE_CHAIN (c) = list;
32311
32312 return c;
32313 }
32314
32315 /* OpenMP 4.0:
32316 thread_limit ( expression ) */
32317
32318 static tree
32319 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32320 location_t location)
32321 {
32322 tree t, c;
32323
32324 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32325 return list;
32326
32327 t = cp_parser_expression (parser);
32328
32329 if (t == error_mark_node
32330 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32331 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32332 /*or_comma=*/false,
32333 /*consume_paren=*/true);
32334
32335 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32336 "thread_limit", location);
32337
32338 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32339 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32340 OMP_CLAUSE_CHAIN (c) = list;
32341
32342 return c;
32343 }
32344
32345 /* OpenMP 4.0:
32346 aligned ( variable-list )
32347 aligned ( variable-list : constant-expression ) */
32348
32349 static tree
32350 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32351 {
32352 tree nlist, c, alignment = NULL_TREE;
32353 bool colon;
32354
32355 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32356 return list;
32357
32358 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32359 &colon);
32360
32361 if (colon)
32362 {
32363 alignment = cp_parser_constant_expression (parser);
32364
32365 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32366 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32367 /*or_comma=*/false,
32368 /*consume_paren=*/true);
32369
32370 if (alignment == error_mark_node)
32371 alignment = NULL_TREE;
32372 }
32373
32374 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32375 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32376
32377 return nlist;
32378 }
32379
32380 /* OpenMP 4.0:
32381 linear ( variable-list )
32382 linear ( variable-list : expression )
32383
32384 OpenMP 4.5:
32385 linear ( modifier ( variable-list ) )
32386 linear ( modifier ( variable-list ) : expression ) */
32387
32388 static tree
32389 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32390 bool is_cilk_simd_fn, bool declare_simd)
32391 {
32392 tree nlist, c, step = integer_one_node;
32393 bool colon;
32394 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32395
32396 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32397 return list;
32398
32399 if (!is_cilk_simd_fn
32400 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32401 {
32402 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32403 const char *p = IDENTIFIER_POINTER (id);
32404
32405 if (strcmp ("ref", p) == 0)
32406 kind = OMP_CLAUSE_LINEAR_REF;
32407 else if (strcmp ("val", p) == 0)
32408 kind = OMP_CLAUSE_LINEAR_VAL;
32409 else if (strcmp ("uval", p) == 0)
32410 kind = OMP_CLAUSE_LINEAR_UVAL;
32411 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32412 cp_lexer_consume_token (parser->lexer);
32413 else
32414 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32415 }
32416
32417 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32418 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32419 &colon);
32420 else
32421 {
32422 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32423 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32424 if (colon)
32425 cp_parser_require (parser, CPP_COLON, RT_COLON);
32426 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32428 /*or_comma=*/false,
32429 /*consume_paren=*/true);
32430 }
32431
32432 if (colon)
32433 {
32434 step = NULL_TREE;
32435 if (declare_simd
32436 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32437 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32438 {
32439 cp_token *token = cp_lexer_peek_token (parser->lexer);
32440 cp_parser_parse_tentatively (parser);
32441 step = cp_parser_id_expression (parser, /*template_p=*/false,
32442 /*check_dependency_p=*/true,
32443 /*template_p=*/NULL,
32444 /*declarator_p=*/false,
32445 /*optional_p=*/false);
32446 if (step != error_mark_node)
32447 step = cp_parser_lookup_name_simple (parser, step, token->location);
32448 if (step == error_mark_node)
32449 {
32450 step = NULL_TREE;
32451 cp_parser_abort_tentative_parse (parser);
32452 }
32453 else if (!cp_parser_parse_definitely (parser))
32454 step = NULL_TREE;
32455 }
32456 if (!step)
32457 step = cp_parser_expression (parser);
32458
32459 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32460 {
32461 sorry ("using parameters for %<linear%> step is not supported yet");
32462 step = integer_one_node;
32463 }
32464 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32465 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32466 /*or_comma=*/false,
32467 /*consume_paren=*/true);
32468
32469 if (step == error_mark_node)
32470 return list;
32471 }
32472
32473 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32474 {
32475 OMP_CLAUSE_LINEAR_STEP (c) = step;
32476 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32477 }
32478
32479 return nlist;
32480 }
32481
32482 /* OpenMP 4.0:
32483 safelen ( constant-expression ) */
32484
32485 static tree
32486 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32487 location_t location)
32488 {
32489 tree t, c;
32490
32491 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32492 return list;
32493
32494 t = cp_parser_constant_expression (parser);
32495
32496 if (t == error_mark_node
32497 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32498 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32499 /*or_comma=*/false,
32500 /*consume_paren=*/true);
32501
32502 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32503
32504 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32505 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32506 OMP_CLAUSE_CHAIN (c) = list;
32507
32508 return c;
32509 }
32510
32511 /* OpenMP 4.0:
32512 simdlen ( constant-expression ) */
32513
32514 static tree
32515 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32516 location_t location)
32517 {
32518 tree t, c;
32519
32520 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32521 return list;
32522
32523 t = cp_parser_constant_expression (parser);
32524
32525 if (t == error_mark_node
32526 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32527 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32528 /*or_comma=*/false,
32529 /*consume_paren=*/true);
32530
32531 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32532
32533 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32534 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32535 OMP_CLAUSE_CHAIN (c) = list;
32536
32537 return c;
32538 }
32539
32540 /* OpenMP 4.5:
32541 vec:
32542 identifier [+/- integer]
32543 vec , identifier [+/- integer]
32544 */
32545
32546 static tree
32547 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32548 tree list)
32549 {
32550 tree vec = NULL;
32551
32552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32553 {
32554 cp_parser_error (parser, "expected identifier");
32555 return list;
32556 }
32557
32558 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32559 {
32560 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32561 tree t, identifier = cp_parser_identifier (parser);
32562 tree addend = NULL;
32563
32564 if (identifier == error_mark_node)
32565 t = error_mark_node;
32566 else
32567 {
32568 t = cp_parser_lookup_name_simple
32569 (parser, identifier,
32570 cp_lexer_peek_token (parser->lexer)->location);
32571 if (t == error_mark_node)
32572 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32573 id_loc);
32574 }
32575
32576 bool neg = false;
32577 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32578 neg = true;
32579 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32580 {
32581 addend = integer_zero_node;
32582 goto add_to_vector;
32583 }
32584 cp_lexer_consume_token (parser->lexer);
32585
32586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32587 {
32588 cp_parser_error (parser, "expected integer");
32589 return list;
32590 }
32591
32592 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32593 if (TREE_CODE (addend) != INTEGER_CST)
32594 {
32595 cp_parser_error (parser, "expected integer");
32596 return list;
32597 }
32598 cp_lexer_consume_token (parser->lexer);
32599
32600 add_to_vector:
32601 if (t != error_mark_node)
32602 {
32603 vec = tree_cons (addend, t, vec);
32604 if (neg)
32605 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32606 }
32607
32608 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32609 break;
32610
32611 cp_lexer_consume_token (parser->lexer);
32612 }
32613
32614 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32615 {
32616 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32617 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32618 OMP_CLAUSE_DECL (u) = nreverse (vec);
32619 OMP_CLAUSE_CHAIN (u) = list;
32620 return u;
32621 }
32622 return list;
32623 }
32624
32625 /* OpenMP 4.0:
32626 depend ( depend-kind : variable-list )
32627
32628 depend-kind:
32629 in | out | inout
32630
32631 OpenMP 4.5:
32632 depend ( source )
32633
32634 depend ( sink : vec ) */
32635
32636 static tree
32637 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32638 {
32639 tree nlist, c;
32640 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32641
32642 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32643 return list;
32644
32645 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32646 {
32647 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32648 const char *p = IDENTIFIER_POINTER (id);
32649
32650 if (strcmp ("in", p) == 0)
32651 kind = OMP_CLAUSE_DEPEND_IN;
32652 else if (strcmp ("inout", p) == 0)
32653 kind = OMP_CLAUSE_DEPEND_INOUT;
32654 else if (strcmp ("out", p) == 0)
32655 kind = OMP_CLAUSE_DEPEND_OUT;
32656 else if (strcmp ("source", p) == 0)
32657 kind = OMP_CLAUSE_DEPEND_SOURCE;
32658 else if (strcmp ("sink", p) == 0)
32659 kind = OMP_CLAUSE_DEPEND_SINK;
32660 else
32661 goto invalid_kind;
32662 }
32663 else
32664 goto invalid_kind;
32665
32666 cp_lexer_consume_token (parser->lexer);
32667
32668 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32669 {
32670 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32671 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32672 OMP_CLAUSE_DECL (c) = NULL_TREE;
32673 OMP_CLAUSE_CHAIN (c) = list;
32674 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32675 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32676 /*or_comma=*/false,
32677 /*consume_paren=*/true);
32678 return c;
32679 }
32680
32681 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32682 goto resync_fail;
32683
32684 if (kind == OMP_CLAUSE_DEPEND_SINK)
32685 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32686 else
32687 {
32688 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32689 list, NULL);
32690
32691 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32692 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32693 }
32694 return nlist;
32695
32696 invalid_kind:
32697 cp_parser_error (parser, "invalid depend kind");
32698 resync_fail:
32699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32700 /*or_comma=*/false,
32701 /*consume_paren=*/true);
32702 return list;
32703 }
32704
32705 /* OpenMP 4.0:
32706 map ( map-kind : variable-list )
32707 map ( variable-list )
32708
32709 map-kind:
32710 alloc | to | from | tofrom
32711
32712 OpenMP 4.5:
32713 map-kind:
32714 alloc | to | from | tofrom | release | delete
32715
32716 map ( always [,] map-kind: variable-list ) */
32717
32718 static tree
32719 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32720 {
32721 tree nlist, c;
32722 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32723 bool always = false;
32724
32725 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32726 return list;
32727
32728 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32729 {
32730 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32731 const char *p = IDENTIFIER_POINTER (id);
32732
32733 if (strcmp ("always", p) == 0)
32734 {
32735 int nth = 2;
32736 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32737 nth++;
32738 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32739 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32740 == RID_DELETE))
32741 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32742 == CPP_COLON))
32743 {
32744 always = true;
32745 cp_lexer_consume_token (parser->lexer);
32746 if (nth == 3)
32747 cp_lexer_consume_token (parser->lexer);
32748 }
32749 }
32750 }
32751
32752 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32753 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32754 {
32755 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32756 const char *p = IDENTIFIER_POINTER (id);
32757
32758 if (strcmp ("alloc", p) == 0)
32759 kind = GOMP_MAP_ALLOC;
32760 else if (strcmp ("to", p) == 0)
32761 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32762 else if (strcmp ("from", p) == 0)
32763 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32764 else if (strcmp ("tofrom", p) == 0)
32765 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32766 else if (strcmp ("release", p) == 0)
32767 kind = GOMP_MAP_RELEASE;
32768 else
32769 {
32770 cp_parser_error (parser, "invalid map kind");
32771 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32772 /*or_comma=*/false,
32773 /*consume_paren=*/true);
32774 return list;
32775 }
32776 cp_lexer_consume_token (parser->lexer);
32777 cp_lexer_consume_token (parser->lexer);
32778 }
32779 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32780 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32781 {
32782 kind = GOMP_MAP_DELETE;
32783 cp_lexer_consume_token (parser->lexer);
32784 cp_lexer_consume_token (parser->lexer);
32785 }
32786
32787 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32788 NULL);
32789
32790 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32791 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32792
32793 return nlist;
32794 }
32795
32796 /* OpenMP 4.0:
32797 device ( expression ) */
32798
32799 static tree
32800 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32801 location_t location)
32802 {
32803 tree t, c;
32804
32805 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32806 return list;
32807
32808 t = cp_parser_expression (parser);
32809
32810 if (t == error_mark_node
32811 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32813 /*or_comma=*/false,
32814 /*consume_paren=*/true);
32815
32816 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32817 "device", location);
32818
32819 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32820 OMP_CLAUSE_DEVICE_ID (c) = t;
32821 OMP_CLAUSE_CHAIN (c) = list;
32822
32823 return c;
32824 }
32825
32826 /* OpenMP 4.0:
32827 dist_schedule ( static )
32828 dist_schedule ( static , expression ) */
32829
32830 static tree
32831 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32832 location_t location)
32833 {
32834 tree c, t;
32835
32836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32837 return list;
32838
32839 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32840
32841 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32842 goto invalid_kind;
32843 cp_lexer_consume_token (parser->lexer);
32844
32845 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32846 {
32847 cp_lexer_consume_token (parser->lexer);
32848
32849 t = cp_parser_assignment_expression (parser);
32850
32851 if (t == error_mark_node)
32852 goto resync_fail;
32853 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32854
32855 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32856 goto resync_fail;
32857 }
32858 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32859 goto resync_fail;
32860
32861 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32862 location);
32863 OMP_CLAUSE_CHAIN (c) = list;
32864 return c;
32865
32866 invalid_kind:
32867 cp_parser_error (parser, "invalid dist_schedule kind");
32868 resync_fail:
32869 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32870 /*or_comma=*/false,
32871 /*consume_paren=*/true);
32872 return list;
32873 }
32874
32875 /* OpenMP 4.0:
32876 proc_bind ( proc-bind-kind )
32877
32878 proc-bind-kind:
32879 master | close | spread */
32880
32881 static tree
32882 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32883 location_t location)
32884 {
32885 tree c;
32886 enum omp_clause_proc_bind_kind kind;
32887
32888 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32889 return list;
32890
32891 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32892 {
32893 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32894 const char *p = IDENTIFIER_POINTER (id);
32895
32896 if (strcmp ("master", p) == 0)
32897 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32898 else if (strcmp ("close", p) == 0)
32899 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32900 else if (strcmp ("spread", p) == 0)
32901 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32902 else
32903 goto invalid_kind;
32904 }
32905 else
32906 goto invalid_kind;
32907
32908 cp_lexer_consume_token (parser->lexer);
32909 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32910 goto resync_fail;
32911
32912 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32913 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32914 location);
32915 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32916 OMP_CLAUSE_CHAIN (c) = list;
32917 return c;
32918
32919 invalid_kind:
32920 cp_parser_error (parser, "invalid depend kind");
32921 resync_fail:
32922 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32923 /*or_comma=*/false,
32924 /*consume_paren=*/true);
32925 return list;
32926 }
32927
32928 /* OpenACC:
32929 async [( int-expr )] */
32930
32931 static tree
32932 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32933 {
32934 tree c, t;
32935 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32936
32937 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32938
32939 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32940 {
32941 cp_lexer_consume_token (parser->lexer);
32942
32943 t = cp_parser_expression (parser);
32944 if (t == error_mark_node
32945 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32946 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32947 /*or_comma=*/false,
32948 /*consume_paren=*/true);
32949 }
32950
32951 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32952
32953 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32954 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32955 OMP_CLAUSE_CHAIN (c) = list;
32956 list = c;
32957
32958 return list;
32959 }
32960
32961 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32962 is a bitmask in MASK. Return the list of clauses found. */
32963
32964 static tree
32965 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32966 const char *where, cp_token *pragma_tok,
32967 bool finish_p = true)
32968 {
32969 tree clauses = NULL;
32970 bool first = true;
32971
32972 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32973 {
32974 location_t here;
32975 pragma_omp_clause c_kind;
32976 omp_clause_code code;
32977 const char *c_name;
32978 tree prev = clauses;
32979
32980 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32981 cp_lexer_consume_token (parser->lexer);
32982
32983 here = cp_lexer_peek_token (parser->lexer)->location;
32984 c_kind = cp_parser_omp_clause_name (parser);
32985
32986 switch (c_kind)
32987 {
32988 case PRAGMA_OACC_CLAUSE_ASYNC:
32989 clauses = cp_parser_oacc_clause_async (parser, clauses);
32990 c_name = "async";
32991 break;
32992 case PRAGMA_OACC_CLAUSE_AUTO:
32993 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32994 clauses, here);
32995 c_name = "auto";
32996 break;
32997 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32998 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32999 c_name = "collapse";
33000 break;
33001 case PRAGMA_OACC_CLAUSE_COPY:
33002 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33003 c_name = "copy";
33004 break;
33005 case PRAGMA_OACC_CLAUSE_COPYIN:
33006 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33007 c_name = "copyin";
33008 break;
33009 case PRAGMA_OACC_CLAUSE_COPYOUT:
33010 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33011 c_name = "copyout";
33012 break;
33013 case PRAGMA_OACC_CLAUSE_CREATE:
33014 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33015 c_name = "create";
33016 break;
33017 case PRAGMA_OACC_CLAUSE_DELETE:
33018 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33019 c_name = "delete";
33020 break;
33021 case PRAGMA_OMP_CLAUSE_DEFAULT:
33022 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33023 c_name = "default";
33024 break;
33025 case PRAGMA_OACC_CLAUSE_DEVICE:
33026 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33027 c_name = "device";
33028 break;
33029 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33030 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33031 c_name = "deviceptr";
33032 break;
33033 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33035 c_name = "device_resident";
33036 break;
33037 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33038 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33039 clauses);
33040 c_name = "firstprivate";
33041 break;
33042 case PRAGMA_OACC_CLAUSE_GANG:
33043 c_name = "gang";
33044 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33045 c_name, clauses);
33046 break;
33047 case PRAGMA_OACC_CLAUSE_HOST:
33048 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33049 c_name = "host";
33050 break;
33051 case PRAGMA_OACC_CLAUSE_IF:
33052 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33053 c_name = "if";
33054 break;
33055 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33056 clauses = cp_parser_oacc_simple_clause (parser,
33057 OMP_CLAUSE_INDEPENDENT,
33058 clauses, here);
33059 c_name = "independent";
33060 break;
33061 case PRAGMA_OACC_CLAUSE_LINK:
33062 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33063 c_name = "link";
33064 break;
33065 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33066 code = OMP_CLAUSE_NUM_GANGS;
33067 c_name = "num_gangs";
33068 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33069 clauses);
33070 break;
33071 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33072 c_name = "num_workers";
33073 code = OMP_CLAUSE_NUM_WORKERS;
33074 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33075 clauses);
33076 break;
33077 case PRAGMA_OACC_CLAUSE_PRESENT:
33078 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33079 c_name = "present";
33080 break;
33081 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33082 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33083 c_name = "present_or_copy";
33084 break;
33085 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33086 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33087 c_name = "present_or_copyin";
33088 break;
33089 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33090 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33091 c_name = "present_or_copyout";
33092 break;
33093 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33094 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33095 c_name = "present_or_create";
33096 break;
33097 case PRAGMA_OACC_CLAUSE_PRIVATE:
33098 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33099 clauses);
33100 c_name = "private";
33101 break;
33102 case PRAGMA_OACC_CLAUSE_REDUCTION:
33103 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33104 c_name = "reduction";
33105 break;
33106 case PRAGMA_OACC_CLAUSE_SELF:
33107 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33108 c_name = "self";
33109 break;
33110 case PRAGMA_OACC_CLAUSE_SEQ:
33111 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33112 clauses, here);
33113 c_name = "seq";
33114 break;
33115 case PRAGMA_OACC_CLAUSE_TILE:
33116 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33117 c_name = "tile";
33118 break;
33119 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33120 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33121 clauses);
33122 c_name = "use_device";
33123 break;
33124 case PRAGMA_OACC_CLAUSE_VECTOR:
33125 c_name = "vector";
33126 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33127 c_name, clauses);
33128 break;
33129 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33130 c_name = "vector_length";
33131 code = OMP_CLAUSE_VECTOR_LENGTH;
33132 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33133 clauses);
33134 break;
33135 case PRAGMA_OACC_CLAUSE_WAIT:
33136 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33137 c_name = "wait";
33138 break;
33139 case PRAGMA_OACC_CLAUSE_WORKER:
33140 c_name = "worker";
33141 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33142 c_name, clauses);
33143 break;
33144 default:
33145 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33146 goto saw_error;
33147 }
33148
33149 first = false;
33150
33151 if (((mask >> c_kind) & 1) == 0)
33152 {
33153 /* Remove the invalid clause(s) from the list to avoid
33154 confusing the rest of the compiler. */
33155 clauses = prev;
33156 error_at (here, "%qs is not valid for %qs", c_name, where);
33157 }
33158 }
33159
33160 saw_error:
33161 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33162
33163 if (finish_p)
33164 return finish_omp_clauses (clauses, C_ORT_ACC);
33165
33166 return clauses;
33167 }
33168
33169 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33170 is a bitmask in MASK. Return the list of clauses found; the result
33171 of clause default goes in *pdefault. */
33172
33173 static tree
33174 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33175 const char *where, cp_token *pragma_tok,
33176 bool finish_p = true)
33177 {
33178 tree clauses = NULL;
33179 bool first = true;
33180 cp_token *token = NULL;
33181
33182 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33183 {
33184 pragma_omp_clause c_kind;
33185 const char *c_name;
33186 tree prev = clauses;
33187
33188 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33189 cp_lexer_consume_token (parser->lexer);
33190
33191 token = cp_lexer_peek_token (parser->lexer);
33192 c_kind = cp_parser_omp_clause_name (parser);
33193
33194 switch (c_kind)
33195 {
33196 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33197 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33198 token->location);
33199 c_name = "collapse";
33200 break;
33201 case PRAGMA_OMP_CLAUSE_COPYIN:
33202 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33203 c_name = "copyin";
33204 break;
33205 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33206 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33207 clauses);
33208 c_name = "copyprivate";
33209 break;
33210 case PRAGMA_OMP_CLAUSE_DEFAULT:
33211 clauses = cp_parser_omp_clause_default (parser, clauses,
33212 token->location, false);
33213 c_name = "default";
33214 break;
33215 case PRAGMA_OMP_CLAUSE_FINAL:
33216 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33217 c_name = "final";
33218 break;
33219 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33220 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33221 clauses);
33222 c_name = "firstprivate";
33223 break;
33224 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33225 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33226 token->location);
33227 c_name = "grainsize";
33228 break;
33229 case PRAGMA_OMP_CLAUSE_HINT:
33230 clauses = cp_parser_omp_clause_hint (parser, clauses,
33231 token->location);
33232 c_name = "hint";
33233 break;
33234 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33235 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33236 token->location);
33237 c_name = "defaultmap";
33238 break;
33239 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33240 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33241 clauses);
33242 c_name = "use_device_ptr";
33243 break;
33244 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33245 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33246 clauses);
33247 c_name = "is_device_ptr";
33248 break;
33249 case PRAGMA_OMP_CLAUSE_IF:
33250 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33251 true);
33252 c_name = "if";
33253 break;
33254 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33255 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33256 clauses);
33257 c_name = "lastprivate";
33258 break;
33259 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33260 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33261 token->location);
33262 c_name = "mergeable";
33263 break;
33264 case PRAGMA_OMP_CLAUSE_NOWAIT:
33265 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33266 c_name = "nowait";
33267 break;
33268 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33269 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33270 token->location);
33271 c_name = "num_tasks";
33272 break;
33273 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33274 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33275 token->location);
33276 c_name = "num_threads";
33277 break;
33278 case PRAGMA_OMP_CLAUSE_ORDERED:
33279 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33280 token->location);
33281 c_name = "ordered";
33282 break;
33283 case PRAGMA_OMP_CLAUSE_PRIORITY:
33284 clauses = cp_parser_omp_clause_priority (parser, clauses,
33285 token->location);
33286 c_name = "priority";
33287 break;
33288 case PRAGMA_OMP_CLAUSE_PRIVATE:
33289 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33290 clauses);
33291 c_name = "private";
33292 break;
33293 case PRAGMA_OMP_CLAUSE_REDUCTION:
33294 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33295 c_name = "reduction";
33296 break;
33297 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33298 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33299 token->location);
33300 c_name = "schedule";
33301 break;
33302 case PRAGMA_OMP_CLAUSE_SHARED:
33303 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33304 clauses);
33305 c_name = "shared";
33306 break;
33307 case PRAGMA_OMP_CLAUSE_UNTIED:
33308 clauses = cp_parser_omp_clause_untied (parser, clauses,
33309 token->location);
33310 c_name = "untied";
33311 break;
33312 case PRAGMA_OMP_CLAUSE_INBRANCH:
33313 case PRAGMA_CILK_CLAUSE_MASK:
33314 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33315 clauses, token->location);
33316 c_name = "inbranch";
33317 break;
33318 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33319 case PRAGMA_CILK_CLAUSE_NOMASK:
33320 clauses = cp_parser_omp_clause_branch (parser,
33321 OMP_CLAUSE_NOTINBRANCH,
33322 clauses, token->location);
33323 c_name = "notinbranch";
33324 break;
33325 case PRAGMA_OMP_CLAUSE_PARALLEL:
33326 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33327 clauses, token->location);
33328 c_name = "parallel";
33329 if (!first)
33330 {
33331 clause_not_first:
33332 error_at (token->location, "%qs must be the first clause of %qs",
33333 c_name, where);
33334 clauses = prev;
33335 }
33336 break;
33337 case PRAGMA_OMP_CLAUSE_FOR:
33338 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33339 clauses, token->location);
33340 c_name = "for";
33341 if (!first)
33342 goto clause_not_first;
33343 break;
33344 case PRAGMA_OMP_CLAUSE_SECTIONS:
33345 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33346 clauses, token->location);
33347 c_name = "sections";
33348 if (!first)
33349 goto clause_not_first;
33350 break;
33351 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33352 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33353 clauses, token->location);
33354 c_name = "taskgroup";
33355 if (!first)
33356 goto clause_not_first;
33357 break;
33358 case PRAGMA_OMP_CLAUSE_LINK:
33359 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33360 c_name = "to";
33361 break;
33362 case PRAGMA_OMP_CLAUSE_TO:
33363 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33364 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33365 clauses);
33366 else
33367 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33368 c_name = "to";
33369 break;
33370 case PRAGMA_OMP_CLAUSE_FROM:
33371 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33372 c_name = "from";
33373 break;
33374 case PRAGMA_OMP_CLAUSE_UNIFORM:
33375 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33376 clauses);
33377 c_name = "uniform";
33378 break;
33379 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33380 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33381 token->location);
33382 c_name = "num_teams";
33383 break;
33384 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33385 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33386 token->location);
33387 c_name = "thread_limit";
33388 break;
33389 case PRAGMA_OMP_CLAUSE_ALIGNED:
33390 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33391 c_name = "aligned";
33392 break;
33393 case PRAGMA_OMP_CLAUSE_LINEAR:
33394 {
33395 bool cilk_simd_fn = false, declare_simd = false;
33396 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33397 cilk_simd_fn = true;
33398 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33399 declare_simd = true;
33400 clauses = cp_parser_omp_clause_linear (parser, clauses,
33401 cilk_simd_fn, declare_simd);
33402 }
33403 c_name = "linear";
33404 break;
33405 case PRAGMA_OMP_CLAUSE_DEPEND:
33406 clauses = cp_parser_omp_clause_depend (parser, clauses,
33407 token->location);
33408 c_name = "depend";
33409 break;
33410 case PRAGMA_OMP_CLAUSE_MAP:
33411 clauses = cp_parser_omp_clause_map (parser, clauses);
33412 c_name = "map";
33413 break;
33414 case PRAGMA_OMP_CLAUSE_DEVICE:
33415 clauses = cp_parser_omp_clause_device (parser, clauses,
33416 token->location);
33417 c_name = "device";
33418 break;
33419 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33420 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33421 token->location);
33422 c_name = "dist_schedule";
33423 break;
33424 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33425 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33426 token->location);
33427 c_name = "proc_bind";
33428 break;
33429 case PRAGMA_OMP_CLAUSE_SAFELEN:
33430 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33431 token->location);
33432 c_name = "safelen";
33433 break;
33434 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33435 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33436 token->location);
33437 c_name = "simdlen";
33438 break;
33439 case PRAGMA_OMP_CLAUSE_NOGROUP:
33440 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33441 token->location);
33442 c_name = "nogroup";
33443 break;
33444 case PRAGMA_OMP_CLAUSE_THREADS:
33445 clauses
33446 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33447 clauses, token->location);
33448 c_name = "threads";
33449 break;
33450 case PRAGMA_OMP_CLAUSE_SIMD:
33451 clauses
33452 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33453 clauses, token->location);
33454 c_name = "simd";
33455 break;
33456 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33457 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33458 c_name = "simdlen";
33459 break;
33460 default:
33461 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33462 goto saw_error;
33463 }
33464
33465 first = false;
33466
33467 if (((mask >> c_kind) & 1) == 0)
33468 {
33469 /* Remove the invalid clause(s) from the list to avoid
33470 confusing the rest of the compiler. */
33471 clauses = prev;
33472 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33473 }
33474 }
33475 saw_error:
33476 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33477 no reason to skip to the end. */
33478 if (!(flag_cilkplus && pragma_tok == NULL))
33479 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33480 if (finish_p)
33481 {
33482 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33483 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33484 else
33485 return finish_omp_clauses (clauses, C_ORT_OMP);
33486 }
33487 return clauses;
33488 }
33489
33490 /* OpenMP 2.5:
33491 structured-block:
33492 statement
33493
33494 In practice, we're also interested in adding the statement to an
33495 outer node. So it is convenient if we work around the fact that
33496 cp_parser_statement calls add_stmt. */
33497
33498 static unsigned
33499 cp_parser_begin_omp_structured_block (cp_parser *parser)
33500 {
33501 unsigned save = parser->in_statement;
33502
33503 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33504 This preserves the "not within loop or switch" style error messages
33505 for nonsense cases like
33506 void foo() {
33507 #pragma omp single
33508 break;
33509 }
33510 */
33511 if (parser->in_statement)
33512 parser->in_statement = IN_OMP_BLOCK;
33513
33514 return save;
33515 }
33516
33517 static void
33518 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33519 {
33520 parser->in_statement = save;
33521 }
33522
33523 static tree
33524 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33525 {
33526 tree stmt = begin_omp_structured_block ();
33527 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33528
33529 cp_parser_statement (parser, NULL_TREE, false, if_p);
33530
33531 cp_parser_end_omp_structured_block (parser, save);
33532 return finish_omp_structured_block (stmt);
33533 }
33534
33535 /* OpenMP 2.5:
33536 # pragma omp atomic new-line
33537 expression-stmt
33538
33539 expression-stmt:
33540 x binop= expr | x++ | ++x | x-- | --x
33541 binop:
33542 +, *, -, /, &, ^, |, <<, >>
33543
33544 where x is an lvalue expression with scalar type.
33545
33546 OpenMP 3.1:
33547 # pragma omp atomic new-line
33548 update-stmt
33549
33550 # pragma omp atomic read new-line
33551 read-stmt
33552
33553 # pragma omp atomic write new-line
33554 write-stmt
33555
33556 # pragma omp atomic update new-line
33557 update-stmt
33558
33559 # pragma omp atomic capture new-line
33560 capture-stmt
33561
33562 # pragma omp atomic capture new-line
33563 capture-block
33564
33565 read-stmt:
33566 v = x
33567 write-stmt:
33568 x = expr
33569 update-stmt:
33570 expression-stmt | x = x binop expr
33571 capture-stmt:
33572 v = expression-stmt
33573 capture-block:
33574 { v = x; update-stmt; } | { update-stmt; v = x; }
33575
33576 OpenMP 4.0:
33577 update-stmt:
33578 expression-stmt | x = x binop expr | x = expr binop x
33579 capture-stmt:
33580 v = update-stmt
33581 capture-block:
33582 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33583
33584 where x and v are lvalue expressions with scalar type. */
33585
33586 static void
33587 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33588 {
33589 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33590 tree rhs1 = NULL_TREE, orig_lhs;
33591 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33592 bool structured_block = false;
33593 bool seq_cst = false;
33594
33595 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33596 {
33597 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33598 const char *p = IDENTIFIER_POINTER (id);
33599
33600 if (!strcmp (p, "seq_cst"))
33601 {
33602 seq_cst = true;
33603 cp_lexer_consume_token (parser->lexer);
33604 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33605 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33606 cp_lexer_consume_token (parser->lexer);
33607 }
33608 }
33609 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33610 {
33611 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33612 const char *p = IDENTIFIER_POINTER (id);
33613
33614 if (!strcmp (p, "read"))
33615 code = OMP_ATOMIC_READ;
33616 else if (!strcmp (p, "write"))
33617 code = NOP_EXPR;
33618 else if (!strcmp (p, "update"))
33619 code = OMP_ATOMIC;
33620 else if (!strcmp (p, "capture"))
33621 code = OMP_ATOMIC_CAPTURE_NEW;
33622 else
33623 p = NULL;
33624 if (p)
33625 cp_lexer_consume_token (parser->lexer);
33626 }
33627 if (!seq_cst)
33628 {
33629 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33630 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33631 cp_lexer_consume_token (parser->lexer);
33632
33633 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33634 {
33635 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33636 const char *p = IDENTIFIER_POINTER (id);
33637
33638 if (!strcmp (p, "seq_cst"))
33639 {
33640 seq_cst = true;
33641 cp_lexer_consume_token (parser->lexer);
33642 }
33643 }
33644 }
33645 cp_parser_require_pragma_eol (parser, pragma_tok);
33646
33647 switch (code)
33648 {
33649 case OMP_ATOMIC_READ:
33650 case NOP_EXPR: /* atomic write */
33651 v = cp_parser_unary_expression (parser);
33652 if (v == error_mark_node)
33653 goto saw_error;
33654 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33655 goto saw_error;
33656 if (code == NOP_EXPR)
33657 lhs = cp_parser_expression (parser);
33658 else
33659 lhs = cp_parser_unary_expression (parser);
33660 if (lhs == error_mark_node)
33661 goto saw_error;
33662 if (code == NOP_EXPR)
33663 {
33664 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33665 opcode. */
33666 code = OMP_ATOMIC;
33667 rhs = lhs;
33668 lhs = v;
33669 v = NULL_TREE;
33670 }
33671 goto done;
33672 case OMP_ATOMIC_CAPTURE_NEW:
33673 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33674 {
33675 cp_lexer_consume_token (parser->lexer);
33676 structured_block = true;
33677 }
33678 else
33679 {
33680 v = cp_parser_unary_expression (parser);
33681 if (v == error_mark_node)
33682 goto saw_error;
33683 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33684 goto saw_error;
33685 }
33686 default:
33687 break;
33688 }
33689
33690 restart:
33691 lhs = cp_parser_unary_expression (parser);
33692 orig_lhs = lhs;
33693 switch (TREE_CODE (lhs))
33694 {
33695 case ERROR_MARK:
33696 goto saw_error;
33697
33698 case POSTINCREMENT_EXPR:
33699 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33700 code = OMP_ATOMIC_CAPTURE_OLD;
33701 /* FALLTHROUGH */
33702 case PREINCREMENT_EXPR:
33703 lhs = TREE_OPERAND (lhs, 0);
33704 opcode = PLUS_EXPR;
33705 rhs = integer_one_node;
33706 break;
33707
33708 case POSTDECREMENT_EXPR:
33709 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33710 code = OMP_ATOMIC_CAPTURE_OLD;
33711 /* FALLTHROUGH */
33712 case PREDECREMENT_EXPR:
33713 lhs = TREE_OPERAND (lhs, 0);
33714 opcode = MINUS_EXPR;
33715 rhs = integer_one_node;
33716 break;
33717
33718 case COMPOUND_EXPR:
33719 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33720 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33721 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33722 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33723 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33724 (TREE_OPERAND (lhs, 1), 0), 0)))
33725 == BOOLEAN_TYPE)
33726 /* Undo effects of boolean_increment for post {in,de}crement. */
33727 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33728 /* FALLTHRU */
33729 case MODIFY_EXPR:
33730 if (TREE_CODE (lhs) == MODIFY_EXPR
33731 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33732 {
33733 /* Undo effects of boolean_increment. */
33734 if (integer_onep (TREE_OPERAND (lhs, 1)))
33735 {
33736 /* This is pre or post increment. */
33737 rhs = TREE_OPERAND (lhs, 1);
33738 lhs = TREE_OPERAND (lhs, 0);
33739 opcode = NOP_EXPR;
33740 if (code == OMP_ATOMIC_CAPTURE_NEW
33741 && !structured_block
33742 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33743 code = OMP_ATOMIC_CAPTURE_OLD;
33744 break;
33745 }
33746 }
33747 /* FALLTHRU */
33748 default:
33749 switch (cp_lexer_peek_token (parser->lexer)->type)
33750 {
33751 case CPP_MULT_EQ:
33752 opcode = MULT_EXPR;
33753 break;
33754 case CPP_DIV_EQ:
33755 opcode = TRUNC_DIV_EXPR;
33756 break;
33757 case CPP_PLUS_EQ:
33758 opcode = PLUS_EXPR;
33759 break;
33760 case CPP_MINUS_EQ:
33761 opcode = MINUS_EXPR;
33762 break;
33763 case CPP_LSHIFT_EQ:
33764 opcode = LSHIFT_EXPR;
33765 break;
33766 case CPP_RSHIFT_EQ:
33767 opcode = RSHIFT_EXPR;
33768 break;
33769 case CPP_AND_EQ:
33770 opcode = BIT_AND_EXPR;
33771 break;
33772 case CPP_OR_EQ:
33773 opcode = BIT_IOR_EXPR;
33774 break;
33775 case CPP_XOR_EQ:
33776 opcode = BIT_XOR_EXPR;
33777 break;
33778 case CPP_EQ:
33779 enum cp_parser_prec oprec;
33780 cp_token *token;
33781 cp_lexer_consume_token (parser->lexer);
33782 cp_parser_parse_tentatively (parser);
33783 rhs1 = cp_parser_simple_cast_expression (parser);
33784 if (rhs1 == error_mark_node)
33785 {
33786 cp_parser_abort_tentative_parse (parser);
33787 cp_parser_simple_cast_expression (parser);
33788 goto saw_error;
33789 }
33790 token = cp_lexer_peek_token (parser->lexer);
33791 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33792 {
33793 cp_parser_abort_tentative_parse (parser);
33794 cp_parser_parse_tentatively (parser);
33795 rhs = cp_parser_binary_expression (parser, false, true,
33796 PREC_NOT_OPERATOR, NULL);
33797 if (rhs == error_mark_node)
33798 {
33799 cp_parser_abort_tentative_parse (parser);
33800 cp_parser_binary_expression (parser, false, true,
33801 PREC_NOT_OPERATOR, NULL);
33802 goto saw_error;
33803 }
33804 switch (TREE_CODE (rhs))
33805 {
33806 case MULT_EXPR:
33807 case TRUNC_DIV_EXPR:
33808 case RDIV_EXPR:
33809 case PLUS_EXPR:
33810 case MINUS_EXPR:
33811 case LSHIFT_EXPR:
33812 case RSHIFT_EXPR:
33813 case BIT_AND_EXPR:
33814 case BIT_IOR_EXPR:
33815 case BIT_XOR_EXPR:
33816 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33817 {
33818 if (cp_parser_parse_definitely (parser))
33819 {
33820 opcode = TREE_CODE (rhs);
33821 rhs1 = TREE_OPERAND (rhs, 0);
33822 rhs = TREE_OPERAND (rhs, 1);
33823 goto stmt_done;
33824 }
33825 else
33826 goto saw_error;
33827 }
33828 break;
33829 default:
33830 break;
33831 }
33832 cp_parser_abort_tentative_parse (parser);
33833 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33834 {
33835 rhs = cp_parser_expression (parser);
33836 if (rhs == error_mark_node)
33837 goto saw_error;
33838 opcode = NOP_EXPR;
33839 rhs1 = NULL_TREE;
33840 goto stmt_done;
33841 }
33842 cp_parser_error (parser,
33843 "invalid form of %<#pragma omp atomic%>");
33844 goto saw_error;
33845 }
33846 if (!cp_parser_parse_definitely (parser))
33847 goto saw_error;
33848 switch (token->type)
33849 {
33850 case CPP_SEMICOLON:
33851 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33852 {
33853 code = OMP_ATOMIC_CAPTURE_OLD;
33854 v = lhs;
33855 lhs = NULL_TREE;
33856 lhs1 = rhs1;
33857 rhs1 = NULL_TREE;
33858 cp_lexer_consume_token (parser->lexer);
33859 goto restart;
33860 }
33861 else if (structured_block)
33862 {
33863 opcode = NOP_EXPR;
33864 rhs = rhs1;
33865 rhs1 = NULL_TREE;
33866 goto stmt_done;
33867 }
33868 cp_parser_error (parser,
33869 "invalid form of %<#pragma omp atomic%>");
33870 goto saw_error;
33871 case CPP_MULT:
33872 opcode = MULT_EXPR;
33873 break;
33874 case CPP_DIV:
33875 opcode = TRUNC_DIV_EXPR;
33876 break;
33877 case CPP_PLUS:
33878 opcode = PLUS_EXPR;
33879 break;
33880 case CPP_MINUS:
33881 opcode = MINUS_EXPR;
33882 break;
33883 case CPP_LSHIFT:
33884 opcode = LSHIFT_EXPR;
33885 break;
33886 case CPP_RSHIFT:
33887 opcode = RSHIFT_EXPR;
33888 break;
33889 case CPP_AND:
33890 opcode = BIT_AND_EXPR;
33891 break;
33892 case CPP_OR:
33893 opcode = BIT_IOR_EXPR;
33894 break;
33895 case CPP_XOR:
33896 opcode = BIT_XOR_EXPR;
33897 break;
33898 default:
33899 cp_parser_error (parser,
33900 "invalid operator for %<#pragma omp atomic%>");
33901 goto saw_error;
33902 }
33903 oprec = TOKEN_PRECEDENCE (token);
33904 gcc_assert (oprec != PREC_NOT_OPERATOR);
33905 if (commutative_tree_code (opcode))
33906 oprec = (enum cp_parser_prec) (oprec - 1);
33907 cp_lexer_consume_token (parser->lexer);
33908 rhs = cp_parser_binary_expression (parser, false, false,
33909 oprec, NULL);
33910 if (rhs == error_mark_node)
33911 goto saw_error;
33912 goto stmt_done;
33913 /* FALLTHROUGH */
33914 default:
33915 cp_parser_error (parser,
33916 "invalid operator for %<#pragma omp atomic%>");
33917 goto saw_error;
33918 }
33919 cp_lexer_consume_token (parser->lexer);
33920
33921 rhs = cp_parser_expression (parser);
33922 if (rhs == error_mark_node)
33923 goto saw_error;
33924 break;
33925 }
33926 stmt_done:
33927 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33928 {
33929 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33930 goto saw_error;
33931 v = cp_parser_unary_expression (parser);
33932 if (v == error_mark_node)
33933 goto saw_error;
33934 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33935 goto saw_error;
33936 lhs1 = cp_parser_unary_expression (parser);
33937 if (lhs1 == error_mark_node)
33938 goto saw_error;
33939 }
33940 if (structured_block)
33941 {
33942 cp_parser_consume_semicolon_at_end_of_statement (parser);
33943 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33944 }
33945 done:
33946 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33947 if (!structured_block)
33948 cp_parser_consume_semicolon_at_end_of_statement (parser);
33949 return;
33950
33951 saw_error:
33952 cp_parser_skip_to_end_of_block_or_statement (parser);
33953 if (structured_block)
33954 {
33955 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33956 cp_lexer_consume_token (parser->lexer);
33957 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33958 {
33959 cp_parser_skip_to_end_of_block_or_statement (parser);
33960 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33961 cp_lexer_consume_token (parser->lexer);
33962 }
33963 }
33964 }
33965
33966
33967 /* OpenMP 2.5:
33968 # pragma omp barrier new-line */
33969
33970 static void
33971 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33972 {
33973 cp_parser_require_pragma_eol (parser, pragma_tok);
33974 finish_omp_barrier ();
33975 }
33976
33977 /* OpenMP 2.5:
33978 # pragma omp critical [(name)] new-line
33979 structured-block
33980
33981 OpenMP 4.5:
33982 # pragma omp critical [(name) [hint(expression)]] new-line
33983 structured-block */
33984
33985 #define OMP_CRITICAL_CLAUSE_MASK \
33986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33987
33988 static tree
33989 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33990 {
33991 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33992
33993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33994 {
33995 cp_lexer_consume_token (parser->lexer);
33996
33997 name = cp_parser_identifier (parser);
33998
33999 if (name == error_mark_node
34000 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34001 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34002 /*or_comma=*/false,
34003 /*consume_paren=*/true);
34004 if (name == error_mark_node)
34005 name = NULL;
34006
34007 clauses = cp_parser_omp_all_clauses (parser,
34008 OMP_CRITICAL_CLAUSE_MASK,
34009 "#pragma omp critical", pragma_tok);
34010 }
34011 else
34012 cp_parser_require_pragma_eol (parser, pragma_tok);
34013
34014 stmt = cp_parser_omp_structured_block (parser, if_p);
34015 return c_finish_omp_critical (input_location, stmt, name, clauses);
34016 }
34017
34018 /* OpenMP 2.5:
34019 # pragma omp flush flush-vars[opt] new-line
34020
34021 flush-vars:
34022 ( variable-list ) */
34023
34024 static void
34025 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34026 {
34027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34028 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34029 cp_parser_require_pragma_eol (parser, pragma_tok);
34030
34031 finish_omp_flush ();
34032 }
34033
34034 /* Helper function, to parse omp for increment expression. */
34035
34036 static tree
34037 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34038 {
34039 tree cond = cp_parser_binary_expression (parser, false, true,
34040 PREC_NOT_OPERATOR, NULL);
34041 if (cond == error_mark_node
34042 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34043 {
34044 cp_parser_skip_to_end_of_statement (parser);
34045 return error_mark_node;
34046 }
34047
34048 switch (TREE_CODE (cond))
34049 {
34050 case GT_EXPR:
34051 case GE_EXPR:
34052 case LT_EXPR:
34053 case LE_EXPR:
34054 break;
34055 case NE_EXPR:
34056 if (code == CILK_SIMD || code == CILK_FOR)
34057 break;
34058 /* Fall through: OpenMP disallows NE_EXPR. */
34059 gcc_fallthrough ();
34060 default:
34061 return error_mark_node;
34062 }
34063
34064 /* If decl is an iterator, preserve LHS and RHS of the relational
34065 expr until finish_omp_for. */
34066 if (decl
34067 && (type_dependent_expression_p (decl)
34068 || CLASS_TYPE_P (TREE_TYPE (decl))))
34069 return cond;
34070
34071 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34072 TREE_CODE (cond),
34073 TREE_OPERAND (cond, 0), ERROR_MARK,
34074 TREE_OPERAND (cond, 1), ERROR_MARK,
34075 /*overload=*/NULL, tf_warning_or_error);
34076 }
34077
34078 /* Helper function, to parse omp for increment expression. */
34079
34080 static tree
34081 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34082 {
34083 cp_token *token = cp_lexer_peek_token (parser->lexer);
34084 enum tree_code op;
34085 tree lhs, rhs;
34086 cp_id_kind idk;
34087 bool decl_first;
34088
34089 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34090 {
34091 op = (token->type == CPP_PLUS_PLUS
34092 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34093 cp_lexer_consume_token (parser->lexer);
34094 lhs = cp_parser_simple_cast_expression (parser);
34095 if (lhs != decl
34096 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34097 return error_mark_node;
34098 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34099 }
34100
34101 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34102 if (lhs != decl
34103 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34104 return error_mark_node;
34105
34106 token = cp_lexer_peek_token (parser->lexer);
34107 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34108 {
34109 op = (token->type == CPP_PLUS_PLUS
34110 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34111 cp_lexer_consume_token (parser->lexer);
34112 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34113 }
34114
34115 op = cp_parser_assignment_operator_opt (parser);
34116 if (op == ERROR_MARK)
34117 return error_mark_node;
34118
34119 if (op != NOP_EXPR)
34120 {
34121 rhs = cp_parser_assignment_expression (parser);
34122 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34123 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34124 }
34125
34126 lhs = cp_parser_binary_expression (parser, false, false,
34127 PREC_ADDITIVE_EXPRESSION, NULL);
34128 token = cp_lexer_peek_token (parser->lexer);
34129 decl_first = (lhs == decl
34130 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34131 if (decl_first)
34132 lhs = NULL_TREE;
34133 if (token->type != CPP_PLUS
34134 && token->type != CPP_MINUS)
34135 return error_mark_node;
34136
34137 do
34138 {
34139 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34140 cp_lexer_consume_token (parser->lexer);
34141 rhs = cp_parser_binary_expression (parser, false, false,
34142 PREC_ADDITIVE_EXPRESSION, NULL);
34143 token = cp_lexer_peek_token (parser->lexer);
34144 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34145 {
34146 if (lhs == NULL_TREE)
34147 {
34148 if (op == PLUS_EXPR)
34149 lhs = rhs;
34150 else
34151 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34152 tf_warning_or_error);
34153 }
34154 else
34155 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34156 ERROR_MARK, NULL, tf_warning_or_error);
34157 }
34158 }
34159 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34160
34161 if (!decl_first)
34162 {
34163 if ((rhs != decl
34164 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34165 || op == MINUS_EXPR)
34166 return error_mark_node;
34167 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34168 }
34169 else
34170 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34171
34172 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34173 }
34174
34175 /* Parse the initialization statement of either an OpenMP for loop or
34176 a Cilk Plus for loop.
34177
34178 Return true if the resulting construct should have an
34179 OMP_CLAUSE_PRIVATE added to it. */
34180
34181 static tree
34182 cp_parser_omp_for_loop_init (cp_parser *parser,
34183 enum tree_code code,
34184 tree &this_pre_body,
34185 vec<tree, va_gc> *for_block,
34186 tree &init,
34187 tree &orig_init,
34188 tree &decl,
34189 tree &real_decl)
34190 {
34191 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34192 return NULL_TREE;
34193
34194 tree add_private_clause = NULL_TREE;
34195
34196 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34197
34198 init-expr:
34199 var = lb
34200 integer-type var = lb
34201 random-access-iterator-type var = lb
34202 pointer-type var = lb
34203 */
34204 cp_decl_specifier_seq type_specifiers;
34205
34206 /* First, try to parse as an initialized declaration. See
34207 cp_parser_condition, from whence the bulk of this is copied. */
34208
34209 cp_parser_parse_tentatively (parser);
34210 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34211 /*is_trailing_return=*/false,
34212 &type_specifiers);
34213 if (cp_parser_parse_definitely (parser))
34214 {
34215 /* If parsing a type specifier seq succeeded, then this
34216 MUST be a initialized declaration. */
34217 tree asm_specification, attributes;
34218 cp_declarator *declarator;
34219
34220 declarator = cp_parser_declarator (parser,
34221 CP_PARSER_DECLARATOR_NAMED,
34222 /*ctor_dtor_or_conv_p=*/NULL,
34223 /*parenthesized_p=*/NULL,
34224 /*member_p=*/false,
34225 /*friend_p=*/false);
34226 attributes = cp_parser_attributes_opt (parser);
34227 asm_specification = cp_parser_asm_specification_opt (parser);
34228
34229 if (declarator == cp_error_declarator)
34230 cp_parser_skip_to_end_of_statement (parser);
34231
34232 else
34233 {
34234 tree pushed_scope, auto_node;
34235
34236 decl = start_decl (declarator, &type_specifiers,
34237 SD_INITIALIZED, attributes,
34238 /*prefix_attributes=*/NULL_TREE,
34239 &pushed_scope);
34240
34241 auto_node = type_uses_auto (TREE_TYPE (decl));
34242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34243 {
34244 if (cp_lexer_next_token_is (parser->lexer,
34245 CPP_OPEN_PAREN))
34246 {
34247 if (code != CILK_SIMD && code != CILK_FOR)
34248 error ("parenthesized initialization is not allowed in "
34249 "OpenMP %<for%> loop");
34250 else
34251 error ("parenthesized initialization is "
34252 "not allowed in for-loop");
34253 }
34254 else
34255 /* Trigger an error. */
34256 cp_parser_require (parser, CPP_EQ, RT_EQ);
34257
34258 init = error_mark_node;
34259 cp_parser_skip_to_end_of_statement (parser);
34260 }
34261 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34262 || type_dependent_expression_p (decl)
34263 || auto_node)
34264 {
34265 bool is_direct_init, is_non_constant_init;
34266
34267 init = cp_parser_initializer (parser,
34268 &is_direct_init,
34269 &is_non_constant_init);
34270
34271 if (auto_node)
34272 {
34273 TREE_TYPE (decl)
34274 = do_auto_deduction (TREE_TYPE (decl), init,
34275 auto_node);
34276
34277 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34278 && !type_dependent_expression_p (decl))
34279 goto non_class;
34280 }
34281
34282 cp_finish_decl (decl, init, !is_non_constant_init,
34283 asm_specification,
34284 LOOKUP_ONLYCONVERTING);
34285 orig_init = init;
34286 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34287 {
34288 vec_safe_push (for_block, this_pre_body);
34289 init = NULL_TREE;
34290 }
34291 else
34292 {
34293 init = pop_stmt_list (this_pre_body);
34294 if (init && TREE_CODE (init) == STATEMENT_LIST)
34295 {
34296 tree_stmt_iterator i = tsi_start (init);
34297 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34298 while (!tsi_end_p (i))
34299 {
34300 tree t = tsi_stmt (i);
34301 if (TREE_CODE (t) == DECL_EXPR
34302 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34303 {
34304 tsi_delink (&i);
34305 vec_safe_push (for_block, t);
34306 continue;
34307 }
34308 break;
34309 }
34310 if (tsi_one_before_end_p (i))
34311 {
34312 tree t = tsi_stmt (i);
34313 tsi_delink (&i);
34314 free_stmt_list (init);
34315 init = t;
34316 }
34317 }
34318 }
34319 this_pre_body = NULL_TREE;
34320 }
34321 else
34322 {
34323 /* Consume '='. */
34324 cp_lexer_consume_token (parser->lexer);
34325 init = cp_parser_assignment_expression (parser);
34326
34327 non_class:
34328 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34329 init = error_mark_node;
34330 else
34331 cp_finish_decl (decl, NULL_TREE,
34332 /*init_const_expr_p=*/false,
34333 asm_specification,
34334 LOOKUP_ONLYCONVERTING);
34335 }
34336
34337 if (pushed_scope)
34338 pop_scope (pushed_scope);
34339 }
34340 }
34341 else
34342 {
34343 cp_id_kind idk;
34344 /* If parsing a type specifier sequence failed, then
34345 this MUST be a simple expression. */
34346 if (code == CILK_FOR)
34347 error ("%<_Cilk_for%> allows expression instead of declaration only "
34348 "in C, not in C++");
34349 cp_parser_parse_tentatively (parser);
34350 decl = cp_parser_primary_expression (parser, false, false,
34351 false, &idk);
34352 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34353 if (!cp_parser_error_occurred (parser)
34354 && decl
34355 && (TREE_CODE (decl) == COMPONENT_REF
34356 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34357 {
34358 cp_parser_abort_tentative_parse (parser);
34359 cp_parser_parse_tentatively (parser);
34360 cp_token *token = cp_lexer_peek_token (parser->lexer);
34361 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34362 /*check_dependency_p=*/true,
34363 /*template_p=*/NULL,
34364 /*declarator_p=*/false,
34365 /*optional_p=*/false);
34366 if (name != error_mark_node
34367 && last_tok == cp_lexer_peek_token (parser->lexer))
34368 {
34369 decl = cp_parser_lookup_name_simple (parser, name,
34370 token->location);
34371 if (TREE_CODE (decl) == FIELD_DECL)
34372 add_private_clause = omp_privatize_field (decl, false);
34373 }
34374 cp_parser_abort_tentative_parse (parser);
34375 cp_parser_parse_tentatively (parser);
34376 decl = cp_parser_primary_expression (parser, false, false,
34377 false, &idk);
34378 }
34379 if (!cp_parser_error_occurred (parser)
34380 && decl
34381 && DECL_P (decl)
34382 && CLASS_TYPE_P (TREE_TYPE (decl)))
34383 {
34384 tree rhs;
34385
34386 cp_parser_parse_definitely (parser);
34387 cp_parser_require (parser, CPP_EQ, RT_EQ);
34388 rhs = cp_parser_assignment_expression (parser);
34389 orig_init = rhs;
34390 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34391 decl, NOP_EXPR,
34392 rhs,
34393 tf_warning_or_error));
34394 if (!add_private_clause)
34395 add_private_clause = decl;
34396 }
34397 else
34398 {
34399 decl = NULL;
34400 cp_parser_abort_tentative_parse (parser);
34401 init = cp_parser_expression (parser);
34402 if (init)
34403 {
34404 if (TREE_CODE (init) == MODIFY_EXPR
34405 || TREE_CODE (init) == MODOP_EXPR)
34406 real_decl = TREE_OPERAND (init, 0);
34407 }
34408 }
34409 }
34410 return add_private_clause;
34411 }
34412
34413 /* Parse the restricted form of the for statement allowed by OpenMP. */
34414
34415 static tree
34416 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34417 tree *cclauses, bool *if_p)
34418 {
34419 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34420 tree real_decl, initv, condv, incrv, declv;
34421 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34422 location_t loc_first;
34423 bool collapse_err = false;
34424 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34425 vec<tree, va_gc> *for_block = make_tree_vector ();
34426 auto_vec<tree, 4> orig_inits;
34427 bool tiling = false;
34428
34429 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34430 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34431 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34432 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34433 {
34434 tiling = true;
34435 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34436 }
34437 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34438 && OMP_CLAUSE_ORDERED_EXPR (cl))
34439 {
34440 ordered_cl = cl;
34441 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34442 }
34443
34444 if (ordered && ordered < collapse)
34445 {
34446 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34447 "%<ordered%> clause parameter is less than %<collapse%>");
34448 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34449 = build_int_cst (NULL_TREE, collapse);
34450 ordered = collapse;
34451 }
34452 if (ordered)
34453 {
34454 for (tree *pc = &clauses; *pc; )
34455 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34456 {
34457 error_at (OMP_CLAUSE_LOCATION (*pc),
34458 "%<linear%> clause may not be specified together "
34459 "with %<ordered%> clause with a parameter");
34460 *pc = OMP_CLAUSE_CHAIN (*pc);
34461 }
34462 else
34463 pc = &OMP_CLAUSE_CHAIN (*pc);
34464 }
34465
34466 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34467 count = ordered ? ordered : collapse;
34468
34469 declv = make_tree_vec (count);
34470 initv = make_tree_vec (count);
34471 condv = make_tree_vec (count);
34472 incrv = make_tree_vec (count);
34473
34474 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34475
34476 for (i = 0; i < count; i++)
34477 {
34478 int bracecount = 0;
34479 tree add_private_clause = NULL_TREE;
34480 location_t loc;
34481
34482 if (code != CILK_FOR
34483 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34484 {
34485 if (!collapse_err)
34486 cp_parser_error (parser, "for statement expected");
34487 return NULL;
34488 }
34489 if (code == CILK_FOR
34490 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34491 {
34492 if (!collapse_err)
34493 cp_parser_error (parser, "_Cilk_for statement expected");
34494 return NULL;
34495 }
34496 loc = cp_lexer_consume_token (parser->lexer)->location;
34497
34498 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34499 return NULL;
34500
34501 init = orig_init = decl = real_decl = NULL;
34502 this_pre_body = push_stmt_list ();
34503
34504 add_private_clause
34505 = cp_parser_omp_for_loop_init (parser, code,
34506 this_pre_body, for_block,
34507 init, orig_init, decl, real_decl);
34508
34509 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34510 if (this_pre_body)
34511 {
34512 this_pre_body = pop_stmt_list (this_pre_body);
34513 if (pre_body)
34514 {
34515 tree t = pre_body;
34516 pre_body = push_stmt_list ();
34517 add_stmt (t);
34518 add_stmt (this_pre_body);
34519 pre_body = pop_stmt_list (pre_body);
34520 }
34521 else
34522 pre_body = this_pre_body;
34523 }
34524
34525 if (decl)
34526 real_decl = decl;
34527 if (cclauses != NULL
34528 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34529 && real_decl != NULL_TREE)
34530 {
34531 tree *c;
34532 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34533 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34534 && OMP_CLAUSE_DECL (*c) == real_decl)
34535 {
34536 error_at (loc, "iteration variable %qD"
34537 " should not be firstprivate", real_decl);
34538 *c = OMP_CLAUSE_CHAIN (*c);
34539 }
34540 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34541 && OMP_CLAUSE_DECL (*c) == real_decl)
34542 {
34543 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34544 tree l = *c;
34545 *c = OMP_CLAUSE_CHAIN (*c);
34546 if (code == OMP_SIMD)
34547 {
34548 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34549 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34550 }
34551 else
34552 {
34553 OMP_CLAUSE_CHAIN (l) = clauses;
34554 clauses = l;
34555 }
34556 add_private_clause = NULL_TREE;
34557 }
34558 else
34559 {
34560 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34561 && OMP_CLAUSE_DECL (*c) == real_decl)
34562 add_private_clause = NULL_TREE;
34563 c = &OMP_CLAUSE_CHAIN (*c);
34564 }
34565 }
34566
34567 if (add_private_clause)
34568 {
34569 tree c;
34570 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34571 {
34572 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34573 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34574 && OMP_CLAUSE_DECL (c) == decl)
34575 break;
34576 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34577 && OMP_CLAUSE_DECL (c) == decl)
34578 error_at (loc, "iteration variable %qD "
34579 "should not be firstprivate",
34580 decl);
34581 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34582 && OMP_CLAUSE_DECL (c) == decl)
34583 error_at (loc, "iteration variable %qD should not be reduction",
34584 decl);
34585 }
34586 if (c == NULL)
34587 {
34588 if (code != OMP_SIMD)
34589 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34590 else if (collapse == 1)
34591 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34592 else
34593 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34594 OMP_CLAUSE_DECL (c) = add_private_clause;
34595 c = finish_omp_clauses (c, C_ORT_OMP);
34596 if (c)
34597 {
34598 OMP_CLAUSE_CHAIN (c) = clauses;
34599 clauses = c;
34600 /* For linear, signal that we need to fill up
34601 the so far unknown linear step. */
34602 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34603 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34604 }
34605 }
34606 }
34607
34608 cond = NULL;
34609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34610 cond = cp_parser_omp_for_cond (parser, decl, code);
34611 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34612
34613 incr = NULL;
34614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34615 {
34616 /* If decl is an iterator, preserve the operator on decl
34617 until finish_omp_for. */
34618 if (real_decl
34619 && ((processing_template_decl
34620 && (TREE_TYPE (real_decl) == NULL_TREE
34621 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34622 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34623 incr = cp_parser_omp_for_incr (parser, real_decl);
34624 else
34625 incr = cp_parser_expression (parser);
34626 if (!EXPR_HAS_LOCATION (incr))
34627 protected_set_expr_location (incr, input_location);
34628 }
34629
34630 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34631 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34632 /*or_comma=*/false,
34633 /*consume_paren=*/true);
34634
34635 TREE_VEC_ELT (declv, i) = decl;
34636 TREE_VEC_ELT (initv, i) = init;
34637 TREE_VEC_ELT (condv, i) = cond;
34638 TREE_VEC_ELT (incrv, i) = incr;
34639 if (orig_init)
34640 {
34641 orig_inits.safe_grow_cleared (i + 1);
34642 orig_inits[i] = orig_init;
34643 }
34644
34645 if (i == count - 1)
34646 break;
34647
34648 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34649 in between the collapsed for loops to be still considered perfectly
34650 nested. Hopefully the final version clarifies this.
34651 For now handle (multiple) {'s and empty statements. */
34652 cp_parser_parse_tentatively (parser);
34653 for (;;)
34654 {
34655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34656 break;
34657 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34658 {
34659 cp_lexer_consume_token (parser->lexer);
34660 bracecount++;
34661 }
34662 else if (bracecount
34663 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34664 cp_lexer_consume_token (parser->lexer);
34665 else
34666 {
34667 loc = cp_lexer_peek_token (parser->lexer)->location;
34668 error_at (loc, "not enough for loops to collapse");
34669 collapse_err = true;
34670 cp_parser_abort_tentative_parse (parser);
34671 declv = NULL_TREE;
34672 break;
34673 }
34674 }
34675
34676 if (declv)
34677 {
34678 cp_parser_parse_definitely (parser);
34679 nbraces += bracecount;
34680 }
34681 }
34682
34683 if (nbraces)
34684 if_p = NULL;
34685
34686 /* Note that we saved the original contents of this flag when we entered
34687 the structured block, and so we don't need to re-save it here. */
34688 if (code == CILK_SIMD || code == CILK_FOR)
34689 parser->in_statement = IN_CILK_SIMD_FOR;
34690 else
34691 parser->in_statement = IN_OMP_FOR;
34692
34693 /* Note that the grammar doesn't call for a structured block here,
34694 though the loop as a whole is a structured block. */
34695 body = push_stmt_list ();
34696 cp_parser_statement (parser, NULL_TREE, false, if_p);
34697 body = pop_stmt_list (body);
34698
34699 if (declv == NULL_TREE)
34700 ret = NULL_TREE;
34701 else
34702 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34703 body, pre_body, &orig_inits, clauses);
34704
34705 while (nbraces)
34706 {
34707 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34708 {
34709 cp_lexer_consume_token (parser->lexer);
34710 nbraces--;
34711 }
34712 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34713 cp_lexer_consume_token (parser->lexer);
34714 else
34715 {
34716 if (!collapse_err)
34717 {
34718 error_at (cp_lexer_peek_token (parser->lexer)->location,
34719 "collapsed loops not perfectly nested");
34720 }
34721 collapse_err = true;
34722 cp_parser_statement_seq_opt (parser, NULL);
34723 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34724 break;
34725 }
34726 }
34727
34728 while (!for_block->is_empty ())
34729 {
34730 tree t = for_block->pop ();
34731 if (TREE_CODE (t) == STATEMENT_LIST)
34732 add_stmt (pop_stmt_list (t));
34733 else
34734 add_stmt (t);
34735 }
34736 release_tree_vector (for_block);
34737
34738 return ret;
34739 }
34740
34741 /* Helper function for OpenMP parsing, split clauses and call
34742 finish_omp_clauses on each of the set of clauses afterwards. */
34743
34744 static void
34745 cp_omp_split_clauses (location_t loc, enum tree_code code,
34746 omp_clause_mask mask, tree clauses, tree *cclauses)
34747 {
34748 int i;
34749 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34750 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34751 if (cclauses[i])
34752 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34753 }
34754
34755 /* OpenMP 4.0:
34756 #pragma omp simd simd-clause[optseq] new-line
34757 for-loop */
34758
34759 #define OMP_SIMD_CLAUSE_MASK \
34760 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34768
34769 static tree
34770 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34771 char *p_name, omp_clause_mask mask, tree *cclauses,
34772 bool *if_p)
34773 {
34774 tree clauses, sb, ret;
34775 unsigned int save;
34776 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34777
34778 strcat (p_name, " simd");
34779 mask |= OMP_SIMD_CLAUSE_MASK;
34780
34781 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34782 cclauses == NULL);
34783 if (cclauses)
34784 {
34785 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34786 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34787 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34788 OMP_CLAUSE_ORDERED);
34789 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34790 {
34791 error_at (OMP_CLAUSE_LOCATION (c),
34792 "%<ordered%> clause with parameter may not be specified "
34793 "on %qs construct", p_name);
34794 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34795 }
34796 }
34797
34798 sb = begin_omp_structured_block ();
34799 save = cp_parser_begin_omp_structured_block (parser);
34800
34801 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34802
34803 cp_parser_end_omp_structured_block (parser, save);
34804 add_stmt (finish_omp_structured_block (sb));
34805
34806 return ret;
34807 }
34808
34809 /* OpenMP 2.5:
34810 #pragma omp for for-clause[optseq] new-line
34811 for-loop
34812
34813 OpenMP 4.0:
34814 #pragma omp for simd for-simd-clause[optseq] new-line
34815 for-loop */
34816
34817 #define OMP_FOR_CLAUSE_MASK \
34818 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34827
34828 static tree
34829 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34830 char *p_name, omp_clause_mask mask, tree *cclauses,
34831 bool *if_p)
34832 {
34833 tree clauses, sb, ret;
34834 unsigned int save;
34835 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34836
34837 strcat (p_name, " for");
34838 mask |= OMP_FOR_CLAUSE_MASK;
34839 /* parallel for{, simd} disallows nowait clause, but for
34840 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34841 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34842 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34843 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34844 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34845 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34846
34847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34848 {
34849 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34850 const char *p = IDENTIFIER_POINTER (id);
34851
34852 if (strcmp (p, "simd") == 0)
34853 {
34854 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34855 if (cclauses == NULL)
34856 cclauses = cclauses_buf;
34857
34858 cp_lexer_consume_token (parser->lexer);
34859 if (!flag_openmp) /* flag_openmp_simd */
34860 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34861 cclauses, if_p);
34862 sb = begin_omp_structured_block ();
34863 save = cp_parser_begin_omp_structured_block (parser);
34864 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34865 cclauses, if_p);
34866 cp_parser_end_omp_structured_block (parser, save);
34867 tree body = finish_omp_structured_block (sb);
34868 if (ret == NULL)
34869 return ret;
34870 ret = make_node (OMP_FOR);
34871 TREE_TYPE (ret) = void_type_node;
34872 OMP_FOR_BODY (ret) = body;
34873 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34874 SET_EXPR_LOCATION (ret, loc);
34875 add_stmt (ret);
34876 return ret;
34877 }
34878 }
34879 if (!flag_openmp) /* flag_openmp_simd */
34880 {
34881 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34882 return NULL_TREE;
34883 }
34884
34885 /* Composite distribute parallel for disallows linear clause. */
34886 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34887 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34888
34889 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34890 cclauses == NULL);
34891 if (cclauses)
34892 {
34893 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34894 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34895 }
34896
34897 sb = begin_omp_structured_block ();
34898 save = cp_parser_begin_omp_structured_block (parser);
34899
34900 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34901
34902 cp_parser_end_omp_structured_block (parser, save);
34903 add_stmt (finish_omp_structured_block (sb));
34904
34905 return ret;
34906 }
34907
34908 /* OpenMP 2.5:
34909 # pragma omp master new-line
34910 structured-block */
34911
34912 static tree
34913 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34914 {
34915 cp_parser_require_pragma_eol (parser, pragma_tok);
34916 return c_finish_omp_master (input_location,
34917 cp_parser_omp_structured_block (parser, if_p));
34918 }
34919
34920 /* OpenMP 2.5:
34921 # pragma omp ordered new-line
34922 structured-block
34923
34924 OpenMP 4.5:
34925 # pragma omp ordered ordered-clauses new-line
34926 structured-block */
34927
34928 #define OMP_ORDERED_CLAUSE_MASK \
34929 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34931
34932 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34933 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34934
34935 static bool
34936 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34937 enum pragma_context context, bool *if_p)
34938 {
34939 location_t loc = pragma_tok->location;
34940
34941 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34942 {
34943 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34944 const char *p = IDENTIFIER_POINTER (id);
34945
34946 if (strcmp (p, "depend") == 0)
34947 {
34948 if (context == pragma_stmt)
34949 {
34950 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34951 "%<depend%> clause may only be used in compound "
34952 "statements");
34953 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34954 return false;
34955 }
34956 tree clauses
34957 = cp_parser_omp_all_clauses (parser,
34958 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34959 "#pragma omp ordered", pragma_tok);
34960 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34961 return false;
34962 }
34963 }
34964
34965 tree clauses
34966 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34967 "#pragma omp ordered", pragma_tok);
34968 c_finish_omp_ordered (loc, clauses,
34969 cp_parser_omp_structured_block (parser, if_p));
34970 return true;
34971 }
34972
34973 /* OpenMP 2.5:
34974
34975 section-scope:
34976 { section-sequence }
34977
34978 section-sequence:
34979 section-directive[opt] structured-block
34980 section-sequence section-directive structured-block */
34981
34982 static tree
34983 cp_parser_omp_sections_scope (cp_parser *parser)
34984 {
34985 tree stmt, substmt;
34986 bool error_suppress = false;
34987 cp_token *tok;
34988
34989 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34990 return NULL_TREE;
34991
34992 stmt = push_stmt_list ();
34993
34994 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34995 != PRAGMA_OMP_SECTION)
34996 {
34997 substmt = cp_parser_omp_structured_block (parser, NULL);
34998 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34999 add_stmt (substmt);
35000 }
35001
35002 while (1)
35003 {
35004 tok = cp_lexer_peek_token (parser->lexer);
35005 if (tok->type == CPP_CLOSE_BRACE)
35006 break;
35007 if (tok->type == CPP_EOF)
35008 break;
35009
35010 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35011 {
35012 cp_lexer_consume_token (parser->lexer);
35013 cp_parser_require_pragma_eol (parser, tok);
35014 error_suppress = false;
35015 }
35016 else if (!error_suppress)
35017 {
35018 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35019 error_suppress = true;
35020 }
35021
35022 substmt = cp_parser_omp_structured_block (parser, NULL);
35023 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35024 add_stmt (substmt);
35025 }
35026 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35027
35028 substmt = pop_stmt_list (stmt);
35029
35030 stmt = make_node (OMP_SECTIONS);
35031 TREE_TYPE (stmt) = void_type_node;
35032 OMP_SECTIONS_BODY (stmt) = substmt;
35033
35034 add_stmt (stmt);
35035 return stmt;
35036 }
35037
35038 /* OpenMP 2.5:
35039 # pragma omp sections sections-clause[optseq] newline
35040 sections-scope */
35041
35042 #define OMP_SECTIONS_CLAUSE_MASK \
35043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35048
35049 static tree
35050 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35051 char *p_name, omp_clause_mask mask, tree *cclauses)
35052 {
35053 tree clauses, ret;
35054 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35055
35056 strcat (p_name, " sections");
35057 mask |= OMP_SECTIONS_CLAUSE_MASK;
35058 if (cclauses)
35059 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35060
35061 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35062 cclauses == NULL);
35063 if (cclauses)
35064 {
35065 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35066 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35067 }
35068
35069 ret = cp_parser_omp_sections_scope (parser);
35070 if (ret)
35071 OMP_SECTIONS_CLAUSES (ret) = clauses;
35072
35073 return ret;
35074 }
35075
35076 /* OpenMP 2.5:
35077 # pragma omp parallel parallel-clause[optseq] new-line
35078 structured-block
35079 # pragma omp parallel for parallel-for-clause[optseq] new-line
35080 structured-block
35081 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35082 structured-block
35083
35084 OpenMP 4.0:
35085 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35086 structured-block */
35087
35088 #define OMP_PARALLEL_CLAUSE_MASK \
35089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35098
35099 static tree
35100 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35101 char *p_name, omp_clause_mask mask, tree *cclauses,
35102 bool *if_p)
35103 {
35104 tree stmt, clauses, block;
35105 unsigned int save;
35106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35107
35108 strcat (p_name, " parallel");
35109 mask |= OMP_PARALLEL_CLAUSE_MASK;
35110 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35111 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35112 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35113 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35114
35115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35116 {
35117 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35118 if (cclauses == NULL)
35119 cclauses = cclauses_buf;
35120
35121 cp_lexer_consume_token (parser->lexer);
35122 if (!flag_openmp) /* flag_openmp_simd */
35123 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35124 if_p);
35125 block = begin_omp_parallel ();
35126 save = cp_parser_begin_omp_structured_block (parser);
35127 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35128 if_p);
35129 cp_parser_end_omp_structured_block (parser, save);
35130 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35131 block);
35132 if (ret == NULL_TREE)
35133 return ret;
35134 OMP_PARALLEL_COMBINED (stmt) = 1;
35135 return stmt;
35136 }
35137 /* When combined with distribute, parallel has to be followed by for.
35138 #pragma omp target parallel is allowed though. */
35139 else if (cclauses
35140 && (mask & (OMP_CLAUSE_MASK_1
35141 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35142 {
35143 error_at (loc, "expected %<for%> after %qs", p_name);
35144 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35145 return NULL_TREE;
35146 }
35147 else if (!flag_openmp) /* flag_openmp_simd */
35148 {
35149 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35150 return NULL_TREE;
35151 }
35152 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35153 {
35154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35155 const char *p = IDENTIFIER_POINTER (id);
35156 if (strcmp (p, "sections") == 0)
35157 {
35158 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35159 cclauses = cclauses_buf;
35160
35161 cp_lexer_consume_token (parser->lexer);
35162 block = begin_omp_parallel ();
35163 save = cp_parser_begin_omp_structured_block (parser);
35164 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35165 cp_parser_end_omp_structured_block (parser, save);
35166 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35167 block);
35168 OMP_PARALLEL_COMBINED (stmt) = 1;
35169 return stmt;
35170 }
35171 }
35172
35173 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35174 cclauses == NULL);
35175 if (cclauses)
35176 {
35177 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35178 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35179 }
35180
35181 block = begin_omp_parallel ();
35182 save = cp_parser_begin_omp_structured_block (parser);
35183 cp_parser_statement (parser, NULL_TREE, false, if_p);
35184 cp_parser_end_omp_structured_block (parser, save);
35185 stmt = finish_omp_parallel (clauses, block);
35186 return stmt;
35187 }
35188
35189 /* OpenMP 2.5:
35190 # pragma omp single single-clause[optseq] new-line
35191 structured-block */
35192
35193 #define OMP_SINGLE_CLAUSE_MASK \
35194 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35198
35199 static tree
35200 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35201 {
35202 tree stmt = make_node (OMP_SINGLE);
35203 TREE_TYPE (stmt) = void_type_node;
35204
35205 OMP_SINGLE_CLAUSES (stmt)
35206 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35207 "#pragma omp single", pragma_tok);
35208 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35209
35210 return add_stmt (stmt);
35211 }
35212
35213 /* OpenMP 3.0:
35214 # pragma omp task task-clause[optseq] new-line
35215 structured-block */
35216
35217 #define OMP_TASK_CLAUSE_MASK \
35218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35228
35229 static tree
35230 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35231 {
35232 tree clauses, block;
35233 unsigned int save;
35234
35235 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35236 "#pragma omp task", pragma_tok);
35237 block = begin_omp_task ();
35238 save = cp_parser_begin_omp_structured_block (parser);
35239 cp_parser_statement (parser, NULL_TREE, false, if_p);
35240 cp_parser_end_omp_structured_block (parser, save);
35241 return finish_omp_task (clauses, block);
35242 }
35243
35244 /* OpenMP 3.0:
35245 # pragma omp taskwait new-line */
35246
35247 static void
35248 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35249 {
35250 cp_parser_require_pragma_eol (parser, pragma_tok);
35251 finish_omp_taskwait ();
35252 }
35253
35254 /* OpenMP 3.1:
35255 # pragma omp taskyield new-line */
35256
35257 static void
35258 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35259 {
35260 cp_parser_require_pragma_eol (parser, pragma_tok);
35261 finish_omp_taskyield ();
35262 }
35263
35264 /* OpenMP 4.0:
35265 # pragma omp taskgroup new-line
35266 structured-block */
35267
35268 static tree
35269 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35270 {
35271 cp_parser_require_pragma_eol (parser, pragma_tok);
35272 return c_finish_omp_taskgroup (input_location,
35273 cp_parser_omp_structured_block (parser,
35274 if_p));
35275 }
35276
35277
35278 /* OpenMP 2.5:
35279 # pragma omp threadprivate (variable-list) */
35280
35281 static void
35282 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35283 {
35284 tree vars;
35285
35286 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35287 cp_parser_require_pragma_eol (parser, pragma_tok);
35288
35289 finish_omp_threadprivate (vars);
35290 }
35291
35292 /* OpenMP 4.0:
35293 # pragma omp cancel cancel-clause[optseq] new-line */
35294
35295 #define OMP_CANCEL_CLAUSE_MASK \
35296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35301
35302 static void
35303 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35304 {
35305 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35306 "#pragma omp cancel", pragma_tok);
35307 finish_omp_cancel (clauses);
35308 }
35309
35310 /* OpenMP 4.0:
35311 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35312
35313 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35318
35319 static void
35320 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35321 enum pragma_context context)
35322 {
35323 tree clauses;
35324 bool point_seen = false;
35325
35326 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35327 {
35328 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35329 const char *p = IDENTIFIER_POINTER (id);
35330
35331 if (strcmp (p, "point") == 0)
35332 {
35333 cp_lexer_consume_token (parser->lexer);
35334 point_seen = true;
35335 }
35336 }
35337 if (!point_seen)
35338 {
35339 cp_parser_error (parser, "expected %<point%>");
35340 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35341 return;
35342 }
35343
35344 if (context != pragma_compound)
35345 {
35346 if (context == pragma_stmt)
35347 error_at (pragma_tok->location,
35348 "%<#pragma %s%> may only be used in compound statements",
35349 "omp cancellation point");
35350 else
35351 cp_parser_error (parser, "expected declaration specifiers");
35352 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35353 return;
35354 }
35355
35356 clauses = cp_parser_omp_all_clauses (parser,
35357 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35358 "#pragma omp cancellation point",
35359 pragma_tok);
35360 finish_omp_cancellation_point (clauses);
35361 }
35362
35363 /* OpenMP 4.0:
35364 #pragma omp distribute distribute-clause[optseq] new-line
35365 for-loop */
35366
35367 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35373
35374 static tree
35375 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35376 char *p_name, omp_clause_mask mask, tree *cclauses,
35377 bool *if_p)
35378 {
35379 tree clauses, sb, ret;
35380 unsigned int save;
35381 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35382
35383 strcat (p_name, " distribute");
35384 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35385
35386 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35387 {
35388 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35389 const char *p = IDENTIFIER_POINTER (id);
35390 bool simd = false;
35391 bool parallel = false;
35392
35393 if (strcmp (p, "simd") == 0)
35394 simd = true;
35395 else
35396 parallel = strcmp (p, "parallel") == 0;
35397 if (parallel || simd)
35398 {
35399 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35400 if (cclauses == NULL)
35401 cclauses = cclauses_buf;
35402 cp_lexer_consume_token (parser->lexer);
35403 if (!flag_openmp) /* flag_openmp_simd */
35404 {
35405 if (simd)
35406 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35407 cclauses, if_p);
35408 else
35409 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35410 cclauses, if_p);
35411 }
35412 sb = begin_omp_structured_block ();
35413 save = cp_parser_begin_omp_structured_block (parser);
35414 if (simd)
35415 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35416 cclauses, if_p);
35417 else
35418 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35419 cclauses, if_p);
35420 cp_parser_end_omp_structured_block (parser, save);
35421 tree body = finish_omp_structured_block (sb);
35422 if (ret == NULL)
35423 return ret;
35424 ret = make_node (OMP_DISTRIBUTE);
35425 TREE_TYPE (ret) = void_type_node;
35426 OMP_FOR_BODY (ret) = body;
35427 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35428 SET_EXPR_LOCATION (ret, loc);
35429 add_stmt (ret);
35430 return ret;
35431 }
35432 }
35433 if (!flag_openmp) /* flag_openmp_simd */
35434 {
35435 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35436 return NULL_TREE;
35437 }
35438
35439 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35440 cclauses == NULL);
35441 if (cclauses)
35442 {
35443 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35444 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35445 }
35446
35447 sb = begin_omp_structured_block ();
35448 save = cp_parser_begin_omp_structured_block (parser);
35449
35450 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35451
35452 cp_parser_end_omp_structured_block (parser, save);
35453 add_stmt (finish_omp_structured_block (sb));
35454
35455 return ret;
35456 }
35457
35458 /* OpenMP 4.0:
35459 # pragma omp teams teams-clause[optseq] new-line
35460 structured-block */
35461
35462 #define OMP_TEAMS_CLAUSE_MASK \
35463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35470
35471 static tree
35472 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35473 char *p_name, omp_clause_mask mask, tree *cclauses,
35474 bool *if_p)
35475 {
35476 tree clauses, sb, ret;
35477 unsigned int save;
35478 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35479
35480 strcat (p_name, " teams");
35481 mask |= OMP_TEAMS_CLAUSE_MASK;
35482
35483 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35484 {
35485 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35486 const char *p = IDENTIFIER_POINTER (id);
35487 if (strcmp (p, "distribute") == 0)
35488 {
35489 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35490 if (cclauses == NULL)
35491 cclauses = cclauses_buf;
35492
35493 cp_lexer_consume_token (parser->lexer);
35494 if (!flag_openmp) /* flag_openmp_simd */
35495 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35496 cclauses, if_p);
35497 sb = begin_omp_structured_block ();
35498 save = cp_parser_begin_omp_structured_block (parser);
35499 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35500 cclauses, if_p);
35501 cp_parser_end_omp_structured_block (parser, save);
35502 tree body = finish_omp_structured_block (sb);
35503 if (ret == NULL)
35504 return ret;
35505 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35506 ret = make_node (OMP_TEAMS);
35507 TREE_TYPE (ret) = void_type_node;
35508 OMP_TEAMS_CLAUSES (ret) = clauses;
35509 OMP_TEAMS_BODY (ret) = body;
35510 OMP_TEAMS_COMBINED (ret) = 1;
35511 SET_EXPR_LOCATION (ret, loc);
35512 return add_stmt (ret);
35513 }
35514 }
35515 if (!flag_openmp) /* flag_openmp_simd */
35516 {
35517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35518 return NULL_TREE;
35519 }
35520
35521 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35522 cclauses == NULL);
35523 if (cclauses)
35524 {
35525 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35526 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35527 }
35528
35529 tree stmt = make_node (OMP_TEAMS);
35530 TREE_TYPE (stmt) = void_type_node;
35531 OMP_TEAMS_CLAUSES (stmt) = clauses;
35532 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35533 SET_EXPR_LOCATION (stmt, loc);
35534
35535 return add_stmt (stmt);
35536 }
35537
35538 /* OpenMP 4.0:
35539 # pragma omp target data target-data-clause[optseq] new-line
35540 structured-block */
35541
35542 #define OMP_TARGET_DATA_CLAUSE_MASK \
35543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35547
35548 static tree
35549 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35550 {
35551 tree clauses
35552 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35553 "#pragma omp target data", pragma_tok);
35554 int map_seen = 0;
35555 for (tree *pc = &clauses; *pc;)
35556 {
35557 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35558 switch (OMP_CLAUSE_MAP_KIND (*pc))
35559 {
35560 case GOMP_MAP_TO:
35561 case GOMP_MAP_ALWAYS_TO:
35562 case GOMP_MAP_FROM:
35563 case GOMP_MAP_ALWAYS_FROM:
35564 case GOMP_MAP_TOFROM:
35565 case GOMP_MAP_ALWAYS_TOFROM:
35566 case GOMP_MAP_ALLOC:
35567 map_seen = 3;
35568 break;
35569 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35570 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35571 case GOMP_MAP_ALWAYS_POINTER:
35572 break;
35573 default:
35574 map_seen |= 1;
35575 error_at (OMP_CLAUSE_LOCATION (*pc),
35576 "%<#pragma omp target data%> with map-type other "
35577 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35578 "on %<map%> clause");
35579 *pc = OMP_CLAUSE_CHAIN (*pc);
35580 continue;
35581 }
35582 pc = &OMP_CLAUSE_CHAIN (*pc);
35583 }
35584
35585 if (map_seen != 3)
35586 {
35587 if (map_seen == 0)
35588 error_at (pragma_tok->location,
35589 "%<#pragma omp target data%> must contain at least "
35590 "one %<map%> clause");
35591 return NULL_TREE;
35592 }
35593
35594 tree stmt = make_node (OMP_TARGET_DATA);
35595 TREE_TYPE (stmt) = void_type_node;
35596 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35597
35598 keep_next_level (true);
35599 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35600
35601 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35602 return add_stmt (stmt);
35603 }
35604
35605 /* OpenMP 4.5:
35606 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35607 structured-block */
35608
35609 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35610 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35615
35616 static tree
35617 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35618 enum pragma_context context)
35619 {
35620 bool data_seen = false;
35621 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35622 {
35623 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35624 const char *p = IDENTIFIER_POINTER (id);
35625
35626 if (strcmp (p, "data") == 0)
35627 {
35628 cp_lexer_consume_token (parser->lexer);
35629 data_seen = true;
35630 }
35631 }
35632 if (!data_seen)
35633 {
35634 cp_parser_error (parser, "expected %<data%>");
35635 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35636 return NULL_TREE;
35637 }
35638
35639 if (context == pragma_stmt)
35640 {
35641 error_at (pragma_tok->location,
35642 "%<#pragma %s%> may only be used in compound statements",
35643 "omp target enter data");
35644 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35645 return NULL_TREE;
35646 }
35647
35648 tree clauses
35649 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35650 "#pragma omp target enter data", pragma_tok);
35651 int map_seen = 0;
35652 for (tree *pc = &clauses; *pc;)
35653 {
35654 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35655 switch (OMP_CLAUSE_MAP_KIND (*pc))
35656 {
35657 case GOMP_MAP_TO:
35658 case GOMP_MAP_ALWAYS_TO:
35659 case GOMP_MAP_ALLOC:
35660 map_seen = 3;
35661 break;
35662 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35663 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35664 case GOMP_MAP_ALWAYS_POINTER:
35665 break;
35666 default:
35667 map_seen |= 1;
35668 error_at (OMP_CLAUSE_LOCATION (*pc),
35669 "%<#pragma omp target enter data%> with map-type other "
35670 "than %<to%> or %<alloc%> on %<map%> clause");
35671 *pc = OMP_CLAUSE_CHAIN (*pc);
35672 continue;
35673 }
35674 pc = &OMP_CLAUSE_CHAIN (*pc);
35675 }
35676
35677 if (map_seen != 3)
35678 {
35679 if (map_seen == 0)
35680 error_at (pragma_tok->location,
35681 "%<#pragma omp target enter data%> must contain at least "
35682 "one %<map%> clause");
35683 return NULL_TREE;
35684 }
35685
35686 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35687 TREE_TYPE (stmt) = void_type_node;
35688 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35689 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35690 return add_stmt (stmt);
35691 }
35692
35693 /* OpenMP 4.5:
35694 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35695 structured-block */
35696
35697 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35703
35704 static tree
35705 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35706 enum pragma_context context)
35707 {
35708 bool data_seen = false;
35709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35710 {
35711 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35712 const char *p = IDENTIFIER_POINTER (id);
35713
35714 if (strcmp (p, "data") == 0)
35715 {
35716 cp_lexer_consume_token (parser->lexer);
35717 data_seen = true;
35718 }
35719 }
35720 if (!data_seen)
35721 {
35722 cp_parser_error (parser, "expected %<data%>");
35723 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35724 return NULL_TREE;
35725 }
35726
35727 if (context == pragma_stmt)
35728 {
35729 error_at (pragma_tok->location,
35730 "%<#pragma %s%> may only be used in compound statements",
35731 "omp target exit data");
35732 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35733 return NULL_TREE;
35734 }
35735
35736 tree clauses
35737 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35738 "#pragma omp target exit data", pragma_tok);
35739 int map_seen = 0;
35740 for (tree *pc = &clauses; *pc;)
35741 {
35742 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35743 switch (OMP_CLAUSE_MAP_KIND (*pc))
35744 {
35745 case GOMP_MAP_FROM:
35746 case GOMP_MAP_ALWAYS_FROM:
35747 case GOMP_MAP_RELEASE:
35748 case GOMP_MAP_DELETE:
35749 map_seen = 3;
35750 break;
35751 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35752 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35753 case GOMP_MAP_ALWAYS_POINTER:
35754 break;
35755 default:
35756 map_seen |= 1;
35757 error_at (OMP_CLAUSE_LOCATION (*pc),
35758 "%<#pragma omp target exit data%> with map-type other "
35759 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35760 " clause");
35761 *pc = OMP_CLAUSE_CHAIN (*pc);
35762 continue;
35763 }
35764 pc = &OMP_CLAUSE_CHAIN (*pc);
35765 }
35766
35767 if (map_seen != 3)
35768 {
35769 if (map_seen == 0)
35770 error_at (pragma_tok->location,
35771 "%<#pragma omp target exit data%> must contain at least "
35772 "one %<map%> clause");
35773 return NULL_TREE;
35774 }
35775
35776 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35777 TREE_TYPE (stmt) = void_type_node;
35778 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35779 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35780 return add_stmt (stmt);
35781 }
35782
35783 /* OpenMP 4.0:
35784 # pragma omp target update target-update-clause[optseq] new-line */
35785
35786 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35793
35794 static bool
35795 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35796 enum pragma_context context)
35797 {
35798 if (context == pragma_stmt)
35799 {
35800 error_at (pragma_tok->location,
35801 "%<#pragma %s%> may only be used in compound statements",
35802 "omp target update");
35803 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35804 return false;
35805 }
35806
35807 tree clauses
35808 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35809 "#pragma omp target update", pragma_tok);
35810 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35811 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35812 {
35813 error_at (pragma_tok->location,
35814 "%<#pragma omp target update%> must contain at least one "
35815 "%<from%> or %<to%> clauses");
35816 return false;
35817 }
35818
35819 tree stmt = make_node (OMP_TARGET_UPDATE);
35820 TREE_TYPE (stmt) = void_type_node;
35821 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35822 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35823 add_stmt (stmt);
35824 return false;
35825 }
35826
35827 /* OpenMP 4.0:
35828 # pragma omp target target-clause[optseq] new-line
35829 structured-block */
35830
35831 #define OMP_TARGET_CLAUSE_MASK \
35832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35841
35842 static bool
35843 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35844 enum pragma_context context, bool *if_p)
35845 {
35846 tree *pc = NULL, stmt;
35847
35848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35849 {
35850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35851 const char *p = IDENTIFIER_POINTER (id);
35852 enum tree_code ccode = ERROR_MARK;
35853
35854 if (strcmp (p, "teams") == 0)
35855 ccode = OMP_TEAMS;
35856 else if (strcmp (p, "parallel") == 0)
35857 ccode = OMP_PARALLEL;
35858 else if (strcmp (p, "simd") == 0)
35859 ccode = OMP_SIMD;
35860 if (ccode != ERROR_MARK)
35861 {
35862 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35863 char p_name[sizeof ("#pragma omp target teams distribute "
35864 "parallel for simd")];
35865
35866 cp_lexer_consume_token (parser->lexer);
35867 strcpy (p_name, "#pragma omp target");
35868 if (!flag_openmp) /* flag_openmp_simd */
35869 {
35870 tree stmt;
35871 switch (ccode)
35872 {
35873 case OMP_TEAMS:
35874 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35875 OMP_TARGET_CLAUSE_MASK,
35876 cclauses, if_p);
35877 break;
35878 case OMP_PARALLEL:
35879 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35880 OMP_TARGET_CLAUSE_MASK,
35881 cclauses, if_p);
35882 break;
35883 case OMP_SIMD:
35884 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35885 OMP_TARGET_CLAUSE_MASK,
35886 cclauses, if_p);
35887 break;
35888 default:
35889 gcc_unreachable ();
35890 }
35891 return stmt != NULL_TREE;
35892 }
35893 keep_next_level (true);
35894 tree sb = begin_omp_structured_block (), ret;
35895 unsigned save = cp_parser_begin_omp_structured_block (parser);
35896 switch (ccode)
35897 {
35898 case OMP_TEAMS:
35899 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35900 OMP_TARGET_CLAUSE_MASK, cclauses,
35901 if_p);
35902 break;
35903 case OMP_PARALLEL:
35904 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35905 OMP_TARGET_CLAUSE_MASK, cclauses,
35906 if_p);
35907 break;
35908 case OMP_SIMD:
35909 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35910 OMP_TARGET_CLAUSE_MASK, cclauses,
35911 if_p);
35912 break;
35913 default:
35914 gcc_unreachable ();
35915 }
35916 cp_parser_end_omp_structured_block (parser, save);
35917 tree body = finish_omp_structured_block (sb);
35918 if (ret == NULL_TREE)
35919 return false;
35920 if (ccode == OMP_TEAMS && !processing_template_decl)
35921 {
35922 /* For combined target teams, ensure the num_teams and
35923 thread_limit clause expressions are evaluated on the host,
35924 before entering the target construct. */
35925 tree c;
35926 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35927 c; c = OMP_CLAUSE_CHAIN (c))
35928 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35929 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35930 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35931 {
35932 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35933 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35934 if (expr == error_mark_node)
35935 continue;
35936 tree tmp = TARGET_EXPR_SLOT (expr);
35937 add_stmt (expr);
35938 OMP_CLAUSE_OPERAND (c, 0) = expr;
35939 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35940 OMP_CLAUSE_FIRSTPRIVATE);
35941 OMP_CLAUSE_DECL (tc) = tmp;
35942 OMP_CLAUSE_CHAIN (tc)
35943 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35944 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35945 }
35946 }
35947 tree stmt = make_node (OMP_TARGET);
35948 TREE_TYPE (stmt) = void_type_node;
35949 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35950 OMP_TARGET_BODY (stmt) = body;
35951 OMP_TARGET_COMBINED (stmt) = 1;
35952 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35953 add_stmt (stmt);
35954 pc = &OMP_TARGET_CLAUSES (stmt);
35955 goto check_clauses;
35956 }
35957 else if (!flag_openmp) /* flag_openmp_simd */
35958 {
35959 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35960 return false;
35961 }
35962 else if (strcmp (p, "data") == 0)
35963 {
35964 cp_lexer_consume_token (parser->lexer);
35965 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35966 return true;
35967 }
35968 else if (strcmp (p, "enter") == 0)
35969 {
35970 cp_lexer_consume_token (parser->lexer);
35971 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35972 return false;
35973 }
35974 else if (strcmp (p, "exit") == 0)
35975 {
35976 cp_lexer_consume_token (parser->lexer);
35977 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35978 return false;
35979 }
35980 else if (strcmp (p, "update") == 0)
35981 {
35982 cp_lexer_consume_token (parser->lexer);
35983 return cp_parser_omp_target_update (parser, pragma_tok, context);
35984 }
35985 }
35986 if (!flag_openmp) /* flag_openmp_simd */
35987 {
35988 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35989 return false;
35990 }
35991
35992 stmt = make_node (OMP_TARGET);
35993 TREE_TYPE (stmt) = void_type_node;
35994
35995 OMP_TARGET_CLAUSES (stmt)
35996 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35997 "#pragma omp target", pragma_tok);
35998 pc = &OMP_TARGET_CLAUSES (stmt);
35999 keep_next_level (true);
36000 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36001
36002 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36003 add_stmt (stmt);
36004
36005 check_clauses:
36006 while (*pc)
36007 {
36008 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36009 switch (OMP_CLAUSE_MAP_KIND (*pc))
36010 {
36011 case GOMP_MAP_TO:
36012 case GOMP_MAP_ALWAYS_TO:
36013 case GOMP_MAP_FROM:
36014 case GOMP_MAP_ALWAYS_FROM:
36015 case GOMP_MAP_TOFROM:
36016 case GOMP_MAP_ALWAYS_TOFROM:
36017 case GOMP_MAP_ALLOC:
36018 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36019 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36020 case GOMP_MAP_ALWAYS_POINTER:
36021 break;
36022 default:
36023 error_at (OMP_CLAUSE_LOCATION (*pc),
36024 "%<#pragma omp target%> with map-type other "
36025 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36026 "on %<map%> clause");
36027 *pc = OMP_CLAUSE_CHAIN (*pc);
36028 continue;
36029 }
36030 pc = &OMP_CLAUSE_CHAIN (*pc);
36031 }
36032 return true;
36033 }
36034
36035 /* OpenACC 2.0:
36036 # pragma acc cache (variable-list) new-line
36037 */
36038
36039 static tree
36040 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36041 {
36042 tree stmt, clauses;
36043
36044 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36045 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36046
36047 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36048
36049 stmt = make_node (OACC_CACHE);
36050 TREE_TYPE (stmt) = void_type_node;
36051 OACC_CACHE_CLAUSES (stmt) = clauses;
36052 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36053 add_stmt (stmt);
36054
36055 return stmt;
36056 }
36057
36058 /* OpenACC 2.0:
36059 # pragma acc data oacc-data-clause[optseq] new-line
36060 structured-block */
36061
36062 #define OACC_DATA_CLAUSE_MASK \
36063 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36074
36075 static tree
36076 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36077 {
36078 tree stmt, clauses, block;
36079 unsigned int save;
36080
36081 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36082 "#pragma acc data", pragma_tok);
36083
36084 block = begin_omp_parallel ();
36085 save = cp_parser_begin_omp_structured_block (parser);
36086 cp_parser_statement (parser, NULL_TREE, false, if_p);
36087 cp_parser_end_omp_structured_block (parser, save);
36088 stmt = finish_oacc_data (clauses, block);
36089 return stmt;
36090 }
36091
36092 /* OpenACC 2.0:
36093 # pragma acc host_data <clauses> new-line
36094 structured-block */
36095
36096 #define OACC_HOST_DATA_CLAUSE_MASK \
36097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36098
36099 static tree
36100 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36101 {
36102 tree stmt, clauses, block;
36103 unsigned int save;
36104
36105 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36106 "#pragma acc host_data", pragma_tok);
36107
36108 block = begin_omp_parallel ();
36109 save = cp_parser_begin_omp_structured_block (parser);
36110 cp_parser_statement (parser, NULL_TREE, false, if_p);
36111 cp_parser_end_omp_structured_block (parser, save);
36112 stmt = finish_oacc_host_data (clauses, block);
36113 return stmt;
36114 }
36115
36116 /* OpenACC 2.0:
36117 # pragma acc declare oacc-data-clause[optseq] new-line
36118 */
36119
36120 #define OACC_DECLARE_CLAUSE_MASK \
36121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36133
36134 static tree
36135 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36136 {
36137 tree clauses, stmt;
36138 bool error = false;
36139
36140 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36141 "#pragma acc declare", pragma_tok, true);
36142
36143
36144 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36145 {
36146 error_at (pragma_tok->location,
36147 "no valid clauses specified in %<#pragma acc declare%>");
36148 return NULL_TREE;
36149 }
36150
36151 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36152 {
36153 location_t loc = OMP_CLAUSE_LOCATION (t);
36154 tree decl = OMP_CLAUSE_DECL (t);
36155 if (!DECL_P (decl))
36156 {
36157 error_at (loc, "array section in %<#pragma acc declare%>");
36158 error = true;
36159 continue;
36160 }
36161 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36162 switch (OMP_CLAUSE_MAP_KIND (t))
36163 {
36164 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36165 case GOMP_MAP_FORCE_ALLOC:
36166 case GOMP_MAP_FORCE_TO:
36167 case GOMP_MAP_FORCE_DEVICEPTR:
36168 case GOMP_MAP_DEVICE_RESIDENT:
36169 break;
36170
36171 case GOMP_MAP_LINK:
36172 if (!global_bindings_p ()
36173 && (TREE_STATIC (decl)
36174 || !DECL_EXTERNAL (decl)))
36175 {
36176 error_at (loc,
36177 "%qD must be a global variable in "
36178 "%<#pragma acc declare link%>",
36179 decl);
36180 error = true;
36181 continue;
36182 }
36183 break;
36184
36185 default:
36186 if (global_bindings_p ())
36187 {
36188 error_at (loc, "invalid OpenACC clause at file scope");
36189 error = true;
36190 continue;
36191 }
36192 if (DECL_EXTERNAL (decl))
36193 {
36194 error_at (loc,
36195 "invalid use of %<extern%> variable %qD "
36196 "in %<#pragma acc declare%>", decl);
36197 error = true;
36198 continue;
36199 }
36200 else if (TREE_PUBLIC (decl))
36201 {
36202 error_at (loc,
36203 "invalid use of %<global%> variable %qD "
36204 "in %<#pragma acc declare%>", decl);
36205 error = true;
36206 continue;
36207 }
36208 break;
36209 }
36210
36211 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36212 || lookup_attribute ("omp declare target link",
36213 DECL_ATTRIBUTES (decl)))
36214 {
36215 error_at (loc, "variable %qD used more than once with "
36216 "%<#pragma acc declare%>", decl);
36217 error = true;
36218 continue;
36219 }
36220
36221 if (!error)
36222 {
36223 tree id;
36224
36225 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36226 id = get_identifier ("omp declare target link");
36227 else
36228 id = get_identifier ("omp declare target");
36229
36230 DECL_ATTRIBUTES (decl)
36231 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36232 if (global_bindings_p ())
36233 {
36234 symtab_node *node = symtab_node::get (decl);
36235 if (node != NULL)
36236 {
36237 node->offloadable = 1;
36238 if (ENABLE_OFFLOADING)
36239 {
36240 g->have_offload = true;
36241 if (is_a <varpool_node *> (node))
36242 vec_safe_push (offload_vars, decl);
36243 }
36244 }
36245 }
36246 }
36247 }
36248
36249 if (error || global_bindings_p ())
36250 return NULL_TREE;
36251
36252 stmt = make_node (OACC_DECLARE);
36253 TREE_TYPE (stmt) = void_type_node;
36254 OACC_DECLARE_CLAUSES (stmt) = clauses;
36255 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36256
36257 add_stmt (stmt);
36258
36259 return NULL_TREE;
36260 }
36261
36262 /* OpenACC 2.0:
36263 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36264
36265 or
36266
36267 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36268
36269 LOC is the location of the #pragma token.
36270 */
36271
36272 #define OACC_ENTER_DATA_CLAUSE_MASK \
36273 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36280
36281 #define OACC_EXIT_DATA_CLAUSE_MASK \
36282 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36287
36288 static tree
36289 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36290 bool enter)
36291 {
36292 location_t loc = pragma_tok->location;
36293 tree stmt, clauses;
36294 const char *p = "";
36295
36296 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36297 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36298
36299 if (strcmp (p, "data") != 0)
36300 {
36301 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36302 enter ? "enter" : "exit");
36303 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36304 return NULL_TREE;
36305 }
36306
36307 cp_lexer_consume_token (parser->lexer);
36308
36309 if (enter)
36310 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36311 "#pragma acc enter data", pragma_tok);
36312 else
36313 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36314 "#pragma acc exit data", pragma_tok);
36315
36316 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36317 {
36318 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36319 enter ? "enter" : "exit");
36320 return NULL_TREE;
36321 }
36322
36323 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36324 TREE_TYPE (stmt) = void_type_node;
36325 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36326 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36327 add_stmt (stmt);
36328 return stmt;
36329 }
36330
36331 /* OpenACC 2.0:
36332 # pragma acc loop oacc-loop-clause[optseq] new-line
36333 structured-block */
36334
36335 #define OACC_LOOP_CLAUSE_MASK \
36336 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36346
36347 static tree
36348 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36349 omp_clause_mask mask, tree *cclauses, bool *if_p)
36350 {
36351 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36352
36353 strcat (p_name, " loop");
36354 mask |= OACC_LOOP_CLAUSE_MASK;
36355
36356 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36357 cclauses == NULL);
36358 if (cclauses)
36359 {
36360 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36361 if (*cclauses)
36362 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36363 if (clauses)
36364 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36365 }
36366
36367 tree block = begin_omp_structured_block ();
36368 int save = cp_parser_begin_omp_structured_block (parser);
36369 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36370 cp_parser_end_omp_structured_block (parser, save);
36371 add_stmt (finish_omp_structured_block (block));
36372
36373 return stmt;
36374 }
36375
36376 /* OpenACC 2.0:
36377 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36378 structured-block
36379
36380 or
36381
36382 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36383 structured-block
36384 */
36385
36386 #define OACC_KERNELS_CLAUSE_MASK \
36387 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36401
36402 #define OACC_PARALLEL_CLAUSE_MASK \
36403 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36423
36424 static tree
36425 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36426 char *p_name, bool *if_p)
36427 {
36428 omp_clause_mask mask;
36429 enum tree_code code;
36430 switch (cp_parser_pragma_kind (pragma_tok))
36431 {
36432 case PRAGMA_OACC_KERNELS:
36433 strcat (p_name, " kernels");
36434 mask = OACC_KERNELS_CLAUSE_MASK;
36435 code = OACC_KERNELS;
36436 break;
36437 case PRAGMA_OACC_PARALLEL:
36438 strcat (p_name, " parallel");
36439 mask = OACC_PARALLEL_CLAUSE_MASK;
36440 code = OACC_PARALLEL;
36441 break;
36442 default:
36443 gcc_unreachable ();
36444 }
36445
36446 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36447 {
36448 const char *p
36449 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36450 if (strcmp (p, "loop") == 0)
36451 {
36452 cp_lexer_consume_token (parser->lexer);
36453 tree block = begin_omp_parallel ();
36454 tree clauses;
36455 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36456 if_p);
36457 return finish_omp_construct (code, block, clauses);
36458 }
36459 }
36460
36461 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36462
36463 tree block = begin_omp_parallel ();
36464 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36465 cp_parser_statement (parser, NULL_TREE, false, if_p);
36466 cp_parser_end_omp_structured_block (parser, save);
36467 return finish_omp_construct (code, block, clauses);
36468 }
36469
36470 /* OpenACC 2.0:
36471 # pragma acc update oacc-update-clause[optseq] new-line
36472 */
36473
36474 #define OACC_UPDATE_CLAUSE_MASK \
36475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36481
36482 static tree
36483 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36484 {
36485 tree stmt, clauses;
36486
36487 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36488 "#pragma acc update", pragma_tok);
36489
36490 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36491 {
36492 error_at (pragma_tok->location,
36493 "%<#pragma acc update%> must contain at least one "
36494 "%<device%> or %<host%> or %<self%> clause");
36495 return NULL_TREE;
36496 }
36497
36498 stmt = make_node (OACC_UPDATE);
36499 TREE_TYPE (stmt) = void_type_node;
36500 OACC_UPDATE_CLAUSES (stmt) = clauses;
36501 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36502 add_stmt (stmt);
36503 return stmt;
36504 }
36505
36506 /* OpenACC 2.0:
36507 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36508
36509 LOC is the location of the #pragma token.
36510 */
36511
36512 #define OACC_WAIT_CLAUSE_MASK \
36513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36514
36515 static tree
36516 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36517 {
36518 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36519 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36520
36521 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36522 list = cp_parser_oacc_wait_list (parser, loc, list);
36523
36524 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36525 "#pragma acc wait", pragma_tok);
36526
36527 stmt = c_finish_oacc_wait (loc, list, clauses);
36528 stmt = finish_expr_stmt (stmt);
36529
36530 return stmt;
36531 }
36532
36533 /* OpenMP 4.0:
36534 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36535
36536 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36543
36544 static void
36545 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36546 enum pragma_context context)
36547 {
36548 bool first_p = parser->omp_declare_simd == NULL;
36549 cp_omp_declare_simd_data data;
36550 if (first_p)
36551 {
36552 data.error_seen = false;
36553 data.fndecl_seen = false;
36554 data.tokens = vNULL;
36555 data.clauses = NULL_TREE;
36556 /* It is safe to take the address of a local variable; it will only be
36557 used while this scope is live. */
36558 parser->omp_declare_simd = &data;
36559 }
36560
36561 /* Store away all pragma tokens. */
36562 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36563 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36564 cp_lexer_consume_token (parser->lexer);
36565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36566 parser->omp_declare_simd->error_seen = true;
36567 cp_parser_require_pragma_eol (parser, pragma_tok);
36568 struct cp_token_cache *cp
36569 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36570 parser->omp_declare_simd->tokens.safe_push (cp);
36571
36572 if (first_p)
36573 {
36574 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36575 cp_parser_pragma (parser, context, NULL);
36576 switch (context)
36577 {
36578 case pragma_external:
36579 cp_parser_declaration (parser);
36580 break;
36581 case pragma_member:
36582 cp_parser_member_declaration (parser);
36583 break;
36584 case pragma_objc_icode:
36585 cp_parser_block_declaration (parser, /*statement_p=*/false);
36586 break;
36587 default:
36588 cp_parser_declaration_statement (parser);
36589 break;
36590 }
36591 if (parser->omp_declare_simd
36592 && !parser->omp_declare_simd->error_seen
36593 && !parser->omp_declare_simd->fndecl_seen)
36594 error_at (pragma_tok->location,
36595 "%<#pragma omp declare simd%> not immediately followed by "
36596 "function declaration or definition");
36597 data.tokens.release ();
36598 parser->omp_declare_simd = NULL;
36599 }
36600 }
36601
36602 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36603 This function is modelled similar to the late parsing of omp declare
36604 simd. */
36605
36606 static tree
36607 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36608 {
36609 struct cp_token_cache *ce;
36610 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36611 int ii = 0;
36612
36613 if (parser->omp_declare_simd != NULL
36614 || lookup_attribute ("simd", attrs))
36615 {
36616 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36617 "used in the same function marked as a Cilk Plus SIMD-enabled "
36618 "function");
36619 parser->cilk_simd_fn_info->tokens.release ();
36620 XDELETE (parser->cilk_simd_fn_info);
36621 parser->cilk_simd_fn_info = NULL;
36622 return attrs;
36623 }
36624 if (!info->error_seen && info->fndecl_seen)
36625 {
36626 error ("vector attribute not immediately followed by a single function"
36627 " declaration or definition");
36628 info->error_seen = true;
36629 }
36630 if (info->error_seen)
36631 return attrs;
36632
36633 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36634 {
36635 tree c, cl;
36636
36637 cp_parser_push_lexer_for_tokens (parser, ce);
36638 parser->lexer->in_pragma = true;
36639 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36640 "SIMD-enabled functions attribute",
36641 NULL);
36642 cp_parser_pop_lexer (parser);
36643 if (cl)
36644 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36645
36646 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36647 TREE_CHAIN (c) = attrs;
36648 attrs = c;
36649
36650 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36651 TREE_CHAIN (c) = attrs;
36652 if (processing_template_decl)
36653 ATTR_IS_DEPENDENT (c) = 1;
36654 attrs = c;
36655 }
36656 info->fndecl_seen = true;
36657 parser->cilk_simd_fn_info->tokens.release ();
36658 XDELETE (parser->cilk_simd_fn_info);
36659 parser->cilk_simd_fn_info = NULL;
36660 return attrs;
36661 }
36662
36663 /* Finalize #pragma omp declare simd clauses after direct declarator has
36664 been parsed, and put that into "omp declare simd" attribute. */
36665
36666 static tree
36667 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36668 {
36669 struct cp_token_cache *ce;
36670 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36671 int i;
36672
36673 if (!data->error_seen && data->fndecl_seen)
36674 {
36675 error ("%<#pragma omp declare simd%> not immediately followed by "
36676 "a single function declaration or definition");
36677 data->error_seen = true;
36678 }
36679 if (data->error_seen)
36680 return attrs;
36681
36682 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36683 {
36684 tree c, cl;
36685
36686 cp_parser_push_lexer_for_tokens (parser, ce);
36687 parser->lexer->in_pragma = true;
36688 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36689 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36690 cp_lexer_consume_token (parser->lexer);
36691 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36692 "#pragma omp declare simd", pragma_tok);
36693 cp_parser_pop_lexer (parser);
36694 if (cl)
36695 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36696 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36697 TREE_CHAIN (c) = attrs;
36698 if (processing_template_decl)
36699 ATTR_IS_DEPENDENT (c) = 1;
36700 attrs = c;
36701 }
36702
36703 data->fndecl_seen = true;
36704 return attrs;
36705 }
36706
36707
36708 /* OpenMP 4.0:
36709 # pragma omp declare target new-line
36710 declarations and definitions
36711 # pragma omp end declare target new-line
36712
36713 OpenMP 4.5:
36714 # pragma omp declare target ( extended-list ) new-line
36715
36716 # pragma omp declare target declare-target-clauses[seq] new-line */
36717
36718 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36721
36722 static void
36723 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36724 {
36725 tree clauses = NULL_TREE;
36726 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36727 clauses
36728 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36729 "#pragma omp declare target", pragma_tok);
36730 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36731 {
36732 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36733 clauses);
36734 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36735 cp_parser_require_pragma_eol (parser, pragma_tok);
36736 }
36737 else
36738 {
36739 cp_parser_require_pragma_eol (parser, pragma_tok);
36740 scope_chain->omp_declare_target_attribute++;
36741 return;
36742 }
36743 if (scope_chain->omp_declare_target_attribute)
36744 error_at (pragma_tok->location,
36745 "%<#pragma omp declare target%> with clauses in between "
36746 "%<#pragma omp declare target%> without clauses and "
36747 "%<#pragma omp end declare target%>");
36748 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36749 {
36750 tree t = OMP_CLAUSE_DECL (c), id;
36751 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36752 tree at2 = lookup_attribute ("omp declare target link",
36753 DECL_ATTRIBUTES (t));
36754 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36755 {
36756 id = get_identifier ("omp declare target link");
36757 std::swap (at1, at2);
36758 }
36759 else
36760 id = get_identifier ("omp declare target");
36761 if (at2)
36762 {
36763 error_at (OMP_CLAUSE_LOCATION (c),
36764 "%qD specified both in declare target %<link%> and %<to%>"
36765 " clauses", t);
36766 continue;
36767 }
36768 if (!at1)
36769 {
36770 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36771 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36772 continue;
36773
36774 symtab_node *node = symtab_node::get (t);
36775 if (node != NULL)
36776 {
36777 node->offloadable = 1;
36778 if (ENABLE_OFFLOADING)
36779 {
36780 g->have_offload = true;
36781 if (is_a <varpool_node *> (node))
36782 vec_safe_push (offload_vars, t);
36783 }
36784 }
36785 }
36786 }
36787 }
36788
36789 static void
36790 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36791 {
36792 const char *p = "";
36793 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36794 {
36795 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36796 p = IDENTIFIER_POINTER (id);
36797 }
36798 if (strcmp (p, "declare") == 0)
36799 {
36800 cp_lexer_consume_token (parser->lexer);
36801 p = "";
36802 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36803 {
36804 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36805 p = IDENTIFIER_POINTER (id);
36806 }
36807 if (strcmp (p, "target") == 0)
36808 cp_lexer_consume_token (parser->lexer);
36809 else
36810 {
36811 cp_parser_error (parser, "expected %<target%>");
36812 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36813 return;
36814 }
36815 }
36816 else
36817 {
36818 cp_parser_error (parser, "expected %<declare%>");
36819 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36820 return;
36821 }
36822 cp_parser_require_pragma_eol (parser, pragma_tok);
36823 if (!scope_chain->omp_declare_target_attribute)
36824 error_at (pragma_tok->location,
36825 "%<#pragma omp end declare target%> without corresponding "
36826 "%<#pragma omp declare target%>");
36827 else
36828 scope_chain->omp_declare_target_attribute--;
36829 }
36830
36831 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36832 expression and optional initializer clause of
36833 #pragma omp declare reduction. We store the expression(s) as
36834 either 3, 6 or 7 special statements inside of the artificial function's
36835 body. The first two statements are DECL_EXPRs for the artificial
36836 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36837 expression that uses those variables.
36838 If there was any INITIALIZER clause, this is followed by further statements,
36839 the fourth and fifth statements are DECL_EXPRs for the artificial
36840 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36841 constructor variant (first token after open paren is not omp_priv),
36842 then the sixth statement is a statement with the function call expression
36843 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36844 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36845 to initialize the OMP_PRIV artificial variable and there is seventh
36846 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36847
36848 static bool
36849 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36850 {
36851 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36852 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36853 type = TREE_TYPE (type);
36854 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36855 DECL_ARTIFICIAL (omp_out) = 1;
36856 pushdecl (omp_out);
36857 add_decl_expr (omp_out);
36858 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36859 DECL_ARTIFICIAL (omp_in) = 1;
36860 pushdecl (omp_in);
36861 add_decl_expr (omp_in);
36862 tree combiner;
36863 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36864
36865 keep_next_level (true);
36866 tree block = begin_omp_structured_block ();
36867 combiner = cp_parser_expression (parser);
36868 finish_expr_stmt (combiner);
36869 block = finish_omp_structured_block (block);
36870 add_stmt (block);
36871
36872 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36873 return false;
36874
36875 const char *p = "";
36876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36877 {
36878 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36879 p = IDENTIFIER_POINTER (id);
36880 }
36881
36882 if (strcmp (p, "initializer") == 0)
36883 {
36884 cp_lexer_consume_token (parser->lexer);
36885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36886 return false;
36887
36888 p = "";
36889 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36890 {
36891 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36892 p = IDENTIFIER_POINTER (id);
36893 }
36894
36895 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36896 DECL_ARTIFICIAL (omp_priv) = 1;
36897 pushdecl (omp_priv);
36898 add_decl_expr (omp_priv);
36899 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36900 DECL_ARTIFICIAL (omp_orig) = 1;
36901 pushdecl (omp_orig);
36902 add_decl_expr (omp_orig);
36903
36904 keep_next_level (true);
36905 block = begin_omp_structured_block ();
36906
36907 bool ctor = false;
36908 if (strcmp (p, "omp_priv") == 0)
36909 {
36910 bool is_direct_init, is_non_constant_init;
36911 ctor = true;
36912 cp_lexer_consume_token (parser->lexer);
36913 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36914 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36915 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36916 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36917 == CPP_CLOSE_PAREN
36918 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36919 == CPP_CLOSE_PAREN))
36920 {
36921 finish_omp_structured_block (block);
36922 error ("invalid initializer clause");
36923 return false;
36924 }
36925 initializer = cp_parser_initializer (parser, &is_direct_init,
36926 &is_non_constant_init);
36927 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36928 NULL_TREE, LOOKUP_ONLYCONVERTING);
36929 }
36930 else
36931 {
36932 cp_parser_parse_tentatively (parser);
36933 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36934 /*check_dependency_p=*/true,
36935 /*template_p=*/NULL,
36936 /*declarator_p=*/false,
36937 /*optional_p=*/false);
36938 vec<tree, va_gc> *args;
36939 if (fn_name == error_mark_node
36940 || cp_parser_error_occurred (parser)
36941 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36942 || ((args = cp_parser_parenthesized_expression_list
36943 (parser, non_attr, /*cast_p=*/false,
36944 /*allow_expansion_p=*/true,
36945 /*non_constant_p=*/NULL)),
36946 cp_parser_error_occurred (parser)))
36947 {
36948 finish_omp_structured_block (block);
36949 cp_parser_abort_tentative_parse (parser);
36950 cp_parser_error (parser, "expected id-expression (arguments)");
36951 return false;
36952 }
36953 unsigned int i;
36954 tree arg;
36955 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36956 if (arg == omp_priv
36957 || (TREE_CODE (arg) == ADDR_EXPR
36958 && TREE_OPERAND (arg, 0) == omp_priv))
36959 break;
36960 cp_parser_abort_tentative_parse (parser);
36961 if (arg == NULL_TREE)
36962 error ("one of the initializer call arguments should be %<omp_priv%>"
36963 " or %<&omp_priv%>");
36964 initializer = cp_parser_postfix_expression (parser, false, false, false,
36965 false, NULL);
36966 finish_expr_stmt (initializer);
36967 }
36968
36969 block = finish_omp_structured_block (block);
36970 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36971 add_stmt (block);
36972
36973 if (ctor)
36974 add_decl_expr (omp_orig);
36975
36976 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36977 return false;
36978 }
36979
36980 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36981 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36982
36983 return true;
36984 }
36985
36986 /* OpenMP 4.0
36987 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36988 initializer-clause[opt] new-line
36989
36990 initializer-clause:
36991 initializer (omp_priv initializer)
36992 initializer (function-name (argument-list)) */
36993
36994 static void
36995 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36996 enum pragma_context)
36997 {
36998 auto_vec<tree> types;
36999 enum tree_code reduc_code = ERROR_MARK;
37000 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37001 unsigned int i;
37002 cp_token *first_token;
37003 cp_token_cache *cp;
37004 int errs;
37005 void *p;
37006
37007 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37008 p = obstack_alloc (&declarator_obstack, 0);
37009
37010 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37011 goto fail;
37012
37013 switch (cp_lexer_peek_token (parser->lexer)->type)
37014 {
37015 case CPP_PLUS:
37016 reduc_code = PLUS_EXPR;
37017 break;
37018 case CPP_MULT:
37019 reduc_code = MULT_EXPR;
37020 break;
37021 case CPP_MINUS:
37022 reduc_code = MINUS_EXPR;
37023 break;
37024 case CPP_AND:
37025 reduc_code = BIT_AND_EXPR;
37026 break;
37027 case CPP_XOR:
37028 reduc_code = BIT_XOR_EXPR;
37029 break;
37030 case CPP_OR:
37031 reduc_code = BIT_IOR_EXPR;
37032 break;
37033 case CPP_AND_AND:
37034 reduc_code = TRUTH_ANDIF_EXPR;
37035 break;
37036 case CPP_OR_OR:
37037 reduc_code = TRUTH_ORIF_EXPR;
37038 break;
37039 case CPP_NAME:
37040 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37041 break;
37042 default:
37043 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37044 "%<|%>, %<&&%>, %<||%> or identifier");
37045 goto fail;
37046 }
37047
37048 if (reduc_code != ERROR_MARK)
37049 cp_lexer_consume_token (parser->lexer);
37050
37051 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37052 if (reduc_id == error_mark_node)
37053 goto fail;
37054
37055 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37056 goto fail;
37057
37058 /* Types may not be defined in declare reduction type list. */
37059 const char *saved_message;
37060 saved_message = parser->type_definition_forbidden_message;
37061 parser->type_definition_forbidden_message
37062 = G_("types may not be defined in declare reduction type list");
37063 bool saved_colon_corrects_to_scope_p;
37064 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37065 parser->colon_corrects_to_scope_p = false;
37066 bool saved_colon_doesnt_start_class_def_p;
37067 saved_colon_doesnt_start_class_def_p
37068 = parser->colon_doesnt_start_class_def_p;
37069 parser->colon_doesnt_start_class_def_p = true;
37070
37071 while (true)
37072 {
37073 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37074 type = cp_parser_type_id (parser);
37075 if (type == error_mark_node)
37076 ;
37077 else if (ARITHMETIC_TYPE_P (type)
37078 && (orig_reduc_id == NULL_TREE
37079 || (TREE_CODE (type) != COMPLEX_TYPE
37080 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37081 "min") == 0
37082 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37083 "max") == 0))))
37084 error_at (loc, "predeclared arithmetic type %qT in "
37085 "%<#pragma omp declare reduction%>", type);
37086 else if (TREE_CODE (type) == FUNCTION_TYPE
37087 || TREE_CODE (type) == METHOD_TYPE
37088 || TREE_CODE (type) == ARRAY_TYPE)
37089 error_at (loc, "function or array type %qT in "
37090 "%<#pragma omp declare reduction%>", type);
37091 else if (TREE_CODE (type) == REFERENCE_TYPE)
37092 error_at (loc, "reference type %qT in "
37093 "%<#pragma omp declare reduction%>", type);
37094 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37095 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37096 "%<#pragma omp declare reduction%>", type);
37097 else
37098 types.safe_push (type);
37099
37100 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37101 cp_lexer_consume_token (parser->lexer);
37102 else
37103 break;
37104 }
37105
37106 /* Restore the saved message. */
37107 parser->type_definition_forbidden_message = saved_message;
37108 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37109 parser->colon_doesnt_start_class_def_p
37110 = saved_colon_doesnt_start_class_def_p;
37111
37112 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37113 || types.is_empty ())
37114 {
37115 fail:
37116 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37117 goto done;
37118 }
37119
37120 first_token = cp_lexer_peek_token (parser->lexer);
37121 cp = NULL;
37122 errs = errorcount;
37123 FOR_EACH_VEC_ELT (types, i, type)
37124 {
37125 tree fntype
37126 = build_function_type_list (void_type_node,
37127 cp_build_reference_type (type, false),
37128 NULL_TREE);
37129 tree this_reduc_id = reduc_id;
37130 if (!dependent_type_p (type))
37131 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37132 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37133 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37134 DECL_ARTIFICIAL (fndecl) = 1;
37135 DECL_EXTERNAL (fndecl) = 1;
37136 DECL_DECLARED_INLINE_P (fndecl) = 1;
37137 DECL_IGNORED_P (fndecl) = 1;
37138 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37139 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37140 DECL_ATTRIBUTES (fndecl)
37141 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37142 DECL_ATTRIBUTES (fndecl));
37143 if (processing_template_decl)
37144 fndecl = push_template_decl (fndecl);
37145 bool block_scope = false;
37146 tree block = NULL_TREE;
37147 if (current_function_decl)
37148 {
37149 block_scope = true;
37150 DECL_CONTEXT (fndecl) = global_namespace;
37151 if (!processing_template_decl)
37152 pushdecl (fndecl);
37153 }
37154 else if (current_class_type)
37155 {
37156 if (cp == NULL)
37157 {
37158 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37159 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37160 cp_lexer_consume_token (parser->lexer);
37161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37162 goto fail;
37163 cp = cp_token_cache_new (first_token,
37164 cp_lexer_peek_nth_token (parser->lexer,
37165 2));
37166 }
37167 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37168 finish_member_declaration (fndecl);
37169 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37170 DECL_PENDING_INLINE_P (fndecl) = 1;
37171 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37172 continue;
37173 }
37174 else
37175 {
37176 DECL_CONTEXT (fndecl) = current_namespace;
37177 pushdecl (fndecl);
37178 }
37179 if (!block_scope)
37180 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37181 else
37182 block = begin_omp_structured_block ();
37183 if (cp)
37184 {
37185 cp_parser_push_lexer_for_tokens (parser, cp);
37186 parser->lexer->in_pragma = true;
37187 }
37188 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37189 {
37190 if (!block_scope)
37191 finish_function (0);
37192 else
37193 DECL_CONTEXT (fndecl) = current_function_decl;
37194 if (cp)
37195 cp_parser_pop_lexer (parser);
37196 goto fail;
37197 }
37198 if (cp)
37199 cp_parser_pop_lexer (parser);
37200 if (!block_scope)
37201 finish_function (0);
37202 else
37203 {
37204 DECL_CONTEXT (fndecl) = current_function_decl;
37205 block = finish_omp_structured_block (block);
37206 if (TREE_CODE (block) == BIND_EXPR)
37207 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37208 else if (TREE_CODE (block) == STATEMENT_LIST)
37209 DECL_SAVED_TREE (fndecl) = block;
37210 if (processing_template_decl)
37211 add_decl_expr (fndecl);
37212 }
37213 cp_check_omp_declare_reduction (fndecl);
37214 if (cp == NULL && types.length () > 1)
37215 cp = cp_token_cache_new (first_token,
37216 cp_lexer_peek_nth_token (parser->lexer, 2));
37217 if (errs != errorcount)
37218 break;
37219 }
37220
37221 cp_parser_require_pragma_eol (parser, pragma_tok);
37222
37223 done:
37224 /* Free any declarators allocated. */
37225 obstack_free (&declarator_obstack, p);
37226 }
37227
37228 /* OpenMP 4.0
37229 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37230 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37231 initializer-clause[opt] new-line
37232 #pragma omp declare target new-line */
37233
37234 static void
37235 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37236 enum pragma_context context)
37237 {
37238 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37239 {
37240 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37241 const char *p = IDENTIFIER_POINTER (id);
37242
37243 if (strcmp (p, "simd") == 0)
37244 {
37245 cp_lexer_consume_token (parser->lexer);
37246 cp_parser_omp_declare_simd (parser, pragma_tok,
37247 context);
37248 return;
37249 }
37250 cp_ensure_no_omp_declare_simd (parser);
37251 if (strcmp (p, "reduction") == 0)
37252 {
37253 cp_lexer_consume_token (parser->lexer);
37254 cp_parser_omp_declare_reduction (parser, pragma_tok,
37255 context);
37256 return;
37257 }
37258 if (!flag_openmp) /* flag_openmp_simd */
37259 {
37260 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37261 return;
37262 }
37263 if (strcmp (p, "target") == 0)
37264 {
37265 cp_lexer_consume_token (parser->lexer);
37266 cp_parser_omp_declare_target (parser, pragma_tok);
37267 return;
37268 }
37269 }
37270 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37271 "or %<target%>");
37272 cp_parser_require_pragma_eol (parser, pragma_tok);
37273 }
37274
37275 /* OpenMP 4.5:
37276 #pragma omp taskloop taskloop-clause[optseq] new-line
37277 for-loop
37278
37279 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37280 for-loop */
37281
37282 #define OMP_TASKLOOP_CLAUSE_MASK \
37283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37297
37298 static tree
37299 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37300 char *p_name, omp_clause_mask mask, tree *cclauses,
37301 bool *if_p)
37302 {
37303 tree clauses, sb, ret;
37304 unsigned int save;
37305 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37306
37307 strcat (p_name, " taskloop");
37308 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37309
37310 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37311 {
37312 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37313 const char *p = IDENTIFIER_POINTER (id);
37314
37315 if (strcmp (p, "simd") == 0)
37316 {
37317 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37318 if (cclauses == NULL)
37319 cclauses = cclauses_buf;
37320
37321 cp_lexer_consume_token (parser->lexer);
37322 if (!flag_openmp) /* flag_openmp_simd */
37323 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37324 cclauses, if_p);
37325 sb = begin_omp_structured_block ();
37326 save = cp_parser_begin_omp_structured_block (parser);
37327 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37328 cclauses, if_p);
37329 cp_parser_end_omp_structured_block (parser, save);
37330 tree body = finish_omp_structured_block (sb);
37331 if (ret == NULL)
37332 return ret;
37333 ret = make_node (OMP_TASKLOOP);
37334 TREE_TYPE (ret) = void_type_node;
37335 OMP_FOR_BODY (ret) = body;
37336 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37337 SET_EXPR_LOCATION (ret, loc);
37338 add_stmt (ret);
37339 return ret;
37340 }
37341 }
37342 if (!flag_openmp) /* flag_openmp_simd */
37343 {
37344 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37345 return NULL_TREE;
37346 }
37347
37348 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37349 cclauses == NULL);
37350 if (cclauses)
37351 {
37352 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37353 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37354 }
37355
37356 sb = begin_omp_structured_block ();
37357 save = cp_parser_begin_omp_structured_block (parser);
37358
37359 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37360 if_p);
37361
37362 cp_parser_end_omp_structured_block (parser, save);
37363 add_stmt (finish_omp_structured_block (sb));
37364
37365 return ret;
37366 }
37367
37368
37369 /* OpenACC 2.0:
37370 # pragma acc routine oacc-routine-clause[optseq] new-line
37371 function-definition
37372
37373 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37374 */
37375
37376 #define OACC_ROUTINE_CLAUSE_MASK \
37377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37381
37382
37383 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37384 component, which must resolve to a declared namespace-scope
37385 function. The clauses are either processed directly (for a named
37386 function), or defered until the immediatley following declaration
37387 is parsed. */
37388
37389 static void
37390 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37391 enum pragma_context context)
37392 {
37393 gcc_checking_assert (context == pragma_external);
37394 /* The checking for "another pragma following this one" in the "no optional
37395 '( name )'" case makes sure that we dont re-enter. */
37396 gcc_checking_assert (parser->oacc_routine == NULL);
37397
37398 cp_oacc_routine_data data;
37399 data.error_seen = false;
37400 data.fndecl_seen = false;
37401 data.tokens = vNULL;
37402 data.clauses = NULL_TREE;
37403 data.loc = pragma_tok->location;
37404 /* It is safe to take the address of a local variable; it will only be
37405 used while this scope is live. */
37406 parser->oacc_routine = &data;
37407
37408 /* Look for optional '( name )'. */
37409 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37410 {
37411 cp_lexer_consume_token (parser->lexer); /* '(' */
37412
37413 /* We parse the name as an id-expression. If it resolves to
37414 anything other than a non-overloaded function at namespace
37415 scope, it's an error. */
37416 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37417 tree name = cp_parser_id_expression (parser,
37418 /*template_keyword_p=*/false,
37419 /*check_dependency_p=*/false,
37420 /*template_p=*/NULL,
37421 /*declarator_p=*/false,
37422 /*optional_p=*/false);
37423 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37424 if (name != error_mark_node && decl == error_mark_node)
37425 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37426
37427 if (decl == error_mark_node
37428 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37429 {
37430 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37431 parser->oacc_routine = NULL;
37432 return;
37433 }
37434
37435 data.clauses
37436 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37437 "#pragma acc routine",
37438 cp_lexer_peek_token (parser->lexer));
37439
37440 if (decl && is_overloaded_fn (decl)
37441 && (TREE_CODE (decl) != FUNCTION_DECL
37442 || DECL_FUNCTION_TEMPLATE_P (decl)))
37443 {
37444 error_at (name_loc,
37445 "%<#pragma acc routine%> names a set of overloads");
37446 parser->oacc_routine = NULL;
37447 return;
37448 }
37449
37450 /* Perhaps we should use the same rule as declarations in different
37451 namespaces? */
37452 if (!DECL_NAMESPACE_SCOPE_P (decl))
37453 {
37454 error_at (name_loc,
37455 "%qD does not refer to a namespace scope function", decl);
37456 parser->oacc_routine = NULL;
37457 return;
37458 }
37459
37460 if (TREE_CODE (decl) != FUNCTION_DECL)
37461 {
37462 error_at (name_loc, "%qD does not refer to a function", decl);
37463 parser->oacc_routine = NULL;
37464 return;
37465 }
37466
37467 cp_finalize_oacc_routine (parser, decl, false);
37468 parser->oacc_routine = NULL;
37469 }
37470 else /* No optional '( name )'. */
37471 {
37472 /* Store away all pragma tokens. */
37473 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37474 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37475 cp_lexer_consume_token (parser->lexer);
37476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37477 parser->oacc_routine->error_seen = true;
37478 cp_parser_require_pragma_eol (parser, pragma_tok);
37479 struct cp_token_cache *cp
37480 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37481 parser->oacc_routine->tokens.safe_push (cp);
37482
37483 /* Emit a helpful diagnostic if there's another pragma following this
37484 one. */
37485 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37486 {
37487 cp_ensure_no_oacc_routine (parser);
37488 data.tokens.release ();
37489 /* ..., and then just keep going. */
37490 return;
37491 }
37492
37493 /* We only have to consider the pragma_external case here. */
37494 cp_parser_declaration (parser);
37495 if (parser->oacc_routine
37496 && !parser->oacc_routine->fndecl_seen)
37497 cp_ensure_no_oacc_routine (parser);
37498 else
37499 parser->oacc_routine = NULL;
37500 data.tokens.release ();
37501 }
37502 }
37503
37504 /* Finalize #pragma acc routine clauses after direct declarator has
37505 been parsed. */
37506
37507 static tree
37508 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37509 {
37510 struct cp_token_cache *ce;
37511 cp_oacc_routine_data *data = parser->oacc_routine;
37512
37513 if (!data->error_seen && data->fndecl_seen)
37514 {
37515 error_at (data->loc,
37516 "%<#pragma acc routine%> not immediately followed by "
37517 "a single function declaration or definition");
37518 data->error_seen = true;
37519 }
37520 if (data->error_seen)
37521 return attrs;
37522
37523 gcc_checking_assert (data->tokens.length () == 1);
37524 ce = data->tokens[0];
37525
37526 cp_parser_push_lexer_for_tokens (parser, ce);
37527 parser->lexer->in_pragma = true;
37528 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37529
37530 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37531 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37532 parser->oacc_routine->clauses
37533 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37534 "#pragma acc routine", pragma_tok);
37535 cp_parser_pop_lexer (parser);
37536 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37537 fndecl_seen. */
37538
37539 return attrs;
37540 }
37541
37542 /* Apply any saved OpenACC routine clauses to a just-parsed
37543 declaration. */
37544
37545 static void
37546 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37547 {
37548 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37549 {
37550 /* Keep going if we're in error reporting mode. */
37551 if (parser->oacc_routine->error_seen
37552 || fndecl == error_mark_node)
37553 return;
37554
37555 if (parser->oacc_routine->fndecl_seen)
37556 {
37557 error_at (parser->oacc_routine->loc,
37558 "%<#pragma acc routine%> not immediately followed by"
37559 " a single function declaration or definition");
37560 parser->oacc_routine = NULL;
37561 return;
37562 }
37563 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37564 {
37565 cp_ensure_no_oacc_routine (parser);
37566 return;
37567 }
37568
37569 if (oacc_get_fn_attrib (fndecl))
37570 {
37571 error_at (parser->oacc_routine->loc,
37572 "%<#pragma acc routine%> already applied to %qD", fndecl);
37573 parser->oacc_routine = NULL;
37574 return;
37575 }
37576
37577 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37578 {
37579 error_at (parser->oacc_routine->loc,
37580 TREE_USED (fndecl)
37581 ? G_("%<#pragma acc routine%> must be applied before use")
37582 : G_("%<#pragma acc routine%> must be applied before "
37583 "definition"));
37584 parser->oacc_routine = NULL;
37585 return;
37586 }
37587
37588 /* Process the routine's dimension clauses. */
37589 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37590 oacc_replace_fn_attrib (fndecl, dims);
37591
37592 /* Add an "omp declare target" attribute. */
37593 DECL_ATTRIBUTES (fndecl)
37594 = tree_cons (get_identifier ("omp declare target"),
37595 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37596
37597 /* Don't unset parser->oacc_routine here: we may still need it to
37598 diagnose wrong usage. But, remember that we've used this "#pragma acc
37599 routine". */
37600 parser->oacc_routine->fndecl_seen = true;
37601 }
37602 }
37603
37604 /* Main entry point to OpenMP statement pragmas. */
37605
37606 static void
37607 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37608 {
37609 tree stmt;
37610 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37611 omp_clause_mask mask (0);
37612
37613 switch (cp_parser_pragma_kind (pragma_tok))
37614 {
37615 case PRAGMA_OACC_ATOMIC:
37616 cp_parser_omp_atomic (parser, pragma_tok);
37617 return;
37618 case PRAGMA_OACC_CACHE:
37619 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37620 break;
37621 case PRAGMA_OACC_DATA:
37622 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37623 break;
37624 case PRAGMA_OACC_ENTER_DATA:
37625 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37626 break;
37627 case PRAGMA_OACC_EXIT_DATA:
37628 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37629 break;
37630 case PRAGMA_OACC_HOST_DATA:
37631 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37632 break;
37633 case PRAGMA_OACC_KERNELS:
37634 case PRAGMA_OACC_PARALLEL:
37635 strcpy (p_name, "#pragma acc");
37636 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37637 if_p);
37638 break;
37639 case PRAGMA_OACC_LOOP:
37640 strcpy (p_name, "#pragma acc");
37641 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37642 if_p);
37643 break;
37644 case PRAGMA_OACC_UPDATE:
37645 stmt = cp_parser_oacc_update (parser, pragma_tok);
37646 break;
37647 case PRAGMA_OACC_WAIT:
37648 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37649 break;
37650 case PRAGMA_OMP_ATOMIC:
37651 cp_parser_omp_atomic (parser, pragma_tok);
37652 return;
37653 case PRAGMA_OMP_CRITICAL:
37654 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37655 break;
37656 case PRAGMA_OMP_DISTRIBUTE:
37657 strcpy (p_name, "#pragma omp");
37658 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37659 if_p);
37660 break;
37661 case PRAGMA_OMP_FOR:
37662 strcpy (p_name, "#pragma omp");
37663 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37664 if_p);
37665 break;
37666 case PRAGMA_OMP_MASTER:
37667 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37668 break;
37669 case PRAGMA_OMP_PARALLEL:
37670 strcpy (p_name, "#pragma omp");
37671 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37672 if_p);
37673 break;
37674 case PRAGMA_OMP_SECTIONS:
37675 strcpy (p_name, "#pragma omp");
37676 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37677 break;
37678 case PRAGMA_OMP_SIMD:
37679 strcpy (p_name, "#pragma omp");
37680 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37681 if_p);
37682 break;
37683 case PRAGMA_OMP_SINGLE:
37684 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37685 break;
37686 case PRAGMA_OMP_TASK:
37687 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37688 break;
37689 case PRAGMA_OMP_TASKGROUP:
37690 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37691 break;
37692 case PRAGMA_OMP_TASKLOOP:
37693 strcpy (p_name, "#pragma omp");
37694 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37695 if_p);
37696 break;
37697 case PRAGMA_OMP_TEAMS:
37698 strcpy (p_name, "#pragma omp");
37699 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37700 if_p);
37701 break;
37702 default:
37703 gcc_unreachable ();
37704 }
37705
37706 protected_set_expr_location (stmt, pragma_tok->location);
37707 }
37708 \f
37709 /* Transactional Memory parsing routines. */
37710
37711 /* Parse a transaction attribute.
37712
37713 txn-attribute:
37714 attribute
37715 [ [ identifier ] ]
37716
37717 We use this instead of cp_parser_attributes_opt for transactions to avoid
37718 the pedwarn in C++98 mode. */
37719
37720 static tree
37721 cp_parser_txn_attribute_opt (cp_parser *parser)
37722 {
37723 cp_token *token;
37724 tree attr_name, attr = NULL;
37725
37726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37727 return cp_parser_attributes_opt (parser);
37728
37729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37730 return NULL_TREE;
37731 cp_lexer_consume_token (parser->lexer);
37732 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37733 goto error1;
37734
37735 token = cp_lexer_peek_token (parser->lexer);
37736 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37737 {
37738 token = cp_lexer_consume_token (parser->lexer);
37739
37740 attr_name = (token->type == CPP_KEYWORD
37741 /* For keywords, use the canonical spelling,
37742 not the parsed identifier. */
37743 ? ridpointers[(int) token->keyword]
37744 : token->u.value);
37745 attr = build_tree_list (attr_name, NULL_TREE);
37746 }
37747 else
37748 cp_parser_error (parser, "expected identifier");
37749
37750 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37751 error1:
37752 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37753 return attr;
37754 }
37755
37756 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37757
37758 transaction-statement:
37759 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37760 compound-statement
37761 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37762 */
37763
37764 static tree
37765 cp_parser_transaction (cp_parser *parser, cp_token *token)
37766 {
37767 unsigned char old_in = parser->in_transaction;
37768 unsigned char this_in = 1, new_in;
37769 enum rid keyword = token->keyword;
37770 tree stmt, attrs, noex;
37771
37772 cp_lexer_consume_token (parser->lexer);
37773
37774 if (keyword == RID_TRANSACTION_RELAXED
37775 || keyword == RID_SYNCHRONIZED)
37776 this_in |= TM_STMT_ATTR_RELAXED;
37777 else
37778 {
37779 attrs = cp_parser_txn_attribute_opt (parser);
37780 if (attrs)
37781 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37782 }
37783
37784 /* Parse a noexcept specification. */
37785 if (keyword == RID_ATOMIC_NOEXCEPT)
37786 noex = boolean_true_node;
37787 else if (keyword == RID_ATOMIC_CANCEL)
37788 {
37789 /* cancel-and-throw is unimplemented. */
37790 sorry ("atomic_cancel");
37791 noex = NULL_TREE;
37792 }
37793 else
37794 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37795
37796 /* Keep track if we're in the lexical scope of an outer transaction. */
37797 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37798
37799 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37800
37801 parser->in_transaction = new_in;
37802 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37803 parser->in_transaction = old_in;
37804
37805 finish_transaction_stmt (stmt, NULL, this_in, noex);
37806
37807 return stmt;
37808 }
37809
37810 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37811
37812 transaction-expression:
37813 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37814 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37815 */
37816
37817 static tree
37818 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37819 {
37820 unsigned char old_in = parser->in_transaction;
37821 unsigned char this_in = 1;
37822 cp_token *token;
37823 tree expr, noex;
37824 bool noex_expr;
37825 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37826
37827 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37828 || keyword == RID_TRANSACTION_RELAXED);
37829
37830 if (!flag_tm)
37831 error_at (loc,
37832 keyword == RID_TRANSACTION_RELAXED
37833 ? G_("%<__transaction_relaxed%> without transactional memory "
37834 "support enabled")
37835 : G_("%<__transaction_atomic%> without transactional memory "
37836 "support enabled"));
37837
37838 token = cp_parser_require_keyword (parser, keyword,
37839 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37840 : RT_TRANSACTION_RELAXED));
37841 gcc_assert (token != NULL);
37842
37843 if (keyword == RID_TRANSACTION_RELAXED)
37844 this_in |= TM_STMT_ATTR_RELAXED;
37845
37846 /* Set this early. This might mean that we allow transaction_cancel in
37847 an expression that we find out later actually has to be a constexpr.
37848 However, we expect that cxx_constant_value will be able to deal with
37849 this; also, if the noexcept has no constexpr, then what we parse next
37850 really is a transaction's body. */
37851 parser->in_transaction = this_in;
37852
37853 /* Parse a noexcept specification. */
37854 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37855 true);
37856
37857 if (!noex || !noex_expr
37858 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37859 {
37860 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37861
37862 expr = cp_parser_expression (parser);
37863 expr = finish_parenthesized_expr (expr);
37864
37865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37866 }
37867 else
37868 {
37869 /* The only expression that is available got parsed for the noexcept
37870 already. noexcept is true then. */
37871 expr = noex;
37872 noex = boolean_true_node;
37873 }
37874
37875 expr = build_transaction_expr (token->location, expr, this_in, noex);
37876 parser->in_transaction = old_in;
37877
37878 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37879 return error_mark_node;
37880
37881 return (flag_tm ? expr : error_mark_node);
37882 }
37883
37884 /* Parse a function-transaction-block.
37885
37886 function-transaction-block:
37887 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37888 function-body
37889 __transaction_atomic txn-attribute[opt] function-try-block
37890 __transaction_relaxed ctor-initializer[opt] function-body
37891 __transaction_relaxed function-try-block
37892 */
37893
37894 static bool
37895 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37896 {
37897 unsigned char old_in = parser->in_transaction;
37898 unsigned char new_in = 1;
37899 tree compound_stmt, stmt, attrs;
37900 bool ctor_initializer_p;
37901 cp_token *token;
37902
37903 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37904 || keyword == RID_TRANSACTION_RELAXED);
37905 token = cp_parser_require_keyword (parser, keyword,
37906 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37907 : RT_TRANSACTION_RELAXED));
37908 gcc_assert (token != NULL);
37909
37910 if (keyword == RID_TRANSACTION_RELAXED)
37911 new_in |= TM_STMT_ATTR_RELAXED;
37912 else
37913 {
37914 attrs = cp_parser_txn_attribute_opt (parser);
37915 if (attrs)
37916 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37917 }
37918
37919 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37920
37921 parser->in_transaction = new_in;
37922
37923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37924 ctor_initializer_p = cp_parser_function_try_block (parser);
37925 else
37926 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37927 (parser, /*in_function_try_block=*/false);
37928
37929 parser->in_transaction = old_in;
37930
37931 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37932
37933 return ctor_initializer_p;
37934 }
37935
37936 /* Parse a __transaction_cancel statement.
37937
37938 cancel-statement:
37939 __transaction_cancel txn-attribute[opt] ;
37940 __transaction_cancel txn-attribute[opt] throw-expression ;
37941
37942 ??? Cancel and throw is not yet implemented. */
37943
37944 static tree
37945 cp_parser_transaction_cancel (cp_parser *parser)
37946 {
37947 cp_token *token;
37948 bool is_outer = false;
37949 tree stmt, attrs;
37950
37951 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37952 RT_TRANSACTION_CANCEL);
37953 gcc_assert (token != NULL);
37954
37955 attrs = cp_parser_txn_attribute_opt (parser);
37956 if (attrs)
37957 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37958
37959 /* ??? Parse cancel-and-throw here. */
37960
37961 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37962
37963 if (!flag_tm)
37964 {
37965 error_at (token->location, "%<__transaction_cancel%> without "
37966 "transactional memory support enabled");
37967 return error_mark_node;
37968 }
37969 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37970 {
37971 error_at (token->location, "%<__transaction_cancel%> within a "
37972 "%<__transaction_relaxed%>");
37973 return error_mark_node;
37974 }
37975 else if (is_outer)
37976 {
37977 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37978 && !is_tm_may_cancel_outer (current_function_decl))
37979 {
37980 error_at (token->location, "outer %<__transaction_cancel%> not "
37981 "within outer %<__transaction_atomic%>");
37982 error_at (token->location,
37983 " or a %<transaction_may_cancel_outer%> function");
37984 return error_mark_node;
37985 }
37986 }
37987 else if (parser->in_transaction == 0)
37988 {
37989 error_at (token->location, "%<__transaction_cancel%> not within "
37990 "%<__transaction_atomic%>");
37991 return error_mark_node;
37992 }
37993
37994 stmt = build_tm_abort_call (token->location, is_outer);
37995 add_stmt (stmt);
37996
37997 return stmt;
37998 }
37999 \f
38000 /* The parser. */
38001
38002 static GTY (()) cp_parser *the_parser;
38003
38004 \f
38005 /* Special handling for the first token or line in the file. The first
38006 thing in the file might be #pragma GCC pch_preprocess, which loads a
38007 PCH file, which is a GC collection point. So we need to handle this
38008 first pragma without benefit of an existing lexer structure.
38009
38010 Always returns one token to the caller in *FIRST_TOKEN. This is
38011 either the true first token of the file, or the first token after
38012 the initial pragma. */
38013
38014 static void
38015 cp_parser_initial_pragma (cp_token *first_token)
38016 {
38017 tree name = NULL;
38018
38019 cp_lexer_get_preprocessor_token (NULL, first_token);
38020 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38021 return;
38022
38023 cp_lexer_get_preprocessor_token (NULL, first_token);
38024 if (first_token->type == CPP_STRING)
38025 {
38026 name = first_token->u.value;
38027
38028 cp_lexer_get_preprocessor_token (NULL, first_token);
38029 if (first_token->type != CPP_PRAGMA_EOL)
38030 error_at (first_token->location,
38031 "junk at end of %<#pragma GCC pch_preprocess%>");
38032 }
38033 else
38034 error_at (first_token->location, "expected string literal");
38035
38036 /* Skip to the end of the pragma. */
38037 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38038 cp_lexer_get_preprocessor_token (NULL, first_token);
38039
38040 /* Now actually load the PCH file. */
38041 if (name)
38042 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38043
38044 /* Read one more token to return to our caller. We have to do this
38045 after reading the PCH file in, since its pointers have to be
38046 live. */
38047 cp_lexer_get_preprocessor_token (NULL, first_token);
38048 }
38049
38050 /* Parses the grainsize pragma for the _Cilk_for statement.
38051 Syntax:
38052 #pragma cilk grainsize = <VALUE>. */
38053
38054 static void
38055 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38056 {
38057 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38058 {
38059 tree exp = cp_parser_binary_expression (parser, false, false,
38060 PREC_NOT_OPERATOR, NULL);
38061 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38062 if (!exp || exp == error_mark_node)
38063 {
38064 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38065 return;
38066 }
38067
38068 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38070 cp_parser_cilk_for (parser, exp, if_p);
38071 else
38072 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38073 "%<#pragma cilk grainsize%> is not followed by "
38074 "%<_Cilk_for%>");
38075 return;
38076 }
38077 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38078 }
38079
38080 /* Normal parsing of a pragma token. Here we can (and must) use the
38081 regular lexer. */
38082
38083 static bool
38084 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38085 {
38086 cp_token *pragma_tok;
38087 unsigned int id;
38088 tree stmt;
38089 bool ret;
38090
38091 pragma_tok = cp_lexer_consume_token (parser->lexer);
38092 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38093 parser->lexer->in_pragma = true;
38094
38095 id = cp_parser_pragma_kind (pragma_tok);
38096 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38097 cp_ensure_no_omp_declare_simd (parser);
38098 switch (id)
38099 {
38100 case PRAGMA_GCC_PCH_PREPROCESS:
38101 error_at (pragma_tok->location,
38102 "%<#pragma GCC pch_preprocess%> must be first");
38103 break;
38104
38105 case PRAGMA_OMP_BARRIER:
38106 switch (context)
38107 {
38108 case pragma_compound:
38109 cp_parser_omp_barrier (parser, pragma_tok);
38110 return false;
38111 case pragma_stmt:
38112 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38113 "used in compound statements", "omp barrier");
38114 break;
38115 default:
38116 goto bad_stmt;
38117 }
38118 break;
38119
38120 case PRAGMA_OMP_FLUSH:
38121 switch (context)
38122 {
38123 case pragma_compound:
38124 cp_parser_omp_flush (parser, pragma_tok);
38125 return false;
38126 case pragma_stmt:
38127 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38128 "used in compound statements", "omp flush");
38129 break;
38130 default:
38131 goto bad_stmt;
38132 }
38133 break;
38134
38135 case PRAGMA_OMP_TASKWAIT:
38136 switch (context)
38137 {
38138 case pragma_compound:
38139 cp_parser_omp_taskwait (parser, pragma_tok);
38140 return false;
38141 case pragma_stmt:
38142 error_at (pragma_tok->location,
38143 "%<#pragma %s%> may only be used in compound statements",
38144 "omp taskwait");
38145 break;
38146 default:
38147 goto bad_stmt;
38148 }
38149 break;
38150
38151 case PRAGMA_OMP_TASKYIELD:
38152 switch (context)
38153 {
38154 case pragma_compound:
38155 cp_parser_omp_taskyield (parser, pragma_tok);
38156 return false;
38157 case pragma_stmt:
38158 error_at (pragma_tok->location,
38159 "%<#pragma %s%> may only be used in compound statements",
38160 "omp taskyield");
38161 break;
38162 default:
38163 goto bad_stmt;
38164 }
38165 break;
38166
38167 case PRAGMA_OMP_CANCEL:
38168 switch (context)
38169 {
38170 case pragma_compound:
38171 cp_parser_omp_cancel (parser, pragma_tok);
38172 return false;
38173 case pragma_stmt:
38174 error_at (pragma_tok->location,
38175 "%<#pragma %s%> may only be used in compound statements",
38176 "omp cancel");
38177 break;
38178 default:
38179 goto bad_stmt;
38180 }
38181 break;
38182
38183 case PRAGMA_OMP_CANCELLATION_POINT:
38184 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38185 return false;
38186
38187 case PRAGMA_OMP_THREADPRIVATE:
38188 cp_parser_omp_threadprivate (parser, pragma_tok);
38189 return false;
38190
38191 case PRAGMA_OMP_DECLARE:
38192 cp_parser_omp_declare (parser, pragma_tok, context);
38193 return false;
38194
38195 case PRAGMA_OACC_DECLARE:
38196 cp_parser_oacc_declare (parser, pragma_tok);
38197 return false;
38198
38199 case PRAGMA_OACC_ENTER_DATA:
38200 if (context == pragma_stmt)
38201 {
38202 error_at (pragma_tok->location,
38203 "%<#pragma %s%> may only be used in compound statements",
38204 "acc enter data");
38205 break;
38206 }
38207 else if (context != pragma_compound)
38208 goto bad_stmt;
38209 cp_parser_omp_construct (parser, pragma_tok, if_p);
38210 return true;
38211
38212 case PRAGMA_OACC_EXIT_DATA:
38213 if (context == pragma_stmt)
38214 {
38215 error_at (pragma_tok->location,
38216 "%<#pragma %s%> may only be used in compound statements",
38217 "acc exit data");
38218 break;
38219 }
38220 else if (context != pragma_compound)
38221 goto bad_stmt;
38222 cp_parser_omp_construct (parser, pragma_tok, if_p);
38223 return true;
38224
38225 case PRAGMA_OACC_ROUTINE:
38226 if (context != pragma_external)
38227 {
38228 error_at (pragma_tok->location,
38229 "%<#pragma acc routine%> must be at file scope");
38230 break;
38231 }
38232 cp_parser_oacc_routine (parser, pragma_tok, context);
38233 return false;
38234
38235 case PRAGMA_OACC_UPDATE:
38236 if (context == pragma_stmt)
38237 {
38238 error_at (pragma_tok->location,
38239 "%<#pragma %s%> may only be used in compound statements",
38240 "acc update");
38241 break;
38242 }
38243 else if (context != pragma_compound)
38244 goto bad_stmt;
38245 cp_parser_omp_construct (parser, pragma_tok, if_p);
38246 return true;
38247
38248 case PRAGMA_OACC_WAIT:
38249 if (context == pragma_stmt)
38250 {
38251 error_at (pragma_tok->location,
38252 "%<#pragma %s%> may only be used in compound statements",
38253 "acc wait");
38254 break;
38255 }
38256 else if (context != pragma_compound)
38257 goto bad_stmt;
38258 cp_parser_omp_construct (parser, pragma_tok, if_p);
38259 return true;
38260
38261 case PRAGMA_OACC_ATOMIC:
38262 case PRAGMA_OACC_CACHE:
38263 case PRAGMA_OACC_DATA:
38264 case PRAGMA_OACC_HOST_DATA:
38265 case PRAGMA_OACC_KERNELS:
38266 case PRAGMA_OACC_PARALLEL:
38267 case PRAGMA_OACC_LOOP:
38268 case PRAGMA_OMP_ATOMIC:
38269 case PRAGMA_OMP_CRITICAL:
38270 case PRAGMA_OMP_DISTRIBUTE:
38271 case PRAGMA_OMP_FOR:
38272 case PRAGMA_OMP_MASTER:
38273 case PRAGMA_OMP_PARALLEL:
38274 case PRAGMA_OMP_SECTIONS:
38275 case PRAGMA_OMP_SIMD:
38276 case PRAGMA_OMP_SINGLE:
38277 case PRAGMA_OMP_TASK:
38278 case PRAGMA_OMP_TASKGROUP:
38279 case PRAGMA_OMP_TASKLOOP:
38280 case PRAGMA_OMP_TEAMS:
38281 if (context != pragma_stmt && context != pragma_compound)
38282 goto bad_stmt;
38283 stmt = push_omp_privatization_clauses (false);
38284 cp_parser_omp_construct (parser, pragma_tok, if_p);
38285 pop_omp_privatization_clauses (stmt);
38286 return true;
38287
38288 case PRAGMA_OMP_ORDERED:
38289 if (context != pragma_stmt && context != pragma_compound)
38290 goto bad_stmt;
38291 stmt = push_omp_privatization_clauses (false);
38292 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38293 pop_omp_privatization_clauses (stmt);
38294 return ret;
38295
38296 case PRAGMA_OMP_TARGET:
38297 if (context != pragma_stmt && context != pragma_compound)
38298 goto bad_stmt;
38299 stmt = push_omp_privatization_clauses (false);
38300 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38301 pop_omp_privatization_clauses (stmt);
38302 return ret;
38303
38304 case PRAGMA_OMP_END_DECLARE_TARGET:
38305 cp_parser_omp_end_declare_target (parser, pragma_tok);
38306 return false;
38307
38308 case PRAGMA_OMP_SECTION:
38309 error_at (pragma_tok->location,
38310 "%<#pragma omp section%> may only be used in "
38311 "%<#pragma omp sections%> construct");
38312 break;
38313
38314 case PRAGMA_IVDEP:
38315 {
38316 if (context == pragma_external)
38317 {
38318 error_at (pragma_tok->location,
38319 "%<#pragma GCC ivdep%> must be inside a function");
38320 break;
38321 }
38322 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38323 cp_token *tok;
38324 tok = cp_lexer_peek_token (the_parser->lexer);
38325 if (tok->type != CPP_KEYWORD
38326 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38327 && tok->keyword != RID_DO))
38328 {
38329 cp_parser_error (parser, "for, while or do statement expected");
38330 return false;
38331 }
38332 cp_parser_iteration_statement (parser, if_p, true);
38333 return true;
38334 }
38335
38336 case PRAGMA_CILK_SIMD:
38337 if (context == pragma_external)
38338 {
38339 error_at (pragma_tok->location,
38340 "%<#pragma simd%> must be inside a function");
38341 break;
38342 }
38343 stmt = push_omp_privatization_clauses (false);
38344 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38345 pop_omp_privatization_clauses (stmt);
38346 return true;
38347
38348 case PRAGMA_CILK_GRAINSIZE:
38349 if (context == pragma_external)
38350 {
38351 error_at (pragma_tok->location,
38352 "%<#pragma cilk grainsize%> must be inside a function");
38353 break;
38354 }
38355
38356 /* Ignore the pragma if Cilk Plus is not enabled. */
38357 if (flag_cilkplus)
38358 {
38359 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38360 return true;
38361 }
38362 else
38363 {
38364 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38365 "%<#pragma cilk grainsize%>");
38366 break;
38367 }
38368
38369 default:
38370 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38371 c_invoke_pragma_handler (id);
38372 break;
38373
38374 bad_stmt:
38375 cp_parser_error (parser, "expected declaration specifiers");
38376 break;
38377 }
38378
38379 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38380 return false;
38381 }
38382
38383 /* The interface the pragma parsers have to the lexer. */
38384
38385 enum cpp_ttype
38386 pragma_lex (tree *value, location_t *loc)
38387 {
38388 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38389 enum cpp_ttype ret = tok->type;
38390
38391 *value = tok->u.value;
38392 if (loc)
38393 *loc = tok->location;
38394
38395 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38396 ret = CPP_EOF;
38397 else if (ret == CPP_STRING)
38398 *value = cp_parser_string_literal (the_parser, false, false);
38399 else
38400 {
38401 if (ret == CPP_KEYWORD)
38402 ret = CPP_NAME;
38403 cp_lexer_consume_token (the_parser->lexer);
38404 }
38405
38406 return ret;
38407 }
38408
38409 \f
38410 /* External interface. */
38411
38412 /* Parse one entire translation unit. */
38413
38414 void
38415 c_parse_file (void)
38416 {
38417 static bool already_called = false;
38418
38419 if (already_called)
38420 fatal_error (input_location,
38421 "inter-module optimizations not implemented for C++");
38422 already_called = true;
38423
38424 the_parser = cp_parser_new ();
38425 push_deferring_access_checks (flag_access_control
38426 ? dk_no_deferred : dk_no_check);
38427 cp_parser_translation_unit (the_parser);
38428 the_parser = NULL;
38429 }
38430
38431 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38432 vectorlength clause:
38433 Syntax:
38434 vectorlength ( constant-expression ) */
38435
38436 static tree
38437 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38438 bool is_simd_fn)
38439 {
38440 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38441 tree expr;
38442 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38443 safelen clause. Thus, vectorlength is represented as OMP 4.0
38444 safelen. For SIMD-enabled function it is represented by OMP 4.0
38445 simdlen. */
38446 if (!is_simd_fn)
38447 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38448 loc);
38449 else
38450 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38451 loc);
38452
38453 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38454 return error_mark_node;
38455
38456 expr = cp_parser_constant_expression (parser);
38457 expr = maybe_constant_value (expr);
38458
38459 /* If expr == error_mark_node, then don't emit any errors nor
38460 create a clause. if any of the above functions returns
38461 error mark node then they would have emitted an error message. */
38462 if (expr == error_mark_node)
38463 ;
38464 else if (!TREE_TYPE (expr)
38465 || !TREE_CONSTANT (expr)
38466 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38467 error_at (loc, "vectorlength must be an integer constant");
38468 else if (TREE_CONSTANT (expr)
38469 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38470 error_at (loc, "vectorlength must be a power of 2");
38471 else
38472 {
38473 tree c;
38474 if (!is_simd_fn)
38475 {
38476 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38477 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38478 OMP_CLAUSE_CHAIN (c) = clauses;
38479 clauses = c;
38480 }
38481 else
38482 {
38483 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38484 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38485 OMP_CLAUSE_CHAIN (c) = clauses;
38486 clauses = c;
38487 }
38488 }
38489
38490 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38491 return error_mark_node;
38492 return clauses;
38493 }
38494
38495 /* Handles the Cilk Plus #pragma simd linear clause.
38496 Syntax:
38497 linear ( simd-linear-variable-list )
38498
38499 simd-linear-variable-list:
38500 simd-linear-variable
38501 simd-linear-variable-list , simd-linear-variable
38502
38503 simd-linear-variable:
38504 id-expression
38505 id-expression : simd-linear-step
38506
38507 simd-linear-step:
38508 conditional-expression */
38509
38510 static tree
38511 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38512 {
38513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38514
38515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38516 return clauses;
38517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38518 {
38519 cp_parser_error (parser, "expected identifier");
38520 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38521 return error_mark_node;
38522 }
38523
38524 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38525 parser->colon_corrects_to_scope_p = false;
38526 while (1)
38527 {
38528 cp_token *token = cp_lexer_peek_token (parser->lexer);
38529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38530 {
38531 cp_parser_error (parser, "expected variable-name");
38532 clauses = error_mark_node;
38533 break;
38534 }
38535
38536 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38537 false, false);
38538 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38539 token->location);
38540 if (decl == error_mark_node)
38541 {
38542 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38543 token->location);
38544 clauses = error_mark_node;
38545 }
38546 else
38547 {
38548 tree e = NULL_TREE;
38549 tree step_size = integer_one_node;
38550
38551 /* If present, parse the linear step. Otherwise, assume the default
38552 value of 1. */
38553 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38554 {
38555 cp_lexer_consume_token (parser->lexer);
38556
38557 e = cp_parser_assignment_expression (parser);
38558 e = maybe_constant_value (e);
38559
38560 if (e == error_mark_node)
38561 {
38562 /* If an error has occurred, then the whole pragma is
38563 considered ill-formed. Thus, no reason to keep
38564 parsing. */
38565 clauses = error_mark_node;
38566 break;
38567 }
38568 else if (type_dependent_expression_p (e)
38569 || value_dependent_expression_p (e)
38570 || (TREE_TYPE (e)
38571 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38572 && (TREE_CONSTANT (e)
38573 || DECL_P (e))))
38574 step_size = e;
38575 else
38576 cp_parser_error (parser,
38577 "step size must be an integer constant "
38578 "expression or an integer variable");
38579 }
38580
38581 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38582 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38583 OMP_CLAUSE_DECL (l) = decl;
38584 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38585 OMP_CLAUSE_CHAIN (l) = clauses;
38586 clauses = l;
38587 }
38588 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38589 cp_lexer_consume_token (parser->lexer);
38590 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38591 break;
38592 else
38593 {
38594 error_at (cp_lexer_peek_token (parser->lexer)->location,
38595 "expected %<,%> or %<)%> after %qE", decl);
38596 clauses = error_mark_node;
38597 break;
38598 }
38599 }
38600 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38601 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38602 return clauses;
38603 }
38604
38605 /* Returns the name of the next clause. If the clause is not
38606 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38607 token is not consumed. Otherwise, the appropriate enum from the
38608 pragma_simd_clause is returned and the token is consumed. */
38609
38610 static pragma_omp_clause
38611 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38612 {
38613 pragma_omp_clause clause_type;
38614 cp_token *token = cp_lexer_peek_token (parser->lexer);
38615
38616 if (token->keyword == RID_PRIVATE)
38617 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38618 else if (!token->u.value || token->type != CPP_NAME)
38619 return PRAGMA_CILK_CLAUSE_NONE;
38620 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38621 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38622 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38623 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38624 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38625 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38626 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38627 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38628 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38629 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38630 else
38631 return PRAGMA_CILK_CLAUSE_NONE;
38632
38633 cp_lexer_consume_token (parser->lexer);
38634 return clause_type;
38635 }
38636
38637 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38638
38639 static tree
38640 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38641 {
38642 tree clauses = NULL_TREE;
38643
38644 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38645 && clauses != error_mark_node)
38646 {
38647 pragma_omp_clause c_kind;
38648 c_kind = cp_parser_cilk_simd_clause_name (parser);
38649 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38650 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38651 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38652 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38653 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38654 /* Use the OpenMP 4.0 equivalent function. */
38655 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38656 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38657 /* Use the OpenMP 4.0 equivalent function. */
38658 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38659 clauses);
38660 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38661 /* Use the OMP 4.0 equivalent function. */
38662 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38663 clauses);
38664 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38665 /* Use the OMP 4.0 equivalent function. */
38666 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38667 else
38668 {
38669 clauses = error_mark_node;
38670 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38671 break;
38672 }
38673 }
38674
38675 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38676
38677 if (clauses == error_mark_node)
38678 return error_mark_node;
38679 else
38680 return finish_omp_clauses (clauses, C_ORT_CILK);
38681 }
38682
38683 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38684
38685 static void
38686 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38687 {
38688 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38689
38690 if (clauses == error_mark_node)
38691 return;
38692
38693 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38694 {
38695 error_at (cp_lexer_peek_token (parser->lexer)->location,
38696 "for statement expected");
38697 return;
38698 }
38699
38700 tree sb = begin_omp_structured_block ();
38701 int save = cp_parser_begin_omp_structured_block (parser);
38702 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38703 if (ret)
38704 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38705 cp_parser_end_omp_structured_block (parser, save);
38706 add_stmt (finish_omp_structured_block (sb));
38707 }
38708
38709 /* Main entry-point for parsing Cilk Plus _Cilk_for
38710 loops. The return value is error_mark_node
38711 when errors happen and CILK_FOR tree on success. */
38712
38713 static tree
38714 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38715 {
38716 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38717 gcc_unreachable ();
38718
38719 tree sb = begin_omp_structured_block ();
38720 int save = cp_parser_begin_omp_structured_block (parser);
38721
38722 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38723 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38724 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38725 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38726
38727 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38728 if (ret)
38729 cpp_validate_cilk_plus_loop (ret);
38730 else
38731 ret = error_mark_node;
38732
38733 cp_parser_end_omp_structured_block (parser, save);
38734 add_stmt (finish_omp_structured_block (sb));
38735 return ret;
38736 }
38737
38738 /* Create an identifier for a generic parameter type (a synthesized
38739 template parameter implied by `auto' or a concept identifier). */
38740
38741 static GTY(()) int generic_parm_count;
38742 static tree
38743 make_generic_type_name ()
38744 {
38745 char buf[32];
38746 sprintf (buf, "auto:%d", ++generic_parm_count);
38747 return get_identifier (buf);
38748 }
38749
38750 /* Predicate that behaves as is_auto_or_concept but matches the parent
38751 node of the generic type rather than the generic type itself. This
38752 allows for type transformation in add_implicit_template_parms. */
38753
38754 static inline bool
38755 tree_type_is_auto_or_concept (const_tree t)
38756 {
38757 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38758 }
38759
38760 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38761 (creating a new template parameter list if necessary). Returns the newly
38762 created template type parm. */
38763
38764 static tree
38765 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38766 {
38767 gcc_assert (current_binding_level->kind == sk_function_parms);
38768
38769 /* Before committing to modifying any scope, if we're in an
38770 implicit template scope, and we're trying to synthesize a
38771 constrained parameter, try to find a previous parameter with
38772 the same name. This is the same-type rule for abbreviated
38773 function templates.
38774
38775 NOTE: We can generate implicit parameters when tentatively
38776 parsing a nested name specifier, only to reject that parse
38777 later. However, matching the same template-id as part of a
38778 direct-declarator should generate an identical template
38779 parameter, so this rule will merge them. */
38780 if (parser->implicit_template_scope && constr)
38781 {
38782 tree t = parser->implicit_template_parms;
38783 while (t)
38784 {
38785 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38786 {
38787 tree d = TREE_VALUE (t);
38788 if (TREE_CODE (d) == PARM_DECL)
38789 /* Return the TEMPLATE_PARM_INDEX. */
38790 d = DECL_INITIAL (d);
38791 return d;
38792 }
38793 t = TREE_CHAIN (t);
38794 }
38795 }
38796
38797 /* We are either continuing a function template that already contains implicit
38798 template parameters, creating a new fully-implicit function template, or
38799 extending an existing explicit function template with implicit template
38800 parameters. */
38801
38802 cp_binding_level *const entry_scope = current_binding_level;
38803
38804 bool become_template = false;
38805 cp_binding_level *parent_scope = 0;
38806
38807 if (parser->implicit_template_scope)
38808 {
38809 gcc_assert (parser->implicit_template_parms);
38810
38811 current_binding_level = parser->implicit_template_scope;
38812 }
38813 else
38814 {
38815 /* Roll back to the existing template parameter scope (in the case of
38816 extending an explicit function template) or introduce a new template
38817 parameter scope ahead of the function parameter scope (or class scope
38818 in the case of out-of-line member definitions). The function scope is
38819 added back after template parameter synthesis below. */
38820
38821 cp_binding_level *scope = entry_scope;
38822
38823 while (scope->kind == sk_function_parms)
38824 {
38825 parent_scope = scope;
38826 scope = scope->level_chain;
38827 }
38828 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38829 {
38830 /* If not defining a class, then any class scope is a scope level in
38831 an out-of-line member definition. In this case simply wind back
38832 beyond the first such scope to inject the template parameter list.
38833 Otherwise wind back to the class being defined. The latter can
38834 occur in class member friend declarations such as:
38835
38836 class A {
38837 void foo (auto);
38838 };
38839 class B {
38840 friend void A::foo (auto);
38841 };
38842
38843 The template parameter list synthesized for the friend declaration
38844 must be injected in the scope of 'B'. This can also occur in
38845 erroneous cases such as:
38846
38847 struct A {
38848 struct B {
38849 void foo (auto);
38850 };
38851 void B::foo (auto) {}
38852 };
38853
38854 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38855 but, nevertheless, the template parameter list synthesized for the
38856 declarator should be injected into the scope of 'A' as if the
38857 ill-formed template was specified explicitly. */
38858
38859 while (scope->kind == sk_class && !scope->defining_class_p)
38860 {
38861 parent_scope = scope;
38862 scope = scope->level_chain;
38863 }
38864 }
38865
38866 current_binding_level = scope;
38867
38868 if (scope->kind != sk_template_parms
38869 || !function_being_declared_is_template_p (parser))
38870 {
38871 /* Introduce a new template parameter list for implicit template
38872 parameters. */
38873
38874 become_template = true;
38875
38876 parser->implicit_template_scope
38877 = begin_scope (sk_template_parms, NULL);
38878
38879 ++processing_template_decl;
38880
38881 parser->fully_implicit_function_template_p = true;
38882 ++parser->num_template_parameter_lists;
38883 }
38884 else
38885 {
38886 /* Synthesize implicit template parameters at the end of the explicit
38887 template parameter list. */
38888
38889 gcc_assert (current_template_parms);
38890
38891 parser->implicit_template_scope = scope;
38892
38893 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38894 parser->implicit_template_parms
38895 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38896 }
38897 }
38898
38899 /* Synthesize a new template parameter and track the current template
38900 parameter chain with implicit_template_parms. */
38901
38902 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38903 tree synth_id = make_generic_type_name ();
38904 tree synth_tmpl_parm;
38905 bool non_type = false;
38906
38907 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38908 synth_tmpl_parm
38909 = finish_template_type_parm (class_type_node, synth_id);
38910 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38911 synth_tmpl_parm
38912 = finish_constrained_template_template_parm (proto, synth_id);
38913 else
38914 {
38915 synth_tmpl_parm = copy_decl (proto);
38916 DECL_NAME (synth_tmpl_parm) = synth_id;
38917 non_type = true;
38918 }
38919
38920 // Attach the constraint to the parm before processing.
38921 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38922 TREE_TYPE (node) = constr;
38923 tree new_parm
38924 = process_template_parm (parser->implicit_template_parms,
38925 input_location,
38926 node,
38927 /*non_type=*/non_type,
38928 /*param_pack=*/false);
38929
38930 // Chain the new parameter to the list of implicit parameters.
38931 if (parser->implicit_template_parms)
38932 parser->implicit_template_parms
38933 = TREE_CHAIN (parser->implicit_template_parms);
38934 else
38935 parser->implicit_template_parms = new_parm;
38936
38937 tree new_decl = getdecls ();
38938 if (non_type)
38939 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38940 new_decl = DECL_INITIAL (new_decl);
38941
38942 /* If creating a fully implicit function template, start the new implicit
38943 template parameter list with this synthesized type, otherwise grow the
38944 current template parameter list. */
38945
38946 if (become_template)
38947 {
38948 parent_scope->level_chain = current_binding_level;
38949
38950 tree new_parms = make_tree_vec (1);
38951 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38952 current_template_parms = tree_cons (size_int (processing_template_decl),
38953 new_parms, current_template_parms);
38954 }
38955 else
38956 {
38957 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38958 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38959 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38960 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38961 }
38962
38963 // If the new parameter was constrained, we need to add that to the
38964 // constraints in the template parameter list.
38965 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38966 {
38967 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38968 reqs = conjoin_constraints (reqs, req);
38969 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38970 }
38971
38972 current_binding_level = entry_scope;
38973
38974 return new_decl;
38975 }
38976
38977 /* Finish the declaration of a fully implicit function template. Such a
38978 template has no explicit template parameter list so has not been through the
38979 normal template head and tail processing. synthesize_implicit_template_parm
38980 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38981 provided if the declaration is a class member such that its template
38982 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38983 form is returned. Otherwise NULL_TREE is returned. */
38984
38985 static tree
38986 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38987 {
38988 gcc_assert (parser->fully_implicit_function_template_p);
38989
38990 if (member_decl_opt && member_decl_opt != error_mark_node
38991 && DECL_VIRTUAL_P (member_decl_opt))
38992 {
38993 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38994 "implicit templates may not be %<virtual%>");
38995 DECL_VIRTUAL_P (member_decl_opt) = false;
38996 }
38997
38998 if (member_decl_opt)
38999 member_decl_opt = finish_member_template_decl (member_decl_opt);
39000 end_template_decl ();
39001
39002 parser->fully_implicit_function_template_p = false;
39003 --parser->num_template_parameter_lists;
39004
39005 return member_decl_opt;
39006 }
39007
39008 #include "gt-cp-parser.h"