]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
Split omp-low into multiple files
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2016 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 tree cp_literal_operator_id
253 (const char *);
254
255 static void cp_parser_cilk_simd
256 (cp_parser *, cp_token *, bool *);
257 static tree cp_parser_cilk_for
258 (cp_parser *, tree, bool *);
259 static bool cp_parser_omp_declare_reduction_exprs
260 (tree, cp_parser *);
261 static tree cp_parser_cilk_simd_vectorlength
262 (cp_parser *, tree, bool);
263 static void cp_finalize_oacc_routine
264 (cp_parser *, tree, bool);
265
266 /* Manifest constants. */
267 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
268 #define CP_SAVED_TOKEN_STACK 5
269
270 /* Variables. */
271
272 /* The stream to which debugging output should be written. */
273 static FILE *cp_lexer_debug_stream;
274
275 /* Nonzero if we are parsing an unevaluated operand: an operand to
276 sizeof, typeof, or alignof. */
277 int cp_unevaluated_operand;
278
279 /* Dump up to NUM tokens in BUFFER to FILE starting with token
280 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
281 first token in BUFFER. If NUM is 0, dump all the tokens. If
282 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
283 highlighted by surrounding it in [[ ]]. */
284
285 static void
286 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
287 cp_token *start_token, unsigned num,
288 cp_token *curr_token)
289 {
290 unsigned i, nprinted;
291 cp_token *token;
292 bool do_print;
293
294 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
295
296 if (buffer == NULL)
297 return;
298
299 if (num == 0)
300 num = buffer->length ();
301
302 if (start_token == NULL)
303 start_token = buffer->address ();
304
305 if (start_token > buffer->address ())
306 {
307 cp_lexer_print_token (file, &(*buffer)[0]);
308 fprintf (file, " ... ");
309 }
310
311 do_print = false;
312 nprinted = 0;
313 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
314 {
315 if (token == start_token)
316 do_print = true;
317
318 if (!do_print)
319 continue;
320
321 nprinted++;
322 if (token == curr_token)
323 fprintf (file, "[[");
324
325 cp_lexer_print_token (file, token);
326
327 if (token == curr_token)
328 fprintf (file, "]]");
329
330 switch (token->type)
331 {
332 case CPP_SEMICOLON:
333 case CPP_OPEN_BRACE:
334 case CPP_CLOSE_BRACE:
335 case CPP_EOF:
336 fputc ('\n', file);
337 break;
338
339 default:
340 fputc (' ', file);
341 }
342 }
343
344 if (i == num && i < buffer->length ())
345 {
346 fprintf (file, " ... ");
347 cp_lexer_print_token (file, &buffer->last ());
348 }
349
350 fprintf (file, "\n");
351 }
352
353
354 /* Dump all tokens in BUFFER to stderr. */
355
356 void
357 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
358 {
359 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
360 }
361
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> &ref)
364 {
365 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
366 }
367
368 DEBUG_FUNCTION void
369 debug (vec<cp_token, va_gc> *ptr)
370 {
371 if (ptr)
372 debug (*ptr);
373 else
374 fprintf (stderr, "<nil>\n");
375 }
376
377
378 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
379 description for T. */
380
381 static void
382 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
383 {
384 if (t)
385 {
386 fprintf (file, "%s: ", desc);
387 print_node_brief (file, "", t, 0);
388 }
389 }
390
391
392 /* Dump parser context C to FILE. */
393
394 static void
395 cp_debug_print_context (FILE *file, cp_parser_context *c)
396 {
397 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
398 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
399 print_node_brief (file, "", c->object_type, 0);
400 fprintf (file, "}\n");
401 }
402
403
404 /* Print the stack of parsing contexts to FILE starting with FIRST. */
405
406 static void
407 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
408 {
409 unsigned i;
410 cp_parser_context *c;
411
412 fprintf (file, "Parsing context stack:\n");
413 for (i = 0, c = first; c; c = c->next, i++)
414 {
415 fprintf (file, "\t#%u: ", i);
416 cp_debug_print_context (file, c);
417 }
418 }
419
420
421 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
422
423 static void
424 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
425 {
426 if (flag)
427 fprintf (file, "%s: true\n", desc);
428 }
429
430
431 /* Print an unparsed function entry UF to FILE. */
432
433 static void
434 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
435 {
436 unsigned i;
437 cp_default_arg_entry *default_arg_fn;
438 tree fn;
439
440 fprintf (file, "\tFunctions with default args:\n");
441 for (i = 0;
442 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
443 i++)
444 {
445 fprintf (file, "\t\tClass type: ");
446 print_node_brief (file, "", default_arg_fn->class_type, 0);
447 fprintf (file, "\t\tDeclaration: ");
448 print_node_brief (file, "", default_arg_fn->decl, 0);
449 fprintf (file, "\n");
450 }
451
452 fprintf (file, "\n\tFunctions with definitions that require "
453 "post-processing\n\t\t");
454 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
455 {
456 print_node_brief (file, "", fn, 0);
457 fprintf (file, " ");
458 }
459 fprintf (file, "\n");
460
461 fprintf (file, "\n\tNon-static data members with initializers that require "
462 "post-processing\n\t\t");
463 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
464 {
465 print_node_brief (file, "", fn, 0);
466 fprintf (file, " ");
467 }
468 fprintf (file, "\n");
469 }
470
471
472 /* Print the stack of unparsed member functions S to FILE. */
473
474 static void
475 cp_debug_print_unparsed_queues (FILE *file,
476 vec<cp_unparsed_functions_entry, va_gc> *s)
477 {
478 unsigned i;
479 cp_unparsed_functions_entry *uf;
480
481 fprintf (file, "Unparsed functions\n");
482 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
483 {
484 fprintf (file, "#%u:\n", i);
485 cp_debug_print_unparsed_function (file, uf);
486 }
487 }
488
489
490 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
491 the given PARSER. If FILE is NULL, the output is printed on stderr. */
492
493 static void
494 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
495 {
496 cp_token *next_token, *first_token, *start_token;
497
498 if (file == NULL)
499 file = stderr;
500
501 next_token = parser->lexer->next_token;
502 first_token = parser->lexer->buffer->address ();
503 start_token = (next_token > first_token + window_size / 2)
504 ? next_token - window_size / 2
505 : first_token;
506 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
507 next_token);
508 }
509
510
511 /* Dump debugging information for the given PARSER. If FILE is NULL,
512 the output is printed on stderr. */
513
514 void
515 cp_debug_parser (FILE *file, cp_parser *parser)
516 {
517 const size_t window_size = 20;
518 cp_token *token;
519 expanded_location eloc;
520
521 if (file == NULL)
522 file = stderr;
523
524 fprintf (file, "Parser state\n\n");
525 fprintf (file, "Number of tokens: %u\n",
526 vec_safe_length (parser->lexer->buffer));
527 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
528 cp_debug_print_tree_if_set (file, "Object scope",
529 parser->object_scope);
530 cp_debug_print_tree_if_set (file, "Qualifying scope",
531 parser->qualifying_scope);
532 cp_debug_print_context_stack (file, parser->context);
533 cp_debug_print_flag (file, "Allow GNU extensions",
534 parser->allow_gnu_extensions_p);
535 cp_debug_print_flag (file, "'>' token is greater-than",
536 parser->greater_than_is_operator_p);
537 cp_debug_print_flag (file, "Default args allowed in current "
538 "parameter list", parser->default_arg_ok_p);
539 cp_debug_print_flag (file, "Parsing integral constant-expression",
540 parser->integral_constant_expression_p);
541 cp_debug_print_flag (file, "Allow non-constant expression in current "
542 "constant-expression",
543 parser->allow_non_integral_constant_expression_p);
544 cp_debug_print_flag (file, "Seen non-constant expression",
545 parser->non_integral_constant_expression_p);
546 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
547 "current context",
548 parser->local_variables_forbidden_p);
549 cp_debug_print_flag (file, "In unbraced linkage specification",
550 parser->in_unbraced_linkage_specification_p);
551 cp_debug_print_flag (file, "Parsing a declarator",
552 parser->in_declarator_p);
553 cp_debug_print_flag (file, "In template argument list",
554 parser->in_template_argument_list_p);
555 cp_debug_print_flag (file, "Parsing an iteration statement",
556 parser->in_statement & IN_ITERATION_STMT);
557 cp_debug_print_flag (file, "Parsing a switch statement",
558 parser->in_statement & IN_SWITCH_STMT);
559 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
560 parser->in_statement & IN_OMP_BLOCK);
561 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
562 parser->in_statement & IN_CILK_SIMD_FOR);
563 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
564 parser->in_statement & IN_OMP_FOR);
565 cp_debug_print_flag (file, "Parsing an if statement",
566 parser->in_statement & IN_IF_STMT);
567 cp_debug_print_flag (file, "Parsing a type-id in an expression "
568 "context", parser->in_type_id_in_expr_p);
569 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
570 parser->implicit_extern_c);
571 cp_debug_print_flag (file, "String expressions should be translated "
572 "to execution character set",
573 parser->translate_strings_p);
574 cp_debug_print_flag (file, "Parsing function body outside of a "
575 "local class", parser->in_function_body);
576 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
577 parser->colon_corrects_to_scope_p);
578 cp_debug_print_flag (file, "Colon doesn't start a class definition",
579 parser->colon_doesnt_start_class_def_p);
580 if (parser->type_definition_forbidden_message)
581 fprintf (file, "Error message for forbidden type definitions: %s\n",
582 parser->type_definition_forbidden_message);
583 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584 fprintf (file, "Number of class definitions in progress: %u\n",
585 parser->num_classes_being_defined);
586 fprintf (file, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser->num_template_parameter_lists);
588 cp_debug_parser_tokens (file, parser, window_size);
589 token = parser->lexer->next_token;
590 fprintf (file, "Next token to parse:\n");
591 fprintf (file, "\tToken: ");
592 cp_lexer_print_token (file, token);
593 eloc = expand_location (token->location);
594 fprintf (file, "\n\tFile: %s\n", eloc.file);
595 fprintf (file, "\tLine: %d\n", eloc.line);
596 fprintf (file, "\tColumn: %d\n", eloc.column);
597 }
598
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
601 {
602 cp_debug_parser (stderr, &ref);
603 }
604
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
607 {
608 if (ptr)
609 debug (*ptr);
610 else
611 fprintf (stderr, "<nil>\n");
612 }
613
614 /* Allocate memory for a new lexer object and return it. */
615
616 static cp_lexer *
617 cp_lexer_alloc (void)
618 {
619 cp_lexer *lexer;
620
621 c_common_no_more_pch ();
622
623 /* Allocate the memory. */
624 lexer = ggc_cleared_alloc<cp_lexer> ();
625
626 /* Initially we are not debugging. */
627 lexer->debugging_p = false;
628
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630
631 /* Create the buffer. */
632 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633
634 return lexer;
635 }
636
637
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 preprocessor. */
640
641 static cp_lexer *
642 cp_lexer_new_main (void)
643 {
644 cp_lexer *lexer;
645 cp_token token;
646
647 /* It's possible that parsing the first pragma will load a PCH file,
648 which is a GC collection point. So we have to do that before
649 allocating any memory. */
650 cp_parser_initial_pragma (&token);
651
652 lexer = cp_lexer_alloc ();
653
654 /* Put the first token in the buffer. */
655 lexer->buffer->quick_push (token);
656
657 /* Get the remaining tokens from the preprocessor. */
658 while (token.type != CPP_EOF)
659 {
660 cp_lexer_get_preprocessor_token (lexer, &token);
661 vec_safe_push (lexer->buffer, token);
662 }
663
664 lexer->last_token = lexer->buffer->address ()
665 + lexer->buffer->length ()
666 - 1;
667 lexer->next_token = lexer->buffer->length ()
668 ? lexer->buffer->address ()
669 : &eof_token;
670
671 /* Subsequent preprocessor diagnostics should use compiler
672 diagnostic functions to get the compiler source location. */
673 done_lexing = true;
674
675 gcc_assert (!lexer->next_token->purged_p);
676 return lexer;
677 }
678
679 /* Create a new lexer whose token stream is primed with the tokens in
680 CACHE. When these tokens are exhausted, no new tokens will be read. */
681
682 static cp_lexer *
683 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 {
685 cp_token *first = cache->first;
686 cp_token *last = cache->last;
687 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688
689 /* We do not own the buffer. */
690 lexer->buffer = NULL;
691 lexer->next_token = first == last ? &eof_token : first;
692 lexer->last_token = last;
693
694 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695
696 /* Initially we are not debugging. */
697 lexer->debugging_p = false;
698
699 gcc_assert (!lexer->next_token->purged_p);
700 return lexer;
701 }
702
703 /* Frees all resources associated with LEXER. */
704
705 static void
706 cp_lexer_destroy (cp_lexer *lexer)
707 {
708 vec_free (lexer->buffer);
709 lexer->saved_tokens.release ();
710 ggc_free (lexer);
711 }
712
713 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
714 be used. The point of this flag is to help the compiler to fold away calls
715 to cp_lexer_debugging_p within this source file at compile time, when the
716 lexer is not being debugged. */
717
718 #define LEXER_DEBUGGING_ENABLED_P false
719
720 /* Returns nonzero if debugging information should be output. */
721
722 static inline bool
723 cp_lexer_debugging_p (cp_lexer *lexer)
724 {
725 if (!LEXER_DEBUGGING_ENABLED_P)
726 return false;
727
728 return lexer->debugging_p;
729 }
730
731
732 static inline cp_token_position
733 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
734 {
735 gcc_assert (!previous_p || lexer->next_token != &eof_token);
736
737 return lexer->next_token - previous_p;
738 }
739
740 static inline cp_token *
741 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
742 {
743 return pos;
744 }
745
746 static inline void
747 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
748 {
749 lexer->next_token = cp_lexer_token_at (lexer, pos);
750 }
751
752 static inline cp_token_position
753 cp_lexer_previous_token_position (cp_lexer *lexer)
754 {
755 if (lexer->next_token == &eof_token)
756 return lexer->last_token - 1;
757 else
758 return cp_lexer_token_position (lexer, true);
759 }
760
761 static inline cp_token *
762 cp_lexer_previous_token (cp_lexer *lexer)
763 {
764 cp_token_position tp = cp_lexer_previous_token_position (lexer);
765
766 /* Skip past purged tokens. */
767 while (tp->purged_p)
768 {
769 gcc_assert (tp != lexer->buffer->address ());
770 tp--;
771 }
772
773 return cp_lexer_token_at (lexer, tp);
774 }
775
776 /* nonzero if we are presently saving tokens. */
777
778 static inline int
779 cp_lexer_saving_tokens (const cp_lexer* lexer)
780 {
781 return lexer->saved_tokens.length () != 0;
782 }
783
784 /* Store the next token from the preprocessor in *TOKEN. Return true
785 if we reach EOF. If LEXER is NULL, assume we are handling an
786 initial #pragma pch_preprocess, and thus want the lexer to return
787 processed strings. */
788
789 static void
790 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
791 {
792 static int is_extern_c = 0;
793
794 /* Get a new token from the preprocessor. */
795 token->type
796 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
797 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
798 token->keyword = RID_MAX;
799 token->purged_p = false;
800 token->error_reported = false;
801
802 /* On some systems, some header files are surrounded by an
803 implicit extern "C" block. Set a flag in the token if it
804 comes from such a header. */
805 is_extern_c += pending_lang_change;
806 pending_lang_change = 0;
807 token->implicit_extern_c = is_extern_c > 0;
808
809 /* Check to see if this token is a keyword. */
810 if (token->type == CPP_NAME)
811 {
812 if (C_IS_RESERVED_WORD (token->u.value))
813 {
814 /* Mark this token as a keyword. */
815 token->type = CPP_KEYWORD;
816 /* Record which keyword. */
817 token->keyword = C_RID_CODE (token->u.value);
818 }
819 else
820 {
821 if (warn_cxx11_compat
822 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
823 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
824 {
825 /* Warn about the C++0x keyword (but still treat it as
826 an identifier). */
827 warning (OPT_Wc__11_compat,
828 "identifier %qE is a keyword in C++11",
829 token->u.value);
830
831 /* Clear out the C_RID_CODE so we don't warn about this
832 particular identifier-turned-keyword again. */
833 C_SET_RID_CODE (token->u.value, RID_MAX);
834 }
835
836 token->keyword = RID_MAX;
837 }
838 }
839 else if (token->type == CPP_AT_NAME)
840 {
841 /* This only happens in Objective-C++; it must be a keyword. */
842 token->type = CPP_KEYWORD;
843 switch (C_RID_CODE (token->u.value))
844 {
845 /* Replace 'class' with '@class', 'private' with '@private',
846 etc. This prevents confusion with the C++ keyword
847 'class', and makes the tokens consistent with other
848 Objective-C 'AT' keywords. For example '@class' is
849 reported as RID_AT_CLASS which is consistent with
850 '@synchronized', which is reported as
851 RID_AT_SYNCHRONIZED.
852 */
853 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
854 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
855 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
856 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
857 case RID_THROW: token->keyword = RID_AT_THROW; break;
858 case RID_TRY: token->keyword = RID_AT_TRY; break;
859 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
860 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
861 default: token->keyword = C_RID_CODE (token->u.value);
862 }
863 }
864 }
865
866 /* Update the globals input_location and the input file stack from TOKEN. */
867 static inline void
868 cp_lexer_set_source_position_from_token (cp_token *token)
869 {
870 if (token->type != CPP_EOF)
871 {
872 input_location = token->location;
873 }
874 }
875
876 /* Update the globals input_location and the input file stack from LEXER. */
877 static inline void
878 cp_lexer_set_source_position (cp_lexer *lexer)
879 {
880 cp_token *token = cp_lexer_peek_token (lexer);
881 cp_lexer_set_source_position_from_token (token);
882 }
883
884 /* Return a pointer to the next token in the token stream, but do not
885 consume it. */
886
887 static inline cp_token *
888 cp_lexer_peek_token (cp_lexer *lexer)
889 {
890 if (cp_lexer_debugging_p (lexer))
891 {
892 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
893 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
894 putc ('\n', cp_lexer_debug_stream);
895 }
896 return lexer->next_token;
897 }
898
899 /* Return true if the next token has the indicated TYPE. */
900
901 static inline bool
902 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
903 {
904 return cp_lexer_peek_token (lexer)->type == type;
905 }
906
907 /* Return true if the next token does not have the indicated TYPE. */
908
909 static inline bool
910 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
911 {
912 return !cp_lexer_next_token_is (lexer, type);
913 }
914
915 /* Return true if the next token is the indicated KEYWORD. */
916
917 static inline bool
918 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
919 {
920 return cp_lexer_peek_token (lexer)->keyword == keyword;
921 }
922
923 static inline bool
924 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
925 {
926 return cp_lexer_peek_nth_token (lexer, n)->type == type;
927 }
928
929 static inline bool
930 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
931 {
932 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
933 }
934
935 /* Return true if the next token is not the indicated KEYWORD. */
936
937 static inline bool
938 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
939 {
940 return cp_lexer_peek_token (lexer)->keyword != keyword;
941 }
942
943 /* Return true if KEYWORD can start a decl-specifier. */
944
945 bool
946 cp_keyword_starts_decl_specifier_p (enum rid keyword)
947 {
948 switch (keyword)
949 {
950 /* auto specifier: storage-class-specifier in C++,
951 simple-type-specifier in C++0x. */
952 case RID_AUTO:
953 /* Storage classes. */
954 case RID_REGISTER:
955 case RID_STATIC:
956 case RID_EXTERN:
957 case RID_MUTABLE:
958 case RID_THREAD:
959 /* Elaborated type specifiers. */
960 case RID_ENUM:
961 case RID_CLASS:
962 case RID_STRUCT:
963 case RID_UNION:
964 case RID_TYPENAME:
965 /* Simple type specifiers. */
966 case RID_CHAR:
967 case RID_CHAR16:
968 case RID_CHAR32:
969 case RID_WCHAR:
970 case RID_BOOL:
971 case RID_SHORT:
972 case RID_INT:
973 case RID_LONG:
974 case RID_SIGNED:
975 case RID_UNSIGNED:
976 case RID_FLOAT:
977 case RID_DOUBLE:
978 case RID_VOID:
979 /* GNU extensions. */
980 case RID_ATTRIBUTE:
981 case RID_TYPEOF:
982 /* C++0x extensions. */
983 case RID_DECLTYPE:
984 case RID_UNDERLYING_TYPE:
985 case RID_CONSTEXPR:
986 return true;
987
988 default:
989 if (keyword >= RID_FIRST_INT_N
990 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
991 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
992 return true;
993 return false;
994 }
995 }
996
997 /* Return true if the next token is a keyword for a decl-specifier. */
998
999 static bool
1000 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1001 {
1002 cp_token *token;
1003
1004 token = cp_lexer_peek_token (lexer);
1005 return cp_keyword_starts_decl_specifier_p (token->keyword);
1006 }
1007
1008 /* Returns TRUE iff the token T begins a decltype type. */
1009
1010 static bool
1011 token_is_decltype (cp_token *t)
1012 {
1013 return (t->keyword == RID_DECLTYPE
1014 || t->type == CPP_DECLTYPE);
1015 }
1016
1017 /* Returns TRUE iff the next token begins a decltype type. */
1018
1019 static bool
1020 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1021 {
1022 cp_token *t = cp_lexer_peek_token (lexer);
1023 return token_is_decltype (t);
1024 }
1025
1026 /* Called when processing a token with tree_check_value; perform or defer the
1027 associated checks and return the value. */
1028
1029 static tree
1030 saved_checks_value (struct tree_check *check_value)
1031 {
1032 /* Perform any access checks that were deferred. */
1033 vec<deferred_access_check, va_gc> *checks;
1034 deferred_access_check *chk;
1035 checks = check_value->checks;
1036 if (checks)
1037 {
1038 int i;
1039 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1040 perform_or_defer_access_check (chk->binfo,
1041 chk->decl,
1042 chk->diag_decl, tf_warning_or_error);
1043 }
1044 /* Return the stored value. */
1045 return check_value->value;
1046 }
1047
1048 /* Return a pointer to the Nth token in the token stream. If N is 1,
1049 then this is precisely equivalent to cp_lexer_peek_token (except
1050 that it is not inline). One would like to disallow that case, but
1051 there is one case (cp_parser_nth_token_starts_template_id) where
1052 the caller passes a variable for N and it might be 1. */
1053
1054 static cp_token *
1055 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1056 {
1057 cp_token *token;
1058
1059 /* N is 1-based, not zero-based. */
1060 gcc_assert (n > 0);
1061
1062 if (cp_lexer_debugging_p (lexer))
1063 fprintf (cp_lexer_debug_stream,
1064 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1065
1066 --n;
1067 token = lexer->next_token;
1068 gcc_assert (!n || token != &eof_token);
1069 while (n != 0)
1070 {
1071 ++token;
1072 if (token == lexer->last_token)
1073 {
1074 token = &eof_token;
1075 break;
1076 }
1077
1078 if (!token->purged_p)
1079 --n;
1080 }
1081
1082 if (cp_lexer_debugging_p (lexer))
1083 {
1084 cp_lexer_print_token (cp_lexer_debug_stream, token);
1085 putc ('\n', cp_lexer_debug_stream);
1086 }
1087
1088 return token;
1089 }
1090
1091 /* Return the next token, and advance the lexer's next_token pointer
1092 to point to the next non-purged token. */
1093
1094 static cp_token *
1095 cp_lexer_consume_token (cp_lexer* lexer)
1096 {
1097 cp_token *token = lexer->next_token;
1098
1099 gcc_assert (token != &eof_token);
1100 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101
1102 do
1103 {
1104 lexer->next_token++;
1105 if (lexer->next_token == lexer->last_token)
1106 {
1107 lexer->next_token = &eof_token;
1108 break;
1109 }
1110
1111 }
1112 while (lexer->next_token->purged_p);
1113
1114 cp_lexer_set_source_position_from_token (token);
1115
1116 /* Provide debugging output. */
1117 if (cp_lexer_debugging_p (lexer))
1118 {
1119 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1120 cp_lexer_print_token (cp_lexer_debug_stream, token);
1121 putc ('\n', cp_lexer_debug_stream);
1122 }
1123
1124 return token;
1125 }
1126
1127 /* Permanently remove the next token from the token stream, and
1128 advance the next_token pointer to refer to the next non-purged
1129 token. */
1130
1131 static void
1132 cp_lexer_purge_token (cp_lexer *lexer)
1133 {
1134 cp_token *tok = lexer->next_token;
1135
1136 gcc_assert (tok != &eof_token);
1137 tok->purged_p = true;
1138 tok->location = UNKNOWN_LOCATION;
1139 tok->u.value = NULL_TREE;
1140 tok->keyword = RID_MAX;
1141
1142 do
1143 {
1144 tok++;
1145 if (tok == lexer->last_token)
1146 {
1147 tok = &eof_token;
1148 break;
1149 }
1150 }
1151 while (tok->purged_p);
1152 lexer->next_token = tok;
1153 }
1154
1155 /* Permanently remove all tokens after TOK, up to, but not
1156 including, the token that will be returned next by
1157 cp_lexer_peek_token. */
1158
1159 static void
1160 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1161 {
1162 cp_token *peek = lexer->next_token;
1163
1164 if (peek == &eof_token)
1165 peek = lexer->last_token;
1166
1167 gcc_assert (tok < peek);
1168
1169 for ( tok += 1; tok != peek; tok += 1)
1170 {
1171 tok->purged_p = true;
1172 tok->location = UNKNOWN_LOCATION;
1173 tok->u.value = NULL_TREE;
1174 tok->keyword = RID_MAX;
1175 }
1176 }
1177
1178 /* Begin saving tokens. All tokens consumed after this point will be
1179 preserved. */
1180
1181 static void
1182 cp_lexer_save_tokens (cp_lexer* lexer)
1183 {
1184 /* Provide debugging output. */
1185 if (cp_lexer_debugging_p (lexer))
1186 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1187
1188 lexer->saved_tokens.safe_push (lexer->next_token);
1189 }
1190
1191 /* Commit to the portion of the token stream most recently saved. */
1192
1193 static void
1194 cp_lexer_commit_tokens (cp_lexer* lexer)
1195 {
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer))
1198 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1199
1200 lexer->saved_tokens.pop ();
1201 }
1202
1203 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1204 to the token stream. Stop saving tokens. */
1205
1206 static void
1207 cp_lexer_rollback_tokens (cp_lexer* lexer)
1208 {
1209 /* Provide debugging output. */
1210 if (cp_lexer_debugging_p (lexer))
1211 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1212
1213 lexer->next_token = lexer->saved_tokens.pop ();
1214 }
1215
1216 /* RAII wrapper around the above functions, with sanity checking. Creating
1217 a variable saves tokens, which are committed when the variable is
1218 destroyed unless they are explicitly rolled back by calling the rollback
1219 member function. */
1220
1221 struct saved_token_sentinel
1222 {
1223 cp_lexer *lexer;
1224 unsigned len;
1225 bool commit;
1226 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1227 {
1228 len = lexer->saved_tokens.length ();
1229 cp_lexer_save_tokens (lexer);
1230 }
1231 void rollback ()
1232 {
1233 cp_lexer_rollback_tokens (lexer);
1234 commit = false;
1235 }
1236 ~saved_token_sentinel()
1237 {
1238 if (commit)
1239 cp_lexer_commit_tokens (lexer);
1240 gcc_assert (lexer->saved_tokens.length () == len);
1241 }
1242 };
1243
1244 /* Print a representation of the TOKEN on the STREAM. */
1245
1246 static void
1247 cp_lexer_print_token (FILE * stream, cp_token *token)
1248 {
1249 /* We don't use cpp_type2name here because the parser defines
1250 a few tokens of its own. */
1251 static const char *const token_names[] = {
1252 /* cpplib-defined token types */
1253 #define OP(e, s) #e,
1254 #define TK(e, s) #e,
1255 TTYPE_TABLE
1256 #undef OP
1257 #undef TK
1258 /* C++ parser token types - see "Manifest constants", above. */
1259 "KEYWORD",
1260 "TEMPLATE_ID",
1261 "NESTED_NAME_SPECIFIER",
1262 };
1263
1264 /* For some tokens, print the associated data. */
1265 switch (token->type)
1266 {
1267 case CPP_KEYWORD:
1268 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1269 For example, `struct' is mapped to an INTEGER_CST. */
1270 if (!identifier_p (token->u.value))
1271 break;
1272 /* fall through */
1273 case CPP_NAME:
1274 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1275 break;
1276
1277 case CPP_STRING:
1278 case CPP_STRING16:
1279 case CPP_STRING32:
1280 case CPP_WSTRING:
1281 case CPP_UTF8STRING:
1282 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1283 break;
1284
1285 case CPP_NUMBER:
1286 print_generic_expr (stream, token->u.value, 0);
1287 break;
1288
1289 default:
1290 /* If we have a name for the token, print it out. Otherwise, we
1291 simply give the numeric code. */
1292 if (token->type < ARRAY_SIZE(token_names))
1293 fputs (token_names[token->type], stream);
1294 else
1295 fprintf (stream, "[%d]", token->type);
1296 break;
1297 }
1298 }
1299
1300 DEBUG_FUNCTION void
1301 debug (cp_token &ref)
1302 {
1303 cp_lexer_print_token (stderr, &ref);
1304 fprintf (stderr, "\n");
1305 }
1306
1307 DEBUG_FUNCTION void
1308 debug (cp_token *ptr)
1309 {
1310 if (ptr)
1311 debug (*ptr);
1312 else
1313 fprintf (stderr, "<nil>\n");
1314 }
1315
1316
1317 /* Start emitting debugging information. */
1318
1319 static void
1320 cp_lexer_start_debugging (cp_lexer* lexer)
1321 {
1322 if (!LEXER_DEBUGGING_ENABLED_P)
1323 fatal_error (input_location,
1324 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325
1326 lexer->debugging_p = true;
1327 cp_lexer_debug_stream = stderr;
1328 }
1329
1330 /* Stop emitting debugging information. */
1331
1332 static void
1333 cp_lexer_stop_debugging (cp_lexer* lexer)
1334 {
1335 if (!LEXER_DEBUGGING_ENABLED_P)
1336 fatal_error (input_location,
1337 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1338
1339 lexer->debugging_p = false;
1340 cp_lexer_debug_stream = NULL;
1341 }
1342
1343 /* Create a new cp_token_cache, representing a range of tokens. */
1344
1345 static cp_token_cache *
1346 cp_token_cache_new (cp_token *first, cp_token *last)
1347 {
1348 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1349 cache->first = first;
1350 cache->last = last;
1351 return cache;
1352 }
1353
1354 /* Diagnose if #pragma omp declare simd isn't followed immediately
1355 by function declaration or definition. */
1356
1357 static inline void
1358 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1359 {
1360 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1361 {
1362 error ("%<#pragma omp declare simd%> not immediately followed by "
1363 "function declaration or definition");
1364 parser->omp_declare_simd = NULL;
1365 }
1366 }
1367
1368 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1369 and put that into "omp declare simd" attribute. */
1370
1371 static inline void
1372 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1373 {
1374 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1375 {
1376 if (fndecl == error_mark_node)
1377 {
1378 parser->omp_declare_simd = NULL;
1379 return;
1380 }
1381 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1382 {
1383 cp_ensure_no_omp_declare_simd (parser);
1384 return;
1385 }
1386 }
1387 }
1388
1389 /* Diagnose if #pragma acc routine isn't followed immediately by function
1390 declaration or definition. */
1391
1392 static inline void
1393 cp_ensure_no_oacc_routine (cp_parser *parser)
1394 {
1395 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1396 {
1397 error_at (parser->oacc_routine->loc,
1398 "%<#pragma acc routine%> not immediately followed by "
1399 "function declaration or definition");
1400 parser->oacc_routine = NULL;
1401 }
1402 }
1403 \f
1404 /* Decl-specifiers. */
1405
1406 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1407
1408 static void
1409 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1410 {
1411 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1412 }
1413
1414 /* Declarators. */
1415
1416 /* Nothing other than the parser should be creating declarators;
1417 declarators are a semi-syntactic representation of C++ entities.
1418 Other parts of the front end that need to create entities (like
1419 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1420
1421 static cp_declarator *make_call_declarator
1422 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1423 static cp_declarator *make_array_declarator
1424 (cp_declarator *, tree);
1425 static cp_declarator *make_pointer_declarator
1426 (cp_cv_quals, cp_declarator *, tree);
1427 static cp_declarator *make_reference_declarator
1428 (cp_cv_quals, cp_declarator *, bool, tree);
1429 static cp_declarator *make_ptrmem_declarator
1430 (cp_cv_quals, tree, cp_declarator *, tree);
1431
1432 /* An erroneous declarator. */
1433 static cp_declarator *cp_error_declarator;
1434
1435 /* The obstack on which declarators and related data structures are
1436 allocated. */
1437 static struct obstack declarator_obstack;
1438
1439 /* Alloc BYTES from the declarator memory pool. */
1440
1441 static inline void *
1442 alloc_declarator (size_t bytes)
1443 {
1444 return obstack_alloc (&declarator_obstack, bytes);
1445 }
1446
1447 /* Allocate a declarator of the indicated KIND. Clear fields that are
1448 common to all declarators. */
1449
1450 static cp_declarator *
1451 make_declarator (cp_declarator_kind kind)
1452 {
1453 cp_declarator *declarator;
1454
1455 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1456 declarator->kind = kind;
1457 declarator->attributes = NULL_TREE;
1458 declarator->std_attributes = NULL_TREE;
1459 declarator->declarator = NULL;
1460 declarator->parameter_pack_p = false;
1461 declarator->id_loc = UNKNOWN_LOCATION;
1462
1463 return declarator;
1464 }
1465
1466 /* Make a declarator for a generalized identifier. If
1467 QUALIFYING_SCOPE is non-NULL, the identifier is
1468 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1469 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1470 is, if any. */
1471
1472 static cp_declarator *
1473 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1474 special_function_kind sfk)
1475 {
1476 cp_declarator *declarator;
1477
1478 /* It is valid to write:
1479
1480 class C { void f(); };
1481 typedef C D;
1482 void D::f();
1483
1484 The standard is not clear about whether `typedef const C D' is
1485 legal; as of 2002-09-15 the committee is considering that
1486 question. EDG 3.0 allows that syntax. Therefore, we do as
1487 well. */
1488 if (qualifying_scope && TYPE_P (qualifying_scope))
1489 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1490
1491 gcc_assert (identifier_p (unqualified_name)
1492 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1493 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1494
1495 declarator = make_declarator (cdk_id);
1496 declarator->u.id.qualifying_scope = qualifying_scope;
1497 declarator->u.id.unqualified_name = unqualified_name;
1498 declarator->u.id.sfk = sfk;
1499
1500 return declarator;
1501 }
1502
1503 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1504 of modifiers such as const or volatile to apply to the pointer
1505 type, represented as identifiers. ATTRIBUTES represent the attributes that
1506 appertain to the pointer or reference. */
1507
1508 cp_declarator *
1509 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1510 tree attributes)
1511 {
1512 cp_declarator *declarator;
1513
1514 declarator = make_declarator (cdk_pointer);
1515 declarator->declarator = target;
1516 declarator->u.pointer.qualifiers = cv_qualifiers;
1517 declarator->u.pointer.class_type = NULL_TREE;
1518 if (target)
1519 {
1520 declarator->id_loc = target->id_loc;
1521 declarator->parameter_pack_p = target->parameter_pack_p;
1522 target->parameter_pack_p = false;
1523 }
1524 else
1525 declarator->parameter_pack_p = false;
1526
1527 declarator->std_attributes = attributes;
1528
1529 return declarator;
1530 }
1531
1532 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1533 represent the attributes that appertain to the pointer or
1534 reference. */
1535
1536 cp_declarator *
1537 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1538 bool rvalue_ref, tree attributes)
1539 {
1540 cp_declarator *declarator;
1541
1542 declarator = make_declarator (cdk_reference);
1543 declarator->declarator = target;
1544 declarator->u.reference.qualifiers = cv_qualifiers;
1545 declarator->u.reference.rvalue_ref = rvalue_ref;
1546 if (target)
1547 {
1548 declarator->id_loc = target->id_loc;
1549 declarator->parameter_pack_p = target->parameter_pack_p;
1550 target->parameter_pack_p = false;
1551 }
1552 else
1553 declarator->parameter_pack_p = false;
1554
1555 declarator->std_attributes = attributes;
1556
1557 return declarator;
1558 }
1559
1560 /* Like make_pointer_declarator -- but for a pointer to a non-static
1561 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1562 appertain to the pointer or reference. */
1563
1564 cp_declarator *
1565 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1566 cp_declarator *pointee,
1567 tree attributes)
1568 {
1569 cp_declarator *declarator;
1570
1571 declarator = make_declarator (cdk_ptrmem);
1572 declarator->declarator = pointee;
1573 declarator->u.pointer.qualifiers = cv_qualifiers;
1574 declarator->u.pointer.class_type = class_type;
1575
1576 if (pointee)
1577 {
1578 declarator->parameter_pack_p = pointee->parameter_pack_p;
1579 pointee->parameter_pack_p = false;
1580 }
1581 else
1582 declarator->parameter_pack_p = false;
1583
1584 declarator->std_attributes = attributes;
1585
1586 return declarator;
1587 }
1588
1589 /* Make a declarator for the function given by TARGET, with the
1590 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1591 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1592 indicates what exceptions can be thrown. */
1593
1594 cp_declarator *
1595 make_call_declarator (cp_declarator *target,
1596 tree parms,
1597 cp_cv_quals cv_qualifiers,
1598 cp_virt_specifiers virt_specifiers,
1599 cp_ref_qualifier ref_qualifier,
1600 tree tx_qualifier,
1601 tree exception_specification,
1602 tree late_return_type,
1603 tree requires_clause)
1604 {
1605 cp_declarator *declarator;
1606
1607 declarator = make_declarator (cdk_function);
1608 declarator->declarator = target;
1609 declarator->u.function.parameters = parms;
1610 declarator->u.function.qualifiers = cv_qualifiers;
1611 declarator->u.function.virt_specifiers = virt_specifiers;
1612 declarator->u.function.ref_qualifier = ref_qualifier;
1613 declarator->u.function.tx_qualifier = tx_qualifier;
1614 declarator->u.function.exception_specification = exception_specification;
1615 declarator->u.function.late_return_type = late_return_type;
1616 declarator->u.function.requires_clause = requires_clause;
1617 if (target)
1618 {
1619 declarator->id_loc = target->id_loc;
1620 declarator->parameter_pack_p = target->parameter_pack_p;
1621 target->parameter_pack_p = false;
1622 }
1623 else
1624 declarator->parameter_pack_p = false;
1625
1626 return declarator;
1627 }
1628
1629 /* Make a declarator for an array of BOUNDS elements, each of which is
1630 defined by ELEMENT. */
1631
1632 cp_declarator *
1633 make_array_declarator (cp_declarator *element, tree bounds)
1634 {
1635 cp_declarator *declarator;
1636
1637 declarator = make_declarator (cdk_array);
1638 declarator->declarator = element;
1639 declarator->u.array.bounds = bounds;
1640 if (element)
1641 {
1642 declarator->id_loc = element->id_loc;
1643 declarator->parameter_pack_p = element->parameter_pack_p;
1644 element->parameter_pack_p = false;
1645 }
1646 else
1647 declarator->parameter_pack_p = false;
1648
1649 return declarator;
1650 }
1651
1652 /* Determine whether the declarator we've seen so far can be a
1653 parameter pack, when followed by an ellipsis. */
1654 static bool
1655 declarator_can_be_parameter_pack (cp_declarator *declarator)
1656 {
1657 if (declarator && declarator->parameter_pack_p)
1658 /* We already saw an ellipsis. */
1659 return false;
1660
1661 /* Search for a declarator name, or any other declarator that goes
1662 after the point where the ellipsis could appear in a parameter
1663 pack. If we find any of these, then this declarator can not be
1664 made into a parameter pack. */
1665 bool found = false;
1666 while (declarator && !found)
1667 {
1668 switch ((int)declarator->kind)
1669 {
1670 case cdk_id:
1671 case cdk_array:
1672 case cdk_decomp:
1673 found = true;
1674 break;
1675
1676 case cdk_error:
1677 return true;
1678
1679 default:
1680 declarator = declarator->declarator;
1681 break;
1682 }
1683 }
1684
1685 return !found;
1686 }
1687
1688 cp_parameter_declarator *no_parameters;
1689
1690 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1691 DECLARATOR and DEFAULT_ARGUMENT. */
1692
1693 cp_parameter_declarator *
1694 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1695 cp_declarator *declarator,
1696 tree default_argument,
1697 bool template_parameter_pack_p = false)
1698 {
1699 cp_parameter_declarator *parameter;
1700
1701 parameter = ((cp_parameter_declarator *)
1702 alloc_declarator (sizeof (cp_parameter_declarator)));
1703 parameter->next = NULL;
1704 if (decl_specifiers)
1705 parameter->decl_specifiers = *decl_specifiers;
1706 else
1707 clear_decl_specs (&parameter->decl_specifiers);
1708 parameter->declarator = declarator;
1709 parameter->default_argument = default_argument;
1710 parameter->template_parameter_pack_p = template_parameter_pack_p;
1711
1712 return parameter;
1713 }
1714
1715 /* Returns true iff DECLARATOR is a declaration for a function. */
1716
1717 static bool
1718 function_declarator_p (const cp_declarator *declarator)
1719 {
1720 while (declarator)
1721 {
1722 if (declarator->kind == cdk_function
1723 && declarator->declarator->kind == cdk_id)
1724 return true;
1725 if (declarator->kind == cdk_id
1726 || declarator->kind == cdk_decomp
1727 || declarator->kind == cdk_error)
1728 return false;
1729 declarator = declarator->declarator;
1730 }
1731 return false;
1732 }
1733
1734 /* The parser. */
1735
1736 /* Overview
1737 --------
1738
1739 A cp_parser parses the token stream as specified by the C++
1740 grammar. Its job is purely parsing, not semantic analysis. For
1741 example, the parser breaks the token stream into declarators,
1742 expressions, statements, and other similar syntactic constructs.
1743 It does not check that the types of the expressions on either side
1744 of an assignment-statement are compatible, or that a function is
1745 not declared with a parameter of type `void'.
1746
1747 The parser invokes routines elsewhere in the compiler to perform
1748 semantic analysis and to build up the abstract syntax tree for the
1749 code processed.
1750
1751 The parser (and the template instantiation code, which is, in a
1752 way, a close relative of parsing) are the only parts of the
1753 compiler that should be calling push_scope and pop_scope, or
1754 related functions. The parser (and template instantiation code)
1755 keeps track of what scope is presently active; everything else
1756 should simply honor that. (The code that generates static
1757 initializers may also need to set the scope, in order to check
1758 access control correctly when emitting the initializers.)
1759
1760 Methodology
1761 -----------
1762
1763 The parser is of the standard recursive-descent variety. Upcoming
1764 tokens in the token stream are examined in order to determine which
1765 production to use when parsing a non-terminal. Some C++ constructs
1766 require arbitrary look ahead to disambiguate. For example, it is
1767 impossible, in the general case, to tell whether a statement is an
1768 expression or declaration without scanning the entire statement.
1769 Therefore, the parser is capable of "parsing tentatively." When the
1770 parser is not sure what construct comes next, it enters this mode.
1771 Then, while we attempt to parse the construct, the parser queues up
1772 error messages, rather than issuing them immediately, and saves the
1773 tokens it consumes. If the construct is parsed successfully, the
1774 parser "commits", i.e., it issues any queued error messages and
1775 the tokens that were being preserved are permanently discarded.
1776 If, however, the construct is not parsed successfully, the parser
1777 rolls back its state completely so that it can resume parsing using
1778 a different alternative.
1779
1780 Future Improvements
1781 -------------------
1782
1783 The performance of the parser could probably be improved substantially.
1784 We could often eliminate the need to parse tentatively by looking ahead
1785 a little bit. In some places, this approach might not entirely eliminate
1786 the need to parse tentatively, but it might still speed up the average
1787 case. */
1788
1789 /* Flags that are passed to some parsing functions. These values can
1790 be bitwise-ored together. */
1791
1792 enum
1793 {
1794 /* No flags. */
1795 CP_PARSER_FLAGS_NONE = 0x0,
1796 /* The construct is optional. If it is not present, then no error
1797 should be issued. */
1798 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1799 /* When parsing a type-specifier, treat user-defined type-names
1800 as non-type identifiers. */
1801 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1802 /* When parsing a type-specifier, do not try to parse a class-specifier
1803 or enum-specifier. */
1804 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1805 /* When parsing a decl-specifier-seq, only allow type-specifier or
1806 constexpr. */
1807 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1808 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1809 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1810 };
1811
1812 /* This type is used for parameters and variables which hold
1813 combinations of the above flags. */
1814 typedef int cp_parser_flags;
1815
1816 /* The different kinds of declarators we want to parse. */
1817
1818 enum cp_parser_declarator_kind
1819 {
1820 /* We want an abstract declarator. */
1821 CP_PARSER_DECLARATOR_ABSTRACT,
1822 /* We want a named declarator. */
1823 CP_PARSER_DECLARATOR_NAMED,
1824 /* We don't mind, but the name must be an unqualified-id. */
1825 CP_PARSER_DECLARATOR_EITHER
1826 };
1827
1828 /* The precedence values used to parse binary expressions. The minimum value
1829 of PREC must be 1, because zero is reserved to quickly discriminate
1830 binary operators from other tokens. */
1831
1832 enum cp_parser_prec
1833 {
1834 PREC_NOT_OPERATOR,
1835 PREC_LOGICAL_OR_EXPRESSION,
1836 PREC_LOGICAL_AND_EXPRESSION,
1837 PREC_INCLUSIVE_OR_EXPRESSION,
1838 PREC_EXCLUSIVE_OR_EXPRESSION,
1839 PREC_AND_EXPRESSION,
1840 PREC_EQUALITY_EXPRESSION,
1841 PREC_RELATIONAL_EXPRESSION,
1842 PREC_SHIFT_EXPRESSION,
1843 PREC_ADDITIVE_EXPRESSION,
1844 PREC_MULTIPLICATIVE_EXPRESSION,
1845 PREC_PM_EXPRESSION,
1846 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1847 };
1848
1849 /* A mapping from a token type to a corresponding tree node type, with a
1850 precedence value. */
1851
1852 struct cp_parser_binary_operations_map_node
1853 {
1854 /* The token type. */
1855 enum cpp_ttype token_type;
1856 /* The corresponding tree code. */
1857 enum tree_code tree_type;
1858 /* The precedence of this operator. */
1859 enum cp_parser_prec prec;
1860 };
1861
1862 struct cp_parser_expression_stack_entry
1863 {
1864 /* Left hand side of the binary operation we are currently
1865 parsing. */
1866 cp_expr lhs;
1867 /* Original tree code for left hand side, if it was a binary
1868 expression itself (used for -Wparentheses). */
1869 enum tree_code lhs_type;
1870 /* Tree code for the binary operation we are parsing. */
1871 enum tree_code tree_type;
1872 /* Precedence of the binary operation we are parsing. */
1873 enum cp_parser_prec prec;
1874 /* Location of the binary operation we are parsing. */
1875 location_t loc;
1876 };
1877
1878 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1879 entries because precedence levels on the stack are monotonically
1880 increasing. */
1881 typedef struct cp_parser_expression_stack_entry
1882 cp_parser_expression_stack[NUM_PREC_VALUES];
1883
1884 /* Prototypes. */
1885
1886 /* Constructors and destructors. */
1887
1888 static cp_parser_context *cp_parser_context_new
1889 (cp_parser_context *);
1890
1891 /* Class variables. */
1892
1893 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1894
1895 /* The operator-precedence table used by cp_parser_binary_expression.
1896 Transformed into an associative array (binops_by_token) by
1897 cp_parser_new. */
1898
1899 static const cp_parser_binary_operations_map_node binops[] = {
1900 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1901 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1902
1903 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1905 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1906
1907 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1908 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1909
1910 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1911 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1912
1913 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1914 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1916 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1917
1918 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1919 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1920
1921 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1922
1923 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1924
1925 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1926
1927 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1928
1929 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1930 };
1931
1932 /* The same as binops, but initialized by cp_parser_new so that
1933 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1934 for speed. */
1935 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1936
1937 /* Constructors and destructors. */
1938
1939 /* Construct a new context. The context below this one on the stack
1940 is given by NEXT. */
1941
1942 static cp_parser_context *
1943 cp_parser_context_new (cp_parser_context* next)
1944 {
1945 cp_parser_context *context;
1946
1947 /* Allocate the storage. */
1948 if (cp_parser_context_free_list != NULL)
1949 {
1950 /* Pull the first entry from the free list. */
1951 context = cp_parser_context_free_list;
1952 cp_parser_context_free_list = context->next;
1953 memset (context, 0, sizeof (*context));
1954 }
1955 else
1956 context = ggc_cleared_alloc<cp_parser_context> ();
1957
1958 /* No errors have occurred yet in this context. */
1959 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1960 /* If this is not the bottommost context, copy information that we
1961 need from the previous context. */
1962 if (next)
1963 {
1964 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1965 expression, then we are parsing one in this context, too. */
1966 context->object_type = next->object_type;
1967 /* Thread the stack. */
1968 context->next = next;
1969 }
1970
1971 return context;
1972 }
1973
1974 /* Managing the unparsed function queues. */
1975
1976 #define unparsed_funs_with_default_args \
1977 parser->unparsed_queues->last ().funs_with_default_args
1978 #define unparsed_funs_with_definitions \
1979 parser->unparsed_queues->last ().funs_with_definitions
1980 #define unparsed_nsdmis \
1981 parser->unparsed_queues->last ().nsdmis
1982 #define unparsed_classes \
1983 parser->unparsed_queues->last ().classes
1984
1985 static void
1986 push_unparsed_function_queues (cp_parser *parser)
1987 {
1988 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1989 vec_safe_push (parser->unparsed_queues, e);
1990 }
1991
1992 static void
1993 pop_unparsed_function_queues (cp_parser *parser)
1994 {
1995 release_tree_vector (unparsed_funs_with_definitions);
1996 parser->unparsed_queues->pop ();
1997 }
1998
1999 /* Prototypes. */
2000
2001 /* Constructors and destructors. */
2002
2003 static cp_parser *cp_parser_new
2004 (void);
2005
2006 /* Routines to parse various constructs.
2007
2008 Those that return `tree' will return the error_mark_node (rather
2009 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2010 Sometimes, they will return an ordinary node if error-recovery was
2011 attempted, even though a parse error occurred. So, to check
2012 whether or not a parse error occurred, you should always use
2013 cp_parser_error_occurred. If the construct is optional (indicated
2014 either by an `_opt' in the name of the function that does the
2015 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2016 the construct is not present. */
2017
2018 /* Lexical conventions [gram.lex] */
2019
2020 static cp_expr cp_parser_identifier
2021 (cp_parser *);
2022 static cp_expr cp_parser_string_literal
2023 (cp_parser *, bool, bool, bool);
2024 static cp_expr cp_parser_userdef_char_literal
2025 (cp_parser *);
2026 static tree cp_parser_userdef_string_literal
2027 (tree);
2028 static cp_expr cp_parser_userdef_numeric_literal
2029 (cp_parser *);
2030
2031 /* Basic concepts [gram.basic] */
2032
2033 static bool cp_parser_translation_unit
2034 (cp_parser *);
2035
2036 /* Expressions [gram.expr] */
2037
2038 static cp_expr cp_parser_primary_expression
2039 (cp_parser *, bool, bool, bool, cp_id_kind *);
2040 static cp_expr cp_parser_id_expression
2041 (cp_parser *, bool, bool, bool *, bool, bool);
2042 static cp_expr cp_parser_unqualified_id
2043 (cp_parser *, bool, bool, bool, bool);
2044 static tree cp_parser_nested_name_specifier_opt
2045 (cp_parser *, bool, bool, bool, bool);
2046 static tree cp_parser_nested_name_specifier
2047 (cp_parser *, bool, bool, bool, bool);
2048 static tree cp_parser_qualifying_entity
2049 (cp_parser *, bool, bool, bool, bool, bool);
2050 static cp_expr cp_parser_postfix_expression
2051 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2052 static tree cp_parser_postfix_open_square_expression
2053 (cp_parser *, tree, bool, bool);
2054 static tree cp_parser_postfix_dot_deref_expression
2055 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2056 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2057 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2058 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2059 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2060 static void cp_parser_pseudo_destructor_name
2061 (cp_parser *, tree, tree *, tree *);
2062 static cp_expr cp_parser_unary_expression
2063 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2064 static enum tree_code cp_parser_unary_operator
2065 (cp_token *);
2066 static tree cp_parser_new_expression
2067 (cp_parser *);
2068 static vec<tree, va_gc> *cp_parser_new_placement
2069 (cp_parser *);
2070 static tree cp_parser_new_type_id
2071 (cp_parser *, tree *);
2072 static cp_declarator *cp_parser_new_declarator_opt
2073 (cp_parser *);
2074 static cp_declarator *cp_parser_direct_new_declarator
2075 (cp_parser *);
2076 static vec<tree, va_gc> *cp_parser_new_initializer
2077 (cp_parser *);
2078 static tree cp_parser_delete_expression
2079 (cp_parser *);
2080 static cp_expr cp_parser_cast_expression
2081 (cp_parser *, bool, bool, bool, cp_id_kind *);
2082 static cp_expr cp_parser_binary_expression
2083 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2084 static tree cp_parser_question_colon_clause
2085 (cp_parser *, cp_expr);
2086 static cp_expr cp_parser_assignment_expression
2087 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2088 static enum tree_code cp_parser_assignment_operator_opt
2089 (cp_parser *);
2090 static cp_expr cp_parser_expression
2091 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2092 static cp_expr cp_parser_constant_expression
2093 (cp_parser *, bool = false, bool * = NULL);
2094 static cp_expr cp_parser_builtin_offsetof
2095 (cp_parser *);
2096 static cp_expr cp_parser_lambda_expression
2097 (cp_parser *);
2098 static void cp_parser_lambda_introducer
2099 (cp_parser *, tree);
2100 static bool cp_parser_lambda_declarator_opt
2101 (cp_parser *, tree);
2102 static void cp_parser_lambda_body
2103 (cp_parser *, tree);
2104
2105 /* Statements [gram.stmt.stmt] */
2106
2107 static void cp_parser_statement
2108 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2109 static void cp_parser_label_for_labeled_statement
2110 (cp_parser *, tree);
2111 static tree cp_parser_expression_statement
2112 (cp_parser *, tree);
2113 static tree cp_parser_compound_statement
2114 (cp_parser *, tree, int, bool);
2115 static void cp_parser_statement_seq_opt
2116 (cp_parser *, tree);
2117 static tree cp_parser_selection_statement
2118 (cp_parser *, bool *, vec<tree> *);
2119 static tree cp_parser_condition
2120 (cp_parser *);
2121 static tree cp_parser_iteration_statement
2122 (cp_parser *, bool *, bool);
2123 static bool cp_parser_init_statement
2124 (cp_parser *, tree *decl);
2125 static tree cp_parser_for
2126 (cp_parser *, bool);
2127 static tree cp_parser_c_for
2128 (cp_parser *, tree, tree, bool);
2129 static tree cp_parser_range_for
2130 (cp_parser *, tree, tree, tree, bool);
2131 static void do_range_for_auto_deduction
2132 (tree, tree);
2133 static tree cp_parser_perform_range_for_lookup
2134 (tree, tree *, tree *);
2135 static tree cp_parser_range_for_member_function
2136 (tree, tree);
2137 static tree cp_parser_jump_statement
2138 (cp_parser *);
2139 static void cp_parser_declaration_statement
2140 (cp_parser *);
2141
2142 static tree cp_parser_implicitly_scoped_statement
2143 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2144 static void cp_parser_already_scoped_statement
2145 (cp_parser *, bool *, const token_indent_info &);
2146
2147 /* Declarations [gram.dcl.dcl] */
2148
2149 static void cp_parser_declaration_seq_opt
2150 (cp_parser *);
2151 static void cp_parser_declaration
2152 (cp_parser *);
2153 static void cp_parser_block_declaration
2154 (cp_parser *, bool);
2155 static void cp_parser_simple_declaration
2156 (cp_parser *, bool, tree *);
2157 static void cp_parser_decl_specifier_seq
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2159 static tree cp_parser_storage_class_specifier_opt
2160 (cp_parser *);
2161 static tree cp_parser_function_specifier_opt
2162 (cp_parser *, cp_decl_specifier_seq *);
2163 static tree cp_parser_type_specifier
2164 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2165 int *, bool *);
2166 static tree cp_parser_simple_type_specifier
2167 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2168 static tree cp_parser_type_name
2169 (cp_parser *, bool);
2170 static tree cp_parser_type_name
2171 (cp_parser *);
2172 static tree cp_parser_nonclass_name
2173 (cp_parser* parser);
2174 static tree cp_parser_elaborated_type_specifier
2175 (cp_parser *, bool, bool);
2176 static tree cp_parser_enum_specifier
2177 (cp_parser *);
2178 static void cp_parser_enumerator_list
2179 (cp_parser *, tree);
2180 static void cp_parser_enumerator_definition
2181 (cp_parser *, tree);
2182 static tree cp_parser_namespace_name
2183 (cp_parser *);
2184 static void cp_parser_namespace_definition
2185 (cp_parser *);
2186 static void cp_parser_namespace_body
2187 (cp_parser *);
2188 static tree cp_parser_qualified_namespace_specifier
2189 (cp_parser *);
2190 static void cp_parser_namespace_alias_definition
2191 (cp_parser *);
2192 static bool cp_parser_using_declaration
2193 (cp_parser *, bool);
2194 static void cp_parser_using_directive
2195 (cp_parser *);
2196 static tree cp_parser_alias_declaration
2197 (cp_parser *);
2198 static void cp_parser_asm_definition
2199 (cp_parser *);
2200 static void cp_parser_linkage_specification
2201 (cp_parser *);
2202 static void cp_parser_static_assert
2203 (cp_parser *, bool);
2204 static tree cp_parser_decltype
2205 (cp_parser *);
2206 static tree cp_parser_decomposition_declaration
2207 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2208
2209 /* Declarators [gram.dcl.decl] */
2210
2211 static tree cp_parser_init_declarator
2212 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2213 bool, bool, int, bool *, tree *, location_t *, tree *);
2214 static cp_declarator *cp_parser_declarator
2215 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2216 static cp_declarator *cp_parser_direct_declarator
2217 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2218 static enum tree_code cp_parser_ptr_operator
2219 (cp_parser *, tree *, cp_cv_quals *, tree *);
2220 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2221 (cp_parser *);
2222 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2223 (cp_parser *);
2224 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2225 (cp_parser *);
2226 static tree cp_parser_tx_qualifier_opt
2227 (cp_parser *);
2228 static tree cp_parser_late_return_type_opt
2229 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2230 static tree cp_parser_declarator_id
2231 (cp_parser *, bool);
2232 static tree cp_parser_type_id
2233 (cp_parser *);
2234 static tree cp_parser_template_type_arg
2235 (cp_parser *);
2236 static tree cp_parser_trailing_type_id (cp_parser *);
2237 static tree cp_parser_type_id_1
2238 (cp_parser *, bool, bool);
2239 static void cp_parser_type_specifier_seq
2240 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2241 static tree cp_parser_parameter_declaration_clause
2242 (cp_parser *);
2243 static tree cp_parser_parameter_declaration_list
2244 (cp_parser *, bool *);
2245 static cp_parameter_declarator *cp_parser_parameter_declaration
2246 (cp_parser *, bool, bool *);
2247 static tree cp_parser_default_argument
2248 (cp_parser *, bool);
2249 static void cp_parser_function_body
2250 (cp_parser *, bool);
2251 static tree cp_parser_initializer
2252 (cp_parser *, bool *, bool *);
2253 static cp_expr cp_parser_initializer_clause
2254 (cp_parser *, bool *);
2255 static cp_expr cp_parser_braced_list
2256 (cp_parser*, bool*);
2257 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2258 (cp_parser *, bool *);
2259
2260 static bool cp_parser_ctor_initializer_opt_and_function_body
2261 (cp_parser *, bool);
2262
2263 static tree cp_parser_late_parsing_omp_declare_simd
2264 (cp_parser *, tree);
2265
2266 static tree cp_parser_late_parsing_cilk_simd_fn_info
2267 (cp_parser *, tree);
2268
2269 static tree cp_parser_late_parsing_oacc_routine
2270 (cp_parser *, tree);
2271
2272 static tree synthesize_implicit_template_parm
2273 (cp_parser *, tree);
2274 static tree finish_fully_implicit_template
2275 (cp_parser *, tree);
2276
2277 /* Classes [gram.class] */
2278
2279 static tree cp_parser_class_name
2280 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2281 static tree cp_parser_class_specifier
2282 (cp_parser *);
2283 static tree cp_parser_class_head
2284 (cp_parser *, bool *);
2285 static enum tag_types cp_parser_class_key
2286 (cp_parser *);
2287 static void cp_parser_type_parameter_key
2288 (cp_parser* parser);
2289 static void cp_parser_member_specification_opt
2290 (cp_parser *);
2291 static void cp_parser_member_declaration
2292 (cp_parser *);
2293 static tree cp_parser_pure_specifier
2294 (cp_parser *);
2295 static tree cp_parser_constant_initializer
2296 (cp_parser *);
2297
2298 /* Derived classes [gram.class.derived] */
2299
2300 static tree cp_parser_base_clause
2301 (cp_parser *);
2302 static tree cp_parser_base_specifier
2303 (cp_parser *);
2304
2305 /* Special member functions [gram.special] */
2306
2307 static tree cp_parser_conversion_function_id
2308 (cp_parser *);
2309 static tree cp_parser_conversion_type_id
2310 (cp_parser *);
2311 static cp_declarator *cp_parser_conversion_declarator_opt
2312 (cp_parser *);
2313 static bool cp_parser_ctor_initializer_opt
2314 (cp_parser *);
2315 static void cp_parser_mem_initializer_list
2316 (cp_parser *);
2317 static tree cp_parser_mem_initializer
2318 (cp_parser *);
2319 static tree cp_parser_mem_initializer_id
2320 (cp_parser *);
2321
2322 /* Overloading [gram.over] */
2323
2324 static cp_expr cp_parser_operator_function_id
2325 (cp_parser *);
2326 static cp_expr cp_parser_operator
2327 (cp_parser *);
2328
2329 /* Templates [gram.temp] */
2330
2331 static void cp_parser_template_declaration
2332 (cp_parser *, bool);
2333 static tree cp_parser_template_parameter_list
2334 (cp_parser *);
2335 static tree cp_parser_template_parameter
2336 (cp_parser *, bool *, bool *);
2337 static tree cp_parser_type_parameter
2338 (cp_parser *, bool *);
2339 static tree cp_parser_template_id
2340 (cp_parser *, bool, bool, enum tag_types, bool);
2341 static tree cp_parser_template_name
2342 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2343 static tree cp_parser_template_argument_list
2344 (cp_parser *);
2345 static tree cp_parser_template_argument
2346 (cp_parser *);
2347 static void cp_parser_explicit_instantiation
2348 (cp_parser *);
2349 static void cp_parser_explicit_specialization
2350 (cp_parser *);
2351
2352 /* Exception handling [gram.exception] */
2353
2354 static tree cp_parser_try_block
2355 (cp_parser *);
2356 static bool cp_parser_function_try_block
2357 (cp_parser *);
2358 static void cp_parser_handler_seq
2359 (cp_parser *);
2360 static void cp_parser_handler
2361 (cp_parser *);
2362 static tree cp_parser_exception_declaration
2363 (cp_parser *);
2364 static tree cp_parser_throw_expression
2365 (cp_parser *);
2366 static tree cp_parser_exception_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_type_id_list
2369 (cp_parser *);
2370
2371 /* GNU Extensions */
2372
2373 static tree cp_parser_asm_specification_opt
2374 (cp_parser *);
2375 static tree cp_parser_asm_operand_list
2376 (cp_parser *);
2377 static tree cp_parser_asm_clobber_list
2378 (cp_parser *);
2379 static tree cp_parser_asm_label_list
2380 (cp_parser *);
2381 static bool cp_next_tokens_can_be_attribute_p
2382 (cp_parser *);
2383 static bool cp_next_tokens_can_be_gnu_attribute_p
2384 (cp_parser *);
2385 static bool cp_next_tokens_can_be_std_attribute_p
2386 (cp_parser *);
2387 static bool cp_nth_tokens_can_be_std_attribute_p
2388 (cp_parser *, size_t);
2389 static bool cp_nth_tokens_can_be_gnu_attribute_p
2390 (cp_parser *, size_t);
2391 static bool cp_nth_tokens_can_be_attribute_p
2392 (cp_parser *, size_t);
2393 static tree cp_parser_attributes_opt
2394 (cp_parser *);
2395 static tree cp_parser_gnu_attributes_opt
2396 (cp_parser *);
2397 static tree cp_parser_gnu_attribute_list
2398 (cp_parser *);
2399 static tree cp_parser_std_attribute
2400 (cp_parser *, tree);
2401 static tree cp_parser_std_attribute_spec
2402 (cp_parser *);
2403 static tree cp_parser_std_attribute_spec_seq
2404 (cp_parser *);
2405 static bool cp_parser_extension_opt
2406 (cp_parser *, int *);
2407 static void cp_parser_label_declaration
2408 (cp_parser *);
2409
2410 /* Concept Extensions */
2411
2412 static tree cp_parser_requires_clause
2413 (cp_parser *);
2414 static tree cp_parser_requires_clause_opt
2415 (cp_parser *);
2416 static tree cp_parser_requires_expression
2417 (cp_parser *);
2418 static tree cp_parser_requirement_parameter_list
2419 (cp_parser *);
2420 static tree cp_parser_requirement_body
2421 (cp_parser *);
2422 static tree cp_parser_requirement_list
2423 (cp_parser *);
2424 static tree cp_parser_requirement
2425 (cp_parser *);
2426 static tree cp_parser_simple_requirement
2427 (cp_parser *);
2428 static tree cp_parser_compound_requirement
2429 (cp_parser *);
2430 static tree cp_parser_type_requirement
2431 (cp_parser *);
2432 static tree cp_parser_nested_requirement
2433 (cp_parser *);
2434
2435 /* Transactional Memory Extensions */
2436
2437 static tree cp_parser_transaction
2438 (cp_parser *, cp_token *);
2439 static tree cp_parser_transaction_expression
2440 (cp_parser *, enum rid);
2441 static bool cp_parser_function_transaction
2442 (cp_parser *, enum rid);
2443 static tree cp_parser_transaction_cancel
2444 (cp_parser *);
2445
2446 enum pragma_context {
2447 pragma_external,
2448 pragma_member,
2449 pragma_objc_icode,
2450 pragma_stmt,
2451 pragma_compound
2452 };
2453 static bool cp_parser_pragma
2454 (cp_parser *, enum pragma_context, bool *);
2455
2456 /* Objective-C++ Productions */
2457
2458 static tree cp_parser_objc_message_receiver
2459 (cp_parser *);
2460 static tree cp_parser_objc_message_args
2461 (cp_parser *);
2462 static tree cp_parser_objc_message_expression
2463 (cp_parser *);
2464 static cp_expr cp_parser_objc_encode_expression
2465 (cp_parser *);
2466 static tree cp_parser_objc_defs_expression
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_expression
2469 (cp_parser *);
2470 static tree cp_parser_objc_selector_expression
2471 (cp_parser *);
2472 static cp_expr cp_parser_objc_expression
2473 (cp_parser *);
2474 static bool cp_parser_objc_selector_p
2475 (enum cpp_ttype);
2476 static tree cp_parser_objc_selector
2477 (cp_parser *);
2478 static tree cp_parser_objc_protocol_refs_opt
2479 (cp_parser *);
2480 static void cp_parser_objc_declaration
2481 (cp_parser *, tree);
2482 static tree cp_parser_objc_statement
2483 (cp_parser *);
2484 static bool cp_parser_objc_valid_prefix_attributes
2485 (cp_parser *, tree *);
2486 static void cp_parser_objc_at_property_declaration
2487 (cp_parser *) ;
2488 static void cp_parser_objc_at_synthesize_declaration
2489 (cp_parser *) ;
2490 static void cp_parser_objc_at_dynamic_declaration
2491 (cp_parser *) ;
2492 static tree cp_parser_objc_struct_declaration
2493 (cp_parser *) ;
2494
2495 /* Utility Routines */
2496
2497 static cp_expr cp_parser_lookup_name
2498 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2499 static tree cp_parser_lookup_name_simple
2500 (cp_parser *, tree, location_t);
2501 static tree cp_parser_maybe_treat_template_as_class
2502 (tree, bool);
2503 static bool cp_parser_check_declarator_template_parameters
2504 (cp_parser *, cp_declarator *, location_t);
2505 static bool cp_parser_check_template_parameters
2506 (cp_parser *, unsigned, location_t, cp_declarator *);
2507 static cp_expr cp_parser_simple_cast_expression
2508 (cp_parser *);
2509 static tree cp_parser_global_scope_opt
2510 (cp_parser *, bool);
2511 static bool cp_parser_constructor_declarator_p
2512 (cp_parser *, bool);
2513 static tree cp_parser_function_definition_from_specifiers_and_declarator
2514 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2515 static tree cp_parser_function_definition_after_declarator
2516 (cp_parser *, bool);
2517 static bool cp_parser_template_declaration_after_export
2518 (cp_parser *, bool);
2519 static void cp_parser_perform_template_parameter_access_checks
2520 (vec<deferred_access_check, va_gc> *);
2521 static tree cp_parser_single_declaration
2522 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2523 static cp_expr cp_parser_functional_cast
2524 (cp_parser *, tree);
2525 static tree cp_parser_save_member_function_body
2526 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2527 static tree cp_parser_save_nsdmi
2528 (cp_parser *);
2529 static tree cp_parser_enclosed_template_argument_list
2530 (cp_parser *);
2531 static void cp_parser_save_default_args
2532 (cp_parser *, tree);
2533 static void cp_parser_late_parsing_for_member
2534 (cp_parser *, tree);
2535 static tree cp_parser_late_parse_one_default_arg
2536 (cp_parser *, tree, tree, tree);
2537 static void cp_parser_late_parsing_nsdmi
2538 (cp_parser *, tree);
2539 static void cp_parser_late_parsing_default_args
2540 (cp_parser *, tree);
2541 static tree cp_parser_sizeof_operand
2542 (cp_parser *, enum rid);
2543 static tree cp_parser_trait_expr
2544 (cp_parser *, enum rid);
2545 static bool cp_parser_declares_only_class_p
2546 (cp_parser *);
2547 static void cp_parser_set_storage_class
2548 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2549 static void cp_parser_set_decl_spec_type
2550 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2551 static void set_and_check_decl_spec_loc
2552 (cp_decl_specifier_seq *decl_specs,
2553 cp_decl_spec ds, cp_token *);
2554 static bool cp_parser_friend_p
2555 (const cp_decl_specifier_seq *);
2556 static void cp_parser_required_error
2557 (cp_parser *, required_token, bool);
2558 static cp_token *cp_parser_require
2559 (cp_parser *, enum cpp_ttype, required_token);
2560 static cp_token *cp_parser_require_keyword
2561 (cp_parser *, enum rid, required_token);
2562 static bool cp_parser_token_starts_function_definition_p
2563 (cp_token *);
2564 static bool cp_parser_next_token_starts_class_definition_p
2565 (cp_parser *);
2566 static bool cp_parser_next_token_ends_template_argument_p
2567 (cp_parser *);
2568 static bool cp_parser_nth_token_starts_template_argument_list_p
2569 (cp_parser *, size_t);
2570 static enum tag_types cp_parser_token_is_class_key
2571 (cp_token *);
2572 static enum tag_types cp_parser_token_is_type_parameter_key
2573 (cp_token *);
2574 static void cp_parser_check_class_key
2575 (enum tag_types, tree type);
2576 static void cp_parser_check_access_in_redeclaration
2577 (tree type, location_t location);
2578 static bool cp_parser_optional_template_keyword
2579 (cp_parser *);
2580 static void cp_parser_pre_parsed_nested_name_specifier
2581 (cp_parser *);
2582 static bool cp_parser_cache_group
2583 (cp_parser *, enum cpp_ttype, unsigned);
2584 static tree cp_parser_cache_defarg
2585 (cp_parser *parser, bool nsdmi);
2586 static void cp_parser_parse_tentatively
2587 (cp_parser *);
2588 static void cp_parser_commit_to_tentative_parse
2589 (cp_parser *);
2590 static void cp_parser_commit_to_topmost_tentative_parse
2591 (cp_parser *);
2592 static void cp_parser_abort_tentative_parse
2593 (cp_parser *);
2594 static bool cp_parser_parse_definitely
2595 (cp_parser *);
2596 static inline bool cp_parser_parsing_tentatively
2597 (cp_parser *);
2598 static bool cp_parser_uncommitted_to_tentative_parse_p
2599 (cp_parser *);
2600 static void cp_parser_error
2601 (cp_parser *, const char *);
2602 static void cp_parser_name_lookup_error
2603 (cp_parser *, tree, tree, name_lookup_error, location_t);
2604 static bool cp_parser_simulate_error
2605 (cp_parser *);
2606 static bool cp_parser_check_type_definition
2607 (cp_parser *);
2608 static void cp_parser_check_for_definition_in_return_type
2609 (cp_declarator *, tree, location_t type_location);
2610 static void cp_parser_check_for_invalid_template_id
2611 (cp_parser *, tree, enum tag_types, location_t location);
2612 static bool cp_parser_non_integral_constant_expression
2613 (cp_parser *, non_integral_constant);
2614 static void cp_parser_diagnose_invalid_type_name
2615 (cp_parser *, tree, location_t);
2616 static bool cp_parser_parse_and_diagnose_invalid_type_name
2617 (cp_parser *);
2618 static int cp_parser_skip_to_closing_parenthesis
2619 (cp_parser *, bool, bool, bool);
2620 static void cp_parser_skip_to_end_of_statement
2621 (cp_parser *);
2622 static void cp_parser_consume_semicolon_at_end_of_statement
2623 (cp_parser *);
2624 static void cp_parser_skip_to_end_of_block_or_statement
2625 (cp_parser *);
2626 static bool cp_parser_skip_to_closing_brace
2627 (cp_parser *);
2628 static void cp_parser_skip_to_end_of_template_parameter_list
2629 (cp_parser *);
2630 static void cp_parser_skip_to_pragma_eol
2631 (cp_parser*, cp_token *);
2632 static bool cp_parser_error_occurred
2633 (cp_parser *);
2634 static bool cp_parser_allow_gnu_extensions_p
2635 (cp_parser *);
2636 static bool cp_parser_is_pure_string_literal
2637 (cp_token *);
2638 static bool cp_parser_is_string_literal
2639 (cp_token *);
2640 static bool cp_parser_is_keyword
2641 (cp_token *, enum rid);
2642 static tree cp_parser_make_typename_type
2643 (cp_parser *, tree, location_t location);
2644 static cp_declarator * cp_parser_make_indirect_declarator
2645 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2646 static bool cp_parser_compound_literal_p
2647 (cp_parser *);
2648 static bool cp_parser_array_designator_p
2649 (cp_parser *);
2650 static bool cp_parser_init_statement_p
2651 (cp_parser *);
2652 static bool cp_parser_skip_to_closing_square_bracket
2653 (cp_parser *);
2654
2655 /* Concept-related syntactic transformations */
2656
2657 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2658 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2659
2660 // -------------------------------------------------------------------------- //
2661 // Unevaluated Operand Guard
2662 //
2663 // Implementation of an RAII helper for unevaluated operand parsing.
2664 cp_unevaluated::cp_unevaluated ()
2665 {
2666 ++cp_unevaluated_operand;
2667 ++c_inhibit_evaluation_warnings;
2668 }
2669
2670 cp_unevaluated::~cp_unevaluated ()
2671 {
2672 --c_inhibit_evaluation_warnings;
2673 --cp_unevaluated_operand;
2674 }
2675
2676 // -------------------------------------------------------------------------- //
2677 // Tentative Parsing
2678
2679 /* Returns nonzero if we are parsing tentatively. */
2680
2681 static inline bool
2682 cp_parser_parsing_tentatively (cp_parser* parser)
2683 {
2684 return parser->context->next != NULL;
2685 }
2686
2687 /* Returns nonzero if TOKEN is a string literal. */
2688
2689 static bool
2690 cp_parser_is_pure_string_literal (cp_token* token)
2691 {
2692 return (token->type == CPP_STRING ||
2693 token->type == CPP_STRING16 ||
2694 token->type == CPP_STRING32 ||
2695 token->type == CPP_WSTRING ||
2696 token->type == CPP_UTF8STRING);
2697 }
2698
2699 /* Returns nonzero if TOKEN is a string literal
2700 of a user-defined string literal. */
2701
2702 static bool
2703 cp_parser_is_string_literal (cp_token* token)
2704 {
2705 return (cp_parser_is_pure_string_literal (token) ||
2706 token->type == CPP_STRING_USERDEF ||
2707 token->type == CPP_STRING16_USERDEF ||
2708 token->type == CPP_STRING32_USERDEF ||
2709 token->type == CPP_WSTRING_USERDEF ||
2710 token->type == CPP_UTF8STRING_USERDEF);
2711 }
2712
2713 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2714
2715 static bool
2716 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2717 {
2718 return token->keyword == keyword;
2719 }
2720
2721 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2722 PRAGMA_NONE. */
2723
2724 static enum pragma_kind
2725 cp_parser_pragma_kind (cp_token *token)
2726 {
2727 if (token->type != CPP_PRAGMA)
2728 return PRAGMA_NONE;
2729 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2730 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2731 }
2732
2733 /* Helper function for cp_parser_error.
2734 Having peeked a token of kind TOK1_KIND that might signify
2735 a conflict marker, peek successor tokens to determine
2736 if we actually do have a conflict marker.
2737 Specifically, we consider a run of 7 '<', '=' or '>' characters
2738 at the start of a line as a conflict marker.
2739 These come through the lexer as three pairs and a single,
2740 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2741 If it returns true, *OUT_LOC is written to with the location/range
2742 of the marker. */
2743
2744 static bool
2745 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2746 location_t *out_loc)
2747 {
2748 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2749 if (token2->type != tok1_kind)
2750 return false;
2751 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2752 if (token3->type != tok1_kind)
2753 return false;
2754 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2755 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2756 return false;
2757
2758 /* It must be at the start of the line. */
2759 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2760 if (LOCATION_COLUMN (start_loc) != 1)
2761 return false;
2762
2763 /* We have a conflict marker. Construct a location of the form:
2764 <<<<<<<
2765 ^~~~~~~
2766 with start == caret, finishing at the end of the marker. */
2767 location_t finish_loc = get_finish (token4->location);
2768 *out_loc = make_location (start_loc, start_loc, finish_loc);
2769
2770 return true;
2771 }
2772
2773 /* If not parsing tentatively, issue a diagnostic of the form
2774 FILE:LINE: MESSAGE before TOKEN
2775 where TOKEN is the next token in the input stream. MESSAGE
2776 (specified by the caller) is usually of the form "expected
2777 OTHER-TOKEN". */
2778
2779 static void
2780 cp_parser_error (cp_parser* parser, const char* gmsgid)
2781 {
2782 if (!cp_parser_simulate_error (parser))
2783 {
2784 cp_token *token = cp_lexer_peek_token (parser->lexer);
2785 /* This diagnostic makes more sense if it is tagged to the line
2786 of the token we just peeked at. */
2787 cp_lexer_set_source_position_from_token (token);
2788
2789 if (token->type == CPP_PRAGMA)
2790 {
2791 error_at (token->location,
2792 "%<#pragma%> is not allowed here");
2793 cp_parser_skip_to_pragma_eol (parser, token);
2794 return;
2795 }
2796
2797 /* If this is actually a conflict marker, report it as such. */
2798 if (token->type == CPP_LSHIFT
2799 || token->type == CPP_RSHIFT
2800 || token->type == CPP_EQ_EQ)
2801 {
2802 location_t loc;
2803 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2804 {
2805 error_at (loc, "version control conflict marker in file");
2806 return;
2807 }
2808 }
2809
2810 c_parse_error (gmsgid,
2811 /* Because c_parser_error does not understand
2812 CPP_KEYWORD, keywords are treated like
2813 identifiers. */
2814 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2815 token->u.value, token->flags);
2816 }
2817 }
2818
2819 /* Issue an error about name-lookup failing. NAME is the
2820 IDENTIFIER_NODE DECL is the result of
2821 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2822 the thing that we hoped to find. */
2823
2824 static void
2825 cp_parser_name_lookup_error (cp_parser* parser,
2826 tree name,
2827 tree decl,
2828 name_lookup_error desired,
2829 location_t location)
2830 {
2831 /* If name lookup completely failed, tell the user that NAME was not
2832 declared. */
2833 if (decl == error_mark_node)
2834 {
2835 if (parser->scope && parser->scope != global_namespace)
2836 error_at (location, "%<%E::%E%> has not been declared",
2837 parser->scope, name);
2838 else if (parser->scope == global_namespace)
2839 error_at (location, "%<::%E%> has not been declared", name);
2840 else if (parser->object_scope
2841 && !CLASS_TYPE_P (parser->object_scope))
2842 error_at (location, "request for member %qE in non-class type %qT",
2843 name, parser->object_scope);
2844 else if (parser->object_scope)
2845 error_at (location, "%<%T::%E%> has not been declared",
2846 parser->object_scope, name);
2847 else
2848 error_at (location, "%qE has not been declared", name);
2849 }
2850 else if (parser->scope && parser->scope != global_namespace)
2851 {
2852 switch (desired)
2853 {
2854 case NLE_TYPE:
2855 error_at (location, "%<%E::%E%> is not a type",
2856 parser->scope, name);
2857 break;
2858 case NLE_CXX98:
2859 error_at (location, "%<%E::%E%> is not a class or namespace",
2860 parser->scope, name);
2861 break;
2862 case NLE_NOT_CXX98:
2863 error_at (location,
2864 "%<%E::%E%> is not a class, namespace, or enumeration",
2865 parser->scope, name);
2866 break;
2867 default:
2868 gcc_unreachable ();
2869
2870 }
2871 }
2872 else if (parser->scope == global_namespace)
2873 {
2874 switch (desired)
2875 {
2876 case NLE_TYPE:
2877 error_at (location, "%<::%E%> is not a type", name);
2878 break;
2879 case NLE_CXX98:
2880 error_at (location, "%<::%E%> is not a class or namespace", name);
2881 break;
2882 case NLE_NOT_CXX98:
2883 error_at (location,
2884 "%<::%E%> is not a class, namespace, or enumeration",
2885 name);
2886 break;
2887 default:
2888 gcc_unreachable ();
2889 }
2890 }
2891 else
2892 {
2893 switch (desired)
2894 {
2895 case NLE_TYPE:
2896 error_at (location, "%qE is not a type", name);
2897 break;
2898 case NLE_CXX98:
2899 error_at (location, "%qE is not a class or namespace", name);
2900 break;
2901 case NLE_NOT_CXX98:
2902 error_at (location,
2903 "%qE is not a class, namespace, or enumeration", name);
2904 break;
2905 default:
2906 gcc_unreachable ();
2907 }
2908 }
2909 }
2910
2911 /* If we are parsing tentatively, remember that an error has occurred
2912 during this tentative parse. Returns true if the error was
2913 simulated; false if a message should be issued by the caller. */
2914
2915 static bool
2916 cp_parser_simulate_error (cp_parser* parser)
2917 {
2918 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2919 {
2920 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2921 return true;
2922 }
2923 return false;
2924 }
2925
2926 /* This function is called when a type is defined. If type
2927 definitions are forbidden at this point, an error message is
2928 issued. */
2929
2930 static bool
2931 cp_parser_check_type_definition (cp_parser* parser)
2932 {
2933 /* If types are forbidden here, issue a message. */
2934 if (parser->type_definition_forbidden_message)
2935 {
2936 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2937 in the message need to be interpreted. */
2938 error (parser->type_definition_forbidden_message);
2939 return false;
2940 }
2941 return true;
2942 }
2943
2944 /* This function is called when the DECLARATOR is processed. The TYPE
2945 was a type defined in the decl-specifiers. If it is invalid to
2946 define a type in the decl-specifiers for DECLARATOR, an error is
2947 issued. TYPE_LOCATION is the location of TYPE and is used
2948 for error reporting. */
2949
2950 static void
2951 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2952 tree type, location_t type_location)
2953 {
2954 /* [dcl.fct] forbids type definitions in return types.
2955 Unfortunately, it's not easy to know whether or not we are
2956 processing a return type until after the fact. */
2957 while (declarator
2958 && (declarator->kind == cdk_pointer
2959 || declarator->kind == cdk_reference
2960 || declarator->kind == cdk_ptrmem))
2961 declarator = declarator->declarator;
2962 if (declarator
2963 && declarator->kind == cdk_function)
2964 {
2965 error_at (type_location,
2966 "new types may not be defined in a return type");
2967 inform (type_location,
2968 "(perhaps a semicolon is missing after the definition of %qT)",
2969 type);
2970 }
2971 }
2972
2973 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2974 "<" in any valid C++ program. If the next token is indeed "<",
2975 issue a message warning the user about what appears to be an
2976 invalid attempt to form a template-id. LOCATION is the location
2977 of the type-specifier (TYPE) */
2978
2979 static void
2980 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2981 tree type,
2982 enum tag_types tag_type,
2983 location_t location)
2984 {
2985 cp_token_position start = 0;
2986
2987 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2988 {
2989 if (TYPE_P (type))
2990 error_at (location, "%qT is not a template", type);
2991 else if (identifier_p (type))
2992 {
2993 if (tag_type != none_type)
2994 error_at (location, "%qE is not a class template", type);
2995 else
2996 error_at (location, "%qE is not a template", type);
2997 }
2998 else
2999 error_at (location, "invalid template-id");
3000 /* Remember the location of the invalid "<". */
3001 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3002 start = cp_lexer_token_position (parser->lexer, true);
3003 /* Consume the "<". */
3004 cp_lexer_consume_token (parser->lexer);
3005 /* Parse the template arguments. */
3006 cp_parser_enclosed_template_argument_list (parser);
3007 /* Permanently remove the invalid template arguments so that
3008 this error message is not issued again. */
3009 if (start)
3010 cp_lexer_purge_tokens_after (parser->lexer, start);
3011 }
3012 }
3013
3014 /* If parsing an integral constant-expression, issue an error message
3015 about the fact that THING appeared and return true. Otherwise,
3016 return false. In either case, set
3017 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3018
3019 static bool
3020 cp_parser_non_integral_constant_expression (cp_parser *parser,
3021 non_integral_constant thing)
3022 {
3023 parser->non_integral_constant_expression_p = true;
3024 if (parser->integral_constant_expression_p)
3025 {
3026 if (!parser->allow_non_integral_constant_expression_p)
3027 {
3028 const char *msg = NULL;
3029 switch (thing)
3030 {
3031 case NIC_FLOAT:
3032 pedwarn (input_location, OPT_Wpedantic,
3033 "ISO C++ forbids using a floating-point literal "
3034 "in a constant-expression");
3035 return true;
3036 case NIC_CAST:
3037 error ("a cast to a type other than an integral or "
3038 "enumeration type cannot appear in a "
3039 "constant-expression");
3040 return true;
3041 case NIC_TYPEID:
3042 error ("%<typeid%> operator "
3043 "cannot appear in a constant-expression");
3044 return true;
3045 case NIC_NCC:
3046 error ("non-constant compound literals "
3047 "cannot appear in a constant-expression");
3048 return true;
3049 case NIC_FUNC_CALL:
3050 error ("a function call "
3051 "cannot appear in a constant-expression");
3052 return true;
3053 case NIC_INC:
3054 error ("an increment "
3055 "cannot appear in a constant-expression");
3056 return true;
3057 case NIC_DEC:
3058 error ("an decrement "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_ARRAY_REF:
3062 error ("an array reference "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_ADDR_LABEL:
3066 error ("the address of a label "
3067 "cannot appear in a constant-expression");
3068 return true;
3069 case NIC_OVERLOADED:
3070 error ("calls to overloaded operators "
3071 "cannot appear in a constant-expression");
3072 return true;
3073 case NIC_ASSIGNMENT:
3074 error ("an assignment cannot appear in a constant-expression");
3075 return true;
3076 case NIC_COMMA:
3077 error ("a comma operator "
3078 "cannot appear in a constant-expression");
3079 return true;
3080 case NIC_CONSTRUCTOR:
3081 error ("a call to a constructor "
3082 "cannot appear in a constant-expression");
3083 return true;
3084 case NIC_TRANSACTION:
3085 error ("a transaction expression "
3086 "cannot appear in a constant-expression");
3087 return true;
3088 case NIC_THIS:
3089 msg = "this";
3090 break;
3091 case NIC_FUNC_NAME:
3092 msg = "__FUNCTION__";
3093 break;
3094 case NIC_PRETTY_FUNC:
3095 msg = "__PRETTY_FUNCTION__";
3096 break;
3097 case NIC_C99_FUNC:
3098 msg = "__func__";
3099 break;
3100 case NIC_VA_ARG:
3101 msg = "va_arg";
3102 break;
3103 case NIC_ARROW:
3104 msg = "->";
3105 break;
3106 case NIC_POINT:
3107 msg = ".";
3108 break;
3109 case NIC_STAR:
3110 msg = "*";
3111 break;
3112 case NIC_ADDR:
3113 msg = "&";
3114 break;
3115 case NIC_PREINCREMENT:
3116 msg = "++";
3117 break;
3118 case NIC_PREDECREMENT:
3119 msg = "--";
3120 break;
3121 case NIC_NEW:
3122 msg = "new";
3123 break;
3124 case NIC_DEL:
3125 msg = "delete";
3126 break;
3127 default:
3128 gcc_unreachable ();
3129 }
3130 if (msg)
3131 error ("%qs cannot appear in a constant-expression", msg);
3132 return true;
3133 }
3134 }
3135 return false;
3136 }
3137
3138 /* Emit a diagnostic for an invalid type name. This function commits
3139 to the current active tentative parse, if any. (Otherwise, the
3140 problematic construct might be encountered again later, resulting
3141 in duplicate error messages.) LOCATION is the location of ID. */
3142
3143 static void
3144 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3145 location_t location)
3146 {
3147 tree decl, ambiguous_decls;
3148 cp_parser_commit_to_tentative_parse (parser);
3149 /* Try to lookup the identifier. */
3150 decl = cp_parser_lookup_name (parser, id, none_type,
3151 /*is_template=*/false,
3152 /*is_namespace=*/false,
3153 /*check_dependency=*/true,
3154 &ambiguous_decls, location);
3155 if (ambiguous_decls)
3156 /* If the lookup was ambiguous, an error will already have
3157 been issued. */
3158 return;
3159 /* If the lookup found a template-name, it means that the user forgot
3160 to specify an argument list. Emit a useful error message. */
3161 if (DECL_TYPE_TEMPLATE_P (decl))
3162 {
3163 error_at (location,
3164 "invalid use of template-name %qE without an argument list",
3165 decl);
3166 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3167 inform (location, "class template argument deduction is only available "
3168 "with -std=c++1z or -std=gnu++1z");
3169 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3170 }
3171 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3172 error_at (location, "invalid use of destructor %qD as a type", id);
3173 else if (TREE_CODE (decl) == TYPE_DECL)
3174 /* Something like 'unsigned A a;' */
3175 error_at (location, "invalid combination of multiple type-specifiers");
3176 else if (!parser->scope)
3177 {
3178 /* Issue an error message. */
3179 const char *suggestion = NULL;
3180 if (TREE_CODE (id) == IDENTIFIER_NODE)
3181 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3182 if (suggestion)
3183 {
3184 gcc_rich_location richloc (location);
3185 richloc.add_fixit_replace (suggestion);
3186 error_at_rich_loc (&richloc,
3187 "%qE does not name a type; did you mean %qs?",
3188 id, suggestion);
3189 }
3190 else
3191 error_at (location, "%qE does not name a type", id);
3192 /* If we're in a template class, it's possible that the user was
3193 referring to a type from a base class. For example:
3194
3195 template <typename T> struct A { typedef T X; };
3196 template <typename T> struct B : public A<T> { X x; };
3197
3198 The user should have said "typename A<T>::X". */
3199 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3200 inform (location, "C++11 %<constexpr%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3203 inform (location, "C++11 %<noexcept%> only available with "
3204 "-std=c++11 or -std=gnu++11");
3205 else if (cxx_dialect < cxx11
3206 && TREE_CODE (id) == IDENTIFIER_NODE
3207 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3208 inform (location, "C++11 %<thread_local%> only available with "
3209 "-std=c++11 or -std=gnu++11");
3210 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3211 inform (location, "%<concept%> only available with -fconcepts");
3212 else if (processing_template_decl && current_class_type
3213 && TYPE_BINFO (current_class_type))
3214 {
3215 tree b;
3216
3217 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3218 b;
3219 b = TREE_CHAIN (b))
3220 {
3221 tree base_type = BINFO_TYPE (b);
3222 if (CLASS_TYPE_P (base_type)
3223 && dependent_type_p (base_type))
3224 {
3225 tree field;
3226 /* Go from a particular instantiation of the
3227 template (which will have an empty TYPE_FIELDs),
3228 to the main version. */
3229 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3230 for (field = TYPE_FIELDS (base_type);
3231 field;
3232 field = DECL_CHAIN (field))
3233 if (TREE_CODE (field) == TYPE_DECL
3234 && DECL_NAME (field) == id)
3235 {
3236 inform (location,
3237 "(perhaps %<typename %T::%E%> was intended)",
3238 BINFO_TYPE (b), id);
3239 break;
3240 }
3241 if (field)
3242 break;
3243 }
3244 }
3245 }
3246 }
3247 /* Here we diagnose qualified-ids where the scope is actually correct,
3248 but the identifier does not resolve to a valid type name. */
3249 else if (parser->scope != error_mark_node)
3250 {
3251 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3252 {
3253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3254 error_at (location_of (id),
3255 "%qE in namespace %qE does not name a template type",
3256 id, parser->scope);
3257 else
3258 error_at (location_of (id),
3259 "%qE in namespace %qE does not name a type",
3260 id, parser->scope);
3261 if (DECL_P (decl))
3262 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3263 }
3264 else if (CLASS_TYPE_P (parser->scope)
3265 && constructor_name_p (id, parser->scope))
3266 {
3267 /* A<T>::A<T>() */
3268 error_at (location, "%<%T::%E%> names the constructor, not"
3269 " the type", parser->scope, id);
3270 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3271 error_at (location, "and %qT has no template constructors",
3272 parser->scope);
3273 }
3274 else if (TYPE_P (parser->scope)
3275 && dependent_scope_p (parser->scope))
3276 error_at (location, "need %<typename%> before %<%T::%E%> because "
3277 "%qT is a dependent scope",
3278 parser->scope, id, parser->scope);
3279 else if (TYPE_P (parser->scope))
3280 {
3281 if (!COMPLETE_TYPE_P (parser->scope))
3282 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3283 parser->scope);
3284 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3285 error_at (location_of (id),
3286 "%qE in %q#T does not name a template type",
3287 id, parser->scope);
3288 else
3289 error_at (location_of (id),
3290 "%qE in %q#T does not name a type",
3291 id, parser->scope);
3292 if (DECL_P (decl))
3293 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3294 }
3295 else
3296 gcc_unreachable ();
3297 }
3298 }
3299
3300 /* Check for a common situation where a type-name should be present,
3301 but is not, and issue a sensible error message. Returns true if an
3302 invalid type-name was detected.
3303
3304 The situation handled by this function are variable declarations of the
3305 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3306 Usually, `ID' should name a type, but if we got here it means that it
3307 does not. We try to emit the best possible error message depending on
3308 how exactly the id-expression looks like. */
3309
3310 static bool
3311 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3312 {
3313 tree id;
3314 cp_token *token = cp_lexer_peek_token (parser->lexer);
3315
3316 /* Avoid duplicate error about ambiguous lookup. */
3317 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3318 {
3319 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3320 if (next->type == CPP_NAME && next->error_reported)
3321 goto out;
3322 }
3323
3324 cp_parser_parse_tentatively (parser);
3325 id = cp_parser_id_expression (parser,
3326 /*template_keyword_p=*/false,
3327 /*check_dependency_p=*/true,
3328 /*template_p=*/NULL,
3329 /*declarator_p=*/true,
3330 /*optional_p=*/false);
3331 /* If the next token is a (, this is a function with no explicit return
3332 type, i.e. constructor, destructor or conversion op. */
3333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3334 || TREE_CODE (id) == TYPE_DECL)
3335 {
3336 cp_parser_abort_tentative_parse (parser);
3337 return false;
3338 }
3339 if (!cp_parser_parse_definitely (parser))
3340 return false;
3341
3342 /* Emit a diagnostic for the invalid type. */
3343 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3344 out:
3345 /* If we aren't in the middle of a declarator (i.e. in a
3346 parameter-declaration-clause), skip to the end of the declaration;
3347 there's no point in trying to process it. */
3348 if (!parser->in_declarator_p)
3349 cp_parser_skip_to_end_of_block_or_statement (parser);
3350 return true;
3351 }
3352
3353 /* Consume tokens up to, and including, the next non-nested closing `)'.
3354 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3355 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3356 found an unnested token of that type. */
3357
3358 static int
3359 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3360 bool recovering,
3361 cpp_ttype or_ttype,
3362 bool consume_paren)
3363 {
3364 unsigned paren_depth = 0;
3365 unsigned brace_depth = 0;
3366 unsigned square_depth = 0;
3367
3368 if (recovering && or_ttype == CPP_EOF
3369 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3370 return 0;
3371
3372 while (true)
3373 {
3374 cp_token * token = cp_lexer_peek_token (parser->lexer);
3375
3376 /* Have we found what we're looking for before the closing paren? */
3377 if (token->type == or_ttype && or_ttype != CPP_EOF
3378 && !brace_depth && !paren_depth && !square_depth)
3379 return -1;
3380
3381 switch (token->type)
3382 {
3383 case CPP_EOF:
3384 case CPP_PRAGMA_EOL:
3385 /* If we've run out of tokens, then there is no closing `)'. */
3386 return 0;
3387
3388 /* This is good for lambda expression capture-lists. */
3389 case CPP_OPEN_SQUARE:
3390 ++square_depth;
3391 break;
3392 case CPP_CLOSE_SQUARE:
3393 if (!square_depth--)
3394 return 0;
3395 break;
3396
3397 case CPP_SEMICOLON:
3398 /* This matches the processing in skip_to_end_of_statement. */
3399 if (!brace_depth)
3400 return 0;
3401 break;
3402
3403 case CPP_OPEN_BRACE:
3404 ++brace_depth;
3405 break;
3406 case CPP_CLOSE_BRACE:
3407 if (!brace_depth--)
3408 return 0;
3409 break;
3410
3411 case CPP_OPEN_PAREN:
3412 if (!brace_depth)
3413 ++paren_depth;
3414 break;
3415
3416 case CPP_CLOSE_PAREN:
3417 if (!brace_depth && !paren_depth--)
3418 {
3419 if (consume_paren)
3420 cp_lexer_consume_token (parser->lexer);
3421 return 1;
3422 }
3423 break;
3424
3425 default:
3426 break;
3427 }
3428
3429 /* Consume the token. */
3430 cp_lexer_consume_token (parser->lexer);
3431 }
3432 }
3433
3434 /* Consume tokens up to, and including, the next non-nested closing `)'.
3435 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3436 are doing error recovery. Returns -1 if OR_COMMA is true and we
3437 found an unnested token of that type. */
3438
3439 static int
3440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3441 bool recovering,
3442 bool or_comma,
3443 bool consume_paren)
3444 {
3445 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3446 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3447 ttype, consume_paren);
3448 }
3449
3450 /* Consume tokens until we reach the end of the current statement.
3451 Normally, that will be just before consuming a `;'. However, if a
3452 non-nested `}' comes first, then we stop before consuming that. */
3453
3454 static void
3455 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3456 {
3457 unsigned nesting_depth = 0;
3458
3459 /* Unwind generic function template scope if necessary. */
3460 if (parser->fully_implicit_function_template_p)
3461 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3462
3463 while (true)
3464 {
3465 cp_token *token = cp_lexer_peek_token (parser->lexer);
3466
3467 switch (token->type)
3468 {
3469 case CPP_EOF:
3470 case CPP_PRAGMA_EOL:
3471 /* If we've run out of tokens, stop. */
3472 return;
3473
3474 case CPP_SEMICOLON:
3475 /* If the next token is a `;', we have reached the end of the
3476 statement. */
3477 if (!nesting_depth)
3478 return;
3479 break;
3480
3481 case CPP_CLOSE_BRACE:
3482 /* If this is a non-nested '}', stop before consuming it.
3483 That way, when confronted with something like:
3484
3485 { 3 + }
3486
3487 we stop before consuming the closing '}', even though we
3488 have not yet reached a `;'. */
3489 if (nesting_depth == 0)
3490 return;
3491
3492 /* If it is the closing '}' for a block that we have
3493 scanned, stop -- but only after consuming the token.
3494 That way given:
3495
3496 void f g () { ... }
3497 typedef int I;
3498
3499 we will stop after the body of the erroneously declared
3500 function, but before consuming the following `typedef'
3501 declaration. */
3502 if (--nesting_depth == 0)
3503 {
3504 cp_lexer_consume_token (parser->lexer);
3505 return;
3506 }
3507 break;
3508
3509 case CPP_OPEN_BRACE:
3510 ++nesting_depth;
3511 break;
3512
3513 default:
3514 break;
3515 }
3516
3517 /* Consume the token. */
3518 cp_lexer_consume_token (parser->lexer);
3519 }
3520 }
3521
3522 /* This function is called at the end of a statement or declaration.
3523 If the next token is a semicolon, it is consumed; otherwise, error
3524 recovery is attempted. */
3525
3526 static void
3527 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3528 {
3529 /* Look for the trailing `;'. */
3530 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3531 {
3532 /* If there is additional (erroneous) input, skip to the end of
3533 the statement. */
3534 cp_parser_skip_to_end_of_statement (parser);
3535 /* If the next token is now a `;', consume it. */
3536 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3537 cp_lexer_consume_token (parser->lexer);
3538 }
3539 }
3540
3541 /* Skip tokens until we have consumed an entire block, or until we
3542 have consumed a non-nested `;'. */
3543
3544 static void
3545 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3546 {
3547 int nesting_depth = 0;
3548
3549 /* Unwind generic function template scope if necessary. */
3550 if (parser->fully_implicit_function_template_p)
3551 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3552
3553 while (nesting_depth >= 0)
3554 {
3555 cp_token *token = cp_lexer_peek_token (parser->lexer);
3556
3557 switch (token->type)
3558 {
3559 case CPP_EOF:
3560 case CPP_PRAGMA_EOL:
3561 /* If we've run out of tokens, stop. */
3562 return;
3563
3564 case CPP_SEMICOLON:
3565 /* Stop if this is an unnested ';'. */
3566 if (!nesting_depth)
3567 nesting_depth = -1;
3568 break;
3569
3570 case CPP_CLOSE_BRACE:
3571 /* Stop if this is an unnested '}', or closes the outermost
3572 nesting level. */
3573 nesting_depth--;
3574 if (nesting_depth < 0)
3575 return;
3576 if (!nesting_depth)
3577 nesting_depth = -1;
3578 break;
3579
3580 case CPP_OPEN_BRACE:
3581 /* Nest. */
3582 nesting_depth++;
3583 break;
3584
3585 default:
3586 break;
3587 }
3588
3589 /* Consume the token. */
3590 cp_lexer_consume_token (parser->lexer);
3591 }
3592 }
3593
3594 /* Skip tokens until a non-nested closing curly brace is the next
3595 token, or there are no more tokens. Return true in the first case,
3596 false otherwise. */
3597
3598 static bool
3599 cp_parser_skip_to_closing_brace (cp_parser *parser)
3600 {
3601 unsigned nesting_depth = 0;
3602
3603 while (true)
3604 {
3605 cp_token *token = cp_lexer_peek_token (parser->lexer);
3606
3607 switch (token->type)
3608 {
3609 case CPP_EOF:
3610 case CPP_PRAGMA_EOL:
3611 /* If we've run out of tokens, stop. */
3612 return false;
3613
3614 case CPP_CLOSE_BRACE:
3615 /* If the next token is a non-nested `}', then we have reached
3616 the end of the current block. */
3617 if (nesting_depth-- == 0)
3618 return true;
3619 break;
3620
3621 case CPP_OPEN_BRACE:
3622 /* If it the next token is a `{', then we are entering a new
3623 block. Consume the entire block. */
3624 ++nesting_depth;
3625 break;
3626
3627 default:
3628 break;
3629 }
3630
3631 /* Consume the token. */
3632 cp_lexer_consume_token (parser->lexer);
3633 }
3634 }
3635
3636 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3637 parameter is the PRAGMA token, allowing us to purge the entire pragma
3638 sequence. */
3639
3640 static void
3641 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3642 {
3643 cp_token *token;
3644
3645 parser->lexer->in_pragma = false;
3646
3647 do
3648 token = cp_lexer_consume_token (parser->lexer);
3649 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3650
3651 /* Ensure that the pragma is not parsed again. */
3652 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3653 }
3654
3655 /* Require pragma end of line, resyncing with it as necessary. The
3656 arguments are as for cp_parser_skip_to_pragma_eol. */
3657
3658 static void
3659 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3660 {
3661 parser->lexer->in_pragma = false;
3662 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3663 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3664 }
3665
3666 /* This is a simple wrapper around make_typename_type. When the id is
3667 an unresolved identifier node, we can provide a superior diagnostic
3668 using cp_parser_diagnose_invalid_type_name. */
3669
3670 static tree
3671 cp_parser_make_typename_type (cp_parser *parser, tree id,
3672 location_t id_location)
3673 {
3674 tree result;
3675 if (identifier_p (id))
3676 {
3677 result = make_typename_type (parser->scope, id, typename_type,
3678 /*complain=*/tf_none);
3679 if (result == error_mark_node)
3680 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3681 return result;
3682 }
3683 return make_typename_type (parser->scope, id, typename_type, tf_error);
3684 }
3685
3686 /* This is a wrapper around the
3687 make_{pointer,ptrmem,reference}_declarator functions that decides
3688 which one to call based on the CODE and CLASS_TYPE arguments. The
3689 CODE argument should be one of the values returned by
3690 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3691 appertain to the pointer or reference. */
3692
3693 static cp_declarator *
3694 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3695 cp_cv_quals cv_qualifiers,
3696 cp_declarator *target,
3697 tree attributes)
3698 {
3699 if (code == ERROR_MARK)
3700 return cp_error_declarator;
3701
3702 if (code == INDIRECT_REF)
3703 if (class_type == NULL_TREE)
3704 return make_pointer_declarator (cv_qualifiers, target, attributes);
3705 else
3706 return make_ptrmem_declarator (cv_qualifiers, class_type,
3707 target, attributes);
3708 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3709 return make_reference_declarator (cv_qualifiers, target,
3710 false, attributes);
3711 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3712 return make_reference_declarator (cv_qualifiers, target,
3713 true, attributes);
3714 gcc_unreachable ();
3715 }
3716
3717 /* Create a new C++ parser. */
3718
3719 static cp_parser *
3720 cp_parser_new (void)
3721 {
3722 cp_parser *parser;
3723 cp_lexer *lexer;
3724 unsigned i;
3725
3726 /* cp_lexer_new_main is called before doing GC allocation because
3727 cp_lexer_new_main might load a PCH file. */
3728 lexer = cp_lexer_new_main ();
3729
3730 /* Initialize the binops_by_token so that we can get the tree
3731 directly from the token. */
3732 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3733 binops_by_token[binops[i].token_type] = binops[i];
3734
3735 parser = ggc_cleared_alloc<cp_parser> ();
3736 parser->lexer = lexer;
3737 parser->context = cp_parser_context_new (NULL);
3738
3739 /* For now, we always accept GNU extensions. */
3740 parser->allow_gnu_extensions_p = 1;
3741
3742 /* The `>' token is a greater-than operator, not the end of a
3743 template-id. */
3744 parser->greater_than_is_operator_p = true;
3745
3746 parser->default_arg_ok_p = true;
3747
3748 /* We are not parsing a constant-expression. */
3749 parser->integral_constant_expression_p = false;
3750 parser->allow_non_integral_constant_expression_p = false;
3751 parser->non_integral_constant_expression_p = false;
3752
3753 /* Local variable names are not forbidden. */
3754 parser->local_variables_forbidden_p = false;
3755
3756 /* We are not processing an `extern "C"' declaration. */
3757 parser->in_unbraced_linkage_specification_p = false;
3758
3759 /* We are not processing a declarator. */
3760 parser->in_declarator_p = false;
3761
3762 /* We are not processing a template-argument-list. */
3763 parser->in_template_argument_list_p = false;
3764
3765 /* We are not in an iteration statement. */
3766 parser->in_statement = 0;
3767
3768 /* We are not in a switch statement. */
3769 parser->in_switch_statement_p = false;
3770
3771 /* We are not parsing a type-id inside an expression. */
3772 parser->in_type_id_in_expr_p = false;
3773
3774 /* Declarations aren't implicitly extern "C". */
3775 parser->implicit_extern_c = false;
3776
3777 /* String literals should be translated to the execution character set. */
3778 parser->translate_strings_p = true;
3779
3780 /* We are not parsing a function body. */
3781 parser->in_function_body = false;
3782
3783 /* We can correct until told otherwise. */
3784 parser->colon_corrects_to_scope_p = true;
3785
3786 /* The unparsed function queue is empty. */
3787 push_unparsed_function_queues (parser);
3788
3789 /* There are no classes being defined. */
3790 parser->num_classes_being_defined = 0;
3791
3792 /* No template parameters apply. */
3793 parser->num_template_parameter_lists = 0;
3794
3795 /* Special parsing data structures. */
3796 parser->omp_declare_simd = NULL;
3797 parser->cilk_simd_fn_info = NULL;
3798 parser->oacc_routine = NULL;
3799
3800 /* Not declaring an implicit function template. */
3801 parser->auto_is_implicit_function_template_parm_p = false;
3802 parser->fully_implicit_function_template_p = false;
3803 parser->implicit_template_parms = 0;
3804 parser->implicit_template_scope = 0;
3805
3806 /* Allow constrained-type-specifiers. */
3807 parser->prevent_constrained_type_specifiers = 0;
3808
3809 return parser;
3810 }
3811
3812 /* Create a cp_lexer structure which will emit the tokens in CACHE
3813 and push it onto the parser's lexer stack. This is used for delayed
3814 parsing of in-class method bodies and default arguments, and should
3815 not be confused with tentative parsing. */
3816 static void
3817 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3818 {
3819 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3820 lexer->next = parser->lexer;
3821 parser->lexer = lexer;
3822
3823 /* Move the current source position to that of the first token in the
3824 new lexer. */
3825 cp_lexer_set_source_position_from_token (lexer->next_token);
3826 }
3827
3828 /* Pop the top lexer off the parser stack. This is never used for the
3829 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3830 static void
3831 cp_parser_pop_lexer (cp_parser *parser)
3832 {
3833 cp_lexer *lexer = parser->lexer;
3834 parser->lexer = lexer->next;
3835 cp_lexer_destroy (lexer);
3836
3837 /* Put the current source position back where it was before this
3838 lexer was pushed. */
3839 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3840 }
3841
3842 /* Lexical conventions [gram.lex] */
3843
3844 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3845 identifier. */
3846
3847 static cp_expr
3848 cp_parser_identifier (cp_parser* parser)
3849 {
3850 cp_token *token;
3851
3852 /* Look for the identifier. */
3853 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3854 /* Return the value. */
3855 if (token)
3856 return cp_expr (token->u.value, token->location);
3857 else
3858 return error_mark_node;
3859 }
3860
3861 /* Parse a sequence of adjacent string constants. Returns a
3862 TREE_STRING representing the combined, nul-terminated string
3863 constant. If TRANSLATE is true, translate the string to the
3864 execution character set. If WIDE_OK is true, a wide string is
3865 invalid here.
3866
3867 C++98 [lex.string] says that if a narrow string literal token is
3868 adjacent to a wide string literal token, the behavior is undefined.
3869 However, C99 6.4.5p4 says that this results in a wide string literal.
3870 We follow C99 here, for consistency with the C front end.
3871
3872 This code is largely lifted from lex_string() in c-lex.c.
3873
3874 FUTURE: ObjC++ will need to handle @-strings here. */
3875 static cp_expr
3876 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3877 bool lookup_udlit = true)
3878 {
3879 tree value;
3880 size_t count;
3881 struct obstack str_ob;
3882 cpp_string str, istr, *strs;
3883 cp_token *tok;
3884 enum cpp_ttype type, curr_type;
3885 int have_suffix_p = 0;
3886 tree string_tree;
3887 tree suffix_id = NULL_TREE;
3888 bool curr_tok_is_userdef_p = false;
3889
3890 tok = cp_lexer_peek_token (parser->lexer);
3891 if (!cp_parser_is_string_literal (tok))
3892 {
3893 cp_parser_error (parser, "expected string-literal");
3894 return error_mark_node;
3895 }
3896
3897 location_t loc = tok->location;
3898
3899 if (cpp_userdef_string_p (tok->type))
3900 {
3901 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3902 curr_type = cpp_userdef_string_remove_type (tok->type);
3903 curr_tok_is_userdef_p = true;
3904 }
3905 else
3906 {
3907 string_tree = tok->u.value;
3908 curr_type = tok->type;
3909 }
3910 type = curr_type;
3911
3912 /* Try to avoid the overhead of creating and destroying an obstack
3913 for the common case of just one string. */
3914 if (!cp_parser_is_string_literal
3915 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3916 {
3917 cp_lexer_consume_token (parser->lexer);
3918
3919 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3920 str.len = TREE_STRING_LENGTH (string_tree);
3921 count = 1;
3922
3923 if (curr_tok_is_userdef_p)
3924 {
3925 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3926 have_suffix_p = 1;
3927 curr_type = cpp_userdef_string_remove_type (tok->type);
3928 }
3929 else
3930 curr_type = tok->type;
3931
3932 strs = &str;
3933 }
3934 else
3935 {
3936 location_t last_tok_loc = tok->location;
3937 gcc_obstack_init (&str_ob);
3938 count = 0;
3939
3940 do
3941 {
3942 cp_lexer_consume_token (parser->lexer);
3943 count++;
3944 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3945 str.len = TREE_STRING_LENGTH (string_tree);
3946
3947 if (curr_tok_is_userdef_p)
3948 {
3949 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3950 if (have_suffix_p == 0)
3951 {
3952 suffix_id = curr_suffix_id;
3953 have_suffix_p = 1;
3954 }
3955 else if (have_suffix_p == 1
3956 && curr_suffix_id != suffix_id)
3957 {
3958 error ("inconsistent user-defined literal suffixes"
3959 " %qD and %qD in string literal",
3960 suffix_id, curr_suffix_id);
3961 have_suffix_p = -1;
3962 }
3963 curr_type = cpp_userdef_string_remove_type (tok->type);
3964 }
3965 else
3966 curr_type = tok->type;
3967
3968 if (type != curr_type)
3969 {
3970 if (type == CPP_STRING)
3971 type = curr_type;
3972 else if (curr_type != CPP_STRING)
3973 {
3974 rich_location rich_loc (line_table, tok->location);
3975 rich_loc.add_range (last_tok_loc, false);
3976 error_at_rich_loc (&rich_loc,
3977 "unsupported non-standard concatenation "
3978 "of string literals");
3979 }
3980 }
3981
3982 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3983
3984 last_tok_loc = tok->location;
3985
3986 tok = cp_lexer_peek_token (parser->lexer);
3987 if (cpp_userdef_string_p (tok->type))
3988 {
3989 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3990 curr_type = cpp_userdef_string_remove_type (tok->type);
3991 curr_tok_is_userdef_p = true;
3992 }
3993 else
3994 {
3995 string_tree = tok->u.value;
3996 curr_type = tok->type;
3997 curr_tok_is_userdef_p = false;
3998 }
3999 }
4000 while (cp_parser_is_string_literal (tok));
4001
4002 /* A string literal built by concatenation has its caret=start at
4003 the start of the initial string, and its finish at the finish of
4004 the final string literal. */
4005 loc = make_location (loc, loc, get_finish (last_tok_loc));
4006
4007 strs = (cpp_string *) obstack_finish (&str_ob);
4008 }
4009
4010 if (type != CPP_STRING && !wide_ok)
4011 {
4012 cp_parser_error (parser, "a wide string is invalid in this context");
4013 type = CPP_STRING;
4014 }
4015
4016 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4017 (parse_in, strs, count, &istr, type))
4018 {
4019 value = build_string (istr.len, (const char *)istr.text);
4020 free (CONST_CAST (unsigned char *, istr.text));
4021
4022 switch (type)
4023 {
4024 default:
4025 case CPP_STRING:
4026 case CPP_UTF8STRING:
4027 TREE_TYPE (value) = char_array_type_node;
4028 break;
4029 case CPP_STRING16:
4030 TREE_TYPE (value) = char16_array_type_node;
4031 break;
4032 case CPP_STRING32:
4033 TREE_TYPE (value) = char32_array_type_node;
4034 break;
4035 case CPP_WSTRING:
4036 TREE_TYPE (value) = wchar_array_type_node;
4037 break;
4038 }
4039
4040 value = fix_string_type (value);
4041
4042 if (have_suffix_p)
4043 {
4044 tree literal = build_userdef_literal (suffix_id, value,
4045 OT_NONE, NULL_TREE);
4046 if (lookup_udlit)
4047 value = cp_parser_userdef_string_literal (literal);
4048 else
4049 value = literal;
4050 }
4051 }
4052 else
4053 /* cpp_interpret_string has issued an error. */
4054 value = error_mark_node;
4055
4056 if (count > 1)
4057 obstack_free (&str_ob, 0);
4058
4059 return cp_expr (value, loc);
4060 }
4061
4062 /* Look up a literal operator with the name and the exact arguments. */
4063
4064 static tree
4065 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4066 {
4067 tree decl, fns;
4068 decl = lookup_name (name);
4069 if (!decl || !is_overloaded_fn (decl))
4070 return error_mark_node;
4071
4072 for (fns = decl; fns; fns = OVL_NEXT (fns))
4073 {
4074 unsigned int ix;
4075 bool found = true;
4076 tree fn = OVL_CURRENT (fns);
4077 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4078 if (parmtypes != NULL_TREE)
4079 {
4080 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4081 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4082 {
4083 tree tparm = TREE_VALUE (parmtypes);
4084 tree targ = TREE_TYPE ((*args)[ix]);
4085 bool ptr = TYPE_PTR_P (tparm);
4086 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4087 if ((ptr || arr || !same_type_p (tparm, targ))
4088 && (!ptr || !arr
4089 || !same_type_p (TREE_TYPE (tparm),
4090 TREE_TYPE (targ))))
4091 found = false;
4092 }
4093 if (found
4094 && ix == vec_safe_length (args)
4095 /* May be this should be sufficient_parms_p instead,
4096 depending on how exactly should user-defined literals
4097 work in presence of default arguments on the literal
4098 operator parameters. */
4099 && parmtypes == void_list_node)
4100 return decl;
4101 }
4102 }
4103
4104 return error_mark_node;
4105 }
4106
4107 /* Parse a user-defined char constant. Returns a call to a user-defined
4108 literal operator taking the character as an argument. */
4109
4110 static cp_expr
4111 cp_parser_userdef_char_literal (cp_parser *parser)
4112 {
4113 cp_token *token = cp_lexer_consume_token (parser->lexer);
4114 tree literal = token->u.value;
4115 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4116 tree value = USERDEF_LITERAL_VALUE (literal);
4117 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4118 tree decl, result;
4119
4120 /* Build up a call to the user-defined operator */
4121 /* Lookup the name we got back from the id-expression. */
4122 vec<tree, va_gc> *args = make_tree_vector ();
4123 vec_safe_push (args, value);
4124 decl = lookup_literal_operator (name, args);
4125 if (!decl || decl == error_mark_node)
4126 {
4127 error ("unable to find character literal operator %qD with %qT argument",
4128 name, TREE_TYPE (value));
4129 release_tree_vector (args);
4130 return error_mark_node;
4131 }
4132 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4133 release_tree_vector (args);
4134 return result;
4135 }
4136
4137 /* A subroutine of cp_parser_userdef_numeric_literal to
4138 create a char... template parameter pack from a string node. */
4139
4140 static tree
4141 make_char_string_pack (tree value)
4142 {
4143 tree charvec;
4144 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4145 const char *str = TREE_STRING_POINTER (value);
4146 int i, len = TREE_STRING_LENGTH (value) - 1;
4147 tree argvec = make_tree_vec (1);
4148
4149 /* Fill in CHARVEC with all of the parameters. */
4150 charvec = make_tree_vec (len);
4151 for (i = 0; i < len; ++i)
4152 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4153
4154 /* Build the argument packs. */
4155 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4156 TREE_TYPE (argpack) = char_type_node;
4157
4158 TREE_VEC_ELT (argvec, 0) = argpack;
4159
4160 return argvec;
4161 }
4162
4163 /* A subroutine of cp_parser_userdef_numeric_literal to
4164 create a char... template parameter pack from a string node. */
4165
4166 static tree
4167 make_string_pack (tree value)
4168 {
4169 tree charvec;
4170 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4171 const unsigned char *str
4172 = (const unsigned char *) TREE_STRING_POINTER (value);
4173 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4174 int len = TREE_STRING_LENGTH (value) / sz - 1;
4175 tree argvec = make_tree_vec (2);
4176
4177 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4178 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4179
4180 /* First template parm is character type. */
4181 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4182
4183 /* Fill in CHARVEC with all of the parameters. */
4184 charvec = make_tree_vec (len);
4185 for (int i = 0; i < len; ++i)
4186 TREE_VEC_ELT (charvec, i)
4187 = double_int_to_tree (str_char_type_node,
4188 double_int::from_buffer (str + i * sz, sz));
4189
4190 /* Build the argument packs. */
4191 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4192 TREE_TYPE (argpack) = str_char_type_node;
4193
4194 TREE_VEC_ELT (argvec, 1) = argpack;
4195
4196 return argvec;
4197 }
4198
4199 /* Parse a user-defined numeric constant. returns a call to a user-defined
4200 literal operator. */
4201
4202 static cp_expr
4203 cp_parser_userdef_numeric_literal (cp_parser *parser)
4204 {
4205 cp_token *token = cp_lexer_consume_token (parser->lexer);
4206 tree literal = token->u.value;
4207 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4208 tree value = USERDEF_LITERAL_VALUE (literal);
4209 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4210 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4211 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4212 tree decl, result;
4213 vec<tree, va_gc> *args;
4214
4215 /* Look for a literal operator taking the exact type of numeric argument
4216 as the literal value. */
4217 args = make_tree_vector ();
4218 vec_safe_push (args, value);
4219 decl = lookup_literal_operator (name, args);
4220 if (decl && decl != error_mark_node)
4221 {
4222 result = finish_call_expr (decl, &args, false, true,
4223 tf_warning_or_error);
4224
4225 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4226 {
4227 warning_at (token->location, OPT_Woverflow,
4228 "integer literal exceeds range of %qT type",
4229 long_long_unsigned_type_node);
4230 }
4231 else
4232 {
4233 if (overflow > 0)
4234 warning_at (token->location, OPT_Woverflow,
4235 "floating literal exceeds range of %qT type",
4236 long_double_type_node);
4237 else if (overflow < 0)
4238 warning_at (token->location, OPT_Woverflow,
4239 "floating literal truncated to zero");
4240 }
4241
4242 release_tree_vector (args);
4243 return result;
4244 }
4245 release_tree_vector (args);
4246
4247 /* If the numeric argument didn't work, look for a raw literal
4248 operator taking a const char* argument consisting of the number
4249 in string format. */
4250 args = make_tree_vector ();
4251 vec_safe_push (args, num_string);
4252 decl = lookup_literal_operator (name, args);
4253 if (decl && decl != error_mark_node)
4254 {
4255 result = finish_call_expr (decl, &args, false, true,
4256 tf_warning_or_error);
4257 release_tree_vector (args);
4258 return result;
4259 }
4260 release_tree_vector (args);
4261
4262 /* If the raw literal didn't work, look for a non-type template
4263 function with parameter pack char.... Call the function with
4264 template parameter characters representing the number. */
4265 args = make_tree_vector ();
4266 decl = lookup_literal_operator (name, args);
4267 if (decl && decl != error_mark_node)
4268 {
4269 tree tmpl_args = make_char_string_pack (num_string);
4270 decl = lookup_template_function (decl, tmpl_args);
4271 result = finish_call_expr (decl, &args, false, true,
4272 tf_warning_or_error);
4273 release_tree_vector (args);
4274 return result;
4275 }
4276
4277 release_tree_vector (args);
4278
4279 error ("unable to find numeric literal operator %qD", name);
4280 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4281 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4282 "to enable more built-in suffixes");
4283 return error_mark_node;
4284 }
4285
4286 /* Parse a user-defined string constant. Returns a call to a user-defined
4287 literal operator taking a character pointer and the length of the string
4288 as arguments. */
4289
4290 static tree
4291 cp_parser_userdef_string_literal (tree literal)
4292 {
4293 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4294 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4295 tree value = USERDEF_LITERAL_VALUE (literal);
4296 int len = TREE_STRING_LENGTH (value)
4297 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4298 tree decl, result;
4299 vec<tree, va_gc> *args;
4300
4301 /* Build up a call to the user-defined operator. */
4302 /* Lookup the name we got back from the id-expression. */
4303 args = make_tree_vector ();
4304 vec_safe_push (args, value);
4305 vec_safe_push (args, build_int_cst (size_type_node, len));
4306 decl = lookup_literal_operator (name, args);
4307
4308 if (decl && decl != error_mark_node)
4309 {
4310 result = finish_call_expr (decl, &args, false, true,
4311 tf_warning_or_error);
4312 release_tree_vector (args);
4313 return result;
4314 }
4315 release_tree_vector (args);
4316
4317 /* Look for a template function with typename parameter CharT
4318 and parameter pack CharT... Call the function with
4319 template parameter characters representing the string. */
4320 args = make_tree_vector ();
4321 decl = lookup_literal_operator (name, args);
4322 if (decl && decl != error_mark_node)
4323 {
4324 tree tmpl_args = make_string_pack (value);
4325 decl = lookup_template_function (decl, tmpl_args);
4326 result = finish_call_expr (decl, &args, false, true,
4327 tf_warning_or_error);
4328 release_tree_vector (args);
4329 return result;
4330 }
4331 release_tree_vector (args);
4332
4333 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4334 name, TREE_TYPE (value), size_type_node);
4335 return error_mark_node;
4336 }
4337
4338
4339 /* Basic concepts [gram.basic] */
4340
4341 /* Parse a translation-unit.
4342
4343 translation-unit:
4344 declaration-seq [opt]
4345
4346 Returns TRUE if all went well. */
4347
4348 static bool
4349 cp_parser_translation_unit (cp_parser* parser)
4350 {
4351 /* The address of the first non-permanent object on the declarator
4352 obstack. */
4353 static void *declarator_obstack_base;
4354
4355 bool success;
4356
4357 /* Create the declarator obstack, if necessary. */
4358 if (!cp_error_declarator)
4359 {
4360 gcc_obstack_init (&declarator_obstack);
4361 /* Create the error declarator. */
4362 cp_error_declarator = make_declarator (cdk_error);
4363 /* Create the empty parameter list. */
4364 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4365 /* Remember where the base of the declarator obstack lies. */
4366 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4367 }
4368
4369 cp_parser_declaration_seq_opt (parser);
4370
4371 /* If there are no tokens left then all went well. */
4372 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4373 {
4374 /* Get rid of the token array; we don't need it any more. */
4375 cp_lexer_destroy (parser->lexer);
4376 parser->lexer = NULL;
4377
4378 /* This file might have been a context that's implicitly extern
4379 "C". If so, pop the lang context. (Only relevant for PCH.) */
4380 if (parser->implicit_extern_c)
4381 {
4382 pop_lang_context ();
4383 parser->implicit_extern_c = false;
4384 }
4385
4386 /* Finish up. */
4387 finish_translation_unit ();
4388
4389 success = true;
4390 }
4391 else
4392 {
4393 cp_parser_error (parser, "expected declaration");
4394 success = false;
4395 }
4396
4397 /* Make sure the declarator obstack was fully cleaned up. */
4398 gcc_assert (obstack_next_free (&declarator_obstack)
4399 == declarator_obstack_base);
4400
4401 /* All went well. */
4402 return success;
4403 }
4404
4405 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4406 decltype context. */
4407
4408 static inline tsubst_flags_t
4409 complain_flags (bool decltype_p)
4410 {
4411 tsubst_flags_t complain = tf_warning_or_error;
4412 if (decltype_p)
4413 complain |= tf_decltype;
4414 return complain;
4415 }
4416
4417 /* We're about to parse a collection of statements. If we're currently
4418 parsing tentatively, set up a firewall so that any nested
4419 cp_parser_commit_to_tentative_parse won't affect the current context. */
4420
4421 static cp_token_position
4422 cp_parser_start_tentative_firewall (cp_parser *parser)
4423 {
4424 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4425 return 0;
4426
4427 cp_parser_parse_tentatively (parser);
4428 cp_parser_commit_to_topmost_tentative_parse (parser);
4429 return cp_lexer_token_position (parser->lexer, false);
4430 }
4431
4432 /* We've finished parsing the collection of statements. Wrap up the
4433 firewall and replace the relevant tokens with the parsed form. */
4434
4435 static void
4436 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4437 tree expr)
4438 {
4439 if (!start)
4440 return;
4441
4442 /* Finish the firewall level. */
4443 cp_parser_parse_definitely (parser);
4444 /* And remember the result of the parse for when we try again. */
4445 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4446 token->type = CPP_PREPARSED_EXPR;
4447 token->u.value = expr;
4448 token->keyword = RID_MAX;
4449 cp_lexer_purge_tokens_after (parser->lexer, start);
4450 }
4451
4452 /* Like the above functions, but let the user modify the tokens. Used by
4453 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4454 later parses, so it makes sense to localize the effects of
4455 cp_parser_commit_to_tentative_parse. */
4456
4457 struct tentative_firewall
4458 {
4459 cp_parser *parser;
4460 bool set;
4461
4462 tentative_firewall (cp_parser *p): parser(p)
4463 {
4464 /* If we're currently parsing tentatively, start a committed level as a
4465 firewall and then an inner tentative parse. */
4466 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4467 {
4468 cp_parser_parse_tentatively (parser);
4469 cp_parser_commit_to_topmost_tentative_parse (parser);
4470 cp_parser_parse_tentatively (parser);
4471 }
4472 }
4473
4474 ~tentative_firewall()
4475 {
4476 if (set)
4477 {
4478 /* Finish the inner tentative parse and the firewall, propagating any
4479 uncommitted error state to the outer tentative parse. */
4480 bool err = cp_parser_error_occurred (parser);
4481 cp_parser_parse_definitely (parser);
4482 cp_parser_parse_definitely (parser);
4483 if (err)
4484 cp_parser_simulate_error (parser);
4485 }
4486 }
4487 };
4488
4489 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4490 enclosing parentheses. */
4491
4492 static cp_expr
4493 cp_parser_statement_expr (cp_parser *parser)
4494 {
4495 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4496
4497 /* Consume the '('. */
4498 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4499 cp_lexer_consume_token (parser->lexer);
4500 /* Start the statement-expression. */
4501 tree expr = begin_stmt_expr ();
4502 /* Parse the compound-statement. */
4503 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4504 /* Finish up. */
4505 expr = finish_stmt_expr (expr, false);
4506 /* Consume the ')'. */
4507 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4508 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4509 cp_parser_skip_to_end_of_statement (parser);
4510
4511 cp_parser_end_tentative_firewall (parser, start, expr);
4512 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4513 return cp_expr (expr, combined_loc);
4514 }
4515
4516 /* Expressions [gram.expr] */
4517
4518 /* Parse a fold-operator.
4519
4520 fold-operator:
4521 - * / % ^ & | = < > << >>
4522 = -= *= /= %= ^= &= |= <<= >>=
4523 == != <= >= && || , .* ->*
4524
4525 This returns the tree code corresponding to the matched operator
4526 as an int. When the current token matches a compound assignment
4527 opertor, the resulting tree code is the negative value of the
4528 non-assignment operator. */
4529
4530 static int
4531 cp_parser_fold_operator (cp_token *token)
4532 {
4533 switch (token->type)
4534 {
4535 case CPP_PLUS: return PLUS_EXPR;
4536 case CPP_MINUS: return MINUS_EXPR;
4537 case CPP_MULT: return MULT_EXPR;
4538 case CPP_DIV: return TRUNC_DIV_EXPR;
4539 case CPP_MOD: return TRUNC_MOD_EXPR;
4540 case CPP_XOR: return BIT_XOR_EXPR;
4541 case CPP_AND: return BIT_AND_EXPR;
4542 case CPP_OR: return BIT_IOR_EXPR;
4543 case CPP_LSHIFT: return LSHIFT_EXPR;
4544 case CPP_RSHIFT: return RSHIFT_EXPR;
4545
4546 case CPP_EQ: return -NOP_EXPR;
4547 case CPP_PLUS_EQ: return -PLUS_EXPR;
4548 case CPP_MINUS_EQ: return -MINUS_EXPR;
4549 case CPP_MULT_EQ: return -MULT_EXPR;
4550 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4551 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4552 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4553 case CPP_AND_EQ: return -BIT_AND_EXPR;
4554 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4555 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4556 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4557
4558 case CPP_EQ_EQ: return EQ_EXPR;
4559 case CPP_NOT_EQ: return NE_EXPR;
4560 case CPP_LESS: return LT_EXPR;
4561 case CPP_GREATER: return GT_EXPR;
4562 case CPP_LESS_EQ: return LE_EXPR;
4563 case CPP_GREATER_EQ: return GE_EXPR;
4564
4565 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4566 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4567
4568 case CPP_COMMA: return COMPOUND_EXPR;
4569
4570 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4571 case CPP_DEREF_STAR: return MEMBER_REF;
4572
4573 default: return ERROR_MARK;
4574 }
4575 }
4576
4577 /* Returns true if CODE indicates a binary expression, which is not allowed in
4578 the LHS of a fold-expression. More codes will need to be added to use this
4579 function in other contexts. */
4580
4581 static bool
4582 is_binary_op (tree_code code)
4583 {
4584 switch (code)
4585 {
4586 case PLUS_EXPR:
4587 case POINTER_PLUS_EXPR:
4588 case MINUS_EXPR:
4589 case MULT_EXPR:
4590 case TRUNC_DIV_EXPR:
4591 case TRUNC_MOD_EXPR:
4592 case BIT_XOR_EXPR:
4593 case BIT_AND_EXPR:
4594 case BIT_IOR_EXPR:
4595 case LSHIFT_EXPR:
4596 case RSHIFT_EXPR:
4597
4598 case MODOP_EXPR:
4599
4600 case EQ_EXPR:
4601 case NE_EXPR:
4602 case LE_EXPR:
4603 case GE_EXPR:
4604 case LT_EXPR:
4605 case GT_EXPR:
4606
4607 case TRUTH_ANDIF_EXPR:
4608 case TRUTH_ORIF_EXPR:
4609
4610 case COMPOUND_EXPR:
4611
4612 case DOTSTAR_EXPR:
4613 case MEMBER_REF:
4614 return true;
4615
4616 default:
4617 return false;
4618 }
4619 }
4620
4621 /* If the next token is a suitable fold operator, consume it and return as
4622 the function above. */
4623
4624 static int
4625 cp_parser_fold_operator (cp_parser *parser)
4626 {
4627 cp_token* token = cp_lexer_peek_token (parser->lexer);
4628 int code = cp_parser_fold_operator (token);
4629 if (code != ERROR_MARK)
4630 cp_lexer_consume_token (parser->lexer);
4631 return code;
4632 }
4633
4634 /* Parse a fold-expression.
4635
4636 fold-expression:
4637 ( ... folding-operator cast-expression)
4638 ( cast-expression folding-operator ... )
4639 ( cast-expression folding operator ... folding-operator cast-expression)
4640
4641 Note that the '(' and ')' are matched in primary expression. */
4642
4643 static cp_expr
4644 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4645 {
4646 cp_id_kind pidk;
4647
4648 // Left fold.
4649 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4650 {
4651 cp_lexer_consume_token (parser->lexer);
4652 int op = cp_parser_fold_operator (parser);
4653 if (op == ERROR_MARK)
4654 {
4655 cp_parser_error (parser, "expected binary operator");
4656 return error_mark_node;
4657 }
4658
4659 tree expr = cp_parser_cast_expression (parser, false, false,
4660 false, &pidk);
4661 if (expr == error_mark_node)
4662 return error_mark_node;
4663 return finish_left_unary_fold_expr (expr, op);
4664 }
4665
4666 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4667 int op = cp_parser_fold_operator (parser);
4668 if (op == ERROR_MARK)
4669 {
4670 cp_parser_error (parser, "expected binary operator");
4671 return error_mark_node;
4672 }
4673
4674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4675 {
4676 cp_parser_error (parser, "expected ...");
4677 return error_mark_node;
4678 }
4679 cp_lexer_consume_token (parser->lexer);
4680
4681 /* The operands of a fold-expression are cast-expressions, so binary or
4682 conditional expressions are not allowed. We check this here to avoid
4683 tentative parsing. */
4684 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4685 /* OK, the expression was parenthesized. */;
4686 else if (is_binary_op (TREE_CODE (expr1)))
4687 error_at (location_of (expr1),
4688 "binary expression in operand of fold-expression");
4689 else if (TREE_CODE (expr1) == COND_EXPR)
4690 error_at (location_of (expr1),
4691 "conditional expression in operand of fold-expression");
4692
4693 // Right fold.
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4695 return finish_right_unary_fold_expr (expr1, op);
4696
4697 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4698 {
4699 cp_parser_error (parser, "mismatched operator in fold-expression");
4700 return error_mark_node;
4701 }
4702 cp_lexer_consume_token (parser->lexer);
4703
4704 // Binary left or right fold.
4705 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4706 if (expr2 == error_mark_node)
4707 return error_mark_node;
4708 return finish_binary_fold_expr (expr1, expr2, op);
4709 }
4710
4711 /* Parse a primary-expression.
4712
4713 primary-expression:
4714 literal
4715 this
4716 ( expression )
4717 id-expression
4718 lambda-expression (C++11)
4719
4720 GNU Extensions:
4721
4722 primary-expression:
4723 ( compound-statement )
4724 __builtin_va_arg ( assignment-expression , type-id )
4725 __builtin_offsetof ( type-id , offsetof-expression )
4726
4727 C++ Extensions:
4728 __has_nothrow_assign ( type-id )
4729 __has_nothrow_constructor ( type-id )
4730 __has_nothrow_copy ( type-id )
4731 __has_trivial_assign ( type-id )
4732 __has_trivial_constructor ( type-id )
4733 __has_trivial_copy ( type-id )
4734 __has_trivial_destructor ( type-id )
4735 __has_virtual_destructor ( type-id )
4736 __is_abstract ( type-id )
4737 __is_base_of ( type-id , type-id )
4738 __is_class ( type-id )
4739 __is_empty ( type-id )
4740 __is_enum ( type-id )
4741 __is_final ( type-id )
4742 __is_literal_type ( type-id )
4743 __is_pod ( type-id )
4744 __is_polymorphic ( type-id )
4745 __is_std_layout ( type-id )
4746 __is_trivial ( type-id )
4747 __is_union ( type-id )
4748
4749 Objective-C++ Extension:
4750
4751 primary-expression:
4752 objc-expression
4753
4754 literal:
4755 __null
4756
4757 ADDRESS_P is true iff this expression was immediately preceded by
4758 "&" and therefore might denote a pointer-to-member. CAST_P is true
4759 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4760 true iff this expression is a template argument.
4761
4762 Returns a representation of the expression. Upon return, *IDK
4763 indicates what kind of id-expression (if any) was present. */
4764
4765 static cp_expr
4766 cp_parser_primary_expression (cp_parser *parser,
4767 bool address_p,
4768 bool cast_p,
4769 bool template_arg_p,
4770 bool decltype_p,
4771 cp_id_kind *idk)
4772 {
4773 cp_token *token = NULL;
4774
4775 /* Assume the primary expression is not an id-expression. */
4776 *idk = CP_ID_KIND_NONE;
4777
4778 /* Peek at the next token. */
4779 token = cp_lexer_peek_token (parser->lexer);
4780 switch ((int) token->type)
4781 {
4782 /* literal:
4783 integer-literal
4784 character-literal
4785 floating-literal
4786 string-literal
4787 boolean-literal
4788 pointer-literal
4789 user-defined-literal */
4790 case CPP_CHAR:
4791 case CPP_CHAR16:
4792 case CPP_CHAR32:
4793 case CPP_WCHAR:
4794 case CPP_UTF8CHAR:
4795 case CPP_NUMBER:
4796 case CPP_PREPARSED_EXPR:
4797 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4798 return cp_parser_userdef_numeric_literal (parser);
4799 token = cp_lexer_consume_token (parser->lexer);
4800 if (TREE_CODE (token->u.value) == FIXED_CST)
4801 {
4802 error_at (token->location,
4803 "fixed-point types not supported in C++");
4804 return error_mark_node;
4805 }
4806 /* Floating-point literals are only allowed in an integral
4807 constant expression if they are cast to an integral or
4808 enumeration type. */
4809 if (TREE_CODE (token->u.value) == REAL_CST
4810 && parser->integral_constant_expression_p
4811 && pedantic)
4812 {
4813 /* CAST_P will be set even in invalid code like "int(2.7 +
4814 ...)". Therefore, we have to check that the next token
4815 is sure to end the cast. */
4816 if (cast_p)
4817 {
4818 cp_token *next_token;
4819
4820 next_token = cp_lexer_peek_token (parser->lexer);
4821 if (/* The comma at the end of an
4822 enumerator-definition. */
4823 next_token->type != CPP_COMMA
4824 /* The curly brace at the end of an enum-specifier. */
4825 && next_token->type != CPP_CLOSE_BRACE
4826 /* The end of a statement. */
4827 && next_token->type != CPP_SEMICOLON
4828 /* The end of the cast-expression. */
4829 && next_token->type != CPP_CLOSE_PAREN
4830 /* The end of an array bound. */
4831 && next_token->type != CPP_CLOSE_SQUARE
4832 /* The closing ">" in a template-argument-list. */
4833 && (next_token->type != CPP_GREATER
4834 || parser->greater_than_is_operator_p)
4835 /* C++0x only: A ">>" treated like two ">" tokens,
4836 in a template-argument-list. */
4837 && (next_token->type != CPP_RSHIFT
4838 || (cxx_dialect == cxx98)
4839 || parser->greater_than_is_operator_p))
4840 cast_p = false;
4841 }
4842
4843 /* If we are within a cast, then the constraint that the
4844 cast is to an integral or enumeration type will be
4845 checked at that point. If we are not within a cast, then
4846 this code is invalid. */
4847 if (!cast_p)
4848 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4849 }
4850 return cp_expr (token->u.value, token->location);
4851
4852 case CPP_CHAR_USERDEF:
4853 case CPP_CHAR16_USERDEF:
4854 case CPP_CHAR32_USERDEF:
4855 case CPP_WCHAR_USERDEF:
4856 case CPP_UTF8CHAR_USERDEF:
4857 return cp_parser_userdef_char_literal (parser);
4858
4859 case CPP_STRING:
4860 case CPP_STRING16:
4861 case CPP_STRING32:
4862 case CPP_WSTRING:
4863 case CPP_UTF8STRING:
4864 case CPP_STRING_USERDEF:
4865 case CPP_STRING16_USERDEF:
4866 case CPP_STRING32_USERDEF:
4867 case CPP_WSTRING_USERDEF:
4868 case CPP_UTF8STRING_USERDEF:
4869 /* ??? Should wide strings be allowed when parser->translate_strings_p
4870 is false (i.e. in attributes)? If not, we can kill the third
4871 argument to cp_parser_string_literal. */
4872 return cp_parser_string_literal (parser,
4873 parser->translate_strings_p,
4874 true);
4875
4876 case CPP_OPEN_PAREN:
4877 /* If we see `( { ' then we are looking at the beginning of
4878 a GNU statement-expression. */
4879 if (cp_parser_allow_gnu_extensions_p (parser)
4880 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4881 {
4882 /* Statement-expressions are not allowed by the standard. */
4883 pedwarn (token->location, OPT_Wpedantic,
4884 "ISO C++ forbids braced-groups within expressions");
4885
4886 /* And they're not allowed outside of a function-body; you
4887 cannot, for example, write:
4888
4889 int i = ({ int j = 3; j + 1; });
4890
4891 at class or namespace scope. */
4892 if (!parser->in_function_body
4893 || parser->in_template_argument_list_p)
4894 {
4895 error_at (token->location,
4896 "statement-expressions are not allowed outside "
4897 "functions nor in template-argument lists");
4898 cp_parser_skip_to_end_of_block_or_statement (parser);
4899 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4900 cp_lexer_consume_token (parser->lexer);
4901 return error_mark_node;
4902 }
4903 else
4904 return cp_parser_statement_expr (parser);
4905 }
4906 /* Otherwise it's a normal parenthesized expression. */
4907 {
4908 cp_expr expr;
4909 bool saved_greater_than_is_operator_p;
4910
4911 location_t open_paren_loc = token->location;
4912
4913 /* Consume the `('. */
4914 cp_lexer_consume_token (parser->lexer);
4915 /* Within a parenthesized expression, a `>' token is always
4916 the greater-than operator. */
4917 saved_greater_than_is_operator_p
4918 = parser->greater_than_is_operator_p;
4919 parser->greater_than_is_operator_p = true;
4920
4921 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4922 /* Left fold expression. */
4923 expr = NULL_TREE;
4924 else
4925 /* Parse the parenthesized expression. */
4926 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4927
4928 token = cp_lexer_peek_token (parser->lexer);
4929 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4930 {
4931 expr = cp_parser_fold_expression (parser, expr);
4932 if (expr != error_mark_node
4933 && cxx_dialect < cxx1z
4934 && !in_system_header_at (input_location))
4935 pedwarn (input_location, 0, "fold-expressions only available "
4936 "with -std=c++1z or -std=gnu++1z");
4937 }
4938 else
4939 /* Let the front end know that this expression was
4940 enclosed in parentheses. This matters in case, for
4941 example, the expression is of the form `A::B', since
4942 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4943 not. */
4944 expr = finish_parenthesized_expr (expr);
4945
4946 /* DR 705: Wrapping an unqualified name in parentheses
4947 suppresses arg-dependent lookup. We want to pass back
4948 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4949 (c++/37862), but none of the others. */
4950 if (*idk != CP_ID_KIND_QUALIFIED)
4951 *idk = CP_ID_KIND_NONE;
4952
4953 /* The `>' token might be the end of a template-id or
4954 template-parameter-list now. */
4955 parser->greater_than_is_operator_p
4956 = saved_greater_than_is_operator_p;
4957
4958 /* Consume the `)'. */
4959 token = cp_lexer_peek_token (parser->lexer);
4960 location_t close_paren_loc = token->location;
4961 expr.set_range (open_paren_loc, close_paren_loc);
4962 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4963 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4964 cp_parser_skip_to_end_of_statement (parser);
4965
4966 return expr;
4967 }
4968
4969 case CPP_OPEN_SQUARE:
4970 {
4971 if (c_dialect_objc ())
4972 {
4973 /* We might have an Objective-C++ message. */
4974 cp_parser_parse_tentatively (parser);
4975 tree msg = cp_parser_objc_message_expression (parser);
4976 /* If that works out, we're done ... */
4977 if (cp_parser_parse_definitely (parser))
4978 return msg;
4979 /* ... else, fall though to see if it's a lambda. */
4980 }
4981 cp_expr lam = cp_parser_lambda_expression (parser);
4982 /* Don't warn about a failed tentative parse. */
4983 if (cp_parser_error_occurred (parser))
4984 return error_mark_node;
4985 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4986 return lam;
4987 }
4988
4989 case CPP_OBJC_STRING:
4990 if (c_dialect_objc ())
4991 /* We have an Objective-C++ string literal. */
4992 return cp_parser_objc_expression (parser);
4993 cp_parser_error (parser, "expected primary-expression");
4994 return error_mark_node;
4995
4996 case CPP_KEYWORD:
4997 switch (token->keyword)
4998 {
4999 /* These two are the boolean literals. */
5000 case RID_TRUE:
5001 cp_lexer_consume_token (parser->lexer);
5002 return cp_expr (boolean_true_node, token->location);
5003 case RID_FALSE:
5004 cp_lexer_consume_token (parser->lexer);
5005 return cp_expr (boolean_false_node, token->location);
5006
5007 /* The `__null' literal. */
5008 case RID_NULL:
5009 cp_lexer_consume_token (parser->lexer);
5010 return cp_expr (null_node, token->location);
5011
5012 /* The `nullptr' literal. */
5013 case RID_NULLPTR:
5014 cp_lexer_consume_token (parser->lexer);
5015 return cp_expr (nullptr_node, token->location);
5016
5017 /* Recognize the `this' keyword. */
5018 case RID_THIS:
5019 cp_lexer_consume_token (parser->lexer);
5020 if (parser->local_variables_forbidden_p)
5021 {
5022 error_at (token->location,
5023 "%<this%> may not be used in this context");
5024 return error_mark_node;
5025 }
5026 /* Pointers cannot appear in constant-expressions. */
5027 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5028 return error_mark_node;
5029 return cp_expr (finish_this_expr (), token->location);
5030
5031 /* The `operator' keyword can be the beginning of an
5032 id-expression. */
5033 case RID_OPERATOR:
5034 goto id_expression;
5035
5036 case RID_FUNCTION_NAME:
5037 case RID_PRETTY_FUNCTION_NAME:
5038 case RID_C99_FUNCTION_NAME:
5039 {
5040 non_integral_constant name;
5041
5042 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5043 __func__ are the names of variables -- but they are
5044 treated specially. Therefore, they are handled here,
5045 rather than relying on the generic id-expression logic
5046 below. Grammatically, these names are id-expressions.
5047
5048 Consume the token. */
5049 token = cp_lexer_consume_token (parser->lexer);
5050
5051 switch (token->keyword)
5052 {
5053 case RID_FUNCTION_NAME:
5054 name = NIC_FUNC_NAME;
5055 break;
5056 case RID_PRETTY_FUNCTION_NAME:
5057 name = NIC_PRETTY_FUNC;
5058 break;
5059 case RID_C99_FUNCTION_NAME:
5060 name = NIC_C99_FUNC;
5061 break;
5062 default:
5063 gcc_unreachable ();
5064 }
5065
5066 if (cp_parser_non_integral_constant_expression (parser, name))
5067 return error_mark_node;
5068
5069 /* Look up the name. */
5070 return finish_fname (token->u.value);
5071 }
5072
5073 case RID_VA_ARG:
5074 {
5075 tree expression;
5076 tree type;
5077 source_location type_location;
5078 location_t start_loc
5079 = cp_lexer_peek_token (parser->lexer)->location;
5080 /* The `__builtin_va_arg' construct is used to handle
5081 `va_arg'. Consume the `__builtin_va_arg' token. */
5082 cp_lexer_consume_token (parser->lexer);
5083 /* Look for the opening `('. */
5084 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5085 /* Now, parse the assignment-expression. */
5086 expression = cp_parser_assignment_expression (parser);
5087 /* Look for the `,'. */
5088 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5089 type_location = cp_lexer_peek_token (parser->lexer)->location;
5090 /* Parse the type-id. */
5091 {
5092 type_id_in_expr_sentinel s (parser);
5093 type = cp_parser_type_id (parser);
5094 }
5095 /* Look for the closing `)'. */
5096 location_t finish_loc
5097 = cp_lexer_peek_token (parser->lexer)->location;
5098 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5099 /* Using `va_arg' in a constant-expression is not
5100 allowed. */
5101 if (cp_parser_non_integral_constant_expression (parser,
5102 NIC_VA_ARG))
5103 return error_mark_node;
5104 /* Construct a location of the form:
5105 __builtin_va_arg (v, int)
5106 ~~~~~~~~~~~~~~~~~~~~~^~~~
5107 with the caret at the type, ranging from the start of the
5108 "__builtin_va_arg" token to the close paren. */
5109 location_t combined_loc
5110 = make_location (type_location, start_loc, finish_loc);
5111 return build_x_va_arg (combined_loc, expression, type);
5112 }
5113
5114 case RID_OFFSETOF:
5115 return cp_parser_builtin_offsetof (parser);
5116
5117 case RID_HAS_NOTHROW_ASSIGN:
5118 case RID_HAS_NOTHROW_CONSTRUCTOR:
5119 case RID_HAS_NOTHROW_COPY:
5120 case RID_HAS_TRIVIAL_ASSIGN:
5121 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5122 case RID_HAS_TRIVIAL_COPY:
5123 case RID_HAS_TRIVIAL_DESTRUCTOR:
5124 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5125 case RID_HAS_VIRTUAL_DESTRUCTOR:
5126 case RID_IS_ABSTRACT:
5127 case RID_IS_BASE_OF:
5128 case RID_IS_CLASS:
5129 case RID_IS_EMPTY:
5130 case RID_IS_ENUM:
5131 case RID_IS_FINAL:
5132 case RID_IS_LITERAL_TYPE:
5133 case RID_IS_POD:
5134 case RID_IS_POLYMORPHIC:
5135 case RID_IS_SAME_AS:
5136 case RID_IS_STD_LAYOUT:
5137 case RID_IS_TRIVIAL:
5138 case RID_IS_TRIVIALLY_ASSIGNABLE:
5139 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5140 case RID_IS_TRIVIALLY_COPYABLE:
5141 case RID_IS_UNION:
5142 return cp_parser_trait_expr (parser, token->keyword);
5143
5144 // C++ concepts
5145 case RID_REQUIRES:
5146 return cp_parser_requires_expression (parser);
5147
5148 /* Objective-C++ expressions. */
5149 case RID_AT_ENCODE:
5150 case RID_AT_PROTOCOL:
5151 case RID_AT_SELECTOR:
5152 return cp_parser_objc_expression (parser);
5153
5154 case RID_TEMPLATE:
5155 if (parser->in_function_body
5156 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5157 == CPP_LESS))
5158 {
5159 error_at (token->location,
5160 "a template declaration cannot appear at block scope");
5161 cp_parser_skip_to_end_of_block_or_statement (parser);
5162 return error_mark_node;
5163 }
5164 /* FALLTHRU */
5165 default:
5166 cp_parser_error (parser, "expected primary-expression");
5167 return error_mark_node;
5168 }
5169
5170 /* An id-expression can start with either an identifier, a
5171 `::' as the beginning of a qualified-id, or the "operator"
5172 keyword. */
5173 case CPP_NAME:
5174 case CPP_SCOPE:
5175 case CPP_TEMPLATE_ID:
5176 case CPP_NESTED_NAME_SPECIFIER:
5177 {
5178 id_expression:
5179 cp_expr id_expression;
5180 cp_expr decl;
5181 const char *error_msg;
5182 bool template_p;
5183 bool done;
5184 cp_token *id_expr_token;
5185
5186 /* Parse the id-expression. */
5187 id_expression
5188 = cp_parser_id_expression (parser,
5189 /*template_keyword_p=*/false,
5190 /*check_dependency_p=*/true,
5191 &template_p,
5192 /*declarator_p=*/false,
5193 /*optional_p=*/false);
5194 if (id_expression == error_mark_node)
5195 return error_mark_node;
5196 id_expr_token = token;
5197 token = cp_lexer_peek_token (parser->lexer);
5198 done = (token->type != CPP_OPEN_SQUARE
5199 && token->type != CPP_OPEN_PAREN
5200 && token->type != CPP_DOT
5201 && token->type != CPP_DEREF
5202 && token->type != CPP_PLUS_PLUS
5203 && token->type != CPP_MINUS_MINUS);
5204 /* If we have a template-id, then no further lookup is
5205 required. If the template-id was for a template-class, we
5206 will sometimes have a TYPE_DECL at this point. */
5207 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5208 || TREE_CODE (id_expression) == TYPE_DECL)
5209 decl = id_expression;
5210 /* Look up the name. */
5211 else
5212 {
5213 tree ambiguous_decls;
5214
5215 /* If we already know that this lookup is ambiguous, then
5216 we've already issued an error message; there's no reason
5217 to check again. */
5218 if (id_expr_token->type == CPP_NAME
5219 && id_expr_token->error_reported)
5220 {
5221 cp_parser_simulate_error (parser);
5222 return error_mark_node;
5223 }
5224
5225 decl = cp_parser_lookup_name (parser, id_expression,
5226 none_type,
5227 template_p,
5228 /*is_namespace=*/false,
5229 /*check_dependency=*/true,
5230 &ambiguous_decls,
5231 id_expr_token->location);
5232 /* If the lookup was ambiguous, an error will already have
5233 been issued. */
5234 if (ambiguous_decls)
5235 return error_mark_node;
5236
5237 /* In Objective-C++, we may have an Objective-C 2.0
5238 dot-syntax for classes here. */
5239 if (c_dialect_objc ()
5240 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5241 && TREE_CODE (decl) == TYPE_DECL
5242 && objc_is_class_name (decl))
5243 {
5244 tree component;
5245 cp_lexer_consume_token (parser->lexer);
5246 component = cp_parser_identifier (parser);
5247 if (component == error_mark_node)
5248 return error_mark_node;
5249
5250 tree result = objc_build_class_component_ref (id_expression,
5251 component);
5252 /* Build a location of the form:
5253 expr.component
5254 ~~~~~^~~~~~~~~
5255 with caret at the start of the component name (at
5256 input_location), ranging from the start of the id_expression
5257 to the end of the component name. */
5258 location_t combined_loc
5259 = make_location (input_location, id_expression.get_start (),
5260 get_finish (input_location));
5261 protected_set_expr_location (result, combined_loc);
5262 return result;
5263 }
5264
5265 /* In Objective-C++, an instance variable (ivar) may be preferred
5266 to whatever cp_parser_lookup_name() found.
5267 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5268 rest of c-family, we have to do a little extra work to preserve
5269 any location information in cp_expr "decl". Given that
5270 objc_lookup_ivar is implemented in "c-family" and "objc", we
5271 have a trip through the pure "tree" type, rather than cp_expr.
5272 Naively copying it back to "decl" would implicitly give the
5273 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5274 store an EXPR_LOCATION. Hence we only update "decl" (and
5275 hence its location_t) if we get back a different tree node. */
5276 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5277 id_expression);
5278 if (decl_tree != decl.get_value ())
5279 decl = cp_expr (decl_tree);
5280
5281 /* If name lookup gives us a SCOPE_REF, then the
5282 qualifying scope was dependent. */
5283 if (TREE_CODE (decl) == SCOPE_REF)
5284 {
5285 /* At this point, we do not know if DECL is a valid
5286 integral constant expression. We assume that it is
5287 in fact such an expression, so that code like:
5288
5289 template <int N> struct A {
5290 int a[B<N>::i];
5291 };
5292
5293 is accepted. At template-instantiation time, we
5294 will check that B<N>::i is actually a constant. */
5295 return decl;
5296 }
5297 /* Check to see if DECL is a local variable in a context
5298 where that is forbidden. */
5299 if (parser->local_variables_forbidden_p
5300 && local_variable_p (decl))
5301 {
5302 /* It might be that we only found DECL because we are
5303 trying to be generous with pre-ISO scoping rules.
5304 For example, consider:
5305
5306 int i;
5307 void g() {
5308 for (int i = 0; i < 10; ++i) {}
5309 extern void f(int j = i);
5310 }
5311
5312 Here, name look up will originally find the out
5313 of scope `i'. We need to issue a warning message,
5314 but then use the global `i'. */
5315 decl = check_for_out_of_scope_variable (decl);
5316 if (local_variable_p (decl))
5317 {
5318 error_at (id_expr_token->location,
5319 "local variable %qD may not appear in this context",
5320 decl.get_value ());
5321 return error_mark_node;
5322 }
5323 }
5324 }
5325
5326 decl = (finish_id_expression
5327 (id_expression, decl, parser->scope,
5328 idk,
5329 parser->integral_constant_expression_p,
5330 parser->allow_non_integral_constant_expression_p,
5331 &parser->non_integral_constant_expression_p,
5332 template_p, done, address_p,
5333 template_arg_p,
5334 &error_msg,
5335 id_expr_token->location));
5336 if (error_msg)
5337 cp_parser_error (parser, error_msg);
5338 decl.set_location (id_expr_token->location);
5339 return decl;
5340 }
5341
5342 /* Anything else is an error. */
5343 default:
5344 cp_parser_error (parser, "expected primary-expression");
5345 return error_mark_node;
5346 }
5347 }
5348
5349 static inline cp_expr
5350 cp_parser_primary_expression (cp_parser *parser,
5351 bool address_p,
5352 bool cast_p,
5353 bool template_arg_p,
5354 cp_id_kind *idk)
5355 {
5356 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5357 /*decltype*/false, idk);
5358 }
5359
5360 /* Parse an id-expression.
5361
5362 id-expression:
5363 unqualified-id
5364 qualified-id
5365
5366 qualified-id:
5367 :: [opt] nested-name-specifier template [opt] unqualified-id
5368 :: identifier
5369 :: operator-function-id
5370 :: template-id
5371
5372 Return a representation of the unqualified portion of the
5373 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5374 a `::' or nested-name-specifier.
5375
5376 Often, if the id-expression was a qualified-id, the caller will
5377 want to make a SCOPE_REF to represent the qualified-id. This
5378 function does not do this in order to avoid wastefully creating
5379 SCOPE_REFs when they are not required.
5380
5381 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5382 `template' keyword.
5383
5384 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5385 uninstantiated templates.
5386
5387 If *TEMPLATE_P is non-NULL, it is set to true iff the
5388 `template' keyword is used to explicitly indicate that the entity
5389 named is a template.
5390
5391 If DECLARATOR_P is true, the id-expression is appearing as part of
5392 a declarator, rather than as part of an expression. */
5393
5394 static cp_expr
5395 cp_parser_id_expression (cp_parser *parser,
5396 bool template_keyword_p,
5397 bool check_dependency_p,
5398 bool *template_p,
5399 bool declarator_p,
5400 bool optional_p)
5401 {
5402 bool global_scope_p;
5403 bool nested_name_specifier_p;
5404
5405 /* Assume the `template' keyword was not used. */
5406 if (template_p)
5407 *template_p = template_keyword_p;
5408
5409 /* Look for the optional `::' operator. */
5410 global_scope_p
5411 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5412 != NULL_TREE);
5413 /* Look for the optional nested-name-specifier. */
5414 nested_name_specifier_p
5415 = (cp_parser_nested_name_specifier_opt (parser,
5416 /*typename_keyword_p=*/false,
5417 check_dependency_p,
5418 /*type_p=*/false,
5419 declarator_p)
5420 != NULL_TREE);
5421 /* If there is a nested-name-specifier, then we are looking at
5422 the first qualified-id production. */
5423 if (nested_name_specifier_p)
5424 {
5425 tree saved_scope;
5426 tree saved_object_scope;
5427 tree saved_qualifying_scope;
5428 tree unqualified_id;
5429 bool is_template;
5430
5431 /* See if the next token is the `template' keyword. */
5432 if (!template_p)
5433 template_p = &is_template;
5434 *template_p = cp_parser_optional_template_keyword (parser);
5435 /* Name lookup we do during the processing of the
5436 unqualified-id might obliterate SCOPE. */
5437 saved_scope = parser->scope;
5438 saved_object_scope = parser->object_scope;
5439 saved_qualifying_scope = parser->qualifying_scope;
5440 /* Process the final unqualified-id. */
5441 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5442 check_dependency_p,
5443 declarator_p,
5444 /*optional_p=*/false);
5445 /* Restore the SAVED_SCOPE for our caller. */
5446 parser->scope = saved_scope;
5447 parser->object_scope = saved_object_scope;
5448 parser->qualifying_scope = saved_qualifying_scope;
5449
5450 return unqualified_id;
5451 }
5452 /* Otherwise, if we are in global scope, then we are looking at one
5453 of the other qualified-id productions. */
5454 else if (global_scope_p)
5455 {
5456 cp_token *token;
5457 tree id;
5458
5459 /* Peek at the next token. */
5460 token = cp_lexer_peek_token (parser->lexer);
5461
5462 /* If it's an identifier, and the next token is not a "<", then
5463 we can avoid the template-id case. This is an optimization
5464 for this common case. */
5465 if (token->type == CPP_NAME
5466 && !cp_parser_nth_token_starts_template_argument_list_p
5467 (parser, 2))
5468 return cp_parser_identifier (parser);
5469
5470 cp_parser_parse_tentatively (parser);
5471 /* Try a template-id. */
5472 id = cp_parser_template_id (parser,
5473 /*template_keyword_p=*/false,
5474 /*check_dependency_p=*/true,
5475 none_type,
5476 declarator_p);
5477 /* If that worked, we're done. */
5478 if (cp_parser_parse_definitely (parser))
5479 return id;
5480
5481 /* Peek at the next token. (Changes in the token buffer may
5482 have invalidated the pointer obtained above.) */
5483 token = cp_lexer_peek_token (parser->lexer);
5484
5485 switch (token->type)
5486 {
5487 case CPP_NAME:
5488 return cp_parser_identifier (parser);
5489
5490 case CPP_KEYWORD:
5491 if (token->keyword == RID_OPERATOR)
5492 return cp_parser_operator_function_id (parser);
5493 /* Fall through. */
5494
5495 default:
5496 cp_parser_error (parser, "expected id-expression");
5497 return error_mark_node;
5498 }
5499 }
5500 else
5501 return cp_parser_unqualified_id (parser, template_keyword_p,
5502 /*check_dependency_p=*/true,
5503 declarator_p,
5504 optional_p);
5505 }
5506
5507 /* Parse an unqualified-id.
5508
5509 unqualified-id:
5510 identifier
5511 operator-function-id
5512 conversion-function-id
5513 ~ class-name
5514 template-id
5515
5516 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5517 keyword, in a construct like `A::template ...'.
5518
5519 Returns a representation of unqualified-id. For the `identifier'
5520 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5521 production a BIT_NOT_EXPR is returned; the operand of the
5522 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5523 other productions, see the documentation accompanying the
5524 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5525 names are looked up in uninstantiated templates. If DECLARATOR_P
5526 is true, the unqualified-id is appearing as part of a declarator,
5527 rather than as part of an expression. */
5528
5529 static cp_expr
5530 cp_parser_unqualified_id (cp_parser* parser,
5531 bool template_keyword_p,
5532 bool check_dependency_p,
5533 bool declarator_p,
5534 bool optional_p)
5535 {
5536 cp_token *token;
5537
5538 /* Peek at the next token. */
5539 token = cp_lexer_peek_token (parser->lexer);
5540
5541 switch ((int) token->type)
5542 {
5543 case CPP_NAME:
5544 {
5545 tree id;
5546
5547 /* We don't know yet whether or not this will be a
5548 template-id. */
5549 cp_parser_parse_tentatively (parser);
5550 /* Try a template-id. */
5551 id = cp_parser_template_id (parser, template_keyword_p,
5552 check_dependency_p,
5553 none_type,
5554 declarator_p);
5555 /* If it worked, we're done. */
5556 if (cp_parser_parse_definitely (parser))
5557 return id;
5558 /* Otherwise, it's an ordinary identifier. */
5559 return cp_parser_identifier (parser);
5560 }
5561
5562 case CPP_TEMPLATE_ID:
5563 return cp_parser_template_id (parser, template_keyword_p,
5564 check_dependency_p,
5565 none_type,
5566 declarator_p);
5567
5568 case CPP_COMPL:
5569 {
5570 tree type_decl;
5571 tree qualifying_scope;
5572 tree object_scope;
5573 tree scope;
5574 bool done;
5575
5576 /* Consume the `~' token. */
5577 cp_lexer_consume_token (parser->lexer);
5578 /* Parse the class-name. The standard, as written, seems to
5579 say that:
5580
5581 template <typename T> struct S { ~S (); };
5582 template <typename T> S<T>::~S() {}
5583
5584 is invalid, since `~' must be followed by a class-name, but
5585 `S<T>' is dependent, and so not known to be a class.
5586 That's not right; we need to look in uninstantiated
5587 templates. A further complication arises from:
5588
5589 template <typename T> void f(T t) {
5590 t.T::~T();
5591 }
5592
5593 Here, it is not possible to look up `T' in the scope of `T'
5594 itself. We must look in both the current scope, and the
5595 scope of the containing complete expression.
5596
5597 Yet another issue is:
5598
5599 struct S {
5600 int S;
5601 ~S();
5602 };
5603
5604 S::~S() {}
5605
5606 The standard does not seem to say that the `S' in `~S'
5607 should refer to the type `S' and not the data member
5608 `S::S'. */
5609
5610 /* DR 244 says that we look up the name after the "~" in the
5611 same scope as we looked up the qualifying name. That idea
5612 isn't fully worked out; it's more complicated than that. */
5613 scope = parser->scope;
5614 object_scope = parser->object_scope;
5615 qualifying_scope = parser->qualifying_scope;
5616
5617 /* Check for invalid scopes. */
5618 if (scope == error_mark_node)
5619 {
5620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5621 cp_lexer_consume_token (parser->lexer);
5622 return error_mark_node;
5623 }
5624 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5625 {
5626 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5627 error_at (token->location,
5628 "scope %qT before %<~%> is not a class-name",
5629 scope);
5630 cp_parser_simulate_error (parser);
5631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5632 cp_lexer_consume_token (parser->lexer);
5633 return error_mark_node;
5634 }
5635 gcc_assert (!scope || TYPE_P (scope));
5636
5637 /* If the name is of the form "X::~X" it's OK even if X is a
5638 typedef. */
5639 token = cp_lexer_peek_token (parser->lexer);
5640 if (scope
5641 && token->type == CPP_NAME
5642 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5643 != CPP_LESS)
5644 && (token->u.value == TYPE_IDENTIFIER (scope)
5645 || (CLASS_TYPE_P (scope)
5646 && constructor_name_p (token->u.value, scope))))
5647 {
5648 cp_lexer_consume_token (parser->lexer);
5649 return build_nt (BIT_NOT_EXPR, scope);
5650 }
5651
5652 /* ~auto means the destructor of whatever the object is. */
5653 if (cp_parser_is_keyword (token, RID_AUTO))
5654 {
5655 if (cxx_dialect < cxx14)
5656 pedwarn (input_location, 0,
5657 "%<~auto%> only available with "
5658 "-std=c++14 or -std=gnu++14");
5659 cp_lexer_consume_token (parser->lexer);
5660 return build_nt (BIT_NOT_EXPR, make_auto ());
5661 }
5662
5663 /* If there was an explicit qualification (S::~T), first look
5664 in the scope given by the qualification (i.e., S).
5665
5666 Note: in the calls to cp_parser_class_name below we pass
5667 typename_type so that lookup finds the injected-class-name
5668 rather than the constructor. */
5669 done = false;
5670 type_decl = NULL_TREE;
5671 if (scope)
5672 {
5673 cp_parser_parse_tentatively (parser);
5674 type_decl = cp_parser_class_name (parser,
5675 /*typename_keyword_p=*/false,
5676 /*template_keyword_p=*/false,
5677 typename_type,
5678 /*check_dependency=*/false,
5679 /*class_head_p=*/false,
5680 declarator_p);
5681 if (cp_parser_parse_definitely (parser))
5682 done = true;
5683 }
5684 /* In "N::S::~S", look in "N" as well. */
5685 if (!done && scope && qualifying_scope)
5686 {
5687 cp_parser_parse_tentatively (parser);
5688 parser->scope = qualifying_scope;
5689 parser->object_scope = NULL_TREE;
5690 parser->qualifying_scope = NULL_TREE;
5691 type_decl
5692 = cp_parser_class_name (parser,
5693 /*typename_keyword_p=*/false,
5694 /*template_keyword_p=*/false,
5695 typename_type,
5696 /*check_dependency=*/false,
5697 /*class_head_p=*/false,
5698 declarator_p);
5699 if (cp_parser_parse_definitely (parser))
5700 done = true;
5701 }
5702 /* In "p->S::~T", look in the scope given by "*p" as well. */
5703 else if (!done && object_scope)
5704 {
5705 cp_parser_parse_tentatively (parser);
5706 parser->scope = object_scope;
5707 parser->object_scope = NULL_TREE;
5708 parser->qualifying_scope = NULL_TREE;
5709 type_decl
5710 = cp_parser_class_name (parser,
5711 /*typename_keyword_p=*/false,
5712 /*template_keyword_p=*/false,
5713 typename_type,
5714 /*check_dependency=*/false,
5715 /*class_head_p=*/false,
5716 declarator_p);
5717 if (cp_parser_parse_definitely (parser))
5718 done = true;
5719 }
5720 /* Look in the surrounding context. */
5721 if (!done)
5722 {
5723 parser->scope = NULL_TREE;
5724 parser->object_scope = NULL_TREE;
5725 parser->qualifying_scope = NULL_TREE;
5726 if (processing_template_decl)
5727 cp_parser_parse_tentatively (parser);
5728 type_decl
5729 = cp_parser_class_name (parser,
5730 /*typename_keyword_p=*/false,
5731 /*template_keyword_p=*/false,
5732 typename_type,
5733 /*check_dependency=*/false,
5734 /*class_head_p=*/false,
5735 declarator_p);
5736 if (processing_template_decl
5737 && ! cp_parser_parse_definitely (parser))
5738 {
5739 /* We couldn't find a type with this name. If we're parsing
5740 tentatively, fail and try something else. */
5741 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5742 {
5743 cp_parser_simulate_error (parser);
5744 return error_mark_node;
5745 }
5746 /* Otherwise, accept it and check for a match at instantiation
5747 time. */
5748 type_decl = cp_parser_identifier (parser);
5749 if (type_decl != error_mark_node)
5750 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5751 return type_decl;
5752 }
5753 }
5754 /* If an error occurred, assume that the name of the
5755 destructor is the same as the name of the qualifying
5756 class. That allows us to keep parsing after running
5757 into ill-formed destructor names. */
5758 if (type_decl == error_mark_node && scope)
5759 return build_nt (BIT_NOT_EXPR, scope);
5760 else if (type_decl == error_mark_node)
5761 return error_mark_node;
5762
5763 /* Check that destructor name and scope match. */
5764 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5765 {
5766 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5767 error_at (token->location,
5768 "declaration of %<~%T%> as member of %qT",
5769 type_decl, scope);
5770 cp_parser_simulate_error (parser);
5771 return error_mark_node;
5772 }
5773
5774 /* [class.dtor]
5775
5776 A typedef-name that names a class shall not be used as the
5777 identifier in the declarator for a destructor declaration. */
5778 if (declarator_p
5779 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5780 && !DECL_SELF_REFERENCE_P (type_decl)
5781 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5782 error_at (token->location,
5783 "typedef-name %qD used as destructor declarator",
5784 type_decl);
5785
5786 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5787 }
5788
5789 case CPP_KEYWORD:
5790 if (token->keyword == RID_OPERATOR)
5791 {
5792 cp_expr id;
5793
5794 /* This could be a template-id, so we try that first. */
5795 cp_parser_parse_tentatively (parser);
5796 /* Try a template-id. */
5797 id = cp_parser_template_id (parser, template_keyword_p,
5798 /*check_dependency_p=*/true,
5799 none_type,
5800 declarator_p);
5801 /* If that worked, we're done. */
5802 if (cp_parser_parse_definitely (parser))
5803 return id;
5804 /* We still don't know whether we're looking at an
5805 operator-function-id or a conversion-function-id. */
5806 cp_parser_parse_tentatively (parser);
5807 /* Try an operator-function-id. */
5808 id = cp_parser_operator_function_id (parser);
5809 /* If that didn't work, try a conversion-function-id. */
5810 if (!cp_parser_parse_definitely (parser))
5811 id = cp_parser_conversion_function_id (parser);
5812 else if (UDLIT_OPER_P (id))
5813 {
5814 /* 17.6.3.3.5 */
5815 const char *name = UDLIT_OP_SUFFIX (id);
5816 if (name[0] != '_' && !in_system_header_at (input_location)
5817 && declarator_p)
5818 warning (0, "literal operator suffixes not preceded by %<_%>"
5819 " are reserved for future standardization");
5820 }
5821
5822 return id;
5823 }
5824 /* Fall through. */
5825
5826 default:
5827 if (optional_p)
5828 return NULL_TREE;
5829 cp_parser_error (parser, "expected unqualified-id");
5830 return error_mark_node;
5831 }
5832 }
5833
5834 /* Parse an (optional) nested-name-specifier.
5835
5836 nested-name-specifier: [C++98]
5837 class-or-namespace-name :: nested-name-specifier [opt]
5838 class-or-namespace-name :: template nested-name-specifier [opt]
5839
5840 nested-name-specifier: [C++0x]
5841 type-name ::
5842 namespace-name ::
5843 nested-name-specifier identifier ::
5844 nested-name-specifier template [opt] simple-template-id ::
5845
5846 PARSER->SCOPE should be set appropriately before this function is
5847 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5848 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5849 in name lookups.
5850
5851 Sets PARSER->SCOPE to the class (TYPE) or namespace
5852 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5853 it unchanged if there is no nested-name-specifier. Returns the new
5854 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5855
5856 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5857 part of a declaration and/or decl-specifier. */
5858
5859 static tree
5860 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5861 bool typename_keyword_p,
5862 bool check_dependency_p,
5863 bool type_p,
5864 bool is_declaration)
5865 {
5866 bool success = false;
5867 cp_token_position start = 0;
5868 cp_token *token;
5869
5870 /* Remember where the nested-name-specifier starts. */
5871 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5872 {
5873 start = cp_lexer_token_position (parser->lexer, false);
5874 push_deferring_access_checks (dk_deferred);
5875 }
5876
5877 while (true)
5878 {
5879 tree new_scope;
5880 tree old_scope;
5881 tree saved_qualifying_scope;
5882 bool template_keyword_p;
5883
5884 /* Spot cases that cannot be the beginning of a
5885 nested-name-specifier. */
5886 token = cp_lexer_peek_token (parser->lexer);
5887
5888 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5889 the already parsed nested-name-specifier. */
5890 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5891 {
5892 /* Grab the nested-name-specifier and continue the loop. */
5893 cp_parser_pre_parsed_nested_name_specifier (parser);
5894 /* If we originally encountered this nested-name-specifier
5895 with IS_DECLARATION set to false, we will not have
5896 resolved TYPENAME_TYPEs, so we must do so here. */
5897 if (is_declaration
5898 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5899 {
5900 new_scope = resolve_typename_type (parser->scope,
5901 /*only_current_p=*/false);
5902 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5903 parser->scope = new_scope;
5904 }
5905 success = true;
5906 continue;
5907 }
5908
5909 /* Spot cases that cannot be the beginning of a
5910 nested-name-specifier. On the second and subsequent times
5911 through the loop, we look for the `template' keyword. */
5912 if (success && token->keyword == RID_TEMPLATE)
5913 ;
5914 /* A template-id can start a nested-name-specifier. */
5915 else if (token->type == CPP_TEMPLATE_ID)
5916 ;
5917 /* DR 743: decltype can be used in a nested-name-specifier. */
5918 else if (token_is_decltype (token))
5919 ;
5920 else
5921 {
5922 /* If the next token is not an identifier, then it is
5923 definitely not a type-name or namespace-name. */
5924 if (token->type != CPP_NAME)
5925 break;
5926 /* If the following token is neither a `<' (to begin a
5927 template-id), nor a `::', then we are not looking at a
5928 nested-name-specifier. */
5929 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5930
5931 if (token->type == CPP_COLON
5932 && parser->colon_corrects_to_scope_p
5933 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5934 {
5935 error_at (token->location,
5936 "found %<:%> in nested-name-specifier, expected %<::%>");
5937 token->type = CPP_SCOPE;
5938 }
5939
5940 if (token->type != CPP_SCOPE
5941 && !cp_parser_nth_token_starts_template_argument_list_p
5942 (parser, 2))
5943 break;
5944 }
5945
5946 /* The nested-name-specifier is optional, so we parse
5947 tentatively. */
5948 cp_parser_parse_tentatively (parser);
5949
5950 /* Look for the optional `template' keyword, if this isn't the
5951 first time through the loop. */
5952 if (success)
5953 template_keyword_p = cp_parser_optional_template_keyword (parser);
5954 else
5955 template_keyword_p = false;
5956
5957 /* Save the old scope since the name lookup we are about to do
5958 might destroy it. */
5959 old_scope = parser->scope;
5960 saved_qualifying_scope = parser->qualifying_scope;
5961 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5962 look up names in "X<T>::I" in order to determine that "Y" is
5963 a template. So, if we have a typename at this point, we make
5964 an effort to look through it. */
5965 if (is_declaration
5966 && !typename_keyword_p
5967 && parser->scope
5968 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5969 parser->scope = resolve_typename_type (parser->scope,
5970 /*only_current_p=*/false);
5971 /* Parse the qualifying entity. */
5972 new_scope
5973 = cp_parser_qualifying_entity (parser,
5974 typename_keyword_p,
5975 template_keyword_p,
5976 check_dependency_p,
5977 type_p,
5978 is_declaration);
5979 /* Look for the `::' token. */
5980 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5981
5982 /* If we found what we wanted, we keep going; otherwise, we're
5983 done. */
5984 if (!cp_parser_parse_definitely (parser))
5985 {
5986 bool error_p = false;
5987
5988 /* Restore the OLD_SCOPE since it was valid before the
5989 failed attempt at finding the last
5990 class-or-namespace-name. */
5991 parser->scope = old_scope;
5992 parser->qualifying_scope = saved_qualifying_scope;
5993
5994 /* If the next token is a decltype, and the one after that is a
5995 `::', then the decltype has failed to resolve to a class or
5996 enumeration type. Give this error even when parsing
5997 tentatively since it can't possibly be valid--and we're going
5998 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5999 won't get another chance.*/
6000 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6001 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6002 == CPP_SCOPE))
6003 {
6004 token = cp_lexer_consume_token (parser->lexer);
6005 error_at (token->location, "decltype evaluates to %qT, "
6006 "which is not a class or enumeration type",
6007 token->u.tree_check_value->value);
6008 parser->scope = error_mark_node;
6009 error_p = true;
6010 /* As below. */
6011 success = true;
6012 cp_lexer_consume_token (parser->lexer);
6013 }
6014
6015 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6016 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6017 {
6018 /* If we have a non-type template-id followed by ::, it can't
6019 possibly be valid. */
6020 token = cp_lexer_peek_token (parser->lexer);
6021 tree tid = token->u.tree_check_value->value;
6022 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6023 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6024 {
6025 tree tmpl = NULL_TREE;
6026 if (is_overloaded_fn (tid))
6027 {
6028 tree fns = get_fns (tid);
6029 if (!OVL_CHAIN (fns))
6030 tmpl = OVL_CURRENT (fns);
6031 error_at (token->location, "function template-id %qD "
6032 "in nested-name-specifier", tid);
6033 }
6034 else
6035 {
6036 /* Variable template. */
6037 tmpl = TREE_OPERAND (tid, 0);
6038 gcc_assert (variable_template_p (tmpl));
6039 error_at (token->location, "variable template-id %qD "
6040 "in nested-name-specifier", tid);
6041 }
6042 if (tmpl)
6043 inform (DECL_SOURCE_LOCATION (tmpl),
6044 "%qD declared here", tmpl);
6045
6046 parser->scope = error_mark_node;
6047 error_p = true;
6048 /* As below. */
6049 success = true;
6050 cp_lexer_consume_token (parser->lexer);
6051 cp_lexer_consume_token (parser->lexer);
6052 }
6053 }
6054
6055 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6056 break;
6057 /* If the next token is an identifier, and the one after
6058 that is a `::', then any valid interpretation would have
6059 found a class-or-namespace-name. */
6060 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6061 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6062 == CPP_SCOPE)
6063 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6064 != CPP_COMPL))
6065 {
6066 token = cp_lexer_consume_token (parser->lexer);
6067 if (!error_p)
6068 {
6069 if (!token->error_reported)
6070 {
6071 tree decl;
6072 tree ambiguous_decls;
6073
6074 decl = cp_parser_lookup_name (parser, token->u.value,
6075 none_type,
6076 /*is_template=*/false,
6077 /*is_namespace=*/false,
6078 /*check_dependency=*/true,
6079 &ambiguous_decls,
6080 token->location);
6081 if (TREE_CODE (decl) == TEMPLATE_DECL)
6082 error_at (token->location,
6083 "%qD used without template parameters",
6084 decl);
6085 else if (ambiguous_decls)
6086 {
6087 // cp_parser_lookup_name has the same diagnostic,
6088 // thus make sure to emit it at most once.
6089 if (cp_parser_uncommitted_to_tentative_parse_p
6090 (parser))
6091 {
6092 error_at (token->location,
6093 "reference to %qD is ambiguous",
6094 token->u.value);
6095 print_candidates (ambiguous_decls);
6096 }
6097 decl = error_mark_node;
6098 }
6099 else
6100 {
6101 if (cxx_dialect != cxx98)
6102 cp_parser_name_lookup_error
6103 (parser, token->u.value, decl, NLE_NOT_CXX98,
6104 token->location);
6105 else
6106 cp_parser_name_lookup_error
6107 (parser, token->u.value, decl, NLE_CXX98,
6108 token->location);
6109 }
6110 }
6111 parser->scope = error_mark_node;
6112 error_p = true;
6113 /* Treat this as a successful nested-name-specifier
6114 due to:
6115
6116 [basic.lookup.qual]
6117
6118 If the name found is not a class-name (clause
6119 _class_) or namespace-name (_namespace.def_), the
6120 program is ill-formed. */
6121 success = true;
6122 }
6123 cp_lexer_consume_token (parser->lexer);
6124 }
6125 break;
6126 }
6127 /* We've found one valid nested-name-specifier. */
6128 success = true;
6129 /* Name lookup always gives us a DECL. */
6130 if (TREE_CODE (new_scope) == TYPE_DECL)
6131 new_scope = TREE_TYPE (new_scope);
6132 /* Uses of "template" must be followed by actual templates. */
6133 if (template_keyword_p
6134 && !(CLASS_TYPE_P (new_scope)
6135 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6136 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6137 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6138 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6139 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6140 == TEMPLATE_ID_EXPR)))
6141 permerror (input_location, TYPE_P (new_scope)
6142 ? G_("%qT is not a template")
6143 : G_("%qD is not a template"),
6144 new_scope);
6145 /* If it is a class scope, try to complete it; we are about to
6146 be looking up names inside the class. */
6147 if (TYPE_P (new_scope)
6148 /* Since checking types for dependency can be expensive,
6149 avoid doing it if the type is already complete. */
6150 && !COMPLETE_TYPE_P (new_scope)
6151 /* Do not try to complete dependent types. */
6152 && !dependent_type_p (new_scope))
6153 {
6154 new_scope = complete_type (new_scope);
6155 /* If it is a typedef to current class, use the current
6156 class instead, as the typedef won't have any names inside
6157 it yet. */
6158 if (!COMPLETE_TYPE_P (new_scope)
6159 && currently_open_class (new_scope))
6160 new_scope = TYPE_MAIN_VARIANT (new_scope);
6161 }
6162 /* Make sure we look in the right scope the next time through
6163 the loop. */
6164 parser->scope = new_scope;
6165 }
6166
6167 /* If parsing tentatively, replace the sequence of tokens that makes
6168 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6169 token. That way, should we re-parse the token stream, we will
6170 not have to repeat the effort required to do the parse, nor will
6171 we issue duplicate error messages. */
6172 if (success && start)
6173 {
6174 cp_token *token;
6175
6176 token = cp_lexer_token_at (parser->lexer, start);
6177 /* Reset the contents of the START token. */
6178 token->type = CPP_NESTED_NAME_SPECIFIER;
6179 /* Retrieve any deferred checks. Do not pop this access checks yet
6180 so the memory will not be reclaimed during token replacing below. */
6181 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6182 token->u.tree_check_value->value = parser->scope;
6183 token->u.tree_check_value->checks = get_deferred_access_checks ();
6184 token->u.tree_check_value->qualifying_scope =
6185 parser->qualifying_scope;
6186 token->keyword = RID_MAX;
6187
6188 /* Purge all subsequent tokens. */
6189 cp_lexer_purge_tokens_after (parser->lexer, start);
6190 }
6191
6192 if (start)
6193 pop_to_parent_deferring_access_checks ();
6194
6195 return success ? parser->scope : NULL_TREE;
6196 }
6197
6198 /* Parse a nested-name-specifier. See
6199 cp_parser_nested_name_specifier_opt for details. This function
6200 behaves identically, except that it will an issue an error if no
6201 nested-name-specifier is present. */
6202
6203 static tree
6204 cp_parser_nested_name_specifier (cp_parser *parser,
6205 bool typename_keyword_p,
6206 bool check_dependency_p,
6207 bool type_p,
6208 bool is_declaration)
6209 {
6210 tree scope;
6211
6212 /* Look for the nested-name-specifier. */
6213 scope = cp_parser_nested_name_specifier_opt (parser,
6214 typename_keyword_p,
6215 check_dependency_p,
6216 type_p,
6217 is_declaration);
6218 /* If it was not present, issue an error message. */
6219 if (!scope)
6220 {
6221 cp_parser_error (parser, "expected nested-name-specifier");
6222 parser->scope = NULL_TREE;
6223 }
6224
6225 return scope;
6226 }
6227
6228 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6229 this is either a class-name or a namespace-name (which corresponds
6230 to the class-or-namespace-name production in the grammar). For
6231 C++0x, it can also be a type-name that refers to an enumeration
6232 type or a simple-template-id.
6233
6234 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6235 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6236 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6237 TYPE_P is TRUE iff the next name should be taken as a class-name,
6238 even the same name is declared to be another entity in the same
6239 scope.
6240
6241 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6242 specified by the class-or-namespace-name. If neither is found the
6243 ERROR_MARK_NODE is returned. */
6244
6245 static tree
6246 cp_parser_qualifying_entity (cp_parser *parser,
6247 bool typename_keyword_p,
6248 bool template_keyword_p,
6249 bool check_dependency_p,
6250 bool type_p,
6251 bool is_declaration)
6252 {
6253 tree saved_scope;
6254 tree saved_qualifying_scope;
6255 tree saved_object_scope;
6256 tree scope;
6257 bool only_class_p;
6258 bool successful_parse_p;
6259
6260 /* DR 743: decltype can appear in a nested-name-specifier. */
6261 if (cp_lexer_next_token_is_decltype (parser->lexer))
6262 {
6263 scope = cp_parser_decltype (parser);
6264 if (TREE_CODE (scope) != ENUMERAL_TYPE
6265 && !MAYBE_CLASS_TYPE_P (scope))
6266 {
6267 cp_parser_simulate_error (parser);
6268 return error_mark_node;
6269 }
6270 if (TYPE_NAME (scope))
6271 scope = TYPE_NAME (scope);
6272 return scope;
6273 }
6274
6275 /* Before we try to parse the class-name, we must save away the
6276 current PARSER->SCOPE since cp_parser_class_name will destroy
6277 it. */
6278 saved_scope = parser->scope;
6279 saved_qualifying_scope = parser->qualifying_scope;
6280 saved_object_scope = parser->object_scope;
6281 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6282 there is no need to look for a namespace-name. */
6283 only_class_p = template_keyword_p
6284 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6285 if (!only_class_p)
6286 cp_parser_parse_tentatively (parser);
6287 scope = cp_parser_class_name (parser,
6288 typename_keyword_p,
6289 template_keyword_p,
6290 type_p ? class_type : none_type,
6291 check_dependency_p,
6292 /*class_head_p=*/false,
6293 is_declaration,
6294 /*enum_ok=*/cxx_dialect > cxx98);
6295 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6296 /* If that didn't work, try for a namespace-name. */
6297 if (!only_class_p && !successful_parse_p)
6298 {
6299 /* Restore the saved scope. */
6300 parser->scope = saved_scope;
6301 parser->qualifying_scope = saved_qualifying_scope;
6302 parser->object_scope = saved_object_scope;
6303 /* If we are not looking at an identifier followed by the scope
6304 resolution operator, then this is not part of a
6305 nested-name-specifier. (Note that this function is only used
6306 to parse the components of a nested-name-specifier.) */
6307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6308 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6309 return error_mark_node;
6310 scope = cp_parser_namespace_name (parser);
6311 }
6312
6313 return scope;
6314 }
6315
6316 /* Return true if we are looking at a compound-literal, false otherwise. */
6317
6318 static bool
6319 cp_parser_compound_literal_p (cp_parser *parser)
6320 {
6321 /* Consume the `('. */
6322 cp_lexer_consume_token (parser->lexer);
6323
6324 cp_lexer_save_tokens (parser->lexer);
6325
6326 /* Skip tokens until the next token is a closing parenthesis.
6327 If we find the closing `)', and the next token is a `{', then
6328 we are looking at a compound-literal. */
6329 bool compound_literal_p
6330 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6331 /*consume_paren=*/true)
6332 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6333
6334 /* Roll back the tokens we skipped. */
6335 cp_lexer_rollback_tokens (parser->lexer);
6336
6337 return compound_literal_p;
6338 }
6339
6340 /* Parse a postfix-expression.
6341
6342 postfix-expression:
6343 primary-expression
6344 postfix-expression [ expression ]
6345 postfix-expression ( expression-list [opt] )
6346 simple-type-specifier ( expression-list [opt] )
6347 typename :: [opt] nested-name-specifier identifier
6348 ( expression-list [opt] )
6349 typename :: [opt] nested-name-specifier template [opt] template-id
6350 ( expression-list [opt] )
6351 postfix-expression . template [opt] id-expression
6352 postfix-expression -> template [opt] id-expression
6353 postfix-expression . pseudo-destructor-name
6354 postfix-expression -> pseudo-destructor-name
6355 postfix-expression ++
6356 postfix-expression --
6357 dynamic_cast < type-id > ( expression )
6358 static_cast < type-id > ( expression )
6359 reinterpret_cast < type-id > ( expression )
6360 const_cast < type-id > ( expression )
6361 typeid ( expression )
6362 typeid ( type-id )
6363
6364 GNU Extension:
6365
6366 postfix-expression:
6367 ( type-id ) { initializer-list , [opt] }
6368
6369 This extension is a GNU version of the C99 compound-literal
6370 construct. (The C99 grammar uses `type-name' instead of `type-id',
6371 but they are essentially the same concept.)
6372
6373 If ADDRESS_P is true, the postfix expression is the operand of the
6374 `&' operator. CAST_P is true if this expression is the target of a
6375 cast.
6376
6377 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6378 class member access expressions [expr.ref].
6379
6380 Returns a representation of the expression. */
6381
6382 static cp_expr
6383 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6384 bool member_access_only_p, bool decltype_p,
6385 cp_id_kind * pidk_return)
6386 {
6387 cp_token *token;
6388 location_t loc;
6389 enum rid keyword;
6390 cp_id_kind idk = CP_ID_KIND_NONE;
6391 cp_expr postfix_expression = NULL_TREE;
6392 bool is_member_access = false;
6393 int saved_in_statement = -1;
6394
6395 /* Peek at the next token. */
6396 token = cp_lexer_peek_token (parser->lexer);
6397 loc = token->location;
6398 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6399
6400 /* Some of the productions are determined by keywords. */
6401 keyword = token->keyword;
6402 switch (keyword)
6403 {
6404 case RID_DYNCAST:
6405 case RID_STATCAST:
6406 case RID_REINTCAST:
6407 case RID_CONSTCAST:
6408 {
6409 tree type;
6410 cp_expr expression;
6411 const char *saved_message;
6412 bool saved_in_type_id_in_expr_p;
6413
6414 /* All of these can be handled in the same way from the point
6415 of view of parsing. Begin by consuming the token
6416 identifying the cast. */
6417 cp_lexer_consume_token (parser->lexer);
6418
6419 /* New types cannot be defined in the cast. */
6420 saved_message = parser->type_definition_forbidden_message;
6421 parser->type_definition_forbidden_message
6422 = G_("types may not be defined in casts");
6423
6424 /* Look for the opening `<'. */
6425 cp_parser_require (parser, CPP_LESS, RT_LESS);
6426 /* Parse the type to which we are casting. */
6427 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6428 parser->in_type_id_in_expr_p = true;
6429 type = cp_parser_type_id (parser);
6430 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6431 /* Look for the closing `>'. */
6432 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6433 /* Restore the old message. */
6434 parser->type_definition_forbidden_message = saved_message;
6435
6436 bool saved_greater_than_is_operator_p
6437 = parser->greater_than_is_operator_p;
6438 parser->greater_than_is_operator_p = true;
6439
6440 /* And the expression which is being cast. */
6441 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6442 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6443 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6444 RT_CLOSE_PAREN);
6445 location_t end_loc = close_paren ?
6446 close_paren->location : UNKNOWN_LOCATION;
6447
6448 parser->greater_than_is_operator_p
6449 = saved_greater_than_is_operator_p;
6450
6451 /* Only type conversions to integral or enumeration types
6452 can be used in constant-expressions. */
6453 if (!cast_valid_in_integral_constant_expression_p (type)
6454 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6455 {
6456 postfix_expression = error_mark_node;
6457 break;
6458 }
6459
6460 switch (keyword)
6461 {
6462 case RID_DYNCAST:
6463 postfix_expression
6464 = build_dynamic_cast (type, expression, tf_warning_or_error);
6465 break;
6466 case RID_STATCAST:
6467 postfix_expression
6468 = build_static_cast (type, expression, tf_warning_or_error);
6469 break;
6470 case RID_REINTCAST:
6471 postfix_expression
6472 = build_reinterpret_cast (type, expression,
6473 tf_warning_or_error);
6474 break;
6475 case RID_CONSTCAST:
6476 postfix_expression
6477 = build_const_cast (type, expression, tf_warning_or_error);
6478 break;
6479 default:
6480 gcc_unreachable ();
6481 }
6482
6483 /* Construct a location e.g. :
6484 reinterpret_cast <int *> (expr)
6485 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6486 ranging from the start of the "*_cast" token to the final closing
6487 paren, with the caret at the start. */
6488 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6489 postfix_expression.set_location (cp_cast_loc);
6490 }
6491 break;
6492
6493 case RID_TYPEID:
6494 {
6495 tree type;
6496 const char *saved_message;
6497 bool saved_in_type_id_in_expr_p;
6498
6499 /* Consume the `typeid' token. */
6500 cp_lexer_consume_token (parser->lexer);
6501 /* Look for the `(' token. */
6502 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6503 /* Types cannot be defined in a `typeid' expression. */
6504 saved_message = parser->type_definition_forbidden_message;
6505 parser->type_definition_forbidden_message
6506 = G_("types may not be defined in a %<typeid%> expression");
6507 /* We can't be sure yet whether we're looking at a type-id or an
6508 expression. */
6509 cp_parser_parse_tentatively (parser);
6510 /* Try a type-id first. */
6511 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6512 parser->in_type_id_in_expr_p = true;
6513 type = cp_parser_type_id (parser);
6514 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6515 /* Look for the `)' token. Otherwise, we can't be sure that
6516 we're not looking at an expression: consider `typeid (int
6517 (3))', for example. */
6518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6519 /* If all went well, simply lookup the type-id. */
6520 if (cp_parser_parse_definitely (parser))
6521 postfix_expression = get_typeid (type, tf_warning_or_error);
6522 /* Otherwise, fall back to the expression variant. */
6523 else
6524 {
6525 tree expression;
6526
6527 /* Look for an expression. */
6528 expression = cp_parser_expression (parser, & idk);
6529 /* Compute its typeid. */
6530 postfix_expression = build_typeid (expression, tf_warning_or_error);
6531 /* Look for the `)' token. */
6532 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6533 }
6534 /* Restore the saved message. */
6535 parser->type_definition_forbidden_message = saved_message;
6536 /* `typeid' may not appear in an integral constant expression. */
6537 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6538 postfix_expression = error_mark_node;
6539 }
6540 break;
6541
6542 case RID_TYPENAME:
6543 {
6544 tree type;
6545 /* The syntax permitted here is the same permitted for an
6546 elaborated-type-specifier. */
6547 ++parser->prevent_constrained_type_specifiers;
6548 type = cp_parser_elaborated_type_specifier (parser,
6549 /*is_friend=*/false,
6550 /*is_declaration=*/false);
6551 --parser->prevent_constrained_type_specifiers;
6552 postfix_expression = cp_parser_functional_cast (parser, type);
6553 }
6554 break;
6555
6556 case RID_CILK_SPAWN:
6557 {
6558 location_t cilk_spawn_loc
6559 = cp_lexer_peek_token (parser->lexer)->location;
6560 cp_lexer_consume_token (parser->lexer);
6561 token = cp_lexer_peek_token (parser->lexer);
6562 if (token->type == CPP_SEMICOLON)
6563 {
6564 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6565 "an expression");
6566 postfix_expression = error_mark_node;
6567 break;
6568 }
6569 else if (!current_function_decl)
6570 {
6571 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6572 "inside a function");
6573 postfix_expression = error_mark_node;
6574 break;
6575 }
6576 else
6577 {
6578 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6579 saved_in_statement = parser->in_statement;
6580 parser->in_statement |= IN_CILK_SPAWN;
6581 }
6582 cfun->calls_cilk_spawn = 1;
6583 postfix_expression =
6584 cp_parser_postfix_expression (parser, false, false,
6585 false, false, &idk);
6586 if (!flag_cilkplus)
6587 {
6588 error_at (token->location, "-fcilkplus must be enabled to use"
6589 " %<_Cilk_spawn%>");
6590 cfun->calls_cilk_spawn = 0;
6591 }
6592 else if (saved_in_statement & IN_CILK_SPAWN)
6593 {
6594 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6595 "are not permitted");
6596 postfix_expression = error_mark_node;
6597 cfun->calls_cilk_spawn = 0;
6598 }
6599 else
6600 {
6601 location_t loc = postfix_expression.get_location ();
6602 postfix_expression = build_cilk_spawn (token->location,
6603 postfix_expression);
6604 /* Build a location of the form:
6605 _Cilk_spawn expr
6606 ~~~~~~~~~~~~^~~~
6607 with caret at the expr, ranging from the start of the
6608 _Cilk_spawn token to the end of the expression. */
6609 location_t combined_loc =
6610 make_location (loc, cilk_spawn_loc, get_finish (loc));
6611 postfix_expression.set_location (combined_loc);
6612 if (postfix_expression != error_mark_node)
6613 SET_EXPR_LOCATION (postfix_expression, input_location);
6614 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6615 }
6616 break;
6617 }
6618
6619 case RID_ADDRESSOF:
6620 case RID_BUILTIN_SHUFFLE:
6621 case RID_BUILTIN_LAUNDER:
6622 {
6623 vec<tree, va_gc> *vec;
6624 unsigned int i;
6625 tree p;
6626
6627 cp_lexer_consume_token (parser->lexer);
6628 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6629 /*cast_p=*/false, /*allow_expansion_p=*/true,
6630 /*non_constant_p=*/NULL);
6631 if (vec == NULL)
6632 {
6633 postfix_expression = error_mark_node;
6634 break;
6635 }
6636
6637 FOR_EACH_VEC_ELT (*vec, i, p)
6638 mark_exp_read (p);
6639
6640 switch (keyword)
6641 {
6642 case RID_ADDRESSOF:
6643 if (vec->length () == 1)
6644 postfix_expression
6645 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6646 else
6647 {
6648 error_at (loc, "wrong number of arguments to "
6649 "%<__builtin_addressof%>");
6650 postfix_expression = error_mark_node;
6651 }
6652 break;
6653
6654 case RID_BUILTIN_LAUNDER:
6655 if (vec->length () == 1)
6656 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6657 tf_warning_or_error);
6658 else
6659 {
6660 error_at (loc, "wrong number of arguments to "
6661 "%<__builtin_launder%>");
6662 postfix_expression = error_mark_node;
6663 }
6664 break;
6665
6666 case RID_BUILTIN_SHUFFLE:
6667 if (vec->length () == 2)
6668 postfix_expression
6669 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6670 (*vec)[1], tf_warning_or_error);
6671 else if (vec->length () == 3)
6672 postfix_expression
6673 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6674 (*vec)[2], tf_warning_or_error);
6675 else
6676 {
6677 error_at (loc, "wrong number of arguments to "
6678 "%<__builtin_shuffle%>");
6679 postfix_expression = error_mark_node;
6680 }
6681 break;
6682
6683 default:
6684 gcc_unreachable ();
6685 }
6686 break;
6687 }
6688
6689 default:
6690 {
6691 tree type;
6692
6693 /* If the next thing is a simple-type-specifier, we may be
6694 looking at a functional cast. We could also be looking at
6695 an id-expression. So, we try the functional cast, and if
6696 that doesn't work we fall back to the primary-expression. */
6697 cp_parser_parse_tentatively (parser);
6698 /* Look for the simple-type-specifier. */
6699 ++parser->prevent_constrained_type_specifiers;
6700 type = cp_parser_simple_type_specifier (parser,
6701 /*decl_specs=*/NULL,
6702 CP_PARSER_FLAGS_NONE);
6703 --parser->prevent_constrained_type_specifiers;
6704 /* Parse the cast itself. */
6705 if (!cp_parser_error_occurred (parser))
6706 postfix_expression
6707 = cp_parser_functional_cast (parser, type);
6708 /* If that worked, we're done. */
6709 if (cp_parser_parse_definitely (parser))
6710 break;
6711
6712 /* If the functional-cast didn't work out, try a
6713 compound-literal. */
6714 if (cp_parser_allow_gnu_extensions_p (parser)
6715 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6716 {
6717 cp_expr initializer = NULL_TREE;
6718
6719 cp_parser_parse_tentatively (parser);
6720
6721 /* Avoid calling cp_parser_type_id pointlessly, see comment
6722 in cp_parser_cast_expression about c++/29234. */
6723 if (!cp_parser_compound_literal_p (parser))
6724 cp_parser_simulate_error (parser);
6725 else
6726 {
6727 /* Parse the type. */
6728 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6729 parser->in_type_id_in_expr_p = true;
6730 type = cp_parser_type_id (parser);
6731 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6732 /* Look for the `)'. */
6733 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6734 }
6735
6736 /* If things aren't going well, there's no need to
6737 keep going. */
6738 if (!cp_parser_error_occurred (parser))
6739 {
6740 bool non_constant_p;
6741 /* Parse the brace-enclosed initializer list. */
6742 initializer = cp_parser_braced_list (parser,
6743 &non_constant_p);
6744 }
6745 /* If that worked, we're definitely looking at a
6746 compound-literal expression. */
6747 if (cp_parser_parse_definitely (parser))
6748 {
6749 /* Warn the user that a compound literal is not
6750 allowed in standard C++. */
6751 pedwarn (input_location, OPT_Wpedantic,
6752 "ISO C++ forbids compound-literals");
6753 /* For simplicity, we disallow compound literals in
6754 constant-expressions. We could
6755 allow compound literals of integer type, whose
6756 initializer was a constant, in constant
6757 expressions. Permitting that usage, as a further
6758 extension, would not change the meaning of any
6759 currently accepted programs. (Of course, as
6760 compound literals are not part of ISO C++, the
6761 standard has nothing to say.) */
6762 if (cp_parser_non_integral_constant_expression (parser,
6763 NIC_NCC))
6764 {
6765 postfix_expression = error_mark_node;
6766 break;
6767 }
6768 /* Form the representation of the compound-literal. */
6769 postfix_expression
6770 = finish_compound_literal (type, initializer,
6771 tf_warning_or_error);
6772 postfix_expression.set_location (initializer.get_location ());
6773 break;
6774 }
6775 }
6776
6777 /* It must be a primary-expression. */
6778 postfix_expression
6779 = cp_parser_primary_expression (parser, address_p, cast_p,
6780 /*template_arg_p=*/false,
6781 decltype_p,
6782 &idk);
6783 }
6784 break;
6785 }
6786
6787 /* Note that we don't need to worry about calling build_cplus_new on a
6788 class-valued CALL_EXPR in decltype when it isn't the end of the
6789 postfix-expression; unary_complex_lvalue will take care of that for
6790 all these cases. */
6791
6792 /* Keep looping until the postfix-expression is complete. */
6793 while (true)
6794 {
6795 if (idk == CP_ID_KIND_UNQUALIFIED
6796 && identifier_p (postfix_expression)
6797 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6798 /* It is not a Koenig lookup function call. */
6799 postfix_expression
6800 = unqualified_name_lookup_error (postfix_expression);
6801
6802 /* Peek at the next token. */
6803 token = cp_lexer_peek_token (parser->lexer);
6804
6805 switch (token->type)
6806 {
6807 case CPP_OPEN_SQUARE:
6808 if (cp_next_tokens_can_be_std_attribute_p (parser))
6809 {
6810 cp_parser_error (parser,
6811 "two consecutive %<[%> shall "
6812 "only introduce an attribute");
6813 return error_mark_node;
6814 }
6815 postfix_expression
6816 = cp_parser_postfix_open_square_expression (parser,
6817 postfix_expression,
6818 false,
6819 decltype_p);
6820 postfix_expression.set_range (start_loc,
6821 postfix_expression.get_location ());
6822
6823 idk = CP_ID_KIND_NONE;
6824 is_member_access = false;
6825 break;
6826
6827 case CPP_OPEN_PAREN:
6828 /* postfix-expression ( expression-list [opt] ) */
6829 {
6830 bool koenig_p;
6831 bool is_builtin_constant_p;
6832 bool saved_integral_constant_expression_p = false;
6833 bool saved_non_integral_constant_expression_p = false;
6834 tsubst_flags_t complain = complain_flags (decltype_p);
6835 vec<tree, va_gc> *args;
6836 location_t close_paren_loc = UNKNOWN_LOCATION;
6837
6838 is_member_access = false;
6839
6840 is_builtin_constant_p
6841 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6842 if (is_builtin_constant_p)
6843 {
6844 /* The whole point of __builtin_constant_p is to allow
6845 non-constant expressions to appear as arguments. */
6846 saved_integral_constant_expression_p
6847 = parser->integral_constant_expression_p;
6848 saved_non_integral_constant_expression_p
6849 = parser->non_integral_constant_expression_p;
6850 parser->integral_constant_expression_p = false;
6851 }
6852 args = (cp_parser_parenthesized_expression_list
6853 (parser, non_attr,
6854 /*cast_p=*/false, /*allow_expansion_p=*/true,
6855 /*non_constant_p=*/NULL,
6856 /*close_paren_loc=*/&close_paren_loc));
6857 if (is_builtin_constant_p)
6858 {
6859 parser->integral_constant_expression_p
6860 = saved_integral_constant_expression_p;
6861 parser->non_integral_constant_expression_p
6862 = saved_non_integral_constant_expression_p;
6863 }
6864
6865 if (args == NULL)
6866 {
6867 postfix_expression = error_mark_node;
6868 break;
6869 }
6870
6871 /* Function calls are not permitted in
6872 constant-expressions. */
6873 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6874 && cp_parser_non_integral_constant_expression (parser,
6875 NIC_FUNC_CALL))
6876 {
6877 postfix_expression = error_mark_node;
6878 release_tree_vector (args);
6879 break;
6880 }
6881
6882 koenig_p = false;
6883 if (idk == CP_ID_KIND_UNQUALIFIED
6884 || idk == CP_ID_KIND_TEMPLATE_ID)
6885 {
6886 if (identifier_p (postfix_expression))
6887 {
6888 if (!args->is_empty ())
6889 {
6890 koenig_p = true;
6891 if (!any_type_dependent_arguments_p (args))
6892 postfix_expression
6893 = perform_koenig_lookup (postfix_expression, args,
6894 complain);
6895 }
6896 else
6897 postfix_expression
6898 = unqualified_fn_lookup_error (postfix_expression);
6899 }
6900 /* We do not perform argument-dependent lookup if
6901 normal lookup finds a non-function, in accordance
6902 with the expected resolution of DR 218. */
6903 else if (!args->is_empty ()
6904 && is_overloaded_fn (postfix_expression))
6905 {
6906 tree fn = get_first_fn (postfix_expression);
6907 fn = STRIP_TEMPLATE (fn);
6908
6909 /* Do not do argument dependent lookup if regular
6910 lookup finds a member function or a block-scope
6911 function declaration. [basic.lookup.argdep]/3 */
6912 if (!DECL_FUNCTION_MEMBER_P (fn)
6913 && !DECL_LOCAL_FUNCTION_P (fn))
6914 {
6915 koenig_p = true;
6916 if (!any_type_dependent_arguments_p (args))
6917 postfix_expression
6918 = perform_koenig_lookup (postfix_expression, args,
6919 complain);
6920 }
6921 }
6922 }
6923
6924 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6925 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6926 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6927 && vec_safe_length (args) == 3)
6928 {
6929 tree arg0 = (*args)[0];
6930 tree arg1 = (*args)[1];
6931 tree arg2 = (*args)[2];
6932 int literal_mask = ((!!integer_zerop (arg1) << 1)
6933 | (!!integer_zerop (arg2) << 2));
6934 if (TREE_CODE (arg2) == CONST_DECL)
6935 arg2 = DECL_INITIAL (arg2);
6936 warn_for_memset (input_location, arg0, arg2, literal_mask);
6937 }
6938
6939 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6940 && warn_restrict)
6941 {
6942 unsigned i;
6943 tree arg;
6944 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6945 TREE_VISITED (arg) = 0;
6946
6947 unsigned param_pos = 0;
6948 for (tree decl = DECL_ARGUMENTS (postfix_expression);
6949 decl != NULL_TREE;
6950 decl = DECL_CHAIN (decl), param_pos++)
6951 {
6952 tree type = TREE_TYPE (decl);
6953 if (POINTER_TYPE_P (type) && TYPE_RESTRICT (type)
6954 && !TYPE_READONLY (TREE_TYPE (type)))
6955 warn_for_restrict (param_pos, args);
6956 }
6957
6958 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6959 TREE_VISITED (arg) = 0;
6960 }
6961
6962 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6963 {
6964 tree instance = TREE_OPERAND (postfix_expression, 0);
6965 tree fn = TREE_OPERAND (postfix_expression, 1);
6966
6967 if (processing_template_decl
6968 && (type_dependent_object_expression_p (instance)
6969 || (!BASELINK_P (fn)
6970 && TREE_CODE (fn) != FIELD_DECL)
6971 || type_dependent_expression_p (fn)
6972 || any_type_dependent_arguments_p (args)))
6973 {
6974 postfix_expression
6975 = build_nt_call_vec (postfix_expression, args);
6976 release_tree_vector (args);
6977 break;
6978 }
6979
6980 if (BASELINK_P (fn))
6981 {
6982 postfix_expression
6983 = (build_new_method_call
6984 (instance, fn, &args, NULL_TREE,
6985 (idk == CP_ID_KIND_QUALIFIED
6986 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6987 : LOOKUP_NORMAL),
6988 /*fn_p=*/NULL,
6989 complain));
6990 }
6991 else
6992 postfix_expression
6993 = finish_call_expr (postfix_expression, &args,
6994 /*disallow_virtual=*/false,
6995 /*koenig_p=*/false,
6996 complain);
6997 }
6998 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6999 || TREE_CODE (postfix_expression) == MEMBER_REF
7000 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7001 postfix_expression = (build_offset_ref_call_from_tree
7002 (postfix_expression, &args,
7003 complain));
7004 else if (idk == CP_ID_KIND_QUALIFIED)
7005 /* A call to a static class member, or a namespace-scope
7006 function. */
7007 postfix_expression
7008 = finish_call_expr (postfix_expression, &args,
7009 /*disallow_virtual=*/true,
7010 koenig_p,
7011 complain);
7012 else
7013 /* All other function calls. */
7014 postfix_expression
7015 = finish_call_expr (postfix_expression, &args,
7016 /*disallow_virtual=*/false,
7017 koenig_p,
7018 complain);
7019
7020 if (close_paren_loc != UNKNOWN_LOCATION)
7021 {
7022 location_t combined_loc = make_location (token->location,
7023 start_loc,
7024 close_paren_loc);
7025 postfix_expression.set_location (combined_loc);
7026 }
7027
7028 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7029 idk = CP_ID_KIND_NONE;
7030
7031 release_tree_vector (args);
7032 }
7033 break;
7034
7035 case CPP_DOT:
7036 case CPP_DEREF:
7037 /* postfix-expression . template [opt] id-expression
7038 postfix-expression . pseudo-destructor-name
7039 postfix-expression -> template [opt] id-expression
7040 postfix-expression -> pseudo-destructor-name */
7041
7042 /* Consume the `.' or `->' operator. */
7043 cp_lexer_consume_token (parser->lexer);
7044
7045 postfix_expression
7046 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7047 postfix_expression,
7048 false, &idk, loc);
7049
7050 is_member_access = true;
7051 break;
7052
7053 case CPP_PLUS_PLUS:
7054 /* postfix-expression ++ */
7055 /* Consume the `++' token. */
7056 cp_lexer_consume_token (parser->lexer);
7057 /* Generate a representation for the complete expression. */
7058 postfix_expression
7059 = finish_increment_expr (postfix_expression,
7060 POSTINCREMENT_EXPR);
7061 /* Increments may not appear in constant-expressions. */
7062 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7063 postfix_expression = error_mark_node;
7064 idk = CP_ID_KIND_NONE;
7065 is_member_access = false;
7066 break;
7067
7068 case CPP_MINUS_MINUS:
7069 /* postfix-expression -- */
7070 /* Consume the `--' token. */
7071 cp_lexer_consume_token (parser->lexer);
7072 /* Generate a representation for the complete expression. */
7073 postfix_expression
7074 = finish_increment_expr (postfix_expression,
7075 POSTDECREMENT_EXPR);
7076 /* Decrements may not appear in constant-expressions. */
7077 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7078 postfix_expression = error_mark_node;
7079 idk = CP_ID_KIND_NONE;
7080 is_member_access = false;
7081 break;
7082
7083 default:
7084 if (pidk_return != NULL)
7085 * pidk_return = idk;
7086 if (member_access_only_p)
7087 return is_member_access
7088 ? postfix_expression
7089 : cp_expr (error_mark_node);
7090 else
7091 return postfix_expression;
7092 }
7093 }
7094
7095 /* We should never get here. */
7096 gcc_unreachable ();
7097 return error_mark_node;
7098 }
7099
7100 /* This function parses Cilk Plus array notations. If a normal array expr. is
7101 parsed then the array index is passed back to the caller through *INIT_INDEX
7102 and the function returns a NULL_TREE. If array notation expr. is parsed,
7103 then *INIT_INDEX is ignored by the caller and the function returns
7104 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7105 error_mark_node. */
7106
7107 static tree
7108 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7109 tree array_value)
7110 {
7111 cp_token *token = NULL;
7112 tree length_index, stride = NULL_TREE, value_tree, array_type;
7113 if (!array_value || array_value == error_mark_node)
7114 {
7115 cp_parser_skip_to_end_of_statement (parser);
7116 return error_mark_node;
7117 }
7118
7119 array_type = TREE_TYPE (array_value);
7120
7121 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7122 parser->colon_corrects_to_scope_p = false;
7123 token = cp_lexer_peek_token (parser->lexer);
7124
7125 if (!token)
7126 {
7127 cp_parser_error (parser, "expected %<:%> or numeral");
7128 return error_mark_node;
7129 }
7130 else if (token->type == CPP_COLON)
7131 {
7132 /* Consume the ':'. */
7133 cp_lexer_consume_token (parser->lexer);
7134
7135 /* If we are here, then we have a case like this A[:]. */
7136 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7137 {
7138 cp_parser_error (parser, "expected %<]%>");
7139 cp_parser_skip_to_end_of_statement (parser);
7140 return error_mark_node;
7141 }
7142 *init_index = NULL_TREE;
7143 stride = NULL_TREE;
7144 length_index = NULL_TREE;
7145 }
7146 else
7147 {
7148 /* If we are here, then there are three valid possibilities:
7149 1. ARRAY [ EXP ]
7150 2. ARRAY [ EXP : EXP ]
7151 3. ARRAY [ EXP : EXP : EXP ] */
7152
7153 *init_index = cp_parser_expression (parser);
7154 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7155 {
7156 /* This indicates that we have a normal array expression. */
7157 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7158 return NULL_TREE;
7159 }
7160
7161 /* Consume the ':'. */
7162 cp_lexer_consume_token (parser->lexer);
7163 length_index = cp_parser_expression (parser);
7164 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7165 {
7166 cp_lexer_consume_token (parser->lexer);
7167 stride = cp_parser_expression (parser);
7168 }
7169 }
7170 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7171
7172 if (*init_index == error_mark_node || length_index == error_mark_node
7173 || stride == error_mark_node || array_type == error_mark_node)
7174 {
7175 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7176 cp_lexer_consume_token (parser->lexer);
7177 return error_mark_node;
7178 }
7179 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7180
7181 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7182 length_index, stride, array_type);
7183 return value_tree;
7184 }
7185
7186 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7187 by cp_parser_builtin_offsetof. We're looking for
7188
7189 postfix-expression [ expression ]
7190 postfix-expression [ braced-init-list ] (C++11)
7191
7192 FOR_OFFSETOF is set if we're being called in that context, which
7193 changes how we deal with integer constant expressions. */
7194
7195 static tree
7196 cp_parser_postfix_open_square_expression (cp_parser *parser,
7197 tree postfix_expression,
7198 bool for_offsetof,
7199 bool decltype_p)
7200 {
7201 tree index = NULL_TREE;
7202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7203 bool saved_greater_than_is_operator_p;
7204
7205 /* Consume the `[' token. */
7206 cp_lexer_consume_token (parser->lexer);
7207
7208 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7209 parser->greater_than_is_operator_p = true;
7210
7211 /* Parse the index expression. */
7212 /* ??? For offsetof, there is a question of what to allow here. If
7213 offsetof is not being used in an integral constant expression context,
7214 then we *could* get the right answer by computing the value at runtime.
7215 If we are in an integral constant expression context, then we might
7216 could accept any constant expression; hard to say without analysis.
7217 Rather than open the barn door too wide right away, allow only integer
7218 constant expressions here. */
7219 if (for_offsetof)
7220 index = cp_parser_constant_expression (parser);
7221 else
7222 {
7223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7224 {
7225 bool expr_nonconst_p;
7226 cp_lexer_set_source_position (parser->lexer);
7227 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7228 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7229 if (flag_cilkplus
7230 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7231 {
7232 error_at (cp_lexer_peek_token (parser->lexer)->location,
7233 "braced list index is not allowed with array "
7234 "notation");
7235 cp_parser_skip_to_end_of_statement (parser);
7236 return error_mark_node;
7237 }
7238 }
7239 else if (flag_cilkplus)
7240 {
7241 /* Here are have these two options:
7242 ARRAY[EXP : EXP] - Array notation expr with default
7243 stride of 1.
7244 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7245 stride. */
7246 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7247 postfix_expression);
7248 if (an_exp)
7249 return an_exp;
7250 }
7251 else
7252 index = cp_parser_expression (parser);
7253 }
7254
7255 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7256
7257 /* Look for the closing `]'. */
7258 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7259
7260 /* Build the ARRAY_REF. */
7261 postfix_expression = grok_array_decl (loc, postfix_expression,
7262 index, decltype_p);
7263
7264 /* When not doing offsetof, array references are not permitted in
7265 constant-expressions. */
7266 if (!for_offsetof
7267 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7268 postfix_expression = error_mark_node;
7269
7270 return postfix_expression;
7271 }
7272
7273 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7274 by cp_parser_builtin_offsetof. We're looking for
7275
7276 postfix-expression . template [opt] id-expression
7277 postfix-expression . pseudo-destructor-name
7278 postfix-expression -> template [opt] id-expression
7279 postfix-expression -> pseudo-destructor-name
7280
7281 FOR_OFFSETOF is set if we're being called in that context. That sorta
7282 limits what of the above we'll actually accept, but nevermind.
7283 TOKEN_TYPE is the "." or "->" token, which will already have been
7284 removed from the stream. */
7285
7286 static tree
7287 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7288 enum cpp_ttype token_type,
7289 cp_expr postfix_expression,
7290 bool for_offsetof, cp_id_kind *idk,
7291 location_t location)
7292 {
7293 tree name;
7294 bool dependent_p;
7295 bool pseudo_destructor_p;
7296 tree scope = NULL_TREE;
7297 location_t start_loc = postfix_expression.get_start ();
7298
7299 /* If this is a `->' operator, dereference the pointer. */
7300 if (token_type == CPP_DEREF)
7301 postfix_expression = build_x_arrow (location, postfix_expression,
7302 tf_warning_or_error);
7303 /* Check to see whether or not the expression is type-dependent and
7304 not the current instantiation. */
7305 dependent_p = type_dependent_object_expression_p (postfix_expression);
7306 /* The identifier following the `->' or `.' is not qualified. */
7307 parser->scope = NULL_TREE;
7308 parser->qualifying_scope = NULL_TREE;
7309 parser->object_scope = NULL_TREE;
7310 *idk = CP_ID_KIND_NONE;
7311
7312 /* Enter the scope corresponding to the type of the object
7313 given by the POSTFIX_EXPRESSION. */
7314 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7315 {
7316 scope = TREE_TYPE (postfix_expression);
7317 /* According to the standard, no expression should ever have
7318 reference type. Unfortunately, we do not currently match
7319 the standard in this respect in that our internal representation
7320 of an expression may have reference type even when the standard
7321 says it does not. Therefore, we have to manually obtain the
7322 underlying type here. */
7323 scope = non_reference (scope);
7324 /* The type of the POSTFIX_EXPRESSION must be complete. */
7325 /* Unlike the object expression in other contexts, *this is not
7326 required to be of complete type for purposes of class member
7327 access (5.2.5) outside the member function body. */
7328 if (postfix_expression != current_class_ref
7329 && !(processing_template_decl
7330 && current_class_type
7331 && (same_type_ignoring_top_level_qualifiers_p
7332 (scope, current_class_type))))
7333 scope = complete_type_or_else (scope, postfix_expression);
7334 /* Let the name lookup machinery know that we are processing a
7335 class member access expression. */
7336 parser->context->object_type = scope;
7337 /* If something went wrong, we want to be able to discern that case,
7338 as opposed to the case where there was no SCOPE due to the type
7339 of expression being dependent. */
7340 if (!scope)
7341 scope = error_mark_node;
7342 /* If the SCOPE was erroneous, make the various semantic analysis
7343 functions exit quickly -- and without issuing additional error
7344 messages. */
7345 if (scope == error_mark_node)
7346 postfix_expression = error_mark_node;
7347 }
7348 else
7349 /* Tell cp_parser_lookup_name that there was an object, even though it's
7350 type-dependent. */
7351 parser->context->object_type = unknown_type_node;
7352
7353 /* Assume this expression is not a pseudo-destructor access. */
7354 pseudo_destructor_p = false;
7355
7356 /* If the SCOPE is a scalar type, then, if this is a valid program,
7357 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7358 is type dependent, it can be pseudo-destructor-name or something else.
7359 Try to parse it as pseudo-destructor-name first. */
7360 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7361 {
7362 tree s;
7363 tree type;
7364
7365 cp_parser_parse_tentatively (parser);
7366 /* Parse the pseudo-destructor-name. */
7367 s = NULL_TREE;
7368 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7369 &s, &type);
7370 if (dependent_p
7371 && (cp_parser_error_occurred (parser)
7372 || !SCALAR_TYPE_P (type)))
7373 cp_parser_abort_tentative_parse (parser);
7374 else if (cp_parser_parse_definitely (parser))
7375 {
7376 pseudo_destructor_p = true;
7377 postfix_expression
7378 = finish_pseudo_destructor_expr (postfix_expression,
7379 s, type, location);
7380 }
7381 }
7382
7383 if (!pseudo_destructor_p)
7384 {
7385 /* If the SCOPE is not a scalar type, we are looking at an
7386 ordinary class member access expression, rather than a
7387 pseudo-destructor-name. */
7388 bool template_p;
7389 cp_token *token = cp_lexer_peek_token (parser->lexer);
7390 /* Parse the id-expression. */
7391 name = (cp_parser_id_expression
7392 (parser,
7393 cp_parser_optional_template_keyword (parser),
7394 /*check_dependency_p=*/true,
7395 &template_p,
7396 /*declarator_p=*/false,
7397 /*optional_p=*/false));
7398 /* In general, build a SCOPE_REF if the member name is qualified.
7399 However, if the name was not dependent and has already been
7400 resolved; there is no need to build the SCOPE_REF. For example;
7401
7402 struct X { void f(); };
7403 template <typename T> void f(T* t) { t->X::f(); }
7404
7405 Even though "t" is dependent, "X::f" is not and has been resolved
7406 to a BASELINK; there is no need to include scope information. */
7407
7408 /* But we do need to remember that there was an explicit scope for
7409 virtual function calls. */
7410 if (parser->scope)
7411 *idk = CP_ID_KIND_QUALIFIED;
7412
7413 /* If the name is a template-id that names a type, we will get a
7414 TYPE_DECL here. That is invalid code. */
7415 if (TREE_CODE (name) == TYPE_DECL)
7416 {
7417 error_at (token->location, "invalid use of %qD", name);
7418 postfix_expression = error_mark_node;
7419 }
7420 else
7421 {
7422 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7423 {
7424 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7425 {
7426 error_at (token->location, "%<%D::%D%> is not a class member",
7427 parser->scope, name);
7428 postfix_expression = error_mark_node;
7429 }
7430 else
7431 name = build_qualified_name (/*type=*/NULL_TREE,
7432 parser->scope,
7433 name,
7434 template_p);
7435 parser->scope = NULL_TREE;
7436 parser->qualifying_scope = NULL_TREE;
7437 parser->object_scope = NULL_TREE;
7438 }
7439 if (parser->scope && name && BASELINK_P (name))
7440 adjust_result_of_qualified_name_lookup
7441 (name, parser->scope, scope);
7442 postfix_expression
7443 = finish_class_member_access_expr (postfix_expression, name,
7444 template_p,
7445 tf_warning_or_error);
7446 /* Build a location e.g.:
7447 ptr->access_expr
7448 ~~~^~~~~~~~~~~~~
7449 where the caret is at the deref token, ranging from
7450 the start of postfix_expression to the end of the access expr. */
7451 location_t end_loc
7452 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7453 location_t combined_loc
7454 = make_location (input_location, start_loc, end_loc);
7455 protected_set_expr_location (postfix_expression, combined_loc);
7456 }
7457 }
7458
7459 /* We no longer need to look up names in the scope of the object on
7460 the left-hand side of the `.' or `->' operator. */
7461 parser->context->object_type = NULL_TREE;
7462
7463 /* Outside of offsetof, these operators may not appear in
7464 constant-expressions. */
7465 if (!for_offsetof
7466 && (cp_parser_non_integral_constant_expression
7467 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7468 postfix_expression = error_mark_node;
7469
7470 return postfix_expression;
7471 }
7472
7473 /* Parse a parenthesized expression-list.
7474
7475 expression-list:
7476 assignment-expression
7477 expression-list, assignment-expression
7478
7479 attribute-list:
7480 expression-list
7481 identifier
7482 identifier, expression-list
7483
7484 CAST_P is true if this expression is the target of a cast.
7485
7486 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7487 argument pack.
7488
7489 Returns a vector of trees. Each element is a representation of an
7490 assignment-expression. NULL is returned if the ( and or ) are
7491 missing. An empty, but allocated, vector is returned on no
7492 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7493 if we are parsing an attribute list for an attribute that wants a
7494 plain identifier argument, normal_attr for an attribute that wants
7495 an expression, or non_attr if we aren't parsing an attribute list. If
7496 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7497 not all of the expressions in the list were constant.
7498 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7499 will be written to with the location of the closing parenthesis. If
7500 an error occurs, it may or may not be written to. */
7501
7502 static vec<tree, va_gc> *
7503 cp_parser_parenthesized_expression_list (cp_parser* parser,
7504 int is_attribute_list,
7505 bool cast_p,
7506 bool allow_expansion_p,
7507 bool *non_constant_p,
7508 location_t *close_paren_loc)
7509 {
7510 vec<tree, va_gc> *expression_list;
7511 bool fold_expr_p = is_attribute_list != non_attr;
7512 tree identifier = NULL_TREE;
7513 bool saved_greater_than_is_operator_p;
7514
7515 /* Assume all the expressions will be constant. */
7516 if (non_constant_p)
7517 *non_constant_p = false;
7518
7519 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7520 return NULL;
7521
7522 expression_list = make_tree_vector ();
7523
7524 /* Within a parenthesized expression, a `>' token is always
7525 the greater-than operator. */
7526 saved_greater_than_is_operator_p
7527 = parser->greater_than_is_operator_p;
7528 parser->greater_than_is_operator_p = true;
7529
7530 /* Consume expressions until there are no more. */
7531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7532 while (true)
7533 {
7534 tree expr;
7535
7536 /* At the beginning of attribute lists, check to see if the
7537 next token is an identifier. */
7538 if (is_attribute_list == id_attr
7539 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7540 {
7541 cp_token *token;
7542
7543 /* Consume the identifier. */
7544 token = cp_lexer_consume_token (parser->lexer);
7545 /* Save the identifier. */
7546 identifier = token->u.value;
7547 }
7548 else
7549 {
7550 bool expr_non_constant_p;
7551
7552 /* Parse the next assignment-expression. */
7553 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7554 {
7555 /* A braced-init-list. */
7556 cp_lexer_set_source_position (parser->lexer);
7557 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7558 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7559 if (non_constant_p && expr_non_constant_p)
7560 *non_constant_p = true;
7561 }
7562 else if (non_constant_p)
7563 {
7564 expr = (cp_parser_constant_expression
7565 (parser, /*allow_non_constant_p=*/true,
7566 &expr_non_constant_p));
7567 if (expr_non_constant_p)
7568 *non_constant_p = true;
7569 }
7570 else
7571 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7572 cast_p);
7573
7574 if (fold_expr_p)
7575 expr = instantiate_non_dependent_expr (expr);
7576
7577 /* If we have an ellipsis, then this is an expression
7578 expansion. */
7579 if (allow_expansion_p
7580 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7581 {
7582 /* Consume the `...'. */
7583 cp_lexer_consume_token (parser->lexer);
7584
7585 /* Build the argument pack. */
7586 expr = make_pack_expansion (expr);
7587 }
7588
7589 /* Add it to the list. We add error_mark_node
7590 expressions to the list, so that we can still tell if
7591 the correct form for a parenthesized expression-list
7592 is found. That gives better errors. */
7593 vec_safe_push (expression_list, expr);
7594
7595 if (expr == error_mark_node)
7596 goto skip_comma;
7597 }
7598
7599 /* After the first item, attribute lists look the same as
7600 expression lists. */
7601 is_attribute_list = non_attr;
7602
7603 get_comma:;
7604 /* If the next token isn't a `,', then we are done. */
7605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7606 break;
7607
7608 /* Otherwise, consume the `,' and keep going. */
7609 cp_lexer_consume_token (parser->lexer);
7610 }
7611
7612 if (close_paren_loc)
7613 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7614
7615 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7616 {
7617 int ending;
7618
7619 skip_comma:;
7620 /* We try and resync to an unnested comma, as that will give the
7621 user better diagnostics. */
7622 ending = cp_parser_skip_to_closing_parenthesis (parser,
7623 /*recovering=*/true,
7624 /*or_comma=*/true,
7625 /*consume_paren=*/true);
7626 if (ending < 0)
7627 goto get_comma;
7628 if (!ending)
7629 {
7630 parser->greater_than_is_operator_p
7631 = saved_greater_than_is_operator_p;
7632 return NULL;
7633 }
7634 }
7635
7636 parser->greater_than_is_operator_p
7637 = saved_greater_than_is_operator_p;
7638
7639 if (identifier)
7640 vec_safe_insert (expression_list, 0, identifier);
7641
7642 return expression_list;
7643 }
7644
7645 /* Parse a pseudo-destructor-name.
7646
7647 pseudo-destructor-name:
7648 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7649 :: [opt] nested-name-specifier template template-id :: ~ type-name
7650 :: [opt] nested-name-specifier [opt] ~ type-name
7651
7652 If either of the first two productions is used, sets *SCOPE to the
7653 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7654 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7655 or ERROR_MARK_NODE if the parse fails. */
7656
7657 static void
7658 cp_parser_pseudo_destructor_name (cp_parser* parser,
7659 tree object,
7660 tree* scope,
7661 tree* type)
7662 {
7663 bool nested_name_specifier_p;
7664
7665 /* Handle ~auto. */
7666 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7667 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7668 && !type_dependent_expression_p (object))
7669 {
7670 if (cxx_dialect < cxx14)
7671 pedwarn (input_location, 0,
7672 "%<~auto%> only available with "
7673 "-std=c++14 or -std=gnu++14");
7674 cp_lexer_consume_token (parser->lexer);
7675 cp_lexer_consume_token (parser->lexer);
7676 *scope = NULL_TREE;
7677 *type = TREE_TYPE (object);
7678 return;
7679 }
7680
7681 /* Assume that things will not work out. */
7682 *type = error_mark_node;
7683
7684 /* Look for the optional `::' operator. */
7685 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7686 /* Look for the optional nested-name-specifier. */
7687 nested_name_specifier_p
7688 = (cp_parser_nested_name_specifier_opt (parser,
7689 /*typename_keyword_p=*/false,
7690 /*check_dependency_p=*/true,
7691 /*type_p=*/false,
7692 /*is_declaration=*/false)
7693 != NULL_TREE);
7694 /* Now, if we saw a nested-name-specifier, we might be doing the
7695 second production. */
7696 if (nested_name_specifier_p
7697 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7698 {
7699 /* Consume the `template' keyword. */
7700 cp_lexer_consume_token (parser->lexer);
7701 /* Parse the template-id. */
7702 cp_parser_template_id (parser,
7703 /*template_keyword_p=*/true,
7704 /*check_dependency_p=*/false,
7705 class_type,
7706 /*is_declaration=*/true);
7707 /* Look for the `::' token. */
7708 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7709 }
7710 /* If the next token is not a `~', then there might be some
7711 additional qualification. */
7712 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7713 {
7714 /* At this point, we're looking for "type-name :: ~". The type-name
7715 must not be a class-name, since this is a pseudo-destructor. So,
7716 it must be either an enum-name, or a typedef-name -- both of which
7717 are just identifiers. So, we peek ahead to check that the "::"
7718 and "~" tokens are present; if they are not, then we can avoid
7719 calling type_name. */
7720 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7721 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7722 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7723 {
7724 cp_parser_error (parser, "non-scalar type");
7725 return;
7726 }
7727
7728 /* Look for the type-name. */
7729 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7730 if (*scope == error_mark_node)
7731 return;
7732
7733 /* Look for the `::' token. */
7734 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7735 }
7736 else
7737 *scope = NULL_TREE;
7738
7739 /* Look for the `~'. */
7740 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7741
7742 /* Once we see the ~, this has to be a pseudo-destructor. */
7743 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7744 cp_parser_commit_to_topmost_tentative_parse (parser);
7745
7746 /* Look for the type-name again. We are not responsible for
7747 checking that it matches the first type-name. */
7748 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7749 }
7750
7751 /* Parse a unary-expression.
7752
7753 unary-expression:
7754 postfix-expression
7755 ++ cast-expression
7756 -- cast-expression
7757 unary-operator cast-expression
7758 sizeof unary-expression
7759 sizeof ( type-id )
7760 alignof ( type-id ) [C++0x]
7761 new-expression
7762 delete-expression
7763
7764 GNU Extensions:
7765
7766 unary-expression:
7767 __extension__ cast-expression
7768 __alignof__ unary-expression
7769 __alignof__ ( type-id )
7770 alignof unary-expression [C++0x]
7771 __real__ cast-expression
7772 __imag__ cast-expression
7773 && identifier
7774 sizeof ( type-id ) { initializer-list , [opt] }
7775 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7776 __alignof__ ( type-id ) { initializer-list , [opt] }
7777
7778 ADDRESS_P is true iff the unary-expression is appearing as the
7779 operand of the `&' operator. CAST_P is true if this expression is
7780 the target of a cast.
7781
7782 Returns a representation of the expression. */
7783
7784 static cp_expr
7785 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7786 bool address_p, bool cast_p, bool decltype_p)
7787 {
7788 cp_token *token;
7789 enum tree_code unary_operator;
7790
7791 /* Peek at the next token. */
7792 token = cp_lexer_peek_token (parser->lexer);
7793 /* Some keywords give away the kind of expression. */
7794 if (token->type == CPP_KEYWORD)
7795 {
7796 enum rid keyword = token->keyword;
7797
7798 switch (keyword)
7799 {
7800 case RID_ALIGNOF:
7801 case RID_SIZEOF:
7802 {
7803 tree operand, ret;
7804 enum tree_code op;
7805 location_t first_loc;
7806
7807 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7808 /* Consume the token. */
7809 cp_lexer_consume_token (parser->lexer);
7810 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7811 /* Parse the operand. */
7812 operand = cp_parser_sizeof_operand (parser, keyword);
7813
7814 if (TYPE_P (operand))
7815 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7816 else
7817 {
7818 /* ISO C++ defines alignof only with types, not with
7819 expressions. So pedwarn if alignof is used with a non-
7820 type expression. However, __alignof__ is ok. */
7821 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7822 pedwarn (token->location, OPT_Wpedantic,
7823 "ISO C++ does not allow %<alignof%> "
7824 "with a non-type");
7825
7826 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7827 }
7828 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7829 SIZEOF_EXPR with the original operand. */
7830 if (op == SIZEOF_EXPR && ret != error_mark_node)
7831 {
7832 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7833 {
7834 if (!processing_template_decl && TYPE_P (operand))
7835 {
7836 ret = build_min (SIZEOF_EXPR, size_type_node,
7837 build1 (NOP_EXPR, operand,
7838 error_mark_node));
7839 SIZEOF_EXPR_TYPE_P (ret) = 1;
7840 }
7841 else
7842 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7843 TREE_SIDE_EFFECTS (ret) = 0;
7844 TREE_READONLY (ret) = 1;
7845 }
7846 SET_EXPR_LOCATION (ret, first_loc);
7847 }
7848 return ret;
7849 }
7850
7851 case RID_NEW:
7852 return cp_parser_new_expression (parser);
7853
7854 case RID_DELETE:
7855 return cp_parser_delete_expression (parser);
7856
7857 case RID_EXTENSION:
7858 {
7859 /* The saved value of the PEDANTIC flag. */
7860 int saved_pedantic;
7861 tree expr;
7862
7863 /* Save away the PEDANTIC flag. */
7864 cp_parser_extension_opt (parser, &saved_pedantic);
7865 /* Parse the cast-expression. */
7866 expr = cp_parser_simple_cast_expression (parser);
7867 /* Restore the PEDANTIC flag. */
7868 pedantic = saved_pedantic;
7869
7870 return expr;
7871 }
7872
7873 case RID_REALPART:
7874 case RID_IMAGPART:
7875 {
7876 tree expression;
7877
7878 /* Consume the `__real__' or `__imag__' token. */
7879 cp_lexer_consume_token (parser->lexer);
7880 /* Parse the cast-expression. */
7881 expression = cp_parser_simple_cast_expression (parser);
7882 /* Create the complete representation. */
7883 return build_x_unary_op (token->location,
7884 (keyword == RID_REALPART
7885 ? REALPART_EXPR : IMAGPART_EXPR),
7886 expression,
7887 tf_warning_or_error);
7888 }
7889 break;
7890
7891 case RID_TRANSACTION_ATOMIC:
7892 case RID_TRANSACTION_RELAXED:
7893 return cp_parser_transaction_expression (parser, keyword);
7894
7895 case RID_NOEXCEPT:
7896 {
7897 tree expr;
7898 const char *saved_message;
7899 bool saved_integral_constant_expression_p;
7900 bool saved_non_integral_constant_expression_p;
7901 bool saved_greater_than_is_operator_p;
7902
7903 cp_lexer_consume_token (parser->lexer);
7904 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7905
7906 saved_message = parser->type_definition_forbidden_message;
7907 parser->type_definition_forbidden_message
7908 = G_("types may not be defined in %<noexcept%> expressions");
7909
7910 saved_integral_constant_expression_p
7911 = parser->integral_constant_expression_p;
7912 saved_non_integral_constant_expression_p
7913 = parser->non_integral_constant_expression_p;
7914 parser->integral_constant_expression_p = false;
7915
7916 saved_greater_than_is_operator_p
7917 = parser->greater_than_is_operator_p;
7918 parser->greater_than_is_operator_p = true;
7919
7920 ++cp_unevaluated_operand;
7921 ++c_inhibit_evaluation_warnings;
7922 ++cp_noexcept_operand;
7923 expr = cp_parser_expression (parser);
7924 --cp_noexcept_operand;
7925 --c_inhibit_evaluation_warnings;
7926 --cp_unevaluated_operand;
7927
7928 parser->greater_than_is_operator_p
7929 = saved_greater_than_is_operator_p;
7930
7931 parser->integral_constant_expression_p
7932 = saved_integral_constant_expression_p;
7933 parser->non_integral_constant_expression_p
7934 = saved_non_integral_constant_expression_p;
7935
7936 parser->type_definition_forbidden_message = saved_message;
7937
7938 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7939 return finish_noexcept_expr (expr, tf_warning_or_error);
7940 }
7941
7942 default:
7943 break;
7944 }
7945 }
7946
7947 /* Look for the `:: new' and `:: delete', which also signal the
7948 beginning of a new-expression, or delete-expression,
7949 respectively. If the next token is `::', then it might be one of
7950 these. */
7951 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7952 {
7953 enum rid keyword;
7954
7955 /* See if the token after the `::' is one of the keywords in
7956 which we're interested. */
7957 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7958 /* If it's `new', we have a new-expression. */
7959 if (keyword == RID_NEW)
7960 return cp_parser_new_expression (parser);
7961 /* Similarly, for `delete'. */
7962 else if (keyword == RID_DELETE)
7963 return cp_parser_delete_expression (parser);
7964 }
7965
7966 /* Look for a unary operator. */
7967 unary_operator = cp_parser_unary_operator (token);
7968 /* The `++' and `--' operators can be handled similarly, even though
7969 they are not technically unary-operators in the grammar. */
7970 if (unary_operator == ERROR_MARK)
7971 {
7972 if (token->type == CPP_PLUS_PLUS)
7973 unary_operator = PREINCREMENT_EXPR;
7974 else if (token->type == CPP_MINUS_MINUS)
7975 unary_operator = PREDECREMENT_EXPR;
7976 /* Handle the GNU address-of-label extension. */
7977 else if (cp_parser_allow_gnu_extensions_p (parser)
7978 && token->type == CPP_AND_AND)
7979 {
7980 tree identifier;
7981 tree expression;
7982 location_t start_loc = token->location;
7983
7984 /* Consume the '&&' token. */
7985 cp_lexer_consume_token (parser->lexer);
7986 /* Look for the identifier. */
7987 location_t finish_loc
7988 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7989 identifier = cp_parser_identifier (parser);
7990 /* Construct a location of the form:
7991 &&label
7992 ^~~~~~~
7993 with caret==start at the "&&", finish at the end of the label. */
7994 location_t combined_loc
7995 = make_location (start_loc, start_loc, finish_loc);
7996 /* Create an expression representing the address. */
7997 expression = finish_label_address_expr (identifier, combined_loc);
7998 if (cp_parser_non_integral_constant_expression (parser,
7999 NIC_ADDR_LABEL))
8000 expression = error_mark_node;
8001 return expression;
8002 }
8003 }
8004 if (unary_operator != ERROR_MARK)
8005 {
8006 cp_expr cast_expression;
8007 cp_expr expression = error_mark_node;
8008 non_integral_constant non_constant_p = NIC_NONE;
8009 location_t loc = token->location;
8010 tsubst_flags_t complain = complain_flags (decltype_p);
8011
8012 /* Consume the operator token. */
8013 token = cp_lexer_consume_token (parser->lexer);
8014 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8015
8016 /* Parse the cast-expression. */
8017 cast_expression
8018 = cp_parser_cast_expression (parser,
8019 unary_operator == ADDR_EXPR,
8020 /*cast_p=*/false,
8021 /*decltype*/false,
8022 pidk);
8023
8024 /* Make a location:
8025 OP_TOKEN CAST_EXPRESSION
8026 ^~~~~~~~~~~~~~~~~~~~~~~~~
8027 with start==caret at the operator token, and
8028 extending to the end of the cast_expression. */
8029 loc = make_location (loc, loc, cast_expression.get_finish ());
8030
8031 /* Now, build an appropriate representation. */
8032 switch (unary_operator)
8033 {
8034 case INDIRECT_REF:
8035 non_constant_p = NIC_STAR;
8036 expression = build_x_indirect_ref (loc, cast_expression,
8037 RO_UNARY_STAR,
8038 complain);
8039 /* TODO: build_x_indirect_ref does not always honor the
8040 location, so ensure it is set. */
8041 expression.set_location (loc);
8042 break;
8043
8044 case ADDR_EXPR:
8045 non_constant_p = NIC_ADDR;
8046 /* Fall through. */
8047 case BIT_NOT_EXPR:
8048 expression = build_x_unary_op (loc, unary_operator,
8049 cast_expression,
8050 complain);
8051 /* TODO: build_x_unary_op does not always honor the location,
8052 so ensure it is set. */
8053 expression.set_location (loc);
8054 break;
8055
8056 case PREINCREMENT_EXPR:
8057 case PREDECREMENT_EXPR:
8058 non_constant_p = unary_operator == PREINCREMENT_EXPR
8059 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8060 /* Fall through. */
8061 case NEGATE_EXPR:
8062 /* Immediately fold negation of a constant, unless the constant is 0
8063 (since -0 == 0) or it would overflow. */
8064 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8065 && CONSTANT_CLASS_P (cast_expression)
8066 && !integer_zerop (cast_expression)
8067 && !TREE_OVERFLOW (cast_expression))
8068 {
8069 tree folded = fold_build1 (unary_operator,
8070 TREE_TYPE (cast_expression),
8071 cast_expression);
8072 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8073 {
8074 expression = cp_expr (folded, loc);
8075 break;
8076 }
8077 }
8078 /* Fall through. */
8079 case UNARY_PLUS_EXPR:
8080 case TRUTH_NOT_EXPR:
8081 expression = finish_unary_op_expr (loc, unary_operator,
8082 cast_expression, complain);
8083 break;
8084
8085 default:
8086 gcc_unreachable ();
8087 }
8088
8089 if (non_constant_p != NIC_NONE
8090 && cp_parser_non_integral_constant_expression (parser,
8091 non_constant_p))
8092 expression = error_mark_node;
8093
8094 return expression;
8095 }
8096
8097 return cp_parser_postfix_expression (parser, address_p, cast_p,
8098 /*member_access_only_p=*/false,
8099 decltype_p,
8100 pidk);
8101 }
8102
8103 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8104 unary-operator, the corresponding tree code is returned. */
8105
8106 static enum tree_code
8107 cp_parser_unary_operator (cp_token* token)
8108 {
8109 switch (token->type)
8110 {
8111 case CPP_MULT:
8112 return INDIRECT_REF;
8113
8114 case CPP_AND:
8115 return ADDR_EXPR;
8116
8117 case CPP_PLUS:
8118 return UNARY_PLUS_EXPR;
8119
8120 case CPP_MINUS:
8121 return NEGATE_EXPR;
8122
8123 case CPP_NOT:
8124 return TRUTH_NOT_EXPR;
8125
8126 case CPP_COMPL:
8127 return BIT_NOT_EXPR;
8128
8129 default:
8130 return ERROR_MARK;
8131 }
8132 }
8133
8134 /* Parse a new-expression.
8135
8136 new-expression:
8137 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8138 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8139
8140 Returns a representation of the expression. */
8141
8142 static tree
8143 cp_parser_new_expression (cp_parser* parser)
8144 {
8145 bool global_scope_p;
8146 vec<tree, va_gc> *placement;
8147 tree type;
8148 vec<tree, va_gc> *initializer;
8149 tree nelts = NULL_TREE;
8150 tree ret;
8151
8152 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8153
8154 /* Look for the optional `::' operator. */
8155 global_scope_p
8156 = (cp_parser_global_scope_opt (parser,
8157 /*current_scope_valid_p=*/false)
8158 != NULL_TREE);
8159 /* Look for the `new' operator. */
8160 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8161 /* There's no easy way to tell a new-placement from the
8162 `( type-id )' construct. */
8163 cp_parser_parse_tentatively (parser);
8164 /* Look for a new-placement. */
8165 placement = cp_parser_new_placement (parser);
8166 /* If that didn't work out, there's no new-placement. */
8167 if (!cp_parser_parse_definitely (parser))
8168 {
8169 if (placement != NULL)
8170 release_tree_vector (placement);
8171 placement = NULL;
8172 }
8173
8174 /* If the next token is a `(', then we have a parenthesized
8175 type-id. */
8176 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8177 {
8178 cp_token *token;
8179 const char *saved_message = parser->type_definition_forbidden_message;
8180
8181 /* Consume the `('. */
8182 cp_lexer_consume_token (parser->lexer);
8183
8184 /* Parse the type-id. */
8185 parser->type_definition_forbidden_message
8186 = G_("types may not be defined in a new-expression");
8187 {
8188 type_id_in_expr_sentinel s (parser);
8189 type = cp_parser_type_id (parser);
8190 }
8191 parser->type_definition_forbidden_message = saved_message;
8192
8193 /* Look for the closing `)'. */
8194 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8195 token = cp_lexer_peek_token (parser->lexer);
8196 /* There should not be a direct-new-declarator in this production,
8197 but GCC used to allowed this, so we check and emit a sensible error
8198 message for this case. */
8199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8200 {
8201 error_at (token->location,
8202 "array bound forbidden after parenthesized type-id");
8203 inform (token->location,
8204 "try removing the parentheses around the type-id");
8205 cp_parser_direct_new_declarator (parser);
8206 }
8207 }
8208 /* Otherwise, there must be a new-type-id. */
8209 else
8210 type = cp_parser_new_type_id (parser, &nelts);
8211
8212 /* If the next token is a `(' or '{', then we have a new-initializer. */
8213 cp_token *token = cp_lexer_peek_token (parser->lexer);
8214 if (token->type == CPP_OPEN_PAREN
8215 || token->type == CPP_OPEN_BRACE)
8216 initializer = cp_parser_new_initializer (parser);
8217 else
8218 initializer = NULL;
8219
8220 /* A new-expression may not appear in an integral constant
8221 expression. */
8222 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8223 ret = error_mark_node;
8224 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8225 of a new-type-id or type-id of a new-expression, the new-expression shall
8226 contain a new-initializer of the form ( assignment-expression )".
8227 Additionally, consistently with the spirit of DR 1467, we want to accept
8228 'new auto { 2 }' too. */
8229 else if (type_uses_auto (type)
8230 && (vec_safe_length (initializer) != 1
8231 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8232 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8233 {
8234 error_at (token->location,
8235 "initialization of new-expression for type %<auto%> "
8236 "requires exactly one element");
8237 ret = error_mark_node;
8238 }
8239 else
8240 {
8241 /* Construct a location e.g.:
8242 ptr = new int[100]
8243 ^~~~~~~~~~~~
8244 with caret == start at the start of the "new" token, and the end
8245 at the end of the final token we consumed. */
8246 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8247 location_t end_loc = get_finish (end_tok->location);
8248 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8249
8250 /* Create a representation of the new-expression. */
8251 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8252 tf_warning_or_error);
8253 protected_set_expr_location (ret, combined_loc);
8254 }
8255
8256 if (placement != NULL)
8257 release_tree_vector (placement);
8258 if (initializer != NULL)
8259 release_tree_vector (initializer);
8260
8261 return ret;
8262 }
8263
8264 /* Parse a new-placement.
8265
8266 new-placement:
8267 ( expression-list )
8268
8269 Returns the same representation as for an expression-list. */
8270
8271 static vec<tree, va_gc> *
8272 cp_parser_new_placement (cp_parser* parser)
8273 {
8274 vec<tree, va_gc> *expression_list;
8275
8276 /* Parse the expression-list. */
8277 expression_list = (cp_parser_parenthesized_expression_list
8278 (parser, non_attr, /*cast_p=*/false,
8279 /*allow_expansion_p=*/true,
8280 /*non_constant_p=*/NULL));
8281
8282 if (expression_list && expression_list->is_empty ())
8283 error ("expected expression-list or type-id");
8284
8285 return expression_list;
8286 }
8287
8288 /* Parse a new-type-id.
8289
8290 new-type-id:
8291 type-specifier-seq new-declarator [opt]
8292
8293 Returns the TYPE allocated. If the new-type-id indicates an array
8294 type, *NELTS is set to the number of elements in the last array
8295 bound; the TYPE will not include the last array bound. */
8296
8297 static tree
8298 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8299 {
8300 cp_decl_specifier_seq type_specifier_seq;
8301 cp_declarator *new_declarator;
8302 cp_declarator *declarator;
8303 cp_declarator *outer_declarator;
8304 const char *saved_message;
8305
8306 /* The type-specifier sequence must not contain type definitions.
8307 (It cannot contain declarations of new types either, but if they
8308 are not definitions we will catch that because they are not
8309 complete.) */
8310 saved_message = parser->type_definition_forbidden_message;
8311 parser->type_definition_forbidden_message
8312 = G_("types may not be defined in a new-type-id");
8313 /* Parse the type-specifier-seq. */
8314 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8315 /*is_trailing_return=*/false,
8316 &type_specifier_seq);
8317 /* Restore the old message. */
8318 parser->type_definition_forbidden_message = saved_message;
8319
8320 if (type_specifier_seq.type == error_mark_node)
8321 return error_mark_node;
8322
8323 /* Parse the new-declarator. */
8324 new_declarator = cp_parser_new_declarator_opt (parser);
8325
8326 /* Determine the number of elements in the last array dimension, if
8327 any. */
8328 *nelts = NULL_TREE;
8329 /* Skip down to the last array dimension. */
8330 declarator = new_declarator;
8331 outer_declarator = NULL;
8332 while (declarator && (declarator->kind == cdk_pointer
8333 || declarator->kind == cdk_ptrmem))
8334 {
8335 outer_declarator = declarator;
8336 declarator = declarator->declarator;
8337 }
8338 while (declarator
8339 && declarator->kind == cdk_array
8340 && declarator->declarator
8341 && declarator->declarator->kind == cdk_array)
8342 {
8343 outer_declarator = declarator;
8344 declarator = declarator->declarator;
8345 }
8346
8347 if (declarator && declarator->kind == cdk_array)
8348 {
8349 *nelts = declarator->u.array.bounds;
8350 if (*nelts == error_mark_node)
8351 *nelts = integer_one_node;
8352
8353 if (outer_declarator)
8354 outer_declarator->declarator = declarator->declarator;
8355 else
8356 new_declarator = NULL;
8357 }
8358
8359 return groktypename (&type_specifier_seq, new_declarator, false);
8360 }
8361
8362 /* Parse an (optional) new-declarator.
8363
8364 new-declarator:
8365 ptr-operator new-declarator [opt]
8366 direct-new-declarator
8367
8368 Returns the declarator. */
8369
8370 static cp_declarator *
8371 cp_parser_new_declarator_opt (cp_parser* parser)
8372 {
8373 enum tree_code code;
8374 tree type, std_attributes = NULL_TREE;
8375 cp_cv_quals cv_quals;
8376
8377 /* We don't know if there's a ptr-operator next, or not. */
8378 cp_parser_parse_tentatively (parser);
8379 /* Look for a ptr-operator. */
8380 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8381 /* If that worked, look for more new-declarators. */
8382 if (cp_parser_parse_definitely (parser))
8383 {
8384 cp_declarator *declarator;
8385
8386 /* Parse another optional declarator. */
8387 declarator = cp_parser_new_declarator_opt (parser);
8388
8389 declarator = cp_parser_make_indirect_declarator
8390 (code, type, cv_quals, declarator, std_attributes);
8391
8392 return declarator;
8393 }
8394
8395 /* If the next token is a `[', there is a direct-new-declarator. */
8396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8397 return cp_parser_direct_new_declarator (parser);
8398
8399 return NULL;
8400 }
8401
8402 /* Parse a direct-new-declarator.
8403
8404 direct-new-declarator:
8405 [ expression ]
8406 direct-new-declarator [constant-expression]
8407
8408 */
8409
8410 static cp_declarator *
8411 cp_parser_direct_new_declarator (cp_parser* parser)
8412 {
8413 cp_declarator *declarator = NULL;
8414
8415 while (true)
8416 {
8417 tree expression;
8418 cp_token *token;
8419
8420 /* Look for the opening `['. */
8421 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8422
8423 token = cp_lexer_peek_token (parser->lexer);
8424 expression = cp_parser_expression (parser);
8425 /* The standard requires that the expression have integral
8426 type. DR 74 adds enumeration types. We believe that the
8427 real intent is that these expressions be handled like the
8428 expression in a `switch' condition, which also allows
8429 classes with a single conversion to integral or
8430 enumeration type. */
8431 if (!processing_template_decl)
8432 {
8433 expression
8434 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8435 expression,
8436 /*complain=*/true);
8437 if (!expression)
8438 {
8439 error_at (token->location,
8440 "expression in new-declarator must have integral "
8441 "or enumeration type");
8442 expression = error_mark_node;
8443 }
8444 }
8445
8446 /* Look for the closing `]'. */
8447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8448
8449 /* Add this bound to the declarator. */
8450 declarator = make_array_declarator (declarator, expression);
8451
8452 /* If the next token is not a `[', then there are no more
8453 bounds. */
8454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8455 break;
8456 }
8457
8458 return declarator;
8459 }
8460
8461 /* Parse a new-initializer.
8462
8463 new-initializer:
8464 ( expression-list [opt] )
8465 braced-init-list
8466
8467 Returns a representation of the expression-list. */
8468
8469 static vec<tree, va_gc> *
8470 cp_parser_new_initializer (cp_parser* parser)
8471 {
8472 vec<tree, va_gc> *expression_list;
8473
8474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8475 {
8476 tree t;
8477 bool expr_non_constant_p;
8478 cp_lexer_set_source_position (parser->lexer);
8479 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8480 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8481 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8482 expression_list = make_tree_vector_single (t);
8483 }
8484 else
8485 expression_list = (cp_parser_parenthesized_expression_list
8486 (parser, non_attr, /*cast_p=*/false,
8487 /*allow_expansion_p=*/true,
8488 /*non_constant_p=*/NULL));
8489
8490 return expression_list;
8491 }
8492
8493 /* Parse a delete-expression.
8494
8495 delete-expression:
8496 :: [opt] delete cast-expression
8497 :: [opt] delete [ ] cast-expression
8498
8499 Returns a representation of the expression. */
8500
8501 static tree
8502 cp_parser_delete_expression (cp_parser* parser)
8503 {
8504 bool global_scope_p;
8505 bool array_p;
8506 tree expression;
8507
8508 /* Look for the optional `::' operator. */
8509 global_scope_p
8510 = (cp_parser_global_scope_opt (parser,
8511 /*current_scope_valid_p=*/false)
8512 != NULL_TREE);
8513 /* Look for the `delete' keyword. */
8514 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8515 /* See if the array syntax is in use. */
8516 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8517 {
8518 /* Consume the `[' token. */
8519 cp_lexer_consume_token (parser->lexer);
8520 /* Look for the `]' token. */
8521 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8522 /* Remember that this is the `[]' construct. */
8523 array_p = true;
8524 }
8525 else
8526 array_p = false;
8527
8528 /* Parse the cast-expression. */
8529 expression = cp_parser_simple_cast_expression (parser);
8530
8531 /* A delete-expression may not appear in an integral constant
8532 expression. */
8533 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8534 return error_mark_node;
8535
8536 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8537 tf_warning_or_error);
8538 }
8539
8540 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8541 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8542 0 otherwise. */
8543
8544 static int
8545 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8546 {
8547 cp_token *token = cp_lexer_peek_token (parser->lexer);
8548 switch (token->type)
8549 {
8550 case CPP_COMMA:
8551 case CPP_SEMICOLON:
8552 case CPP_QUERY:
8553 case CPP_COLON:
8554 case CPP_CLOSE_SQUARE:
8555 case CPP_CLOSE_PAREN:
8556 case CPP_CLOSE_BRACE:
8557 case CPP_OPEN_BRACE:
8558 case CPP_DOT:
8559 case CPP_DOT_STAR:
8560 case CPP_DEREF:
8561 case CPP_DEREF_STAR:
8562 case CPP_DIV:
8563 case CPP_MOD:
8564 case CPP_LSHIFT:
8565 case CPP_RSHIFT:
8566 case CPP_LESS:
8567 case CPP_GREATER:
8568 case CPP_LESS_EQ:
8569 case CPP_GREATER_EQ:
8570 case CPP_EQ_EQ:
8571 case CPP_NOT_EQ:
8572 case CPP_EQ:
8573 case CPP_MULT_EQ:
8574 case CPP_DIV_EQ:
8575 case CPP_MOD_EQ:
8576 case CPP_PLUS_EQ:
8577 case CPP_MINUS_EQ:
8578 case CPP_RSHIFT_EQ:
8579 case CPP_LSHIFT_EQ:
8580 case CPP_AND_EQ:
8581 case CPP_XOR_EQ:
8582 case CPP_OR_EQ:
8583 case CPP_XOR:
8584 case CPP_OR:
8585 case CPP_OR_OR:
8586 case CPP_EOF:
8587 case CPP_ELLIPSIS:
8588 return 0;
8589
8590 case CPP_OPEN_PAREN:
8591 /* In ((type ()) () the last () isn't a valid cast-expression,
8592 so the whole must be parsed as postfix-expression. */
8593 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8594 != CPP_CLOSE_PAREN;
8595
8596 case CPP_OPEN_SQUARE:
8597 /* '[' may start a primary-expression in obj-c++ and in C++11,
8598 as a lambda-expression, eg, '(void)[]{}'. */
8599 if (cxx_dialect >= cxx11)
8600 return -1;
8601 return c_dialect_objc ();
8602
8603 case CPP_PLUS_PLUS:
8604 case CPP_MINUS_MINUS:
8605 /* '++' and '--' may or may not start a cast-expression:
8606
8607 struct T { void operator++(int); };
8608 void f() { (T())++; }
8609
8610 vs
8611
8612 int a;
8613 (int)++a; */
8614 return -1;
8615
8616 default:
8617 return 1;
8618 }
8619 }
8620
8621 /* Parse a cast-expression.
8622
8623 cast-expression:
8624 unary-expression
8625 ( type-id ) cast-expression
8626
8627 ADDRESS_P is true iff the unary-expression is appearing as the
8628 operand of the `&' operator. CAST_P is true if this expression is
8629 the target of a cast.
8630
8631 Returns a representation of the expression. */
8632
8633 static cp_expr
8634 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8635 bool decltype_p, cp_id_kind * pidk)
8636 {
8637 /* If it's a `(', then we might be looking at a cast. */
8638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8639 {
8640 tree type = NULL_TREE;
8641 cp_expr expr (NULL_TREE);
8642 int cast_expression = 0;
8643 const char *saved_message;
8644
8645 /* There's no way to know yet whether or not this is a cast.
8646 For example, `(int (3))' is a unary-expression, while `(int)
8647 3' is a cast. So, we resort to parsing tentatively. */
8648 cp_parser_parse_tentatively (parser);
8649 /* Types may not be defined in a cast. */
8650 saved_message = parser->type_definition_forbidden_message;
8651 parser->type_definition_forbidden_message
8652 = G_("types may not be defined in casts");
8653 /* Consume the `('. */
8654 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8655 location_t open_paren_loc = open_paren->location;
8656
8657 /* A very tricky bit is that `(struct S) { 3 }' is a
8658 compound-literal (which we permit in C++ as an extension).
8659 But, that construct is not a cast-expression -- it is a
8660 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8661 is legal; if the compound-literal were a cast-expression,
8662 you'd need an extra set of parentheses.) But, if we parse
8663 the type-id, and it happens to be a class-specifier, then we
8664 will commit to the parse at that point, because we cannot
8665 undo the action that is done when creating a new class. So,
8666 then we cannot back up and do a postfix-expression.
8667
8668 Another tricky case is the following (c++/29234):
8669
8670 struct S { void operator () (); };
8671
8672 void foo ()
8673 {
8674 ( S()() );
8675 }
8676
8677 As a type-id we parse the parenthesized S()() as a function
8678 returning a function, groktypename complains and we cannot
8679 back up in this case either.
8680
8681 Therefore, we scan ahead to the closing `)', and check to see
8682 if the tokens after the `)' can start a cast-expression. Otherwise
8683 we are dealing with an unary-expression, a postfix-expression
8684 or something else.
8685
8686 Yet another tricky case, in C++11, is the following (c++/54891):
8687
8688 (void)[]{};
8689
8690 The issue is that usually, besides the case of lambda-expressions,
8691 the parenthesized type-id cannot be followed by '[', and, eg, we
8692 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8693 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8694 we don't commit, we try a cast-expression, then an unary-expression.
8695
8696 Save tokens so that we can put them back. */
8697 cp_lexer_save_tokens (parser->lexer);
8698
8699 /* We may be looking at a cast-expression. */
8700 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8701 /*consume_paren=*/true))
8702 cast_expression
8703 = cp_parser_tokens_start_cast_expression (parser);
8704
8705 /* Roll back the tokens we skipped. */
8706 cp_lexer_rollback_tokens (parser->lexer);
8707 /* If we aren't looking at a cast-expression, simulate an error so
8708 that the call to cp_parser_error_occurred below returns true. */
8709 if (!cast_expression)
8710 cp_parser_simulate_error (parser);
8711 else
8712 {
8713 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8714 parser->in_type_id_in_expr_p = true;
8715 /* Look for the type-id. */
8716 type = cp_parser_type_id (parser);
8717 /* Look for the closing `)'. */
8718 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8719 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8720 }
8721
8722 /* Restore the saved message. */
8723 parser->type_definition_forbidden_message = saved_message;
8724
8725 /* At this point this can only be either a cast or a
8726 parenthesized ctor such as `(T ())' that looks like a cast to
8727 function returning T. */
8728 if (!cp_parser_error_occurred (parser))
8729 {
8730 /* Only commit if the cast-expression doesn't start with
8731 '++', '--', or '[' in C++11. */
8732 if (cast_expression > 0)
8733 cp_parser_commit_to_topmost_tentative_parse (parser);
8734
8735 expr = cp_parser_cast_expression (parser,
8736 /*address_p=*/false,
8737 /*cast_p=*/true,
8738 /*decltype_p=*/false,
8739 pidk);
8740
8741 if (cp_parser_parse_definitely (parser))
8742 {
8743 /* Warn about old-style casts, if so requested. */
8744 if (warn_old_style_cast
8745 && !in_system_header_at (input_location)
8746 && !VOID_TYPE_P (type)
8747 && current_lang_name != lang_name_c)
8748 warning (OPT_Wold_style_cast, "use of old-style cast");
8749
8750 /* Only type conversions to integral or enumeration types
8751 can be used in constant-expressions. */
8752 if (!cast_valid_in_integral_constant_expression_p (type)
8753 && cp_parser_non_integral_constant_expression (parser,
8754 NIC_CAST))
8755 return error_mark_node;
8756
8757 /* Perform the cast. */
8758 /* Make a location:
8759 (TYPE) EXPR
8760 ^~~~~~~~~~~
8761 with start==caret at the open paren, extending to the
8762 end of "expr". */
8763 location_t cast_loc = make_location (open_paren_loc,
8764 open_paren_loc,
8765 expr.get_finish ());
8766 expr = build_c_cast (cast_loc, type, expr);
8767 return expr;
8768 }
8769 }
8770 else
8771 cp_parser_abort_tentative_parse (parser);
8772 }
8773
8774 /* If we get here, then it's not a cast, so it must be a
8775 unary-expression. */
8776 return cp_parser_unary_expression (parser, pidk, address_p,
8777 cast_p, decltype_p);
8778 }
8779
8780 /* Parse a binary expression of the general form:
8781
8782 pm-expression:
8783 cast-expression
8784 pm-expression .* cast-expression
8785 pm-expression ->* cast-expression
8786
8787 multiplicative-expression:
8788 pm-expression
8789 multiplicative-expression * pm-expression
8790 multiplicative-expression / pm-expression
8791 multiplicative-expression % pm-expression
8792
8793 additive-expression:
8794 multiplicative-expression
8795 additive-expression + multiplicative-expression
8796 additive-expression - multiplicative-expression
8797
8798 shift-expression:
8799 additive-expression
8800 shift-expression << additive-expression
8801 shift-expression >> additive-expression
8802
8803 relational-expression:
8804 shift-expression
8805 relational-expression < shift-expression
8806 relational-expression > shift-expression
8807 relational-expression <= shift-expression
8808 relational-expression >= shift-expression
8809
8810 GNU Extension:
8811
8812 relational-expression:
8813 relational-expression <? shift-expression
8814 relational-expression >? shift-expression
8815
8816 equality-expression:
8817 relational-expression
8818 equality-expression == relational-expression
8819 equality-expression != relational-expression
8820
8821 and-expression:
8822 equality-expression
8823 and-expression & equality-expression
8824
8825 exclusive-or-expression:
8826 and-expression
8827 exclusive-or-expression ^ and-expression
8828
8829 inclusive-or-expression:
8830 exclusive-or-expression
8831 inclusive-or-expression | exclusive-or-expression
8832
8833 logical-and-expression:
8834 inclusive-or-expression
8835 logical-and-expression && inclusive-or-expression
8836
8837 logical-or-expression:
8838 logical-and-expression
8839 logical-or-expression || logical-and-expression
8840
8841 All these are implemented with a single function like:
8842
8843 binary-expression:
8844 simple-cast-expression
8845 binary-expression <token> binary-expression
8846
8847 CAST_P is true if this expression is the target of a cast.
8848
8849 The binops_by_token map is used to get the tree codes for each <token> type.
8850 binary-expressions are associated according to a precedence table. */
8851
8852 #define TOKEN_PRECEDENCE(token) \
8853 (((token->type == CPP_GREATER \
8854 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8855 && !parser->greater_than_is_operator_p) \
8856 ? PREC_NOT_OPERATOR \
8857 : binops_by_token[token->type].prec)
8858
8859 static cp_expr
8860 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8861 bool no_toplevel_fold_p,
8862 bool decltype_p,
8863 enum cp_parser_prec prec,
8864 cp_id_kind * pidk)
8865 {
8866 cp_parser_expression_stack stack;
8867 cp_parser_expression_stack_entry *sp = &stack[0];
8868 cp_parser_expression_stack_entry current;
8869 cp_expr rhs;
8870 cp_token *token;
8871 enum tree_code rhs_type;
8872 enum cp_parser_prec new_prec, lookahead_prec;
8873 tree overload;
8874
8875 /* Parse the first expression. */
8876 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8877 ? TRUTH_NOT_EXPR : ERROR_MARK);
8878 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8879 cast_p, decltype_p, pidk);
8880 current.prec = prec;
8881
8882 if (cp_parser_error_occurred (parser))
8883 return error_mark_node;
8884
8885 for (;;)
8886 {
8887 /* Get an operator token. */
8888 token = cp_lexer_peek_token (parser->lexer);
8889
8890 if (warn_cxx11_compat
8891 && token->type == CPP_RSHIFT
8892 && !parser->greater_than_is_operator_p)
8893 {
8894 if (warning_at (token->location, OPT_Wc__11_compat,
8895 "%<>>%> operator is treated"
8896 " as two right angle brackets in C++11"))
8897 inform (token->location,
8898 "suggest parentheses around %<>>%> expression");
8899 }
8900
8901 new_prec = TOKEN_PRECEDENCE (token);
8902 if (new_prec != PREC_NOT_OPERATOR
8903 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8904 /* This is a fold-expression; handle it later. */
8905 new_prec = PREC_NOT_OPERATOR;
8906
8907 /* Popping an entry off the stack means we completed a subexpression:
8908 - either we found a token which is not an operator (`>' where it is not
8909 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8910 will happen repeatedly;
8911 - or, we found an operator which has lower priority. This is the case
8912 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8913 parsing `3 * 4'. */
8914 if (new_prec <= current.prec)
8915 {
8916 if (sp == stack)
8917 break;
8918 else
8919 goto pop;
8920 }
8921
8922 get_rhs:
8923 current.tree_type = binops_by_token[token->type].tree_type;
8924 current.loc = token->location;
8925
8926 /* We used the operator token. */
8927 cp_lexer_consume_token (parser->lexer);
8928
8929 /* For "false && x" or "true || x", x will never be executed;
8930 disable warnings while evaluating it. */
8931 if (current.tree_type == TRUTH_ANDIF_EXPR)
8932 c_inhibit_evaluation_warnings +=
8933 cp_fully_fold (current.lhs) == truthvalue_false_node;
8934 else if (current.tree_type == TRUTH_ORIF_EXPR)
8935 c_inhibit_evaluation_warnings +=
8936 cp_fully_fold (current.lhs) == truthvalue_true_node;
8937
8938 /* Extract another operand. It may be the RHS of this expression
8939 or the LHS of a new, higher priority expression. */
8940 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8941 ? TRUTH_NOT_EXPR : ERROR_MARK);
8942 rhs = cp_parser_simple_cast_expression (parser);
8943
8944 /* Get another operator token. Look up its precedence to avoid
8945 building a useless (immediately popped) stack entry for common
8946 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8947 token = cp_lexer_peek_token (parser->lexer);
8948 lookahead_prec = TOKEN_PRECEDENCE (token);
8949 if (lookahead_prec != PREC_NOT_OPERATOR
8950 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8951 lookahead_prec = PREC_NOT_OPERATOR;
8952 if (lookahead_prec > new_prec)
8953 {
8954 /* ... and prepare to parse the RHS of the new, higher priority
8955 expression. Since precedence levels on the stack are
8956 monotonically increasing, we do not have to care about
8957 stack overflows. */
8958 *sp = current;
8959 ++sp;
8960 current.lhs = rhs;
8961 current.lhs_type = rhs_type;
8962 current.prec = new_prec;
8963 new_prec = lookahead_prec;
8964 goto get_rhs;
8965
8966 pop:
8967 lookahead_prec = new_prec;
8968 /* If the stack is not empty, we have parsed into LHS the right side
8969 (`4' in the example above) of an expression we had suspended.
8970 We can use the information on the stack to recover the LHS (`3')
8971 from the stack together with the tree code (`MULT_EXPR'), and
8972 the precedence of the higher level subexpression
8973 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8974 which will be used to actually build the additive expression. */
8975 rhs = current.lhs;
8976 rhs_type = current.lhs_type;
8977 --sp;
8978 current = *sp;
8979 }
8980
8981 /* Undo the disabling of warnings done above. */
8982 if (current.tree_type == TRUTH_ANDIF_EXPR)
8983 c_inhibit_evaluation_warnings -=
8984 cp_fully_fold (current.lhs) == truthvalue_false_node;
8985 else if (current.tree_type == TRUTH_ORIF_EXPR)
8986 c_inhibit_evaluation_warnings -=
8987 cp_fully_fold (current.lhs) == truthvalue_true_node;
8988
8989 if (warn_logical_not_paren
8990 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8991 && current.lhs_type == TRUTH_NOT_EXPR
8992 /* Avoid warning for !!x == y. */
8993 && (TREE_CODE (current.lhs) != NE_EXPR
8994 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8995 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8996 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8997 /* Avoid warning for !b == y where b is boolean. */
8998 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8999 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9000 != BOOLEAN_TYPE))))
9001 /* Avoid warning for !!b == y where b is boolean. */
9002 && (!DECL_P (current.lhs)
9003 || TREE_TYPE (current.lhs) == NULL_TREE
9004 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9005 warn_logical_not_parentheses (current.loc, current.tree_type,
9006 current.lhs, maybe_constant_value (rhs));
9007
9008 overload = NULL;
9009
9010 location_t combined_loc = make_location (current.loc,
9011 current.lhs.get_start (),
9012 rhs.get_finish ());
9013
9014 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9015 ERROR_MARK for everything that is not a binary expression.
9016 This makes warn_about_parentheses miss some warnings that
9017 involve unary operators. For unary expressions we should
9018 pass the correct tree_code unless the unary expression was
9019 surrounded by parentheses.
9020 */
9021 if (no_toplevel_fold_p
9022 && lookahead_prec <= current.prec
9023 && sp == stack)
9024 current.lhs = build2_loc (combined_loc,
9025 current.tree_type,
9026 TREE_CODE_CLASS (current.tree_type)
9027 == tcc_comparison
9028 ? boolean_type_node : TREE_TYPE (current.lhs),
9029 current.lhs, rhs);
9030 else
9031 {
9032 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9033 current.lhs, current.lhs_type,
9034 rhs, rhs_type, &overload,
9035 complain_flags (decltype_p));
9036 /* TODO: build_x_binary_op doesn't always honor the location. */
9037 current.lhs.set_location (combined_loc);
9038 }
9039 current.lhs_type = current.tree_type;
9040
9041 /* If the binary operator required the use of an overloaded operator,
9042 then this expression cannot be an integral constant-expression.
9043 An overloaded operator can be used even if both operands are
9044 otherwise permissible in an integral constant-expression if at
9045 least one of the operands is of enumeration type. */
9046
9047 if (overload
9048 && cp_parser_non_integral_constant_expression (parser,
9049 NIC_OVERLOADED))
9050 return error_mark_node;
9051 }
9052
9053 return current.lhs;
9054 }
9055
9056 static cp_expr
9057 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9058 bool no_toplevel_fold_p,
9059 enum cp_parser_prec prec,
9060 cp_id_kind * pidk)
9061 {
9062 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9063 /*decltype*/false, prec, pidk);
9064 }
9065
9066 /* Parse the `? expression : assignment-expression' part of a
9067 conditional-expression. The LOGICAL_OR_EXPR is the
9068 logical-or-expression that started the conditional-expression.
9069 Returns a representation of the entire conditional-expression.
9070
9071 This routine is used by cp_parser_assignment_expression.
9072
9073 ? expression : assignment-expression
9074
9075 GNU Extensions:
9076
9077 ? : assignment-expression */
9078
9079 static tree
9080 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9081 {
9082 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9083 cp_expr assignment_expr;
9084 struct cp_token *token;
9085 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9086
9087 /* Consume the `?' token. */
9088 cp_lexer_consume_token (parser->lexer);
9089 token = cp_lexer_peek_token (parser->lexer);
9090 if (cp_parser_allow_gnu_extensions_p (parser)
9091 && token->type == CPP_COLON)
9092 {
9093 pedwarn (token->location, OPT_Wpedantic,
9094 "ISO C++ does not allow ?: with omitted middle operand");
9095 /* Implicit true clause. */
9096 expr = NULL_TREE;
9097 c_inhibit_evaluation_warnings +=
9098 folded_logical_or_expr == truthvalue_true_node;
9099 warn_for_omitted_condop (token->location, logical_or_expr);
9100 }
9101 else
9102 {
9103 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9104 parser->colon_corrects_to_scope_p = false;
9105 /* Parse the expression. */
9106 c_inhibit_evaluation_warnings +=
9107 folded_logical_or_expr == truthvalue_false_node;
9108 expr = cp_parser_expression (parser);
9109 c_inhibit_evaluation_warnings +=
9110 ((folded_logical_or_expr == truthvalue_true_node)
9111 - (folded_logical_or_expr == truthvalue_false_node));
9112 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9113 }
9114
9115 /* The next token should be a `:'. */
9116 cp_parser_require (parser, CPP_COLON, RT_COLON);
9117 /* Parse the assignment-expression. */
9118 assignment_expr = cp_parser_assignment_expression (parser);
9119 c_inhibit_evaluation_warnings -=
9120 folded_logical_or_expr == truthvalue_true_node;
9121
9122 /* Make a location:
9123 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9124 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9125 with the caret at the "?", ranging from the start of
9126 the logical_or_expr to the end of the assignment_expr. */
9127 loc = make_location (loc,
9128 logical_or_expr.get_start (),
9129 assignment_expr.get_finish ());
9130
9131 /* Build the conditional-expression. */
9132 return build_x_conditional_expr (loc, logical_or_expr,
9133 expr,
9134 assignment_expr,
9135 tf_warning_or_error);
9136 }
9137
9138 /* Parse an assignment-expression.
9139
9140 assignment-expression:
9141 conditional-expression
9142 logical-or-expression assignment-operator assignment_expression
9143 throw-expression
9144
9145 CAST_P is true if this expression is the target of a cast.
9146 DECLTYPE_P is true if this expression is the operand of decltype.
9147
9148 Returns a representation for the expression. */
9149
9150 static cp_expr
9151 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9152 bool cast_p, bool decltype_p)
9153 {
9154 cp_expr expr;
9155
9156 /* If the next token is the `throw' keyword, then we're looking at
9157 a throw-expression. */
9158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9159 expr = cp_parser_throw_expression (parser);
9160 /* Otherwise, it must be that we are looking at a
9161 logical-or-expression. */
9162 else
9163 {
9164 /* Parse the binary expressions (logical-or-expression). */
9165 expr = cp_parser_binary_expression (parser, cast_p, false,
9166 decltype_p,
9167 PREC_NOT_OPERATOR, pidk);
9168 /* If the next token is a `?' then we're actually looking at a
9169 conditional-expression. */
9170 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9171 return cp_parser_question_colon_clause (parser, expr);
9172 else
9173 {
9174 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9175
9176 /* If it's an assignment-operator, we're using the second
9177 production. */
9178 enum tree_code assignment_operator
9179 = cp_parser_assignment_operator_opt (parser);
9180 if (assignment_operator != ERROR_MARK)
9181 {
9182 bool non_constant_p;
9183
9184 /* Parse the right-hand side of the assignment. */
9185 cp_expr rhs = cp_parser_initializer_clause (parser,
9186 &non_constant_p);
9187
9188 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9189 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9190
9191 /* An assignment may not appear in a
9192 constant-expression. */
9193 if (cp_parser_non_integral_constant_expression (parser,
9194 NIC_ASSIGNMENT))
9195 return error_mark_node;
9196 /* Build the assignment expression. Its default
9197 location:
9198 LHS = RHS
9199 ~~~~^~~~~
9200 is the location of the '=' token as the
9201 caret, ranging from the start of the lhs to the
9202 end of the rhs. */
9203 loc = make_location (loc,
9204 expr.get_start (),
9205 rhs.get_finish ());
9206 expr = build_x_modify_expr (loc, expr,
9207 assignment_operator,
9208 rhs,
9209 complain_flags (decltype_p));
9210 /* TODO: build_x_modify_expr doesn't honor the location,
9211 so we must set it here. */
9212 expr.set_location (loc);
9213 }
9214 }
9215 }
9216
9217 return expr;
9218 }
9219
9220 /* Parse an (optional) assignment-operator.
9221
9222 assignment-operator: one of
9223 = *= /= %= += -= >>= <<= &= ^= |=
9224
9225 GNU Extension:
9226
9227 assignment-operator: one of
9228 <?= >?=
9229
9230 If the next token is an assignment operator, the corresponding tree
9231 code is returned, and the token is consumed. For example, for
9232 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9233 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9234 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9235 operator, ERROR_MARK is returned. */
9236
9237 static enum tree_code
9238 cp_parser_assignment_operator_opt (cp_parser* parser)
9239 {
9240 enum tree_code op;
9241 cp_token *token;
9242
9243 /* Peek at the next token. */
9244 token = cp_lexer_peek_token (parser->lexer);
9245
9246 switch (token->type)
9247 {
9248 case CPP_EQ:
9249 op = NOP_EXPR;
9250 break;
9251
9252 case CPP_MULT_EQ:
9253 op = MULT_EXPR;
9254 break;
9255
9256 case CPP_DIV_EQ:
9257 op = TRUNC_DIV_EXPR;
9258 break;
9259
9260 case CPP_MOD_EQ:
9261 op = TRUNC_MOD_EXPR;
9262 break;
9263
9264 case CPP_PLUS_EQ:
9265 op = PLUS_EXPR;
9266 break;
9267
9268 case CPP_MINUS_EQ:
9269 op = MINUS_EXPR;
9270 break;
9271
9272 case CPP_RSHIFT_EQ:
9273 op = RSHIFT_EXPR;
9274 break;
9275
9276 case CPP_LSHIFT_EQ:
9277 op = LSHIFT_EXPR;
9278 break;
9279
9280 case CPP_AND_EQ:
9281 op = BIT_AND_EXPR;
9282 break;
9283
9284 case CPP_XOR_EQ:
9285 op = BIT_XOR_EXPR;
9286 break;
9287
9288 case CPP_OR_EQ:
9289 op = BIT_IOR_EXPR;
9290 break;
9291
9292 default:
9293 /* Nothing else is an assignment operator. */
9294 op = ERROR_MARK;
9295 }
9296
9297 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9298 if (op != ERROR_MARK
9299 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9300 op = ERROR_MARK;
9301
9302 /* If it was an assignment operator, consume it. */
9303 if (op != ERROR_MARK)
9304 cp_lexer_consume_token (parser->lexer);
9305
9306 return op;
9307 }
9308
9309 /* Parse an expression.
9310
9311 expression:
9312 assignment-expression
9313 expression , assignment-expression
9314
9315 CAST_P is true if this expression is the target of a cast.
9316 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9317 except possibly parenthesized or on the RHS of a comma (N3276).
9318
9319 Returns a representation of the expression. */
9320
9321 static cp_expr
9322 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9323 bool cast_p, bool decltype_p)
9324 {
9325 cp_expr expression = NULL_TREE;
9326 location_t loc = UNKNOWN_LOCATION;
9327
9328 while (true)
9329 {
9330 cp_expr assignment_expression;
9331
9332 /* Parse the next assignment-expression. */
9333 assignment_expression
9334 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9335
9336 /* We don't create a temporary for a call that is the immediate operand
9337 of decltype or on the RHS of a comma. But when we see a comma, we
9338 need to create a temporary for a call on the LHS. */
9339 if (decltype_p && !processing_template_decl
9340 && TREE_CODE (assignment_expression) == CALL_EXPR
9341 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9342 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9343 assignment_expression
9344 = build_cplus_new (TREE_TYPE (assignment_expression),
9345 assignment_expression, tf_warning_or_error);
9346
9347 /* If this is the first assignment-expression, we can just
9348 save it away. */
9349 if (!expression)
9350 expression = assignment_expression;
9351 else
9352 {
9353 /* Create a location with caret at the comma, ranging
9354 from the start of the LHS to the end of the RHS. */
9355 loc = make_location (loc,
9356 expression.get_start (),
9357 assignment_expression.get_finish ());
9358 expression = build_x_compound_expr (loc, expression,
9359 assignment_expression,
9360 complain_flags (decltype_p));
9361 expression.set_location (loc);
9362 }
9363 /* If the next token is not a comma, or we're in a fold-expression, then
9364 we are done with the expression. */
9365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9366 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9367 break;
9368 /* Consume the `,'. */
9369 loc = cp_lexer_peek_token (parser->lexer)->location;
9370 cp_lexer_consume_token (parser->lexer);
9371 /* A comma operator cannot appear in a constant-expression. */
9372 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9373 expression = error_mark_node;
9374 }
9375
9376 return expression;
9377 }
9378
9379 /* Parse a constant-expression.
9380
9381 constant-expression:
9382 conditional-expression
9383
9384 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9385 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9386 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9387 is false, NON_CONSTANT_P should be NULL. */
9388
9389 static cp_expr
9390 cp_parser_constant_expression (cp_parser* parser,
9391 bool allow_non_constant_p,
9392 bool *non_constant_p)
9393 {
9394 bool saved_integral_constant_expression_p;
9395 bool saved_allow_non_integral_constant_expression_p;
9396 bool saved_non_integral_constant_expression_p;
9397 cp_expr expression;
9398
9399 /* It might seem that we could simply parse the
9400 conditional-expression, and then check to see if it were
9401 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9402 one that the compiler can figure out is constant, possibly after
9403 doing some simplifications or optimizations. The standard has a
9404 precise definition of constant-expression, and we must honor
9405 that, even though it is somewhat more restrictive.
9406
9407 For example:
9408
9409 int i[(2, 3)];
9410
9411 is not a legal declaration, because `(2, 3)' is not a
9412 constant-expression. The `,' operator is forbidden in a
9413 constant-expression. However, GCC's constant-folding machinery
9414 will fold this operation to an INTEGER_CST for `3'. */
9415
9416 /* Save the old settings. */
9417 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9418 saved_allow_non_integral_constant_expression_p
9419 = parser->allow_non_integral_constant_expression_p;
9420 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9421 /* We are now parsing a constant-expression. */
9422 parser->integral_constant_expression_p = true;
9423 parser->allow_non_integral_constant_expression_p
9424 = (allow_non_constant_p || cxx_dialect >= cxx11);
9425 parser->non_integral_constant_expression_p = false;
9426 /* Although the grammar says "conditional-expression", we parse an
9427 "assignment-expression", which also permits "throw-expression"
9428 and the use of assignment operators. In the case that
9429 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9430 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9431 actually essential that we look for an assignment-expression.
9432 For example, cp_parser_initializer_clauses uses this function to
9433 determine whether a particular assignment-expression is in fact
9434 constant. */
9435 expression = cp_parser_assignment_expression (parser);
9436 /* Restore the old settings. */
9437 parser->integral_constant_expression_p
9438 = saved_integral_constant_expression_p;
9439 parser->allow_non_integral_constant_expression_p
9440 = saved_allow_non_integral_constant_expression_p;
9441 if (cxx_dialect >= cxx11)
9442 {
9443 /* Require an rvalue constant expression here; that's what our
9444 callers expect. Reference constant expressions are handled
9445 separately in e.g. cp_parser_template_argument. */
9446 bool is_const = potential_rvalue_constant_expression (expression);
9447 parser->non_integral_constant_expression_p = !is_const;
9448 if (!is_const && !allow_non_constant_p)
9449 require_potential_rvalue_constant_expression (expression);
9450 }
9451 if (allow_non_constant_p)
9452 *non_constant_p = parser->non_integral_constant_expression_p;
9453 parser->non_integral_constant_expression_p
9454 = saved_non_integral_constant_expression_p;
9455
9456 return expression;
9457 }
9458
9459 /* Parse __builtin_offsetof.
9460
9461 offsetof-expression:
9462 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9463
9464 offsetof-member-designator:
9465 id-expression
9466 | offsetof-member-designator "." id-expression
9467 | offsetof-member-designator "[" expression "]"
9468 | offsetof-member-designator "->" id-expression */
9469
9470 static cp_expr
9471 cp_parser_builtin_offsetof (cp_parser *parser)
9472 {
9473 int save_ice_p, save_non_ice_p;
9474 tree type;
9475 cp_expr expr;
9476 cp_id_kind dummy;
9477 cp_token *token;
9478 location_t finish_loc;
9479
9480 /* We're about to accept non-integral-constant things, but will
9481 definitely yield an integral constant expression. Save and
9482 restore these values around our local parsing. */
9483 save_ice_p = parser->integral_constant_expression_p;
9484 save_non_ice_p = parser->non_integral_constant_expression_p;
9485
9486 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9487
9488 /* Consume the "__builtin_offsetof" token. */
9489 cp_lexer_consume_token (parser->lexer);
9490 /* Consume the opening `('. */
9491 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9492 /* Parse the type-id. */
9493 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9494 type = cp_parser_type_id (parser);
9495 /* Look for the `,'. */
9496 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9497 token = cp_lexer_peek_token (parser->lexer);
9498
9499 /* Build the (type *)null that begins the traditional offsetof macro. */
9500 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9501 tf_warning_or_error);
9502
9503 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9504 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9505 true, &dummy, token->location);
9506 while (true)
9507 {
9508 token = cp_lexer_peek_token (parser->lexer);
9509 switch (token->type)
9510 {
9511 case CPP_OPEN_SQUARE:
9512 /* offsetof-member-designator "[" expression "]" */
9513 expr = cp_parser_postfix_open_square_expression (parser, expr,
9514 true, false);
9515 break;
9516
9517 case CPP_DEREF:
9518 /* offsetof-member-designator "->" identifier */
9519 expr = grok_array_decl (token->location, expr,
9520 integer_zero_node, false);
9521 /* FALLTHRU */
9522
9523 case CPP_DOT:
9524 /* offsetof-member-designator "." identifier */
9525 cp_lexer_consume_token (parser->lexer);
9526 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9527 expr, true, &dummy,
9528 token->location);
9529 break;
9530
9531 case CPP_CLOSE_PAREN:
9532 /* Consume the ")" token. */
9533 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9534 cp_lexer_consume_token (parser->lexer);
9535 goto success;
9536
9537 default:
9538 /* Error. We know the following require will fail, but
9539 that gives the proper error message. */
9540 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9541 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9542 expr = error_mark_node;
9543 goto failure;
9544 }
9545 }
9546
9547 success:
9548 /* Make a location of the form:
9549 __builtin_offsetof (struct s, f)
9550 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9551 with caret at the type-id, ranging from the start of the
9552 "_builtin_offsetof" token to the close paren. */
9553 loc = make_location (loc, start_loc, finish_loc);
9554 /* The result will be an INTEGER_CST, so we need to explicitly
9555 preserve the location. */
9556 expr = cp_expr (finish_offsetof (expr, loc), loc);
9557
9558 failure:
9559 parser->integral_constant_expression_p = save_ice_p;
9560 parser->non_integral_constant_expression_p = save_non_ice_p;
9561
9562 return expr;
9563 }
9564
9565 /* Parse a trait expression.
9566
9567 Returns a representation of the expression, the underlying type
9568 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9569
9570 static tree
9571 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9572 {
9573 cp_trait_kind kind;
9574 tree type1, type2 = NULL_TREE;
9575 bool binary = false;
9576 bool variadic = false;
9577
9578 switch (keyword)
9579 {
9580 case RID_HAS_NOTHROW_ASSIGN:
9581 kind = CPTK_HAS_NOTHROW_ASSIGN;
9582 break;
9583 case RID_HAS_NOTHROW_CONSTRUCTOR:
9584 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9585 break;
9586 case RID_HAS_NOTHROW_COPY:
9587 kind = CPTK_HAS_NOTHROW_COPY;
9588 break;
9589 case RID_HAS_TRIVIAL_ASSIGN:
9590 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9591 break;
9592 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9593 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9594 break;
9595 case RID_HAS_TRIVIAL_COPY:
9596 kind = CPTK_HAS_TRIVIAL_COPY;
9597 break;
9598 case RID_HAS_TRIVIAL_DESTRUCTOR:
9599 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9600 break;
9601 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9602 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9603 break;
9604 case RID_HAS_VIRTUAL_DESTRUCTOR:
9605 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9606 break;
9607 case RID_IS_ABSTRACT:
9608 kind = CPTK_IS_ABSTRACT;
9609 break;
9610 case RID_IS_BASE_OF:
9611 kind = CPTK_IS_BASE_OF;
9612 binary = true;
9613 break;
9614 case RID_IS_CLASS:
9615 kind = CPTK_IS_CLASS;
9616 break;
9617 case RID_IS_EMPTY:
9618 kind = CPTK_IS_EMPTY;
9619 break;
9620 case RID_IS_ENUM:
9621 kind = CPTK_IS_ENUM;
9622 break;
9623 case RID_IS_FINAL:
9624 kind = CPTK_IS_FINAL;
9625 break;
9626 case RID_IS_LITERAL_TYPE:
9627 kind = CPTK_IS_LITERAL_TYPE;
9628 break;
9629 case RID_IS_POD:
9630 kind = CPTK_IS_POD;
9631 break;
9632 case RID_IS_POLYMORPHIC:
9633 kind = CPTK_IS_POLYMORPHIC;
9634 break;
9635 case RID_IS_SAME_AS:
9636 kind = CPTK_IS_SAME_AS;
9637 binary = true;
9638 break;
9639 case RID_IS_STD_LAYOUT:
9640 kind = CPTK_IS_STD_LAYOUT;
9641 break;
9642 case RID_IS_TRIVIAL:
9643 kind = CPTK_IS_TRIVIAL;
9644 break;
9645 case RID_IS_TRIVIALLY_ASSIGNABLE:
9646 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9647 binary = true;
9648 break;
9649 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9650 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9651 variadic = true;
9652 break;
9653 case RID_IS_TRIVIALLY_COPYABLE:
9654 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9655 break;
9656 case RID_IS_UNION:
9657 kind = CPTK_IS_UNION;
9658 break;
9659 case RID_UNDERLYING_TYPE:
9660 kind = CPTK_UNDERLYING_TYPE;
9661 break;
9662 case RID_BASES:
9663 kind = CPTK_BASES;
9664 break;
9665 case RID_DIRECT_BASES:
9666 kind = CPTK_DIRECT_BASES;
9667 break;
9668 default:
9669 gcc_unreachable ();
9670 }
9671
9672 /* Consume the token. */
9673 cp_lexer_consume_token (parser->lexer);
9674
9675 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9676
9677 {
9678 type_id_in_expr_sentinel s (parser);
9679 type1 = cp_parser_type_id (parser);
9680 }
9681
9682 if (type1 == error_mark_node)
9683 return error_mark_node;
9684
9685 if (binary)
9686 {
9687 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9688
9689 {
9690 type_id_in_expr_sentinel s (parser);
9691 type2 = cp_parser_type_id (parser);
9692 }
9693
9694 if (type2 == error_mark_node)
9695 return error_mark_node;
9696 }
9697 else if (variadic)
9698 {
9699 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9700 {
9701 cp_lexer_consume_token (parser->lexer);
9702 tree elt = cp_parser_type_id (parser);
9703 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9704 {
9705 cp_lexer_consume_token (parser->lexer);
9706 elt = make_pack_expansion (elt);
9707 }
9708 if (elt == error_mark_node)
9709 return error_mark_node;
9710 type2 = tree_cons (NULL_TREE, elt, type2);
9711 }
9712 }
9713
9714 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9715
9716 /* Complete the trait expression, which may mean either processing
9717 the trait expr now or saving it for template instantiation. */
9718 switch (kind)
9719 {
9720 case CPTK_UNDERLYING_TYPE:
9721 return finish_underlying_type (type1);
9722 case CPTK_BASES:
9723 return finish_bases (type1, false);
9724 case CPTK_DIRECT_BASES:
9725 return finish_bases (type1, true);
9726 default:
9727 return finish_trait_expr (kind, type1, type2);
9728 }
9729 }
9730
9731 /* Lambdas that appear in variable initializer or default argument scope
9732 get that in their mangling, so we need to record it. We might as well
9733 use the count for function and namespace scopes as well. */
9734 static GTY(()) tree lambda_scope;
9735 static GTY(()) int lambda_count;
9736 struct GTY(()) tree_int
9737 {
9738 tree t;
9739 int i;
9740 };
9741 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9742
9743 static void
9744 start_lambda_scope (tree decl)
9745 {
9746 tree_int ti;
9747 gcc_assert (decl);
9748 /* Once we're inside a function, we ignore other scopes and just push
9749 the function again so that popping works properly. */
9750 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9751 decl = current_function_decl;
9752 ti.t = lambda_scope;
9753 ti.i = lambda_count;
9754 vec_safe_push (lambda_scope_stack, ti);
9755 if (lambda_scope != decl)
9756 {
9757 /* Don't reset the count if we're still in the same function. */
9758 lambda_scope = decl;
9759 lambda_count = 0;
9760 }
9761 }
9762
9763 static void
9764 record_lambda_scope (tree lambda)
9765 {
9766 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9767 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9768 }
9769
9770 static void
9771 finish_lambda_scope (void)
9772 {
9773 tree_int *p = &lambda_scope_stack->last ();
9774 if (lambda_scope != p->t)
9775 {
9776 lambda_scope = p->t;
9777 lambda_count = p->i;
9778 }
9779 lambda_scope_stack->pop ();
9780 }
9781
9782 /* Parse a lambda expression.
9783
9784 lambda-expression:
9785 lambda-introducer lambda-declarator [opt] compound-statement
9786
9787 Returns a representation of the expression. */
9788
9789 static cp_expr
9790 cp_parser_lambda_expression (cp_parser* parser)
9791 {
9792 tree lambda_expr = build_lambda_expr ();
9793 tree type;
9794 bool ok = true;
9795 cp_token *token = cp_lexer_peek_token (parser->lexer);
9796 cp_token_position start = 0;
9797
9798 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9799
9800 if (cp_unevaluated_operand)
9801 {
9802 if (!token->error_reported)
9803 {
9804 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9805 "lambda-expression in unevaluated context");
9806 token->error_reported = true;
9807 }
9808 ok = false;
9809 }
9810 else if (parser->in_template_argument_list_p)
9811 {
9812 if (!token->error_reported)
9813 {
9814 error_at (token->location, "lambda-expression in template-argument");
9815 token->error_reported = true;
9816 }
9817 ok = false;
9818 }
9819
9820 /* We may be in the middle of deferred access check. Disable
9821 it now. */
9822 push_deferring_access_checks (dk_no_deferred);
9823
9824 cp_parser_lambda_introducer (parser, lambda_expr);
9825
9826 type = begin_lambda_type (lambda_expr);
9827 if (type == error_mark_node)
9828 return error_mark_node;
9829
9830 record_lambda_scope (lambda_expr);
9831
9832 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9833 determine_visibility (TYPE_NAME (type));
9834
9835 /* Now that we've started the type, add the capture fields for any
9836 explicit captures. */
9837 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9838
9839 {
9840 /* Inside the class, surrounding template-parameter-lists do not apply. */
9841 unsigned int saved_num_template_parameter_lists
9842 = parser->num_template_parameter_lists;
9843 unsigned char in_statement = parser->in_statement;
9844 bool in_switch_statement_p = parser->in_switch_statement_p;
9845 bool fully_implicit_function_template_p
9846 = parser->fully_implicit_function_template_p;
9847 tree implicit_template_parms = parser->implicit_template_parms;
9848 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9849 bool auto_is_implicit_function_template_parm_p
9850 = parser->auto_is_implicit_function_template_parm_p;
9851
9852 parser->num_template_parameter_lists = 0;
9853 parser->in_statement = 0;
9854 parser->in_switch_statement_p = false;
9855 parser->fully_implicit_function_template_p = false;
9856 parser->implicit_template_parms = 0;
9857 parser->implicit_template_scope = 0;
9858 parser->auto_is_implicit_function_template_parm_p = false;
9859
9860 /* By virtue of defining a local class, a lambda expression has access to
9861 the private variables of enclosing classes. */
9862
9863 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9864
9865 if (ok && cp_parser_error_occurred (parser))
9866 ok = false;
9867
9868 if (ok)
9869 {
9870 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9871 && cp_parser_start_tentative_firewall (parser))
9872 start = token;
9873 cp_parser_lambda_body (parser, lambda_expr);
9874 }
9875 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9876 {
9877 if (cp_parser_skip_to_closing_brace (parser))
9878 cp_lexer_consume_token (parser->lexer);
9879 }
9880
9881 /* The capture list was built up in reverse order; fix that now. */
9882 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9883 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9884
9885 if (ok)
9886 maybe_add_lambda_conv_op (type);
9887
9888 type = finish_struct (type, /*attributes=*/NULL_TREE);
9889
9890 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9891 parser->in_statement = in_statement;
9892 parser->in_switch_statement_p = in_switch_statement_p;
9893 parser->fully_implicit_function_template_p
9894 = fully_implicit_function_template_p;
9895 parser->implicit_template_parms = implicit_template_parms;
9896 parser->implicit_template_scope = implicit_template_scope;
9897 parser->auto_is_implicit_function_template_parm_p
9898 = auto_is_implicit_function_template_parm_p;
9899 }
9900
9901 /* This field is only used during parsing of the lambda. */
9902 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9903
9904 /* This lambda shouldn't have any proxies left at this point. */
9905 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9906 /* And now that we're done, push proxies for an enclosing lambda. */
9907 insert_pending_capture_proxies ();
9908
9909 if (ok)
9910 lambda_expr = build_lambda_object (lambda_expr);
9911 else
9912 lambda_expr = error_mark_node;
9913
9914 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9915
9916 pop_deferring_access_checks ();
9917
9918 return lambda_expr;
9919 }
9920
9921 /* Parse the beginning of a lambda expression.
9922
9923 lambda-introducer:
9924 [ lambda-capture [opt] ]
9925
9926 LAMBDA_EXPR is the current representation of the lambda expression. */
9927
9928 static void
9929 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9930 {
9931 /* Need commas after the first capture. */
9932 bool first = true;
9933
9934 /* Eat the leading `['. */
9935 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9936
9937 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9938 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9939 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9940 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9941 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9942 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9943
9944 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9945 {
9946 cp_lexer_consume_token (parser->lexer);
9947 first = false;
9948 }
9949
9950 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9951 {
9952 cp_token* capture_token;
9953 tree capture_id;
9954 tree capture_init_expr;
9955 cp_id_kind idk = CP_ID_KIND_NONE;
9956 bool explicit_init_p = false;
9957
9958 enum capture_kind_type
9959 {
9960 BY_COPY,
9961 BY_REFERENCE
9962 };
9963 enum capture_kind_type capture_kind = BY_COPY;
9964
9965 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9966 {
9967 error ("expected end of capture-list");
9968 return;
9969 }
9970
9971 if (first)
9972 first = false;
9973 else
9974 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9975
9976 /* Possibly capture `this'. */
9977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9978 {
9979 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9980 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9981 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9982 "with by-copy capture default");
9983 cp_lexer_consume_token (parser->lexer);
9984 add_capture (lambda_expr,
9985 /*id=*/this_identifier,
9986 /*initializer=*/finish_this_expr (),
9987 /*by_reference_p=*/true,
9988 explicit_init_p);
9989 continue;
9990 }
9991
9992 /* Possibly capture `*this'. */
9993 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
9994 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
9995 {
9996 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9997 if (cxx_dialect < cxx1z)
9998 pedwarn (loc, 0, "%<*this%> capture only available with "
9999 "-std=c++1z or -std=gnu++1z");
10000 cp_lexer_consume_token (parser->lexer);
10001 cp_lexer_consume_token (parser->lexer);
10002 add_capture (lambda_expr,
10003 /*id=*/this_identifier,
10004 /*initializer=*/finish_this_expr (),
10005 /*by_reference_p=*/false,
10006 explicit_init_p);
10007 continue;
10008 }
10009
10010 /* Remember whether we want to capture as a reference or not. */
10011 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10012 {
10013 capture_kind = BY_REFERENCE;
10014 cp_lexer_consume_token (parser->lexer);
10015 }
10016
10017 /* Get the identifier. */
10018 capture_token = cp_lexer_peek_token (parser->lexer);
10019 capture_id = cp_parser_identifier (parser);
10020
10021 if (capture_id == error_mark_node)
10022 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10023 delimiters, but I modified this to stop on unnested ']' as well. It
10024 was already changed to stop on unnested '}', so the
10025 "closing_parenthesis" name is no more misleading with my change. */
10026 {
10027 cp_parser_skip_to_closing_parenthesis (parser,
10028 /*recovering=*/true,
10029 /*or_comma=*/true,
10030 /*consume_paren=*/true);
10031 break;
10032 }
10033
10034 /* Find the initializer for this capture. */
10035 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10036 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10037 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10038 {
10039 bool direct, non_constant;
10040 /* An explicit initializer exists. */
10041 if (cxx_dialect < cxx14)
10042 pedwarn (input_location, 0,
10043 "lambda capture initializers "
10044 "only available with -std=c++14 or -std=gnu++14");
10045 capture_init_expr = cp_parser_initializer (parser, &direct,
10046 &non_constant);
10047 explicit_init_p = true;
10048 if (capture_init_expr == NULL_TREE)
10049 {
10050 error ("empty initializer for lambda init-capture");
10051 capture_init_expr = error_mark_node;
10052 }
10053 }
10054 else
10055 {
10056 const char* error_msg;
10057
10058 /* Turn the identifier into an id-expression. */
10059 capture_init_expr
10060 = cp_parser_lookup_name_simple (parser, capture_id,
10061 capture_token->location);
10062
10063 if (capture_init_expr == error_mark_node)
10064 {
10065 unqualified_name_lookup_error (capture_id);
10066 continue;
10067 }
10068 else if (DECL_P (capture_init_expr)
10069 && (!VAR_P (capture_init_expr)
10070 && TREE_CODE (capture_init_expr) != PARM_DECL))
10071 {
10072 error_at (capture_token->location,
10073 "capture of non-variable %qD ",
10074 capture_init_expr);
10075 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10076 "%q#D declared here", capture_init_expr);
10077 continue;
10078 }
10079 if (VAR_P (capture_init_expr)
10080 && decl_storage_duration (capture_init_expr) != dk_auto)
10081 {
10082 if (pedwarn (capture_token->location, 0, "capture of variable "
10083 "%qD with non-automatic storage duration",
10084 capture_init_expr))
10085 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10086 "%q#D declared here", capture_init_expr);
10087 continue;
10088 }
10089
10090 capture_init_expr
10091 = finish_id_expression
10092 (capture_id,
10093 capture_init_expr,
10094 parser->scope,
10095 &idk,
10096 /*integral_constant_expression_p=*/false,
10097 /*allow_non_integral_constant_expression_p=*/false,
10098 /*non_integral_constant_expression_p=*/NULL,
10099 /*template_p=*/false,
10100 /*done=*/true,
10101 /*address_p=*/false,
10102 /*template_arg_p=*/false,
10103 &error_msg,
10104 capture_token->location);
10105
10106 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10107 {
10108 cp_lexer_consume_token (parser->lexer);
10109 capture_init_expr = make_pack_expansion (capture_init_expr);
10110 }
10111 else
10112 check_for_bare_parameter_packs (capture_init_expr);
10113 }
10114
10115 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10116 && !explicit_init_p)
10117 {
10118 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10119 && capture_kind == BY_COPY)
10120 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10121 "of %qD redundant with by-copy capture default",
10122 capture_id);
10123 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10124 && capture_kind == BY_REFERENCE)
10125 pedwarn (capture_token->location, 0, "explicit by-reference "
10126 "capture of %qD redundant with by-reference capture "
10127 "default", capture_id);
10128 }
10129
10130 add_capture (lambda_expr,
10131 capture_id,
10132 capture_init_expr,
10133 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10134 explicit_init_p);
10135 }
10136
10137 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10138 }
10139
10140 /* Parse the (optional) middle of a lambda expression.
10141
10142 lambda-declarator:
10143 < template-parameter-list [opt] >
10144 ( parameter-declaration-clause [opt] )
10145 attribute-specifier [opt]
10146 decl-specifier-seq [opt]
10147 exception-specification [opt]
10148 lambda-return-type-clause [opt]
10149
10150 LAMBDA_EXPR is the current representation of the lambda expression. */
10151
10152 static bool
10153 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10154 {
10155 /* 5.1.1.4 of the standard says:
10156 If a lambda-expression does not include a lambda-declarator, it is as if
10157 the lambda-declarator were ().
10158 This means an empty parameter list, no attributes, and no exception
10159 specification. */
10160 tree param_list = void_list_node;
10161 tree attributes = NULL_TREE;
10162 tree exception_spec = NULL_TREE;
10163 tree template_param_list = NULL_TREE;
10164 tree tx_qual = NULL_TREE;
10165 cp_decl_specifier_seq lambda_specs;
10166 clear_decl_specs (&lambda_specs);
10167
10168 /* The template-parameter-list is optional, but must begin with
10169 an opening angle if present. */
10170 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10171 {
10172 if (cxx_dialect < cxx14)
10173 pedwarn (parser->lexer->next_token->location, 0,
10174 "lambda templates are only available with "
10175 "-std=c++14 or -std=gnu++14");
10176
10177 cp_lexer_consume_token (parser->lexer);
10178
10179 template_param_list = cp_parser_template_parameter_list (parser);
10180
10181 cp_parser_skip_to_end_of_template_parameter_list (parser);
10182
10183 /* We just processed one more parameter list. */
10184 ++parser->num_template_parameter_lists;
10185 }
10186
10187 /* The parameter-declaration-clause is optional (unless
10188 template-parameter-list was given), but must begin with an
10189 opening parenthesis if present. */
10190 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10191 {
10192 cp_lexer_consume_token (parser->lexer);
10193
10194 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10195
10196 /* Parse parameters. */
10197 param_list = cp_parser_parameter_declaration_clause (parser);
10198
10199 /* Default arguments shall not be specified in the
10200 parameter-declaration-clause of a lambda-declarator. */
10201 if (cxx_dialect < cxx14)
10202 for (tree t = param_list; t; t = TREE_CHAIN (t))
10203 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10204 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10205 "default argument specified for lambda parameter");
10206
10207 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10208
10209 attributes = cp_parser_attributes_opt (parser);
10210
10211 /* In the decl-specifier-seq of the lambda-declarator, each
10212 decl-specifier shall either be mutable or constexpr. */
10213 int declares_class_or_enum;
10214 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10215 cp_parser_decl_specifier_seq (parser,
10216 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10217 &lambda_specs, &declares_class_or_enum);
10218 if (lambda_specs.storage_class == sc_mutable)
10219 {
10220 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10221 if (lambda_specs.conflicting_specifiers_p)
10222 error_at (lambda_specs.locations[ds_storage_class],
10223 "duplicate %<mutable%>");
10224 }
10225
10226 tx_qual = cp_parser_tx_qualifier_opt (parser);
10227
10228 /* Parse optional exception specification. */
10229 exception_spec = cp_parser_exception_specification_opt (parser);
10230
10231 /* Parse optional trailing return type. */
10232 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10233 {
10234 cp_lexer_consume_token (parser->lexer);
10235 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10236 = cp_parser_trailing_type_id (parser);
10237 }
10238
10239 /* The function parameters must be in scope all the way until after the
10240 trailing-return-type in case of decltype. */
10241 pop_bindings_and_leave_scope ();
10242 }
10243 else if (template_param_list != NULL_TREE) // generate diagnostic
10244 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10245
10246 /* Create the function call operator.
10247
10248 Messing with declarators like this is no uglier than building up the
10249 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10250 other code. */
10251 {
10252 cp_decl_specifier_seq return_type_specs;
10253 cp_declarator* declarator;
10254 tree fco;
10255 int quals;
10256 void *p;
10257
10258 clear_decl_specs (&return_type_specs);
10259 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10260 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10261 else
10262 /* Maybe we will deduce the return type later. */
10263 return_type_specs.type = make_auto ();
10264
10265 if (lambda_specs.locations[ds_constexpr])
10266 {
10267 if (cxx_dialect >= cxx1z)
10268 return_type_specs.locations[ds_constexpr]
10269 = lambda_specs.locations[ds_constexpr];
10270 else
10271 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10272 "lambda only available with -std=c++1z or -std=gnu++1z");
10273 }
10274
10275 p = obstack_alloc (&declarator_obstack, 0);
10276
10277 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10278 sfk_none);
10279
10280 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10281 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10282 declarator = make_call_declarator (declarator, param_list, quals,
10283 VIRT_SPEC_UNSPECIFIED,
10284 REF_QUAL_NONE,
10285 tx_qual,
10286 exception_spec,
10287 /*late_return_type=*/NULL_TREE,
10288 /*requires_clause*/NULL_TREE);
10289 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10290
10291 fco = grokmethod (&return_type_specs,
10292 declarator,
10293 attributes);
10294 if (fco != error_mark_node)
10295 {
10296 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10297 DECL_ARTIFICIAL (fco) = 1;
10298 /* Give the object parameter a different name. */
10299 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10300 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10301 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10302 }
10303 if (template_param_list)
10304 {
10305 fco = finish_member_template_decl (fco);
10306 finish_template_decl (template_param_list);
10307 --parser->num_template_parameter_lists;
10308 }
10309 else if (parser->fully_implicit_function_template_p)
10310 fco = finish_fully_implicit_template (parser, fco);
10311
10312 finish_member_declaration (fco);
10313
10314 obstack_free (&declarator_obstack, p);
10315
10316 return (fco != error_mark_node);
10317 }
10318 }
10319
10320 /* Parse the body of a lambda expression, which is simply
10321
10322 compound-statement
10323
10324 but which requires special handling.
10325 LAMBDA_EXPR is the current representation of the lambda expression. */
10326
10327 static void
10328 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10329 {
10330 bool nested = (current_function_decl != NULL_TREE);
10331 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10332 if (nested)
10333 push_function_context ();
10334 else
10335 /* Still increment function_depth so that we don't GC in the
10336 middle of an expression. */
10337 ++function_depth;
10338 vec<tree> omp_privatization_save;
10339 save_omp_privatization_clauses (omp_privatization_save);
10340 /* Clear this in case we're in the middle of a default argument. */
10341 parser->local_variables_forbidden_p = false;
10342
10343 /* Finish the function call operator
10344 - class_specifier
10345 + late_parsing_for_member
10346 + function_definition_after_declarator
10347 + ctor_initializer_opt_and_function_body */
10348 {
10349 tree fco = lambda_function (lambda_expr);
10350 tree body;
10351 bool done = false;
10352 tree compound_stmt;
10353 tree cap;
10354
10355 /* Let the front end know that we are going to be defining this
10356 function. */
10357 start_preparsed_function (fco,
10358 NULL_TREE,
10359 SF_PRE_PARSED | SF_INCLASS_INLINE);
10360
10361 start_lambda_scope (fco);
10362 body = begin_function_body ();
10363
10364 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10365 goto out;
10366
10367 /* Push the proxies for any explicit captures. */
10368 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10369 cap = TREE_CHAIN (cap))
10370 build_capture_proxy (TREE_PURPOSE (cap));
10371
10372 compound_stmt = begin_compound_stmt (0);
10373
10374 /* 5.1.1.4 of the standard says:
10375 If a lambda-expression does not include a trailing-return-type, it
10376 is as if the trailing-return-type denotes the following type:
10377 * if the compound-statement is of the form
10378 { return attribute-specifier [opt] expression ; }
10379 the type of the returned expression after lvalue-to-rvalue
10380 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10381 (_conv.array_ 4.2), and function-to-pointer conversion
10382 (_conv.func_ 4.3);
10383 * otherwise, void. */
10384
10385 /* In a lambda that has neither a lambda-return-type-clause
10386 nor a deducible form, errors should be reported for return statements
10387 in the body. Since we used void as the placeholder return type, parsing
10388 the body as usual will give such desired behavior. */
10389 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10390 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10391 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10392 {
10393 tree expr = NULL_TREE;
10394 cp_id_kind idk = CP_ID_KIND_NONE;
10395
10396 /* Parse tentatively in case there's more after the initial return
10397 statement. */
10398 cp_parser_parse_tentatively (parser);
10399
10400 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10401
10402 expr = cp_parser_expression (parser, &idk);
10403
10404 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10405 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10406
10407 if (cp_parser_parse_definitely (parser))
10408 {
10409 if (!processing_template_decl)
10410 {
10411 tree type = lambda_return_type (expr);
10412 apply_deduced_return_type (fco, type);
10413 if (type == error_mark_node)
10414 expr = error_mark_node;
10415 }
10416
10417 /* Will get error here if type not deduced yet. */
10418 finish_return_stmt (expr);
10419
10420 done = true;
10421 }
10422 }
10423
10424 if (!done)
10425 {
10426 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10427 cp_parser_label_declaration (parser);
10428 cp_parser_statement_seq_opt (parser, NULL_TREE);
10429 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10430 }
10431
10432 finish_compound_stmt (compound_stmt);
10433
10434 out:
10435 finish_function_body (body);
10436 finish_lambda_scope ();
10437
10438 /* Finish the function and generate code for it if necessary. */
10439 tree fn = finish_function (/*inline*/2);
10440
10441 /* Only expand if the call op is not a template. */
10442 if (!DECL_TEMPLATE_INFO (fco))
10443 expand_or_defer_fn (fn);
10444 }
10445
10446 restore_omp_privatization_clauses (omp_privatization_save);
10447 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10448 if (nested)
10449 pop_function_context();
10450 else
10451 --function_depth;
10452 }
10453
10454 /* Statements [gram.stmt.stmt] */
10455
10456 /* Parse a statement.
10457
10458 statement:
10459 labeled-statement
10460 expression-statement
10461 compound-statement
10462 selection-statement
10463 iteration-statement
10464 jump-statement
10465 declaration-statement
10466 try-block
10467
10468 C++11:
10469
10470 statement:
10471 labeled-statement
10472 attribute-specifier-seq (opt) expression-statement
10473 attribute-specifier-seq (opt) compound-statement
10474 attribute-specifier-seq (opt) selection-statement
10475 attribute-specifier-seq (opt) iteration-statement
10476 attribute-specifier-seq (opt) jump-statement
10477 declaration-statement
10478 attribute-specifier-seq (opt) try-block
10479
10480 init-statement:
10481 expression-statement
10482 simple-declaration
10483
10484 TM Extension:
10485
10486 statement:
10487 atomic-statement
10488
10489 IN_COMPOUND is true when the statement is nested inside a
10490 cp_parser_compound_statement; this matters for certain pragmas.
10491
10492 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10493 is a (possibly labeled) if statement which is not enclosed in braces
10494 and has an else clause. This is used to implement -Wparentheses.
10495
10496 CHAIN is a vector of if-else-if conditions. */
10497
10498 static void
10499 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10500 bool in_compound, bool *if_p, vec<tree> *chain)
10501 {
10502 tree statement, std_attrs = NULL_TREE;
10503 cp_token *token;
10504 location_t statement_location, attrs_location;
10505
10506 restart:
10507 if (if_p != NULL)
10508 *if_p = false;
10509 /* There is no statement yet. */
10510 statement = NULL_TREE;
10511
10512 saved_token_sentinel saved_tokens (parser->lexer);
10513 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10514 if (c_dialect_objc ())
10515 /* In obj-c++, seeing '[[' might be the either the beginning of
10516 c++11 attributes, or a nested objc-message-expression. So
10517 let's parse the c++11 attributes tentatively. */
10518 cp_parser_parse_tentatively (parser);
10519 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10520 if (c_dialect_objc ())
10521 {
10522 if (!cp_parser_parse_definitely (parser))
10523 std_attrs = NULL_TREE;
10524 }
10525
10526 /* Peek at the next token. */
10527 token = cp_lexer_peek_token (parser->lexer);
10528 /* Remember the location of the first token in the statement. */
10529 statement_location = token->location;
10530 /* If this is a keyword, then that will often determine what kind of
10531 statement we have. */
10532 if (token->type == CPP_KEYWORD)
10533 {
10534 enum rid keyword = token->keyword;
10535
10536 switch (keyword)
10537 {
10538 case RID_CASE:
10539 case RID_DEFAULT:
10540 /* Looks like a labeled-statement with a case label.
10541 Parse the label, and then use tail recursion to parse
10542 the statement. */
10543 cp_parser_label_for_labeled_statement (parser, std_attrs);
10544 in_compound = false;
10545 goto restart;
10546
10547 case RID_IF:
10548 case RID_SWITCH:
10549 statement = cp_parser_selection_statement (parser, if_p, chain);
10550 break;
10551
10552 case RID_WHILE:
10553 case RID_DO:
10554 case RID_FOR:
10555 statement = cp_parser_iteration_statement (parser, if_p, false);
10556 break;
10557
10558 case RID_CILK_FOR:
10559 if (!flag_cilkplus)
10560 {
10561 error_at (cp_lexer_peek_token (parser->lexer)->location,
10562 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10563 cp_lexer_consume_token (parser->lexer);
10564 statement = error_mark_node;
10565 }
10566 else
10567 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10568 break;
10569
10570 case RID_BREAK:
10571 case RID_CONTINUE:
10572 case RID_RETURN:
10573 case RID_GOTO:
10574 statement = cp_parser_jump_statement (parser);
10575 break;
10576
10577 case RID_CILK_SYNC:
10578 cp_lexer_consume_token (parser->lexer);
10579 if (flag_cilkplus)
10580 {
10581 tree sync_expr = build_cilk_sync ();
10582 SET_EXPR_LOCATION (sync_expr,
10583 token->location);
10584 statement = finish_expr_stmt (sync_expr);
10585 }
10586 else
10587 {
10588 error_at (token->location, "-fcilkplus must be enabled to use"
10589 " %<_Cilk_sync%>");
10590 statement = error_mark_node;
10591 }
10592 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10593 break;
10594
10595 /* Objective-C++ exception-handling constructs. */
10596 case RID_AT_TRY:
10597 case RID_AT_CATCH:
10598 case RID_AT_FINALLY:
10599 case RID_AT_SYNCHRONIZED:
10600 case RID_AT_THROW:
10601 statement = cp_parser_objc_statement (parser);
10602 break;
10603
10604 case RID_TRY:
10605 statement = cp_parser_try_block (parser);
10606 break;
10607
10608 case RID_NAMESPACE:
10609 /* This must be a namespace alias definition. */
10610 cp_parser_declaration_statement (parser);
10611 return;
10612
10613 case RID_TRANSACTION_ATOMIC:
10614 case RID_TRANSACTION_RELAXED:
10615 case RID_SYNCHRONIZED:
10616 case RID_ATOMIC_NOEXCEPT:
10617 case RID_ATOMIC_CANCEL:
10618 statement = cp_parser_transaction (parser, token);
10619 break;
10620 case RID_TRANSACTION_CANCEL:
10621 statement = cp_parser_transaction_cancel (parser);
10622 break;
10623
10624 default:
10625 /* It might be a keyword like `int' that can start a
10626 declaration-statement. */
10627 break;
10628 }
10629 }
10630 else if (token->type == CPP_NAME)
10631 {
10632 /* If the next token is a `:', then we are looking at a
10633 labeled-statement. */
10634 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10635 if (token->type == CPP_COLON)
10636 {
10637 /* Looks like a labeled-statement with an ordinary label.
10638 Parse the label, and then use tail recursion to parse
10639 the statement. */
10640
10641 cp_parser_label_for_labeled_statement (parser, std_attrs);
10642 in_compound = false;
10643 goto restart;
10644 }
10645 }
10646 /* Anything that starts with a `{' must be a compound-statement. */
10647 else if (token->type == CPP_OPEN_BRACE)
10648 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10649 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10650 a statement all its own. */
10651 else if (token->type == CPP_PRAGMA)
10652 {
10653 /* Only certain OpenMP pragmas are attached to statements, and thus
10654 are considered statements themselves. All others are not. In
10655 the context of a compound, accept the pragma as a "statement" and
10656 return so that we can check for a close brace. Otherwise we
10657 require a real statement and must go back and read one. */
10658 if (in_compound)
10659 cp_parser_pragma (parser, pragma_compound, if_p);
10660 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10661 goto restart;
10662 return;
10663 }
10664 else if (token->type == CPP_EOF)
10665 {
10666 cp_parser_error (parser, "expected statement");
10667 return;
10668 }
10669
10670 /* Everything else must be a declaration-statement or an
10671 expression-statement. Try for the declaration-statement
10672 first, unless we are looking at a `;', in which case we know that
10673 we have an expression-statement. */
10674 if (!statement)
10675 {
10676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10677 {
10678 if (std_attrs != NULL_TREE)
10679 {
10680 /* Attributes should be parsed as part of the the
10681 declaration, so let's un-parse them. */
10682 saved_tokens.rollback();
10683 std_attrs = NULL_TREE;
10684 }
10685
10686 cp_parser_parse_tentatively (parser);
10687 /* Try to parse the declaration-statement. */
10688 cp_parser_declaration_statement (parser);
10689 /* If that worked, we're done. */
10690 if (cp_parser_parse_definitely (parser))
10691 return;
10692 }
10693 /* Look for an expression-statement instead. */
10694 statement = cp_parser_expression_statement (parser, in_statement_expr);
10695
10696 /* Handle [[fallthrough]];. */
10697 if (attribute_fallthrough_p (std_attrs))
10698 {
10699 /* The next token after the fallthrough attribute is ';'. */
10700 if (statement == NULL_TREE)
10701 {
10702 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10703 statement = build_call_expr_internal_loc (statement_location,
10704 IFN_FALLTHROUGH,
10705 void_type_node, 0);
10706 finish_expr_stmt (statement);
10707 }
10708 else
10709 warning_at (statement_location, OPT_Wattributes,
10710 "%<fallthrough%> attribute not followed by %<;%>");
10711 std_attrs = NULL_TREE;
10712 }
10713 }
10714
10715 /* Set the line number for the statement. */
10716 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10717 SET_EXPR_LOCATION (statement, statement_location);
10718
10719 /* Allow "[[fallthrough]];", but warn otherwise. */
10720 if (std_attrs != NULL_TREE)
10721 warning_at (attrs_location,
10722 OPT_Wattributes,
10723 "attributes at the beginning of statement are ignored");
10724 }
10725
10726 /* Parse the label for a labeled-statement, i.e.
10727
10728 identifier :
10729 case constant-expression :
10730 default :
10731
10732 GNU Extension:
10733 case constant-expression ... constant-expression : statement
10734
10735 When a label is parsed without errors, the label is added to the
10736 parse tree by the finish_* functions, so this function doesn't
10737 have to return the label. */
10738
10739 static void
10740 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10741 {
10742 cp_token *token;
10743 tree label = NULL_TREE;
10744 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10745
10746 /* The next token should be an identifier. */
10747 token = cp_lexer_peek_token (parser->lexer);
10748 if (token->type != CPP_NAME
10749 && token->type != CPP_KEYWORD)
10750 {
10751 cp_parser_error (parser, "expected labeled-statement");
10752 return;
10753 }
10754
10755 /* Remember whether this case or a user-defined label is allowed to fall
10756 through to. */
10757 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10758
10759 parser->colon_corrects_to_scope_p = false;
10760 switch (token->keyword)
10761 {
10762 case RID_CASE:
10763 {
10764 tree expr, expr_hi;
10765 cp_token *ellipsis;
10766
10767 /* Consume the `case' token. */
10768 cp_lexer_consume_token (parser->lexer);
10769 /* Parse the constant-expression. */
10770 expr = cp_parser_constant_expression (parser);
10771 if (check_for_bare_parameter_packs (expr))
10772 expr = error_mark_node;
10773
10774 ellipsis = cp_lexer_peek_token (parser->lexer);
10775 if (ellipsis->type == CPP_ELLIPSIS)
10776 {
10777 /* Consume the `...' token. */
10778 cp_lexer_consume_token (parser->lexer);
10779 expr_hi = cp_parser_constant_expression (parser);
10780 if (check_for_bare_parameter_packs (expr_hi))
10781 expr_hi = error_mark_node;
10782
10783 /* We don't need to emit warnings here, as the common code
10784 will do this for us. */
10785 }
10786 else
10787 expr_hi = NULL_TREE;
10788
10789 if (parser->in_switch_statement_p)
10790 {
10791 tree l = finish_case_label (token->location, expr, expr_hi);
10792 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10793 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10794 }
10795 else
10796 error_at (token->location,
10797 "case label %qE not within a switch statement",
10798 expr);
10799 }
10800 break;
10801
10802 case RID_DEFAULT:
10803 /* Consume the `default' token. */
10804 cp_lexer_consume_token (parser->lexer);
10805
10806 if (parser->in_switch_statement_p)
10807 {
10808 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10809 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10810 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10811 }
10812 else
10813 error_at (token->location, "case label not within a switch statement");
10814 break;
10815
10816 default:
10817 /* Anything else must be an ordinary label. */
10818 label = finish_label_stmt (cp_parser_identifier (parser));
10819 if (label && TREE_CODE (label) == LABEL_DECL)
10820 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10821 break;
10822 }
10823
10824 /* Require the `:' token. */
10825 cp_parser_require (parser, CPP_COLON, RT_COLON);
10826
10827 /* An ordinary label may optionally be followed by attributes.
10828 However, this is only permitted if the attributes are then
10829 followed by a semicolon. This is because, for backward
10830 compatibility, when parsing
10831 lab: __attribute__ ((unused)) int i;
10832 we want the attribute to attach to "i", not "lab". */
10833 if (label != NULL_TREE
10834 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10835 {
10836 tree attrs;
10837 cp_parser_parse_tentatively (parser);
10838 attrs = cp_parser_gnu_attributes_opt (parser);
10839 if (attrs == NULL_TREE
10840 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10841 cp_parser_abort_tentative_parse (parser);
10842 else if (!cp_parser_parse_definitely (parser))
10843 ;
10844 else
10845 attributes = chainon (attributes, attrs);
10846 }
10847
10848 if (attributes != NULL_TREE)
10849 cplus_decl_attributes (&label, attributes, 0);
10850
10851 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10852 }
10853
10854 /* Parse an expression-statement.
10855
10856 expression-statement:
10857 expression [opt] ;
10858
10859 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10860 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10861 indicates whether this expression-statement is part of an
10862 expression statement. */
10863
10864 static tree
10865 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10866 {
10867 tree statement = NULL_TREE;
10868 cp_token *token = cp_lexer_peek_token (parser->lexer);
10869 location_t loc = token->location;
10870
10871 /* There might be attribute fallthrough. */
10872 tree attr = cp_parser_gnu_attributes_opt (parser);
10873
10874 /* If the next token is a ';', then there is no expression
10875 statement. */
10876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10877 {
10878 statement = cp_parser_expression (parser);
10879 if (statement == error_mark_node
10880 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10881 {
10882 cp_parser_skip_to_end_of_block_or_statement (parser);
10883 return error_mark_node;
10884 }
10885 }
10886
10887 /* Handle [[fallthrough]];. */
10888 if (attribute_fallthrough_p (attr))
10889 {
10890 /* The next token after the fallthrough attribute is ';'. */
10891 if (statement == NULL_TREE)
10892 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10893 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10894 void_type_node, 0);
10895 else
10896 warning_at (loc, OPT_Wattributes,
10897 "%<fallthrough%> attribute not followed by %<;%>");
10898 attr = NULL_TREE;
10899 }
10900
10901 /* Allow "[[fallthrough]];", but warn otherwise. */
10902 if (attr != NULL_TREE)
10903 warning_at (loc, OPT_Wattributes,
10904 "attributes at the beginning of statement are ignored");
10905
10906 /* Give a helpful message for "A<T>::type t;" and the like. */
10907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10908 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10909 {
10910 if (TREE_CODE (statement) == SCOPE_REF)
10911 error_at (token->location, "need %<typename%> before %qE because "
10912 "%qT is a dependent scope",
10913 statement, TREE_OPERAND (statement, 0));
10914 else if (is_overloaded_fn (statement)
10915 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10916 {
10917 /* A::A a; */
10918 tree fn = get_first_fn (statement);
10919 error_at (token->location,
10920 "%<%T::%D%> names the constructor, not the type",
10921 DECL_CONTEXT (fn), DECL_NAME (fn));
10922 }
10923 }
10924
10925 /* Consume the final `;'. */
10926 cp_parser_consume_semicolon_at_end_of_statement (parser);
10927
10928 if (in_statement_expr
10929 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10930 /* This is the final expression statement of a statement
10931 expression. */
10932 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10933 else if (statement)
10934 statement = finish_expr_stmt (statement);
10935
10936 return statement;
10937 }
10938
10939 /* Parse a compound-statement.
10940
10941 compound-statement:
10942 { statement-seq [opt] }
10943
10944 GNU extension:
10945
10946 compound-statement:
10947 { label-declaration-seq [opt] statement-seq [opt] }
10948
10949 label-declaration-seq:
10950 label-declaration
10951 label-declaration-seq label-declaration
10952
10953 Returns a tree representing the statement. */
10954
10955 static tree
10956 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10957 int bcs_flags, bool function_body)
10958 {
10959 tree compound_stmt;
10960
10961 /* Consume the `{'. */
10962 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10963 return error_mark_node;
10964 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10965 && !function_body && cxx_dialect < cxx14)
10966 pedwarn (input_location, OPT_Wpedantic,
10967 "compound-statement in constexpr function");
10968 /* Begin the compound-statement. */
10969 compound_stmt = begin_compound_stmt (bcs_flags);
10970 /* If the next keyword is `__label__' we have a label declaration. */
10971 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10972 cp_parser_label_declaration (parser);
10973 /* Parse an (optional) statement-seq. */
10974 cp_parser_statement_seq_opt (parser, in_statement_expr);
10975 /* Finish the compound-statement. */
10976 finish_compound_stmt (compound_stmt);
10977 /* Consume the `}'. */
10978 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10979
10980 return compound_stmt;
10981 }
10982
10983 /* Parse an (optional) statement-seq.
10984
10985 statement-seq:
10986 statement
10987 statement-seq [opt] statement */
10988
10989 static void
10990 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10991 {
10992 /* Scan statements until there aren't any more. */
10993 while (true)
10994 {
10995 cp_token *token = cp_lexer_peek_token (parser->lexer);
10996
10997 /* If we are looking at a `}', then we have run out of
10998 statements; the same is true if we have reached the end
10999 of file, or have stumbled upon a stray '@end'. */
11000 if (token->type == CPP_CLOSE_BRACE
11001 || token->type == CPP_EOF
11002 || token->type == CPP_PRAGMA_EOL
11003 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11004 break;
11005
11006 /* If we are in a compound statement and find 'else' then
11007 something went wrong. */
11008 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11009 {
11010 if (parser->in_statement & IN_IF_STMT)
11011 break;
11012 else
11013 {
11014 token = cp_lexer_consume_token (parser->lexer);
11015 error_at (token->location, "%<else%> without a previous %<if%>");
11016 }
11017 }
11018
11019 /* Parse the statement. */
11020 cp_parser_statement (parser, in_statement_expr, true, NULL);
11021 }
11022 }
11023
11024 /* Return true if we're looking at (init; cond), false otherwise. */
11025
11026 static bool
11027 cp_parser_init_statement_p (cp_parser *parser)
11028 {
11029 /* Save tokens so that we can put them back. */
11030 cp_lexer_save_tokens (parser->lexer);
11031
11032 /* Look for ';' that is not nested in () or {}. */
11033 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11034 /*recovering=*/false,
11035 CPP_SEMICOLON,
11036 /*consume_paren=*/false);
11037
11038 /* Roll back the tokens we skipped. */
11039 cp_lexer_rollback_tokens (parser->lexer);
11040
11041 return ret == -1;
11042 }
11043
11044 /* Parse a selection-statement.
11045
11046 selection-statement:
11047 if ( init-statement [opt] condition ) statement
11048 if ( init-statement [opt] condition ) statement else statement
11049 switch ( init-statement [opt] condition ) statement
11050
11051 Returns the new IF_STMT or SWITCH_STMT.
11052
11053 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11054 is a (possibly labeled) if statement which is not enclosed in
11055 braces and has an else clause. This is used to implement
11056 -Wparentheses.
11057
11058 CHAIN is a vector of if-else-if conditions. This is used to implement
11059 -Wduplicated-cond. */
11060
11061 static tree
11062 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11063 vec<tree> *chain)
11064 {
11065 cp_token *token;
11066 enum rid keyword;
11067 token_indent_info guard_tinfo;
11068
11069 if (if_p != NULL)
11070 *if_p = false;
11071
11072 /* Peek at the next token. */
11073 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11074 guard_tinfo = get_token_indent_info (token);
11075
11076 /* See what kind of keyword it is. */
11077 keyword = token->keyword;
11078 switch (keyword)
11079 {
11080 case RID_IF:
11081 case RID_SWITCH:
11082 {
11083 tree statement;
11084 tree condition;
11085
11086 bool cx = false;
11087 if (keyword == RID_IF
11088 && cp_lexer_next_token_is_keyword (parser->lexer,
11089 RID_CONSTEXPR))
11090 {
11091 cx = true;
11092 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11093 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11094 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11095 "with -std=c++1z or -std=gnu++1z");
11096 }
11097
11098 /* Look for the `('. */
11099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11100 {
11101 cp_parser_skip_to_end_of_statement (parser);
11102 return error_mark_node;
11103 }
11104
11105 /* Begin the selection-statement. */
11106 if (keyword == RID_IF)
11107 {
11108 statement = begin_if_stmt ();
11109 IF_STMT_CONSTEXPR_P (statement) = cx;
11110 }
11111 else
11112 statement = begin_switch_stmt ();
11113
11114 /* Parse the optional init-statement. */
11115 if (cp_parser_init_statement_p (parser))
11116 {
11117 tree decl;
11118 if (cxx_dialect < cxx1z)
11119 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11120 "init-statement in selection statements only available "
11121 "with -std=c++1z or -std=gnu++1z");
11122 cp_parser_init_statement (parser, &decl);
11123 }
11124
11125 /* Parse the condition. */
11126 condition = cp_parser_condition (parser);
11127 /* Look for the `)'. */
11128 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11129 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11130 /*consume_paren=*/true);
11131
11132 if (keyword == RID_IF)
11133 {
11134 bool nested_if;
11135 unsigned char in_statement;
11136
11137 /* Add the condition. */
11138 condition = finish_if_stmt_cond (condition, statement);
11139
11140 if (warn_duplicated_cond)
11141 warn_duplicated_cond_add_or_warn (token->location, condition,
11142 &chain);
11143
11144 /* Parse the then-clause. */
11145 in_statement = parser->in_statement;
11146 parser->in_statement |= IN_IF_STMT;
11147
11148 /* Outside a template, the non-selected branch of a constexpr
11149 if is a 'discarded statement', i.e. unevaluated. */
11150 bool was_discarded = parser->in_discarded_stmt;
11151 bool discard_then = (cx && !processing_template_decl
11152 && integer_zerop (condition));
11153 if (discard_then)
11154 {
11155 parser->in_discarded_stmt = true;
11156 ++c_inhibit_evaluation_warnings;
11157 }
11158
11159 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11160 guard_tinfo);
11161
11162 parser->in_statement = in_statement;
11163
11164 finish_then_clause (statement);
11165
11166 if (discard_then)
11167 {
11168 THEN_CLAUSE (statement) = NULL_TREE;
11169 parser->in_discarded_stmt = was_discarded;
11170 --c_inhibit_evaluation_warnings;
11171 }
11172
11173 /* If the next token is `else', parse the else-clause. */
11174 if (cp_lexer_next_token_is_keyword (parser->lexer,
11175 RID_ELSE))
11176 {
11177 bool discard_else = (cx && !processing_template_decl
11178 && integer_nonzerop (condition));
11179 if (discard_else)
11180 {
11181 parser->in_discarded_stmt = true;
11182 ++c_inhibit_evaluation_warnings;
11183 }
11184
11185 guard_tinfo
11186 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11187 /* Consume the `else' keyword. */
11188 cp_lexer_consume_token (parser->lexer);
11189 if (warn_duplicated_cond)
11190 {
11191 if (cp_lexer_next_token_is_keyword (parser->lexer,
11192 RID_IF)
11193 && chain == NULL)
11194 {
11195 /* We've got "if (COND) else if (COND2)". Start
11196 the condition chain and add COND as the first
11197 element. */
11198 chain = new vec<tree> ();
11199 if (!CONSTANT_CLASS_P (condition)
11200 && !TREE_SIDE_EFFECTS (condition))
11201 {
11202 /* Wrap it in a NOP_EXPR so that we can set the
11203 location of the condition. */
11204 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11205 condition);
11206 SET_EXPR_LOCATION (e, token->location);
11207 chain->safe_push (e);
11208 }
11209 }
11210 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11211 RID_IF))
11212 {
11213 /* This is if-else without subsequent if. Zap the
11214 condition chain; we would have already warned at
11215 this point. */
11216 delete chain;
11217 chain = NULL;
11218 }
11219 }
11220 begin_else_clause (statement);
11221 /* Parse the else-clause. */
11222 cp_parser_implicitly_scoped_statement (parser, NULL,
11223 guard_tinfo, chain);
11224
11225 finish_else_clause (statement);
11226
11227 /* If we are currently parsing a then-clause, then
11228 IF_P will not be NULL. We set it to true to
11229 indicate that this if statement has an else clause.
11230 This may trigger the Wparentheses warning below
11231 when we get back up to the parent if statement. */
11232 if (if_p != NULL)
11233 *if_p = true;
11234
11235 if (discard_else)
11236 {
11237 ELSE_CLAUSE (statement) = NULL_TREE;
11238 parser->in_discarded_stmt = was_discarded;
11239 --c_inhibit_evaluation_warnings;
11240 }
11241 }
11242 else
11243 {
11244 /* This if statement does not have an else clause. If
11245 NESTED_IF is true, then the then-clause has an if
11246 statement which does have an else clause. We warn
11247 about the potential ambiguity. */
11248 if (nested_if)
11249 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11250 "suggest explicit braces to avoid ambiguous"
11251 " %<else%>");
11252 if (warn_duplicated_cond)
11253 {
11254 /* We don't need the condition chain anymore. */
11255 delete chain;
11256 chain = NULL;
11257 }
11258 }
11259
11260 /* Now we're all done with the if-statement. */
11261 finish_if_stmt (statement);
11262 }
11263 else
11264 {
11265 bool in_switch_statement_p;
11266 unsigned char in_statement;
11267
11268 /* Add the condition. */
11269 finish_switch_cond (condition, statement);
11270
11271 /* Parse the body of the switch-statement. */
11272 in_switch_statement_p = parser->in_switch_statement_p;
11273 in_statement = parser->in_statement;
11274 parser->in_switch_statement_p = true;
11275 parser->in_statement |= IN_SWITCH_STMT;
11276 cp_parser_implicitly_scoped_statement (parser, if_p,
11277 guard_tinfo);
11278 parser->in_switch_statement_p = in_switch_statement_p;
11279 parser->in_statement = in_statement;
11280
11281 /* Now we're all done with the switch-statement. */
11282 finish_switch_stmt (statement);
11283 }
11284
11285 return statement;
11286 }
11287 break;
11288
11289 default:
11290 cp_parser_error (parser, "expected selection-statement");
11291 return error_mark_node;
11292 }
11293 }
11294
11295 /* Parse a condition.
11296
11297 condition:
11298 expression
11299 type-specifier-seq declarator = initializer-clause
11300 type-specifier-seq declarator braced-init-list
11301
11302 GNU Extension:
11303
11304 condition:
11305 type-specifier-seq declarator asm-specification [opt]
11306 attributes [opt] = assignment-expression
11307
11308 Returns the expression that should be tested. */
11309
11310 static tree
11311 cp_parser_condition (cp_parser* parser)
11312 {
11313 cp_decl_specifier_seq type_specifiers;
11314 const char *saved_message;
11315 int declares_class_or_enum;
11316
11317 /* Try the declaration first. */
11318 cp_parser_parse_tentatively (parser);
11319 /* New types are not allowed in the type-specifier-seq for a
11320 condition. */
11321 saved_message = parser->type_definition_forbidden_message;
11322 parser->type_definition_forbidden_message
11323 = G_("types may not be defined in conditions");
11324 /* Parse the type-specifier-seq. */
11325 cp_parser_decl_specifier_seq (parser,
11326 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11327 &type_specifiers,
11328 &declares_class_or_enum);
11329 /* Restore the saved message. */
11330 parser->type_definition_forbidden_message = saved_message;
11331 /* If all is well, we might be looking at a declaration. */
11332 if (!cp_parser_error_occurred (parser))
11333 {
11334 tree decl;
11335 tree asm_specification;
11336 tree attributes;
11337 cp_declarator *declarator;
11338 tree initializer = NULL_TREE;
11339
11340 /* Parse the declarator. */
11341 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11342 /*ctor_dtor_or_conv_p=*/NULL,
11343 /*parenthesized_p=*/NULL,
11344 /*member_p=*/false,
11345 /*friend_p=*/false);
11346 /* Parse the attributes. */
11347 attributes = cp_parser_attributes_opt (parser);
11348 /* Parse the asm-specification. */
11349 asm_specification = cp_parser_asm_specification_opt (parser);
11350 /* If the next token is not an `=' or '{', then we might still be
11351 looking at an expression. For example:
11352
11353 if (A(a).x)
11354
11355 looks like a decl-specifier-seq and a declarator -- but then
11356 there is no `=', so this is an expression. */
11357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11358 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11359 cp_parser_simulate_error (parser);
11360
11361 /* If we did see an `=' or '{', then we are looking at a declaration
11362 for sure. */
11363 if (cp_parser_parse_definitely (parser))
11364 {
11365 tree pushed_scope;
11366 bool non_constant_p;
11367 int flags = LOOKUP_ONLYCONVERTING;
11368
11369 /* Create the declaration. */
11370 decl = start_decl (declarator, &type_specifiers,
11371 /*initialized_p=*/true,
11372 attributes, /*prefix_attributes=*/NULL_TREE,
11373 &pushed_scope);
11374
11375 /* Parse the initializer. */
11376 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11377 {
11378 initializer = cp_parser_braced_list (parser, &non_constant_p);
11379 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11380 flags = 0;
11381 }
11382 else
11383 {
11384 /* Consume the `='. */
11385 cp_parser_require (parser, CPP_EQ, RT_EQ);
11386 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11387 }
11388 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11389 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11390
11391 /* Process the initializer. */
11392 cp_finish_decl (decl,
11393 initializer, !non_constant_p,
11394 asm_specification,
11395 flags);
11396
11397 if (pushed_scope)
11398 pop_scope (pushed_scope);
11399
11400 return convert_from_reference (decl);
11401 }
11402 }
11403 /* If we didn't even get past the declarator successfully, we are
11404 definitely not looking at a declaration. */
11405 else
11406 cp_parser_abort_tentative_parse (parser);
11407
11408 /* Otherwise, we are looking at an expression. */
11409 return cp_parser_expression (parser);
11410 }
11411
11412 /* Parses a for-statement or range-for-statement until the closing ')',
11413 not included. */
11414
11415 static tree
11416 cp_parser_for (cp_parser *parser, bool ivdep)
11417 {
11418 tree init, scope, decl;
11419 bool is_range_for;
11420
11421 /* Begin the for-statement. */
11422 scope = begin_for_scope (&init);
11423
11424 /* Parse the initialization. */
11425 is_range_for = cp_parser_init_statement (parser, &decl);
11426
11427 if (is_range_for)
11428 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11429 else
11430 return cp_parser_c_for (parser, scope, init, ivdep);
11431 }
11432
11433 static tree
11434 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11435 {
11436 /* Normal for loop */
11437 tree condition = NULL_TREE;
11438 tree expression = NULL_TREE;
11439 tree stmt;
11440
11441 stmt = begin_for_stmt (scope, init);
11442 /* The init-statement has already been parsed in
11443 cp_parser_init_statement, so no work is needed here. */
11444 finish_init_stmt (stmt);
11445
11446 /* If there's a condition, process it. */
11447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11448 condition = cp_parser_condition (parser);
11449 else if (ivdep)
11450 {
11451 cp_parser_error (parser, "missing loop condition in loop with "
11452 "%<GCC ivdep%> pragma");
11453 condition = error_mark_node;
11454 }
11455 finish_for_cond (condition, stmt, ivdep);
11456 /* Look for the `;'. */
11457 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11458
11459 /* If there's an expression, process it. */
11460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11461 expression = cp_parser_expression (parser);
11462 finish_for_expr (expression, stmt);
11463
11464 return stmt;
11465 }
11466
11467 /* Tries to parse a range-based for-statement:
11468
11469 range-based-for:
11470 decl-specifier-seq declarator : expression
11471
11472 The decl-specifier-seq declarator and the `:' are already parsed by
11473 cp_parser_init_statement. If processing_template_decl it returns a
11474 newly created RANGE_FOR_STMT; if not, it is converted to a
11475 regular FOR_STMT. */
11476
11477 static tree
11478 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11479 bool ivdep)
11480 {
11481 tree stmt, range_expr;
11482 auto_vec <cxx_binding *, 16> bindings;
11483 auto_vec <tree, 16> names;
11484 tree decomp_first_name = NULL_TREE;
11485 unsigned int decomp_cnt = 0;
11486
11487 /* Get the range declaration momentarily out of the way so that
11488 the range expression doesn't clash with it. */
11489 if (range_decl != error_mark_node)
11490 {
11491 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11492 {
11493 tree v = DECL_VALUE_EXPR (range_decl);
11494 /* For decomposition declaration get all of the corresponding
11495 declarations out of the way. */
11496 if (TREE_CODE (v) == ARRAY_REF
11497 && VAR_P (TREE_OPERAND (v, 0))
11498 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11499 {
11500 tree d = range_decl;
11501 range_decl = TREE_OPERAND (v, 0);
11502 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11503 decomp_first_name = d;
11504 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11505 {
11506 tree name = DECL_NAME (d);
11507 names.safe_push (name);
11508 bindings.safe_push (IDENTIFIER_BINDING (name));
11509 IDENTIFIER_BINDING (name)
11510 = IDENTIFIER_BINDING (name)->previous;
11511 }
11512 }
11513 }
11514 if (names.is_empty ())
11515 {
11516 tree name = DECL_NAME (range_decl);
11517 names.safe_push (name);
11518 bindings.safe_push (IDENTIFIER_BINDING (name));
11519 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11520 }
11521 }
11522
11523 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11524 {
11525 bool expr_non_constant_p;
11526 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11527 }
11528 else
11529 range_expr = cp_parser_expression (parser);
11530
11531 /* Put the range declaration(s) back into scope. */
11532 for (unsigned int i = 0; i < names.length (); i++)
11533 {
11534 cxx_binding *binding = bindings[i];
11535 binding->previous = IDENTIFIER_BINDING (names[i]);
11536 IDENTIFIER_BINDING (names[i]) = binding;
11537 }
11538
11539 /* If in template, STMT is converted to a normal for-statement
11540 at instantiation. If not, it is done just ahead. */
11541 if (processing_template_decl)
11542 {
11543 if (check_for_bare_parameter_packs (range_expr))
11544 range_expr = error_mark_node;
11545 stmt = begin_range_for_stmt (scope, init);
11546 if (ivdep)
11547 RANGE_FOR_IVDEP (stmt) = 1;
11548 finish_range_for_decl (stmt, range_decl, range_expr);
11549 if (!type_dependent_expression_p (range_expr)
11550 /* do_auto_deduction doesn't mess with template init-lists. */
11551 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11552 do_range_for_auto_deduction (range_decl, range_expr);
11553 }
11554 else
11555 {
11556 stmt = begin_for_stmt (scope, init);
11557 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11558 decomp_first_name, decomp_cnt, ivdep);
11559 }
11560 return stmt;
11561 }
11562
11563 /* Subroutine of cp_convert_range_for: given the initializer expression,
11564 builds up the range temporary. */
11565
11566 static tree
11567 build_range_temp (tree range_expr)
11568 {
11569 tree range_type, range_temp;
11570
11571 /* Find out the type deduced by the declaration
11572 `auto &&__range = range_expr'. */
11573 range_type = cp_build_reference_type (make_auto (), true);
11574 range_type = do_auto_deduction (range_type, range_expr,
11575 type_uses_auto (range_type));
11576
11577 /* Create the __range variable. */
11578 range_temp = build_decl (input_location, VAR_DECL,
11579 get_identifier ("__for_range"), range_type);
11580 TREE_USED (range_temp) = 1;
11581 DECL_ARTIFICIAL (range_temp) = 1;
11582
11583 return range_temp;
11584 }
11585
11586 /* Used by cp_parser_range_for in template context: we aren't going to
11587 do a full conversion yet, but we still need to resolve auto in the
11588 type of the for-range-declaration if present. This is basically
11589 a shortcut version of cp_convert_range_for. */
11590
11591 static void
11592 do_range_for_auto_deduction (tree decl, tree range_expr)
11593 {
11594 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11595 if (auto_node)
11596 {
11597 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11598 range_temp = convert_from_reference (build_range_temp (range_expr));
11599 iter_type = (cp_parser_perform_range_for_lookup
11600 (range_temp, &begin_dummy, &end_dummy));
11601 if (iter_type)
11602 {
11603 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11604 iter_type);
11605 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11606 tf_warning_or_error);
11607 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11608 iter_decl, auto_node);
11609 }
11610 }
11611 }
11612
11613 /* Converts a range-based for-statement into a normal
11614 for-statement, as per the definition.
11615
11616 for (RANGE_DECL : RANGE_EXPR)
11617 BLOCK
11618
11619 should be equivalent to:
11620
11621 {
11622 auto &&__range = RANGE_EXPR;
11623 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11624 __begin != __end;
11625 ++__begin)
11626 {
11627 RANGE_DECL = *__begin;
11628 BLOCK
11629 }
11630 }
11631
11632 If RANGE_EXPR is an array:
11633 BEGIN_EXPR = __range
11634 END_EXPR = __range + ARRAY_SIZE(__range)
11635 Else if RANGE_EXPR has a member 'begin' or 'end':
11636 BEGIN_EXPR = __range.begin()
11637 END_EXPR = __range.end()
11638 Else:
11639 BEGIN_EXPR = begin(__range)
11640 END_EXPR = end(__range);
11641
11642 If __range has a member 'begin' but not 'end', or vice versa, we must
11643 still use the second alternative (it will surely fail, however).
11644 When calling begin()/end() in the third alternative we must use
11645 argument dependent lookup, but always considering 'std' as an associated
11646 namespace. */
11647
11648 tree
11649 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11650 tree decomp_first_name, unsigned int decomp_cnt,
11651 bool ivdep)
11652 {
11653 tree begin, end;
11654 tree iter_type, begin_expr, end_expr;
11655 tree condition, expression;
11656
11657 if (range_decl == error_mark_node || range_expr == error_mark_node)
11658 /* If an error happened previously do nothing or else a lot of
11659 unhelpful errors would be issued. */
11660 begin_expr = end_expr = iter_type = error_mark_node;
11661 else
11662 {
11663 tree range_temp;
11664
11665 if (VAR_P (range_expr)
11666 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11667 /* Can't bind a reference to an array of runtime bound. */
11668 range_temp = range_expr;
11669 else
11670 {
11671 range_temp = build_range_temp (range_expr);
11672 pushdecl (range_temp);
11673 cp_finish_decl (range_temp, range_expr,
11674 /*is_constant_init*/false, NULL_TREE,
11675 LOOKUP_ONLYCONVERTING);
11676 range_temp = convert_from_reference (range_temp);
11677 }
11678 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11679 &begin_expr, &end_expr);
11680 }
11681
11682 /* The new for initialization statement. */
11683 begin = build_decl (input_location, VAR_DECL,
11684 get_identifier ("__for_begin"), iter_type);
11685 TREE_USED (begin) = 1;
11686 DECL_ARTIFICIAL (begin) = 1;
11687 pushdecl (begin);
11688 cp_finish_decl (begin, begin_expr,
11689 /*is_constant_init*/false, NULL_TREE,
11690 LOOKUP_ONLYCONVERTING);
11691
11692 if (cxx_dialect >= cxx1z)
11693 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11694 end = build_decl (input_location, VAR_DECL,
11695 get_identifier ("__for_end"), iter_type);
11696 TREE_USED (end) = 1;
11697 DECL_ARTIFICIAL (end) = 1;
11698 pushdecl (end);
11699 cp_finish_decl (end, end_expr,
11700 /*is_constant_init*/false, NULL_TREE,
11701 LOOKUP_ONLYCONVERTING);
11702
11703 finish_init_stmt (statement);
11704
11705 /* The new for condition. */
11706 condition = build_x_binary_op (input_location, NE_EXPR,
11707 begin, ERROR_MARK,
11708 end, ERROR_MARK,
11709 NULL, tf_warning_or_error);
11710 finish_for_cond (condition, statement, ivdep);
11711
11712 /* The new increment expression. */
11713 expression = finish_unary_op_expr (input_location,
11714 PREINCREMENT_EXPR, begin,
11715 tf_warning_or_error);
11716 finish_for_expr (expression, statement);
11717
11718 /* The declaration is initialized with *__begin inside the loop body. */
11719 cp_finish_decl (range_decl,
11720 build_x_indirect_ref (input_location, begin, RO_NULL,
11721 tf_warning_or_error),
11722 /*is_constant_init*/false, NULL_TREE,
11723 LOOKUP_ONLYCONVERTING);
11724 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11725 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11726
11727 return statement;
11728 }
11729
11730 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11731 We need to solve both at the same time because the method used
11732 depends on the existence of members begin or end.
11733 Returns the type deduced for the iterator expression. */
11734
11735 static tree
11736 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11737 {
11738 if (error_operand_p (range))
11739 {
11740 *begin = *end = error_mark_node;
11741 return error_mark_node;
11742 }
11743
11744 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11745 {
11746 error ("range-based %<for%> expression of type %qT "
11747 "has incomplete type", TREE_TYPE (range));
11748 *begin = *end = error_mark_node;
11749 return error_mark_node;
11750 }
11751 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11752 {
11753 /* If RANGE is an array, we will use pointer arithmetic. */
11754 *begin = decay_conversion (range, tf_warning_or_error);
11755 *end = build_binary_op (input_location, PLUS_EXPR,
11756 range,
11757 array_type_nelts_top (TREE_TYPE (range)),
11758 0);
11759 return TREE_TYPE (*begin);
11760 }
11761 else
11762 {
11763 /* If it is not an array, we must do a bit of magic. */
11764 tree id_begin, id_end;
11765 tree member_begin, member_end;
11766
11767 *begin = *end = error_mark_node;
11768
11769 id_begin = get_identifier ("begin");
11770 id_end = get_identifier ("end");
11771 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11772 /*protect=*/2, /*want_type=*/false,
11773 tf_warning_or_error);
11774 member_end = lookup_member (TREE_TYPE (range), id_end,
11775 /*protect=*/2, /*want_type=*/false,
11776 tf_warning_or_error);
11777
11778 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11779 {
11780 /* Use the member functions. */
11781 if (member_begin != NULL_TREE)
11782 *begin = cp_parser_range_for_member_function (range, id_begin);
11783 else
11784 error ("range-based %<for%> expression of type %qT has an "
11785 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11786
11787 if (member_end != NULL_TREE)
11788 *end = cp_parser_range_for_member_function (range, id_end);
11789 else
11790 error ("range-based %<for%> expression of type %qT has a "
11791 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11792 }
11793 else
11794 {
11795 /* Use global functions with ADL. */
11796 vec<tree, va_gc> *vec;
11797 vec = make_tree_vector ();
11798
11799 vec_safe_push (vec, range);
11800
11801 member_begin = perform_koenig_lookup (id_begin, vec,
11802 tf_warning_or_error);
11803 *begin = finish_call_expr (member_begin, &vec, false, true,
11804 tf_warning_or_error);
11805 member_end = perform_koenig_lookup (id_end, vec,
11806 tf_warning_or_error);
11807 *end = finish_call_expr (member_end, &vec, false, true,
11808 tf_warning_or_error);
11809
11810 release_tree_vector (vec);
11811 }
11812
11813 /* Last common checks. */
11814 if (*begin == error_mark_node || *end == error_mark_node)
11815 {
11816 /* If one of the expressions is an error do no more checks. */
11817 *begin = *end = error_mark_node;
11818 return error_mark_node;
11819 }
11820 else if (type_dependent_expression_p (*begin)
11821 || type_dependent_expression_p (*end))
11822 /* Can happen, when, eg, in a template context, Koenig lookup
11823 can't resolve begin/end (c++/58503). */
11824 return NULL_TREE;
11825 else
11826 {
11827 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11828 /* The unqualified type of the __begin and __end temporaries should
11829 be the same, as required by the multiple auto declaration. */
11830 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11831 {
11832 if (cxx_dialect >= cxx1z
11833 && (build_x_binary_op (input_location, NE_EXPR,
11834 *begin, ERROR_MARK,
11835 *end, ERROR_MARK,
11836 NULL, tf_none)
11837 != error_mark_node))
11838 /* P0184R0 allows __begin and __end to have different types,
11839 but make sure they are comparable so we can give a better
11840 diagnostic. */;
11841 else
11842 error ("inconsistent begin/end types in range-based %<for%> "
11843 "statement: %qT and %qT",
11844 TREE_TYPE (*begin), TREE_TYPE (*end));
11845 }
11846 return iter_type;
11847 }
11848 }
11849 }
11850
11851 /* Helper function for cp_parser_perform_range_for_lookup.
11852 Builds a tree for RANGE.IDENTIFIER(). */
11853
11854 static tree
11855 cp_parser_range_for_member_function (tree range, tree identifier)
11856 {
11857 tree member, res;
11858 vec<tree, va_gc> *vec;
11859
11860 member = finish_class_member_access_expr (range, identifier,
11861 false, tf_warning_or_error);
11862 if (member == error_mark_node)
11863 return error_mark_node;
11864
11865 vec = make_tree_vector ();
11866 res = finish_call_expr (member, &vec,
11867 /*disallow_virtual=*/false,
11868 /*koenig_p=*/false,
11869 tf_warning_or_error);
11870 release_tree_vector (vec);
11871 return res;
11872 }
11873
11874 /* Parse an iteration-statement.
11875
11876 iteration-statement:
11877 while ( condition ) statement
11878 do statement while ( expression ) ;
11879 for ( init-statement condition [opt] ; expression [opt] )
11880 statement
11881
11882 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11883
11884 static tree
11885 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11886 {
11887 cp_token *token;
11888 enum rid keyword;
11889 tree statement;
11890 unsigned char in_statement;
11891 token_indent_info guard_tinfo;
11892
11893 /* Peek at the next token. */
11894 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11895 if (!token)
11896 return error_mark_node;
11897
11898 guard_tinfo = get_token_indent_info (token);
11899
11900 /* Remember whether or not we are already within an iteration
11901 statement. */
11902 in_statement = parser->in_statement;
11903
11904 /* See what kind of keyword it is. */
11905 keyword = token->keyword;
11906 switch (keyword)
11907 {
11908 case RID_WHILE:
11909 {
11910 tree condition;
11911
11912 /* Begin the while-statement. */
11913 statement = begin_while_stmt ();
11914 /* Look for the `('. */
11915 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11916 /* Parse the condition. */
11917 condition = cp_parser_condition (parser);
11918 finish_while_stmt_cond (condition, statement, ivdep);
11919 /* Look for the `)'. */
11920 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11921 /* Parse the dependent statement. */
11922 parser->in_statement = IN_ITERATION_STMT;
11923 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11924 parser->in_statement = in_statement;
11925 /* We're done with the while-statement. */
11926 finish_while_stmt (statement);
11927 }
11928 break;
11929
11930 case RID_DO:
11931 {
11932 tree expression;
11933
11934 /* Begin the do-statement. */
11935 statement = begin_do_stmt ();
11936 /* Parse the body of the do-statement. */
11937 parser->in_statement = IN_ITERATION_STMT;
11938 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11939 parser->in_statement = in_statement;
11940 finish_do_body (statement);
11941 /* Look for the `while' keyword. */
11942 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11943 /* Look for the `('. */
11944 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11945 /* Parse the expression. */
11946 expression = cp_parser_expression (parser);
11947 /* We're done with the do-statement. */
11948 finish_do_stmt (expression, statement, ivdep);
11949 /* Look for the `)'. */
11950 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11951 /* Look for the `;'. */
11952 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11953 }
11954 break;
11955
11956 case RID_FOR:
11957 {
11958 /* Look for the `('. */
11959 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11960
11961 statement = cp_parser_for (parser, ivdep);
11962
11963 /* Look for the `)'. */
11964 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11965
11966 /* Parse the body of the for-statement. */
11967 parser->in_statement = IN_ITERATION_STMT;
11968 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11969 parser->in_statement = in_statement;
11970
11971 /* We're done with the for-statement. */
11972 finish_for_stmt (statement);
11973 }
11974 break;
11975
11976 default:
11977 cp_parser_error (parser, "expected iteration-statement");
11978 statement = error_mark_node;
11979 break;
11980 }
11981
11982 return statement;
11983 }
11984
11985 /* Parse a init-statement or the declarator of a range-based-for.
11986 Returns true if a range-based-for declaration is seen.
11987
11988 init-statement:
11989 expression-statement
11990 simple-declaration */
11991
11992 static bool
11993 cp_parser_init_statement (cp_parser* parser, tree *decl)
11994 {
11995 /* If the next token is a `;', then we have an empty
11996 expression-statement. Grammatically, this is also a
11997 simple-declaration, but an invalid one, because it does not
11998 declare anything. Therefore, if we did not handle this case
11999 specially, we would issue an error message about an invalid
12000 declaration. */
12001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12002 {
12003 bool is_range_for = false;
12004 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12005
12006 /* A colon is used in range-based for. */
12007 parser->colon_corrects_to_scope_p = false;
12008
12009 /* We're going to speculatively look for a declaration, falling back
12010 to an expression, if necessary. */
12011 cp_parser_parse_tentatively (parser);
12012 /* Parse the declaration. */
12013 cp_parser_simple_declaration (parser,
12014 /*function_definition_allowed_p=*/false,
12015 decl);
12016 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12017 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12018 {
12019 /* It is a range-for, consume the ':' */
12020 cp_lexer_consume_token (parser->lexer);
12021 is_range_for = true;
12022 if (cxx_dialect < cxx11)
12023 {
12024 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12025 "range-based %<for%> loops only available with "
12026 "-std=c++11 or -std=gnu++11");
12027 *decl = error_mark_node;
12028 }
12029 }
12030 else
12031 /* The ';' is not consumed yet because we told
12032 cp_parser_simple_declaration not to. */
12033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12034
12035 if (cp_parser_parse_definitely (parser))
12036 return is_range_for;
12037 /* If the tentative parse failed, then we shall need to look for an
12038 expression-statement. */
12039 }
12040 /* If we are here, it is an expression-statement. */
12041 cp_parser_expression_statement (parser, NULL_TREE);
12042 return false;
12043 }
12044
12045 /* Parse a jump-statement.
12046
12047 jump-statement:
12048 break ;
12049 continue ;
12050 return expression [opt] ;
12051 return braced-init-list ;
12052 goto identifier ;
12053
12054 GNU extension:
12055
12056 jump-statement:
12057 goto * expression ;
12058
12059 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12060
12061 static tree
12062 cp_parser_jump_statement (cp_parser* parser)
12063 {
12064 tree statement = error_mark_node;
12065 cp_token *token;
12066 enum rid keyword;
12067 unsigned char in_statement;
12068
12069 /* Peek at the next token. */
12070 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12071 if (!token)
12072 return error_mark_node;
12073
12074 /* See what kind of keyword it is. */
12075 keyword = token->keyword;
12076 switch (keyword)
12077 {
12078 case RID_BREAK:
12079 in_statement = parser->in_statement & ~IN_IF_STMT;
12080 switch (in_statement)
12081 {
12082 case 0:
12083 error_at (token->location, "break statement not within loop or switch");
12084 break;
12085 default:
12086 gcc_assert ((in_statement & IN_SWITCH_STMT)
12087 || in_statement == IN_ITERATION_STMT);
12088 statement = finish_break_stmt ();
12089 if (in_statement == IN_ITERATION_STMT)
12090 break_maybe_infinite_loop ();
12091 break;
12092 case IN_OMP_BLOCK:
12093 error_at (token->location, "invalid exit from OpenMP structured block");
12094 break;
12095 case IN_OMP_FOR:
12096 error_at (token->location, "break statement used with OpenMP for loop");
12097 break;
12098 case IN_CILK_SIMD_FOR:
12099 error_at (token->location, "break statement used with Cilk Plus for loop");
12100 break;
12101 }
12102 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12103 break;
12104
12105 case RID_CONTINUE:
12106 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12107 {
12108 case 0:
12109 error_at (token->location, "continue statement not within a loop");
12110 break;
12111 case IN_CILK_SIMD_FOR:
12112 error_at (token->location,
12113 "continue statement within %<#pragma simd%> loop body");
12114 /* Fall through. */
12115 case IN_ITERATION_STMT:
12116 case IN_OMP_FOR:
12117 statement = finish_continue_stmt ();
12118 break;
12119 case IN_OMP_BLOCK:
12120 error_at (token->location, "invalid exit from OpenMP structured block");
12121 break;
12122 default:
12123 gcc_unreachable ();
12124 }
12125 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12126 break;
12127
12128 case RID_RETURN:
12129 {
12130 tree expr;
12131 bool expr_non_constant_p;
12132
12133 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12134 {
12135 cp_lexer_set_source_position (parser->lexer);
12136 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12137 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12138 }
12139 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12140 expr = cp_parser_expression (parser);
12141 else
12142 /* If the next token is a `;', then there is no
12143 expression. */
12144 expr = NULL_TREE;
12145 /* Build the return-statement. */
12146 if (current_function_auto_return_pattern && parser->in_discarded_stmt)
12147 /* Don't deduce from a discarded return statement. */;
12148 else
12149 statement = finish_return_stmt (expr);
12150 /* Look for the final `;'. */
12151 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12152 }
12153 break;
12154
12155 case RID_GOTO:
12156 if (parser->in_function_body
12157 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12158 {
12159 error ("%<goto%> in %<constexpr%> function");
12160 cp_function_chain->invalid_constexpr = true;
12161 }
12162
12163 /* Create the goto-statement. */
12164 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12165 {
12166 /* Issue a warning about this use of a GNU extension. */
12167 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12168 /* Consume the '*' token. */
12169 cp_lexer_consume_token (parser->lexer);
12170 /* Parse the dependent expression. */
12171 finish_goto_stmt (cp_parser_expression (parser));
12172 }
12173 else
12174 finish_goto_stmt (cp_parser_identifier (parser));
12175 /* Look for the final `;'. */
12176 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12177 break;
12178
12179 default:
12180 cp_parser_error (parser, "expected jump-statement");
12181 break;
12182 }
12183
12184 return statement;
12185 }
12186
12187 /* Parse a declaration-statement.
12188
12189 declaration-statement:
12190 block-declaration */
12191
12192 static void
12193 cp_parser_declaration_statement (cp_parser* parser)
12194 {
12195 void *p;
12196
12197 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12198 p = obstack_alloc (&declarator_obstack, 0);
12199
12200 /* Parse the block-declaration. */
12201 cp_parser_block_declaration (parser, /*statement_p=*/true);
12202
12203 /* Free any declarators allocated. */
12204 obstack_free (&declarator_obstack, p);
12205 }
12206
12207 /* Some dependent statements (like `if (cond) statement'), are
12208 implicitly in their own scope. In other words, if the statement is
12209 a single statement (as opposed to a compound-statement), it is
12210 none-the-less treated as if it were enclosed in braces. Any
12211 declarations appearing in the dependent statement are out of scope
12212 after control passes that point. This function parses a statement,
12213 but ensures that is in its own scope, even if it is not a
12214 compound-statement.
12215
12216 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12217 is a (possibly labeled) if statement which is not enclosed in
12218 braces and has an else clause. This is used to implement
12219 -Wparentheses.
12220
12221 CHAIN is a vector of if-else-if conditions. This is used to implement
12222 -Wduplicated-cond.
12223
12224 Returns the new statement. */
12225
12226 static tree
12227 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12228 const token_indent_info &guard_tinfo,
12229 vec<tree> *chain)
12230 {
12231 tree statement;
12232 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12233 token_indent_info body_tinfo
12234 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12235
12236 if (if_p != NULL)
12237 *if_p = false;
12238
12239 /* Mark if () ; with a special NOP_EXPR. */
12240 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12241 {
12242 cp_lexer_consume_token (parser->lexer);
12243 statement = add_stmt (build_empty_stmt (body_loc));
12244
12245 if (guard_tinfo.keyword == RID_IF
12246 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12247 warning_at (body_loc, OPT_Wempty_body,
12248 "suggest braces around empty body in an %<if%> statement");
12249 else if (guard_tinfo.keyword == RID_ELSE)
12250 warning_at (body_loc, OPT_Wempty_body,
12251 "suggest braces around empty body in an %<else%> statement");
12252 }
12253 /* if a compound is opened, we simply parse the statement directly. */
12254 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12255 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12256 /* If the token is not a `{', then we must take special action. */
12257 else
12258 {
12259 /* Create a compound-statement. */
12260 statement = begin_compound_stmt (0);
12261 /* Parse the dependent-statement. */
12262 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12263 /* Finish the dummy compound-statement. */
12264 finish_compound_stmt (statement);
12265 }
12266
12267 token_indent_info next_tinfo
12268 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12269 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12270
12271 /* Return the statement. */
12272 return statement;
12273 }
12274
12275 /* For some dependent statements (like `while (cond) statement'), we
12276 have already created a scope. Therefore, even if the dependent
12277 statement is a compound-statement, we do not want to create another
12278 scope. */
12279
12280 static void
12281 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12282 const token_indent_info &guard_tinfo)
12283 {
12284 /* If the token is a `{', then we must take special action. */
12285 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12286 {
12287 token_indent_info body_tinfo
12288 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12289
12290 cp_parser_statement (parser, NULL_TREE, false, if_p);
12291 token_indent_info next_tinfo
12292 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12293 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12294 }
12295 else
12296 {
12297 /* Avoid calling cp_parser_compound_statement, so that we
12298 don't create a new scope. Do everything else by hand. */
12299 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12300 /* If the next keyword is `__label__' we have a label declaration. */
12301 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12302 cp_parser_label_declaration (parser);
12303 /* Parse an (optional) statement-seq. */
12304 cp_parser_statement_seq_opt (parser, NULL_TREE);
12305 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12306 }
12307 }
12308
12309 /* Declarations [gram.dcl.dcl] */
12310
12311 /* Parse an optional declaration-sequence.
12312
12313 declaration-seq:
12314 declaration
12315 declaration-seq declaration */
12316
12317 static void
12318 cp_parser_declaration_seq_opt (cp_parser* parser)
12319 {
12320 while (true)
12321 {
12322 cp_token *token;
12323
12324 token = cp_lexer_peek_token (parser->lexer);
12325
12326 if (token->type == CPP_CLOSE_BRACE
12327 || token->type == CPP_EOF
12328 || token->type == CPP_PRAGMA_EOL)
12329 break;
12330
12331 if (token->type == CPP_SEMICOLON)
12332 {
12333 /* A declaration consisting of a single semicolon is
12334 invalid. Allow it unless we're being pedantic. */
12335 cp_lexer_consume_token (parser->lexer);
12336 if (!in_system_header_at (input_location))
12337 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12338 continue;
12339 }
12340
12341 /* If we're entering or exiting a region that's implicitly
12342 extern "C", modify the lang context appropriately. */
12343 if (!parser->implicit_extern_c && token->implicit_extern_c)
12344 {
12345 push_lang_context (lang_name_c);
12346 parser->implicit_extern_c = true;
12347 }
12348 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12349 {
12350 pop_lang_context ();
12351 parser->implicit_extern_c = false;
12352 }
12353
12354 if (token->type == CPP_PRAGMA)
12355 {
12356 /* A top-level declaration can consist solely of a #pragma.
12357 A nested declaration cannot, so this is done here and not
12358 in cp_parser_declaration. (A #pragma at block scope is
12359 handled in cp_parser_statement.) */
12360 cp_parser_pragma (parser, pragma_external, NULL);
12361 continue;
12362 }
12363
12364 /* Parse the declaration itself. */
12365 cp_parser_declaration (parser);
12366 }
12367 }
12368
12369 /* Parse a declaration.
12370
12371 declaration:
12372 block-declaration
12373 function-definition
12374 template-declaration
12375 explicit-instantiation
12376 explicit-specialization
12377 linkage-specification
12378 namespace-definition
12379
12380 C++17:
12381 deduction-guide
12382
12383 GNU extension:
12384
12385 declaration:
12386 __extension__ declaration */
12387
12388 static void
12389 cp_parser_declaration (cp_parser* parser)
12390 {
12391 cp_token token1;
12392 cp_token token2;
12393 int saved_pedantic;
12394 void *p;
12395 tree attributes = NULL_TREE;
12396
12397 /* Check for the `__extension__' keyword. */
12398 if (cp_parser_extension_opt (parser, &saved_pedantic))
12399 {
12400 /* Parse the qualified declaration. */
12401 cp_parser_declaration (parser);
12402 /* Restore the PEDANTIC flag. */
12403 pedantic = saved_pedantic;
12404
12405 return;
12406 }
12407
12408 /* Try to figure out what kind of declaration is present. */
12409 token1 = *cp_lexer_peek_token (parser->lexer);
12410
12411 if (token1.type != CPP_EOF)
12412 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12413 else
12414 {
12415 token2.type = CPP_EOF;
12416 token2.keyword = RID_MAX;
12417 }
12418
12419 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12420 p = obstack_alloc (&declarator_obstack, 0);
12421
12422 /* If the next token is `extern' and the following token is a string
12423 literal, then we have a linkage specification. */
12424 if (token1.keyword == RID_EXTERN
12425 && cp_parser_is_pure_string_literal (&token2))
12426 cp_parser_linkage_specification (parser);
12427 /* If the next token is `template', then we have either a template
12428 declaration, an explicit instantiation, or an explicit
12429 specialization. */
12430 else if (token1.keyword == RID_TEMPLATE)
12431 {
12432 /* `template <>' indicates a template specialization. */
12433 if (token2.type == CPP_LESS
12434 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12435 cp_parser_explicit_specialization (parser);
12436 /* `template <' indicates a template declaration. */
12437 else if (token2.type == CPP_LESS)
12438 cp_parser_template_declaration (parser, /*member_p=*/false);
12439 /* Anything else must be an explicit instantiation. */
12440 else
12441 cp_parser_explicit_instantiation (parser);
12442 }
12443 /* If the next token is `export', then we have a template
12444 declaration. */
12445 else if (token1.keyword == RID_EXPORT)
12446 cp_parser_template_declaration (parser, /*member_p=*/false);
12447 /* If the next token is `extern', 'static' or 'inline' and the one
12448 after that is `template', we have a GNU extended explicit
12449 instantiation directive. */
12450 else if (cp_parser_allow_gnu_extensions_p (parser)
12451 && (token1.keyword == RID_EXTERN
12452 || token1.keyword == RID_STATIC
12453 || token1.keyword == RID_INLINE)
12454 && token2.keyword == RID_TEMPLATE)
12455 cp_parser_explicit_instantiation (parser);
12456 /* If the next token is `namespace', check for a named or unnamed
12457 namespace definition. */
12458 else if (token1.keyword == RID_NAMESPACE
12459 && (/* A named namespace definition. */
12460 (token2.type == CPP_NAME
12461 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12462 != CPP_EQ))
12463 || (token2.type == CPP_OPEN_SQUARE
12464 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12465 == CPP_OPEN_SQUARE)
12466 /* An unnamed namespace definition. */
12467 || token2.type == CPP_OPEN_BRACE
12468 || token2.keyword == RID_ATTRIBUTE))
12469 cp_parser_namespace_definition (parser);
12470 /* An inline (associated) namespace definition. */
12471 else if (token1.keyword == RID_INLINE
12472 && token2.keyword == RID_NAMESPACE)
12473 cp_parser_namespace_definition (parser);
12474 /* Objective-C++ declaration/definition. */
12475 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12476 cp_parser_objc_declaration (parser, NULL_TREE);
12477 else if (c_dialect_objc ()
12478 && token1.keyword == RID_ATTRIBUTE
12479 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12480 cp_parser_objc_declaration (parser, attributes);
12481 /* At this point we may have a template declared by a concept
12482 introduction. */
12483 else if (flag_concepts
12484 && cp_parser_template_declaration_after_export (parser,
12485 /*member_p=*/false))
12486 /* We did. */;
12487 else
12488 /* Try to parse a block-declaration, or a function-definition. */
12489 cp_parser_block_declaration (parser, /*statement_p=*/false);
12490
12491 /* Free any declarators allocated. */
12492 obstack_free (&declarator_obstack, p);
12493 }
12494
12495 /* Parse a block-declaration.
12496
12497 block-declaration:
12498 simple-declaration
12499 asm-definition
12500 namespace-alias-definition
12501 using-declaration
12502 using-directive
12503
12504 GNU Extension:
12505
12506 block-declaration:
12507 __extension__ block-declaration
12508
12509 C++0x Extension:
12510
12511 block-declaration:
12512 static_assert-declaration
12513
12514 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12515 part of a declaration-statement. */
12516
12517 static void
12518 cp_parser_block_declaration (cp_parser *parser,
12519 bool statement_p)
12520 {
12521 cp_token *token1;
12522 int saved_pedantic;
12523
12524 /* Check for the `__extension__' keyword. */
12525 if (cp_parser_extension_opt (parser, &saved_pedantic))
12526 {
12527 /* Parse the qualified declaration. */
12528 cp_parser_block_declaration (parser, statement_p);
12529 /* Restore the PEDANTIC flag. */
12530 pedantic = saved_pedantic;
12531
12532 return;
12533 }
12534
12535 /* Peek at the next token to figure out which kind of declaration is
12536 present. */
12537 token1 = cp_lexer_peek_token (parser->lexer);
12538
12539 /* If the next keyword is `asm', we have an asm-definition. */
12540 if (token1->keyword == RID_ASM)
12541 {
12542 if (statement_p)
12543 cp_parser_commit_to_tentative_parse (parser);
12544 cp_parser_asm_definition (parser);
12545 }
12546 /* If the next keyword is `namespace', we have a
12547 namespace-alias-definition. */
12548 else if (token1->keyword == RID_NAMESPACE)
12549 cp_parser_namespace_alias_definition (parser);
12550 /* If the next keyword is `using', we have a
12551 using-declaration, a using-directive, or an alias-declaration. */
12552 else if (token1->keyword == RID_USING)
12553 {
12554 cp_token *token2;
12555
12556 if (statement_p)
12557 cp_parser_commit_to_tentative_parse (parser);
12558 /* If the token after `using' is `namespace', then we have a
12559 using-directive. */
12560 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12561 if (token2->keyword == RID_NAMESPACE)
12562 cp_parser_using_directive (parser);
12563 /* If the second token after 'using' is '=', then we have an
12564 alias-declaration. */
12565 else if (cxx_dialect >= cxx11
12566 && token2->type == CPP_NAME
12567 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12568 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12569 cp_parser_alias_declaration (parser);
12570 /* Otherwise, it's a using-declaration. */
12571 else
12572 cp_parser_using_declaration (parser,
12573 /*access_declaration_p=*/false);
12574 }
12575 /* If the next keyword is `__label__' we have a misplaced label
12576 declaration. */
12577 else if (token1->keyword == RID_LABEL)
12578 {
12579 cp_lexer_consume_token (parser->lexer);
12580 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12581 cp_parser_skip_to_end_of_statement (parser);
12582 /* If the next token is now a `;', consume it. */
12583 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12584 cp_lexer_consume_token (parser->lexer);
12585 }
12586 /* If the next token is `static_assert' we have a static assertion. */
12587 else if (token1->keyword == RID_STATIC_ASSERT)
12588 cp_parser_static_assert (parser, /*member_p=*/false);
12589 /* Anything else must be a simple-declaration. */
12590 else
12591 cp_parser_simple_declaration (parser, !statement_p,
12592 /*maybe_range_for_decl*/NULL);
12593 }
12594
12595 /* Parse a simple-declaration.
12596
12597 simple-declaration:
12598 decl-specifier-seq [opt] init-declarator-list [opt] ;
12599 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12600 brace-or-equal-initializer ;
12601
12602 init-declarator-list:
12603 init-declarator
12604 init-declarator-list , init-declarator
12605
12606 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12607 function-definition as a simple-declaration.
12608
12609 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12610 parsed declaration if it is an uninitialized single declarator not followed
12611 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12612 if present, will not be consumed. */
12613
12614 static void
12615 cp_parser_simple_declaration (cp_parser* parser,
12616 bool function_definition_allowed_p,
12617 tree *maybe_range_for_decl)
12618 {
12619 cp_decl_specifier_seq decl_specifiers;
12620 int declares_class_or_enum;
12621 bool saw_declarator;
12622 location_t comma_loc = UNKNOWN_LOCATION;
12623 location_t init_loc = UNKNOWN_LOCATION;
12624
12625 if (maybe_range_for_decl)
12626 *maybe_range_for_decl = NULL_TREE;
12627
12628 /* Defer access checks until we know what is being declared; the
12629 checks for names appearing in the decl-specifier-seq should be
12630 done as if we were in the scope of the thing being declared. */
12631 push_deferring_access_checks (dk_deferred);
12632
12633 /* Parse the decl-specifier-seq. We have to keep track of whether
12634 or not the decl-specifier-seq declares a named class or
12635 enumeration type, since that is the only case in which the
12636 init-declarator-list is allowed to be empty.
12637
12638 [dcl.dcl]
12639
12640 In a simple-declaration, the optional init-declarator-list can be
12641 omitted only when declaring a class or enumeration, that is when
12642 the decl-specifier-seq contains either a class-specifier, an
12643 elaborated-type-specifier, or an enum-specifier. */
12644 cp_parser_decl_specifier_seq (parser,
12645 CP_PARSER_FLAGS_OPTIONAL,
12646 &decl_specifiers,
12647 &declares_class_or_enum);
12648 /* We no longer need to defer access checks. */
12649 stop_deferring_access_checks ();
12650
12651 /* In a block scope, a valid declaration must always have a
12652 decl-specifier-seq. By not trying to parse declarators, we can
12653 resolve the declaration/expression ambiguity more quickly. */
12654 if (!function_definition_allowed_p
12655 && !decl_specifiers.any_specifiers_p)
12656 {
12657 cp_parser_error (parser, "expected declaration");
12658 goto done;
12659 }
12660
12661 /* If the next two tokens are both identifiers, the code is
12662 erroneous. The usual cause of this situation is code like:
12663
12664 T t;
12665
12666 where "T" should name a type -- but does not. */
12667 if (!decl_specifiers.any_type_specifiers_p
12668 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12669 {
12670 /* If parsing tentatively, we should commit; we really are
12671 looking at a declaration. */
12672 cp_parser_commit_to_tentative_parse (parser);
12673 /* Give up. */
12674 goto done;
12675 }
12676
12677 /* If we have seen at least one decl-specifier, and the next token
12678 is not a parenthesis, then we must be looking at a declaration.
12679 (After "int (" we might be looking at a functional cast.) */
12680 if (decl_specifiers.any_specifiers_p
12681 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12682 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12683 && !cp_parser_error_occurred (parser))
12684 cp_parser_commit_to_tentative_parse (parser);
12685
12686 /* Look for C++17 decomposition declaration. */
12687 for (size_t n = 1; ; n++)
12688 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12689 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12690 continue;
12691 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12692 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12693 && decl_specifiers.any_specifiers_p)
12694 {
12695 tree decl
12696 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12697 maybe_range_for_decl,
12698 &init_loc);
12699
12700 /* The next token should be either a `,' or a `;'. */
12701 cp_token *token = cp_lexer_peek_token (parser->lexer);
12702 /* If it's a `;', we are done. */
12703 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12704 goto finish;
12705 /* Anything else is an error. */
12706 else
12707 {
12708 /* If we have already issued an error message we don't need
12709 to issue another one. */
12710 if ((decl != error_mark_node
12711 && DECL_INITIAL (decl) != error_mark_node)
12712 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12713 cp_parser_error (parser, "expected %<,%> or %<;%>");
12714 /* Skip tokens until we reach the end of the statement. */
12715 cp_parser_skip_to_end_of_statement (parser);
12716 /* If the next token is now a `;', consume it. */
12717 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12718 cp_lexer_consume_token (parser->lexer);
12719 goto done;
12720 }
12721 }
12722 else
12723 break;
12724
12725 tree last_type;
12726
12727 last_type = NULL_TREE;
12728
12729 /* Keep going until we hit the `;' at the end of the simple
12730 declaration. */
12731 saw_declarator = false;
12732 while (cp_lexer_next_token_is_not (parser->lexer,
12733 CPP_SEMICOLON))
12734 {
12735 cp_token *token;
12736 bool function_definition_p;
12737 tree decl;
12738 tree auto_result = NULL_TREE;
12739
12740 if (saw_declarator)
12741 {
12742 /* If we are processing next declarator, comma is expected */
12743 token = cp_lexer_peek_token (parser->lexer);
12744 gcc_assert (token->type == CPP_COMMA);
12745 cp_lexer_consume_token (parser->lexer);
12746 if (maybe_range_for_decl)
12747 {
12748 *maybe_range_for_decl = error_mark_node;
12749 if (comma_loc == UNKNOWN_LOCATION)
12750 comma_loc = token->location;
12751 }
12752 }
12753 else
12754 saw_declarator = true;
12755
12756 /* Parse the init-declarator. */
12757 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12758 /*checks=*/NULL,
12759 function_definition_allowed_p,
12760 /*member_p=*/false,
12761 declares_class_or_enum,
12762 &function_definition_p,
12763 maybe_range_for_decl,
12764 &init_loc,
12765 &auto_result);
12766 /* If an error occurred while parsing tentatively, exit quickly.
12767 (That usually happens when in the body of a function; each
12768 statement is treated as a declaration-statement until proven
12769 otherwise.) */
12770 if (cp_parser_error_occurred (parser))
12771 goto done;
12772
12773 if (auto_result)
12774 {
12775 if (last_type && last_type != error_mark_node
12776 && !same_type_p (auto_result, last_type))
12777 {
12778 /* If the list of declarators contains more than one declarator,
12779 the type of each declared variable is determined as described
12780 above. If the type deduced for the template parameter U is not
12781 the same in each deduction, the program is ill-formed. */
12782 error_at (decl_specifiers.locations[ds_type_spec],
12783 "inconsistent deduction for %qT: %qT and then %qT",
12784 decl_specifiers.type, last_type, auto_result);
12785 last_type = error_mark_node;
12786 }
12787 else
12788 last_type = auto_result;
12789 }
12790
12791 /* Handle function definitions specially. */
12792 if (function_definition_p)
12793 {
12794 /* If the next token is a `,', then we are probably
12795 processing something like:
12796
12797 void f() {}, *p;
12798
12799 which is erroneous. */
12800 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12801 {
12802 cp_token *token = cp_lexer_peek_token (parser->lexer);
12803 error_at (token->location,
12804 "mixing"
12805 " declarations and function-definitions is forbidden");
12806 }
12807 /* Otherwise, we're done with the list of declarators. */
12808 else
12809 {
12810 pop_deferring_access_checks ();
12811 return;
12812 }
12813 }
12814 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12815 *maybe_range_for_decl = decl;
12816 /* The next token should be either a `,' or a `;'. */
12817 token = cp_lexer_peek_token (parser->lexer);
12818 /* If it's a `,', there are more declarators to come. */
12819 if (token->type == CPP_COMMA)
12820 /* will be consumed next time around */;
12821 /* If it's a `;', we are done. */
12822 else if (token->type == CPP_SEMICOLON)
12823 break;
12824 else if (maybe_range_for_decl)
12825 {
12826 if (declares_class_or_enum && token->type == CPP_COLON)
12827 permerror (decl_specifiers.locations[ds_type_spec],
12828 "types may not be defined in a for-range-declaration");
12829 break;
12830 }
12831 /* Anything else is an error. */
12832 else
12833 {
12834 /* If we have already issued an error message we don't need
12835 to issue another one. */
12836 if ((decl != error_mark_node
12837 && DECL_INITIAL (decl) != error_mark_node)
12838 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12839 cp_parser_error (parser, "expected %<,%> or %<;%>");
12840 /* Skip tokens until we reach the end of the statement. */
12841 cp_parser_skip_to_end_of_statement (parser);
12842 /* If the next token is now a `;', consume it. */
12843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12844 cp_lexer_consume_token (parser->lexer);
12845 goto done;
12846 }
12847 /* After the first time around, a function-definition is not
12848 allowed -- even if it was OK at first. For example:
12849
12850 int i, f() {}
12851
12852 is not valid. */
12853 function_definition_allowed_p = false;
12854 }
12855
12856 /* Issue an error message if no declarators are present, and the
12857 decl-specifier-seq does not itself declare a class or
12858 enumeration: [dcl.dcl]/3. */
12859 if (!saw_declarator)
12860 {
12861 if (cp_parser_declares_only_class_p (parser))
12862 {
12863 if (!declares_class_or_enum
12864 && decl_specifiers.type
12865 && OVERLOAD_TYPE_P (decl_specifiers.type))
12866 /* Ensure an error is issued anyway when finish_decltype_type,
12867 called via cp_parser_decl_specifier_seq, returns a class or
12868 an enumeration (c++/51786). */
12869 decl_specifiers.type = NULL_TREE;
12870 shadow_tag (&decl_specifiers);
12871 }
12872 /* Perform any deferred access checks. */
12873 perform_deferred_access_checks (tf_warning_or_error);
12874 }
12875
12876 /* Consume the `;'. */
12877 finish:
12878 if (!maybe_range_for_decl)
12879 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12880 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12881 {
12882 if (init_loc != UNKNOWN_LOCATION)
12883 error_at (init_loc, "initializer in range-based %<for%> loop");
12884 if (comma_loc != UNKNOWN_LOCATION)
12885 error_at (comma_loc,
12886 "multiple declarations in range-based %<for%> loop");
12887 }
12888
12889 done:
12890 pop_deferring_access_checks ();
12891 }
12892
12893 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12894 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12895 brace-or-equal-initializer ; */
12896
12897 static tree
12898 cp_parser_decomposition_declaration (cp_parser *parser,
12899 cp_decl_specifier_seq *decl_specifiers,
12900 tree *maybe_range_for_decl,
12901 location_t *init_loc)
12902 {
12903 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12904 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12905 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12906
12907 /* Parse the identifier-list. */
12908 auto_vec<cp_expr, 10> v;
12909 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12910 while (true)
12911 {
12912 cp_expr e = cp_parser_identifier (parser);
12913 if (e.get_value () == error_mark_node)
12914 break;
12915 v.safe_push (e);
12916 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12917 break;
12918 cp_lexer_consume_token (parser->lexer);
12919 }
12920
12921 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12922 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12923 {
12924 end_loc = UNKNOWN_LOCATION;
12925 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12926 false);
12927 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12928 cp_lexer_consume_token (parser->lexer);
12929 else
12930 {
12931 cp_parser_skip_to_end_of_statement (parser);
12932 return error_mark_node;
12933 }
12934 }
12935
12936 if (cxx_dialect < cxx1z)
12937 pedwarn (loc, 0, "decomposition declaration only available with "
12938 "-std=c++1z or -std=gnu++1z");
12939
12940 tree pushed_scope;
12941 cp_declarator *declarator = make_declarator (cdk_decomp);
12942 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
12943 declarator->id_loc = loc;
12944 if (ref_qual != REF_QUAL_NONE)
12945 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
12946 ref_qual == REF_QUAL_RVALUE,
12947 NULL_TREE);
12948 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
12949 NULL_TREE, decl_specifiers->attributes,
12950 &pushed_scope);
12951 tree orig_decl = decl;
12952
12953 unsigned int i;
12954 cp_expr e;
12955 cp_decl_specifier_seq decl_specs;
12956 clear_decl_specs (&decl_specs);
12957 decl_specs.type = make_auto ();
12958 tree prev = decl;
12959 FOR_EACH_VEC_ELT (v, i, e)
12960 {
12961 if (i == 0)
12962 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
12963 else
12964 declarator->u.id.unqualified_name = e.get_value ();
12965 declarator->id_loc = e.get_location ();
12966 tree elt_pushed_scope;
12967 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
12968 NULL_TREE, NULL_TREE, &elt_pushed_scope);
12969 if (decl2 == error_mark_node)
12970 decl = error_mark_node;
12971 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
12972 {
12973 /* Ensure we've diagnosed redeclaration if we aren't creating
12974 a new VAR_DECL. */
12975 gcc_assert (errorcount);
12976 decl = error_mark_node;
12977 }
12978 else
12979 prev = decl2;
12980 if (elt_pushed_scope)
12981 pop_scope (elt_pushed_scope);
12982 }
12983
12984 if (v.is_empty ())
12985 {
12986 error_at (loc, "empty decomposition declaration");
12987 decl = error_mark_node;
12988 }
12989
12990 if (maybe_range_for_decl == NULL
12991 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12992 {
12993 bool non_constant_p = false, is_direct_init = false;
12994 tree initializer;
12995 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
12996 /* Parse the initializer. */
12997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12998 {
12999 initializer = cp_parser_braced_list (parser, &non_constant_p);
13000 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13001 is_direct_init = true;
13002 }
13003 else
13004 {
13005 /* Consume the `='. */
13006 cp_parser_require (parser, CPP_EQ, RT_EQ);
13007 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
13008 }
13009
13010 if (decl != error_mark_node)
13011 {
13012 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13013 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13014 cp_finish_decomp (decl, prev, v.length ());
13015 }
13016 }
13017 else if (decl != error_mark_node)
13018 {
13019 *maybe_range_for_decl = prev;
13020 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13021 the underlying DECL. */
13022 cp_finish_decomp (decl, prev, v.length ());
13023 }
13024
13025 if (pushed_scope)
13026 pop_scope (pushed_scope);
13027
13028 if (decl == error_mark_node && DECL_P (orig_decl))
13029 {
13030 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13031 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13032 }
13033
13034 return decl;
13035 }
13036
13037 /* Parse a decl-specifier-seq.
13038
13039 decl-specifier-seq:
13040 decl-specifier-seq [opt] decl-specifier
13041 decl-specifier attribute-specifier-seq [opt] (C++11)
13042
13043 decl-specifier:
13044 storage-class-specifier
13045 type-specifier
13046 function-specifier
13047 friend
13048 typedef
13049
13050 GNU Extension:
13051
13052 decl-specifier:
13053 attributes
13054
13055 Concepts Extension:
13056
13057 decl-specifier:
13058 concept
13059
13060 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13061
13062 The parser flags FLAGS is used to control type-specifier parsing.
13063
13064 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13065 flags:
13066
13067 1: one of the decl-specifiers is an elaborated-type-specifier
13068 (i.e., a type declaration)
13069 2: one of the decl-specifiers is an enum-specifier or a
13070 class-specifier (i.e., a type definition)
13071
13072 */
13073
13074 static void
13075 cp_parser_decl_specifier_seq (cp_parser* parser,
13076 cp_parser_flags flags,
13077 cp_decl_specifier_seq *decl_specs,
13078 int* declares_class_or_enum)
13079 {
13080 bool constructor_possible_p = !parser->in_declarator_p;
13081 bool found_decl_spec = false;
13082 cp_token *start_token = NULL;
13083 cp_decl_spec ds;
13084
13085 /* Clear DECL_SPECS. */
13086 clear_decl_specs (decl_specs);
13087
13088 /* Assume no class or enumeration type is declared. */
13089 *declares_class_or_enum = 0;
13090
13091 /* Keep reading specifiers until there are no more to read. */
13092 while (true)
13093 {
13094 bool constructor_p;
13095 cp_token *token;
13096 ds = ds_last;
13097
13098 /* Peek at the next token. */
13099 token = cp_lexer_peek_token (parser->lexer);
13100
13101 /* Save the first token of the decl spec list for error
13102 reporting. */
13103 if (!start_token)
13104 start_token = token;
13105 /* Handle attributes. */
13106 if (cp_next_tokens_can_be_attribute_p (parser))
13107 {
13108 /* Parse the attributes. */
13109 tree attrs = cp_parser_attributes_opt (parser);
13110
13111 /* In a sequence of declaration specifiers, c++11 attributes
13112 appertain to the type that precede them. In that case
13113 [dcl.spec]/1 says:
13114
13115 The attribute-specifier-seq affects the type only for
13116 the declaration it appears in, not other declarations
13117 involving the same type.
13118
13119 But for now let's force the user to position the
13120 attribute either at the beginning of the declaration or
13121 after the declarator-id, which would clearly mean that it
13122 applies to the declarator. */
13123 if (cxx11_attribute_p (attrs))
13124 {
13125 if (!found_decl_spec)
13126 /* The c++11 attribute is at the beginning of the
13127 declaration. It appertains to the entity being
13128 declared. */;
13129 else
13130 {
13131 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13132 {
13133 /* This is an attribute following a
13134 class-specifier. */
13135 if (decl_specs->type_definition_p)
13136 warn_misplaced_attr_for_class_type (token->location,
13137 decl_specs->type);
13138 attrs = NULL_TREE;
13139 }
13140 else
13141 {
13142 decl_specs->std_attributes
13143 = chainon (decl_specs->std_attributes,
13144 attrs);
13145 if (decl_specs->locations[ds_std_attribute] == 0)
13146 decl_specs->locations[ds_std_attribute] = token->location;
13147 }
13148 continue;
13149 }
13150 }
13151
13152 decl_specs->attributes
13153 = chainon (decl_specs->attributes,
13154 attrs);
13155 if (decl_specs->locations[ds_attribute] == 0)
13156 decl_specs->locations[ds_attribute] = token->location;
13157 continue;
13158 }
13159 /* Assume we will find a decl-specifier keyword. */
13160 found_decl_spec = true;
13161 /* If the next token is an appropriate keyword, we can simply
13162 add it to the list. */
13163 switch (token->keyword)
13164 {
13165 /* decl-specifier:
13166 friend
13167 constexpr */
13168 case RID_FRIEND:
13169 if (!at_class_scope_p ())
13170 {
13171 error_at (token->location, "%<friend%> used outside of class");
13172 cp_lexer_purge_token (parser->lexer);
13173 }
13174 else
13175 {
13176 ds = ds_friend;
13177 /* Consume the token. */
13178 cp_lexer_consume_token (parser->lexer);
13179 }
13180 break;
13181
13182 case RID_CONSTEXPR:
13183 ds = ds_constexpr;
13184 cp_lexer_consume_token (parser->lexer);
13185 break;
13186
13187 case RID_CONCEPT:
13188 ds = ds_concept;
13189 cp_lexer_consume_token (parser->lexer);
13190 break;
13191
13192 /* function-specifier:
13193 inline
13194 virtual
13195 explicit */
13196 case RID_INLINE:
13197 case RID_VIRTUAL:
13198 case RID_EXPLICIT:
13199 cp_parser_function_specifier_opt (parser, decl_specs);
13200 break;
13201
13202 /* decl-specifier:
13203 typedef */
13204 case RID_TYPEDEF:
13205 ds = ds_typedef;
13206 /* Consume the token. */
13207 cp_lexer_consume_token (parser->lexer);
13208 /* A constructor declarator cannot appear in a typedef. */
13209 constructor_possible_p = false;
13210 /* The "typedef" keyword can only occur in a declaration; we
13211 may as well commit at this point. */
13212 cp_parser_commit_to_tentative_parse (parser);
13213
13214 if (decl_specs->storage_class != sc_none)
13215 decl_specs->conflicting_specifiers_p = true;
13216 break;
13217
13218 /* storage-class-specifier:
13219 auto
13220 register
13221 static
13222 extern
13223 mutable
13224
13225 GNU Extension:
13226 thread */
13227 case RID_AUTO:
13228 if (cxx_dialect == cxx98)
13229 {
13230 /* Consume the token. */
13231 cp_lexer_consume_token (parser->lexer);
13232
13233 /* Complain about `auto' as a storage specifier, if
13234 we're complaining about C++0x compatibility. */
13235 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13236 " changes meaning in C++11; please remove it");
13237
13238 /* Set the storage class anyway. */
13239 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13240 token);
13241 }
13242 else
13243 /* C++0x auto type-specifier. */
13244 found_decl_spec = false;
13245 break;
13246
13247 case RID_REGISTER:
13248 case RID_STATIC:
13249 case RID_EXTERN:
13250 case RID_MUTABLE:
13251 /* Consume the token. */
13252 cp_lexer_consume_token (parser->lexer);
13253 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13254 token);
13255 break;
13256 case RID_THREAD:
13257 /* Consume the token. */
13258 ds = ds_thread;
13259 cp_lexer_consume_token (parser->lexer);
13260 break;
13261
13262 default:
13263 /* We did not yet find a decl-specifier yet. */
13264 found_decl_spec = false;
13265 break;
13266 }
13267
13268 if (found_decl_spec
13269 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13270 && token->keyword != RID_CONSTEXPR)
13271 error ("decl-specifier invalid in condition");
13272
13273 if (found_decl_spec
13274 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13275 && token->keyword != RID_MUTABLE
13276 && token->keyword != RID_CONSTEXPR)
13277 error_at (token->location, "%qD invalid in lambda",
13278 ridpointers[token->keyword]);
13279
13280 if (ds != ds_last)
13281 set_and_check_decl_spec_loc (decl_specs, ds, token);
13282
13283 /* Constructors are a special case. The `S' in `S()' is not a
13284 decl-specifier; it is the beginning of the declarator. */
13285 constructor_p
13286 = (!found_decl_spec
13287 && constructor_possible_p
13288 && (cp_parser_constructor_declarator_p
13289 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13290
13291 /* If we don't have a DECL_SPEC yet, then we must be looking at
13292 a type-specifier. */
13293 if (!found_decl_spec && !constructor_p)
13294 {
13295 int decl_spec_declares_class_or_enum;
13296 bool is_cv_qualifier;
13297 tree type_spec;
13298
13299 type_spec
13300 = cp_parser_type_specifier (parser, flags,
13301 decl_specs,
13302 /*is_declaration=*/true,
13303 &decl_spec_declares_class_or_enum,
13304 &is_cv_qualifier);
13305 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13306
13307 /* If this type-specifier referenced a user-defined type
13308 (a typedef, class-name, etc.), then we can't allow any
13309 more such type-specifiers henceforth.
13310
13311 [dcl.spec]
13312
13313 The longest sequence of decl-specifiers that could
13314 possibly be a type name is taken as the
13315 decl-specifier-seq of a declaration. The sequence shall
13316 be self-consistent as described below.
13317
13318 [dcl.type]
13319
13320 As a general rule, at most one type-specifier is allowed
13321 in the complete decl-specifier-seq of a declaration. The
13322 only exceptions are the following:
13323
13324 -- const or volatile can be combined with any other
13325 type-specifier.
13326
13327 -- signed or unsigned can be combined with char, long,
13328 short, or int.
13329
13330 -- ..
13331
13332 Example:
13333
13334 typedef char* Pc;
13335 void g (const int Pc);
13336
13337 Here, Pc is *not* part of the decl-specifier seq; it's
13338 the declarator. Therefore, once we see a type-specifier
13339 (other than a cv-qualifier), we forbid any additional
13340 user-defined types. We *do* still allow things like `int
13341 int' to be considered a decl-specifier-seq, and issue the
13342 error message later. */
13343 if (type_spec && !is_cv_qualifier)
13344 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13345 /* A constructor declarator cannot follow a type-specifier. */
13346 if (type_spec)
13347 {
13348 constructor_possible_p = false;
13349 found_decl_spec = true;
13350 if (!is_cv_qualifier)
13351 decl_specs->any_type_specifiers_p = true;
13352 }
13353 }
13354
13355 /* If we still do not have a DECL_SPEC, then there are no more
13356 decl-specifiers. */
13357 if (!found_decl_spec)
13358 break;
13359
13360 decl_specs->any_specifiers_p = true;
13361 /* After we see one decl-specifier, further decl-specifiers are
13362 always optional. */
13363 flags |= CP_PARSER_FLAGS_OPTIONAL;
13364 }
13365
13366 /* Don't allow a friend specifier with a class definition. */
13367 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13368 && (*declares_class_or_enum & 2))
13369 error_at (decl_specs->locations[ds_friend],
13370 "class definition may not be declared a friend");
13371 }
13372
13373 /* Parse an (optional) storage-class-specifier.
13374
13375 storage-class-specifier:
13376 auto
13377 register
13378 static
13379 extern
13380 mutable
13381
13382 GNU Extension:
13383
13384 storage-class-specifier:
13385 thread
13386
13387 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13388
13389 static tree
13390 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13391 {
13392 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13393 {
13394 case RID_AUTO:
13395 if (cxx_dialect != cxx98)
13396 return NULL_TREE;
13397 /* Fall through for C++98. */
13398 gcc_fallthrough ();
13399
13400 case RID_REGISTER:
13401 case RID_STATIC:
13402 case RID_EXTERN:
13403 case RID_MUTABLE:
13404 case RID_THREAD:
13405 /* Consume the token. */
13406 return cp_lexer_consume_token (parser->lexer)->u.value;
13407
13408 default:
13409 return NULL_TREE;
13410 }
13411 }
13412
13413 /* Parse an (optional) function-specifier.
13414
13415 function-specifier:
13416 inline
13417 virtual
13418 explicit
13419
13420 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13421 Updates DECL_SPECS, if it is non-NULL. */
13422
13423 static tree
13424 cp_parser_function_specifier_opt (cp_parser* parser,
13425 cp_decl_specifier_seq *decl_specs)
13426 {
13427 cp_token *token = cp_lexer_peek_token (parser->lexer);
13428 switch (token->keyword)
13429 {
13430 case RID_INLINE:
13431 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13432 break;
13433
13434 case RID_VIRTUAL:
13435 /* 14.5.2.3 [temp.mem]
13436
13437 A member function template shall not be virtual. */
13438 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13439 && current_class_type)
13440 error_at (token->location, "templates may not be %<virtual%>");
13441 else
13442 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13443 break;
13444
13445 case RID_EXPLICIT:
13446 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13447 break;
13448
13449 default:
13450 return NULL_TREE;
13451 }
13452
13453 /* Consume the token. */
13454 return cp_lexer_consume_token (parser->lexer)->u.value;
13455 }
13456
13457 /* Parse a linkage-specification.
13458
13459 linkage-specification:
13460 extern string-literal { declaration-seq [opt] }
13461 extern string-literal declaration */
13462
13463 static void
13464 cp_parser_linkage_specification (cp_parser* parser)
13465 {
13466 tree linkage;
13467
13468 /* Look for the `extern' keyword. */
13469 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13470
13471 /* Look for the string-literal. */
13472 linkage = cp_parser_string_literal (parser, false, false);
13473
13474 /* Transform the literal into an identifier. If the literal is a
13475 wide-character string, or contains embedded NULs, then we can't
13476 handle it as the user wants. */
13477 if (strlen (TREE_STRING_POINTER (linkage))
13478 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13479 {
13480 cp_parser_error (parser, "invalid linkage-specification");
13481 /* Assume C++ linkage. */
13482 linkage = lang_name_cplusplus;
13483 }
13484 else
13485 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13486
13487 /* We're now using the new linkage. */
13488 push_lang_context (linkage);
13489
13490 /* If the next token is a `{', then we're using the first
13491 production. */
13492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13493 {
13494 cp_ensure_no_omp_declare_simd (parser);
13495 cp_ensure_no_oacc_routine (parser);
13496
13497 /* Consume the `{' token. */
13498 cp_lexer_consume_token (parser->lexer);
13499 /* Parse the declarations. */
13500 cp_parser_declaration_seq_opt (parser);
13501 /* Look for the closing `}'. */
13502 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13503 }
13504 /* Otherwise, there's just one declaration. */
13505 else
13506 {
13507 bool saved_in_unbraced_linkage_specification_p;
13508
13509 saved_in_unbraced_linkage_specification_p
13510 = parser->in_unbraced_linkage_specification_p;
13511 parser->in_unbraced_linkage_specification_p = true;
13512 cp_parser_declaration (parser);
13513 parser->in_unbraced_linkage_specification_p
13514 = saved_in_unbraced_linkage_specification_p;
13515 }
13516
13517 /* We're done with the linkage-specification. */
13518 pop_lang_context ();
13519 }
13520
13521 /* Parse a static_assert-declaration.
13522
13523 static_assert-declaration:
13524 static_assert ( constant-expression , string-literal ) ;
13525 static_assert ( constant-expression ) ; (C++1Z)
13526
13527 If MEMBER_P, this static_assert is a class member. */
13528
13529 static void
13530 cp_parser_static_assert(cp_parser *parser, bool member_p)
13531 {
13532 tree condition;
13533 tree message;
13534 cp_token *token;
13535 location_t saved_loc;
13536 bool dummy;
13537
13538 /* Peek at the `static_assert' token so we can keep track of exactly
13539 where the static assertion started. */
13540 token = cp_lexer_peek_token (parser->lexer);
13541 saved_loc = token->location;
13542
13543 /* Look for the `static_assert' keyword. */
13544 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13545 RT_STATIC_ASSERT))
13546 return;
13547
13548 /* We know we are in a static assertion; commit to any tentative
13549 parse. */
13550 if (cp_parser_parsing_tentatively (parser))
13551 cp_parser_commit_to_tentative_parse (parser);
13552
13553 /* Parse the `(' starting the static assertion condition. */
13554 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13555
13556 /* Parse the constant-expression. Allow a non-constant expression
13557 here in order to give better diagnostics in finish_static_assert. */
13558 condition =
13559 cp_parser_constant_expression (parser,
13560 /*allow_non_constant_p=*/true,
13561 /*non_constant_p=*/&dummy);
13562
13563 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13564 {
13565 if (cxx_dialect < cxx1z)
13566 pedwarn (input_location, OPT_Wpedantic,
13567 "static_assert without a message "
13568 "only available with -std=c++1z or -std=gnu++1z");
13569 /* Eat the ')' */
13570 cp_lexer_consume_token (parser->lexer);
13571 message = build_string (1, "");
13572 TREE_TYPE (message) = char_array_type_node;
13573 fix_string_type (message);
13574 }
13575 else
13576 {
13577 /* Parse the separating `,'. */
13578 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13579
13580 /* Parse the string-literal message. */
13581 message = cp_parser_string_literal (parser,
13582 /*translate=*/false,
13583 /*wide_ok=*/true);
13584
13585 /* A `)' completes the static assertion. */
13586 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13587 cp_parser_skip_to_closing_parenthesis (parser,
13588 /*recovering=*/true,
13589 /*or_comma=*/false,
13590 /*consume_paren=*/true);
13591 }
13592
13593 /* A semicolon terminates the declaration. */
13594 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13595
13596 /* Complete the static assertion, which may mean either processing
13597 the static assert now or saving it for template instantiation. */
13598 finish_static_assert (condition, message, saved_loc, member_p);
13599 }
13600
13601 /* Parse the expression in decltype ( expression ). */
13602
13603 static tree
13604 cp_parser_decltype_expr (cp_parser *parser,
13605 bool &id_expression_or_member_access_p)
13606 {
13607 cp_token *id_expr_start_token;
13608 tree expr;
13609
13610 /* Since we're going to preserve any side-effects from this parse, set up a
13611 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13612 in the expression. */
13613 tentative_firewall firewall (parser);
13614
13615 /* First, try parsing an id-expression. */
13616 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13617 cp_parser_parse_tentatively (parser);
13618 expr = cp_parser_id_expression (parser,
13619 /*template_keyword_p=*/false,
13620 /*check_dependency_p=*/true,
13621 /*template_p=*/NULL,
13622 /*declarator_p=*/false,
13623 /*optional_p=*/false);
13624
13625 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13626 {
13627 bool non_integral_constant_expression_p = false;
13628 tree id_expression = expr;
13629 cp_id_kind idk;
13630 const char *error_msg;
13631
13632 if (identifier_p (expr))
13633 /* Lookup the name we got back from the id-expression. */
13634 expr = cp_parser_lookup_name_simple (parser, expr,
13635 id_expr_start_token->location);
13636
13637 if (expr
13638 && expr != error_mark_node
13639 && TREE_CODE (expr) != TYPE_DECL
13640 && (TREE_CODE (expr) != BIT_NOT_EXPR
13641 || !TYPE_P (TREE_OPERAND (expr, 0)))
13642 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13643 {
13644 /* Complete lookup of the id-expression. */
13645 expr = (finish_id_expression
13646 (id_expression, expr, parser->scope, &idk,
13647 /*integral_constant_expression_p=*/false,
13648 /*allow_non_integral_constant_expression_p=*/true,
13649 &non_integral_constant_expression_p,
13650 /*template_p=*/false,
13651 /*done=*/true,
13652 /*address_p=*/false,
13653 /*template_arg_p=*/false,
13654 &error_msg,
13655 id_expr_start_token->location));
13656
13657 if (expr == error_mark_node)
13658 /* We found an id-expression, but it was something that we
13659 should not have found. This is an error, not something
13660 we can recover from, so note that we found an
13661 id-expression and we'll recover as gracefully as
13662 possible. */
13663 id_expression_or_member_access_p = true;
13664 }
13665
13666 if (expr
13667 && expr != error_mark_node
13668 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13669 /* We have an id-expression. */
13670 id_expression_or_member_access_p = true;
13671 }
13672
13673 if (!id_expression_or_member_access_p)
13674 {
13675 /* Abort the id-expression parse. */
13676 cp_parser_abort_tentative_parse (parser);
13677
13678 /* Parsing tentatively, again. */
13679 cp_parser_parse_tentatively (parser);
13680
13681 /* Parse a class member access. */
13682 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13683 /*cast_p=*/false, /*decltype*/true,
13684 /*member_access_only_p=*/true, NULL);
13685
13686 if (expr
13687 && expr != error_mark_node
13688 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13689 /* We have an id-expression. */
13690 id_expression_or_member_access_p = true;
13691 }
13692
13693 if (id_expression_or_member_access_p)
13694 /* We have parsed the complete id-expression or member access. */
13695 cp_parser_parse_definitely (parser);
13696 else
13697 {
13698 /* Abort our attempt to parse an id-expression or member access
13699 expression. */
13700 cp_parser_abort_tentative_parse (parser);
13701
13702 /* Parse a full expression. */
13703 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13704 /*decltype_p=*/true);
13705 }
13706
13707 return expr;
13708 }
13709
13710 /* Parse a `decltype' type. Returns the type.
13711
13712 simple-type-specifier:
13713 decltype ( expression )
13714 C++14 proposal:
13715 decltype ( auto ) */
13716
13717 static tree
13718 cp_parser_decltype (cp_parser *parser)
13719 {
13720 tree expr;
13721 bool id_expression_or_member_access_p = false;
13722 const char *saved_message;
13723 bool saved_integral_constant_expression_p;
13724 bool saved_non_integral_constant_expression_p;
13725 bool saved_greater_than_is_operator_p;
13726 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13727
13728 if (start_token->type == CPP_DECLTYPE)
13729 {
13730 /* Already parsed. */
13731 cp_lexer_consume_token (parser->lexer);
13732 return saved_checks_value (start_token->u.tree_check_value);
13733 }
13734
13735 /* Look for the `decltype' token. */
13736 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13737 return error_mark_node;
13738
13739 /* Parse the opening `('. */
13740 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13741 return error_mark_node;
13742
13743 /* decltype (auto) */
13744 if (cxx_dialect >= cxx14
13745 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13746 {
13747 cp_lexer_consume_token (parser->lexer);
13748 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13749 return error_mark_node;
13750 expr = make_decltype_auto ();
13751 AUTO_IS_DECLTYPE (expr) = true;
13752 goto rewrite;
13753 }
13754
13755 /* Types cannot be defined in a `decltype' expression. Save away the
13756 old message. */
13757 saved_message = parser->type_definition_forbidden_message;
13758
13759 /* And create the new one. */
13760 parser->type_definition_forbidden_message
13761 = G_("types may not be defined in %<decltype%> expressions");
13762
13763 /* The restrictions on constant-expressions do not apply inside
13764 decltype expressions. */
13765 saved_integral_constant_expression_p
13766 = parser->integral_constant_expression_p;
13767 saved_non_integral_constant_expression_p
13768 = parser->non_integral_constant_expression_p;
13769 parser->integral_constant_expression_p = false;
13770
13771 /* Within a parenthesized expression, a `>' token is always
13772 the greater-than operator. */
13773 saved_greater_than_is_operator_p
13774 = parser->greater_than_is_operator_p;
13775 parser->greater_than_is_operator_p = true;
13776
13777 /* Do not actually evaluate the expression. */
13778 ++cp_unevaluated_operand;
13779
13780 /* Do not warn about problems with the expression. */
13781 ++c_inhibit_evaluation_warnings;
13782
13783 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13784
13785 /* Go back to evaluating expressions. */
13786 --cp_unevaluated_operand;
13787 --c_inhibit_evaluation_warnings;
13788
13789 /* The `>' token might be the end of a template-id or
13790 template-parameter-list now. */
13791 parser->greater_than_is_operator_p
13792 = saved_greater_than_is_operator_p;
13793
13794 /* Restore the old message and the integral constant expression
13795 flags. */
13796 parser->type_definition_forbidden_message = saved_message;
13797 parser->integral_constant_expression_p
13798 = saved_integral_constant_expression_p;
13799 parser->non_integral_constant_expression_p
13800 = saved_non_integral_constant_expression_p;
13801
13802 /* Parse to the closing `)'. */
13803 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13804 {
13805 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13806 /*consume_paren=*/true);
13807 return error_mark_node;
13808 }
13809
13810 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13811 tf_warning_or_error);
13812
13813 rewrite:
13814 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13815 it again. */
13816 start_token->type = CPP_DECLTYPE;
13817 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13818 start_token->u.tree_check_value->value = expr;
13819 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13820 start_token->keyword = RID_MAX;
13821 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13822
13823 return expr;
13824 }
13825
13826 /* Special member functions [gram.special] */
13827
13828 /* Parse a conversion-function-id.
13829
13830 conversion-function-id:
13831 operator conversion-type-id
13832
13833 Returns an IDENTIFIER_NODE representing the operator. */
13834
13835 static tree
13836 cp_parser_conversion_function_id (cp_parser* parser)
13837 {
13838 tree type;
13839 tree saved_scope;
13840 tree saved_qualifying_scope;
13841 tree saved_object_scope;
13842 tree pushed_scope = NULL_TREE;
13843
13844 /* Look for the `operator' token. */
13845 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13846 return error_mark_node;
13847 /* When we parse the conversion-type-id, the current scope will be
13848 reset. However, we need that information in able to look up the
13849 conversion function later, so we save it here. */
13850 saved_scope = parser->scope;
13851 saved_qualifying_scope = parser->qualifying_scope;
13852 saved_object_scope = parser->object_scope;
13853 /* We must enter the scope of the class so that the names of
13854 entities declared within the class are available in the
13855 conversion-type-id. For example, consider:
13856
13857 struct S {
13858 typedef int I;
13859 operator I();
13860 };
13861
13862 S::operator I() { ... }
13863
13864 In order to see that `I' is a type-name in the definition, we
13865 must be in the scope of `S'. */
13866 if (saved_scope)
13867 pushed_scope = push_scope (saved_scope);
13868 /* Parse the conversion-type-id. */
13869 type = cp_parser_conversion_type_id (parser);
13870 /* Leave the scope of the class, if any. */
13871 if (pushed_scope)
13872 pop_scope (pushed_scope);
13873 /* Restore the saved scope. */
13874 parser->scope = saved_scope;
13875 parser->qualifying_scope = saved_qualifying_scope;
13876 parser->object_scope = saved_object_scope;
13877 /* If the TYPE is invalid, indicate failure. */
13878 if (type == error_mark_node)
13879 return error_mark_node;
13880 return mangle_conv_op_name_for_type (type);
13881 }
13882
13883 /* Parse a conversion-type-id:
13884
13885 conversion-type-id:
13886 type-specifier-seq conversion-declarator [opt]
13887
13888 Returns the TYPE specified. */
13889
13890 static tree
13891 cp_parser_conversion_type_id (cp_parser* parser)
13892 {
13893 tree attributes;
13894 cp_decl_specifier_seq type_specifiers;
13895 cp_declarator *declarator;
13896 tree type_specified;
13897 const char *saved_message;
13898
13899 /* Parse the attributes. */
13900 attributes = cp_parser_attributes_opt (parser);
13901
13902 saved_message = parser->type_definition_forbidden_message;
13903 parser->type_definition_forbidden_message
13904 = G_("types may not be defined in a conversion-type-id");
13905
13906 /* Parse the type-specifiers. */
13907 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13908 /*is_trailing_return=*/false,
13909 &type_specifiers);
13910
13911 parser->type_definition_forbidden_message = saved_message;
13912
13913 /* If that didn't work, stop. */
13914 if (type_specifiers.type == error_mark_node)
13915 return error_mark_node;
13916 /* Parse the conversion-declarator. */
13917 declarator = cp_parser_conversion_declarator_opt (parser);
13918
13919 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13920 /*initialized=*/0, &attributes);
13921 if (attributes)
13922 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13923
13924 /* Don't give this error when parsing tentatively. This happens to
13925 work because we always parse this definitively once. */
13926 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13927 && type_uses_auto (type_specified))
13928 {
13929 if (cxx_dialect < cxx14)
13930 {
13931 error ("invalid use of %<auto%> in conversion operator");
13932 return error_mark_node;
13933 }
13934 else if (template_parm_scope_p ())
13935 warning (0, "use of %<auto%> in member template "
13936 "conversion operator can never be deduced");
13937 }
13938
13939 return type_specified;
13940 }
13941
13942 /* Parse an (optional) conversion-declarator.
13943
13944 conversion-declarator:
13945 ptr-operator conversion-declarator [opt]
13946
13947 */
13948
13949 static cp_declarator *
13950 cp_parser_conversion_declarator_opt (cp_parser* parser)
13951 {
13952 enum tree_code code;
13953 tree class_type, std_attributes = NULL_TREE;
13954 cp_cv_quals cv_quals;
13955
13956 /* We don't know if there's a ptr-operator next, or not. */
13957 cp_parser_parse_tentatively (parser);
13958 /* Try the ptr-operator. */
13959 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13960 &std_attributes);
13961 /* If it worked, look for more conversion-declarators. */
13962 if (cp_parser_parse_definitely (parser))
13963 {
13964 cp_declarator *declarator;
13965
13966 /* Parse another optional declarator. */
13967 declarator = cp_parser_conversion_declarator_opt (parser);
13968
13969 declarator = cp_parser_make_indirect_declarator
13970 (code, class_type, cv_quals, declarator, std_attributes);
13971
13972 return declarator;
13973 }
13974
13975 return NULL;
13976 }
13977
13978 /* Parse an (optional) ctor-initializer.
13979
13980 ctor-initializer:
13981 : mem-initializer-list
13982
13983 Returns TRUE iff the ctor-initializer was actually present. */
13984
13985 static bool
13986 cp_parser_ctor_initializer_opt (cp_parser* parser)
13987 {
13988 /* If the next token is not a `:', then there is no
13989 ctor-initializer. */
13990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13991 {
13992 /* Do default initialization of any bases and members. */
13993 if (DECL_CONSTRUCTOR_P (current_function_decl))
13994 finish_mem_initializers (NULL_TREE);
13995
13996 return false;
13997 }
13998
13999 /* Consume the `:' token. */
14000 cp_lexer_consume_token (parser->lexer);
14001 /* And the mem-initializer-list. */
14002 cp_parser_mem_initializer_list (parser);
14003
14004 return true;
14005 }
14006
14007 /* Parse a mem-initializer-list.
14008
14009 mem-initializer-list:
14010 mem-initializer ... [opt]
14011 mem-initializer ... [opt] , mem-initializer-list */
14012
14013 static void
14014 cp_parser_mem_initializer_list (cp_parser* parser)
14015 {
14016 tree mem_initializer_list = NULL_TREE;
14017 tree target_ctor = error_mark_node;
14018 cp_token *token = cp_lexer_peek_token (parser->lexer);
14019
14020 /* Let the semantic analysis code know that we are starting the
14021 mem-initializer-list. */
14022 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14023 error_at (token->location,
14024 "only constructors take member initializers");
14025
14026 /* Loop through the list. */
14027 while (true)
14028 {
14029 tree mem_initializer;
14030
14031 token = cp_lexer_peek_token (parser->lexer);
14032 /* Parse the mem-initializer. */
14033 mem_initializer = cp_parser_mem_initializer (parser);
14034 /* If the next token is a `...', we're expanding member initializers. */
14035 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14036 {
14037 /* Consume the `...'. */
14038 cp_lexer_consume_token (parser->lexer);
14039
14040 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14041 can be expanded but members cannot. */
14042 if (mem_initializer != error_mark_node
14043 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14044 {
14045 error_at (token->location,
14046 "cannot expand initializer for member %<%D%>",
14047 TREE_PURPOSE (mem_initializer));
14048 mem_initializer = error_mark_node;
14049 }
14050
14051 /* Construct the pack expansion type. */
14052 if (mem_initializer != error_mark_node)
14053 mem_initializer = make_pack_expansion (mem_initializer);
14054 }
14055 if (target_ctor != error_mark_node
14056 && mem_initializer != error_mark_node)
14057 {
14058 error ("mem-initializer for %qD follows constructor delegation",
14059 TREE_PURPOSE (mem_initializer));
14060 mem_initializer = error_mark_node;
14061 }
14062 /* Look for a target constructor. */
14063 if (mem_initializer != error_mark_node
14064 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14065 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14066 {
14067 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14068 if (mem_initializer_list)
14069 {
14070 error ("constructor delegation follows mem-initializer for %qD",
14071 TREE_PURPOSE (mem_initializer_list));
14072 mem_initializer = error_mark_node;
14073 }
14074 target_ctor = mem_initializer;
14075 }
14076 /* Add it to the list, unless it was erroneous. */
14077 if (mem_initializer != error_mark_node)
14078 {
14079 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14080 mem_initializer_list = mem_initializer;
14081 }
14082 /* If the next token is not a `,', we're done. */
14083 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14084 break;
14085 /* Consume the `,' token. */
14086 cp_lexer_consume_token (parser->lexer);
14087 }
14088
14089 /* Perform semantic analysis. */
14090 if (DECL_CONSTRUCTOR_P (current_function_decl))
14091 finish_mem_initializers (mem_initializer_list);
14092 }
14093
14094 /* Parse a mem-initializer.
14095
14096 mem-initializer:
14097 mem-initializer-id ( expression-list [opt] )
14098 mem-initializer-id braced-init-list
14099
14100 GNU extension:
14101
14102 mem-initializer:
14103 ( expression-list [opt] )
14104
14105 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14106 class) or FIELD_DECL (for a non-static data member) to initialize;
14107 the TREE_VALUE is the expression-list. An empty initialization
14108 list is represented by void_list_node. */
14109
14110 static tree
14111 cp_parser_mem_initializer (cp_parser* parser)
14112 {
14113 tree mem_initializer_id;
14114 tree expression_list;
14115 tree member;
14116 cp_token *token = cp_lexer_peek_token (parser->lexer);
14117
14118 /* Find out what is being initialized. */
14119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14120 {
14121 permerror (token->location,
14122 "anachronistic old-style base class initializer");
14123 mem_initializer_id = NULL_TREE;
14124 }
14125 else
14126 {
14127 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14128 if (mem_initializer_id == error_mark_node)
14129 return mem_initializer_id;
14130 }
14131 member = expand_member_init (mem_initializer_id);
14132 if (member && !DECL_P (member))
14133 in_base_initializer = 1;
14134
14135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14136 {
14137 bool expr_non_constant_p;
14138 cp_lexer_set_source_position (parser->lexer);
14139 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14140 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14141 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14142 expression_list = build_tree_list (NULL_TREE, expression_list);
14143 }
14144 else
14145 {
14146 vec<tree, va_gc> *vec;
14147 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14148 /*cast_p=*/false,
14149 /*allow_expansion_p=*/true,
14150 /*non_constant_p=*/NULL);
14151 if (vec == NULL)
14152 return error_mark_node;
14153 expression_list = build_tree_list_vec (vec);
14154 release_tree_vector (vec);
14155 }
14156
14157 if (expression_list == error_mark_node)
14158 return error_mark_node;
14159 if (!expression_list)
14160 expression_list = void_type_node;
14161
14162 in_base_initializer = 0;
14163
14164 return member ? build_tree_list (member, expression_list) : error_mark_node;
14165 }
14166
14167 /* Parse a mem-initializer-id.
14168
14169 mem-initializer-id:
14170 :: [opt] nested-name-specifier [opt] class-name
14171 decltype-specifier (C++11)
14172 identifier
14173
14174 Returns a TYPE indicating the class to be initialized for the first
14175 production (and the second in C++11). Returns an IDENTIFIER_NODE
14176 indicating the data member to be initialized for the last production. */
14177
14178 static tree
14179 cp_parser_mem_initializer_id (cp_parser* parser)
14180 {
14181 bool global_scope_p;
14182 bool nested_name_specifier_p;
14183 bool template_p = false;
14184 tree id;
14185
14186 cp_token *token = cp_lexer_peek_token (parser->lexer);
14187
14188 /* `typename' is not allowed in this context ([temp.res]). */
14189 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14190 {
14191 error_at (token->location,
14192 "keyword %<typename%> not allowed in this context (a qualified "
14193 "member initializer is implicitly a type)");
14194 cp_lexer_consume_token (parser->lexer);
14195 }
14196 /* Look for the optional `::' operator. */
14197 global_scope_p
14198 = (cp_parser_global_scope_opt (parser,
14199 /*current_scope_valid_p=*/false)
14200 != NULL_TREE);
14201 /* Look for the optional nested-name-specifier. The simplest way to
14202 implement:
14203
14204 [temp.res]
14205
14206 The keyword `typename' is not permitted in a base-specifier or
14207 mem-initializer; in these contexts a qualified name that
14208 depends on a template-parameter is implicitly assumed to be a
14209 type name.
14210
14211 is to assume that we have seen the `typename' keyword at this
14212 point. */
14213 nested_name_specifier_p
14214 = (cp_parser_nested_name_specifier_opt (parser,
14215 /*typename_keyword_p=*/true,
14216 /*check_dependency_p=*/true,
14217 /*type_p=*/true,
14218 /*is_declaration=*/true)
14219 != NULL_TREE);
14220 if (nested_name_specifier_p)
14221 template_p = cp_parser_optional_template_keyword (parser);
14222 /* If there is a `::' operator or a nested-name-specifier, then we
14223 are definitely looking for a class-name. */
14224 if (global_scope_p || nested_name_specifier_p)
14225 return cp_parser_class_name (parser,
14226 /*typename_keyword_p=*/true,
14227 /*template_keyword_p=*/template_p,
14228 typename_type,
14229 /*check_dependency_p=*/true,
14230 /*class_head_p=*/false,
14231 /*is_declaration=*/true);
14232 /* Otherwise, we could also be looking for an ordinary identifier. */
14233 cp_parser_parse_tentatively (parser);
14234 if (cp_lexer_next_token_is_decltype (parser->lexer))
14235 /* Try a decltype-specifier. */
14236 id = cp_parser_decltype (parser);
14237 else
14238 /* Otherwise, try a class-name. */
14239 id = cp_parser_class_name (parser,
14240 /*typename_keyword_p=*/true,
14241 /*template_keyword_p=*/false,
14242 none_type,
14243 /*check_dependency_p=*/true,
14244 /*class_head_p=*/false,
14245 /*is_declaration=*/true);
14246 /* If we found one, we're done. */
14247 if (cp_parser_parse_definitely (parser))
14248 return id;
14249 /* Otherwise, look for an ordinary identifier. */
14250 return cp_parser_identifier (parser);
14251 }
14252
14253 /* Overloading [gram.over] */
14254
14255 /* Parse an operator-function-id.
14256
14257 operator-function-id:
14258 operator operator
14259
14260 Returns an IDENTIFIER_NODE for the operator which is a
14261 human-readable spelling of the identifier, e.g., `operator +'. */
14262
14263 static cp_expr
14264 cp_parser_operator_function_id (cp_parser* parser)
14265 {
14266 /* Look for the `operator' keyword. */
14267 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14268 return error_mark_node;
14269 /* And then the name of the operator itself. */
14270 return cp_parser_operator (parser);
14271 }
14272
14273 /* Return an identifier node for a user-defined literal operator.
14274 The suffix identifier is chained to the operator name identifier. */
14275
14276 static tree
14277 cp_literal_operator_id (const char* name)
14278 {
14279 tree identifier;
14280 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14281 + strlen (name) + 10);
14282 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14283 identifier = get_identifier (buffer);
14284
14285 return identifier;
14286 }
14287
14288 /* Parse an operator.
14289
14290 operator:
14291 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14292 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14293 || ++ -- , ->* -> () []
14294
14295 GNU Extensions:
14296
14297 operator:
14298 <? >? <?= >?=
14299
14300 Returns an IDENTIFIER_NODE for the operator which is a
14301 human-readable spelling of the identifier, e.g., `operator +'. */
14302
14303 static cp_expr
14304 cp_parser_operator (cp_parser* parser)
14305 {
14306 tree id = NULL_TREE;
14307 cp_token *token;
14308 bool utf8 = false;
14309
14310 /* Peek at the next token. */
14311 token = cp_lexer_peek_token (parser->lexer);
14312
14313 location_t start_loc = token->location;
14314
14315 /* Figure out which operator we have. */
14316 switch (token->type)
14317 {
14318 case CPP_KEYWORD:
14319 {
14320 enum tree_code op;
14321
14322 /* The keyword should be either `new' or `delete'. */
14323 if (token->keyword == RID_NEW)
14324 op = NEW_EXPR;
14325 else if (token->keyword == RID_DELETE)
14326 op = DELETE_EXPR;
14327 else
14328 break;
14329
14330 /* Consume the `new' or `delete' token. */
14331 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14332
14333 /* Peek at the next token. */
14334 token = cp_lexer_peek_token (parser->lexer);
14335 /* If it's a `[' token then this is the array variant of the
14336 operator. */
14337 if (token->type == CPP_OPEN_SQUARE)
14338 {
14339 /* Consume the `[' token. */
14340 cp_lexer_consume_token (parser->lexer);
14341 /* Look for the `]' token. */
14342 if (cp_token *close_token
14343 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14344 end_loc = close_token->location;
14345 id = ansi_opname (op == NEW_EXPR
14346 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14347 }
14348 /* Otherwise, we have the non-array variant. */
14349 else
14350 id = ansi_opname (op);
14351
14352 location_t loc = make_location (start_loc, start_loc, end_loc);
14353
14354 return cp_expr (id, loc);
14355 }
14356
14357 case CPP_PLUS:
14358 id = ansi_opname (PLUS_EXPR);
14359 break;
14360
14361 case CPP_MINUS:
14362 id = ansi_opname (MINUS_EXPR);
14363 break;
14364
14365 case CPP_MULT:
14366 id = ansi_opname (MULT_EXPR);
14367 break;
14368
14369 case CPP_DIV:
14370 id = ansi_opname (TRUNC_DIV_EXPR);
14371 break;
14372
14373 case CPP_MOD:
14374 id = ansi_opname (TRUNC_MOD_EXPR);
14375 break;
14376
14377 case CPP_XOR:
14378 id = ansi_opname (BIT_XOR_EXPR);
14379 break;
14380
14381 case CPP_AND:
14382 id = ansi_opname (BIT_AND_EXPR);
14383 break;
14384
14385 case CPP_OR:
14386 id = ansi_opname (BIT_IOR_EXPR);
14387 break;
14388
14389 case CPP_COMPL:
14390 id = ansi_opname (BIT_NOT_EXPR);
14391 break;
14392
14393 case CPP_NOT:
14394 id = ansi_opname (TRUTH_NOT_EXPR);
14395 break;
14396
14397 case CPP_EQ:
14398 id = ansi_assopname (NOP_EXPR);
14399 break;
14400
14401 case CPP_LESS:
14402 id = ansi_opname (LT_EXPR);
14403 break;
14404
14405 case CPP_GREATER:
14406 id = ansi_opname (GT_EXPR);
14407 break;
14408
14409 case CPP_PLUS_EQ:
14410 id = ansi_assopname (PLUS_EXPR);
14411 break;
14412
14413 case CPP_MINUS_EQ:
14414 id = ansi_assopname (MINUS_EXPR);
14415 break;
14416
14417 case CPP_MULT_EQ:
14418 id = ansi_assopname (MULT_EXPR);
14419 break;
14420
14421 case CPP_DIV_EQ:
14422 id = ansi_assopname (TRUNC_DIV_EXPR);
14423 break;
14424
14425 case CPP_MOD_EQ:
14426 id = ansi_assopname (TRUNC_MOD_EXPR);
14427 break;
14428
14429 case CPP_XOR_EQ:
14430 id = ansi_assopname (BIT_XOR_EXPR);
14431 break;
14432
14433 case CPP_AND_EQ:
14434 id = ansi_assopname (BIT_AND_EXPR);
14435 break;
14436
14437 case CPP_OR_EQ:
14438 id = ansi_assopname (BIT_IOR_EXPR);
14439 break;
14440
14441 case CPP_LSHIFT:
14442 id = ansi_opname (LSHIFT_EXPR);
14443 break;
14444
14445 case CPP_RSHIFT:
14446 id = ansi_opname (RSHIFT_EXPR);
14447 break;
14448
14449 case CPP_LSHIFT_EQ:
14450 id = ansi_assopname (LSHIFT_EXPR);
14451 break;
14452
14453 case CPP_RSHIFT_EQ:
14454 id = ansi_assopname (RSHIFT_EXPR);
14455 break;
14456
14457 case CPP_EQ_EQ:
14458 id = ansi_opname (EQ_EXPR);
14459 break;
14460
14461 case CPP_NOT_EQ:
14462 id = ansi_opname (NE_EXPR);
14463 break;
14464
14465 case CPP_LESS_EQ:
14466 id = ansi_opname (LE_EXPR);
14467 break;
14468
14469 case CPP_GREATER_EQ:
14470 id = ansi_opname (GE_EXPR);
14471 break;
14472
14473 case CPP_AND_AND:
14474 id = ansi_opname (TRUTH_ANDIF_EXPR);
14475 break;
14476
14477 case CPP_OR_OR:
14478 id = ansi_opname (TRUTH_ORIF_EXPR);
14479 break;
14480
14481 case CPP_PLUS_PLUS:
14482 id = ansi_opname (POSTINCREMENT_EXPR);
14483 break;
14484
14485 case CPP_MINUS_MINUS:
14486 id = ansi_opname (PREDECREMENT_EXPR);
14487 break;
14488
14489 case CPP_COMMA:
14490 id = ansi_opname (COMPOUND_EXPR);
14491 break;
14492
14493 case CPP_DEREF_STAR:
14494 id = ansi_opname (MEMBER_REF);
14495 break;
14496
14497 case CPP_DEREF:
14498 id = ansi_opname (COMPONENT_REF);
14499 break;
14500
14501 case CPP_OPEN_PAREN:
14502 /* Consume the `('. */
14503 cp_lexer_consume_token (parser->lexer);
14504 /* Look for the matching `)'. */
14505 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14506 return ansi_opname (CALL_EXPR);
14507
14508 case CPP_OPEN_SQUARE:
14509 /* Consume the `['. */
14510 cp_lexer_consume_token (parser->lexer);
14511 /* Look for the matching `]'. */
14512 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14513 return ansi_opname (ARRAY_REF);
14514
14515 case CPP_UTF8STRING:
14516 case CPP_UTF8STRING_USERDEF:
14517 utf8 = true;
14518 /* FALLTHRU */
14519 case CPP_STRING:
14520 case CPP_WSTRING:
14521 case CPP_STRING16:
14522 case CPP_STRING32:
14523 case CPP_STRING_USERDEF:
14524 case CPP_WSTRING_USERDEF:
14525 case CPP_STRING16_USERDEF:
14526 case CPP_STRING32_USERDEF:
14527 {
14528 tree str, string_tree;
14529 int sz, len;
14530
14531 if (cxx_dialect == cxx98)
14532 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14533
14534 /* Consume the string. */
14535 str = cp_parser_string_literal (parser, /*translate=*/true,
14536 /*wide_ok=*/true, /*lookup_udlit=*/false);
14537 if (str == error_mark_node)
14538 return error_mark_node;
14539 else if (TREE_CODE (str) == USERDEF_LITERAL)
14540 {
14541 string_tree = USERDEF_LITERAL_VALUE (str);
14542 id = USERDEF_LITERAL_SUFFIX_ID (str);
14543 }
14544 else
14545 {
14546 string_tree = str;
14547 /* Look for the suffix identifier. */
14548 token = cp_lexer_peek_token (parser->lexer);
14549 if (token->type == CPP_NAME)
14550 id = cp_parser_identifier (parser);
14551 else if (token->type == CPP_KEYWORD)
14552 {
14553 error ("unexpected keyword;"
14554 " remove space between quotes and suffix identifier");
14555 return error_mark_node;
14556 }
14557 else
14558 {
14559 error ("expected suffix identifier");
14560 return error_mark_node;
14561 }
14562 }
14563 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14564 (TREE_TYPE (TREE_TYPE (string_tree))));
14565 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14566 if (len != 0)
14567 {
14568 error ("expected empty string after %<operator%> keyword");
14569 return error_mark_node;
14570 }
14571 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14572 != char_type_node)
14573 {
14574 error ("invalid encoding prefix in literal operator");
14575 return error_mark_node;
14576 }
14577 if (id != error_mark_node)
14578 {
14579 const char *name = IDENTIFIER_POINTER (id);
14580 id = cp_literal_operator_id (name);
14581 }
14582 return id;
14583 }
14584
14585 default:
14586 /* Anything else is an error. */
14587 break;
14588 }
14589
14590 /* If we have selected an identifier, we need to consume the
14591 operator token. */
14592 if (id)
14593 cp_lexer_consume_token (parser->lexer);
14594 /* Otherwise, no valid operator name was present. */
14595 else
14596 {
14597 cp_parser_error (parser, "expected operator");
14598 id = error_mark_node;
14599 }
14600
14601 return cp_expr (id, start_loc);
14602 }
14603
14604 /* Parse a template-declaration.
14605
14606 template-declaration:
14607 export [opt] template < template-parameter-list > declaration
14608
14609 If MEMBER_P is TRUE, this template-declaration occurs within a
14610 class-specifier.
14611
14612 The grammar rule given by the standard isn't correct. What
14613 is really meant is:
14614
14615 template-declaration:
14616 export [opt] template-parameter-list-seq
14617 decl-specifier-seq [opt] init-declarator [opt] ;
14618 export [opt] template-parameter-list-seq
14619 function-definition
14620
14621 template-parameter-list-seq:
14622 template-parameter-list-seq [opt]
14623 template < template-parameter-list >
14624
14625 Concept Extensions:
14626
14627 template-parameter-list-seq:
14628 template < template-parameter-list > requires-clause [opt]
14629
14630 requires-clause:
14631 requires logical-or-expression */
14632
14633 static void
14634 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14635 {
14636 /* Check for `export'. */
14637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14638 {
14639 /* Consume the `export' token. */
14640 cp_lexer_consume_token (parser->lexer);
14641 /* Warn that we do not support `export'. */
14642 warning (0, "keyword %<export%> not implemented, and will be ignored");
14643 }
14644
14645 cp_parser_template_declaration_after_export (parser, member_p);
14646 }
14647
14648 /* Parse a template-parameter-list.
14649
14650 template-parameter-list:
14651 template-parameter
14652 template-parameter-list , template-parameter
14653
14654 Returns a TREE_LIST. Each node represents a template parameter.
14655 The nodes are connected via their TREE_CHAINs. */
14656
14657 static tree
14658 cp_parser_template_parameter_list (cp_parser* parser)
14659 {
14660 tree parameter_list = NULL_TREE;
14661
14662 begin_template_parm_list ();
14663
14664 /* The loop below parses the template parms. We first need to know
14665 the total number of template parms to be able to compute proper
14666 canonical types of each dependent type. So after the loop, when
14667 we know the total number of template parms,
14668 end_template_parm_list computes the proper canonical types and
14669 fixes up the dependent types accordingly. */
14670 while (true)
14671 {
14672 tree parameter;
14673 bool is_non_type;
14674 bool is_parameter_pack;
14675 location_t parm_loc;
14676
14677 /* Parse the template-parameter. */
14678 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14679 parameter = cp_parser_template_parameter (parser,
14680 &is_non_type,
14681 &is_parameter_pack);
14682 /* Add it to the list. */
14683 if (parameter != error_mark_node)
14684 parameter_list = process_template_parm (parameter_list,
14685 parm_loc,
14686 parameter,
14687 is_non_type,
14688 is_parameter_pack);
14689 else
14690 {
14691 tree err_parm = build_tree_list (parameter, parameter);
14692 parameter_list = chainon (parameter_list, err_parm);
14693 }
14694
14695 /* If the next token is not a `,', we're done. */
14696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14697 break;
14698 /* Otherwise, consume the `,' token. */
14699 cp_lexer_consume_token (parser->lexer);
14700 }
14701
14702 return end_template_parm_list (parameter_list);
14703 }
14704
14705 /* Parse a introduction-list.
14706
14707 introduction-list:
14708 introduced-parameter
14709 introduction-list , introduced-parameter
14710
14711 introduced-parameter:
14712 ...[opt] identifier
14713
14714 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14715 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14716 WILDCARD_DECL will also have DECL_NAME set and token location in
14717 DECL_SOURCE_LOCATION. */
14718
14719 static tree
14720 cp_parser_introduction_list (cp_parser *parser)
14721 {
14722 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14723
14724 while (true)
14725 {
14726 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14727 if (is_pack)
14728 cp_lexer_consume_token (parser->lexer);
14729
14730 /* Build placeholder. */
14731 tree parm = build_nt (WILDCARD_DECL);
14732 DECL_SOURCE_LOCATION (parm)
14733 = cp_lexer_peek_token (parser->lexer)->location;
14734 DECL_NAME (parm) = cp_parser_identifier (parser);
14735 WILDCARD_PACK_P (parm) = is_pack;
14736 vec_safe_push (introduction_vec, parm);
14737
14738 /* If the next token is not a `,', we're done. */
14739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14740 break;
14741 /* Otherwise, consume the `,' token. */
14742 cp_lexer_consume_token (parser->lexer);
14743 }
14744
14745 /* Convert the vec into a TREE_VEC. */
14746 tree introduction_list = make_tree_vec (introduction_vec->length ());
14747 unsigned int n;
14748 tree parm;
14749 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14750 TREE_VEC_ELT (introduction_list, n) = parm;
14751
14752 release_tree_vector (introduction_vec);
14753 return introduction_list;
14754 }
14755
14756 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14757 is an abstract declarator. */
14758
14759 static inline cp_declarator*
14760 get_id_declarator (cp_declarator *declarator)
14761 {
14762 cp_declarator *d = declarator;
14763 while (d && d->kind != cdk_id)
14764 d = d->declarator;
14765 return d;
14766 }
14767
14768 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14769 is an abstract declarator. */
14770
14771 static inline tree
14772 get_unqualified_id (cp_declarator *declarator)
14773 {
14774 declarator = get_id_declarator (declarator);
14775 if (declarator)
14776 return declarator->u.id.unqualified_name;
14777 else
14778 return NULL_TREE;
14779 }
14780
14781 /* Returns true if DECL represents a constrained-parameter. */
14782
14783 static inline bool
14784 is_constrained_parameter (tree decl)
14785 {
14786 return (decl
14787 && TREE_CODE (decl) == TYPE_DECL
14788 && CONSTRAINED_PARM_CONCEPT (decl)
14789 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14790 }
14791
14792 /* Returns true if PARM declares a constrained-parameter. */
14793
14794 static inline bool
14795 is_constrained_parameter (cp_parameter_declarator *parm)
14796 {
14797 return is_constrained_parameter (parm->decl_specifiers.type);
14798 }
14799
14800 /* Check that the type parameter is only a declarator-id, and that its
14801 type is not cv-qualified. */
14802
14803 bool
14804 cp_parser_check_constrained_type_parm (cp_parser *parser,
14805 cp_parameter_declarator *parm)
14806 {
14807 if (!parm->declarator)
14808 return true;
14809
14810 if (parm->declarator->kind != cdk_id)
14811 {
14812 cp_parser_error (parser, "invalid constrained type parameter");
14813 return false;
14814 }
14815
14816 /* Don't allow cv-qualified type parameters. */
14817 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14818 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14819 {
14820 cp_parser_error (parser, "cv-qualified type parameter");
14821 return false;
14822 }
14823
14824 return true;
14825 }
14826
14827 /* Finish parsing/processing a template type parameter and checking
14828 various restrictions. */
14829
14830 static inline tree
14831 cp_parser_constrained_type_template_parm (cp_parser *parser,
14832 tree id,
14833 cp_parameter_declarator* parmdecl)
14834 {
14835 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14836 return finish_template_type_parm (class_type_node, id);
14837 else
14838 return error_mark_node;
14839 }
14840
14841 static tree
14842 finish_constrained_template_template_parm (tree proto, tree id)
14843 {
14844 /* FIXME: This should probably be copied, and we may need to adjust
14845 the template parameter depths. */
14846 tree saved_parms = current_template_parms;
14847 begin_template_parm_list ();
14848 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14849 end_template_parm_list ();
14850
14851 tree parm = finish_template_template_parm (class_type_node, id);
14852 current_template_parms = saved_parms;
14853
14854 return parm;
14855 }
14856
14857 /* Finish parsing/processing a template template parameter by borrowing
14858 the template parameter list from the prototype parameter. */
14859
14860 static tree
14861 cp_parser_constrained_template_template_parm (cp_parser *parser,
14862 tree proto,
14863 tree id,
14864 cp_parameter_declarator *parmdecl)
14865 {
14866 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14867 return error_mark_node;
14868 return finish_constrained_template_template_parm (proto, id);
14869 }
14870
14871 /* Create a new non-type template parameter from the given PARM
14872 declarator. */
14873
14874 static tree
14875 constrained_non_type_template_parm (bool *is_non_type,
14876 cp_parameter_declarator *parm)
14877 {
14878 *is_non_type = true;
14879 cp_declarator *decl = parm->declarator;
14880 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14881 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14882 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14883 }
14884
14885 /* Build a constrained template parameter based on the PARMDECL
14886 declarator. The type of PARMDECL is the constrained type, which
14887 refers to the prototype template parameter that ultimately
14888 specifies the type of the declared parameter. */
14889
14890 static tree
14891 finish_constrained_parameter (cp_parser *parser,
14892 cp_parameter_declarator *parmdecl,
14893 bool *is_non_type,
14894 bool *is_parameter_pack)
14895 {
14896 tree decl = parmdecl->decl_specifiers.type;
14897 tree id = get_unqualified_id (parmdecl->declarator);
14898 tree def = parmdecl->default_argument;
14899 tree proto = DECL_INITIAL (decl);
14900
14901 /* A template parameter constrained by a variadic concept shall also
14902 be declared as a template parameter pack. */
14903 bool is_variadic = template_parameter_pack_p (proto);
14904 if (is_variadic && !*is_parameter_pack)
14905 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14906
14907 /* Build the parameter. Return an error if the declarator was invalid. */
14908 tree parm;
14909 if (TREE_CODE (proto) == TYPE_DECL)
14910 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14911 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14912 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14913 parmdecl);
14914 else
14915 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14916 if (parm == error_mark_node)
14917 return error_mark_node;
14918
14919 /* Finish the parameter decl and create a node attaching the
14920 default argument and constraint. */
14921 parm = build_tree_list (def, parm);
14922 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14923
14924 return parm;
14925 }
14926
14927 /* Returns true if the parsed type actually represents the declaration
14928 of a type template-parameter. */
14929
14930 static inline bool
14931 declares_constrained_type_template_parameter (tree type)
14932 {
14933 return (is_constrained_parameter (type)
14934 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14935 }
14936
14937
14938 /* Returns true if the parsed type actually represents the declaration of
14939 a template template-parameter. */
14940
14941 static bool
14942 declares_constrained_template_template_parameter (tree type)
14943 {
14944 return (is_constrained_parameter (type)
14945 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14946 }
14947
14948 /* Parse a default argument for a type template-parameter.
14949 Note that diagnostics are handled in cp_parser_template_parameter. */
14950
14951 static tree
14952 cp_parser_default_type_template_argument (cp_parser *parser)
14953 {
14954 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14955
14956 /* Consume the `=' token. */
14957 cp_lexer_consume_token (parser->lexer);
14958
14959 cp_token *token = cp_lexer_peek_token (parser->lexer);
14960
14961 /* Parse the default-argument. */
14962 push_deferring_access_checks (dk_no_deferred);
14963 tree default_argument = cp_parser_type_id (parser);
14964 pop_deferring_access_checks ();
14965
14966 if (flag_concepts && type_uses_auto (default_argument))
14967 {
14968 error_at (token->location,
14969 "invalid use of %<auto%> in default template argument");
14970 return error_mark_node;
14971 }
14972
14973 return default_argument;
14974 }
14975
14976 /* Parse a default argument for a template template-parameter. */
14977
14978 static tree
14979 cp_parser_default_template_template_argument (cp_parser *parser)
14980 {
14981 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14982
14983 bool is_template;
14984
14985 /* Consume the `='. */
14986 cp_lexer_consume_token (parser->lexer);
14987 /* Parse the id-expression. */
14988 push_deferring_access_checks (dk_no_deferred);
14989 /* save token before parsing the id-expression, for error
14990 reporting */
14991 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14992 tree default_argument
14993 = cp_parser_id_expression (parser,
14994 /*template_keyword_p=*/false,
14995 /*check_dependency_p=*/true,
14996 /*template_p=*/&is_template,
14997 /*declarator_p=*/false,
14998 /*optional_p=*/false);
14999 if (TREE_CODE (default_argument) == TYPE_DECL)
15000 /* If the id-expression was a template-id that refers to
15001 a template-class, we already have the declaration here,
15002 so no further lookup is needed. */
15003 ;
15004 else
15005 /* Look up the name. */
15006 default_argument
15007 = cp_parser_lookup_name (parser, default_argument,
15008 none_type,
15009 /*is_template=*/is_template,
15010 /*is_namespace=*/false,
15011 /*check_dependency=*/true,
15012 /*ambiguous_decls=*/NULL,
15013 token->location);
15014 /* See if the default argument is valid. */
15015 default_argument = check_template_template_default_arg (default_argument);
15016 pop_deferring_access_checks ();
15017 return default_argument;
15018 }
15019
15020 /* Parse a template-parameter.
15021
15022 template-parameter:
15023 type-parameter
15024 parameter-declaration
15025
15026 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15027 the parameter. The TREE_PURPOSE is the default value, if any.
15028 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15029 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15030 set to true iff this parameter is a parameter pack. */
15031
15032 static tree
15033 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15034 bool *is_parameter_pack)
15035 {
15036 cp_token *token;
15037 cp_parameter_declarator *parameter_declarator;
15038 tree parm;
15039
15040 /* Assume it is a type parameter or a template parameter. */
15041 *is_non_type = false;
15042 /* Assume it not a parameter pack. */
15043 *is_parameter_pack = false;
15044 /* Peek at the next token. */
15045 token = cp_lexer_peek_token (parser->lexer);
15046 /* If it is `class' or `template', we have a type-parameter. */
15047 if (token->keyword == RID_TEMPLATE)
15048 return cp_parser_type_parameter (parser, is_parameter_pack);
15049 /* If it is `class' or `typename' we do not know yet whether it is a
15050 type parameter or a non-type parameter. Consider:
15051
15052 template <typename T, typename T::X X> ...
15053
15054 or:
15055
15056 template <class C, class D*> ...
15057
15058 Here, the first parameter is a type parameter, and the second is
15059 a non-type parameter. We can tell by looking at the token after
15060 the identifier -- if it is a `,', `=', or `>' then we have a type
15061 parameter. */
15062 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15063 {
15064 /* Peek at the token after `class' or `typename'. */
15065 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15066 /* If it's an ellipsis, we have a template type parameter
15067 pack. */
15068 if (token->type == CPP_ELLIPSIS)
15069 return cp_parser_type_parameter (parser, is_parameter_pack);
15070 /* If it's an identifier, skip it. */
15071 if (token->type == CPP_NAME)
15072 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15073 /* Now, see if the token looks like the end of a template
15074 parameter. */
15075 if (token->type == CPP_COMMA
15076 || token->type == CPP_EQ
15077 || token->type == CPP_GREATER)
15078 return cp_parser_type_parameter (parser, is_parameter_pack);
15079 }
15080
15081 /* Otherwise, it is a non-type parameter or a constrained parameter.
15082
15083 [temp.param]
15084
15085 When parsing a default template-argument for a non-type
15086 template-parameter, the first non-nested `>' is taken as the end
15087 of the template parameter-list rather than a greater-than
15088 operator. */
15089 parameter_declarator
15090 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15091 /*parenthesized_p=*/NULL);
15092
15093 if (!parameter_declarator)
15094 return error_mark_node;
15095
15096 /* If the parameter declaration is marked as a parameter pack, set
15097 *IS_PARAMETER_PACK to notify the caller. */
15098 if (parameter_declarator->template_parameter_pack_p)
15099 *is_parameter_pack = true;
15100
15101 if (parameter_declarator->default_argument)
15102 {
15103 /* Can happen in some cases of erroneous input (c++/34892). */
15104 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15105 /* Consume the `...' for better error recovery. */
15106 cp_lexer_consume_token (parser->lexer);
15107 }
15108
15109 // The parameter may have been constrained.
15110 if (is_constrained_parameter (parameter_declarator))
15111 return finish_constrained_parameter (parser,
15112 parameter_declarator,
15113 is_non_type,
15114 is_parameter_pack);
15115
15116 // Now we're sure that the parameter is a non-type parameter.
15117 *is_non_type = true;
15118
15119 parm = grokdeclarator (parameter_declarator->declarator,
15120 &parameter_declarator->decl_specifiers,
15121 TPARM, /*initialized=*/0,
15122 /*attrlist=*/NULL);
15123 if (parm == error_mark_node)
15124 return error_mark_node;
15125
15126 return build_tree_list (parameter_declarator->default_argument, parm);
15127 }
15128
15129 /* Parse a type-parameter.
15130
15131 type-parameter:
15132 class identifier [opt]
15133 class identifier [opt] = type-id
15134 typename identifier [opt]
15135 typename identifier [opt] = type-id
15136 template < template-parameter-list > class identifier [opt]
15137 template < template-parameter-list > class identifier [opt]
15138 = id-expression
15139
15140 GNU Extension (variadic templates):
15141
15142 type-parameter:
15143 class ... identifier [opt]
15144 typename ... identifier [opt]
15145
15146 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15147 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15148 the declaration of the parameter.
15149
15150 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15151
15152 static tree
15153 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15154 {
15155 cp_token *token;
15156 tree parameter;
15157
15158 /* Look for a keyword to tell us what kind of parameter this is. */
15159 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15160 if (!token)
15161 return error_mark_node;
15162
15163 switch (token->keyword)
15164 {
15165 case RID_CLASS:
15166 case RID_TYPENAME:
15167 {
15168 tree identifier;
15169 tree default_argument;
15170
15171 /* If the next token is an ellipsis, we have a template
15172 argument pack. */
15173 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15174 {
15175 /* Consume the `...' token. */
15176 cp_lexer_consume_token (parser->lexer);
15177 maybe_warn_variadic_templates ();
15178
15179 *is_parameter_pack = true;
15180 }
15181
15182 /* If the next token is an identifier, then it names the
15183 parameter. */
15184 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15185 identifier = cp_parser_identifier (parser);
15186 else
15187 identifier = NULL_TREE;
15188
15189 /* Create the parameter. */
15190 parameter = finish_template_type_parm (class_type_node, identifier);
15191
15192 /* If the next token is an `=', we have a default argument. */
15193 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15194 {
15195 default_argument
15196 = cp_parser_default_type_template_argument (parser);
15197
15198 /* Template parameter packs cannot have default
15199 arguments. */
15200 if (*is_parameter_pack)
15201 {
15202 if (identifier)
15203 error_at (token->location,
15204 "template parameter pack %qD cannot have a "
15205 "default argument", identifier);
15206 else
15207 error_at (token->location,
15208 "template parameter packs cannot have "
15209 "default arguments");
15210 default_argument = NULL_TREE;
15211 }
15212 else if (check_for_bare_parameter_packs (default_argument))
15213 default_argument = error_mark_node;
15214 }
15215 else
15216 default_argument = NULL_TREE;
15217
15218 /* Create the combined representation of the parameter and the
15219 default argument. */
15220 parameter = build_tree_list (default_argument, parameter);
15221 }
15222 break;
15223
15224 case RID_TEMPLATE:
15225 {
15226 tree identifier;
15227 tree default_argument;
15228
15229 /* Look for the `<'. */
15230 cp_parser_require (parser, CPP_LESS, RT_LESS);
15231 /* Parse the template-parameter-list. */
15232 cp_parser_template_parameter_list (parser);
15233 /* Look for the `>'. */
15234 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15235
15236 // If template requirements are present, parse them.
15237 if (flag_concepts)
15238 {
15239 tree reqs = get_shorthand_constraints (current_template_parms);
15240 if (tree r = cp_parser_requires_clause_opt (parser))
15241 reqs = conjoin_constraints (reqs, normalize_expression (r));
15242 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15243 }
15244
15245 /* Look for the `class' or 'typename' keywords. */
15246 cp_parser_type_parameter_key (parser);
15247 /* If the next token is an ellipsis, we have a template
15248 argument pack. */
15249 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15250 {
15251 /* Consume the `...' token. */
15252 cp_lexer_consume_token (parser->lexer);
15253 maybe_warn_variadic_templates ();
15254
15255 *is_parameter_pack = true;
15256 }
15257 /* If the next token is an `=', then there is a
15258 default-argument. If the next token is a `>', we are at
15259 the end of the parameter-list. If the next token is a `,',
15260 then we are at the end of this parameter. */
15261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15262 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15263 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15264 {
15265 identifier = cp_parser_identifier (parser);
15266 /* Treat invalid names as if the parameter were nameless. */
15267 if (identifier == error_mark_node)
15268 identifier = NULL_TREE;
15269 }
15270 else
15271 identifier = NULL_TREE;
15272
15273 /* Create the template parameter. */
15274 parameter = finish_template_template_parm (class_type_node,
15275 identifier);
15276
15277 /* If the next token is an `=', then there is a
15278 default-argument. */
15279 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15280 {
15281 default_argument
15282 = cp_parser_default_template_template_argument (parser);
15283
15284 /* Template parameter packs cannot have default
15285 arguments. */
15286 if (*is_parameter_pack)
15287 {
15288 if (identifier)
15289 error_at (token->location,
15290 "template parameter pack %qD cannot "
15291 "have a default argument",
15292 identifier);
15293 else
15294 error_at (token->location, "template parameter packs cannot "
15295 "have default arguments");
15296 default_argument = NULL_TREE;
15297 }
15298 }
15299 else
15300 default_argument = NULL_TREE;
15301
15302 /* Create the combined representation of the parameter and the
15303 default argument. */
15304 parameter = build_tree_list (default_argument, parameter);
15305 }
15306 break;
15307
15308 default:
15309 gcc_unreachable ();
15310 break;
15311 }
15312
15313 return parameter;
15314 }
15315
15316 /* Parse a template-id.
15317
15318 template-id:
15319 template-name < template-argument-list [opt] >
15320
15321 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15322 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15323 returned. Otherwise, if the template-name names a function, or set
15324 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15325 names a class, returns a TYPE_DECL for the specialization.
15326
15327 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15328 uninstantiated templates. */
15329
15330 static tree
15331 cp_parser_template_id (cp_parser *parser,
15332 bool template_keyword_p,
15333 bool check_dependency_p,
15334 enum tag_types tag_type,
15335 bool is_declaration)
15336 {
15337 tree templ;
15338 tree arguments;
15339 tree template_id;
15340 cp_token_position start_of_id = 0;
15341 cp_token *next_token = NULL, *next_token_2 = NULL;
15342 bool is_identifier;
15343
15344 /* If the next token corresponds to a template-id, there is no need
15345 to reparse it. */
15346 next_token = cp_lexer_peek_token (parser->lexer);
15347 if (next_token->type == CPP_TEMPLATE_ID)
15348 {
15349 cp_lexer_consume_token (parser->lexer);
15350 return saved_checks_value (next_token->u.tree_check_value);
15351 }
15352
15353 /* Avoid performing name lookup if there is no possibility of
15354 finding a template-id. */
15355 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15356 || (next_token->type == CPP_NAME
15357 && !cp_parser_nth_token_starts_template_argument_list_p
15358 (parser, 2)))
15359 {
15360 cp_parser_error (parser, "expected template-id");
15361 return error_mark_node;
15362 }
15363
15364 /* Remember where the template-id starts. */
15365 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15366 start_of_id = cp_lexer_token_position (parser->lexer, false);
15367
15368 push_deferring_access_checks (dk_deferred);
15369
15370 /* Parse the template-name. */
15371 is_identifier = false;
15372 templ = cp_parser_template_name (parser, template_keyword_p,
15373 check_dependency_p,
15374 is_declaration,
15375 tag_type,
15376 &is_identifier);
15377 if (templ == error_mark_node || is_identifier)
15378 {
15379 pop_deferring_access_checks ();
15380 return templ;
15381 }
15382
15383 /* Since we're going to preserve any side-effects from this parse, set up a
15384 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15385 in the template arguments. */
15386 tentative_firewall firewall (parser);
15387
15388 /* If we find the sequence `[:' after a template-name, it's probably
15389 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15390 parse correctly the argument list. */
15391 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15392 == CPP_OPEN_SQUARE)
15393 && next_token->flags & DIGRAPH
15394 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15395 == CPP_COLON)
15396 && !(next_token_2->flags & PREV_WHITE))
15397 {
15398 cp_parser_parse_tentatively (parser);
15399 /* Change `:' into `::'. */
15400 next_token_2->type = CPP_SCOPE;
15401 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15402 CPP_LESS. */
15403 cp_lexer_consume_token (parser->lexer);
15404
15405 /* Parse the arguments. */
15406 arguments = cp_parser_enclosed_template_argument_list (parser);
15407 if (!cp_parser_parse_definitely (parser))
15408 {
15409 /* If we couldn't parse an argument list, then we revert our changes
15410 and return simply an error. Maybe this is not a template-id
15411 after all. */
15412 next_token_2->type = CPP_COLON;
15413 cp_parser_error (parser, "expected %<<%>");
15414 pop_deferring_access_checks ();
15415 return error_mark_node;
15416 }
15417 /* Otherwise, emit an error about the invalid digraph, but continue
15418 parsing because we got our argument list. */
15419 if (permerror (next_token->location,
15420 "%<<::%> cannot begin a template-argument list"))
15421 {
15422 static bool hint = false;
15423 inform (next_token->location,
15424 "%<<:%> is an alternate spelling for %<[%>."
15425 " Insert whitespace between %<<%> and %<::%>");
15426 if (!hint && !flag_permissive)
15427 {
15428 inform (next_token->location, "(if you use %<-fpermissive%> "
15429 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15430 "accept your code)");
15431 hint = true;
15432 }
15433 }
15434 }
15435 else
15436 {
15437 /* Look for the `<' that starts the template-argument-list. */
15438 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15439 {
15440 pop_deferring_access_checks ();
15441 return error_mark_node;
15442 }
15443 /* Parse the arguments. */
15444 arguments = cp_parser_enclosed_template_argument_list (parser);
15445 }
15446
15447 /* Build a representation of the specialization. */
15448 if (identifier_p (templ))
15449 template_id = build_min_nt_loc (next_token->location,
15450 TEMPLATE_ID_EXPR,
15451 templ, arguments);
15452 else if (DECL_TYPE_TEMPLATE_P (templ)
15453 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15454 {
15455 bool entering_scope;
15456 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15457 template (rather than some instantiation thereof) only if
15458 is not nested within some other construct. For example, in
15459 "template <typename T> void f(T) { A<T>::", A<T> is just an
15460 instantiation of A. */
15461 entering_scope = (template_parm_scope_p ()
15462 && cp_lexer_next_token_is (parser->lexer,
15463 CPP_SCOPE));
15464 template_id
15465 = finish_template_type (templ, arguments, entering_scope);
15466 }
15467 /* A template-like identifier may be a partial concept id. */
15468 else if (flag_concepts
15469 && (template_id = (cp_parser_maybe_partial_concept_id
15470 (parser, templ, arguments))))
15471 return template_id;
15472 else if (variable_template_p (templ))
15473 {
15474 template_id = lookup_template_variable (templ, arguments);
15475 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15476 SET_EXPR_LOCATION (template_id, next_token->location);
15477 }
15478 else
15479 {
15480 /* If it's not a class-template or a template-template, it should be
15481 a function-template. */
15482 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15483 || TREE_CODE (templ) == OVERLOAD
15484 || BASELINK_P (templ)));
15485
15486 template_id = lookup_template_function (templ, arguments);
15487 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15488 SET_EXPR_LOCATION (template_id, next_token->location);
15489 }
15490
15491 /* If parsing tentatively, replace the sequence of tokens that makes
15492 up the template-id with a CPP_TEMPLATE_ID token. That way,
15493 should we re-parse the token stream, we will not have to repeat
15494 the effort required to do the parse, nor will we issue duplicate
15495 error messages about problems during instantiation of the
15496 template. */
15497 if (start_of_id
15498 /* Don't do this if we had a parse error in a declarator; re-parsing
15499 might succeed if a name changes meaning (60361). */
15500 && !(cp_parser_error_occurred (parser)
15501 && cp_parser_parsing_tentatively (parser)
15502 && parser->in_declarator_p))
15503 {
15504 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15505
15506 /* Reset the contents of the START_OF_ID token. */
15507 token->type = CPP_TEMPLATE_ID;
15508
15509 /* Update the location to be of the form:
15510 template-name < template-argument-list [opt] >
15511 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15512 with caret == start at the start of the template-name,
15513 ranging until the closing '>'. */
15514 location_t finish_loc
15515 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15516 location_t combined_loc
15517 = make_location (token->location, token->location, finish_loc);
15518 token->location = combined_loc;
15519
15520 /* Retrieve any deferred checks. Do not pop this access checks yet
15521 so the memory will not be reclaimed during token replacing below. */
15522 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15523 token->u.tree_check_value->value = template_id;
15524 token->u.tree_check_value->checks = get_deferred_access_checks ();
15525 token->keyword = RID_MAX;
15526
15527 /* Purge all subsequent tokens. */
15528 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15529
15530 /* ??? Can we actually assume that, if template_id ==
15531 error_mark_node, we will have issued a diagnostic to the
15532 user, as opposed to simply marking the tentative parse as
15533 failed? */
15534 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15535 error_at (token->location, "parse error in template argument list");
15536 }
15537
15538 pop_to_parent_deferring_access_checks ();
15539 return template_id;
15540 }
15541
15542 /* Parse a template-name.
15543
15544 template-name:
15545 identifier
15546
15547 The standard should actually say:
15548
15549 template-name:
15550 identifier
15551 operator-function-id
15552
15553 A defect report has been filed about this issue.
15554
15555 A conversion-function-id cannot be a template name because they cannot
15556 be part of a template-id. In fact, looking at this code:
15557
15558 a.operator K<int>()
15559
15560 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15561 It is impossible to call a templated conversion-function-id with an
15562 explicit argument list, since the only allowed template parameter is
15563 the type to which it is converting.
15564
15565 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15566 `template' keyword, in a construction like:
15567
15568 T::template f<3>()
15569
15570 In that case `f' is taken to be a template-name, even though there
15571 is no way of knowing for sure.
15572
15573 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15574 name refers to a set of overloaded functions, at least one of which
15575 is a template, or an IDENTIFIER_NODE with the name of the template,
15576 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15577 names are looked up inside uninstantiated templates. */
15578
15579 static tree
15580 cp_parser_template_name (cp_parser* parser,
15581 bool template_keyword_p,
15582 bool check_dependency_p,
15583 bool is_declaration,
15584 enum tag_types tag_type,
15585 bool *is_identifier)
15586 {
15587 tree identifier;
15588 tree decl;
15589 tree fns;
15590 cp_token *token = cp_lexer_peek_token (parser->lexer);
15591
15592 /* If the next token is `operator', then we have either an
15593 operator-function-id or a conversion-function-id. */
15594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15595 {
15596 /* We don't know whether we're looking at an
15597 operator-function-id or a conversion-function-id. */
15598 cp_parser_parse_tentatively (parser);
15599 /* Try an operator-function-id. */
15600 identifier = cp_parser_operator_function_id (parser);
15601 /* If that didn't work, try a conversion-function-id. */
15602 if (!cp_parser_parse_definitely (parser))
15603 {
15604 cp_parser_error (parser, "expected template-name");
15605 return error_mark_node;
15606 }
15607 }
15608 /* Look for the identifier. */
15609 else
15610 identifier = cp_parser_identifier (parser);
15611
15612 /* If we didn't find an identifier, we don't have a template-id. */
15613 if (identifier == error_mark_node)
15614 return error_mark_node;
15615
15616 /* If the name immediately followed the `template' keyword, then it
15617 is a template-name. However, if the next token is not `<', then
15618 we do not treat it as a template-name, since it is not being used
15619 as part of a template-id. This enables us to handle constructs
15620 like:
15621
15622 template <typename T> struct S { S(); };
15623 template <typename T> S<T>::S();
15624
15625 correctly. We would treat `S' as a template -- if it were `S<T>'
15626 -- but we do not if there is no `<'. */
15627
15628 if (processing_template_decl
15629 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15630 {
15631 /* In a declaration, in a dependent context, we pretend that the
15632 "template" keyword was present in order to improve error
15633 recovery. For example, given:
15634
15635 template <typename T> void f(T::X<int>);
15636
15637 we want to treat "X<int>" as a template-id. */
15638 if (is_declaration
15639 && !template_keyword_p
15640 && parser->scope && TYPE_P (parser->scope)
15641 && check_dependency_p
15642 && dependent_scope_p (parser->scope)
15643 /* Do not do this for dtors (or ctors), since they never
15644 need the template keyword before their name. */
15645 && !constructor_name_p (identifier, parser->scope))
15646 {
15647 cp_token_position start = 0;
15648
15649 /* Explain what went wrong. */
15650 error_at (token->location, "non-template %qD used as template",
15651 identifier);
15652 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15653 parser->scope, identifier);
15654 /* If parsing tentatively, find the location of the "<" token. */
15655 if (cp_parser_simulate_error (parser))
15656 start = cp_lexer_token_position (parser->lexer, true);
15657 /* Parse the template arguments so that we can issue error
15658 messages about them. */
15659 cp_lexer_consume_token (parser->lexer);
15660 cp_parser_enclosed_template_argument_list (parser);
15661 /* Skip tokens until we find a good place from which to
15662 continue parsing. */
15663 cp_parser_skip_to_closing_parenthesis (parser,
15664 /*recovering=*/true,
15665 /*or_comma=*/true,
15666 /*consume_paren=*/false);
15667 /* If parsing tentatively, permanently remove the
15668 template argument list. That will prevent duplicate
15669 error messages from being issued about the missing
15670 "template" keyword. */
15671 if (start)
15672 cp_lexer_purge_tokens_after (parser->lexer, start);
15673 if (is_identifier)
15674 *is_identifier = true;
15675 return identifier;
15676 }
15677
15678 /* If the "template" keyword is present, then there is generally
15679 no point in doing name-lookup, so we just return IDENTIFIER.
15680 But, if the qualifying scope is non-dependent then we can
15681 (and must) do name-lookup normally. */
15682 if (template_keyword_p
15683 && (!parser->scope
15684 || (TYPE_P (parser->scope)
15685 && dependent_type_p (parser->scope))))
15686 return identifier;
15687 }
15688
15689 /* Look up the name. */
15690 decl = cp_parser_lookup_name (parser, identifier,
15691 tag_type,
15692 /*is_template=*/true,
15693 /*is_namespace=*/false,
15694 check_dependency_p,
15695 /*ambiguous_decls=*/NULL,
15696 token->location);
15697
15698 decl = strip_using_decl (decl);
15699
15700 /* If DECL is a template, then the name was a template-name. */
15701 if (TREE_CODE (decl) == TEMPLATE_DECL)
15702 {
15703 if (TREE_DEPRECATED (decl)
15704 && deprecated_state != DEPRECATED_SUPPRESS)
15705 warn_deprecated_use (decl, NULL_TREE);
15706 }
15707 else
15708 {
15709 tree fn = NULL_TREE;
15710
15711 /* The standard does not explicitly indicate whether a name that
15712 names a set of overloaded declarations, some of which are
15713 templates, is a template-name. However, such a name should
15714 be a template-name; otherwise, there is no way to form a
15715 template-id for the overloaded templates. */
15716 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15717 if (TREE_CODE (fns) == OVERLOAD)
15718 for (fn = fns; fn; fn = OVL_NEXT (fn))
15719 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15720 break;
15721
15722 if (!fn)
15723 {
15724 /* The name does not name a template. */
15725 cp_parser_error (parser, "expected template-name");
15726 return error_mark_node;
15727 }
15728 }
15729
15730 /* If DECL is dependent, and refers to a function, then just return
15731 its name; we will look it up again during template instantiation. */
15732 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15733 {
15734 tree scope = ovl_scope (decl);
15735 if (TYPE_P (scope) && dependent_type_p (scope))
15736 return identifier;
15737 }
15738
15739 return decl;
15740 }
15741
15742 /* Parse a template-argument-list.
15743
15744 template-argument-list:
15745 template-argument ... [opt]
15746 template-argument-list , template-argument ... [opt]
15747
15748 Returns a TREE_VEC containing the arguments. */
15749
15750 static tree
15751 cp_parser_template_argument_list (cp_parser* parser)
15752 {
15753 tree fixed_args[10];
15754 unsigned n_args = 0;
15755 unsigned alloced = 10;
15756 tree *arg_ary = fixed_args;
15757 tree vec;
15758 bool saved_in_template_argument_list_p;
15759 bool saved_ice_p;
15760 bool saved_non_ice_p;
15761
15762 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15763 parser->in_template_argument_list_p = true;
15764 /* Even if the template-id appears in an integral
15765 constant-expression, the contents of the argument list do
15766 not. */
15767 saved_ice_p = parser->integral_constant_expression_p;
15768 parser->integral_constant_expression_p = false;
15769 saved_non_ice_p = parser->non_integral_constant_expression_p;
15770 parser->non_integral_constant_expression_p = false;
15771
15772 /* Parse the arguments. */
15773 do
15774 {
15775 tree argument;
15776
15777 if (n_args)
15778 /* Consume the comma. */
15779 cp_lexer_consume_token (parser->lexer);
15780
15781 /* Parse the template-argument. */
15782 argument = cp_parser_template_argument (parser);
15783
15784 /* If the next token is an ellipsis, we're expanding a template
15785 argument pack. */
15786 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15787 {
15788 if (argument == error_mark_node)
15789 {
15790 cp_token *token = cp_lexer_peek_token (parser->lexer);
15791 error_at (token->location,
15792 "expected parameter pack before %<...%>");
15793 }
15794 /* Consume the `...' token. */
15795 cp_lexer_consume_token (parser->lexer);
15796
15797 /* Make the argument into a TYPE_PACK_EXPANSION or
15798 EXPR_PACK_EXPANSION. */
15799 argument = make_pack_expansion (argument);
15800 }
15801
15802 if (n_args == alloced)
15803 {
15804 alloced *= 2;
15805
15806 if (arg_ary == fixed_args)
15807 {
15808 arg_ary = XNEWVEC (tree, alloced);
15809 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15810 }
15811 else
15812 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15813 }
15814 arg_ary[n_args++] = argument;
15815 }
15816 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15817
15818 vec = make_tree_vec (n_args);
15819
15820 while (n_args--)
15821 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15822
15823 if (arg_ary != fixed_args)
15824 free (arg_ary);
15825 parser->non_integral_constant_expression_p = saved_non_ice_p;
15826 parser->integral_constant_expression_p = saved_ice_p;
15827 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15828 if (CHECKING_P)
15829 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15830 return vec;
15831 }
15832
15833 /* Parse a template-argument.
15834
15835 template-argument:
15836 assignment-expression
15837 type-id
15838 id-expression
15839
15840 The representation is that of an assignment-expression, type-id, or
15841 id-expression -- except that the qualified id-expression is
15842 evaluated, so that the value returned is either a DECL or an
15843 OVERLOAD.
15844
15845 Although the standard says "assignment-expression", it forbids
15846 throw-expressions or assignments in the template argument.
15847 Therefore, we use "conditional-expression" instead. */
15848
15849 static tree
15850 cp_parser_template_argument (cp_parser* parser)
15851 {
15852 tree argument;
15853 bool template_p;
15854 bool address_p;
15855 bool maybe_type_id = false;
15856 cp_token *token = NULL, *argument_start_token = NULL;
15857 location_t loc = 0;
15858 cp_id_kind idk;
15859
15860 /* There's really no way to know what we're looking at, so we just
15861 try each alternative in order.
15862
15863 [temp.arg]
15864
15865 In a template-argument, an ambiguity between a type-id and an
15866 expression is resolved to a type-id, regardless of the form of
15867 the corresponding template-parameter.
15868
15869 Therefore, we try a type-id first. */
15870 cp_parser_parse_tentatively (parser);
15871 argument = cp_parser_template_type_arg (parser);
15872 /* If there was no error parsing the type-id but the next token is a
15873 '>>', our behavior depends on which dialect of C++ we're
15874 parsing. In C++98, we probably found a typo for '> >'. But there
15875 are type-id which are also valid expressions. For instance:
15876
15877 struct X { int operator >> (int); };
15878 template <int V> struct Foo {};
15879 Foo<X () >> 5> r;
15880
15881 Here 'X()' is a valid type-id of a function type, but the user just
15882 wanted to write the expression "X() >> 5". Thus, we remember that we
15883 found a valid type-id, but we still try to parse the argument as an
15884 expression to see what happens.
15885
15886 In C++0x, the '>>' will be considered two separate '>'
15887 tokens. */
15888 if (!cp_parser_error_occurred (parser)
15889 && cxx_dialect == cxx98
15890 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15891 {
15892 maybe_type_id = true;
15893 cp_parser_abort_tentative_parse (parser);
15894 }
15895 else
15896 {
15897 /* If the next token isn't a `,' or a `>', then this argument wasn't
15898 really finished. This means that the argument is not a valid
15899 type-id. */
15900 if (!cp_parser_next_token_ends_template_argument_p (parser))
15901 cp_parser_error (parser, "expected template-argument");
15902 /* If that worked, we're done. */
15903 if (cp_parser_parse_definitely (parser))
15904 return argument;
15905 }
15906 /* We're still not sure what the argument will be. */
15907 cp_parser_parse_tentatively (parser);
15908 /* Try a template. */
15909 argument_start_token = cp_lexer_peek_token (parser->lexer);
15910 argument = cp_parser_id_expression (parser,
15911 /*template_keyword_p=*/false,
15912 /*check_dependency_p=*/true,
15913 &template_p,
15914 /*declarator_p=*/false,
15915 /*optional_p=*/false);
15916 /* If the next token isn't a `,' or a `>', then this argument wasn't
15917 really finished. */
15918 if (!cp_parser_next_token_ends_template_argument_p (parser))
15919 cp_parser_error (parser, "expected template-argument");
15920 if (!cp_parser_error_occurred (parser))
15921 {
15922 /* Figure out what is being referred to. If the id-expression
15923 was for a class template specialization, then we will have a
15924 TYPE_DECL at this point. There is no need to do name lookup
15925 at this point in that case. */
15926 if (TREE_CODE (argument) != TYPE_DECL)
15927 argument = cp_parser_lookup_name (parser, argument,
15928 none_type,
15929 /*is_template=*/template_p,
15930 /*is_namespace=*/false,
15931 /*check_dependency=*/true,
15932 /*ambiguous_decls=*/NULL,
15933 argument_start_token->location);
15934 /* Handle a constrained-type-specifier for a non-type template
15935 parameter. */
15936 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15937 argument = decl;
15938 else if (TREE_CODE (argument) != TEMPLATE_DECL
15939 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15940 cp_parser_error (parser, "expected template-name");
15941 }
15942 if (cp_parser_parse_definitely (parser))
15943 {
15944 if (TREE_DEPRECATED (argument))
15945 warn_deprecated_use (argument, NULL_TREE);
15946 return argument;
15947 }
15948 /* It must be a non-type argument. In C++17 any constant-expression is
15949 allowed. */
15950 if (cxx_dialect > cxx14)
15951 goto general_expr;
15952
15953 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15954
15955 -- an integral constant-expression of integral or enumeration
15956 type; or
15957
15958 -- the name of a non-type template-parameter; or
15959
15960 -- the name of an object or function with external linkage...
15961
15962 -- the address of an object or function with external linkage...
15963
15964 -- a pointer to member... */
15965 /* Look for a non-type template parameter. */
15966 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15967 {
15968 cp_parser_parse_tentatively (parser);
15969 argument = cp_parser_primary_expression (parser,
15970 /*address_p=*/false,
15971 /*cast_p=*/false,
15972 /*template_arg_p=*/true,
15973 &idk);
15974 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15975 || !cp_parser_next_token_ends_template_argument_p (parser))
15976 cp_parser_simulate_error (parser);
15977 if (cp_parser_parse_definitely (parser))
15978 return argument;
15979 }
15980
15981 /* If the next token is "&", the argument must be the address of an
15982 object or function with external linkage. */
15983 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15984 if (address_p)
15985 {
15986 loc = cp_lexer_peek_token (parser->lexer)->location;
15987 cp_lexer_consume_token (parser->lexer);
15988 }
15989 /* See if we might have an id-expression. */
15990 token = cp_lexer_peek_token (parser->lexer);
15991 if (token->type == CPP_NAME
15992 || token->keyword == RID_OPERATOR
15993 || token->type == CPP_SCOPE
15994 || token->type == CPP_TEMPLATE_ID
15995 || token->type == CPP_NESTED_NAME_SPECIFIER)
15996 {
15997 cp_parser_parse_tentatively (parser);
15998 argument = cp_parser_primary_expression (parser,
15999 address_p,
16000 /*cast_p=*/false,
16001 /*template_arg_p=*/true,
16002 &idk);
16003 if (cp_parser_error_occurred (parser)
16004 || !cp_parser_next_token_ends_template_argument_p (parser))
16005 cp_parser_abort_tentative_parse (parser);
16006 else
16007 {
16008 tree probe;
16009
16010 if (INDIRECT_REF_P (argument))
16011 {
16012 /* Strip the dereference temporarily. */
16013 gcc_assert (REFERENCE_REF_P (argument));
16014 argument = TREE_OPERAND (argument, 0);
16015 }
16016
16017 /* If we're in a template, we represent a qualified-id referring
16018 to a static data member as a SCOPE_REF even if the scope isn't
16019 dependent so that we can check access control later. */
16020 probe = argument;
16021 if (TREE_CODE (probe) == SCOPE_REF)
16022 probe = TREE_OPERAND (probe, 1);
16023 if (VAR_P (probe))
16024 {
16025 /* A variable without external linkage might still be a
16026 valid constant-expression, so no error is issued here
16027 if the external-linkage check fails. */
16028 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16029 cp_parser_simulate_error (parser);
16030 }
16031 else if (is_overloaded_fn (argument))
16032 /* All overloaded functions are allowed; if the external
16033 linkage test does not pass, an error will be issued
16034 later. */
16035 ;
16036 else if (address_p
16037 && (TREE_CODE (argument) == OFFSET_REF
16038 || TREE_CODE (argument) == SCOPE_REF))
16039 /* A pointer-to-member. */
16040 ;
16041 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16042 ;
16043 else
16044 cp_parser_simulate_error (parser);
16045
16046 if (cp_parser_parse_definitely (parser))
16047 {
16048 if (address_p)
16049 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16050 tf_warning_or_error);
16051 else
16052 argument = convert_from_reference (argument);
16053 return argument;
16054 }
16055 }
16056 }
16057 /* If the argument started with "&", there are no other valid
16058 alternatives at this point. */
16059 if (address_p)
16060 {
16061 cp_parser_error (parser, "invalid non-type template argument");
16062 return error_mark_node;
16063 }
16064
16065 general_expr:
16066 /* If the argument wasn't successfully parsed as a type-id followed
16067 by '>>', the argument can only be a constant expression now.
16068 Otherwise, we try parsing the constant-expression tentatively,
16069 because the argument could really be a type-id. */
16070 if (maybe_type_id)
16071 cp_parser_parse_tentatively (parser);
16072
16073 if (cxx_dialect <= cxx14)
16074 argument = cp_parser_constant_expression (parser);
16075 else
16076 {
16077 /* With C++17 generalized non-type template arguments we need to handle
16078 lvalue constant expressions, too. */
16079 argument = cp_parser_assignment_expression (parser);
16080 require_potential_constant_expression (argument);
16081 }
16082
16083 if (!maybe_type_id)
16084 return argument;
16085 if (!cp_parser_next_token_ends_template_argument_p (parser))
16086 cp_parser_error (parser, "expected template-argument");
16087 if (cp_parser_parse_definitely (parser))
16088 return argument;
16089 /* We did our best to parse the argument as a non type-id, but that
16090 was the only alternative that matched (albeit with a '>' after
16091 it). We can assume it's just a typo from the user, and a
16092 diagnostic will then be issued. */
16093 return cp_parser_template_type_arg (parser);
16094 }
16095
16096 /* Parse an explicit-instantiation.
16097
16098 explicit-instantiation:
16099 template declaration
16100
16101 Although the standard says `declaration', what it really means is:
16102
16103 explicit-instantiation:
16104 template decl-specifier-seq [opt] declarator [opt] ;
16105
16106 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16107 supposed to be allowed. A defect report has been filed about this
16108 issue.
16109
16110 GNU Extension:
16111
16112 explicit-instantiation:
16113 storage-class-specifier template
16114 decl-specifier-seq [opt] declarator [opt] ;
16115 function-specifier template
16116 decl-specifier-seq [opt] declarator [opt] ; */
16117
16118 static void
16119 cp_parser_explicit_instantiation (cp_parser* parser)
16120 {
16121 int declares_class_or_enum;
16122 cp_decl_specifier_seq decl_specifiers;
16123 tree extension_specifier = NULL_TREE;
16124
16125 timevar_push (TV_TEMPLATE_INST);
16126
16127 /* Look for an (optional) storage-class-specifier or
16128 function-specifier. */
16129 if (cp_parser_allow_gnu_extensions_p (parser))
16130 {
16131 extension_specifier
16132 = cp_parser_storage_class_specifier_opt (parser);
16133 if (!extension_specifier)
16134 extension_specifier
16135 = cp_parser_function_specifier_opt (parser,
16136 /*decl_specs=*/NULL);
16137 }
16138
16139 /* Look for the `template' keyword. */
16140 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16141 /* Let the front end know that we are processing an explicit
16142 instantiation. */
16143 begin_explicit_instantiation ();
16144 /* [temp.explicit] says that we are supposed to ignore access
16145 control while processing explicit instantiation directives. */
16146 push_deferring_access_checks (dk_no_check);
16147 /* Parse a decl-specifier-seq. */
16148 cp_parser_decl_specifier_seq (parser,
16149 CP_PARSER_FLAGS_OPTIONAL,
16150 &decl_specifiers,
16151 &declares_class_or_enum);
16152 /* If there was exactly one decl-specifier, and it declared a class,
16153 and there's no declarator, then we have an explicit type
16154 instantiation. */
16155 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16156 {
16157 tree type;
16158
16159 type = check_tag_decl (&decl_specifiers,
16160 /*explicit_type_instantiation_p=*/true);
16161 /* Turn access control back on for names used during
16162 template instantiation. */
16163 pop_deferring_access_checks ();
16164 if (type)
16165 do_type_instantiation (type, extension_specifier,
16166 /*complain=*/tf_error);
16167 }
16168 else
16169 {
16170 cp_declarator *declarator;
16171 tree decl;
16172
16173 /* Parse the declarator. */
16174 declarator
16175 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16176 /*ctor_dtor_or_conv_p=*/NULL,
16177 /*parenthesized_p=*/NULL,
16178 /*member_p=*/false,
16179 /*friend_p=*/false);
16180 if (declares_class_or_enum & 2)
16181 cp_parser_check_for_definition_in_return_type (declarator,
16182 decl_specifiers.type,
16183 decl_specifiers.locations[ds_type_spec]);
16184 if (declarator != cp_error_declarator)
16185 {
16186 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16187 permerror (decl_specifiers.locations[ds_inline],
16188 "explicit instantiation shall not use"
16189 " %<inline%> specifier");
16190 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16191 permerror (decl_specifiers.locations[ds_constexpr],
16192 "explicit instantiation shall not use"
16193 " %<constexpr%> specifier");
16194
16195 decl = grokdeclarator (declarator, &decl_specifiers,
16196 NORMAL, 0, &decl_specifiers.attributes);
16197 /* Turn access control back on for names used during
16198 template instantiation. */
16199 pop_deferring_access_checks ();
16200 /* Do the explicit instantiation. */
16201 do_decl_instantiation (decl, extension_specifier);
16202 }
16203 else
16204 {
16205 pop_deferring_access_checks ();
16206 /* Skip the body of the explicit instantiation. */
16207 cp_parser_skip_to_end_of_statement (parser);
16208 }
16209 }
16210 /* We're done with the instantiation. */
16211 end_explicit_instantiation ();
16212
16213 cp_parser_consume_semicolon_at_end_of_statement (parser);
16214
16215 timevar_pop (TV_TEMPLATE_INST);
16216 }
16217
16218 /* Parse an explicit-specialization.
16219
16220 explicit-specialization:
16221 template < > declaration
16222
16223 Although the standard says `declaration', what it really means is:
16224
16225 explicit-specialization:
16226 template <> decl-specifier [opt] init-declarator [opt] ;
16227 template <> function-definition
16228 template <> explicit-specialization
16229 template <> template-declaration */
16230
16231 static void
16232 cp_parser_explicit_specialization (cp_parser* parser)
16233 {
16234 bool need_lang_pop;
16235 cp_token *token = cp_lexer_peek_token (parser->lexer);
16236
16237 /* Look for the `template' keyword. */
16238 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16239 /* Look for the `<'. */
16240 cp_parser_require (parser, CPP_LESS, RT_LESS);
16241 /* Look for the `>'. */
16242 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16243 /* We have processed another parameter list. */
16244 ++parser->num_template_parameter_lists;
16245 /* [temp]
16246
16247 A template ... explicit specialization ... shall not have C
16248 linkage. */
16249 if (current_lang_name == lang_name_c)
16250 {
16251 error_at (token->location, "template specialization with C linkage");
16252 /* Give it C++ linkage to avoid confusing other parts of the
16253 front end. */
16254 push_lang_context (lang_name_cplusplus);
16255 need_lang_pop = true;
16256 }
16257 else
16258 need_lang_pop = false;
16259 /* Let the front end know that we are beginning a specialization. */
16260 if (!begin_specialization ())
16261 {
16262 end_specialization ();
16263 return;
16264 }
16265
16266 /* If the next keyword is `template', we need to figure out whether
16267 or not we're looking a template-declaration. */
16268 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16269 {
16270 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16271 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16272 cp_parser_template_declaration_after_export (parser,
16273 /*member_p=*/false);
16274 else
16275 cp_parser_explicit_specialization (parser);
16276 }
16277 else
16278 /* Parse the dependent declaration. */
16279 cp_parser_single_declaration (parser,
16280 /*checks=*/NULL,
16281 /*member_p=*/false,
16282 /*explicit_specialization_p=*/true,
16283 /*friend_p=*/NULL);
16284 /* We're done with the specialization. */
16285 end_specialization ();
16286 /* For the erroneous case of a template with C linkage, we pushed an
16287 implicit C++ linkage scope; exit that scope now. */
16288 if (need_lang_pop)
16289 pop_lang_context ();
16290 /* We're done with this parameter list. */
16291 --parser->num_template_parameter_lists;
16292 }
16293
16294 /* Parse a type-specifier.
16295
16296 type-specifier:
16297 simple-type-specifier
16298 class-specifier
16299 enum-specifier
16300 elaborated-type-specifier
16301 cv-qualifier
16302
16303 GNU Extension:
16304
16305 type-specifier:
16306 __complex__
16307
16308 Returns a representation of the type-specifier. For a
16309 class-specifier, enum-specifier, or elaborated-type-specifier, a
16310 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16311
16312 The parser flags FLAGS is used to control type-specifier parsing.
16313
16314 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16315 in a decl-specifier-seq.
16316
16317 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16318 class-specifier, enum-specifier, or elaborated-type-specifier, then
16319 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16320 if a type is declared; 2 if it is defined. Otherwise, it is set to
16321 zero.
16322
16323 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16324 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16325 is set to FALSE. */
16326
16327 static tree
16328 cp_parser_type_specifier (cp_parser* parser,
16329 cp_parser_flags flags,
16330 cp_decl_specifier_seq *decl_specs,
16331 bool is_declaration,
16332 int* declares_class_or_enum,
16333 bool* is_cv_qualifier)
16334 {
16335 tree type_spec = NULL_TREE;
16336 cp_token *token;
16337 enum rid keyword;
16338 cp_decl_spec ds = ds_last;
16339
16340 /* Assume this type-specifier does not declare a new type. */
16341 if (declares_class_or_enum)
16342 *declares_class_or_enum = 0;
16343 /* And that it does not specify a cv-qualifier. */
16344 if (is_cv_qualifier)
16345 *is_cv_qualifier = false;
16346 /* Peek at the next token. */
16347 token = cp_lexer_peek_token (parser->lexer);
16348
16349 /* If we're looking at a keyword, we can use that to guide the
16350 production we choose. */
16351 keyword = token->keyword;
16352 switch (keyword)
16353 {
16354 case RID_ENUM:
16355 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16356 goto elaborated_type_specifier;
16357
16358 /* Look for the enum-specifier. */
16359 type_spec = cp_parser_enum_specifier (parser);
16360 /* If that worked, we're done. */
16361 if (type_spec)
16362 {
16363 if (declares_class_or_enum)
16364 *declares_class_or_enum = 2;
16365 if (decl_specs)
16366 cp_parser_set_decl_spec_type (decl_specs,
16367 type_spec,
16368 token,
16369 /*type_definition_p=*/true);
16370 return type_spec;
16371 }
16372 else
16373 goto elaborated_type_specifier;
16374
16375 /* Any of these indicate either a class-specifier, or an
16376 elaborated-type-specifier. */
16377 case RID_CLASS:
16378 case RID_STRUCT:
16379 case RID_UNION:
16380 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16381 goto elaborated_type_specifier;
16382
16383 /* Parse tentatively so that we can back up if we don't find a
16384 class-specifier. */
16385 cp_parser_parse_tentatively (parser);
16386 /* Look for the class-specifier. */
16387 type_spec = cp_parser_class_specifier (parser);
16388 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16389 /* If that worked, we're done. */
16390 if (cp_parser_parse_definitely (parser))
16391 {
16392 if (declares_class_or_enum)
16393 *declares_class_or_enum = 2;
16394 if (decl_specs)
16395 cp_parser_set_decl_spec_type (decl_specs,
16396 type_spec,
16397 token,
16398 /*type_definition_p=*/true);
16399 return type_spec;
16400 }
16401
16402 /* Fall through. */
16403 elaborated_type_specifier:
16404 /* We're declaring (not defining) a class or enum. */
16405 if (declares_class_or_enum)
16406 *declares_class_or_enum = 1;
16407
16408 /* Fall through. */
16409 case RID_TYPENAME:
16410 /* Look for an elaborated-type-specifier. */
16411 type_spec
16412 = (cp_parser_elaborated_type_specifier
16413 (parser,
16414 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16415 is_declaration));
16416 if (decl_specs)
16417 cp_parser_set_decl_spec_type (decl_specs,
16418 type_spec,
16419 token,
16420 /*type_definition_p=*/false);
16421 return type_spec;
16422
16423 case RID_CONST:
16424 ds = ds_const;
16425 if (is_cv_qualifier)
16426 *is_cv_qualifier = true;
16427 break;
16428
16429 case RID_VOLATILE:
16430 ds = ds_volatile;
16431 if (is_cv_qualifier)
16432 *is_cv_qualifier = true;
16433 break;
16434
16435 case RID_RESTRICT:
16436 ds = ds_restrict;
16437 if (is_cv_qualifier)
16438 *is_cv_qualifier = true;
16439 break;
16440
16441 case RID_COMPLEX:
16442 /* The `__complex__' keyword is a GNU extension. */
16443 ds = ds_complex;
16444 break;
16445
16446 default:
16447 break;
16448 }
16449
16450 /* Handle simple keywords. */
16451 if (ds != ds_last)
16452 {
16453 if (decl_specs)
16454 {
16455 set_and_check_decl_spec_loc (decl_specs, ds, token);
16456 decl_specs->any_specifiers_p = true;
16457 }
16458 return cp_lexer_consume_token (parser->lexer)->u.value;
16459 }
16460
16461 /* If we do not already have a type-specifier, assume we are looking
16462 at a simple-type-specifier. */
16463 type_spec = cp_parser_simple_type_specifier (parser,
16464 decl_specs,
16465 flags);
16466
16467 /* If we didn't find a type-specifier, and a type-specifier was not
16468 optional in this context, issue an error message. */
16469 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16470 {
16471 cp_parser_error (parser, "expected type specifier");
16472 return error_mark_node;
16473 }
16474
16475 return type_spec;
16476 }
16477
16478 /* Parse a simple-type-specifier.
16479
16480 simple-type-specifier:
16481 :: [opt] nested-name-specifier [opt] type-name
16482 :: [opt] nested-name-specifier template template-id
16483 char
16484 wchar_t
16485 bool
16486 short
16487 int
16488 long
16489 signed
16490 unsigned
16491 float
16492 double
16493 void
16494
16495 C++11 Extension:
16496
16497 simple-type-specifier:
16498 auto
16499 decltype ( expression )
16500 char16_t
16501 char32_t
16502 __underlying_type ( type-id )
16503
16504 C++17 extension:
16505
16506 nested-name-specifier(opt) template-name
16507
16508 GNU Extension:
16509
16510 simple-type-specifier:
16511 __int128
16512 __typeof__ unary-expression
16513 __typeof__ ( type-id )
16514 __typeof__ ( type-id ) { initializer-list , [opt] }
16515
16516 Concepts Extension:
16517
16518 simple-type-specifier:
16519 constrained-type-specifier
16520
16521 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16522 appropriately updated. */
16523
16524 static tree
16525 cp_parser_simple_type_specifier (cp_parser* parser,
16526 cp_decl_specifier_seq *decl_specs,
16527 cp_parser_flags flags)
16528 {
16529 tree type = NULL_TREE;
16530 cp_token *token;
16531 int idx;
16532
16533 /* Peek at the next token. */
16534 token = cp_lexer_peek_token (parser->lexer);
16535
16536 /* If we're looking at a keyword, things are easy. */
16537 switch (token->keyword)
16538 {
16539 case RID_CHAR:
16540 if (decl_specs)
16541 decl_specs->explicit_char_p = true;
16542 type = char_type_node;
16543 break;
16544 case RID_CHAR16:
16545 type = char16_type_node;
16546 break;
16547 case RID_CHAR32:
16548 type = char32_type_node;
16549 break;
16550 case RID_WCHAR:
16551 type = wchar_type_node;
16552 break;
16553 case RID_BOOL:
16554 type = boolean_type_node;
16555 break;
16556 case RID_SHORT:
16557 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16558 type = short_integer_type_node;
16559 break;
16560 case RID_INT:
16561 if (decl_specs)
16562 decl_specs->explicit_int_p = true;
16563 type = integer_type_node;
16564 break;
16565 case RID_INT_N_0:
16566 case RID_INT_N_1:
16567 case RID_INT_N_2:
16568 case RID_INT_N_3:
16569 idx = token->keyword - RID_INT_N_0;
16570 if (! int_n_enabled_p [idx])
16571 break;
16572 if (decl_specs)
16573 {
16574 decl_specs->explicit_intN_p = true;
16575 decl_specs->int_n_idx = idx;
16576 }
16577 type = int_n_trees [idx].signed_type;
16578 break;
16579 case RID_LONG:
16580 if (decl_specs)
16581 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16582 type = long_integer_type_node;
16583 break;
16584 case RID_SIGNED:
16585 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16586 type = integer_type_node;
16587 break;
16588 case RID_UNSIGNED:
16589 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16590 type = unsigned_type_node;
16591 break;
16592 case RID_FLOAT:
16593 type = float_type_node;
16594 break;
16595 case RID_DOUBLE:
16596 type = double_type_node;
16597 break;
16598 case RID_VOID:
16599 type = void_type_node;
16600 break;
16601
16602 case RID_AUTO:
16603 maybe_warn_cpp0x (CPP0X_AUTO);
16604 if (parser->auto_is_implicit_function_template_parm_p)
16605 {
16606 /* The 'auto' might be the placeholder return type for a function decl
16607 with trailing return type. */
16608 bool have_trailing_return_fn_decl = false;
16609
16610 cp_parser_parse_tentatively (parser);
16611 cp_lexer_consume_token (parser->lexer);
16612 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16613 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16614 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16615 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16616 {
16617 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16618 {
16619 cp_lexer_consume_token (parser->lexer);
16620 cp_parser_skip_to_closing_parenthesis (parser,
16621 /*recovering*/false,
16622 /*or_comma*/false,
16623 /*consume_paren*/true);
16624 continue;
16625 }
16626
16627 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16628 {
16629 have_trailing_return_fn_decl = true;
16630 break;
16631 }
16632
16633 cp_lexer_consume_token (parser->lexer);
16634 }
16635 cp_parser_abort_tentative_parse (parser);
16636
16637 if (have_trailing_return_fn_decl)
16638 {
16639 type = make_auto ();
16640 break;
16641 }
16642
16643 if (cxx_dialect >= cxx14)
16644 {
16645 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16646 type = TREE_TYPE (type);
16647 }
16648 else
16649 type = error_mark_node;
16650
16651 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16652 {
16653 if (cxx_dialect < cxx14)
16654 error_at (token->location,
16655 "use of %<auto%> in lambda parameter declaration "
16656 "only available with "
16657 "-std=c++14 or -std=gnu++14");
16658 }
16659 else if (cxx_dialect < cxx14)
16660 error_at (token->location,
16661 "use of %<auto%> in parameter declaration "
16662 "only available with "
16663 "-std=c++14 or -std=gnu++14");
16664 else if (!flag_concepts)
16665 pedwarn (token->location, OPT_Wpedantic,
16666 "ISO C++ forbids use of %<auto%> in parameter "
16667 "declaration");
16668 }
16669 else
16670 type = make_auto ();
16671 break;
16672
16673 case RID_DECLTYPE:
16674 /* Since DR 743, decltype can either be a simple-type-specifier by
16675 itself or begin a nested-name-specifier. Parsing it will replace
16676 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16677 handling below decide what to do. */
16678 cp_parser_decltype (parser);
16679 cp_lexer_set_token_position (parser->lexer, token);
16680 break;
16681
16682 case RID_TYPEOF:
16683 /* Consume the `typeof' token. */
16684 cp_lexer_consume_token (parser->lexer);
16685 /* Parse the operand to `typeof'. */
16686 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16687 /* If it is not already a TYPE, take its type. */
16688 if (!TYPE_P (type))
16689 type = finish_typeof (type);
16690
16691 if (decl_specs)
16692 cp_parser_set_decl_spec_type (decl_specs, type,
16693 token,
16694 /*type_definition_p=*/false);
16695
16696 return type;
16697
16698 case RID_UNDERLYING_TYPE:
16699 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16700 if (decl_specs)
16701 cp_parser_set_decl_spec_type (decl_specs, type,
16702 token,
16703 /*type_definition_p=*/false);
16704
16705 return type;
16706
16707 case RID_BASES:
16708 case RID_DIRECT_BASES:
16709 type = cp_parser_trait_expr (parser, token->keyword);
16710 if (decl_specs)
16711 cp_parser_set_decl_spec_type (decl_specs, type,
16712 token,
16713 /*type_definition_p=*/false);
16714 return type;
16715 default:
16716 break;
16717 }
16718
16719 /* If token is an already-parsed decltype not followed by ::,
16720 it's a simple-type-specifier. */
16721 if (token->type == CPP_DECLTYPE
16722 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16723 {
16724 type = saved_checks_value (token->u.tree_check_value);
16725 if (decl_specs)
16726 {
16727 cp_parser_set_decl_spec_type (decl_specs, type,
16728 token,
16729 /*type_definition_p=*/false);
16730 /* Remember that we are handling a decltype in order to
16731 implement the resolution of DR 1510 when the argument
16732 isn't instantiation dependent. */
16733 decl_specs->decltype_p = true;
16734 }
16735 cp_lexer_consume_token (parser->lexer);
16736 return type;
16737 }
16738
16739 /* If the type-specifier was for a built-in type, we're done. */
16740 if (type)
16741 {
16742 /* Record the type. */
16743 if (decl_specs
16744 && (token->keyword != RID_SIGNED
16745 && token->keyword != RID_UNSIGNED
16746 && token->keyword != RID_SHORT
16747 && token->keyword != RID_LONG))
16748 cp_parser_set_decl_spec_type (decl_specs,
16749 type,
16750 token,
16751 /*type_definition_p=*/false);
16752 if (decl_specs)
16753 decl_specs->any_specifiers_p = true;
16754
16755 /* Consume the token. */
16756 cp_lexer_consume_token (parser->lexer);
16757
16758 if (type == error_mark_node)
16759 return error_mark_node;
16760
16761 /* There is no valid C++ program where a non-template type is
16762 followed by a "<". That usually indicates that the user thought
16763 that the type was a template. */
16764 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16765 token->location);
16766
16767 return TYPE_NAME (type);
16768 }
16769
16770 /* The type-specifier must be a user-defined type. */
16771 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16772 {
16773 bool qualified_p;
16774 bool global_p;
16775
16776 /* Don't gobble tokens or issue error messages if this is an
16777 optional type-specifier. */
16778 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16779 cp_parser_parse_tentatively (parser);
16780
16781 token = cp_lexer_peek_token (parser->lexer);
16782
16783 /* Look for the optional `::' operator. */
16784 global_p
16785 = (cp_parser_global_scope_opt (parser,
16786 /*current_scope_valid_p=*/false)
16787 != NULL_TREE);
16788 /* Look for the nested-name specifier. */
16789 qualified_p
16790 = (cp_parser_nested_name_specifier_opt (parser,
16791 /*typename_keyword_p=*/false,
16792 /*check_dependency_p=*/true,
16793 /*type_p=*/false,
16794 /*is_declaration=*/false)
16795 != NULL_TREE);
16796 /* If we have seen a nested-name-specifier, and the next token
16797 is `template', then we are using the template-id production. */
16798 if (parser->scope
16799 && cp_parser_optional_template_keyword (parser))
16800 {
16801 /* Look for the template-id. */
16802 type = cp_parser_template_id (parser,
16803 /*template_keyword_p=*/true,
16804 /*check_dependency_p=*/true,
16805 none_type,
16806 /*is_declaration=*/false);
16807 /* If the template-id did not name a type, we are out of
16808 luck. */
16809 if (TREE_CODE (type) != TYPE_DECL)
16810 {
16811 cp_parser_error (parser, "expected template-id for type");
16812 type = NULL_TREE;
16813 }
16814 }
16815 /* Otherwise, look for a type-name. */
16816 else
16817 type = cp_parser_type_name (parser);
16818 /* Keep track of all name-lookups performed in class scopes. */
16819 if (type
16820 && !global_p
16821 && !qualified_p
16822 && TREE_CODE (type) == TYPE_DECL
16823 && identifier_p (DECL_NAME (type)))
16824 maybe_note_name_used_in_class (DECL_NAME (type), type);
16825 /* If it didn't work out, we don't have a TYPE. */
16826 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16827 && !cp_parser_parse_definitely (parser))
16828 type = NULL_TREE;
16829 if (!type && cxx_dialect >= cxx1z)
16830 {
16831 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16832 cp_parser_parse_tentatively (parser);
16833
16834 cp_parser_global_scope_opt (parser,
16835 /*current_scope_valid_p=*/false);
16836 cp_parser_nested_name_specifier_opt (parser,
16837 /*typename_keyword_p=*/false,
16838 /*check_dependency_p=*/true,
16839 /*type_p=*/false,
16840 /*is_declaration=*/false);
16841 tree name = cp_parser_identifier (parser);
16842 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16843 && parser->scope != error_mark_node)
16844 {
16845 tree tmpl = cp_parser_lookup_name (parser, name,
16846 none_type,
16847 /*is_template=*/false,
16848 /*is_namespace=*/false,
16849 /*check_dependency=*/true,
16850 /*ambiguous_decls=*/NULL,
16851 token->location);
16852 if (tmpl && tmpl != error_mark_node
16853 && (DECL_CLASS_TEMPLATE_P (tmpl)
16854 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16855 type = make_template_placeholder (tmpl);
16856 else
16857 {
16858 type = error_mark_node;
16859 if (!cp_parser_simulate_error (parser))
16860 cp_parser_name_lookup_error (parser, name, tmpl,
16861 NLE_TYPE, token->location);
16862 }
16863 }
16864 else
16865 type = error_mark_node;
16866
16867 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16868 && !cp_parser_parse_definitely (parser))
16869 type = NULL_TREE;
16870 }
16871 if (type && decl_specs)
16872 cp_parser_set_decl_spec_type (decl_specs, type,
16873 token,
16874 /*type_definition_p=*/false);
16875 }
16876
16877 /* If we didn't get a type-name, issue an error message. */
16878 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16879 {
16880 cp_parser_error (parser, "expected type-name");
16881 return error_mark_node;
16882 }
16883
16884 if (type && type != error_mark_node)
16885 {
16886 /* See if TYPE is an Objective-C type, and if so, parse and
16887 accept any protocol references following it. Do this before
16888 the cp_parser_check_for_invalid_template_id() call, because
16889 Objective-C types can be followed by '<...>' which would
16890 enclose protocol names rather than template arguments, and so
16891 everything is fine. */
16892 if (c_dialect_objc () && !parser->scope
16893 && (objc_is_id (type) || objc_is_class_name (type)))
16894 {
16895 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16896 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16897
16898 /* Clobber the "unqualified" type previously entered into
16899 DECL_SPECS with the new, improved protocol-qualified version. */
16900 if (decl_specs)
16901 decl_specs->type = qual_type;
16902
16903 return qual_type;
16904 }
16905
16906 /* There is no valid C++ program where a non-template type is
16907 followed by a "<". That usually indicates that the user
16908 thought that the type was a template. */
16909 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16910 none_type,
16911 token->location);
16912 }
16913
16914 return type;
16915 }
16916
16917 /* Parse a type-name.
16918
16919 type-name:
16920 class-name
16921 enum-name
16922 typedef-name
16923 simple-template-id [in c++0x]
16924
16925 enum-name:
16926 identifier
16927
16928 typedef-name:
16929 identifier
16930
16931 Concepts:
16932
16933 type-name:
16934 concept-name
16935 partial-concept-id
16936
16937 concept-name:
16938 identifier
16939
16940 Returns a TYPE_DECL for the type. */
16941
16942 static tree
16943 cp_parser_type_name (cp_parser* parser)
16944 {
16945 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16946 }
16947
16948 /* See above. */
16949 static tree
16950 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16951 {
16952 tree type_decl;
16953
16954 /* We can't know yet whether it is a class-name or not. */
16955 cp_parser_parse_tentatively (parser);
16956 /* Try a class-name. */
16957 type_decl = cp_parser_class_name (parser,
16958 typename_keyword_p,
16959 /*template_keyword_p=*/false,
16960 none_type,
16961 /*check_dependency_p=*/true,
16962 /*class_head_p=*/false,
16963 /*is_declaration=*/false);
16964 /* If it's not a class-name, keep looking. */
16965 if (!cp_parser_parse_definitely (parser))
16966 {
16967 if (cxx_dialect < cxx11)
16968 /* It must be a typedef-name or an enum-name. */
16969 return cp_parser_nonclass_name (parser);
16970
16971 cp_parser_parse_tentatively (parser);
16972 /* It is either a simple-template-id representing an
16973 instantiation of an alias template... */
16974 type_decl = cp_parser_template_id (parser,
16975 /*template_keyword_p=*/false,
16976 /*check_dependency_p=*/true,
16977 none_type,
16978 /*is_declaration=*/false);
16979 /* Note that this must be an instantiation of an alias template
16980 because [temp.names]/6 says:
16981
16982 A template-id that names an alias template specialization
16983 is a type-name.
16984
16985 Whereas [temp.names]/7 says:
16986
16987 A simple-template-id that names a class template
16988 specialization is a class-name.
16989
16990 With concepts, this could also be a partial-concept-id that
16991 declares a non-type template parameter. */
16992 if (type_decl != NULL_TREE
16993 && TREE_CODE (type_decl) == TYPE_DECL
16994 && TYPE_DECL_ALIAS_P (type_decl))
16995 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16996 else if (is_constrained_parameter (type_decl))
16997 /* Don't do anything. */ ;
16998 else
16999 cp_parser_simulate_error (parser);
17000
17001 if (!cp_parser_parse_definitely (parser))
17002 /* ... Or a typedef-name or an enum-name. */
17003 return cp_parser_nonclass_name (parser);
17004 }
17005
17006 return type_decl;
17007 }
17008
17009 /* Check if DECL and ARGS can form a constrained-type-specifier.
17010 If ARGS is non-null, we try to form a concept check of the
17011 form DECL<?, ARGS> where ? is a wildcard that matches any
17012 kind of template argument. If ARGS is NULL, then we try to
17013 form a concept check of the form DECL<?>. */
17014
17015 static tree
17016 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17017 tree decl, tree args)
17018 {
17019 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17020
17021 /* If we a constrained-type-specifier cannot be deduced. */
17022 if (parser->prevent_constrained_type_specifiers)
17023 return NULL_TREE;
17024
17025 /* A constrained type specifier can only be found in an
17026 overload set or as a reference to a template declaration.
17027
17028 FIXME: This might be masking a bug. It's possible that
17029 that the deduction below is causing template specializations
17030 to be formed with the wildcard as an argument. */
17031 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17032 return NULL_TREE;
17033
17034 /* Try to build a call expression that evaluates the
17035 concept. This can fail if the overload set refers
17036 only to non-templates. */
17037 tree placeholder = build_nt (WILDCARD_DECL);
17038 tree check = build_concept_check (decl, placeholder, args);
17039 if (check == error_mark_node)
17040 return NULL_TREE;
17041
17042 /* Deduce the checked constraint and the prototype parameter.
17043
17044 FIXME: In certain cases, failure to deduce should be a
17045 diagnosable error. */
17046 tree conc;
17047 tree proto;
17048 if (!deduce_constrained_parameter (check, conc, proto))
17049 return NULL_TREE;
17050
17051 /* In template parameter scope, this results in a constrained
17052 parameter. Return a descriptor of that parm. */
17053 if (processing_template_parmlist)
17054 return build_constrained_parameter (conc, proto, args);
17055
17056 /* In a parameter-declaration-clause, constrained-type
17057 specifiers result in invented template parameters. */
17058 if (parser->auto_is_implicit_function_template_parm_p)
17059 {
17060 tree x = build_constrained_parameter (conc, proto, args);
17061 return synthesize_implicit_template_parm (parser, x);
17062 }
17063 else
17064 {
17065 /* Otherwise, we're in a context where the constrained
17066 type name is deduced and the constraint applies
17067 after deduction. */
17068 return make_constrained_auto (conc, args);
17069 }
17070
17071 return NULL_TREE;
17072 }
17073
17074 /* If DECL refers to a concept, return a TYPE_DECL representing
17075 the result of using the constrained type specifier in the
17076 current context. DECL refers to a concept if
17077
17078 - it is an overload set containing a function concept taking a single
17079 type argument, or
17080
17081 - it is a variable concept taking a single type argument. */
17082
17083 static tree
17084 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17085 {
17086 if (flag_concepts
17087 && (TREE_CODE (decl) == OVERLOAD
17088 || BASELINK_P (decl)
17089 || variable_concept_p (decl)))
17090 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17091 else
17092 return NULL_TREE;
17093 }
17094
17095 /* Check if DECL and ARGS form a partial-concept-id. If so,
17096 assign ID to the resulting constrained placeholder.
17097
17098 Returns true if the partial-concept-id designates a placeholder
17099 and false otherwise. Note that *id is set to NULL_TREE in
17100 this case. */
17101
17102 static tree
17103 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17104 {
17105 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17106 }
17107
17108 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17109 or a concept-name.
17110
17111 enum-name:
17112 identifier
17113
17114 typedef-name:
17115 identifier
17116
17117 concept-name:
17118 identifier
17119
17120 Returns a TYPE_DECL for the type. */
17121
17122 static tree
17123 cp_parser_nonclass_name (cp_parser* parser)
17124 {
17125 tree type_decl;
17126 tree identifier;
17127
17128 cp_token *token = cp_lexer_peek_token (parser->lexer);
17129 identifier = cp_parser_identifier (parser);
17130 if (identifier == error_mark_node)
17131 return error_mark_node;
17132
17133 /* Look up the type-name. */
17134 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17135
17136 type_decl = strip_using_decl (type_decl);
17137
17138 /* If we found an overload set, then it may refer to a concept-name. */
17139 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17140 type_decl = decl;
17141
17142 if (TREE_CODE (type_decl) != TYPE_DECL
17143 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17144 {
17145 /* See if this is an Objective-C type. */
17146 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17147 tree type = objc_get_protocol_qualified_type (identifier, protos);
17148 if (type)
17149 type_decl = TYPE_NAME (type);
17150 }
17151
17152 /* Issue an error if we did not find a type-name. */
17153 if (TREE_CODE (type_decl) != TYPE_DECL
17154 /* In Objective-C, we have the complication that class names are
17155 normally type names and start declarations (eg, the
17156 "NSObject" in "NSObject *object;"), but can be used in an
17157 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17158 is an expression. So, a classname followed by a dot is not a
17159 valid type-name. */
17160 || (objc_is_class_name (TREE_TYPE (type_decl))
17161 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17162 {
17163 if (!cp_parser_simulate_error (parser))
17164 cp_parser_name_lookup_error (parser, identifier, type_decl,
17165 NLE_TYPE, token->location);
17166 return error_mark_node;
17167 }
17168 /* Remember that the name was used in the definition of the
17169 current class so that we can check later to see if the
17170 meaning would have been different after the class was
17171 entirely defined. */
17172 else if (type_decl != error_mark_node
17173 && !parser->scope)
17174 maybe_note_name_used_in_class (identifier, type_decl);
17175
17176 return type_decl;
17177 }
17178
17179 /* Parse an elaborated-type-specifier. Note that the grammar given
17180 here incorporates the resolution to DR68.
17181
17182 elaborated-type-specifier:
17183 class-key :: [opt] nested-name-specifier [opt] identifier
17184 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17185 enum-key :: [opt] nested-name-specifier [opt] identifier
17186 typename :: [opt] nested-name-specifier identifier
17187 typename :: [opt] nested-name-specifier template [opt]
17188 template-id
17189
17190 GNU extension:
17191
17192 elaborated-type-specifier:
17193 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17194 class-key attributes :: [opt] nested-name-specifier [opt]
17195 template [opt] template-id
17196 enum attributes :: [opt] nested-name-specifier [opt] identifier
17197
17198 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17199 declared `friend'. If IS_DECLARATION is TRUE, then this
17200 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17201 something is being declared.
17202
17203 Returns the TYPE specified. */
17204
17205 static tree
17206 cp_parser_elaborated_type_specifier (cp_parser* parser,
17207 bool is_friend,
17208 bool is_declaration)
17209 {
17210 enum tag_types tag_type;
17211 tree identifier;
17212 tree type = NULL_TREE;
17213 tree attributes = NULL_TREE;
17214 tree globalscope;
17215 cp_token *token = NULL;
17216
17217 /* See if we're looking at the `enum' keyword. */
17218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17219 {
17220 /* Consume the `enum' token. */
17221 cp_lexer_consume_token (parser->lexer);
17222 /* Remember that it's an enumeration type. */
17223 tag_type = enum_type;
17224 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17225 enums) is used here. */
17226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17227 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17228 {
17229 pedwarn (input_location, 0, "elaborated-type-specifier "
17230 "for a scoped enum must not use the %<%D%> keyword",
17231 cp_lexer_peek_token (parser->lexer)->u.value);
17232 /* Consume the `struct' or `class' and parse it anyway. */
17233 cp_lexer_consume_token (parser->lexer);
17234 }
17235 /* Parse the attributes. */
17236 attributes = cp_parser_attributes_opt (parser);
17237 }
17238 /* Or, it might be `typename'. */
17239 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17240 RID_TYPENAME))
17241 {
17242 /* Consume the `typename' token. */
17243 cp_lexer_consume_token (parser->lexer);
17244 /* Remember that it's a `typename' type. */
17245 tag_type = typename_type;
17246 }
17247 /* Otherwise it must be a class-key. */
17248 else
17249 {
17250 tag_type = cp_parser_class_key (parser);
17251 if (tag_type == none_type)
17252 return error_mark_node;
17253 /* Parse the attributes. */
17254 attributes = cp_parser_attributes_opt (parser);
17255 }
17256
17257 /* Look for the `::' operator. */
17258 globalscope = cp_parser_global_scope_opt (parser,
17259 /*current_scope_valid_p=*/false);
17260 /* Look for the nested-name-specifier. */
17261 tree nested_name_specifier;
17262 if (tag_type == typename_type && !globalscope)
17263 {
17264 nested_name_specifier
17265 = cp_parser_nested_name_specifier (parser,
17266 /*typename_keyword_p=*/true,
17267 /*check_dependency_p=*/true,
17268 /*type_p=*/true,
17269 is_declaration);
17270 if (!nested_name_specifier)
17271 return error_mark_node;
17272 }
17273 else
17274 /* Even though `typename' is not present, the proposed resolution
17275 to Core Issue 180 says that in `class A<T>::B', `B' should be
17276 considered a type-name, even if `A<T>' is dependent. */
17277 nested_name_specifier
17278 = cp_parser_nested_name_specifier_opt (parser,
17279 /*typename_keyword_p=*/true,
17280 /*check_dependency_p=*/true,
17281 /*type_p=*/true,
17282 is_declaration);
17283 /* For everything but enumeration types, consider a template-id.
17284 For an enumeration type, consider only a plain identifier. */
17285 if (tag_type != enum_type)
17286 {
17287 bool template_p = false;
17288 tree decl;
17289
17290 /* Allow the `template' keyword. */
17291 template_p = cp_parser_optional_template_keyword (parser);
17292 /* If we didn't see `template', we don't know if there's a
17293 template-id or not. */
17294 if (!template_p)
17295 cp_parser_parse_tentatively (parser);
17296 /* Parse the template-id. */
17297 token = cp_lexer_peek_token (parser->lexer);
17298 decl = cp_parser_template_id (parser, template_p,
17299 /*check_dependency_p=*/true,
17300 tag_type,
17301 is_declaration);
17302 /* If we didn't find a template-id, look for an ordinary
17303 identifier. */
17304 if (!template_p && !cp_parser_parse_definitely (parser))
17305 ;
17306 /* We can get here when cp_parser_template_id, called by
17307 cp_parser_class_name with tag_type == none_type, succeeds
17308 and caches a BASELINK. Then, when called again here,
17309 instead of failing and returning an error_mark_node
17310 returns it (see template/typename17.C in C++11).
17311 ??? Could we diagnose this earlier? */
17312 else if (tag_type == typename_type && BASELINK_P (decl))
17313 {
17314 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17315 type = error_mark_node;
17316 }
17317 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17318 in effect, then we must assume that, upon instantiation, the
17319 template will correspond to a class. */
17320 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17321 && tag_type == typename_type)
17322 type = make_typename_type (parser->scope, decl,
17323 typename_type,
17324 /*complain=*/tf_error);
17325 /* If the `typename' keyword is in effect and DECL is not a type
17326 decl, then type is non existent. */
17327 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17328 ;
17329 else if (TREE_CODE (decl) == TYPE_DECL)
17330 {
17331 type = check_elaborated_type_specifier (tag_type, decl,
17332 /*allow_template_p=*/true);
17333
17334 /* If the next token is a semicolon, this must be a specialization,
17335 instantiation, or friend declaration. Check the scope while we
17336 still know whether or not we had a nested-name-specifier. */
17337 if (type != error_mark_node
17338 && !nested_name_specifier && !is_friend
17339 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17340 check_unqualified_spec_or_inst (type, token->location);
17341 }
17342 else if (decl == error_mark_node)
17343 type = error_mark_node;
17344 }
17345
17346 if (!type)
17347 {
17348 token = cp_lexer_peek_token (parser->lexer);
17349 identifier = cp_parser_identifier (parser);
17350
17351 if (identifier == error_mark_node)
17352 {
17353 parser->scope = NULL_TREE;
17354 return error_mark_node;
17355 }
17356
17357 /* For a `typename', we needn't call xref_tag. */
17358 if (tag_type == typename_type
17359 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17360 return cp_parser_make_typename_type (parser, identifier,
17361 token->location);
17362
17363 /* Template parameter lists apply only if we are not within a
17364 function parameter list. */
17365 bool template_parm_lists_apply
17366 = parser->num_template_parameter_lists;
17367 if (template_parm_lists_apply)
17368 for (cp_binding_level *s = current_binding_level;
17369 s && s->kind != sk_template_parms;
17370 s = s->level_chain)
17371 if (s->kind == sk_function_parms)
17372 template_parm_lists_apply = false;
17373
17374 /* Look up a qualified name in the usual way. */
17375 if (parser->scope)
17376 {
17377 tree decl;
17378 tree ambiguous_decls;
17379
17380 decl = cp_parser_lookup_name (parser, identifier,
17381 tag_type,
17382 /*is_template=*/false,
17383 /*is_namespace=*/false,
17384 /*check_dependency=*/true,
17385 &ambiguous_decls,
17386 token->location);
17387
17388 /* If the lookup was ambiguous, an error will already have been
17389 issued. */
17390 if (ambiguous_decls)
17391 return error_mark_node;
17392
17393 /* If we are parsing friend declaration, DECL may be a
17394 TEMPLATE_DECL tree node here. However, we need to check
17395 whether this TEMPLATE_DECL results in valid code. Consider
17396 the following example:
17397
17398 namespace N {
17399 template <class T> class C {};
17400 }
17401 class X {
17402 template <class T> friend class N::C; // #1, valid code
17403 };
17404 template <class T> class Y {
17405 friend class N::C; // #2, invalid code
17406 };
17407
17408 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17409 name lookup of `N::C'. We see that friend declaration must
17410 be template for the code to be valid. Note that
17411 processing_template_decl does not work here since it is
17412 always 1 for the above two cases. */
17413
17414 decl = (cp_parser_maybe_treat_template_as_class
17415 (decl, /*tag_name_p=*/is_friend
17416 && template_parm_lists_apply));
17417
17418 if (TREE_CODE (decl) != TYPE_DECL)
17419 {
17420 cp_parser_diagnose_invalid_type_name (parser,
17421 identifier,
17422 token->location);
17423 return error_mark_node;
17424 }
17425
17426 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17427 {
17428 bool allow_template = (template_parm_lists_apply
17429 || DECL_SELF_REFERENCE_P (decl));
17430 type = check_elaborated_type_specifier (tag_type, decl,
17431 allow_template);
17432
17433 if (type == error_mark_node)
17434 return error_mark_node;
17435 }
17436
17437 /* Forward declarations of nested types, such as
17438
17439 class C1::C2;
17440 class C1::C2::C3;
17441
17442 are invalid unless all components preceding the final '::'
17443 are complete. If all enclosing types are complete, these
17444 declarations become merely pointless.
17445
17446 Invalid forward declarations of nested types are errors
17447 caught elsewhere in parsing. Those that are pointless arrive
17448 here. */
17449
17450 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17451 && !is_friend && !processing_explicit_instantiation)
17452 warning (0, "declaration %qD does not declare anything", decl);
17453
17454 type = TREE_TYPE (decl);
17455 }
17456 else
17457 {
17458 /* An elaborated-type-specifier sometimes introduces a new type and
17459 sometimes names an existing type. Normally, the rule is that it
17460 introduces a new type only if there is not an existing type of
17461 the same name already in scope. For example, given:
17462
17463 struct S {};
17464 void f() { struct S s; }
17465
17466 the `struct S' in the body of `f' is the same `struct S' as in
17467 the global scope; the existing definition is used. However, if
17468 there were no global declaration, this would introduce a new
17469 local class named `S'.
17470
17471 An exception to this rule applies to the following code:
17472
17473 namespace N { struct S; }
17474
17475 Here, the elaborated-type-specifier names a new type
17476 unconditionally; even if there is already an `S' in the
17477 containing scope this declaration names a new type.
17478 This exception only applies if the elaborated-type-specifier
17479 forms the complete declaration:
17480
17481 [class.name]
17482
17483 A declaration consisting solely of `class-key identifier ;' is
17484 either a redeclaration of the name in the current scope or a
17485 forward declaration of the identifier as a class name. It
17486 introduces the name into the current scope.
17487
17488 We are in this situation precisely when the next token is a `;'.
17489
17490 An exception to the exception is that a `friend' declaration does
17491 *not* name a new type; i.e., given:
17492
17493 struct S { friend struct T; };
17494
17495 `T' is not a new type in the scope of `S'.
17496
17497 Also, `new struct S' or `sizeof (struct S)' never results in the
17498 definition of a new type; a new type can only be declared in a
17499 declaration context. */
17500
17501 tag_scope ts;
17502 bool template_p;
17503
17504 if (is_friend)
17505 /* Friends have special name lookup rules. */
17506 ts = ts_within_enclosing_non_class;
17507 else if (is_declaration
17508 && cp_lexer_next_token_is (parser->lexer,
17509 CPP_SEMICOLON))
17510 /* This is a `class-key identifier ;' */
17511 ts = ts_current;
17512 else
17513 ts = ts_global;
17514
17515 template_p =
17516 (template_parm_lists_apply
17517 && (cp_parser_next_token_starts_class_definition_p (parser)
17518 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17519 /* An unqualified name was used to reference this type, so
17520 there were no qualifying templates. */
17521 if (template_parm_lists_apply
17522 && !cp_parser_check_template_parameters (parser,
17523 /*num_templates=*/0,
17524 token->location,
17525 /*declarator=*/NULL))
17526 return error_mark_node;
17527 type = xref_tag (tag_type, identifier, ts, template_p);
17528 }
17529 }
17530
17531 if (type == error_mark_node)
17532 return error_mark_node;
17533
17534 /* Allow attributes on forward declarations of classes. */
17535 if (attributes)
17536 {
17537 if (TREE_CODE (type) == TYPENAME_TYPE)
17538 warning (OPT_Wattributes,
17539 "attributes ignored on uninstantiated type");
17540 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17541 && ! processing_explicit_instantiation)
17542 warning (OPT_Wattributes,
17543 "attributes ignored on template instantiation");
17544 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17545 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17546 else
17547 warning (OPT_Wattributes,
17548 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17549 }
17550
17551 if (tag_type != enum_type)
17552 {
17553 /* Indicate whether this class was declared as a `class' or as a
17554 `struct'. */
17555 if (CLASS_TYPE_P (type))
17556 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17557 cp_parser_check_class_key (tag_type, type);
17558 }
17559
17560 /* A "<" cannot follow an elaborated type specifier. If that
17561 happens, the user was probably trying to form a template-id. */
17562 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17563 token->location);
17564
17565 return type;
17566 }
17567
17568 /* Parse an enum-specifier.
17569
17570 enum-specifier:
17571 enum-head { enumerator-list [opt] }
17572 enum-head { enumerator-list , } [C++0x]
17573
17574 enum-head:
17575 enum-key identifier [opt] enum-base [opt]
17576 enum-key nested-name-specifier identifier enum-base [opt]
17577
17578 enum-key:
17579 enum
17580 enum class [C++0x]
17581 enum struct [C++0x]
17582
17583 enum-base: [C++0x]
17584 : type-specifier-seq
17585
17586 opaque-enum-specifier:
17587 enum-key identifier enum-base [opt] ;
17588
17589 GNU Extensions:
17590 enum-key attributes[opt] identifier [opt] enum-base [opt]
17591 { enumerator-list [opt] }attributes[opt]
17592 enum-key attributes[opt] identifier [opt] enum-base [opt]
17593 { enumerator-list, }attributes[opt] [C++0x]
17594
17595 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17596 if the token stream isn't an enum-specifier after all. */
17597
17598 static tree
17599 cp_parser_enum_specifier (cp_parser* parser)
17600 {
17601 tree identifier;
17602 tree type = NULL_TREE;
17603 tree prev_scope;
17604 tree nested_name_specifier = NULL_TREE;
17605 tree attributes;
17606 bool scoped_enum_p = false;
17607 bool has_underlying_type = false;
17608 bool nested_being_defined = false;
17609 bool new_value_list = false;
17610 bool is_new_type = false;
17611 bool is_unnamed = false;
17612 tree underlying_type = NULL_TREE;
17613 cp_token *type_start_token = NULL;
17614 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17615
17616 parser->colon_corrects_to_scope_p = false;
17617
17618 /* Parse tentatively so that we can back up if we don't find a
17619 enum-specifier. */
17620 cp_parser_parse_tentatively (parser);
17621
17622 /* Caller guarantees that the current token is 'enum', an identifier
17623 possibly follows, and the token after that is an opening brace.
17624 If we don't have an identifier, fabricate an anonymous name for
17625 the enumeration being defined. */
17626 cp_lexer_consume_token (parser->lexer);
17627
17628 /* Parse the "class" or "struct", which indicates a scoped
17629 enumeration type in C++0x. */
17630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17631 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17632 {
17633 if (cxx_dialect < cxx11)
17634 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17635
17636 /* Consume the `struct' or `class' token. */
17637 cp_lexer_consume_token (parser->lexer);
17638
17639 scoped_enum_p = true;
17640 }
17641
17642 attributes = cp_parser_attributes_opt (parser);
17643
17644 /* Clear the qualification. */
17645 parser->scope = NULL_TREE;
17646 parser->qualifying_scope = NULL_TREE;
17647 parser->object_scope = NULL_TREE;
17648
17649 /* Figure out in what scope the declaration is being placed. */
17650 prev_scope = current_scope ();
17651
17652 type_start_token = cp_lexer_peek_token (parser->lexer);
17653
17654 push_deferring_access_checks (dk_no_check);
17655 nested_name_specifier
17656 = cp_parser_nested_name_specifier_opt (parser,
17657 /*typename_keyword_p=*/true,
17658 /*check_dependency_p=*/false,
17659 /*type_p=*/false,
17660 /*is_declaration=*/false);
17661
17662 if (nested_name_specifier)
17663 {
17664 tree name;
17665
17666 identifier = cp_parser_identifier (parser);
17667 name = cp_parser_lookup_name (parser, identifier,
17668 enum_type,
17669 /*is_template=*/false,
17670 /*is_namespace=*/false,
17671 /*check_dependency=*/true,
17672 /*ambiguous_decls=*/NULL,
17673 input_location);
17674 if (name && name != error_mark_node)
17675 {
17676 type = TREE_TYPE (name);
17677 if (TREE_CODE (type) == TYPENAME_TYPE)
17678 {
17679 /* Are template enums allowed in ISO? */
17680 if (template_parm_scope_p ())
17681 pedwarn (type_start_token->location, OPT_Wpedantic,
17682 "%qD is an enumeration template", name);
17683 /* ignore a typename reference, for it will be solved by name
17684 in start_enum. */
17685 type = NULL_TREE;
17686 }
17687 }
17688 else if (nested_name_specifier == error_mark_node)
17689 /* We already issued an error. */;
17690 else
17691 {
17692 error_at (type_start_token->location,
17693 "%qD does not name an enumeration in %qT",
17694 identifier, nested_name_specifier);
17695 nested_name_specifier = error_mark_node;
17696 }
17697 }
17698 else
17699 {
17700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17701 identifier = cp_parser_identifier (parser);
17702 else
17703 {
17704 identifier = make_anon_name ();
17705 is_unnamed = true;
17706 if (scoped_enum_p)
17707 error_at (type_start_token->location,
17708 "unnamed scoped enum is not allowed");
17709 }
17710 }
17711 pop_deferring_access_checks ();
17712
17713 /* Check for the `:' that denotes a specified underlying type in C++0x.
17714 Note that a ':' could also indicate a bitfield width, however. */
17715 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17716 {
17717 cp_decl_specifier_seq type_specifiers;
17718
17719 /* Consume the `:'. */
17720 cp_lexer_consume_token (parser->lexer);
17721
17722 /* Parse the type-specifier-seq. */
17723 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17724 /*is_trailing_return=*/false,
17725 &type_specifiers);
17726
17727 /* At this point this is surely not elaborated type specifier. */
17728 if (!cp_parser_parse_definitely (parser))
17729 return NULL_TREE;
17730
17731 if (cxx_dialect < cxx11)
17732 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17733
17734 has_underlying_type = true;
17735
17736 /* If that didn't work, stop. */
17737 if (type_specifiers.type != error_mark_node)
17738 {
17739 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17740 /*initialized=*/0, NULL);
17741 if (underlying_type == error_mark_node
17742 || check_for_bare_parameter_packs (underlying_type))
17743 underlying_type = NULL_TREE;
17744 }
17745 }
17746
17747 /* Look for the `{' but don't consume it yet. */
17748 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17749 {
17750 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17751 {
17752 cp_parser_error (parser, "expected %<{%>");
17753 if (has_underlying_type)
17754 {
17755 type = NULL_TREE;
17756 goto out;
17757 }
17758 }
17759 /* An opaque-enum-specifier must have a ';' here. */
17760 if ((scoped_enum_p || underlying_type)
17761 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17762 {
17763 cp_parser_error (parser, "expected %<;%> or %<{%>");
17764 if (has_underlying_type)
17765 {
17766 type = NULL_TREE;
17767 goto out;
17768 }
17769 }
17770 }
17771
17772 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17773 return NULL_TREE;
17774
17775 if (nested_name_specifier)
17776 {
17777 if (CLASS_TYPE_P (nested_name_specifier))
17778 {
17779 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17780 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17781 push_scope (nested_name_specifier);
17782 }
17783 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17784 {
17785 push_nested_namespace (nested_name_specifier);
17786 }
17787 }
17788
17789 /* Issue an error message if type-definitions are forbidden here. */
17790 if (!cp_parser_check_type_definition (parser))
17791 type = error_mark_node;
17792 else
17793 /* Create the new type. We do this before consuming the opening
17794 brace so the enum will be recorded as being on the line of its
17795 tag (or the 'enum' keyword, if there is no tag). */
17796 type = start_enum (identifier, type, underlying_type,
17797 attributes, scoped_enum_p, &is_new_type);
17798
17799 /* If the next token is not '{' it is an opaque-enum-specifier or an
17800 elaborated-type-specifier. */
17801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17802 {
17803 timevar_push (TV_PARSE_ENUM);
17804 if (nested_name_specifier
17805 && nested_name_specifier != error_mark_node)
17806 {
17807 /* The following catches invalid code such as:
17808 enum class S<int>::E { A, B, C }; */
17809 if (!processing_specialization
17810 && CLASS_TYPE_P (nested_name_specifier)
17811 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17812 error_at (type_start_token->location, "cannot add an enumerator "
17813 "list to a template instantiation");
17814
17815 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17816 {
17817 error_at (type_start_token->location,
17818 "%<%T::%E%> has not been declared",
17819 TYPE_CONTEXT (nested_name_specifier),
17820 nested_name_specifier);
17821 type = error_mark_node;
17822 }
17823 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17824 && !CLASS_TYPE_P (nested_name_specifier))
17825 {
17826 error_at (type_start_token->location, "nested name specifier "
17827 "%qT for enum declaration does not name a class "
17828 "or namespace", nested_name_specifier);
17829 type = error_mark_node;
17830 }
17831 /* If that scope does not contain the scope in which the
17832 class was originally declared, the program is invalid. */
17833 else if (prev_scope && !is_ancestor (prev_scope,
17834 nested_name_specifier))
17835 {
17836 if (at_namespace_scope_p ())
17837 error_at (type_start_token->location,
17838 "declaration of %qD in namespace %qD which does not "
17839 "enclose %qD",
17840 type, prev_scope, nested_name_specifier);
17841 else
17842 error_at (type_start_token->location,
17843 "declaration of %qD in %qD which does not "
17844 "enclose %qD",
17845 type, prev_scope, nested_name_specifier);
17846 type = error_mark_node;
17847 }
17848 /* If that scope is the scope where the declaration is being placed
17849 the program is invalid. */
17850 else if (CLASS_TYPE_P (nested_name_specifier)
17851 && CLASS_TYPE_P (prev_scope)
17852 && same_type_p (nested_name_specifier, prev_scope))
17853 {
17854 permerror (type_start_token->location,
17855 "extra qualification not allowed");
17856 nested_name_specifier = NULL_TREE;
17857 }
17858 }
17859
17860 if (scoped_enum_p)
17861 begin_scope (sk_scoped_enum, type);
17862
17863 /* Consume the opening brace. */
17864 cp_lexer_consume_token (parser->lexer);
17865
17866 if (type == error_mark_node)
17867 ; /* Nothing to add */
17868 else if (OPAQUE_ENUM_P (type)
17869 || (cxx_dialect > cxx98 && processing_specialization))
17870 {
17871 new_value_list = true;
17872 SET_OPAQUE_ENUM_P (type, false);
17873 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17874 }
17875 else
17876 {
17877 error_at (type_start_token->location,
17878 "multiple definition of %q#T", type);
17879 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17880 "previous definition here");
17881 type = error_mark_node;
17882 }
17883
17884 if (type == error_mark_node)
17885 cp_parser_skip_to_end_of_block_or_statement (parser);
17886 /* If the next token is not '}', then there are some enumerators. */
17887 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17888 {
17889 if (is_unnamed && !scoped_enum_p)
17890 pedwarn (type_start_token->location, OPT_Wpedantic,
17891 "ISO C++ forbids empty unnamed enum");
17892 }
17893 else
17894 cp_parser_enumerator_list (parser, type);
17895
17896 /* Consume the final '}'. */
17897 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17898
17899 if (scoped_enum_p)
17900 finish_scope ();
17901 timevar_pop (TV_PARSE_ENUM);
17902 }
17903 else
17904 {
17905 /* If a ';' follows, then it is an opaque-enum-specifier
17906 and additional restrictions apply. */
17907 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17908 {
17909 if (is_unnamed)
17910 error_at (type_start_token->location,
17911 "opaque-enum-specifier without name");
17912 else if (nested_name_specifier)
17913 error_at (type_start_token->location,
17914 "opaque-enum-specifier must use a simple identifier");
17915 }
17916 }
17917
17918 /* Look for trailing attributes to apply to this enumeration, and
17919 apply them if appropriate. */
17920 if (cp_parser_allow_gnu_extensions_p (parser))
17921 {
17922 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17923 cplus_decl_attributes (&type,
17924 trailing_attr,
17925 (int) ATTR_FLAG_TYPE_IN_PLACE);
17926 }
17927
17928 /* Finish up the enumeration. */
17929 if (type != error_mark_node)
17930 {
17931 if (new_value_list)
17932 finish_enum_value_list (type);
17933 if (is_new_type)
17934 finish_enum (type);
17935 }
17936
17937 if (nested_name_specifier)
17938 {
17939 if (CLASS_TYPE_P (nested_name_specifier))
17940 {
17941 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17942 pop_scope (nested_name_specifier);
17943 }
17944 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17945 {
17946 pop_nested_namespace (nested_name_specifier);
17947 }
17948 }
17949 out:
17950 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17951 return type;
17952 }
17953
17954 /* Parse an enumerator-list. The enumerators all have the indicated
17955 TYPE.
17956
17957 enumerator-list:
17958 enumerator-definition
17959 enumerator-list , enumerator-definition */
17960
17961 static void
17962 cp_parser_enumerator_list (cp_parser* parser, tree type)
17963 {
17964 while (true)
17965 {
17966 /* Parse an enumerator-definition. */
17967 cp_parser_enumerator_definition (parser, type);
17968
17969 /* If the next token is not a ',', we've reached the end of
17970 the list. */
17971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17972 break;
17973 /* Otherwise, consume the `,' and keep going. */
17974 cp_lexer_consume_token (parser->lexer);
17975 /* If the next token is a `}', there is a trailing comma. */
17976 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17977 {
17978 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17979 pedwarn (input_location, OPT_Wpedantic,
17980 "comma at end of enumerator list");
17981 break;
17982 }
17983 }
17984 }
17985
17986 /* Parse an enumerator-definition. The enumerator has the indicated
17987 TYPE.
17988
17989 enumerator-definition:
17990 enumerator
17991 enumerator = constant-expression
17992
17993 enumerator:
17994 identifier
17995
17996 GNU Extensions:
17997
17998 enumerator-definition:
17999 enumerator attributes [opt]
18000 enumerator attributes [opt] = constant-expression */
18001
18002 static void
18003 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18004 {
18005 tree identifier;
18006 tree value;
18007 location_t loc;
18008
18009 /* Save the input location because we are interested in the location
18010 of the identifier and not the location of the explicit value. */
18011 loc = cp_lexer_peek_token (parser->lexer)->location;
18012
18013 /* Look for the identifier. */
18014 identifier = cp_parser_identifier (parser);
18015 if (identifier == error_mark_node)
18016 return;
18017
18018 /* Parse any specified attributes. */
18019 tree attrs = cp_parser_attributes_opt (parser);
18020
18021 /* If the next token is an '=', then there is an explicit value. */
18022 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18023 {
18024 /* Consume the `=' token. */
18025 cp_lexer_consume_token (parser->lexer);
18026 /* Parse the value. */
18027 value = cp_parser_constant_expression (parser);
18028 }
18029 else
18030 value = NULL_TREE;
18031
18032 /* If we are processing a template, make sure the initializer of the
18033 enumerator doesn't contain any bare template parameter pack. */
18034 if (check_for_bare_parameter_packs (value))
18035 value = error_mark_node;
18036
18037 /* Create the enumerator. */
18038 build_enumerator (identifier, value, type, attrs, loc);
18039 }
18040
18041 /* Parse a namespace-name.
18042
18043 namespace-name:
18044 original-namespace-name
18045 namespace-alias
18046
18047 Returns the NAMESPACE_DECL for the namespace. */
18048
18049 static tree
18050 cp_parser_namespace_name (cp_parser* parser)
18051 {
18052 tree identifier;
18053 tree namespace_decl;
18054
18055 cp_token *token = cp_lexer_peek_token (parser->lexer);
18056
18057 /* Get the name of the namespace. */
18058 identifier = cp_parser_identifier (parser);
18059 if (identifier == error_mark_node)
18060 return error_mark_node;
18061
18062 /* Look up the identifier in the currently active scope. Look only
18063 for namespaces, due to:
18064
18065 [basic.lookup.udir]
18066
18067 When looking up a namespace-name in a using-directive or alias
18068 definition, only namespace names are considered.
18069
18070 And:
18071
18072 [basic.lookup.qual]
18073
18074 During the lookup of a name preceding the :: scope resolution
18075 operator, object, function, and enumerator names are ignored.
18076
18077 (Note that cp_parser_qualifying_entity only calls this
18078 function if the token after the name is the scope resolution
18079 operator.) */
18080 namespace_decl = cp_parser_lookup_name (parser, identifier,
18081 none_type,
18082 /*is_template=*/false,
18083 /*is_namespace=*/true,
18084 /*check_dependency=*/true,
18085 /*ambiguous_decls=*/NULL,
18086 token->location);
18087 /* If it's not a namespace, issue an error. */
18088 if (namespace_decl == error_mark_node
18089 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18090 {
18091 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18092 error_at (token->location, "%qD is not a namespace-name", identifier);
18093 cp_parser_error (parser, "expected namespace-name");
18094 namespace_decl = error_mark_node;
18095 }
18096
18097 return namespace_decl;
18098 }
18099
18100 /* Parse a namespace-definition.
18101
18102 namespace-definition:
18103 named-namespace-definition
18104 unnamed-namespace-definition
18105
18106 named-namespace-definition:
18107 original-namespace-definition
18108 extension-namespace-definition
18109
18110 original-namespace-definition:
18111 namespace identifier { namespace-body }
18112
18113 extension-namespace-definition:
18114 namespace original-namespace-name { namespace-body }
18115
18116 unnamed-namespace-definition:
18117 namespace { namespace-body } */
18118
18119 static void
18120 cp_parser_namespace_definition (cp_parser* parser)
18121 {
18122 tree identifier, attribs;
18123 bool has_visibility;
18124 bool is_inline;
18125 cp_token* token;
18126 int nested_definition_count = 0;
18127
18128 cp_ensure_no_omp_declare_simd (parser);
18129 cp_ensure_no_oacc_routine (parser);
18130 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18131 {
18132 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18133 is_inline = true;
18134 cp_lexer_consume_token (parser->lexer);
18135 }
18136 else
18137 is_inline = false;
18138
18139 /* Look for the `namespace' keyword. */
18140 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18141
18142 /* Parse any specified attributes before the identifier. */
18143 attribs = cp_parser_attributes_opt (parser);
18144
18145 /* Get the name of the namespace. We do not attempt to distinguish
18146 between an original-namespace-definition and an
18147 extension-namespace-definition at this point. The semantic
18148 analysis routines are responsible for that. */
18149 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18150 identifier = cp_parser_identifier (parser);
18151 else
18152 identifier = NULL_TREE;
18153
18154 /* Parse any specified attributes after the identifier. */
18155 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18156 if (post_ident_attribs)
18157 {
18158 if (attribs)
18159 attribs = chainon (attribs, post_ident_attribs);
18160 else
18161 attribs = post_ident_attribs;
18162 }
18163
18164 /* Start the namespace. */
18165 bool ok = push_namespace (identifier);
18166
18167 /* Parse any nested namespace definition. */
18168 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18169 {
18170 if (attribs)
18171 error_at (token->location, "a nested namespace definition cannot have attributes");
18172 if (cxx_dialect < cxx1z)
18173 pedwarn (input_location, OPT_Wpedantic,
18174 "nested namespace definitions only available with "
18175 "-std=c++1z or -std=gnu++1z");
18176 if (is_inline)
18177 error_at (token->location, "a nested namespace definition cannot be inline");
18178 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18179 {
18180 cp_lexer_consume_token (parser->lexer);
18181 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18182 identifier = cp_parser_identifier (parser);
18183 else
18184 {
18185 cp_parser_error (parser, "nested identifier required");
18186 break;
18187 }
18188 if (push_namespace (identifier))
18189 ++nested_definition_count;
18190 }
18191 }
18192
18193 /* Look for the `{' to validate starting the namespace. */
18194 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18195
18196 /* "inline namespace" is equivalent to a stub namespace definition
18197 followed by a strong using directive. */
18198 if (is_inline && ok)
18199 {
18200 tree name_space = current_namespace;
18201 /* Set up namespace association. */
18202 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18203 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18204 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18205 /* Import the contents of the inline namespace. */
18206 pop_namespace ();
18207 do_using_directive (name_space);
18208 push_namespace (identifier);
18209 }
18210
18211 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18212
18213 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18214
18215 /* Parse the body of the namespace. */
18216 cp_parser_namespace_body (parser);
18217
18218 if (has_visibility)
18219 pop_visibility (1);
18220
18221 /* Finish the nested namespace definitions. */
18222 while (nested_definition_count--)
18223 pop_namespace ();
18224
18225 /* Finish the namespace. */
18226 if (ok)
18227 pop_namespace ();
18228 /* Look for the final `}'. */
18229 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18230 }
18231
18232 /* Parse a namespace-body.
18233
18234 namespace-body:
18235 declaration-seq [opt] */
18236
18237 static void
18238 cp_parser_namespace_body (cp_parser* parser)
18239 {
18240 cp_parser_declaration_seq_opt (parser);
18241 }
18242
18243 /* Parse a namespace-alias-definition.
18244
18245 namespace-alias-definition:
18246 namespace identifier = qualified-namespace-specifier ; */
18247
18248 static void
18249 cp_parser_namespace_alias_definition (cp_parser* parser)
18250 {
18251 tree identifier;
18252 tree namespace_specifier;
18253
18254 cp_token *token = cp_lexer_peek_token (parser->lexer);
18255
18256 /* Look for the `namespace' keyword. */
18257 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18258 /* Look for the identifier. */
18259 identifier = cp_parser_identifier (parser);
18260 if (identifier == error_mark_node)
18261 return;
18262 /* Look for the `=' token. */
18263 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18264 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18265 {
18266 error_at (token->location, "%<namespace%> definition is not allowed here");
18267 /* Skip the definition. */
18268 cp_lexer_consume_token (parser->lexer);
18269 if (cp_parser_skip_to_closing_brace (parser))
18270 cp_lexer_consume_token (parser->lexer);
18271 return;
18272 }
18273 cp_parser_require (parser, CPP_EQ, RT_EQ);
18274 /* Look for the qualified-namespace-specifier. */
18275 namespace_specifier
18276 = cp_parser_qualified_namespace_specifier (parser);
18277 /* Look for the `;' token. */
18278 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18279
18280 /* Register the alias in the symbol table. */
18281 do_namespace_alias (identifier, namespace_specifier);
18282 }
18283
18284 /* Parse a qualified-namespace-specifier.
18285
18286 qualified-namespace-specifier:
18287 :: [opt] nested-name-specifier [opt] namespace-name
18288
18289 Returns a NAMESPACE_DECL corresponding to the specified
18290 namespace. */
18291
18292 static tree
18293 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18294 {
18295 /* Look for the optional `::'. */
18296 cp_parser_global_scope_opt (parser,
18297 /*current_scope_valid_p=*/false);
18298
18299 /* Look for the optional nested-name-specifier. */
18300 cp_parser_nested_name_specifier_opt (parser,
18301 /*typename_keyword_p=*/false,
18302 /*check_dependency_p=*/true,
18303 /*type_p=*/false,
18304 /*is_declaration=*/true);
18305
18306 return cp_parser_namespace_name (parser);
18307 }
18308
18309 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18310 access declaration.
18311
18312 using-declaration:
18313 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18314 using :: unqualified-id ;
18315
18316 access-declaration:
18317 qualified-id ;
18318
18319 */
18320
18321 static bool
18322 cp_parser_using_declaration (cp_parser* parser,
18323 bool access_declaration_p)
18324 {
18325 cp_token *token;
18326 bool typename_p = false;
18327 bool global_scope_p;
18328 tree decl;
18329 tree identifier;
18330 tree qscope;
18331 int oldcount = errorcount;
18332 cp_token *diag_token = NULL;
18333
18334 if (access_declaration_p)
18335 {
18336 diag_token = cp_lexer_peek_token (parser->lexer);
18337 cp_parser_parse_tentatively (parser);
18338 }
18339 else
18340 {
18341 /* Look for the `using' keyword. */
18342 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18343
18344 /* Peek at the next token. */
18345 token = cp_lexer_peek_token (parser->lexer);
18346 /* See if it's `typename'. */
18347 if (token->keyword == RID_TYPENAME)
18348 {
18349 /* Remember that we've seen it. */
18350 typename_p = true;
18351 /* Consume the `typename' token. */
18352 cp_lexer_consume_token (parser->lexer);
18353 }
18354 }
18355
18356 /* Look for the optional global scope qualification. */
18357 global_scope_p
18358 = (cp_parser_global_scope_opt (parser,
18359 /*current_scope_valid_p=*/false)
18360 != NULL_TREE);
18361
18362 /* If we saw `typename', or didn't see `::', then there must be a
18363 nested-name-specifier present. */
18364 if (typename_p || !global_scope_p)
18365 {
18366 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18367 /*check_dependency_p=*/true,
18368 /*type_p=*/false,
18369 /*is_declaration=*/true);
18370 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18371 {
18372 cp_parser_skip_to_end_of_block_or_statement (parser);
18373 return false;
18374 }
18375 }
18376 /* Otherwise, we could be in either of the two productions. In that
18377 case, treat the nested-name-specifier as optional. */
18378 else
18379 qscope = cp_parser_nested_name_specifier_opt (parser,
18380 /*typename_keyword_p=*/false,
18381 /*check_dependency_p=*/true,
18382 /*type_p=*/false,
18383 /*is_declaration=*/true);
18384 if (!qscope)
18385 qscope = global_namespace;
18386 else if (UNSCOPED_ENUM_P (qscope))
18387 qscope = CP_TYPE_CONTEXT (qscope);
18388
18389 if (access_declaration_p && cp_parser_error_occurred (parser))
18390 /* Something has already gone wrong; there's no need to parse
18391 further. Since an error has occurred, the return value of
18392 cp_parser_parse_definitely will be false, as required. */
18393 return cp_parser_parse_definitely (parser);
18394
18395 token = cp_lexer_peek_token (parser->lexer);
18396 /* Parse the unqualified-id. */
18397 identifier = cp_parser_unqualified_id (parser,
18398 /*template_keyword_p=*/false,
18399 /*check_dependency_p=*/true,
18400 /*declarator_p=*/true,
18401 /*optional_p=*/false);
18402
18403 if (access_declaration_p)
18404 {
18405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18406 cp_parser_simulate_error (parser);
18407 if (!cp_parser_parse_definitely (parser))
18408 return false;
18409 }
18410
18411 /* The function we call to handle a using-declaration is different
18412 depending on what scope we are in. */
18413 if (qscope == error_mark_node || identifier == error_mark_node)
18414 ;
18415 else if (!identifier_p (identifier)
18416 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18417 /* [namespace.udecl]
18418
18419 A using declaration shall not name a template-id. */
18420 error_at (token->location,
18421 "a template-id may not appear in a using-declaration");
18422 else
18423 {
18424 if (at_class_scope_p ())
18425 {
18426 /* Create the USING_DECL. */
18427 decl = do_class_using_decl (parser->scope, identifier);
18428
18429 if (decl && typename_p)
18430 USING_DECL_TYPENAME_P (decl) = 1;
18431
18432 if (check_for_bare_parameter_packs (decl))
18433 {
18434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18435 return false;
18436 }
18437 else
18438 /* Add it to the list of members in this class. */
18439 finish_member_declaration (decl);
18440 }
18441 else
18442 {
18443 decl = cp_parser_lookup_name_simple (parser,
18444 identifier,
18445 token->location);
18446 if (decl == error_mark_node)
18447 cp_parser_name_lookup_error (parser, identifier,
18448 decl, NLE_NULL,
18449 token->location);
18450 else if (check_for_bare_parameter_packs (decl))
18451 {
18452 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18453 return false;
18454 }
18455 else if (!at_namespace_scope_p ())
18456 do_local_using_decl (decl, qscope, identifier);
18457 else
18458 do_toplevel_using_decl (decl, qscope, identifier);
18459 }
18460 }
18461
18462 /* Look for the final `;'. */
18463 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18464
18465 if (access_declaration_p && errorcount == oldcount)
18466 warning_at (diag_token->location, OPT_Wdeprecated,
18467 "access declarations are deprecated "
18468 "in favour of using-declarations; "
18469 "suggestion: add the %<using%> keyword");
18470
18471 return true;
18472 }
18473
18474 /* Parse an alias-declaration.
18475
18476 alias-declaration:
18477 using identifier attribute-specifier-seq [opt] = type-id */
18478
18479 static tree
18480 cp_parser_alias_declaration (cp_parser* parser)
18481 {
18482 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18483 location_t id_location;
18484 cp_declarator *declarator;
18485 cp_decl_specifier_seq decl_specs;
18486 bool member_p;
18487 const char *saved_message = NULL;
18488
18489 /* Look for the `using' keyword. */
18490 cp_token *using_token
18491 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18492 if (using_token == NULL)
18493 return error_mark_node;
18494
18495 id_location = cp_lexer_peek_token (parser->lexer)->location;
18496 id = cp_parser_identifier (parser);
18497 if (id == error_mark_node)
18498 return error_mark_node;
18499
18500 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18501 attributes = cp_parser_attributes_opt (parser);
18502 if (attributes == error_mark_node)
18503 return error_mark_node;
18504
18505 cp_parser_require (parser, CPP_EQ, RT_EQ);
18506
18507 if (cp_parser_error_occurred (parser))
18508 return error_mark_node;
18509
18510 cp_parser_commit_to_tentative_parse (parser);
18511
18512 /* Now we are going to parse the type-id of the declaration. */
18513
18514 /*
18515 [dcl.type]/3 says:
18516
18517 "A type-specifier-seq shall not define a class or enumeration
18518 unless it appears in the type-id of an alias-declaration (7.1.3) that
18519 is not the declaration of a template-declaration."
18520
18521 In other words, if we currently are in an alias template, the
18522 type-id should not define a type.
18523
18524 So let's set parser->type_definition_forbidden_message in that
18525 case; cp_parser_check_type_definition (called by
18526 cp_parser_class_specifier) will then emit an error if a type is
18527 defined in the type-id. */
18528 if (parser->num_template_parameter_lists)
18529 {
18530 saved_message = parser->type_definition_forbidden_message;
18531 parser->type_definition_forbidden_message =
18532 G_("types may not be defined in alias template declarations");
18533 }
18534
18535 type = cp_parser_type_id (parser);
18536
18537 /* Restore the error message if need be. */
18538 if (parser->num_template_parameter_lists)
18539 parser->type_definition_forbidden_message = saved_message;
18540
18541 if (type == error_mark_node
18542 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18543 {
18544 cp_parser_skip_to_end_of_block_or_statement (parser);
18545 return error_mark_node;
18546 }
18547
18548 /* A typedef-name can also be introduced by an alias-declaration. The
18549 identifier following the using keyword becomes a typedef-name. It has
18550 the same semantics as if it were introduced by the typedef
18551 specifier. In particular, it does not define a new type and it shall
18552 not appear in the type-id. */
18553
18554 clear_decl_specs (&decl_specs);
18555 decl_specs.type = type;
18556 if (attributes != NULL_TREE)
18557 {
18558 decl_specs.attributes = attributes;
18559 set_and_check_decl_spec_loc (&decl_specs,
18560 ds_attribute,
18561 attrs_token);
18562 }
18563 set_and_check_decl_spec_loc (&decl_specs,
18564 ds_typedef,
18565 using_token);
18566 set_and_check_decl_spec_loc (&decl_specs,
18567 ds_alias,
18568 using_token);
18569
18570 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18571 declarator->id_loc = id_location;
18572
18573 member_p = at_class_scope_p ();
18574 if (member_p)
18575 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18576 NULL_TREE, attributes);
18577 else
18578 decl = start_decl (declarator, &decl_specs, 0,
18579 attributes, NULL_TREE, &pushed_scope);
18580 if (decl == error_mark_node)
18581 return decl;
18582
18583 // Attach constraints to the alias declaration.
18584 if (flag_concepts && current_template_parms)
18585 {
18586 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18587 tree constr = build_constraints (reqs, NULL_TREE);
18588 set_constraints (decl, constr);
18589 }
18590
18591 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18592
18593 if (pushed_scope)
18594 pop_scope (pushed_scope);
18595
18596 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18597 added into the symbol table; otherwise, return the TYPE_DECL. */
18598 if (DECL_LANG_SPECIFIC (decl)
18599 && DECL_TEMPLATE_INFO (decl)
18600 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18601 {
18602 decl = DECL_TI_TEMPLATE (decl);
18603 if (member_p)
18604 check_member_template (decl);
18605 }
18606
18607 return decl;
18608 }
18609
18610 /* Parse a using-directive.
18611
18612 using-directive:
18613 using namespace :: [opt] nested-name-specifier [opt]
18614 namespace-name ; */
18615
18616 static void
18617 cp_parser_using_directive (cp_parser* parser)
18618 {
18619 tree namespace_decl;
18620 tree attribs;
18621
18622 /* Look for the `using' keyword. */
18623 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18624 /* And the `namespace' keyword. */
18625 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18626 /* Look for the optional `::' operator. */
18627 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18628 /* And the optional nested-name-specifier. */
18629 cp_parser_nested_name_specifier_opt (parser,
18630 /*typename_keyword_p=*/false,
18631 /*check_dependency_p=*/true,
18632 /*type_p=*/false,
18633 /*is_declaration=*/true);
18634 /* Get the namespace being used. */
18635 namespace_decl = cp_parser_namespace_name (parser);
18636 /* And any specified attributes. */
18637 attribs = cp_parser_attributes_opt (parser);
18638 /* Update the symbol table. */
18639 parse_using_directive (namespace_decl, attribs);
18640 /* Look for the final `;'. */
18641 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18642 }
18643
18644 /* Parse an asm-definition.
18645
18646 asm-definition:
18647 asm ( string-literal ) ;
18648
18649 GNU Extension:
18650
18651 asm-definition:
18652 asm volatile [opt] ( string-literal ) ;
18653 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18654 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18655 : asm-operand-list [opt] ) ;
18656 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18657 : asm-operand-list [opt]
18658 : asm-clobber-list [opt] ) ;
18659 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18660 : asm-clobber-list [opt]
18661 : asm-goto-list ) ; */
18662
18663 static void
18664 cp_parser_asm_definition (cp_parser* parser)
18665 {
18666 tree string;
18667 tree outputs = NULL_TREE;
18668 tree inputs = NULL_TREE;
18669 tree clobbers = NULL_TREE;
18670 tree labels = NULL_TREE;
18671 tree asm_stmt;
18672 bool volatile_p = false;
18673 bool extended_p = false;
18674 bool invalid_inputs_p = false;
18675 bool invalid_outputs_p = false;
18676 bool goto_p = false;
18677 required_token missing = RT_NONE;
18678
18679 /* Look for the `asm' keyword. */
18680 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18681
18682 if (parser->in_function_body
18683 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18684 {
18685 error ("%<asm%> in %<constexpr%> function");
18686 cp_function_chain->invalid_constexpr = true;
18687 }
18688
18689 /* See if the next token is `volatile'. */
18690 if (cp_parser_allow_gnu_extensions_p (parser)
18691 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18692 {
18693 /* Remember that we saw the `volatile' keyword. */
18694 volatile_p = true;
18695 /* Consume the token. */
18696 cp_lexer_consume_token (parser->lexer);
18697 }
18698 if (cp_parser_allow_gnu_extensions_p (parser)
18699 && parser->in_function_body
18700 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18701 {
18702 /* Remember that we saw the `goto' keyword. */
18703 goto_p = true;
18704 /* Consume the token. */
18705 cp_lexer_consume_token (parser->lexer);
18706 }
18707 /* Look for the opening `('. */
18708 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18709 return;
18710 /* Look for the string. */
18711 string = cp_parser_string_literal (parser, false, false);
18712 if (string == error_mark_node)
18713 {
18714 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18715 /*consume_paren=*/true);
18716 return;
18717 }
18718
18719 /* If we're allowing GNU extensions, check for the extended assembly
18720 syntax. Unfortunately, the `:' tokens need not be separated by
18721 a space in C, and so, for compatibility, we tolerate that here
18722 too. Doing that means that we have to treat the `::' operator as
18723 two `:' tokens. */
18724 if (cp_parser_allow_gnu_extensions_p (parser)
18725 && parser->in_function_body
18726 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18727 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18728 {
18729 bool inputs_p = false;
18730 bool clobbers_p = false;
18731 bool labels_p = false;
18732
18733 /* The extended syntax was used. */
18734 extended_p = true;
18735
18736 /* Look for outputs. */
18737 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18738 {
18739 /* Consume the `:'. */
18740 cp_lexer_consume_token (parser->lexer);
18741 /* Parse the output-operands. */
18742 if (cp_lexer_next_token_is_not (parser->lexer,
18743 CPP_COLON)
18744 && cp_lexer_next_token_is_not (parser->lexer,
18745 CPP_SCOPE)
18746 && cp_lexer_next_token_is_not (parser->lexer,
18747 CPP_CLOSE_PAREN)
18748 && !goto_p)
18749 {
18750 outputs = cp_parser_asm_operand_list (parser);
18751 if (outputs == error_mark_node)
18752 invalid_outputs_p = true;
18753 }
18754 }
18755 /* If the next token is `::', there are no outputs, and the
18756 next token is the beginning of the inputs. */
18757 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18758 /* The inputs are coming next. */
18759 inputs_p = true;
18760
18761 /* Look for inputs. */
18762 if (inputs_p
18763 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18764 {
18765 /* Consume the `:' or `::'. */
18766 cp_lexer_consume_token (parser->lexer);
18767 /* Parse the output-operands. */
18768 if (cp_lexer_next_token_is_not (parser->lexer,
18769 CPP_COLON)
18770 && cp_lexer_next_token_is_not (parser->lexer,
18771 CPP_SCOPE)
18772 && cp_lexer_next_token_is_not (parser->lexer,
18773 CPP_CLOSE_PAREN))
18774 {
18775 inputs = cp_parser_asm_operand_list (parser);
18776 if (inputs == error_mark_node)
18777 invalid_inputs_p = true;
18778 }
18779 }
18780 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18781 /* The clobbers are coming next. */
18782 clobbers_p = true;
18783
18784 /* Look for clobbers. */
18785 if (clobbers_p
18786 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18787 {
18788 clobbers_p = true;
18789 /* Consume the `:' or `::'. */
18790 cp_lexer_consume_token (parser->lexer);
18791 /* Parse the clobbers. */
18792 if (cp_lexer_next_token_is_not (parser->lexer,
18793 CPP_COLON)
18794 && cp_lexer_next_token_is_not (parser->lexer,
18795 CPP_CLOSE_PAREN))
18796 clobbers = cp_parser_asm_clobber_list (parser);
18797 }
18798 else if (goto_p
18799 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18800 /* The labels are coming next. */
18801 labels_p = true;
18802
18803 /* Look for labels. */
18804 if (labels_p
18805 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18806 {
18807 labels_p = true;
18808 /* Consume the `:' or `::'. */
18809 cp_lexer_consume_token (parser->lexer);
18810 /* Parse the labels. */
18811 labels = cp_parser_asm_label_list (parser);
18812 }
18813
18814 if (goto_p && !labels_p)
18815 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18816 }
18817 else if (goto_p)
18818 missing = RT_COLON_SCOPE;
18819
18820 /* Look for the closing `)'. */
18821 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18822 missing ? missing : RT_CLOSE_PAREN))
18823 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18824 /*consume_paren=*/true);
18825 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18826
18827 if (!invalid_inputs_p && !invalid_outputs_p)
18828 {
18829 /* Create the ASM_EXPR. */
18830 if (parser->in_function_body)
18831 {
18832 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18833 inputs, clobbers, labels);
18834 /* If the extended syntax was not used, mark the ASM_EXPR. */
18835 if (!extended_p)
18836 {
18837 tree temp = asm_stmt;
18838 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18839 temp = TREE_OPERAND (temp, 0);
18840
18841 ASM_INPUT_P (temp) = 1;
18842 }
18843 }
18844 else
18845 symtab->finalize_toplevel_asm (string);
18846 }
18847 }
18848
18849 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18850 type that comes from the decl-specifier-seq. */
18851
18852 static tree
18853 strip_declarator_types (tree type, cp_declarator *declarator)
18854 {
18855 for (cp_declarator *d = declarator; d;)
18856 switch (d->kind)
18857 {
18858 case cdk_id:
18859 case cdk_decomp:
18860 case cdk_error:
18861 d = NULL;
18862 break;
18863
18864 default:
18865 if (TYPE_PTRMEMFUNC_P (type))
18866 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18867 type = TREE_TYPE (type);
18868 d = d->declarator;
18869 break;
18870 }
18871
18872 return type;
18873 }
18874
18875 /* Declarators [gram.dcl.decl] */
18876
18877 /* Parse an init-declarator.
18878
18879 init-declarator:
18880 declarator initializer [opt]
18881
18882 GNU Extension:
18883
18884 init-declarator:
18885 declarator asm-specification [opt] attributes [opt] initializer [opt]
18886
18887 function-definition:
18888 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18889 function-body
18890 decl-specifier-seq [opt] declarator function-try-block
18891
18892 GNU Extension:
18893
18894 function-definition:
18895 __extension__ function-definition
18896
18897 TM Extension:
18898
18899 function-definition:
18900 decl-specifier-seq [opt] declarator function-transaction-block
18901
18902 The DECL_SPECIFIERS apply to this declarator. Returns a
18903 representation of the entity declared. If MEMBER_P is TRUE, then
18904 this declarator appears in a class scope. The new DECL created by
18905 this declarator is returned.
18906
18907 The CHECKS are access checks that should be performed once we know
18908 what entity is being declared (and, therefore, what classes have
18909 befriended it).
18910
18911 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18912 for a function-definition here as well. If the declarator is a
18913 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18914 be TRUE upon return. By that point, the function-definition will
18915 have been completely parsed.
18916
18917 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18918 is FALSE.
18919
18920 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18921 parsed declaration if it is an uninitialized single declarator not followed
18922 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18923 if present, will not be consumed. If returned, this declarator will be
18924 created with SD_INITIALIZED but will not call cp_finish_decl.
18925
18926 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18927 and there is an initializer, the pointed location_t is set to the
18928 location of the '=' or `(', or '{' in C++11 token introducing the
18929 initializer. */
18930
18931 static tree
18932 cp_parser_init_declarator (cp_parser* parser,
18933 cp_decl_specifier_seq *decl_specifiers,
18934 vec<deferred_access_check, va_gc> *checks,
18935 bool function_definition_allowed_p,
18936 bool member_p,
18937 int declares_class_or_enum,
18938 bool* function_definition_p,
18939 tree* maybe_range_for_decl,
18940 location_t* init_loc,
18941 tree* auto_result)
18942 {
18943 cp_token *token = NULL, *asm_spec_start_token = NULL,
18944 *attributes_start_token = NULL;
18945 cp_declarator *declarator;
18946 tree prefix_attributes;
18947 tree attributes = NULL;
18948 tree asm_specification;
18949 tree initializer;
18950 tree decl = NULL_TREE;
18951 tree scope;
18952 int is_initialized;
18953 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18954 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18955 "(...)". */
18956 enum cpp_ttype initialization_kind;
18957 bool is_direct_init = false;
18958 bool is_non_constant_init;
18959 int ctor_dtor_or_conv_p;
18960 bool friend_p = cp_parser_friend_p (decl_specifiers);
18961 tree pushed_scope = NULL_TREE;
18962 bool range_for_decl_p = false;
18963 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18964 location_t tmp_init_loc = UNKNOWN_LOCATION;
18965
18966 /* Gather the attributes that were provided with the
18967 decl-specifiers. */
18968 prefix_attributes = decl_specifiers->attributes;
18969
18970 /* Assume that this is not the declarator for a function
18971 definition. */
18972 if (function_definition_p)
18973 *function_definition_p = false;
18974
18975 /* Default arguments are only permitted for function parameters. */
18976 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18977 parser->default_arg_ok_p = false;
18978
18979 /* Defer access checks while parsing the declarator; we cannot know
18980 what names are accessible until we know what is being
18981 declared. */
18982 resume_deferring_access_checks ();
18983
18984 token = cp_lexer_peek_token (parser->lexer);
18985
18986 cp_parser_declarator_kind cdk = CP_PARSER_DECLARATOR_NAMED;
18987 if (token->type == CPP_OPEN_PAREN
18988 && decl_specifiers->type
18989 && is_auto (decl_specifiers->type)
18990 && CLASS_PLACEHOLDER_TEMPLATE (decl_specifiers->type))
18991 {
18992 // C++17 deduction guide.
18993 cdk = CP_PARSER_DECLARATOR_ABSTRACT;
18994
18995 for (int i = 0; i < ds_last; ++i)
18996 if (i != ds_type_spec
18997 && decl_specifiers->locations[i]
18998 && !cp_parser_simulate_error (parser))
18999 error_at (decl_specifiers->locations[i],
19000 "decl-specifier in declaration of deduction guide");
19001 }
19002
19003 /* Parse the declarator. */
19004 declarator
19005 = cp_parser_declarator (parser, cdk,
19006 &ctor_dtor_or_conv_p,
19007 /*parenthesized_p=*/NULL,
19008 member_p, friend_p);
19009 /* Gather up the deferred checks. */
19010 stop_deferring_access_checks ();
19011
19012 parser->default_arg_ok_p = saved_default_arg_ok_p;
19013
19014 /* If the DECLARATOR was erroneous, there's no need to go
19015 further. */
19016 if (declarator == cp_error_declarator)
19017 return error_mark_node;
19018
19019 if (cdk == CP_PARSER_DECLARATOR_ABSTRACT)
19020 {
19021 gcc_assert (declarator->kind == cdk_function
19022 && !declarator->declarator);
19023 tree t = CLASS_PLACEHOLDER_TEMPLATE (decl_specifiers->type);
19024 declarator->declarator = make_id_declarator (NULL_TREE, dguide_name (t),
19025 sfk_none);
19026 declarator->declarator->id_loc
19027 = decl_specifiers->locations[ds_type_spec];
19028 }
19029
19030 /* Check that the number of template-parameter-lists is OK. */
19031 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19032 token->location))
19033 return error_mark_node;
19034
19035 if (declares_class_or_enum & 2)
19036 cp_parser_check_for_definition_in_return_type (declarator,
19037 decl_specifiers->type,
19038 decl_specifiers->locations[ds_type_spec]);
19039
19040 /* Figure out what scope the entity declared by the DECLARATOR is
19041 located in. `grokdeclarator' sometimes changes the scope, so
19042 we compute it now. */
19043 scope = get_scope_of_declarator (declarator);
19044
19045 /* Perform any lookups in the declared type which were thought to be
19046 dependent, but are not in the scope of the declarator. */
19047 decl_specifiers->type
19048 = maybe_update_decl_type (decl_specifiers->type, scope);
19049
19050 /* If we're allowing GNU extensions, look for an
19051 asm-specification. */
19052 if (cp_parser_allow_gnu_extensions_p (parser))
19053 {
19054 /* Look for an asm-specification. */
19055 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19056 asm_specification = cp_parser_asm_specification_opt (parser);
19057 }
19058 else
19059 asm_specification = NULL_TREE;
19060
19061 /* Look for attributes. */
19062 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19063 attributes = cp_parser_attributes_opt (parser);
19064
19065 /* Peek at the next token. */
19066 token = cp_lexer_peek_token (parser->lexer);
19067
19068 bool bogus_implicit_tmpl = false;
19069
19070 if (function_declarator_p (declarator))
19071 {
19072 /* Check to see if the token indicates the start of a
19073 function-definition. */
19074 if (cp_parser_token_starts_function_definition_p (token))
19075 {
19076 if (!function_definition_allowed_p)
19077 {
19078 /* If a function-definition should not appear here, issue an
19079 error message. */
19080 cp_parser_error (parser,
19081 "a function-definition is not allowed here");
19082 return error_mark_node;
19083 }
19084
19085 location_t func_brace_location
19086 = cp_lexer_peek_token (parser->lexer)->location;
19087
19088 /* Neither attributes nor an asm-specification are allowed
19089 on a function-definition. */
19090 if (asm_specification)
19091 error_at (asm_spec_start_token->location,
19092 "an asm-specification is not allowed "
19093 "on a function-definition");
19094 if (attributes)
19095 error_at (attributes_start_token->location,
19096 "attributes are not allowed "
19097 "on a function-definition");
19098 /* This is a function-definition. */
19099 *function_definition_p = true;
19100
19101 /* Parse the function definition. */
19102 if (member_p)
19103 decl = cp_parser_save_member_function_body (parser,
19104 decl_specifiers,
19105 declarator,
19106 prefix_attributes);
19107 else
19108 decl =
19109 (cp_parser_function_definition_from_specifiers_and_declarator
19110 (parser, decl_specifiers, prefix_attributes, declarator));
19111
19112 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19113 {
19114 /* This is where the prologue starts... */
19115 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19116 = func_brace_location;
19117 }
19118
19119 return decl;
19120 }
19121 }
19122 else if (parser->fully_implicit_function_template_p)
19123 {
19124 /* A non-template declaration involving a function parameter list
19125 containing an implicit template parameter will be made into a
19126 template. If the resulting declaration is not going to be an
19127 actual function then finish the template scope here to prevent it.
19128 An error message will be issued once we have a decl to talk about.
19129
19130 FIXME probably we should do type deduction rather than create an
19131 implicit template, but the standard currently doesn't allow it. */
19132 bogus_implicit_tmpl = true;
19133 finish_fully_implicit_template (parser, NULL_TREE);
19134 }
19135
19136 /* [dcl.dcl]
19137
19138 Only in function declarations for constructors, destructors, and
19139 type conversions can the decl-specifier-seq be omitted.
19140
19141 We explicitly postpone this check past the point where we handle
19142 function-definitions because we tolerate function-definitions
19143 that are missing their return types in some modes. */
19144 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19145 {
19146 cp_parser_error (parser,
19147 "expected constructor, destructor, or type conversion");
19148 return error_mark_node;
19149 }
19150
19151 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19152 if (token->type == CPP_EQ
19153 || token->type == CPP_OPEN_PAREN
19154 || token->type == CPP_OPEN_BRACE)
19155 {
19156 is_initialized = SD_INITIALIZED;
19157 initialization_kind = token->type;
19158 if (maybe_range_for_decl)
19159 *maybe_range_for_decl = error_mark_node;
19160 tmp_init_loc = token->location;
19161 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19162 *init_loc = tmp_init_loc;
19163
19164 if (token->type == CPP_EQ
19165 && function_declarator_p (declarator))
19166 {
19167 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19168 if (t2->keyword == RID_DEFAULT)
19169 is_initialized = SD_DEFAULTED;
19170 else if (t2->keyword == RID_DELETE)
19171 is_initialized = SD_DELETED;
19172 }
19173 }
19174 else
19175 {
19176 /* If the init-declarator isn't initialized and isn't followed by a
19177 `,' or `;', it's not a valid init-declarator. */
19178 if (token->type != CPP_COMMA
19179 && token->type != CPP_SEMICOLON)
19180 {
19181 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19182 range_for_decl_p = true;
19183 else
19184 {
19185 if (!maybe_range_for_decl)
19186 cp_parser_error (parser, "expected initializer");
19187 return error_mark_node;
19188 }
19189 }
19190 is_initialized = SD_UNINITIALIZED;
19191 initialization_kind = CPP_EOF;
19192 }
19193
19194 /* Because start_decl has side-effects, we should only call it if we
19195 know we're going ahead. By this point, we know that we cannot
19196 possibly be looking at any other construct. */
19197 cp_parser_commit_to_tentative_parse (parser);
19198
19199 /* Enter the newly declared entry in the symbol table. If we're
19200 processing a declaration in a class-specifier, we wait until
19201 after processing the initializer. */
19202 if (!member_p)
19203 {
19204 if (parser->in_unbraced_linkage_specification_p)
19205 decl_specifiers->storage_class = sc_extern;
19206 decl = start_decl (declarator, decl_specifiers,
19207 range_for_decl_p? SD_INITIALIZED : is_initialized,
19208 attributes, prefix_attributes, &pushed_scope);
19209 cp_finalize_omp_declare_simd (parser, decl);
19210 cp_finalize_oacc_routine (parser, decl, false);
19211 /* Adjust location of decl if declarator->id_loc is more appropriate:
19212 set, and decl wasn't merged with another decl, in which case its
19213 location would be different from input_location, and more accurate. */
19214 if (DECL_P (decl)
19215 && declarator->id_loc != UNKNOWN_LOCATION
19216 && DECL_SOURCE_LOCATION (decl) == input_location)
19217 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19218 }
19219 else if (scope)
19220 /* Enter the SCOPE. That way unqualified names appearing in the
19221 initializer will be looked up in SCOPE. */
19222 pushed_scope = push_scope (scope);
19223
19224 /* Perform deferred access control checks, now that we know in which
19225 SCOPE the declared entity resides. */
19226 if (!member_p && decl)
19227 {
19228 tree saved_current_function_decl = NULL_TREE;
19229
19230 /* If the entity being declared is a function, pretend that we
19231 are in its scope. If it is a `friend', it may have access to
19232 things that would not otherwise be accessible. */
19233 if (TREE_CODE (decl) == FUNCTION_DECL)
19234 {
19235 saved_current_function_decl = current_function_decl;
19236 current_function_decl = decl;
19237 }
19238
19239 /* Perform access checks for template parameters. */
19240 cp_parser_perform_template_parameter_access_checks (checks);
19241
19242 /* Perform the access control checks for the declarator and the
19243 decl-specifiers. */
19244 perform_deferred_access_checks (tf_warning_or_error);
19245
19246 /* Restore the saved value. */
19247 if (TREE_CODE (decl) == FUNCTION_DECL)
19248 current_function_decl = saved_current_function_decl;
19249 }
19250
19251 /* Parse the initializer. */
19252 initializer = NULL_TREE;
19253 is_direct_init = false;
19254 is_non_constant_init = true;
19255 if (is_initialized)
19256 {
19257 if (function_declarator_p (declarator))
19258 {
19259 if (initialization_kind == CPP_EQ)
19260 initializer = cp_parser_pure_specifier (parser);
19261 else
19262 {
19263 /* If the declaration was erroneous, we don't really
19264 know what the user intended, so just silently
19265 consume the initializer. */
19266 if (decl != error_mark_node)
19267 error_at (tmp_init_loc, "initializer provided for function");
19268 cp_parser_skip_to_closing_parenthesis (parser,
19269 /*recovering=*/true,
19270 /*or_comma=*/false,
19271 /*consume_paren=*/true);
19272 }
19273 }
19274 else
19275 {
19276 /* We want to record the extra mangling scope for in-class
19277 initializers of class members and initializers of static data
19278 member templates. The former involves deferring
19279 parsing of the initializer until end of class as with default
19280 arguments. So right here we only handle the latter. */
19281 if (!member_p && processing_template_decl)
19282 start_lambda_scope (decl);
19283 initializer = cp_parser_initializer (parser,
19284 &is_direct_init,
19285 &is_non_constant_init);
19286 if (!member_p && processing_template_decl)
19287 finish_lambda_scope ();
19288 if (initializer == error_mark_node)
19289 cp_parser_skip_to_end_of_statement (parser);
19290 }
19291 }
19292
19293 /* The old parser allows attributes to appear after a parenthesized
19294 initializer. Mark Mitchell proposed removing this functionality
19295 on the GCC mailing lists on 2002-08-13. This parser accepts the
19296 attributes -- but ignores them. */
19297 if (cp_parser_allow_gnu_extensions_p (parser)
19298 && initialization_kind == CPP_OPEN_PAREN)
19299 if (cp_parser_attributes_opt (parser))
19300 warning (OPT_Wattributes,
19301 "attributes after parenthesized initializer ignored");
19302
19303 /* And now complain about a non-function implicit template. */
19304 if (bogus_implicit_tmpl && decl != error_mark_node)
19305 error_at (DECL_SOURCE_LOCATION (decl),
19306 "non-function %qD declared as implicit template", decl);
19307
19308 /* For an in-class declaration, use `grokfield' to create the
19309 declaration. */
19310 if (member_p)
19311 {
19312 if (pushed_scope)
19313 {
19314 pop_scope (pushed_scope);
19315 pushed_scope = NULL_TREE;
19316 }
19317 decl = grokfield (declarator, decl_specifiers,
19318 initializer, !is_non_constant_init,
19319 /*asmspec=*/NULL_TREE,
19320 chainon (attributes, prefix_attributes));
19321 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19322 cp_parser_save_default_args (parser, decl);
19323 cp_finalize_omp_declare_simd (parser, decl);
19324 cp_finalize_oacc_routine (parser, decl, false);
19325 }
19326
19327 /* Finish processing the declaration. But, skip member
19328 declarations. */
19329 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19330 {
19331 cp_finish_decl (decl,
19332 initializer, !is_non_constant_init,
19333 asm_specification,
19334 /* If the initializer is in parentheses, then this is
19335 a direct-initialization, which means that an
19336 `explicit' constructor is OK. Otherwise, an
19337 `explicit' constructor cannot be used. */
19338 ((is_direct_init || !is_initialized)
19339 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19340 }
19341 else if ((cxx_dialect != cxx98) && friend_p
19342 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19343 /* Core issue #226 (C++0x only): A default template-argument
19344 shall not be specified in a friend class template
19345 declaration. */
19346 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19347 /*is_partial=*/false, /*is_friend_decl=*/1);
19348
19349 if (!friend_p && pushed_scope)
19350 pop_scope (pushed_scope);
19351
19352 if (function_declarator_p (declarator)
19353 && parser->fully_implicit_function_template_p)
19354 {
19355 if (member_p)
19356 decl = finish_fully_implicit_template (parser, decl);
19357 else
19358 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19359 }
19360
19361 if (auto_result && is_initialized && decl_specifiers->type
19362 && type_uses_auto (decl_specifiers->type))
19363 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19364
19365 return decl;
19366 }
19367
19368 /* Parse a declarator.
19369
19370 declarator:
19371 direct-declarator
19372 ptr-operator declarator
19373
19374 abstract-declarator:
19375 ptr-operator abstract-declarator [opt]
19376 direct-abstract-declarator
19377
19378 GNU Extensions:
19379
19380 declarator:
19381 attributes [opt] direct-declarator
19382 attributes [opt] ptr-operator declarator
19383
19384 abstract-declarator:
19385 attributes [opt] ptr-operator abstract-declarator [opt]
19386 attributes [opt] direct-abstract-declarator
19387
19388 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19389 detect constructor, destructor or conversion operators. It is set
19390 to -1 if the declarator is a name, and +1 if it is a
19391 function. Otherwise it is set to zero. Usually you just want to
19392 test for >0, but internally the negative value is used.
19393
19394 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19395 a decl-specifier-seq unless it declares a constructor, destructor,
19396 or conversion. It might seem that we could check this condition in
19397 semantic analysis, rather than parsing, but that makes it difficult
19398 to handle something like `f()'. We want to notice that there are
19399 no decl-specifiers, and therefore realize that this is an
19400 expression, not a declaration.)
19401
19402 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19403 the declarator is a direct-declarator of the form "(...)".
19404
19405 MEMBER_P is true iff this declarator is a member-declarator.
19406
19407 FRIEND_P is true iff this declarator is a friend. */
19408
19409 static cp_declarator *
19410 cp_parser_declarator (cp_parser* parser,
19411 cp_parser_declarator_kind dcl_kind,
19412 int* ctor_dtor_or_conv_p,
19413 bool* parenthesized_p,
19414 bool member_p, bool friend_p)
19415 {
19416 cp_declarator *declarator;
19417 enum tree_code code;
19418 cp_cv_quals cv_quals;
19419 tree class_type;
19420 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19421
19422 /* Assume this is not a constructor, destructor, or type-conversion
19423 operator. */
19424 if (ctor_dtor_or_conv_p)
19425 *ctor_dtor_or_conv_p = 0;
19426
19427 if (cp_parser_allow_gnu_extensions_p (parser))
19428 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19429
19430 /* Check for the ptr-operator production. */
19431 cp_parser_parse_tentatively (parser);
19432 /* Parse the ptr-operator. */
19433 code = cp_parser_ptr_operator (parser,
19434 &class_type,
19435 &cv_quals,
19436 &std_attributes);
19437
19438 /* If that worked, then we have a ptr-operator. */
19439 if (cp_parser_parse_definitely (parser))
19440 {
19441 /* If a ptr-operator was found, then this declarator was not
19442 parenthesized. */
19443 if (parenthesized_p)
19444 *parenthesized_p = true;
19445 /* The dependent declarator is optional if we are parsing an
19446 abstract-declarator. */
19447 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19448 cp_parser_parse_tentatively (parser);
19449
19450 /* Parse the dependent declarator. */
19451 declarator = cp_parser_declarator (parser, dcl_kind,
19452 /*ctor_dtor_or_conv_p=*/NULL,
19453 /*parenthesized_p=*/NULL,
19454 /*member_p=*/false,
19455 friend_p);
19456
19457 /* If we are parsing an abstract-declarator, we must handle the
19458 case where the dependent declarator is absent. */
19459 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19460 && !cp_parser_parse_definitely (parser))
19461 declarator = NULL;
19462
19463 declarator = cp_parser_make_indirect_declarator
19464 (code, class_type, cv_quals, declarator, std_attributes);
19465 }
19466 /* Everything else is a direct-declarator. */
19467 else
19468 {
19469 if (parenthesized_p)
19470 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19471 CPP_OPEN_PAREN);
19472 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19473 ctor_dtor_or_conv_p,
19474 member_p, friend_p);
19475 }
19476
19477 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19478 declarator->attributes = gnu_attributes;
19479 return declarator;
19480 }
19481
19482 /* Parse a direct-declarator or direct-abstract-declarator.
19483
19484 direct-declarator:
19485 declarator-id
19486 direct-declarator ( parameter-declaration-clause )
19487 cv-qualifier-seq [opt]
19488 ref-qualifier [opt]
19489 exception-specification [opt]
19490 direct-declarator [ constant-expression [opt] ]
19491 ( declarator )
19492
19493 direct-abstract-declarator:
19494 direct-abstract-declarator [opt]
19495 ( parameter-declaration-clause )
19496 cv-qualifier-seq [opt]
19497 ref-qualifier [opt]
19498 exception-specification [opt]
19499 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19500 ( abstract-declarator )
19501
19502 Returns a representation of the declarator. DCL_KIND is
19503 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19504 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19505 we are parsing a direct-declarator. It is
19506 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19507 of ambiguity we prefer an abstract declarator, as per
19508 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19509 as for cp_parser_declarator. */
19510
19511 static cp_declarator *
19512 cp_parser_direct_declarator (cp_parser* parser,
19513 cp_parser_declarator_kind dcl_kind,
19514 int* ctor_dtor_or_conv_p,
19515 bool member_p, bool friend_p)
19516 {
19517 cp_token *token;
19518 cp_declarator *declarator = NULL;
19519 tree scope = NULL_TREE;
19520 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19521 bool saved_in_declarator_p = parser->in_declarator_p;
19522 bool first = true;
19523 tree pushed_scope = NULL_TREE;
19524
19525 while (true)
19526 {
19527 /* Peek at the next token. */
19528 token = cp_lexer_peek_token (parser->lexer);
19529 if (token->type == CPP_OPEN_PAREN)
19530 {
19531 /* This is either a parameter-declaration-clause, or a
19532 parenthesized declarator. When we know we are parsing a
19533 named declarator, it must be a parenthesized declarator
19534 if FIRST is true. For instance, `(int)' is a
19535 parameter-declaration-clause, with an omitted
19536 direct-abstract-declarator. But `((*))', is a
19537 parenthesized abstract declarator. Finally, when T is a
19538 template parameter `(T)' is a
19539 parameter-declaration-clause, and not a parenthesized
19540 named declarator.
19541
19542 We first try and parse a parameter-declaration-clause,
19543 and then try a nested declarator (if FIRST is true).
19544
19545 It is not an error for it not to be a
19546 parameter-declaration-clause, even when FIRST is
19547 false. Consider,
19548
19549 int i (int);
19550 int i (3);
19551
19552 The first is the declaration of a function while the
19553 second is the definition of a variable, including its
19554 initializer.
19555
19556 Having seen only the parenthesis, we cannot know which of
19557 these two alternatives should be selected. Even more
19558 complex are examples like:
19559
19560 int i (int (a));
19561 int i (int (3));
19562
19563 The former is a function-declaration; the latter is a
19564 variable initialization.
19565
19566 Thus again, we try a parameter-declaration-clause, and if
19567 that fails, we back out and return. */
19568
19569 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19570 {
19571 tree params;
19572 bool is_declarator = false;
19573
19574 /* In a member-declarator, the only valid interpretation
19575 of a parenthesis is the start of a
19576 parameter-declaration-clause. (It is invalid to
19577 initialize a static data member with a parenthesized
19578 initializer; only the "=" form of initialization is
19579 permitted.) */
19580 if (!member_p)
19581 cp_parser_parse_tentatively (parser);
19582
19583 /* Consume the `('. */
19584 cp_lexer_consume_token (parser->lexer);
19585 if (first)
19586 {
19587 /* If this is going to be an abstract declarator, we're
19588 in a declarator and we can't have default args. */
19589 parser->default_arg_ok_p = false;
19590 parser->in_declarator_p = true;
19591 }
19592
19593 begin_scope (sk_function_parms, NULL_TREE);
19594
19595 /* Parse the parameter-declaration-clause. */
19596 params = cp_parser_parameter_declaration_clause (parser);
19597
19598 /* Consume the `)'. */
19599 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19600
19601 /* If all went well, parse the cv-qualifier-seq,
19602 ref-qualifier and the exception-specification. */
19603 if (member_p || cp_parser_parse_definitely (parser))
19604 {
19605 cp_cv_quals cv_quals;
19606 cp_virt_specifiers virt_specifiers;
19607 cp_ref_qualifier ref_qual;
19608 tree exception_specification;
19609 tree late_return;
19610 tree attrs;
19611 bool memfn = (member_p || (pushed_scope
19612 && CLASS_TYPE_P (pushed_scope)));
19613
19614 is_declarator = true;
19615
19616 if (ctor_dtor_or_conv_p)
19617 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19618 first = false;
19619
19620 /* Parse the cv-qualifier-seq. */
19621 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19622 /* Parse the ref-qualifier. */
19623 ref_qual = cp_parser_ref_qualifier_opt (parser);
19624 /* Parse the tx-qualifier. */
19625 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19626 /* And the exception-specification. */
19627 exception_specification
19628 = cp_parser_exception_specification_opt (parser);
19629
19630 attrs = cp_parser_std_attribute_spec_seq (parser);
19631
19632 /* In here, we handle cases where attribute is used after
19633 the function declaration. For example:
19634 void func (int x) __attribute__((vector(..))); */
19635 tree gnu_attrs = NULL_TREE;
19636 if (flag_cilkplus
19637 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19638 {
19639 cp_parser_parse_tentatively (parser);
19640 tree attr = cp_parser_gnu_attributes_opt (parser);
19641 if (cp_lexer_next_token_is_not (parser->lexer,
19642 CPP_SEMICOLON)
19643 && cp_lexer_next_token_is_not (parser->lexer,
19644 CPP_OPEN_BRACE))
19645 cp_parser_abort_tentative_parse (parser);
19646 else if (!cp_parser_parse_definitely (parser))
19647 ;
19648 else
19649 gnu_attrs = attr;
19650 }
19651 tree requires_clause = NULL_TREE;
19652 late_return = (cp_parser_late_return_type_opt
19653 (parser, declarator, requires_clause,
19654 memfn ? cv_quals : -1));
19655
19656 /* Parse the virt-specifier-seq. */
19657 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19658
19659 /* Create the function-declarator. */
19660 declarator = make_call_declarator (declarator,
19661 params,
19662 cv_quals,
19663 virt_specifiers,
19664 ref_qual,
19665 tx_qual,
19666 exception_specification,
19667 late_return,
19668 requires_clause);
19669 declarator->std_attributes = attrs;
19670 declarator->attributes = gnu_attrs;
19671 /* Any subsequent parameter lists are to do with
19672 return type, so are not those of the declared
19673 function. */
19674 parser->default_arg_ok_p = false;
19675 }
19676
19677 /* Remove the function parms from scope. */
19678 pop_bindings_and_leave_scope ();
19679
19680 if (is_declarator)
19681 /* Repeat the main loop. */
19682 continue;
19683 }
19684
19685 /* If this is the first, we can try a parenthesized
19686 declarator. */
19687 if (first)
19688 {
19689 bool saved_in_type_id_in_expr_p;
19690
19691 parser->default_arg_ok_p = saved_default_arg_ok_p;
19692 parser->in_declarator_p = saved_in_declarator_p;
19693
19694 /* Consume the `('. */
19695 cp_lexer_consume_token (parser->lexer);
19696 /* Parse the nested declarator. */
19697 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19698 parser->in_type_id_in_expr_p = true;
19699 declarator
19700 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19701 /*parenthesized_p=*/NULL,
19702 member_p, friend_p);
19703 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19704 first = false;
19705 /* Expect a `)'. */
19706 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19707 declarator = cp_error_declarator;
19708 if (declarator == cp_error_declarator)
19709 break;
19710
19711 goto handle_declarator;
19712 }
19713 /* Otherwise, we must be done. */
19714 else
19715 break;
19716 }
19717 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19718 && token->type == CPP_OPEN_SQUARE
19719 && !cp_next_tokens_can_be_attribute_p (parser))
19720 {
19721 /* Parse an array-declarator. */
19722 tree bounds, attrs;
19723
19724 if (ctor_dtor_or_conv_p)
19725 *ctor_dtor_or_conv_p = 0;
19726
19727 first = false;
19728 parser->default_arg_ok_p = false;
19729 parser->in_declarator_p = true;
19730 /* Consume the `['. */
19731 cp_lexer_consume_token (parser->lexer);
19732 /* Peek at the next token. */
19733 token = cp_lexer_peek_token (parser->lexer);
19734 /* If the next token is `]', then there is no
19735 constant-expression. */
19736 if (token->type != CPP_CLOSE_SQUARE)
19737 {
19738 bool non_constant_p;
19739 bounds
19740 = cp_parser_constant_expression (parser,
19741 /*allow_non_constant=*/true,
19742 &non_constant_p);
19743 if (!non_constant_p)
19744 /* OK */;
19745 else if (error_operand_p (bounds))
19746 /* Already gave an error. */;
19747 else if (!parser->in_function_body
19748 || current_binding_level->kind == sk_function_parms)
19749 {
19750 /* Normally, the array bound must be an integral constant
19751 expression. However, as an extension, we allow VLAs
19752 in function scopes as long as they aren't part of a
19753 parameter declaration. */
19754 cp_parser_error (parser,
19755 "array bound is not an integer constant");
19756 bounds = error_mark_node;
19757 }
19758 else if (processing_template_decl
19759 && !type_dependent_expression_p (bounds))
19760 {
19761 /* Remember this wasn't a constant-expression. */
19762 bounds = build_nop (TREE_TYPE (bounds), bounds);
19763 TREE_SIDE_EFFECTS (bounds) = 1;
19764 }
19765 }
19766 else
19767 bounds = NULL_TREE;
19768 /* Look for the closing `]'. */
19769 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19770 {
19771 declarator = cp_error_declarator;
19772 break;
19773 }
19774
19775 attrs = cp_parser_std_attribute_spec_seq (parser);
19776 declarator = make_array_declarator (declarator, bounds);
19777 declarator->std_attributes = attrs;
19778 }
19779 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19780 {
19781 {
19782 tree qualifying_scope;
19783 tree unqualified_name;
19784 tree attrs;
19785 special_function_kind sfk;
19786 bool abstract_ok;
19787 bool pack_expansion_p = false;
19788 cp_token *declarator_id_start_token;
19789
19790 /* Parse a declarator-id */
19791 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19792 if (abstract_ok)
19793 {
19794 cp_parser_parse_tentatively (parser);
19795
19796 /* If we see an ellipsis, we should be looking at a
19797 parameter pack. */
19798 if (token->type == CPP_ELLIPSIS)
19799 {
19800 /* Consume the `...' */
19801 cp_lexer_consume_token (parser->lexer);
19802
19803 pack_expansion_p = true;
19804 }
19805 }
19806
19807 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19808 unqualified_name
19809 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19810 qualifying_scope = parser->scope;
19811 if (abstract_ok)
19812 {
19813 bool okay = false;
19814
19815 if (!unqualified_name && pack_expansion_p)
19816 {
19817 /* Check whether an error occurred. */
19818 okay = !cp_parser_error_occurred (parser);
19819
19820 /* We already consumed the ellipsis to mark a
19821 parameter pack, but we have no way to report it,
19822 so abort the tentative parse. We will be exiting
19823 immediately anyway. */
19824 cp_parser_abort_tentative_parse (parser);
19825 }
19826 else
19827 okay = cp_parser_parse_definitely (parser);
19828
19829 if (!okay)
19830 unqualified_name = error_mark_node;
19831 else if (unqualified_name
19832 && (qualifying_scope
19833 || (!identifier_p (unqualified_name))))
19834 {
19835 cp_parser_error (parser, "expected unqualified-id");
19836 unqualified_name = error_mark_node;
19837 }
19838 }
19839
19840 if (!unqualified_name)
19841 return NULL;
19842 if (unqualified_name == error_mark_node)
19843 {
19844 declarator = cp_error_declarator;
19845 pack_expansion_p = false;
19846 declarator->parameter_pack_p = false;
19847 break;
19848 }
19849
19850 attrs = cp_parser_std_attribute_spec_seq (parser);
19851
19852 if (qualifying_scope && at_namespace_scope_p ()
19853 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19854 {
19855 /* In the declaration of a member of a template class
19856 outside of the class itself, the SCOPE will sometimes
19857 be a TYPENAME_TYPE. For example, given:
19858
19859 template <typename T>
19860 int S<T>::R::i = 3;
19861
19862 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19863 this context, we must resolve S<T>::R to an ordinary
19864 type, rather than a typename type.
19865
19866 The reason we normally avoid resolving TYPENAME_TYPEs
19867 is that a specialization of `S' might render
19868 `S<T>::R' not a type. However, if `S' is
19869 specialized, then this `i' will not be used, so there
19870 is no harm in resolving the types here. */
19871 tree type;
19872
19873 /* Resolve the TYPENAME_TYPE. */
19874 type = resolve_typename_type (qualifying_scope,
19875 /*only_current_p=*/false);
19876 /* If that failed, the declarator is invalid. */
19877 if (TREE_CODE (type) == TYPENAME_TYPE)
19878 {
19879 if (typedef_variant_p (type))
19880 error_at (declarator_id_start_token->location,
19881 "cannot define member of dependent typedef "
19882 "%qT", type);
19883 else
19884 error_at (declarator_id_start_token->location,
19885 "%<%T::%E%> is not a type",
19886 TYPE_CONTEXT (qualifying_scope),
19887 TYPE_IDENTIFIER (qualifying_scope));
19888 }
19889 qualifying_scope = type;
19890 }
19891
19892 sfk = sfk_none;
19893
19894 if (unqualified_name)
19895 {
19896 tree class_type;
19897
19898 if (qualifying_scope
19899 && CLASS_TYPE_P (qualifying_scope))
19900 class_type = qualifying_scope;
19901 else
19902 class_type = current_class_type;
19903
19904 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19905 {
19906 tree name_type = TREE_TYPE (unqualified_name);
19907 if (class_type && same_type_p (name_type, class_type))
19908 {
19909 if (qualifying_scope
19910 && CLASSTYPE_USE_TEMPLATE (name_type))
19911 {
19912 error_at (declarator_id_start_token->location,
19913 "invalid use of constructor as a template");
19914 inform (declarator_id_start_token->location,
19915 "use %<%T::%D%> instead of %<%T::%D%> to "
19916 "name the constructor in a qualified name",
19917 class_type,
19918 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19919 class_type, name_type);
19920 declarator = cp_error_declarator;
19921 break;
19922 }
19923 else
19924 unqualified_name = constructor_name (class_type);
19925 }
19926 else
19927 {
19928 /* We do not attempt to print the declarator
19929 here because we do not have enough
19930 information about its original syntactic
19931 form. */
19932 cp_parser_error (parser, "invalid declarator");
19933 declarator = cp_error_declarator;
19934 break;
19935 }
19936 }
19937
19938 if (class_type)
19939 {
19940 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19941 sfk = sfk_destructor;
19942 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19943 sfk = sfk_conversion;
19944 else if (/* There's no way to declare a constructor
19945 for an unnamed type, even if the type
19946 got a name for linkage purposes. */
19947 !TYPE_WAS_UNNAMED (class_type)
19948 /* Handle correctly (c++/19200):
19949
19950 struct S {
19951 struct T{};
19952 friend void S(T);
19953 };
19954
19955 and also:
19956
19957 namespace N {
19958 void S();
19959 }
19960
19961 struct S {
19962 friend void N::S();
19963 }; */
19964 && !(friend_p
19965 && class_type != qualifying_scope)
19966 && constructor_name_p (unqualified_name,
19967 class_type))
19968 {
19969 unqualified_name = constructor_name (class_type);
19970 sfk = sfk_constructor;
19971 }
19972 else if (is_overloaded_fn (unqualified_name)
19973 && DECL_CONSTRUCTOR_P (get_first_fn
19974 (unqualified_name)))
19975 sfk = sfk_constructor;
19976
19977 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19978 *ctor_dtor_or_conv_p = -1;
19979 }
19980 }
19981 declarator = make_id_declarator (qualifying_scope,
19982 unqualified_name,
19983 sfk);
19984 declarator->std_attributes = attrs;
19985 declarator->id_loc = token->location;
19986 declarator->parameter_pack_p = pack_expansion_p;
19987
19988 if (pack_expansion_p)
19989 maybe_warn_variadic_templates ();
19990 }
19991
19992 handle_declarator:;
19993 scope = get_scope_of_declarator (declarator);
19994 if (scope)
19995 {
19996 /* Any names that appear after the declarator-id for a
19997 member are looked up in the containing scope. */
19998 if (at_function_scope_p ())
19999 {
20000 /* But declarations with qualified-ids can't appear in a
20001 function. */
20002 cp_parser_error (parser, "qualified-id in declaration");
20003 declarator = cp_error_declarator;
20004 break;
20005 }
20006 pushed_scope = push_scope (scope);
20007 }
20008 parser->in_declarator_p = true;
20009 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20010 || (declarator && declarator->kind == cdk_id))
20011 /* Default args are only allowed on function
20012 declarations. */
20013 parser->default_arg_ok_p = saved_default_arg_ok_p;
20014 else
20015 parser->default_arg_ok_p = false;
20016
20017 first = false;
20018 }
20019 /* We're done. */
20020 else
20021 break;
20022 }
20023
20024 /* For an abstract declarator, we might wind up with nothing at this
20025 point. That's an error; the declarator is not optional. */
20026 if (!declarator)
20027 cp_parser_error (parser, "expected declarator");
20028
20029 /* If we entered a scope, we must exit it now. */
20030 if (pushed_scope)
20031 pop_scope (pushed_scope);
20032
20033 parser->default_arg_ok_p = saved_default_arg_ok_p;
20034 parser->in_declarator_p = saved_in_declarator_p;
20035
20036 return declarator;
20037 }
20038
20039 /* Parse a ptr-operator.
20040
20041 ptr-operator:
20042 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20043 * cv-qualifier-seq [opt]
20044 &
20045 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20046 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20047
20048 GNU Extension:
20049
20050 ptr-operator:
20051 & cv-qualifier-seq [opt]
20052
20053 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20054 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20055 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20056 filled in with the TYPE containing the member. *CV_QUALS is
20057 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20058 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20059 Note that the tree codes returned by this function have nothing
20060 to do with the types of trees that will be eventually be created
20061 to represent the pointer or reference type being parsed. They are
20062 just constants with suggestive names. */
20063 static enum tree_code
20064 cp_parser_ptr_operator (cp_parser* parser,
20065 tree* type,
20066 cp_cv_quals *cv_quals,
20067 tree *attributes)
20068 {
20069 enum tree_code code = ERROR_MARK;
20070 cp_token *token;
20071 tree attrs = NULL_TREE;
20072
20073 /* Assume that it's not a pointer-to-member. */
20074 *type = NULL_TREE;
20075 /* And that there are no cv-qualifiers. */
20076 *cv_quals = TYPE_UNQUALIFIED;
20077
20078 /* Peek at the next token. */
20079 token = cp_lexer_peek_token (parser->lexer);
20080
20081 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20082 if (token->type == CPP_MULT)
20083 code = INDIRECT_REF;
20084 else if (token->type == CPP_AND)
20085 code = ADDR_EXPR;
20086 else if ((cxx_dialect != cxx98) &&
20087 token->type == CPP_AND_AND) /* C++0x only */
20088 code = NON_LVALUE_EXPR;
20089
20090 if (code != ERROR_MARK)
20091 {
20092 /* Consume the `*', `&' or `&&'. */
20093 cp_lexer_consume_token (parser->lexer);
20094
20095 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20096 `&', if we are allowing GNU extensions. (The only qualifier
20097 that can legally appear after `&' is `restrict', but that is
20098 enforced during semantic analysis. */
20099 if (code == INDIRECT_REF
20100 || cp_parser_allow_gnu_extensions_p (parser))
20101 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20102
20103 attrs = cp_parser_std_attribute_spec_seq (parser);
20104 if (attributes != NULL)
20105 *attributes = attrs;
20106 }
20107 else
20108 {
20109 /* Try the pointer-to-member case. */
20110 cp_parser_parse_tentatively (parser);
20111 /* Look for the optional `::' operator. */
20112 cp_parser_global_scope_opt (parser,
20113 /*current_scope_valid_p=*/false);
20114 /* Look for the nested-name specifier. */
20115 token = cp_lexer_peek_token (parser->lexer);
20116 cp_parser_nested_name_specifier (parser,
20117 /*typename_keyword_p=*/false,
20118 /*check_dependency_p=*/true,
20119 /*type_p=*/false,
20120 /*is_declaration=*/false);
20121 /* If we found it, and the next token is a `*', then we are
20122 indeed looking at a pointer-to-member operator. */
20123 if (!cp_parser_error_occurred (parser)
20124 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20125 {
20126 /* Indicate that the `*' operator was used. */
20127 code = INDIRECT_REF;
20128
20129 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20130 error_at (token->location, "%qD is a namespace", parser->scope);
20131 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20132 error_at (token->location, "cannot form pointer to member of "
20133 "non-class %q#T", parser->scope);
20134 else
20135 {
20136 /* The type of which the member is a member is given by the
20137 current SCOPE. */
20138 *type = parser->scope;
20139 /* The next name will not be qualified. */
20140 parser->scope = NULL_TREE;
20141 parser->qualifying_scope = NULL_TREE;
20142 parser->object_scope = NULL_TREE;
20143 /* Look for optional c++11 attributes. */
20144 attrs = cp_parser_std_attribute_spec_seq (parser);
20145 if (attributes != NULL)
20146 *attributes = attrs;
20147 /* Look for the optional cv-qualifier-seq. */
20148 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20149 }
20150 }
20151 /* If that didn't work we don't have a ptr-operator. */
20152 if (!cp_parser_parse_definitely (parser))
20153 cp_parser_error (parser, "expected ptr-operator");
20154 }
20155
20156 return code;
20157 }
20158
20159 /* Parse an (optional) cv-qualifier-seq.
20160
20161 cv-qualifier-seq:
20162 cv-qualifier cv-qualifier-seq [opt]
20163
20164 cv-qualifier:
20165 const
20166 volatile
20167
20168 GNU Extension:
20169
20170 cv-qualifier:
20171 __restrict__
20172
20173 Returns a bitmask representing the cv-qualifiers. */
20174
20175 static cp_cv_quals
20176 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20177 {
20178 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20179
20180 while (true)
20181 {
20182 cp_token *token;
20183 cp_cv_quals cv_qualifier;
20184
20185 /* Peek at the next token. */
20186 token = cp_lexer_peek_token (parser->lexer);
20187 /* See if it's a cv-qualifier. */
20188 switch (token->keyword)
20189 {
20190 case RID_CONST:
20191 cv_qualifier = TYPE_QUAL_CONST;
20192 break;
20193
20194 case RID_VOLATILE:
20195 cv_qualifier = TYPE_QUAL_VOLATILE;
20196 break;
20197
20198 case RID_RESTRICT:
20199 cv_qualifier = TYPE_QUAL_RESTRICT;
20200 break;
20201
20202 default:
20203 cv_qualifier = TYPE_UNQUALIFIED;
20204 break;
20205 }
20206
20207 if (!cv_qualifier)
20208 break;
20209
20210 if (cv_quals & cv_qualifier)
20211 {
20212 error_at (token->location, "duplicate cv-qualifier");
20213 cp_lexer_purge_token (parser->lexer);
20214 }
20215 else
20216 {
20217 cp_lexer_consume_token (parser->lexer);
20218 cv_quals |= cv_qualifier;
20219 }
20220 }
20221
20222 return cv_quals;
20223 }
20224
20225 /* Parse an (optional) ref-qualifier
20226
20227 ref-qualifier:
20228 &
20229 &&
20230
20231 Returns cp_ref_qualifier representing ref-qualifier. */
20232
20233 static cp_ref_qualifier
20234 cp_parser_ref_qualifier_opt (cp_parser* parser)
20235 {
20236 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20237
20238 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20239 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20240 return ref_qual;
20241
20242 while (true)
20243 {
20244 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20245 cp_token *token = cp_lexer_peek_token (parser->lexer);
20246
20247 switch (token->type)
20248 {
20249 case CPP_AND:
20250 curr_ref_qual = REF_QUAL_LVALUE;
20251 break;
20252
20253 case CPP_AND_AND:
20254 curr_ref_qual = REF_QUAL_RVALUE;
20255 break;
20256
20257 default:
20258 curr_ref_qual = REF_QUAL_NONE;
20259 break;
20260 }
20261
20262 if (!curr_ref_qual)
20263 break;
20264 else if (ref_qual)
20265 {
20266 error_at (token->location, "multiple ref-qualifiers");
20267 cp_lexer_purge_token (parser->lexer);
20268 }
20269 else
20270 {
20271 ref_qual = curr_ref_qual;
20272 cp_lexer_consume_token (parser->lexer);
20273 }
20274 }
20275
20276 return ref_qual;
20277 }
20278
20279 /* Parse an optional tx-qualifier.
20280
20281 tx-qualifier:
20282 transaction_safe
20283 transaction_safe_dynamic */
20284
20285 static tree
20286 cp_parser_tx_qualifier_opt (cp_parser *parser)
20287 {
20288 cp_token *token = cp_lexer_peek_token (parser->lexer);
20289 if (token->type == CPP_NAME)
20290 {
20291 tree name = token->u.value;
20292 const char *p = IDENTIFIER_POINTER (name);
20293 const int len = strlen ("transaction_safe");
20294 if (!strncmp (p, "transaction_safe", len))
20295 {
20296 p += len;
20297 if (*p == '\0'
20298 || !strcmp (p, "_dynamic"))
20299 {
20300 cp_lexer_consume_token (parser->lexer);
20301 if (!flag_tm)
20302 {
20303 error ("%E requires %<-fgnu-tm%>", name);
20304 return NULL_TREE;
20305 }
20306 else
20307 return name;
20308 }
20309 }
20310 }
20311 return NULL_TREE;
20312 }
20313
20314 /* Parse an (optional) virt-specifier-seq.
20315
20316 virt-specifier-seq:
20317 virt-specifier virt-specifier-seq [opt]
20318
20319 virt-specifier:
20320 override
20321 final
20322
20323 Returns a bitmask representing the virt-specifiers. */
20324
20325 static cp_virt_specifiers
20326 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20327 {
20328 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20329
20330 while (true)
20331 {
20332 cp_token *token;
20333 cp_virt_specifiers virt_specifier;
20334
20335 /* Peek at the next token. */
20336 token = cp_lexer_peek_token (parser->lexer);
20337 /* See if it's a virt-specifier-qualifier. */
20338 if (token->type != CPP_NAME)
20339 break;
20340 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20341 {
20342 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20343 virt_specifier = VIRT_SPEC_OVERRIDE;
20344 }
20345 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20346 {
20347 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20348 virt_specifier = VIRT_SPEC_FINAL;
20349 }
20350 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20351 {
20352 virt_specifier = VIRT_SPEC_FINAL;
20353 }
20354 else
20355 break;
20356
20357 if (virt_specifiers & virt_specifier)
20358 {
20359 error_at (token->location, "duplicate virt-specifier");
20360 cp_lexer_purge_token (parser->lexer);
20361 }
20362 else
20363 {
20364 cp_lexer_consume_token (parser->lexer);
20365 virt_specifiers |= virt_specifier;
20366 }
20367 }
20368 return virt_specifiers;
20369 }
20370
20371 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20372 is in scope even though it isn't real. */
20373
20374 void
20375 inject_this_parameter (tree ctype, cp_cv_quals quals)
20376 {
20377 tree this_parm;
20378
20379 if (current_class_ptr)
20380 {
20381 /* We don't clear this between NSDMIs. Is it already what we want? */
20382 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20383 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20384 && cp_type_quals (type) == quals)
20385 return;
20386 }
20387
20388 this_parm = build_this_parm (ctype, quals);
20389 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20390 current_class_ptr = NULL_TREE;
20391 current_class_ref
20392 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20393 current_class_ptr = this_parm;
20394 }
20395
20396 /* Return true iff our current scope is a non-static data member
20397 initializer. */
20398
20399 bool
20400 parsing_nsdmi (void)
20401 {
20402 /* We recognize NSDMI context by the context-less 'this' pointer set up
20403 by the function above. */
20404 if (current_class_ptr
20405 && TREE_CODE (current_class_ptr) == PARM_DECL
20406 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20407 return true;
20408 return false;
20409 }
20410
20411 /* Parse a late-specified return type, if any. This is not a separate
20412 non-terminal, but part of a function declarator, which looks like
20413
20414 -> trailing-type-specifier-seq abstract-declarator(opt)
20415
20416 Returns the type indicated by the type-id.
20417
20418 In addition to this, parse any queued up #pragma omp declare simd
20419 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20420 #pragma acc routine clauses.
20421
20422 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20423 function. */
20424
20425 static tree
20426 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20427 tree& requires_clause, cp_cv_quals quals)
20428 {
20429 cp_token *token;
20430 tree type = NULL_TREE;
20431 bool declare_simd_p = (parser->omp_declare_simd
20432 && declarator
20433 && declarator->kind == cdk_id);
20434
20435 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20436 && declarator && declarator->kind == cdk_id);
20437
20438 bool oacc_routine_p = (parser->oacc_routine
20439 && declarator
20440 && declarator->kind == cdk_id);
20441
20442 /* Peek at the next token. */
20443 token = cp_lexer_peek_token (parser->lexer);
20444 /* A late-specified return type is indicated by an initial '->'. */
20445 if (token->type != CPP_DEREF
20446 && token->keyword != RID_REQUIRES
20447 && !(token->type == CPP_NAME
20448 && token->u.value == ridpointers[RID_REQUIRES])
20449 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20450 return NULL_TREE;
20451
20452 tree save_ccp = current_class_ptr;
20453 tree save_ccr = current_class_ref;
20454 if (quals >= 0)
20455 {
20456 /* DR 1207: 'this' is in scope in the trailing return type. */
20457 inject_this_parameter (current_class_type, quals);
20458 }
20459
20460 if (token->type == CPP_DEREF)
20461 {
20462 /* Consume the ->. */
20463 cp_lexer_consume_token (parser->lexer);
20464
20465 type = cp_parser_trailing_type_id (parser);
20466 }
20467
20468 /* Function declarations may be followed by a trailing
20469 requires-clause. */
20470 requires_clause = cp_parser_requires_clause_opt (parser);
20471
20472 if (cilk_simd_fn_vector_p)
20473 declarator->attributes
20474 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20475 declarator->attributes);
20476 if (declare_simd_p)
20477 declarator->attributes
20478 = cp_parser_late_parsing_omp_declare_simd (parser,
20479 declarator->attributes);
20480 if (oacc_routine_p)
20481 declarator->attributes
20482 = cp_parser_late_parsing_oacc_routine (parser,
20483 declarator->attributes);
20484
20485 if (quals >= 0)
20486 {
20487 current_class_ptr = save_ccp;
20488 current_class_ref = save_ccr;
20489 }
20490
20491 return type;
20492 }
20493
20494 /* Parse a declarator-id.
20495
20496 declarator-id:
20497 id-expression
20498 :: [opt] nested-name-specifier [opt] type-name
20499
20500 In the `id-expression' case, the value returned is as for
20501 cp_parser_id_expression if the id-expression was an unqualified-id.
20502 If the id-expression was a qualified-id, then a SCOPE_REF is
20503 returned. The first operand is the scope (either a NAMESPACE_DECL
20504 or TREE_TYPE), but the second is still just a representation of an
20505 unqualified-id. */
20506
20507 static tree
20508 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20509 {
20510 tree id;
20511 /* The expression must be an id-expression. Assume that qualified
20512 names are the names of types so that:
20513
20514 template <class T>
20515 int S<T>::R::i = 3;
20516
20517 will work; we must treat `S<T>::R' as the name of a type.
20518 Similarly, assume that qualified names are templates, where
20519 required, so that:
20520
20521 template <class T>
20522 int S<T>::R<T>::i = 3;
20523
20524 will work, too. */
20525 id = cp_parser_id_expression (parser,
20526 /*template_keyword_p=*/false,
20527 /*check_dependency_p=*/false,
20528 /*template_p=*/NULL,
20529 /*declarator_p=*/true,
20530 optional_p);
20531 if (id && BASELINK_P (id))
20532 id = BASELINK_FUNCTIONS (id);
20533 return id;
20534 }
20535
20536 /* Parse a type-id.
20537
20538 type-id:
20539 type-specifier-seq abstract-declarator [opt]
20540
20541 Returns the TYPE specified. */
20542
20543 static tree
20544 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20545 bool is_trailing_return)
20546 {
20547 cp_decl_specifier_seq type_specifier_seq;
20548 cp_declarator *abstract_declarator;
20549
20550 /* Parse the type-specifier-seq. */
20551 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20552 is_trailing_return,
20553 &type_specifier_seq);
20554 if (is_template_arg && type_specifier_seq.type
20555 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20556 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20557 /* A bare template name as a template argument is a template template
20558 argument, not a placeholder, so fail parsing it as a type argument. */
20559 {
20560 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20561 cp_parser_simulate_error (parser);
20562 return error_mark_node;
20563 }
20564 if (type_specifier_seq.type == error_mark_node)
20565 return error_mark_node;
20566
20567 /* There might or might not be an abstract declarator. */
20568 cp_parser_parse_tentatively (parser);
20569 /* Look for the declarator. */
20570 abstract_declarator
20571 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20572 /*parenthesized_p=*/NULL,
20573 /*member_p=*/false,
20574 /*friend_p=*/false);
20575 /* Check to see if there really was a declarator. */
20576 if (!cp_parser_parse_definitely (parser))
20577 abstract_declarator = NULL;
20578
20579 if (type_specifier_seq.type
20580 /* The concepts TS allows 'auto' as a type-id. */
20581 && (!flag_concepts || parser->in_type_id_in_expr_p)
20582 /* None of the valid uses of 'auto' in C++14 involve the type-id
20583 nonterminal, but it is valid in a trailing-return-type. */
20584 && !(cxx_dialect >= cxx14 && is_trailing_return))
20585 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20586 {
20587 /* A type-id with type 'auto' is only ok if the abstract declarator
20588 is a function declarator with a late-specified return type.
20589
20590 A type-id with 'auto' is also valid in a trailing-return-type
20591 in a compound-requirement. */
20592 if (abstract_declarator
20593 && abstract_declarator->kind == cdk_function
20594 && abstract_declarator->u.function.late_return_type)
20595 /* OK */;
20596 else if (parser->in_result_type_constraint_p)
20597 /* OK */;
20598 else
20599 {
20600 location_t loc = type_specifier_seq.locations[ds_type_spec];
20601 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20602 {
20603 error_at (loc, "missing template arguments after %qT",
20604 auto_node);
20605 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20606 tmpl);
20607 }
20608 else
20609 error_at (loc, "invalid use of %qT", auto_node);
20610 return error_mark_node;
20611 }
20612 }
20613
20614 return groktypename (&type_specifier_seq, abstract_declarator,
20615 is_template_arg);
20616 }
20617
20618 static tree
20619 cp_parser_type_id (cp_parser *parser)
20620 {
20621 return cp_parser_type_id_1 (parser, false, false);
20622 }
20623
20624 static tree
20625 cp_parser_template_type_arg (cp_parser *parser)
20626 {
20627 tree r;
20628 const char *saved_message = parser->type_definition_forbidden_message;
20629 parser->type_definition_forbidden_message
20630 = G_("types may not be defined in template arguments");
20631 r = cp_parser_type_id_1 (parser, true, false);
20632 parser->type_definition_forbidden_message = saved_message;
20633 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20634 {
20635 error ("invalid use of %<auto%> in template argument");
20636 r = error_mark_node;
20637 }
20638 return r;
20639 }
20640
20641 static tree
20642 cp_parser_trailing_type_id (cp_parser *parser)
20643 {
20644 return cp_parser_type_id_1 (parser, false, true);
20645 }
20646
20647 /* Parse a type-specifier-seq.
20648
20649 type-specifier-seq:
20650 type-specifier type-specifier-seq [opt]
20651
20652 GNU extension:
20653
20654 type-specifier-seq:
20655 attributes type-specifier-seq [opt]
20656
20657 If IS_DECLARATION is true, we are at the start of a "condition" or
20658 exception-declaration, so we might be followed by a declarator-id.
20659
20660 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20661 i.e. we've just seen "->".
20662
20663 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20664
20665 static void
20666 cp_parser_type_specifier_seq (cp_parser* parser,
20667 bool is_declaration,
20668 bool is_trailing_return,
20669 cp_decl_specifier_seq *type_specifier_seq)
20670 {
20671 bool seen_type_specifier = false;
20672 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20673 cp_token *start_token = NULL;
20674
20675 /* Clear the TYPE_SPECIFIER_SEQ. */
20676 clear_decl_specs (type_specifier_seq);
20677
20678 /* In the context of a trailing return type, enum E { } is an
20679 elaborated-type-specifier followed by a function-body, not an
20680 enum-specifier. */
20681 if (is_trailing_return)
20682 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20683
20684 /* Parse the type-specifiers and attributes. */
20685 while (true)
20686 {
20687 tree type_specifier;
20688 bool is_cv_qualifier;
20689
20690 /* Check for attributes first. */
20691 if (cp_next_tokens_can_be_attribute_p (parser))
20692 {
20693 type_specifier_seq->attributes =
20694 chainon (type_specifier_seq->attributes,
20695 cp_parser_attributes_opt (parser));
20696 continue;
20697 }
20698
20699 /* record the token of the beginning of the type specifier seq,
20700 for error reporting purposes*/
20701 if (!start_token)
20702 start_token = cp_lexer_peek_token (parser->lexer);
20703
20704 /* Look for the type-specifier. */
20705 type_specifier = cp_parser_type_specifier (parser,
20706 flags,
20707 type_specifier_seq,
20708 /*is_declaration=*/false,
20709 NULL,
20710 &is_cv_qualifier);
20711 if (!type_specifier)
20712 {
20713 /* If the first type-specifier could not be found, this is not a
20714 type-specifier-seq at all. */
20715 if (!seen_type_specifier)
20716 {
20717 /* Set in_declarator_p to avoid skipping to the semicolon. */
20718 int in_decl = parser->in_declarator_p;
20719 parser->in_declarator_p = true;
20720
20721 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20722 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20723 cp_parser_error (parser, "expected type-specifier");
20724
20725 parser->in_declarator_p = in_decl;
20726
20727 type_specifier_seq->type = error_mark_node;
20728 return;
20729 }
20730 /* If subsequent type-specifiers could not be found, the
20731 type-specifier-seq is complete. */
20732 break;
20733 }
20734
20735 seen_type_specifier = true;
20736 /* The standard says that a condition can be:
20737
20738 type-specifier-seq declarator = assignment-expression
20739
20740 However, given:
20741
20742 struct S {};
20743 if (int S = ...)
20744
20745 we should treat the "S" as a declarator, not as a
20746 type-specifier. The standard doesn't say that explicitly for
20747 type-specifier-seq, but it does say that for
20748 decl-specifier-seq in an ordinary declaration. Perhaps it
20749 would be clearer just to allow a decl-specifier-seq here, and
20750 then add a semantic restriction that if any decl-specifiers
20751 that are not type-specifiers appear, the program is invalid. */
20752 if (is_declaration && !is_cv_qualifier)
20753 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20754 }
20755 }
20756
20757 /* Return whether the function currently being declared has an associated
20758 template parameter list. */
20759
20760 static bool
20761 function_being_declared_is_template_p (cp_parser* parser)
20762 {
20763 if (!current_template_parms || processing_template_parmlist)
20764 return false;
20765
20766 if (parser->implicit_template_scope)
20767 return true;
20768
20769 if (at_class_scope_p ()
20770 && TYPE_BEING_DEFINED (current_class_type))
20771 return parser->num_template_parameter_lists != 0;
20772
20773 return ((int) parser->num_template_parameter_lists > template_class_depth
20774 (current_class_type));
20775 }
20776
20777 /* Parse a parameter-declaration-clause.
20778
20779 parameter-declaration-clause:
20780 parameter-declaration-list [opt] ... [opt]
20781 parameter-declaration-list , ...
20782
20783 Returns a representation for the parameter declarations. A return
20784 value of NULL indicates a parameter-declaration-clause consisting
20785 only of an ellipsis. */
20786
20787 static tree
20788 cp_parser_parameter_declaration_clause (cp_parser* parser)
20789 {
20790 tree parameters;
20791 cp_token *token;
20792 bool ellipsis_p;
20793 bool is_error;
20794
20795 struct cleanup {
20796 cp_parser* parser;
20797 int auto_is_implicit_function_template_parm_p;
20798 ~cleanup() {
20799 parser->auto_is_implicit_function_template_parm_p
20800 = auto_is_implicit_function_template_parm_p;
20801 }
20802 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20803
20804 (void) cleanup;
20805
20806 if (!processing_specialization
20807 && !processing_template_parmlist
20808 && !processing_explicit_instantiation)
20809 if (!current_function_decl
20810 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20811 parser->auto_is_implicit_function_template_parm_p = true;
20812
20813 /* Peek at the next token. */
20814 token = cp_lexer_peek_token (parser->lexer);
20815 /* Check for trivial parameter-declaration-clauses. */
20816 if (token->type == CPP_ELLIPSIS)
20817 {
20818 /* Consume the `...' token. */
20819 cp_lexer_consume_token (parser->lexer);
20820 return NULL_TREE;
20821 }
20822 else if (token->type == CPP_CLOSE_PAREN)
20823 /* There are no parameters. */
20824 {
20825 #ifndef NO_IMPLICIT_EXTERN_C
20826 if (in_system_header_at (input_location)
20827 && current_class_type == NULL
20828 && current_lang_name == lang_name_c)
20829 return NULL_TREE;
20830 else
20831 #endif
20832 return void_list_node;
20833 }
20834 /* Check for `(void)', too, which is a special case. */
20835 else if (token->keyword == RID_VOID
20836 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20837 == CPP_CLOSE_PAREN))
20838 {
20839 /* Consume the `void' token. */
20840 cp_lexer_consume_token (parser->lexer);
20841 /* There are no parameters. */
20842 return void_list_node;
20843 }
20844
20845 /* Parse the parameter-declaration-list. */
20846 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20847 /* If a parse error occurred while parsing the
20848 parameter-declaration-list, then the entire
20849 parameter-declaration-clause is erroneous. */
20850 if (is_error)
20851 return NULL;
20852
20853 /* Peek at the next token. */
20854 token = cp_lexer_peek_token (parser->lexer);
20855 /* If it's a `,', the clause should terminate with an ellipsis. */
20856 if (token->type == CPP_COMMA)
20857 {
20858 /* Consume the `,'. */
20859 cp_lexer_consume_token (parser->lexer);
20860 /* Expect an ellipsis. */
20861 ellipsis_p
20862 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20863 }
20864 /* It might also be `...' if the optional trailing `,' was
20865 omitted. */
20866 else if (token->type == CPP_ELLIPSIS)
20867 {
20868 /* Consume the `...' token. */
20869 cp_lexer_consume_token (parser->lexer);
20870 /* And remember that we saw it. */
20871 ellipsis_p = true;
20872 }
20873 else
20874 ellipsis_p = false;
20875
20876 /* Finish the parameter list. */
20877 if (!ellipsis_p)
20878 parameters = chainon (parameters, void_list_node);
20879
20880 return parameters;
20881 }
20882
20883 /* Parse a parameter-declaration-list.
20884
20885 parameter-declaration-list:
20886 parameter-declaration
20887 parameter-declaration-list , parameter-declaration
20888
20889 Returns a representation of the parameter-declaration-list, as for
20890 cp_parser_parameter_declaration_clause. However, the
20891 `void_list_node' is never appended to the list. Upon return,
20892 *IS_ERROR will be true iff an error occurred. */
20893
20894 static tree
20895 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20896 {
20897 tree parameters = NULL_TREE;
20898 tree *tail = &parameters;
20899 bool saved_in_unbraced_linkage_specification_p;
20900 int index = 0;
20901
20902 /* Assume all will go well. */
20903 *is_error = false;
20904 /* The special considerations that apply to a function within an
20905 unbraced linkage specifications do not apply to the parameters
20906 to the function. */
20907 saved_in_unbraced_linkage_specification_p
20908 = parser->in_unbraced_linkage_specification_p;
20909 parser->in_unbraced_linkage_specification_p = false;
20910
20911 /* Look for more parameters. */
20912 while (true)
20913 {
20914 cp_parameter_declarator *parameter;
20915 tree decl = error_mark_node;
20916 bool parenthesized_p = false;
20917 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20918 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20919 (current_template_parms)) : 0);
20920
20921 /* Parse the parameter. */
20922 parameter
20923 = cp_parser_parameter_declaration (parser,
20924 /*template_parm_p=*/false,
20925 &parenthesized_p);
20926
20927 /* We don't know yet if the enclosing context is deprecated, so wait
20928 and warn in grokparms if appropriate. */
20929 deprecated_state = DEPRECATED_SUPPRESS;
20930
20931 if (parameter)
20932 {
20933 /* If a function parameter pack was specified and an implicit template
20934 parameter was introduced during cp_parser_parameter_declaration,
20935 change any implicit parameters introduced into packs. */
20936 if (parser->implicit_template_parms
20937 && parameter->declarator
20938 && parameter->declarator->parameter_pack_p)
20939 {
20940 int latest_template_parm_idx = TREE_VEC_LENGTH
20941 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20942
20943 if (latest_template_parm_idx != template_parm_idx)
20944 parameter->decl_specifiers.type = convert_generic_types_to_packs
20945 (parameter->decl_specifiers.type,
20946 template_parm_idx, latest_template_parm_idx);
20947 }
20948
20949 decl = grokdeclarator (parameter->declarator,
20950 &parameter->decl_specifiers,
20951 PARM,
20952 parameter->default_argument != NULL_TREE,
20953 &parameter->decl_specifiers.attributes);
20954 }
20955
20956 deprecated_state = DEPRECATED_NORMAL;
20957
20958 /* If a parse error occurred parsing the parameter declaration,
20959 then the entire parameter-declaration-list is erroneous. */
20960 if (decl == error_mark_node)
20961 {
20962 *is_error = true;
20963 parameters = error_mark_node;
20964 break;
20965 }
20966
20967 if (parameter->decl_specifiers.attributes)
20968 cplus_decl_attributes (&decl,
20969 parameter->decl_specifiers.attributes,
20970 0);
20971 if (DECL_NAME (decl))
20972 decl = pushdecl (decl);
20973
20974 if (decl != error_mark_node)
20975 {
20976 retrofit_lang_decl (decl);
20977 DECL_PARM_INDEX (decl) = ++index;
20978 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20979 }
20980
20981 /* Add the new parameter to the list. */
20982 *tail = build_tree_list (parameter->default_argument, decl);
20983 tail = &TREE_CHAIN (*tail);
20984
20985 /* Peek at the next token. */
20986 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20987 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20988 /* These are for Objective-C++ */
20989 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20990 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20991 /* The parameter-declaration-list is complete. */
20992 break;
20993 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20994 {
20995 cp_token *token;
20996
20997 /* Peek at the next token. */
20998 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20999 /* If it's an ellipsis, then the list is complete. */
21000 if (token->type == CPP_ELLIPSIS)
21001 break;
21002 /* Otherwise, there must be more parameters. Consume the
21003 `,'. */
21004 cp_lexer_consume_token (parser->lexer);
21005 /* When parsing something like:
21006
21007 int i(float f, double d)
21008
21009 we can tell after seeing the declaration for "f" that we
21010 are not looking at an initialization of a variable "i",
21011 but rather at the declaration of a function "i".
21012
21013 Due to the fact that the parsing of template arguments
21014 (as specified to a template-id) requires backtracking we
21015 cannot use this technique when inside a template argument
21016 list. */
21017 if (!parser->in_template_argument_list_p
21018 && !parser->in_type_id_in_expr_p
21019 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21020 /* However, a parameter-declaration of the form
21021 "float(f)" (which is a valid declaration of a
21022 parameter "f") can also be interpreted as an
21023 expression (the conversion of "f" to "float"). */
21024 && !parenthesized_p)
21025 cp_parser_commit_to_tentative_parse (parser);
21026 }
21027 else
21028 {
21029 cp_parser_error (parser, "expected %<,%> or %<...%>");
21030 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21031 cp_parser_skip_to_closing_parenthesis (parser,
21032 /*recovering=*/true,
21033 /*or_comma=*/false,
21034 /*consume_paren=*/false);
21035 break;
21036 }
21037 }
21038
21039 parser->in_unbraced_linkage_specification_p
21040 = saved_in_unbraced_linkage_specification_p;
21041
21042 /* Reset implicit_template_scope if we are about to leave the function
21043 parameter list that introduced it. Note that for out-of-line member
21044 definitions, there will be one or more class scopes before we get to
21045 the template parameter scope. */
21046
21047 if (cp_binding_level *its = parser->implicit_template_scope)
21048 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21049 {
21050 while (maybe_its->kind == sk_class)
21051 maybe_its = maybe_its->level_chain;
21052 if (maybe_its == its)
21053 {
21054 parser->implicit_template_parms = 0;
21055 parser->implicit_template_scope = 0;
21056 }
21057 }
21058
21059 return parameters;
21060 }
21061
21062 /* Parse a parameter declaration.
21063
21064 parameter-declaration:
21065 decl-specifier-seq ... [opt] declarator
21066 decl-specifier-seq declarator = assignment-expression
21067 decl-specifier-seq ... [opt] abstract-declarator [opt]
21068 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21069
21070 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21071 declares a template parameter. (In that case, a non-nested `>'
21072 token encountered during the parsing of the assignment-expression
21073 is not interpreted as a greater-than operator.)
21074
21075 Returns a representation of the parameter, or NULL if an error
21076 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21077 true iff the declarator is of the form "(p)". */
21078
21079 static cp_parameter_declarator *
21080 cp_parser_parameter_declaration (cp_parser *parser,
21081 bool template_parm_p,
21082 bool *parenthesized_p)
21083 {
21084 int declares_class_or_enum;
21085 cp_decl_specifier_seq decl_specifiers;
21086 cp_declarator *declarator;
21087 tree default_argument;
21088 cp_token *token = NULL, *declarator_token_start = NULL;
21089 const char *saved_message;
21090 bool template_parameter_pack_p = false;
21091
21092 /* In a template parameter, `>' is not an operator.
21093
21094 [temp.param]
21095
21096 When parsing a default template-argument for a non-type
21097 template-parameter, the first non-nested `>' is taken as the end
21098 of the template parameter-list rather than a greater-than
21099 operator. */
21100
21101 /* Type definitions may not appear in parameter types. */
21102 saved_message = parser->type_definition_forbidden_message;
21103 parser->type_definition_forbidden_message
21104 = G_("types may not be defined in parameter types");
21105
21106 /* Parse the declaration-specifiers. */
21107 cp_parser_decl_specifier_seq (parser,
21108 CP_PARSER_FLAGS_NONE,
21109 &decl_specifiers,
21110 &declares_class_or_enum);
21111
21112 /* Complain about missing 'typename' or other invalid type names. */
21113 if (!decl_specifiers.any_type_specifiers_p
21114 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21115 decl_specifiers.type = error_mark_node;
21116
21117 /* If an error occurred, there's no reason to attempt to parse the
21118 rest of the declaration. */
21119 if (cp_parser_error_occurred (parser))
21120 {
21121 parser->type_definition_forbidden_message = saved_message;
21122 return NULL;
21123 }
21124
21125 /* Peek at the next token. */
21126 token = cp_lexer_peek_token (parser->lexer);
21127
21128 /* If the next token is a `)', `,', `=', `>', or `...', then there
21129 is no declarator. However, when variadic templates are enabled,
21130 there may be a declarator following `...'. */
21131 if (token->type == CPP_CLOSE_PAREN
21132 || token->type == CPP_COMMA
21133 || token->type == CPP_EQ
21134 || token->type == CPP_GREATER)
21135 {
21136 declarator = NULL;
21137 if (parenthesized_p)
21138 *parenthesized_p = false;
21139 }
21140 /* Otherwise, there should be a declarator. */
21141 else
21142 {
21143 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21144 parser->default_arg_ok_p = false;
21145
21146 /* After seeing a decl-specifier-seq, if the next token is not a
21147 "(", there is no possibility that the code is a valid
21148 expression. Therefore, if parsing tentatively, we commit at
21149 this point. */
21150 if (!parser->in_template_argument_list_p
21151 /* In an expression context, having seen:
21152
21153 (int((char ...
21154
21155 we cannot be sure whether we are looking at a
21156 function-type (taking a "char" as a parameter) or a cast
21157 of some object of type "char" to "int". */
21158 && !parser->in_type_id_in_expr_p
21159 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21160 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21161 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21162 cp_parser_commit_to_tentative_parse (parser);
21163 /* Parse the declarator. */
21164 declarator_token_start = token;
21165 declarator = cp_parser_declarator (parser,
21166 CP_PARSER_DECLARATOR_EITHER,
21167 /*ctor_dtor_or_conv_p=*/NULL,
21168 parenthesized_p,
21169 /*member_p=*/false,
21170 /*friend_p=*/false);
21171 parser->default_arg_ok_p = saved_default_arg_ok_p;
21172 /* After the declarator, allow more attributes. */
21173 decl_specifiers.attributes
21174 = chainon (decl_specifiers.attributes,
21175 cp_parser_attributes_opt (parser));
21176
21177 /* If the declarator is a template parameter pack, remember that and
21178 clear the flag in the declarator itself so we don't get errors
21179 from grokdeclarator. */
21180 if (template_parm_p && declarator && declarator->parameter_pack_p)
21181 {
21182 declarator->parameter_pack_p = false;
21183 template_parameter_pack_p = true;
21184 }
21185 }
21186
21187 /* If the next token is an ellipsis, and we have not seen a declarator
21188 name, and if either the type of the declarator contains parameter
21189 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21190 for, eg, abbreviated integral type names), then we actually have a
21191 parameter pack expansion expression. Otherwise, leave the ellipsis
21192 for a C-style variadic function. */
21193 token = cp_lexer_peek_token (parser->lexer);
21194 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21195 {
21196 tree type = decl_specifiers.type;
21197
21198 if (type && DECL_P (type))
21199 type = TREE_TYPE (type);
21200
21201 if (((type
21202 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21203 && (template_parm_p || uses_parameter_packs (type)))
21204 || (!type && template_parm_p))
21205 && declarator_can_be_parameter_pack (declarator))
21206 {
21207 /* Consume the `...'. */
21208 cp_lexer_consume_token (parser->lexer);
21209 maybe_warn_variadic_templates ();
21210
21211 /* Build a pack expansion type */
21212 if (template_parm_p)
21213 template_parameter_pack_p = true;
21214 else if (declarator)
21215 declarator->parameter_pack_p = true;
21216 else
21217 decl_specifiers.type = make_pack_expansion (type);
21218 }
21219 }
21220
21221 /* The restriction on defining new types applies only to the type
21222 of the parameter, not to the default argument. */
21223 parser->type_definition_forbidden_message = saved_message;
21224
21225 /* If the next token is `=', then process a default argument. */
21226 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21227 {
21228 tree type = decl_specifiers.type;
21229 token = cp_lexer_peek_token (parser->lexer);
21230 /* If we are defining a class, then the tokens that make up the
21231 default argument must be saved and processed later. */
21232 if (!template_parm_p && at_class_scope_p ()
21233 && TYPE_BEING_DEFINED (current_class_type)
21234 && !LAMBDA_TYPE_P (current_class_type))
21235 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21236
21237 // A constrained-type-specifier may declare a type template-parameter.
21238 else if (declares_constrained_type_template_parameter (type))
21239 default_argument
21240 = cp_parser_default_type_template_argument (parser);
21241
21242 // A constrained-type-specifier may declare a template-template-parameter.
21243 else if (declares_constrained_template_template_parameter (type))
21244 default_argument
21245 = cp_parser_default_template_template_argument (parser);
21246
21247 /* Outside of a class definition, we can just parse the
21248 assignment-expression. */
21249 else
21250 default_argument
21251 = cp_parser_default_argument (parser, template_parm_p);
21252
21253 if (!parser->default_arg_ok_p)
21254 {
21255 permerror (token->location,
21256 "default arguments are only "
21257 "permitted for function parameters");
21258 }
21259 else if ((declarator && declarator->parameter_pack_p)
21260 || template_parameter_pack_p
21261 || (decl_specifiers.type
21262 && PACK_EXPANSION_P (decl_specifiers.type)))
21263 {
21264 /* Find the name of the parameter pack. */
21265 cp_declarator *id_declarator = declarator;
21266 while (id_declarator && id_declarator->kind != cdk_id)
21267 id_declarator = id_declarator->declarator;
21268
21269 if (id_declarator && id_declarator->kind == cdk_id)
21270 error_at (declarator_token_start->location,
21271 template_parm_p
21272 ? G_("template parameter pack %qD "
21273 "cannot have a default argument")
21274 : G_("parameter pack %qD cannot have "
21275 "a default argument"),
21276 id_declarator->u.id.unqualified_name);
21277 else
21278 error_at (declarator_token_start->location,
21279 template_parm_p
21280 ? G_("template parameter pack cannot have "
21281 "a default argument")
21282 : G_("parameter pack cannot have a "
21283 "default argument"));
21284
21285 default_argument = NULL_TREE;
21286 }
21287 }
21288 else
21289 default_argument = NULL_TREE;
21290
21291 return make_parameter_declarator (&decl_specifiers,
21292 declarator,
21293 default_argument,
21294 template_parameter_pack_p);
21295 }
21296
21297 /* Parse a default argument and return it.
21298
21299 TEMPLATE_PARM_P is true if this is a default argument for a
21300 non-type template parameter. */
21301 static tree
21302 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21303 {
21304 tree default_argument = NULL_TREE;
21305 bool saved_greater_than_is_operator_p;
21306 bool saved_local_variables_forbidden_p;
21307 bool non_constant_p, is_direct_init;
21308
21309 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21310 set correctly. */
21311 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21312 parser->greater_than_is_operator_p = !template_parm_p;
21313 /* Local variable names (and the `this' keyword) may not
21314 appear in a default argument. */
21315 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21316 parser->local_variables_forbidden_p = true;
21317 /* Parse the assignment-expression. */
21318 if (template_parm_p)
21319 push_deferring_access_checks (dk_no_deferred);
21320 tree saved_class_ptr = NULL_TREE;
21321 tree saved_class_ref = NULL_TREE;
21322 /* The "this" pointer is not valid in a default argument. */
21323 if (cfun)
21324 {
21325 saved_class_ptr = current_class_ptr;
21326 cp_function_chain->x_current_class_ptr = NULL_TREE;
21327 saved_class_ref = current_class_ref;
21328 cp_function_chain->x_current_class_ref = NULL_TREE;
21329 }
21330 default_argument
21331 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21332 /* Restore the "this" pointer. */
21333 if (cfun)
21334 {
21335 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21336 cp_function_chain->x_current_class_ref = saved_class_ref;
21337 }
21338 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21339 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21340 if (template_parm_p)
21341 pop_deferring_access_checks ();
21342 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21343 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21344
21345 return default_argument;
21346 }
21347
21348 /* Parse a function-body.
21349
21350 function-body:
21351 compound_statement */
21352
21353 static void
21354 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21355 {
21356 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21357 ? BCS_TRY_BLOCK : BCS_NORMAL),
21358 true);
21359 }
21360
21361 /* Parse a ctor-initializer-opt followed by a function-body. Return
21362 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21363 is true we are parsing a function-try-block. */
21364
21365 static bool
21366 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21367 bool in_function_try_block)
21368 {
21369 tree body, list;
21370 bool ctor_initializer_p;
21371 const bool check_body_p =
21372 DECL_CONSTRUCTOR_P (current_function_decl)
21373 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21374 tree last = NULL;
21375
21376 /* Begin the function body. */
21377 body = begin_function_body ();
21378 /* Parse the optional ctor-initializer. */
21379 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21380
21381 /* If we're parsing a constexpr constructor definition, we need
21382 to check that the constructor body is indeed empty. However,
21383 before we get to cp_parser_function_body lot of junk has been
21384 generated, so we can't just check that we have an empty block.
21385 Rather we take a snapshot of the outermost block, and check whether
21386 cp_parser_function_body changed its state. */
21387 if (check_body_p)
21388 {
21389 list = cur_stmt_list;
21390 if (STATEMENT_LIST_TAIL (list))
21391 last = STATEMENT_LIST_TAIL (list)->stmt;
21392 }
21393 /* Parse the function-body. */
21394 cp_parser_function_body (parser, in_function_try_block);
21395 if (check_body_p)
21396 check_constexpr_ctor_body (last, list, /*complain=*/true);
21397 /* Finish the function body. */
21398 finish_function_body (body);
21399
21400 return ctor_initializer_p;
21401 }
21402
21403 /* Parse an initializer.
21404
21405 initializer:
21406 = initializer-clause
21407 ( expression-list )
21408
21409 Returns an expression representing the initializer. If no
21410 initializer is present, NULL_TREE is returned.
21411
21412 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21413 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21414 set to TRUE if there is no initializer present. If there is an
21415 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21416 is set to true; otherwise it is set to false. */
21417
21418 static tree
21419 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21420 bool* non_constant_p)
21421 {
21422 cp_token *token;
21423 tree init;
21424
21425 /* Peek at the next token. */
21426 token = cp_lexer_peek_token (parser->lexer);
21427
21428 /* Let our caller know whether or not this initializer was
21429 parenthesized. */
21430 *is_direct_init = (token->type != CPP_EQ);
21431 /* Assume that the initializer is constant. */
21432 *non_constant_p = false;
21433
21434 if (token->type == CPP_EQ)
21435 {
21436 /* Consume the `='. */
21437 cp_lexer_consume_token (parser->lexer);
21438 /* Parse the initializer-clause. */
21439 init = cp_parser_initializer_clause (parser, non_constant_p);
21440 }
21441 else if (token->type == CPP_OPEN_PAREN)
21442 {
21443 vec<tree, va_gc> *vec;
21444 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21445 /*cast_p=*/false,
21446 /*allow_expansion_p=*/true,
21447 non_constant_p);
21448 if (vec == NULL)
21449 return error_mark_node;
21450 init = build_tree_list_vec (vec);
21451 release_tree_vector (vec);
21452 }
21453 else if (token->type == CPP_OPEN_BRACE)
21454 {
21455 cp_lexer_set_source_position (parser->lexer);
21456 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21457 init = cp_parser_braced_list (parser, non_constant_p);
21458 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21459 }
21460 else
21461 {
21462 /* Anything else is an error. */
21463 cp_parser_error (parser, "expected initializer");
21464 init = error_mark_node;
21465 }
21466
21467 if (check_for_bare_parameter_packs (init))
21468 init = error_mark_node;
21469
21470 return init;
21471 }
21472
21473 /* Parse an initializer-clause.
21474
21475 initializer-clause:
21476 assignment-expression
21477 braced-init-list
21478
21479 Returns an expression representing the initializer.
21480
21481 If the `assignment-expression' production is used the value
21482 returned is simply a representation for the expression.
21483
21484 Otherwise, calls cp_parser_braced_list. */
21485
21486 static cp_expr
21487 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21488 {
21489 cp_expr initializer;
21490
21491 /* Assume the expression is constant. */
21492 *non_constant_p = false;
21493
21494 /* If it is not a `{', then we are looking at an
21495 assignment-expression. */
21496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21497 {
21498 initializer
21499 = cp_parser_constant_expression (parser,
21500 /*allow_non_constant_p=*/true,
21501 non_constant_p);
21502 }
21503 else
21504 initializer = cp_parser_braced_list (parser, non_constant_p);
21505
21506 return initializer;
21507 }
21508
21509 /* Parse a brace-enclosed initializer list.
21510
21511 braced-init-list:
21512 { initializer-list , [opt] }
21513 { }
21514
21515 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21516 the elements of the initializer-list (or NULL, if the last
21517 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21518 NULL_TREE. There is no way to detect whether or not the optional
21519 trailing `,' was provided. NON_CONSTANT_P is as for
21520 cp_parser_initializer. */
21521
21522 static cp_expr
21523 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21524 {
21525 tree initializer;
21526 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21527
21528 /* Consume the `{' token. */
21529 cp_lexer_consume_token (parser->lexer);
21530 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21531 initializer = make_node (CONSTRUCTOR);
21532 /* If it's not a `}', then there is a non-trivial initializer. */
21533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21534 {
21535 /* Parse the initializer list. */
21536 CONSTRUCTOR_ELTS (initializer)
21537 = cp_parser_initializer_list (parser, non_constant_p);
21538 /* A trailing `,' token is allowed. */
21539 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21540 cp_lexer_consume_token (parser->lexer);
21541 }
21542 else
21543 *non_constant_p = false;
21544 /* Now, there should be a trailing `}'. */
21545 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21546 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21547 TREE_TYPE (initializer) = init_list_type_node;
21548
21549 cp_expr result (initializer);
21550 /* Build a location of the form:
21551 { ... }
21552 ^~~~~~~
21553 with caret==start at the open brace, finish at the close brace. */
21554 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21555 result.set_location (combined_loc);
21556 return result;
21557 }
21558
21559 /* Consume tokens up to, and including, the next non-nested closing `]'.
21560 Returns true iff we found a closing `]'. */
21561
21562 static bool
21563 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21564 {
21565 unsigned square_depth = 0;
21566
21567 while (true)
21568 {
21569 cp_token * token = cp_lexer_peek_token (parser->lexer);
21570
21571 switch (token->type)
21572 {
21573 case CPP_EOF:
21574 case CPP_PRAGMA_EOL:
21575 /* If we've run out of tokens, then there is no closing `]'. */
21576 return false;
21577
21578 case CPP_OPEN_SQUARE:
21579 ++square_depth;
21580 break;
21581
21582 case CPP_CLOSE_SQUARE:
21583 if (!square_depth--)
21584 {
21585 cp_lexer_consume_token (parser->lexer);
21586 return true;
21587 }
21588 break;
21589
21590 default:
21591 break;
21592 }
21593
21594 /* Consume the token. */
21595 cp_lexer_consume_token (parser->lexer);
21596 }
21597 }
21598
21599 /* Return true if we are looking at an array-designator, false otherwise. */
21600
21601 static bool
21602 cp_parser_array_designator_p (cp_parser *parser)
21603 {
21604 /* Consume the `['. */
21605 cp_lexer_consume_token (parser->lexer);
21606
21607 cp_lexer_save_tokens (parser->lexer);
21608
21609 /* Skip tokens until the next token is a closing square bracket.
21610 If we find the closing `]', and the next token is a `=', then
21611 we are looking at an array designator. */
21612 bool array_designator_p
21613 = (cp_parser_skip_to_closing_square_bracket (parser)
21614 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21615
21616 /* Roll back the tokens we skipped. */
21617 cp_lexer_rollback_tokens (parser->lexer);
21618
21619 return array_designator_p;
21620 }
21621
21622 /* Parse an initializer-list.
21623
21624 initializer-list:
21625 initializer-clause ... [opt]
21626 initializer-list , initializer-clause ... [opt]
21627
21628 GNU Extension:
21629
21630 initializer-list:
21631 designation initializer-clause ...[opt]
21632 initializer-list , designation initializer-clause ...[opt]
21633
21634 designation:
21635 . identifier =
21636 identifier :
21637 [ constant-expression ] =
21638
21639 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21640 for the initializer. If the INDEX of the elt is non-NULL, it is the
21641 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21642 as for cp_parser_initializer. */
21643
21644 static vec<constructor_elt, va_gc> *
21645 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21646 {
21647 vec<constructor_elt, va_gc> *v = NULL;
21648
21649 /* Assume all of the expressions are constant. */
21650 *non_constant_p = false;
21651
21652 /* Parse the rest of the list. */
21653 while (true)
21654 {
21655 cp_token *token;
21656 tree designator;
21657 tree initializer;
21658 bool clause_non_constant_p;
21659
21660 /* If the next token is an identifier and the following one is a
21661 colon, we are looking at the GNU designated-initializer
21662 syntax. */
21663 if (cp_parser_allow_gnu_extensions_p (parser)
21664 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21665 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21666 {
21667 /* Warn the user that they are using an extension. */
21668 pedwarn (input_location, OPT_Wpedantic,
21669 "ISO C++ does not allow designated initializers");
21670 /* Consume the identifier. */
21671 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21672 /* Consume the `:'. */
21673 cp_lexer_consume_token (parser->lexer);
21674 }
21675 /* Also handle the C99 syntax, '. id ='. */
21676 else if (cp_parser_allow_gnu_extensions_p (parser)
21677 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21678 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21679 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21680 {
21681 /* Warn the user that they are using an extension. */
21682 pedwarn (input_location, OPT_Wpedantic,
21683 "ISO C++ does not allow C99 designated initializers");
21684 /* Consume the `.'. */
21685 cp_lexer_consume_token (parser->lexer);
21686 /* Consume the identifier. */
21687 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21688 /* Consume the `='. */
21689 cp_lexer_consume_token (parser->lexer);
21690 }
21691 /* Also handle C99 array designators, '[ const ] ='. */
21692 else if (cp_parser_allow_gnu_extensions_p (parser)
21693 && !c_dialect_objc ()
21694 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21695 {
21696 /* In C++11, [ could start a lambda-introducer. */
21697 bool non_const = false;
21698
21699 cp_parser_parse_tentatively (parser);
21700
21701 if (!cp_parser_array_designator_p (parser))
21702 {
21703 cp_parser_simulate_error (parser);
21704 designator = NULL_TREE;
21705 }
21706 else
21707 {
21708 designator = cp_parser_constant_expression (parser, true,
21709 &non_const);
21710 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21711 cp_parser_require (parser, CPP_EQ, RT_EQ);
21712 }
21713
21714 if (!cp_parser_parse_definitely (parser))
21715 designator = NULL_TREE;
21716 else if (non_const)
21717 require_potential_rvalue_constant_expression (designator);
21718 }
21719 else
21720 designator = NULL_TREE;
21721
21722 /* Parse the initializer. */
21723 initializer = cp_parser_initializer_clause (parser,
21724 &clause_non_constant_p);
21725 /* If any clause is non-constant, so is the entire initializer. */
21726 if (clause_non_constant_p)
21727 *non_constant_p = true;
21728
21729 /* If we have an ellipsis, this is an initializer pack
21730 expansion. */
21731 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21732 {
21733 /* Consume the `...'. */
21734 cp_lexer_consume_token (parser->lexer);
21735
21736 /* Turn the initializer into an initializer expansion. */
21737 initializer = make_pack_expansion (initializer);
21738 }
21739
21740 /* Add it to the vector. */
21741 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21742
21743 /* If the next token is not a comma, we have reached the end of
21744 the list. */
21745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21746 break;
21747
21748 /* Peek at the next token. */
21749 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21750 /* If the next token is a `}', then we're still done. An
21751 initializer-clause can have a trailing `,' after the
21752 initializer-list and before the closing `}'. */
21753 if (token->type == CPP_CLOSE_BRACE)
21754 break;
21755
21756 /* Consume the `,' token. */
21757 cp_lexer_consume_token (parser->lexer);
21758 }
21759
21760 return v;
21761 }
21762
21763 /* Classes [gram.class] */
21764
21765 /* Parse a class-name.
21766
21767 class-name:
21768 identifier
21769 template-id
21770
21771 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21772 to indicate that names looked up in dependent types should be
21773 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21774 keyword has been used to indicate that the name that appears next
21775 is a template. TAG_TYPE indicates the explicit tag given before
21776 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21777 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21778 is the class being defined in a class-head. If ENUM_OK is TRUE,
21779 enum-names are also accepted.
21780
21781 Returns the TYPE_DECL representing the class. */
21782
21783 static tree
21784 cp_parser_class_name (cp_parser *parser,
21785 bool typename_keyword_p,
21786 bool template_keyword_p,
21787 enum tag_types tag_type,
21788 bool check_dependency_p,
21789 bool class_head_p,
21790 bool is_declaration,
21791 bool enum_ok)
21792 {
21793 tree decl;
21794 tree scope;
21795 bool typename_p;
21796 cp_token *token;
21797 tree identifier = NULL_TREE;
21798
21799 /* All class-names start with an identifier. */
21800 token = cp_lexer_peek_token (parser->lexer);
21801 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21802 {
21803 cp_parser_error (parser, "expected class-name");
21804 return error_mark_node;
21805 }
21806
21807 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21808 to a template-id, so we save it here. */
21809 scope = parser->scope;
21810 if (scope == error_mark_node)
21811 return error_mark_node;
21812
21813 /* Any name names a type if we're following the `typename' keyword
21814 in a qualified name where the enclosing scope is type-dependent. */
21815 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21816 && dependent_type_p (scope));
21817 /* Handle the common case (an identifier, but not a template-id)
21818 efficiently. */
21819 if (token->type == CPP_NAME
21820 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21821 {
21822 cp_token *identifier_token;
21823 bool ambiguous_p;
21824
21825 /* Look for the identifier. */
21826 identifier_token = cp_lexer_peek_token (parser->lexer);
21827 ambiguous_p = identifier_token->error_reported;
21828 identifier = cp_parser_identifier (parser);
21829 /* If the next token isn't an identifier, we are certainly not
21830 looking at a class-name. */
21831 if (identifier == error_mark_node)
21832 decl = error_mark_node;
21833 /* If we know this is a type-name, there's no need to look it
21834 up. */
21835 else if (typename_p)
21836 decl = identifier;
21837 else
21838 {
21839 tree ambiguous_decls;
21840 /* If we already know that this lookup is ambiguous, then
21841 we've already issued an error message; there's no reason
21842 to check again. */
21843 if (ambiguous_p)
21844 {
21845 cp_parser_simulate_error (parser);
21846 return error_mark_node;
21847 }
21848 /* If the next token is a `::', then the name must be a type
21849 name.
21850
21851 [basic.lookup.qual]
21852
21853 During the lookup for a name preceding the :: scope
21854 resolution operator, object, function, and enumerator
21855 names are ignored. */
21856 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21857 tag_type = scope_type;
21858 /* Look up the name. */
21859 decl = cp_parser_lookup_name (parser, identifier,
21860 tag_type,
21861 /*is_template=*/false,
21862 /*is_namespace=*/false,
21863 check_dependency_p,
21864 &ambiguous_decls,
21865 identifier_token->location);
21866 if (ambiguous_decls)
21867 {
21868 if (cp_parser_parsing_tentatively (parser))
21869 cp_parser_simulate_error (parser);
21870 return error_mark_node;
21871 }
21872 }
21873 }
21874 else
21875 {
21876 /* Try a template-id. */
21877 decl = cp_parser_template_id (parser, template_keyword_p,
21878 check_dependency_p,
21879 tag_type,
21880 is_declaration);
21881 if (decl == error_mark_node)
21882 return error_mark_node;
21883 }
21884
21885 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21886
21887 /* If this is a typename, create a TYPENAME_TYPE. */
21888 if (typename_p && decl != error_mark_node)
21889 {
21890 decl = make_typename_type (scope, decl, typename_type,
21891 /*complain=*/tf_error);
21892 if (decl != error_mark_node)
21893 decl = TYPE_NAME (decl);
21894 }
21895
21896 decl = strip_using_decl (decl);
21897
21898 /* Check to see that it is really the name of a class. */
21899 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21900 && identifier_p (TREE_OPERAND (decl, 0))
21901 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21902 /* Situations like this:
21903
21904 template <typename T> struct A {
21905 typename T::template X<int>::I i;
21906 };
21907
21908 are problematic. Is `T::template X<int>' a class-name? The
21909 standard does not seem to be definitive, but there is no other
21910 valid interpretation of the following `::'. Therefore, those
21911 names are considered class-names. */
21912 {
21913 decl = make_typename_type (scope, decl, tag_type, tf_error);
21914 if (decl != error_mark_node)
21915 decl = TYPE_NAME (decl);
21916 }
21917 else if (TREE_CODE (decl) != TYPE_DECL
21918 || TREE_TYPE (decl) == error_mark_node
21919 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21920 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21921 /* In Objective-C 2.0, a classname followed by '.' starts a
21922 dot-syntax expression, and it's not a type-name. */
21923 || (c_dialect_objc ()
21924 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21925 && objc_is_class_name (decl)))
21926 decl = error_mark_node;
21927
21928 if (decl == error_mark_node)
21929 cp_parser_error (parser, "expected class-name");
21930 else if (identifier && !parser->scope)
21931 maybe_note_name_used_in_class (identifier, decl);
21932
21933 return decl;
21934 }
21935
21936 /* Parse a class-specifier.
21937
21938 class-specifier:
21939 class-head { member-specification [opt] }
21940
21941 Returns the TREE_TYPE representing the class. */
21942
21943 static tree
21944 cp_parser_class_specifier_1 (cp_parser* parser)
21945 {
21946 tree type;
21947 tree attributes = NULL_TREE;
21948 bool nested_name_specifier_p;
21949 unsigned saved_num_template_parameter_lists;
21950 bool saved_in_function_body;
21951 unsigned char in_statement;
21952 bool in_switch_statement_p;
21953 bool saved_in_unbraced_linkage_specification_p;
21954 tree old_scope = NULL_TREE;
21955 tree scope = NULL_TREE;
21956 cp_token *closing_brace;
21957
21958 push_deferring_access_checks (dk_no_deferred);
21959
21960 /* Parse the class-head. */
21961 type = cp_parser_class_head (parser,
21962 &nested_name_specifier_p);
21963 /* If the class-head was a semantic disaster, skip the entire body
21964 of the class. */
21965 if (!type)
21966 {
21967 cp_parser_skip_to_end_of_block_or_statement (parser);
21968 pop_deferring_access_checks ();
21969 return error_mark_node;
21970 }
21971
21972 /* Look for the `{'. */
21973 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21974 {
21975 pop_deferring_access_checks ();
21976 return error_mark_node;
21977 }
21978
21979 cp_ensure_no_omp_declare_simd (parser);
21980 cp_ensure_no_oacc_routine (parser);
21981
21982 /* Issue an error message if type-definitions are forbidden here. */
21983 cp_parser_check_type_definition (parser);
21984 /* Remember that we are defining one more class. */
21985 ++parser->num_classes_being_defined;
21986 /* Inside the class, surrounding template-parameter-lists do not
21987 apply. */
21988 saved_num_template_parameter_lists
21989 = parser->num_template_parameter_lists;
21990 parser->num_template_parameter_lists = 0;
21991 /* We are not in a function body. */
21992 saved_in_function_body = parser->in_function_body;
21993 parser->in_function_body = false;
21994 /* Or in a loop. */
21995 in_statement = parser->in_statement;
21996 parser->in_statement = 0;
21997 /* Or in a switch. */
21998 in_switch_statement_p = parser->in_switch_statement_p;
21999 parser->in_switch_statement_p = false;
22000 /* We are not immediately inside an extern "lang" block. */
22001 saved_in_unbraced_linkage_specification_p
22002 = parser->in_unbraced_linkage_specification_p;
22003 parser->in_unbraced_linkage_specification_p = false;
22004
22005 // Associate constraints with the type.
22006 if (flag_concepts)
22007 type = associate_classtype_constraints (type);
22008
22009 /* Start the class. */
22010 if (nested_name_specifier_p)
22011 {
22012 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22013 old_scope = push_inner_scope (scope);
22014 }
22015 type = begin_class_definition (type);
22016
22017 if (type == error_mark_node)
22018 /* If the type is erroneous, skip the entire body of the class. */
22019 cp_parser_skip_to_closing_brace (parser);
22020 else
22021 /* Parse the member-specification. */
22022 cp_parser_member_specification_opt (parser);
22023
22024 /* Look for the trailing `}'. */
22025 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22026 /* Look for trailing attributes to apply to this class. */
22027 if (cp_parser_allow_gnu_extensions_p (parser))
22028 attributes = cp_parser_gnu_attributes_opt (parser);
22029 if (type != error_mark_node)
22030 type = finish_struct (type, attributes);
22031 if (nested_name_specifier_p)
22032 pop_inner_scope (old_scope, scope);
22033
22034 /* We've finished a type definition. Check for the common syntax
22035 error of forgetting a semicolon after the definition. We need to
22036 be careful, as we can't just check for not-a-semicolon and be done
22037 with it; the user might have typed:
22038
22039 class X { } c = ...;
22040 class X { } *p = ...;
22041
22042 and so forth. Instead, enumerate all the possible tokens that
22043 might follow this production; if we don't see one of them, then
22044 complain and silently insert the semicolon. */
22045 {
22046 cp_token *token = cp_lexer_peek_token (parser->lexer);
22047 bool want_semicolon = true;
22048
22049 if (cp_next_tokens_can_be_std_attribute_p (parser))
22050 /* Don't try to parse c++11 attributes here. As per the
22051 grammar, that should be a task for
22052 cp_parser_decl_specifier_seq. */
22053 want_semicolon = false;
22054
22055 switch (token->type)
22056 {
22057 case CPP_NAME:
22058 case CPP_SEMICOLON:
22059 case CPP_MULT:
22060 case CPP_AND:
22061 case CPP_OPEN_PAREN:
22062 case CPP_CLOSE_PAREN:
22063 case CPP_COMMA:
22064 want_semicolon = false;
22065 break;
22066
22067 /* While it's legal for type qualifiers and storage class
22068 specifiers to follow type definitions in the grammar, only
22069 compiler testsuites contain code like that. Assume that if
22070 we see such code, then what we're really seeing is a case
22071 like:
22072
22073 class X { }
22074 const <type> var = ...;
22075
22076 or
22077
22078 class Y { }
22079 static <type> func (...) ...
22080
22081 i.e. the qualifier or specifier applies to the next
22082 declaration. To do so, however, we need to look ahead one
22083 more token to see if *that* token is a type specifier.
22084
22085 This code could be improved to handle:
22086
22087 class Z { }
22088 static const <type> var = ...; */
22089 case CPP_KEYWORD:
22090 if (keyword_is_decl_specifier (token->keyword))
22091 {
22092 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22093
22094 /* Handling user-defined types here would be nice, but very
22095 tricky. */
22096 want_semicolon
22097 = (lookahead->type == CPP_KEYWORD
22098 && keyword_begins_type_specifier (lookahead->keyword));
22099 }
22100 break;
22101 default:
22102 break;
22103 }
22104
22105 /* If we don't have a type, then something is very wrong and we
22106 shouldn't try to do anything clever. Likewise for not seeing the
22107 closing brace. */
22108 if (closing_brace && TYPE_P (type) && want_semicolon)
22109 {
22110 /* Locate the closing brace. */
22111 cp_token_position prev
22112 = cp_lexer_previous_token_position (parser->lexer);
22113 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22114 location_t loc = prev_token->location;
22115
22116 /* We want to suggest insertion of a ';' immediately *after* the
22117 closing brace, so, if we can, offset the location by 1 column. */
22118 location_t next_loc = loc;
22119 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22120 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22121
22122 rich_location richloc (line_table, next_loc);
22123 richloc.add_fixit_insert_before (next_loc, ";");
22124
22125 if (CLASSTYPE_DECLARED_CLASS (type))
22126 error_at_rich_loc (&richloc,
22127 "expected %<;%> after class definition");
22128 else if (TREE_CODE (type) == RECORD_TYPE)
22129 error_at_rich_loc (&richloc,
22130 "expected %<;%> after struct definition");
22131 else if (TREE_CODE (type) == UNION_TYPE)
22132 error_at_rich_loc (&richloc,
22133 "expected %<;%> after union definition");
22134 else
22135 gcc_unreachable ();
22136
22137 /* Unget one token and smash it to look as though we encountered
22138 a semicolon in the input stream. */
22139 cp_lexer_set_token_position (parser->lexer, prev);
22140 token = cp_lexer_peek_token (parser->lexer);
22141 token->type = CPP_SEMICOLON;
22142 token->keyword = RID_MAX;
22143 }
22144 }
22145
22146 /* If this class is not itself within the scope of another class,
22147 then we need to parse the bodies of all of the queued function
22148 definitions. Note that the queued functions defined in a class
22149 are not always processed immediately following the
22150 class-specifier for that class. Consider:
22151
22152 struct A {
22153 struct B { void f() { sizeof (A); } };
22154 };
22155
22156 If `f' were processed before the processing of `A' were
22157 completed, there would be no way to compute the size of `A'.
22158 Note that the nesting we are interested in here is lexical --
22159 not the semantic nesting given by TYPE_CONTEXT. In particular,
22160 for:
22161
22162 struct A { struct B; };
22163 struct A::B { void f() { } };
22164
22165 there is no need to delay the parsing of `A::B::f'. */
22166 if (--parser->num_classes_being_defined == 0)
22167 {
22168 tree decl;
22169 tree class_type = NULL_TREE;
22170 tree pushed_scope = NULL_TREE;
22171 unsigned ix;
22172 cp_default_arg_entry *e;
22173 tree save_ccp, save_ccr;
22174
22175 /* In a first pass, parse default arguments to the functions.
22176 Then, in a second pass, parse the bodies of the functions.
22177 This two-phased approach handles cases like:
22178
22179 struct S {
22180 void f() { g(); }
22181 void g(int i = 3);
22182 };
22183
22184 */
22185 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22186 {
22187 decl = e->decl;
22188 /* If there are default arguments that have not yet been processed,
22189 take care of them now. */
22190 if (class_type != e->class_type)
22191 {
22192 if (pushed_scope)
22193 pop_scope (pushed_scope);
22194 class_type = e->class_type;
22195 pushed_scope = push_scope (class_type);
22196 }
22197 /* Make sure that any template parameters are in scope. */
22198 maybe_begin_member_template_processing (decl);
22199 /* Parse the default argument expressions. */
22200 cp_parser_late_parsing_default_args (parser, decl);
22201 /* Remove any template parameters from the symbol table. */
22202 maybe_end_member_template_processing ();
22203 }
22204 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22205 /* Now parse any NSDMIs. */
22206 save_ccp = current_class_ptr;
22207 save_ccr = current_class_ref;
22208 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22209 {
22210 if (class_type != DECL_CONTEXT (decl))
22211 {
22212 if (pushed_scope)
22213 pop_scope (pushed_scope);
22214 class_type = DECL_CONTEXT (decl);
22215 pushed_scope = push_scope (class_type);
22216 }
22217 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22218 cp_parser_late_parsing_nsdmi (parser, decl);
22219 }
22220 vec_safe_truncate (unparsed_nsdmis, 0);
22221 current_class_ptr = save_ccp;
22222 current_class_ref = save_ccr;
22223 if (pushed_scope)
22224 pop_scope (pushed_scope);
22225
22226 /* Now do some post-NSDMI bookkeeping. */
22227 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22228 after_nsdmi_defaulted_late_checks (class_type);
22229 vec_safe_truncate (unparsed_classes, 0);
22230 after_nsdmi_defaulted_late_checks (type);
22231
22232 /* Now parse the body of the functions. */
22233 if (flag_openmp)
22234 {
22235 /* OpenMP UDRs need to be parsed before all other functions. */
22236 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22237 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22238 cp_parser_late_parsing_for_member (parser, decl);
22239 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22240 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22241 cp_parser_late_parsing_for_member (parser, decl);
22242 }
22243 else
22244 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22245 cp_parser_late_parsing_for_member (parser, decl);
22246 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22247 }
22248 else
22249 vec_safe_push (unparsed_classes, type);
22250
22251 /* Put back any saved access checks. */
22252 pop_deferring_access_checks ();
22253
22254 /* Restore saved state. */
22255 parser->in_switch_statement_p = in_switch_statement_p;
22256 parser->in_statement = in_statement;
22257 parser->in_function_body = saved_in_function_body;
22258 parser->num_template_parameter_lists
22259 = saved_num_template_parameter_lists;
22260 parser->in_unbraced_linkage_specification_p
22261 = saved_in_unbraced_linkage_specification_p;
22262
22263 return type;
22264 }
22265
22266 static tree
22267 cp_parser_class_specifier (cp_parser* parser)
22268 {
22269 tree ret;
22270 timevar_push (TV_PARSE_STRUCT);
22271 ret = cp_parser_class_specifier_1 (parser);
22272 timevar_pop (TV_PARSE_STRUCT);
22273 return ret;
22274 }
22275
22276 /* Parse a class-head.
22277
22278 class-head:
22279 class-key identifier [opt] base-clause [opt]
22280 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22281 class-key nested-name-specifier [opt] template-id
22282 base-clause [opt]
22283
22284 class-virt-specifier:
22285 final
22286
22287 GNU Extensions:
22288 class-key attributes identifier [opt] base-clause [opt]
22289 class-key attributes nested-name-specifier identifier base-clause [opt]
22290 class-key attributes nested-name-specifier [opt] template-id
22291 base-clause [opt]
22292
22293 Upon return BASES is initialized to the list of base classes (or
22294 NULL, if there are none) in the same form returned by
22295 cp_parser_base_clause.
22296
22297 Returns the TYPE of the indicated class. Sets
22298 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22299 involving a nested-name-specifier was used, and FALSE otherwise.
22300
22301 Returns error_mark_node if this is not a class-head.
22302
22303 Returns NULL_TREE if the class-head is syntactically valid, but
22304 semantically invalid in a way that means we should skip the entire
22305 body of the class. */
22306
22307 static tree
22308 cp_parser_class_head (cp_parser* parser,
22309 bool* nested_name_specifier_p)
22310 {
22311 tree nested_name_specifier;
22312 enum tag_types class_key;
22313 tree id = NULL_TREE;
22314 tree type = NULL_TREE;
22315 tree attributes;
22316 tree bases;
22317 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22318 bool template_id_p = false;
22319 bool qualified_p = false;
22320 bool invalid_nested_name_p = false;
22321 bool invalid_explicit_specialization_p = false;
22322 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22323 tree pushed_scope = NULL_TREE;
22324 unsigned num_templates;
22325 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22326 /* Assume no nested-name-specifier will be present. */
22327 *nested_name_specifier_p = false;
22328 /* Assume no template parameter lists will be used in defining the
22329 type. */
22330 num_templates = 0;
22331 parser->colon_corrects_to_scope_p = false;
22332
22333 /* Look for the class-key. */
22334 class_key = cp_parser_class_key (parser);
22335 if (class_key == none_type)
22336 return error_mark_node;
22337
22338 location_t class_head_start_location = input_location;
22339
22340 /* Parse the attributes. */
22341 attributes = cp_parser_attributes_opt (parser);
22342
22343 /* If the next token is `::', that is invalid -- but sometimes
22344 people do try to write:
22345
22346 struct ::S {};
22347
22348 Handle this gracefully by accepting the extra qualifier, and then
22349 issuing an error about it later if this really is a
22350 class-head. If it turns out just to be an elaborated type
22351 specifier, remain silent. */
22352 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22353 qualified_p = true;
22354
22355 push_deferring_access_checks (dk_no_check);
22356
22357 /* Determine the name of the class. Begin by looking for an
22358 optional nested-name-specifier. */
22359 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22360 nested_name_specifier
22361 = cp_parser_nested_name_specifier_opt (parser,
22362 /*typename_keyword_p=*/false,
22363 /*check_dependency_p=*/false,
22364 /*type_p=*/true,
22365 /*is_declaration=*/false);
22366 /* If there was a nested-name-specifier, then there *must* be an
22367 identifier. */
22368 if (nested_name_specifier)
22369 {
22370 type_start_token = cp_lexer_peek_token (parser->lexer);
22371 /* Although the grammar says `identifier', it really means
22372 `class-name' or `template-name'. You are only allowed to
22373 define a class that has already been declared with this
22374 syntax.
22375
22376 The proposed resolution for Core Issue 180 says that wherever
22377 you see `class T::X' you should treat `X' as a type-name.
22378
22379 It is OK to define an inaccessible class; for example:
22380
22381 class A { class B; };
22382 class A::B {};
22383
22384 We do not know if we will see a class-name, or a
22385 template-name. We look for a class-name first, in case the
22386 class-name is a template-id; if we looked for the
22387 template-name first we would stop after the template-name. */
22388 cp_parser_parse_tentatively (parser);
22389 type = cp_parser_class_name (parser,
22390 /*typename_keyword_p=*/false,
22391 /*template_keyword_p=*/false,
22392 class_type,
22393 /*check_dependency_p=*/false,
22394 /*class_head_p=*/true,
22395 /*is_declaration=*/false);
22396 /* If that didn't work, ignore the nested-name-specifier. */
22397 if (!cp_parser_parse_definitely (parser))
22398 {
22399 invalid_nested_name_p = true;
22400 type_start_token = cp_lexer_peek_token (parser->lexer);
22401 id = cp_parser_identifier (parser);
22402 if (id == error_mark_node)
22403 id = NULL_TREE;
22404 }
22405 /* If we could not find a corresponding TYPE, treat this
22406 declaration like an unqualified declaration. */
22407 if (type == error_mark_node)
22408 nested_name_specifier = NULL_TREE;
22409 /* Otherwise, count the number of templates used in TYPE and its
22410 containing scopes. */
22411 else
22412 {
22413 tree scope;
22414
22415 for (scope = TREE_TYPE (type);
22416 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22417 scope = get_containing_scope (scope))
22418 if (TYPE_P (scope)
22419 && CLASS_TYPE_P (scope)
22420 && CLASSTYPE_TEMPLATE_INFO (scope)
22421 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22422 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22423 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22424 ++num_templates;
22425 }
22426 }
22427 /* Otherwise, the identifier is optional. */
22428 else
22429 {
22430 /* We don't know whether what comes next is a template-id,
22431 an identifier, or nothing at all. */
22432 cp_parser_parse_tentatively (parser);
22433 /* Check for a template-id. */
22434 type_start_token = cp_lexer_peek_token (parser->lexer);
22435 id = cp_parser_template_id (parser,
22436 /*template_keyword_p=*/false,
22437 /*check_dependency_p=*/true,
22438 class_key,
22439 /*is_declaration=*/true);
22440 /* If that didn't work, it could still be an identifier. */
22441 if (!cp_parser_parse_definitely (parser))
22442 {
22443 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22444 {
22445 type_start_token = cp_lexer_peek_token (parser->lexer);
22446 id = cp_parser_identifier (parser);
22447 }
22448 else
22449 id = NULL_TREE;
22450 }
22451 else
22452 {
22453 template_id_p = true;
22454 ++num_templates;
22455 }
22456 }
22457
22458 pop_deferring_access_checks ();
22459
22460 if (id)
22461 {
22462 cp_parser_check_for_invalid_template_id (parser, id,
22463 class_key,
22464 type_start_token->location);
22465 }
22466 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22467
22468 /* If it's not a `:' or a `{' then we can't really be looking at a
22469 class-head, since a class-head only appears as part of a
22470 class-specifier. We have to detect this situation before calling
22471 xref_tag, since that has irreversible side-effects. */
22472 if (!cp_parser_next_token_starts_class_definition_p (parser))
22473 {
22474 cp_parser_error (parser, "expected %<{%> or %<:%>");
22475 type = error_mark_node;
22476 goto out;
22477 }
22478
22479 /* At this point, we're going ahead with the class-specifier, even
22480 if some other problem occurs. */
22481 cp_parser_commit_to_tentative_parse (parser);
22482 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22483 {
22484 cp_parser_error (parser,
22485 "cannot specify %<override%> for a class");
22486 type = error_mark_node;
22487 goto out;
22488 }
22489 /* Issue the error about the overly-qualified name now. */
22490 if (qualified_p)
22491 {
22492 cp_parser_error (parser,
22493 "global qualification of class name is invalid");
22494 type = error_mark_node;
22495 goto out;
22496 }
22497 else if (invalid_nested_name_p)
22498 {
22499 cp_parser_error (parser,
22500 "qualified name does not name a class");
22501 type = error_mark_node;
22502 goto out;
22503 }
22504 else if (nested_name_specifier)
22505 {
22506 tree scope;
22507
22508 /* Reject typedef-names in class heads. */
22509 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22510 {
22511 error_at (type_start_token->location,
22512 "invalid class name in declaration of %qD",
22513 type);
22514 type = NULL_TREE;
22515 goto done;
22516 }
22517
22518 /* Figure out in what scope the declaration is being placed. */
22519 scope = current_scope ();
22520 /* If that scope does not contain the scope in which the
22521 class was originally declared, the program is invalid. */
22522 if (scope && !is_ancestor (scope, nested_name_specifier))
22523 {
22524 if (at_namespace_scope_p ())
22525 error_at (type_start_token->location,
22526 "declaration of %qD in namespace %qD which does not "
22527 "enclose %qD",
22528 type, scope, nested_name_specifier);
22529 else
22530 error_at (type_start_token->location,
22531 "declaration of %qD in %qD which does not enclose %qD",
22532 type, scope, nested_name_specifier);
22533 type = NULL_TREE;
22534 goto done;
22535 }
22536 /* [dcl.meaning]
22537
22538 A declarator-id shall not be qualified except for the
22539 definition of a ... nested class outside of its class
22540 ... [or] the definition or explicit instantiation of a
22541 class member of a namespace outside of its namespace. */
22542 if (scope == nested_name_specifier)
22543 {
22544 permerror (nested_name_specifier_token_start->location,
22545 "extra qualification not allowed");
22546 nested_name_specifier = NULL_TREE;
22547 num_templates = 0;
22548 }
22549 }
22550 /* An explicit-specialization must be preceded by "template <>". If
22551 it is not, try to recover gracefully. */
22552 if (at_namespace_scope_p ()
22553 && parser->num_template_parameter_lists == 0
22554 && !processing_template_parmlist
22555 && template_id_p)
22556 {
22557 /* Build a location of this form:
22558 struct typename <ARGS>
22559 ^~~~~~~~~~~~~~~~~~~~~~
22560 with caret==start at the start token, and
22561 finishing at the end of the type. */
22562 location_t reported_loc
22563 = make_location (class_head_start_location,
22564 class_head_start_location,
22565 get_finish (type_start_token->location));
22566 rich_location richloc (line_table, reported_loc);
22567 richloc.add_fixit_insert_before (class_head_start_location,
22568 "template <> ");
22569 error_at_rich_loc
22570 (&richloc,
22571 "an explicit specialization must be preceded by %<template <>%>");
22572 invalid_explicit_specialization_p = true;
22573 /* Take the same action that would have been taken by
22574 cp_parser_explicit_specialization. */
22575 ++parser->num_template_parameter_lists;
22576 begin_specialization ();
22577 }
22578 /* There must be no "return" statements between this point and the
22579 end of this function; set "type "to the correct return value and
22580 use "goto done;" to return. */
22581 /* Make sure that the right number of template parameters were
22582 present. */
22583 if (!cp_parser_check_template_parameters (parser, num_templates,
22584 type_start_token->location,
22585 /*declarator=*/NULL))
22586 {
22587 /* If something went wrong, there is no point in even trying to
22588 process the class-definition. */
22589 type = NULL_TREE;
22590 goto done;
22591 }
22592
22593 /* Look up the type. */
22594 if (template_id_p)
22595 {
22596 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22597 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22598 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22599 {
22600 error_at (type_start_token->location,
22601 "function template %qD redeclared as a class template", id);
22602 type = error_mark_node;
22603 }
22604 else
22605 {
22606 type = TREE_TYPE (id);
22607 type = maybe_process_partial_specialization (type);
22608
22609 /* Check the scope while we still know whether or not we had a
22610 nested-name-specifier. */
22611 if (type != error_mark_node)
22612 check_unqualified_spec_or_inst (type, type_start_token->location);
22613 }
22614 if (nested_name_specifier)
22615 pushed_scope = push_scope (nested_name_specifier);
22616 }
22617 else if (nested_name_specifier)
22618 {
22619 tree class_type;
22620
22621 /* Given:
22622
22623 template <typename T> struct S { struct T };
22624 template <typename T> struct S<T>::T { };
22625
22626 we will get a TYPENAME_TYPE when processing the definition of
22627 `S::T'. We need to resolve it to the actual type before we
22628 try to define it. */
22629 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22630 {
22631 class_type = resolve_typename_type (TREE_TYPE (type),
22632 /*only_current_p=*/false);
22633 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22634 type = TYPE_NAME (class_type);
22635 else
22636 {
22637 cp_parser_error (parser, "could not resolve typename type");
22638 type = error_mark_node;
22639 }
22640 }
22641
22642 if (maybe_process_partial_specialization (TREE_TYPE (type))
22643 == error_mark_node)
22644 {
22645 type = NULL_TREE;
22646 goto done;
22647 }
22648
22649 class_type = current_class_type;
22650 /* Enter the scope indicated by the nested-name-specifier. */
22651 pushed_scope = push_scope (nested_name_specifier);
22652 /* Get the canonical version of this type. */
22653 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22654 /* Call push_template_decl if it seems like we should be defining a
22655 template either from the template headers or the type we're
22656 defining, so that we diagnose both extra and missing headers. */
22657 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22658 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22659 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22660 {
22661 type = push_template_decl (type);
22662 if (type == error_mark_node)
22663 {
22664 type = NULL_TREE;
22665 goto done;
22666 }
22667 }
22668
22669 type = TREE_TYPE (type);
22670 *nested_name_specifier_p = true;
22671 }
22672 else /* The name is not a nested name. */
22673 {
22674 /* If the class was unnamed, create a dummy name. */
22675 if (!id)
22676 id = make_anon_name ();
22677 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22678 parser->num_template_parameter_lists);
22679 }
22680
22681 /* Indicate whether this class was declared as a `class' or as a
22682 `struct'. */
22683 if (TREE_CODE (type) == RECORD_TYPE)
22684 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22685 cp_parser_check_class_key (class_key, type);
22686
22687 /* If this type was already complete, and we see another definition,
22688 that's an error. */
22689 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22690 {
22691 error_at (type_start_token->location, "redefinition of %q#T",
22692 type);
22693 inform (location_of (type), "previous definition of %q#T",
22694 type);
22695 type = NULL_TREE;
22696 goto done;
22697 }
22698 else if (type == error_mark_node)
22699 type = NULL_TREE;
22700
22701 if (type)
22702 {
22703 /* Apply attributes now, before any use of the class as a template
22704 argument in its base list. */
22705 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22706 fixup_attribute_variants (type);
22707 }
22708
22709 /* We will have entered the scope containing the class; the names of
22710 base classes should be looked up in that context. For example:
22711
22712 struct A { struct B {}; struct C; };
22713 struct A::C : B {};
22714
22715 is valid. */
22716
22717 /* Get the list of base-classes, if there is one. */
22718 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22719 {
22720 /* PR59482: enter the class scope so that base-specifiers are looked
22721 up correctly. */
22722 if (type)
22723 pushclass (type);
22724 bases = cp_parser_base_clause (parser);
22725 /* PR59482: get out of the previously pushed class scope so that the
22726 subsequent pops pop the right thing. */
22727 if (type)
22728 popclass ();
22729 }
22730 else
22731 bases = NULL_TREE;
22732
22733 /* If we're really defining a class, process the base classes.
22734 If they're invalid, fail. */
22735 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22736 xref_basetypes (type, bases);
22737
22738 done:
22739 /* Leave the scope given by the nested-name-specifier. We will
22740 enter the class scope itself while processing the members. */
22741 if (pushed_scope)
22742 pop_scope (pushed_scope);
22743
22744 if (invalid_explicit_specialization_p)
22745 {
22746 end_specialization ();
22747 --parser->num_template_parameter_lists;
22748 }
22749
22750 if (type)
22751 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22752 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22753 CLASSTYPE_FINAL (type) = 1;
22754 out:
22755 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22756 return type;
22757 }
22758
22759 /* Parse a class-key.
22760
22761 class-key:
22762 class
22763 struct
22764 union
22765
22766 Returns the kind of class-key specified, or none_type to indicate
22767 error. */
22768
22769 static enum tag_types
22770 cp_parser_class_key (cp_parser* parser)
22771 {
22772 cp_token *token;
22773 enum tag_types tag_type;
22774
22775 /* Look for the class-key. */
22776 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22777 if (!token)
22778 return none_type;
22779
22780 /* Check to see if the TOKEN is a class-key. */
22781 tag_type = cp_parser_token_is_class_key (token);
22782 if (!tag_type)
22783 cp_parser_error (parser, "expected class-key");
22784 return tag_type;
22785 }
22786
22787 /* Parse a type-parameter-key.
22788
22789 type-parameter-key:
22790 class
22791 typename
22792 */
22793
22794 static void
22795 cp_parser_type_parameter_key (cp_parser* parser)
22796 {
22797 /* Look for the type-parameter-key. */
22798 enum tag_types tag_type = none_type;
22799 cp_token *token = cp_lexer_peek_token (parser->lexer);
22800 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22801 {
22802 cp_lexer_consume_token (parser->lexer);
22803 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22804 /* typename is not allowed in a template template parameter
22805 by the standard until C++1Z. */
22806 pedwarn (token->location, OPT_Wpedantic,
22807 "ISO C++ forbids typename key in template template parameter;"
22808 " use -std=c++1z or -std=gnu++1z");
22809 }
22810 else
22811 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22812
22813 return;
22814 }
22815
22816 /* Parse an (optional) member-specification.
22817
22818 member-specification:
22819 member-declaration member-specification [opt]
22820 access-specifier : member-specification [opt] */
22821
22822 static void
22823 cp_parser_member_specification_opt (cp_parser* parser)
22824 {
22825 while (true)
22826 {
22827 cp_token *token;
22828 enum rid keyword;
22829
22830 /* Peek at the next token. */
22831 token = cp_lexer_peek_token (parser->lexer);
22832 /* If it's a `}', or EOF then we've seen all the members. */
22833 if (token->type == CPP_CLOSE_BRACE
22834 || token->type == CPP_EOF
22835 || token->type == CPP_PRAGMA_EOL)
22836 break;
22837
22838 /* See if this token is a keyword. */
22839 keyword = token->keyword;
22840 switch (keyword)
22841 {
22842 case RID_PUBLIC:
22843 case RID_PROTECTED:
22844 case RID_PRIVATE:
22845 /* Consume the access-specifier. */
22846 cp_lexer_consume_token (parser->lexer);
22847 /* Remember which access-specifier is active. */
22848 current_access_specifier = token->u.value;
22849 /* Look for the `:'. */
22850 cp_parser_require (parser, CPP_COLON, RT_COLON);
22851 break;
22852
22853 default:
22854 /* Accept #pragmas at class scope. */
22855 if (token->type == CPP_PRAGMA)
22856 {
22857 cp_parser_pragma (parser, pragma_member, NULL);
22858 break;
22859 }
22860
22861 /* Otherwise, the next construction must be a
22862 member-declaration. */
22863 cp_parser_member_declaration (parser);
22864 }
22865 }
22866 }
22867
22868 /* Parse a member-declaration.
22869
22870 member-declaration:
22871 decl-specifier-seq [opt] member-declarator-list [opt] ;
22872 function-definition ; [opt]
22873 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22874 using-declaration
22875 template-declaration
22876 alias-declaration
22877
22878 member-declarator-list:
22879 member-declarator
22880 member-declarator-list , member-declarator
22881
22882 member-declarator:
22883 declarator pure-specifier [opt]
22884 declarator constant-initializer [opt]
22885 identifier [opt] : constant-expression
22886
22887 GNU Extensions:
22888
22889 member-declaration:
22890 __extension__ member-declaration
22891
22892 member-declarator:
22893 declarator attributes [opt] pure-specifier [opt]
22894 declarator attributes [opt] constant-initializer [opt]
22895 identifier [opt] attributes [opt] : constant-expression
22896
22897 C++0x Extensions:
22898
22899 member-declaration:
22900 static_assert-declaration */
22901
22902 static void
22903 cp_parser_member_declaration (cp_parser* parser)
22904 {
22905 cp_decl_specifier_seq decl_specifiers;
22906 tree prefix_attributes;
22907 tree decl;
22908 int declares_class_or_enum;
22909 bool friend_p;
22910 cp_token *token = NULL;
22911 cp_token *decl_spec_token_start = NULL;
22912 cp_token *initializer_token_start = NULL;
22913 int saved_pedantic;
22914 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22915
22916 /* Check for the `__extension__' keyword. */
22917 if (cp_parser_extension_opt (parser, &saved_pedantic))
22918 {
22919 /* Recurse. */
22920 cp_parser_member_declaration (parser);
22921 /* Restore the old value of the PEDANTIC flag. */
22922 pedantic = saved_pedantic;
22923
22924 return;
22925 }
22926
22927 /* Check for a template-declaration. */
22928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22929 {
22930 /* An explicit specialization here is an error condition, and we
22931 expect the specialization handler to detect and report this. */
22932 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22933 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22934 cp_parser_explicit_specialization (parser);
22935 else
22936 cp_parser_template_declaration (parser, /*member_p=*/true);
22937
22938 return;
22939 }
22940 /* Check for a template introduction. */
22941 else if (cp_parser_template_declaration_after_export (parser, true))
22942 return;
22943
22944 /* Check for a using-declaration. */
22945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22946 {
22947 if (cxx_dialect < cxx11)
22948 {
22949 /* Parse the using-declaration. */
22950 cp_parser_using_declaration (parser,
22951 /*access_declaration_p=*/false);
22952 return;
22953 }
22954 else
22955 {
22956 tree decl;
22957 bool alias_decl_expected;
22958 cp_parser_parse_tentatively (parser);
22959 decl = cp_parser_alias_declaration (parser);
22960 /* Note that if we actually see the '=' token after the
22961 identifier, cp_parser_alias_declaration commits the
22962 tentative parse. In that case, we really expect an
22963 alias-declaration. Otherwise, we expect a using
22964 declaration. */
22965 alias_decl_expected =
22966 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22967 cp_parser_parse_definitely (parser);
22968
22969 if (alias_decl_expected)
22970 finish_member_declaration (decl);
22971 else
22972 cp_parser_using_declaration (parser,
22973 /*access_declaration_p=*/false);
22974 return;
22975 }
22976 }
22977
22978 /* Check for @defs. */
22979 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22980 {
22981 tree ivar, member;
22982 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22983 ivar = ivar_chains;
22984 while (ivar)
22985 {
22986 member = ivar;
22987 ivar = TREE_CHAIN (member);
22988 TREE_CHAIN (member) = NULL_TREE;
22989 finish_member_declaration (member);
22990 }
22991 return;
22992 }
22993
22994 /* If the next token is `static_assert' we have a static assertion. */
22995 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22996 {
22997 cp_parser_static_assert (parser, /*member_p=*/true);
22998 return;
22999 }
23000
23001 parser->colon_corrects_to_scope_p = false;
23002
23003 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23004 goto out;
23005
23006 /* Parse the decl-specifier-seq. */
23007 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23008 cp_parser_decl_specifier_seq (parser,
23009 CP_PARSER_FLAGS_OPTIONAL,
23010 &decl_specifiers,
23011 &declares_class_or_enum);
23012 /* Check for an invalid type-name. */
23013 if (!decl_specifiers.any_type_specifiers_p
23014 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23015 goto out;
23016 /* If there is no declarator, then the decl-specifier-seq should
23017 specify a type. */
23018 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23019 {
23020 /* If there was no decl-specifier-seq, and the next token is a
23021 `;', then we have something like:
23022
23023 struct S { ; };
23024
23025 [class.mem]
23026
23027 Each member-declaration shall declare at least one member
23028 name of the class. */
23029 if (!decl_specifiers.any_specifiers_p)
23030 {
23031 cp_token *token = cp_lexer_peek_token (parser->lexer);
23032 if (!in_system_header_at (token->location))
23033 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23034 }
23035 else
23036 {
23037 tree type;
23038
23039 /* See if this declaration is a friend. */
23040 friend_p = cp_parser_friend_p (&decl_specifiers);
23041 /* If there were decl-specifiers, check to see if there was
23042 a class-declaration. */
23043 type = check_tag_decl (&decl_specifiers,
23044 /*explicit_type_instantiation_p=*/false);
23045 /* Nested classes have already been added to the class, but
23046 a `friend' needs to be explicitly registered. */
23047 if (friend_p)
23048 {
23049 /* If the `friend' keyword was present, the friend must
23050 be introduced with a class-key. */
23051 if (!declares_class_or_enum && cxx_dialect < cxx11)
23052 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23053 "in C++03 a class-key must be used "
23054 "when declaring a friend");
23055 /* In this case:
23056
23057 template <typename T> struct A {
23058 friend struct A<T>::B;
23059 };
23060
23061 A<T>::B will be represented by a TYPENAME_TYPE, and
23062 therefore not recognized by check_tag_decl. */
23063 if (!type)
23064 {
23065 type = decl_specifiers.type;
23066 if (type && TREE_CODE (type) == TYPE_DECL)
23067 type = TREE_TYPE (type);
23068 }
23069 if (!type || !TYPE_P (type))
23070 error_at (decl_spec_token_start->location,
23071 "friend declaration does not name a class or "
23072 "function");
23073 else
23074 make_friend_class (current_class_type, type,
23075 /*complain=*/true);
23076 }
23077 /* If there is no TYPE, an error message will already have
23078 been issued. */
23079 else if (!type || type == error_mark_node)
23080 ;
23081 /* An anonymous aggregate has to be handled specially; such
23082 a declaration really declares a data member (with a
23083 particular type), as opposed to a nested class. */
23084 else if (ANON_AGGR_TYPE_P (type))
23085 {
23086 /* C++11 9.5/6. */
23087 if (decl_specifiers.storage_class != sc_none)
23088 error_at (decl_spec_token_start->location,
23089 "a storage class on an anonymous aggregate "
23090 "in class scope is not allowed");
23091
23092 /* Remove constructors and such from TYPE, now that we
23093 know it is an anonymous aggregate. */
23094 fixup_anonymous_aggr (type);
23095 /* And make the corresponding data member. */
23096 decl = build_decl (decl_spec_token_start->location,
23097 FIELD_DECL, NULL_TREE, type);
23098 /* Add it to the class. */
23099 finish_member_declaration (decl);
23100 }
23101 else
23102 cp_parser_check_access_in_redeclaration
23103 (TYPE_NAME (type),
23104 decl_spec_token_start->location);
23105 }
23106 }
23107 else
23108 {
23109 bool assume_semicolon = false;
23110
23111 /* Clear attributes from the decl_specifiers but keep them
23112 around as prefix attributes that apply them to the entity
23113 being declared. */
23114 prefix_attributes = decl_specifiers.attributes;
23115 decl_specifiers.attributes = NULL_TREE;
23116
23117 /* See if these declarations will be friends. */
23118 friend_p = cp_parser_friend_p (&decl_specifiers);
23119
23120 /* Keep going until we hit the `;' at the end of the
23121 declaration. */
23122 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23123 {
23124 tree attributes = NULL_TREE;
23125 tree first_attribute;
23126
23127 /* Peek at the next token. */
23128 token = cp_lexer_peek_token (parser->lexer);
23129
23130 /* Check for a bitfield declaration. */
23131 if (token->type == CPP_COLON
23132 || (token->type == CPP_NAME
23133 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23134 == CPP_COLON))
23135 {
23136 tree identifier;
23137 tree width;
23138
23139 /* Get the name of the bitfield. Note that we cannot just
23140 check TOKEN here because it may have been invalidated by
23141 the call to cp_lexer_peek_nth_token above. */
23142 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23143 identifier = cp_parser_identifier (parser);
23144 else
23145 identifier = NULL_TREE;
23146
23147 /* Consume the `:' token. */
23148 cp_lexer_consume_token (parser->lexer);
23149 /* Get the width of the bitfield. */
23150 width
23151 = cp_parser_constant_expression (parser);
23152
23153 /* Look for attributes that apply to the bitfield. */
23154 attributes = cp_parser_attributes_opt (parser);
23155 /* Remember which attributes are prefix attributes and
23156 which are not. */
23157 first_attribute = attributes;
23158 /* Combine the attributes. */
23159 attributes = chainon (prefix_attributes, attributes);
23160
23161 /* Create the bitfield declaration. */
23162 decl = grokbitfield (identifier
23163 ? make_id_declarator (NULL_TREE,
23164 identifier,
23165 sfk_none)
23166 : NULL,
23167 &decl_specifiers,
23168 width,
23169 attributes);
23170 }
23171 else
23172 {
23173 cp_declarator *declarator;
23174 tree initializer;
23175 tree asm_specification;
23176 int ctor_dtor_or_conv_p;
23177
23178 /* Parse the declarator. */
23179 declarator
23180 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23181 &ctor_dtor_or_conv_p,
23182 /*parenthesized_p=*/NULL,
23183 /*member_p=*/true,
23184 friend_p);
23185
23186 /* If something went wrong parsing the declarator, make sure
23187 that we at least consume some tokens. */
23188 if (declarator == cp_error_declarator)
23189 {
23190 /* Skip to the end of the statement. */
23191 cp_parser_skip_to_end_of_statement (parser);
23192 /* If the next token is not a semicolon, that is
23193 probably because we just skipped over the body of
23194 a function. So, we consume a semicolon if
23195 present, but do not issue an error message if it
23196 is not present. */
23197 if (cp_lexer_next_token_is (parser->lexer,
23198 CPP_SEMICOLON))
23199 cp_lexer_consume_token (parser->lexer);
23200 goto out;
23201 }
23202
23203 if (declares_class_or_enum & 2)
23204 cp_parser_check_for_definition_in_return_type
23205 (declarator, decl_specifiers.type,
23206 decl_specifiers.locations[ds_type_spec]);
23207
23208 /* Look for an asm-specification. */
23209 asm_specification = cp_parser_asm_specification_opt (parser);
23210 /* Look for attributes that apply to the declaration. */
23211 attributes = cp_parser_attributes_opt (parser);
23212 /* Remember which attributes are prefix attributes and
23213 which are not. */
23214 first_attribute = attributes;
23215 /* Combine the attributes. */
23216 attributes = chainon (prefix_attributes, attributes);
23217
23218 /* If it's an `=', then we have a constant-initializer or a
23219 pure-specifier. It is not correct to parse the
23220 initializer before registering the member declaration
23221 since the member declaration should be in scope while
23222 its initializer is processed. However, the rest of the
23223 front end does not yet provide an interface that allows
23224 us to handle this correctly. */
23225 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23226 {
23227 /* In [class.mem]:
23228
23229 A pure-specifier shall be used only in the declaration of
23230 a virtual function.
23231
23232 A member-declarator can contain a constant-initializer
23233 only if it declares a static member of integral or
23234 enumeration type.
23235
23236 Therefore, if the DECLARATOR is for a function, we look
23237 for a pure-specifier; otherwise, we look for a
23238 constant-initializer. When we call `grokfield', it will
23239 perform more stringent semantics checks. */
23240 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23241 if (function_declarator_p (declarator)
23242 || (decl_specifiers.type
23243 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23244 && declarator->kind == cdk_id
23245 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23246 == FUNCTION_TYPE)))
23247 initializer = cp_parser_pure_specifier (parser);
23248 else if (decl_specifiers.storage_class != sc_static)
23249 initializer = cp_parser_save_nsdmi (parser);
23250 else if (cxx_dialect >= cxx11)
23251 {
23252 bool nonconst;
23253 /* Don't require a constant rvalue in C++11, since we
23254 might want a reference constant. We'll enforce
23255 constancy later. */
23256 cp_lexer_consume_token (parser->lexer);
23257 /* Parse the initializer. */
23258 initializer = cp_parser_initializer_clause (parser,
23259 &nonconst);
23260 }
23261 else
23262 /* Parse the initializer. */
23263 initializer = cp_parser_constant_initializer (parser);
23264 }
23265 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23266 && !function_declarator_p (declarator))
23267 {
23268 bool x;
23269 if (decl_specifiers.storage_class != sc_static)
23270 initializer = cp_parser_save_nsdmi (parser);
23271 else
23272 initializer = cp_parser_initializer (parser, &x, &x);
23273 }
23274 /* Otherwise, there is no initializer. */
23275 else
23276 initializer = NULL_TREE;
23277
23278 /* See if we are probably looking at a function
23279 definition. We are certainly not looking at a
23280 member-declarator. Calling `grokfield' has
23281 side-effects, so we must not do it unless we are sure
23282 that we are looking at a member-declarator. */
23283 if (cp_parser_token_starts_function_definition_p
23284 (cp_lexer_peek_token (parser->lexer)))
23285 {
23286 /* The grammar does not allow a pure-specifier to be
23287 used when a member function is defined. (It is
23288 possible that this fact is an oversight in the
23289 standard, since a pure function may be defined
23290 outside of the class-specifier. */
23291 if (initializer && initializer_token_start)
23292 error_at (initializer_token_start->location,
23293 "pure-specifier on function-definition");
23294 decl = cp_parser_save_member_function_body (parser,
23295 &decl_specifiers,
23296 declarator,
23297 attributes);
23298 if (parser->fully_implicit_function_template_p)
23299 decl = finish_fully_implicit_template (parser, decl);
23300 /* If the member was not a friend, declare it here. */
23301 if (!friend_p)
23302 finish_member_declaration (decl);
23303 /* Peek at the next token. */
23304 token = cp_lexer_peek_token (parser->lexer);
23305 /* If the next token is a semicolon, consume it. */
23306 if (token->type == CPP_SEMICOLON)
23307 cp_lexer_consume_token (parser->lexer);
23308 goto out;
23309 }
23310 else
23311 if (declarator->kind == cdk_function)
23312 declarator->id_loc = token->location;
23313 /* Create the declaration. */
23314 decl = grokfield (declarator, &decl_specifiers,
23315 initializer, /*init_const_expr_p=*/true,
23316 asm_specification, attributes);
23317 if (parser->fully_implicit_function_template_p)
23318 {
23319 if (friend_p)
23320 finish_fully_implicit_template (parser, 0);
23321 else
23322 decl = finish_fully_implicit_template (parser, decl);
23323 }
23324 }
23325
23326 cp_finalize_omp_declare_simd (parser, decl);
23327 cp_finalize_oacc_routine (parser, decl, false);
23328
23329 /* Reset PREFIX_ATTRIBUTES. */
23330 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23331 attributes = TREE_CHAIN (attributes);
23332 if (attributes)
23333 TREE_CHAIN (attributes) = NULL_TREE;
23334
23335 /* If there is any qualification still in effect, clear it
23336 now; we will be starting fresh with the next declarator. */
23337 parser->scope = NULL_TREE;
23338 parser->qualifying_scope = NULL_TREE;
23339 parser->object_scope = NULL_TREE;
23340 /* If it's a `,', then there are more declarators. */
23341 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23342 {
23343 cp_lexer_consume_token (parser->lexer);
23344 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23345 {
23346 cp_token *token = cp_lexer_previous_token (parser->lexer);
23347 error_at (token->location,
23348 "stray %<,%> at end of member declaration");
23349 }
23350 }
23351 /* If the next token isn't a `;', then we have a parse error. */
23352 else if (cp_lexer_next_token_is_not (parser->lexer,
23353 CPP_SEMICOLON))
23354 {
23355 /* The next token might be a ways away from where the
23356 actual semicolon is missing. Find the previous token
23357 and use that for our error position. */
23358 cp_token *token = cp_lexer_previous_token (parser->lexer);
23359 error_at (token->location,
23360 "expected %<;%> at end of member declaration");
23361
23362 /* Assume that the user meant to provide a semicolon. If
23363 we were to cp_parser_skip_to_end_of_statement, we might
23364 skip to a semicolon inside a member function definition
23365 and issue nonsensical error messages. */
23366 assume_semicolon = true;
23367 }
23368
23369 if (decl)
23370 {
23371 /* Add DECL to the list of members. */
23372 if (!friend_p
23373 /* Explicitly include, eg, NSDMIs, for better error
23374 recovery (c++/58650). */
23375 || !DECL_DECLARES_FUNCTION_P (decl))
23376 finish_member_declaration (decl);
23377
23378 if (TREE_CODE (decl) == FUNCTION_DECL)
23379 cp_parser_save_default_args (parser, decl);
23380 else if (TREE_CODE (decl) == FIELD_DECL
23381 && !DECL_C_BIT_FIELD (decl)
23382 && DECL_INITIAL (decl))
23383 /* Add DECL to the queue of NSDMI to be parsed later. */
23384 vec_safe_push (unparsed_nsdmis, decl);
23385 }
23386
23387 if (assume_semicolon)
23388 goto out;
23389 }
23390 }
23391
23392 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23393 out:
23394 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23395 }
23396
23397 /* Parse a pure-specifier.
23398
23399 pure-specifier:
23400 = 0
23401
23402 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23403 Otherwise, ERROR_MARK_NODE is returned. */
23404
23405 static tree
23406 cp_parser_pure_specifier (cp_parser* parser)
23407 {
23408 cp_token *token;
23409
23410 /* Look for the `=' token. */
23411 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23412 return error_mark_node;
23413 /* Look for the `0' token. */
23414 token = cp_lexer_peek_token (parser->lexer);
23415
23416 if (token->type == CPP_EOF
23417 || token->type == CPP_PRAGMA_EOL)
23418 return error_mark_node;
23419
23420 cp_lexer_consume_token (parser->lexer);
23421
23422 /* Accept = default or = delete in c++0x mode. */
23423 if (token->keyword == RID_DEFAULT
23424 || token->keyword == RID_DELETE)
23425 {
23426 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23427 return token->u.value;
23428 }
23429
23430 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23431 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23432 {
23433 cp_parser_error (parser,
23434 "invalid pure specifier (only %<= 0%> is allowed)");
23435 cp_parser_skip_to_end_of_statement (parser);
23436 return error_mark_node;
23437 }
23438 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23439 {
23440 error_at (token->location, "templates may not be %<virtual%>");
23441 return error_mark_node;
23442 }
23443
23444 return integer_zero_node;
23445 }
23446
23447 /* Parse a constant-initializer.
23448
23449 constant-initializer:
23450 = constant-expression
23451
23452 Returns a representation of the constant-expression. */
23453
23454 static tree
23455 cp_parser_constant_initializer (cp_parser* parser)
23456 {
23457 /* Look for the `=' token. */
23458 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23459 return error_mark_node;
23460
23461 /* It is invalid to write:
23462
23463 struct S { static const int i = { 7 }; };
23464
23465 */
23466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23467 {
23468 cp_parser_error (parser,
23469 "a brace-enclosed initializer is not allowed here");
23470 /* Consume the opening brace. */
23471 cp_lexer_consume_token (parser->lexer);
23472 /* Skip the initializer. */
23473 cp_parser_skip_to_closing_brace (parser);
23474 /* Look for the trailing `}'. */
23475 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23476
23477 return error_mark_node;
23478 }
23479
23480 return cp_parser_constant_expression (parser);
23481 }
23482
23483 /* Derived classes [gram.class.derived] */
23484
23485 /* Parse a base-clause.
23486
23487 base-clause:
23488 : base-specifier-list
23489
23490 base-specifier-list:
23491 base-specifier ... [opt]
23492 base-specifier-list , base-specifier ... [opt]
23493
23494 Returns a TREE_LIST representing the base-classes, in the order in
23495 which they were declared. The representation of each node is as
23496 described by cp_parser_base_specifier.
23497
23498 In the case that no bases are specified, this function will return
23499 NULL_TREE, not ERROR_MARK_NODE. */
23500
23501 static tree
23502 cp_parser_base_clause (cp_parser* parser)
23503 {
23504 tree bases = NULL_TREE;
23505
23506 /* Look for the `:' that begins the list. */
23507 cp_parser_require (parser, CPP_COLON, RT_COLON);
23508
23509 /* Scan the base-specifier-list. */
23510 while (true)
23511 {
23512 cp_token *token;
23513 tree base;
23514 bool pack_expansion_p = false;
23515
23516 /* Look for the base-specifier. */
23517 base = cp_parser_base_specifier (parser);
23518 /* Look for the (optional) ellipsis. */
23519 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23520 {
23521 /* Consume the `...'. */
23522 cp_lexer_consume_token (parser->lexer);
23523
23524 pack_expansion_p = true;
23525 }
23526
23527 /* Add BASE to the front of the list. */
23528 if (base && base != error_mark_node)
23529 {
23530 if (pack_expansion_p)
23531 /* Make this a pack expansion type. */
23532 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23533
23534 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23535 {
23536 TREE_CHAIN (base) = bases;
23537 bases = base;
23538 }
23539 }
23540 /* Peek at the next token. */
23541 token = cp_lexer_peek_token (parser->lexer);
23542 /* If it's not a comma, then the list is complete. */
23543 if (token->type != CPP_COMMA)
23544 break;
23545 /* Consume the `,'. */
23546 cp_lexer_consume_token (parser->lexer);
23547 }
23548
23549 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23550 base class had a qualified name. However, the next name that
23551 appears is certainly not qualified. */
23552 parser->scope = NULL_TREE;
23553 parser->qualifying_scope = NULL_TREE;
23554 parser->object_scope = NULL_TREE;
23555
23556 return nreverse (bases);
23557 }
23558
23559 /* Parse a base-specifier.
23560
23561 base-specifier:
23562 :: [opt] nested-name-specifier [opt] class-name
23563 virtual access-specifier [opt] :: [opt] nested-name-specifier
23564 [opt] class-name
23565 access-specifier virtual [opt] :: [opt] nested-name-specifier
23566 [opt] class-name
23567
23568 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23569 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23570 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23571 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23572
23573 static tree
23574 cp_parser_base_specifier (cp_parser* parser)
23575 {
23576 cp_token *token;
23577 bool done = false;
23578 bool virtual_p = false;
23579 bool duplicate_virtual_error_issued_p = false;
23580 bool duplicate_access_error_issued_p = false;
23581 bool class_scope_p, template_p;
23582 tree access = access_default_node;
23583 tree type;
23584
23585 /* Process the optional `virtual' and `access-specifier'. */
23586 while (!done)
23587 {
23588 /* Peek at the next token. */
23589 token = cp_lexer_peek_token (parser->lexer);
23590 /* Process `virtual'. */
23591 switch (token->keyword)
23592 {
23593 case RID_VIRTUAL:
23594 /* If `virtual' appears more than once, issue an error. */
23595 if (virtual_p && !duplicate_virtual_error_issued_p)
23596 {
23597 cp_parser_error (parser,
23598 "%<virtual%> specified more than once in base-specified");
23599 duplicate_virtual_error_issued_p = true;
23600 }
23601
23602 virtual_p = true;
23603
23604 /* Consume the `virtual' token. */
23605 cp_lexer_consume_token (parser->lexer);
23606
23607 break;
23608
23609 case RID_PUBLIC:
23610 case RID_PROTECTED:
23611 case RID_PRIVATE:
23612 /* If more than one access specifier appears, issue an
23613 error. */
23614 if (access != access_default_node
23615 && !duplicate_access_error_issued_p)
23616 {
23617 cp_parser_error (parser,
23618 "more than one access specifier in base-specified");
23619 duplicate_access_error_issued_p = true;
23620 }
23621
23622 access = ridpointers[(int) token->keyword];
23623
23624 /* Consume the access-specifier. */
23625 cp_lexer_consume_token (parser->lexer);
23626
23627 break;
23628
23629 default:
23630 done = true;
23631 break;
23632 }
23633 }
23634 /* It is not uncommon to see programs mechanically, erroneously, use
23635 the 'typename' keyword to denote (dependent) qualified types
23636 as base classes. */
23637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23638 {
23639 token = cp_lexer_peek_token (parser->lexer);
23640 if (!processing_template_decl)
23641 error_at (token->location,
23642 "keyword %<typename%> not allowed outside of templates");
23643 else
23644 error_at (token->location,
23645 "keyword %<typename%> not allowed in this context "
23646 "(the base class is implicitly a type)");
23647 cp_lexer_consume_token (parser->lexer);
23648 }
23649
23650 /* Look for the optional `::' operator. */
23651 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23652 /* Look for the nested-name-specifier. The simplest way to
23653 implement:
23654
23655 [temp.res]
23656
23657 The keyword `typename' is not permitted in a base-specifier or
23658 mem-initializer; in these contexts a qualified name that
23659 depends on a template-parameter is implicitly assumed to be a
23660 type name.
23661
23662 is to pretend that we have seen the `typename' keyword at this
23663 point. */
23664 cp_parser_nested_name_specifier_opt (parser,
23665 /*typename_keyword_p=*/true,
23666 /*check_dependency_p=*/true,
23667 /*type_p=*/true,
23668 /*is_declaration=*/true);
23669 /* If the base class is given by a qualified name, assume that names
23670 we see are type names or templates, as appropriate. */
23671 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23672 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23673
23674 if (!parser->scope
23675 && cp_lexer_next_token_is_decltype (parser->lexer))
23676 /* DR 950 allows decltype as a base-specifier. */
23677 type = cp_parser_decltype (parser);
23678 else
23679 {
23680 /* Otherwise, look for the class-name. */
23681 type = cp_parser_class_name (parser,
23682 class_scope_p,
23683 template_p,
23684 typename_type,
23685 /*check_dependency_p=*/true,
23686 /*class_head_p=*/false,
23687 /*is_declaration=*/true);
23688 type = TREE_TYPE (type);
23689 }
23690
23691 if (type == error_mark_node)
23692 return error_mark_node;
23693
23694 return finish_base_specifier (type, access, virtual_p);
23695 }
23696
23697 /* Exception handling [gram.exception] */
23698
23699 /* Parse an (optional) noexcept-specification.
23700
23701 noexcept-specification:
23702 noexcept ( constant-expression ) [opt]
23703
23704 If no noexcept-specification is present, returns NULL_TREE.
23705 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23706 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23707 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23708 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23709 in which case a boolean condition is returned instead. */
23710
23711 static tree
23712 cp_parser_noexcept_specification_opt (cp_parser* parser,
23713 bool require_constexpr,
23714 bool* consumed_expr,
23715 bool return_cond)
23716 {
23717 cp_token *token;
23718 const char *saved_message;
23719
23720 /* Peek at the next token. */
23721 token = cp_lexer_peek_token (parser->lexer);
23722
23723 /* Is it a noexcept-specification? */
23724 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23725 {
23726 tree expr;
23727 cp_lexer_consume_token (parser->lexer);
23728
23729 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23730 {
23731 cp_lexer_consume_token (parser->lexer);
23732
23733 if (require_constexpr)
23734 {
23735 /* Types may not be defined in an exception-specification. */
23736 saved_message = parser->type_definition_forbidden_message;
23737 parser->type_definition_forbidden_message
23738 = G_("types may not be defined in an exception-specification");
23739
23740 expr = cp_parser_constant_expression (parser);
23741
23742 /* Restore the saved message. */
23743 parser->type_definition_forbidden_message = saved_message;
23744 }
23745 else
23746 {
23747 expr = cp_parser_expression (parser);
23748 *consumed_expr = true;
23749 }
23750
23751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23752 }
23753 else
23754 {
23755 expr = boolean_true_node;
23756 if (!require_constexpr)
23757 *consumed_expr = false;
23758 }
23759
23760 /* We cannot build a noexcept-spec right away because this will check
23761 that expr is a constexpr. */
23762 if (!return_cond)
23763 return build_noexcept_spec (expr, tf_warning_or_error);
23764 else
23765 return expr;
23766 }
23767 else
23768 return NULL_TREE;
23769 }
23770
23771 /* Parse an (optional) exception-specification.
23772
23773 exception-specification:
23774 throw ( type-id-list [opt] )
23775
23776 Returns a TREE_LIST representing the exception-specification. The
23777 TREE_VALUE of each node is a type. */
23778
23779 static tree
23780 cp_parser_exception_specification_opt (cp_parser* parser)
23781 {
23782 cp_token *token;
23783 tree type_id_list;
23784 const char *saved_message;
23785
23786 /* Peek at the next token. */
23787 token = cp_lexer_peek_token (parser->lexer);
23788
23789 /* Is it a noexcept-specification? */
23790 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23791 false);
23792 if (type_id_list != NULL_TREE)
23793 return type_id_list;
23794
23795 /* If it's not `throw', then there's no exception-specification. */
23796 if (!cp_parser_is_keyword (token, RID_THROW))
23797 return NULL_TREE;
23798
23799 location_t loc = token->location;
23800
23801 /* Consume the `throw'. */
23802 cp_lexer_consume_token (parser->lexer);
23803
23804 /* Look for the `('. */
23805 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23806
23807 /* Peek at the next token. */
23808 token = cp_lexer_peek_token (parser->lexer);
23809 /* If it's not a `)', then there is a type-id-list. */
23810 if (token->type != CPP_CLOSE_PAREN)
23811 {
23812 /* Types may not be defined in an exception-specification. */
23813 saved_message = parser->type_definition_forbidden_message;
23814 parser->type_definition_forbidden_message
23815 = G_("types may not be defined in an exception-specification");
23816 /* Parse the type-id-list. */
23817 type_id_list = cp_parser_type_id_list (parser);
23818 /* Restore the saved message. */
23819 parser->type_definition_forbidden_message = saved_message;
23820
23821 if (cxx_dialect >= cxx1z)
23822 {
23823 error_at (loc, "ISO C++1z does not allow dynamic exception "
23824 "specifications");
23825 type_id_list = NULL_TREE;
23826 }
23827 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23828 warning_at (loc, OPT_Wdeprecated,
23829 "dynamic exception specifications are deprecated in C++11;"
23830 " use %<noexcept%> instead");
23831 }
23832 /* In C++17, throw() is equivalent to noexcept (true). throw()
23833 is deprecated in C++11 and above as well, but is still widely used,
23834 so don't warn about it yet. */
23835 else if (cxx_dialect >= cxx1z)
23836 type_id_list = noexcept_true_spec;
23837 else
23838 type_id_list = empty_except_spec;
23839
23840 /* Look for the `)'. */
23841 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23842
23843 return type_id_list;
23844 }
23845
23846 /* Parse an (optional) type-id-list.
23847
23848 type-id-list:
23849 type-id ... [opt]
23850 type-id-list , type-id ... [opt]
23851
23852 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23853 in the order that the types were presented. */
23854
23855 static tree
23856 cp_parser_type_id_list (cp_parser* parser)
23857 {
23858 tree types = NULL_TREE;
23859
23860 while (true)
23861 {
23862 cp_token *token;
23863 tree type;
23864
23865 token = cp_lexer_peek_token (parser->lexer);
23866
23867 /* Get the next type-id. */
23868 type = cp_parser_type_id (parser);
23869 /* Check for invalid 'auto'. */
23870 if (flag_concepts && type_uses_auto (type))
23871 {
23872 error_at (token->location,
23873 "invalid use of %<auto%> in exception-specification");
23874 type = error_mark_node;
23875 }
23876 /* Parse the optional ellipsis. */
23877 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23878 {
23879 /* Consume the `...'. */
23880 cp_lexer_consume_token (parser->lexer);
23881
23882 /* Turn the type into a pack expansion expression. */
23883 type = make_pack_expansion (type);
23884 }
23885 /* Add it to the list. */
23886 types = add_exception_specifier (types, type, /*complain=*/1);
23887 /* Peek at the next token. */
23888 token = cp_lexer_peek_token (parser->lexer);
23889 /* If it is not a `,', we are done. */
23890 if (token->type != CPP_COMMA)
23891 break;
23892 /* Consume the `,'. */
23893 cp_lexer_consume_token (parser->lexer);
23894 }
23895
23896 return nreverse (types);
23897 }
23898
23899 /* Parse a try-block.
23900
23901 try-block:
23902 try compound-statement handler-seq */
23903
23904 static tree
23905 cp_parser_try_block (cp_parser* parser)
23906 {
23907 tree try_block;
23908
23909 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23910 if (parser->in_function_body
23911 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23912 error ("%<try%> in %<constexpr%> function");
23913
23914 try_block = begin_try_block ();
23915 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23916 finish_try_block (try_block);
23917 cp_parser_handler_seq (parser);
23918 finish_handler_sequence (try_block);
23919
23920 return try_block;
23921 }
23922
23923 /* Parse a function-try-block.
23924
23925 function-try-block:
23926 try ctor-initializer [opt] function-body handler-seq */
23927
23928 static bool
23929 cp_parser_function_try_block (cp_parser* parser)
23930 {
23931 tree compound_stmt;
23932 tree try_block;
23933 bool ctor_initializer_p;
23934
23935 /* Look for the `try' keyword. */
23936 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23937 return false;
23938 /* Let the rest of the front end know where we are. */
23939 try_block = begin_function_try_block (&compound_stmt);
23940 /* Parse the function-body. */
23941 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23942 (parser, /*in_function_try_block=*/true);
23943 /* We're done with the `try' part. */
23944 finish_function_try_block (try_block);
23945 /* Parse the handlers. */
23946 cp_parser_handler_seq (parser);
23947 /* We're done with the handlers. */
23948 finish_function_handler_sequence (try_block, compound_stmt);
23949
23950 return ctor_initializer_p;
23951 }
23952
23953 /* Parse a handler-seq.
23954
23955 handler-seq:
23956 handler handler-seq [opt] */
23957
23958 static void
23959 cp_parser_handler_seq (cp_parser* parser)
23960 {
23961 while (true)
23962 {
23963 cp_token *token;
23964
23965 /* Parse the handler. */
23966 cp_parser_handler (parser);
23967 /* Peek at the next token. */
23968 token = cp_lexer_peek_token (parser->lexer);
23969 /* If it's not `catch' then there are no more handlers. */
23970 if (!cp_parser_is_keyword (token, RID_CATCH))
23971 break;
23972 }
23973 }
23974
23975 /* Parse a handler.
23976
23977 handler:
23978 catch ( exception-declaration ) compound-statement */
23979
23980 static void
23981 cp_parser_handler (cp_parser* parser)
23982 {
23983 tree handler;
23984 tree declaration;
23985
23986 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23987 handler = begin_handler ();
23988 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23989 declaration = cp_parser_exception_declaration (parser);
23990 finish_handler_parms (declaration, handler);
23991 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23992 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23993 finish_handler (handler);
23994 }
23995
23996 /* Parse an exception-declaration.
23997
23998 exception-declaration:
23999 type-specifier-seq declarator
24000 type-specifier-seq abstract-declarator
24001 type-specifier-seq
24002 ...
24003
24004 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24005 ellipsis variant is used. */
24006
24007 static tree
24008 cp_parser_exception_declaration (cp_parser* parser)
24009 {
24010 cp_decl_specifier_seq type_specifiers;
24011 cp_declarator *declarator;
24012 const char *saved_message;
24013
24014 /* If it's an ellipsis, it's easy to handle. */
24015 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24016 {
24017 /* Consume the `...' token. */
24018 cp_lexer_consume_token (parser->lexer);
24019 return NULL_TREE;
24020 }
24021
24022 /* Types may not be defined in exception-declarations. */
24023 saved_message = parser->type_definition_forbidden_message;
24024 parser->type_definition_forbidden_message
24025 = G_("types may not be defined in exception-declarations");
24026
24027 /* Parse the type-specifier-seq. */
24028 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24029 /*is_trailing_return=*/false,
24030 &type_specifiers);
24031 /* If it's a `)', then there is no declarator. */
24032 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24033 declarator = NULL;
24034 else
24035 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24036 /*ctor_dtor_or_conv_p=*/NULL,
24037 /*parenthesized_p=*/NULL,
24038 /*member_p=*/false,
24039 /*friend_p=*/false);
24040
24041 /* Restore the saved message. */
24042 parser->type_definition_forbidden_message = saved_message;
24043
24044 if (!type_specifiers.any_specifiers_p)
24045 return error_mark_node;
24046
24047 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24048 }
24049
24050 /* Parse a throw-expression.
24051
24052 throw-expression:
24053 throw assignment-expression [opt]
24054
24055 Returns a THROW_EXPR representing the throw-expression. */
24056
24057 static tree
24058 cp_parser_throw_expression (cp_parser* parser)
24059 {
24060 tree expression;
24061 cp_token* token;
24062
24063 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24064 token = cp_lexer_peek_token (parser->lexer);
24065 /* Figure out whether or not there is an assignment-expression
24066 following the "throw" keyword. */
24067 if (token->type == CPP_COMMA
24068 || token->type == CPP_SEMICOLON
24069 || token->type == CPP_CLOSE_PAREN
24070 || token->type == CPP_CLOSE_SQUARE
24071 || token->type == CPP_CLOSE_BRACE
24072 || token->type == CPP_COLON)
24073 expression = NULL_TREE;
24074 else
24075 expression = cp_parser_assignment_expression (parser);
24076
24077 return build_throw (expression);
24078 }
24079
24080 /* GNU Extensions */
24081
24082 /* Parse an (optional) asm-specification.
24083
24084 asm-specification:
24085 asm ( string-literal )
24086
24087 If the asm-specification is present, returns a STRING_CST
24088 corresponding to the string-literal. Otherwise, returns
24089 NULL_TREE. */
24090
24091 static tree
24092 cp_parser_asm_specification_opt (cp_parser* parser)
24093 {
24094 cp_token *token;
24095 tree asm_specification;
24096
24097 /* Peek at the next token. */
24098 token = cp_lexer_peek_token (parser->lexer);
24099 /* If the next token isn't the `asm' keyword, then there's no
24100 asm-specification. */
24101 if (!cp_parser_is_keyword (token, RID_ASM))
24102 return NULL_TREE;
24103
24104 /* Consume the `asm' token. */
24105 cp_lexer_consume_token (parser->lexer);
24106 /* Look for the `('. */
24107 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24108
24109 /* Look for the string-literal. */
24110 asm_specification = cp_parser_string_literal (parser, false, false);
24111
24112 /* Look for the `)'. */
24113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24114
24115 return asm_specification;
24116 }
24117
24118 /* Parse an asm-operand-list.
24119
24120 asm-operand-list:
24121 asm-operand
24122 asm-operand-list , asm-operand
24123
24124 asm-operand:
24125 string-literal ( expression )
24126 [ string-literal ] string-literal ( expression )
24127
24128 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24129 each node is the expression. The TREE_PURPOSE is itself a
24130 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24131 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24132 is a STRING_CST for the string literal before the parenthesis. Returns
24133 ERROR_MARK_NODE if any of the operands are invalid. */
24134
24135 static tree
24136 cp_parser_asm_operand_list (cp_parser* parser)
24137 {
24138 tree asm_operands = NULL_TREE;
24139 bool invalid_operands = false;
24140
24141 while (true)
24142 {
24143 tree string_literal;
24144 tree expression;
24145 tree name;
24146
24147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24148 {
24149 /* Consume the `[' token. */
24150 cp_lexer_consume_token (parser->lexer);
24151 /* Read the operand name. */
24152 name = cp_parser_identifier (parser);
24153 if (name != error_mark_node)
24154 name = build_string (IDENTIFIER_LENGTH (name),
24155 IDENTIFIER_POINTER (name));
24156 /* Look for the closing `]'. */
24157 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24158 }
24159 else
24160 name = NULL_TREE;
24161 /* Look for the string-literal. */
24162 string_literal = cp_parser_string_literal (parser, false, false);
24163
24164 /* Look for the `('. */
24165 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24166 /* Parse the expression. */
24167 expression = cp_parser_expression (parser);
24168 /* Look for the `)'. */
24169 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24170
24171 if (name == error_mark_node
24172 || string_literal == error_mark_node
24173 || expression == error_mark_node)
24174 invalid_operands = true;
24175
24176 /* Add this operand to the list. */
24177 asm_operands = tree_cons (build_tree_list (name, string_literal),
24178 expression,
24179 asm_operands);
24180 /* If the next token is not a `,', there are no more
24181 operands. */
24182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24183 break;
24184 /* Consume the `,'. */
24185 cp_lexer_consume_token (parser->lexer);
24186 }
24187
24188 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24189 }
24190
24191 /* Parse an asm-clobber-list.
24192
24193 asm-clobber-list:
24194 string-literal
24195 asm-clobber-list , string-literal
24196
24197 Returns a TREE_LIST, indicating the clobbers in the order that they
24198 appeared. The TREE_VALUE of each node is a STRING_CST. */
24199
24200 static tree
24201 cp_parser_asm_clobber_list (cp_parser* parser)
24202 {
24203 tree clobbers = NULL_TREE;
24204
24205 while (true)
24206 {
24207 tree string_literal;
24208
24209 /* Look for the string literal. */
24210 string_literal = cp_parser_string_literal (parser, false, false);
24211 /* Add it to the list. */
24212 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24213 /* If the next token is not a `,', then the list is
24214 complete. */
24215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24216 break;
24217 /* Consume the `,' token. */
24218 cp_lexer_consume_token (parser->lexer);
24219 }
24220
24221 return clobbers;
24222 }
24223
24224 /* Parse an asm-label-list.
24225
24226 asm-label-list:
24227 identifier
24228 asm-label-list , identifier
24229
24230 Returns a TREE_LIST, indicating the labels in the order that they
24231 appeared. The TREE_VALUE of each node is a label. */
24232
24233 static tree
24234 cp_parser_asm_label_list (cp_parser* parser)
24235 {
24236 tree labels = NULL_TREE;
24237
24238 while (true)
24239 {
24240 tree identifier, label, name;
24241
24242 /* Look for the identifier. */
24243 identifier = cp_parser_identifier (parser);
24244 if (!error_operand_p (identifier))
24245 {
24246 label = lookup_label (identifier);
24247 if (TREE_CODE (label) == LABEL_DECL)
24248 {
24249 TREE_USED (label) = 1;
24250 check_goto (label);
24251 name = build_string (IDENTIFIER_LENGTH (identifier),
24252 IDENTIFIER_POINTER (identifier));
24253 labels = tree_cons (name, label, labels);
24254 }
24255 }
24256 /* If the next token is not a `,', then the list is
24257 complete. */
24258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24259 break;
24260 /* Consume the `,' token. */
24261 cp_lexer_consume_token (parser->lexer);
24262 }
24263
24264 return nreverse (labels);
24265 }
24266
24267 /* Return TRUE iff the next tokens in the stream are possibly the
24268 beginning of a GNU extension attribute. */
24269
24270 static bool
24271 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24272 {
24273 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24274 }
24275
24276 /* Return TRUE iff the next tokens in the stream are possibly the
24277 beginning of a standard C++-11 attribute specifier. */
24278
24279 static bool
24280 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24281 {
24282 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24283 }
24284
24285 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24286 beginning of a standard C++-11 attribute specifier. */
24287
24288 static bool
24289 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24290 {
24291 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24292
24293 return (cxx_dialect >= cxx11
24294 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24295 || (token->type == CPP_OPEN_SQUARE
24296 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24297 && token->type == CPP_OPEN_SQUARE)));
24298 }
24299
24300 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24301 beginning of a GNU extension attribute. */
24302
24303 static bool
24304 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24305 {
24306 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24307
24308 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24309 }
24310
24311 /* Return true iff the next tokens can be the beginning of either a
24312 GNU attribute list, or a standard C++11 attribute sequence. */
24313
24314 static bool
24315 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24316 {
24317 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24318 || cp_next_tokens_can_be_std_attribute_p (parser));
24319 }
24320
24321 /* Return true iff the next Nth tokens can be the beginning of either
24322 a GNU attribute list, or a standard C++11 attribute sequence. */
24323
24324 static bool
24325 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24326 {
24327 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24328 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24329 }
24330
24331 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24332 of GNU attributes, or return NULL. */
24333
24334 static tree
24335 cp_parser_attributes_opt (cp_parser *parser)
24336 {
24337 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24338 return cp_parser_gnu_attributes_opt (parser);
24339 return cp_parser_std_attribute_spec_seq (parser);
24340 }
24341
24342 #define CILK_SIMD_FN_CLAUSE_MASK \
24343 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24344 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24345 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24346 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24347 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24348
24349 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24350 vector [(<clauses>)] */
24351
24352 static void
24353 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24354 {
24355 bool first_p = parser->cilk_simd_fn_info == NULL;
24356 cp_token *token = v_token;
24357 if (first_p)
24358 {
24359 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24360 parser->cilk_simd_fn_info->error_seen = false;
24361 parser->cilk_simd_fn_info->fndecl_seen = false;
24362 parser->cilk_simd_fn_info->tokens = vNULL;
24363 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24364 }
24365 int paren_scope = 0;
24366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24367 {
24368 cp_lexer_consume_token (parser->lexer);
24369 v_token = cp_lexer_peek_token (parser->lexer);
24370 paren_scope++;
24371 }
24372 while (paren_scope > 0)
24373 {
24374 token = cp_lexer_peek_token (parser->lexer);
24375 if (token->type == CPP_OPEN_PAREN)
24376 paren_scope++;
24377 else if (token->type == CPP_CLOSE_PAREN)
24378 paren_scope--;
24379 /* Do not push the last ')' */
24380 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24381 cp_lexer_consume_token (parser->lexer);
24382 }
24383
24384 token->type = CPP_PRAGMA_EOL;
24385 parser->lexer->next_token = token;
24386 cp_lexer_consume_token (parser->lexer);
24387
24388 struct cp_token_cache *cp
24389 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24390 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24391 }
24392
24393 /* Parse an (optional) series of attributes.
24394
24395 attributes:
24396 attributes attribute
24397
24398 attribute:
24399 __attribute__ (( attribute-list [opt] ))
24400
24401 The return value is as for cp_parser_gnu_attribute_list. */
24402
24403 static tree
24404 cp_parser_gnu_attributes_opt (cp_parser* parser)
24405 {
24406 tree attributes = NULL_TREE;
24407
24408 while (true)
24409 {
24410 cp_token *token;
24411 tree attribute_list;
24412 bool ok = true;
24413
24414 /* Peek at the next token. */
24415 token = cp_lexer_peek_token (parser->lexer);
24416 /* If it's not `__attribute__', then we're done. */
24417 if (token->keyword != RID_ATTRIBUTE)
24418 break;
24419
24420 /* Consume the `__attribute__' keyword. */
24421 cp_lexer_consume_token (parser->lexer);
24422 /* Look for the two `(' tokens. */
24423 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24424 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24425
24426 /* Peek at the next token. */
24427 token = cp_lexer_peek_token (parser->lexer);
24428 if (token->type != CPP_CLOSE_PAREN)
24429 /* Parse the attribute-list. */
24430 attribute_list = cp_parser_gnu_attribute_list (parser);
24431 else
24432 /* If the next token is a `)', then there is no attribute
24433 list. */
24434 attribute_list = NULL;
24435
24436 /* Look for the two `)' tokens. */
24437 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24438 ok = false;
24439 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24440 ok = false;
24441 if (!ok)
24442 cp_parser_skip_to_end_of_statement (parser);
24443
24444 /* Add these new attributes to the list. */
24445 attributes = chainon (attributes, attribute_list);
24446 }
24447
24448 return attributes;
24449 }
24450
24451 /* Parse a GNU attribute-list.
24452
24453 attribute-list:
24454 attribute
24455 attribute-list , attribute
24456
24457 attribute:
24458 identifier
24459 identifier ( identifier )
24460 identifier ( identifier , expression-list )
24461 identifier ( expression-list )
24462
24463 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24464 to an attribute. The TREE_PURPOSE of each node is the identifier
24465 indicating which attribute is in use. The TREE_VALUE represents
24466 the arguments, if any. */
24467
24468 static tree
24469 cp_parser_gnu_attribute_list (cp_parser* parser)
24470 {
24471 tree attribute_list = NULL_TREE;
24472 bool save_translate_strings_p = parser->translate_strings_p;
24473
24474 parser->translate_strings_p = false;
24475 while (true)
24476 {
24477 cp_token *token;
24478 tree identifier;
24479 tree attribute;
24480
24481 /* Look for the identifier. We also allow keywords here; for
24482 example `__attribute__ ((const))' is legal. */
24483 token = cp_lexer_peek_token (parser->lexer);
24484 if (token->type == CPP_NAME
24485 || token->type == CPP_KEYWORD)
24486 {
24487 tree arguments = NULL_TREE;
24488
24489 /* Consume the token, but save it since we need it for the
24490 SIMD enabled function parsing. */
24491 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24492
24493 /* Save away the identifier that indicates which attribute
24494 this is. */
24495 identifier = (token->type == CPP_KEYWORD)
24496 /* For keywords, use the canonical spelling, not the
24497 parsed identifier. */
24498 ? ridpointers[(int) token->keyword]
24499 : id_token->u.value;
24500
24501 attribute = build_tree_list (identifier, NULL_TREE);
24502
24503 /* Peek at the next token. */
24504 token = cp_lexer_peek_token (parser->lexer);
24505 /* If it's an `(', then parse the attribute arguments. */
24506 if (token->type == CPP_OPEN_PAREN)
24507 {
24508 vec<tree, va_gc> *vec;
24509 int attr_flag = (attribute_takes_identifier_p (identifier)
24510 ? id_attr : normal_attr);
24511 if (is_cilkplus_vector_p (identifier))
24512 {
24513 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24514 continue;
24515 }
24516 else
24517 vec = cp_parser_parenthesized_expression_list
24518 (parser, attr_flag, /*cast_p=*/false,
24519 /*allow_expansion_p=*/false,
24520 /*non_constant_p=*/NULL);
24521 if (vec == NULL)
24522 arguments = error_mark_node;
24523 else
24524 {
24525 arguments = build_tree_list_vec (vec);
24526 release_tree_vector (vec);
24527 }
24528 /* Save the arguments away. */
24529 TREE_VALUE (attribute) = arguments;
24530 }
24531 else if (is_cilkplus_vector_p (identifier))
24532 {
24533 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24534 continue;
24535 }
24536
24537 if (arguments != error_mark_node)
24538 {
24539 /* Add this attribute to the list. */
24540 TREE_CHAIN (attribute) = attribute_list;
24541 attribute_list = attribute;
24542 }
24543
24544 token = cp_lexer_peek_token (parser->lexer);
24545 }
24546 /* Now, look for more attributes. If the next token isn't a
24547 `,', we're done. */
24548 if (token->type != CPP_COMMA)
24549 break;
24550
24551 /* Consume the comma and keep going. */
24552 cp_lexer_consume_token (parser->lexer);
24553 }
24554 parser->translate_strings_p = save_translate_strings_p;
24555
24556 /* We built up the list in reverse order. */
24557 return nreverse (attribute_list);
24558 }
24559
24560 /* Parse a standard C++11 attribute.
24561
24562 The returned representation is a TREE_LIST which TREE_PURPOSE is
24563 the scoped name of the attribute, and the TREE_VALUE is its
24564 arguments list.
24565
24566 Note that the scoped name of the attribute is itself a TREE_LIST
24567 which TREE_PURPOSE is the namespace of the attribute, and
24568 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24569 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24570 and which TREE_PURPOSE is directly the attribute name.
24571
24572 Clients of the attribute code should use get_attribute_namespace
24573 and get_attribute_name to get the actual namespace and name of
24574 attributes, regardless of their being GNU or C++11 attributes.
24575
24576 attribute:
24577 attribute-token attribute-argument-clause [opt]
24578
24579 attribute-token:
24580 identifier
24581 attribute-scoped-token
24582
24583 attribute-scoped-token:
24584 attribute-namespace :: identifier
24585
24586 attribute-namespace:
24587 identifier
24588
24589 attribute-argument-clause:
24590 ( balanced-token-seq )
24591
24592 balanced-token-seq:
24593 balanced-token [opt]
24594 balanced-token-seq balanced-token
24595
24596 balanced-token:
24597 ( balanced-token-seq )
24598 [ balanced-token-seq ]
24599 { balanced-token-seq }. */
24600
24601 static tree
24602 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24603 {
24604 tree attribute, attr_id = NULL_TREE, arguments;
24605 cp_token *token;
24606
24607 /* First, parse name of the attribute, a.k.a attribute-token. */
24608
24609 token = cp_lexer_peek_token (parser->lexer);
24610 if (token->type == CPP_NAME)
24611 attr_id = token->u.value;
24612 else if (token->type == CPP_KEYWORD)
24613 attr_id = ridpointers[(int) token->keyword];
24614 else if (token->flags & NAMED_OP)
24615 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24616
24617 if (attr_id == NULL_TREE)
24618 return NULL_TREE;
24619
24620 cp_lexer_consume_token (parser->lexer);
24621
24622 token = cp_lexer_peek_token (parser->lexer);
24623 if (token->type == CPP_SCOPE)
24624 {
24625 /* We are seeing a scoped attribute token. */
24626
24627 cp_lexer_consume_token (parser->lexer);
24628 if (attr_ns)
24629 error_at (token->location, "attribute using prefix used together "
24630 "with scoped attribute token");
24631 attr_ns = attr_id;
24632
24633 token = cp_lexer_consume_token (parser->lexer);
24634 if (token->type == CPP_NAME)
24635 attr_id = token->u.value;
24636 else if (token->type == CPP_KEYWORD)
24637 attr_id = ridpointers[(int) token->keyword];
24638 else if (token->flags & NAMED_OP)
24639 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24640 else
24641 {
24642 error_at (token->location,
24643 "expected an identifier for the attribute name");
24644 return error_mark_node;
24645 }
24646 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24647 NULL_TREE);
24648 token = cp_lexer_peek_token (parser->lexer);
24649 }
24650 else if (attr_ns)
24651 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24652 NULL_TREE);
24653 else
24654 {
24655 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24656 NULL_TREE);
24657 /* C++11 noreturn attribute is equivalent to GNU's. */
24658 if (is_attribute_p ("noreturn", attr_id))
24659 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24660 /* C++14 deprecated attribute is equivalent to GNU's. */
24661 else if (is_attribute_p ("deprecated", attr_id))
24662 {
24663 if (cxx_dialect == cxx11)
24664 pedwarn (token->location, OPT_Wpedantic,
24665 "%<deprecated%> is a C++14 feature;"
24666 " use %<gnu::deprecated%>");
24667 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24668 }
24669 /* C++17 fallthrough attribute is equivalent to GNU's. */
24670 else if (is_attribute_p ("fallthrough", attr_id))
24671 {
24672 if (cxx_dialect < cxx1z)
24673 pedwarn (token->location, OPT_Wpedantic,
24674 "%<fallthrough%> is a C++17 feature;"
24675 " use %<gnu::fallthrough%>");
24676 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24677 }
24678 /* Transactional Memory TS optimize_for_synchronized attribute is
24679 equivalent to GNU transaction_callable. */
24680 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24681 TREE_PURPOSE (attribute)
24682 = get_identifier ("transaction_callable");
24683 /* Transactional Memory attributes are GNU attributes. */
24684 else if (tm_attr_to_mask (attr_id))
24685 TREE_PURPOSE (attribute) = attr_id;
24686 }
24687
24688 /* Now parse the optional argument clause of the attribute. */
24689
24690 if (token->type != CPP_OPEN_PAREN)
24691 return attribute;
24692
24693 {
24694 vec<tree, va_gc> *vec;
24695 int attr_flag = normal_attr;
24696
24697 if (attr_ns == get_identifier ("gnu")
24698 && attribute_takes_identifier_p (attr_id))
24699 /* A GNU attribute that takes an identifier in parameter. */
24700 attr_flag = id_attr;
24701
24702 vec = cp_parser_parenthesized_expression_list
24703 (parser, attr_flag, /*cast_p=*/false,
24704 /*allow_expansion_p=*/true,
24705 /*non_constant_p=*/NULL);
24706 if (vec == NULL)
24707 arguments = error_mark_node;
24708 else
24709 {
24710 arguments = build_tree_list_vec (vec);
24711 release_tree_vector (vec);
24712 }
24713
24714 if (arguments == error_mark_node)
24715 attribute = error_mark_node;
24716 else
24717 TREE_VALUE (attribute) = arguments;
24718 }
24719
24720 return attribute;
24721 }
24722
24723 /* Check that the attribute ATTRIBUTE appears at most once in the
24724 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24725 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24726 isn't implemented yet in GCC. */
24727
24728 static void
24729 cp_parser_check_std_attribute (tree attributes, tree attribute)
24730 {
24731 if (attributes)
24732 {
24733 tree name = get_attribute_name (attribute);
24734 if (is_attribute_p ("noreturn", name)
24735 && lookup_attribute ("noreturn", attributes))
24736 error ("attribute %<noreturn%> can appear at most once "
24737 "in an attribute-list");
24738 else if (is_attribute_p ("deprecated", name)
24739 && lookup_attribute ("deprecated", attributes))
24740 error ("attribute %<deprecated%> can appear at most once "
24741 "in an attribute-list");
24742 }
24743 }
24744
24745 /* Parse a list of standard C++-11 attributes.
24746
24747 attribute-list:
24748 attribute [opt]
24749 attribute-list , attribute[opt]
24750 attribute ...
24751 attribute-list , attribute ...
24752 */
24753
24754 static tree
24755 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24756 {
24757 tree attributes = NULL_TREE, attribute = NULL_TREE;
24758 cp_token *token = NULL;
24759
24760 while (true)
24761 {
24762 attribute = cp_parser_std_attribute (parser, attr_ns);
24763 if (attribute == error_mark_node)
24764 break;
24765 if (attribute != NULL_TREE)
24766 {
24767 cp_parser_check_std_attribute (attributes, attribute);
24768 TREE_CHAIN (attribute) = attributes;
24769 attributes = attribute;
24770 }
24771 token = cp_lexer_peek_token (parser->lexer);
24772 if (token->type == CPP_ELLIPSIS)
24773 {
24774 cp_lexer_consume_token (parser->lexer);
24775 if (attribute == NULL_TREE)
24776 error_at (token->location,
24777 "expected attribute before %<...%>");
24778 else
24779 TREE_VALUE (attribute)
24780 = make_pack_expansion (TREE_VALUE (attribute));
24781 token = cp_lexer_peek_token (parser->lexer);
24782 }
24783 if (token->type != CPP_COMMA)
24784 break;
24785 cp_lexer_consume_token (parser->lexer);
24786 }
24787 attributes = nreverse (attributes);
24788 return attributes;
24789 }
24790
24791 /* Parse a standard C++-11 attribute specifier.
24792
24793 attribute-specifier:
24794 [ [ attribute-using-prefix [opt] attribute-list ] ]
24795 alignment-specifier
24796
24797 attribute-using-prefix:
24798 using attribute-namespace :
24799
24800 alignment-specifier:
24801 alignas ( type-id ... [opt] )
24802 alignas ( alignment-expression ... [opt] ). */
24803
24804 static tree
24805 cp_parser_std_attribute_spec (cp_parser *parser)
24806 {
24807 tree attributes = NULL_TREE;
24808 cp_token *token = cp_lexer_peek_token (parser->lexer);
24809
24810 if (token->type == CPP_OPEN_SQUARE
24811 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24812 {
24813 tree attr_ns = NULL_TREE;
24814
24815 cp_lexer_consume_token (parser->lexer);
24816 cp_lexer_consume_token (parser->lexer);
24817
24818 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24819 {
24820 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24821 if (token->type == CPP_NAME)
24822 attr_ns = token->u.value;
24823 else if (token->type == CPP_KEYWORD)
24824 attr_ns = ridpointers[(int) token->keyword];
24825 else if (token->flags & NAMED_OP)
24826 attr_ns = get_identifier (cpp_type2name (token->type,
24827 token->flags));
24828 if (attr_ns
24829 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24830 {
24831 if (cxx_dialect < cxx1z
24832 && !in_system_header_at (input_location))
24833 pedwarn (input_location, 0,
24834 "attribute using prefix only available "
24835 "with -std=c++1z or -std=gnu++1z");
24836
24837 cp_lexer_consume_token (parser->lexer);
24838 cp_lexer_consume_token (parser->lexer);
24839 cp_lexer_consume_token (parser->lexer);
24840 }
24841 else
24842 attr_ns = NULL_TREE;
24843 }
24844
24845 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24846
24847 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24848 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24849 cp_parser_skip_to_end_of_statement (parser);
24850 else
24851 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24852 when we are sure that we have actually parsed them. */
24853 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24854 }
24855 else
24856 {
24857 tree alignas_expr;
24858
24859 /* Look for an alignment-specifier. */
24860
24861 token = cp_lexer_peek_token (parser->lexer);
24862
24863 if (token->type != CPP_KEYWORD
24864 || token->keyword != RID_ALIGNAS)
24865 return NULL_TREE;
24866
24867 cp_lexer_consume_token (parser->lexer);
24868 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24869
24870 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24871 {
24872 cp_parser_error (parser, "expected %<(%>");
24873 return error_mark_node;
24874 }
24875
24876 cp_parser_parse_tentatively (parser);
24877 alignas_expr = cp_parser_type_id (parser);
24878
24879 if (!cp_parser_parse_definitely (parser))
24880 {
24881 gcc_assert (alignas_expr == error_mark_node
24882 || alignas_expr == NULL_TREE);
24883
24884 alignas_expr =
24885 cp_parser_assignment_expression (parser);
24886 if (alignas_expr == error_mark_node)
24887 cp_parser_skip_to_end_of_statement (parser);
24888 if (alignas_expr == NULL_TREE
24889 || alignas_expr == error_mark_node)
24890 return alignas_expr;
24891 }
24892
24893 alignas_expr = cxx_alignas_expr (alignas_expr);
24894 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24895
24896 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24897 {
24898 cp_lexer_consume_token (parser->lexer);
24899 alignas_expr = make_pack_expansion (alignas_expr);
24900 }
24901
24902 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24903 {
24904 cp_parser_error (parser, "expected %<)%>");
24905 return error_mark_node;
24906 }
24907
24908 /* Build the C++-11 representation of an 'aligned'
24909 attribute. */
24910 attributes =
24911 build_tree_list (build_tree_list (get_identifier ("gnu"),
24912 get_identifier ("aligned")),
24913 alignas_expr);
24914 }
24915
24916 return attributes;
24917 }
24918
24919 /* Parse a standard C++-11 attribute-specifier-seq.
24920
24921 attribute-specifier-seq:
24922 attribute-specifier-seq [opt] attribute-specifier
24923 */
24924
24925 static tree
24926 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24927 {
24928 tree attr_specs = NULL_TREE;
24929 tree attr_last = NULL_TREE;
24930
24931 while (true)
24932 {
24933 tree attr_spec = cp_parser_std_attribute_spec (parser);
24934 if (attr_spec == NULL_TREE)
24935 break;
24936 if (attr_spec == error_mark_node)
24937 return error_mark_node;
24938
24939 if (attr_last)
24940 TREE_CHAIN (attr_last) = attr_spec;
24941 else
24942 attr_specs = attr_last = attr_spec;
24943 attr_last = tree_last (attr_last);
24944 }
24945
24946 return attr_specs;
24947 }
24948
24949 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24950 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24951 current value of the PEDANTIC flag, regardless of whether or not
24952 the `__extension__' keyword is present. The caller is responsible
24953 for restoring the value of the PEDANTIC flag. */
24954
24955 static bool
24956 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24957 {
24958 /* Save the old value of the PEDANTIC flag. */
24959 *saved_pedantic = pedantic;
24960
24961 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24962 {
24963 /* Consume the `__extension__' token. */
24964 cp_lexer_consume_token (parser->lexer);
24965 /* We're not being pedantic while the `__extension__' keyword is
24966 in effect. */
24967 pedantic = 0;
24968
24969 return true;
24970 }
24971
24972 return false;
24973 }
24974
24975 /* Parse a label declaration.
24976
24977 label-declaration:
24978 __label__ label-declarator-seq ;
24979
24980 label-declarator-seq:
24981 identifier , label-declarator-seq
24982 identifier */
24983
24984 static void
24985 cp_parser_label_declaration (cp_parser* parser)
24986 {
24987 /* Look for the `__label__' keyword. */
24988 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24989
24990 while (true)
24991 {
24992 tree identifier;
24993
24994 /* Look for an identifier. */
24995 identifier = cp_parser_identifier (parser);
24996 /* If we failed, stop. */
24997 if (identifier == error_mark_node)
24998 break;
24999 /* Declare it as a label. */
25000 finish_label_decl (identifier);
25001 /* If the next token is a `;', stop. */
25002 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25003 break;
25004 /* Look for the `,' separating the label declarations. */
25005 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25006 }
25007
25008 /* Look for the final `;'. */
25009 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25010 }
25011
25012 // -------------------------------------------------------------------------- //
25013 // Requires Clause
25014
25015 // Parse a requires clause.
25016 //
25017 // requires-clause:
25018 // 'requires' logical-or-expression
25019 //
25020 // The required logical-or-expression must be a constant expression. Note
25021 // that we don't check that the expression is constepxr here. We defer until
25022 // we analyze constraints and then, we only check atomic constraints.
25023 static tree
25024 cp_parser_requires_clause (cp_parser *parser)
25025 {
25026 // Parse the requires clause so that it is not automatically folded.
25027 ++processing_template_decl;
25028 tree expr = cp_parser_binary_expression (parser, false, false,
25029 PREC_NOT_OPERATOR, NULL);
25030 if (check_for_bare_parameter_packs (expr))
25031 expr = error_mark_node;
25032 --processing_template_decl;
25033 return expr;
25034 }
25035
25036 // Optionally parse a requires clause:
25037 static tree
25038 cp_parser_requires_clause_opt (cp_parser *parser)
25039 {
25040 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25041 if (tok->keyword != RID_REQUIRES)
25042 {
25043 if (!flag_concepts && tok->type == CPP_NAME
25044 && tok->u.value == ridpointers[RID_REQUIRES])
25045 {
25046 error_at (cp_lexer_peek_token (parser->lexer)->location,
25047 "%<requires%> only available with -fconcepts");
25048 /* Parse and discard the requires-clause. */
25049 cp_lexer_consume_token (parser->lexer);
25050 cp_parser_requires_clause (parser);
25051 }
25052 return NULL_TREE;
25053 }
25054 cp_lexer_consume_token (parser->lexer);
25055 return cp_parser_requires_clause (parser);
25056 }
25057
25058
25059 /*---------------------------------------------------------------------------
25060 Requires expressions
25061 ---------------------------------------------------------------------------*/
25062
25063 /* Parse a requires expression
25064
25065 requirement-expression:
25066 'requires' requirement-parameter-list [opt] requirement-body */
25067 static tree
25068 cp_parser_requires_expression (cp_parser *parser)
25069 {
25070 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25071 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25072
25073 /* A requires-expression shall appear only within a concept
25074 definition or a requires-clause.
25075
25076 TODO: Implement this diagnostic correctly. */
25077 if (!processing_template_decl)
25078 {
25079 error_at (loc, "a requires expression cannot appear outside a template");
25080 cp_parser_skip_to_end_of_statement (parser);
25081 return error_mark_node;
25082 }
25083
25084 tree parms, reqs;
25085 {
25086 /* Local parameters are delared as variables within the scope
25087 of the expression. They are not visible past the end of
25088 the expression. Expressions within the requires-expression
25089 are unevaluated. */
25090 struct scope_sentinel
25091 {
25092 scope_sentinel ()
25093 {
25094 ++cp_unevaluated_operand;
25095 begin_scope (sk_block, NULL_TREE);
25096 }
25097
25098 ~scope_sentinel ()
25099 {
25100 pop_bindings_and_leave_scope ();
25101 --cp_unevaluated_operand;
25102 }
25103 } s;
25104
25105 /* Parse the optional parameter list. */
25106 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25107 {
25108 parms = cp_parser_requirement_parameter_list (parser);
25109 if (parms == error_mark_node)
25110 return error_mark_node;
25111 }
25112 else
25113 parms = NULL_TREE;
25114
25115 /* Parse the requirement body. */
25116 reqs = cp_parser_requirement_body (parser);
25117 if (reqs == error_mark_node)
25118 return error_mark_node;
25119 }
25120
25121 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25122 the parm chain. */
25123 grokparms (parms, &parms);
25124 return finish_requires_expr (parms, reqs);
25125 }
25126
25127 /* Parse a parameterized requirement.
25128
25129 requirement-parameter-list:
25130 '(' parameter-declaration-clause ')' */
25131 static tree
25132 cp_parser_requirement_parameter_list (cp_parser *parser)
25133 {
25134 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25135 return error_mark_node;
25136
25137 tree parms = cp_parser_parameter_declaration_clause (parser);
25138
25139 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25140 return error_mark_node;
25141
25142 return parms;
25143 }
25144
25145 /* Parse the body of a requirement.
25146
25147 requirement-body:
25148 '{' requirement-list '}' */
25149 static tree
25150 cp_parser_requirement_body (cp_parser *parser)
25151 {
25152 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25153 return error_mark_node;
25154
25155 tree reqs = cp_parser_requirement_list (parser);
25156
25157 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25158 return error_mark_node;
25159
25160 return reqs;
25161 }
25162
25163 /* Parse a list of requirements.
25164
25165 requirement-list:
25166 requirement
25167 requirement-list ';' requirement[opt] */
25168 static tree
25169 cp_parser_requirement_list (cp_parser *parser)
25170 {
25171 tree result = NULL_TREE;
25172 while (true)
25173 {
25174 tree req = cp_parser_requirement (parser);
25175 if (req == error_mark_node)
25176 return error_mark_node;
25177
25178 result = tree_cons (NULL_TREE, req, result);
25179
25180 /* If we see a semi-colon, consume it. */
25181 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25182 cp_lexer_consume_token (parser->lexer);
25183
25184 /* Stop processing at the end of the list. */
25185 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25186 break;
25187 }
25188
25189 /* Reverse the order of requirements so they are analyzed in
25190 declaration order. */
25191 return nreverse (result);
25192 }
25193
25194 /* Parse a syntactic requirement or type requirement.
25195
25196 requirement:
25197 simple-requirement
25198 compound-requirement
25199 type-requirement
25200 nested-requirement */
25201 static tree
25202 cp_parser_requirement (cp_parser *parser)
25203 {
25204 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25205 return cp_parser_compound_requirement (parser);
25206 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25207 return cp_parser_type_requirement (parser);
25208 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25209 return cp_parser_nested_requirement (parser);
25210 else
25211 return cp_parser_simple_requirement (parser);
25212 }
25213
25214 /* Parse a simple requirement.
25215
25216 simple-requirement:
25217 expression ';' */
25218 static tree
25219 cp_parser_simple_requirement (cp_parser *parser)
25220 {
25221 tree expr = cp_parser_expression (parser, NULL, false, false);
25222 if (!expr || expr == error_mark_node)
25223 return error_mark_node;
25224
25225 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25226 return error_mark_node;
25227
25228 return finish_simple_requirement (expr);
25229 }
25230
25231 /* Parse a type requirement
25232
25233 type-requirement
25234 nested-name-specifier [opt] required-type-name ';'
25235
25236 required-type-name:
25237 type-name
25238 'template' [opt] simple-template-id */
25239 static tree
25240 cp_parser_type_requirement (cp_parser *parser)
25241 {
25242 cp_lexer_consume_token (parser->lexer);
25243
25244 // Save the scope before parsing name specifiers.
25245 tree saved_scope = parser->scope;
25246 tree saved_object_scope = parser->object_scope;
25247 tree saved_qualifying_scope = parser->qualifying_scope;
25248 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25249 cp_parser_nested_name_specifier_opt (parser,
25250 /*typename_keyword_p=*/true,
25251 /*check_dependency_p=*/false,
25252 /*type_p=*/true,
25253 /*is_declaration=*/false);
25254
25255 tree type;
25256 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25257 {
25258 cp_lexer_consume_token (parser->lexer);
25259 type = cp_parser_template_id (parser,
25260 /*template_keyword_p=*/true,
25261 /*check_dependency=*/false,
25262 /*tag_type=*/none_type,
25263 /*is_declaration=*/false);
25264 type = make_typename_type (parser->scope, type, typename_type,
25265 /*complain=*/tf_error);
25266 }
25267 else
25268 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25269
25270 if (TREE_CODE (type) == TYPE_DECL)
25271 type = TREE_TYPE (type);
25272
25273 parser->scope = saved_scope;
25274 parser->object_scope = saved_object_scope;
25275 parser->qualifying_scope = saved_qualifying_scope;
25276
25277 if (type == error_mark_node)
25278 cp_parser_skip_to_end_of_statement (parser);
25279
25280 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25281 return error_mark_node;
25282 if (type == error_mark_node)
25283 return error_mark_node;
25284
25285 return finish_type_requirement (type);
25286 }
25287
25288 /* Parse a compound requirement
25289
25290 compound-requirement:
25291 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25292 static tree
25293 cp_parser_compound_requirement (cp_parser *parser)
25294 {
25295 /* Parse an expression enclosed in '{ }'s. */
25296 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25297 return error_mark_node;
25298
25299 tree expr = cp_parser_expression (parser, NULL, false, false);
25300 if (!expr || expr == error_mark_node)
25301 return error_mark_node;
25302
25303 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25304 return error_mark_node;
25305
25306 /* Parse the optional noexcept. */
25307 bool noexcept_p = false;
25308 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25309 {
25310 cp_lexer_consume_token (parser->lexer);
25311 noexcept_p = true;
25312 }
25313
25314 /* Parse the optional trailing return type. */
25315 tree type = NULL_TREE;
25316 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25317 {
25318 cp_lexer_consume_token (parser->lexer);
25319 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25320 parser->in_result_type_constraint_p = true;
25321 type = cp_parser_trailing_type_id (parser);
25322 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25323 if (type == error_mark_node)
25324 return error_mark_node;
25325 }
25326
25327 return finish_compound_requirement (expr, type, noexcept_p);
25328 }
25329
25330 /* Parse a nested requirement. This is the same as a requires clause.
25331
25332 nested-requirement:
25333 requires-clause */
25334 static tree
25335 cp_parser_nested_requirement (cp_parser *parser)
25336 {
25337 cp_lexer_consume_token (parser->lexer);
25338 tree req = cp_parser_requires_clause (parser);
25339 if (req == error_mark_node)
25340 return error_mark_node;
25341 return finish_nested_requirement (req);
25342 }
25343
25344 /* Support Functions */
25345
25346 /* Return the appropriate prefer_type argument for lookup_name_real based on
25347 tag_type and template_mem_access. */
25348
25349 static inline int
25350 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25351 {
25352 /* DR 141: When looking in the current enclosing context for a template-name
25353 after -> or ., only consider class templates. */
25354 if (template_mem_access)
25355 return 2;
25356 switch (tag_type)
25357 {
25358 case none_type: return 0; // No preference.
25359 case scope_type: return 1; // Type or namespace.
25360 default: return 2; // Type only.
25361 }
25362 }
25363
25364 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25365 NAME should have one of the representations used for an
25366 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25367 is returned. If PARSER->SCOPE is a dependent type, then a
25368 SCOPE_REF is returned.
25369
25370 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25371 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25372 was formed. Abstractly, such entities should not be passed to this
25373 function, because they do not need to be looked up, but it is
25374 simpler to check for this special case here, rather than at the
25375 call-sites.
25376
25377 In cases not explicitly covered above, this function returns a
25378 DECL, OVERLOAD, or baselink representing the result of the lookup.
25379 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25380 is returned.
25381
25382 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25383 (e.g., "struct") that was used. In that case bindings that do not
25384 refer to types are ignored.
25385
25386 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25387 ignored.
25388
25389 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25390 are ignored.
25391
25392 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25393 types.
25394
25395 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25396 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25397 NULL_TREE otherwise. */
25398
25399 static cp_expr
25400 cp_parser_lookup_name (cp_parser *parser, tree name,
25401 enum tag_types tag_type,
25402 bool is_template,
25403 bool is_namespace,
25404 bool check_dependency,
25405 tree *ambiguous_decls,
25406 location_t name_location)
25407 {
25408 tree decl;
25409 tree object_type = parser->context->object_type;
25410
25411 /* Assume that the lookup will be unambiguous. */
25412 if (ambiguous_decls)
25413 *ambiguous_decls = NULL_TREE;
25414
25415 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25416 no longer valid. Note that if we are parsing tentatively, and
25417 the parse fails, OBJECT_TYPE will be automatically restored. */
25418 parser->context->object_type = NULL_TREE;
25419
25420 if (name == error_mark_node)
25421 return error_mark_node;
25422
25423 /* A template-id has already been resolved; there is no lookup to
25424 do. */
25425 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25426 return name;
25427 if (BASELINK_P (name))
25428 {
25429 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25430 == TEMPLATE_ID_EXPR);
25431 return name;
25432 }
25433
25434 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25435 it should already have been checked to make sure that the name
25436 used matches the type being destroyed. */
25437 if (TREE_CODE (name) == BIT_NOT_EXPR)
25438 {
25439 tree type;
25440
25441 /* Figure out to which type this destructor applies. */
25442 if (parser->scope)
25443 type = parser->scope;
25444 else if (object_type)
25445 type = object_type;
25446 else
25447 type = current_class_type;
25448 /* If that's not a class type, there is no destructor. */
25449 if (!type || !CLASS_TYPE_P (type))
25450 return error_mark_node;
25451 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25452 lazily_declare_fn (sfk_destructor, type);
25453 if (!CLASSTYPE_DESTRUCTORS (type))
25454 return error_mark_node;
25455 /* If it was a class type, return the destructor. */
25456 return CLASSTYPE_DESTRUCTORS (type);
25457 }
25458
25459 /* By this point, the NAME should be an ordinary identifier. If
25460 the id-expression was a qualified name, the qualifying scope is
25461 stored in PARSER->SCOPE at this point. */
25462 gcc_assert (identifier_p (name));
25463
25464 /* Perform the lookup. */
25465 if (parser->scope)
25466 {
25467 bool dependent_p;
25468
25469 if (parser->scope == error_mark_node)
25470 return error_mark_node;
25471
25472 /* If the SCOPE is dependent, the lookup must be deferred until
25473 the template is instantiated -- unless we are explicitly
25474 looking up names in uninstantiated templates. Even then, we
25475 cannot look up the name if the scope is not a class type; it
25476 might, for example, be a template type parameter. */
25477 dependent_p = (TYPE_P (parser->scope)
25478 && dependent_scope_p (parser->scope));
25479 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25480 && dependent_p)
25481 /* Defer lookup. */
25482 decl = error_mark_node;
25483 else
25484 {
25485 tree pushed_scope = NULL_TREE;
25486
25487 /* If PARSER->SCOPE is a dependent type, then it must be a
25488 class type, and we must not be checking dependencies;
25489 otherwise, we would have processed this lookup above. So
25490 that PARSER->SCOPE is not considered a dependent base by
25491 lookup_member, we must enter the scope here. */
25492 if (dependent_p)
25493 pushed_scope = push_scope (parser->scope);
25494
25495 /* If the PARSER->SCOPE is a template specialization, it
25496 may be instantiated during name lookup. In that case,
25497 errors may be issued. Even if we rollback the current
25498 tentative parse, those errors are valid. */
25499 decl = lookup_qualified_name (parser->scope, name,
25500 prefer_type_arg (tag_type),
25501 /*complain=*/true);
25502
25503 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25504 lookup result and the nested-name-specifier nominates a class C:
25505 * if the name specified after the nested-name-specifier, when
25506 looked up in C, is the injected-class-name of C (Clause 9), or
25507 * if the name specified after the nested-name-specifier is the
25508 same as the identifier or the simple-template-id's template-
25509 name in the last component of the nested-name-specifier,
25510 the name is instead considered to name the constructor of
25511 class C. [ Note: for example, the constructor is not an
25512 acceptable lookup result in an elaborated-type-specifier so
25513 the constructor would not be used in place of the
25514 injected-class-name. --end note ] Such a constructor name
25515 shall be used only in the declarator-id of a declaration that
25516 names a constructor or in a using-declaration. */
25517 if (tag_type == none_type
25518 && DECL_SELF_REFERENCE_P (decl)
25519 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25520 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25521 prefer_type_arg (tag_type),
25522 /*complain=*/true);
25523
25524 /* If we have a single function from a using decl, pull it out. */
25525 if (TREE_CODE (decl) == OVERLOAD
25526 && !really_overloaded_fn (decl))
25527 decl = OVL_FUNCTION (decl);
25528
25529 if (pushed_scope)
25530 pop_scope (pushed_scope);
25531 }
25532
25533 /* If the scope is a dependent type and either we deferred lookup or
25534 we did lookup but didn't find the name, rememeber the name. */
25535 if (decl == error_mark_node && TYPE_P (parser->scope)
25536 && dependent_type_p (parser->scope))
25537 {
25538 if (tag_type)
25539 {
25540 tree type;
25541
25542 /* The resolution to Core Issue 180 says that `struct
25543 A::B' should be considered a type-name, even if `A'
25544 is dependent. */
25545 type = make_typename_type (parser->scope, name, tag_type,
25546 /*complain=*/tf_error);
25547 if (type != error_mark_node)
25548 decl = TYPE_NAME (type);
25549 }
25550 else if (is_template
25551 && (cp_parser_next_token_ends_template_argument_p (parser)
25552 || cp_lexer_next_token_is (parser->lexer,
25553 CPP_CLOSE_PAREN)))
25554 decl = make_unbound_class_template (parser->scope,
25555 name, NULL_TREE,
25556 /*complain=*/tf_error);
25557 else
25558 decl = build_qualified_name (/*type=*/NULL_TREE,
25559 parser->scope, name,
25560 is_template);
25561 }
25562 parser->qualifying_scope = parser->scope;
25563 parser->object_scope = NULL_TREE;
25564 }
25565 else if (object_type)
25566 {
25567 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25568 OBJECT_TYPE is not a class. */
25569 if (CLASS_TYPE_P (object_type))
25570 /* If the OBJECT_TYPE is a template specialization, it may
25571 be instantiated during name lookup. In that case, errors
25572 may be issued. Even if we rollback the current tentative
25573 parse, those errors are valid. */
25574 decl = lookup_member (object_type,
25575 name,
25576 /*protect=*/0,
25577 prefer_type_arg (tag_type),
25578 tf_warning_or_error);
25579 else
25580 decl = NULL_TREE;
25581
25582 if (!decl)
25583 /* Look it up in the enclosing context. DR 141: When looking for a
25584 template-name after -> or ., only consider class templates. */
25585 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25586 /*nonclass=*/0,
25587 /*block_p=*/true, is_namespace, 0);
25588 if (object_type == unknown_type_node)
25589 /* The object is type-dependent, so we can't look anything up; we used
25590 this to get the DR 141 behavior. */
25591 object_type = NULL_TREE;
25592 parser->object_scope = object_type;
25593 parser->qualifying_scope = NULL_TREE;
25594 }
25595 else
25596 {
25597 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25598 /*nonclass=*/0,
25599 /*block_p=*/true, is_namespace, 0);
25600 parser->qualifying_scope = NULL_TREE;
25601 parser->object_scope = NULL_TREE;
25602 }
25603
25604 /* If the lookup failed, let our caller know. */
25605 if (!decl || decl == error_mark_node)
25606 return error_mark_node;
25607
25608 /* Pull out the template from an injected-class-name (or multiple). */
25609 if (is_template)
25610 decl = maybe_get_template_decl_from_type_decl (decl);
25611
25612 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25613 if (TREE_CODE (decl) == TREE_LIST)
25614 {
25615 if (ambiguous_decls)
25616 *ambiguous_decls = decl;
25617 /* The error message we have to print is too complicated for
25618 cp_parser_error, so we incorporate its actions directly. */
25619 if (!cp_parser_simulate_error (parser))
25620 {
25621 error_at (name_location, "reference to %qD is ambiguous",
25622 name);
25623 print_candidates (decl);
25624 }
25625 return error_mark_node;
25626 }
25627
25628 gcc_assert (DECL_P (decl)
25629 || TREE_CODE (decl) == OVERLOAD
25630 || TREE_CODE (decl) == SCOPE_REF
25631 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25632 || BASELINK_P (decl));
25633
25634 /* If we have resolved the name of a member declaration, check to
25635 see if the declaration is accessible. When the name resolves to
25636 set of overloaded functions, accessibility is checked when
25637 overload resolution is done.
25638
25639 During an explicit instantiation, access is not checked at all,
25640 as per [temp.explicit]. */
25641 if (DECL_P (decl))
25642 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25643
25644 maybe_record_typedef_use (decl);
25645
25646 return cp_expr (decl, name_location);
25647 }
25648
25649 /* Like cp_parser_lookup_name, but for use in the typical case where
25650 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25651 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25652
25653 static tree
25654 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25655 {
25656 return cp_parser_lookup_name (parser, name,
25657 none_type,
25658 /*is_template=*/false,
25659 /*is_namespace=*/false,
25660 /*check_dependency=*/true,
25661 /*ambiguous_decls=*/NULL,
25662 location);
25663 }
25664
25665 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25666 the current context, return the TYPE_DECL. If TAG_NAME_P is
25667 true, the DECL indicates the class being defined in a class-head,
25668 or declared in an elaborated-type-specifier.
25669
25670 Otherwise, return DECL. */
25671
25672 static tree
25673 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25674 {
25675 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25676 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25677
25678 struct A {
25679 template <typename T> struct B;
25680 };
25681
25682 template <typename T> struct A::B {};
25683
25684 Similarly, in an elaborated-type-specifier:
25685
25686 namespace N { struct X{}; }
25687
25688 struct A {
25689 template <typename T> friend struct N::X;
25690 };
25691
25692 However, if the DECL refers to a class type, and we are in
25693 the scope of the class, then the name lookup automatically
25694 finds the TYPE_DECL created by build_self_reference rather
25695 than a TEMPLATE_DECL. For example, in:
25696
25697 template <class T> struct S {
25698 S s;
25699 };
25700
25701 there is no need to handle such case. */
25702
25703 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25704 return DECL_TEMPLATE_RESULT (decl);
25705
25706 return decl;
25707 }
25708
25709 /* If too many, or too few, template-parameter lists apply to the
25710 declarator, issue an error message. Returns TRUE if all went well,
25711 and FALSE otherwise. */
25712
25713 static bool
25714 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25715 cp_declarator *declarator,
25716 location_t declarator_location)
25717 {
25718 switch (declarator->kind)
25719 {
25720 case cdk_id:
25721 {
25722 unsigned num_templates = 0;
25723 tree scope = declarator->u.id.qualifying_scope;
25724
25725 if (scope)
25726 num_templates = num_template_headers_for_class (scope);
25727 else if (TREE_CODE (declarator->u.id.unqualified_name)
25728 == TEMPLATE_ID_EXPR)
25729 /* If the DECLARATOR has the form `X<y>' then it uses one
25730 additional level of template parameters. */
25731 ++num_templates;
25732
25733 return cp_parser_check_template_parameters
25734 (parser, num_templates, declarator_location, declarator);
25735 }
25736
25737 case cdk_function:
25738 case cdk_array:
25739 case cdk_pointer:
25740 case cdk_reference:
25741 case cdk_ptrmem:
25742 return (cp_parser_check_declarator_template_parameters
25743 (parser, declarator->declarator, declarator_location));
25744
25745 case cdk_decomp:
25746 case cdk_error:
25747 return true;
25748
25749 default:
25750 gcc_unreachable ();
25751 }
25752 return false;
25753 }
25754
25755 /* NUM_TEMPLATES were used in the current declaration. If that is
25756 invalid, return FALSE and issue an error messages. Otherwise,
25757 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25758 declarator and we can print more accurate diagnostics. */
25759
25760 static bool
25761 cp_parser_check_template_parameters (cp_parser* parser,
25762 unsigned num_templates,
25763 location_t location,
25764 cp_declarator *declarator)
25765 {
25766 /* If there are the same number of template classes and parameter
25767 lists, that's OK. */
25768 if (parser->num_template_parameter_lists == num_templates)
25769 return true;
25770 /* If there are more, but only one more, then we are referring to a
25771 member template. That's OK too. */
25772 if (parser->num_template_parameter_lists == num_templates + 1)
25773 return true;
25774 /* If there are more template classes than parameter lists, we have
25775 something like:
25776
25777 template <class T> void S<T>::R<T>::f (); */
25778 if (parser->num_template_parameter_lists < num_templates)
25779 {
25780 if (declarator && !current_function_decl)
25781 error_at (location, "specializing member %<%T::%E%> "
25782 "requires %<template<>%> syntax",
25783 declarator->u.id.qualifying_scope,
25784 declarator->u.id.unqualified_name);
25785 else if (declarator)
25786 error_at (location, "invalid declaration of %<%T::%E%>",
25787 declarator->u.id.qualifying_scope,
25788 declarator->u.id.unqualified_name);
25789 else
25790 error_at (location, "too few template-parameter-lists");
25791 return false;
25792 }
25793 /* Otherwise, there are too many template parameter lists. We have
25794 something like:
25795
25796 template <class T> template <class U> void S::f(); */
25797 error_at (location, "too many template-parameter-lists");
25798 return false;
25799 }
25800
25801 /* Parse an optional `::' token indicating that the following name is
25802 from the global namespace. If so, PARSER->SCOPE is set to the
25803 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25804 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25805 Returns the new value of PARSER->SCOPE, if the `::' token is
25806 present, and NULL_TREE otherwise. */
25807
25808 static tree
25809 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25810 {
25811 cp_token *token;
25812
25813 /* Peek at the next token. */
25814 token = cp_lexer_peek_token (parser->lexer);
25815 /* If we're looking at a `::' token then we're starting from the
25816 global namespace, not our current location. */
25817 if (token->type == CPP_SCOPE)
25818 {
25819 /* Consume the `::' token. */
25820 cp_lexer_consume_token (parser->lexer);
25821 /* Set the SCOPE so that we know where to start the lookup. */
25822 parser->scope = global_namespace;
25823 parser->qualifying_scope = global_namespace;
25824 parser->object_scope = NULL_TREE;
25825
25826 return parser->scope;
25827 }
25828 else if (!current_scope_valid_p)
25829 {
25830 parser->scope = NULL_TREE;
25831 parser->qualifying_scope = NULL_TREE;
25832 parser->object_scope = NULL_TREE;
25833 }
25834
25835 return NULL_TREE;
25836 }
25837
25838 /* Returns TRUE if the upcoming token sequence is the start of a
25839 constructor declarator. If FRIEND_P is true, the declarator is
25840 preceded by the `friend' specifier. */
25841
25842 static bool
25843 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25844 {
25845 bool constructor_p;
25846 bool outside_class_specifier_p;
25847 tree nested_name_specifier;
25848 cp_token *next_token;
25849
25850 /* The common case is that this is not a constructor declarator, so
25851 try to avoid doing lots of work if at all possible. It's not
25852 valid declare a constructor at function scope. */
25853 if (parser->in_function_body)
25854 return false;
25855 /* And only certain tokens can begin a constructor declarator. */
25856 next_token = cp_lexer_peek_token (parser->lexer);
25857 if (next_token->type != CPP_NAME
25858 && next_token->type != CPP_SCOPE
25859 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25860 && next_token->type != CPP_TEMPLATE_ID)
25861 return false;
25862
25863 /* Parse tentatively; we are going to roll back all of the tokens
25864 consumed here. */
25865 cp_parser_parse_tentatively (parser);
25866 /* Assume that we are looking at a constructor declarator. */
25867 constructor_p = true;
25868
25869 /* Look for the optional `::' operator. */
25870 cp_parser_global_scope_opt (parser,
25871 /*current_scope_valid_p=*/false);
25872 /* Look for the nested-name-specifier. */
25873 nested_name_specifier
25874 = (cp_parser_nested_name_specifier_opt (parser,
25875 /*typename_keyword_p=*/false,
25876 /*check_dependency_p=*/false,
25877 /*type_p=*/false,
25878 /*is_declaration=*/false));
25879
25880 outside_class_specifier_p = (!at_class_scope_p ()
25881 || !TYPE_BEING_DEFINED (current_class_type)
25882 || friend_p);
25883
25884 /* Outside of a class-specifier, there must be a
25885 nested-name-specifier. */
25886 if (!nested_name_specifier && outside_class_specifier_p)
25887 constructor_p = false;
25888 else if (nested_name_specifier == error_mark_node)
25889 constructor_p = false;
25890
25891 /* If we have a class scope, this is easy; DR 147 says that S::S always
25892 names the constructor, and no other qualified name could. */
25893 if (constructor_p && nested_name_specifier
25894 && CLASS_TYPE_P (nested_name_specifier))
25895 {
25896 tree id = cp_parser_unqualified_id (parser,
25897 /*template_keyword_p=*/false,
25898 /*check_dependency_p=*/false,
25899 /*declarator_p=*/true,
25900 /*optional_p=*/false);
25901 if (is_overloaded_fn (id))
25902 id = DECL_NAME (get_first_fn (id));
25903 if (!constructor_name_p (id, nested_name_specifier))
25904 constructor_p = false;
25905 }
25906 /* If we still think that this might be a constructor-declarator,
25907 look for a class-name. */
25908 else if (constructor_p)
25909 {
25910 /* If we have:
25911
25912 template <typename T> struct S {
25913 S();
25914 };
25915
25916 we must recognize that the nested `S' names a class. */
25917 tree type_decl;
25918 type_decl = cp_parser_class_name (parser,
25919 /*typename_keyword_p=*/false,
25920 /*template_keyword_p=*/false,
25921 none_type,
25922 /*check_dependency_p=*/false,
25923 /*class_head_p=*/false,
25924 /*is_declaration=*/false);
25925 /* If there was no class-name, then this is not a constructor.
25926 Otherwise, if we are in a class-specifier and we aren't
25927 handling a friend declaration, check that its type matches
25928 current_class_type (c++/38313). Note: error_mark_node
25929 is left alone for error recovery purposes. */
25930 constructor_p = (!cp_parser_error_occurred (parser)
25931 && (outside_class_specifier_p
25932 || type_decl == error_mark_node
25933 || same_type_p (current_class_type,
25934 TREE_TYPE (type_decl))));
25935
25936 /* If we're still considering a constructor, we have to see a `(',
25937 to begin the parameter-declaration-clause, followed by either a
25938 `)', an `...', or a decl-specifier. We need to check for a
25939 type-specifier to avoid being fooled into thinking that:
25940
25941 S (f) (int);
25942
25943 is a constructor. (It is actually a function named `f' that
25944 takes one parameter (of type `int') and returns a value of type
25945 `S'. */
25946 if (constructor_p
25947 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25948 constructor_p = false;
25949
25950 if (constructor_p
25951 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25952 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25953 /* A parameter declaration begins with a decl-specifier,
25954 which is either the "attribute" keyword, a storage class
25955 specifier, or (usually) a type-specifier. */
25956 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25957 {
25958 tree type;
25959 tree pushed_scope = NULL_TREE;
25960 unsigned saved_num_template_parameter_lists;
25961
25962 /* Names appearing in the type-specifier should be looked up
25963 in the scope of the class. */
25964 if (current_class_type)
25965 type = NULL_TREE;
25966 else
25967 {
25968 type = TREE_TYPE (type_decl);
25969 if (TREE_CODE (type) == TYPENAME_TYPE)
25970 {
25971 type = resolve_typename_type (type,
25972 /*only_current_p=*/false);
25973 if (TREE_CODE (type) == TYPENAME_TYPE)
25974 {
25975 cp_parser_abort_tentative_parse (parser);
25976 return false;
25977 }
25978 }
25979 pushed_scope = push_scope (type);
25980 }
25981
25982 /* Inside the constructor parameter list, surrounding
25983 template-parameter-lists do not apply. */
25984 saved_num_template_parameter_lists
25985 = parser->num_template_parameter_lists;
25986 parser->num_template_parameter_lists = 0;
25987
25988 /* Look for the type-specifier. */
25989 cp_parser_type_specifier (parser,
25990 CP_PARSER_FLAGS_NONE,
25991 /*decl_specs=*/NULL,
25992 /*is_declarator=*/true,
25993 /*declares_class_or_enum=*/NULL,
25994 /*is_cv_qualifier=*/NULL);
25995
25996 parser->num_template_parameter_lists
25997 = saved_num_template_parameter_lists;
25998
25999 /* Leave the scope of the class. */
26000 if (pushed_scope)
26001 pop_scope (pushed_scope);
26002
26003 constructor_p = !cp_parser_error_occurred (parser);
26004 }
26005 }
26006
26007 /* We did not really want to consume any tokens. */
26008 cp_parser_abort_tentative_parse (parser);
26009
26010 return constructor_p;
26011 }
26012
26013 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26014 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26015 they must be performed once we are in the scope of the function.
26016
26017 Returns the function defined. */
26018
26019 static tree
26020 cp_parser_function_definition_from_specifiers_and_declarator
26021 (cp_parser* parser,
26022 cp_decl_specifier_seq *decl_specifiers,
26023 tree attributes,
26024 const cp_declarator *declarator)
26025 {
26026 tree fn;
26027 bool success_p;
26028
26029 /* Begin the function-definition. */
26030 success_p = start_function (decl_specifiers, declarator, attributes);
26031
26032 /* The things we're about to see are not directly qualified by any
26033 template headers we've seen thus far. */
26034 reset_specialization ();
26035
26036 /* If there were names looked up in the decl-specifier-seq that we
26037 did not check, check them now. We must wait until we are in the
26038 scope of the function to perform the checks, since the function
26039 might be a friend. */
26040 perform_deferred_access_checks (tf_warning_or_error);
26041
26042 if (success_p)
26043 {
26044 cp_finalize_omp_declare_simd (parser, current_function_decl);
26045 parser->omp_declare_simd = NULL;
26046 cp_finalize_oacc_routine (parser, current_function_decl, true);
26047 parser->oacc_routine = NULL;
26048 }
26049
26050 if (!success_p)
26051 {
26052 /* Skip the entire function. */
26053 cp_parser_skip_to_end_of_block_or_statement (parser);
26054 fn = error_mark_node;
26055 }
26056 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26057 {
26058 /* Seen already, skip it. An error message has already been output. */
26059 cp_parser_skip_to_end_of_block_or_statement (parser);
26060 fn = current_function_decl;
26061 current_function_decl = NULL_TREE;
26062 /* If this is a function from a class, pop the nested class. */
26063 if (current_class_name)
26064 pop_nested_class ();
26065 }
26066 else
26067 {
26068 timevar_id_t tv;
26069 if (DECL_DECLARED_INLINE_P (current_function_decl))
26070 tv = TV_PARSE_INLINE;
26071 else
26072 tv = TV_PARSE_FUNC;
26073 timevar_push (tv);
26074 fn = cp_parser_function_definition_after_declarator (parser,
26075 /*inline_p=*/false);
26076 timevar_pop (tv);
26077 }
26078
26079 return fn;
26080 }
26081
26082 /* Parse the part of a function-definition that follows the
26083 declarator. INLINE_P is TRUE iff this function is an inline
26084 function defined within a class-specifier.
26085
26086 Returns the function defined. */
26087
26088 static tree
26089 cp_parser_function_definition_after_declarator (cp_parser* parser,
26090 bool inline_p)
26091 {
26092 tree fn;
26093 bool ctor_initializer_p = false;
26094 bool saved_in_unbraced_linkage_specification_p;
26095 bool saved_in_function_body;
26096 unsigned saved_num_template_parameter_lists;
26097 cp_token *token;
26098 bool fully_implicit_function_template_p
26099 = parser->fully_implicit_function_template_p;
26100 parser->fully_implicit_function_template_p = false;
26101 tree implicit_template_parms
26102 = parser->implicit_template_parms;
26103 parser->implicit_template_parms = 0;
26104 cp_binding_level* implicit_template_scope
26105 = parser->implicit_template_scope;
26106 parser->implicit_template_scope = 0;
26107
26108 saved_in_function_body = parser->in_function_body;
26109 parser->in_function_body = true;
26110 /* If the next token is `return', then the code may be trying to
26111 make use of the "named return value" extension that G++ used to
26112 support. */
26113 token = cp_lexer_peek_token (parser->lexer);
26114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26115 {
26116 /* Consume the `return' keyword. */
26117 cp_lexer_consume_token (parser->lexer);
26118 /* Look for the identifier that indicates what value is to be
26119 returned. */
26120 cp_parser_identifier (parser);
26121 /* Issue an error message. */
26122 error_at (token->location,
26123 "named return values are no longer supported");
26124 /* Skip tokens until we reach the start of the function body. */
26125 while (true)
26126 {
26127 cp_token *token = cp_lexer_peek_token (parser->lexer);
26128 if (token->type == CPP_OPEN_BRACE
26129 || token->type == CPP_EOF
26130 || token->type == CPP_PRAGMA_EOL)
26131 break;
26132 cp_lexer_consume_token (parser->lexer);
26133 }
26134 }
26135 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26136 anything declared inside `f'. */
26137 saved_in_unbraced_linkage_specification_p
26138 = parser->in_unbraced_linkage_specification_p;
26139 parser->in_unbraced_linkage_specification_p = false;
26140 /* Inside the function, surrounding template-parameter-lists do not
26141 apply. */
26142 saved_num_template_parameter_lists
26143 = parser->num_template_parameter_lists;
26144 parser->num_template_parameter_lists = 0;
26145
26146 start_lambda_scope (current_function_decl);
26147
26148 /* If the next token is `try', `__transaction_atomic', or
26149 `__transaction_relaxed`, then we are looking at either function-try-block
26150 or function-transaction-block. Note that all of these include the
26151 function-body. */
26152 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26153 ctor_initializer_p = cp_parser_function_transaction (parser,
26154 RID_TRANSACTION_ATOMIC);
26155 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26156 RID_TRANSACTION_RELAXED))
26157 ctor_initializer_p = cp_parser_function_transaction (parser,
26158 RID_TRANSACTION_RELAXED);
26159 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26160 ctor_initializer_p = cp_parser_function_try_block (parser);
26161 else
26162 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26163 (parser, /*in_function_try_block=*/false);
26164
26165 finish_lambda_scope ();
26166
26167 /* Finish the function. */
26168 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26169 (inline_p ? 2 : 0));
26170 /* Generate code for it, if necessary. */
26171 expand_or_defer_fn (fn);
26172 /* Restore the saved values. */
26173 parser->in_unbraced_linkage_specification_p
26174 = saved_in_unbraced_linkage_specification_p;
26175 parser->num_template_parameter_lists
26176 = saved_num_template_parameter_lists;
26177 parser->in_function_body = saved_in_function_body;
26178
26179 parser->fully_implicit_function_template_p
26180 = fully_implicit_function_template_p;
26181 parser->implicit_template_parms
26182 = implicit_template_parms;
26183 parser->implicit_template_scope
26184 = implicit_template_scope;
26185
26186 if (parser->fully_implicit_function_template_p)
26187 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26188
26189 return fn;
26190 }
26191
26192 /* Parse a template-declaration body (following argument list). */
26193
26194 static void
26195 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26196 tree parameter_list,
26197 bool member_p)
26198 {
26199 tree decl = NULL_TREE;
26200 bool friend_p = false;
26201
26202 /* We just processed one more parameter list. */
26203 ++parser->num_template_parameter_lists;
26204
26205 /* Get the deferred access checks from the parameter list. These
26206 will be checked once we know what is being declared, as for a
26207 member template the checks must be performed in the scope of the
26208 class containing the member. */
26209 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26210
26211 /* Tentatively parse for a new template parameter list, which can either be
26212 the template keyword or a template introduction. */
26213 if (cp_parser_template_declaration_after_export (parser, member_p))
26214 /* OK */;
26215 else if (cxx_dialect >= cxx11
26216 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26217 decl = cp_parser_alias_declaration (parser);
26218 else
26219 {
26220 /* There are no access checks when parsing a template, as we do not
26221 know if a specialization will be a friend. */
26222 push_deferring_access_checks (dk_no_check);
26223 cp_token *token = cp_lexer_peek_token (parser->lexer);
26224 decl = cp_parser_single_declaration (parser,
26225 checks,
26226 member_p,
26227 /*explicit_specialization_p=*/false,
26228 &friend_p);
26229 pop_deferring_access_checks ();
26230
26231 /* If this is a member template declaration, let the front
26232 end know. */
26233 if (member_p && !friend_p && decl)
26234 {
26235 if (TREE_CODE (decl) == TYPE_DECL)
26236 cp_parser_check_access_in_redeclaration (decl, token->location);
26237
26238 decl = finish_member_template_decl (decl);
26239 }
26240 else if (friend_p && decl
26241 && DECL_DECLARES_TYPE_P (decl))
26242 make_friend_class (current_class_type, TREE_TYPE (decl),
26243 /*complain=*/true);
26244 }
26245 /* We are done with the current parameter list. */
26246 --parser->num_template_parameter_lists;
26247
26248 pop_deferring_access_checks ();
26249
26250 /* Finish up. */
26251 finish_template_decl (parameter_list);
26252
26253 /* Check the template arguments for a literal operator template. */
26254 if (decl
26255 && DECL_DECLARES_FUNCTION_P (decl)
26256 && UDLIT_OPER_P (DECL_NAME (decl)))
26257 {
26258 bool ok = true;
26259 if (parameter_list == NULL_TREE)
26260 ok = false;
26261 else
26262 {
26263 int num_parms = TREE_VEC_LENGTH (parameter_list);
26264 if (num_parms == 1)
26265 {
26266 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26267 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26268 if (TREE_TYPE (parm) != char_type_node
26269 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26270 ok = false;
26271 }
26272 else if (num_parms == 2 && cxx_dialect >= cxx14)
26273 {
26274 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26275 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26276 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26277 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26278 if (parm == error_mark_node
26279 || TREE_TYPE (parm) != TREE_TYPE (type)
26280 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26281 ok = false;
26282 }
26283 else
26284 ok = false;
26285 }
26286 if (!ok)
26287 {
26288 if (cxx_dialect >= cxx14)
26289 error ("literal operator template %qD has invalid parameter list."
26290 " Expected non-type template argument pack <char...>"
26291 " or <typename CharT, CharT...>",
26292 decl);
26293 else
26294 error ("literal operator template %qD has invalid parameter list."
26295 " Expected non-type template argument pack <char...>",
26296 decl);
26297 }
26298 }
26299
26300 /* Register member declarations. */
26301 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26302 finish_member_declaration (decl);
26303 /* If DECL is a function template, we must return to parse it later.
26304 (Even though there is no definition, there might be default
26305 arguments that need handling.) */
26306 if (member_p && decl
26307 && DECL_DECLARES_FUNCTION_P (decl))
26308 vec_safe_push (unparsed_funs_with_definitions, decl);
26309 }
26310
26311 /* Parse a template introduction header for a template-declaration. Returns
26312 false if tentative parse fails. */
26313
26314 static bool
26315 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26316 {
26317 cp_parser_parse_tentatively (parser);
26318
26319 tree saved_scope = parser->scope;
26320 tree saved_object_scope = parser->object_scope;
26321 tree saved_qualifying_scope = parser->qualifying_scope;
26322
26323 /* Look for the optional `::' operator. */
26324 cp_parser_global_scope_opt (parser,
26325 /*current_scope_valid_p=*/false);
26326 /* Look for the nested-name-specifier. */
26327 cp_parser_nested_name_specifier_opt (parser,
26328 /*typename_keyword_p=*/false,
26329 /*check_dependency_p=*/true,
26330 /*type_p=*/false,
26331 /*is_declaration=*/false);
26332
26333 cp_token *token = cp_lexer_peek_token (parser->lexer);
26334 tree concept_name = cp_parser_identifier (parser);
26335
26336 /* Look up the concept for which we will be matching
26337 template parameters. */
26338 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26339 token->location);
26340 parser->scope = saved_scope;
26341 parser->object_scope = saved_object_scope;
26342 parser->qualifying_scope = saved_qualifying_scope;
26343
26344 if (concept_name == error_mark_node)
26345 cp_parser_simulate_error (parser);
26346
26347 /* Look for opening brace for introduction. */
26348 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26349
26350 if (!cp_parser_parse_definitely (parser))
26351 return false;
26352
26353 push_deferring_access_checks (dk_deferred);
26354
26355 /* Build vector of placeholder parameters and grab
26356 matching identifiers. */
26357 tree introduction_list = cp_parser_introduction_list (parser);
26358
26359 /* The introduction-list shall not be empty. */
26360 int nargs = TREE_VEC_LENGTH (introduction_list);
26361 if (nargs == 0)
26362 {
26363 error ("empty introduction-list");
26364 return true;
26365 }
26366
26367 /* Look for closing brace for introduction. */
26368 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26369 return true;
26370
26371 if (tmpl_decl == error_mark_node)
26372 {
26373 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26374 token->location);
26375 return true;
26376 }
26377
26378 /* Build and associate the constraint. */
26379 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26380 if (parms && parms != error_mark_node)
26381 {
26382 cp_parser_template_declaration_after_parameters (parser, parms,
26383 member_p);
26384 return true;
26385 }
26386
26387 error_at (token->location, "no matching concept for template-introduction");
26388 return true;
26389 }
26390
26391 /* Parse a normal template-declaration following the template keyword. */
26392
26393 static void
26394 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26395 {
26396 tree parameter_list;
26397 bool need_lang_pop;
26398 location_t location = input_location;
26399
26400 /* Look for the `<' token. */
26401 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26402 return;
26403 if (at_class_scope_p () && current_function_decl)
26404 {
26405 /* 14.5.2.2 [temp.mem]
26406
26407 A local class shall not have member templates. */
26408 error_at (location,
26409 "invalid declaration of member template in local class");
26410 cp_parser_skip_to_end_of_block_or_statement (parser);
26411 return;
26412 }
26413 /* [temp]
26414
26415 A template ... shall not have C linkage. */
26416 if (current_lang_name == lang_name_c)
26417 {
26418 error_at (location, "template with C linkage");
26419 /* Give it C++ linkage to avoid confusing other parts of the
26420 front end. */
26421 push_lang_context (lang_name_cplusplus);
26422 need_lang_pop = true;
26423 }
26424 else
26425 need_lang_pop = false;
26426
26427 /* We cannot perform access checks on the template parameter
26428 declarations until we know what is being declared, just as we
26429 cannot check the decl-specifier list. */
26430 push_deferring_access_checks (dk_deferred);
26431
26432 /* If the next token is `>', then we have an invalid
26433 specialization. Rather than complain about an invalid template
26434 parameter, issue an error message here. */
26435 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26436 {
26437 cp_parser_error (parser, "invalid explicit specialization");
26438 begin_specialization ();
26439 parameter_list = NULL_TREE;
26440 }
26441 else
26442 {
26443 /* Parse the template parameters. */
26444 parameter_list = cp_parser_template_parameter_list (parser);
26445 }
26446
26447 /* Look for the `>'. */
26448 cp_parser_skip_to_end_of_template_parameter_list (parser);
26449
26450 /* Manage template requirements */
26451 if (flag_concepts)
26452 {
26453 tree reqs = get_shorthand_constraints (current_template_parms);
26454 if (tree r = cp_parser_requires_clause_opt (parser))
26455 reqs = conjoin_constraints (reqs, normalize_expression (r));
26456 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26457 }
26458
26459 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26460 member_p);
26461
26462 /* For the erroneous case of a template with C linkage, we pushed an
26463 implicit C++ linkage scope; exit that scope now. */
26464 if (need_lang_pop)
26465 pop_lang_context ();
26466 }
26467
26468 /* Parse a template-declaration, assuming that the `export' (and
26469 `extern') keywords, if present, has already been scanned. MEMBER_P
26470 is as for cp_parser_template_declaration. */
26471
26472 static bool
26473 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26474 {
26475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26476 {
26477 cp_lexer_consume_token (parser->lexer);
26478 cp_parser_explicit_template_declaration (parser, member_p);
26479 return true;
26480 }
26481 else if (flag_concepts)
26482 return cp_parser_template_introduction (parser, member_p);
26483
26484 return false;
26485 }
26486
26487 /* Perform the deferred access checks from a template-parameter-list.
26488 CHECKS is a TREE_LIST of access checks, as returned by
26489 get_deferred_access_checks. */
26490
26491 static void
26492 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26493 {
26494 ++processing_template_parmlist;
26495 perform_access_checks (checks, tf_warning_or_error);
26496 --processing_template_parmlist;
26497 }
26498
26499 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26500 `function-definition' sequence that follows a template header.
26501 If MEMBER_P is true, this declaration appears in a class scope.
26502
26503 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26504 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26505
26506 static tree
26507 cp_parser_single_declaration (cp_parser* parser,
26508 vec<deferred_access_check, va_gc> *checks,
26509 bool member_p,
26510 bool explicit_specialization_p,
26511 bool* friend_p)
26512 {
26513 int declares_class_or_enum;
26514 tree decl = NULL_TREE;
26515 cp_decl_specifier_seq decl_specifiers;
26516 bool function_definition_p = false;
26517 cp_token *decl_spec_token_start;
26518
26519 /* This function is only used when processing a template
26520 declaration. */
26521 gcc_assert (innermost_scope_kind () == sk_template_parms
26522 || innermost_scope_kind () == sk_template_spec);
26523
26524 /* Defer access checks until we know what is being declared. */
26525 push_deferring_access_checks (dk_deferred);
26526
26527 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26528 alternative. */
26529 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26530 cp_parser_decl_specifier_seq (parser,
26531 CP_PARSER_FLAGS_OPTIONAL,
26532 &decl_specifiers,
26533 &declares_class_or_enum);
26534 if (friend_p)
26535 *friend_p = cp_parser_friend_p (&decl_specifiers);
26536
26537 /* There are no template typedefs. */
26538 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26539 {
26540 error_at (decl_spec_token_start->location,
26541 "template declaration of %<typedef%>");
26542 decl = error_mark_node;
26543 }
26544
26545 /* Gather up the access checks that occurred the
26546 decl-specifier-seq. */
26547 stop_deferring_access_checks ();
26548
26549 /* Check for the declaration of a template class. */
26550 if (declares_class_or_enum)
26551 {
26552 if (cp_parser_declares_only_class_p (parser)
26553 || (declares_class_or_enum & 2))
26554 {
26555 // If this is a declaration, but not a definition, associate
26556 // any constraints with the type declaration. Constraints
26557 // are associated with definitions in cp_parser_class_specifier.
26558 if (declares_class_or_enum == 1)
26559 associate_classtype_constraints (decl_specifiers.type);
26560
26561 decl = shadow_tag (&decl_specifiers);
26562
26563 /* In this case:
26564
26565 struct C {
26566 friend template <typename T> struct A<T>::B;
26567 };
26568
26569 A<T>::B will be represented by a TYPENAME_TYPE, and
26570 therefore not recognized by shadow_tag. */
26571 if (friend_p && *friend_p
26572 && !decl
26573 && decl_specifiers.type
26574 && TYPE_P (decl_specifiers.type))
26575 decl = decl_specifiers.type;
26576
26577 if (decl && decl != error_mark_node)
26578 decl = TYPE_NAME (decl);
26579 else
26580 decl = error_mark_node;
26581
26582 /* Perform access checks for template parameters. */
26583 cp_parser_perform_template_parameter_access_checks (checks);
26584
26585 /* Give a helpful diagnostic for
26586 template <class T> struct A { } a;
26587 if we aren't already recovering from an error. */
26588 if (!cp_parser_declares_only_class_p (parser)
26589 && !seen_error ())
26590 {
26591 error_at (cp_lexer_peek_token (parser->lexer)->location,
26592 "a class template declaration must not declare "
26593 "anything else");
26594 cp_parser_skip_to_end_of_block_or_statement (parser);
26595 goto out;
26596 }
26597 }
26598 }
26599
26600 /* Complain about missing 'typename' or other invalid type names. */
26601 if (!decl_specifiers.any_type_specifiers_p
26602 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26603 {
26604 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26605 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26606 the rest of this declaration. */
26607 decl = error_mark_node;
26608 goto out;
26609 }
26610
26611 /* If it's not a template class, try for a template function. If
26612 the next token is a `;', then this declaration does not declare
26613 anything. But, if there were errors in the decl-specifiers, then
26614 the error might well have come from an attempted class-specifier.
26615 In that case, there's no need to warn about a missing declarator. */
26616 if (!decl
26617 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26618 || decl_specifiers.type != error_mark_node))
26619 {
26620 decl = cp_parser_init_declarator (parser,
26621 &decl_specifiers,
26622 checks,
26623 /*function_definition_allowed_p=*/true,
26624 member_p,
26625 declares_class_or_enum,
26626 &function_definition_p,
26627 NULL, NULL, NULL);
26628
26629 /* 7.1.1-1 [dcl.stc]
26630
26631 A storage-class-specifier shall not be specified in an explicit
26632 specialization... */
26633 if (decl
26634 && explicit_specialization_p
26635 && decl_specifiers.storage_class != sc_none)
26636 {
26637 error_at (decl_spec_token_start->location,
26638 "explicit template specialization cannot have a storage class");
26639 decl = error_mark_node;
26640 }
26641
26642 if (decl && VAR_P (decl))
26643 check_template_variable (decl);
26644 }
26645
26646 /* Look for a trailing `;' after the declaration. */
26647 if (!function_definition_p
26648 && (decl == error_mark_node
26649 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26650 cp_parser_skip_to_end_of_block_or_statement (parser);
26651
26652 out:
26653 pop_deferring_access_checks ();
26654
26655 /* Clear any current qualification; whatever comes next is the start
26656 of something new. */
26657 parser->scope = NULL_TREE;
26658 parser->qualifying_scope = NULL_TREE;
26659 parser->object_scope = NULL_TREE;
26660
26661 return decl;
26662 }
26663
26664 /* Parse a cast-expression that is not the operand of a unary "&". */
26665
26666 static cp_expr
26667 cp_parser_simple_cast_expression (cp_parser *parser)
26668 {
26669 return cp_parser_cast_expression (parser, /*address_p=*/false,
26670 /*cast_p=*/false, /*decltype*/false, NULL);
26671 }
26672
26673 /* Parse a functional cast to TYPE. Returns an expression
26674 representing the cast. */
26675
26676 static cp_expr
26677 cp_parser_functional_cast (cp_parser* parser, tree type)
26678 {
26679 vec<tree, va_gc> *vec;
26680 tree expression_list;
26681 cp_expr cast;
26682 bool nonconst_p;
26683
26684 location_t start_loc = input_location;
26685
26686 if (!type)
26687 type = error_mark_node;
26688
26689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26690 {
26691 cp_lexer_set_source_position (parser->lexer);
26692 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26693 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26694 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26695 if (TREE_CODE (type) == TYPE_DECL)
26696 type = TREE_TYPE (type);
26697
26698 cast = finish_compound_literal (type, expression_list,
26699 tf_warning_or_error);
26700 /* Create a location of the form:
26701 type_name{i, f}
26702 ^~~~~~~~~~~~~~~
26703 with caret == start at the start of the type name,
26704 finishing at the closing brace. */
26705 location_t finish_loc
26706 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26707 location_t combined_loc = make_location (start_loc, start_loc,
26708 finish_loc);
26709 cast.set_location (combined_loc);
26710 return cast;
26711 }
26712
26713
26714 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26715 /*cast_p=*/true,
26716 /*allow_expansion_p=*/true,
26717 /*non_constant_p=*/NULL);
26718 if (vec == NULL)
26719 expression_list = error_mark_node;
26720 else
26721 {
26722 expression_list = build_tree_list_vec (vec);
26723 release_tree_vector (vec);
26724 }
26725
26726 cast = build_functional_cast (type, expression_list,
26727 tf_warning_or_error);
26728 /* [expr.const]/1: In an integral constant expression "only type
26729 conversions to integral or enumeration type can be used". */
26730 if (TREE_CODE (type) == TYPE_DECL)
26731 type = TREE_TYPE (type);
26732 if (cast != error_mark_node
26733 && !cast_valid_in_integral_constant_expression_p (type)
26734 && cp_parser_non_integral_constant_expression (parser,
26735 NIC_CONSTRUCTOR))
26736 return error_mark_node;
26737
26738 /* Create a location of the form:
26739 float(i)
26740 ^~~~~~~~
26741 with caret == start at the start of the type name,
26742 finishing at the closing paren. */
26743 location_t finish_loc
26744 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26745 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26746 cast.set_location (combined_loc);
26747 return cast;
26748 }
26749
26750 /* Save the tokens that make up the body of a member function defined
26751 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26752 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26753 specifiers applied to the declaration. Returns the FUNCTION_DECL
26754 for the member function. */
26755
26756 static tree
26757 cp_parser_save_member_function_body (cp_parser* parser,
26758 cp_decl_specifier_seq *decl_specifiers,
26759 cp_declarator *declarator,
26760 tree attributes)
26761 {
26762 cp_token *first;
26763 cp_token *last;
26764 tree fn;
26765 bool function_try_block = false;
26766
26767 /* Create the FUNCTION_DECL. */
26768 fn = grokmethod (decl_specifiers, declarator, attributes);
26769 cp_finalize_omp_declare_simd (parser, fn);
26770 cp_finalize_oacc_routine (parser, fn, true);
26771 /* If something went badly wrong, bail out now. */
26772 if (fn == error_mark_node)
26773 {
26774 /* If there's a function-body, skip it. */
26775 if (cp_parser_token_starts_function_definition_p
26776 (cp_lexer_peek_token (parser->lexer)))
26777 cp_parser_skip_to_end_of_block_or_statement (parser);
26778 return error_mark_node;
26779 }
26780
26781 /* Remember it, if there default args to post process. */
26782 cp_parser_save_default_args (parser, fn);
26783
26784 /* Save away the tokens that make up the body of the
26785 function. */
26786 first = parser->lexer->next_token;
26787
26788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26789 cp_lexer_consume_token (parser->lexer);
26790 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26791 RID_TRANSACTION_ATOMIC))
26792 {
26793 cp_lexer_consume_token (parser->lexer);
26794 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26796 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26797 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26798 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26799 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26800 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26801 {
26802 cp_lexer_consume_token (parser->lexer);
26803 cp_lexer_consume_token (parser->lexer);
26804 cp_lexer_consume_token (parser->lexer);
26805 cp_lexer_consume_token (parser->lexer);
26806 cp_lexer_consume_token (parser->lexer);
26807 }
26808 else
26809 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26810 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26811 {
26812 cp_lexer_consume_token (parser->lexer);
26813 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26814 break;
26815 }
26816 }
26817
26818 /* Handle function try blocks. */
26819 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26820 {
26821 cp_lexer_consume_token (parser->lexer);
26822 function_try_block = true;
26823 }
26824 /* We can have braced-init-list mem-initializers before the fn body. */
26825 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26826 {
26827 cp_lexer_consume_token (parser->lexer);
26828 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26829 {
26830 /* cache_group will stop after an un-nested { } pair, too. */
26831 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26832 break;
26833
26834 /* variadic mem-inits have ... after the ')'. */
26835 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26836 cp_lexer_consume_token (parser->lexer);
26837 }
26838 }
26839 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26840 /* Handle function try blocks. */
26841 if (function_try_block)
26842 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26843 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26844 last = parser->lexer->next_token;
26845
26846 /* Save away the inline definition; we will process it when the
26847 class is complete. */
26848 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26849 DECL_PENDING_INLINE_P (fn) = 1;
26850
26851 /* We need to know that this was defined in the class, so that
26852 friend templates are handled correctly. */
26853 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26854
26855 /* Add FN to the queue of functions to be parsed later. */
26856 vec_safe_push (unparsed_funs_with_definitions, fn);
26857
26858 return fn;
26859 }
26860
26861 /* Save the tokens that make up the in-class initializer for a non-static
26862 data member. Returns a DEFAULT_ARG. */
26863
26864 static tree
26865 cp_parser_save_nsdmi (cp_parser* parser)
26866 {
26867 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26868 }
26869
26870 /* Parse a template-argument-list, as well as the trailing ">" (but
26871 not the opening "<"). See cp_parser_template_argument_list for the
26872 return value. */
26873
26874 static tree
26875 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26876 {
26877 tree arguments;
26878 tree saved_scope;
26879 tree saved_qualifying_scope;
26880 tree saved_object_scope;
26881 bool saved_greater_than_is_operator_p;
26882 int saved_unevaluated_operand;
26883 int saved_inhibit_evaluation_warnings;
26884
26885 /* [temp.names]
26886
26887 When parsing a template-id, the first non-nested `>' is taken as
26888 the end of the template-argument-list rather than a greater-than
26889 operator. */
26890 saved_greater_than_is_operator_p
26891 = parser->greater_than_is_operator_p;
26892 parser->greater_than_is_operator_p = false;
26893 /* Parsing the argument list may modify SCOPE, so we save it
26894 here. */
26895 saved_scope = parser->scope;
26896 saved_qualifying_scope = parser->qualifying_scope;
26897 saved_object_scope = parser->object_scope;
26898 /* We need to evaluate the template arguments, even though this
26899 template-id may be nested within a "sizeof". */
26900 saved_unevaluated_operand = cp_unevaluated_operand;
26901 cp_unevaluated_operand = 0;
26902 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26903 c_inhibit_evaluation_warnings = 0;
26904 /* Parse the template-argument-list itself. */
26905 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26906 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26907 arguments = NULL_TREE;
26908 else
26909 arguments = cp_parser_template_argument_list (parser);
26910 /* Look for the `>' that ends the template-argument-list. If we find
26911 a '>>' instead, it's probably just a typo. */
26912 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26913 {
26914 if (cxx_dialect != cxx98)
26915 {
26916 /* In C++0x, a `>>' in a template argument list or cast
26917 expression is considered to be two separate `>'
26918 tokens. So, change the current token to a `>', but don't
26919 consume it: it will be consumed later when the outer
26920 template argument list (or cast expression) is parsed.
26921 Note that this replacement of `>' for `>>' is necessary
26922 even if we are parsing tentatively: in the tentative
26923 case, after calling
26924 cp_parser_enclosed_template_argument_list we will always
26925 throw away all of the template arguments and the first
26926 closing `>', either because the template argument list
26927 was erroneous or because we are replacing those tokens
26928 with a CPP_TEMPLATE_ID token. The second `>' (which will
26929 not have been thrown away) is needed either to close an
26930 outer template argument list or to complete a new-style
26931 cast. */
26932 cp_token *token = cp_lexer_peek_token (parser->lexer);
26933 token->type = CPP_GREATER;
26934 }
26935 else if (!saved_greater_than_is_operator_p)
26936 {
26937 /* If we're in a nested template argument list, the '>>' has
26938 to be a typo for '> >'. We emit the error message, but we
26939 continue parsing and we push a '>' as next token, so that
26940 the argument list will be parsed correctly. Note that the
26941 global source location is still on the token before the
26942 '>>', so we need to say explicitly where we want it. */
26943 cp_token *token = cp_lexer_peek_token (parser->lexer);
26944 gcc_rich_location richloc (token->location);
26945 richloc.add_fixit_replace ("> >");
26946 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
26947 "within a nested template argument list");
26948
26949 token->type = CPP_GREATER;
26950 }
26951 else
26952 {
26953 /* If this is not a nested template argument list, the '>>'
26954 is a typo for '>'. Emit an error message and continue.
26955 Same deal about the token location, but here we can get it
26956 right by consuming the '>>' before issuing the diagnostic. */
26957 cp_token *token = cp_lexer_consume_token (parser->lexer);
26958 error_at (token->location,
26959 "spurious %<>>%>, use %<>%> to terminate "
26960 "a template argument list");
26961 }
26962 }
26963 else
26964 cp_parser_skip_to_end_of_template_parameter_list (parser);
26965 /* The `>' token might be a greater-than operator again now. */
26966 parser->greater_than_is_operator_p
26967 = saved_greater_than_is_operator_p;
26968 /* Restore the SAVED_SCOPE. */
26969 parser->scope = saved_scope;
26970 parser->qualifying_scope = saved_qualifying_scope;
26971 parser->object_scope = saved_object_scope;
26972 cp_unevaluated_operand = saved_unevaluated_operand;
26973 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26974
26975 return arguments;
26976 }
26977
26978 /* MEMBER_FUNCTION is a member function, or a friend. If default
26979 arguments, or the body of the function have not yet been parsed,
26980 parse them now. */
26981
26982 static void
26983 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26984 {
26985 timevar_push (TV_PARSE_INMETH);
26986 /* If this member is a template, get the underlying
26987 FUNCTION_DECL. */
26988 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26989 member_function = DECL_TEMPLATE_RESULT (member_function);
26990
26991 /* There should not be any class definitions in progress at this
26992 point; the bodies of members are only parsed outside of all class
26993 definitions. */
26994 gcc_assert (parser->num_classes_being_defined == 0);
26995 /* While we're parsing the member functions we might encounter more
26996 classes. We want to handle them right away, but we don't want
26997 them getting mixed up with functions that are currently in the
26998 queue. */
26999 push_unparsed_function_queues (parser);
27000
27001 /* Make sure that any template parameters are in scope. */
27002 maybe_begin_member_template_processing (member_function);
27003
27004 /* If the body of the function has not yet been parsed, parse it
27005 now. */
27006 if (DECL_PENDING_INLINE_P (member_function))
27007 {
27008 tree function_scope;
27009 cp_token_cache *tokens;
27010
27011 /* The function is no longer pending; we are processing it. */
27012 tokens = DECL_PENDING_INLINE_INFO (member_function);
27013 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27014 DECL_PENDING_INLINE_P (member_function) = 0;
27015
27016 /* If this is a local class, enter the scope of the containing
27017 function. */
27018 function_scope = current_function_decl;
27019 if (function_scope)
27020 push_function_context ();
27021
27022 /* Push the body of the function onto the lexer stack. */
27023 cp_parser_push_lexer_for_tokens (parser, tokens);
27024
27025 /* Let the front end know that we going to be defining this
27026 function. */
27027 start_preparsed_function (member_function, NULL_TREE,
27028 SF_PRE_PARSED | SF_INCLASS_INLINE);
27029
27030 /* Don't do access checking if it is a templated function. */
27031 if (processing_template_decl)
27032 push_deferring_access_checks (dk_no_check);
27033
27034 /* #pragma omp declare reduction needs special parsing. */
27035 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27036 {
27037 parser->lexer->in_pragma = true;
27038 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27039 finish_function (/*inline*/2);
27040 cp_check_omp_declare_reduction (member_function);
27041 }
27042 else
27043 /* Now, parse the body of the function. */
27044 cp_parser_function_definition_after_declarator (parser,
27045 /*inline_p=*/true);
27046
27047 if (processing_template_decl)
27048 pop_deferring_access_checks ();
27049
27050 /* Leave the scope of the containing function. */
27051 if (function_scope)
27052 pop_function_context ();
27053 cp_parser_pop_lexer (parser);
27054 }
27055
27056 /* Remove any template parameters from the symbol table. */
27057 maybe_end_member_template_processing ();
27058
27059 /* Restore the queue. */
27060 pop_unparsed_function_queues (parser);
27061 timevar_pop (TV_PARSE_INMETH);
27062 }
27063
27064 /* If DECL contains any default args, remember it on the unparsed
27065 functions queue. */
27066
27067 static void
27068 cp_parser_save_default_args (cp_parser* parser, tree decl)
27069 {
27070 tree probe;
27071
27072 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27073 probe;
27074 probe = TREE_CHAIN (probe))
27075 if (TREE_PURPOSE (probe))
27076 {
27077 cp_default_arg_entry entry = {current_class_type, decl};
27078 vec_safe_push (unparsed_funs_with_default_args, entry);
27079 break;
27080 }
27081 }
27082
27083 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27084 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27085 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27086 from the parameter-type-list. */
27087
27088 static tree
27089 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27090 tree default_arg, tree parmtype)
27091 {
27092 cp_token_cache *tokens;
27093 tree parsed_arg;
27094 bool dummy;
27095
27096 if (default_arg == error_mark_node)
27097 return error_mark_node;
27098
27099 /* Push the saved tokens for the default argument onto the parser's
27100 lexer stack. */
27101 tokens = DEFARG_TOKENS (default_arg);
27102 cp_parser_push_lexer_for_tokens (parser, tokens);
27103
27104 start_lambda_scope (decl);
27105
27106 /* Parse the default argument. */
27107 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27108 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27109 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27110
27111 finish_lambda_scope ();
27112
27113 if (parsed_arg == error_mark_node)
27114 cp_parser_skip_to_end_of_statement (parser);
27115
27116 if (!processing_template_decl)
27117 {
27118 /* In a non-template class, check conversions now. In a template,
27119 we'll wait and instantiate these as needed. */
27120 if (TREE_CODE (decl) == PARM_DECL)
27121 parsed_arg = check_default_argument (parmtype, parsed_arg,
27122 tf_warning_or_error);
27123 else
27124 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27125 }
27126
27127 /* If the token stream has not been completely used up, then
27128 there was extra junk after the end of the default
27129 argument. */
27130 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27131 {
27132 if (TREE_CODE (decl) == PARM_DECL)
27133 cp_parser_error (parser, "expected %<,%>");
27134 else
27135 cp_parser_error (parser, "expected %<;%>");
27136 }
27137
27138 /* Revert to the main lexer. */
27139 cp_parser_pop_lexer (parser);
27140
27141 return parsed_arg;
27142 }
27143
27144 /* FIELD is a non-static data member with an initializer which we saved for
27145 later; parse it now. */
27146
27147 static void
27148 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27149 {
27150 tree def;
27151
27152 maybe_begin_member_template_processing (field);
27153
27154 push_unparsed_function_queues (parser);
27155 def = cp_parser_late_parse_one_default_arg (parser, field,
27156 DECL_INITIAL (field),
27157 NULL_TREE);
27158 pop_unparsed_function_queues (parser);
27159
27160 maybe_end_member_template_processing ();
27161
27162 DECL_INITIAL (field) = def;
27163 }
27164
27165 /* FN is a FUNCTION_DECL which may contains a parameter with an
27166 unparsed DEFAULT_ARG. Parse the default args now. This function
27167 assumes that the current scope is the scope in which the default
27168 argument should be processed. */
27169
27170 static void
27171 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27172 {
27173 bool saved_local_variables_forbidden_p;
27174 tree parm, parmdecl;
27175
27176 /* While we're parsing the default args, we might (due to the
27177 statement expression extension) encounter more classes. We want
27178 to handle them right away, but we don't want them getting mixed
27179 up with default args that are currently in the queue. */
27180 push_unparsed_function_queues (parser);
27181
27182 /* Local variable names (and the `this' keyword) may not appear
27183 in a default argument. */
27184 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27185 parser->local_variables_forbidden_p = true;
27186
27187 push_defarg_context (fn);
27188
27189 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27190 parmdecl = DECL_ARGUMENTS (fn);
27191 parm && parm != void_list_node;
27192 parm = TREE_CHAIN (parm),
27193 parmdecl = DECL_CHAIN (parmdecl))
27194 {
27195 tree default_arg = TREE_PURPOSE (parm);
27196 tree parsed_arg;
27197 vec<tree, va_gc> *insts;
27198 tree copy;
27199 unsigned ix;
27200
27201 if (!default_arg)
27202 continue;
27203
27204 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27205 /* This can happen for a friend declaration for a function
27206 already declared with default arguments. */
27207 continue;
27208
27209 parsed_arg
27210 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27211 default_arg,
27212 TREE_VALUE (parm));
27213 if (parsed_arg == error_mark_node)
27214 {
27215 continue;
27216 }
27217
27218 TREE_PURPOSE (parm) = parsed_arg;
27219
27220 /* Update any instantiations we've already created. */
27221 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27222 vec_safe_iterate (insts, ix, &copy); ix++)
27223 TREE_PURPOSE (copy) = parsed_arg;
27224 }
27225
27226 pop_defarg_context ();
27227
27228 /* Make sure no default arg is missing. */
27229 check_default_args (fn);
27230
27231 /* Restore the state of local_variables_forbidden_p. */
27232 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27233
27234 /* Restore the queue. */
27235 pop_unparsed_function_queues (parser);
27236 }
27237
27238 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27239
27240 sizeof ... ( identifier )
27241
27242 where the 'sizeof' token has already been consumed. */
27243
27244 static tree
27245 cp_parser_sizeof_pack (cp_parser *parser)
27246 {
27247 /* Consume the `...'. */
27248 cp_lexer_consume_token (parser->lexer);
27249 maybe_warn_variadic_templates ();
27250
27251 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27252 if (paren)
27253 cp_lexer_consume_token (parser->lexer);
27254 else
27255 permerror (cp_lexer_peek_token (parser->lexer)->location,
27256 "%<sizeof...%> argument must be surrounded by parentheses");
27257
27258 cp_token *token = cp_lexer_peek_token (parser->lexer);
27259 tree name = cp_parser_identifier (parser);
27260 if (name == error_mark_node)
27261 return error_mark_node;
27262 /* The name is not qualified. */
27263 parser->scope = NULL_TREE;
27264 parser->qualifying_scope = NULL_TREE;
27265 parser->object_scope = NULL_TREE;
27266 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27267 if (expr == error_mark_node)
27268 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27269 token->location);
27270 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27271 expr = TREE_TYPE (expr);
27272 else if (TREE_CODE (expr) == CONST_DECL)
27273 expr = DECL_INITIAL (expr);
27274 expr = make_pack_expansion (expr);
27275 PACK_EXPANSION_SIZEOF_P (expr) = true;
27276
27277 if (paren)
27278 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27279
27280 return expr;
27281 }
27282
27283 /* Parse the operand of `sizeof' (or a similar operator). Returns
27284 either a TYPE or an expression, depending on the form of the
27285 input. The KEYWORD indicates which kind of expression we have
27286 encountered. */
27287
27288 static tree
27289 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27290 {
27291 tree expr = NULL_TREE;
27292 const char *saved_message;
27293 char *tmp;
27294 bool saved_integral_constant_expression_p;
27295 bool saved_non_integral_constant_expression_p;
27296
27297 /* If it's a `...', then we are computing the length of a parameter
27298 pack. */
27299 if (keyword == RID_SIZEOF
27300 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27301 return cp_parser_sizeof_pack (parser);
27302
27303 /* Types cannot be defined in a `sizeof' expression. Save away the
27304 old message. */
27305 saved_message = parser->type_definition_forbidden_message;
27306 /* And create the new one. */
27307 tmp = concat ("types may not be defined in %<",
27308 IDENTIFIER_POINTER (ridpointers[keyword]),
27309 "%> expressions", NULL);
27310 parser->type_definition_forbidden_message = tmp;
27311
27312 /* The restrictions on constant-expressions do not apply inside
27313 sizeof expressions. */
27314 saved_integral_constant_expression_p
27315 = parser->integral_constant_expression_p;
27316 saved_non_integral_constant_expression_p
27317 = parser->non_integral_constant_expression_p;
27318 parser->integral_constant_expression_p = false;
27319
27320 /* Do not actually evaluate the expression. */
27321 ++cp_unevaluated_operand;
27322 ++c_inhibit_evaluation_warnings;
27323 /* If it's a `(', then we might be looking at the type-id
27324 construction. */
27325 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27326 {
27327 tree type = NULL_TREE;
27328
27329 /* We can't be sure yet whether we're looking at a type-id or an
27330 expression. */
27331 cp_parser_parse_tentatively (parser);
27332 /* Note: as a GNU Extension, compound literals are considered
27333 postfix-expressions as they are in C99, so they are valid
27334 arguments to sizeof. See comment in cp_parser_cast_expression
27335 for details. */
27336 if (cp_parser_compound_literal_p (parser))
27337 cp_parser_simulate_error (parser);
27338 else
27339 {
27340 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27341 parser->in_type_id_in_expr_p = true;
27342 /* Look for the type-id. */
27343 type = cp_parser_type_id (parser);
27344 /* Look for the closing `)'. */
27345 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27346 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27347 }
27348
27349 /* If all went well, then we're done. */
27350 if (cp_parser_parse_definitely (parser))
27351 {
27352 cp_decl_specifier_seq decl_specs;
27353
27354 /* Build a trivial decl-specifier-seq. */
27355 clear_decl_specs (&decl_specs);
27356 decl_specs.type = type;
27357
27358 /* Call grokdeclarator to figure out what type this is. */
27359 expr = grokdeclarator (NULL,
27360 &decl_specs,
27361 TYPENAME,
27362 /*initialized=*/0,
27363 /*attrlist=*/NULL);
27364 }
27365 }
27366
27367 /* If the type-id production did not work out, then we must be
27368 looking at the unary-expression production. */
27369 if (!expr)
27370 expr = cp_parser_unary_expression (parser);
27371
27372 /* Go back to evaluating expressions. */
27373 --cp_unevaluated_operand;
27374 --c_inhibit_evaluation_warnings;
27375
27376 /* Free the message we created. */
27377 free (tmp);
27378 /* And restore the old one. */
27379 parser->type_definition_forbidden_message = saved_message;
27380 parser->integral_constant_expression_p
27381 = saved_integral_constant_expression_p;
27382 parser->non_integral_constant_expression_p
27383 = saved_non_integral_constant_expression_p;
27384
27385 return expr;
27386 }
27387
27388 /* If the current declaration has no declarator, return true. */
27389
27390 static bool
27391 cp_parser_declares_only_class_p (cp_parser *parser)
27392 {
27393 /* If the next token is a `;' or a `,' then there is no
27394 declarator. */
27395 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27396 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27397 }
27398
27399 /* Update the DECL_SPECS to reflect the storage class indicated by
27400 KEYWORD. */
27401
27402 static void
27403 cp_parser_set_storage_class (cp_parser *parser,
27404 cp_decl_specifier_seq *decl_specs,
27405 enum rid keyword,
27406 cp_token *token)
27407 {
27408 cp_storage_class storage_class;
27409
27410 if (parser->in_unbraced_linkage_specification_p)
27411 {
27412 error_at (token->location, "invalid use of %qD in linkage specification",
27413 ridpointers[keyword]);
27414 return;
27415 }
27416 else if (decl_specs->storage_class != sc_none)
27417 {
27418 decl_specs->conflicting_specifiers_p = true;
27419 return;
27420 }
27421
27422 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27423 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27424 && decl_specs->gnu_thread_keyword_p)
27425 {
27426 pedwarn (decl_specs->locations[ds_thread], 0,
27427 "%<__thread%> before %qD", ridpointers[keyword]);
27428 }
27429
27430 switch (keyword)
27431 {
27432 case RID_AUTO:
27433 storage_class = sc_auto;
27434 break;
27435 case RID_REGISTER:
27436 storage_class = sc_register;
27437 break;
27438 case RID_STATIC:
27439 storage_class = sc_static;
27440 break;
27441 case RID_EXTERN:
27442 storage_class = sc_extern;
27443 break;
27444 case RID_MUTABLE:
27445 storage_class = sc_mutable;
27446 break;
27447 default:
27448 gcc_unreachable ();
27449 }
27450 decl_specs->storage_class = storage_class;
27451 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27452
27453 /* A storage class specifier cannot be applied alongside a typedef
27454 specifier. If there is a typedef specifier present then set
27455 conflicting_specifiers_p which will trigger an error later
27456 on in grokdeclarator. */
27457 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27458 decl_specs->conflicting_specifiers_p = true;
27459 }
27460
27461 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27462 is true, the type is a class or enum definition. */
27463
27464 static void
27465 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27466 tree type_spec,
27467 cp_token *token,
27468 bool type_definition_p)
27469 {
27470 decl_specs->any_specifiers_p = true;
27471
27472 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27473 (with, for example, in "typedef int wchar_t;") we remember that
27474 this is what happened. In system headers, we ignore these
27475 declarations so that G++ can work with system headers that are not
27476 C++-safe. */
27477 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27478 && !type_definition_p
27479 && (type_spec == boolean_type_node
27480 || type_spec == char16_type_node
27481 || type_spec == char32_type_node
27482 || type_spec == wchar_type_node)
27483 && (decl_specs->type
27484 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27485 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27486 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27487 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27488 {
27489 decl_specs->redefined_builtin_type = type_spec;
27490 set_and_check_decl_spec_loc (decl_specs,
27491 ds_redefined_builtin_type_spec,
27492 token);
27493 if (!decl_specs->type)
27494 {
27495 decl_specs->type = type_spec;
27496 decl_specs->type_definition_p = false;
27497 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27498 }
27499 }
27500 else if (decl_specs->type)
27501 decl_specs->multiple_types_p = true;
27502 else
27503 {
27504 decl_specs->type = type_spec;
27505 decl_specs->type_definition_p = type_definition_p;
27506 decl_specs->redefined_builtin_type = NULL_TREE;
27507 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27508 }
27509 }
27510
27511 /* True iff TOKEN is the GNU keyword __thread. */
27512
27513 static bool
27514 token_is__thread (cp_token *token)
27515 {
27516 gcc_assert (token->keyword == RID_THREAD);
27517 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27518 }
27519
27520 /* Set the location for a declarator specifier and check if it is
27521 duplicated.
27522
27523 DECL_SPECS is the sequence of declarator specifiers onto which to
27524 set the location.
27525
27526 DS is the single declarator specifier to set which location is to
27527 be set onto the existing sequence of declarators.
27528
27529 LOCATION is the location for the declarator specifier to
27530 consider. */
27531
27532 static void
27533 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27534 cp_decl_spec ds, cp_token *token)
27535 {
27536 gcc_assert (ds < ds_last);
27537
27538 if (decl_specs == NULL)
27539 return;
27540
27541 source_location location = token->location;
27542
27543 if (decl_specs->locations[ds] == 0)
27544 {
27545 decl_specs->locations[ds] = location;
27546 if (ds == ds_thread)
27547 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27548 }
27549 else
27550 {
27551 if (ds == ds_long)
27552 {
27553 if (decl_specs->locations[ds_long_long] != 0)
27554 error_at (location,
27555 "%<long long long%> is too long for GCC");
27556 else
27557 {
27558 decl_specs->locations[ds_long_long] = location;
27559 pedwarn_cxx98 (location,
27560 OPT_Wlong_long,
27561 "ISO C++ 1998 does not support %<long long%>");
27562 }
27563 }
27564 else if (ds == ds_thread)
27565 {
27566 bool gnu = token_is__thread (token);
27567 if (gnu != decl_specs->gnu_thread_keyword_p)
27568 error_at (location,
27569 "both %<__thread%> and %<thread_local%> specified");
27570 else
27571 error_at (location, "duplicate %qD", token->u.value);
27572 }
27573 else
27574 {
27575 static const char *const decl_spec_names[] = {
27576 "signed",
27577 "unsigned",
27578 "short",
27579 "long",
27580 "const",
27581 "volatile",
27582 "restrict",
27583 "inline",
27584 "virtual",
27585 "explicit",
27586 "friend",
27587 "typedef",
27588 "using",
27589 "constexpr",
27590 "__complex"
27591 };
27592 error_at (location,
27593 "duplicate %qs", decl_spec_names[ds]);
27594 }
27595 }
27596 }
27597
27598 /* Return true iff the declarator specifier DS is present in the
27599 sequence of declarator specifiers DECL_SPECS. */
27600
27601 bool
27602 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27603 cp_decl_spec ds)
27604 {
27605 gcc_assert (ds < ds_last);
27606
27607 if (decl_specs == NULL)
27608 return false;
27609
27610 return decl_specs->locations[ds] != 0;
27611 }
27612
27613 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27614 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27615
27616 static bool
27617 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27618 {
27619 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27620 }
27621
27622 /* Issue an error message indicating that TOKEN_DESC was expected.
27623 If KEYWORD is true, it indicated this function is called by
27624 cp_parser_require_keword and the required token can only be
27625 a indicated keyword. */
27626
27627 static void
27628 cp_parser_required_error (cp_parser *parser,
27629 required_token token_desc,
27630 bool keyword)
27631 {
27632 switch (token_desc)
27633 {
27634 case RT_NEW:
27635 cp_parser_error (parser, "expected %<new%>");
27636 return;
27637 case RT_DELETE:
27638 cp_parser_error (parser, "expected %<delete%>");
27639 return;
27640 case RT_RETURN:
27641 cp_parser_error (parser, "expected %<return%>");
27642 return;
27643 case RT_WHILE:
27644 cp_parser_error (parser, "expected %<while%>");
27645 return;
27646 case RT_EXTERN:
27647 cp_parser_error (parser, "expected %<extern%>");
27648 return;
27649 case RT_STATIC_ASSERT:
27650 cp_parser_error (parser, "expected %<static_assert%>");
27651 return;
27652 case RT_DECLTYPE:
27653 cp_parser_error (parser, "expected %<decltype%>");
27654 return;
27655 case RT_OPERATOR:
27656 cp_parser_error (parser, "expected %<operator%>");
27657 return;
27658 case RT_CLASS:
27659 cp_parser_error (parser, "expected %<class%>");
27660 return;
27661 case RT_TEMPLATE:
27662 cp_parser_error (parser, "expected %<template%>");
27663 return;
27664 case RT_NAMESPACE:
27665 cp_parser_error (parser, "expected %<namespace%>");
27666 return;
27667 case RT_USING:
27668 cp_parser_error (parser, "expected %<using%>");
27669 return;
27670 case RT_ASM:
27671 cp_parser_error (parser, "expected %<asm%>");
27672 return;
27673 case RT_TRY:
27674 cp_parser_error (parser, "expected %<try%>");
27675 return;
27676 case RT_CATCH:
27677 cp_parser_error (parser, "expected %<catch%>");
27678 return;
27679 case RT_THROW:
27680 cp_parser_error (parser, "expected %<throw%>");
27681 return;
27682 case RT_LABEL:
27683 cp_parser_error (parser, "expected %<__label__%>");
27684 return;
27685 case RT_AT_TRY:
27686 cp_parser_error (parser, "expected %<@try%>");
27687 return;
27688 case RT_AT_SYNCHRONIZED:
27689 cp_parser_error (parser, "expected %<@synchronized%>");
27690 return;
27691 case RT_AT_THROW:
27692 cp_parser_error (parser, "expected %<@throw%>");
27693 return;
27694 case RT_TRANSACTION_ATOMIC:
27695 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27696 return;
27697 case RT_TRANSACTION_RELAXED:
27698 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27699 return;
27700 default:
27701 break;
27702 }
27703 if (!keyword)
27704 {
27705 switch (token_desc)
27706 {
27707 case RT_SEMICOLON:
27708 cp_parser_error (parser, "expected %<;%>");
27709 return;
27710 case RT_OPEN_PAREN:
27711 cp_parser_error (parser, "expected %<(%>");
27712 return;
27713 case RT_CLOSE_BRACE:
27714 cp_parser_error (parser, "expected %<}%>");
27715 return;
27716 case RT_OPEN_BRACE:
27717 cp_parser_error (parser, "expected %<{%>");
27718 return;
27719 case RT_CLOSE_SQUARE:
27720 cp_parser_error (parser, "expected %<]%>");
27721 return;
27722 case RT_OPEN_SQUARE:
27723 cp_parser_error (parser, "expected %<[%>");
27724 return;
27725 case RT_COMMA:
27726 cp_parser_error (parser, "expected %<,%>");
27727 return;
27728 case RT_SCOPE:
27729 cp_parser_error (parser, "expected %<::%>");
27730 return;
27731 case RT_LESS:
27732 cp_parser_error (parser, "expected %<<%>");
27733 return;
27734 case RT_GREATER:
27735 cp_parser_error (parser, "expected %<>%>");
27736 return;
27737 case RT_EQ:
27738 cp_parser_error (parser, "expected %<=%>");
27739 return;
27740 case RT_ELLIPSIS:
27741 cp_parser_error (parser, "expected %<...%>");
27742 return;
27743 case RT_MULT:
27744 cp_parser_error (parser, "expected %<*%>");
27745 return;
27746 case RT_COMPL:
27747 cp_parser_error (parser, "expected %<~%>");
27748 return;
27749 case RT_COLON:
27750 cp_parser_error (parser, "expected %<:%>");
27751 return;
27752 case RT_COLON_SCOPE:
27753 cp_parser_error (parser, "expected %<:%> or %<::%>");
27754 return;
27755 case RT_CLOSE_PAREN:
27756 cp_parser_error (parser, "expected %<)%>");
27757 return;
27758 case RT_COMMA_CLOSE_PAREN:
27759 cp_parser_error (parser, "expected %<,%> or %<)%>");
27760 return;
27761 case RT_PRAGMA_EOL:
27762 cp_parser_error (parser, "expected end of line");
27763 return;
27764 case RT_NAME:
27765 cp_parser_error (parser, "expected identifier");
27766 return;
27767 case RT_SELECT:
27768 cp_parser_error (parser, "expected selection-statement");
27769 return;
27770 case RT_INTERATION:
27771 cp_parser_error (parser, "expected iteration-statement");
27772 return;
27773 case RT_JUMP:
27774 cp_parser_error (parser, "expected jump-statement");
27775 return;
27776 case RT_CLASS_KEY:
27777 cp_parser_error (parser, "expected class-key");
27778 return;
27779 case RT_CLASS_TYPENAME_TEMPLATE:
27780 cp_parser_error (parser,
27781 "expected %<class%>, %<typename%>, or %<template%>");
27782 return;
27783 default:
27784 gcc_unreachable ();
27785 }
27786 }
27787 else
27788 gcc_unreachable ();
27789 }
27790
27791
27792
27793 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27794 issue an error message indicating that TOKEN_DESC was expected.
27795
27796 Returns the token consumed, if the token had the appropriate type.
27797 Otherwise, returns NULL. */
27798
27799 static cp_token *
27800 cp_parser_require (cp_parser* parser,
27801 enum cpp_ttype type,
27802 required_token token_desc)
27803 {
27804 if (cp_lexer_next_token_is (parser->lexer, type))
27805 return cp_lexer_consume_token (parser->lexer);
27806 else
27807 {
27808 /* Output the MESSAGE -- unless we're parsing tentatively. */
27809 if (!cp_parser_simulate_error (parser))
27810 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27811 return NULL;
27812 }
27813 }
27814
27815 /* An error message is produced if the next token is not '>'.
27816 All further tokens are skipped until the desired token is
27817 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27818
27819 static void
27820 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27821 {
27822 /* Current level of '< ... >'. */
27823 unsigned level = 0;
27824 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27825 unsigned nesting_depth = 0;
27826
27827 /* Are we ready, yet? If not, issue error message. */
27828 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27829 return;
27830
27831 /* Skip tokens until the desired token is found. */
27832 while (true)
27833 {
27834 /* Peek at the next token. */
27835 switch (cp_lexer_peek_token (parser->lexer)->type)
27836 {
27837 case CPP_LESS:
27838 if (!nesting_depth)
27839 ++level;
27840 break;
27841
27842 case CPP_RSHIFT:
27843 if (cxx_dialect == cxx98)
27844 /* C++0x views the `>>' operator as two `>' tokens, but
27845 C++98 does not. */
27846 break;
27847 else if (!nesting_depth && level-- == 0)
27848 {
27849 /* We've hit a `>>' where the first `>' closes the
27850 template argument list, and the second `>' is
27851 spurious. Just consume the `>>' and stop; we've
27852 already produced at least one error. */
27853 cp_lexer_consume_token (parser->lexer);
27854 return;
27855 }
27856 /* Fall through for C++0x, so we handle the second `>' in
27857 the `>>'. */
27858 gcc_fallthrough ();
27859
27860 case CPP_GREATER:
27861 if (!nesting_depth && level-- == 0)
27862 {
27863 /* We've reached the token we want, consume it and stop. */
27864 cp_lexer_consume_token (parser->lexer);
27865 return;
27866 }
27867 break;
27868
27869 case CPP_OPEN_PAREN:
27870 case CPP_OPEN_SQUARE:
27871 ++nesting_depth;
27872 break;
27873
27874 case CPP_CLOSE_PAREN:
27875 case CPP_CLOSE_SQUARE:
27876 if (nesting_depth-- == 0)
27877 return;
27878 break;
27879
27880 case CPP_EOF:
27881 case CPP_PRAGMA_EOL:
27882 case CPP_SEMICOLON:
27883 case CPP_OPEN_BRACE:
27884 case CPP_CLOSE_BRACE:
27885 /* The '>' was probably forgotten, don't look further. */
27886 return;
27887
27888 default:
27889 break;
27890 }
27891
27892 /* Consume this token. */
27893 cp_lexer_consume_token (parser->lexer);
27894 }
27895 }
27896
27897 /* If the next token is the indicated keyword, consume it. Otherwise,
27898 issue an error message indicating that TOKEN_DESC was expected.
27899
27900 Returns the token consumed, if the token had the appropriate type.
27901 Otherwise, returns NULL. */
27902
27903 static cp_token *
27904 cp_parser_require_keyword (cp_parser* parser,
27905 enum rid keyword,
27906 required_token token_desc)
27907 {
27908 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27909
27910 if (token && token->keyword != keyword)
27911 {
27912 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27913 return NULL;
27914 }
27915
27916 return token;
27917 }
27918
27919 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27920 function-definition. */
27921
27922 static bool
27923 cp_parser_token_starts_function_definition_p (cp_token* token)
27924 {
27925 return (/* An ordinary function-body begins with an `{'. */
27926 token->type == CPP_OPEN_BRACE
27927 /* A ctor-initializer begins with a `:'. */
27928 || token->type == CPP_COLON
27929 /* A function-try-block begins with `try'. */
27930 || token->keyword == RID_TRY
27931 /* A function-transaction-block begins with `__transaction_atomic'
27932 or `__transaction_relaxed'. */
27933 || token->keyword == RID_TRANSACTION_ATOMIC
27934 || token->keyword == RID_TRANSACTION_RELAXED
27935 /* The named return value extension begins with `return'. */
27936 || token->keyword == RID_RETURN);
27937 }
27938
27939 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27940 definition. */
27941
27942 static bool
27943 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27944 {
27945 cp_token *token;
27946
27947 token = cp_lexer_peek_token (parser->lexer);
27948 return (token->type == CPP_OPEN_BRACE
27949 || (token->type == CPP_COLON
27950 && !parser->colon_doesnt_start_class_def_p));
27951 }
27952
27953 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27954 C++0x) ending a template-argument. */
27955
27956 static bool
27957 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27958 {
27959 cp_token *token;
27960
27961 token = cp_lexer_peek_token (parser->lexer);
27962 return (token->type == CPP_COMMA
27963 || token->type == CPP_GREATER
27964 || token->type == CPP_ELLIPSIS
27965 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27966 }
27967
27968 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27969 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27970
27971 static bool
27972 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27973 size_t n)
27974 {
27975 cp_token *token;
27976
27977 token = cp_lexer_peek_nth_token (parser->lexer, n);
27978 if (token->type == CPP_LESS)
27979 return true;
27980 /* Check for the sequence `<::' in the original code. It would be lexed as
27981 `[:', where `[' is a digraph, and there is no whitespace before
27982 `:'. */
27983 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27984 {
27985 cp_token *token2;
27986 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27987 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27988 return true;
27989 }
27990 return false;
27991 }
27992
27993 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27994 or none_type otherwise. */
27995
27996 static enum tag_types
27997 cp_parser_token_is_class_key (cp_token* token)
27998 {
27999 switch (token->keyword)
28000 {
28001 case RID_CLASS:
28002 return class_type;
28003 case RID_STRUCT:
28004 return record_type;
28005 case RID_UNION:
28006 return union_type;
28007
28008 default:
28009 return none_type;
28010 }
28011 }
28012
28013 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28014 or none_type otherwise or if the token is null. */
28015
28016 static enum tag_types
28017 cp_parser_token_is_type_parameter_key (cp_token* token)
28018 {
28019 if (!token)
28020 return none_type;
28021
28022 switch (token->keyword)
28023 {
28024 case RID_CLASS:
28025 return class_type;
28026 case RID_TYPENAME:
28027 return typename_type;
28028
28029 default:
28030 return none_type;
28031 }
28032 }
28033
28034 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28035
28036 static void
28037 cp_parser_check_class_key (enum tag_types class_key, tree type)
28038 {
28039 if (type == error_mark_node)
28040 return;
28041 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28042 {
28043 if (permerror (input_location, "%qs tag used in naming %q#T",
28044 class_key == union_type ? "union"
28045 : class_key == record_type ? "struct" : "class",
28046 type))
28047 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28048 "%q#T was previously declared here", type);
28049 }
28050 }
28051
28052 /* Issue an error message if DECL is redeclared with different
28053 access than its original declaration [class.access.spec/3].
28054 This applies to nested classes, nested class templates and
28055 enumerations [class.mem/1]. */
28056
28057 static void
28058 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28059 {
28060 if (!decl
28061 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28062 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28063 return;
28064
28065 if ((TREE_PRIVATE (decl)
28066 != (current_access_specifier == access_private_node))
28067 || (TREE_PROTECTED (decl)
28068 != (current_access_specifier == access_protected_node)))
28069 error_at (location, "%qD redeclared with different access", decl);
28070 }
28071
28072 /* Look for the `template' keyword, as a syntactic disambiguator.
28073 Return TRUE iff it is present, in which case it will be
28074 consumed. */
28075
28076 static bool
28077 cp_parser_optional_template_keyword (cp_parser *parser)
28078 {
28079 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28080 {
28081 /* In C++98 the `template' keyword can only be used within templates;
28082 outside templates the parser can always figure out what is a
28083 template and what is not. In C++11, per the resolution of DR 468,
28084 `template' is allowed in cases where it is not strictly necessary. */
28085 if (!processing_template_decl
28086 && pedantic && cxx_dialect == cxx98)
28087 {
28088 cp_token *token = cp_lexer_peek_token (parser->lexer);
28089 pedwarn (token->location, OPT_Wpedantic,
28090 "in C++98 %<template%> (as a disambiguator) is only "
28091 "allowed within templates");
28092 /* If this part of the token stream is rescanned, the same
28093 error message would be generated. So, we purge the token
28094 from the stream. */
28095 cp_lexer_purge_token (parser->lexer);
28096 return false;
28097 }
28098 else
28099 {
28100 /* Consume the `template' keyword. */
28101 cp_lexer_consume_token (parser->lexer);
28102 return true;
28103 }
28104 }
28105 return false;
28106 }
28107
28108 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28109 set PARSER->SCOPE, and perform other related actions. */
28110
28111 static void
28112 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28113 {
28114 struct tree_check *check_value;
28115
28116 /* Get the stored value. */
28117 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28118 /* Set the scope from the stored value. */
28119 parser->scope = saved_checks_value (check_value);
28120 parser->qualifying_scope = check_value->qualifying_scope;
28121 parser->object_scope = NULL_TREE;
28122 }
28123
28124 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28125 encounter the end of a block before what we were looking for. */
28126
28127 static bool
28128 cp_parser_cache_group (cp_parser *parser,
28129 enum cpp_ttype end,
28130 unsigned depth)
28131 {
28132 while (true)
28133 {
28134 cp_token *token = cp_lexer_peek_token (parser->lexer);
28135
28136 /* Abort a parenthesized expression if we encounter a semicolon. */
28137 if ((end == CPP_CLOSE_PAREN || depth == 0)
28138 && token->type == CPP_SEMICOLON)
28139 return true;
28140 /* If we've reached the end of the file, stop. */
28141 if (token->type == CPP_EOF
28142 || (end != CPP_PRAGMA_EOL
28143 && token->type == CPP_PRAGMA_EOL))
28144 return true;
28145 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28146 /* We've hit the end of an enclosing block, so there's been some
28147 kind of syntax error. */
28148 return true;
28149
28150 /* Consume the token. */
28151 cp_lexer_consume_token (parser->lexer);
28152 /* See if it starts a new group. */
28153 if (token->type == CPP_OPEN_BRACE)
28154 {
28155 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28156 /* In theory this should probably check end == '}', but
28157 cp_parser_save_member_function_body needs it to exit
28158 after either '}' or ')' when called with ')'. */
28159 if (depth == 0)
28160 return false;
28161 }
28162 else if (token->type == CPP_OPEN_PAREN)
28163 {
28164 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28165 if (depth == 0 && end == CPP_CLOSE_PAREN)
28166 return false;
28167 }
28168 else if (token->type == CPP_PRAGMA)
28169 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28170 else if (token->type == end)
28171 return false;
28172 }
28173 }
28174
28175 /* Like above, for caching a default argument or NSDMI. Both of these are
28176 terminated by a non-nested comma, but it can be unclear whether or not a
28177 comma is nested in a template argument list unless we do more parsing.
28178 In order to handle this ambiguity, when we encounter a ',' after a '<'
28179 we try to parse what follows as a parameter-declaration-list (in the
28180 case of a default argument) or a member-declarator (in the case of an
28181 NSDMI). If that succeeds, then we stop caching. */
28182
28183 static tree
28184 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28185 {
28186 unsigned depth = 0;
28187 int maybe_template_id = 0;
28188 cp_token *first_token;
28189 cp_token *token;
28190 tree default_argument;
28191
28192 /* Add tokens until we have processed the entire default
28193 argument. We add the range [first_token, token). */
28194 first_token = cp_lexer_peek_token (parser->lexer);
28195 if (first_token->type == CPP_OPEN_BRACE)
28196 {
28197 /* For list-initialization, this is straightforward. */
28198 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28199 token = cp_lexer_peek_token (parser->lexer);
28200 }
28201 else while (true)
28202 {
28203 bool done = false;
28204
28205 /* Peek at the next token. */
28206 token = cp_lexer_peek_token (parser->lexer);
28207 /* What we do depends on what token we have. */
28208 switch (token->type)
28209 {
28210 /* In valid code, a default argument must be
28211 immediately followed by a `,' `)', or `...'. */
28212 case CPP_COMMA:
28213 if (depth == 0 && maybe_template_id)
28214 {
28215 /* If we've seen a '<', we might be in a
28216 template-argument-list. Until Core issue 325 is
28217 resolved, we don't know how this situation ought
28218 to be handled, so try to DTRT. We check whether
28219 what comes after the comma is a valid parameter
28220 declaration list. If it is, then the comma ends
28221 the default argument; otherwise the default
28222 argument continues. */
28223 bool error = false;
28224 cp_token *peek;
28225
28226 /* Set ITALP so cp_parser_parameter_declaration_list
28227 doesn't decide to commit to this parse. */
28228 bool saved_italp = parser->in_template_argument_list_p;
28229 parser->in_template_argument_list_p = true;
28230
28231 cp_parser_parse_tentatively (parser);
28232
28233 if (nsdmi)
28234 {
28235 /* Parse declarators until we reach a non-comma or
28236 somthing that cannot be an initializer.
28237 Just checking whether we're looking at a single
28238 declarator is insufficient. Consider:
28239 int var = tuple<T,U>::x;
28240 The template parameter 'U' looks exactly like a
28241 declarator. */
28242 do
28243 {
28244 int ctor_dtor_or_conv_p;
28245 cp_lexer_consume_token (parser->lexer);
28246 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28247 &ctor_dtor_or_conv_p,
28248 /*parenthesized_p=*/NULL,
28249 /*member_p=*/true,
28250 /*friend_p=*/false);
28251 peek = cp_lexer_peek_token (parser->lexer);
28252 if (cp_parser_error_occurred (parser))
28253 break;
28254 }
28255 while (peek->type == CPP_COMMA);
28256 /* If we met an '=' or ';' then the original comma
28257 was the end of the NSDMI. Otherwise assume
28258 we're still in the NSDMI. */
28259 error = (peek->type != CPP_EQ
28260 && peek->type != CPP_SEMICOLON);
28261 }
28262 else
28263 {
28264 cp_lexer_consume_token (parser->lexer);
28265 begin_scope (sk_function_parms, NULL_TREE);
28266 cp_parser_parameter_declaration_list (parser, &error);
28267 pop_bindings_and_leave_scope ();
28268 }
28269 if (!cp_parser_error_occurred (parser) && !error)
28270 done = true;
28271 cp_parser_abort_tentative_parse (parser);
28272
28273 parser->in_template_argument_list_p = saved_italp;
28274 break;
28275 }
28276 /* FALLTHRU */
28277 case CPP_CLOSE_PAREN:
28278 case CPP_ELLIPSIS:
28279 /* If we run into a non-nested `;', `}', or `]',
28280 then the code is invalid -- but the default
28281 argument is certainly over. */
28282 case CPP_SEMICOLON:
28283 case CPP_CLOSE_BRACE:
28284 case CPP_CLOSE_SQUARE:
28285 if (depth == 0
28286 /* Handle correctly int n = sizeof ... ( p ); */
28287 && token->type != CPP_ELLIPSIS)
28288 done = true;
28289 /* Update DEPTH, if necessary. */
28290 else if (token->type == CPP_CLOSE_PAREN
28291 || token->type == CPP_CLOSE_BRACE
28292 || token->type == CPP_CLOSE_SQUARE)
28293 --depth;
28294 break;
28295
28296 case CPP_OPEN_PAREN:
28297 case CPP_OPEN_SQUARE:
28298 case CPP_OPEN_BRACE:
28299 ++depth;
28300 break;
28301
28302 case CPP_LESS:
28303 if (depth == 0)
28304 /* This might be the comparison operator, or it might
28305 start a template argument list. */
28306 ++maybe_template_id;
28307 break;
28308
28309 case CPP_RSHIFT:
28310 if (cxx_dialect == cxx98)
28311 break;
28312 /* Fall through for C++0x, which treats the `>>'
28313 operator like two `>' tokens in certain
28314 cases. */
28315 gcc_fallthrough ();
28316
28317 case CPP_GREATER:
28318 if (depth == 0)
28319 {
28320 /* This might be an operator, or it might close a
28321 template argument list. But if a previous '<'
28322 started a template argument list, this will have
28323 closed it, so we can't be in one anymore. */
28324 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28325 if (maybe_template_id < 0)
28326 maybe_template_id = 0;
28327 }
28328 break;
28329
28330 /* If we run out of tokens, issue an error message. */
28331 case CPP_EOF:
28332 case CPP_PRAGMA_EOL:
28333 error_at (token->location, "file ends in default argument");
28334 return error_mark_node;
28335
28336 case CPP_NAME:
28337 case CPP_SCOPE:
28338 /* In these cases, we should look for template-ids.
28339 For example, if the default argument is
28340 `X<int, double>()', we need to do name lookup to
28341 figure out whether or not `X' is a template; if
28342 so, the `,' does not end the default argument.
28343
28344 That is not yet done. */
28345 break;
28346
28347 default:
28348 break;
28349 }
28350
28351 /* If we've reached the end, stop. */
28352 if (done)
28353 break;
28354
28355 /* Add the token to the token block. */
28356 token = cp_lexer_consume_token (parser->lexer);
28357 }
28358
28359 /* Create a DEFAULT_ARG to represent the unparsed default
28360 argument. */
28361 default_argument = make_node (DEFAULT_ARG);
28362 DEFARG_TOKENS (default_argument)
28363 = cp_token_cache_new (first_token, token);
28364 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28365
28366 return default_argument;
28367 }
28368
28369 /* Begin parsing tentatively. We always save tokens while parsing
28370 tentatively so that if the tentative parsing fails we can restore the
28371 tokens. */
28372
28373 static void
28374 cp_parser_parse_tentatively (cp_parser* parser)
28375 {
28376 /* Enter a new parsing context. */
28377 parser->context = cp_parser_context_new (parser->context);
28378 /* Begin saving tokens. */
28379 cp_lexer_save_tokens (parser->lexer);
28380 /* In order to avoid repetitive access control error messages,
28381 access checks are queued up until we are no longer parsing
28382 tentatively. */
28383 push_deferring_access_checks (dk_deferred);
28384 }
28385
28386 /* Commit to the currently active tentative parse. */
28387
28388 static void
28389 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28390 {
28391 cp_parser_context *context;
28392 cp_lexer *lexer;
28393
28394 /* Mark all of the levels as committed. */
28395 lexer = parser->lexer;
28396 for (context = parser->context; context->next; context = context->next)
28397 {
28398 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28399 break;
28400 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28401 while (!cp_lexer_saving_tokens (lexer))
28402 lexer = lexer->next;
28403 cp_lexer_commit_tokens (lexer);
28404 }
28405 }
28406
28407 /* Commit to the topmost currently active tentative parse.
28408
28409 Note that this function shouldn't be called when there are
28410 irreversible side-effects while in a tentative state. For
28411 example, we shouldn't create a permanent entry in the symbol
28412 table, or issue an error message that might not apply if the
28413 tentative parse is aborted. */
28414
28415 static void
28416 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28417 {
28418 cp_parser_context *context = parser->context;
28419 cp_lexer *lexer = parser->lexer;
28420
28421 if (context)
28422 {
28423 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28424 return;
28425 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28426
28427 while (!cp_lexer_saving_tokens (lexer))
28428 lexer = lexer->next;
28429 cp_lexer_commit_tokens (lexer);
28430 }
28431 }
28432
28433 /* Abort the currently active tentative parse. All consumed tokens
28434 will be rolled back, and no diagnostics will be issued. */
28435
28436 static void
28437 cp_parser_abort_tentative_parse (cp_parser* parser)
28438 {
28439 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28440 || errorcount > 0);
28441 cp_parser_simulate_error (parser);
28442 /* Now, pretend that we want to see if the construct was
28443 successfully parsed. */
28444 cp_parser_parse_definitely (parser);
28445 }
28446
28447 /* Stop parsing tentatively. If a parse error has occurred, restore the
28448 token stream. Otherwise, commit to the tokens we have consumed.
28449 Returns true if no error occurred; false otherwise. */
28450
28451 static bool
28452 cp_parser_parse_definitely (cp_parser* parser)
28453 {
28454 bool error_occurred;
28455 cp_parser_context *context;
28456
28457 /* Remember whether or not an error occurred, since we are about to
28458 destroy that information. */
28459 error_occurred = cp_parser_error_occurred (parser);
28460 /* Remove the topmost context from the stack. */
28461 context = parser->context;
28462 parser->context = context->next;
28463 /* If no parse errors occurred, commit to the tentative parse. */
28464 if (!error_occurred)
28465 {
28466 /* Commit to the tokens read tentatively, unless that was
28467 already done. */
28468 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28469 cp_lexer_commit_tokens (parser->lexer);
28470
28471 pop_to_parent_deferring_access_checks ();
28472 }
28473 /* Otherwise, if errors occurred, roll back our state so that things
28474 are just as they were before we began the tentative parse. */
28475 else
28476 {
28477 cp_lexer_rollback_tokens (parser->lexer);
28478 pop_deferring_access_checks ();
28479 }
28480 /* Add the context to the front of the free list. */
28481 context->next = cp_parser_context_free_list;
28482 cp_parser_context_free_list = context;
28483
28484 return !error_occurred;
28485 }
28486
28487 /* Returns true if we are parsing tentatively and are not committed to
28488 this tentative parse. */
28489
28490 static bool
28491 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28492 {
28493 return (cp_parser_parsing_tentatively (parser)
28494 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28495 }
28496
28497 /* Returns nonzero iff an error has occurred during the most recent
28498 tentative parse. */
28499
28500 static bool
28501 cp_parser_error_occurred (cp_parser* parser)
28502 {
28503 return (cp_parser_parsing_tentatively (parser)
28504 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28505 }
28506
28507 /* Returns nonzero if GNU extensions are allowed. */
28508
28509 static bool
28510 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28511 {
28512 return parser->allow_gnu_extensions_p;
28513 }
28514 \f
28515 /* Objective-C++ Productions */
28516
28517
28518 /* Parse an Objective-C expression, which feeds into a primary-expression
28519 above.
28520
28521 objc-expression:
28522 objc-message-expression
28523 objc-string-literal
28524 objc-encode-expression
28525 objc-protocol-expression
28526 objc-selector-expression
28527
28528 Returns a tree representation of the expression. */
28529
28530 static cp_expr
28531 cp_parser_objc_expression (cp_parser* parser)
28532 {
28533 /* Try to figure out what kind of declaration is present. */
28534 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28535
28536 switch (kwd->type)
28537 {
28538 case CPP_OPEN_SQUARE:
28539 return cp_parser_objc_message_expression (parser);
28540
28541 case CPP_OBJC_STRING:
28542 kwd = cp_lexer_consume_token (parser->lexer);
28543 return objc_build_string_object (kwd->u.value);
28544
28545 case CPP_KEYWORD:
28546 switch (kwd->keyword)
28547 {
28548 case RID_AT_ENCODE:
28549 return cp_parser_objc_encode_expression (parser);
28550
28551 case RID_AT_PROTOCOL:
28552 return cp_parser_objc_protocol_expression (parser);
28553
28554 case RID_AT_SELECTOR:
28555 return cp_parser_objc_selector_expression (parser);
28556
28557 default:
28558 break;
28559 }
28560 default:
28561 error_at (kwd->location,
28562 "misplaced %<@%D%> Objective-C++ construct",
28563 kwd->u.value);
28564 cp_parser_skip_to_end_of_block_or_statement (parser);
28565 }
28566
28567 return error_mark_node;
28568 }
28569
28570 /* Parse an Objective-C message expression.
28571
28572 objc-message-expression:
28573 [ objc-message-receiver objc-message-args ]
28574
28575 Returns a representation of an Objective-C message. */
28576
28577 static tree
28578 cp_parser_objc_message_expression (cp_parser* parser)
28579 {
28580 tree receiver, messageargs;
28581
28582 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28583 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28584 receiver = cp_parser_objc_message_receiver (parser);
28585 messageargs = cp_parser_objc_message_args (parser);
28586 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28587 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28588
28589 tree result = objc_build_message_expr (receiver, messageargs);
28590
28591 /* Construct a location e.g.
28592 [self func1:5]
28593 ^~~~~~~~~~~~~~
28594 ranging from the '[' to the ']', with the caret at the start. */
28595 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28596 protected_set_expr_location (result, combined_loc);
28597
28598 return result;
28599 }
28600
28601 /* Parse an objc-message-receiver.
28602
28603 objc-message-receiver:
28604 expression
28605 simple-type-specifier
28606
28607 Returns a representation of the type or expression. */
28608
28609 static tree
28610 cp_parser_objc_message_receiver (cp_parser* parser)
28611 {
28612 tree rcv;
28613
28614 /* An Objective-C message receiver may be either (1) a type
28615 or (2) an expression. */
28616 cp_parser_parse_tentatively (parser);
28617 rcv = cp_parser_expression (parser);
28618
28619 /* If that worked out, fine. */
28620 if (cp_parser_parse_definitely (parser))
28621 return rcv;
28622
28623 cp_parser_parse_tentatively (parser);
28624 rcv = cp_parser_simple_type_specifier (parser,
28625 /*decl_specs=*/NULL,
28626 CP_PARSER_FLAGS_NONE);
28627
28628 if (cp_parser_parse_definitely (parser))
28629 return objc_get_class_reference (rcv);
28630
28631 cp_parser_error (parser, "objective-c++ message receiver expected");
28632 return error_mark_node;
28633 }
28634
28635 /* Parse the arguments and selectors comprising an Objective-C message.
28636
28637 objc-message-args:
28638 objc-selector
28639 objc-selector-args
28640 objc-selector-args , objc-comma-args
28641
28642 objc-selector-args:
28643 objc-selector [opt] : assignment-expression
28644 objc-selector-args objc-selector [opt] : assignment-expression
28645
28646 objc-comma-args:
28647 assignment-expression
28648 objc-comma-args , assignment-expression
28649
28650 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28651 selector arguments and TREE_VALUE containing a list of comma
28652 arguments. */
28653
28654 static tree
28655 cp_parser_objc_message_args (cp_parser* parser)
28656 {
28657 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28658 bool maybe_unary_selector_p = true;
28659 cp_token *token = cp_lexer_peek_token (parser->lexer);
28660
28661 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28662 {
28663 tree selector = NULL_TREE, arg;
28664
28665 if (token->type != CPP_COLON)
28666 selector = cp_parser_objc_selector (parser);
28667
28668 /* Detect if we have a unary selector. */
28669 if (maybe_unary_selector_p
28670 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28671 return build_tree_list (selector, NULL_TREE);
28672
28673 maybe_unary_selector_p = false;
28674 cp_parser_require (parser, CPP_COLON, RT_COLON);
28675 arg = cp_parser_assignment_expression (parser);
28676
28677 sel_args
28678 = chainon (sel_args,
28679 build_tree_list (selector, arg));
28680
28681 token = cp_lexer_peek_token (parser->lexer);
28682 }
28683
28684 /* Handle non-selector arguments, if any. */
28685 while (token->type == CPP_COMMA)
28686 {
28687 tree arg;
28688
28689 cp_lexer_consume_token (parser->lexer);
28690 arg = cp_parser_assignment_expression (parser);
28691
28692 addl_args
28693 = chainon (addl_args,
28694 build_tree_list (NULL_TREE, arg));
28695
28696 token = cp_lexer_peek_token (parser->lexer);
28697 }
28698
28699 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28700 {
28701 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28702 return build_tree_list (error_mark_node, error_mark_node);
28703 }
28704
28705 return build_tree_list (sel_args, addl_args);
28706 }
28707
28708 /* Parse an Objective-C encode expression.
28709
28710 objc-encode-expression:
28711 @encode objc-typename
28712
28713 Returns an encoded representation of the type argument. */
28714
28715 static cp_expr
28716 cp_parser_objc_encode_expression (cp_parser* parser)
28717 {
28718 tree type;
28719 cp_token *token;
28720 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28721
28722 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28723 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28724 token = cp_lexer_peek_token (parser->lexer);
28725 type = complete_type (cp_parser_type_id (parser));
28726 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28727
28728 if (!type)
28729 {
28730 error_at (token->location,
28731 "%<@encode%> must specify a type as an argument");
28732 return error_mark_node;
28733 }
28734
28735 /* This happens if we find @encode(T) (where T is a template
28736 typename or something dependent on a template typename) when
28737 parsing a template. In that case, we can't compile it
28738 immediately, but we rather create an AT_ENCODE_EXPR which will
28739 need to be instantiated when the template is used.
28740 */
28741 if (dependent_type_p (type))
28742 {
28743 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28744 TREE_READONLY (value) = 1;
28745 return value;
28746 }
28747
28748
28749 /* Build a location of the form:
28750 @encode(int)
28751 ^~~~~~~~~~~~
28752 with caret==start at the @ token, finishing at the close paren. */
28753 location_t combined_loc
28754 = make_location (start_loc, start_loc,
28755 cp_lexer_previous_token (parser->lexer)->location);
28756
28757 return cp_expr (objc_build_encode_expr (type), combined_loc);
28758 }
28759
28760 /* Parse an Objective-C @defs expression. */
28761
28762 static tree
28763 cp_parser_objc_defs_expression (cp_parser *parser)
28764 {
28765 tree name;
28766
28767 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28768 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28769 name = cp_parser_identifier (parser);
28770 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28771
28772 return objc_get_class_ivars (name);
28773 }
28774
28775 /* Parse an Objective-C protocol expression.
28776
28777 objc-protocol-expression:
28778 @protocol ( identifier )
28779
28780 Returns a representation of the protocol expression. */
28781
28782 static tree
28783 cp_parser_objc_protocol_expression (cp_parser* parser)
28784 {
28785 tree proto;
28786 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28787
28788 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28789 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28790 proto = cp_parser_identifier (parser);
28791 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28792
28793 /* Build a location of the form:
28794 @protocol(prot)
28795 ^~~~~~~~~~~~~~~
28796 with caret==start at the @ token, finishing at the close paren. */
28797 location_t combined_loc
28798 = make_location (start_loc, start_loc,
28799 cp_lexer_previous_token (parser->lexer)->location);
28800 tree result = objc_build_protocol_expr (proto);
28801 protected_set_expr_location (result, combined_loc);
28802 return result;
28803 }
28804
28805 /* Parse an Objective-C selector expression.
28806
28807 objc-selector-expression:
28808 @selector ( objc-method-signature )
28809
28810 objc-method-signature:
28811 objc-selector
28812 objc-selector-seq
28813
28814 objc-selector-seq:
28815 objc-selector :
28816 objc-selector-seq objc-selector :
28817
28818 Returns a representation of the method selector. */
28819
28820 static tree
28821 cp_parser_objc_selector_expression (cp_parser* parser)
28822 {
28823 tree sel_seq = NULL_TREE;
28824 bool maybe_unary_selector_p = true;
28825 cp_token *token;
28826 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28827
28828 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28829 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28830 token = cp_lexer_peek_token (parser->lexer);
28831
28832 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28833 || token->type == CPP_SCOPE)
28834 {
28835 tree selector = NULL_TREE;
28836
28837 if (token->type != CPP_COLON
28838 || token->type == CPP_SCOPE)
28839 selector = cp_parser_objc_selector (parser);
28840
28841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28842 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28843 {
28844 /* Detect if we have a unary selector. */
28845 if (maybe_unary_selector_p)
28846 {
28847 sel_seq = selector;
28848 goto finish_selector;
28849 }
28850 else
28851 {
28852 cp_parser_error (parser, "expected %<:%>");
28853 }
28854 }
28855 maybe_unary_selector_p = false;
28856 token = cp_lexer_consume_token (parser->lexer);
28857
28858 if (token->type == CPP_SCOPE)
28859 {
28860 sel_seq
28861 = chainon (sel_seq,
28862 build_tree_list (selector, NULL_TREE));
28863 sel_seq
28864 = chainon (sel_seq,
28865 build_tree_list (NULL_TREE, NULL_TREE));
28866 }
28867 else
28868 sel_seq
28869 = chainon (sel_seq,
28870 build_tree_list (selector, NULL_TREE));
28871
28872 token = cp_lexer_peek_token (parser->lexer);
28873 }
28874
28875 finish_selector:
28876 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28877
28878
28879 /* Build a location of the form:
28880 @selector(func)
28881 ^~~~~~~~~~~~~~~
28882 with caret==start at the @ token, finishing at the close paren. */
28883 location_t combined_loc
28884 = make_location (loc, loc,
28885 cp_lexer_previous_token (parser->lexer)->location);
28886 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28887 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28888 protected_set_expr_location (result, combined_loc);
28889 return result;
28890 }
28891
28892 /* Parse a list of identifiers.
28893
28894 objc-identifier-list:
28895 identifier
28896 objc-identifier-list , identifier
28897
28898 Returns a TREE_LIST of identifier nodes. */
28899
28900 static tree
28901 cp_parser_objc_identifier_list (cp_parser* parser)
28902 {
28903 tree identifier;
28904 tree list;
28905 cp_token *sep;
28906
28907 identifier = cp_parser_identifier (parser);
28908 if (identifier == error_mark_node)
28909 return error_mark_node;
28910
28911 list = build_tree_list (NULL_TREE, identifier);
28912 sep = cp_lexer_peek_token (parser->lexer);
28913
28914 while (sep->type == CPP_COMMA)
28915 {
28916 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28917 identifier = cp_parser_identifier (parser);
28918 if (identifier == error_mark_node)
28919 return list;
28920
28921 list = chainon (list, build_tree_list (NULL_TREE,
28922 identifier));
28923 sep = cp_lexer_peek_token (parser->lexer);
28924 }
28925
28926 return list;
28927 }
28928
28929 /* Parse an Objective-C alias declaration.
28930
28931 objc-alias-declaration:
28932 @compatibility_alias identifier identifier ;
28933
28934 This function registers the alias mapping with the Objective-C front end.
28935 It returns nothing. */
28936
28937 static void
28938 cp_parser_objc_alias_declaration (cp_parser* parser)
28939 {
28940 tree alias, orig;
28941
28942 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28943 alias = cp_parser_identifier (parser);
28944 orig = cp_parser_identifier (parser);
28945 objc_declare_alias (alias, orig);
28946 cp_parser_consume_semicolon_at_end_of_statement (parser);
28947 }
28948
28949 /* Parse an Objective-C class forward-declaration.
28950
28951 objc-class-declaration:
28952 @class objc-identifier-list ;
28953
28954 The function registers the forward declarations with the Objective-C
28955 front end. It returns nothing. */
28956
28957 static void
28958 cp_parser_objc_class_declaration (cp_parser* parser)
28959 {
28960 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28961 while (true)
28962 {
28963 tree id;
28964
28965 id = cp_parser_identifier (parser);
28966 if (id == error_mark_node)
28967 break;
28968
28969 objc_declare_class (id);
28970
28971 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28972 cp_lexer_consume_token (parser->lexer);
28973 else
28974 break;
28975 }
28976 cp_parser_consume_semicolon_at_end_of_statement (parser);
28977 }
28978
28979 /* Parse a list of Objective-C protocol references.
28980
28981 objc-protocol-refs-opt:
28982 objc-protocol-refs [opt]
28983
28984 objc-protocol-refs:
28985 < objc-identifier-list >
28986
28987 Returns a TREE_LIST of identifiers, if any. */
28988
28989 static tree
28990 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28991 {
28992 tree protorefs = NULL_TREE;
28993
28994 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28995 {
28996 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28997 protorefs = cp_parser_objc_identifier_list (parser);
28998 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28999 }
29000
29001 return protorefs;
29002 }
29003
29004 /* Parse a Objective-C visibility specification. */
29005
29006 static void
29007 cp_parser_objc_visibility_spec (cp_parser* parser)
29008 {
29009 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29010
29011 switch (vis->keyword)
29012 {
29013 case RID_AT_PRIVATE:
29014 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29015 break;
29016 case RID_AT_PROTECTED:
29017 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29018 break;
29019 case RID_AT_PUBLIC:
29020 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29021 break;
29022 case RID_AT_PACKAGE:
29023 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29024 break;
29025 default:
29026 return;
29027 }
29028
29029 /* Eat '@private'/'@protected'/'@public'. */
29030 cp_lexer_consume_token (parser->lexer);
29031 }
29032
29033 /* Parse an Objective-C method type. Return 'true' if it is a class
29034 (+) method, and 'false' if it is an instance (-) method. */
29035
29036 static inline bool
29037 cp_parser_objc_method_type (cp_parser* parser)
29038 {
29039 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29040 return true;
29041 else
29042 return false;
29043 }
29044
29045 /* Parse an Objective-C protocol qualifier. */
29046
29047 static tree
29048 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29049 {
29050 tree quals = NULL_TREE, node;
29051 cp_token *token = cp_lexer_peek_token (parser->lexer);
29052
29053 node = token->u.value;
29054
29055 while (node && identifier_p (node)
29056 && (node == ridpointers [(int) RID_IN]
29057 || node == ridpointers [(int) RID_OUT]
29058 || node == ridpointers [(int) RID_INOUT]
29059 || node == ridpointers [(int) RID_BYCOPY]
29060 || node == ridpointers [(int) RID_BYREF]
29061 || node == ridpointers [(int) RID_ONEWAY]))
29062 {
29063 quals = tree_cons (NULL_TREE, node, quals);
29064 cp_lexer_consume_token (parser->lexer);
29065 token = cp_lexer_peek_token (parser->lexer);
29066 node = token->u.value;
29067 }
29068
29069 return quals;
29070 }
29071
29072 /* Parse an Objective-C typename. */
29073
29074 static tree
29075 cp_parser_objc_typename (cp_parser* parser)
29076 {
29077 tree type_name = NULL_TREE;
29078
29079 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29080 {
29081 tree proto_quals, cp_type = NULL_TREE;
29082
29083 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29084 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29085
29086 /* An ObjC type name may consist of just protocol qualifiers, in which
29087 case the type shall default to 'id'. */
29088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29089 {
29090 cp_type = cp_parser_type_id (parser);
29091
29092 /* If the type could not be parsed, an error has already
29093 been produced. For error recovery, behave as if it had
29094 not been specified, which will use the default type
29095 'id'. */
29096 if (cp_type == error_mark_node)
29097 {
29098 cp_type = NULL_TREE;
29099 /* We need to skip to the closing parenthesis as
29100 cp_parser_type_id() does not seem to do it for
29101 us. */
29102 cp_parser_skip_to_closing_parenthesis (parser,
29103 /*recovering=*/true,
29104 /*or_comma=*/false,
29105 /*consume_paren=*/false);
29106 }
29107 }
29108
29109 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29110 type_name = build_tree_list (proto_quals, cp_type);
29111 }
29112
29113 return type_name;
29114 }
29115
29116 /* Check to see if TYPE refers to an Objective-C selector name. */
29117
29118 static bool
29119 cp_parser_objc_selector_p (enum cpp_ttype type)
29120 {
29121 return (type == CPP_NAME || type == CPP_KEYWORD
29122 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29123 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29124 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29125 || type == CPP_XOR || type == CPP_XOR_EQ);
29126 }
29127
29128 /* Parse an Objective-C selector. */
29129
29130 static tree
29131 cp_parser_objc_selector (cp_parser* parser)
29132 {
29133 cp_token *token = cp_lexer_consume_token (parser->lexer);
29134
29135 if (!cp_parser_objc_selector_p (token->type))
29136 {
29137 error_at (token->location, "invalid Objective-C++ selector name");
29138 return error_mark_node;
29139 }
29140
29141 /* C++ operator names are allowed to appear in ObjC selectors. */
29142 switch (token->type)
29143 {
29144 case CPP_AND_AND: return get_identifier ("and");
29145 case CPP_AND_EQ: return get_identifier ("and_eq");
29146 case CPP_AND: return get_identifier ("bitand");
29147 case CPP_OR: return get_identifier ("bitor");
29148 case CPP_COMPL: return get_identifier ("compl");
29149 case CPP_NOT: return get_identifier ("not");
29150 case CPP_NOT_EQ: return get_identifier ("not_eq");
29151 case CPP_OR_OR: return get_identifier ("or");
29152 case CPP_OR_EQ: return get_identifier ("or_eq");
29153 case CPP_XOR: return get_identifier ("xor");
29154 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29155 default: return token->u.value;
29156 }
29157 }
29158
29159 /* Parse an Objective-C params list. */
29160
29161 static tree
29162 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29163 {
29164 tree params = NULL_TREE;
29165 bool maybe_unary_selector_p = true;
29166 cp_token *token = cp_lexer_peek_token (parser->lexer);
29167
29168 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29169 {
29170 tree selector = NULL_TREE, type_name, identifier;
29171 tree parm_attr = NULL_TREE;
29172
29173 if (token->keyword == RID_ATTRIBUTE)
29174 break;
29175
29176 if (token->type != CPP_COLON)
29177 selector = cp_parser_objc_selector (parser);
29178
29179 /* Detect if we have a unary selector. */
29180 if (maybe_unary_selector_p
29181 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29182 {
29183 params = selector; /* Might be followed by attributes. */
29184 break;
29185 }
29186
29187 maybe_unary_selector_p = false;
29188 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29189 {
29190 /* Something went quite wrong. There should be a colon
29191 here, but there is not. Stop parsing parameters. */
29192 break;
29193 }
29194 type_name = cp_parser_objc_typename (parser);
29195 /* New ObjC allows attributes on parameters too. */
29196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29197 parm_attr = cp_parser_attributes_opt (parser);
29198 identifier = cp_parser_identifier (parser);
29199
29200 params
29201 = chainon (params,
29202 objc_build_keyword_decl (selector,
29203 type_name,
29204 identifier,
29205 parm_attr));
29206
29207 token = cp_lexer_peek_token (parser->lexer);
29208 }
29209
29210 if (params == NULL_TREE)
29211 {
29212 cp_parser_error (parser, "objective-c++ method declaration is expected");
29213 return error_mark_node;
29214 }
29215
29216 /* We allow tail attributes for the method. */
29217 if (token->keyword == RID_ATTRIBUTE)
29218 {
29219 *attributes = cp_parser_attributes_opt (parser);
29220 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29221 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29222 return params;
29223 cp_parser_error (parser,
29224 "method attributes must be specified at the end");
29225 return error_mark_node;
29226 }
29227
29228 if (params == NULL_TREE)
29229 {
29230 cp_parser_error (parser, "objective-c++ method declaration is expected");
29231 return error_mark_node;
29232 }
29233 return params;
29234 }
29235
29236 /* Parse the non-keyword Objective-C params. */
29237
29238 static tree
29239 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29240 tree* attributes)
29241 {
29242 tree params = make_node (TREE_LIST);
29243 cp_token *token = cp_lexer_peek_token (parser->lexer);
29244 *ellipsisp = false; /* Initially, assume no ellipsis. */
29245
29246 while (token->type == CPP_COMMA)
29247 {
29248 cp_parameter_declarator *parmdecl;
29249 tree parm;
29250
29251 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29252 token = cp_lexer_peek_token (parser->lexer);
29253
29254 if (token->type == CPP_ELLIPSIS)
29255 {
29256 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29257 *ellipsisp = true;
29258 token = cp_lexer_peek_token (parser->lexer);
29259 break;
29260 }
29261
29262 /* TODO: parse attributes for tail parameters. */
29263 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29264 parm = grokdeclarator (parmdecl->declarator,
29265 &parmdecl->decl_specifiers,
29266 PARM, /*initialized=*/0,
29267 /*attrlist=*/NULL);
29268
29269 chainon (params, build_tree_list (NULL_TREE, parm));
29270 token = cp_lexer_peek_token (parser->lexer);
29271 }
29272
29273 /* We allow tail attributes for the method. */
29274 if (token->keyword == RID_ATTRIBUTE)
29275 {
29276 if (*attributes == NULL_TREE)
29277 {
29278 *attributes = cp_parser_attributes_opt (parser);
29279 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29280 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29281 return params;
29282 }
29283 else
29284 /* We have an error, but parse the attributes, so that we can
29285 carry on. */
29286 *attributes = cp_parser_attributes_opt (parser);
29287
29288 cp_parser_error (parser,
29289 "method attributes must be specified at the end");
29290 return error_mark_node;
29291 }
29292
29293 return params;
29294 }
29295
29296 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29297
29298 static void
29299 cp_parser_objc_interstitial_code (cp_parser* parser)
29300 {
29301 cp_token *token = cp_lexer_peek_token (parser->lexer);
29302
29303 /* If the next token is `extern' and the following token is a string
29304 literal, then we have a linkage specification. */
29305 if (token->keyword == RID_EXTERN
29306 && cp_parser_is_pure_string_literal
29307 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29308 cp_parser_linkage_specification (parser);
29309 /* Handle #pragma, if any. */
29310 else if (token->type == CPP_PRAGMA)
29311 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29312 /* Allow stray semicolons. */
29313 else if (token->type == CPP_SEMICOLON)
29314 cp_lexer_consume_token (parser->lexer);
29315 /* Mark methods as optional or required, when building protocols. */
29316 else if (token->keyword == RID_AT_OPTIONAL)
29317 {
29318 cp_lexer_consume_token (parser->lexer);
29319 objc_set_method_opt (true);
29320 }
29321 else if (token->keyword == RID_AT_REQUIRED)
29322 {
29323 cp_lexer_consume_token (parser->lexer);
29324 objc_set_method_opt (false);
29325 }
29326 else if (token->keyword == RID_NAMESPACE)
29327 cp_parser_namespace_definition (parser);
29328 /* Other stray characters must generate errors. */
29329 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29330 {
29331 cp_lexer_consume_token (parser->lexer);
29332 error ("stray %qs between Objective-C++ methods",
29333 token->type == CPP_OPEN_BRACE ? "{" : "}");
29334 }
29335 /* Finally, try to parse a block-declaration, or a function-definition. */
29336 else
29337 cp_parser_block_declaration (parser, /*statement_p=*/false);
29338 }
29339
29340 /* Parse a method signature. */
29341
29342 static tree
29343 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29344 {
29345 tree rettype, kwdparms, optparms;
29346 bool ellipsis = false;
29347 bool is_class_method;
29348
29349 is_class_method = cp_parser_objc_method_type (parser);
29350 rettype = cp_parser_objc_typename (parser);
29351 *attributes = NULL_TREE;
29352 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29353 if (kwdparms == error_mark_node)
29354 return error_mark_node;
29355 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29356 if (optparms == error_mark_node)
29357 return error_mark_node;
29358
29359 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29360 }
29361
29362 static bool
29363 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29364 {
29365 tree tattr;
29366 cp_lexer_save_tokens (parser->lexer);
29367 tattr = cp_parser_attributes_opt (parser);
29368 gcc_assert (tattr) ;
29369
29370 /* If the attributes are followed by a method introducer, this is not allowed.
29371 Dump the attributes and flag the situation. */
29372 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29373 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29374 return true;
29375
29376 /* Otherwise, the attributes introduce some interstitial code, possibly so
29377 rewind to allow that check. */
29378 cp_lexer_rollback_tokens (parser->lexer);
29379 return false;
29380 }
29381
29382 /* Parse an Objective-C method prototype list. */
29383
29384 static void
29385 cp_parser_objc_method_prototype_list (cp_parser* parser)
29386 {
29387 cp_token *token = cp_lexer_peek_token (parser->lexer);
29388
29389 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29390 {
29391 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29392 {
29393 tree attributes, sig;
29394 bool is_class_method;
29395 if (token->type == CPP_PLUS)
29396 is_class_method = true;
29397 else
29398 is_class_method = false;
29399 sig = cp_parser_objc_method_signature (parser, &attributes);
29400 if (sig == error_mark_node)
29401 {
29402 cp_parser_skip_to_end_of_block_or_statement (parser);
29403 token = cp_lexer_peek_token (parser->lexer);
29404 continue;
29405 }
29406 objc_add_method_declaration (is_class_method, sig, attributes);
29407 cp_parser_consume_semicolon_at_end_of_statement (parser);
29408 }
29409 else if (token->keyword == RID_AT_PROPERTY)
29410 cp_parser_objc_at_property_declaration (parser);
29411 else if (token->keyword == RID_ATTRIBUTE
29412 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29413 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29414 OPT_Wattributes,
29415 "prefix attributes are ignored for methods");
29416 else
29417 /* Allow for interspersed non-ObjC++ code. */
29418 cp_parser_objc_interstitial_code (parser);
29419
29420 token = cp_lexer_peek_token (parser->lexer);
29421 }
29422
29423 if (token->type != CPP_EOF)
29424 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29425 else
29426 cp_parser_error (parser, "expected %<@end%>");
29427
29428 objc_finish_interface ();
29429 }
29430
29431 /* Parse an Objective-C method definition list. */
29432
29433 static void
29434 cp_parser_objc_method_definition_list (cp_parser* parser)
29435 {
29436 cp_token *token = cp_lexer_peek_token (parser->lexer);
29437
29438 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29439 {
29440 tree meth;
29441
29442 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29443 {
29444 cp_token *ptk;
29445 tree sig, attribute;
29446 bool is_class_method;
29447 if (token->type == CPP_PLUS)
29448 is_class_method = true;
29449 else
29450 is_class_method = false;
29451 push_deferring_access_checks (dk_deferred);
29452 sig = cp_parser_objc_method_signature (parser, &attribute);
29453 if (sig == error_mark_node)
29454 {
29455 cp_parser_skip_to_end_of_block_or_statement (parser);
29456 token = cp_lexer_peek_token (parser->lexer);
29457 continue;
29458 }
29459 objc_start_method_definition (is_class_method, sig, attribute,
29460 NULL_TREE);
29461
29462 /* For historical reasons, we accept an optional semicolon. */
29463 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29464 cp_lexer_consume_token (parser->lexer);
29465
29466 ptk = cp_lexer_peek_token (parser->lexer);
29467 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29468 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29469 {
29470 perform_deferred_access_checks (tf_warning_or_error);
29471 stop_deferring_access_checks ();
29472 meth = cp_parser_function_definition_after_declarator (parser,
29473 false);
29474 pop_deferring_access_checks ();
29475 objc_finish_method_definition (meth);
29476 }
29477 }
29478 /* The following case will be removed once @synthesize is
29479 completely implemented. */
29480 else if (token->keyword == RID_AT_PROPERTY)
29481 cp_parser_objc_at_property_declaration (parser);
29482 else if (token->keyword == RID_AT_SYNTHESIZE)
29483 cp_parser_objc_at_synthesize_declaration (parser);
29484 else if (token->keyword == RID_AT_DYNAMIC)
29485 cp_parser_objc_at_dynamic_declaration (parser);
29486 else if (token->keyword == RID_ATTRIBUTE
29487 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29488 warning_at (token->location, OPT_Wattributes,
29489 "prefix attributes are ignored for methods");
29490 else
29491 /* Allow for interspersed non-ObjC++ code. */
29492 cp_parser_objc_interstitial_code (parser);
29493
29494 token = cp_lexer_peek_token (parser->lexer);
29495 }
29496
29497 if (token->type != CPP_EOF)
29498 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29499 else
29500 cp_parser_error (parser, "expected %<@end%>");
29501
29502 objc_finish_implementation ();
29503 }
29504
29505 /* Parse Objective-C ivars. */
29506
29507 static void
29508 cp_parser_objc_class_ivars (cp_parser* parser)
29509 {
29510 cp_token *token = cp_lexer_peek_token (parser->lexer);
29511
29512 if (token->type != CPP_OPEN_BRACE)
29513 return; /* No ivars specified. */
29514
29515 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29516 token = cp_lexer_peek_token (parser->lexer);
29517
29518 while (token->type != CPP_CLOSE_BRACE
29519 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29520 {
29521 cp_decl_specifier_seq declspecs;
29522 int decl_class_or_enum_p;
29523 tree prefix_attributes;
29524
29525 cp_parser_objc_visibility_spec (parser);
29526
29527 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29528 break;
29529
29530 cp_parser_decl_specifier_seq (parser,
29531 CP_PARSER_FLAGS_OPTIONAL,
29532 &declspecs,
29533 &decl_class_or_enum_p);
29534
29535 /* auto, register, static, extern, mutable. */
29536 if (declspecs.storage_class != sc_none)
29537 {
29538 cp_parser_error (parser, "invalid type for instance variable");
29539 declspecs.storage_class = sc_none;
29540 }
29541
29542 /* thread_local. */
29543 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29544 {
29545 cp_parser_error (parser, "invalid type for instance variable");
29546 declspecs.locations[ds_thread] = 0;
29547 }
29548
29549 /* typedef. */
29550 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29551 {
29552 cp_parser_error (parser, "invalid type for instance variable");
29553 declspecs.locations[ds_typedef] = 0;
29554 }
29555
29556 prefix_attributes = declspecs.attributes;
29557 declspecs.attributes = NULL_TREE;
29558
29559 /* Keep going until we hit the `;' at the end of the
29560 declaration. */
29561 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29562 {
29563 tree width = NULL_TREE, attributes, first_attribute, decl;
29564 cp_declarator *declarator = NULL;
29565 int ctor_dtor_or_conv_p;
29566
29567 /* Check for a (possibly unnamed) bitfield declaration. */
29568 token = cp_lexer_peek_token (parser->lexer);
29569 if (token->type == CPP_COLON)
29570 goto eat_colon;
29571
29572 if (token->type == CPP_NAME
29573 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29574 == CPP_COLON))
29575 {
29576 /* Get the name of the bitfield. */
29577 declarator = make_id_declarator (NULL_TREE,
29578 cp_parser_identifier (parser),
29579 sfk_none);
29580
29581 eat_colon:
29582 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29583 /* Get the width of the bitfield. */
29584 width
29585 = cp_parser_constant_expression (parser);
29586 }
29587 else
29588 {
29589 /* Parse the declarator. */
29590 declarator
29591 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29592 &ctor_dtor_or_conv_p,
29593 /*parenthesized_p=*/NULL,
29594 /*member_p=*/false,
29595 /*friend_p=*/false);
29596 }
29597
29598 /* Look for attributes that apply to the ivar. */
29599 attributes = cp_parser_attributes_opt (parser);
29600 /* Remember which attributes are prefix attributes and
29601 which are not. */
29602 first_attribute = attributes;
29603 /* Combine the attributes. */
29604 attributes = chainon (prefix_attributes, attributes);
29605
29606 if (width)
29607 /* Create the bitfield declaration. */
29608 decl = grokbitfield (declarator, &declspecs,
29609 width,
29610 attributes);
29611 else
29612 decl = grokfield (declarator, &declspecs,
29613 NULL_TREE, /*init_const_expr_p=*/false,
29614 NULL_TREE, attributes);
29615
29616 /* Add the instance variable. */
29617 if (decl != error_mark_node && decl != NULL_TREE)
29618 objc_add_instance_variable (decl);
29619
29620 /* Reset PREFIX_ATTRIBUTES. */
29621 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29622 attributes = TREE_CHAIN (attributes);
29623 if (attributes)
29624 TREE_CHAIN (attributes) = NULL_TREE;
29625
29626 token = cp_lexer_peek_token (parser->lexer);
29627
29628 if (token->type == CPP_COMMA)
29629 {
29630 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29631 continue;
29632 }
29633 break;
29634 }
29635
29636 cp_parser_consume_semicolon_at_end_of_statement (parser);
29637 token = cp_lexer_peek_token (parser->lexer);
29638 }
29639
29640 if (token->keyword == RID_AT_END)
29641 cp_parser_error (parser, "expected %<}%>");
29642
29643 /* Do not consume the RID_AT_END, so it will be read again as terminating
29644 the @interface of @implementation. */
29645 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29646 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29647
29648 /* For historical reasons, we accept an optional semicolon. */
29649 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29650 cp_lexer_consume_token (parser->lexer);
29651 }
29652
29653 /* Parse an Objective-C protocol declaration. */
29654
29655 static void
29656 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29657 {
29658 tree proto, protorefs;
29659 cp_token *tok;
29660
29661 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29663 {
29664 tok = cp_lexer_peek_token (parser->lexer);
29665 error_at (tok->location, "identifier expected after %<@protocol%>");
29666 cp_parser_consume_semicolon_at_end_of_statement (parser);
29667 return;
29668 }
29669
29670 /* See if we have a forward declaration or a definition. */
29671 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29672
29673 /* Try a forward declaration first. */
29674 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29675 {
29676 while (true)
29677 {
29678 tree id;
29679
29680 id = cp_parser_identifier (parser);
29681 if (id == error_mark_node)
29682 break;
29683
29684 objc_declare_protocol (id, attributes);
29685
29686 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29687 cp_lexer_consume_token (parser->lexer);
29688 else
29689 break;
29690 }
29691 cp_parser_consume_semicolon_at_end_of_statement (parser);
29692 }
29693
29694 /* Ok, we got a full-fledged definition (or at least should). */
29695 else
29696 {
29697 proto = cp_parser_identifier (parser);
29698 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29699 objc_start_protocol (proto, protorefs, attributes);
29700 cp_parser_objc_method_prototype_list (parser);
29701 }
29702 }
29703
29704 /* Parse an Objective-C superclass or category. */
29705
29706 static void
29707 cp_parser_objc_superclass_or_category (cp_parser *parser,
29708 bool iface_p,
29709 tree *super,
29710 tree *categ, bool *is_class_extension)
29711 {
29712 cp_token *next = cp_lexer_peek_token (parser->lexer);
29713
29714 *super = *categ = NULL_TREE;
29715 *is_class_extension = false;
29716 if (next->type == CPP_COLON)
29717 {
29718 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29719 *super = cp_parser_identifier (parser);
29720 }
29721 else if (next->type == CPP_OPEN_PAREN)
29722 {
29723 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29724
29725 /* If there is no category name, and this is an @interface, we
29726 have a class extension. */
29727 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29728 {
29729 *categ = NULL_TREE;
29730 *is_class_extension = true;
29731 }
29732 else
29733 *categ = cp_parser_identifier (parser);
29734
29735 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29736 }
29737 }
29738
29739 /* Parse an Objective-C class interface. */
29740
29741 static void
29742 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29743 {
29744 tree name, super, categ, protos;
29745 bool is_class_extension;
29746
29747 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29748 name = cp_parser_identifier (parser);
29749 if (name == error_mark_node)
29750 {
29751 /* It's hard to recover because even if valid @interface stuff
29752 is to follow, we can't compile it (or validate it) if we
29753 don't even know which class it refers to. Let's assume this
29754 was a stray '@interface' token in the stream and skip it.
29755 */
29756 return;
29757 }
29758 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29759 &is_class_extension);
29760 protos = cp_parser_objc_protocol_refs_opt (parser);
29761
29762 /* We have either a class or a category on our hands. */
29763 if (categ || is_class_extension)
29764 objc_start_category_interface (name, categ, protos, attributes);
29765 else
29766 {
29767 objc_start_class_interface (name, super, protos, attributes);
29768 /* Handle instance variable declarations, if any. */
29769 cp_parser_objc_class_ivars (parser);
29770 objc_continue_interface ();
29771 }
29772
29773 cp_parser_objc_method_prototype_list (parser);
29774 }
29775
29776 /* Parse an Objective-C class implementation. */
29777
29778 static void
29779 cp_parser_objc_class_implementation (cp_parser* parser)
29780 {
29781 tree name, super, categ;
29782 bool is_class_extension;
29783
29784 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29785 name = cp_parser_identifier (parser);
29786 if (name == error_mark_node)
29787 {
29788 /* It's hard to recover because even if valid @implementation
29789 stuff is to follow, we can't compile it (or validate it) if
29790 we don't even know which class it refers to. Let's assume
29791 this was a stray '@implementation' token in the stream and
29792 skip it.
29793 */
29794 return;
29795 }
29796 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29797 &is_class_extension);
29798
29799 /* We have either a class or a category on our hands. */
29800 if (categ)
29801 objc_start_category_implementation (name, categ);
29802 else
29803 {
29804 objc_start_class_implementation (name, super);
29805 /* Handle instance variable declarations, if any. */
29806 cp_parser_objc_class_ivars (parser);
29807 objc_continue_implementation ();
29808 }
29809
29810 cp_parser_objc_method_definition_list (parser);
29811 }
29812
29813 /* Consume the @end token and finish off the implementation. */
29814
29815 static void
29816 cp_parser_objc_end_implementation (cp_parser* parser)
29817 {
29818 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29819 objc_finish_implementation ();
29820 }
29821
29822 /* Parse an Objective-C declaration. */
29823
29824 static void
29825 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29826 {
29827 /* Try to figure out what kind of declaration is present. */
29828 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29829
29830 if (attributes)
29831 switch (kwd->keyword)
29832 {
29833 case RID_AT_ALIAS:
29834 case RID_AT_CLASS:
29835 case RID_AT_END:
29836 error_at (kwd->location, "attributes may not be specified before"
29837 " the %<@%D%> Objective-C++ keyword",
29838 kwd->u.value);
29839 attributes = NULL;
29840 break;
29841 case RID_AT_IMPLEMENTATION:
29842 warning_at (kwd->location, OPT_Wattributes,
29843 "prefix attributes are ignored before %<@%D%>",
29844 kwd->u.value);
29845 attributes = NULL;
29846 default:
29847 break;
29848 }
29849
29850 switch (kwd->keyword)
29851 {
29852 case RID_AT_ALIAS:
29853 cp_parser_objc_alias_declaration (parser);
29854 break;
29855 case RID_AT_CLASS:
29856 cp_parser_objc_class_declaration (parser);
29857 break;
29858 case RID_AT_PROTOCOL:
29859 cp_parser_objc_protocol_declaration (parser, attributes);
29860 break;
29861 case RID_AT_INTERFACE:
29862 cp_parser_objc_class_interface (parser, attributes);
29863 break;
29864 case RID_AT_IMPLEMENTATION:
29865 cp_parser_objc_class_implementation (parser);
29866 break;
29867 case RID_AT_END:
29868 cp_parser_objc_end_implementation (parser);
29869 break;
29870 default:
29871 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29872 kwd->u.value);
29873 cp_parser_skip_to_end_of_block_or_statement (parser);
29874 }
29875 }
29876
29877 /* Parse an Objective-C try-catch-finally statement.
29878
29879 objc-try-catch-finally-stmt:
29880 @try compound-statement objc-catch-clause-seq [opt]
29881 objc-finally-clause [opt]
29882
29883 objc-catch-clause-seq:
29884 objc-catch-clause objc-catch-clause-seq [opt]
29885
29886 objc-catch-clause:
29887 @catch ( objc-exception-declaration ) compound-statement
29888
29889 objc-finally-clause:
29890 @finally compound-statement
29891
29892 objc-exception-declaration:
29893 parameter-declaration
29894 '...'
29895
29896 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29897
29898 Returns NULL_TREE.
29899
29900 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29901 for C. Keep them in sync. */
29902
29903 static tree
29904 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29905 {
29906 location_t location;
29907 tree stmt;
29908
29909 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29910 location = cp_lexer_peek_token (parser->lexer)->location;
29911 objc_maybe_warn_exceptions (location);
29912 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29913 node, lest it get absorbed into the surrounding block. */
29914 stmt = push_stmt_list ();
29915 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29916 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29917
29918 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29919 {
29920 cp_parameter_declarator *parm;
29921 tree parameter_declaration = error_mark_node;
29922 bool seen_open_paren = false;
29923
29924 cp_lexer_consume_token (parser->lexer);
29925 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29926 seen_open_paren = true;
29927 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29928 {
29929 /* We have "@catch (...)" (where the '...' are literally
29930 what is in the code). Skip the '...'.
29931 parameter_declaration is set to NULL_TREE, and
29932 objc_being_catch_clauses() knows that that means
29933 '...'. */
29934 cp_lexer_consume_token (parser->lexer);
29935 parameter_declaration = NULL_TREE;
29936 }
29937 else
29938 {
29939 /* We have "@catch (NSException *exception)" or something
29940 like that. Parse the parameter declaration. */
29941 parm = cp_parser_parameter_declaration (parser, false, NULL);
29942 if (parm == NULL)
29943 parameter_declaration = error_mark_node;
29944 else
29945 parameter_declaration = grokdeclarator (parm->declarator,
29946 &parm->decl_specifiers,
29947 PARM, /*initialized=*/0,
29948 /*attrlist=*/NULL);
29949 }
29950 if (seen_open_paren)
29951 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29952 else
29953 {
29954 /* If there was no open parenthesis, we are recovering from
29955 an error, and we are trying to figure out what mistake
29956 the user has made. */
29957
29958 /* If there is an immediate closing parenthesis, the user
29959 probably forgot the opening one (ie, they typed "@catch
29960 NSException *e)". Parse the closing parenthesis and keep
29961 going. */
29962 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29963 cp_lexer_consume_token (parser->lexer);
29964
29965 /* If these is no immediate closing parenthesis, the user
29966 probably doesn't know that parenthesis are required at
29967 all (ie, they typed "@catch NSException *e"). So, just
29968 forget about the closing parenthesis and keep going. */
29969 }
29970 objc_begin_catch_clause (parameter_declaration);
29971 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29972 objc_finish_catch_clause ();
29973 }
29974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29975 {
29976 cp_lexer_consume_token (parser->lexer);
29977 location = cp_lexer_peek_token (parser->lexer)->location;
29978 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29979 node, lest it get absorbed into the surrounding block. */
29980 stmt = push_stmt_list ();
29981 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29982 objc_build_finally_clause (location, pop_stmt_list (stmt));
29983 }
29984
29985 return objc_finish_try_stmt ();
29986 }
29987
29988 /* Parse an Objective-C synchronized statement.
29989
29990 objc-synchronized-stmt:
29991 @synchronized ( expression ) compound-statement
29992
29993 Returns NULL_TREE. */
29994
29995 static tree
29996 cp_parser_objc_synchronized_statement (cp_parser *parser)
29997 {
29998 location_t location;
29999 tree lock, stmt;
30000
30001 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30002
30003 location = cp_lexer_peek_token (parser->lexer)->location;
30004 objc_maybe_warn_exceptions (location);
30005 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30006 lock = cp_parser_expression (parser);
30007 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30008
30009 /* NB: The @synchronized 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
30014 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30015 }
30016
30017 /* Parse an Objective-C throw statement.
30018
30019 objc-throw-stmt:
30020 @throw assignment-expression [opt] ;
30021
30022 Returns a constructed '@throw' statement. */
30023
30024 static tree
30025 cp_parser_objc_throw_statement (cp_parser *parser)
30026 {
30027 tree expr = NULL_TREE;
30028 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30029
30030 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30031
30032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30033 expr = cp_parser_expression (parser);
30034
30035 cp_parser_consume_semicolon_at_end_of_statement (parser);
30036
30037 return objc_build_throw_stmt (loc, expr);
30038 }
30039
30040 /* Parse an Objective-C statement. */
30041
30042 static tree
30043 cp_parser_objc_statement (cp_parser * parser)
30044 {
30045 /* Try to figure out what kind of declaration is present. */
30046 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30047
30048 switch (kwd->keyword)
30049 {
30050 case RID_AT_TRY:
30051 return cp_parser_objc_try_catch_finally_statement (parser);
30052 case RID_AT_SYNCHRONIZED:
30053 return cp_parser_objc_synchronized_statement (parser);
30054 case RID_AT_THROW:
30055 return cp_parser_objc_throw_statement (parser);
30056 default:
30057 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30058 kwd->u.value);
30059 cp_parser_skip_to_end_of_block_or_statement (parser);
30060 }
30061
30062 return error_mark_node;
30063 }
30064
30065 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30066 look ahead to see if an objc keyword follows the attributes. This
30067 is to detect the use of prefix attributes on ObjC @interface and
30068 @protocol. */
30069
30070 static bool
30071 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30072 {
30073 cp_lexer_save_tokens (parser->lexer);
30074 *attrib = cp_parser_attributes_opt (parser);
30075 gcc_assert (*attrib);
30076 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30077 {
30078 cp_lexer_commit_tokens (parser->lexer);
30079 return true;
30080 }
30081 cp_lexer_rollback_tokens (parser->lexer);
30082 return false;
30083 }
30084
30085 /* This routine is a minimal replacement for
30086 c_parser_struct_declaration () used when parsing the list of
30087 types/names or ObjC++ properties. For example, when parsing the
30088 code
30089
30090 @property (readonly) int a, b, c;
30091
30092 this function is responsible for parsing "int a, int b, int c" and
30093 returning the declarations as CHAIN of DECLs.
30094
30095 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30096 similar parsing. */
30097 static tree
30098 cp_parser_objc_struct_declaration (cp_parser *parser)
30099 {
30100 tree decls = NULL_TREE;
30101 cp_decl_specifier_seq declspecs;
30102 int decl_class_or_enum_p;
30103 tree prefix_attributes;
30104
30105 cp_parser_decl_specifier_seq (parser,
30106 CP_PARSER_FLAGS_NONE,
30107 &declspecs,
30108 &decl_class_or_enum_p);
30109
30110 if (declspecs.type == error_mark_node)
30111 return error_mark_node;
30112
30113 /* auto, register, static, extern, mutable. */
30114 if (declspecs.storage_class != sc_none)
30115 {
30116 cp_parser_error (parser, "invalid type for property");
30117 declspecs.storage_class = sc_none;
30118 }
30119
30120 /* thread_local. */
30121 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30122 {
30123 cp_parser_error (parser, "invalid type for property");
30124 declspecs.locations[ds_thread] = 0;
30125 }
30126
30127 /* typedef. */
30128 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30129 {
30130 cp_parser_error (parser, "invalid type for property");
30131 declspecs.locations[ds_typedef] = 0;
30132 }
30133
30134 prefix_attributes = declspecs.attributes;
30135 declspecs.attributes = NULL_TREE;
30136
30137 /* Keep going until we hit the `;' at the end of the declaration. */
30138 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30139 {
30140 tree attributes, first_attribute, decl;
30141 cp_declarator *declarator;
30142 cp_token *token;
30143
30144 /* Parse the declarator. */
30145 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30146 NULL, NULL, false, false);
30147
30148 /* Look for attributes that apply to the ivar. */
30149 attributes = cp_parser_attributes_opt (parser);
30150 /* Remember which attributes are prefix attributes and
30151 which are not. */
30152 first_attribute = attributes;
30153 /* Combine the attributes. */
30154 attributes = chainon (prefix_attributes, attributes);
30155
30156 decl = grokfield (declarator, &declspecs,
30157 NULL_TREE, /*init_const_expr_p=*/false,
30158 NULL_TREE, attributes);
30159
30160 if (decl == error_mark_node || decl == NULL_TREE)
30161 return error_mark_node;
30162
30163 /* Reset PREFIX_ATTRIBUTES. */
30164 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30165 attributes = TREE_CHAIN (attributes);
30166 if (attributes)
30167 TREE_CHAIN (attributes) = NULL_TREE;
30168
30169 DECL_CHAIN (decl) = decls;
30170 decls = decl;
30171
30172 token = cp_lexer_peek_token (parser->lexer);
30173 if (token->type == CPP_COMMA)
30174 {
30175 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30176 continue;
30177 }
30178 else
30179 break;
30180 }
30181 return decls;
30182 }
30183
30184 /* Parse an Objective-C @property declaration. The syntax is:
30185
30186 objc-property-declaration:
30187 '@property' objc-property-attributes[opt] struct-declaration ;
30188
30189 objc-property-attributes:
30190 '(' objc-property-attribute-list ')'
30191
30192 objc-property-attribute-list:
30193 objc-property-attribute
30194 objc-property-attribute-list, objc-property-attribute
30195
30196 objc-property-attribute
30197 'getter' = identifier
30198 'setter' = identifier
30199 'readonly'
30200 'readwrite'
30201 'assign'
30202 'retain'
30203 'copy'
30204 'nonatomic'
30205
30206 For example:
30207 @property NSString *name;
30208 @property (readonly) id object;
30209 @property (retain, nonatomic, getter=getTheName) id name;
30210 @property int a, b, c;
30211
30212 PS: This function is identical to
30213 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30214 static void
30215 cp_parser_objc_at_property_declaration (cp_parser *parser)
30216 {
30217 /* The following variables hold the attributes of the properties as
30218 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30219 seen. When we see an attribute, we set them to 'true' (if they
30220 are boolean properties) or to the identifier (if they have an
30221 argument, ie, for getter and setter). Note that here we only
30222 parse the list of attributes, check the syntax and accumulate the
30223 attributes that we find. objc_add_property_declaration() will
30224 then process the information. */
30225 bool property_assign = false;
30226 bool property_copy = false;
30227 tree property_getter_ident = NULL_TREE;
30228 bool property_nonatomic = false;
30229 bool property_readonly = false;
30230 bool property_readwrite = false;
30231 bool property_retain = false;
30232 tree property_setter_ident = NULL_TREE;
30233
30234 /* 'properties' is the list of properties that we read. Usually a
30235 single one, but maybe more (eg, in "@property int a, b, c;" there
30236 are three). */
30237 tree properties;
30238 location_t loc;
30239
30240 loc = cp_lexer_peek_token (parser->lexer)->location;
30241
30242 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30243
30244 /* Parse the optional attribute list... */
30245 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30246 {
30247 /* Eat the '('. */
30248 cp_lexer_consume_token (parser->lexer);
30249
30250 while (true)
30251 {
30252 bool syntax_error = false;
30253 cp_token *token = cp_lexer_peek_token (parser->lexer);
30254 enum rid keyword;
30255
30256 if (token->type != CPP_NAME)
30257 {
30258 cp_parser_error (parser, "expected identifier");
30259 break;
30260 }
30261 keyword = C_RID_CODE (token->u.value);
30262 cp_lexer_consume_token (parser->lexer);
30263 switch (keyword)
30264 {
30265 case RID_ASSIGN: property_assign = true; break;
30266 case RID_COPY: property_copy = true; break;
30267 case RID_NONATOMIC: property_nonatomic = true; break;
30268 case RID_READONLY: property_readonly = true; break;
30269 case RID_READWRITE: property_readwrite = true; break;
30270 case RID_RETAIN: property_retain = true; break;
30271
30272 case RID_GETTER:
30273 case RID_SETTER:
30274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30275 {
30276 if (keyword == RID_GETTER)
30277 cp_parser_error (parser,
30278 "missing %<=%> (after %<getter%> attribute)");
30279 else
30280 cp_parser_error (parser,
30281 "missing %<=%> (after %<setter%> attribute)");
30282 syntax_error = true;
30283 break;
30284 }
30285 cp_lexer_consume_token (parser->lexer); /* eat the = */
30286 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30287 {
30288 cp_parser_error (parser, "expected identifier");
30289 syntax_error = true;
30290 break;
30291 }
30292 if (keyword == RID_SETTER)
30293 {
30294 if (property_setter_ident != NULL_TREE)
30295 {
30296 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30297 cp_lexer_consume_token (parser->lexer);
30298 }
30299 else
30300 property_setter_ident = cp_parser_objc_selector (parser);
30301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30302 cp_parser_error (parser, "setter name must terminate with %<:%>");
30303 else
30304 cp_lexer_consume_token (parser->lexer);
30305 }
30306 else
30307 {
30308 if (property_getter_ident != NULL_TREE)
30309 {
30310 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30311 cp_lexer_consume_token (parser->lexer);
30312 }
30313 else
30314 property_getter_ident = cp_parser_objc_selector (parser);
30315 }
30316 break;
30317 default:
30318 cp_parser_error (parser, "unknown property attribute");
30319 syntax_error = true;
30320 break;
30321 }
30322
30323 if (syntax_error)
30324 break;
30325
30326 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30327 cp_lexer_consume_token (parser->lexer);
30328 else
30329 break;
30330 }
30331
30332 /* FIXME: "@property (setter, assign);" will generate a spurious
30333 "error: expected ‘)’ before ‘,’ token". This is because
30334 cp_parser_require, unlike the C counterpart, will produce an
30335 error even if we are in error recovery. */
30336 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30337 {
30338 cp_parser_skip_to_closing_parenthesis (parser,
30339 /*recovering=*/true,
30340 /*or_comma=*/false,
30341 /*consume_paren=*/true);
30342 }
30343 }
30344
30345 /* ... and the property declaration(s). */
30346 properties = cp_parser_objc_struct_declaration (parser);
30347
30348 if (properties == error_mark_node)
30349 {
30350 cp_parser_skip_to_end_of_statement (parser);
30351 /* If the next token is now a `;', consume it. */
30352 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30353 cp_lexer_consume_token (parser->lexer);
30354 return;
30355 }
30356
30357 if (properties == NULL_TREE)
30358 cp_parser_error (parser, "expected identifier");
30359 else
30360 {
30361 /* Comma-separated properties are chained together in
30362 reverse order; add them one by one. */
30363 properties = nreverse (properties);
30364
30365 for (; properties; properties = TREE_CHAIN (properties))
30366 objc_add_property_declaration (loc, copy_node (properties),
30367 property_readonly, property_readwrite,
30368 property_assign, property_retain,
30369 property_copy, property_nonatomic,
30370 property_getter_ident, property_setter_ident);
30371 }
30372
30373 cp_parser_consume_semicolon_at_end_of_statement (parser);
30374 }
30375
30376 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30377
30378 objc-synthesize-declaration:
30379 @synthesize objc-synthesize-identifier-list ;
30380
30381 objc-synthesize-identifier-list:
30382 objc-synthesize-identifier
30383 objc-synthesize-identifier-list, objc-synthesize-identifier
30384
30385 objc-synthesize-identifier
30386 identifier
30387 identifier = identifier
30388
30389 For example:
30390 @synthesize MyProperty;
30391 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30392
30393 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30394 for C. Keep them in sync.
30395 */
30396 static void
30397 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30398 {
30399 tree list = NULL_TREE;
30400 location_t loc;
30401 loc = cp_lexer_peek_token (parser->lexer)->location;
30402
30403 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30404 while (true)
30405 {
30406 tree property, ivar;
30407 property = cp_parser_identifier (parser);
30408 if (property == error_mark_node)
30409 {
30410 cp_parser_consume_semicolon_at_end_of_statement (parser);
30411 return;
30412 }
30413 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30414 {
30415 cp_lexer_consume_token (parser->lexer);
30416 ivar = cp_parser_identifier (parser);
30417 if (ivar == error_mark_node)
30418 {
30419 cp_parser_consume_semicolon_at_end_of_statement (parser);
30420 return;
30421 }
30422 }
30423 else
30424 ivar = NULL_TREE;
30425 list = chainon (list, build_tree_list (ivar, property));
30426 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30427 cp_lexer_consume_token (parser->lexer);
30428 else
30429 break;
30430 }
30431 cp_parser_consume_semicolon_at_end_of_statement (parser);
30432 objc_add_synthesize_declaration (loc, list);
30433 }
30434
30435 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30436
30437 objc-dynamic-declaration:
30438 @dynamic identifier-list ;
30439
30440 For example:
30441 @dynamic MyProperty;
30442 @dynamic MyProperty, AnotherProperty;
30443
30444 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30445 for C. Keep them in sync.
30446 */
30447 static void
30448 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30449 {
30450 tree list = NULL_TREE;
30451 location_t loc;
30452 loc = cp_lexer_peek_token (parser->lexer)->location;
30453
30454 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30455 while (true)
30456 {
30457 tree property;
30458 property = cp_parser_identifier (parser);
30459 if (property == error_mark_node)
30460 {
30461 cp_parser_consume_semicolon_at_end_of_statement (parser);
30462 return;
30463 }
30464 list = chainon (list, build_tree_list (NULL, property));
30465 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30466 cp_lexer_consume_token (parser->lexer);
30467 else
30468 break;
30469 }
30470 cp_parser_consume_semicolon_at_end_of_statement (parser);
30471 objc_add_dynamic_declaration (loc, list);
30472 }
30473
30474 \f
30475 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30476
30477 /* Returns name of the next clause.
30478 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30479 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30480 returned and the token is consumed. */
30481
30482 static pragma_omp_clause
30483 cp_parser_omp_clause_name (cp_parser *parser)
30484 {
30485 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30486
30487 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30488 result = PRAGMA_OACC_CLAUSE_AUTO;
30489 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30490 result = PRAGMA_OMP_CLAUSE_IF;
30491 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30492 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30493 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30494 result = PRAGMA_OACC_CLAUSE_DELETE;
30495 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30496 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30497 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30498 result = PRAGMA_OMP_CLAUSE_FOR;
30499 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30500 {
30501 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30502 const char *p = IDENTIFIER_POINTER (id);
30503
30504 switch (p[0])
30505 {
30506 case 'a':
30507 if (!strcmp ("aligned", p))
30508 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30509 else if (!strcmp ("async", p))
30510 result = PRAGMA_OACC_CLAUSE_ASYNC;
30511 break;
30512 case 'c':
30513 if (!strcmp ("collapse", p))
30514 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30515 else if (!strcmp ("copy", p))
30516 result = PRAGMA_OACC_CLAUSE_COPY;
30517 else if (!strcmp ("copyin", p))
30518 result = PRAGMA_OMP_CLAUSE_COPYIN;
30519 else if (!strcmp ("copyout", p))
30520 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30521 else if (!strcmp ("copyprivate", p))
30522 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30523 else if (!strcmp ("create", p))
30524 result = PRAGMA_OACC_CLAUSE_CREATE;
30525 break;
30526 case 'd':
30527 if (!strcmp ("defaultmap", p))
30528 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30529 else if (!strcmp ("depend", p))
30530 result = PRAGMA_OMP_CLAUSE_DEPEND;
30531 else if (!strcmp ("device", p))
30532 result = PRAGMA_OMP_CLAUSE_DEVICE;
30533 else if (!strcmp ("deviceptr", p))
30534 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30535 else if (!strcmp ("device_resident", p))
30536 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30537 else if (!strcmp ("dist_schedule", p))
30538 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30539 break;
30540 case 'f':
30541 if (!strcmp ("final", p))
30542 result = PRAGMA_OMP_CLAUSE_FINAL;
30543 else if (!strcmp ("firstprivate", p))
30544 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30545 else if (!strcmp ("from", p))
30546 result = PRAGMA_OMP_CLAUSE_FROM;
30547 break;
30548 case 'g':
30549 if (!strcmp ("gang", p))
30550 result = PRAGMA_OACC_CLAUSE_GANG;
30551 else if (!strcmp ("grainsize", p))
30552 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30553 break;
30554 case 'h':
30555 if (!strcmp ("hint", p))
30556 result = PRAGMA_OMP_CLAUSE_HINT;
30557 else if (!strcmp ("host", p))
30558 result = PRAGMA_OACC_CLAUSE_HOST;
30559 break;
30560 case 'i':
30561 if (!strcmp ("inbranch", p))
30562 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30563 else if (!strcmp ("independent", p))
30564 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30565 else if (!strcmp ("is_device_ptr", p))
30566 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30567 break;
30568 case 'l':
30569 if (!strcmp ("lastprivate", p))
30570 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30571 else if (!strcmp ("linear", p))
30572 result = PRAGMA_OMP_CLAUSE_LINEAR;
30573 else if (!strcmp ("link", p))
30574 result = PRAGMA_OMP_CLAUSE_LINK;
30575 break;
30576 case 'm':
30577 if (!strcmp ("map", p))
30578 result = PRAGMA_OMP_CLAUSE_MAP;
30579 else if (!strcmp ("mergeable", p))
30580 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30581 else if (flag_cilkplus && !strcmp ("mask", p))
30582 result = PRAGMA_CILK_CLAUSE_MASK;
30583 break;
30584 case 'n':
30585 if (!strcmp ("nogroup", p))
30586 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30587 else if (!strcmp ("notinbranch", p))
30588 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30589 else if (!strcmp ("nowait", p))
30590 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30591 else if (flag_cilkplus && !strcmp ("nomask", p))
30592 result = PRAGMA_CILK_CLAUSE_NOMASK;
30593 else if (!strcmp ("num_gangs", p))
30594 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30595 else if (!strcmp ("num_tasks", p))
30596 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30597 else if (!strcmp ("num_teams", p))
30598 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30599 else if (!strcmp ("num_threads", p))
30600 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30601 else if (!strcmp ("num_workers", p))
30602 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30603 break;
30604 case 'o':
30605 if (!strcmp ("ordered", p))
30606 result = PRAGMA_OMP_CLAUSE_ORDERED;
30607 break;
30608 case 'p':
30609 if (!strcmp ("parallel", p))
30610 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30611 else if (!strcmp ("present", p))
30612 result = PRAGMA_OACC_CLAUSE_PRESENT;
30613 else if (!strcmp ("present_or_copy", p)
30614 || !strcmp ("pcopy", p))
30615 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30616 else if (!strcmp ("present_or_copyin", p)
30617 || !strcmp ("pcopyin", p))
30618 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30619 else if (!strcmp ("present_or_copyout", p)
30620 || !strcmp ("pcopyout", p))
30621 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30622 else if (!strcmp ("present_or_create", p)
30623 || !strcmp ("pcreate", p))
30624 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30625 else if (!strcmp ("priority", p))
30626 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30627 else if (!strcmp ("proc_bind", p))
30628 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30629 break;
30630 case 'r':
30631 if (!strcmp ("reduction", p))
30632 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30633 break;
30634 case 's':
30635 if (!strcmp ("safelen", p))
30636 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30637 else if (!strcmp ("schedule", p))
30638 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30639 else if (!strcmp ("sections", p))
30640 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30641 else if (!strcmp ("self", p))
30642 result = PRAGMA_OACC_CLAUSE_SELF;
30643 else if (!strcmp ("seq", p))
30644 result = PRAGMA_OACC_CLAUSE_SEQ;
30645 else if (!strcmp ("shared", p))
30646 result = PRAGMA_OMP_CLAUSE_SHARED;
30647 else if (!strcmp ("simd", p))
30648 result = PRAGMA_OMP_CLAUSE_SIMD;
30649 else if (!strcmp ("simdlen", p))
30650 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30651 break;
30652 case 't':
30653 if (!strcmp ("taskgroup", p))
30654 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30655 else if (!strcmp ("thread_limit", p))
30656 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30657 else if (!strcmp ("threads", p))
30658 result = PRAGMA_OMP_CLAUSE_THREADS;
30659 else if (!strcmp ("tile", p))
30660 result = PRAGMA_OACC_CLAUSE_TILE;
30661 else if (!strcmp ("to", p))
30662 result = PRAGMA_OMP_CLAUSE_TO;
30663 break;
30664 case 'u':
30665 if (!strcmp ("uniform", p))
30666 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30667 else if (!strcmp ("untied", p))
30668 result = PRAGMA_OMP_CLAUSE_UNTIED;
30669 else if (!strcmp ("use_device", p))
30670 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30671 else if (!strcmp ("use_device_ptr", p))
30672 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30673 break;
30674 case 'v':
30675 if (!strcmp ("vector", p))
30676 result = PRAGMA_OACC_CLAUSE_VECTOR;
30677 else if (!strcmp ("vector_length", p))
30678 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30679 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30680 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30681 break;
30682 case 'w':
30683 if (!strcmp ("wait", p))
30684 result = PRAGMA_OACC_CLAUSE_WAIT;
30685 else if (!strcmp ("worker", p))
30686 result = PRAGMA_OACC_CLAUSE_WORKER;
30687 break;
30688 }
30689 }
30690
30691 if (result != PRAGMA_OMP_CLAUSE_NONE)
30692 cp_lexer_consume_token (parser->lexer);
30693
30694 return result;
30695 }
30696
30697 /* Validate that a clause of the given type does not already exist. */
30698
30699 static void
30700 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30701 const char *name, location_t location)
30702 {
30703 tree c;
30704
30705 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30706 if (OMP_CLAUSE_CODE (c) == code)
30707 {
30708 error_at (location, "too many %qs clauses", name);
30709 break;
30710 }
30711 }
30712
30713 /* OpenMP 2.5:
30714 variable-list:
30715 identifier
30716 variable-list , identifier
30717
30718 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30719 colon). An opening parenthesis will have been consumed by the caller.
30720
30721 If KIND is nonzero, create the appropriate node and install the decl
30722 in OMP_CLAUSE_DECL and add the node to the head of the list.
30723
30724 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30725 return the list created.
30726
30727 COLON can be NULL if only closing parenthesis should end the list,
30728 or pointer to bool which will receive false if the list is terminated
30729 by closing parenthesis or true if the list is terminated by colon. */
30730
30731 static tree
30732 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30733 tree list, bool *colon)
30734 {
30735 cp_token *token;
30736 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30737 if (colon)
30738 {
30739 parser->colon_corrects_to_scope_p = false;
30740 *colon = false;
30741 }
30742 while (1)
30743 {
30744 tree name, decl;
30745
30746 token = cp_lexer_peek_token (parser->lexer);
30747 if (kind != 0
30748 && current_class_ptr
30749 && cp_parser_is_keyword (token, RID_THIS))
30750 {
30751 decl = finish_this_expr ();
30752 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30753 || CONVERT_EXPR_P (decl))
30754 decl = TREE_OPERAND (decl, 0);
30755 cp_lexer_consume_token (parser->lexer);
30756 }
30757 else
30758 {
30759 name = cp_parser_id_expression (parser, /*template_p=*/false,
30760 /*check_dependency_p=*/true,
30761 /*template_p=*/NULL,
30762 /*declarator_p=*/false,
30763 /*optional_p=*/false);
30764 if (name == error_mark_node)
30765 goto skip_comma;
30766
30767 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30768 if (decl == error_mark_node)
30769 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30770 token->location);
30771 }
30772 if (decl == error_mark_node)
30773 ;
30774 else if (kind != 0)
30775 {
30776 switch (kind)
30777 {
30778 case OMP_CLAUSE__CACHE_:
30779 /* The OpenACC cache directive explicitly only allows "array
30780 elements or subarrays". */
30781 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30782 {
30783 error_at (token->location, "expected %<[%>");
30784 decl = error_mark_node;
30785 break;
30786 }
30787 /* FALLTHROUGH. */
30788 case OMP_CLAUSE_MAP:
30789 case OMP_CLAUSE_FROM:
30790 case OMP_CLAUSE_TO:
30791 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30792 {
30793 location_t loc
30794 = cp_lexer_peek_token (parser->lexer)->location;
30795 cp_id_kind idk = CP_ID_KIND_NONE;
30796 cp_lexer_consume_token (parser->lexer);
30797 decl = convert_from_reference (decl);
30798 decl
30799 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30800 decl, false,
30801 &idk, loc);
30802 }
30803 /* FALLTHROUGH. */
30804 case OMP_CLAUSE_DEPEND:
30805 case OMP_CLAUSE_REDUCTION:
30806 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30807 {
30808 tree low_bound = NULL_TREE, length = NULL_TREE;
30809
30810 parser->colon_corrects_to_scope_p = false;
30811 cp_lexer_consume_token (parser->lexer);
30812 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30813 low_bound = cp_parser_expression (parser);
30814 if (!colon)
30815 parser->colon_corrects_to_scope_p
30816 = saved_colon_corrects_to_scope_p;
30817 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30818 length = integer_one_node;
30819 else
30820 {
30821 /* Look for `:'. */
30822 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30823 goto skip_comma;
30824 if (!cp_lexer_next_token_is (parser->lexer,
30825 CPP_CLOSE_SQUARE))
30826 length = cp_parser_expression (parser);
30827 }
30828 /* Look for the closing `]'. */
30829 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30830 RT_CLOSE_SQUARE))
30831 goto skip_comma;
30832
30833 decl = tree_cons (low_bound, length, decl);
30834 }
30835 break;
30836 default:
30837 break;
30838 }
30839
30840 tree u = build_omp_clause (token->location, kind);
30841 OMP_CLAUSE_DECL (u) = decl;
30842 OMP_CLAUSE_CHAIN (u) = list;
30843 list = u;
30844 }
30845 else
30846 list = tree_cons (decl, NULL_TREE, list);
30847
30848 get_comma:
30849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30850 break;
30851 cp_lexer_consume_token (parser->lexer);
30852 }
30853
30854 if (colon)
30855 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30856
30857 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30858 {
30859 *colon = true;
30860 cp_parser_require (parser, CPP_COLON, RT_COLON);
30861 return list;
30862 }
30863
30864 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30865 {
30866 int ending;
30867
30868 /* Try to resync to an unnested comma. Copied from
30869 cp_parser_parenthesized_expression_list. */
30870 skip_comma:
30871 if (colon)
30872 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30873 ending = cp_parser_skip_to_closing_parenthesis (parser,
30874 /*recovering=*/true,
30875 /*or_comma=*/true,
30876 /*consume_paren=*/true);
30877 if (ending < 0)
30878 goto get_comma;
30879 }
30880
30881 return list;
30882 }
30883
30884 /* Similarly, but expect leading and trailing parenthesis. This is a very
30885 common case for omp clauses. */
30886
30887 static tree
30888 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30889 {
30890 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30891 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30892 return list;
30893 }
30894
30895 /* OpenACC 2.0:
30896 copy ( variable-list )
30897 copyin ( variable-list )
30898 copyout ( variable-list )
30899 create ( variable-list )
30900 delete ( variable-list )
30901 present ( variable-list )
30902 present_or_copy ( variable-list )
30903 pcopy ( variable-list )
30904 present_or_copyin ( variable-list )
30905 pcopyin ( variable-list )
30906 present_or_copyout ( variable-list )
30907 pcopyout ( variable-list )
30908 present_or_create ( variable-list )
30909 pcreate ( variable-list ) */
30910
30911 static tree
30912 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30913 tree list)
30914 {
30915 enum gomp_map_kind kind;
30916 switch (c_kind)
30917 {
30918 case PRAGMA_OACC_CLAUSE_COPY:
30919 kind = GOMP_MAP_FORCE_TOFROM;
30920 break;
30921 case PRAGMA_OACC_CLAUSE_COPYIN:
30922 kind = GOMP_MAP_FORCE_TO;
30923 break;
30924 case PRAGMA_OACC_CLAUSE_COPYOUT:
30925 kind = GOMP_MAP_FORCE_FROM;
30926 break;
30927 case PRAGMA_OACC_CLAUSE_CREATE:
30928 kind = GOMP_MAP_FORCE_ALLOC;
30929 break;
30930 case PRAGMA_OACC_CLAUSE_DELETE:
30931 kind = GOMP_MAP_DELETE;
30932 break;
30933 case PRAGMA_OACC_CLAUSE_DEVICE:
30934 kind = GOMP_MAP_FORCE_TO;
30935 break;
30936 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30937 kind = GOMP_MAP_DEVICE_RESIDENT;
30938 break;
30939 case PRAGMA_OACC_CLAUSE_HOST:
30940 case PRAGMA_OACC_CLAUSE_SELF:
30941 kind = GOMP_MAP_FORCE_FROM;
30942 break;
30943 case PRAGMA_OACC_CLAUSE_LINK:
30944 kind = GOMP_MAP_LINK;
30945 break;
30946 case PRAGMA_OACC_CLAUSE_PRESENT:
30947 kind = GOMP_MAP_FORCE_PRESENT;
30948 break;
30949 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30950 kind = GOMP_MAP_TOFROM;
30951 break;
30952 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30953 kind = GOMP_MAP_TO;
30954 break;
30955 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30956 kind = GOMP_MAP_FROM;
30957 break;
30958 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30959 kind = GOMP_MAP_ALLOC;
30960 break;
30961 default:
30962 gcc_unreachable ();
30963 }
30964 tree nl, c;
30965 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30966
30967 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30968 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30969
30970 return nl;
30971 }
30972
30973 /* OpenACC 2.0:
30974 deviceptr ( variable-list ) */
30975
30976 static tree
30977 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30978 {
30979 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30980 tree vars, t;
30981
30982 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30983 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30984 variable-list must only allow for pointer variables. */
30985 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30986 for (t = vars; t; t = TREE_CHAIN (t))
30987 {
30988 tree v = TREE_PURPOSE (t);
30989 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30990 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30991 OMP_CLAUSE_DECL (u) = v;
30992 OMP_CLAUSE_CHAIN (u) = list;
30993 list = u;
30994 }
30995
30996 return list;
30997 }
30998
30999 /* OpenACC 2.0:
31000 auto
31001 independent
31002 nohost
31003 seq */
31004
31005 static tree
31006 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31007 enum omp_clause_code code,
31008 tree list, location_t location)
31009 {
31010 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31011 tree c = build_omp_clause (location, code);
31012 OMP_CLAUSE_CHAIN (c) = list;
31013 return c;
31014 }
31015
31016 /* OpenACC:
31017 num_gangs ( expression )
31018 num_workers ( expression )
31019 vector_length ( expression ) */
31020
31021 static tree
31022 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31023 const char *str, tree list)
31024 {
31025 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31026
31027 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31028 return list;
31029
31030 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31031
31032 if (t == error_mark_node
31033 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31034 {
31035 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31036 /*or_comma=*/false,
31037 /*consume_paren=*/true);
31038 return list;
31039 }
31040
31041 check_no_duplicate_clause (list, code, str, loc);
31042
31043 tree c = build_omp_clause (loc, code);
31044 OMP_CLAUSE_OPERAND (c, 0) = t;
31045 OMP_CLAUSE_CHAIN (c) = list;
31046 return c;
31047 }
31048
31049 /* OpenACC:
31050
31051 gang [( gang-arg-list )]
31052 worker [( [num:] int-expr )]
31053 vector [( [length:] int-expr )]
31054
31055 where gang-arg is one of:
31056
31057 [num:] int-expr
31058 static: size-expr
31059
31060 and size-expr may be:
31061
31062 *
31063 int-expr
31064 */
31065
31066 static tree
31067 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31068 const char *str, tree list)
31069 {
31070 const char *id = "num";
31071 cp_lexer *lexer = parser->lexer;
31072 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31073 location_t loc = cp_lexer_peek_token (lexer)->location;
31074
31075 if (kind == OMP_CLAUSE_VECTOR)
31076 id = "length";
31077
31078 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31079 {
31080 cp_lexer_consume_token (lexer);
31081
31082 do
31083 {
31084 cp_token *next = cp_lexer_peek_token (lexer);
31085 int idx = 0;
31086
31087 /* Gang static argument. */
31088 if (kind == OMP_CLAUSE_GANG
31089 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31090 {
31091 cp_lexer_consume_token (lexer);
31092
31093 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31094 goto cleanup_error;
31095
31096 idx = 1;
31097 if (ops[idx] != NULL)
31098 {
31099 cp_parser_error (parser, "too many %<static%> arguments");
31100 goto cleanup_error;
31101 }
31102
31103 /* Check for the '*' argument. */
31104 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31105 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31106 || cp_lexer_nth_token_is (parser->lexer, 2,
31107 CPP_CLOSE_PAREN)))
31108 {
31109 cp_lexer_consume_token (lexer);
31110 ops[idx] = integer_minus_one_node;
31111
31112 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31113 {
31114 cp_lexer_consume_token (lexer);
31115 continue;
31116 }
31117 else break;
31118 }
31119 }
31120 /* Worker num: argument and vector length: arguments. */
31121 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31122 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31123 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31124 {
31125 cp_lexer_consume_token (lexer); /* id */
31126 cp_lexer_consume_token (lexer); /* ':' */
31127 }
31128
31129 /* Now collect the actual argument. */
31130 if (ops[idx] != NULL_TREE)
31131 {
31132 cp_parser_error (parser, "unexpected argument");
31133 goto cleanup_error;
31134 }
31135
31136 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31137 false);
31138 if (expr == error_mark_node)
31139 goto cleanup_error;
31140
31141 mark_exp_read (expr);
31142 ops[idx] = expr;
31143
31144 if (kind == OMP_CLAUSE_GANG
31145 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31146 {
31147 cp_lexer_consume_token (lexer);
31148 continue;
31149 }
31150 break;
31151 }
31152 while (1);
31153
31154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31155 goto cleanup_error;
31156 }
31157
31158 check_no_duplicate_clause (list, kind, str, loc);
31159
31160 c = build_omp_clause (loc, kind);
31161
31162 if (ops[1])
31163 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31164
31165 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31166 OMP_CLAUSE_CHAIN (c) = list;
31167
31168 return c;
31169
31170 cleanup_error:
31171 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31172 return list;
31173 }
31174
31175 /* OpenACC 2.0:
31176 tile ( size-expr-list ) */
31177
31178 static tree
31179 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31180 {
31181 tree c, expr = error_mark_node;
31182 tree tile = NULL_TREE;
31183
31184 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31185
31186 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31187 return list;
31188
31189 do
31190 {
31191 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31192 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31193 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31194 {
31195 cp_lexer_consume_token (parser->lexer);
31196 expr = integer_minus_one_node;
31197 }
31198 else
31199 expr = cp_parser_assignment_expression (parser, NULL, false, false);
31200
31201 if (expr == error_mark_node)
31202 return list;
31203
31204 tile = tree_cons (NULL_TREE, expr, tile);
31205
31206 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31207 cp_lexer_consume_token (parser->lexer);
31208 }
31209 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31210
31211 /* Consume the trailing ')'. */
31212 cp_lexer_consume_token (parser->lexer);
31213
31214 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31215 tile = nreverse (tile);
31216 OMP_CLAUSE_TILE_LIST (c) = tile;
31217 OMP_CLAUSE_CHAIN (c) = list;
31218 return c;
31219 }
31220
31221 /* OpenACC 2.0
31222 Parse wait clause or directive parameters. */
31223
31224 static tree
31225 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31226 {
31227 vec<tree, va_gc> *args;
31228 tree t, args_tree;
31229
31230 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31231 /*cast_p=*/false,
31232 /*allow_expansion_p=*/true,
31233 /*non_constant_p=*/NULL);
31234
31235 if (args == NULL || args->length () == 0)
31236 {
31237 cp_parser_error (parser, "expected integer expression before ')'");
31238 if (args != NULL)
31239 release_tree_vector (args);
31240 return list;
31241 }
31242
31243 args_tree = build_tree_list_vec (args);
31244
31245 release_tree_vector (args);
31246
31247 for (t = args_tree; t; t = TREE_CHAIN (t))
31248 {
31249 tree targ = TREE_VALUE (t);
31250
31251 if (targ != error_mark_node)
31252 {
31253 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31254 error ("%<wait%> expression must be integral");
31255 else
31256 {
31257 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31258
31259 mark_rvalue_use (targ);
31260 OMP_CLAUSE_DECL (c) = targ;
31261 OMP_CLAUSE_CHAIN (c) = list;
31262 list = c;
31263 }
31264 }
31265 }
31266
31267 return list;
31268 }
31269
31270 /* OpenACC:
31271 wait ( int-expr-list ) */
31272
31273 static tree
31274 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31275 {
31276 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31277
31278 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31279 return list;
31280
31281 list = cp_parser_oacc_wait_list (parser, location, list);
31282
31283 return list;
31284 }
31285
31286 /* OpenMP 3.0:
31287 collapse ( constant-expression ) */
31288
31289 static tree
31290 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31291 {
31292 tree c, num;
31293 location_t loc;
31294 HOST_WIDE_INT n;
31295
31296 loc = cp_lexer_peek_token (parser->lexer)->location;
31297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31298 return list;
31299
31300 num = cp_parser_constant_expression (parser);
31301
31302 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31303 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31304 /*or_comma=*/false,
31305 /*consume_paren=*/true);
31306
31307 if (num == error_mark_node)
31308 return list;
31309 num = fold_non_dependent_expr (num);
31310 if (!tree_fits_shwi_p (num)
31311 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31312 || (n = tree_to_shwi (num)) <= 0
31313 || (int) n != n)
31314 {
31315 error_at (loc, "collapse argument needs positive constant integer expression");
31316 return list;
31317 }
31318
31319 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31320 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31321 OMP_CLAUSE_CHAIN (c) = list;
31322 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31323
31324 return c;
31325 }
31326
31327 /* OpenMP 2.5:
31328 default ( shared | none )
31329
31330 OpenACC 2.0
31331 default (none) */
31332
31333 static tree
31334 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31335 location_t location, bool is_oacc)
31336 {
31337 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31338 tree c;
31339
31340 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31341 return list;
31342 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31343 {
31344 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31345 const char *p = IDENTIFIER_POINTER (id);
31346
31347 switch (p[0])
31348 {
31349 case 'n':
31350 if (strcmp ("none", p) != 0)
31351 goto invalid_kind;
31352 kind = OMP_CLAUSE_DEFAULT_NONE;
31353 break;
31354
31355 case 's':
31356 if (strcmp ("shared", p) != 0 || is_oacc)
31357 goto invalid_kind;
31358 kind = OMP_CLAUSE_DEFAULT_SHARED;
31359 break;
31360
31361 default:
31362 goto invalid_kind;
31363 }
31364
31365 cp_lexer_consume_token (parser->lexer);
31366 }
31367 else
31368 {
31369 invalid_kind:
31370 if (is_oacc)
31371 cp_parser_error (parser, "expected %<none%>");
31372 else
31373 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31374 }
31375
31376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31378 /*or_comma=*/false,
31379 /*consume_paren=*/true);
31380
31381 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31382 return list;
31383
31384 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31385 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31386 OMP_CLAUSE_CHAIN (c) = list;
31387 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31388
31389 return c;
31390 }
31391
31392 /* OpenMP 3.1:
31393 final ( expression ) */
31394
31395 static tree
31396 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31397 {
31398 tree t, c;
31399
31400 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31401 return list;
31402
31403 t = cp_parser_condition (parser);
31404
31405 if (t == error_mark_node
31406 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31407 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31408 /*or_comma=*/false,
31409 /*consume_paren=*/true);
31410
31411 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31412
31413 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31414 OMP_CLAUSE_FINAL_EXPR (c) = t;
31415 OMP_CLAUSE_CHAIN (c) = list;
31416
31417 return c;
31418 }
31419
31420 /* OpenMP 2.5:
31421 if ( expression )
31422
31423 OpenMP 4.5:
31424 if ( directive-name-modifier : expression )
31425
31426 directive-name-modifier:
31427 parallel | task | taskloop | target data | target | target update
31428 | target enter data | target exit data */
31429
31430 static tree
31431 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31432 bool is_omp)
31433 {
31434 tree t, c;
31435 enum tree_code if_modifier = ERROR_MARK;
31436
31437 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31438 return list;
31439
31440 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31441 {
31442 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31443 const char *p = IDENTIFIER_POINTER (id);
31444 int n = 2;
31445
31446 if (strcmp ("parallel", p) == 0)
31447 if_modifier = OMP_PARALLEL;
31448 else if (strcmp ("task", p) == 0)
31449 if_modifier = OMP_TASK;
31450 else if (strcmp ("taskloop", p) == 0)
31451 if_modifier = OMP_TASKLOOP;
31452 else if (strcmp ("target", p) == 0)
31453 {
31454 if_modifier = OMP_TARGET;
31455 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31456 {
31457 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31458 p = IDENTIFIER_POINTER (id);
31459 if (strcmp ("data", p) == 0)
31460 if_modifier = OMP_TARGET_DATA;
31461 else if (strcmp ("update", p) == 0)
31462 if_modifier = OMP_TARGET_UPDATE;
31463 else if (strcmp ("enter", p) == 0)
31464 if_modifier = OMP_TARGET_ENTER_DATA;
31465 else if (strcmp ("exit", p) == 0)
31466 if_modifier = OMP_TARGET_EXIT_DATA;
31467 if (if_modifier != OMP_TARGET)
31468 n = 3;
31469 else
31470 {
31471 location_t loc
31472 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31473 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31474 "or %<exit%>");
31475 if_modifier = ERROR_MARK;
31476 }
31477 if (if_modifier == OMP_TARGET_ENTER_DATA
31478 || if_modifier == OMP_TARGET_EXIT_DATA)
31479 {
31480 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31481 {
31482 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31483 p = IDENTIFIER_POINTER (id);
31484 if (strcmp ("data", p) == 0)
31485 n = 4;
31486 }
31487 if (n != 4)
31488 {
31489 location_t loc
31490 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31491 error_at (loc, "expected %<data%>");
31492 if_modifier = ERROR_MARK;
31493 }
31494 }
31495 }
31496 }
31497 if (if_modifier != ERROR_MARK)
31498 {
31499 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31500 {
31501 while (n-- > 0)
31502 cp_lexer_consume_token (parser->lexer);
31503 }
31504 else
31505 {
31506 if (n > 2)
31507 {
31508 location_t loc
31509 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31510 error_at (loc, "expected %<:%>");
31511 }
31512 if_modifier = ERROR_MARK;
31513 }
31514 }
31515 }
31516
31517 t = cp_parser_condition (parser);
31518
31519 if (t == error_mark_node
31520 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31521 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31522 /*or_comma=*/false,
31523 /*consume_paren=*/true);
31524
31525 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31526 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31527 {
31528 if (if_modifier != ERROR_MARK
31529 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31530 {
31531 const char *p = NULL;
31532 switch (if_modifier)
31533 {
31534 case OMP_PARALLEL: p = "parallel"; break;
31535 case OMP_TASK: p = "task"; break;
31536 case OMP_TASKLOOP: p = "taskloop"; break;
31537 case OMP_TARGET_DATA: p = "target data"; break;
31538 case OMP_TARGET: p = "target"; break;
31539 case OMP_TARGET_UPDATE: p = "target update"; break;
31540 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31541 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31542 default: gcc_unreachable ();
31543 }
31544 error_at (location, "too many %<if%> clauses with %qs modifier",
31545 p);
31546 return list;
31547 }
31548 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31549 {
31550 if (!is_omp)
31551 error_at (location, "too many %<if%> clauses");
31552 else
31553 error_at (location, "too many %<if%> clauses without modifier");
31554 return list;
31555 }
31556 else if (if_modifier == ERROR_MARK
31557 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31558 {
31559 error_at (location, "if any %<if%> clause has modifier, then all "
31560 "%<if%> clauses have to use modifier");
31561 return list;
31562 }
31563 }
31564
31565 c = build_omp_clause (location, OMP_CLAUSE_IF);
31566 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31567 OMP_CLAUSE_IF_EXPR (c) = t;
31568 OMP_CLAUSE_CHAIN (c) = list;
31569
31570 return c;
31571 }
31572
31573 /* OpenMP 3.1:
31574 mergeable */
31575
31576 static tree
31577 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31578 tree list, location_t location)
31579 {
31580 tree c;
31581
31582 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31583 location);
31584
31585 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31586 OMP_CLAUSE_CHAIN (c) = list;
31587 return c;
31588 }
31589
31590 /* OpenMP 2.5:
31591 nowait */
31592
31593 static tree
31594 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31595 tree list, location_t location)
31596 {
31597 tree c;
31598
31599 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31600
31601 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31602 OMP_CLAUSE_CHAIN (c) = list;
31603 return c;
31604 }
31605
31606 /* OpenMP 2.5:
31607 num_threads ( expression ) */
31608
31609 static tree
31610 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31611 location_t location)
31612 {
31613 tree t, c;
31614
31615 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31616 return list;
31617
31618 t = cp_parser_expression (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 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31627 "num_threads", location);
31628
31629 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31630 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31631 OMP_CLAUSE_CHAIN (c) = list;
31632
31633 return c;
31634 }
31635
31636 /* OpenMP 4.5:
31637 num_tasks ( expression ) */
31638
31639 static tree
31640 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31641 location_t location)
31642 {
31643 tree t, c;
31644
31645 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31646 return list;
31647
31648 t = cp_parser_expression (parser);
31649
31650 if (t == error_mark_node
31651 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31652 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31653 /*or_comma=*/false,
31654 /*consume_paren=*/true);
31655
31656 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31657 "num_tasks", location);
31658
31659 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31660 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31661 OMP_CLAUSE_CHAIN (c) = list;
31662
31663 return c;
31664 }
31665
31666 /* OpenMP 4.5:
31667 grainsize ( expression ) */
31668
31669 static tree
31670 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31671 location_t location)
31672 {
31673 tree t, c;
31674
31675 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31676 return list;
31677
31678 t = cp_parser_expression (parser);
31679
31680 if (t == error_mark_node
31681 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31682 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31683 /*or_comma=*/false,
31684 /*consume_paren=*/true);
31685
31686 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31687 "grainsize", location);
31688
31689 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31690 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31691 OMP_CLAUSE_CHAIN (c) = list;
31692
31693 return c;
31694 }
31695
31696 /* OpenMP 4.5:
31697 priority ( expression ) */
31698
31699 static tree
31700 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31701 location_t location)
31702 {
31703 tree t, c;
31704
31705 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31706 return list;
31707
31708 t = cp_parser_expression (parser);
31709
31710 if (t == error_mark_node
31711 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31712 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31713 /*or_comma=*/false,
31714 /*consume_paren=*/true);
31715
31716 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31717 "priority", location);
31718
31719 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31720 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31721 OMP_CLAUSE_CHAIN (c) = list;
31722
31723 return c;
31724 }
31725
31726 /* OpenMP 4.5:
31727 hint ( expression ) */
31728
31729 static tree
31730 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31731 location_t location)
31732 {
31733 tree t, c;
31734
31735 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31736 return list;
31737
31738 t = cp_parser_expression (parser);
31739
31740 if (t == error_mark_node
31741 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31742 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31743 /*or_comma=*/false,
31744 /*consume_paren=*/true);
31745
31746 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31747
31748 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31749 OMP_CLAUSE_HINT_EXPR (c) = t;
31750 OMP_CLAUSE_CHAIN (c) = list;
31751
31752 return c;
31753 }
31754
31755 /* OpenMP 4.5:
31756 defaultmap ( tofrom : scalar ) */
31757
31758 static tree
31759 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31760 location_t location)
31761 {
31762 tree c, id;
31763 const char *p;
31764
31765 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31766 return list;
31767
31768 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31769 {
31770 cp_parser_error (parser, "expected %<tofrom%>");
31771 goto out_err;
31772 }
31773 id = cp_lexer_peek_token (parser->lexer)->u.value;
31774 p = IDENTIFIER_POINTER (id);
31775 if (strcmp (p, "tofrom") != 0)
31776 {
31777 cp_parser_error (parser, "expected %<tofrom%>");
31778 goto out_err;
31779 }
31780 cp_lexer_consume_token (parser->lexer);
31781 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31782 goto out_err;
31783
31784 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31785 {
31786 cp_parser_error (parser, "expected %<scalar%>");
31787 goto out_err;
31788 }
31789 id = cp_lexer_peek_token (parser->lexer)->u.value;
31790 p = IDENTIFIER_POINTER (id);
31791 if (strcmp (p, "scalar") != 0)
31792 {
31793 cp_parser_error (parser, "expected %<scalar%>");
31794 goto out_err;
31795 }
31796 cp_lexer_consume_token (parser->lexer);
31797 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31798 goto out_err;
31799
31800 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31801 location);
31802
31803 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31804 OMP_CLAUSE_CHAIN (c) = list;
31805 return c;
31806
31807 out_err:
31808 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31809 /*or_comma=*/false,
31810 /*consume_paren=*/true);
31811 return list;
31812 }
31813
31814 /* OpenMP 2.5:
31815 ordered
31816
31817 OpenMP 4.5:
31818 ordered ( constant-expression ) */
31819
31820 static tree
31821 cp_parser_omp_clause_ordered (cp_parser *parser,
31822 tree list, location_t location)
31823 {
31824 tree c, num = NULL_TREE;
31825 HOST_WIDE_INT n;
31826
31827 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31828 "ordered", location);
31829
31830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31831 {
31832 cp_lexer_consume_token (parser->lexer);
31833
31834 num = cp_parser_constant_expression (parser);
31835
31836 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31837 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31838 /*or_comma=*/false,
31839 /*consume_paren=*/true);
31840
31841 if (num == error_mark_node)
31842 return list;
31843 num = fold_non_dependent_expr (num);
31844 if (!tree_fits_shwi_p (num)
31845 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31846 || (n = tree_to_shwi (num)) <= 0
31847 || (int) n != n)
31848 {
31849 error_at (location,
31850 "ordered argument needs positive constant integer "
31851 "expression");
31852 return list;
31853 }
31854 }
31855
31856 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31857 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31858 OMP_CLAUSE_CHAIN (c) = list;
31859 return c;
31860 }
31861
31862 /* OpenMP 2.5:
31863 reduction ( reduction-operator : variable-list )
31864
31865 reduction-operator:
31866 One of: + * - & ^ | && ||
31867
31868 OpenMP 3.1:
31869
31870 reduction-operator:
31871 One of: + * - & ^ | && || min max
31872
31873 OpenMP 4.0:
31874
31875 reduction-operator:
31876 One of: + * - & ^ | && ||
31877 id-expression */
31878
31879 static tree
31880 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31881 {
31882 enum tree_code code = ERROR_MARK;
31883 tree nlist, c, id = NULL_TREE;
31884
31885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31886 return list;
31887
31888 switch (cp_lexer_peek_token (parser->lexer)->type)
31889 {
31890 case CPP_PLUS: code = PLUS_EXPR; break;
31891 case CPP_MULT: code = MULT_EXPR; break;
31892 case CPP_MINUS: code = MINUS_EXPR; break;
31893 case CPP_AND: code = BIT_AND_EXPR; break;
31894 case CPP_XOR: code = BIT_XOR_EXPR; break;
31895 case CPP_OR: code = BIT_IOR_EXPR; break;
31896 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31897 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31898 default: break;
31899 }
31900
31901 if (code != ERROR_MARK)
31902 cp_lexer_consume_token (parser->lexer);
31903 else
31904 {
31905 bool saved_colon_corrects_to_scope_p;
31906 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31907 parser->colon_corrects_to_scope_p = false;
31908 id = cp_parser_id_expression (parser, /*template_p=*/false,
31909 /*check_dependency_p=*/true,
31910 /*template_p=*/NULL,
31911 /*declarator_p=*/false,
31912 /*optional_p=*/false);
31913 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31914 if (identifier_p (id))
31915 {
31916 const char *p = IDENTIFIER_POINTER (id);
31917
31918 if (strcmp (p, "min") == 0)
31919 code = MIN_EXPR;
31920 else if (strcmp (p, "max") == 0)
31921 code = MAX_EXPR;
31922 else if (id == ansi_opname (PLUS_EXPR))
31923 code = PLUS_EXPR;
31924 else if (id == ansi_opname (MULT_EXPR))
31925 code = MULT_EXPR;
31926 else if (id == ansi_opname (MINUS_EXPR))
31927 code = MINUS_EXPR;
31928 else if (id == ansi_opname (BIT_AND_EXPR))
31929 code = BIT_AND_EXPR;
31930 else if (id == ansi_opname (BIT_IOR_EXPR))
31931 code = BIT_IOR_EXPR;
31932 else if (id == ansi_opname (BIT_XOR_EXPR))
31933 code = BIT_XOR_EXPR;
31934 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31935 code = TRUTH_ANDIF_EXPR;
31936 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31937 code = TRUTH_ORIF_EXPR;
31938 id = omp_reduction_id (code, id, NULL_TREE);
31939 tree scope = parser->scope;
31940 if (scope)
31941 id = build_qualified_name (NULL_TREE, scope, id, false);
31942 parser->scope = NULL_TREE;
31943 parser->qualifying_scope = NULL_TREE;
31944 parser->object_scope = NULL_TREE;
31945 }
31946 else
31947 {
31948 error ("invalid reduction-identifier");
31949 resync_fail:
31950 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31951 /*or_comma=*/false,
31952 /*consume_paren=*/true);
31953 return list;
31954 }
31955 }
31956
31957 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31958 goto resync_fail;
31959
31960 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31961 NULL);
31962 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31963 {
31964 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31965 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31966 }
31967
31968 return nlist;
31969 }
31970
31971 /* OpenMP 2.5:
31972 schedule ( schedule-kind )
31973 schedule ( schedule-kind , expression )
31974
31975 schedule-kind:
31976 static | dynamic | guided | runtime | auto
31977
31978 OpenMP 4.5:
31979 schedule ( schedule-modifier : schedule-kind )
31980 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31981
31982 schedule-modifier:
31983 simd
31984 monotonic
31985 nonmonotonic */
31986
31987 static tree
31988 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31989 {
31990 tree c, t;
31991 int modifiers = 0, nmodifiers = 0;
31992
31993 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31994 return list;
31995
31996 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31997
31998 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31999 {
32000 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32001 const char *p = IDENTIFIER_POINTER (id);
32002 if (strcmp ("simd", p) == 0)
32003 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32004 else if (strcmp ("monotonic", p) == 0)
32005 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32006 else if (strcmp ("nonmonotonic", p) == 0)
32007 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32008 else
32009 break;
32010 cp_lexer_consume_token (parser->lexer);
32011 if (nmodifiers++ == 0
32012 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32013 cp_lexer_consume_token (parser->lexer);
32014 else
32015 {
32016 cp_parser_require (parser, CPP_COLON, RT_COLON);
32017 break;
32018 }
32019 }
32020
32021 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32022 {
32023 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32024 const char *p = IDENTIFIER_POINTER (id);
32025
32026 switch (p[0])
32027 {
32028 case 'd':
32029 if (strcmp ("dynamic", p) != 0)
32030 goto invalid_kind;
32031 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32032 break;
32033
32034 case 'g':
32035 if (strcmp ("guided", p) != 0)
32036 goto invalid_kind;
32037 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32038 break;
32039
32040 case 'r':
32041 if (strcmp ("runtime", p) != 0)
32042 goto invalid_kind;
32043 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32044 break;
32045
32046 default:
32047 goto invalid_kind;
32048 }
32049 }
32050 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32051 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32052 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32053 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32054 else
32055 goto invalid_kind;
32056 cp_lexer_consume_token (parser->lexer);
32057
32058 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32059 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32060 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32061 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32062 {
32063 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32064 "specified");
32065 modifiers = 0;
32066 }
32067
32068 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32069 {
32070 cp_token *token;
32071 cp_lexer_consume_token (parser->lexer);
32072
32073 token = cp_lexer_peek_token (parser->lexer);
32074 t = cp_parser_assignment_expression (parser);
32075
32076 if (t == error_mark_node)
32077 goto resync_fail;
32078 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32079 error_at (token->location, "schedule %<runtime%> does not take "
32080 "a %<chunk_size%> parameter");
32081 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32082 error_at (token->location, "schedule %<auto%> does not take "
32083 "a %<chunk_size%> parameter");
32084 else
32085 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32086
32087 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32088 goto resync_fail;
32089 }
32090 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32091 goto resync_fail;
32092
32093 OMP_CLAUSE_SCHEDULE_KIND (c)
32094 = (enum omp_clause_schedule_kind)
32095 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32096
32097 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32098 OMP_CLAUSE_CHAIN (c) = list;
32099 return c;
32100
32101 invalid_kind:
32102 cp_parser_error (parser, "invalid schedule kind");
32103 resync_fail:
32104 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32105 /*or_comma=*/false,
32106 /*consume_paren=*/true);
32107 return list;
32108 }
32109
32110 /* OpenMP 3.0:
32111 untied */
32112
32113 static tree
32114 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32115 tree list, location_t location)
32116 {
32117 tree c;
32118
32119 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32120
32121 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32122 OMP_CLAUSE_CHAIN (c) = list;
32123 return c;
32124 }
32125
32126 /* OpenMP 4.0:
32127 inbranch
32128 notinbranch */
32129
32130 static tree
32131 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32132 tree list, location_t location)
32133 {
32134 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32135 tree c = build_omp_clause (location, code);
32136 OMP_CLAUSE_CHAIN (c) = list;
32137 return c;
32138 }
32139
32140 /* OpenMP 4.0:
32141 parallel
32142 for
32143 sections
32144 taskgroup */
32145
32146 static tree
32147 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32148 enum omp_clause_code code,
32149 tree list, location_t location)
32150 {
32151 tree c = build_omp_clause (location, code);
32152 OMP_CLAUSE_CHAIN (c) = list;
32153 return c;
32154 }
32155
32156 /* OpenMP 4.5:
32157 nogroup */
32158
32159 static tree
32160 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32161 tree list, location_t location)
32162 {
32163 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32164 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32165 OMP_CLAUSE_CHAIN (c) = list;
32166 return c;
32167 }
32168
32169 /* OpenMP 4.5:
32170 simd
32171 threads */
32172
32173 static tree
32174 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32175 enum omp_clause_code code,
32176 tree list, location_t location)
32177 {
32178 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32179 tree c = build_omp_clause (location, code);
32180 OMP_CLAUSE_CHAIN (c) = list;
32181 return c;
32182 }
32183
32184 /* OpenMP 4.0:
32185 num_teams ( expression ) */
32186
32187 static tree
32188 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32189 location_t location)
32190 {
32191 tree t, c;
32192
32193 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32194 return list;
32195
32196 t = cp_parser_expression (parser);
32197
32198 if (t == error_mark_node
32199 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32200 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32201 /*or_comma=*/false,
32202 /*consume_paren=*/true);
32203
32204 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32205 "num_teams", location);
32206
32207 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32208 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32209 OMP_CLAUSE_CHAIN (c) = list;
32210
32211 return c;
32212 }
32213
32214 /* OpenMP 4.0:
32215 thread_limit ( expression ) */
32216
32217 static tree
32218 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32219 location_t location)
32220 {
32221 tree t, c;
32222
32223 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32224 return list;
32225
32226 t = cp_parser_expression (parser);
32227
32228 if (t == error_mark_node
32229 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32230 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32231 /*or_comma=*/false,
32232 /*consume_paren=*/true);
32233
32234 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32235 "thread_limit", location);
32236
32237 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32238 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32239 OMP_CLAUSE_CHAIN (c) = list;
32240
32241 return c;
32242 }
32243
32244 /* OpenMP 4.0:
32245 aligned ( variable-list )
32246 aligned ( variable-list : constant-expression ) */
32247
32248 static tree
32249 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32250 {
32251 tree nlist, c, alignment = NULL_TREE;
32252 bool colon;
32253
32254 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32255 return list;
32256
32257 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32258 &colon);
32259
32260 if (colon)
32261 {
32262 alignment = cp_parser_constant_expression (parser);
32263
32264 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32265 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32266 /*or_comma=*/false,
32267 /*consume_paren=*/true);
32268
32269 if (alignment == error_mark_node)
32270 alignment = NULL_TREE;
32271 }
32272
32273 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32274 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32275
32276 return nlist;
32277 }
32278
32279 /* OpenMP 4.0:
32280 linear ( variable-list )
32281 linear ( variable-list : expression )
32282
32283 OpenMP 4.5:
32284 linear ( modifier ( variable-list ) )
32285 linear ( modifier ( variable-list ) : expression ) */
32286
32287 static tree
32288 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32289 bool is_cilk_simd_fn, bool declare_simd)
32290 {
32291 tree nlist, c, step = integer_one_node;
32292 bool colon;
32293 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32294
32295 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32296 return list;
32297
32298 if (!is_cilk_simd_fn
32299 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32300 {
32301 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32302 const char *p = IDENTIFIER_POINTER (id);
32303
32304 if (strcmp ("ref", p) == 0)
32305 kind = OMP_CLAUSE_LINEAR_REF;
32306 else if (strcmp ("val", p) == 0)
32307 kind = OMP_CLAUSE_LINEAR_VAL;
32308 else if (strcmp ("uval", p) == 0)
32309 kind = OMP_CLAUSE_LINEAR_UVAL;
32310 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32311 cp_lexer_consume_token (parser->lexer);
32312 else
32313 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32314 }
32315
32316 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32317 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32318 &colon);
32319 else
32320 {
32321 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32322 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32323 if (colon)
32324 cp_parser_require (parser, CPP_COLON, RT_COLON);
32325 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32326 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32327 /*or_comma=*/false,
32328 /*consume_paren=*/true);
32329 }
32330
32331 if (colon)
32332 {
32333 step = NULL_TREE;
32334 if (declare_simd
32335 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32336 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32337 {
32338 cp_token *token = cp_lexer_peek_token (parser->lexer);
32339 cp_parser_parse_tentatively (parser);
32340 step = cp_parser_id_expression (parser, /*template_p=*/false,
32341 /*check_dependency_p=*/true,
32342 /*template_p=*/NULL,
32343 /*declarator_p=*/false,
32344 /*optional_p=*/false);
32345 if (step != error_mark_node)
32346 step = cp_parser_lookup_name_simple (parser, step, token->location);
32347 if (step == error_mark_node)
32348 {
32349 step = NULL_TREE;
32350 cp_parser_abort_tentative_parse (parser);
32351 }
32352 else if (!cp_parser_parse_definitely (parser))
32353 step = NULL_TREE;
32354 }
32355 if (!step)
32356 step = cp_parser_expression (parser);
32357
32358 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32359 {
32360 sorry ("using parameters for %<linear%> step is not supported yet");
32361 step = integer_one_node;
32362 }
32363 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32364 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32365 /*or_comma=*/false,
32366 /*consume_paren=*/true);
32367
32368 if (step == error_mark_node)
32369 return list;
32370 }
32371
32372 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32373 {
32374 OMP_CLAUSE_LINEAR_STEP (c) = step;
32375 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32376 }
32377
32378 return nlist;
32379 }
32380
32381 /* OpenMP 4.0:
32382 safelen ( constant-expression ) */
32383
32384 static tree
32385 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32386 location_t location)
32387 {
32388 tree t, c;
32389
32390 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32391 return list;
32392
32393 t = cp_parser_constant_expression (parser);
32394
32395 if (t == error_mark_node
32396 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32397 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32398 /*or_comma=*/false,
32399 /*consume_paren=*/true);
32400
32401 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32402
32403 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32404 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32405 OMP_CLAUSE_CHAIN (c) = list;
32406
32407 return c;
32408 }
32409
32410 /* OpenMP 4.0:
32411 simdlen ( constant-expression ) */
32412
32413 static tree
32414 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32415 location_t location)
32416 {
32417 tree t, c;
32418
32419 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32420 return list;
32421
32422 t = cp_parser_constant_expression (parser);
32423
32424 if (t == error_mark_node
32425 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32426 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32427 /*or_comma=*/false,
32428 /*consume_paren=*/true);
32429
32430 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32431
32432 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32433 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32434 OMP_CLAUSE_CHAIN (c) = list;
32435
32436 return c;
32437 }
32438
32439 /* OpenMP 4.5:
32440 vec:
32441 identifier [+/- integer]
32442 vec , identifier [+/- integer]
32443 */
32444
32445 static tree
32446 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32447 tree list)
32448 {
32449 tree vec = NULL;
32450
32451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32452 {
32453 cp_parser_error (parser, "expected identifier");
32454 return list;
32455 }
32456
32457 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32458 {
32459 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32460 tree t, identifier = cp_parser_identifier (parser);
32461 tree addend = NULL;
32462
32463 if (identifier == error_mark_node)
32464 t = error_mark_node;
32465 else
32466 {
32467 t = cp_parser_lookup_name_simple
32468 (parser, identifier,
32469 cp_lexer_peek_token (parser->lexer)->location);
32470 if (t == error_mark_node)
32471 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32472 id_loc);
32473 }
32474
32475 bool neg = false;
32476 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32477 neg = true;
32478 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32479 {
32480 addend = integer_zero_node;
32481 goto add_to_vector;
32482 }
32483 cp_lexer_consume_token (parser->lexer);
32484
32485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32486 {
32487 cp_parser_error (parser, "expected integer");
32488 return list;
32489 }
32490
32491 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32492 if (TREE_CODE (addend) != INTEGER_CST)
32493 {
32494 cp_parser_error (parser, "expected integer");
32495 return list;
32496 }
32497 cp_lexer_consume_token (parser->lexer);
32498
32499 add_to_vector:
32500 if (t != error_mark_node)
32501 {
32502 vec = tree_cons (addend, t, vec);
32503 if (neg)
32504 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32505 }
32506
32507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32508 break;
32509
32510 cp_lexer_consume_token (parser->lexer);
32511 }
32512
32513 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32514 {
32515 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32516 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32517 OMP_CLAUSE_DECL (u) = nreverse (vec);
32518 OMP_CLAUSE_CHAIN (u) = list;
32519 return u;
32520 }
32521 return list;
32522 }
32523
32524 /* OpenMP 4.0:
32525 depend ( depend-kind : variable-list )
32526
32527 depend-kind:
32528 in | out | inout
32529
32530 OpenMP 4.5:
32531 depend ( source )
32532
32533 depend ( sink : vec ) */
32534
32535 static tree
32536 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32537 {
32538 tree nlist, c;
32539 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32540
32541 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32542 return list;
32543
32544 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32545 {
32546 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32547 const char *p = IDENTIFIER_POINTER (id);
32548
32549 if (strcmp ("in", p) == 0)
32550 kind = OMP_CLAUSE_DEPEND_IN;
32551 else if (strcmp ("inout", p) == 0)
32552 kind = OMP_CLAUSE_DEPEND_INOUT;
32553 else if (strcmp ("out", p) == 0)
32554 kind = OMP_CLAUSE_DEPEND_OUT;
32555 else if (strcmp ("source", p) == 0)
32556 kind = OMP_CLAUSE_DEPEND_SOURCE;
32557 else if (strcmp ("sink", p) == 0)
32558 kind = OMP_CLAUSE_DEPEND_SINK;
32559 else
32560 goto invalid_kind;
32561 }
32562 else
32563 goto invalid_kind;
32564
32565 cp_lexer_consume_token (parser->lexer);
32566
32567 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32568 {
32569 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32570 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32571 OMP_CLAUSE_DECL (c) = NULL_TREE;
32572 OMP_CLAUSE_CHAIN (c) = list;
32573 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32574 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32575 /*or_comma=*/false,
32576 /*consume_paren=*/true);
32577 return c;
32578 }
32579
32580 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32581 goto resync_fail;
32582
32583 if (kind == OMP_CLAUSE_DEPEND_SINK)
32584 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32585 else
32586 {
32587 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32588 list, NULL);
32589
32590 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32591 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32592 }
32593 return nlist;
32594
32595 invalid_kind:
32596 cp_parser_error (parser, "invalid depend kind");
32597 resync_fail:
32598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32599 /*or_comma=*/false,
32600 /*consume_paren=*/true);
32601 return list;
32602 }
32603
32604 /* OpenMP 4.0:
32605 map ( map-kind : variable-list )
32606 map ( variable-list )
32607
32608 map-kind:
32609 alloc | to | from | tofrom
32610
32611 OpenMP 4.5:
32612 map-kind:
32613 alloc | to | from | tofrom | release | delete
32614
32615 map ( always [,] map-kind: variable-list ) */
32616
32617 static tree
32618 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32619 {
32620 tree nlist, c;
32621 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32622 bool always = false;
32623
32624 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32625 return list;
32626
32627 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32628 {
32629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32630 const char *p = IDENTIFIER_POINTER (id);
32631
32632 if (strcmp ("always", p) == 0)
32633 {
32634 int nth = 2;
32635 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32636 nth++;
32637 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32638 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32639 == RID_DELETE))
32640 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32641 == CPP_COLON))
32642 {
32643 always = true;
32644 cp_lexer_consume_token (parser->lexer);
32645 if (nth == 3)
32646 cp_lexer_consume_token (parser->lexer);
32647 }
32648 }
32649 }
32650
32651 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32652 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32653 {
32654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32655 const char *p = IDENTIFIER_POINTER (id);
32656
32657 if (strcmp ("alloc", p) == 0)
32658 kind = GOMP_MAP_ALLOC;
32659 else if (strcmp ("to", p) == 0)
32660 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32661 else if (strcmp ("from", p) == 0)
32662 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32663 else if (strcmp ("tofrom", p) == 0)
32664 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32665 else if (strcmp ("release", p) == 0)
32666 kind = GOMP_MAP_RELEASE;
32667 else
32668 {
32669 cp_parser_error (parser, "invalid map kind");
32670 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32671 /*or_comma=*/false,
32672 /*consume_paren=*/true);
32673 return list;
32674 }
32675 cp_lexer_consume_token (parser->lexer);
32676 cp_lexer_consume_token (parser->lexer);
32677 }
32678 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32679 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32680 {
32681 kind = GOMP_MAP_DELETE;
32682 cp_lexer_consume_token (parser->lexer);
32683 cp_lexer_consume_token (parser->lexer);
32684 }
32685
32686 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32687 NULL);
32688
32689 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32690 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32691
32692 return nlist;
32693 }
32694
32695 /* OpenMP 4.0:
32696 device ( expression ) */
32697
32698 static tree
32699 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32700 location_t location)
32701 {
32702 tree t, c;
32703
32704 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32705 return list;
32706
32707 t = cp_parser_expression (parser);
32708
32709 if (t == error_mark_node
32710 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32712 /*or_comma=*/false,
32713 /*consume_paren=*/true);
32714
32715 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32716 "device", location);
32717
32718 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32719 OMP_CLAUSE_DEVICE_ID (c) = t;
32720 OMP_CLAUSE_CHAIN (c) = list;
32721
32722 return c;
32723 }
32724
32725 /* OpenMP 4.0:
32726 dist_schedule ( static )
32727 dist_schedule ( static , expression ) */
32728
32729 static tree
32730 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32731 location_t location)
32732 {
32733 tree c, t;
32734
32735 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32736 return list;
32737
32738 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32739
32740 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32741 goto invalid_kind;
32742 cp_lexer_consume_token (parser->lexer);
32743
32744 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32745 {
32746 cp_lexer_consume_token (parser->lexer);
32747
32748 t = cp_parser_assignment_expression (parser);
32749
32750 if (t == error_mark_node)
32751 goto resync_fail;
32752 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32753
32754 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32755 goto resync_fail;
32756 }
32757 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32758 goto resync_fail;
32759
32760 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32761 location);
32762 OMP_CLAUSE_CHAIN (c) = list;
32763 return c;
32764
32765 invalid_kind:
32766 cp_parser_error (parser, "invalid dist_schedule kind");
32767 resync_fail:
32768 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32769 /*or_comma=*/false,
32770 /*consume_paren=*/true);
32771 return list;
32772 }
32773
32774 /* OpenMP 4.0:
32775 proc_bind ( proc-bind-kind )
32776
32777 proc-bind-kind:
32778 master | close | spread */
32779
32780 static tree
32781 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32782 location_t location)
32783 {
32784 tree c;
32785 enum omp_clause_proc_bind_kind kind;
32786
32787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32788 return list;
32789
32790 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32791 {
32792 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32793 const char *p = IDENTIFIER_POINTER (id);
32794
32795 if (strcmp ("master", p) == 0)
32796 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32797 else if (strcmp ("close", p) == 0)
32798 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32799 else if (strcmp ("spread", p) == 0)
32800 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32801 else
32802 goto invalid_kind;
32803 }
32804 else
32805 goto invalid_kind;
32806
32807 cp_lexer_consume_token (parser->lexer);
32808 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32809 goto resync_fail;
32810
32811 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32812 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32813 location);
32814 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32815 OMP_CLAUSE_CHAIN (c) = list;
32816 return c;
32817
32818 invalid_kind:
32819 cp_parser_error (parser, "invalid depend kind");
32820 resync_fail:
32821 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32822 /*or_comma=*/false,
32823 /*consume_paren=*/true);
32824 return list;
32825 }
32826
32827 /* OpenACC:
32828 async [( int-expr )] */
32829
32830 static tree
32831 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32832 {
32833 tree c, t;
32834 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32835
32836 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32837
32838 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32839 {
32840 cp_lexer_consume_token (parser->lexer);
32841
32842 t = cp_parser_expression (parser);
32843 if (t == error_mark_node
32844 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32845 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32846 /*or_comma=*/false,
32847 /*consume_paren=*/true);
32848 }
32849
32850 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32851
32852 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32853 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32854 OMP_CLAUSE_CHAIN (c) = list;
32855 list = c;
32856
32857 return list;
32858 }
32859
32860 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32861 is a bitmask in MASK. Return the list of clauses found. */
32862
32863 static tree
32864 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32865 const char *where, cp_token *pragma_tok,
32866 bool finish_p = true)
32867 {
32868 tree clauses = NULL;
32869 bool first = true;
32870
32871 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32872 {
32873 location_t here;
32874 pragma_omp_clause c_kind;
32875 omp_clause_code code;
32876 const char *c_name;
32877 tree prev = clauses;
32878
32879 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32880 cp_lexer_consume_token (parser->lexer);
32881
32882 here = cp_lexer_peek_token (parser->lexer)->location;
32883 c_kind = cp_parser_omp_clause_name (parser);
32884
32885 switch (c_kind)
32886 {
32887 case PRAGMA_OACC_CLAUSE_ASYNC:
32888 clauses = cp_parser_oacc_clause_async (parser, clauses);
32889 c_name = "async";
32890 break;
32891 case PRAGMA_OACC_CLAUSE_AUTO:
32892 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32893 clauses, here);
32894 c_name = "auto";
32895 break;
32896 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32897 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32898 c_name = "collapse";
32899 break;
32900 case PRAGMA_OACC_CLAUSE_COPY:
32901 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32902 c_name = "copy";
32903 break;
32904 case PRAGMA_OACC_CLAUSE_COPYIN:
32905 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32906 c_name = "copyin";
32907 break;
32908 case PRAGMA_OACC_CLAUSE_COPYOUT:
32909 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32910 c_name = "copyout";
32911 break;
32912 case PRAGMA_OACC_CLAUSE_CREATE:
32913 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32914 c_name = "create";
32915 break;
32916 case PRAGMA_OACC_CLAUSE_DELETE:
32917 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32918 c_name = "delete";
32919 break;
32920 case PRAGMA_OMP_CLAUSE_DEFAULT:
32921 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32922 c_name = "default";
32923 break;
32924 case PRAGMA_OACC_CLAUSE_DEVICE:
32925 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32926 c_name = "device";
32927 break;
32928 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32929 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32930 c_name = "deviceptr";
32931 break;
32932 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32933 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32934 c_name = "device_resident";
32935 break;
32936 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32937 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32938 clauses);
32939 c_name = "firstprivate";
32940 break;
32941 case PRAGMA_OACC_CLAUSE_GANG:
32942 c_name = "gang";
32943 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32944 c_name, clauses);
32945 break;
32946 case PRAGMA_OACC_CLAUSE_HOST:
32947 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32948 c_name = "host";
32949 break;
32950 case PRAGMA_OACC_CLAUSE_IF:
32951 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32952 c_name = "if";
32953 break;
32954 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32955 clauses = cp_parser_oacc_simple_clause (parser,
32956 OMP_CLAUSE_INDEPENDENT,
32957 clauses, here);
32958 c_name = "independent";
32959 break;
32960 case PRAGMA_OACC_CLAUSE_LINK:
32961 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32962 c_name = "link";
32963 break;
32964 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32965 code = OMP_CLAUSE_NUM_GANGS;
32966 c_name = "num_gangs";
32967 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32968 clauses);
32969 break;
32970 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32971 c_name = "num_workers";
32972 code = OMP_CLAUSE_NUM_WORKERS;
32973 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32974 clauses);
32975 break;
32976 case PRAGMA_OACC_CLAUSE_PRESENT:
32977 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32978 c_name = "present";
32979 break;
32980 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32981 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32982 c_name = "present_or_copy";
32983 break;
32984 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32985 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32986 c_name = "present_or_copyin";
32987 break;
32988 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32989 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32990 c_name = "present_or_copyout";
32991 break;
32992 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32993 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32994 c_name = "present_or_create";
32995 break;
32996 case PRAGMA_OACC_CLAUSE_PRIVATE:
32997 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32998 clauses);
32999 c_name = "private";
33000 break;
33001 case PRAGMA_OACC_CLAUSE_REDUCTION:
33002 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33003 c_name = "reduction";
33004 break;
33005 case PRAGMA_OACC_CLAUSE_SELF:
33006 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33007 c_name = "self";
33008 break;
33009 case PRAGMA_OACC_CLAUSE_SEQ:
33010 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33011 clauses, here);
33012 c_name = "seq";
33013 break;
33014 case PRAGMA_OACC_CLAUSE_TILE:
33015 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33016 c_name = "tile";
33017 break;
33018 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33019 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33020 clauses);
33021 c_name = "use_device";
33022 break;
33023 case PRAGMA_OACC_CLAUSE_VECTOR:
33024 c_name = "vector";
33025 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33026 c_name, clauses);
33027 break;
33028 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33029 c_name = "vector_length";
33030 code = OMP_CLAUSE_VECTOR_LENGTH;
33031 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33032 clauses);
33033 break;
33034 case PRAGMA_OACC_CLAUSE_WAIT:
33035 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33036 c_name = "wait";
33037 break;
33038 case PRAGMA_OACC_CLAUSE_WORKER:
33039 c_name = "worker";
33040 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33041 c_name, clauses);
33042 break;
33043 default:
33044 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33045 goto saw_error;
33046 }
33047
33048 first = false;
33049
33050 if (((mask >> c_kind) & 1) == 0)
33051 {
33052 /* Remove the invalid clause(s) from the list to avoid
33053 confusing the rest of the compiler. */
33054 clauses = prev;
33055 error_at (here, "%qs is not valid for %qs", c_name, where);
33056 }
33057 }
33058
33059 saw_error:
33060 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33061
33062 if (finish_p)
33063 return finish_omp_clauses (clauses, C_ORT_ACC);
33064
33065 return clauses;
33066 }
33067
33068 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33069 is a bitmask in MASK. Return the list of clauses found; the result
33070 of clause default goes in *pdefault. */
33071
33072 static tree
33073 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33074 const char *where, cp_token *pragma_tok,
33075 bool finish_p = true)
33076 {
33077 tree clauses = NULL;
33078 bool first = true;
33079 cp_token *token = NULL;
33080
33081 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33082 {
33083 pragma_omp_clause c_kind;
33084 const char *c_name;
33085 tree prev = clauses;
33086
33087 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33088 cp_lexer_consume_token (parser->lexer);
33089
33090 token = cp_lexer_peek_token (parser->lexer);
33091 c_kind = cp_parser_omp_clause_name (parser);
33092
33093 switch (c_kind)
33094 {
33095 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33096 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33097 token->location);
33098 c_name = "collapse";
33099 break;
33100 case PRAGMA_OMP_CLAUSE_COPYIN:
33101 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33102 c_name = "copyin";
33103 break;
33104 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33105 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33106 clauses);
33107 c_name = "copyprivate";
33108 break;
33109 case PRAGMA_OMP_CLAUSE_DEFAULT:
33110 clauses = cp_parser_omp_clause_default (parser, clauses,
33111 token->location, false);
33112 c_name = "default";
33113 break;
33114 case PRAGMA_OMP_CLAUSE_FINAL:
33115 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33116 c_name = "final";
33117 break;
33118 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33119 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33120 clauses);
33121 c_name = "firstprivate";
33122 break;
33123 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33124 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33125 token->location);
33126 c_name = "grainsize";
33127 break;
33128 case PRAGMA_OMP_CLAUSE_HINT:
33129 clauses = cp_parser_omp_clause_hint (parser, clauses,
33130 token->location);
33131 c_name = "hint";
33132 break;
33133 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33134 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33135 token->location);
33136 c_name = "defaultmap";
33137 break;
33138 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33139 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33140 clauses);
33141 c_name = "use_device_ptr";
33142 break;
33143 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33144 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33145 clauses);
33146 c_name = "is_device_ptr";
33147 break;
33148 case PRAGMA_OMP_CLAUSE_IF:
33149 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33150 true);
33151 c_name = "if";
33152 break;
33153 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33154 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33155 clauses);
33156 c_name = "lastprivate";
33157 break;
33158 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33159 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33160 token->location);
33161 c_name = "mergeable";
33162 break;
33163 case PRAGMA_OMP_CLAUSE_NOWAIT:
33164 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33165 c_name = "nowait";
33166 break;
33167 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33168 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33169 token->location);
33170 c_name = "num_tasks";
33171 break;
33172 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33173 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33174 token->location);
33175 c_name = "num_threads";
33176 break;
33177 case PRAGMA_OMP_CLAUSE_ORDERED:
33178 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33179 token->location);
33180 c_name = "ordered";
33181 break;
33182 case PRAGMA_OMP_CLAUSE_PRIORITY:
33183 clauses = cp_parser_omp_clause_priority (parser, clauses,
33184 token->location);
33185 c_name = "priority";
33186 break;
33187 case PRAGMA_OMP_CLAUSE_PRIVATE:
33188 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33189 clauses);
33190 c_name = "private";
33191 break;
33192 case PRAGMA_OMP_CLAUSE_REDUCTION:
33193 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33194 c_name = "reduction";
33195 break;
33196 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33197 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33198 token->location);
33199 c_name = "schedule";
33200 break;
33201 case PRAGMA_OMP_CLAUSE_SHARED:
33202 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33203 clauses);
33204 c_name = "shared";
33205 break;
33206 case PRAGMA_OMP_CLAUSE_UNTIED:
33207 clauses = cp_parser_omp_clause_untied (parser, clauses,
33208 token->location);
33209 c_name = "untied";
33210 break;
33211 case PRAGMA_OMP_CLAUSE_INBRANCH:
33212 case PRAGMA_CILK_CLAUSE_MASK:
33213 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33214 clauses, token->location);
33215 c_name = "inbranch";
33216 break;
33217 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33218 case PRAGMA_CILK_CLAUSE_NOMASK:
33219 clauses = cp_parser_omp_clause_branch (parser,
33220 OMP_CLAUSE_NOTINBRANCH,
33221 clauses, token->location);
33222 c_name = "notinbranch";
33223 break;
33224 case PRAGMA_OMP_CLAUSE_PARALLEL:
33225 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33226 clauses, token->location);
33227 c_name = "parallel";
33228 if (!first)
33229 {
33230 clause_not_first:
33231 error_at (token->location, "%qs must be the first clause of %qs",
33232 c_name, where);
33233 clauses = prev;
33234 }
33235 break;
33236 case PRAGMA_OMP_CLAUSE_FOR:
33237 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33238 clauses, token->location);
33239 c_name = "for";
33240 if (!first)
33241 goto clause_not_first;
33242 break;
33243 case PRAGMA_OMP_CLAUSE_SECTIONS:
33244 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33245 clauses, token->location);
33246 c_name = "sections";
33247 if (!first)
33248 goto clause_not_first;
33249 break;
33250 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33251 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33252 clauses, token->location);
33253 c_name = "taskgroup";
33254 if (!first)
33255 goto clause_not_first;
33256 break;
33257 case PRAGMA_OMP_CLAUSE_LINK:
33258 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33259 c_name = "to";
33260 break;
33261 case PRAGMA_OMP_CLAUSE_TO:
33262 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33263 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33264 clauses);
33265 else
33266 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33267 c_name = "to";
33268 break;
33269 case PRAGMA_OMP_CLAUSE_FROM:
33270 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33271 c_name = "from";
33272 break;
33273 case PRAGMA_OMP_CLAUSE_UNIFORM:
33274 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33275 clauses);
33276 c_name = "uniform";
33277 break;
33278 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33279 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33280 token->location);
33281 c_name = "num_teams";
33282 break;
33283 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33284 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33285 token->location);
33286 c_name = "thread_limit";
33287 break;
33288 case PRAGMA_OMP_CLAUSE_ALIGNED:
33289 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33290 c_name = "aligned";
33291 break;
33292 case PRAGMA_OMP_CLAUSE_LINEAR:
33293 {
33294 bool cilk_simd_fn = false, declare_simd = false;
33295 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33296 cilk_simd_fn = true;
33297 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33298 declare_simd = true;
33299 clauses = cp_parser_omp_clause_linear (parser, clauses,
33300 cilk_simd_fn, declare_simd);
33301 }
33302 c_name = "linear";
33303 break;
33304 case PRAGMA_OMP_CLAUSE_DEPEND:
33305 clauses = cp_parser_omp_clause_depend (parser, clauses,
33306 token->location);
33307 c_name = "depend";
33308 break;
33309 case PRAGMA_OMP_CLAUSE_MAP:
33310 clauses = cp_parser_omp_clause_map (parser, clauses);
33311 c_name = "map";
33312 break;
33313 case PRAGMA_OMP_CLAUSE_DEVICE:
33314 clauses = cp_parser_omp_clause_device (parser, clauses,
33315 token->location);
33316 c_name = "device";
33317 break;
33318 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33319 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33320 token->location);
33321 c_name = "dist_schedule";
33322 break;
33323 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33324 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33325 token->location);
33326 c_name = "proc_bind";
33327 break;
33328 case PRAGMA_OMP_CLAUSE_SAFELEN:
33329 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33330 token->location);
33331 c_name = "safelen";
33332 break;
33333 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33334 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33335 token->location);
33336 c_name = "simdlen";
33337 break;
33338 case PRAGMA_OMP_CLAUSE_NOGROUP:
33339 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33340 token->location);
33341 c_name = "nogroup";
33342 break;
33343 case PRAGMA_OMP_CLAUSE_THREADS:
33344 clauses
33345 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33346 clauses, token->location);
33347 c_name = "threads";
33348 break;
33349 case PRAGMA_OMP_CLAUSE_SIMD:
33350 clauses
33351 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33352 clauses, token->location);
33353 c_name = "simd";
33354 break;
33355 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33356 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33357 c_name = "simdlen";
33358 break;
33359 default:
33360 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33361 goto saw_error;
33362 }
33363
33364 first = false;
33365
33366 if (((mask >> c_kind) & 1) == 0)
33367 {
33368 /* Remove the invalid clause(s) from the list to avoid
33369 confusing the rest of the compiler. */
33370 clauses = prev;
33371 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33372 }
33373 }
33374 saw_error:
33375 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33376 no reason to skip to the end. */
33377 if (!(flag_cilkplus && pragma_tok == NULL))
33378 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33379 if (finish_p)
33380 {
33381 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33382 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33383 else
33384 return finish_omp_clauses (clauses, C_ORT_OMP);
33385 }
33386 return clauses;
33387 }
33388
33389 /* OpenMP 2.5:
33390 structured-block:
33391 statement
33392
33393 In practice, we're also interested in adding the statement to an
33394 outer node. So it is convenient if we work around the fact that
33395 cp_parser_statement calls add_stmt. */
33396
33397 static unsigned
33398 cp_parser_begin_omp_structured_block (cp_parser *parser)
33399 {
33400 unsigned save = parser->in_statement;
33401
33402 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33403 This preserves the "not within loop or switch" style error messages
33404 for nonsense cases like
33405 void foo() {
33406 #pragma omp single
33407 break;
33408 }
33409 */
33410 if (parser->in_statement)
33411 parser->in_statement = IN_OMP_BLOCK;
33412
33413 return save;
33414 }
33415
33416 static void
33417 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33418 {
33419 parser->in_statement = save;
33420 }
33421
33422 static tree
33423 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33424 {
33425 tree stmt = begin_omp_structured_block ();
33426 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33427
33428 cp_parser_statement (parser, NULL_TREE, false, if_p);
33429
33430 cp_parser_end_omp_structured_block (parser, save);
33431 return finish_omp_structured_block (stmt);
33432 }
33433
33434 /* OpenMP 2.5:
33435 # pragma omp atomic new-line
33436 expression-stmt
33437
33438 expression-stmt:
33439 x binop= expr | x++ | ++x | x-- | --x
33440 binop:
33441 +, *, -, /, &, ^, |, <<, >>
33442
33443 where x is an lvalue expression with scalar type.
33444
33445 OpenMP 3.1:
33446 # pragma omp atomic new-line
33447 update-stmt
33448
33449 # pragma omp atomic read new-line
33450 read-stmt
33451
33452 # pragma omp atomic write new-line
33453 write-stmt
33454
33455 # pragma omp atomic update new-line
33456 update-stmt
33457
33458 # pragma omp atomic capture new-line
33459 capture-stmt
33460
33461 # pragma omp atomic capture new-line
33462 capture-block
33463
33464 read-stmt:
33465 v = x
33466 write-stmt:
33467 x = expr
33468 update-stmt:
33469 expression-stmt | x = x binop expr
33470 capture-stmt:
33471 v = expression-stmt
33472 capture-block:
33473 { v = x; update-stmt; } | { update-stmt; v = x; }
33474
33475 OpenMP 4.0:
33476 update-stmt:
33477 expression-stmt | x = x binop expr | x = expr binop x
33478 capture-stmt:
33479 v = update-stmt
33480 capture-block:
33481 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33482
33483 where x and v are lvalue expressions with scalar type. */
33484
33485 static void
33486 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33487 {
33488 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33489 tree rhs1 = NULL_TREE, orig_lhs;
33490 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33491 bool structured_block = false;
33492 bool seq_cst = false;
33493
33494 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33495 {
33496 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33497 const char *p = IDENTIFIER_POINTER (id);
33498
33499 if (!strcmp (p, "seq_cst"))
33500 {
33501 seq_cst = true;
33502 cp_lexer_consume_token (parser->lexer);
33503 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33504 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33505 cp_lexer_consume_token (parser->lexer);
33506 }
33507 }
33508 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33509 {
33510 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33511 const char *p = IDENTIFIER_POINTER (id);
33512
33513 if (!strcmp (p, "read"))
33514 code = OMP_ATOMIC_READ;
33515 else if (!strcmp (p, "write"))
33516 code = NOP_EXPR;
33517 else if (!strcmp (p, "update"))
33518 code = OMP_ATOMIC;
33519 else if (!strcmp (p, "capture"))
33520 code = OMP_ATOMIC_CAPTURE_NEW;
33521 else
33522 p = NULL;
33523 if (p)
33524 cp_lexer_consume_token (parser->lexer);
33525 }
33526 if (!seq_cst)
33527 {
33528 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33529 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33530 cp_lexer_consume_token (parser->lexer);
33531
33532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33533 {
33534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33535 const char *p = IDENTIFIER_POINTER (id);
33536
33537 if (!strcmp (p, "seq_cst"))
33538 {
33539 seq_cst = true;
33540 cp_lexer_consume_token (parser->lexer);
33541 }
33542 }
33543 }
33544 cp_parser_require_pragma_eol (parser, pragma_tok);
33545
33546 switch (code)
33547 {
33548 case OMP_ATOMIC_READ:
33549 case NOP_EXPR: /* atomic write */
33550 v = cp_parser_unary_expression (parser);
33551 if (v == error_mark_node)
33552 goto saw_error;
33553 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33554 goto saw_error;
33555 if (code == NOP_EXPR)
33556 lhs = cp_parser_expression (parser);
33557 else
33558 lhs = cp_parser_unary_expression (parser);
33559 if (lhs == error_mark_node)
33560 goto saw_error;
33561 if (code == NOP_EXPR)
33562 {
33563 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33564 opcode. */
33565 code = OMP_ATOMIC;
33566 rhs = lhs;
33567 lhs = v;
33568 v = NULL_TREE;
33569 }
33570 goto done;
33571 case OMP_ATOMIC_CAPTURE_NEW:
33572 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33573 {
33574 cp_lexer_consume_token (parser->lexer);
33575 structured_block = true;
33576 }
33577 else
33578 {
33579 v = cp_parser_unary_expression (parser);
33580 if (v == error_mark_node)
33581 goto saw_error;
33582 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33583 goto saw_error;
33584 }
33585 default:
33586 break;
33587 }
33588
33589 restart:
33590 lhs = cp_parser_unary_expression (parser);
33591 orig_lhs = lhs;
33592 switch (TREE_CODE (lhs))
33593 {
33594 case ERROR_MARK:
33595 goto saw_error;
33596
33597 case POSTINCREMENT_EXPR:
33598 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33599 code = OMP_ATOMIC_CAPTURE_OLD;
33600 /* FALLTHROUGH */
33601 case PREINCREMENT_EXPR:
33602 lhs = TREE_OPERAND (lhs, 0);
33603 opcode = PLUS_EXPR;
33604 rhs = integer_one_node;
33605 break;
33606
33607 case POSTDECREMENT_EXPR:
33608 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33609 code = OMP_ATOMIC_CAPTURE_OLD;
33610 /* FALLTHROUGH */
33611 case PREDECREMENT_EXPR:
33612 lhs = TREE_OPERAND (lhs, 0);
33613 opcode = MINUS_EXPR;
33614 rhs = integer_one_node;
33615 break;
33616
33617 case COMPOUND_EXPR:
33618 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33619 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33620 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33621 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33622 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33623 (TREE_OPERAND (lhs, 1), 0), 0)))
33624 == BOOLEAN_TYPE)
33625 /* Undo effects of boolean_increment for post {in,de}crement. */
33626 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33627 /* FALLTHRU */
33628 case MODIFY_EXPR:
33629 if (TREE_CODE (lhs) == MODIFY_EXPR
33630 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33631 {
33632 /* Undo effects of boolean_increment. */
33633 if (integer_onep (TREE_OPERAND (lhs, 1)))
33634 {
33635 /* This is pre or post increment. */
33636 rhs = TREE_OPERAND (lhs, 1);
33637 lhs = TREE_OPERAND (lhs, 0);
33638 opcode = NOP_EXPR;
33639 if (code == OMP_ATOMIC_CAPTURE_NEW
33640 && !structured_block
33641 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33642 code = OMP_ATOMIC_CAPTURE_OLD;
33643 break;
33644 }
33645 }
33646 /* FALLTHRU */
33647 default:
33648 switch (cp_lexer_peek_token (parser->lexer)->type)
33649 {
33650 case CPP_MULT_EQ:
33651 opcode = MULT_EXPR;
33652 break;
33653 case CPP_DIV_EQ:
33654 opcode = TRUNC_DIV_EXPR;
33655 break;
33656 case CPP_PLUS_EQ:
33657 opcode = PLUS_EXPR;
33658 break;
33659 case CPP_MINUS_EQ:
33660 opcode = MINUS_EXPR;
33661 break;
33662 case CPP_LSHIFT_EQ:
33663 opcode = LSHIFT_EXPR;
33664 break;
33665 case CPP_RSHIFT_EQ:
33666 opcode = RSHIFT_EXPR;
33667 break;
33668 case CPP_AND_EQ:
33669 opcode = BIT_AND_EXPR;
33670 break;
33671 case CPP_OR_EQ:
33672 opcode = BIT_IOR_EXPR;
33673 break;
33674 case CPP_XOR_EQ:
33675 opcode = BIT_XOR_EXPR;
33676 break;
33677 case CPP_EQ:
33678 enum cp_parser_prec oprec;
33679 cp_token *token;
33680 cp_lexer_consume_token (parser->lexer);
33681 cp_parser_parse_tentatively (parser);
33682 rhs1 = cp_parser_simple_cast_expression (parser);
33683 if (rhs1 == error_mark_node)
33684 {
33685 cp_parser_abort_tentative_parse (parser);
33686 cp_parser_simple_cast_expression (parser);
33687 goto saw_error;
33688 }
33689 token = cp_lexer_peek_token (parser->lexer);
33690 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33691 {
33692 cp_parser_abort_tentative_parse (parser);
33693 cp_parser_parse_tentatively (parser);
33694 rhs = cp_parser_binary_expression (parser, false, true,
33695 PREC_NOT_OPERATOR, NULL);
33696 if (rhs == error_mark_node)
33697 {
33698 cp_parser_abort_tentative_parse (parser);
33699 cp_parser_binary_expression (parser, false, true,
33700 PREC_NOT_OPERATOR, NULL);
33701 goto saw_error;
33702 }
33703 switch (TREE_CODE (rhs))
33704 {
33705 case MULT_EXPR:
33706 case TRUNC_DIV_EXPR:
33707 case RDIV_EXPR:
33708 case PLUS_EXPR:
33709 case MINUS_EXPR:
33710 case LSHIFT_EXPR:
33711 case RSHIFT_EXPR:
33712 case BIT_AND_EXPR:
33713 case BIT_IOR_EXPR:
33714 case BIT_XOR_EXPR:
33715 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33716 {
33717 if (cp_parser_parse_definitely (parser))
33718 {
33719 opcode = TREE_CODE (rhs);
33720 rhs1 = TREE_OPERAND (rhs, 0);
33721 rhs = TREE_OPERAND (rhs, 1);
33722 goto stmt_done;
33723 }
33724 else
33725 goto saw_error;
33726 }
33727 break;
33728 default:
33729 break;
33730 }
33731 cp_parser_abort_tentative_parse (parser);
33732 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33733 {
33734 rhs = cp_parser_expression (parser);
33735 if (rhs == error_mark_node)
33736 goto saw_error;
33737 opcode = NOP_EXPR;
33738 rhs1 = NULL_TREE;
33739 goto stmt_done;
33740 }
33741 cp_parser_error (parser,
33742 "invalid form of %<#pragma omp atomic%>");
33743 goto saw_error;
33744 }
33745 if (!cp_parser_parse_definitely (parser))
33746 goto saw_error;
33747 switch (token->type)
33748 {
33749 case CPP_SEMICOLON:
33750 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33751 {
33752 code = OMP_ATOMIC_CAPTURE_OLD;
33753 v = lhs;
33754 lhs = NULL_TREE;
33755 lhs1 = rhs1;
33756 rhs1 = NULL_TREE;
33757 cp_lexer_consume_token (parser->lexer);
33758 goto restart;
33759 }
33760 else if (structured_block)
33761 {
33762 opcode = NOP_EXPR;
33763 rhs = rhs1;
33764 rhs1 = NULL_TREE;
33765 goto stmt_done;
33766 }
33767 cp_parser_error (parser,
33768 "invalid form of %<#pragma omp atomic%>");
33769 goto saw_error;
33770 case CPP_MULT:
33771 opcode = MULT_EXPR;
33772 break;
33773 case CPP_DIV:
33774 opcode = TRUNC_DIV_EXPR;
33775 break;
33776 case CPP_PLUS:
33777 opcode = PLUS_EXPR;
33778 break;
33779 case CPP_MINUS:
33780 opcode = MINUS_EXPR;
33781 break;
33782 case CPP_LSHIFT:
33783 opcode = LSHIFT_EXPR;
33784 break;
33785 case CPP_RSHIFT:
33786 opcode = RSHIFT_EXPR;
33787 break;
33788 case CPP_AND:
33789 opcode = BIT_AND_EXPR;
33790 break;
33791 case CPP_OR:
33792 opcode = BIT_IOR_EXPR;
33793 break;
33794 case CPP_XOR:
33795 opcode = BIT_XOR_EXPR;
33796 break;
33797 default:
33798 cp_parser_error (parser,
33799 "invalid operator for %<#pragma omp atomic%>");
33800 goto saw_error;
33801 }
33802 oprec = TOKEN_PRECEDENCE (token);
33803 gcc_assert (oprec != PREC_NOT_OPERATOR);
33804 if (commutative_tree_code (opcode))
33805 oprec = (enum cp_parser_prec) (oprec - 1);
33806 cp_lexer_consume_token (parser->lexer);
33807 rhs = cp_parser_binary_expression (parser, false, false,
33808 oprec, NULL);
33809 if (rhs == error_mark_node)
33810 goto saw_error;
33811 goto stmt_done;
33812 /* FALLTHROUGH */
33813 default:
33814 cp_parser_error (parser,
33815 "invalid operator for %<#pragma omp atomic%>");
33816 goto saw_error;
33817 }
33818 cp_lexer_consume_token (parser->lexer);
33819
33820 rhs = cp_parser_expression (parser);
33821 if (rhs == error_mark_node)
33822 goto saw_error;
33823 break;
33824 }
33825 stmt_done:
33826 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33827 {
33828 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33829 goto saw_error;
33830 v = cp_parser_unary_expression (parser);
33831 if (v == error_mark_node)
33832 goto saw_error;
33833 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33834 goto saw_error;
33835 lhs1 = cp_parser_unary_expression (parser);
33836 if (lhs1 == error_mark_node)
33837 goto saw_error;
33838 }
33839 if (structured_block)
33840 {
33841 cp_parser_consume_semicolon_at_end_of_statement (parser);
33842 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33843 }
33844 done:
33845 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33846 if (!structured_block)
33847 cp_parser_consume_semicolon_at_end_of_statement (parser);
33848 return;
33849
33850 saw_error:
33851 cp_parser_skip_to_end_of_block_or_statement (parser);
33852 if (structured_block)
33853 {
33854 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33855 cp_lexer_consume_token (parser->lexer);
33856 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33857 {
33858 cp_parser_skip_to_end_of_block_or_statement (parser);
33859 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33860 cp_lexer_consume_token (parser->lexer);
33861 }
33862 }
33863 }
33864
33865
33866 /* OpenMP 2.5:
33867 # pragma omp barrier new-line */
33868
33869 static void
33870 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33871 {
33872 cp_parser_require_pragma_eol (parser, pragma_tok);
33873 finish_omp_barrier ();
33874 }
33875
33876 /* OpenMP 2.5:
33877 # pragma omp critical [(name)] new-line
33878 structured-block
33879
33880 OpenMP 4.5:
33881 # pragma omp critical [(name) [hint(expression)]] new-line
33882 structured-block */
33883
33884 #define OMP_CRITICAL_CLAUSE_MASK \
33885 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33886
33887 static tree
33888 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33889 {
33890 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33891
33892 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33893 {
33894 cp_lexer_consume_token (parser->lexer);
33895
33896 name = cp_parser_identifier (parser);
33897
33898 if (name == error_mark_node
33899 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33900 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33901 /*or_comma=*/false,
33902 /*consume_paren=*/true);
33903 if (name == error_mark_node)
33904 name = NULL;
33905
33906 clauses = cp_parser_omp_all_clauses (parser,
33907 OMP_CRITICAL_CLAUSE_MASK,
33908 "#pragma omp critical", pragma_tok);
33909 }
33910 else
33911 cp_parser_require_pragma_eol (parser, pragma_tok);
33912
33913 stmt = cp_parser_omp_structured_block (parser, if_p);
33914 return c_finish_omp_critical (input_location, stmt, name, clauses);
33915 }
33916
33917 /* OpenMP 2.5:
33918 # pragma omp flush flush-vars[opt] new-line
33919
33920 flush-vars:
33921 ( variable-list ) */
33922
33923 static void
33924 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33925 {
33926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33927 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33928 cp_parser_require_pragma_eol (parser, pragma_tok);
33929
33930 finish_omp_flush ();
33931 }
33932
33933 /* Helper function, to parse omp for increment expression. */
33934
33935 static tree
33936 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33937 {
33938 tree cond = cp_parser_binary_expression (parser, false, true,
33939 PREC_NOT_OPERATOR, NULL);
33940 if (cond == error_mark_node
33941 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33942 {
33943 cp_parser_skip_to_end_of_statement (parser);
33944 return error_mark_node;
33945 }
33946
33947 switch (TREE_CODE (cond))
33948 {
33949 case GT_EXPR:
33950 case GE_EXPR:
33951 case LT_EXPR:
33952 case LE_EXPR:
33953 break;
33954 case NE_EXPR:
33955 if (code == CILK_SIMD || code == CILK_FOR)
33956 break;
33957 /* Fall through: OpenMP disallows NE_EXPR. */
33958 gcc_fallthrough ();
33959 default:
33960 return error_mark_node;
33961 }
33962
33963 /* If decl is an iterator, preserve LHS and RHS of the relational
33964 expr until finish_omp_for. */
33965 if (decl
33966 && (type_dependent_expression_p (decl)
33967 || CLASS_TYPE_P (TREE_TYPE (decl))))
33968 return cond;
33969
33970 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33971 TREE_CODE (cond),
33972 TREE_OPERAND (cond, 0), ERROR_MARK,
33973 TREE_OPERAND (cond, 1), ERROR_MARK,
33974 /*overload=*/NULL, tf_warning_or_error);
33975 }
33976
33977 /* Helper function, to parse omp for increment expression. */
33978
33979 static tree
33980 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33981 {
33982 cp_token *token = cp_lexer_peek_token (parser->lexer);
33983 enum tree_code op;
33984 tree lhs, rhs;
33985 cp_id_kind idk;
33986 bool decl_first;
33987
33988 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33989 {
33990 op = (token->type == CPP_PLUS_PLUS
33991 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33992 cp_lexer_consume_token (parser->lexer);
33993 lhs = cp_parser_simple_cast_expression (parser);
33994 if (lhs != decl
33995 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33996 return error_mark_node;
33997 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33998 }
33999
34000 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34001 if (lhs != decl
34002 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34003 return error_mark_node;
34004
34005 token = cp_lexer_peek_token (parser->lexer);
34006 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34007 {
34008 op = (token->type == CPP_PLUS_PLUS
34009 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34010 cp_lexer_consume_token (parser->lexer);
34011 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34012 }
34013
34014 op = cp_parser_assignment_operator_opt (parser);
34015 if (op == ERROR_MARK)
34016 return error_mark_node;
34017
34018 if (op != NOP_EXPR)
34019 {
34020 rhs = cp_parser_assignment_expression (parser);
34021 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34022 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34023 }
34024
34025 lhs = cp_parser_binary_expression (parser, false, false,
34026 PREC_ADDITIVE_EXPRESSION, NULL);
34027 token = cp_lexer_peek_token (parser->lexer);
34028 decl_first = (lhs == decl
34029 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34030 if (decl_first)
34031 lhs = NULL_TREE;
34032 if (token->type != CPP_PLUS
34033 && token->type != CPP_MINUS)
34034 return error_mark_node;
34035
34036 do
34037 {
34038 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34039 cp_lexer_consume_token (parser->lexer);
34040 rhs = cp_parser_binary_expression (parser, false, false,
34041 PREC_ADDITIVE_EXPRESSION, NULL);
34042 token = cp_lexer_peek_token (parser->lexer);
34043 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34044 {
34045 if (lhs == NULL_TREE)
34046 {
34047 if (op == PLUS_EXPR)
34048 lhs = rhs;
34049 else
34050 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34051 tf_warning_or_error);
34052 }
34053 else
34054 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34055 ERROR_MARK, NULL, tf_warning_or_error);
34056 }
34057 }
34058 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34059
34060 if (!decl_first)
34061 {
34062 if ((rhs != decl
34063 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34064 || op == MINUS_EXPR)
34065 return error_mark_node;
34066 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34067 }
34068 else
34069 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34070
34071 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34072 }
34073
34074 /* Parse the initialization statement of either an OpenMP for loop or
34075 a Cilk Plus for loop.
34076
34077 Return true if the resulting construct should have an
34078 OMP_CLAUSE_PRIVATE added to it. */
34079
34080 static tree
34081 cp_parser_omp_for_loop_init (cp_parser *parser,
34082 enum tree_code code,
34083 tree &this_pre_body,
34084 vec<tree, va_gc> *for_block,
34085 tree &init,
34086 tree &orig_init,
34087 tree &decl,
34088 tree &real_decl)
34089 {
34090 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34091 return NULL_TREE;
34092
34093 tree add_private_clause = NULL_TREE;
34094
34095 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34096
34097 init-expr:
34098 var = lb
34099 integer-type var = lb
34100 random-access-iterator-type var = lb
34101 pointer-type var = lb
34102 */
34103 cp_decl_specifier_seq type_specifiers;
34104
34105 /* First, try to parse as an initialized declaration. See
34106 cp_parser_condition, from whence the bulk of this is copied. */
34107
34108 cp_parser_parse_tentatively (parser);
34109 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34110 /*is_trailing_return=*/false,
34111 &type_specifiers);
34112 if (cp_parser_parse_definitely (parser))
34113 {
34114 /* If parsing a type specifier seq succeeded, then this
34115 MUST be a initialized declaration. */
34116 tree asm_specification, attributes;
34117 cp_declarator *declarator;
34118
34119 declarator = cp_parser_declarator (parser,
34120 CP_PARSER_DECLARATOR_NAMED,
34121 /*ctor_dtor_or_conv_p=*/NULL,
34122 /*parenthesized_p=*/NULL,
34123 /*member_p=*/false,
34124 /*friend_p=*/false);
34125 attributes = cp_parser_attributes_opt (parser);
34126 asm_specification = cp_parser_asm_specification_opt (parser);
34127
34128 if (declarator == cp_error_declarator)
34129 cp_parser_skip_to_end_of_statement (parser);
34130
34131 else
34132 {
34133 tree pushed_scope, auto_node;
34134
34135 decl = start_decl (declarator, &type_specifiers,
34136 SD_INITIALIZED, attributes,
34137 /*prefix_attributes=*/NULL_TREE,
34138 &pushed_scope);
34139
34140 auto_node = type_uses_auto (TREE_TYPE (decl));
34141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34142 {
34143 if (cp_lexer_next_token_is (parser->lexer,
34144 CPP_OPEN_PAREN))
34145 {
34146 if (code != CILK_SIMD && code != CILK_FOR)
34147 error ("parenthesized initialization is not allowed in "
34148 "OpenMP %<for%> loop");
34149 else
34150 error ("parenthesized initialization is "
34151 "not allowed in for-loop");
34152 }
34153 else
34154 /* Trigger an error. */
34155 cp_parser_require (parser, CPP_EQ, RT_EQ);
34156
34157 init = error_mark_node;
34158 cp_parser_skip_to_end_of_statement (parser);
34159 }
34160 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34161 || type_dependent_expression_p (decl)
34162 || auto_node)
34163 {
34164 bool is_direct_init, is_non_constant_init;
34165
34166 init = cp_parser_initializer (parser,
34167 &is_direct_init,
34168 &is_non_constant_init);
34169
34170 if (auto_node)
34171 {
34172 TREE_TYPE (decl)
34173 = do_auto_deduction (TREE_TYPE (decl), init,
34174 auto_node);
34175
34176 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34177 && !type_dependent_expression_p (decl))
34178 goto non_class;
34179 }
34180
34181 cp_finish_decl (decl, init, !is_non_constant_init,
34182 asm_specification,
34183 LOOKUP_ONLYCONVERTING);
34184 orig_init = init;
34185 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34186 {
34187 vec_safe_push (for_block, this_pre_body);
34188 init = NULL_TREE;
34189 }
34190 else
34191 {
34192 init = pop_stmt_list (this_pre_body);
34193 if (init && TREE_CODE (init) == STATEMENT_LIST)
34194 {
34195 tree_stmt_iterator i = tsi_start (init);
34196 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34197 while (!tsi_end_p (i))
34198 {
34199 tree t = tsi_stmt (i);
34200 if (TREE_CODE (t) == DECL_EXPR
34201 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34202 {
34203 tsi_delink (&i);
34204 vec_safe_push (for_block, t);
34205 continue;
34206 }
34207 break;
34208 }
34209 if (tsi_one_before_end_p (i))
34210 {
34211 tree t = tsi_stmt (i);
34212 tsi_delink (&i);
34213 free_stmt_list (init);
34214 init = t;
34215 }
34216 }
34217 }
34218 this_pre_body = NULL_TREE;
34219 }
34220 else
34221 {
34222 /* Consume '='. */
34223 cp_lexer_consume_token (parser->lexer);
34224 init = cp_parser_assignment_expression (parser);
34225
34226 non_class:
34227 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34228 init = error_mark_node;
34229 else
34230 cp_finish_decl (decl, NULL_TREE,
34231 /*init_const_expr_p=*/false,
34232 asm_specification,
34233 LOOKUP_ONLYCONVERTING);
34234 }
34235
34236 if (pushed_scope)
34237 pop_scope (pushed_scope);
34238 }
34239 }
34240 else
34241 {
34242 cp_id_kind idk;
34243 /* If parsing a type specifier sequence failed, then
34244 this MUST be a simple expression. */
34245 if (code == CILK_FOR)
34246 error ("%<_Cilk_for%> allows expression instead of declaration only "
34247 "in C, not in C++");
34248 cp_parser_parse_tentatively (parser);
34249 decl = cp_parser_primary_expression (parser, false, false,
34250 false, &idk);
34251 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34252 if (!cp_parser_error_occurred (parser)
34253 && decl
34254 && (TREE_CODE (decl) == COMPONENT_REF
34255 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34256 {
34257 cp_parser_abort_tentative_parse (parser);
34258 cp_parser_parse_tentatively (parser);
34259 cp_token *token = cp_lexer_peek_token (parser->lexer);
34260 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34261 /*check_dependency_p=*/true,
34262 /*template_p=*/NULL,
34263 /*declarator_p=*/false,
34264 /*optional_p=*/false);
34265 if (name != error_mark_node
34266 && last_tok == cp_lexer_peek_token (parser->lexer))
34267 {
34268 decl = cp_parser_lookup_name_simple (parser, name,
34269 token->location);
34270 if (TREE_CODE (decl) == FIELD_DECL)
34271 add_private_clause = omp_privatize_field (decl, false);
34272 }
34273 cp_parser_abort_tentative_parse (parser);
34274 cp_parser_parse_tentatively (parser);
34275 decl = cp_parser_primary_expression (parser, false, false,
34276 false, &idk);
34277 }
34278 if (!cp_parser_error_occurred (parser)
34279 && decl
34280 && DECL_P (decl)
34281 && CLASS_TYPE_P (TREE_TYPE (decl)))
34282 {
34283 tree rhs;
34284
34285 cp_parser_parse_definitely (parser);
34286 cp_parser_require (parser, CPP_EQ, RT_EQ);
34287 rhs = cp_parser_assignment_expression (parser);
34288 orig_init = rhs;
34289 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34290 decl, NOP_EXPR,
34291 rhs,
34292 tf_warning_or_error));
34293 if (!add_private_clause)
34294 add_private_clause = decl;
34295 }
34296 else
34297 {
34298 decl = NULL;
34299 cp_parser_abort_tentative_parse (parser);
34300 init = cp_parser_expression (parser);
34301 if (init)
34302 {
34303 if (TREE_CODE (init) == MODIFY_EXPR
34304 || TREE_CODE (init) == MODOP_EXPR)
34305 real_decl = TREE_OPERAND (init, 0);
34306 }
34307 }
34308 }
34309 return add_private_clause;
34310 }
34311
34312 /* Parse the restricted form of the for statement allowed by OpenMP. */
34313
34314 static tree
34315 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34316 tree *cclauses, bool *if_p)
34317 {
34318 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34319 tree real_decl, initv, condv, incrv, declv;
34320 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34321 location_t loc_first;
34322 bool collapse_err = false;
34323 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34324 vec<tree, va_gc> *for_block = make_tree_vector ();
34325 auto_vec<tree, 4> orig_inits;
34326
34327 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34328 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34329 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34330 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34331 && OMP_CLAUSE_ORDERED_EXPR (cl))
34332 {
34333 ordered_cl = cl;
34334 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34335 }
34336
34337 if (ordered && ordered < collapse)
34338 {
34339 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34340 "%<ordered%> clause parameter is less than %<collapse%>");
34341 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34342 = build_int_cst (NULL_TREE, collapse);
34343 ordered = collapse;
34344 }
34345 if (ordered)
34346 {
34347 for (tree *pc = &clauses; *pc; )
34348 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34349 {
34350 error_at (OMP_CLAUSE_LOCATION (*pc),
34351 "%<linear%> clause may not be specified together "
34352 "with %<ordered%> clause with a parameter");
34353 *pc = OMP_CLAUSE_CHAIN (*pc);
34354 }
34355 else
34356 pc = &OMP_CLAUSE_CHAIN (*pc);
34357 }
34358
34359 gcc_assert (collapse >= 1 && ordered >= 0);
34360 count = ordered ? ordered : collapse;
34361
34362 declv = make_tree_vec (count);
34363 initv = make_tree_vec (count);
34364 condv = make_tree_vec (count);
34365 incrv = make_tree_vec (count);
34366
34367 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34368
34369 for (i = 0; i < count; i++)
34370 {
34371 int bracecount = 0;
34372 tree add_private_clause = NULL_TREE;
34373 location_t loc;
34374
34375 if (code != CILK_FOR
34376 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34377 {
34378 cp_parser_error (parser, "for statement expected");
34379 return NULL;
34380 }
34381 if (code == CILK_FOR
34382 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34383 {
34384 cp_parser_error (parser, "_Cilk_for statement expected");
34385 return NULL;
34386 }
34387 loc = cp_lexer_consume_token (parser->lexer)->location;
34388
34389 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34390 return NULL;
34391
34392 init = orig_init = decl = real_decl = NULL;
34393 this_pre_body = push_stmt_list ();
34394
34395 add_private_clause
34396 = cp_parser_omp_for_loop_init (parser, code,
34397 this_pre_body, for_block,
34398 init, orig_init, decl, real_decl);
34399
34400 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34401 if (this_pre_body)
34402 {
34403 this_pre_body = pop_stmt_list (this_pre_body);
34404 if (pre_body)
34405 {
34406 tree t = pre_body;
34407 pre_body = push_stmt_list ();
34408 add_stmt (t);
34409 add_stmt (this_pre_body);
34410 pre_body = pop_stmt_list (pre_body);
34411 }
34412 else
34413 pre_body = this_pre_body;
34414 }
34415
34416 if (decl)
34417 real_decl = decl;
34418 if (cclauses != NULL
34419 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34420 && real_decl != NULL_TREE)
34421 {
34422 tree *c;
34423 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34424 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34425 && OMP_CLAUSE_DECL (*c) == real_decl)
34426 {
34427 error_at (loc, "iteration variable %qD"
34428 " should not be firstprivate", real_decl);
34429 *c = OMP_CLAUSE_CHAIN (*c);
34430 }
34431 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34432 && OMP_CLAUSE_DECL (*c) == real_decl)
34433 {
34434 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34435 tree l = *c;
34436 *c = OMP_CLAUSE_CHAIN (*c);
34437 if (code == OMP_SIMD)
34438 {
34439 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34440 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34441 }
34442 else
34443 {
34444 OMP_CLAUSE_CHAIN (l) = clauses;
34445 clauses = l;
34446 }
34447 add_private_clause = NULL_TREE;
34448 }
34449 else
34450 {
34451 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34452 && OMP_CLAUSE_DECL (*c) == real_decl)
34453 add_private_clause = NULL_TREE;
34454 c = &OMP_CLAUSE_CHAIN (*c);
34455 }
34456 }
34457
34458 if (add_private_clause)
34459 {
34460 tree c;
34461 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34462 {
34463 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34464 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34465 && OMP_CLAUSE_DECL (c) == decl)
34466 break;
34467 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34468 && OMP_CLAUSE_DECL (c) == decl)
34469 error_at (loc, "iteration variable %qD "
34470 "should not be firstprivate",
34471 decl);
34472 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34473 && OMP_CLAUSE_DECL (c) == decl)
34474 error_at (loc, "iteration variable %qD should not be reduction",
34475 decl);
34476 }
34477 if (c == NULL)
34478 {
34479 if (code != OMP_SIMD)
34480 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34481 else if (collapse == 1)
34482 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34483 else
34484 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34485 OMP_CLAUSE_DECL (c) = add_private_clause;
34486 c = finish_omp_clauses (c, C_ORT_OMP);
34487 if (c)
34488 {
34489 OMP_CLAUSE_CHAIN (c) = clauses;
34490 clauses = c;
34491 /* For linear, signal that we need to fill up
34492 the so far unknown linear step. */
34493 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34494 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34495 }
34496 }
34497 }
34498
34499 cond = NULL;
34500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34501 cond = cp_parser_omp_for_cond (parser, decl, code);
34502 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34503
34504 incr = NULL;
34505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34506 {
34507 /* If decl is an iterator, preserve the operator on decl
34508 until finish_omp_for. */
34509 if (real_decl
34510 && ((processing_template_decl
34511 && (TREE_TYPE (real_decl) == NULL_TREE
34512 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34513 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34514 incr = cp_parser_omp_for_incr (parser, real_decl);
34515 else
34516 incr = cp_parser_expression (parser);
34517 if (!EXPR_HAS_LOCATION (incr))
34518 protected_set_expr_location (incr, input_location);
34519 }
34520
34521 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34523 /*or_comma=*/false,
34524 /*consume_paren=*/true);
34525
34526 TREE_VEC_ELT (declv, i) = decl;
34527 TREE_VEC_ELT (initv, i) = init;
34528 TREE_VEC_ELT (condv, i) = cond;
34529 TREE_VEC_ELT (incrv, i) = incr;
34530 if (orig_init)
34531 {
34532 orig_inits.safe_grow_cleared (i + 1);
34533 orig_inits[i] = orig_init;
34534 }
34535
34536 if (i == count - 1)
34537 break;
34538
34539 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34540 in between the collapsed for loops to be still considered perfectly
34541 nested. Hopefully the final version clarifies this.
34542 For now handle (multiple) {'s and empty statements. */
34543 cp_parser_parse_tentatively (parser);
34544 do
34545 {
34546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34547 break;
34548 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34549 {
34550 cp_lexer_consume_token (parser->lexer);
34551 bracecount++;
34552 }
34553 else if (bracecount
34554 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34555 cp_lexer_consume_token (parser->lexer);
34556 else
34557 {
34558 loc = cp_lexer_peek_token (parser->lexer)->location;
34559 error_at (loc, "not enough collapsed for loops");
34560 collapse_err = true;
34561 cp_parser_abort_tentative_parse (parser);
34562 declv = NULL_TREE;
34563 break;
34564 }
34565 }
34566 while (1);
34567
34568 if (declv)
34569 {
34570 cp_parser_parse_definitely (parser);
34571 nbraces += bracecount;
34572 }
34573 }
34574
34575 if (nbraces)
34576 if_p = NULL;
34577
34578 /* Note that we saved the original contents of this flag when we entered
34579 the structured block, and so we don't need to re-save it here. */
34580 if (code == CILK_SIMD || code == CILK_FOR)
34581 parser->in_statement = IN_CILK_SIMD_FOR;
34582 else
34583 parser->in_statement = IN_OMP_FOR;
34584
34585 /* Note that the grammar doesn't call for a structured block here,
34586 though the loop as a whole is a structured block. */
34587 body = push_stmt_list ();
34588 cp_parser_statement (parser, NULL_TREE, false, if_p);
34589 body = pop_stmt_list (body);
34590
34591 if (declv == NULL_TREE)
34592 ret = NULL_TREE;
34593 else
34594 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34595 body, pre_body, &orig_inits, clauses);
34596
34597 while (nbraces)
34598 {
34599 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34600 {
34601 cp_lexer_consume_token (parser->lexer);
34602 nbraces--;
34603 }
34604 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34605 cp_lexer_consume_token (parser->lexer);
34606 else
34607 {
34608 if (!collapse_err)
34609 {
34610 error_at (cp_lexer_peek_token (parser->lexer)->location,
34611 "collapsed loops not perfectly nested");
34612 }
34613 collapse_err = true;
34614 cp_parser_statement_seq_opt (parser, NULL);
34615 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34616 break;
34617 }
34618 }
34619
34620 while (!for_block->is_empty ())
34621 {
34622 tree t = for_block->pop ();
34623 if (TREE_CODE (t) == STATEMENT_LIST)
34624 add_stmt (pop_stmt_list (t));
34625 else
34626 add_stmt (t);
34627 }
34628 release_tree_vector (for_block);
34629
34630 return ret;
34631 }
34632
34633 /* Helper function for OpenMP parsing, split clauses and call
34634 finish_omp_clauses on each of the set of clauses afterwards. */
34635
34636 static void
34637 cp_omp_split_clauses (location_t loc, enum tree_code code,
34638 omp_clause_mask mask, tree clauses, tree *cclauses)
34639 {
34640 int i;
34641 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34642 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34643 if (cclauses[i])
34644 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34645 }
34646
34647 /* OpenMP 4.0:
34648 #pragma omp simd simd-clause[optseq] new-line
34649 for-loop */
34650
34651 #define OMP_SIMD_CLAUSE_MASK \
34652 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34660
34661 static tree
34662 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34663 char *p_name, omp_clause_mask mask, tree *cclauses,
34664 bool *if_p)
34665 {
34666 tree clauses, sb, ret;
34667 unsigned int save;
34668 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34669
34670 strcat (p_name, " simd");
34671 mask |= OMP_SIMD_CLAUSE_MASK;
34672
34673 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34674 cclauses == NULL);
34675 if (cclauses)
34676 {
34677 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34678 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34679 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34680 OMP_CLAUSE_ORDERED);
34681 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34682 {
34683 error_at (OMP_CLAUSE_LOCATION (c),
34684 "%<ordered%> clause with parameter may not be specified "
34685 "on %qs construct", p_name);
34686 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34687 }
34688 }
34689
34690 sb = begin_omp_structured_block ();
34691 save = cp_parser_begin_omp_structured_block (parser);
34692
34693 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34694
34695 cp_parser_end_omp_structured_block (parser, save);
34696 add_stmt (finish_omp_structured_block (sb));
34697
34698 return ret;
34699 }
34700
34701 /* OpenMP 2.5:
34702 #pragma omp for for-clause[optseq] new-line
34703 for-loop
34704
34705 OpenMP 4.0:
34706 #pragma omp for simd for-simd-clause[optseq] new-line
34707 for-loop */
34708
34709 #define OMP_FOR_CLAUSE_MASK \
34710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34719
34720 static tree
34721 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34722 char *p_name, omp_clause_mask mask, tree *cclauses,
34723 bool *if_p)
34724 {
34725 tree clauses, sb, ret;
34726 unsigned int save;
34727 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34728
34729 strcat (p_name, " for");
34730 mask |= OMP_FOR_CLAUSE_MASK;
34731 /* parallel for{, simd} disallows nowait clause, but for
34732 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34733 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34734 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34735 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34736 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34737 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34738
34739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34740 {
34741 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34742 const char *p = IDENTIFIER_POINTER (id);
34743
34744 if (strcmp (p, "simd") == 0)
34745 {
34746 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34747 if (cclauses == NULL)
34748 cclauses = cclauses_buf;
34749
34750 cp_lexer_consume_token (parser->lexer);
34751 if (!flag_openmp) /* flag_openmp_simd */
34752 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34753 cclauses, if_p);
34754 sb = begin_omp_structured_block ();
34755 save = cp_parser_begin_omp_structured_block (parser);
34756 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34757 cclauses, if_p);
34758 cp_parser_end_omp_structured_block (parser, save);
34759 tree body = finish_omp_structured_block (sb);
34760 if (ret == NULL)
34761 return ret;
34762 ret = make_node (OMP_FOR);
34763 TREE_TYPE (ret) = void_type_node;
34764 OMP_FOR_BODY (ret) = body;
34765 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34766 SET_EXPR_LOCATION (ret, loc);
34767 add_stmt (ret);
34768 return ret;
34769 }
34770 }
34771 if (!flag_openmp) /* flag_openmp_simd */
34772 {
34773 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34774 return NULL_TREE;
34775 }
34776
34777 /* Composite distribute parallel for disallows linear clause. */
34778 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34779 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
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_FOR, mask, clauses, cclauses);
34786 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34787 }
34788
34789 sb = begin_omp_structured_block ();
34790 save = cp_parser_begin_omp_structured_block (parser);
34791
34792 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34793
34794 cp_parser_end_omp_structured_block (parser, save);
34795 add_stmt (finish_omp_structured_block (sb));
34796
34797 return ret;
34798 }
34799
34800 /* OpenMP 2.5:
34801 # pragma omp master new-line
34802 structured-block */
34803
34804 static tree
34805 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34806 {
34807 cp_parser_require_pragma_eol (parser, pragma_tok);
34808 return c_finish_omp_master (input_location,
34809 cp_parser_omp_structured_block (parser, if_p));
34810 }
34811
34812 /* OpenMP 2.5:
34813 # pragma omp ordered new-line
34814 structured-block
34815
34816 OpenMP 4.5:
34817 # pragma omp ordered ordered-clauses new-line
34818 structured-block */
34819
34820 #define OMP_ORDERED_CLAUSE_MASK \
34821 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34823
34824 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34825 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34826
34827 static bool
34828 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34829 enum pragma_context context, bool *if_p)
34830 {
34831 location_t loc = pragma_tok->location;
34832
34833 if (context != pragma_stmt && context != pragma_compound)
34834 {
34835 cp_parser_error (parser, "expected declaration specifiers");
34836 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34837 return false;
34838 }
34839
34840 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34841 {
34842 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34843 const char *p = IDENTIFIER_POINTER (id);
34844
34845 if (strcmp (p, "depend") == 0)
34846 {
34847 if (context == pragma_stmt)
34848 {
34849 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34850 "%<depend%> clause may only be used in compound "
34851 "statements");
34852 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34853 return false;
34854 }
34855 tree clauses
34856 = cp_parser_omp_all_clauses (parser,
34857 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34858 "#pragma omp ordered", pragma_tok);
34859 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34860 return false;
34861 }
34862 }
34863
34864 tree clauses
34865 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34866 "#pragma omp ordered", pragma_tok);
34867 c_finish_omp_ordered (loc, clauses,
34868 cp_parser_omp_structured_block (parser, if_p));
34869 return true;
34870 }
34871
34872 /* OpenMP 2.5:
34873
34874 section-scope:
34875 { section-sequence }
34876
34877 section-sequence:
34878 section-directive[opt] structured-block
34879 section-sequence section-directive structured-block */
34880
34881 static tree
34882 cp_parser_omp_sections_scope (cp_parser *parser)
34883 {
34884 tree stmt, substmt;
34885 bool error_suppress = false;
34886 cp_token *tok;
34887
34888 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34889 return NULL_TREE;
34890
34891 stmt = push_stmt_list ();
34892
34893 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34894 != PRAGMA_OMP_SECTION)
34895 {
34896 substmt = cp_parser_omp_structured_block (parser, NULL);
34897 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34898 add_stmt (substmt);
34899 }
34900
34901 while (1)
34902 {
34903 tok = cp_lexer_peek_token (parser->lexer);
34904 if (tok->type == CPP_CLOSE_BRACE)
34905 break;
34906 if (tok->type == CPP_EOF)
34907 break;
34908
34909 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34910 {
34911 cp_lexer_consume_token (parser->lexer);
34912 cp_parser_require_pragma_eol (parser, tok);
34913 error_suppress = false;
34914 }
34915 else if (!error_suppress)
34916 {
34917 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34918 error_suppress = true;
34919 }
34920
34921 substmt = cp_parser_omp_structured_block (parser, NULL);
34922 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34923 add_stmt (substmt);
34924 }
34925 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34926
34927 substmt = pop_stmt_list (stmt);
34928
34929 stmt = make_node (OMP_SECTIONS);
34930 TREE_TYPE (stmt) = void_type_node;
34931 OMP_SECTIONS_BODY (stmt) = substmt;
34932
34933 add_stmt (stmt);
34934 return stmt;
34935 }
34936
34937 /* OpenMP 2.5:
34938 # pragma omp sections sections-clause[optseq] newline
34939 sections-scope */
34940
34941 #define OMP_SECTIONS_CLAUSE_MASK \
34942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34947
34948 static tree
34949 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34950 char *p_name, omp_clause_mask mask, tree *cclauses)
34951 {
34952 tree clauses, ret;
34953 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34954
34955 strcat (p_name, " sections");
34956 mask |= OMP_SECTIONS_CLAUSE_MASK;
34957 if (cclauses)
34958 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34959
34960 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34961 cclauses == NULL);
34962 if (cclauses)
34963 {
34964 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34965 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34966 }
34967
34968 ret = cp_parser_omp_sections_scope (parser);
34969 if (ret)
34970 OMP_SECTIONS_CLAUSES (ret) = clauses;
34971
34972 return ret;
34973 }
34974
34975 /* OpenMP 2.5:
34976 # pragma omp parallel parallel-clause[optseq] new-line
34977 structured-block
34978 # pragma omp parallel for parallel-for-clause[optseq] new-line
34979 structured-block
34980 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34981 structured-block
34982
34983 OpenMP 4.0:
34984 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34985 structured-block */
34986
34987 #define OMP_PARALLEL_CLAUSE_MASK \
34988 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34997
34998 static tree
34999 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35000 char *p_name, omp_clause_mask mask, tree *cclauses,
35001 bool *if_p)
35002 {
35003 tree stmt, clauses, block;
35004 unsigned int save;
35005 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35006
35007 strcat (p_name, " parallel");
35008 mask |= OMP_PARALLEL_CLAUSE_MASK;
35009 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35010 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35011 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35012 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35013
35014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35015 {
35016 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35017 if (cclauses == NULL)
35018 cclauses = cclauses_buf;
35019
35020 cp_lexer_consume_token (parser->lexer);
35021 if (!flag_openmp) /* flag_openmp_simd */
35022 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35023 if_p);
35024 block = begin_omp_parallel ();
35025 save = cp_parser_begin_omp_structured_block (parser);
35026 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35027 if_p);
35028 cp_parser_end_omp_structured_block (parser, save);
35029 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35030 block);
35031 if (ret == NULL_TREE)
35032 return ret;
35033 OMP_PARALLEL_COMBINED (stmt) = 1;
35034 return stmt;
35035 }
35036 /* When combined with distribute, parallel has to be followed by for.
35037 #pragma omp target parallel is allowed though. */
35038 else if (cclauses
35039 && (mask & (OMP_CLAUSE_MASK_1
35040 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35041 {
35042 error_at (loc, "expected %<for%> after %qs", p_name);
35043 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35044 return NULL_TREE;
35045 }
35046 else if (!flag_openmp) /* flag_openmp_simd */
35047 {
35048 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35049 return NULL_TREE;
35050 }
35051 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35052 {
35053 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35054 const char *p = IDENTIFIER_POINTER (id);
35055 if (strcmp (p, "sections") == 0)
35056 {
35057 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35058 cclauses = cclauses_buf;
35059
35060 cp_lexer_consume_token (parser->lexer);
35061 block = begin_omp_parallel ();
35062 save = cp_parser_begin_omp_structured_block (parser);
35063 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35064 cp_parser_end_omp_structured_block (parser, save);
35065 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35066 block);
35067 OMP_PARALLEL_COMBINED (stmt) = 1;
35068 return stmt;
35069 }
35070 }
35071
35072 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35073 cclauses == NULL);
35074 if (cclauses)
35075 {
35076 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35077 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35078 }
35079
35080 block = begin_omp_parallel ();
35081 save = cp_parser_begin_omp_structured_block (parser);
35082 cp_parser_statement (parser, NULL_TREE, false, if_p);
35083 cp_parser_end_omp_structured_block (parser, save);
35084 stmt = finish_omp_parallel (clauses, block);
35085 return stmt;
35086 }
35087
35088 /* OpenMP 2.5:
35089 # pragma omp single single-clause[optseq] new-line
35090 structured-block */
35091
35092 #define OMP_SINGLE_CLAUSE_MASK \
35093 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35097
35098 static tree
35099 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35100 {
35101 tree stmt = make_node (OMP_SINGLE);
35102 TREE_TYPE (stmt) = void_type_node;
35103
35104 OMP_SINGLE_CLAUSES (stmt)
35105 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35106 "#pragma omp single", pragma_tok);
35107 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35108
35109 return add_stmt (stmt);
35110 }
35111
35112 /* OpenMP 3.0:
35113 # pragma omp task task-clause[optseq] new-line
35114 structured-block */
35115
35116 #define OMP_TASK_CLAUSE_MASK \
35117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35127
35128 static tree
35129 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35130 {
35131 tree clauses, block;
35132 unsigned int save;
35133
35134 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35135 "#pragma omp task", pragma_tok);
35136 block = begin_omp_task ();
35137 save = cp_parser_begin_omp_structured_block (parser);
35138 cp_parser_statement (parser, NULL_TREE, false, if_p);
35139 cp_parser_end_omp_structured_block (parser, save);
35140 return finish_omp_task (clauses, block);
35141 }
35142
35143 /* OpenMP 3.0:
35144 # pragma omp taskwait new-line */
35145
35146 static void
35147 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35148 {
35149 cp_parser_require_pragma_eol (parser, pragma_tok);
35150 finish_omp_taskwait ();
35151 }
35152
35153 /* OpenMP 3.1:
35154 # pragma omp taskyield new-line */
35155
35156 static void
35157 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35158 {
35159 cp_parser_require_pragma_eol (parser, pragma_tok);
35160 finish_omp_taskyield ();
35161 }
35162
35163 /* OpenMP 4.0:
35164 # pragma omp taskgroup new-line
35165 structured-block */
35166
35167 static tree
35168 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35169 {
35170 cp_parser_require_pragma_eol (parser, pragma_tok);
35171 return c_finish_omp_taskgroup (input_location,
35172 cp_parser_omp_structured_block (parser,
35173 if_p));
35174 }
35175
35176
35177 /* OpenMP 2.5:
35178 # pragma omp threadprivate (variable-list) */
35179
35180 static void
35181 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35182 {
35183 tree vars;
35184
35185 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35186 cp_parser_require_pragma_eol (parser, pragma_tok);
35187
35188 finish_omp_threadprivate (vars);
35189 }
35190
35191 /* OpenMP 4.0:
35192 # pragma omp cancel cancel-clause[optseq] new-line */
35193
35194 #define OMP_CANCEL_CLAUSE_MASK \
35195 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35200
35201 static void
35202 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35203 {
35204 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35205 "#pragma omp cancel", pragma_tok);
35206 finish_omp_cancel (clauses);
35207 }
35208
35209 /* OpenMP 4.0:
35210 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35211
35212 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35217
35218 static void
35219 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35220 enum pragma_context context)
35221 {
35222 tree clauses;
35223 bool point_seen = false;
35224
35225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35226 {
35227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35228 const char *p = IDENTIFIER_POINTER (id);
35229
35230 if (strcmp (p, "point") == 0)
35231 {
35232 cp_lexer_consume_token (parser->lexer);
35233 point_seen = true;
35234 }
35235 }
35236 if (!point_seen)
35237 {
35238 cp_parser_error (parser, "expected %<point%>");
35239 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35240 return;
35241 }
35242
35243 if (context != pragma_compound)
35244 {
35245 if (context == pragma_stmt)
35246 error_at (pragma_tok->location,
35247 "%<#pragma omp cancellation point%> may only be used in"
35248 " compound statements");
35249 else
35250 cp_parser_error (parser, "expected declaration specifiers");
35251 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35252 return;
35253 }
35254
35255 clauses = cp_parser_omp_all_clauses (parser,
35256 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35257 "#pragma omp cancellation point",
35258 pragma_tok);
35259 finish_omp_cancellation_point (clauses);
35260 }
35261
35262 /* OpenMP 4.0:
35263 #pragma omp distribute distribute-clause[optseq] new-line
35264 for-loop */
35265
35266 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35272
35273 static tree
35274 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35275 char *p_name, omp_clause_mask mask, tree *cclauses,
35276 bool *if_p)
35277 {
35278 tree clauses, sb, ret;
35279 unsigned int save;
35280 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35281
35282 strcat (p_name, " distribute");
35283 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35284
35285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35286 {
35287 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35288 const char *p = IDENTIFIER_POINTER (id);
35289 bool simd = false;
35290 bool parallel = false;
35291
35292 if (strcmp (p, "simd") == 0)
35293 simd = true;
35294 else
35295 parallel = strcmp (p, "parallel") == 0;
35296 if (parallel || simd)
35297 {
35298 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35299 if (cclauses == NULL)
35300 cclauses = cclauses_buf;
35301 cp_lexer_consume_token (parser->lexer);
35302 if (!flag_openmp) /* flag_openmp_simd */
35303 {
35304 if (simd)
35305 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35306 cclauses, if_p);
35307 else
35308 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35309 cclauses, if_p);
35310 }
35311 sb = begin_omp_structured_block ();
35312 save = cp_parser_begin_omp_structured_block (parser);
35313 if (simd)
35314 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35315 cclauses, if_p);
35316 else
35317 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35318 cclauses, if_p);
35319 cp_parser_end_omp_structured_block (parser, save);
35320 tree body = finish_omp_structured_block (sb);
35321 if (ret == NULL)
35322 return ret;
35323 ret = make_node (OMP_DISTRIBUTE);
35324 TREE_TYPE (ret) = void_type_node;
35325 OMP_FOR_BODY (ret) = body;
35326 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35327 SET_EXPR_LOCATION (ret, loc);
35328 add_stmt (ret);
35329 return ret;
35330 }
35331 }
35332 if (!flag_openmp) /* flag_openmp_simd */
35333 {
35334 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35335 return NULL_TREE;
35336 }
35337
35338 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35339 cclauses == NULL);
35340 if (cclauses)
35341 {
35342 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35343 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35344 }
35345
35346 sb = begin_omp_structured_block ();
35347 save = cp_parser_begin_omp_structured_block (parser);
35348
35349 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35350
35351 cp_parser_end_omp_structured_block (parser, save);
35352 add_stmt (finish_omp_structured_block (sb));
35353
35354 return ret;
35355 }
35356
35357 /* OpenMP 4.0:
35358 # pragma omp teams teams-clause[optseq] new-line
35359 structured-block */
35360
35361 #define OMP_TEAMS_CLAUSE_MASK \
35362 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35369
35370 static tree
35371 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35372 char *p_name, omp_clause_mask mask, tree *cclauses,
35373 bool *if_p)
35374 {
35375 tree clauses, sb, ret;
35376 unsigned int save;
35377 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35378
35379 strcat (p_name, " teams");
35380 mask |= OMP_TEAMS_CLAUSE_MASK;
35381
35382 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35383 {
35384 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35385 const char *p = IDENTIFIER_POINTER (id);
35386 if (strcmp (p, "distribute") == 0)
35387 {
35388 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35389 if (cclauses == NULL)
35390 cclauses = cclauses_buf;
35391
35392 cp_lexer_consume_token (parser->lexer);
35393 if (!flag_openmp) /* flag_openmp_simd */
35394 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35395 cclauses, if_p);
35396 sb = begin_omp_structured_block ();
35397 save = cp_parser_begin_omp_structured_block (parser);
35398 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35399 cclauses, if_p);
35400 cp_parser_end_omp_structured_block (parser, save);
35401 tree body = finish_omp_structured_block (sb);
35402 if (ret == NULL)
35403 return ret;
35404 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35405 ret = make_node (OMP_TEAMS);
35406 TREE_TYPE (ret) = void_type_node;
35407 OMP_TEAMS_CLAUSES (ret) = clauses;
35408 OMP_TEAMS_BODY (ret) = body;
35409 OMP_TEAMS_COMBINED (ret) = 1;
35410 return add_stmt (ret);
35411 }
35412 }
35413 if (!flag_openmp) /* flag_openmp_simd */
35414 {
35415 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35416 return NULL_TREE;
35417 }
35418
35419 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35420 cclauses == NULL);
35421 if (cclauses)
35422 {
35423 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35424 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35425 }
35426
35427 tree stmt = make_node (OMP_TEAMS);
35428 TREE_TYPE (stmt) = void_type_node;
35429 OMP_TEAMS_CLAUSES (stmt) = clauses;
35430 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35431
35432 return add_stmt (stmt);
35433 }
35434
35435 /* OpenMP 4.0:
35436 # pragma omp target data target-data-clause[optseq] new-line
35437 structured-block */
35438
35439 #define OMP_TARGET_DATA_CLAUSE_MASK \
35440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35444
35445 static tree
35446 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35447 {
35448 tree clauses
35449 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35450 "#pragma omp target data", pragma_tok);
35451 int map_seen = 0;
35452 for (tree *pc = &clauses; *pc;)
35453 {
35454 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35455 switch (OMP_CLAUSE_MAP_KIND (*pc))
35456 {
35457 case GOMP_MAP_TO:
35458 case GOMP_MAP_ALWAYS_TO:
35459 case GOMP_MAP_FROM:
35460 case GOMP_MAP_ALWAYS_FROM:
35461 case GOMP_MAP_TOFROM:
35462 case GOMP_MAP_ALWAYS_TOFROM:
35463 case GOMP_MAP_ALLOC:
35464 map_seen = 3;
35465 break;
35466 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35467 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35468 case GOMP_MAP_ALWAYS_POINTER:
35469 break;
35470 default:
35471 map_seen |= 1;
35472 error_at (OMP_CLAUSE_LOCATION (*pc),
35473 "%<#pragma omp target data%> with map-type other "
35474 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35475 "on %<map%> clause");
35476 *pc = OMP_CLAUSE_CHAIN (*pc);
35477 continue;
35478 }
35479 pc = &OMP_CLAUSE_CHAIN (*pc);
35480 }
35481
35482 if (map_seen != 3)
35483 {
35484 if (map_seen == 0)
35485 error_at (pragma_tok->location,
35486 "%<#pragma omp target data%> must contain at least "
35487 "one %<map%> clause");
35488 return NULL_TREE;
35489 }
35490
35491 tree stmt = make_node (OMP_TARGET_DATA);
35492 TREE_TYPE (stmt) = void_type_node;
35493 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35494
35495 keep_next_level (true);
35496 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35497
35498 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35499 return add_stmt (stmt);
35500 }
35501
35502 /* OpenMP 4.5:
35503 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35504 structured-block */
35505
35506 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35507 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35512
35513 static tree
35514 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35515 enum pragma_context context)
35516 {
35517 bool data_seen = false;
35518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35519 {
35520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35521 const char *p = IDENTIFIER_POINTER (id);
35522
35523 if (strcmp (p, "data") == 0)
35524 {
35525 cp_lexer_consume_token (parser->lexer);
35526 data_seen = true;
35527 }
35528 }
35529 if (!data_seen)
35530 {
35531 cp_parser_error (parser, "expected %<data%>");
35532 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35533 return NULL_TREE;
35534 }
35535
35536 if (context == pragma_stmt)
35537 {
35538 error_at (pragma_tok->location,
35539 "%<#pragma omp target enter data%> may only be "
35540 "used in compound statements");
35541 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35542 return NULL_TREE;
35543 }
35544
35545 tree clauses
35546 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35547 "#pragma omp target enter data", pragma_tok);
35548 int map_seen = 0;
35549 for (tree *pc = &clauses; *pc;)
35550 {
35551 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35552 switch (OMP_CLAUSE_MAP_KIND (*pc))
35553 {
35554 case GOMP_MAP_TO:
35555 case GOMP_MAP_ALWAYS_TO:
35556 case GOMP_MAP_ALLOC:
35557 map_seen = 3;
35558 break;
35559 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35560 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35561 case GOMP_MAP_ALWAYS_POINTER:
35562 break;
35563 default:
35564 map_seen |= 1;
35565 error_at (OMP_CLAUSE_LOCATION (*pc),
35566 "%<#pragma omp target enter data%> with map-type other "
35567 "than %<to%> or %<alloc%> on %<map%> clause");
35568 *pc = OMP_CLAUSE_CHAIN (*pc);
35569 continue;
35570 }
35571 pc = &OMP_CLAUSE_CHAIN (*pc);
35572 }
35573
35574 if (map_seen != 3)
35575 {
35576 if (map_seen == 0)
35577 error_at (pragma_tok->location,
35578 "%<#pragma omp target enter data%> must contain at least "
35579 "one %<map%> clause");
35580 return NULL_TREE;
35581 }
35582
35583 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35584 TREE_TYPE (stmt) = void_type_node;
35585 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35586 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35587 return add_stmt (stmt);
35588 }
35589
35590 /* OpenMP 4.5:
35591 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35592 structured-block */
35593
35594 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35595 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35600
35601 static tree
35602 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35603 enum pragma_context context)
35604 {
35605 bool data_seen = false;
35606 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35607 {
35608 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35609 const char *p = IDENTIFIER_POINTER (id);
35610
35611 if (strcmp (p, "data") == 0)
35612 {
35613 cp_lexer_consume_token (parser->lexer);
35614 data_seen = true;
35615 }
35616 }
35617 if (!data_seen)
35618 {
35619 cp_parser_error (parser, "expected %<data%>");
35620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35621 return NULL_TREE;
35622 }
35623
35624 if (context == pragma_stmt)
35625 {
35626 error_at (pragma_tok->location,
35627 "%<#pragma omp target exit data%> may only be "
35628 "used in compound statements");
35629 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35630 return NULL_TREE;
35631 }
35632
35633 tree clauses
35634 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35635 "#pragma omp target exit data", pragma_tok);
35636 int map_seen = 0;
35637 for (tree *pc = &clauses; *pc;)
35638 {
35639 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35640 switch (OMP_CLAUSE_MAP_KIND (*pc))
35641 {
35642 case GOMP_MAP_FROM:
35643 case GOMP_MAP_ALWAYS_FROM:
35644 case GOMP_MAP_RELEASE:
35645 case GOMP_MAP_DELETE:
35646 map_seen = 3;
35647 break;
35648 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35649 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35650 case GOMP_MAP_ALWAYS_POINTER:
35651 break;
35652 default:
35653 map_seen |= 1;
35654 error_at (OMP_CLAUSE_LOCATION (*pc),
35655 "%<#pragma omp target exit data%> with map-type other "
35656 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35657 " clause");
35658 *pc = OMP_CLAUSE_CHAIN (*pc);
35659 continue;
35660 }
35661 pc = &OMP_CLAUSE_CHAIN (*pc);
35662 }
35663
35664 if (map_seen != 3)
35665 {
35666 if (map_seen == 0)
35667 error_at (pragma_tok->location,
35668 "%<#pragma omp target exit data%> must contain at least "
35669 "one %<map%> clause");
35670 return NULL_TREE;
35671 }
35672
35673 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35674 TREE_TYPE (stmt) = void_type_node;
35675 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35676 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35677 return add_stmt (stmt);
35678 }
35679
35680 /* OpenMP 4.0:
35681 # pragma omp target update target-update-clause[optseq] new-line */
35682
35683 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35684 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35690
35691 static bool
35692 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35693 enum pragma_context context)
35694 {
35695 if (context == pragma_stmt)
35696 {
35697 error_at (pragma_tok->location,
35698 "%<#pragma omp target update%> may only be "
35699 "used in compound statements");
35700 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35701 return false;
35702 }
35703
35704 tree clauses
35705 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35706 "#pragma omp target update", pragma_tok);
35707 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35708 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35709 {
35710 error_at (pragma_tok->location,
35711 "%<#pragma omp target update%> must contain at least one "
35712 "%<from%> or %<to%> clauses");
35713 return false;
35714 }
35715
35716 tree stmt = make_node (OMP_TARGET_UPDATE);
35717 TREE_TYPE (stmt) = void_type_node;
35718 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35719 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35720 add_stmt (stmt);
35721 return false;
35722 }
35723
35724 /* OpenMP 4.0:
35725 # pragma omp target target-clause[optseq] new-line
35726 structured-block */
35727
35728 #define OMP_TARGET_CLAUSE_MASK \
35729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35738
35739 static bool
35740 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35741 enum pragma_context context, bool *if_p)
35742 {
35743 tree *pc = NULL, stmt;
35744
35745 if (context != pragma_stmt && context != pragma_compound)
35746 {
35747 cp_parser_error (parser, "expected declaration specifiers");
35748 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35749 return false;
35750 }
35751
35752 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35753 {
35754 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35755 const char *p = IDENTIFIER_POINTER (id);
35756 enum tree_code ccode = ERROR_MARK;
35757
35758 if (strcmp (p, "teams") == 0)
35759 ccode = OMP_TEAMS;
35760 else if (strcmp (p, "parallel") == 0)
35761 ccode = OMP_PARALLEL;
35762 else if (strcmp (p, "simd") == 0)
35763 ccode = OMP_SIMD;
35764 if (ccode != ERROR_MARK)
35765 {
35766 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35767 char p_name[sizeof ("#pragma omp target teams distribute "
35768 "parallel for simd")];
35769
35770 cp_lexer_consume_token (parser->lexer);
35771 strcpy (p_name, "#pragma omp target");
35772 if (!flag_openmp) /* flag_openmp_simd */
35773 {
35774 tree stmt;
35775 switch (ccode)
35776 {
35777 case OMP_TEAMS:
35778 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35779 OMP_TARGET_CLAUSE_MASK,
35780 cclauses, if_p);
35781 break;
35782 case OMP_PARALLEL:
35783 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35784 OMP_TARGET_CLAUSE_MASK,
35785 cclauses, if_p);
35786 break;
35787 case OMP_SIMD:
35788 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35789 OMP_TARGET_CLAUSE_MASK,
35790 cclauses, if_p);
35791 break;
35792 default:
35793 gcc_unreachable ();
35794 }
35795 return stmt != NULL_TREE;
35796 }
35797 keep_next_level (true);
35798 tree sb = begin_omp_structured_block (), ret;
35799 unsigned save = cp_parser_begin_omp_structured_block (parser);
35800 switch (ccode)
35801 {
35802 case OMP_TEAMS:
35803 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35804 OMP_TARGET_CLAUSE_MASK, cclauses,
35805 if_p);
35806 break;
35807 case OMP_PARALLEL:
35808 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35809 OMP_TARGET_CLAUSE_MASK, cclauses,
35810 if_p);
35811 break;
35812 case OMP_SIMD:
35813 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35814 OMP_TARGET_CLAUSE_MASK, cclauses,
35815 if_p);
35816 break;
35817 default:
35818 gcc_unreachable ();
35819 }
35820 cp_parser_end_omp_structured_block (parser, save);
35821 tree body = finish_omp_structured_block (sb);
35822 if (ret == NULL_TREE)
35823 return false;
35824 if (ccode == OMP_TEAMS && !processing_template_decl)
35825 {
35826 /* For combined target teams, ensure the num_teams and
35827 thread_limit clause expressions are evaluated on the host,
35828 before entering the target construct. */
35829 tree c;
35830 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35831 c; c = OMP_CLAUSE_CHAIN (c))
35832 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35833 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35834 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35835 {
35836 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35837 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35838 if (expr == error_mark_node)
35839 continue;
35840 tree tmp = TARGET_EXPR_SLOT (expr);
35841 add_stmt (expr);
35842 OMP_CLAUSE_OPERAND (c, 0) = expr;
35843 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35844 OMP_CLAUSE_FIRSTPRIVATE);
35845 OMP_CLAUSE_DECL (tc) = tmp;
35846 OMP_CLAUSE_CHAIN (tc)
35847 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35848 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35849 }
35850 }
35851 tree stmt = make_node (OMP_TARGET);
35852 TREE_TYPE (stmt) = void_type_node;
35853 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35854 OMP_TARGET_BODY (stmt) = body;
35855 OMP_TARGET_COMBINED (stmt) = 1;
35856 add_stmt (stmt);
35857 pc = &OMP_TARGET_CLAUSES (stmt);
35858 goto check_clauses;
35859 }
35860 else if (!flag_openmp) /* flag_openmp_simd */
35861 {
35862 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35863 return false;
35864 }
35865 else if (strcmp (p, "data") == 0)
35866 {
35867 cp_lexer_consume_token (parser->lexer);
35868 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35869 return true;
35870 }
35871 else if (strcmp (p, "enter") == 0)
35872 {
35873 cp_lexer_consume_token (parser->lexer);
35874 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35875 return false;
35876 }
35877 else if (strcmp (p, "exit") == 0)
35878 {
35879 cp_lexer_consume_token (parser->lexer);
35880 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35881 return false;
35882 }
35883 else if (strcmp (p, "update") == 0)
35884 {
35885 cp_lexer_consume_token (parser->lexer);
35886 return cp_parser_omp_target_update (parser, pragma_tok, context);
35887 }
35888 }
35889
35890 stmt = make_node (OMP_TARGET);
35891 TREE_TYPE (stmt) = void_type_node;
35892
35893 OMP_TARGET_CLAUSES (stmt)
35894 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35895 "#pragma omp target", pragma_tok);
35896 pc = &OMP_TARGET_CLAUSES (stmt);
35897 keep_next_level (true);
35898 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35899
35900 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35901 add_stmt (stmt);
35902
35903 check_clauses:
35904 while (*pc)
35905 {
35906 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35907 switch (OMP_CLAUSE_MAP_KIND (*pc))
35908 {
35909 case GOMP_MAP_TO:
35910 case GOMP_MAP_ALWAYS_TO:
35911 case GOMP_MAP_FROM:
35912 case GOMP_MAP_ALWAYS_FROM:
35913 case GOMP_MAP_TOFROM:
35914 case GOMP_MAP_ALWAYS_TOFROM:
35915 case GOMP_MAP_ALLOC:
35916 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35917 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35918 case GOMP_MAP_ALWAYS_POINTER:
35919 break;
35920 default:
35921 error_at (OMP_CLAUSE_LOCATION (*pc),
35922 "%<#pragma omp target%> with map-type other "
35923 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35924 "on %<map%> clause");
35925 *pc = OMP_CLAUSE_CHAIN (*pc);
35926 continue;
35927 }
35928 pc = &OMP_CLAUSE_CHAIN (*pc);
35929 }
35930 return true;
35931 }
35932
35933 /* OpenACC 2.0:
35934 # pragma acc cache (variable-list) new-line
35935 */
35936
35937 static tree
35938 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35939 {
35940 tree stmt, clauses;
35941
35942 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35943 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35944
35945 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35946
35947 stmt = make_node (OACC_CACHE);
35948 TREE_TYPE (stmt) = void_type_node;
35949 OACC_CACHE_CLAUSES (stmt) = clauses;
35950 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35951 add_stmt (stmt);
35952
35953 return stmt;
35954 }
35955
35956 /* OpenACC 2.0:
35957 # pragma acc data oacc-data-clause[optseq] new-line
35958 structured-block */
35959
35960 #define OACC_DATA_CLAUSE_MASK \
35961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35972
35973 static tree
35974 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35975 {
35976 tree stmt, clauses, block;
35977 unsigned int save;
35978
35979 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35980 "#pragma acc data", pragma_tok);
35981
35982 block = begin_omp_parallel ();
35983 save = cp_parser_begin_omp_structured_block (parser);
35984 cp_parser_statement (parser, NULL_TREE, false, if_p);
35985 cp_parser_end_omp_structured_block (parser, save);
35986 stmt = finish_oacc_data (clauses, block);
35987 return stmt;
35988 }
35989
35990 /* OpenACC 2.0:
35991 # pragma acc host_data <clauses> new-line
35992 structured-block */
35993
35994 #define OACC_HOST_DATA_CLAUSE_MASK \
35995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35996
35997 static tree
35998 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35999 {
36000 tree stmt, clauses, block;
36001 unsigned int save;
36002
36003 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36004 "#pragma acc host_data", pragma_tok);
36005
36006 block = begin_omp_parallel ();
36007 save = cp_parser_begin_omp_structured_block (parser);
36008 cp_parser_statement (parser, NULL_TREE, false, if_p);
36009 cp_parser_end_omp_structured_block (parser, save);
36010 stmt = finish_oacc_host_data (clauses, block);
36011 return stmt;
36012 }
36013
36014 /* OpenACC 2.0:
36015 # pragma acc declare oacc-data-clause[optseq] new-line
36016 */
36017
36018 #define OACC_DECLARE_CLAUSE_MASK \
36019 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36031
36032 static tree
36033 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36034 {
36035 tree clauses, stmt;
36036 bool error = false;
36037
36038 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36039 "#pragma acc declare", pragma_tok, true);
36040
36041
36042 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36043 {
36044 error_at (pragma_tok->location,
36045 "no valid clauses specified in %<#pragma acc declare%>");
36046 return NULL_TREE;
36047 }
36048
36049 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36050 {
36051 location_t loc = OMP_CLAUSE_LOCATION (t);
36052 tree decl = OMP_CLAUSE_DECL (t);
36053 if (!DECL_P (decl))
36054 {
36055 error_at (loc, "array section in %<#pragma acc declare%>");
36056 error = true;
36057 continue;
36058 }
36059 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36060 switch (OMP_CLAUSE_MAP_KIND (t))
36061 {
36062 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36063 case GOMP_MAP_FORCE_ALLOC:
36064 case GOMP_MAP_FORCE_TO:
36065 case GOMP_MAP_FORCE_DEVICEPTR:
36066 case GOMP_MAP_DEVICE_RESIDENT:
36067 break;
36068
36069 case GOMP_MAP_LINK:
36070 if (!global_bindings_p ()
36071 && (TREE_STATIC (decl)
36072 || !DECL_EXTERNAL (decl)))
36073 {
36074 error_at (loc,
36075 "%qD must be a global variable in"
36076 "%<#pragma acc declare link%>",
36077 decl);
36078 error = true;
36079 continue;
36080 }
36081 break;
36082
36083 default:
36084 if (global_bindings_p ())
36085 {
36086 error_at (loc, "invalid OpenACC clause at file scope");
36087 error = true;
36088 continue;
36089 }
36090 if (DECL_EXTERNAL (decl))
36091 {
36092 error_at (loc,
36093 "invalid use of %<extern%> variable %qD "
36094 "in %<#pragma acc declare%>", decl);
36095 error = true;
36096 continue;
36097 }
36098 else if (TREE_PUBLIC (decl))
36099 {
36100 error_at (loc,
36101 "invalid use of %<global%> variable %qD "
36102 "in %<#pragma acc declare%>", decl);
36103 error = true;
36104 continue;
36105 }
36106 break;
36107 }
36108
36109 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36110 || lookup_attribute ("omp declare target link",
36111 DECL_ATTRIBUTES (decl)))
36112 {
36113 error_at (loc, "variable %qD used more than once with "
36114 "%<#pragma acc declare%>", decl);
36115 error = true;
36116 continue;
36117 }
36118
36119 if (!error)
36120 {
36121 tree id;
36122
36123 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36124 id = get_identifier ("omp declare target link");
36125 else
36126 id = get_identifier ("omp declare target");
36127
36128 DECL_ATTRIBUTES (decl)
36129 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36130 if (global_bindings_p ())
36131 {
36132 symtab_node *node = symtab_node::get (decl);
36133 if (node != NULL)
36134 {
36135 node->offloadable = 1;
36136 if (ENABLE_OFFLOADING)
36137 {
36138 g->have_offload = true;
36139 if (is_a <varpool_node *> (node))
36140 vec_safe_push (offload_vars, decl);
36141 }
36142 }
36143 }
36144 }
36145 }
36146
36147 if (error || global_bindings_p ())
36148 return NULL_TREE;
36149
36150 stmt = make_node (OACC_DECLARE);
36151 TREE_TYPE (stmt) = void_type_node;
36152 OACC_DECLARE_CLAUSES (stmt) = clauses;
36153 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36154
36155 add_stmt (stmt);
36156
36157 return NULL_TREE;
36158 }
36159
36160 /* OpenACC 2.0:
36161 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36162
36163 or
36164
36165 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36166
36167 LOC is the location of the #pragma token.
36168 */
36169
36170 #define OACC_ENTER_DATA_CLAUSE_MASK \
36171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36178
36179 #define OACC_EXIT_DATA_CLAUSE_MASK \
36180 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36185
36186 static tree
36187 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36188 bool enter)
36189 {
36190 location_t loc = pragma_tok->location;
36191 tree stmt, clauses;
36192 const char *p = "";
36193
36194 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36195 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36196
36197 if (strcmp (p, "data") != 0)
36198 {
36199 error_at (loc, enter
36200 ? "expected %<data%> after %<#pragma acc enter%>"
36201 : "expected %<data%> after %<#pragma acc exit%>");
36202 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36203 return NULL_TREE;
36204 }
36205
36206 cp_lexer_consume_token (parser->lexer);
36207
36208 if (enter)
36209 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36210 "#pragma acc enter data", pragma_tok);
36211 else
36212 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36213 "#pragma acc exit data", pragma_tok);
36214
36215 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36216 {
36217 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36218 enter ? "enter" : "exit");
36219 return NULL_TREE;
36220 }
36221
36222 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36223 TREE_TYPE (stmt) = void_type_node;
36224 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36225 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36226 add_stmt (stmt);
36227 return stmt;
36228 }
36229
36230 /* OpenACC 2.0:
36231 # pragma acc loop oacc-loop-clause[optseq] new-line
36232 structured-block */
36233
36234 #define OACC_LOOP_CLAUSE_MASK \
36235 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36245
36246 static tree
36247 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36248 omp_clause_mask mask, tree *cclauses, bool *if_p)
36249 {
36250 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36251
36252 strcat (p_name, " loop");
36253 mask |= OACC_LOOP_CLAUSE_MASK;
36254
36255 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36256 cclauses == NULL);
36257 if (cclauses)
36258 {
36259 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36260 if (*cclauses)
36261 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36262 if (clauses)
36263 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36264 }
36265
36266 tree block = begin_omp_structured_block ();
36267 int save = cp_parser_begin_omp_structured_block (parser);
36268 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36269 cp_parser_end_omp_structured_block (parser, save);
36270 add_stmt (finish_omp_structured_block (block));
36271
36272 return stmt;
36273 }
36274
36275 /* OpenACC 2.0:
36276 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36277 structured-block
36278
36279 or
36280
36281 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36282 structured-block
36283 */
36284
36285 #define OACC_KERNELS_CLAUSE_MASK \
36286 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36300
36301 #define OACC_PARALLEL_CLAUSE_MASK \
36302 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36322
36323 static tree
36324 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36325 char *p_name, bool *if_p)
36326 {
36327 omp_clause_mask mask;
36328 enum tree_code code;
36329 switch (cp_parser_pragma_kind (pragma_tok))
36330 {
36331 case PRAGMA_OACC_KERNELS:
36332 strcat (p_name, " kernels");
36333 mask = OACC_KERNELS_CLAUSE_MASK;
36334 code = OACC_KERNELS;
36335 break;
36336 case PRAGMA_OACC_PARALLEL:
36337 strcat (p_name, " parallel");
36338 mask = OACC_PARALLEL_CLAUSE_MASK;
36339 code = OACC_PARALLEL;
36340 break;
36341 default:
36342 gcc_unreachable ();
36343 }
36344
36345 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36346 {
36347 const char *p
36348 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36349 if (strcmp (p, "loop") == 0)
36350 {
36351 cp_lexer_consume_token (parser->lexer);
36352 tree block = begin_omp_parallel ();
36353 tree clauses;
36354 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36355 if_p);
36356 return finish_omp_construct (code, block, clauses);
36357 }
36358 }
36359
36360 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36361
36362 tree block = begin_omp_parallel ();
36363 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36364 cp_parser_statement (parser, NULL_TREE, false, if_p);
36365 cp_parser_end_omp_structured_block (parser, save);
36366 return finish_omp_construct (code, block, clauses);
36367 }
36368
36369 /* OpenACC 2.0:
36370 # pragma acc update oacc-update-clause[optseq] new-line
36371 */
36372
36373 #define OACC_UPDATE_CLAUSE_MASK \
36374 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36380
36381 static tree
36382 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36383 {
36384 tree stmt, clauses;
36385
36386 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36387 "#pragma acc update", pragma_tok);
36388
36389 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36390 {
36391 error_at (pragma_tok->location,
36392 "%<#pragma acc update%> must contain at least one "
36393 "%<device%> or %<host%> or %<self%> clause");
36394 return NULL_TREE;
36395 }
36396
36397 stmt = make_node (OACC_UPDATE);
36398 TREE_TYPE (stmt) = void_type_node;
36399 OACC_UPDATE_CLAUSES (stmt) = clauses;
36400 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36401 add_stmt (stmt);
36402 return stmt;
36403 }
36404
36405 /* OpenACC 2.0:
36406 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36407
36408 LOC is the location of the #pragma token.
36409 */
36410
36411 #define OACC_WAIT_CLAUSE_MASK \
36412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36413
36414 static tree
36415 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36416 {
36417 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36419
36420 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36421 list = cp_parser_oacc_wait_list (parser, loc, list);
36422
36423 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36424 "#pragma acc wait", pragma_tok);
36425
36426 stmt = c_finish_oacc_wait (loc, list, clauses);
36427 stmt = finish_expr_stmt (stmt);
36428
36429 return stmt;
36430 }
36431
36432 /* OpenMP 4.0:
36433 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36434
36435 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36436 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36442
36443 static void
36444 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36445 enum pragma_context context)
36446 {
36447 bool first_p = parser->omp_declare_simd == NULL;
36448 cp_omp_declare_simd_data data;
36449 if (first_p)
36450 {
36451 data.error_seen = false;
36452 data.fndecl_seen = false;
36453 data.tokens = vNULL;
36454 data.clauses = NULL_TREE;
36455 /* It is safe to take the address of a local variable; it will only be
36456 used while this scope is live. */
36457 parser->omp_declare_simd = &data;
36458 }
36459
36460 /* Store away all pragma tokens. */
36461 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36462 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36463 cp_lexer_consume_token (parser->lexer);
36464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36465 parser->omp_declare_simd->error_seen = true;
36466 cp_parser_require_pragma_eol (parser, pragma_tok);
36467 struct cp_token_cache *cp
36468 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36469 parser->omp_declare_simd->tokens.safe_push (cp);
36470
36471 if (first_p)
36472 {
36473 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36474 cp_parser_pragma (parser, context, NULL);
36475 switch (context)
36476 {
36477 case pragma_external:
36478 cp_parser_declaration (parser);
36479 break;
36480 case pragma_member:
36481 cp_parser_member_declaration (parser);
36482 break;
36483 case pragma_objc_icode:
36484 cp_parser_block_declaration (parser, /*statement_p=*/false);
36485 break;
36486 default:
36487 cp_parser_declaration_statement (parser);
36488 break;
36489 }
36490 if (parser->omp_declare_simd
36491 && !parser->omp_declare_simd->error_seen
36492 && !parser->omp_declare_simd->fndecl_seen)
36493 error_at (pragma_tok->location,
36494 "%<#pragma omp declare simd%> not immediately followed by "
36495 "function declaration or definition");
36496 data.tokens.release ();
36497 parser->omp_declare_simd = NULL;
36498 }
36499 }
36500
36501 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36502 This function is modelled similar to the late parsing of omp declare
36503 simd. */
36504
36505 static tree
36506 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36507 {
36508 struct cp_token_cache *ce;
36509 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36510 int ii = 0;
36511
36512 if (parser->omp_declare_simd != NULL
36513 || lookup_attribute ("simd", attrs))
36514 {
36515 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36516 "used in the same function marked as a Cilk Plus SIMD-enabled "
36517 "function");
36518 parser->cilk_simd_fn_info->tokens.release ();
36519 XDELETE (parser->cilk_simd_fn_info);
36520 parser->cilk_simd_fn_info = NULL;
36521 return attrs;
36522 }
36523 if (!info->error_seen && info->fndecl_seen)
36524 {
36525 error ("vector attribute not immediately followed by a single function"
36526 " declaration or definition");
36527 info->error_seen = true;
36528 }
36529 if (info->error_seen)
36530 return attrs;
36531
36532 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36533 {
36534 tree c, cl;
36535
36536 cp_parser_push_lexer_for_tokens (parser, ce);
36537 parser->lexer->in_pragma = true;
36538 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36539 "SIMD-enabled functions attribute",
36540 NULL);
36541 cp_parser_pop_lexer (parser);
36542 if (cl)
36543 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36544
36545 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36546 TREE_CHAIN (c) = attrs;
36547 attrs = c;
36548
36549 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36550 TREE_CHAIN (c) = attrs;
36551 if (processing_template_decl)
36552 ATTR_IS_DEPENDENT (c) = 1;
36553 attrs = c;
36554 }
36555 info->fndecl_seen = true;
36556 parser->cilk_simd_fn_info->tokens.release ();
36557 XDELETE (parser->cilk_simd_fn_info);
36558 parser->cilk_simd_fn_info = NULL;
36559 return attrs;
36560 }
36561
36562 /* Finalize #pragma omp declare simd clauses after direct declarator has
36563 been parsed, and put that into "omp declare simd" attribute. */
36564
36565 static tree
36566 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36567 {
36568 struct cp_token_cache *ce;
36569 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36570 int i;
36571
36572 if (!data->error_seen && data->fndecl_seen)
36573 {
36574 error ("%<#pragma omp declare simd%> not immediately followed by "
36575 "a single function declaration or definition");
36576 data->error_seen = true;
36577 }
36578 if (data->error_seen)
36579 return attrs;
36580
36581 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36582 {
36583 tree c, cl;
36584
36585 cp_parser_push_lexer_for_tokens (parser, ce);
36586 parser->lexer->in_pragma = true;
36587 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36588 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36589 cp_lexer_consume_token (parser->lexer);
36590 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36591 "#pragma omp declare simd", pragma_tok);
36592 cp_parser_pop_lexer (parser);
36593 if (cl)
36594 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36595 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36596 TREE_CHAIN (c) = attrs;
36597 if (processing_template_decl)
36598 ATTR_IS_DEPENDENT (c) = 1;
36599 attrs = c;
36600 }
36601
36602 data->fndecl_seen = true;
36603 return attrs;
36604 }
36605
36606
36607 /* OpenMP 4.0:
36608 # pragma omp declare target new-line
36609 declarations and definitions
36610 # pragma omp end declare target new-line
36611
36612 OpenMP 4.5:
36613 # pragma omp declare target ( extended-list ) new-line
36614
36615 # pragma omp declare target declare-target-clauses[seq] new-line */
36616
36617 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36618 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36620
36621 static void
36622 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36623 {
36624 tree clauses = NULL_TREE;
36625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36626 clauses
36627 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36628 "#pragma omp declare target", pragma_tok);
36629 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36630 {
36631 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36632 clauses);
36633 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36634 cp_parser_require_pragma_eol (parser, pragma_tok);
36635 }
36636 else
36637 {
36638 cp_parser_require_pragma_eol (parser, pragma_tok);
36639 scope_chain->omp_declare_target_attribute++;
36640 return;
36641 }
36642 if (scope_chain->omp_declare_target_attribute)
36643 error_at (pragma_tok->location,
36644 "%<#pragma omp declare target%> with clauses in between "
36645 "%<#pragma omp declare target%> without clauses and "
36646 "%<#pragma omp end declare target%>");
36647 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36648 {
36649 tree t = OMP_CLAUSE_DECL (c), id;
36650 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36651 tree at2 = lookup_attribute ("omp declare target link",
36652 DECL_ATTRIBUTES (t));
36653 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36654 {
36655 id = get_identifier ("omp declare target link");
36656 std::swap (at1, at2);
36657 }
36658 else
36659 id = get_identifier ("omp declare target");
36660 if (at2)
36661 {
36662 error_at (OMP_CLAUSE_LOCATION (c),
36663 "%qD specified both in declare target %<link%> and %<to%>"
36664 " clauses", t);
36665 continue;
36666 }
36667 if (!at1)
36668 {
36669 symtab_node *node = symtab_node::get (t);
36670 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36671 if (node != NULL)
36672 {
36673 node->offloadable = 1;
36674 if (ENABLE_OFFLOADING)
36675 {
36676 g->have_offload = true;
36677 if (is_a <varpool_node *> (node))
36678 vec_safe_push (offload_vars, t);
36679 }
36680 }
36681 }
36682 }
36683 }
36684
36685 static void
36686 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36687 {
36688 const char *p = "";
36689 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36690 {
36691 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36692 p = IDENTIFIER_POINTER (id);
36693 }
36694 if (strcmp (p, "declare") == 0)
36695 {
36696 cp_lexer_consume_token (parser->lexer);
36697 p = "";
36698 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36699 {
36700 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36701 p = IDENTIFIER_POINTER (id);
36702 }
36703 if (strcmp (p, "target") == 0)
36704 cp_lexer_consume_token (parser->lexer);
36705 else
36706 {
36707 cp_parser_error (parser, "expected %<target%>");
36708 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36709 return;
36710 }
36711 }
36712 else
36713 {
36714 cp_parser_error (parser, "expected %<declare%>");
36715 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36716 return;
36717 }
36718 cp_parser_require_pragma_eol (parser, pragma_tok);
36719 if (!scope_chain->omp_declare_target_attribute)
36720 error_at (pragma_tok->location,
36721 "%<#pragma omp end declare target%> without corresponding "
36722 "%<#pragma omp declare target%>");
36723 else
36724 scope_chain->omp_declare_target_attribute--;
36725 }
36726
36727 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36728 expression and optional initializer clause of
36729 #pragma omp declare reduction. We store the expression(s) as
36730 either 3, 6 or 7 special statements inside of the artificial function's
36731 body. The first two statements are DECL_EXPRs for the artificial
36732 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36733 expression that uses those variables.
36734 If there was any INITIALIZER clause, this is followed by further statements,
36735 the fourth and fifth statements are DECL_EXPRs for the artificial
36736 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36737 constructor variant (first token after open paren is not omp_priv),
36738 then the sixth statement is a statement with the function call expression
36739 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36740 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36741 to initialize the OMP_PRIV artificial variable and there is seventh
36742 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36743
36744 static bool
36745 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36746 {
36747 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36748 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36749 type = TREE_TYPE (type);
36750 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36751 DECL_ARTIFICIAL (omp_out) = 1;
36752 pushdecl (omp_out);
36753 add_decl_expr (omp_out);
36754 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36755 DECL_ARTIFICIAL (omp_in) = 1;
36756 pushdecl (omp_in);
36757 add_decl_expr (omp_in);
36758 tree combiner;
36759 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36760
36761 keep_next_level (true);
36762 tree block = begin_omp_structured_block ();
36763 combiner = cp_parser_expression (parser);
36764 finish_expr_stmt (combiner);
36765 block = finish_omp_structured_block (block);
36766 add_stmt (block);
36767
36768 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36769 return false;
36770
36771 const char *p = "";
36772 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36773 {
36774 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36775 p = IDENTIFIER_POINTER (id);
36776 }
36777
36778 if (strcmp (p, "initializer") == 0)
36779 {
36780 cp_lexer_consume_token (parser->lexer);
36781 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36782 return false;
36783
36784 p = "";
36785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36786 {
36787 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36788 p = IDENTIFIER_POINTER (id);
36789 }
36790
36791 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36792 DECL_ARTIFICIAL (omp_priv) = 1;
36793 pushdecl (omp_priv);
36794 add_decl_expr (omp_priv);
36795 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36796 DECL_ARTIFICIAL (omp_orig) = 1;
36797 pushdecl (omp_orig);
36798 add_decl_expr (omp_orig);
36799
36800 keep_next_level (true);
36801 block = begin_omp_structured_block ();
36802
36803 bool ctor = false;
36804 if (strcmp (p, "omp_priv") == 0)
36805 {
36806 bool is_direct_init, is_non_constant_init;
36807 ctor = true;
36808 cp_lexer_consume_token (parser->lexer);
36809 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36810 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36811 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36812 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36813 == CPP_CLOSE_PAREN
36814 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36815 == CPP_CLOSE_PAREN))
36816 {
36817 finish_omp_structured_block (block);
36818 error ("invalid initializer clause");
36819 return false;
36820 }
36821 initializer = cp_parser_initializer (parser, &is_direct_init,
36822 &is_non_constant_init);
36823 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36824 NULL_TREE, LOOKUP_ONLYCONVERTING);
36825 }
36826 else
36827 {
36828 cp_parser_parse_tentatively (parser);
36829 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36830 /*check_dependency_p=*/true,
36831 /*template_p=*/NULL,
36832 /*declarator_p=*/false,
36833 /*optional_p=*/false);
36834 vec<tree, va_gc> *args;
36835 if (fn_name == error_mark_node
36836 || cp_parser_error_occurred (parser)
36837 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36838 || ((args = cp_parser_parenthesized_expression_list
36839 (parser, non_attr, /*cast_p=*/false,
36840 /*allow_expansion_p=*/true,
36841 /*non_constant_p=*/NULL)),
36842 cp_parser_error_occurred (parser)))
36843 {
36844 finish_omp_structured_block (block);
36845 cp_parser_abort_tentative_parse (parser);
36846 cp_parser_error (parser, "expected id-expression (arguments)");
36847 return false;
36848 }
36849 unsigned int i;
36850 tree arg;
36851 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36852 if (arg == omp_priv
36853 || (TREE_CODE (arg) == ADDR_EXPR
36854 && TREE_OPERAND (arg, 0) == omp_priv))
36855 break;
36856 cp_parser_abort_tentative_parse (parser);
36857 if (arg == NULL_TREE)
36858 error ("one of the initializer call arguments should be %<omp_priv%>"
36859 " or %<&omp_priv%>");
36860 initializer = cp_parser_postfix_expression (parser, false, false, false,
36861 false, NULL);
36862 finish_expr_stmt (initializer);
36863 }
36864
36865 block = finish_omp_structured_block (block);
36866 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36867 add_stmt (block);
36868
36869 if (ctor)
36870 add_decl_expr (omp_orig);
36871
36872 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36873 return false;
36874 }
36875
36876 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36877 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36878
36879 return true;
36880 }
36881
36882 /* OpenMP 4.0
36883 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36884 initializer-clause[opt] new-line
36885
36886 initializer-clause:
36887 initializer (omp_priv initializer)
36888 initializer (function-name (argument-list)) */
36889
36890 static void
36891 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36892 enum pragma_context)
36893 {
36894 auto_vec<tree> types;
36895 enum tree_code reduc_code = ERROR_MARK;
36896 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36897 unsigned int i;
36898 cp_token *first_token;
36899 cp_token_cache *cp;
36900 int errs;
36901 void *p;
36902
36903 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36904 p = obstack_alloc (&declarator_obstack, 0);
36905
36906 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36907 goto fail;
36908
36909 switch (cp_lexer_peek_token (parser->lexer)->type)
36910 {
36911 case CPP_PLUS:
36912 reduc_code = PLUS_EXPR;
36913 break;
36914 case CPP_MULT:
36915 reduc_code = MULT_EXPR;
36916 break;
36917 case CPP_MINUS:
36918 reduc_code = MINUS_EXPR;
36919 break;
36920 case CPP_AND:
36921 reduc_code = BIT_AND_EXPR;
36922 break;
36923 case CPP_XOR:
36924 reduc_code = BIT_XOR_EXPR;
36925 break;
36926 case CPP_OR:
36927 reduc_code = BIT_IOR_EXPR;
36928 break;
36929 case CPP_AND_AND:
36930 reduc_code = TRUTH_ANDIF_EXPR;
36931 break;
36932 case CPP_OR_OR:
36933 reduc_code = TRUTH_ORIF_EXPR;
36934 break;
36935 case CPP_NAME:
36936 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36937 break;
36938 default:
36939 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36940 "%<|%>, %<&&%>, %<||%> or identifier");
36941 goto fail;
36942 }
36943
36944 if (reduc_code != ERROR_MARK)
36945 cp_lexer_consume_token (parser->lexer);
36946
36947 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36948 if (reduc_id == error_mark_node)
36949 goto fail;
36950
36951 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36952 goto fail;
36953
36954 /* Types may not be defined in declare reduction type list. */
36955 const char *saved_message;
36956 saved_message = parser->type_definition_forbidden_message;
36957 parser->type_definition_forbidden_message
36958 = G_("types may not be defined in declare reduction type list");
36959 bool saved_colon_corrects_to_scope_p;
36960 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36961 parser->colon_corrects_to_scope_p = false;
36962 bool saved_colon_doesnt_start_class_def_p;
36963 saved_colon_doesnt_start_class_def_p
36964 = parser->colon_doesnt_start_class_def_p;
36965 parser->colon_doesnt_start_class_def_p = true;
36966
36967 while (true)
36968 {
36969 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36970 type = cp_parser_type_id (parser);
36971 if (type == error_mark_node)
36972 ;
36973 else if (ARITHMETIC_TYPE_P (type)
36974 && (orig_reduc_id == NULL_TREE
36975 || (TREE_CODE (type) != COMPLEX_TYPE
36976 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36977 "min") == 0
36978 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36979 "max") == 0))))
36980 error_at (loc, "predeclared arithmetic type %qT in "
36981 "%<#pragma omp declare reduction%>", type);
36982 else if (TREE_CODE (type) == FUNCTION_TYPE
36983 || TREE_CODE (type) == METHOD_TYPE
36984 || TREE_CODE (type) == ARRAY_TYPE)
36985 error_at (loc, "function or array type %qT in "
36986 "%<#pragma omp declare reduction%>", type);
36987 else if (TREE_CODE (type) == REFERENCE_TYPE)
36988 error_at (loc, "reference type %qT in "
36989 "%<#pragma omp declare reduction%>", type);
36990 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36991 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36992 "%<#pragma omp declare reduction%>", type);
36993 else
36994 types.safe_push (type);
36995
36996 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36997 cp_lexer_consume_token (parser->lexer);
36998 else
36999 break;
37000 }
37001
37002 /* Restore the saved message. */
37003 parser->type_definition_forbidden_message = saved_message;
37004 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37005 parser->colon_doesnt_start_class_def_p
37006 = saved_colon_doesnt_start_class_def_p;
37007
37008 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37009 || types.is_empty ())
37010 {
37011 fail:
37012 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37013 goto done;
37014 }
37015
37016 first_token = cp_lexer_peek_token (parser->lexer);
37017 cp = NULL;
37018 errs = errorcount;
37019 FOR_EACH_VEC_ELT (types, i, type)
37020 {
37021 tree fntype
37022 = build_function_type_list (void_type_node,
37023 cp_build_reference_type (type, false),
37024 NULL_TREE);
37025 tree this_reduc_id = reduc_id;
37026 if (!dependent_type_p (type))
37027 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37028 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37029 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37030 DECL_ARTIFICIAL (fndecl) = 1;
37031 DECL_EXTERNAL (fndecl) = 1;
37032 DECL_DECLARED_INLINE_P (fndecl) = 1;
37033 DECL_IGNORED_P (fndecl) = 1;
37034 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37035 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37036 DECL_ATTRIBUTES (fndecl)
37037 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37038 DECL_ATTRIBUTES (fndecl));
37039 if (processing_template_decl)
37040 fndecl = push_template_decl (fndecl);
37041 bool block_scope = false;
37042 tree block = NULL_TREE;
37043 if (current_function_decl)
37044 {
37045 block_scope = true;
37046 DECL_CONTEXT (fndecl) = global_namespace;
37047 if (!processing_template_decl)
37048 pushdecl (fndecl);
37049 }
37050 else if (current_class_type)
37051 {
37052 if (cp == NULL)
37053 {
37054 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37055 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37056 cp_lexer_consume_token (parser->lexer);
37057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37058 goto fail;
37059 cp = cp_token_cache_new (first_token,
37060 cp_lexer_peek_nth_token (parser->lexer,
37061 2));
37062 }
37063 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37064 finish_member_declaration (fndecl);
37065 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37066 DECL_PENDING_INLINE_P (fndecl) = 1;
37067 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37068 continue;
37069 }
37070 else
37071 {
37072 DECL_CONTEXT (fndecl) = current_namespace;
37073 pushdecl (fndecl);
37074 }
37075 if (!block_scope)
37076 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37077 else
37078 block = begin_omp_structured_block ();
37079 if (cp)
37080 {
37081 cp_parser_push_lexer_for_tokens (parser, cp);
37082 parser->lexer->in_pragma = true;
37083 }
37084 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37085 {
37086 if (!block_scope)
37087 finish_function (0);
37088 else
37089 DECL_CONTEXT (fndecl) = current_function_decl;
37090 if (cp)
37091 cp_parser_pop_lexer (parser);
37092 goto fail;
37093 }
37094 if (cp)
37095 cp_parser_pop_lexer (parser);
37096 if (!block_scope)
37097 finish_function (0);
37098 else
37099 {
37100 DECL_CONTEXT (fndecl) = current_function_decl;
37101 block = finish_omp_structured_block (block);
37102 if (TREE_CODE (block) == BIND_EXPR)
37103 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37104 else if (TREE_CODE (block) == STATEMENT_LIST)
37105 DECL_SAVED_TREE (fndecl) = block;
37106 if (processing_template_decl)
37107 add_decl_expr (fndecl);
37108 }
37109 cp_check_omp_declare_reduction (fndecl);
37110 if (cp == NULL && types.length () > 1)
37111 cp = cp_token_cache_new (first_token,
37112 cp_lexer_peek_nth_token (parser->lexer, 2));
37113 if (errs != errorcount)
37114 break;
37115 }
37116
37117 cp_parser_require_pragma_eol (parser, pragma_tok);
37118
37119 done:
37120 /* Free any declarators allocated. */
37121 obstack_free (&declarator_obstack, p);
37122 }
37123
37124 /* OpenMP 4.0
37125 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37126 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37127 initializer-clause[opt] new-line
37128 #pragma omp declare target new-line */
37129
37130 static void
37131 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37132 enum pragma_context context)
37133 {
37134 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37135 {
37136 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37137 const char *p = IDENTIFIER_POINTER (id);
37138
37139 if (strcmp (p, "simd") == 0)
37140 {
37141 cp_lexer_consume_token (parser->lexer);
37142 cp_parser_omp_declare_simd (parser, pragma_tok,
37143 context);
37144 return;
37145 }
37146 cp_ensure_no_omp_declare_simd (parser);
37147 if (strcmp (p, "reduction") == 0)
37148 {
37149 cp_lexer_consume_token (parser->lexer);
37150 cp_parser_omp_declare_reduction (parser, pragma_tok,
37151 context);
37152 return;
37153 }
37154 if (!flag_openmp) /* flag_openmp_simd */
37155 {
37156 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37157 return;
37158 }
37159 if (strcmp (p, "target") == 0)
37160 {
37161 cp_lexer_consume_token (parser->lexer);
37162 cp_parser_omp_declare_target (parser, pragma_tok);
37163 return;
37164 }
37165 }
37166 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37167 "or %<target%>");
37168 cp_parser_require_pragma_eol (parser, pragma_tok);
37169 }
37170
37171 /* OpenMP 4.5:
37172 #pragma omp taskloop taskloop-clause[optseq] new-line
37173 for-loop
37174
37175 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37176 for-loop */
37177
37178 #define OMP_TASKLOOP_CLAUSE_MASK \
37179 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37193
37194 static tree
37195 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37196 char *p_name, omp_clause_mask mask, tree *cclauses,
37197 bool *if_p)
37198 {
37199 tree clauses, sb, ret;
37200 unsigned int save;
37201 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37202
37203 strcat (p_name, " taskloop");
37204 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37205
37206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37207 {
37208 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37209 const char *p = IDENTIFIER_POINTER (id);
37210
37211 if (strcmp (p, "simd") == 0)
37212 {
37213 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37214 if (cclauses == NULL)
37215 cclauses = cclauses_buf;
37216
37217 cp_lexer_consume_token (parser->lexer);
37218 if (!flag_openmp) /* flag_openmp_simd */
37219 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37220 cclauses, if_p);
37221 sb = begin_omp_structured_block ();
37222 save = cp_parser_begin_omp_structured_block (parser);
37223 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37224 cclauses, if_p);
37225 cp_parser_end_omp_structured_block (parser, save);
37226 tree body = finish_omp_structured_block (sb);
37227 if (ret == NULL)
37228 return ret;
37229 ret = make_node (OMP_TASKLOOP);
37230 TREE_TYPE (ret) = void_type_node;
37231 OMP_FOR_BODY (ret) = body;
37232 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37233 SET_EXPR_LOCATION (ret, loc);
37234 add_stmt (ret);
37235 return ret;
37236 }
37237 }
37238 if (!flag_openmp) /* flag_openmp_simd */
37239 {
37240 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37241 return NULL_TREE;
37242 }
37243
37244 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37245 cclauses == NULL);
37246 if (cclauses)
37247 {
37248 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37249 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37250 }
37251
37252 sb = begin_omp_structured_block ();
37253 save = cp_parser_begin_omp_structured_block (parser);
37254
37255 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37256 if_p);
37257
37258 cp_parser_end_omp_structured_block (parser, save);
37259 add_stmt (finish_omp_structured_block (sb));
37260
37261 return ret;
37262 }
37263
37264
37265 /* OpenACC 2.0:
37266 # pragma acc routine oacc-routine-clause[optseq] new-line
37267 function-definition
37268
37269 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37270 */
37271
37272 #define OACC_ROUTINE_CLAUSE_MASK \
37273 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37277
37278
37279 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37280 component, which must resolve to a declared namespace-scope
37281 function. The clauses are either processed directly (for a named
37282 function), or defered until the immediatley following declaration
37283 is parsed. */
37284
37285 static void
37286 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37287 enum pragma_context context)
37288 {
37289 gcc_checking_assert (context == pragma_external);
37290 /* The checking for "another pragma following this one" in the "no optional
37291 '( name )'" case makes sure that we dont re-enter. */
37292 gcc_checking_assert (parser->oacc_routine == NULL);
37293
37294 cp_oacc_routine_data data;
37295 data.error_seen = false;
37296 data.fndecl_seen = false;
37297 data.tokens = vNULL;
37298 data.clauses = NULL_TREE;
37299 data.loc = pragma_tok->location;
37300 /* It is safe to take the address of a local variable; it will only be
37301 used while this scope is live. */
37302 parser->oacc_routine = &data;
37303
37304 /* Look for optional '( name )'. */
37305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37306 {
37307 cp_lexer_consume_token (parser->lexer); /* '(' */
37308
37309 /* We parse the name as an id-expression. If it resolves to
37310 anything other than a non-overloaded function at namespace
37311 scope, it's an error. */
37312 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37313 tree name = cp_parser_id_expression (parser,
37314 /*template_keyword_p=*/false,
37315 /*check_dependency_p=*/false,
37316 /*template_p=*/NULL,
37317 /*declarator_p=*/false,
37318 /*optional_p=*/false);
37319 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37320 if (name != error_mark_node && decl == error_mark_node)
37321 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37322
37323 if (decl == error_mark_node
37324 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37325 {
37326 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37327 parser->oacc_routine = NULL;
37328 return;
37329 }
37330
37331 data.clauses
37332 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37333 "#pragma acc routine",
37334 cp_lexer_peek_token (parser->lexer));
37335
37336 if (decl && is_overloaded_fn (decl)
37337 && (TREE_CODE (decl) != FUNCTION_DECL
37338 || DECL_FUNCTION_TEMPLATE_P (decl)))
37339 {
37340 error_at (name_loc,
37341 "%<#pragma acc routine%> names a set of overloads");
37342 parser->oacc_routine = NULL;
37343 return;
37344 }
37345
37346 /* Perhaps we should use the same rule as declarations in different
37347 namespaces? */
37348 if (!DECL_NAMESPACE_SCOPE_P (decl))
37349 {
37350 error_at (name_loc,
37351 "%qD does not refer to a namespace scope function", decl);
37352 parser->oacc_routine = NULL;
37353 return;
37354 }
37355
37356 if (TREE_CODE (decl) != FUNCTION_DECL)
37357 {
37358 error_at (name_loc, "%qD does not refer to a function", decl);
37359 parser->oacc_routine = NULL;
37360 return;
37361 }
37362
37363 cp_finalize_oacc_routine (parser, decl, false);
37364 parser->oacc_routine = NULL;
37365 }
37366 else /* No optional '( name )'. */
37367 {
37368 /* Store away all pragma tokens. */
37369 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37370 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37371 cp_lexer_consume_token (parser->lexer);
37372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37373 parser->oacc_routine->error_seen = true;
37374 cp_parser_require_pragma_eol (parser, pragma_tok);
37375 struct cp_token_cache *cp
37376 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37377 parser->oacc_routine->tokens.safe_push (cp);
37378
37379 /* Emit a helpful diagnostic if there's another pragma following this
37380 one. */
37381 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37382 {
37383 cp_ensure_no_oacc_routine (parser);
37384 data.tokens.release ();
37385 /* ..., and then just keep going. */
37386 return;
37387 }
37388
37389 /* We only have to consider the pragma_external case here. */
37390 cp_parser_declaration (parser);
37391 if (parser->oacc_routine
37392 && !parser->oacc_routine->fndecl_seen)
37393 cp_ensure_no_oacc_routine (parser);
37394 else
37395 parser->oacc_routine = NULL;
37396 data.tokens.release ();
37397 }
37398 }
37399
37400 /* Finalize #pragma acc routine clauses after direct declarator has
37401 been parsed. */
37402
37403 static tree
37404 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37405 {
37406 struct cp_token_cache *ce;
37407 cp_oacc_routine_data *data = parser->oacc_routine;
37408
37409 if (!data->error_seen && data->fndecl_seen)
37410 {
37411 error_at (data->loc,
37412 "%<#pragma acc routine%> not immediately followed by "
37413 "a single function declaration or definition");
37414 data->error_seen = true;
37415 }
37416 if (data->error_seen)
37417 return attrs;
37418
37419 gcc_checking_assert (data->tokens.length () == 1);
37420 ce = data->tokens[0];
37421
37422 cp_parser_push_lexer_for_tokens (parser, ce);
37423 parser->lexer->in_pragma = true;
37424 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37425
37426 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37427 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37428 parser->oacc_routine->clauses
37429 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37430 "#pragma acc routine", pragma_tok);
37431 cp_parser_pop_lexer (parser);
37432 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37433 fndecl_seen. */
37434
37435 return attrs;
37436 }
37437
37438 /* Apply any saved OpenACC routine clauses to a just-parsed
37439 declaration. */
37440
37441 static void
37442 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37443 {
37444 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37445 {
37446 /* Keep going if we're in error reporting mode. */
37447 if (parser->oacc_routine->error_seen
37448 || fndecl == error_mark_node)
37449 return;
37450
37451 if (parser->oacc_routine->fndecl_seen)
37452 {
37453 error_at (parser->oacc_routine->loc,
37454 "%<#pragma acc routine%> not immediately followed by"
37455 " a single function declaration or definition");
37456 parser->oacc_routine = NULL;
37457 return;
37458 }
37459 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37460 {
37461 cp_ensure_no_oacc_routine (parser);
37462 return;
37463 }
37464
37465 if (oacc_get_fn_attrib (fndecl))
37466 {
37467 error_at (parser->oacc_routine->loc,
37468 "%<#pragma acc routine%> already applied to %qD", fndecl);
37469 parser->oacc_routine = NULL;
37470 return;
37471 }
37472
37473 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37474 {
37475 error_at (parser->oacc_routine->loc,
37476 "%<#pragma acc routine%> must be applied before %s",
37477 TREE_USED (fndecl) ? "use" : "definition");
37478 parser->oacc_routine = NULL;
37479 return;
37480 }
37481
37482 /* Process the routine's dimension clauses. */
37483 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37484 oacc_replace_fn_attrib (fndecl, dims);
37485
37486 /* Add an "omp declare target" attribute. */
37487 DECL_ATTRIBUTES (fndecl)
37488 = tree_cons (get_identifier ("omp declare target"),
37489 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37490
37491 /* Don't unset parser->oacc_routine here: we may still need it to
37492 diagnose wrong usage. But, remember that we've used this "#pragma acc
37493 routine". */
37494 parser->oacc_routine->fndecl_seen = true;
37495 }
37496 }
37497
37498 /* Main entry point to OpenMP statement pragmas. */
37499
37500 static void
37501 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37502 {
37503 tree stmt;
37504 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37505 omp_clause_mask mask (0);
37506
37507 switch (cp_parser_pragma_kind (pragma_tok))
37508 {
37509 case PRAGMA_OACC_ATOMIC:
37510 cp_parser_omp_atomic (parser, pragma_tok);
37511 return;
37512 case PRAGMA_OACC_CACHE:
37513 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37514 break;
37515 case PRAGMA_OACC_DATA:
37516 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37517 break;
37518 case PRAGMA_OACC_ENTER_DATA:
37519 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37520 break;
37521 case PRAGMA_OACC_EXIT_DATA:
37522 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37523 break;
37524 case PRAGMA_OACC_HOST_DATA:
37525 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37526 break;
37527 case PRAGMA_OACC_KERNELS:
37528 case PRAGMA_OACC_PARALLEL:
37529 strcpy (p_name, "#pragma acc");
37530 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37531 if_p);
37532 break;
37533 case PRAGMA_OACC_LOOP:
37534 strcpy (p_name, "#pragma acc");
37535 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37536 if_p);
37537 break;
37538 case PRAGMA_OACC_UPDATE:
37539 stmt = cp_parser_oacc_update (parser, pragma_tok);
37540 break;
37541 case PRAGMA_OACC_WAIT:
37542 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37543 break;
37544 case PRAGMA_OMP_ATOMIC:
37545 cp_parser_omp_atomic (parser, pragma_tok);
37546 return;
37547 case PRAGMA_OMP_CRITICAL:
37548 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37549 break;
37550 case PRAGMA_OMP_DISTRIBUTE:
37551 strcpy (p_name, "#pragma omp");
37552 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37553 if_p);
37554 break;
37555 case PRAGMA_OMP_FOR:
37556 strcpy (p_name, "#pragma omp");
37557 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37558 if_p);
37559 break;
37560 case PRAGMA_OMP_MASTER:
37561 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37562 break;
37563 case PRAGMA_OMP_PARALLEL:
37564 strcpy (p_name, "#pragma omp");
37565 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37566 if_p);
37567 break;
37568 case PRAGMA_OMP_SECTIONS:
37569 strcpy (p_name, "#pragma omp");
37570 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37571 break;
37572 case PRAGMA_OMP_SIMD:
37573 strcpy (p_name, "#pragma omp");
37574 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37575 if_p);
37576 break;
37577 case PRAGMA_OMP_SINGLE:
37578 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37579 break;
37580 case PRAGMA_OMP_TASK:
37581 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37582 break;
37583 case PRAGMA_OMP_TASKGROUP:
37584 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37585 break;
37586 case PRAGMA_OMP_TASKLOOP:
37587 strcpy (p_name, "#pragma omp");
37588 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37589 if_p);
37590 break;
37591 case PRAGMA_OMP_TEAMS:
37592 strcpy (p_name, "#pragma omp");
37593 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37594 if_p);
37595 break;
37596 default:
37597 gcc_unreachable ();
37598 }
37599
37600 protected_set_expr_location (stmt, pragma_tok->location);
37601 }
37602 \f
37603 /* Transactional Memory parsing routines. */
37604
37605 /* Parse a transaction attribute.
37606
37607 txn-attribute:
37608 attribute
37609 [ [ identifier ] ]
37610
37611 We use this instead of cp_parser_attributes_opt for transactions to avoid
37612 the pedwarn in C++98 mode. */
37613
37614 static tree
37615 cp_parser_txn_attribute_opt (cp_parser *parser)
37616 {
37617 cp_token *token;
37618 tree attr_name, attr = NULL;
37619
37620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37621 return cp_parser_attributes_opt (parser);
37622
37623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37624 return NULL_TREE;
37625 cp_lexer_consume_token (parser->lexer);
37626 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37627 goto error1;
37628
37629 token = cp_lexer_peek_token (parser->lexer);
37630 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37631 {
37632 token = cp_lexer_consume_token (parser->lexer);
37633
37634 attr_name = (token->type == CPP_KEYWORD
37635 /* For keywords, use the canonical spelling,
37636 not the parsed identifier. */
37637 ? ridpointers[(int) token->keyword]
37638 : token->u.value);
37639 attr = build_tree_list (attr_name, NULL_TREE);
37640 }
37641 else
37642 cp_parser_error (parser, "expected identifier");
37643
37644 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37645 error1:
37646 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37647 return attr;
37648 }
37649
37650 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37651
37652 transaction-statement:
37653 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37654 compound-statement
37655 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37656 */
37657
37658 static tree
37659 cp_parser_transaction (cp_parser *parser, cp_token *token)
37660 {
37661 unsigned char old_in = parser->in_transaction;
37662 unsigned char this_in = 1, new_in;
37663 enum rid keyword = token->keyword;
37664 tree stmt, attrs, noex;
37665
37666 cp_lexer_consume_token (parser->lexer);
37667
37668 if (keyword == RID_TRANSACTION_RELAXED
37669 || keyword == RID_SYNCHRONIZED)
37670 this_in |= TM_STMT_ATTR_RELAXED;
37671 else
37672 {
37673 attrs = cp_parser_txn_attribute_opt (parser);
37674 if (attrs)
37675 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37676 }
37677
37678 /* Parse a noexcept specification. */
37679 if (keyword == RID_ATOMIC_NOEXCEPT)
37680 noex = boolean_true_node;
37681 else if (keyword == RID_ATOMIC_CANCEL)
37682 {
37683 /* cancel-and-throw is unimplemented. */
37684 sorry ("atomic_cancel");
37685 noex = NULL_TREE;
37686 }
37687 else
37688 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37689
37690 /* Keep track if we're in the lexical scope of an outer transaction. */
37691 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37692
37693 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37694
37695 parser->in_transaction = new_in;
37696 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37697 parser->in_transaction = old_in;
37698
37699 finish_transaction_stmt (stmt, NULL, this_in, noex);
37700
37701 return stmt;
37702 }
37703
37704 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37705
37706 transaction-expression:
37707 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37708 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37709 */
37710
37711 static tree
37712 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37713 {
37714 unsigned char old_in = parser->in_transaction;
37715 unsigned char this_in = 1;
37716 cp_token *token;
37717 tree expr, noex;
37718 bool noex_expr;
37719 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37720
37721 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37722 || keyword == RID_TRANSACTION_RELAXED);
37723
37724 if (!flag_tm)
37725 error_at (loc,
37726 keyword == RID_TRANSACTION_RELAXED
37727 ? G_("%<__transaction_relaxed%> without transactional memory "
37728 "support enabled")
37729 : G_("%<__transaction_atomic%> without transactional memory "
37730 "support enabled"));
37731
37732 token = cp_parser_require_keyword (parser, keyword,
37733 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37734 : RT_TRANSACTION_RELAXED));
37735 gcc_assert (token != NULL);
37736
37737 if (keyword == RID_TRANSACTION_RELAXED)
37738 this_in |= TM_STMT_ATTR_RELAXED;
37739
37740 /* Set this early. This might mean that we allow transaction_cancel in
37741 an expression that we find out later actually has to be a constexpr.
37742 However, we expect that cxx_constant_value will be able to deal with
37743 this; also, if the noexcept has no constexpr, then what we parse next
37744 really is a transaction's body. */
37745 parser->in_transaction = this_in;
37746
37747 /* Parse a noexcept specification. */
37748 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37749 true);
37750
37751 if (!noex || !noex_expr
37752 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37753 {
37754 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37755
37756 expr = cp_parser_expression (parser);
37757 expr = finish_parenthesized_expr (expr);
37758
37759 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37760 }
37761 else
37762 {
37763 /* The only expression that is available got parsed for the noexcept
37764 already. noexcept is true then. */
37765 expr = noex;
37766 noex = boolean_true_node;
37767 }
37768
37769 expr = build_transaction_expr (token->location, expr, this_in, noex);
37770 parser->in_transaction = old_in;
37771
37772 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37773 return error_mark_node;
37774
37775 return (flag_tm ? expr : error_mark_node);
37776 }
37777
37778 /* Parse a function-transaction-block.
37779
37780 function-transaction-block:
37781 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37782 function-body
37783 __transaction_atomic txn-attribute[opt] function-try-block
37784 __transaction_relaxed ctor-initializer[opt] function-body
37785 __transaction_relaxed function-try-block
37786 */
37787
37788 static bool
37789 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37790 {
37791 unsigned char old_in = parser->in_transaction;
37792 unsigned char new_in = 1;
37793 tree compound_stmt, stmt, attrs;
37794 bool ctor_initializer_p;
37795 cp_token *token;
37796
37797 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37798 || keyword == RID_TRANSACTION_RELAXED);
37799 token = cp_parser_require_keyword (parser, keyword,
37800 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37801 : RT_TRANSACTION_RELAXED));
37802 gcc_assert (token != NULL);
37803
37804 if (keyword == RID_TRANSACTION_RELAXED)
37805 new_in |= TM_STMT_ATTR_RELAXED;
37806 else
37807 {
37808 attrs = cp_parser_txn_attribute_opt (parser);
37809 if (attrs)
37810 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37811 }
37812
37813 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37814
37815 parser->in_transaction = new_in;
37816
37817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37818 ctor_initializer_p = cp_parser_function_try_block (parser);
37819 else
37820 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37821 (parser, /*in_function_try_block=*/false);
37822
37823 parser->in_transaction = old_in;
37824
37825 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37826
37827 return ctor_initializer_p;
37828 }
37829
37830 /* Parse a __transaction_cancel statement.
37831
37832 cancel-statement:
37833 __transaction_cancel txn-attribute[opt] ;
37834 __transaction_cancel txn-attribute[opt] throw-expression ;
37835
37836 ??? Cancel and throw is not yet implemented. */
37837
37838 static tree
37839 cp_parser_transaction_cancel (cp_parser *parser)
37840 {
37841 cp_token *token;
37842 bool is_outer = false;
37843 tree stmt, attrs;
37844
37845 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37846 RT_TRANSACTION_CANCEL);
37847 gcc_assert (token != NULL);
37848
37849 attrs = cp_parser_txn_attribute_opt (parser);
37850 if (attrs)
37851 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37852
37853 /* ??? Parse cancel-and-throw here. */
37854
37855 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37856
37857 if (!flag_tm)
37858 {
37859 error_at (token->location, "%<__transaction_cancel%> without "
37860 "transactional memory support enabled");
37861 return error_mark_node;
37862 }
37863 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37864 {
37865 error_at (token->location, "%<__transaction_cancel%> within a "
37866 "%<__transaction_relaxed%>");
37867 return error_mark_node;
37868 }
37869 else if (is_outer)
37870 {
37871 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37872 && !is_tm_may_cancel_outer (current_function_decl))
37873 {
37874 error_at (token->location, "outer %<__transaction_cancel%> not "
37875 "within outer %<__transaction_atomic%>");
37876 error_at (token->location,
37877 " or a %<transaction_may_cancel_outer%> function");
37878 return error_mark_node;
37879 }
37880 }
37881 else if (parser->in_transaction == 0)
37882 {
37883 error_at (token->location, "%<__transaction_cancel%> not within "
37884 "%<__transaction_atomic%>");
37885 return error_mark_node;
37886 }
37887
37888 stmt = build_tm_abort_call (token->location, is_outer);
37889 add_stmt (stmt);
37890
37891 return stmt;
37892 }
37893 \f
37894 /* The parser. */
37895
37896 static GTY (()) cp_parser *the_parser;
37897
37898 \f
37899 /* Special handling for the first token or line in the file. The first
37900 thing in the file might be #pragma GCC pch_preprocess, which loads a
37901 PCH file, which is a GC collection point. So we need to handle this
37902 first pragma without benefit of an existing lexer structure.
37903
37904 Always returns one token to the caller in *FIRST_TOKEN. This is
37905 either the true first token of the file, or the first token after
37906 the initial pragma. */
37907
37908 static void
37909 cp_parser_initial_pragma (cp_token *first_token)
37910 {
37911 tree name = NULL;
37912
37913 cp_lexer_get_preprocessor_token (NULL, first_token);
37914 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37915 return;
37916
37917 cp_lexer_get_preprocessor_token (NULL, first_token);
37918 if (first_token->type == CPP_STRING)
37919 {
37920 name = first_token->u.value;
37921
37922 cp_lexer_get_preprocessor_token (NULL, first_token);
37923 if (first_token->type != CPP_PRAGMA_EOL)
37924 error_at (first_token->location,
37925 "junk at end of %<#pragma GCC pch_preprocess%>");
37926 }
37927 else
37928 error_at (first_token->location, "expected string literal");
37929
37930 /* Skip to the end of the pragma. */
37931 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37932 cp_lexer_get_preprocessor_token (NULL, first_token);
37933
37934 /* Now actually load the PCH file. */
37935 if (name)
37936 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37937
37938 /* Read one more token to return to our caller. We have to do this
37939 after reading the PCH file in, since its pointers have to be
37940 live. */
37941 cp_lexer_get_preprocessor_token (NULL, first_token);
37942 }
37943
37944 /* Parses the grainsize pragma for the _Cilk_for statement.
37945 Syntax:
37946 #pragma cilk grainsize = <VALUE>. */
37947
37948 static void
37949 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37950 {
37951 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37952 {
37953 tree exp = cp_parser_binary_expression (parser, false, false,
37954 PREC_NOT_OPERATOR, NULL);
37955 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37956 if (!exp || exp == error_mark_node)
37957 {
37958 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37959 return;
37960 }
37961
37962 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37963 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37964 cp_parser_cilk_for (parser, exp, if_p);
37965 else
37966 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37967 "%<#pragma cilk grainsize%> is not followed by "
37968 "%<_Cilk_for%>");
37969 return;
37970 }
37971 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37972 }
37973
37974 /* Normal parsing of a pragma token. Here we can (and must) use the
37975 regular lexer. */
37976
37977 static bool
37978 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37979 {
37980 cp_token *pragma_tok;
37981 unsigned int id;
37982 tree stmt;
37983 bool ret;
37984
37985 pragma_tok = cp_lexer_consume_token (parser->lexer);
37986 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37987 parser->lexer->in_pragma = true;
37988
37989 id = cp_parser_pragma_kind (pragma_tok);
37990 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
37991 cp_ensure_no_omp_declare_simd (parser);
37992 switch (id)
37993 {
37994 case PRAGMA_GCC_PCH_PREPROCESS:
37995 error_at (pragma_tok->location,
37996 "%<#pragma GCC pch_preprocess%> must be first");
37997 break;
37998
37999 case PRAGMA_OMP_BARRIER:
38000 switch (context)
38001 {
38002 case pragma_compound:
38003 cp_parser_omp_barrier (parser, pragma_tok);
38004 return false;
38005 case pragma_stmt:
38006 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
38007 "used in compound statements");
38008 break;
38009 default:
38010 goto bad_stmt;
38011 }
38012 break;
38013
38014 case PRAGMA_OMP_FLUSH:
38015 switch (context)
38016 {
38017 case pragma_compound:
38018 cp_parser_omp_flush (parser, pragma_tok);
38019 return false;
38020 case pragma_stmt:
38021 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
38022 "used in compound statements");
38023 break;
38024 default:
38025 goto bad_stmt;
38026 }
38027 break;
38028
38029 case PRAGMA_OMP_TASKWAIT:
38030 switch (context)
38031 {
38032 case pragma_compound:
38033 cp_parser_omp_taskwait (parser, pragma_tok);
38034 return false;
38035 case pragma_stmt:
38036 error_at (pragma_tok->location,
38037 "%<#pragma omp taskwait%> may only be "
38038 "used in compound statements");
38039 break;
38040 default:
38041 goto bad_stmt;
38042 }
38043 break;
38044
38045 case PRAGMA_OMP_TASKYIELD:
38046 switch (context)
38047 {
38048 case pragma_compound:
38049 cp_parser_omp_taskyield (parser, pragma_tok);
38050 return false;
38051 case pragma_stmt:
38052 error_at (pragma_tok->location,
38053 "%<#pragma omp taskyield%> may only be "
38054 "used in compound statements");
38055 break;
38056 default:
38057 goto bad_stmt;
38058 }
38059 break;
38060
38061 case PRAGMA_OMP_CANCEL:
38062 switch (context)
38063 {
38064 case pragma_compound:
38065 cp_parser_omp_cancel (parser, pragma_tok);
38066 return false;
38067 case pragma_stmt:
38068 error_at (pragma_tok->location,
38069 "%<#pragma omp cancel%> may only be "
38070 "used in compound statements");
38071 break;
38072 default:
38073 goto bad_stmt;
38074 }
38075 break;
38076
38077 case PRAGMA_OMP_CANCELLATION_POINT:
38078 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38079 return false;
38080
38081 case PRAGMA_OMP_THREADPRIVATE:
38082 cp_parser_omp_threadprivate (parser, pragma_tok);
38083 return false;
38084
38085 case PRAGMA_OMP_DECLARE:
38086 cp_parser_omp_declare (parser, pragma_tok, context);
38087 return false;
38088
38089 case PRAGMA_OACC_DECLARE:
38090 cp_parser_oacc_declare (parser, pragma_tok);
38091 return false;
38092
38093 case PRAGMA_OACC_ENTER_DATA:
38094 if (context == pragma_stmt)
38095 {
38096 cp_parser_error (parser, "%<#pragma acc enter data%> may only be "
38097 "used in compound statements");
38098 break;
38099 }
38100 else if (context != pragma_compound)
38101 goto bad_stmt;
38102 cp_parser_omp_construct (parser, pragma_tok, if_p);
38103 return true;
38104
38105 case PRAGMA_OACC_EXIT_DATA:
38106 if (context == pragma_stmt)
38107 {
38108 cp_parser_error (parser, "%<#pragma acc exit data%> may only be "
38109 "used in compound statements");
38110 break;
38111 }
38112 else if (context != pragma_compound)
38113 goto bad_stmt;
38114 cp_parser_omp_construct (parser, pragma_tok, if_p);
38115 return true;
38116
38117 case PRAGMA_OACC_ROUTINE:
38118 if (context != pragma_external)
38119 {
38120 error_at (pragma_tok->location,
38121 "%<#pragma acc routine%> must be at file scope");
38122 break;
38123 }
38124 cp_parser_oacc_routine (parser, pragma_tok, context);
38125 return false;
38126
38127 case PRAGMA_OACC_UPDATE:
38128 if (context == pragma_stmt)
38129 {
38130 cp_parser_error (parser, "%<#pragma acc update%> may only be "
38131 "used in compound statements");
38132 break;
38133 }
38134 else if (context != pragma_compound)
38135 goto bad_stmt;
38136 cp_parser_omp_construct (parser, pragma_tok, if_p);
38137 return true;
38138
38139 case PRAGMA_OACC_WAIT:
38140 if (context == pragma_stmt)
38141 {
38142 cp_parser_error (parser, "%<#pragma acc wait%> may only be "
38143 "used in compound statements");
38144 break;
38145 }
38146 else if (context != pragma_compound)
38147 goto bad_stmt;
38148 cp_parser_omp_construct (parser, pragma_tok, if_p);
38149 return true;
38150
38151 case PRAGMA_OACC_ATOMIC:
38152 case PRAGMA_OACC_CACHE:
38153 case PRAGMA_OACC_DATA:
38154 case PRAGMA_OACC_HOST_DATA:
38155 case PRAGMA_OACC_KERNELS:
38156 case PRAGMA_OACC_PARALLEL:
38157 case PRAGMA_OACC_LOOP:
38158 case PRAGMA_OMP_ATOMIC:
38159 case PRAGMA_OMP_CRITICAL:
38160 case PRAGMA_OMP_DISTRIBUTE:
38161 case PRAGMA_OMP_FOR:
38162 case PRAGMA_OMP_MASTER:
38163 case PRAGMA_OMP_PARALLEL:
38164 case PRAGMA_OMP_SECTIONS:
38165 case PRAGMA_OMP_SIMD:
38166 case PRAGMA_OMP_SINGLE:
38167 case PRAGMA_OMP_TASK:
38168 case PRAGMA_OMP_TASKGROUP:
38169 case PRAGMA_OMP_TASKLOOP:
38170 case PRAGMA_OMP_TEAMS:
38171 if (context != pragma_stmt && context != pragma_compound)
38172 goto bad_stmt;
38173 stmt = push_omp_privatization_clauses (false);
38174 cp_parser_omp_construct (parser, pragma_tok, if_p);
38175 pop_omp_privatization_clauses (stmt);
38176 return true;
38177
38178 case PRAGMA_OMP_ORDERED:
38179 stmt = push_omp_privatization_clauses (false);
38180 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38181 pop_omp_privatization_clauses (stmt);
38182 return ret;
38183
38184 case PRAGMA_OMP_TARGET:
38185 stmt = push_omp_privatization_clauses (false);
38186 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38187 pop_omp_privatization_clauses (stmt);
38188 return ret;
38189
38190 case PRAGMA_OMP_END_DECLARE_TARGET:
38191 cp_parser_omp_end_declare_target (parser, pragma_tok);
38192 return false;
38193
38194 case PRAGMA_OMP_SECTION:
38195 error_at (pragma_tok->location,
38196 "%<#pragma omp section%> may only be used in "
38197 "%<#pragma omp sections%> construct");
38198 break;
38199
38200 case PRAGMA_IVDEP:
38201 {
38202 if (context == pragma_external)
38203 {
38204 error_at (pragma_tok->location,
38205 "%<#pragma GCC ivdep%> must be inside a function");
38206 break;
38207 }
38208 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38209 cp_token *tok;
38210 tok = cp_lexer_peek_token (the_parser->lexer);
38211 if (tok->type != CPP_KEYWORD
38212 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38213 && tok->keyword != RID_DO))
38214 {
38215 cp_parser_error (parser, "for, while or do statement expected");
38216 return false;
38217 }
38218 cp_parser_iteration_statement (parser, if_p, true);
38219 return true;
38220 }
38221
38222 case PRAGMA_CILK_SIMD:
38223 if (context == pragma_external)
38224 {
38225 error_at (pragma_tok->location,
38226 "%<#pragma simd%> must be inside a function");
38227 break;
38228 }
38229 stmt = push_omp_privatization_clauses (false);
38230 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38231 pop_omp_privatization_clauses (stmt);
38232 return true;
38233
38234 case PRAGMA_CILK_GRAINSIZE:
38235 if (context == pragma_external)
38236 {
38237 error_at (pragma_tok->location,
38238 "%<#pragma cilk grainsize%> must be inside a function");
38239 break;
38240 }
38241
38242 /* Ignore the pragma if Cilk Plus is not enabled. */
38243 if (flag_cilkplus)
38244 {
38245 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38246 return true;
38247 }
38248 else
38249 {
38250 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38251 "%<#pragma cilk grainsize%>");
38252 break;
38253 }
38254
38255 default:
38256 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38257 c_invoke_pragma_handler (id);
38258 break;
38259
38260 bad_stmt:
38261 cp_parser_error (parser, "expected declaration specifiers");
38262 break;
38263 }
38264
38265 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38266 return false;
38267 }
38268
38269 /* The interface the pragma parsers have to the lexer. */
38270
38271 enum cpp_ttype
38272 pragma_lex (tree *value, location_t *loc)
38273 {
38274 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38275 enum cpp_ttype ret = tok->type;
38276
38277 *value = tok->u.value;
38278 if (loc)
38279 *loc = tok->location;
38280
38281 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38282 ret = CPP_EOF;
38283 else if (ret == CPP_STRING)
38284 *value = cp_parser_string_literal (the_parser, false, false);
38285 else
38286 {
38287 if (ret == CPP_KEYWORD)
38288 ret = CPP_NAME;
38289 cp_lexer_consume_token (the_parser->lexer);
38290 }
38291
38292 return ret;
38293 }
38294
38295 \f
38296 /* External interface. */
38297
38298 /* Parse one entire translation unit. */
38299
38300 void
38301 c_parse_file (void)
38302 {
38303 static bool already_called = false;
38304
38305 if (already_called)
38306 fatal_error (input_location,
38307 "inter-module optimizations not implemented for C++");
38308 already_called = true;
38309
38310 the_parser = cp_parser_new ();
38311 push_deferring_access_checks (flag_access_control
38312 ? dk_no_deferred : dk_no_check);
38313 cp_parser_translation_unit (the_parser);
38314 the_parser = NULL;
38315 }
38316
38317 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38318 vectorlength clause:
38319 Syntax:
38320 vectorlength ( constant-expression ) */
38321
38322 static tree
38323 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38324 bool is_simd_fn)
38325 {
38326 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38327 tree expr;
38328 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38329 safelen clause. Thus, vectorlength is represented as OMP 4.0
38330 safelen. For SIMD-enabled function it is represented by OMP 4.0
38331 simdlen. */
38332 if (!is_simd_fn)
38333 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38334 loc);
38335 else
38336 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38337 loc);
38338
38339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38340 return error_mark_node;
38341
38342 expr = cp_parser_constant_expression (parser);
38343 expr = maybe_constant_value (expr);
38344
38345 /* If expr == error_mark_node, then don't emit any errors nor
38346 create a clause. if any of the above functions returns
38347 error mark node then they would have emitted an error message. */
38348 if (expr == error_mark_node)
38349 ;
38350 else if (!TREE_TYPE (expr)
38351 || !TREE_CONSTANT (expr)
38352 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38353 error_at (loc, "vectorlength must be an integer constant");
38354 else if (TREE_CONSTANT (expr)
38355 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38356 error_at (loc, "vectorlength must be a power of 2");
38357 else
38358 {
38359 tree c;
38360 if (!is_simd_fn)
38361 {
38362 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38363 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38364 OMP_CLAUSE_CHAIN (c) = clauses;
38365 clauses = c;
38366 }
38367 else
38368 {
38369 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38370 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38371 OMP_CLAUSE_CHAIN (c) = clauses;
38372 clauses = c;
38373 }
38374 }
38375
38376 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38377 return error_mark_node;
38378 return clauses;
38379 }
38380
38381 /* Handles the Cilk Plus #pragma simd linear clause.
38382 Syntax:
38383 linear ( simd-linear-variable-list )
38384
38385 simd-linear-variable-list:
38386 simd-linear-variable
38387 simd-linear-variable-list , simd-linear-variable
38388
38389 simd-linear-variable:
38390 id-expression
38391 id-expression : simd-linear-step
38392
38393 simd-linear-step:
38394 conditional-expression */
38395
38396 static tree
38397 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38398 {
38399 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38400
38401 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38402 return clauses;
38403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38404 {
38405 cp_parser_error (parser, "expected identifier");
38406 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38407 return error_mark_node;
38408 }
38409
38410 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38411 parser->colon_corrects_to_scope_p = false;
38412 while (1)
38413 {
38414 cp_token *token = cp_lexer_peek_token (parser->lexer);
38415 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38416 {
38417 cp_parser_error (parser, "expected variable-name");
38418 clauses = error_mark_node;
38419 break;
38420 }
38421
38422 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38423 false, false);
38424 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38425 token->location);
38426 if (decl == error_mark_node)
38427 {
38428 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38429 token->location);
38430 clauses = error_mark_node;
38431 }
38432 else
38433 {
38434 tree e = NULL_TREE;
38435 tree step_size = integer_one_node;
38436
38437 /* If present, parse the linear step. Otherwise, assume the default
38438 value of 1. */
38439 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38440 {
38441 cp_lexer_consume_token (parser->lexer);
38442
38443 e = cp_parser_assignment_expression (parser);
38444 e = maybe_constant_value (e);
38445
38446 if (e == error_mark_node)
38447 {
38448 /* If an error has occurred, then the whole pragma is
38449 considered ill-formed. Thus, no reason to keep
38450 parsing. */
38451 clauses = error_mark_node;
38452 break;
38453 }
38454 else if (type_dependent_expression_p (e)
38455 || value_dependent_expression_p (e)
38456 || (TREE_TYPE (e)
38457 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38458 && (TREE_CONSTANT (e)
38459 || DECL_P (e))))
38460 step_size = e;
38461 else
38462 cp_parser_error (parser,
38463 "step size must be an integer constant "
38464 "expression or an integer variable");
38465 }
38466
38467 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38468 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38469 OMP_CLAUSE_DECL (l) = decl;
38470 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38471 OMP_CLAUSE_CHAIN (l) = clauses;
38472 clauses = l;
38473 }
38474 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38475 cp_lexer_consume_token (parser->lexer);
38476 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38477 break;
38478 else
38479 {
38480 error_at (cp_lexer_peek_token (parser->lexer)->location,
38481 "expected %<,%> or %<)%> after %qE", decl);
38482 clauses = error_mark_node;
38483 break;
38484 }
38485 }
38486 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38487 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38488 return clauses;
38489 }
38490
38491 /* Returns the name of the next clause. If the clause is not
38492 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38493 token is not consumed. Otherwise, the appropriate enum from the
38494 pragma_simd_clause is returned and the token is consumed. */
38495
38496 static pragma_omp_clause
38497 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38498 {
38499 pragma_omp_clause clause_type;
38500 cp_token *token = cp_lexer_peek_token (parser->lexer);
38501
38502 if (token->keyword == RID_PRIVATE)
38503 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38504 else if (!token->u.value || token->type != CPP_NAME)
38505 return PRAGMA_CILK_CLAUSE_NONE;
38506 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38507 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38508 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38509 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38510 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38511 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38512 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38513 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38514 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38515 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38516 else
38517 return PRAGMA_CILK_CLAUSE_NONE;
38518
38519 cp_lexer_consume_token (parser->lexer);
38520 return clause_type;
38521 }
38522
38523 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38524
38525 static tree
38526 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38527 {
38528 tree clauses = NULL_TREE;
38529
38530 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38531 && clauses != error_mark_node)
38532 {
38533 pragma_omp_clause c_kind;
38534 c_kind = cp_parser_cilk_simd_clause_name (parser);
38535 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38536 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38537 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38538 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38539 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38540 /* Use the OpenMP 4.0 equivalent function. */
38541 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38542 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38543 /* Use the OpenMP 4.0 equivalent function. */
38544 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38545 clauses);
38546 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38547 /* Use the OMP 4.0 equivalent function. */
38548 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38549 clauses);
38550 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38551 /* Use the OMP 4.0 equivalent function. */
38552 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38553 else
38554 {
38555 clauses = error_mark_node;
38556 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38557 break;
38558 }
38559 }
38560
38561 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38562
38563 if (clauses == error_mark_node)
38564 return error_mark_node;
38565 else
38566 return finish_omp_clauses (clauses, C_ORT_CILK);
38567 }
38568
38569 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38570
38571 static void
38572 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38573 {
38574 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38575
38576 if (clauses == error_mark_node)
38577 return;
38578
38579 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38580 {
38581 error_at (cp_lexer_peek_token (parser->lexer)->location,
38582 "for statement expected");
38583 return;
38584 }
38585
38586 tree sb = begin_omp_structured_block ();
38587 int save = cp_parser_begin_omp_structured_block (parser);
38588 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38589 if (ret)
38590 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38591 cp_parser_end_omp_structured_block (parser, save);
38592 add_stmt (finish_omp_structured_block (sb));
38593 }
38594
38595 /* Main entry-point for parsing Cilk Plus _Cilk_for
38596 loops. The return value is error_mark_node
38597 when errors happen and CILK_FOR tree on success. */
38598
38599 static tree
38600 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38601 {
38602 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38603 gcc_unreachable ();
38604
38605 tree sb = begin_omp_structured_block ();
38606 int save = cp_parser_begin_omp_structured_block (parser);
38607
38608 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38609 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38610 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38611 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38612
38613 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38614 if (ret)
38615 cpp_validate_cilk_plus_loop (ret);
38616 else
38617 ret = error_mark_node;
38618
38619 cp_parser_end_omp_structured_block (parser, save);
38620 add_stmt (finish_omp_structured_block (sb));
38621 return ret;
38622 }
38623
38624 /* Create an identifier for a generic parameter type (a synthesized
38625 template parameter implied by `auto' or a concept identifier). */
38626
38627 static GTY(()) int generic_parm_count;
38628 static tree
38629 make_generic_type_name ()
38630 {
38631 char buf[32];
38632 sprintf (buf, "auto:%d", ++generic_parm_count);
38633 return get_identifier (buf);
38634 }
38635
38636 /* Predicate that behaves as is_auto_or_concept but matches the parent
38637 node of the generic type rather than the generic type itself. This
38638 allows for type transformation in add_implicit_template_parms. */
38639
38640 static inline bool
38641 tree_type_is_auto_or_concept (const_tree t)
38642 {
38643 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38644 }
38645
38646 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38647 (creating a new template parameter list if necessary). Returns the newly
38648 created template type parm. */
38649
38650 static tree
38651 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38652 {
38653 gcc_assert (current_binding_level->kind == sk_function_parms);
38654
38655 /* Before committing to modifying any scope, if we're in an
38656 implicit template scope, and we're trying to synthesize a
38657 constrained parameter, try to find a previous parameter with
38658 the same name. This is the same-type rule for abbreviated
38659 function templates.
38660
38661 NOTE: We can generate implicit parameters when tentatively
38662 parsing a nested name specifier, only to reject that parse
38663 later. However, matching the same template-id as part of a
38664 direct-declarator should generate an identical template
38665 parameter, so this rule will merge them. */
38666 if (parser->implicit_template_scope && constr)
38667 {
38668 tree t = parser->implicit_template_parms;
38669 while (t)
38670 {
38671 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38672 {
38673 tree d = TREE_VALUE (t);
38674 if (TREE_CODE (d) == PARM_DECL)
38675 /* Return the TEMPLATE_PARM_INDEX. */
38676 d = DECL_INITIAL (d);
38677 return d;
38678 }
38679 t = TREE_CHAIN (t);
38680 }
38681 }
38682
38683 /* We are either continuing a function template that already contains implicit
38684 template parameters, creating a new fully-implicit function template, or
38685 extending an existing explicit function template with implicit template
38686 parameters. */
38687
38688 cp_binding_level *const entry_scope = current_binding_level;
38689
38690 bool become_template = false;
38691 cp_binding_level *parent_scope = 0;
38692
38693 if (parser->implicit_template_scope)
38694 {
38695 gcc_assert (parser->implicit_template_parms);
38696
38697 current_binding_level = parser->implicit_template_scope;
38698 }
38699 else
38700 {
38701 /* Roll back to the existing template parameter scope (in the case of
38702 extending an explicit function template) or introduce a new template
38703 parameter scope ahead of the function parameter scope (or class scope
38704 in the case of out-of-line member definitions). The function scope is
38705 added back after template parameter synthesis below. */
38706
38707 cp_binding_level *scope = entry_scope;
38708
38709 while (scope->kind == sk_function_parms)
38710 {
38711 parent_scope = scope;
38712 scope = scope->level_chain;
38713 }
38714 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38715 {
38716 /* If not defining a class, then any class scope is a scope level in
38717 an out-of-line member definition. In this case simply wind back
38718 beyond the first such scope to inject the template parameter list.
38719 Otherwise wind back to the class being defined. The latter can
38720 occur in class member friend declarations such as:
38721
38722 class A {
38723 void foo (auto);
38724 };
38725 class B {
38726 friend void A::foo (auto);
38727 };
38728
38729 The template parameter list synthesized for the friend declaration
38730 must be injected in the scope of 'B'. This can also occur in
38731 erroneous cases such as:
38732
38733 struct A {
38734 struct B {
38735 void foo (auto);
38736 };
38737 void B::foo (auto) {}
38738 };
38739
38740 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38741 but, nevertheless, the template parameter list synthesized for the
38742 declarator should be injected into the scope of 'A' as if the
38743 ill-formed template was specified explicitly. */
38744
38745 while (scope->kind == sk_class && !scope->defining_class_p)
38746 {
38747 parent_scope = scope;
38748 scope = scope->level_chain;
38749 }
38750 }
38751
38752 current_binding_level = scope;
38753
38754 if (scope->kind != sk_template_parms
38755 || !function_being_declared_is_template_p (parser))
38756 {
38757 /* Introduce a new template parameter list for implicit template
38758 parameters. */
38759
38760 become_template = true;
38761
38762 parser->implicit_template_scope
38763 = begin_scope (sk_template_parms, NULL);
38764
38765 ++processing_template_decl;
38766
38767 parser->fully_implicit_function_template_p = true;
38768 ++parser->num_template_parameter_lists;
38769 }
38770 else
38771 {
38772 /* Synthesize implicit template parameters at the end of the explicit
38773 template parameter list. */
38774
38775 gcc_assert (current_template_parms);
38776
38777 parser->implicit_template_scope = scope;
38778
38779 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38780 parser->implicit_template_parms
38781 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38782 }
38783 }
38784
38785 /* Synthesize a new template parameter and track the current template
38786 parameter chain with implicit_template_parms. */
38787
38788 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38789 tree synth_id = make_generic_type_name ();
38790 tree synth_tmpl_parm;
38791 bool non_type = false;
38792
38793 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38794 synth_tmpl_parm
38795 = finish_template_type_parm (class_type_node, synth_id);
38796 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38797 synth_tmpl_parm
38798 = finish_constrained_template_template_parm (proto, synth_id);
38799 else
38800 {
38801 synth_tmpl_parm = copy_decl (proto);
38802 DECL_NAME (synth_tmpl_parm) = synth_id;
38803 non_type = true;
38804 }
38805
38806 // Attach the constraint to the parm before processing.
38807 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38808 TREE_TYPE (node) = constr;
38809 tree new_parm
38810 = process_template_parm (parser->implicit_template_parms,
38811 input_location,
38812 node,
38813 /*non_type=*/non_type,
38814 /*param_pack=*/false);
38815
38816 // Chain the new parameter to the list of implicit parameters.
38817 if (parser->implicit_template_parms)
38818 parser->implicit_template_parms
38819 = TREE_CHAIN (parser->implicit_template_parms);
38820 else
38821 parser->implicit_template_parms = new_parm;
38822
38823 tree new_decl = getdecls ();
38824 if (non_type)
38825 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38826 new_decl = DECL_INITIAL (new_decl);
38827
38828 /* If creating a fully implicit function template, start the new implicit
38829 template parameter list with this synthesized type, otherwise grow the
38830 current template parameter list. */
38831
38832 if (become_template)
38833 {
38834 parent_scope->level_chain = current_binding_level;
38835
38836 tree new_parms = make_tree_vec (1);
38837 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38838 current_template_parms = tree_cons (size_int (processing_template_decl),
38839 new_parms, current_template_parms);
38840 }
38841 else
38842 {
38843 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38844 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38845 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38846 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38847 }
38848
38849 // If the new parameter was constrained, we need to add that to the
38850 // constraints in the template parameter list.
38851 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38852 {
38853 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38854 reqs = conjoin_constraints (reqs, req);
38855 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38856 }
38857
38858 current_binding_level = entry_scope;
38859
38860 return new_decl;
38861 }
38862
38863 /* Finish the declaration of a fully implicit function template. Such a
38864 template has no explicit template parameter list so has not been through the
38865 normal template head and tail processing. synthesize_implicit_template_parm
38866 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38867 provided if the declaration is a class member such that its template
38868 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38869 form is returned. Otherwise NULL_TREE is returned. */
38870
38871 static tree
38872 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38873 {
38874 gcc_assert (parser->fully_implicit_function_template_p);
38875
38876 if (member_decl_opt && member_decl_opt != error_mark_node
38877 && DECL_VIRTUAL_P (member_decl_opt))
38878 {
38879 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38880 "implicit templates may not be %<virtual%>");
38881 DECL_VIRTUAL_P (member_decl_opt) = false;
38882 }
38883
38884 if (member_decl_opt)
38885 member_decl_opt = finish_member_template_decl (member_decl_opt);
38886 end_template_decl ();
38887
38888 parser->fully_implicit_function_template_p = false;
38889 --parser->num_template_parameter_lists;
38890
38891 return member_decl_opt;
38892 }
38893
38894 #include "gt-cp-parser.h"